2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file social/gnunet-service-social.c
23 * @brief Social service
24 * @author Gabor X Toth
31 #include "gnunet_util_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_identity_service.h"
35 #include "gnunet_namestore_service.h"
36 #include "gnunet_gns_service.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_psyc_service.h"
39 #include "gnunet_psyc_util_lib.h"
40 #include "gnunet_social_service.h"
45 * Handle to our current configuration.
47 static const struct GNUNET_CONFIGURATION_Handle *cfg;
52 static struct GNUNET_SERVICE_Handle *service;
54 /* Handles to other services */
55 static struct GNUNET_IDENTITY_Handle *id;
56 static struct GNUNET_GNS_Handle *gns;
57 static struct GNUNET_NAMESTORE_Handle *namestore;
58 static struct GNUNET_STATISTICS_Handle *stats;
63 static struct GNUNET_PeerIdentity this_peer;
66 * All connected hosts.
67 * H(place_pub_key) -> struct Host
69 static struct GNUNET_CONTAINER_MultiHashMap *hosts;
72 * All connected guests.
73 * H(place_pub_key) -> struct Guest
75 static struct GNUNET_CONTAINER_MultiHashMap *guests;
78 * Connected guests per place.
79 * H(place_pub_key) -> ego_pub_key -> struct Guest
81 static struct GNUNET_CONTAINER_MultiHashMap *place_guests;
84 * Places entered as host or guest.
85 * H(place_pub_key) -> struct HostEnterRequest OR struct GuestEnterRequest
87 static struct GNUNET_CONTAINER_MultiHashMap *places;
90 * Places entered per application.
91 * H(app_id) -> H(place_pub_key) -> NULL
93 static struct GNUNET_CONTAINER_MultiHashMap *apps_places;
96 * Application subscriptions per place.
97 * H(place_pub_key) -> H(app_id)
99 static struct GNUNET_CONTAINER_MultiHashMap *places_apps;
102 * Connected applications.
103 * H(app_id) -> struct Application
105 static struct GNUNET_CONTAINER_MultiHashMap *apps;
109 * H(ego_pub_key) -> struct Ego
111 static struct GNUNET_CONTAINER_MultiHashMap *egos;
114 * Directory for storing social data.
115 * Default: $GNUNET_DATA_HOME/social
117 static char *dir_social;
120 * Directory for storing place data.
123 static char *dir_places;
126 * Directory for storing app data.
129 static char *dir_apps;
133 * Message fragment transmission queue.
135 struct FragmentTransmitQueue
137 struct FragmentTransmitQueue *prev;
138 struct FragmentTransmitQueue *next;
140 struct GNUNET_SERVICE_Client *client;
143 * Pointer to the next message part inside the data after this struct.
145 struct GNUNET_MessageHeader *next_part;
153 * @see enum GNUNET_PSYC_MessageState
157 /* Followed by one or more message parts. */
162 * Message transmission queue.
164 struct MessageTransmitQueue
166 struct MessageTransmitQueue *prev;
167 struct MessageTransmitQueue *next;
169 struct FragmentTransmitQueue *frags_head;
170 struct FragmentTransmitQueue *frags_tail;
172 struct GNUNET_SERVICE_Client *client;
176 * List of connected clients.
178 struct ClientListItem
180 struct ClientListItem *prev;
181 struct ClientListItem *next;
183 struct GNUNET_SERVICE_Client *client;
188 * Common part of the client context for both a host and guest.
192 struct ClientListItem *clients_head;
193 struct ClientListItem *clients_tail;
195 struct MessageTransmitQueue *tmit_msgs_head;
196 struct MessageTransmitQueue *tmit_msgs_tail;
198 struct GNUNET_PSYC_Channel *channel;
201 * Private key of home in case of a host.
203 struct GNUNET_CRYPTO_EddsaPublicKey key;
206 * Public key of place.
208 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
211 * Hash of @a pub_key.
213 struct GNUNET_HashCode pub_key_hash;
216 * Private key of ego.
218 struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
223 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
226 * Hash of @a ego_pub_key.
228 struct GNUNET_HashCode ego_pub_hash;
231 * Slicer for processing incoming messages.
233 struct GNUNET_PSYC_Slicer *slicer;
236 * Last message ID received for the place.
237 * 0 if there is no such message.
239 uint64_t max_message_id;
242 * Offset where the file is currently being written.
244 uint64_t file_offset;
247 * Whether or not to save the file (#GNUNET_YES or #GNUNET_NO)
252 * Is this place ready to receive messages from client?
253 * #GNUNET_YES or #GNUNET_NO
258 * Is the client disconnected?
259 * #GNUNET_YES or #GNUNET_NO
261 uint8_t is_disconnected;
264 * Is this a host (#GNUNET_YES), or guest (#GNUNET_NO)?
276 * Client context for a host.
281 * Place struct common for Host and Guest
286 * Handle for the multicast origin.
288 struct GNUNET_PSYC_Master *master;
291 * Transmit handle for multicast.
293 struct GNUNET_PSYC_MasterTransmitHandle *tmit_handle;
296 * Incoming join requests.
297 * guest_key -> struct GNUNET_PSYC_JoinHandle *
299 struct GNUNET_CONTAINER_MultiHashMap *join_reqs;
302 * Messages being relayed.
304 struct GNUNET_CONTAINER_MultiHashMap *relay_msgs;
307 * @see enum GNUNET_PSYC_Policy
309 enum GNUNET_PSYC_Policy policy;
314 * Client context for a guest.
319 * Place struct common for Host and Guest.
324 * Handle for the PSYC slave.
326 struct GNUNET_PSYC_Slave *slave;
329 * Transmit handle for multicast.
331 struct GNUNET_PSYC_SlaveTransmitHandle *tmit_handle;
334 * Peer identity of the origin.
336 struct GNUNET_PeerIdentity origin;
339 * Number of items in @a relays.
341 uint32_t relay_count;
344 * Relays that multicast can use to connect.
346 struct GNUNET_PeerIdentity *relays;
349 * Join request to be transmitted to the master on join.
351 struct GNUNET_MessageHeader *join_req;
354 * Join decision received from PSYC.
356 struct GNUNET_PSYC_JoinDecisionMessage *join_dcsn;
359 * Join flags for the PSYC service.
361 enum GNUNET_PSYC_SlaveJoinFlags join_flags;
366 * Context for a client.
373 struct GNUNET_SERVICE_Client *client;
376 * Place where the client entered.
381 * Message queue for the message currently being transmitted
384 struct MessageTransmitQueue *tmit_msg;
387 * ID for application clients.
395 struct ClientListItem *clients_head;
396 struct ClientListItem *clients_tail;
401 struct GNUNET_CRYPTO_EcdsaPrivateKey key;
406 struct OperationClosure
408 struct Client *client;
415 psyc_transmit_message (struct Place *plc);
419 * Clean up place data structures after a client disconnected.
421 * @param cls the `struct Place` to clean up
424 cleanup_place (void *cls);
427 static struct MessageTransmitQueue *
428 psyc_transmit_queue_message (struct Place *plc,
429 struct GNUNET_SERVICE_Client *client,
432 uint16_t first_ptype, uint16_t last_ptype,
433 struct MessageTransmitQueue *tmit_msg);
437 place_entry_cleanup (void *cls,
438 const struct GNUNET_HashCode *key,
441 struct Place *plc = value;
449 * Task run during shutdown.
454 shutdown_task (void *cls)
456 GNUNET_CONTAINER_multihashmap_iterate (hosts, place_entry_cleanup, NULL);
457 GNUNET_CONTAINER_multihashmap_iterate (guests, place_entry_cleanup, NULL);
461 GNUNET_IDENTITY_disconnect (id);
464 if (NULL != namestore)
466 GNUNET_NAMESTORE_disconnect (namestore);
471 GNUNET_GNS_disconnect (gns);
476 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
483 * Clean up host data structures after a client disconnected.
486 cleanup_host (struct Host *hst)
488 struct Place *plc = &hst->place;
490 if (NULL != hst->master)
491 GNUNET_PSYC_master_stop (hst->master, GNUNET_NO, NULL, NULL); // FIXME
492 GNUNET_CONTAINER_multihashmap_destroy (hst->join_reqs);
493 GNUNET_CONTAINER_multihashmap_destroy (hst->relay_msgs);
494 GNUNET_CONTAINER_multihashmap_remove (hosts, &plc->pub_key_hash, plc);
499 * Clean up guest data structures after a client disconnected.
502 cleanup_guest (struct Guest *gst)
504 struct Place *plc = &gst->place;
505 struct GNUNET_CONTAINER_MultiHashMap *
506 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests,
508 GNUNET_assert (NULL != plc_gst); // FIXME
509 GNUNET_CONTAINER_multihashmap_remove (plc_gst, &plc->ego_pub_hash, gst);
511 if (0 == GNUNET_CONTAINER_multihashmap_size (plc_gst))
513 GNUNET_CONTAINER_multihashmap_remove (place_guests, &plc->pub_key_hash,
515 GNUNET_CONTAINER_multihashmap_destroy (plc_gst);
517 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, gst);
519 if (NULL != gst->join_req)
520 GNUNET_free (gst->join_req);
521 if (NULL != gst->relays)
522 GNUNET_free (gst->relays);
523 if (NULL != gst->slave)
524 GNUNET_PSYC_slave_part (gst->slave, GNUNET_NO, NULL, NULL); // FIXME
525 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, plc);
530 * Clean up place data structures after a client disconnected.
532 * @param cls the `struct Place` to clean up
535 cleanup_place (void *cls)
537 struct Place *plc = cls;
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "%p Cleaning up place %s\n",
541 plc, GNUNET_h2s (&plc->pub_key_hash));
543 (GNUNET_YES == plc->is_host)
544 ? cleanup_host ((struct Host *) plc)
545 : cleanup_guest ((struct Guest *) plc);
547 GNUNET_PSYC_slicer_destroy (plc->slicer);
553 * Called whenever a client is disconnected.
554 * Frees our resources associated with that client.
557 * @param client identification of the client
558 * @param app_ctx must match @a client
561 client_notify_disconnect (void *cls,
562 struct GNUNET_SERVICE_Client *client,
565 struct Client *c = app_ctx;
566 struct Place *plc = c->place;
568 if (NULL != c->app_id)
569 GNUNET_free (c->app_id);
574 return; // application client, nothing to do
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "%p Client (%s) disconnected from place %s\n",
578 plc, (GNUNET_YES == plc->is_host) ? "host" : "guest",
579 GNUNET_h2s (&plc->pub_key_hash));
581 struct ClientListItem *cli = plc->clients_head;
584 if (cli->client == client)
586 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
596 * A new client connected.
599 * @param client client to add
600 * @param mq message queue for @a client
604 client_notify_connect (void *cls,
605 struct GNUNET_SERVICE_Client *client,
606 struct GNUNET_MQ_Handle *mq)
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
610 struct Client *c = GNUNET_malloc (sizeof (*c));
618 * Send message to a client.
621 client_send_msg (struct GNUNET_SERVICE_Client *client,
622 const struct GNUNET_MessageHeader *msg)
624 struct GNUNET_MQ_Envelope *
625 env = GNUNET_MQ_msg_copy (msg);
627 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
633 * Send message to all clients connected to a place.
636 place_send_msg (const struct Place *plc,
637 const struct GNUNET_MessageHeader *msg)
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "%p Sending message to clients of place.\n", plc);
642 struct ClientListItem *cli = plc->clients_head;
645 client_send_msg (cli->client, msg);
652 * Send a result code back to the client.
655 * Client that should receive the result code.
659 * Operation ID in network byte order.
661 * Data payload or NULL.
666 client_send_result (struct GNUNET_SERVICE_Client *client, uint64_t op_id,
667 int64_t result_code, const void *data, uint16_t data_size)
669 struct GNUNET_OperationResultMessage *res;
671 res = GNUNET_malloc (sizeof (*res) + data_size);
672 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
673 res->header.size = htons (sizeof (*res) + data_size);
674 res->result_code = GNUNET_htonll (result_code);
677 GNUNET_memcpy (&res[1], data, data_size);
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "%p Sending result to client for operation #%" PRIu64 ": "
681 "%" PRId64 " (size: %u)\n",
682 client, GNUNET_ntohll (op_id), result_code, data_size);
684 client_send_msg (client, &res->header);
690 client_send_host_enter_ack (struct GNUNET_SERVICE_Client *client,
691 struct Host *hst, uint32_t result)
693 struct Place *plc = &hst->place;
695 struct HostEnterAck hack;
696 hack.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK);
697 hack.header.size = htons (sizeof (hack));
698 hack.result_code = htonl (result);
699 hack.max_message_id = GNUNET_htonll (plc->max_message_id);
700 hack.place_pub_key = plc->pub_key;
703 client_send_msg (client, &hack.header);
705 place_send_msg (plc, &hack.header);
710 * Called after a PSYC master is started.
713 psyc_master_started (void *cls, int result, uint64_t max_message_id)
715 struct Host *hst = cls;
716 struct Place *plc = &hst->place;
717 plc->max_message_id = max_message_id;
718 plc->is_ready = GNUNET_YES;
720 client_send_host_enter_ack (NULL, hst, result);
725 * Called when a PSYC master receives a join request.
728 psyc_recv_join_request (void *cls,
729 const struct GNUNET_PSYC_JoinRequestMessage *req,
730 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
731 const struct GNUNET_PSYC_Message *join_msg,
732 struct GNUNET_PSYC_JoinHandle *jh)
734 struct Host *hst = cls;
735 struct GNUNET_HashCode slave_key_hash;
736 GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
737 GNUNET_CONTAINER_multihashmap_put (hst->join_reqs, &slave_key_hash, jh,
738 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
739 place_send_msg (&hst->place, &req->header);
744 * Called after a PSYC slave is connected.
747 psyc_slave_connected (void *cls, int result, uint64_t max_message_id)
749 struct Guest *gst = cls;
750 struct Place *plc = &gst->place;
751 plc->max_message_id = max_message_id;
752 plc->is_ready = GNUNET_YES;
754 struct GNUNET_PSYC_CountersResultMessage res;
755 res.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
756 res.header.size = htons (sizeof (res));
757 res.result_code = htonl (result);
758 res.max_message_id = GNUNET_htonll (plc->max_message_id);
760 place_send_msg (plc, &res.header);
765 * Called when a PSYC slave receives a join decision.
768 psyc_recv_join_dcsn (void *cls,
769 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
771 const struct GNUNET_PSYC_Message *join_msg)
773 struct Guest *gst = cls;
774 place_send_msg (&gst->place, &dcsn->header);
779 * Called when a PSYC master or slave receives a message.
782 psyc_recv_message (void *cls,
783 const struct GNUNET_PSYC_MessageHeader *msg)
785 struct Place *plc = cls;
787 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&msg->slave_pub_key);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "%p Received PSYC message of size %u from %s.\n",
790 plc, ntohs (msg->header.size), str);
793 GNUNET_PSYC_slicer_message (plc->slicer, msg);
795 place_send_msg (plc, &msg->header);
800 * Relay a message part received from a guest to the the place.
807 * Nym the message is received from.
810 host_relay_message_part (struct Host *hst,
811 const struct GNUNET_MessageHeader *pmsg,
812 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key)
814 /* separate queue per nym */
815 struct GNUNET_HashCode nym_pub_hash;
816 GNUNET_CRYPTO_hash (nym_pub_key, sizeof (*nym_pub_key), &nym_pub_hash);
818 struct MessageTransmitQueue *
819 tmit_msg = GNUNET_CONTAINER_multihashmap_get (hst->relay_msgs, &nym_pub_hash);
821 uint16_t ptype = ntohs (pmsg->type);
823 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
825 /* FIXME: last message was unfinished, cancel & remove from queue */
826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
827 "FIXME: last message was unfinished.\n");
830 tmit_msg = psyc_transmit_queue_message (&hst->place, NULL, ntohs (pmsg->size),
831 pmsg, ptype, ptype, tmit_msg);
835 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
836 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_put
837 (hst->relay_msgs, &nym_pub_hash, tmit_msg,
838 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
840 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
841 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
842 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove
843 (hst->relay_msgs, &nym_pub_hash, tmit_msg));
850 * Received a method to be relayed from a guest.
853 place_recv_relay_method (void *cls,
854 const struct GNUNET_PSYC_MessageHeader *msg,
855 const struct GNUNET_PSYC_MessageMethod *meth,
857 const char *method_name)
859 struct Place *plc = cls;
861 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
862 && GNUNET_YES == plc->is_host)
864 struct Host *hst = cls;
865 host_relay_message_part (hst, &meth->header, &msg->slave_pub_key);
871 * Received a modifier to be relayed from a guest.
874 place_recv_relay_modifier (void *cls,
875 const struct GNUNET_PSYC_MessageHeader *msg,
876 const struct GNUNET_MessageHeader *pmsg,
878 enum GNUNET_PSYC_Operator oper,
882 uint16_t full_value_size)
884 struct Place *plc = cls;
886 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
887 && GNUNET_YES == plc->is_host)
889 struct Host *hst = cls;
890 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
895 * Received a data fragment to be relayed from a guest.
898 place_recv_relay_data (void *cls,
899 const struct GNUNET_PSYC_MessageHeader *msg,
900 const struct GNUNET_MessageHeader *pmsg,
905 struct Place *plc = cls;
907 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
908 && GNUNET_YES == plc->is_host)
910 struct Host *hst = cls;
911 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
917 * Received end of message to be relayed from a guest.
920 place_recv_relay_eom (void *cls,
921 const struct GNUNET_PSYC_MessageHeader *msg,
922 const struct GNUNET_MessageHeader *pmsg,
924 uint8_t is_cancelled)
926 struct Place *plc = cls;
928 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
929 && GNUNET_YES == plc->is_host)
931 struct Host *hst = cls;
932 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
938 * Received a method to be saved to disk.
940 * Create a new file for writing the data part of the message into,
941 * if the file does not yet exist.
944 place_recv_save_method (void *cls,
945 const struct GNUNET_PSYC_MessageHeader *msg,
946 const struct GNUNET_PSYC_MessageMethod *meth,
948 const char *method_name)
950 struct Place *plc = cls;
951 plc->file_offset = 0;
952 plc->file_save = GNUNET_NO;
954 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
955 char *filename = NULL;
956 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
957 dir_social, DIR_SEPARATOR,
958 "files", DIR_SEPARATOR,
959 place_pub_str, DIR_SEPARATOR,
960 GNUNET_ntohll (msg->message_id));
961 GNUNET_free (place_pub_str);
963 /* save if does not already exist */
964 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
966 if (0 == GNUNET_DISK_fn_write (filename, NULL, 0,
967 GNUNET_DISK_PERM_USER_READ
968 | GNUNET_DISK_PERM_USER_WRITE))
970 plc->file_save = GNUNET_YES;
977 GNUNET_free (filename);
982 * Received a data fragment to be saved to disk.
984 * Append data fragment to the file.
987 place_recv_save_data (void *cls,
988 const struct GNUNET_PSYC_MessageHeader *msg,
989 const struct GNUNET_MessageHeader *pmsg,
994 struct Place *plc = cls;
995 if (GNUNET_YES != plc->file_save)
998 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
999 char *filename = NULL;
1000 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
1001 dir_social, DIR_SEPARATOR,
1002 "files", DIR_SEPARATOR,
1003 place_pub_str, DIR_SEPARATOR,
1004 GNUNET_ntohll (msg->message_id));
1005 GNUNET_free (place_pub_str);
1006 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
1008 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "create", filename);
1009 GNUNET_free (filename);
1013 struct GNUNET_DISK_FileHandle *
1014 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
1015 GNUNET_DISK_PERM_NONE);
1018 if (plc->file_offset != GNUNET_DISK_file_seek
1019 (fh, plc->file_offset, GNUNET_DISK_SEEK_SET)) {
1020 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "seek", filename);
1021 GNUNET_DISK_file_close (fh);
1022 GNUNET_free (filename);
1025 GNUNET_DISK_file_write (fh, data, data_size);
1026 GNUNET_DISK_file_close (fh);
1027 GNUNET_free (filename);
1031 GNUNET_free (filename);
1034 plc->file_offset += data_size;
1039 * Received end of message to be saved to disk.
1041 * Remove .part ending from the filename.
1044 place_recv_save_eom (void *cls,
1045 const struct GNUNET_PSYC_MessageHeader *msg,
1046 const struct GNUNET_MessageHeader *pmsg,
1047 uint64_t message_id,
1048 uint8_t is_cancelled)
1050 struct Place *plc = cls;
1051 if (GNUNET_YES != plc->file_save)
1054 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
1056 GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64,
1057 dir_social, DIR_SEPARATOR,
1058 "files", DIR_SEPARATOR,
1059 place_pub_str, DIR_SEPARATOR,
1060 GNUNET_ntohll (msg->message_id));
1061 GNUNET_free (place_pub_str);
1062 char *fn_part = NULL;
1063 GNUNET_asprintf (&fn_part, "%s.part", fn);
1065 if (rename (fn_part, fn)) {
1066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1067 "Failed to rename %s into %s: %s (%d)\n",
1068 fn_part, fn, strerror (errno), errno);
1072 GNUNET_free (fn_part);
1077 * Initialize place data structure.
1080 place_init (struct Place *plc)
1082 plc->slicer = GNUNET_PSYC_slicer_create ();
1087 * Add a place to the @e places hash map.
1092 * @return #GNUNET_OK if the place was added
1093 * #GNUNET_NO if the place already exists in the hash map
1094 * #GNUNET_SYSERR on error
1097 place_add (const struct PlaceEnterRequest *ereq)
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "Adding place to hashmap:\n");
1102 struct EgoPlacePublicKey ego_place_pub_key = {
1103 .ego_pub_key = ereq->ego_pub_key,
1104 .place_pub_key = ereq->place_pub_key,
1106 struct GNUNET_HashCode ego_place_pub_hash;
1107 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1112 struct GNUNET_MessageHeader *
1113 place_msg = GNUNET_CONTAINER_multihashmap_get (places, &ego_place_pub_hash);
1114 if (NULL != place_msg)
1117 place_msg = GNUNET_copy_message (&ereq->header);
1118 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places, &ego_place_pub_hash, place_msg,
1119 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1122 GNUNET_free (place_msg);
1123 return GNUNET_SYSERR;
1130 * Add a place to the @e app_places hash map.
1137 * @return #GNUNET_OK if the place was added
1138 * #GNUNET_NO if the place already exists in the hash map
1139 * #GNUNET_SYSERR on error
1142 app_place_add (const char *app_id,
1143 const struct PlaceEnterRequest *ereq)
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "Adding app place to hashmap:\n");
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 " app_id = %s\n", app_id);
1150 struct GNUNET_HashCode app_id_hash;
1151 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1153 struct EgoPlacePublicKey ego_place_pub_key = {
1154 .ego_pub_key = ereq->ego_pub_key,
1155 .place_pub_key = ereq->place_pub_key,
1157 struct GNUNET_HashCode ego_place_pub_hash;
1158 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1163 struct GNUNET_CONTAINER_MultiHashMap *
1164 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1165 if (NULL == app_places)
1167 app_places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1168 GNUNET_CONTAINER_multihashmap_put (apps_places, &app_id_hash, app_places,
1169 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1172 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (app_places, &ego_place_pub_hash))
1175 if (GNUNET_SYSERR == place_add (ereq))
1176 return GNUNET_SYSERR;
1178 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (app_places, &ego_place_pub_hash, NULL,
1179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1182 return GNUNET_SYSERR;
1185 struct GNUNET_HashCode place_pub_hash;
1186 GNUNET_CRYPTO_hash (&ereq->place_pub_key, sizeof (ereq->place_pub_key), &place_pub_hash);
1188 struct GNUNET_CONTAINER_MultiHashMap *
1189 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &place_pub_hash);
1190 if (NULL == place_apps)
1192 place_apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1193 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places_apps, &place_pub_hash, place_apps,
1194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1200 size_t app_id_size = strlen (app_id) + 1;
1201 void *app_id_value = GNUNET_malloc (app_id_size);
1202 GNUNET_memcpy (app_id_value, app_id, app_id_size);
1204 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (place_apps, &app_id_hash, app_id_value,
1205 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1215 * Save place entry message to disk.
1220 * Entry request message.
1223 app_place_save (const char *app_id,
1224 const struct PlaceEnterRequest *ereq)
1226 app_place_add (app_id, ereq);
1228 if (NULL == dir_places)
1229 return GNUNET_SYSERR;
1231 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ereq->ego_pub_key);
1232 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&ereq->place_pub_key);
1233 char *filename = NULL;
1234 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
1235 dir_social, DIR_SEPARATOR,
1236 "places", DIR_SEPARATOR,
1237 ego_pub_str, DIR_SEPARATOR,
1239 int ret = GNUNET_DISK_directory_create_for_file (filename);
1240 if (GNUNET_OK != ret
1241 || 0 > GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size),
1242 GNUNET_DISK_PERM_USER_READ
1243 | GNUNET_DISK_PERM_USER_WRITE))
1246 ret = GNUNET_SYSERR;
1248 GNUNET_free (filename);
1250 if (ret == GNUNET_OK)
1252 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1253 dir_social, DIR_SEPARATOR,
1254 "apps", DIR_SEPARATOR,
1255 app_id, DIR_SEPARATOR,
1256 ego_pub_str, DIR_SEPARATOR,
1258 ret = GNUNET_DISK_directory_create_for_file (filename);
1259 if (GNUNET_OK != ret
1260 || 0 > GNUNET_DISK_fn_write (filename, "", 0,
1261 GNUNET_DISK_PERM_USER_READ
1262 | GNUNET_DISK_PERM_USER_WRITE))
1265 ret = GNUNET_SYSERR;
1267 GNUNET_free (filename);
1269 GNUNET_free (ego_pub_str);
1270 GNUNET_free (place_pub_str);
1276 app_place_remove (const char *app_id,
1277 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1278 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key)
1280 struct GNUNET_HashCode ego_pub_hash;
1281 struct GNUNET_HashCode place_pub_hash;
1282 GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash);
1283 GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash);
1285 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (ego_pub_key);
1286 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (place_pub_key);
1287 char *app_place_filename = NULL;
1288 GNUNET_asprintf (&app_place_filename,
1289 "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1290 dir_social, DIR_SEPARATOR,
1291 "apps", DIR_SEPARATOR,
1292 app_id, DIR_SEPARATOR,
1293 ego_pub_str, DIR_SEPARATOR,
1295 GNUNET_free (ego_pub_str);
1296 GNUNET_free (place_pub_str);
1298 struct GNUNET_HashCode app_id_hash;
1299 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1301 struct GNUNET_CONTAINER_MultiHashMap *
1302 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1304 if (NULL != app_places)
1305 GNUNET_CONTAINER_multihashmap_remove (app_places, &place_pub_hash, NULL);
1307 struct GNUNET_CONTAINER_MultiHashMap *
1308 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &place_pub_hash);
1309 if (NULL != place_apps)
1311 void *app_id_value = GNUNET_CONTAINER_multihashmap_get (place_apps, &app_id_hash);
1312 if (NULL != app_id_value)
1314 GNUNET_CONTAINER_multihashmap_remove (place_apps, &app_id_hash, app_id_value);
1315 GNUNET_free (app_id_value);
1319 int ret = GNUNET_OK;
1321 if (0 != unlink (app_place_filename))
1324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1325 "Error removing app place file: %s: %s (%d)\n",
1326 app_place_filename, strerror (errno), errno);
1327 ret = GNUNET_SYSERR;
1329 GNUNET_free (app_place_filename);
1336 * Enter place as host.
1339 * Host entry request.
1340 * @param[out] ret_hst
1341 * Returned Host struct.
1343 * @return #GNUNET_YES if the host entered the place just now,
1344 * #GNUNET_NO if the place is already entered,
1345 * #GNUNET_SYSERR if place_pub_key was set
1346 * but its private key was not found
1349 host_enter (const struct HostEnterRequest *hreq, struct Host **ret_hst)
1351 int ret = GNUNET_NO;
1352 struct GNUNET_HashCode place_pub_hash;
1353 GNUNET_CRYPTO_hash (&hreq->place_pub_key, sizeof (hreq->place_pub_key),
1355 struct Host *hst = GNUNET_CONTAINER_multihashmap_get (hosts, &place_pub_hash);
1359 hst = GNUNET_new (struct Host);
1360 hst->policy = hreq->policy;
1361 hst->join_reqs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1362 hst->relay_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1364 struct Place *plc = &hst->place;
1366 plc->is_host = GNUNET_YES;
1367 plc->pub_key = hreq->place_pub_key;
1368 plc->pub_key_hash = place_pub_hash;
1370 GNUNET_CONTAINER_multihashmap_put (hosts, &plc->pub_key_hash, plc,
1371 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1372 hst->master = GNUNET_PSYC_master_start (cfg, &hreq->place_key, hst->policy,
1373 &psyc_master_started,
1374 &psyc_recv_join_request,
1375 &psyc_recv_message, NULL, hst);
1376 plc->channel = GNUNET_PSYC_master_get_channel (hst->master);
1380 if (NULL != ret_hst)
1387 msg_proc_parse (const struct MsgProcRequest *mpreq,
1389 const char **method_prefix,
1390 struct GNUNET_HashCode *method_hash)
1392 uint8_t method_size = ntohs (mpreq->header.size) - sizeof (*mpreq);
1393 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &mpreq[1],
1394 method_size, 1, method_prefix);
1396 if (0 == offset || offset != method_size || *method_prefix == NULL)
1398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1399 "offset = %u, method_size = %u, method_name = %s\n",
1400 offset, method_size, *method_prefix);
1401 return GNUNET_SYSERR;
1404 GNUNET_CRYPTO_hash (*method_prefix, method_size, method_hash);
1405 *flags = ntohl (mpreq->flags);
1411 check_client_msg_proc_set (void *cls,
1412 const struct MsgProcRequest *mpreq)
1419 * Handle a client setting message proccesing flags for a method prefix.
1422 handle_client_msg_proc_set (void *cls,
1423 const struct MsgProcRequest *mpreq)
1425 struct Client *c = cls;
1426 struct GNUNET_SERVICE_Client *client = c->client;
1427 struct Place *plc = c->place;
1431 GNUNET_SERVICE_client_drop (client);
1435 const char *method_prefix = NULL;
1437 struct GNUNET_HashCode method_hash;
1440 msg_proc_parse (mpreq, &flags, &method_prefix, &method_hash))
1443 GNUNET_SERVICE_client_drop (client);
1447 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1448 place_recv_relay_method,
1449 place_recv_relay_modifier,
1450 place_recv_relay_data,
1451 place_recv_relay_eom);
1452 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1453 place_recv_save_method,
1455 place_recv_save_data,
1456 place_recv_save_eom);
1458 if (flags & GNUNET_SOCIAL_MSG_PROC_RELAY)
1460 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1461 place_recv_relay_method,
1462 place_recv_relay_modifier,
1463 place_recv_relay_data,
1464 place_recv_relay_eom,
1467 if (flags & GNUNET_SOCIAL_MSG_PROC_SAVE)
1469 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1470 place_recv_save_method,
1472 place_recv_save_data,
1473 place_recv_save_eom,
1477 /** @todo Save flags to be able to resume relaying/saving after restart */
1479 GNUNET_SERVICE_client_continue (client);
1484 * Handle a connecting client requesting to clear all relay rules.
1487 handle_client_msg_proc_clear (void *cls,
1488 const struct GNUNET_MessageHeader *msg)
1490 struct Client *c = cls;
1491 struct GNUNET_SERVICE_Client *client = c->client;
1492 struct Place *plc = c->place;
1496 GNUNET_SERVICE_client_drop (client);
1500 GNUNET_PSYC_slicer_clear (plc->slicer);
1502 GNUNET_SERVICE_client_continue (client);
1507 check_client_host_enter (void *cls,
1508 const struct HostEnterRequest *hr)
1515 * Handle a connecting client entering a place as host.
1518 handle_client_host_enter (void *cls,
1519 const struct HostEnterRequest *hr)
1521 struct Client *c = cls;
1522 struct GNUNET_SERVICE_Client *client = c->client;
1524 struct HostEnterRequest *
1525 hreq = (struct HostEnterRequest *) GNUNET_copy_message (&hr->header);
1527 uint8_t app_id_size = ntohs (hreq->header.size) - sizeof (*hreq);
1528 const char *app_id = NULL;
1529 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &hreq[1],
1530 app_id_size, 1, &app_id);
1531 if (0 == offset || offset != app_id_size || app_id == NULL)
1533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1534 "offset = %u, app_id_size = %u, app_id = %s\n",
1535 offset, app_id_size, app_id);
1537 GNUNET_SERVICE_client_drop (client);
1541 struct Host *hst = NULL;
1542 struct Place *plc = NULL;
1543 int ret = GNUNET_OK;
1545 struct GNUNET_CRYPTO_EddsaPublicKey empty_pub_key;
1546 memset (&empty_pub_key, 0, sizeof (empty_pub_key));
1548 if (0 == memcmp (&hreq->place_pub_key, &empty_pub_key, sizeof (empty_pub_key)))
1549 { // no public key set: create new private key & save the place
1550 struct GNUNET_CRYPTO_EddsaPrivateKey *
1551 place_key = GNUNET_CRYPTO_eddsa_key_create ();
1552 hreq->place_key = *place_key;
1553 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &hreq->place_pub_key);
1554 GNUNET_CRYPTO_eddsa_key_clear (place_key);
1555 GNUNET_free (place_key);
1557 app_place_save (app_id, (const struct PlaceEnterRequest *) hreq);
1560 switch (host_enter (hreq, &hst))
1563 plc = c->place = &hst->place;
1569 plc = c->place = &hst->place;
1571 client_send_host_enter_ack (client, hst, GNUNET_OK);
1575 ret = GNUNET_SYSERR;
1578 if (ret != GNUNET_SYSERR)
1581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1582 "%p Client connected as host to place %s.\n",
1583 hst, GNUNET_h2s (&plc->pub_key_hash));
1585 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1586 cli->client = client;
1587 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1591 GNUNET_CRYPTO_eddsa_key_clear (&hreq->place_key);
1594 if (GNUNET_OK == ret)
1595 GNUNET_SERVICE_client_continue (client);
1597 GNUNET_SERVICE_client_drop (client);
1602 * Enter place as guest.
1605 * Guest entry request.
1606 * @param[out] ret_gst
1607 * Returned Guest struct.
1609 * @return #GNUNET_YES if the guest entered the place just now,
1610 * #GNUNET_NO if the place is already entered,
1611 * #GNUNET_SYSERR on error.
1614 guest_enter (const struct GuestEnterRequest *greq, struct Guest **ret_gst)
1616 int ret = GNUNET_NO;
1617 uint16_t greq_size = ntohs (greq->header.size);
1619 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key = greq->ego_pub_key;
1620 struct GNUNET_HashCode ego_pub_hash;
1621 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
1622 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
1625 return GNUNET_SYSERR;
1627 struct GNUNET_HashCode place_pub_hash;
1628 GNUNET_CRYPTO_hash (&greq->place_pub_key, sizeof (greq->place_pub_key),
1631 struct GNUNET_CONTAINER_MultiHashMap *
1632 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests, &place_pub_hash);
1633 struct Guest *gst = NULL;
1635 if (NULL != plc_gst)
1636 gst = GNUNET_CONTAINER_multihashmap_get (plc_gst, &ego_pub_hash);
1638 if (NULL == gst || NULL == gst->slave)
1640 gst = GNUNET_new (struct Guest);
1641 gst->origin = greq->origin;
1642 gst->relay_count = ntohl (greq->relay_count);
1645 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1646 const char *app_id = (const char *) &greq[1];
1647 const char *p = app_id;
1649 len = strnlen (app_id, remaining);
1650 if (len == remaining)
1654 return GNUNET_SYSERR;
1657 remaining -= len + 1;
1659 const struct GNUNET_PeerIdentity *relays = NULL;
1660 uint16_t relay_size = gst->relay_count * sizeof (*relays);
1661 if (remaining < relay_size)
1665 return GNUNET_SYSERR;
1668 relays = (const struct GNUNET_PeerIdentity *) p;
1670 remaining -= relay_size;
1672 struct GNUNET_PSYC_Message *join_msg = NULL;
1673 uint16_t join_msg_size = 0;
1675 if (sizeof (struct GNUNET_MessageHeader) <= remaining)
1677 join_msg = (struct GNUNET_PSYC_Message *) p;
1678 join_msg_size = ntohs (join_msg->header.size);
1680 remaining -= join_msg_size;
1684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1685 "%zu + %u + %u != %u\n",
1686 sizeof (*greq), relay_size, join_msg_size, greq_size);
1689 return GNUNET_SYSERR;
1693 gst->relays = GNUNET_malloc (relay_size);
1694 GNUNET_memcpy (gst->relays, relays, relay_size);
1697 gst->join_flags = ntohl (greq->flags);
1699 struct Place *plc = &gst->place;
1701 plc->is_host = GNUNET_NO;
1702 plc->pub_key = greq->place_pub_key;
1703 plc->pub_key_hash = place_pub_hash;
1704 plc->ego_pub_key = ego_pub_key;
1705 plc->ego_pub_hash = ego_pub_hash;
1706 plc->ego_key = ego->key;
1708 if (NULL == plc_gst)
1710 plc_gst = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1711 (void) GNUNET_CONTAINER_multihashmap_put (place_guests, &plc->pub_key_hash, plc_gst,
1712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1714 (void) GNUNET_CONTAINER_multihashmap_put (plc_gst, &plc->ego_pub_hash, gst,
1715 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1716 (void) GNUNET_CONTAINER_multihashmap_put (guests, &plc->pub_key_hash, gst,
1717 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1719 = GNUNET_PSYC_slave_join (cfg, &plc->pub_key, &plc->ego_key,
1720 gst->join_flags, &gst->origin,
1721 gst->relay_count, gst->relays,
1722 &psyc_recv_message, NULL,
1723 &psyc_slave_connected,
1724 &psyc_recv_join_dcsn,
1726 plc->channel = GNUNET_PSYC_slave_get_channel (gst->slave);
1730 if (NULL != ret_gst)
1737 check_client_guest_enter (void *cls,
1738 const struct GuestEnterRequest *greq)
1745 * Handle a connecting client entering a place as guest.
1748 handle_client_guest_enter (void *cls,
1749 const struct GuestEnterRequest *greq)
1751 struct Client *c = cls;
1752 struct GNUNET_SERVICE_Client *client = c->client;
1754 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1755 const char *app_id = NULL;
1756 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &greq[1],
1757 remaining, 1, &app_id);
1761 GNUNET_SERVICE_client_drop (client);
1765 struct Guest *gst = NULL;
1766 struct Place *plc = NULL;
1768 switch (guest_enter (greq, &gst))
1771 plc = c->place = &gst->place;
1773 app_place_save (app_id, (const struct PlaceEnterRequest *) greq);
1778 plc = c->place = &gst->place;
1781 struct GNUNET_PSYC_CountersResultMessage res;
1782 res.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
1783 res.header.size = htons (sizeof (res));
1784 res.result_code = htonl (GNUNET_OK);
1785 res.max_message_id = GNUNET_htonll (plc->max_message_id);
1787 client_send_msg (client, &res.header);
1788 if (NULL != gst->join_dcsn)
1789 client_send_msg (client, &gst->join_dcsn->header);
1795 GNUNET_SERVICE_client_drop (client);
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1800 "%p Client connected as guest to place %s.\n",
1801 gst, GNUNET_h2s (&plc->pub_key_hash));
1803 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1804 cli->client = client;
1805 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1808 GNUNET_SERVICE_client_continue (client);
1812 struct GuestEnterByNameClosure
1814 struct Client *client;
1817 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1818 struct GNUNET_MessageHeader *join_msg;
1823 * Result of a GNS name lookup for entering a place.
1825 * @see GNUNET_SOCIAL_guest_enter_by_name
1828 gns_result_guest_enter (void *cls, uint32_t rd_count,
1829 const struct GNUNET_GNSRECORD_Data *rd)
1831 struct GuestEnterByNameClosure *gcls = cls;
1832 struct Client *c = gcls->client;
1833 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1834 "%p GNS result: %u records.\n",
1837 const struct GNUNET_GNSRECORD_PlaceData *
1838 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
1840 if (0 == rd_count || rd->data_size < sizeof (*rec))
1843 GNUNET_SERVICE_client_drop (c->client);
1847 uint16_t relay_count = ntohl (rec->relay_count);
1848 struct GNUNET_PeerIdentity *relays = NULL;
1850 if (0 < relay_count)
1852 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
1854 relays = (struct GNUNET_PeerIdentity *) &rec[1];
1859 GNUNET_break_op (0);
1863 uint16_t app_id_size = strlen (gcls->app_id) + 1;
1864 uint16_t relay_size = relay_count * sizeof (*relays);
1865 uint16_t join_msg_size = 0;
1866 if (NULL != gcls->join_msg)
1867 join_msg_size = ntohs (gcls->join_msg->size);
1868 uint16_t greq_size = sizeof (struct GuestEnterRequest)
1869 + app_id_size + relay_size + join_msg_size;
1870 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1871 greq->header.size = htons (greq_size);
1872 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1873 greq->ego_pub_key = gcls->ego_pub_key;
1874 greq->place_pub_key = rec->place_pub_key;
1875 greq->origin = rec->origin;
1876 greq->relay_count = rec->relay_count;
1879 GNUNET_memcpy (p, gcls->app_id, app_id_size);
1881 GNUNET_memcpy (p, relays, relay_size);
1883 GNUNET_memcpy (p, gcls->join_msg, join_msg_size);
1885 handle_client_guest_enter (c, greq);
1887 GNUNET_free (gcls->app_id);
1888 if (NULL != gcls->password)
1889 GNUNET_free (gcls->password);
1890 if (NULL != gcls->join_msg)
1891 GNUNET_free (gcls->join_msg);
1898 check_client_guest_enter_by_name (void *cls,
1899 const struct GuestEnterByNameRequest *greq)
1906 * Handle a connecting client entering a place as guest using a GNS address.
1908 * Look up GNS address and generate a GuestEnterRequest from that.
1911 handle_client_guest_enter_by_name (void *cls,
1912 const struct GuestEnterByNameRequest *greq)
1914 struct Client *c = cls;
1915 struct GNUNET_SERVICE_Client *client = c->client;
1917 struct GuestEnterByNameClosure *gcls = GNUNET_malloc (sizeof (*gcls));
1919 gcls->ego_pub_key = greq->ego_pub_key;
1921 const char *p = (const char *) &greq[1];
1922 const char *app_id = NULL, *password = NULL, *gns_name = NULL;
1923 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1924 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 3,
1929 remaining -= offset;
1931 if (0 != offset && sizeof (*gcls->join_msg) <= remaining)
1933 gcls->join_msg = GNUNET_copy_message ((struct GNUNET_MessageHeader *) p);
1934 remaining -= ntohs (gcls->join_msg->size);
1937 if (0 == offset || 0 != remaining)
1939 if (NULL != gcls->join_msg)
1940 GNUNET_free (gcls->join_msg);
1943 GNUNET_SERVICE_client_drop (client);
1947 uint16_t app_id_size = strlen (app_id) + 1;
1948 gcls->app_id = GNUNET_malloc (app_id_size);
1949 GNUNET_memcpy (gcls->app_id, app_id, app_id_size);
1951 uint16_t password_size = strlen (password);
1952 if (0 < password_size++)
1954 gcls->password = GNUNET_malloc (password_size);
1955 GNUNET_memcpy (gcls->password, password, password_size);
1958 GNUNET_GNS_lookup (gns, gns_name,
1960 GNUNET_GNSRECORD_TYPE_PLACE,
1961 GNUNET_GNS_LO_DEFAULT,
1962 &gns_result_guest_enter, gcls);
1967 app_notify_place (struct GNUNET_MessageHeader *msg,
1968 struct GNUNET_SERVICE_Client *client)
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1971 "%p Sending place notification of type %u to client.\n",
1972 client, ntohs (msg->type));
1974 uint16_t msg_size = ntohs (msg->size);
1975 struct AppPlaceMessage amsg;
1976 amsg.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
1977 amsg.header.size = htons (sizeof (amsg));
1978 // FIXME: also notify about not entered places
1979 amsg.place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
1981 switch (ntohs (msg->type))
1983 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
1984 if (msg_size < sizeof (struct HostEnterRequest))
1986 struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
1987 amsg.is_host = GNUNET_YES;
1988 amsg.ego_pub_key = hreq->ego_pub_key;
1989 amsg.place_pub_key = hreq->place_pub_key;
1992 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
1993 if (msg_size < sizeof (struct GuestEnterRequest))
1995 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) msg;
1996 amsg.is_host = GNUNET_NO;
1997 amsg.ego_pub_key = greq->ego_pub_key;
1998 amsg.place_pub_key = greq->place_pub_key;
2005 client_send_msg (client, &amsg.header);
2010 app_notify_place_end (struct GNUNET_SERVICE_Client *client)
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2013 "%p Sending end of place list notification to client\n",
2016 struct GNUNET_MessageHeader msg;
2017 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END);
2018 msg.size = htons (sizeof (msg));
2020 client_send_msg (client, &msg);
2025 app_notify_ego (struct Ego *ego, struct GNUNET_SERVICE_Client *client)
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 "%p Sending ego notification to client: %s\n",
2031 size_t name_size = strlen (ego->name) + 1;
2032 struct AppEgoMessage *emsg = GNUNET_malloc (sizeof (*emsg) + name_size);
2033 emsg->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO);
2034 emsg->header.size = htons (sizeof (*emsg) + name_size);
2036 GNUNET_CRYPTO_ecdsa_key_get_public (&ego->key, &emsg->ego_pub_key);
2037 GNUNET_memcpy (&emsg[1], ego->name, name_size);
2039 client_send_msg (client, &emsg->header);
2045 app_notify_ego_end (struct GNUNET_SERVICE_Client *client)
2047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2048 "%p Sending end of ego list notification to client\n",
2051 struct GNUNET_MessageHeader msg;
2052 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END);
2053 msg.size = htons (sizeof (msg));
2055 client_send_msg (client, &msg);
2060 app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key, void *value)
2062 struct GNUNET_MessageHeader *
2063 msg = GNUNET_CONTAINER_multihashmap_get (places, key);
2065 app_notify_place (msg, cls);
2071 ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
2073 app_notify_ego (value, cls);
2079 check_client_app_connect (void *cls,
2080 const struct AppConnectRequest *creq)
2087 * Handle application connection.
2090 handle_client_app_connect (void *cls,
2091 const struct AppConnectRequest *creq)
2093 struct Client *c = cls;
2094 struct GNUNET_SERVICE_Client *client = c->client;
2096 uint8_t app_id_size = ntohs (creq->header.size) - sizeof (*creq);
2097 const char *app_id = NULL;
2098 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
2099 app_id_size, 1, &app_id);
2100 if (0 == offset || offset != app_id_size)
2103 GNUNET_SERVICE_client_drop (client);
2107 struct GNUNET_HashCode app_id_hash;
2108 GNUNET_CRYPTO_hash (app_id, app_id_size, &app_id_hash);
2110 GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
2111 app_notify_ego_end (client);
2113 struct GNUNET_CONTAINER_MultiHashMap *
2114 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
2115 if (NULL != app_places)
2116 GNUNET_CONTAINER_multihashmap_iterate (app_places, app_place_entry_notify, client);
2117 app_notify_place_end (client);
2119 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
2120 cli->client = client;
2121 struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
2124 app = GNUNET_malloc (sizeof (*app));
2125 (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
2126 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2128 GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "%p Application %s connected.\n", app, app_id);
2133 c->app_id = GNUNET_malloc (app_id_size);
2134 GNUNET_memcpy (c->app_id, app_id, app_id_size);
2136 GNUNET_SERVICE_client_continue (client);
2141 * Handle application detach request.
2144 handle_client_app_detach (void *cls,
2145 const struct AppDetachRequest *req)
2147 struct Client *c = cls;
2148 struct GNUNET_SERVICE_Client *client = c->client;
2150 int ret = app_place_remove (c->app_id, &req->ego_pub_key, &req->place_pub_key);
2151 client_send_result (client, req->op_id, ret, NULL, 0);
2153 GNUNET_SERVICE_client_continue (client);
2158 app_places_entry_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
2160 struct Place *plc = cls;
2161 const char *app_id = value;
2162 app_place_remove (app_id, &plc->ego_pub_key, &plc->pub_key);
2168 * Handle application leave request.
2171 handle_client_place_leave (void *cls,
2172 const struct GNUNET_MessageHeader *msg)
2174 struct Client *c = cls;
2175 struct GNUNET_SERVICE_Client *client = c->client;
2176 struct Place *plc = c->place;
2180 GNUNET_SERVICE_client_drop (client);
2184 /* FIXME: remove all app subscriptions and leave this place */
2186 struct GNUNET_CONTAINER_MultiHashMap *
2187 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &plc->pub_key_hash);
2188 if (NULL != place_apps)
2190 GNUNET_CONTAINER_multihashmap_iterate (place_apps, app_places_entry_remove, plc);
2193 /* FIXME: disconnect from the network, but keep local connection for history access */
2195 /* Disconnect all clients connected to the place */
2196 struct ClientListItem *cli = plc->clients_head, *next;
2199 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
2200 GNUNET_SERVICE_client_drop (cli->client);
2206 if (GNUNET_YES != plc->is_disconnected)
2208 plc->is_disconnected = GNUNET_YES;
2209 if (NULL != plc->tmit_msgs_head)
2210 { /* Send pending messages to PSYC before cleanup. */
2211 psyc_transmit_message (plc);
2215 cleanup_place (plc);
2221 struct JoinDecisionClosure
2223 int32_t is_admitted;
2224 struct GNUNET_PSYC_Message *msg;
2229 * Iterator callback for responding to join requests.
2232 psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
2235 struct JoinDecisionClosure *jcls = cls;
2236 struct GNUNET_PSYC_JoinHandle *jh = value;
2237 // FIXME: add relays
2238 GNUNET_PSYC_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
2244 check_client_join_decision (void *cls,
2245 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2252 * Handle an entry decision from a host client.
2255 handle_client_join_decision (void *cls,
2256 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2258 struct Client *c = cls;
2259 struct GNUNET_SERVICE_Client *client = c->client;
2260 struct Place *plc = c->place;
2261 if (NULL == plc || GNUNET_YES != plc->is_host)
2264 GNUNET_SERVICE_client_drop (client);
2267 struct Host *hst = plc->host;
2269 struct JoinDecisionClosure jcls;
2270 jcls.is_admitted = ntohl (dcsn->is_admitted);
2272 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (dcsn->header.size))
2273 ? (struct GNUNET_PSYC_Message *) &dcsn[1]
2276 struct GNUNET_HashCode slave_pub_hash;
2277 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 "%p Got join decision (%d) from client for place %s..\n",
2282 hst, jcls.is_admitted, GNUNET_h2s (&plc->pub_key_hash));
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2284 "%p ..and slave %s.\n",
2285 hst, GNUNET_h2s (&slave_pub_hash));
2287 GNUNET_CONTAINER_multihashmap_get_multiple (hst->join_reqs, &slave_pub_hash,
2288 &psyc_send_join_decision, &jcls);
2289 GNUNET_CONTAINER_multihashmap_remove_all (hst->join_reqs, &slave_pub_hash);
2291 GNUNET_SERVICE_client_continue (client);
2296 * Send acknowledgement to a client.
2298 * Sent after a message fragment has been passed on to multicast.
2300 * @param plc The place struct for the client.
2303 send_message_ack (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2305 struct GNUNET_MessageHeader res;
2306 res.size = htons (sizeof (res));
2307 res.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
2308 client_send_msg (client, &res);
2313 * Proceed to the next message part in the transmission queue.
2316 * Place where the transmission is going on.
2318 * Currently transmitted message.
2320 * Currently transmitted message fragment.
2322 * @return @a tmit_frag, or NULL if reached the end of fragment.
2324 static struct FragmentTransmitQueue *
2325 psyc_transmit_queue_next_part (struct Place *plc,
2326 struct MessageTransmitQueue *tmit_msg,
2327 struct FragmentTransmitQueue *tmit_frag)
2329 uint16_t psize = ntohs (tmit_frag->next_part->size);
2330 if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
2333 tmit_frag->next_part
2334 = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
2336 else /* Reached end of current fragment. */
2338 if (NULL != tmit_frag->client)
2339 send_message_ack (plc, tmit_frag->client);
2340 GNUNET_CONTAINER_DLL_remove (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2341 GNUNET_free (tmit_frag);
2349 * Proceed to next message in transmission queue.
2352 * Place where the transmission is going on.
2354 * Currently transmitted message.
2356 * @return The next message in queue, or NULL if queue is empty.
2358 static struct MessageTransmitQueue *
2359 psyc_transmit_queue_next_msg (struct Place *plc,
2360 struct MessageTransmitQueue *tmit_msg)
2362 GNUNET_CONTAINER_DLL_remove (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2363 GNUNET_free (tmit_msg);
2364 return plc->tmit_msgs_head;
2369 * Callback for data transmission to PSYC.
2372 psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2374 struct Place *plc = cls;
2375 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2376 GNUNET_assert (NULL != tmit_msg);
2377 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2378 if (NULL == tmit_frag)
2379 { /* Rest of the message have not arrived yet, pause transmission */
2383 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "%p psyc_transmit_notify_data: nothing to send.\n", plc);
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "%p psyc_transmit_notify_data()\n", plc);
2394 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2396 uint16_t ptype = ntohs (pmsg->type);
2397 uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
2402 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2403 if (*data_size < pdata_size)
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 "%p psyc_transmit_notify_data: buffer size too small for data.\n", plc);
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "%p psyc_transmit_notify_data: sending %u bytes.\n",
2414 *data_size = pdata_size;
2415 GNUNET_memcpy (data, &pmsg[1], *data_size);
2419 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2424 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2426 ret = GNUNET_SYSERR;
2430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2431 "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
2433 ret = GNUNET_SYSERR;
2436 if (GNUNET_SYSERR == ret && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL != ptype)
2439 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2440 plc->is_disconnected = GNUNET_YES;
2441 GNUNET_SERVICE_client_drop (tmit_frag->client);
2442 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2447 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2448 if (NULL != tmit_frag)
2450 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2451 ptype = ntohs (pmsg->type);
2454 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2457 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2458 ret = GNUNET_SYSERR;
2463 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2464 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2465 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2469 if (NULL == tmit_msg->frags_head
2470 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2471 { /* Reached end of current message. */
2472 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2476 if (ret != GNUNET_NO)
2478 if (NULL != tmit_msg)
2480 psyc_transmit_message (plc);
2482 else if (GNUNET_YES == plc->is_disconnected)
2484 /* FIXME: handle partial message (when still in_transmit) */
2485 cleanup_place (plc);
2493 * Callback for modifier transmission to PSYC.
2496 psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2497 uint8_t *oper, uint32_t *full_value_size)
2499 struct Place *plc = cls;
2500 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2501 GNUNET_assert (NULL != tmit_msg);
2502 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2503 if (NULL == tmit_frag)
2504 { /* Rest of the message have not arrived yet, pause transmission */
2508 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 "%p psyc_transmit_notify_mod()\n", plc);
2519 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2521 uint16_t ptype = ntohs (pmsg->type);
2526 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
2530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2531 "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
2532 ret = GNUNET_SYSERR;
2535 struct GNUNET_PSYC_MessageModifier *
2536 pmod = (struct GNUNET_PSYC_MessageModifier *) tmit_frag->next_part;
2537 uint16_t mod_size = ntohs (pmod->header.size) - sizeof (*pmod);
2539 if (*data_size < mod_size)
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2547 *full_value_size = ntohl (pmod->value_size);
2549 *data_size = mod_size;
2550 GNUNET_memcpy (data, &pmod[1], mod_size);
2555 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
2559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2560 "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
2561 ret = GNUNET_SYSERR;
2564 uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
2565 if (*data_size < mod_size)
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc, mod_size);
2575 *data_size = mod_size;
2576 GNUNET_memcpy (data, &pmsg[1], *data_size);
2581 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2582 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2583 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2590 "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
2592 ret = GNUNET_SYSERR;
2595 if (GNUNET_SYSERR == ret)
2598 ret = GNUNET_SYSERR;
2599 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2600 plc->is_disconnected = GNUNET_YES;
2601 GNUNET_SERVICE_client_drop (tmit_frag->client);
2602 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2606 if (GNUNET_YES != ret)
2607 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2609 if (NULL == tmit_msg->frags_head
2610 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2611 { /* Reached end of current message. */
2612 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2619 * Callback for data transmission from a host to PSYC.
2622 host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2624 int ret = psyc_transmit_notify_data (cls, data_size, data);
2626 if (GNUNET_NO != ret)
2628 struct Host *hst = cls;
2629 hst->tmit_handle = NULL;
2636 * Callback for the transmit functions of multicast.
2639 guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2641 int ret = psyc_transmit_notify_data (cls, data_size, data);
2643 if (GNUNET_NO != ret)
2645 struct Guest *gst = cls;
2646 gst->tmit_handle = NULL;
2653 * Callback for modifier transmission from a host to PSYC.
2656 host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2657 uint8_t *oper, uint32_t *full_value_size)
2659 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2660 oper, full_value_size);
2661 if (GNUNET_SYSERR == ret)
2663 struct Host *hst = cls;
2664 hst->tmit_handle = NULL;
2671 * Callback for modifier transmission from a guest to PSYC.
2674 guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2675 uint8_t *oper, uint32_t *full_value_size)
2677 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2678 oper, full_value_size);
2679 if (GNUNET_SYSERR == ret)
2681 struct Guest *gst = cls;
2682 gst->tmit_handle = NULL;
2689 * Get method part of next message from transmission queue.
2694 * @return #GNUNET_OK on success
2695 * #GNUNET_NO if there are no more messages in queue.
2696 * #GNUNET_SYSERR if the next message is malformed.
2698 static struct GNUNET_PSYC_MessageMethod *
2699 psyc_transmit_queue_next_method (struct Place *plc)
2701 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2702 if (NULL == tmit_msg)
2705 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2706 if (NULL == tmit_frag)
2712 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2714 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type))
2716 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2717 "%p psyc_transmit_queue_next_method: unexpected message part of type %u.\n",
2718 plc, NULL != pmsg ? ntohs (pmsg->type) : 0);
2723 uint16_t psize = ntohs (pmsg->size);
2724 struct GNUNET_PSYC_MessageMethod *
2725 pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);
2727 if (psize < sizeof (*pmeth) + 1 || '\0' != *((char *) pmeth + psize - 1))
2729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2730 "%p psyc_transmit_queue_next_method: invalid method name.\n",
2732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2733 "%zu <= %u || NUL != %u\n",
2734 sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
2736 GNUNET_free (pmeth);
2740 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2746 * Transmit the next message in queue from the host to the PSYC channel.
2749 psyc_master_transmit_message (struct Host *hst)
2751 struct Place *plc = &hst->place;
2753 if (NULL == hst->tmit_handle)
2755 struct GNUNET_PSYC_MessageMethod *
2756 pmeth = psyc_transmit_queue_next_method (plc);
2758 return GNUNET_SYSERR;
2760 hst->tmit_handle = (void *) &hst->tmit_handle;
2761 struct GNUNET_PSYC_MasterTransmitHandle *
2762 tmit_handle = GNUNET_PSYC_master_transmit (hst->master, (const char *) &pmeth[1],
2763 &host_transmit_notify_mod,
2764 &host_transmit_notify_data, hst,
2766 if (NULL != hst->tmit_handle)
2767 hst->tmit_handle = tmit_handle;
2768 GNUNET_free (pmeth);
2772 GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
2779 * Transmit the next message in queue from a guest to the PSYC channel.
2782 psyc_slave_transmit_message (struct Guest *gst)
2784 struct Place *plc = &gst->place;
2786 if (NULL == gst->tmit_handle)
2788 struct GNUNET_PSYC_MessageMethod *
2789 pmeth = psyc_transmit_queue_next_method (plc);
2791 return GNUNET_SYSERR;
2793 gst->tmit_handle = (void *) &gst->tmit_handle;
2794 struct GNUNET_PSYC_SlaveTransmitHandle *
2795 tmit_handle = GNUNET_PSYC_slave_transmit (gst->slave, (const char *) &pmeth[1],
2796 &guest_transmit_notify_mod,
2797 &guest_transmit_notify_data, gst,
2799 if (NULL != gst->tmit_handle)
2800 gst->tmit_handle = tmit_handle;
2801 GNUNET_free (pmeth);
2805 GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
2812 * Transmit a message to PSYC.
2815 psyc_transmit_message (struct Place *plc)
2819 ? psyc_master_transmit_message ((struct Host *) plc)
2820 : psyc_slave_transmit_message ((struct Guest *) plc);
2825 * Queue message parts for sending to PSYC.
2827 * @param plc Place to send to.
2828 * @param client Client the message originates from.
2829 * @param data_size Size of @a data.
2830 * @param data Concatenated message parts.
2831 * @param first_ptype First message part type in @a data.
2832 * @param last_ptype Last message part type in @a data.
2834 static struct MessageTransmitQueue *
2835 psyc_transmit_queue_message (struct Place *plc,
2836 struct GNUNET_SERVICE_Client *client,
2839 uint16_t first_ptype, uint16_t last_ptype,
2840 struct MessageTransmitQueue *tmit_msg)
2842 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
2844 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
2845 GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2847 else if (NULL == tmit_msg)
2852 struct FragmentTransmitQueue *
2853 tmit_frag = GNUNET_malloc (sizeof (*tmit_frag) + data_size);
2854 GNUNET_memcpy (&tmit_frag[1], data, data_size);
2855 tmit_frag->next_part = (struct GNUNET_MessageHeader *) &tmit_frag[1];
2856 tmit_frag->client = client;
2857 tmit_frag->size = data_size;
2859 GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2860 tmit_msg->client = client;
2866 * Cancel transmission of current message to PSYC.
2868 * @param plc Place to send to.
2869 * @param client Client the message originates from.
2872 psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2874 uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2876 struct GNUNET_MessageHeader msg;
2877 msg.size = htons (sizeof (msg));
2878 msg.type = htons (type);
2880 psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
2881 psyc_transmit_message (plc);
2883 /* FIXME: cleanup */
2888 check_client_psyc_message (void *cls,
2889 const struct GNUNET_MessageHeader *msg)
2896 * Handle an incoming message from a client, to be transmitted to the place.
2899 handle_client_psyc_message (void *cls,
2900 const struct GNUNET_MessageHeader *msg)
2902 struct Client *c = cls;
2903 struct GNUNET_SERVICE_Client *client = c->client;
2904 struct Place *plc = c->place;
2908 GNUNET_SERVICE_client_drop (client);
2912 int ret = GNUNET_SYSERR;
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2915 "%p Received message from client.\n", plc);
2916 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);
2918 if (GNUNET_YES != plc->is_ready)
2920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2921 "%p Place is not ready yet, disconnecting client.\n", plc);
2923 GNUNET_SERVICE_client_drop (client);
2927 uint16_t size = ntohs (msg->size);
2928 uint16_t psize = size - sizeof (*msg);
2929 if (psize < sizeof (struct GNUNET_MessageHeader)
2930 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < psize)
2932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2933 "%p Received message with invalid payload size (%u) from client.\n",
2936 psyc_transmit_cancel (plc, client);
2937 GNUNET_SERVICE_client_drop (client);
2941 uint16_t first_ptype = 0, last_ptype = 0;
2943 == GNUNET_PSYC_receive_check_parts (psize, (const char *) &msg[1],
2944 &first_ptype, &last_ptype))
2946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2947 "%p Received invalid message part from client.\n", plc);
2949 psyc_transmit_cancel (plc, client);
2950 GNUNET_SERVICE_client_drop (client);
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2954 "%p Received message with first part type %u and last part type %u.\n",
2955 plc, first_ptype, last_ptype);
2958 = psyc_transmit_queue_message (plc, client, psize, &msg[1],
2959 first_ptype, last_ptype, c->tmit_msg);
2960 if (NULL != c->tmit_msg)
2962 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
2964 ret = psyc_transmit_message (plc);
2967 if (GNUNET_OK != ret)
2969 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2970 "%p Received invalid message part from client.\n", plc);
2972 psyc_transmit_cancel (plc, client);
2973 ret = GNUNET_SYSERR;
2976 if (GNUNET_OK == ret)
2977 GNUNET_SERVICE_client_continue (client);
2979 GNUNET_SERVICE_client_drop (client);
2984 * A historic message arrived from PSYC.
2987 psyc_recv_history_message (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
2989 struct OperationClosure *opcls = cls;
2990 struct Client *c = opcls->client;
2991 struct Place *plc = c->place;
2993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2994 "%p Received historic message #%" PRId64 " (flags: %x)\n",
2995 plc, GNUNET_ntohll (msg->message_id), ntohl (msg->flags));
2997 uint16_t size = ntohs (msg->header.size);
2999 struct GNUNET_OperationResultMessage *
3000 res = GNUNET_malloc (sizeof (*res) + size);
3001 res->header.size = htons (sizeof (*res) + size);
3002 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
3003 res->op_id = opcls->op_id;
3004 res->result_code = GNUNET_htonll (GNUNET_OK);
3006 GNUNET_memcpy (&res[1], msg, size);
3008 /** @todo FIXME: send only to requesting client */
3009 place_send_msg (plc, &res->header);
3016 * Result of message history replay from PSYC.
3019 psyc_recv_history_result (void *cls, int64_t result,
3020 const void *err_msg, uint16_t err_msg_size)
3022 struct OperationClosure *opcls = cls;
3023 struct Client *c = opcls->client;
3024 struct Place *plc = c->place;
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3027 "%p History replay #%" PRIu64 ": "
3028 "PSYCstore returned %" PRId64 " (%.*s)\n",
3029 plc, GNUNET_ntohll (opcls->op_id), result,
3030 err_msg_size, (const char *) err_msg);
3032 // FIXME: place might have been destroyed
3033 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3038 check_client_history_replay (void *cls,
3039 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3046 * Client requests channel history.
3049 handle_client_history_replay (void *cls,
3050 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3052 struct Client *c = cls;
3053 struct GNUNET_SERVICE_Client *client = c->client;
3054 struct Place *plc = c->place;
3058 GNUNET_SERVICE_client_drop (client);
3062 uint16_t size = ntohs (req->header.size);
3063 const char *method_prefix = (const char *) &req[1];
3065 if (size < sizeof (*req) + 1
3066 || '\0' != method_prefix[size - sizeof (*req) - 1])
3068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3069 "%p History replay #%" PRIu64 ": "
3070 "invalid method prefix. size: %u < %zu?\n",
3071 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3073 GNUNET_SERVICE_client_drop (client);
3077 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3079 opcls->op_id = req->op_id;
3080 opcls->flags = ntohl (req->flags);
3082 if (0 == req->message_limit)
3083 GNUNET_PSYC_channel_history_replay (plc->channel,
3084 GNUNET_ntohll (req->start_message_id),
3085 GNUNET_ntohll (req->end_message_id),
3086 method_prefix, opcls->flags,
3087 psyc_recv_history_message, NULL,
3088 psyc_recv_history_result, opcls);
3090 GNUNET_PSYC_channel_history_replay_latest (plc->channel,
3091 GNUNET_ntohll (req->message_limit),
3092 method_prefix, opcls->flags,
3093 psyc_recv_history_message, NULL,
3094 psyc_recv_history_result, opcls);
3096 GNUNET_SERVICE_client_continue (client);
3101 * A state variable part arrived from PSYC.
3104 psyc_recv_state_var (void *cls,
3105 const struct GNUNET_MessageHeader *mod,
3108 uint32_t value_size,
3109 uint32_t full_value_size)
3111 struct OperationClosure *opcls = cls;
3112 struct Client *c = opcls->client;
3113 struct Place *plc = c->place;
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3116 "%p Received state variable %s from PSYC\n",
3119 uint16_t size = ntohs (mod->size);
3121 struct GNUNET_OperationResultMessage *
3122 res = GNUNET_malloc (sizeof (*res) + size);
3123 res->header.size = htons (sizeof (*res) + size);
3124 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
3125 res->op_id = opcls->op_id;
3126 res->result_code = GNUNET_htonll (GNUNET_OK);
3128 GNUNET_memcpy (&res[1], mod, size);
3130 /** @todo FIXME: send only to requesting client */
3131 place_send_msg (plc, &res->header);
3138 * Result of retrieving state variable from PSYC.
3141 psyc_recv_state_result (void *cls, int64_t result,
3142 const void *err_msg, uint16_t err_msg_size)
3144 struct OperationClosure *opcls = cls;
3145 struct Client *c = opcls->client;
3146 struct Place *plc = c->place;
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 "%p State get #%" PRIu64 ": "
3150 "PSYCstore returned %" PRId64 " (%.*s)\n",
3151 plc, GNUNET_ntohll (opcls->op_id), result,
3152 err_msg_size, (const char *) err_msg);
3154 // FIXME: place might have been destroyed
3155 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3160 check_client_state_get (void *cls,
3161 const struct GNUNET_PSYC_StateRequestMessage *req)
3168 * Client requests channel history.
3171 handle_client_state_get (void *cls,
3172 const struct GNUNET_PSYC_StateRequestMessage *req)
3174 struct Client *c = cls;
3175 struct GNUNET_SERVICE_Client *client = c->client;
3176 struct Place *plc = c->place;
3180 GNUNET_SERVICE_client_drop (client);
3184 uint16_t size = ntohs (req->header.size);
3185 const char *name = (const char *) &req[1];
3187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3188 "%p State get #%" PRIu64 ": %s\n",
3189 plc, GNUNET_ntohll (req->op_id), name);
3191 if (size < sizeof (*req) + 1
3192 || '\0' != name[size - sizeof (*req) - 1])
3194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3195 "%p State get #%" PRIu64 ": "
3196 "invalid name. size: %u < %zu?\n",
3197 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3199 GNUNET_SERVICE_client_drop (client);
3203 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3205 opcls->op_id = req->op_id;
3207 switch (ntohs (req->header.type))
3209 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET:
3210 GNUNET_PSYC_channel_state_get (plc->channel, name,
3211 psyc_recv_state_var,
3212 psyc_recv_state_result, opcls);
3215 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX:
3216 GNUNET_PSYC_channel_state_get_prefix (plc->channel, name,
3217 psyc_recv_state_var,
3218 psyc_recv_state_result, opcls);
3225 GNUNET_SERVICE_client_continue (client);
3229 #define check_client_state_get_prefix check_client_state_get
3230 #define handle_client_state_get_prefix handle_client_state_get
3234 namestore_recv_records_store_result (void *cls, int32_t result,
3235 const char *err_msg)
3237 struct OperationClosure *opcls = cls;
3238 struct Client *c = opcls->client;
3240 // FIXME: client might have been disconnected
3241 client_send_result (c->client, opcls->op_id, result, err_msg,
3242 (NULL != err_msg) ? strlen (err_msg) : 0);
3243 GNUNET_free (opcls);
3248 check_client_zone_add_place (void *cls,
3249 const struct ZoneAddPlaceRequest *preq)
3256 * Handle request to add PLACE record to GNS zone.
3259 handle_client_zone_add_place (void *cls,
3260 const struct ZoneAddPlaceRequest *preq)
3262 struct Client *c = cls;
3263 struct GNUNET_SERVICE_Client *client = c->client;
3265 uint16_t remaining = ntohs (preq->header.size) - sizeof (*preq);
3266 const char *p = (const char *) &preq[1];
3267 const char *name = NULL, *password = NULL;
3268 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 2,
3270 remaining -= offset;
3272 const struct GNUNET_PeerIdentity *
3273 relays = (const struct GNUNET_PeerIdentity *) p;
3274 uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);
3276 if (0 == offset || remaining != relay_size)
3279 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3280 GNUNET_SERVICE_client_drop (client);
3284 struct GNUNET_GNSRECORD_Data rd = { };
3285 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
3286 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3287 rd.expiration_time = GNUNET_ntohll (preq->expiration_time);
3289 struct GNUNET_GNSRECORD_PlaceData *
3290 rec = GNUNET_malloc (sizeof (*rec) + relay_size);
3291 rec->place_pub_key = preq->place_pub_key;
3292 rec->origin = this_peer;
3293 rec->relay_count = preq->relay_count;
3294 GNUNET_memcpy (&rec[1], relays, relay_size);
3297 rd.data_size = sizeof (*rec) + relay_size;
3299 struct GNUNET_HashCode ego_pub_hash;
3300 GNUNET_CRYPTO_hash (&preq->ego_pub_key, sizeof (preq->ego_pub_key), &ego_pub_hash);
3301 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3304 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3308 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3310 opcls->op_id = preq->op_id;
3311 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3313 namestore_recv_records_store_result, opcls);
3314 /** @todo refresh stored records later */
3316 GNUNET_SERVICE_client_continue (client);
3321 check_client_zone_add_nym (void *cls,
3322 const struct ZoneAddNymRequest *nreq)
3329 * Handle request to add PLACE record to GNS zone.
3332 handle_client_zone_add_nym (void *cls,
3333 const struct ZoneAddNymRequest *nreq)
3335 struct Client *c = cls;
3336 struct GNUNET_SERVICE_Client *client = c->client;
3338 uint16_t name_size = ntohs (nreq->header.size) - sizeof (*nreq);
3339 const char *name = NULL;
3340 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &nreq[1],
3341 name_size, 1, &name);
3342 if (0 == offset || offset != name_size)
3345 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3346 GNUNET_SERVICE_client_continue (client);
3350 struct GNUNET_GNSRECORD_Data rd = { };
3351 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
3352 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3353 rd.expiration_time = GNUNET_ntohll (nreq->expiration_time);
3354 rd.data = &nreq->nym_pub_key;
3355 rd.data_size = sizeof (nreq->nym_pub_key);
3357 struct GNUNET_HashCode ego_pub_hash;
3358 GNUNET_CRYPTO_hash (&nreq->ego_pub_key, sizeof (nreq->ego_pub_key), &ego_pub_hash);
3359 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3362 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3366 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3368 opcls->op_id = nreq->op_id;
3369 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3371 namestore_recv_records_store_result, opcls);
3372 /** @todo refresh stored records later */
3374 GNUNET_SERVICE_client_continue (client);
3379 path_basename (const char *path)
3381 const char *basename = strrchr (path, DIR_SEPARATOR);
3382 if (NULL != basename)
3385 if (NULL == basename || '\0' == basename)
3392 struct PlaceLoadClosure
3395 const char *ego_pub_str;
3399 /** Load a place file */
3401 file_place_load (void *cls, const char *place_filename)
3403 struct PlaceLoadClosure *plcls = cls;
3405 const char *place_pub_str = path_basename (place_filename);
3406 if (NULL == place_pub_str)
3412 char *filename = NULL;
3413 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3414 dir_social, DIR_SEPARATOR,
3415 "places", DIR_SEPARATOR,
3416 plcls->ego_pub_str, DIR_SEPARATOR,
3419 uint64_t file_size = 0;
3421 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3422 || file_size < sizeof (struct PlaceEnterRequest))
3424 GNUNET_free (filename);
3428 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
3429 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
3430 GNUNET_free (filename);
3431 if (read_size < 0 || read_size < sizeof (*ereq))
3437 uint16_t ereq_size = ntohs (ereq->header.size);
3438 if (read_size != ereq_size)
3444 switch (ntohs (ereq->header.type))
3446 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3447 if (ereq_size < sizeof (struct HostEnterRequest))
3452 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3453 host_enter (hreq, NULL);
3456 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3457 if (ereq_size < sizeof (struct GuestEnterRequest))
3462 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3463 guest_enter (greq, NULL);
3471 app_place_add (plcls->app_id, ereq);
3478 * Read @e place_pub_str entries in @a dir_ego
3481 * Data directory of an application ego.
3482 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_str/
3485 scan_app_ego_dir (void *cls, const char *dir_ego)
3487 struct PlaceLoadClosure *plcls = cls;
3488 plcls->ego_pub_str = path_basename (dir_ego);
3490 if (NULL != plcls->ego_pub_str)
3491 GNUNET_DISK_directory_scan (dir_ego, file_place_load, plcls);
3497 * Read @e ego_pub_str entries in @a dir_app
3500 * Data directory of an application.
3501 * $GNUNET_DATA_HOME/social/apps/$app_id/
3504 scan_app_dir (void *cls, const char *dir_app)
3506 if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
3509 struct PlaceLoadClosure plcls;
3510 plcls.app_id = path_basename (dir_app);
3512 if (NULL != plcls.app_id)
3513 GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);
3520 identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
3521 void **ctx, const char *name)
3523 if (NULL == id_ego) // end of initial list of egos
3526 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
3527 GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);
3529 struct GNUNET_HashCode ego_pub_hash;
3530 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
3532 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3535 GNUNET_free (ego->name);
3536 if (NULL == name) // deleted
3538 GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
3545 ego = GNUNET_malloc (sizeof (*ego));
3549 ego->key = *(GNUNET_IDENTITY_ego_get_private_key (id_ego));
3550 size_t name_size = strlen (name) + 1;
3551 ego->name = GNUNET_malloc (name_size);
3552 GNUNET_memcpy (ego->name, name, name_size);
3554 GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
3555 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3558 // FIXME: notify clients about changed ego
3563 * Initialize the PSYC service.
3565 * @param cls Closure.
3566 * @param server The initialized server.
3567 * @param c Configuration to use.
3571 const struct GNUNET_CONFIGURATION_Handle *c,
3572 struct GNUNET_SERVICE_Handle *svc)
3576 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
3578 hosts = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3579 guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3580 place_guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3582 egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3583 apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3584 places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3585 apps_places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3586 places_apps = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3588 id = GNUNET_IDENTITY_connect (cfg, &identity_recv_ego, NULL);
3589 gns = GNUNET_GNS_connect (cfg);
3590 namestore = GNUNET_NAMESTORE_connect (cfg);
3591 stats = GNUNET_STATISTICS_create ("social", cfg);
3594 GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
3597 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3598 "social", "DATA_HOME");
3602 GNUNET_asprintf (&dir_places, "%s%c%s",
3603 dir_social, DIR_SEPARATOR, "places");
3604 GNUNET_asprintf (&dir_apps, "%s%c%s",
3605 dir_social, DIR_SEPARATOR, "apps");
3607 GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);
3609 GNUNET_SCHEDULER_add_shutdown (shutdown_task, NULL);
3614 * Define "main" method using service macro.
3618 GNUNET_SERVICE_OPTION_NONE,
3620 client_notify_connect,
3621 client_notify_disconnect,
3623 GNUNET_MQ_hd_var_size (client_host_enter,
3624 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER,
3625 struct HostEnterRequest,
3627 GNUNET_MQ_hd_var_size (client_guest_enter,
3628 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER,
3629 struct GuestEnterRequest,
3631 GNUNET_MQ_hd_var_size (client_guest_enter_by_name,
3632 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME,
3633 struct GuestEnterByNameRequest,
3635 GNUNET_MQ_hd_var_size (client_join_decision,
3636 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
3637 struct GNUNET_PSYC_JoinDecisionMessage,
3639 GNUNET_MQ_hd_var_size (client_psyc_message,
3640 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
3641 struct GNUNET_MessageHeader,
3643 GNUNET_MQ_hd_var_size (client_history_replay,
3644 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY,
3645 struct GNUNET_PSYC_HistoryRequestMessage,
3647 GNUNET_MQ_hd_var_size (client_state_get,
3648 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
3649 struct GNUNET_PSYC_StateRequestMessage,
3651 GNUNET_MQ_hd_var_size (client_state_get_prefix,
3652 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
3653 struct GNUNET_PSYC_StateRequestMessage,
3655 GNUNET_MQ_hd_var_size (client_zone_add_place,
3656 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE,
3657 struct ZoneAddPlaceRequest,
3659 GNUNET_MQ_hd_var_size (client_zone_add_nym,
3660 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM,
3661 struct ZoneAddNymRequest,
3663 GNUNET_MQ_hd_var_size (client_app_connect,
3664 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT,
3665 struct AppConnectRequest,
3667 GNUNET_MQ_hd_fixed_size (client_app_detach,
3668 GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH,
3669 struct AppDetachRequest,
3671 GNUNET_MQ_hd_fixed_size (client_place_leave,
3672 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE,
3673 struct GNUNET_MessageHeader,
3675 GNUNET_MQ_hd_var_size (client_msg_proc_set,
3676 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET,
3677 struct MsgProcRequest,
3679 GNUNET_MQ_hd_fixed_size (client_msg_proc_clear,
3680 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR,
3681 struct GNUNET_MessageHeader,
3684 /* end of gnunet-service-social.c */