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, &greq->ego_pub_key,
1959 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
1960 NULL, gns_result_guest_enter, gcls);
1965 app_notify_place (struct GNUNET_MessageHeader *msg,
1966 struct GNUNET_SERVICE_Client *client)
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 "%p Sending place notification of type %u to client.\n",
1970 client, ntohs (msg->type));
1972 uint16_t msg_size = ntohs (msg->size);
1973 struct AppPlaceMessage amsg;
1974 amsg.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
1975 amsg.header.size = htons (sizeof (amsg));
1976 // FIXME: also notify about not entered places
1977 amsg.place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
1979 switch (ntohs (msg->type))
1981 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
1982 if (msg_size < sizeof (struct HostEnterRequest))
1984 struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
1985 amsg.is_host = GNUNET_YES;
1986 amsg.ego_pub_key = hreq->ego_pub_key;
1987 amsg.place_pub_key = hreq->place_pub_key;
1990 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
1991 if (msg_size < sizeof (struct GuestEnterRequest))
1993 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) msg;
1994 amsg.is_host = GNUNET_NO;
1995 amsg.ego_pub_key = greq->ego_pub_key;
1996 amsg.place_pub_key = greq->place_pub_key;
2003 client_send_msg (client, &amsg.header);
2008 app_notify_place_end (struct GNUNET_SERVICE_Client *client)
2010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2011 "%p Sending end of place list notification to client\n",
2014 struct GNUNET_MessageHeader msg;
2015 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END);
2016 msg.size = htons (sizeof (msg));
2018 client_send_msg (client, &msg);
2023 app_notify_ego (struct Ego *ego, struct GNUNET_SERVICE_Client *client)
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "%p Sending ego notification to client: %s\n",
2029 size_t name_size = strlen (ego->name) + 1;
2030 struct AppEgoMessage *emsg = GNUNET_malloc (sizeof (*emsg) + name_size);
2031 emsg->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO);
2032 emsg->header.size = htons (sizeof (*emsg) + name_size);
2034 GNUNET_CRYPTO_ecdsa_key_get_public (&ego->key, &emsg->ego_pub_key);
2035 GNUNET_memcpy (&emsg[1], ego->name, name_size);
2037 client_send_msg (client, &emsg->header);
2043 app_notify_ego_end (struct GNUNET_SERVICE_Client *client)
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "%p Sending end of ego list notification to client\n",
2049 struct GNUNET_MessageHeader msg;
2050 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END);
2051 msg.size = htons (sizeof (msg));
2053 client_send_msg (client, &msg);
2058 app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key, void *value)
2060 struct GNUNET_MessageHeader *
2061 msg = GNUNET_CONTAINER_multihashmap_get (places, key);
2063 app_notify_place (msg, cls);
2069 ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
2071 app_notify_ego (value, cls);
2077 check_client_app_connect (void *cls,
2078 const struct AppConnectRequest *creq)
2085 * Handle application connection.
2088 handle_client_app_connect (void *cls,
2089 const struct AppConnectRequest *creq)
2091 struct Client *c = cls;
2092 struct GNUNET_SERVICE_Client *client = c->client;
2094 uint8_t app_id_size = ntohs (creq->header.size) - sizeof (*creq);
2095 const char *app_id = NULL;
2096 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
2097 app_id_size, 1, &app_id);
2098 if (0 == offset || offset != app_id_size)
2101 GNUNET_SERVICE_client_drop (client);
2105 struct GNUNET_HashCode app_id_hash;
2106 GNUNET_CRYPTO_hash (app_id, app_id_size, &app_id_hash);
2108 GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
2109 app_notify_ego_end (client);
2111 struct GNUNET_CONTAINER_MultiHashMap *
2112 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
2113 if (NULL != app_places)
2114 GNUNET_CONTAINER_multihashmap_iterate (app_places, app_place_entry_notify, client);
2115 app_notify_place_end (client);
2117 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
2118 cli->client = client;
2119 struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
2122 app = GNUNET_malloc (sizeof (*app));
2123 (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
2124 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2126 GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "%p Application %s connected.\n", app, app_id);
2131 c->app_id = GNUNET_malloc (app_id_size);
2132 GNUNET_memcpy (c->app_id, app_id, app_id_size);
2134 GNUNET_SERVICE_client_continue (client);
2139 * Handle application detach request.
2142 handle_client_app_detach (void *cls,
2143 const struct AppDetachRequest *req)
2145 struct Client *c = cls;
2146 struct GNUNET_SERVICE_Client *client = c->client;
2148 int ret = app_place_remove (c->app_id, &req->ego_pub_key, &req->place_pub_key);
2149 client_send_result (client, req->op_id, ret, NULL, 0);
2151 GNUNET_SERVICE_client_continue (client);
2156 app_places_entry_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
2158 struct Place *plc = cls;
2159 const char *app_id = value;
2160 app_place_remove (app_id, &plc->ego_pub_key, &plc->pub_key);
2166 * Handle application leave request.
2169 handle_client_place_leave (void *cls,
2170 const struct GNUNET_MessageHeader *msg)
2172 struct Client *c = cls;
2173 struct GNUNET_SERVICE_Client *client = c->client;
2174 struct Place *plc = c->place;
2178 GNUNET_SERVICE_client_drop (client);
2182 /* FIXME: remove all app subscriptions and leave this place */
2184 struct GNUNET_CONTAINER_MultiHashMap *
2185 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &plc->pub_key_hash);
2186 if (NULL != place_apps)
2188 GNUNET_CONTAINER_multihashmap_iterate (place_apps, app_places_entry_remove, plc);
2191 /* FIXME: disconnect from the network, but keep local connection for history access */
2193 /* Disconnect all clients connected to the place */
2194 struct ClientListItem *cli = plc->clients_head, *next;
2197 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
2198 GNUNET_SERVICE_client_drop (cli->client);
2204 if (GNUNET_YES != plc->is_disconnected)
2206 plc->is_disconnected = GNUNET_YES;
2207 if (NULL != plc->tmit_msgs_head)
2208 { /* Send pending messages to PSYC before cleanup. */
2209 psyc_transmit_message (plc);
2213 cleanup_place (plc);
2219 struct JoinDecisionClosure
2221 int32_t is_admitted;
2222 struct GNUNET_PSYC_Message *msg;
2227 * Iterator callback for responding to join requests.
2230 psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
2233 struct JoinDecisionClosure *jcls = cls;
2234 struct GNUNET_PSYC_JoinHandle *jh = value;
2235 // FIXME: add relays
2236 GNUNET_PSYC_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
2242 check_client_join_decision (void *cls,
2243 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2250 * Handle an entry decision from a host client.
2253 handle_client_join_decision (void *cls,
2254 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2256 struct Client *c = cls;
2257 struct GNUNET_SERVICE_Client *client = c->client;
2258 struct Place *plc = c->place;
2259 if (NULL == plc || GNUNET_YES != plc->is_host)
2262 GNUNET_SERVICE_client_drop (client);
2265 struct Host *hst = plc->host;
2267 struct JoinDecisionClosure jcls;
2268 jcls.is_admitted = ntohl (dcsn->is_admitted);
2270 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (dcsn->header.size))
2271 ? (struct GNUNET_PSYC_Message *) &dcsn[1]
2274 struct GNUNET_HashCode slave_pub_hash;
2275 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "%p Got join decision (%d) from client for place %s..\n",
2280 hst, jcls.is_admitted, GNUNET_h2s (&plc->pub_key_hash));
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 "%p ..and slave %s.\n",
2283 hst, GNUNET_h2s (&slave_pub_hash));
2285 GNUNET_CONTAINER_multihashmap_get_multiple (hst->join_reqs, &slave_pub_hash,
2286 &psyc_send_join_decision, &jcls);
2287 GNUNET_CONTAINER_multihashmap_remove_all (hst->join_reqs, &slave_pub_hash);
2289 GNUNET_SERVICE_client_continue (client);
2294 * Send acknowledgement to a client.
2296 * Sent after a message fragment has been passed on to multicast.
2298 * @param plc The place struct for the client.
2301 send_message_ack (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2303 struct GNUNET_MessageHeader res;
2304 res.size = htons (sizeof (res));
2305 res.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
2306 client_send_msg (client, &res);
2311 * Proceed to the next message part in the transmission queue.
2314 * Place where the transmission is going on.
2316 * Currently transmitted message.
2318 * Currently transmitted message fragment.
2320 * @return @a tmit_frag, or NULL if reached the end of fragment.
2322 static struct FragmentTransmitQueue *
2323 psyc_transmit_queue_next_part (struct Place *plc,
2324 struct MessageTransmitQueue *tmit_msg,
2325 struct FragmentTransmitQueue *tmit_frag)
2327 uint16_t psize = ntohs (tmit_frag->next_part->size);
2328 if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
2331 tmit_frag->next_part
2332 = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
2334 else /* Reached end of current fragment. */
2336 if (NULL != tmit_frag->client)
2337 send_message_ack (plc, tmit_frag->client);
2338 GNUNET_CONTAINER_DLL_remove (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2339 GNUNET_free (tmit_frag);
2347 * Proceed to next message in transmission queue.
2350 * Place where the transmission is going on.
2352 * Currently transmitted message.
2354 * @return The next message in queue, or NULL if queue is empty.
2356 static struct MessageTransmitQueue *
2357 psyc_transmit_queue_next_msg (struct Place *plc,
2358 struct MessageTransmitQueue *tmit_msg)
2360 GNUNET_CONTAINER_DLL_remove (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2361 GNUNET_free (tmit_msg);
2362 return plc->tmit_msgs_head;
2367 * Callback for data transmission to PSYC.
2370 psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2372 struct Place *plc = cls;
2373 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2374 GNUNET_assert (NULL != tmit_msg);
2375 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2376 if (NULL == tmit_frag)
2377 { /* Rest of the message have not arrived yet, pause transmission */
2381 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 "%p psyc_transmit_notify_data: nothing to send.\n", plc);
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 "%p psyc_transmit_notify_data()\n", plc);
2392 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2394 uint16_t ptype = ntohs (pmsg->type);
2395 uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
2400 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2401 if (*data_size < pdata_size)
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "%p psyc_transmit_notify_data: buffer size too small for data.\n", plc);
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "%p psyc_transmit_notify_data: sending %u bytes.\n",
2412 *data_size = pdata_size;
2413 GNUNET_memcpy (data, &pmsg[1], *data_size);
2417 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2422 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2424 ret = GNUNET_SYSERR;
2428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2429 "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
2431 ret = GNUNET_SYSERR;
2434 if (GNUNET_SYSERR == ret && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL != ptype)
2437 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2438 plc->is_disconnected = GNUNET_YES;
2439 GNUNET_SERVICE_client_drop (tmit_frag->client);
2440 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2445 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2446 if (NULL != tmit_frag)
2448 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2449 ptype = ntohs (pmsg->type);
2452 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2455 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2456 ret = GNUNET_SYSERR;
2461 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2462 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2463 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2467 if (NULL == tmit_msg->frags_head
2468 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2469 { /* Reached end of current message. */
2470 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2474 if (ret != GNUNET_NO)
2476 if (NULL != tmit_msg)
2478 psyc_transmit_message (plc);
2480 else if (GNUNET_YES == plc->is_disconnected)
2482 /* FIXME: handle partial message (when still in_transmit) */
2483 cleanup_place (plc);
2491 * Callback for modifier transmission to PSYC.
2494 psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2495 uint8_t *oper, uint32_t *full_value_size)
2497 struct Place *plc = cls;
2498 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2499 GNUNET_assert (NULL != tmit_msg);
2500 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2501 if (NULL == tmit_frag)
2502 { /* Rest of the message have not arrived yet, pause transmission */
2506 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 "%p psyc_transmit_notify_mod()\n", plc);
2517 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2519 uint16_t ptype = ntohs (pmsg->type);
2524 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
2528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2529 "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
2530 ret = GNUNET_SYSERR;
2533 struct GNUNET_PSYC_MessageModifier *
2534 pmod = (struct GNUNET_PSYC_MessageModifier *) tmit_frag->next_part;
2535 uint16_t mod_size = ntohs (pmod->header.size) - sizeof (*pmod);
2537 if (*data_size < mod_size)
2539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2545 *full_value_size = ntohl (pmod->value_size);
2547 *data_size = mod_size;
2548 GNUNET_memcpy (data, &pmod[1], mod_size);
2553 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
2557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2558 "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
2559 ret = GNUNET_SYSERR;
2562 uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
2563 if (*data_size < mod_size)
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc, mod_size);
2573 *data_size = mod_size;
2574 GNUNET_memcpy (data, &pmsg[1], *data_size);
2579 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2580 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2581 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2588 "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
2590 ret = GNUNET_SYSERR;
2593 if (GNUNET_SYSERR == ret)
2596 ret = GNUNET_SYSERR;
2597 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2598 plc->is_disconnected = GNUNET_YES;
2599 GNUNET_SERVICE_client_drop (tmit_frag->client);
2600 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2604 if (GNUNET_YES != ret)
2605 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2607 if (NULL == tmit_msg->frags_head
2608 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2609 { /* Reached end of current message. */
2610 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2617 * Callback for data transmission from a host to PSYC.
2620 host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2622 int ret = psyc_transmit_notify_data (cls, data_size, data);
2624 if (GNUNET_NO != ret)
2626 struct Host *hst = cls;
2627 hst->tmit_handle = NULL;
2634 * Callback for the transmit functions of multicast.
2637 guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2639 int ret = psyc_transmit_notify_data (cls, data_size, data);
2641 if (GNUNET_NO != ret)
2643 struct Guest *gst = cls;
2644 gst->tmit_handle = NULL;
2651 * Callback for modifier transmission from a host to PSYC.
2654 host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2655 uint8_t *oper, uint32_t *full_value_size)
2657 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2658 oper, full_value_size);
2659 if (GNUNET_SYSERR == ret)
2661 struct Host *hst = cls;
2662 hst->tmit_handle = NULL;
2669 * Callback for modifier transmission from a guest to PSYC.
2672 guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2673 uint8_t *oper, uint32_t *full_value_size)
2675 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2676 oper, full_value_size);
2677 if (GNUNET_SYSERR == ret)
2679 struct Guest *gst = cls;
2680 gst->tmit_handle = NULL;
2687 * Get method part of next message from transmission queue.
2692 * @return #GNUNET_OK on success
2693 * #GNUNET_NO if there are no more messages in queue.
2694 * #GNUNET_SYSERR if the next message is malformed.
2696 static struct GNUNET_PSYC_MessageMethod *
2697 psyc_transmit_queue_next_method (struct Place *plc)
2699 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2700 if (NULL == tmit_msg)
2703 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2704 if (NULL == tmit_frag)
2710 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2712 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type))
2714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2715 "%p psyc_transmit_queue_next_method: unexpected message part of type %u.\n",
2716 plc, NULL != pmsg ? ntohs (pmsg->type) : 0);
2721 uint16_t psize = ntohs (pmsg->size);
2722 struct GNUNET_PSYC_MessageMethod *
2723 pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);
2725 if (psize < sizeof (*pmeth) + 1 || '\0' != *((char *) pmeth + psize - 1))
2727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2728 "%p psyc_transmit_queue_next_method: invalid method name.\n",
2730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2731 "%zu <= %u || NUL != %u\n",
2732 sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
2734 GNUNET_free (pmeth);
2738 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2744 * Transmit the next message in queue from the host to the PSYC channel.
2747 psyc_master_transmit_message (struct Host *hst)
2749 struct Place *plc = &hst->place;
2751 if (NULL == hst->tmit_handle)
2753 struct GNUNET_PSYC_MessageMethod *
2754 pmeth = psyc_transmit_queue_next_method (plc);
2756 return GNUNET_SYSERR;
2758 hst->tmit_handle = (void *) &hst->tmit_handle;
2759 struct GNUNET_PSYC_MasterTransmitHandle *
2760 tmit_handle = GNUNET_PSYC_master_transmit (hst->master, (const char *) &pmeth[1],
2761 &host_transmit_notify_mod,
2762 &host_transmit_notify_data, hst,
2764 if (NULL != hst->tmit_handle)
2765 hst->tmit_handle = tmit_handle;
2766 GNUNET_free (pmeth);
2770 GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
2777 * Transmit the next message in queue from a guest to the PSYC channel.
2780 psyc_slave_transmit_message (struct Guest *gst)
2782 struct Place *plc = &gst->place;
2784 if (NULL == gst->tmit_handle)
2786 struct GNUNET_PSYC_MessageMethod *
2787 pmeth = psyc_transmit_queue_next_method (plc);
2789 return GNUNET_SYSERR;
2791 gst->tmit_handle = (void *) &gst->tmit_handle;
2792 struct GNUNET_PSYC_SlaveTransmitHandle *
2793 tmit_handle = GNUNET_PSYC_slave_transmit (gst->slave, (const char *) &pmeth[1],
2794 &guest_transmit_notify_mod,
2795 &guest_transmit_notify_data, gst,
2797 if (NULL != gst->tmit_handle)
2798 gst->tmit_handle = tmit_handle;
2799 GNUNET_free (pmeth);
2803 GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
2810 * Transmit a message to PSYC.
2813 psyc_transmit_message (struct Place *plc)
2817 ? psyc_master_transmit_message ((struct Host *) plc)
2818 : psyc_slave_transmit_message ((struct Guest *) plc);
2823 * Queue message parts for sending to PSYC.
2825 * @param plc Place to send to.
2826 * @param client Client the message originates from.
2827 * @param data_size Size of @a data.
2828 * @param data Concatenated message parts.
2829 * @param first_ptype First message part type in @a data.
2830 * @param last_ptype Last message part type in @a data.
2832 static struct MessageTransmitQueue *
2833 psyc_transmit_queue_message (struct Place *plc,
2834 struct GNUNET_SERVICE_Client *client,
2837 uint16_t first_ptype, uint16_t last_ptype,
2838 struct MessageTransmitQueue *tmit_msg)
2840 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
2842 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
2843 GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2845 else if (NULL == tmit_msg)
2850 struct FragmentTransmitQueue *
2851 tmit_frag = GNUNET_malloc (sizeof (*tmit_frag) + data_size);
2852 GNUNET_memcpy (&tmit_frag[1], data, data_size);
2853 tmit_frag->next_part = (struct GNUNET_MessageHeader *) &tmit_frag[1];
2854 tmit_frag->client = client;
2855 tmit_frag->size = data_size;
2857 GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2858 tmit_msg->client = client;
2864 * Cancel transmission of current message to PSYC.
2866 * @param plc Place to send to.
2867 * @param client Client the message originates from.
2870 psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2872 uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2874 struct GNUNET_MessageHeader msg;
2875 msg.size = htons (sizeof (msg));
2876 msg.type = htons (type);
2878 psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
2879 psyc_transmit_message (plc);
2881 /* FIXME: cleanup */
2886 check_client_psyc_message (void *cls,
2887 const struct GNUNET_MessageHeader *msg)
2894 * Handle an incoming message from a client, to be transmitted to the place.
2897 handle_client_psyc_message (void *cls,
2898 const struct GNUNET_MessageHeader *msg)
2900 struct Client *c = cls;
2901 struct GNUNET_SERVICE_Client *client = c->client;
2902 struct Place *plc = c->place;
2906 GNUNET_SERVICE_client_drop (client);
2910 int ret = GNUNET_SYSERR;
2912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2913 "%p Received message from client.\n", plc);
2914 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);
2916 if (GNUNET_YES != plc->is_ready)
2918 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2919 "%p Place is not ready yet, disconnecting client.\n", plc);
2921 GNUNET_SERVICE_client_drop (client);
2925 uint16_t size = ntohs (msg->size);
2926 uint16_t psize = size - sizeof (*msg);
2927 if (psize < sizeof (struct GNUNET_MessageHeader)
2928 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < psize)
2930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2931 "%p Received message with invalid payload size (%u) from client.\n",
2934 psyc_transmit_cancel (plc, client);
2935 GNUNET_SERVICE_client_drop (client);
2939 uint16_t first_ptype = 0, last_ptype = 0;
2941 == GNUNET_PSYC_receive_check_parts (psize, (const char *) &msg[1],
2942 &first_ptype, &last_ptype))
2944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2945 "%p Received invalid message part from client.\n", plc);
2947 psyc_transmit_cancel (plc, client);
2948 GNUNET_SERVICE_client_drop (client);
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 "%p Received message with first part type %u and last part type %u.\n",
2953 plc, first_ptype, last_ptype);
2956 = psyc_transmit_queue_message (plc, client, psize, &msg[1],
2957 first_ptype, last_ptype, c->tmit_msg);
2958 if (NULL != c->tmit_msg)
2960 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
2962 ret = psyc_transmit_message (plc);
2965 if (GNUNET_OK != ret)
2967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2968 "%p Received invalid message part from client.\n", plc);
2970 psyc_transmit_cancel (plc, client);
2971 ret = GNUNET_SYSERR;
2974 if (GNUNET_OK == ret)
2975 GNUNET_SERVICE_client_continue (client);
2977 GNUNET_SERVICE_client_drop (client);
2982 * A historic message arrived from PSYC.
2985 psyc_recv_history_message (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
2987 struct OperationClosure *opcls = cls;
2988 struct Client *c = opcls->client;
2989 struct Place *plc = c->place;
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2992 "%p Received historic message #%" PRId64 " (flags: %x)\n",
2993 plc, GNUNET_ntohll (msg->message_id), ntohl (msg->flags));
2995 uint16_t size = ntohs (msg->header.size);
2997 struct GNUNET_OperationResultMessage *
2998 res = GNUNET_malloc (sizeof (*res) + size);
2999 res->header.size = htons (sizeof (*res) + size);
3000 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
3001 res->op_id = opcls->op_id;
3002 res->result_code = GNUNET_htonll (GNUNET_OK);
3004 GNUNET_memcpy (&res[1], msg, size);
3006 /** @todo FIXME: send only to requesting client */
3007 place_send_msg (plc, &res->header);
3014 * Result of message history replay from PSYC.
3017 psyc_recv_history_result (void *cls, int64_t result,
3018 const void *err_msg, uint16_t err_msg_size)
3020 struct OperationClosure *opcls = cls;
3021 struct Client *c = opcls->client;
3022 struct Place *plc = c->place;
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3025 "%p History replay #%" PRIu64 ": "
3026 "PSYCstore returned %" PRId64 " (%.*s)\n",
3027 plc, GNUNET_ntohll (opcls->op_id), result,
3028 err_msg_size, (const char *) err_msg);
3030 // FIXME: place might have been destroyed
3031 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3036 check_client_history_replay (void *cls,
3037 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3044 * Client requests channel history.
3047 handle_client_history_replay (void *cls,
3048 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3050 struct Client *c = cls;
3051 struct GNUNET_SERVICE_Client *client = c->client;
3052 struct Place *plc = c->place;
3056 GNUNET_SERVICE_client_drop (client);
3060 uint16_t size = ntohs (req->header.size);
3061 const char *method_prefix = (const char *) &req[1];
3063 if (size < sizeof (*req) + 1
3064 || '\0' != method_prefix[size - sizeof (*req) - 1])
3066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3067 "%p History replay #%" PRIu64 ": "
3068 "invalid method prefix. size: %u < %zu?\n",
3069 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3071 GNUNET_SERVICE_client_drop (client);
3075 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3077 opcls->op_id = req->op_id;
3078 opcls->flags = ntohl (req->flags);
3080 if (0 == req->message_limit)
3081 GNUNET_PSYC_channel_history_replay (plc->channel,
3082 GNUNET_ntohll (req->start_message_id),
3083 GNUNET_ntohll (req->end_message_id),
3084 method_prefix, opcls->flags,
3085 psyc_recv_history_message, NULL,
3086 psyc_recv_history_result, opcls);
3088 GNUNET_PSYC_channel_history_replay_latest (plc->channel,
3089 GNUNET_ntohll (req->message_limit),
3090 method_prefix, opcls->flags,
3091 psyc_recv_history_message, NULL,
3092 psyc_recv_history_result, opcls);
3094 GNUNET_SERVICE_client_continue (client);
3099 * A state variable part arrived from PSYC.
3102 psyc_recv_state_var (void *cls,
3103 const struct GNUNET_MessageHeader *mod,
3106 uint32_t value_size,
3107 uint32_t full_value_size)
3109 struct OperationClosure *opcls = cls;
3110 struct Client *c = opcls->client;
3111 struct Place *plc = c->place;
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3114 "%p Received state variable %s from PSYC\n",
3117 uint16_t size = ntohs (mod->size);
3119 struct GNUNET_OperationResultMessage *
3120 res = GNUNET_malloc (sizeof (*res) + size);
3121 res->header.size = htons (sizeof (*res) + size);
3122 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
3123 res->op_id = opcls->op_id;
3124 res->result_code = GNUNET_htonll (GNUNET_OK);
3126 GNUNET_memcpy (&res[1], mod, size);
3128 /** @todo FIXME: send only to requesting client */
3129 place_send_msg (plc, &res->header);
3136 * Result of retrieving state variable from PSYC.
3139 psyc_recv_state_result (void *cls, int64_t result,
3140 const void *err_msg, uint16_t err_msg_size)
3142 struct OperationClosure *opcls = cls;
3143 struct Client *c = opcls->client;
3144 struct Place *plc = c->place;
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "%p State get #%" PRIu64 ": "
3148 "PSYCstore returned %" PRId64 " (%.*s)\n",
3149 plc, GNUNET_ntohll (opcls->op_id), result,
3150 err_msg_size, (const char *) err_msg);
3152 // FIXME: place might have been destroyed
3153 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3158 check_client_state_get (void *cls,
3159 const struct GNUNET_PSYC_StateRequestMessage *req)
3166 * Client requests channel history.
3169 handle_client_state_get (void *cls,
3170 const struct GNUNET_PSYC_StateRequestMessage *req)
3172 struct Client *c = cls;
3173 struct GNUNET_SERVICE_Client *client = c->client;
3174 struct Place *plc = c->place;
3178 GNUNET_SERVICE_client_drop (client);
3182 uint16_t size = ntohs (req->header.size);
3183 const char *name = (const char *) &req[1];
3185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3186 "%p State get #%" PRIu64 ": %s\n",
3187 plc, GNUNET_ntohll (req->op_id), name);
3189 if (size < sizeof (*req) + 1
3190 || '\0' != name[size - sizeof (*req) - 1])
3192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3193 "%p State get #%" PRIu64 ": "
3194 "invalid name. size: %u < %zu?\n",
3195 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3197 GNUNET_SERVICE_client_drop (client);
3201 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3203 opcls->op_id = req->op_id;
3205 switch (ntohs (req->header.type))
3207 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET:
3208 GNUNET_PSYC_channel_state_get (plc->channel, name,
3209 psyc_recv_state_var,
3210 psyc_recv_state_result, opcls);
3213 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX:
3214 GNUNET_PSYC_channel_state_get_prefix (plc->channel, name,
3215 psyc_recv_state_var,
3216 psyc_recv_state_result, opcls);
3223 GNUNET_SERVICE_client_continue (client);
3227 #define check_client_state_get_prefix check_client_state_get
3228 #define handle_client_state_get_prefix handle_client_state_get
3232 namestore_recv_records_store_result (void *cls, int32_t result,
3233 const char *err_msg)
3235 struct OperationClosure *opcls = cls;
3236 struct Client *c = opcls->client;
3238 // FIXME: client might have been disconnected
3239 client_send_result (c->client, opcls->op_id, result, err_msg,
3240 (NULL != err_msg) ? strlen (err_msg) : 0);
3241 GNUNET_free (opcls);
3246 check_client_zone_add_place (void *cls,
3247 const struct ZoneAddPlaceRequest *preq)
3254 * Handle request to add PLACE record to GNS zone.
3257 handle_client_zone_add_place (void *cls,
3258 const struct ZoneAddPlaceRequest *preq)
3260 struct Client *c = cls;
3261 struct GNUNET_SERVICE_Client *client = c->client;
3263 uint16_t remaining = ntohs (preq->header.size) - sizeof (*preq);
3264 const char *p = (const char *) &preq[1];
3265 const char *name = NULL, *password = NULL;
3266 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 2,
3268 remaining -= offset;
3270 const struct GNUNET_PeerIdentity *
3271 relays = (const struct GNUNET_PeerIdentity *) p;
3272 uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);
3274 if (0 == offset || remaining != relay_size)
3277 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3278 GNUNET_SERVICE_client_drop (client);
3282 struct GNUNET_GNSRECORD_Data rd = { };
3283 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
3284 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3285 rd.expiration_time = GNUNET_ntohll (preq->expiration_time);
3287 struct GNUNET_GNSRECORD_PlaceData *
3288 rec = GNUNET_malloc (sizeof (*rec) + relay_size);
3289 rec->place_pub_key = preq->place_pub_key;
3290 rec->origin = this_peer;
3291 rec->relay_count = preq->relay_count;
3292 GNUNET_memcpy (&rec[1], relays, relay_size);
3295 rd.data_size = sizeof (*rec) + relay_size;
3297 struct GNUNET_HashCode ego_pub_hash;
3298 GNUNET_CRYPTO_hash (&preq->ego_pub_key, sizeof (preq->ego_pub_key), &ego_pub_hash);
3299 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3302 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3306 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3308 opcls->op_id = preq->op_id;
3309 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3311 namestore_recv_records_store_result, opcls);
3312 /** @todo refresh stored records later */
3314 GNUNET_SERVICE_client_continue (client);
3319 check_client_zone_add_nym (void *cls,
3320 const struct ZoneAddNymRequest *nreq)
3327 * Handle request to add PLACE record to GNS zone.
3330 handle_client_zone_add_nym (void *cls,
3331 const struct ZoneAddNymRequest *nreq)
3333 struct Client *c = cls;
3334 struct GNUNET_SERVICE_Client *client = c->client;
3336 uint16_t name_size = ntohs (nreq->header.size) - sizeof (*nreq);
3337 const char *name = NULL;
3338 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &nreq[1],
3339 name_size, 1, &name);
3340 if (0 == offset || offset != name_size)
3343 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3344 GNUNET_SERVICE_client_continue (client);
3348 struct GNUNET_GNSRECORD_Data rd = { };
3349 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
3350 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3351 rd.expiration_time = GNUNET_ntohll (nreq->expiration_time);
3352 rd.data = &nreq->nym_pub_key;
3353 rd.data_size = sizeof (nreq->nym_pub_key);
3355 struct GNUNET_HashCode ego_pub_hash;
3356 GNUNET_CRYPTO_hash (&nreq->ego_pub_key, sizeof (nreq->ego_pub_key), &ego_pub_hash);
3357 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3360 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3364 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3366 opcls->op_id = nreq->op_id;
3367 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3369 namestore_recv_records_store_result, opcls);
3370 /** @todo refresh stored records later */
3372 GNUNET_SERVICE_client_continue (client);
3377 path_basename (const char *path)
3379 const char *basename = strrchr (path, DIR_SEPARATOR);
3380 if (NULL != basename)
3383 if (NULL == basename || '\0' == basename)
3390 struct PlaceLoadClosure
3393 const char *ego_pub_str;
3397 /** Load a place file */
3399 file_place_load (void *cls, const char *place_filename)
3401 struct PlaceLoadClosure *plcls = cls;
3403 const char *place_pub_str = path_basename (place_filename);
3404 if (NULL == place_pub_str)
3410 char *filename = NULL;
3411 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3412 dir_social, DIR_SEPARATOR,
3413 "places", DIR_SEPARATOR,
3414 plcls->ego_pub_str, DIR_SEPARATOR,
3417 uint64_t file_size = 0;
3419 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3420 || file_size < sizeof (struct PlaceEnterRequest))
3422 GNUNET_free (filename);
3426 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
3427 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
3428 GNUNET_free (filename);
3429 if (read_size < 0 || read_size < sizeof (*ereq))
3435 uint16_t ereq_size = ntohs (ereq->header.size);
3436 if (read_size != ereq_size)
3442 switch (ntohs (ereq->header.type))
3444 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3445 if (ereq_size < sizeof (struct HostEnterRequest))
3450 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3451 host_enter (hreq, NULL);
3454 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3455 if (ereq_size < sizeof (struct GuestEnterRequest))
3460 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3461 guest_enter (greq, NULL);
3469 app_place_add (plcls->app_id, ereq);
3476 * Read @e place_pub_str entries in @a dir_ego
3479 * Data directory of an application ego.
3480 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_str/
3483 scan_app_ego_dir (void *cls, const char *dir_ego)
3485 struct PlaceLoadClosure *plcls = cls;
3486 plcls->ego_pub_str = path_basename (dir_ego);
3488 if (NULL != plcls->ego_pub_str)
3489 GNUNET_DISK_directory_scan (dir_ego, file_place_load, plcls);
3495 * Read @e ego_pub_str entries in @a dir_app
3498 * Data directory of an application.
3499 * $GNUNET_DATA_HOME/social/apps/$app_id/
3502 scan_app_dir (void *cls, const char *dir_app)
3504 if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
3507 struct PlaceLoadClosure plcls;
3508 plcls.app_id = path_basename (dir_app);
3510 if (NULL != plcls.app_id)
3511 GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);
3518 identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
3519 void **ctx, const char *name)
3521 if (NULL == id_ego) // end of initial list of egos
3524 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
3525 GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);
3527 struct GNUNET_HashCode ego_pub_hash;
3528 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
3530 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3533 GNUNET_free (ego->name);
3534 if (NULL == name) // deleted
3536 GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
3543 ego = GNUNET_malloc (sizeof (*ego));
3547 ego->key = *(GNUNET_IDENTITY_ego_get_private_key (id_ego));
3548 size_t name_size = strlen (name) + 1;
3549 ego->name = GNUNET_malloc (name_size);
3550 GNUNET_memcpy (ego->name, name, name_size);
3552 GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
3553 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3556 // FIXME: notify clients about changed ego
3561 * Initialize the PSYC service.
3563 * @param cls Closure.
3564 * @param server The initialized server.
3565 * @param c Configuration to use.
3569 const struct GNUNET_CONFIGURATION_Handle *c,
3570 struct GNUNET_SERVICE_Handle *svc)
3574 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
3576 hosts = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3577 guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3578 place_guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3580 egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3581 apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3582 places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3583 apps_places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3584 places_apps = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3586 id = GNUNET_IDENTITY_connect (cfg, &identity_recv_ego, NULL);
3587 gns = GNUNET_GNS_connect (cfg);
3588 namestore = GNUNET_NAMESTORE_connect (cfg);
3589 stats = GNUNET_STATISTICS_create ("social", cfg);
3592 GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
3595 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3596 "social", "DATA_HOME");
3600 GNUNET_asprintf (&dir_places, "%s%c%s",
3601 dir_social, DIR_SEPARATOR, "places");
3602 GNUNET_asprintf (&dir_apps, "%s%c%s",
3603 dir_social, DIR_SEPARATOR, "apps");
3605 GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);
3607 GNUNET_SCHEDULER_add_shutdown (shutdown_task, NULL);
3612 * Define "main" method using service macro.
3616 GNUNET_SERVICE_OPTION_NONE,
3618 client_notify_connect,
3619 client_notify_disconnect,
3621 GNUNET_MQ_hd_var_size (client_host_enter,
3622 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER,
3623 struct HostEnterRequest,
3625 GNUNET_MQ_hd_var_size (client_guest_enter,
3626 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER,
3627 struct GuestEnterRequest,
3629 GNUNET_MQ_hd_var_size (client_guest_enter_by_name,
3630 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME,
3631 struct GuestEnterByNameRequest,
3633 GNUNET_MQ_hd_var_size (client_join_decision,
3634 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
3635 struct GNUNET_PSYC_JoinDecisionMessage,
3637 GNUNET_MQ_hd_var_size (client_psyc_message,
3638 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
3639 struct GNUNET_MessageHeader,
3641 GNUNET_MQ_hd_var_size (client_history_replay,
3642 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY,
3643 struct GNUNET_PSYC_HistoryRequestMessage,
3645 GNUNET_MQ_hd_var_size (client_state_get,
3646 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
3647 struct GNUNET_PSYC_StateRequestMessage,
3649 GNUNET_MQ_hd_var_size (client_state_get_prefix,
3650 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
3651 struct GNUNET_PSYC_StateRequestMessage,
3653 GNUNET_MQ_hd_var_size (client_zone_add_place,
3654 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE,
3655 struct ZoneAddPlaceRequest,
3657 GNUNET_MQ_hd_var_size (client_zone_add_nym,
3658 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM,
3659 struct ZoneAddNymRequest,
3661 GNUNET_MQ_hd_var_size (client_app_connect,
3662 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT,
3663 struct AppConnectRequest,
3665 GNUNET_MQ_hd_fixed_size (client_app_detach,
3666 GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH,
3667 struct AppDetachRequest,
3669 GNUNET_MQ_hd_fixed_size (client_place_leave,
3670 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE,
3671 struct GNUNET_MessageHeader,
3673 GNUNET_MQ_hd_var_size (client_msg_proc_set,
3674 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET,
3675 struct MsgProcRequest,
3677 GNUNET_MQ_hd_fixed_size (client_msg_proc_clear,
3678 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR,
3679 struct GNUNET_MessageHeader,
3682 /* end of gnunet-service-social.c */