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_core_service.h"
35 #include "gnunet_identity_service.h"
36 #include "gnunet_namestore_service.h"
37 #include "gnunet_gns_service.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_psyc_service.h"
40 #include "gnunet_psyc_util_lib.h"
41 #include "gnunet_social_service.h"
46 * Handle to our current configuration.
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 /* Handles to other services */
51 static struct GNUNET_CORE_Handle *core;
52 static struct GNUNET_IDENTITY_Handle *id;
53 static struct GNUNET_GNS_Handle *gns;
54 static struct GNUNET_NAMESTORE_Handle *namestore;
55 static struct GNUNET_STATISTICS_Handle *stats;
60 static struct GNUNET_PeerIdentity this_peer;
63 * Notification context, simplifies client broadcasts.
65 static struct GNUNET_SERVER_NotificationContext *nc;
68 * All connected hosts.
69 * H(place_pub_key) -> struct Host
71 static struct GNUNET_CONTAINER_MultiHashMap *hosts;
74 * All connected guests.
75 * H(place_pub_key) -> struct Guest
77 static struct GNUNET_CONTAINER_MultiHashMap *guests;
80 * Connected guests per place.
81 * H(place_pub_key) -> ego_pub_key -> struct Guest
83 static struct GNUNET_CONTAINER_MultiHashMap *place_guests;
86 * Places entered as host or guest.
87 * H(place_pub_key) -> struct HostEnterRequest OR struct GuestEnterRequest
89 static struct GNUNET_CONTAINER_MultiHashMap *places;
92 * Places entered per application.
93 * H(app_id) -> H(place_pub_key) -> NULL
95 static struct GNUNET_CONTAINER_MultiHashMap *apps_places;
98 * Application subscriptions per place.
99 * H(place_pub_key) -> H(app_id)
101 static struct GNUNET_CONTAINER_MultiHashMap *places_apps;
104 * Connected applications.
105 * H(app_id) -> struct Application
107 static struct GNUNET_CONTAINER_MultiHashMap *apps;
111 * H(ego_pub_key) -> struct Ego
113 static struct GNUNET_CONTAINER_MultiHashMap *egos;
116 * Directory for storing social data.
117 * Default: $GNUNET_DATA_HOME/social
119 static char *dir_social;
122 * Directory for storing place data.
125 static char *dir_places;
128 * Directory for storing app data.
131 static char *dir_apps;
135 * Message fragment transmission queue.
137 struct FragmentTransmitQueue
139 struct FragmentTransmitQueue *prev;
140 struct FragmentTransmitQueue *next;
142 struct GNUNET_SERVER_Client *client;
145 * Pointer to the next message part inside the data after this struct.
147 struct GNUNET_MessageHeader *next_part;
155 * @see enum GNUNET_PSYC_MessageState
159 /* Followed by one or more message parts. */
164 * Message transmission queue.
166 struct MessageTransmitQueue
168 struct MessageTransmitQueue *prev;
169 struct MessageTransmitQueue *next;
171 struct FragmentTransmitQueue *frags_head;
172 struct FragmentTransmitQueue *frags_tail;
174 struct GNUNET_SERVER_Client *client;
178 * List of connected clients.
180 struct ClientListItem
182 struct ClientListItem *prev;
183 struct ClientListItem *next;
185 struct GNUNET_SERVER_Client *client;
190 * Common part of the client context for both a host and guest.
194 struct ClientListItem *clients_head;
195 struct ClientListItem *clients_tail;
197 struct MessageTransmitQueue *tmit_msgs_head;
198 struct MessageTransmitQueue *tmit_msgs_tail;
200 struct GNUNET_PSYC_Channel *channel;
203 * Private key of home in case of a host.
205 struct GNUNET_CRYPTO_EddsaPublicKey key;
208 * Public key of place.
210 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
213 * Hash of @a pub_key.
215 struct GNUNET_HashCode pub_key_hash;
218 * Private key of ego.
220 struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
225 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
228 * Hash of @a ego_pub_key.
230 struct GNUNET_HashCode ego_pub_hash;
233 * Slicer for processing incoming messages.
235 struct GNUNET_PSYC_Slicer *slicer;
238 * Last message ID received for the place.
239 * 0 if there is no such message.
241 uint64_t max_message_id;
244 * Offset where the file is currently being written.
246 uint64_t file_offset;
249 * Whether or not to save the file (#GNUNET_YES or #GNUNET_NO)
254 * Is this a host (#GNUNET_YES), or guest (#GNUNET_NO)?
259 * Is this place ready to receive messages from client?
260 * #GNUNET_YES or #GNUNET_NO
265 * Is the client disconnected?
266 * #GNUNET_YES or #GNUNET_NO
268 uint8_t is_disconnected;
273 * Client context for a host.
278 * Place struct common for Host and Guest
283 * Handle for the multicast origin.
285 struct GNUNET_PSYC_Master *master;
288 * Transmit handle for multicast.
290 struct GNUNET_PSYC_MasterTransmitHandle *tmit_handle;
293 * Incoming join requests.
294 * guest_key -> struct GNUNET_PSYC_JoinHandle *
296 struct GNUNET_CONTAINER_MultiHashMap *join_reqs;
299 * Messages being relayed.
301 struct GNUNET_CONTAINER_MultiHashMap *relay_msgs;
304 * @see enum GNUNET_PSYC_Policy
306 enum GNUNET_PSYC_Policy policy;
311 * Client context for a guest.
316 * Place struct common for Host and Guest.
321 * Handle for the PSYC slave.
323 struct GNUNET_PSYC_Slave *slave;
326 * Transmit handle for multicast.
328 struct GNUNET_PSYC_SlaveTransmitHandle *tmit_handle;
331 * Peer identity of the origin.
333 struct GNUNET_PeerIdentity origin;
336 * Number of items in @a relays.
338 uint32_t relay_count;
341 * Relays that multicast can use to connect.
343 struct GNUNET_PeerIdentity *relays;
346 * Join request to be transmitted to the master on join.
348 struct GNUNET_MessageHeader *join_req;
351 * Join decision received from PSYC.
353 struct GNUNET_PSYC_JoinDecisionMessage *join_dcsn;
356 * Join flags for the PSYC service.
358 enum GNUNET_PSYC_SlaveJoinFlags join_flags;
363 * Context for a client.
368 * Place where the client entered.
373 * Message queue for the message currently being transmitted
376 struct MessageTransmitQueue *tmit_msg;
379 * ID for application clients.
387 struct ClientListItem *clients_head;
388 struct ClientListItem *clients_tail;
393 struct GNUNET_CRYPTO_EcdsaPrivateKey key;
398 struct OperationClosure
400 struct GNUNET_SERVER_Client *client;
408 psyc_transmit_message (struct Place *plc);
412 * Clean up place data structures after a client disconnected.
414 * @param cls the `struct Place` to clean up
417 cleanup_place (void *cls);
420 static struct MessageTransmitQueue *
421 psyc_transmit_queue_message (struct Place *plc,
422 struct GNUNET_SERVER_Client *client,
425 uint16_t first_ptype, uint16_t last_ptype,
426 struct MessageTransmitQueue *tmit_msg);
430 place_entry_cleanup (void *cls,
431 const struct GNUNET_HashCode *key,
434 struct Place *plc = value;
442 * Task run during shutdown.
447 shutdown_task (void *cls)
449 GNUNET_CONTAINER_multihashmap_iterate (hosts, place_entry_cleanup, NULL);
450 GNUNET_CONTAINER_multihashmap_iterate (guests, place_entry_cleanup, NULL);
454 GNUNET_SERVER_notification_context_destroy (nc);
459 GNUNET_CORE_disconnect (core);
464 GNUNET_IDENTITY_disconnect (id);
467 if (NULL != namestore)
469 GNUNET_NAMESTORE_disconnect (namestore);
474 GNUNET_GNS_disconnect (gns);
479 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
486 * Clean up host data structures after a client disconnected.
489 cleanup_host (struct Host *hst)
491 struct Place *plc = &hst->plc;
493 if (NULL != hst->master)
494 GNUNET_PSYC_master_stop (hst->master, GNUNET_NO, NULL, NULL); // FIXME
495 GNUNET_CONTAINER_multihashmap_destroy (hst->join_reqs);
496 GNUNET_CONTAINER_multihashmap_destroy (hst->relay_msgs);
497 GNUNET_CONTAINER_multihashmap_remove (hosts, &plc->pub_key_hash, plc);
502 * Clean up guest data structures after a client disconnected.
505 cleanup_guest (struct Guest *gst)
507 struct Place *plc = &gst->plc;
508 struct GNUNET_CONTAINER_MultiHashMap *
509 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests,
511 GNUNET_assert (NULL != plc_gst); // FIXME
512 GNUNET_CONTAINER_multihashmap_remove (plc_gst, &plc->ego_pub_hash, gst);
514 if (0 == GNUNET_CONTAINER_multihashmap_size (plc_gst))
516 GNUNET_CONTAINER_multihashmap_remove (place_guests, &plc->pub_key_hash,
518 GNUNET_CONTAINER_multihashmap_destroy (plc_gst);
520 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, gst);
522 if (NULL != gst->join_req)
523 GNUNET_free (gst->join_req);
524 if (NULL != gst->relays)
525 GNUNET_free (gst->relays);
526 if (NULL != gst->slave)
527 GNUNET_PSYC_slave_part (gst->slave, GNUNET_NO, NULL, NULL); // FIXME
528 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, plc);
533 * Clean up place data structures after a client disconnected.
535 * @param cls the `struct Place` to clean up
538 cleanup_place (void *cls)
540 struct Place *plc = cls;
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "%p Cleaning up place %s\n",
544 plc, GNUNET_h2s (&plc->pub_key_hash));
546 (GNUNET_YES == plc->is_host)
547 ? cleanup_host ((struct Host *) plc)
548 : cleanup_guest ((struct Guest *) plc);
550 GNUNET_PSYC_slicer_destroy (plc->slicer);
556 * Called whenever a client is disconnected.
557 * Frees our resources associated with that client.
559 * @param cls Closure.
560 * @param client Identification of the client.
563 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
569 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "%p User context is NULL in client_disconnect()\n", ctx);
577 struct Place *plc = ctx->plc;
579 if (NULL != ctx->app_id)
580 GNUNET_free (ctx->app_id);
585 return; // application client, nothing to do
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "%p Client (%s) disconnected from place %s\n",
589 plc, (GNUNET_YES == plc->is_host) ? "host" : "guest",
590 GNUNET_h2s (&plc->pub_key_hash));
592 struct ClientListItem *cli = plc->clients_head;
595 if (cli->client == client)
597 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
607 * Send message to a client.
610 client_send_msg (struct GNUNET_SERVER_Client *client,
611 const struct GNUNET_MessageHeader *msg)
613 GNUNET_SERVER_notification_context_add (nc, client);
614 GNUNET_SERVER_notification_context_unicast (nc, client, msg, GNUNET_NO);
619 * Send message to all clients connected to a place.
622 place_send_msg (const struct Place *plc,
623 const struct GNUNET_MessageHeader *msg)
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "%p Sending message to clients of place.\n", plc);
628 struct ClientListItem *cli = plc->clients_head;
631 client_send_msg (cli->client, msg);
638 * Send a result code back to the client.
641 * Client that should receive the result code.
645 * Operation ID in network byte order.
647 * Data payload or NULL.
652 client_send_result (struct GNUNET_SERVER_Client *client, uint64_t op_id,
653 int64_t result_code, const void *data, uint16_t data_size)
655 struct GNUNET_OperationResultMessage *res;
657 res = GNUNET_malloc (sizeof (*res) + data_size);
658 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
659 res->header.size = htons (sizeof (*res) + data_size);
660 res->result_code = GNUNET_htonll (result_code);
663 memcpy (&res[1], data, data_size);
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "%p Sending result to client for operation #%" PRIu64 ": "
667 "%" PRId64 " (size: %u)\n",
668 client, GNUNET_ntohll (op_id), result_code, data_size);
670 client_send_msg (client, &res->header);
676 client_send_host_enter_ack (struct GNUNET_SERVER_Client *client,
677 struct Host *hst, uint32_t result)
679 struct Place *plc = &hst->plc;
681 struct HostEnterAck hack;
682 hack.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK);
683 hack.header.size = htons (sizeof (hack));
684 hack.result_code = htonl (result);
685 hack.max_message_id = GNUNET_htonll (plc->max_message_id);
686 hack.place_pub_key = plc->pub_key;
689 client_send_msg (client, &hack.header);
691 place_send_msg (plc, &hack.header);
696 * Called after a PSYC master is started.
699 psyc_master_started (void *cls, int result, uint64_t max_message_id)
701 struct Host *hst = cls;
702 struct Place *plc = &hst->plc;
703 plc->max_message_id = max_message_id;
704 plc->is_ready = GNUNET_YES;
706 client_send_host_enter_ack (NULL, hst, result);
711 * Called when a PSYC master receives a join request.
714 psyc_recv_join_request (void *cls,
715 const struct GNUNET_PSYC_JoinRequestMessage *req,
716 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
717 const struct GNUNET_PSYC_Message *join_msg,
718 struct GNUNET_PSYC_JoinHandle *jh)
720 struct Host *hst = cls;
721 struct GNUNET_HashCode slave_key_hash;
722 GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
723 GNUNET_CONTAINER_multihashmap_put (hst->join_reqs, &slave_key_hash, jh,
724 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
725 place_send_msg (&hst->plc, &req->header);
730 * Called after a PSYC slave is connected.
733 psyc_slave_connected (void *cls, int result, uint64_t max_message_id)
735 struct Guest *gst = cls;
736 struct Place *plc = &gst->plc;
737 plc->max_message_id = max_message_id;
738 plc->is_ready = GNUNET_YES;
740 struct GNUNET_PSYC_CountersResultMessage res;
741 res.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
742 res.header.size = htons (sizeof (res));
743 res.result_code = htonl (result);
744 res.max_message_id = GNUNET_htonll (plc->max_message_id);
746 place_send_msg (plc, &res.header);
751 * Called when a PSYC slave receives a join decision.
754 psyc_recv_join_dcsn (void *cls,
755 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
757 const struct GNUNET_PSYC_Message *join_msg)
759 struct Guest *gst = cls;
760 place_send_msg (&gst->plc, &dcsn->header);
765 * Called when a PSYC master or slave receives a message.
768 psyc_recv_message (void *cls,
769 const struct GNUNET_PSYC_MessageHeader *msg)
771 struct Place *plc = cls;
773 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&msg->slave_pub_key);
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "%p Received PSYC message of size %u from %s.\n",
776 plc, ntohs (msg->header.size), str);
779 GNUNET_PSYC_slicer_message (plc->slicer, msg);
781 place_send_msg (plc, &msg->header);
786 * Relay a message part received from a guest to the the place.
793 * Nym the message is received from.
796 host_relay_message_part (struct Host *hst,
797 const struct GNUNET_MessageHeader *pmsg,
798 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key)
800 /* separate queue per nym */
801 struct GNUNET_HashCode nym_pub_hash;
802 GNUNET_CRYPTO_hash (nym_pub_key, sizeof (*nym_pub_key), &nym_pub_hash);
804 struct MessageTransmitQueue *
805 tmit_msg = GNUNET_CONTAINER_multihashmap_get (hst->relay_msgs, &nym_pub_hash);
807 uint16_t ptype = ntohs (pmsg->type);
809 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
811 /* FIXME: last message was unfinished, cancel & remove from queue */
814 tmit_msg = psyc_transmit_queue_message (&hst->plc, NULL, ntohs (pmsg->size),
815 pmsg, ptype, ptype, tmit_msg);
819 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
820 GNUNET_CONTAINER_multihashmap_put (hst->relay_msgs, &nym_pub_hash, tmit_msg,
821 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
823 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
824 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
825 GNUNET_CONTAINER_multihashmap_remove (hst->relay_msgs, &nym_pub_hash, tmit_msg);
832 * Received a method to be relayed from a guest.
835 place_recv_relay_method (void *cls,
836 const struct GNUNET_PSYC_MessageHeader *msg,
837 const struct GNUNET_PSYC_MessageMethod *meth,
839 const char *method_name)
841 struct Place *plc = cls;
843 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
844 && GNUNET_YES == plc->is_host);
846 struct Host *hst = cls;
847 host_relay_message_part (hst, &meth->header, &msg->slave_pub_key);
853 * Received a modifier to be relayed from a guest.
856 place_recv_relay_modifier (void *cls,
857 const struct GNUNET_PSYC_MessageHeader *msg,
858 const struct GNUNET_MessageHeader *pmsg,
860 enum GNUNET_PSYC_Operator oper,
864 uint16_t full_value_size)
866 struct Place *plc = cls;
868 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
869 && GNUNET_YES == plc->is_host);
871 struct Host *hst = cls;
872 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
877 * Received a data fragment to be relayed from a guest.
880 place_recv_relay_data (void *cls,
881 const struct GNUNET_PSYC_MessageHeader *msg,
882 const struct GNUNET_MessageHeader *pmsg,
887 struct Place *plc = cls;
889 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
890 && GNUNET_YES == plc->is_host);
892 struct Host *hst = cls;
893 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
899 * Received end of message to be relayed from a guest.
902 place_recv_relay_eom (void *cls,
903 const struct GNUNET_PSYC_MessageHeader *msg,
904 const struct GNUNET_MessageHeader *pmsg,
906 uint8_t is_cancelled)
908 struct Place *plc = cls;
910 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
911 && GNUNET_YES == plc->is_host);
913 struct Host *hst = cls;
914 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
920 * Received a method to be saved to disk.
922 * Create a new file for writing the data part of the message into,
923 * if the file does not yet exist.
926 place_recv_save_method (void *cls,
927 const struct GNUNET_PSYC_MessageHeader *msg,
928 const struct GNUNET_PSYC_MessageMethod *meth,
930 const char *method_name)
932 struct Place *plc = cls;
933 plc->file_offset = 0;
934 plc->file_save = GNUNET_NO;
936 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
937 memcpy (&place_pub_hash_ascii.encoding,
938 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
940 char *filename = NULL;
941 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
942 dir_social, DIR_SEPARATOR,
943 "files", DIR_SEPARATOR,
944 place_pub_hash_ascii.encoding, DIR_SEPARATOR,
945 GNUNET_ntohll (msg->message_id));
947 /* save if does not already exist */
948 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
950 if (0 == GNUNET_DISK_fn_write (filename, NULL, 0,
951 GNUNET_DISK_PERM_USER_READ
952 | GNUNET_DISK_PERM_USER_WRITE))
954 plc->file_save = GNUNET_YES;
961 GNUNET_free (filename);
966 * Received a data fragment to be saved to disk.
968 * Append data fragment to the file.
971 place_recv_save_data (void *cls,
972 const struct GNUNET_PSYC_MessageHeader *msg,
973 const struct GNUNET_MessageHeader *pmsg,
978 struct Place *plc = cls;
979 if (GNUNET_YES != plc->file_save)
982 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
983 memcpy (&place_pub_hash_ascii.encoding,
984 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
986 char *filename = NULL;
987 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
988 dir_social, DIR_SEPARATOR,
989 "files", DIR_SEPARATOR,
990 place_pub_hash_ascii.encoding, DIR_SEPARATOR,
991 GNUNET_ntohll (msg->message_id));
992 GNUNET_DISK_directory_create_for_file (filename);
993 struct GNUNET_DISK_FileHandle *
994 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
995 GNUNET_DISK_PERM_NONE);
996 GNUNET_free (filename);
1000 GNUNET_DISK_file_seek (fh, plc->file_offset, GNUNET_DISK_SEEK_SET);
1001 GNUNET_DISK_file_write (fh, data, data_size);
1002 GNUNET_DISK_file_close (fh);
1009 plc->file_offset += data_size;
1014 * Received end of message to be saved to disk.
1016 * Remove .part ending from the filename.
1019 place_recv_save_eom (void *cls,
1020 const struct GNUNET_PSYC_MessageHeader *msg,
1021 const struct GNUNET_MessageHeader *pmsg,
1022 uint64_t message_id,
1023 uint8_t is_cancelled)
1025 struct Place *plc = cls;
1026 if (GNUNET_YES != plc->file_save)
1029 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1030 memcpy (&place_pub_hash_ascii.encoding,
1031 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
1034 GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64,
1035 dir_social, DIR_SEPARATOR,
1036 "files", DIR_SEPARATOR,
1037 place_pub_hash_ascii.encoding, DIR_SEPARATOR,
1038 GNUNET_ntohll (msg->message_id));
1039 char *fn_part = NULL;
1040 GNUNET_asprintf (&fn_part, "%s.part", fn);
1042 rename (fn_part, fn);
1045 GNUNET_free (fn_part);
1050 * Initialize place data structure.
1053 place_init (struct Place *plc)
1055 plc->slicer = GNUNET_PSYC_slicer_create ();
1060 * Add a place to the @e places hash map.
1065 * @return #GNUNET_OK if the place was added
1066 * #GNUNET_NO if the place already exists in the hash map
1067 * #GNUNET_SYSERR on error
1070 place_add (const struct PlaceEnterRequest *ereq)
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "Adding place to hashmap:\n");
1075 struct EgoPlacePublicKey ego_place_pub_key = {
1076 .ego_pub_key = ereq->ego_pub_key,
1077 .place_pub_key = ereq->place_pub_key,
1079 struct GNUNET_HashCode ego_place_pub_hash;
1080 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1085 struct GNUNET_MessageHeader *
1086 place_msg = GNUNET_CONTAINER_multihashmap_get (places, &ego_place_pub_hash);
1087 if (NULL != place_msg)
1090 place_msg = GNUNET_copy_message (&ereq->header);
1091 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places, &ego_place_pub_hash, place_msg,
1092 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1095 GNUNET_free (place_msg);
1096 return GNUNET_SYSERR;
1103 * Add a place to the @e app_places hash map.
1110 * @return #GNUNET_OK if the place was added
1111 * #GNUNET_NO if the place already exists in the hash map
1112 * #GNUNET_SYSERR on error
1115 app_place_add (const char *app_id,
1116 const struct PlaceEnterRequest *ereq)
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Adding app place to hashmap:\n");
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1121 " app_id = %s\n", app_id);
1123 struct GNUNET_HashCode app_id_hash;
1124 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1126 struct EgoPlacePublicKey ego_place_pub_key = {
1127 .ego_pub_key = ereq->ego_pub_key,
1128 .place_pub_key = ereq->place_pub_key,
1130 struct GNUNET_HashCode ego_place_pub_hash;
1131 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1136 struct GNUNET_CONTAINER_MultiHashMap *
1137 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1138 if (NULL == app_places)
1140 app_places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1141 GNUNET_CONTAINER_multihashmap_put (apps_places, &app_id_hash, app_places,
1142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1145 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (app_places, &ego_place_pub_hash))
1148 if (GNUNET_SYSERR == place_add (ereq))
1149 return GNUNET_SYSERR;
1151 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (app_places, &ego_place_pub_hash, NULL,
1152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1155 return GNUNET_SYSERR;
1158 struct GNUNET_HashCode place_pub_hash;
1159 GNUNET_CRYPTO_hash (&ereq->place_pub_key, sizeof (ereq->place_pub_key), &place_pub_hash);
1161 struct GNUNET_CONTAINER_MultiHashMap *
1162 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &place_pub_hash);
1163 if (NULL == place_apps)
1165 place_apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1166 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places_apps, &place_pub_hash, place_apps,
1167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1173 size_t app_id_size = strlen (app_id) + 1;
1174 void *app_id_value = GNUNET_malloc (app_id_size);
1175 memcpy (app_id_value, app_id, app_id_size);
1177 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (place_apps, &app_id_hash, app_id_value,
1178 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1188 * Save place entry message to disk.
1196 app_place_save (const char *app_id,
1197 const struct PlaceEnterRequest *ereq)
1199 app_place_add (app_id, ereq);
1201 if (NULL == dir_places)
1202 return GNUNET_SYSERR;
1204 struct GNUNET_HashCode ego_pub_hash;
1205 struct GNUNET_HashCode place_pub_hash;
1206 GNUNET_CRYPTO_hash (&ereq->ego_pub_key, sizeof (ereq->ego_pub_key),
1208 GNUNET_CRYPTO_hash (&ereq->place_pub_key, sizeof (ereq->place_pub_key),
1211 struct GNUNET_CRYPTO_HashAsciiEncoded ego_pub_hash_ascii;
1212 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1213 memcpy (&ego_pub_hash_ascii.encoding,
1214 GNUNET_h2s_full (&ego_pub_hash), sizeof (ego_pub_hash_ascii));
1215 memcpy (&place_pub_hash_ascii.encoding,
1216 GNUNET_h2s_full (&place_pub_hash), sizeof (place_pub_hash_ascii));
1218 char *filename = NULL;
1219 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
1220 dir_social, DIR_SEPARATOR,
1221 "places", DIR_SEPARATOR,
1222 ego_pub_hash_ascii.encoding, DIR_SEPARATOR,
1223 place_pub_hash_ascii.encoding);
1224 int ret = GNUNET_DISK_directory_create_for_file (filename);
1225 if (GNUNET_OK != ret
1226 || 0 > GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size),
1227 GNUNET_DISK_PERM_USER_READ
1228 | GNUNET_DISK_PERM_USER_WRITE))
1231 ret = GNUNET_SYSERR;
1233 GNUNET_free (filename);
1235 if (ret == GNUNET_OK)
1237 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1238 dir_social, DIR_SEPARATOR,
1239 "apps", DIR_SEPARATOR,
1240 app_id, DIR_SEPARATOR,
1241 ego_pub_hash_ascii.encoding, DIR_SEPARATOR,
1242 place_pub_hash_ascii.encoding);
1243 ret = GNUNET_DISK_directory_create_for_file (filename);
1244 if (GNUNET_OK != ret
1245 || 0 > GNUNET_DISK_fn_write (filename, "", 0,
1246 GNUNET_DISK_PERM_USER_READ
1247 | GNUNET_DISK_PERM_USER_WRITE))
1250 ret = GNUNET_SYSERR;
1252 GNUNET_free (filename);
1259 app_place_remove (const char *app_id,
1260 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1261 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key)
1263 struct GNUNET_HashCode ego_pub_hash;
1264 struct GNUNET_HashCode place_pub_hash;
1265 GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash);
1266 GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash);
1268 struct GNUNET_CRYPTO_HashAsciiEncoded ego_pub_hash_ascii;
1269 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1270 memcpy (&ego_pub_hash_ascii.encoding,
1271 GNUNET_h2s_full (&ego_pub_hash), sizeof (ego_pub_hash_ascii));
1272 memcpy (&place_pub_hash_ascii.encoding,
1273 GNUNET_h2s_full (&place_pub_hash), sizeof (place_pub_hash_ascii));
1275 char *app_place_filename = NULL;
1276 GNUNET_asprintf (&app_place_filename,
1277 "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1278 dir_social, DIR_SEPARATOR,
1279 "apps", DIR_SEPARATOR,
1280 app_id, DIR_SEPARATOR,
1281 ego_pub_hash_ascii.encoding, DIR_SEPARATOR,
1282 place_pub_hash_ascii.encoding);
1284 struct GNUNET_HashCode app_id_hash;
1285 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1287 struct GNUNET_CONTAINER_MultiHashMap *
1288 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1290 if (NULL != app_places)
1291 GNUNET_CONTAINER_multihashmap_remove (app_places, &place_pub_hash, NULL);
1293 struct GNUNET_CONTAINER_MultiHashMap *
1294 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &place_pub_hash);
1295 if (NULL != place_apps)
1297 void *app_id_value = GNUNET_CONTAINER_multihashmap_get (place_apps, &app_id_hash);
1298 if (NULL != app_id_value)
1300 GNUNET_CONTAINER_multihashmap_remove (place_apps, &app_id_hash, app_id_value);
1301 GNUNET_free (app_id_value);
1305 int ret = GNUNET_OK;
1307 if (0 != unlink (app_place_filename))
1310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1311 "Error removing app place file: %s: %s (%d)\n",
1312 app_place_filename, strerror (errno), errno);
1313 ret = GNUNET_SYSERR;
1315 GNUNET_free (app_place_filename);
1322 * Enter place as host.
1326 * @param[out] ret_hst
1327 * Returned Host struct.
1329 * @return #GNUNET_YES if the host entered the place just now,
1330 * #GNUNET_NO if the place is already entered,
1331 * #GNUNET_SYSERR if place_pub_key was set
1332 * but its private key was not found
1335 host_enter (const struct HostEnterRequest *hreq, struct Host **ret_hst)
1337 int ret = GNUNET_NO;
1338 struct GNUNET_HashCode place_pub_hash;
1339 GNUNET_CRYPTO_hash (&hreq->place_pub_key, sizeof (hreq->place_pub_key),
1341 struct Host *hst = GNUNET_CONTAINER_multihashmap_get (hosts, &place_pub_hash);
1345 hst = GNUNET_new (struct Host);
1346 hst->policy = hreq->policy;
1347 hst->join_reqs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1348 hst->relay_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1350 struct Place *plc = &hst->plc;
1352 plc->is_host = GNUNET_YES;
1353 plc->pub_key = hreq->place_pub_key;
1354 plc->pub_key_hash = place_pub_hash;
1356 GNUNET_CONTAINER_multihashmap_put (hosts, &plc->pub_key_hash, plc,
1357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1358 hst->master = GNUNET_PSYC_master_start (cfg, &hreq->place_key, hst->policy,
1359 &psyc_master_started,
1360 &psyc_recv_join_request,
1361 &psyc_recv_message, NULL, hst);
1362 plc->channel = GNUNET_PSYC_master_get_channel (hst->master);
1366 if (NULL != ret_hst)
1372 const struct MsgProcRequest *
1373 msg_proc_parse (const struct GNUNET_MessageHeader *msg,
1375 const char **method_prefix,
1376 struct GNUNET_HashCode *method_hash)
1378 const struct MsgProcRequest *mpreq = (const struct MsgProcRequest *) msg;
1379 uint8_t method_size = ntohs (mpreq->header.size) - sizeof (*mpreq);
1380 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &mpreq[1],
1381 method_size, 1, method_prefix);
1383 if (0 == offset || offset != method_size || *method_prefix == NULL)
1385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1386 "offset = %u, method_size = %u, method_name = %s\n",
1387 offset, method_size, *method_prefix);
1391 GNUNET_CRYPTO_hash (*method_prefix, method_size, method_hash);
1392 *flags = ntohl (mpreq->flags);
1398 * Handle a client setting message proccesing flags for a method prefix.
1401 client_recv_msg_proc_set (void *cls, struct GNUNET_SERVER_Client *client,
1402 const struct GNUNET_MessageHeader *msg)
1405 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
1406 GNUNET_assert (NULL != ctx);
1407 struct Place *plc = ctx->plc;
1409 const char *method_prefix = NULL;
1411 struct GNUNET_HashCode method_hash;
1412 const struct MsgProcRequest *
1413 mpreq = msg_proc_parse (msg, &flags, &method_prefix, &method_hash);
1415 if (NULL == mpreq) {
1417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1421 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1422 place_recv_relay_method,
1423 place_recv_relay_modifier,
1424 place_recv_relay_data,
1425 place_recv_relay_eom);
1426 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1427 place_recv_save_method,
1429 place_recv_save_data,
1430 place_recv_save_eom);
1432 if (flags & GNUNET_SOCIAL_MSG_PROC_RELAY)
1434 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1435 place_recv_relay_method,
1436 place_recv_relay_modifier,
1437 place_recv_relay_data,
1438 place_recv_relay_eom,
1441 if (flags & GNUNET_SOCIAL_MSG_PROC_SAVE)
1443 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1444 place_recv_save_method,
1446 place_recv_save_data,
1447 place_recv_save_eom,
1451 /** @todo Save flags to be able to resume relaying/saving after restart */
1453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1458 * Handle a connecting client requesting to clear all relay rules.
1461 client_recv_msg_proc_clear (void *cls, struct GNUNET_SERVER_Client *client,
1462 const struct GNUNET_MessageHeader *msg)
1465 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
1466 GNUNET_assert (NULL != ctx);
1467 struct Place *plc = ctx->plc;
1468 if (GNUNET_YES != plc->is_host) {
1470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1473 struct Host *hst = (struct Host *) plc;
1475 GNUNET_PSYC_slicer_clear (plc->slicer);
1477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1482 * Handle a connecting client entering a place as host.
1485 client_recv_host_enter (void *cls, struct GNUNET_SERVER_Client *client,
1486 const struct GNUNET_MessageHeader *msg)
1488 struct HostEnterRequest *hreq
1489 = (struct HostEnterRequest *) GNUNET_copy_message (msg);
1491 uint8_t app_id_size = ntohs (hreq->header.size) - sizeof (*hreq);
1492 const char *app_id = NULL;
1493 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &hreq[1],
1494 app_id_size, 1, &app_id);
1495 if (0 == offset || offset != app_id_size || app_id == NULL)
1497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1498 "offset = %u, app_id_size = %u, app_id = %s\n",
1499 offset, app_id_size, app_id);
1501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1505 struct Host *hst = NULL;
1506 struct Place *plc = NULL;
1507 int ret = GNUNET_OK;
1509 struct GNUNET_CRYPTO_EddsaPublicKey empty_pub_key;
1510 memset (&empty_pub_key, 0, sizeof (empty_pub_key));
1512 if (0 == memcmp (&hreq->place_pub_key, &empty_pub_key, sizeof (empty_pub_key)))
1513 { // no public key set: create new private key & save the place
1514 struct GNUNET_CRYPTO_EddsaPrivateKey *
1515 place_key = GNUNET_CRYPTO_eddsa_key_create ();
1516 hreq->place_key = *place_key;
1517 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &hreq->place_pub_key);
1518 GNUNET_CRYPTO_eddsa_key_clear (place_key);
1519 GNUNET_free (place_key);
1521 app_place_save (app_id, (const struct PlaceEnterRequest *) hreq);
1524 switch (host_enter (hreq, &hst))
1533 client_send_host_enter_ack (client, hst, GNUNET_OK);
1537 ret = GNUNET_SYSERR;
1540 if (ret != GNUNET_SYSERR)
1543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1544 "%p Client connected as host to place %s.\n",
1545 hst, GNUNET_h2s (&plc->pub_key_hash));
1547 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1548 cli->client = client;
1549 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1551 struct Client *ctx = GNUNET_new (struct Client);
1553 GNUNET_SERVER_client_set_user_context (client, ctx);
1556 GNUNET_CRYPTO_eddsa_key_clear (&hreq->place_key);
1558 GNUNET_SERVER_receive_done (client, ret);
1563 * Enter place as guest.
1567 * @param[out] ret_gst
1568 * Returned Guest struct.
1570 * @return #GNUNET_YES if the guest entered the place just now,
1571 * #GNUNET_NO if the place is already entered,
1572 * #GNUNET_SYSERR on error.
1575 guest_enter (const struct GuestEnterRequest *greq, struct Guest **ret_gst)
1577 int ret = GNUNET_NO;
1578 uint16_t greq_size = ntohs (greq->header.size);
1580 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key = greq->ego_pub_key;
1581 struct GNUNET_HashCode ego_pub_hash;
1582 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
1583 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
1586 return GNUNET_SYSERR;
1588 struct GNUNET_HashCode place_pub_hash;
1589 GNUNET_CRYPTO_hash (&greq->place_pub_key, sizeof (greq->place_pub_key),
1592 struct GNUNET_CONTAINER_MultiHashMap *
1593 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests, &place_pub_hash);
1594 struct Guest *gst = NULL;
1596 if (NULL != plc_gst)
1597 gst = GNUNET_CONTAINER_multihashmap_get (plc_gst, &ego_pub_hash);
1599 if (NULL == gst || NULL == gst->slave)
1601 gst = GNUNET_new (struct Guest);
1602 gst->origin = greq->origin;
1603 gst->relay_count = ntohl (greq->relay_count);
1606 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1607 const char *app_id = (const char *) &greq[1];
1608 const char *p = app_id;
1610 len = strnlen (app_id, remaining);
1611 if (len == remaining)
1614 return GNUNET_SYSERR;
1617 remaining -= len + 1;
1619 const struct GNUNET_PeerIdentity *relays = NULL;
1620 uint16_t relay_size = gst->relay_count * sizeof (*relays);
1621 if (remaining < relay_size)
1624 return GNUNET_SYSERR;
1627 relays = (const struct GNUNET_PeerIdentity *) p;
1629 remaining -= relay_size;
1631 struct GNUNET_PSYC_Message *join_msg = NULL;
1632 uint16_t join_msg_size = 0;
1634 if (sizeof (struct GNUNET_MessageHeader) <= remaining)
1636 join_msg = (struct GNUNET_PSYC_Message *) p;
1637 join_msg_size = ntohs (join_msg->header.size);
1639 remaining -= join_msg_size;
1643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1644 "%zu + %u + %u != %u\n",
1645 sizeof (*greq), relay_size, join_msg_size, greq_size);
1648 return GNUNET_SYSERR;
1652 gst->relays = GNUNET_malloc (relay_size);
1653 memcpy (gst->relays, relays, relay_size);
1656 gst->join_flags = ntohl (greq->flags);
1658 struct Place *plc = &gst->plc;
1660 plc->is_host = GNUNET_NO;
1661 plc->pub_key = greq->place_pub_key;
1662 plc->pub_key_hash = place_pub_hash;
1663 plc->ego_pub_key = ego_pub_key;
1664 plc->ego_pub_hash = ego_pub_hash;
1665 plc->ego_key = ego->key;
1667 if (NULL == plc_gst)
1669 plc_gst = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1670 (void) GNUNET_CONTAINER_multihashmap_put (place_guests, &plc->pub_key_hash, plc_gst,
1671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1673 (void) GNUNET_CONTAINER_multihashmap_put (plc_gst, &plc->ego_pub_hash, gst,
1674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1675 (void) GNUNET_CONTAINER_multihashmap_put (guests, &plc->pub_key_hash, gst,
1676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1678 = GNUNET_PSYC_slave_join (cfg, &plc->pub_key, &plc->ego_key,
1679 gst->join_flags, &gst->origin,
1680 gst->relay_count, gst->relays,
1681 &psyc_recv_message, NULL,
1682 &psyc_slave_connected,
1683 &psyc_recv_join_dcsn,
1685 plc->channel = GNUNET_PSYC_slave_get_channel (gst->slave);
1689 if (NULL != ret_gst)
1696 * Handle a connecting client entering a place as guest.
1699 client_recv_guest_enter (void *cls, struct GNUNET_SERVER_Client *client,
1700 const struct GNUNET_MessageHeader *msg)
1702 const struct GuestEnterRequest *
1703 greq = (const struct GuestEnterRequest *) msg;
1705 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1706 const char *app_id = NULL;
1707 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &greq[1],
1708 remaining, 1, &app_id);
1712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1716 struct Guest *gst = NULL;
1717 struct Place *plc = NULL;
1719 switch (guest_enter (greq, &gst))
1723 app_place_save (app_id, (const struct PlaceEnterRequest *) greq);
1730 struct GNUNET_PSYC_CountersResultMessage res;
1731 res.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
1732 res.header.size = htons (sizeof (res));
1733 res.result_code = htonl (GNUNET_OK);
1734 res.max_message_id = GNUNET_htonll (plc->max_message_id);
1736 client_send_msg (client, &res.header);
1737 if (NULL != gst->join_dcsn)
1738 client_send_msg (client, &gst->join_dcsn->header);
1744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1749 "%p Client connected as guest to place %s.\n",
1750 gst, GNUNET_h2s (&plc->pub_key_hash));
1752 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1753 cli->client = client;
1754 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1756 struct Client *ctx = GNUNET_new (struct Client);
1758 GNUNET_SERVER_client_set_user_context (client, ctx);
1759 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1763 struct GuestEnterByNameClosure
1765 struct GNUNET_SERVER_Client *client;
1768 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1769 struct GNUNET_MessageHeader *join_msg;
1774 * Result of a GNS name lookup for entering a place.
1776 * @see GNUNET_SOCIAL_guest_enter_by_name
1779 gns_result_guest_enter (void *cls, uint32_t rd_count,
1780 const struct GNUNET_GNSRECORD_Data *rd)
1782 struct GuestEnterByNameClosure *gcls = cls;
1783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1784 "%p GNS result: %u records.\n", gcls->client, rd_count);
1786 const struct GNUNET_GNSRECORD_PlaceData *
1787 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
1789 if (0 == rd_count || rd->data_size < sizeof (*rec))
1792 GNUNET_SERVER_receive_done (gcls->client, GNUNET_SYSERR);
1796 uint16_t relay_count = ntohl (rec->relay_count);
1797 struct GNUNET_PeerIdentity *relays = NULL;
1799 if (0 < relay_count)
1801 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
1803 relays = (struct GNUNET_PeerIdentity *) &rec[1];
1808 GNUNET_break_op (0);
1812 uint16_t app_id_size = strlen (gcls->app_id) + 1;
1813 uint16_t relay_size = relay_count * sizeof (*relays);
1814 uint16_t join_msg_size = 0;
1815 if (NULL != gcls->join_msg)
1816 join_msg_size = ntohs (gcls->join_msg->size);
1817 uint16_t greq_size = sizeof (struct GuestEnterRequest)
1818 + app_id_size + relay_size + join_msg_size;
1819 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1820 greq->header.size = htons (greq_size);
1821 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1822 greq->ego_pub_key = gcls->ego_pub_key;
1823 greq->place_pub_key = rec->place_pub_key;
1824 greq->origin = rec->origin;
1825 greq->relay_count = rec->relay_count;
1828 memcpy (p, gcls->app_id, app_id_size);
1830 memcpy (p, relays, relay_size);
1832 memcpy (p, gcls->join_msg, join_msg_size);
1834 client_recv_guest_enter (NULL, gcls->client, &greq->header);
1836 GNUNET_free (gcls->app_id);
1837 if (NULL != gcls->password)
1838 GNUNET_free (gcls->password);
1839 if (NULL != gcls->join_msg)
1840 GNUNET_free (gcls->join_msg);
1847 * Handle a connecting client entering a place as guest using a GNS address.
1849 * Look up GNS address and generate a GuestEnterRequest from that.
1852 client_recv_guest_enter_by_name (void *cls, struct GNUNET_SERVER_Client *client,
1853 const struct GNUNET_MessageHeader *msg)
1855 const struct GuestEnterByNameRequest *
1856 greq = (const struct GuestEnterByNameRequest *) msg;
1858 struct GuestEnterByNameClosure *gcls = GNUNET_malloc (sizeof (*gcls));
1859 gcls->client = client;
1860 gcls->ego_pub_key = greq->ego_pub_key;
1862 const char *p = (const char *) &greq[1];
1863 const char *app_id = NULL, *password = NULL, *gns_name = NULL;
1864 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1865 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 3,
1870 remaining -= offset;
1872 if (0 != offset && sizeof (*gcls->join_msg) <= remaining)
1874 gcls->join_msg = GNUNET_copy_message ((struct GNUNET_MessageHeader *) p);
1875 remaining -= ntohs (gcls->join_msg->size);
1878 if (0 == offset || 0 != remaining)
1880 if (NULL != gcls->join_msg)
1881 GNUNET_free (gcls->join_msg);
1883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1887 uint16_t app_id_size = strlen (app_id) + 1;
1888 gcls->app_id = GNUNET_malloc (app_id_size);
1889 memcpy (gcls->app_id, app_id, app_id_size);
1891 uint16_t password_size = strlen (password);
1892 if (0 < password_size++)
1894 gcls->password = GNUNET_malloc (password_size);
1895 memcpy (gcls->password, password, password_size);
1898 GNUNET_GNS_lookup (gns, gns_name, &greq->ego_pub_key,
1899 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
1900 NULL, gns_result_guest_enter, gcls);
1905 app_notify_place (struct GNUNET_MessageHeader *msg,
1906 struct GNUNET_SERVER_Client *client)
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "%p Sending place notification of type %u to client.\n",
1910 client, ntohs (msg->type));
1912 uint16_t msg_size = ntohs (msg->size);
1913 struct AppPlaceMessage amsg;
1914 amsg.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
1915 amsg.header.size = htons (sizeof (amsg));
1916 // FIXME: also notify about not entered places
1917 amsg.place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
1919 switch (ntohs (msg->type))
1921 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
1922 if (msg_size < sizeof (struct HostEnterRequest))
1924 struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
1925 amsg.is_host = GNUNET_YES;
1926 amsg.ego_pub_key = hreq->ego_pub_key;
1927 amsg.place_pub_key = hreq->place_pub_key;
1930 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
1931 if (msg_size < sizeof (struct GuestEnterRequest))
1933 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) msg;
1934 amsg.is_host = GNUNET_NO;
1935 amsg.ego_pub_key = greq->ego_pub_key;
1936 amsg.place_pub_key = greq->place_pub_key;
1943 client_send_msg (client, &amsg.header);
1948 app_notify_place_end (struct GNUNET_SERVER_Client *client)
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "%p Sending end of place list notification to client\n",
1954 struct GNUNET_MessageHeader msg;
1955 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END);
1956 msg.size = htons (sizeof (msg));
1958 client_send_msg (client, &msg);
1963 app_notify_ego (struct Ego *ego, struct GNUNET_SERVER_Client *client)
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1966 "%p Sending ego notification to client: %s\n",
1969 size_t name_size = strlen (ego->name) + 1;
1970 struct AppEgoMessage *emsg = GNUNET_malloc (sizeof (*emsg) + name_size);
1971 emsg->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO);
1972 emsg->header.size = htons (sizeof (*emsg) + name_size);
1974 GNUNET_CRYPTO_ecdsa_key_get_public (&ego->key, &emsg->ego_pub_key);
1975 memcpy (&emsg[1], ego->name, name_size);
1977 client_send_msg (client, &emsg->header);
1983 app_notify_ego_end (struct GNUNET_SERVER_Client *client)
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "%p Sending end of ego list notification to client\n",
1989 struct GNUNET_MessageHeader msg;
1990 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END);
1991 msg.size = htons (sizeof (msg));
1993 client_send_msg (client, &msg);
1998 app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key, void *value)
2000 struct GNUNET_MessageHeader *
2001 msg = GNUNET_CONTAINER_multihashmap_get (places, key);
2003 app_notify_place (msg, cls);
2009 ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
2011 app_notify_ego (value, cls);
2017 * Handle application connection.
2020 client_recv_app_connect (void *cls, struct GNUNET_SERVER_Client *client,
2021 const struct GNUNET_MessageHeader *msg)
2023 const struct AppConnectRequest *creq
2024 = (const struct AppConnectRequest *) msg;
2026 uint8_t app_id_size = ntohs (creq->header.size) - sizeof (*creq);
2027 const char *app_id = NULL;
2028 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
2029 app_id_size, 1, &app_id);
2030 if (0 == offset || offset != app_id_size)
2033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2037 struct GNUNET_HashCode app_id_hash;
2038 GNUNET_CRYPTO_hash (app_id, app_id_size, &app_id_hash);
2040 GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
2041 app_notify_ego_end (client);
2043 struct GNUNET_CONTAINER_MultiHashMap *
2044 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
2045 if (NULL != app_places)
2046 GNUNET_CONTAINER_multihashmap_iterate (app_places, app_place_entry_notify, client);
2047 app_notify_place_end (client);
2049 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
2050 cli->client = client;
2051 struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
2054 app = GNUNET_malloc (sizeof (*app));
2055 (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
2056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2058 GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 "%p Application %s connected.\n", app, app_id);
2063 struct Client *ctx = GNUNET_new (struct Client);
2064 ctx->app_id = GNUNET_malloc (app_id_size);
2065 memcpy (ctx->app_id, app_id, app_id_size);
2067 GNUNET_SERVER_client_set_user_context (client, ctx);
2068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2073 * Handle application detach request.
2076 client_recv_app_detach (void *cls, struct GNUNET_SERVER_Client *client,
2077 const struct GNUNET_MessageHeader *msg)
2080 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2081 GNUNET_assert (NULL != ctx);
2083 struct Place *plc = ctx->plc;
2085 const struct AppDetachRequest *req
2086 = (const struct AppDetachRequest *) msg;
2088 int ret = app_place_remove (ctx->app_id, &plc->ego_pub_key, &req->place_pub_key);
2089 client_send_result (client, req->op_id, ret, NULL, 0);
2091 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2096 app_places_entry_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
2098 struct Place *plc = cls;
2099 const char *app_id = value;
2100 app_place_remove (app_id, &plc->ego_pub_key, &plc->pub_key);
2106 * Handle application detach request.
2109 client_recv_place_leave (void *cls, struct GNUNET_SERVER_Client *client,
2110 const struct GNUNET_MessageHeader *msg)
2113 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2114 GNUNET_assert (NULL != ctx);
2115 struct Place *plc = ctx->plc;
2117 /* FIXME: remove all app subscriptions and leave this place */
2119 struct GNUNET_CONTAINER_MultiHashMap *
2120 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &plc->pub_key_hash);
2121 if (NULL != place_apps)
2123 GNUNET_CONTAINER_multihashmap_iterate (place_apps, app_places_entry_remove, plc);
2126 /* FIXME: disconnect from the network, but keep local connection for history access */
2128 /* Disconnect all clients connected to the place */
2129 struct ClientListItem *cli = plc->clients_head, *next;
2132 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
2133 GNUNET_SERVER_client_disconnect (cli->client);
2139 if (GNUNET_YES != plc->is_disconnected)
2141 plc->is_disconnected = GNUNET_YES;
2142 if (NULL != plc->tmit_msgs_head)
2143 { /* Send pending messages to PSYC before cleanup. */
2144 psyc_transmit_message (plc);
2148 cleanup_place (plc);
2154 struct JoinDecisionClosure
2156 int32_t is_admitted;
2157 struct GNUNET_PSYC_Message *msg;
2162 * Iterator callback for responding to join requests.
2165 psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
2168 struct JoinDecisionClosure *jcls = cls;
2169 struct GNUNET_PSYC_JoinHandle *jh = value;
2170 // FIXME: add relays
2171 GNUNET_PSYC_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
2177 * Handle an entry decision from a host client.
2180 client_recv_join_decision (void *cls, struct GNUNET_SERVER_Client *client,
2181 const struct GNUNET_MessageHeader *msg)
2184 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2185 GNUNET_assert (NULL != ctx);
2186 struct Place *plc = ctx->plc;
2187 if (GNUNET_YES != plc->is_host) {
2189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2192 struct Host *hst = (struct Host *) plc;
2194 struct GNUNET_PSYC_JoinDecisionMessage *
2195 dcsn = (struct GNUNET_PSYC_JoinDecisionMessage *) msg;
2196 struct JoinDecisionClosure jcls;
2197 jcls.is_admitted = ntohl (dcsn->is_admitted);
2199 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (msg->size))
2200 ? (struct GNUNET_PSYC_Message *) &dcsn[1]
2203 struct GNUNET_HashCode slave_pub_hash;
2204 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2208 "%p Got join decision (%d) from client for place %s..\n",
2209 hst, jcls.is_admitted, GNUNET_h2s (&plc->pub_key_hash));
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2211 "%p ..and slave %s.\n",
2212 hst, GNUNET_h2s (&slave_pub_hash));
2214 GNUNET_CONTAINER_multihashmap_get_multiple (hst->join_reqs, &slave_pub_hash,
2215 &psyc_send_join_decision, &jcls);
2216 GNUNET_CONTAINER_multihashmap_remove_all (hst->join_reqs, &slave_pub_hash);
2217 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2222 * Send acknowledgement to a client.
2224 * Sent after a message fragment has been passed on to multicast.
2226 * @param plc The place struct for the client.
2229 send_message_ack (struct Place *plc, struct GNUNET_SERVER_Client *client)
2231 struct GNUNET_MessageHeader res;
2232 res.size = htons (sizeof (res));
2233 res.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
2234 client_send_msg (client, &res);
2239 * Proceed to the next message part in the transmission queue.
2242 * Place where the transmission is going on.
2244 * Currently transmitted message.
2246 * Currently transmitted message fragment.
2248 * @return @a tmit_frag, or NULL if reached the end of fragment.
2250 static struct FragmentTransmitQueue *
2251 psyc_transmit_queue_next_part (struct Place *plc,
2252 struct MessageTransmitQueue *tmit_msg,
2253 struct FragmentTransmitQueue *tmit_frag)
2255 uint16_t psize = ntohs (tmit_frag->next_part->size);
2256 if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
2259 tmit_frag->next_part
2260 = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
2262 else /* Reached end of current fragment. */
2264 if (NULL != tmit_frag->client)
2265 send_message_ack (plc, tmit_frag->client);
2266 GNUNET_CONTAINER_DLL_remove (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2267 GNUNET_free (tmit_frag);
2275 * Proceed to next message in transmission queue.
2278 * Place where the transmission is going on.
2280 * Currently transmitted message.
2282 * @return The next message in queue, or NULL if queue is empty.
2284 static struct MessageTransmitQueue *
2285 psyc_transmit_queue_next_msg (struct Place *plc,
2286 struct MessageTransmitQueue *tmit_msg)
2288 GNUNET_CONTAINER_DLL_remove (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2289 GNUNET_free (tmit_msg);
2290 return plc->tmit_msgs_head;
2295 * Callback for data transmission to PSYC.
2298 psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2300 struct Place *plc = cls;
2301 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2302 GNUNET_assert (NULL != tmit_msg);
2303 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2304 if (NULL == tmit_frag)
2305 { /* Rest of the message have not arrived yet, pause transmission */
2309 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 "%p psyc_transmit_notify_data: nothing to send.\n", plc);
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "%p psyc_transmit_notify_data()\n", plc);
2320 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2322 uint16_t ptype = ntohs (pmsg->type);
2323 uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
2328 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2329 if (*data_size < pdata_size)
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 "%p psyc_transmit_notify_data: buffer size too small for data.\n", plc);
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "%p psyc_transmit_notify_data: sending %u bytes.\n",
2340 *data_size = pdata_size;
2341 memcpy (data, &pmsg[1], *data_size);
2345 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2350 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2352 ret = GNUNET_SYSERR;
2356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2357 "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
2359 ret = GNUNET_SYSERR;
2362 if (GNUNET_SYSERR == ret && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL != ptype)
2365 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2366 plc->is_disconnected = GNUNET_YES;
2367 GNUNET_SERVER_client_disconnect (tmit_frag->client);
2368 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2373 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2374 if (NULL != tmit_frag)
2376 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2377 ptype = ntohs (pmsg->type);
2380 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2383 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2384 ret = GNUNET_SYSERR;
2389 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2390 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2391 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2395 if (NULL == tmit_msg->frags_head
2396 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2397 { /* Reached end of current message. */
2398 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2402 if (ret != GNUNET_NO)
2404 if (NULL != tmit_msg)
2406 psyc_transmit_message (plc);
2408 else if (GNUNET_YES == plc->is_disconnected)
2410 /* FIXME: handle partial message (when still in_transmit) */
2411 cleanup_place (plc);
2419 * Callback for modifier transmission to PSYC.
2422 psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2423 uint8_t *oper, uint32_t *full_value_size)
2425 struct Place *plc = cls;
2426 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2427 GNUNET_assert (NULL != tmit_msg);
2428 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2429 if (NULL == tmit_frag)
2430 { /* Rest of the message have not arrived yet, pause transmission */
2434 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2438 "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "%p psyc_transmit_notify_mod()\n", plc);
2445 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2447 uint16_t ptype = ntohs (pmsg->type);
2452 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
2456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2457 "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
2458 ret = GNUNET_SYSERR;
2461 struct GNUNET_PSYC_MessageModifier *
2462 pmod = (struct GNUNET_PSYC_MessageModifier *) tmit_frag->next_part;
2463 uint16_t mod_size = ntohs (pmod->header.size) - sizeof (*pmod);
2465 if (*data_size < mod_size)
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2473 *full_value_size = ntohl (pmod->value_size);
2475 *data_size = mod_size;
2476 memcpy (data, &pmod[1], mod_size);
2481 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
2485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2486 "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
2487 ret = GNUNET_SYSERR;
2490 uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
2491 if (*data_size < mod_size)
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc, mod_size);
2501 *data_size = mod_size;
2502 memcpy (data, &pmsg[1], *data_size);
2507 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2508 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2509 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2516 "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
2518 ret = GNUNET_SYSERR;
2521 if (GNUNET_SYSERR == ret)
2524 ret = GNUNET_SYSERR;
2525 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2526 plc->is_disconnected = GNUNET_YES;
2527 GNUNET_SERVER_client_disconnect (tmit_frag->client);
2528 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2532 if (GNUNET_YES != ret)
2533 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2535 if (NULL == tmit_msg->frags_head
2536 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2537 { /* Reached end of current message. */
2538 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2545 * Callback for data transmission from a host to PSYC.
2548 host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2550 int ret = psyc_transmit_notify_data (cls, data_size, data);
2552 if (GNUNET_NO != ret)
2554 struct Host *hst = cls;
2555 hst->tmit_handle = NULL;
2562 * Callback for the transmit functions of multicast.
2565 guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2567 int ret = psyc_transmit_notify_data (cls, data_size, data);
2569 if (GNUNET_NO != ret)
2571 struct Guest *gst = cls;
2572 gst->tmit_handle = NULL;
2579 * Callback for modifier transmission from a host to PSYC.
2582 host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2583 uint8_t *oper, uint32_t *full_value_size)
2585 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2586 oper, full_value_size);
2587 if (GNUNET_SYSERR == ret)
2589 struct Host *hst = cls;
2590 hst->tmit_handle = NULL;
2597 * Callback for modifier transmission from a guest to PSYC.
2600 guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2601 uint8_t *oper, uint32_t *full_value_size)
2603 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2604 oper, full_value_size);
2605 if (GNUNET_SYSERR == ret)
2607 struct Guest *gst = cls;
2608 gst->tmit_handle = NULL;
2615 * Get method part of next message from transmission queue.
2618 * Next item in message transmission queue.
2620 * The malloc'd message method is returned here.
2622 * @return #GNUNET_OK on success
2623 * #GNUNET_NO if there are no more messages in queue.
2624 * #GNUNET_SYSERR if the next message is malformed.
2626 static struct GNUNET_PSYC_MessageMethod *
2627 psyc_transmit_queue_next_method (struct Place *plc)
2629 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2630 if (NULL == tmit_msg)
2633 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2634 if (NULL == tmit_frag)
2640 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2642 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type))
2644 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2645 "%p psyc_transmit_queue_next_method: unexpected message part of type %u.\n",
2646 plc, NULL != pmsg ? ntohs (pmsg->type) : 0);
2651 uint16_t psize = ntohs (pmsg->size);
2652 struct GNUNET_PSYC_MessageMethod *
2653 pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);
2655 if (psize < sizeof (*pmeth) + 1 || '\0' != *((char *) pmeth + psize - 1))
2657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2658 "%p psyc_transmit_queue_next_method: invalid method name.\n",
2660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2661 "%zu <= %u || NUL != %u\n",
2662 sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
2664 GNUNET_free (pmeth);
2668 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2674 * Transmit the next message in queue from the host to the PSYC channel.
2677 psyc_master_transmit_message (struct Host *hst)
2679 struct Place *plc = &hst->plc;
2681 if (NULL == hst->tmit_handle)
2683 struct GNUNET_PSYC_MessageMethod *
2684 pmeth = psyc_transmit_queue_next_method (plc);
2686 return GNUNET_SYSERR;
2689 = GNUNET_PSYC_master_transmit (hst->master, (const char *) &pmeth[1],
2690 &host_transmit_notify_mod,
2691 &host_transmit_notify_data, hst,
2693 GNUNET_free (pmeth);
2697 GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
2704 * Transmit the next message in queue from a guest to the PSYC channel.
2707 psyc_slave_transmit_message (struct Guest *gst)
2709 struct Place *plc = &gst->plc;
2711 if (NULL == gst->tmit_handle)
2713 struct GNUNET_PSYC_MessageMethod *
2714 pmeth = psyc_transmit_queue_next_method (plc);
2716 return GNUNET_SYSERR;
2719 = GNUNET_PSYC_slave_transmit (gst->slave, (const char *) &pmeth[1],
2720 &guest_transmit_notify_mod,
2721 &guest_transmit_notify_data, gst,
2723 GNUNET_free (pmeth);
2727 GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
2734 * Transmit a message to PSYC.
2737 psyc_transmit_message (struct Place *plc)
2741 ? psyc_master_transmit_message ((struct Host *) plc)
2742 : psyc_slave_transmit_message ((struct Guest *) plc);
2747 * Queue message parts for sending to PSYC.
2749 * @param plc Place to send to.
2750 * @param client Client the message originates from.
2751 * @param data_size Size of @a data.
2752 * @param data Concatenated message parts.
2753 * @param first_ptype First message part type in @a data.
2754 * @param last_ptype Last message part type in @a data.
2756 static struct MessageTransmitQueue *
2757 psyc_transmit_queue_message (struct Place *plc,
2758 struct GNUNET_SERVER_Client *client,
2761 uint16_t first_ptype, uint16_t last_ptype,
2762 struct MessageTransmitQueue *tmit_msg)
2764 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
2766 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
2767 GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2769 else if (NULL == tmit_msg)
2774 struct FragmentTransmitQueue *
2775 tmit_frag = GNUNET_malloc (sizeof (*tmit_frag) + data_size);
2776 memcpy (&tmit_frag[1], data, data_size);
2777 tmit_frag->next_part = (struct GNUNET_MessageHeader *) &tmit_frag[1];
2778 tmit_frag->client = client;
2779 tmit_frag->size = data_size;
2781 GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2782 tmit_msg->client = client;
2788 * Cancel transmission of current message to PSYC.
2790 * @param plc Place to send to.
2791 * @param client Client the message originates from.
2794 psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVER_Client *client)
2796 uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2798 struct GNUNET_MessageHeader msg;
2799 msg.size = htons (sizeof (msg));
2800 msg.type = htons (type);
2802 psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
2803 psyc_transmit_message (plc);
2805 /* FIXME: cleanup */
2810 * Handle an incoming message from a client, to be transmitted to the place.
2813 client_recv_psyc_message (void *cls, struct GNUNET_SERVER_Client *client,
2814 const struct GNUNET_MessageHeader *msg)
2817 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2818 GNUNET_assert (NULL != ctx);
2819 struct Place *plc = ctx->plc;
2820 int ret = GNUNET_SYSERR;
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2823 "%p Received message from client.\n", plc);
2824 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);
2826 if (GNUNET_YES != plc->is_ready)
2828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2829 "%p Place is not ready yet, disconnecting client.\n", plc);
2831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2835 uint16_t size = ntohs (msg->size);
2836 uint16_t psize = size - sizeof (*msg);
2837 if (psize < sizeof (struct GNUNET_MessageHeader)
2838 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < psize)
2840 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2841 "%p Received message with invalid payload size (%u) from client.\n",
2844 psyc_transmit_cancel (plc, client);
2845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2849 uint16_t first_ptype = 0, last_ptype = 0;
2851 == GNUNET_PSYC_receive_check_parts (psize, (const char *) &msg[1],
2852 &first_ptype, &last_ptype))
2854 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2855 "%p Received invalid message part from client.\n", plc);
2857 psyc_transmit_cancel (plc, client);
2858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2862 "%p Received message with first part type %u and last part type %u.\n",
2863 plc, first_ptype, last_ptype);
2866 = psyc_transmit_queue_message (plc, client, psize, &msg[1],
2867 first_ptype, last_ptype, ctx->tmit_msg);
2868 if (NULL != ctx->tmit_msg)
2870 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
2871 ctx->tmit_msg = NULL;
2872 ret = psyc_transmit_message (plc);
2875 if (GNUNET_OK != ret)
2877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2878 "%p Received invalid message part from client.\n", plc);
2880 psyc_transmit_cancel (plc, client);
2881 ret = GNUNET_SYSERR;
2883 GNUNET_SERVER_receive_done (client, ret);
2888 * A historic message arrived from PSYC.
2891 psyc_recv_history_message (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
2893 struct OperationClosure *opcls = cls;
2894 struct Place *plc = opcls->plc;
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2897 "%p Received historic message #%" PRId64 " (flags: %x)\n",
2898 plc, GNUNET_ntohll (msg->message_id), ntohl (msg->flags));
2900 uint16_t size = ntohs (msg->header.size);
2902 struct GNUNET_OperationResultMessage *
2903 res = GNUNET_malloc (sizeof (*res) + size);
2904 res->header.size = htons (sizeof (*res) + size);
2905 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
2906 res->op_id = opcls->op_id;
2907 res->result_code = GNUNET_htonll (GNUNET_OK);
2909 memcpy (&res[1], msg, size);
2911 /** @todo FIXME: send only to requesting client */
2912 place_send_msg (plc, &res->header);
2917 * Result of message history replay from PSYC.
2920 psyc_recv_history_result (void *cls, int64_t result,
2921 const void *err_msg, uint16_t err_msg_size)
2923 struct OperationClosure *opcls = cls;
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2925 "%p History replay #%" PRIu64 ": "
2926 "PSYCstore returned %" PRId64 " (%.*s)\n",
2927 opcls->plc, GNUNET_ntohll (opcls->op_id), result,
2928 err_msg_size, (const char *) err_msg);
2930 // FIXME: place might have been destroyed
2931 client_send_result (opcls->client, opcls->op_id, result, err_msg, err_msg_size);
2936 * Client requests channel history.
2939 client_recv_history_replay (void *cls, struct GNUNET_SERVER_Client *client,
2940 const struct GNUNET_MessageHeader *msg)
2943 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2944 GNUNET_assert (NULL != ctx);
2945 struct Place *plc = ctx->plc;
2947 const struct GNUNET_PSYC_HistoryRequestMessage *
2948 req = (const struct GNUNET_PSYC_HistoryRequestMessage *) msg;
2949 uint16_t size = ntohs (msg->size);
2950 const char *method_prefix = (const char *) &req[1];
2952 if (size < sizeof (*req) + 1
2953 || '\0' != method_prefix[size - sizeof (*req) - 1])
2955 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2956 "%p History replay #%" PRIu64 ": "
2957 "invalid method prefix. size: %u < %zu?\n",
2958 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
2960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2964 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
2965 opcls->client = client;
2967 opcls->op_id = req->op_id;
2968 opcls->flags = ntohl (req->flags);
2970 if (0 == req->message_limit)
2971 GNUNET_PSYC_channel_history_replay (plc->channel,
2972 GNUNET_ntohll (req->start_message_id),
2973 GNUNET_ntohll (req->end_message_id),
2974 method_prefix, opcls->flags,
2975 psyc_recv_history_message, NULL,
2976 psyc_recv_history_result, opcls);
2978 GNUNET_PSYC_channel_history_replay_latest (plc->channel,
2979 GNUNET_ntohll (req->message_limit),
2980 method_prefix, opcls->flags,
2981 psyc_recv_history_message, NULL,
2982 psyc_recv_history_result, opcls);
2984 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2989 * A state variable part arrived from PSYC.
2992 psyc_recv_state_var (void *cls,
2993 const struct GNUNET_MessageHeader *mod,
2996 uint32_t value_size,
2997 uint32_t full_value_size)
2999 struct OperationClosure *opcls = cls;
3000 struct Place *plc = opcls->plc;
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3003 "%p Received state variable %s from PSYC\n",
3006 uint16_t size = ntohs (mod->size);
3008 struct GNUNET_OperationResultMessage *
3009 res = GNUNET_malloc (sizeof (*res) + size);
3010 res->header.size = htons (sizeof (*res) + size);
3011 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
3012 res->op_id = opcls->op_id;
3013 res->result_code = GNUNET_htonll (GNUNET_OK);
3015 memcpy (&res[1], mod, size);
3017 /** @todo FIXME: send only to requesting client */
3018 place_send_msg (plc, &res->header);
3023 * Result of retrieving state variable from PSYC.
3026 psyc_recv_state_result (void *cls, int64_t result,
3027 const void *err_msg, uint16_t err_msg_size)
3029 struct OperationClosure *opcls = cls;
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3031 "%p State get #%" PRIu64 ": "
3032 "PSYCstore returned %" PRId64 " (%.*s)\n",
3033 opcls->plc, GNUNET_ntohll (opcls->op_id), result,
3034 err_msg_size, (const char *) err_msg);
3036 // FIXME: place might have been destroyed
3037 client_send_result (opcls->client, opcls->op_id, result, err_msg, err_msg_size);
3042 * Client requests channel history.
3045 client_recv_state_get (void *cls, struct GNUNET_SERVER_Client *client,
3046 const struct GNUNET_MessageHeader *msg)
3049 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
3050 GNUNET_assert (NULL != ctx);
3051 struct Place *plc = ctx->plc;
3053 const struct GNUNET_PSYC_StateRequestMessage *
3054 req = (const struct GNUNET_PSYC_StateRequestMessage *) msg;
3055 uint16_t size = ntohs (msg->size);
3056 const char *name = (const char *) &req[1];
3058 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3059 "%p State get #%" PRIu64 ": %s\n",
3060 plc, GNUNET_ntohll (req->op_id), name);
3062 if (size < sizeof (*req) + 1
3063 || '\0' != name[size - sizeof (*req) - 1])
3065 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3066 "%p State get #%" PRIu64 ": "
3067 "invalid name. size: %u < %zu?\n",
3068 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3074 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3075 opcls->client = client;
3077 opcls->op_id = req->op_id;
3079 switch (ntohs (msg->type))
3081 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET:
3082 GNUNET_PSYC_channel_state_get (plc->channel, name,
3083 psyc_recv_state_var,
3084 psyc_recv_state_result, opcls);
3087 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX:
3088 GNUNET_PSYC_channel_state_get_prefix (plc->channel, name,
3089 psyc_recv_state_var,
3090 psyc_recv_state_result, opcls);
3097 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3102 namestore_recv_records_store_result (void *cls, int32_t result,
3103 const char *err_msg)
3105 struct OperationClosure *ocls = cls;
3106 client_send_result (ocls->client, ocls->op_id, result, err_msg,
3107 (NULL != err_msg) ? strlen (err_msg) : 0);
3113 * Handle request to add PLACE record to GNS zone.
3116 client_recv_zone_add_place (void *cls, struct GNUNET_SERVER_Client *client,
3117 const struct GNUNET_MessageHeader *msg)
3119 const struct ZoneAddPlaceRequest *preq
3120 = (const struct ZoneAddPlaceRequest *) msg;
3122 uint16_t remaining = ntohs (preq->header.size) - sizeof (*preq);
3123 const char *p = (const char *) &preq[1];
3124 const char *name = NULL, *password = NULL;
3125 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 2,
3127 remaining -= offset;
3129 const struct GNUNET_PeerIdentity *
3130 relays = (const struct GNUNET_PeerIdentity *) p;
3131 uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);
3133 if (0 == offset || remaining != relay_size)
3136 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3141 struct GNUNET_GNSRECORD_Data rd = { };
3142 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
3143 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3144 rd.expiration_time = GNUNET_ntohll (preq->expiration_time);
3146 struct GNUNET_GNSRECORD_PlaceData *
3147 rec = GNUNET_malloc (sizeof (*rec) + relay_size);
3148 rec->place_pub_key = preq->place_pub_key;
3149 rec->origin = this_peer;
3150 rec->relay_count = preq->relay_count;
3151 memcpy (&rec[1], relays, relay_size);
3154 rd.data_size = sizeof (*rec) + relay_size;
3156 struct GNUNET_HashCode ego_pub_hash;
3157 GNUNET_CRYPTO_hash (&preq->ego_pub_key, sizeof (preq->ego_pub_key), &ego_pub_hash);
3158 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3161 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3165 struct OperationClosure *ocls = GNUNET_malloc (sizeof (*ocls));
3166 ocls->client = client;
3167 ocls->op_id = preq->op_id;
3168 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3170 namestore_recv_records_store_result, ocls);
3171 /** @todo refresh stored records later */
3173 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3178 * Handle request to add PLACE record to GNS zone.
3181 client_recv_zone_add_nym (void *cls, struct GNUNET_SERVER_Client *client,
3182 const struct GNUNET_MessageHeader *msg)
3184 const struct ZoneAddNymRequest *nreq
3185 = (const struct ZoneAddNymRequest *) msg;
3187 uint16_t name_size = ntohs (nreq->header.size) - sizeof (*nreq);
3188 const char *name = NULL;
3189 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &nreq[1],
3190 name_size, 1, &name);
3191 if (0 == offset || offset != name_size)
3194 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3195 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3199 struct GNUNET_GNSRECORD_Data rd = { };
3200 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
3201 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3202 rd.expiration_time = GNUNET_ntohll (nreq->expiration_time);
3203 rd.data = &nreq->nym_pub_key;
3204 rd.data_size = sizeof (nreq->nym_pub_key);
3206 struct GNUNET_HashCode ego_pub_hash;
3207 GNUNET_CRYPTO_hash (&nreq->ego_pub_key, sizeof (nreq->ego_pub_key), &ego_pub_hash);
3208 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3211 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3215 struct OperationClosure *ocls = GNUNET_malloc (sizeof (*ocls));
3216 ocls->client = client;
3217 ocls->op_id = nreq->op_id;
3218 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3220 namestore_recv_records_store_result, ocls);
3221 /** @todo refresh stored records later */
3223 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3227 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3228 { client_recv_host_enter, NULL,
3229 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER, 0 },
3231 { client_recv_guest_enter, NULL,
3232 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER, 0 },
3234 { client_recv_guest_enter_by_name, NULL,
3235 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME, 0 },
3237 { client_recv_join_decision, NULL,
3238 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION, 0 },
3240 { client_recv_psyc_message, NULL,
3241 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE, 0 },
3243 { client_recv_history_replay, NULL,
3244 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY, 0 },
3246 { client_recv_state_get, NULL,
3247 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET, 0 },
3249 { client_recv_state_get, NULL,
3250 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX, 0 },
3252 { client_recv_zone_add_place, NULL,
3253 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE, 0 },
3255 { client_recv_zone_add_nym, NULL,
3256 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM, 0 },
3258 { client_recv_app_connect, NULL,
3259 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT, 0 },
3261 { client_recv_app_detach, NULL,
3262 GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH, 0 },
3264 { client_recv_place_leave, NULL,
3265 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE, 0 },
3267 { client_recv_msg_proc_set, NULL,
3268 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET, 0 },
3270 { client_recv_msg_proc_clear, NULL,
3271 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR, 0 },
3273 { NULL, NULL, 0, 0 }
3278 path_basename (const char *path)
3280 const char *basename = strrchr (path, DIR_SEPARATOR);
3281 if (NULL != basename)
3284 if (NULL == basename || '\0' == basename)
3291 struct PlaceLoadClosure
3294 const char *ego_pub_hash_str;
3298 /** Load a place file */
3300 file_place_load (void *cls, const char *filename)
3303 uint64_t file_size = 0;
3305 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3306 || file_size < sizeof (struct HostEnterRequest))
3309 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
3310 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
3311 if (read_size < 0 || read_size < sizeof (*ereq))
3314 uint16_t ereq_size = ntohs (ereq->header.size);
3315 if (read_size != ereq_size)
3318 switch (ntohs (ereq->header.type))
3320 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3321 if (ereq_size < sizeof (struct HostEnterRequest))
3323 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3324 host_enter (hreq, NULL);
3327 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3328 if (ereq_size < sizeof (struct GuestEnterRequest))
3330 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3331 guest_enter (greq, NULL);
3338 app_place_add (app_id, ereq);
3343 /** Load an ego place file */
3345 file_ego_place_load (void *cls, const char *place_filename)
3347 struct PlaceLoadClosure *plcls = cls;
3349 const char *place_pub_hash_str = path_basename (place_filename);
3350 if (NULL == place_pub_hash_str)
3356 char *filename = NULL;
3357 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3358 dir_social, DIR_SEPARATOR,
3359 "places", DIR_SEPARATOR,
3360 plcls->ego_pub_hash_str, DIR_SEPARATOR,
3361 place_pub_hash_str);
3363 struct PlaceEnterRequest ereq[GNUNET_SERVER_MAX_MESSAGE_SIZE];
3365 int read_size = GNUNET_DISK_fn_read (filename, &ereq,
3366 GNUNET_SERVER_MAX_MESSAGE_SIZE);
3367 GNUNET_free (filename);
3369 if (read_size < (ssize_t) sizeof (ereq))
3372 app_place_add (plcls->app_id, ereq);
3378 * Read @e place_pub_hash_str entries in @a dir_ego
3381 * Data directory of an application ego.
3382 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_hash_str/
3385 scan_app_ego_dir (void *cls, const char *dir_ego)
3387 struct PlaceLoadClosure *plcls = cls;
3388 plcls->ego_pub_hash_str = path_basename (dir_ego);
3390 if (NULL != plcls->ego_pub_hash_str)
3391 GNUNET_DISK_directory_scan (dir_ego, file_ego_place_load, plcls);
3397 * Read @e ego_pub_hash_str entries in @a dir_app
3400 * Data directory of an application.
3401 * $GNUNET_DATA_HOME/social/apps/$app_id/
3404 scan_app_dir (void *cls, const char *dir_app)
3406 if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
3409 struct PlaceLoadClosure plcls;
3410 plcls.app_id = path_basename (dir_app);
3412 if (NULL != plcls.app_id)
3413 GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);
3420 identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
3421 void **ctx, const char *name)
3423 if (NULL == id_ego) // end of initial list of egos
3426 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
3427 GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);
3429 struct GNUNET_HashCode ego_pub_hash;
3430 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
3432 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3435 GNUNET_free (ego->name);
3436 if (NULL == name) // deleted
3438 GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
3445 ego = GNUNET_malloc (sizeof (*ego));
3449 ego->key = *(GNUNET_IDENTITY_ego_get_private_key (id_ego));
3450 size_t name_size = strlen (name) + 1;
3451 ego->name = GNUNET_malloc (name_size);
3452 memcpy (ego->name, name, name_size);
3454 GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
3455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3458 // FIXME: notify clients about changed ego
3463 * Connected to core service.
3466 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
3468 this_peer = *my_identity;
3473 * Initialize the PSYC service.
3475 * @param cls Closure.
3476 * @param server The initialized server.
3477 * @param c Configuration to use.
3480 run (void *cls, struct GNUNET_SERVER_Handle *server,
3481 const struct GNUNET_CONFIGURATION_Handle *c)
3485 hosts = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3486 guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3487 place_guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3489 egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3490 apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3491 places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3492 apps_places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3493 places_apps = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3495 core = GNUNET_CORE_connect (cfg, NULL, core_connected, NULL, NULL,
3496 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
3497 id = GNUNET_IDENTITY_connect (cfg, &identity_recv_ego, NULL);
3498 gns = GNUNET_GNS_connect (cfg);
3499 namestore = GNUNET_NAMESTORE_connect (cfg);
3500 stats = GNUNET_STATISTICS_create ("social", cfg);
3503 GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
3506 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3507 "social", "DATA_HOME");
3511 GNUNET_asprintf (&dir_places, "%s%c%s",
3512 dir_social, DIR_SEPARATOR, "places");
3513 GNUNET_asprintf (&dir_apps, "%s%c%s",
3514 dir_social, DIR_SEPARATOR, "apps");
3516 GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);
3518 nc = GNUNET_SERVER_notification_context_create (server, 1);
3519 GNUNET_SERVER_add_handlers (server, handlers);
3520 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
3521 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
3526 * The main function for the service.
3528 * @param argc number of arguments from the command line
3529 * @param argv command line arguments
3530 * @return 0 ok, 1 on error
3533 main (int argc, char *const *argv)
3535 return (GNUNET_OK ==
3536 GNUNET_SERVICE_run (argc, argv, "social",
3537 GNUNET_SERVICE_OPTION_NONE,
3538 &run, NULL)) ? 0 : 1;
3541 /* end of gnunet-service-social.c */