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 cleanup_place (struct Place *plc);
415 static struct MessageTransmitQueue *
416 psyc_transmit_queue_message (struct Place *plc,
417 struct GNUNET_SERVER_Client *client,
420 uint16_t first_ptype, uint16_t last_ptype,
421 struct MessageTransmitQueue *tmit_msg);
425 place_entry_cleanup (void *cls,
426 const struct GNUNET_HashCode *key,
429 cleanup_place (value);
435 * Task run during shutdown.
440 shutdown_task (void *cls)
442 GNUNET_CONTAINER_multihashmap_iterate (hosts, place_entry_cleanup, NULL);
443 GNUNET_CONTAINER_multihashmap_iterate (guests, place_entry_cleanup, NULL);
447 GNUNET_SERVER_notification_context_destroy (nc);
452 GNUNET_CORE_disconnect (core);
457 GNUNET_IDENTITY_disconnect (id);
460 if (NULL != namestore)
462 GNUNET_NAMESTORE_disconnect (namestore);
467 GNUNET_GNS_disconnect (gns);
472 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
479 * Clean up host data structures after a client disconnected.
482 cleanup_host (struct Host *hst)
484 struct Place *plc = &hst->plc;
486 if (NULL != hst->master)
487 GNUNET_PSYC_master_stop (hst->master, GNUNET_NO, NULL, NULL); // FIXME
488 GNUNET_CONTAINER_multihashmap_destroy (hst->join_reqs);
489 GNUNET_CONTAINER_multihashmap_destroy (hst->relay_msgs);
490 GNUNET_CONTAINER_multihashmap_remove (hosts, &plc->pub_key_hash, plc);
495 * Clean up guest data structures after a client disconnected.
498 cleanup_guest (struct Guest *gst)
500 struct Place *plc = &gst->plc;
501 struct GNUNET_CONTAINER_MultiHashMap *
502 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests,
504 GNUNET_assert (NULL != plc_gst); // FIXME
505 GNUNET_CONTAINER_multihashmap_remove (plc_gst, &plc->ego_pub_hash, gst);
507 if (0 == GNUNET_CONTAINER_multihashmap_size (plc_gst))
509 GNUNET_CONTAINER_multihashmap_remove (place_guests, &plc->pub_key_hash,
511 GNUNET_CONTAINER_multihashmap_destroy (plc_gst);
513 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, gst);
515 if (NULL != gst->join_req)
516 GNUNET_free (gst->join_req);
517 if (NULL != gst->relays)
518 GNUNET_free (gst->relays);
519 if (NULL != gst->slave)
520 GNUNET_PSYC_slave_part (gst->slave, GNUNET_NO, NULL, NULL); // FIXME
521 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, plc);
526 * Clean up place data structures after a client disconnected.
529 cleanup_place (struct Place *plc)
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "%p Cleaning up place %s\n",
533 plc, GNUNET_h2s (&plc->pub_key_hash));
535 (GNUNET_YES == plc->is_host)
536 ? cleanup_host ((struct Host *) plc)
537 : cleanup_guest ((struct Guest *) plc);
539 GNUNET_PSYC_slicer_destroy (plc->slicer);
545 schedule_cleanup_place (void *cls)
552 * Called whenever a client is disconnected.
553 * Frees our resources associated with that client.
555 * @param cls Closure.
556 * @param client Identification of the client.
559 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
565 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "%p User context is NULL in client_disconnect()\n", ctx);
573 struct Place *plc = ctx->plc;
575 if (NULL != ctx->app_id)
576 GNUNET_free (ctx->app_id);
581 return; // application client, nothing to do
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "%p Client (%s) disconnected from place %s\n",
585 plc, (GNUNET_YES == plc->is_host) ? "host" : "guest",
586 GNUNET_h2s (&plc->pub_key_hash));
588 struct ClientListItem *cli = plc->clients_head;
591 if (cli->client == client)
593 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
603 * Send message to a client.
606 client_send_msg (struct GNUNET_SERVER_Client *client,
607 const struct GNUNET_MessageHeader *msg)
609 GNUNET_SERVER_notification_context_add (nc, client);
610 GNUNET_SERVER_notification_context_unicast (nc, client, msg, GNUNET_NO);
615 * Send message to all clients connected to a place.
618 place_send_msg (const struct Place *plc,
619 const struct GNUNET_MessageHeader *msg)
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "%p Sending message to clients of place.\n", plc);
624 struct ClientListItem *cli = plc->clients_head;
627 client_send_msg (cli->client, msg);
634 * Send a result code back to the client.
637 * Client that should receive the result code.
641 * Operation ID in network byte order.
643 * Data payload or NULL.
648 client_send_result (struct GNUNET_SERVER_Client *client, uint64_t op_id,
649 int64_t result_code, const void *data, uint16_t data_size)
651 struct GNUNET_OperationResultMessage *res;
653 res = GNUNET_malloc (sizeof (*res) + data_size);
654 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
655 res->header.size = htons (sizeof (*res) + data_size);
656 res->result_code = GNUNET_htonll (result_code);
659 memcpy (&res[1], data, data_size);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662 "%p Sending result to client for operation #%" PRIu64 ": "
663 "%" PRId64 " (size: %u)\n",
664 client, GNUNET_ntohll (op_id), result_code, data_size);
666 client_send_msg (client, &res->header);
672 client_send_host_enter_ack (struct GNUNET_SERVER_Client *client,
673 struct Host *hst, uint32_t result)
675 struct Place *plc = &hst->plc;
677 struct HostEnterAck hack;
678 hack.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK);
679 hack.header.size = htons (sizeof (hack));
680 hack.result_code = htonl (result);
681 hack.max_message_id = GNUNET_htonll (plc->max_message_id);
682 hack.place_pub_key = plc->pub_key;
685 client_send_msg (client, &hack.header);
687 place_send_msg (plc, &hack.header);
692 * Called after a PSYC master is started.
695 psyc_master_started (void *cls, int result, uint64_t max_message_id)
697 struct Host *hst = cls;
698 struct Place *plc = &hst->plc;
699 plc->max_message_id = max_message_id;
700 plc->is_ready = GNUNET_YES;
702 client_send_host_enter_ack (NULL, hst, result);
707 * Called when a PSYC master receives a join request.
710 psyc_recv_join_request (void *cls,
711 const struct GNUNET_PSYC_JoinRequestMessage *req,
712 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
713 const struct GNUNET_PSYC_Message *join_msg,
714 struct GNUNET_PSYC_JoinHandle *jh)
716 struct Host *hst = cls;
717 struct GNUNET_HashCode slave_key_hash;
718 GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
719 GNUNET_CONTAINER_multihashmap_put (hst->join_reqs, &slave_key_hash, jh,
720 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
721 place_send_msg (&hst->plc, &req->header);
726 * Called after a PSYC slave is connected.
729 psyc_slave_connected (void *cls, int result, uint64_t max_message_id)
731 struct Guest *gst = cls;
732 struct Place *plc = &gst->plc;
733 plc->max_message_id = max_message_id;
734 plc->is_ready = GNUNET_YES;
736 struct GNUNET_PSYC_CountersResultMessage res;
737 res.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
738 res.header.size = htons (sizeof (res));
739 res.result_code = htonl (result);
740 res.max_message_id = GNUNET_htonll (plc->max_message_id);
742 place_send_msg (plc, &res.header);
747 * Called when a PSYC slave receives a join decision.
750 psyc_recv_join_dcsn (void *cls,
751 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
753 const struct GNUNET_PSYC_Message *join_msg)
755 struct Guest *gst = cls;
756 place_send_msg (&gst->plc, &dcsn->header);
761 * Called when a PSYC master or slave receives a message.
764 psyc_recv_message (void *cls,
765 const struct GNUNET_PSYC_MessageHeader *msg)
767 struct Place *plc = cls;
769 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&msg->slave_pub_key);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "%p Received PSYC message of size %u from %s.\n",
772 plc, ntohs (msg->header.size), str);
775 GNUNET_PSYC_slicer_message (plc->slicer, msg);
777 place_send_msg (plc, &msg->header);
782 * Relay a message part received from a guest to the the place.
789 * Nym the message is received from.
792 host_relay_message_part (struct Host *hst,
793 const struct GNUNET_MessageHeader *pmsg,
794 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key)
796 /* separate queue per nym */
797 struct GNUNET_HashCode nym_pub_hash;
798 GNUNET_CRYPTO_hash (nym_pub_key, sizeof (*nym_pub_key), &nym_pub_hash);
800 struct MessageTransmitQueue *
801 tmit_msg = GNUNET_CONTAINER_multihashmap_get (hst->relay_msgs, &nym_pub_hash);
803 uint16_t ptype = ntohs (pmsg->type);
805 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
807 /* FIXME: last message was unfinished, cancel & remove from queue */
810 tmit_msg = psyc_transmit_queue_message (&hst->plc, NULL, ntohs (pmsg->size),
811 pmsg, ptype, ptype, tmit_msg);
815 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
816 GNUNET_CONTAINER_multihashmap_put (hst->relay_msgs, &nym_pub_hash, tmit_msg,
817 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
819 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
820 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
821 GNUNET_CONTAINER_multihashmap_remove (hst->relay_msgs, &nym_pub_hash, tmit_msg);
828 * Received a method to be relayed from a guest.
831 place_recv_relay_method (void *cls,
832 const struct GNUNET_PSYC_MessageHeader *msg,
833 const struct GNUNET_PSYC_MessageMethod *meth,
835 const char *method_name)
837 struct Place *plc = cls;
839 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
840 && GNUNET_YES == plc->is_host);
842 struct Host *hst = cls;
843 host_relay_message_part (hst, &meth->header, &msg->slave_pub_key);
849 * Received a modifier to be relayed from a guest.
852 place_recv_relay_modifier (void *cls,
853 const struct GNUNET_PSYC_MessageHeader *msg,
854 const struct GNUNET_MessageHeader *pmsg,
856 enum GNUNET_PSYC_Operator oper,
860 uint16_t full_value_size)
862 struct Place *plc = cls;
864 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
865 && GNUNET_YES == plc->is_host);
867 struct Host *hst = cls;
868 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
873 * Received a data fragment to be relayed from a guest.
876 place_recv_relay_data (void *cls,
877 const struct GNUNET_PSYC_MessageHeader *msg,
878 const struct GNUNET_MessageHeader *pmsg,
883 struct Place *plc = cls;
885 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
886 && GNUNET_YES == plc->is_host);
888 struct Host *hst = cls;
889 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
895 * Received end of message to be relayed from a guest.
898 place_recv_relay_eom (void *cls,
899 const struct GNUNET_PSYC_MessageHeader *msg,
900 const struct GNUNET_MessageHeader *pmsg,
902 uint8_t is_cancelled)
904 struct Place *plc = cls;
906 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
907 && GNUNET_YES == plc->is_host);
909 struct Host *hst = cls;
910 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
916 * Received a method to be saved to disk.
918 * Create a new file for writing the data part of the message into,
919 * if the file does not yet exist.
922 place_recv_save_method (void *cls,
923 const struct GNUNET_PSYC_MessageHeader *msg,
924 const struct GNUNET_PSYC_MessageMethod *meth,
926 const char *method_name)
928 struct Place *plc = cls;
929 plc->file_offset = 0;
930 plc->file_save = GNUNET_NO;
932 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
933 memcpy (&place_pub_hash_ascii.encoding,
934 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
936 char *filename = NULL;
937 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
938 dir_social, DIR_SEPARATOR,
939 "files", DIR_SEPARATOR,
940 place_pub_hash_ascii.encoding, DIR_SEPARATOR,
941 GNUNET_ntohll (msg->message_id));
943 /* save if does not already exist */
944 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
946 if (0 == GNUNET_DISK_fn_write (filename, NULL, 0,
947 GNUNET_DISK_PERM_USER_READ
948 | GNUNET_DISK_PERM_USER_WRITE))
950 plc->file_save = GNUNET_YES;
957 GNUNET_free (filename);
962 * Received a data fragment to be saved to disk.
964 * Append data fragment to the file.
967 place_recv_save_data (void *cls,
968 const struct GNUNET_PSYC_MessageHeader *msg,
969 const struct GNUNET_MessageHeader *pmsg,
974 struct Place *plc = cls;
975 if (GNUNET_YES != plc->file_save)
978 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
979 memcpy (&place_pub_hash_ascii.encoding,
980 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
982 char *filename = NULL;
983 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
984 dir_social, DIR_SEPARATOR,
985 "files", DIR_SEPARATOR,
986 place_pub_hash_ascii.encoding, DIR_SEPARATOR,
987 GNUNET_ntohll (msg->message_id));
988 GNUNET_DISK_directory_create_for_file (filename);
989 struct GNUNET_DISK_FileHandle *
990 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
991 GNUNET_DISK_PERM_NONE);
992 GNUNET_free (filename);
996 GNUNET_DISK_file_seek (fh, plc->file_offset, GNUNET_DISK_SEEK_SET);
997 GNUNET_DISK_file_write (fh, data, data_size);
998 GNUNET_DISK_file_close (fh);
1005 plc->file_offset += data_size;
1010 * Received end of message to be saved to disk.
1012 * Remove .part ending from the filename.
1015 place_recv_save_eom (void *cls,
1016 const struct GNUNET_PSYC_MessageHeader *msg,
1017 const struct GNUNET_MessageHeader *pmsg,
1018 uint64_t message_id,
1019 uint8_t is_cancelled)
1021 struct Place *plc = cls;
1022 if (GNUNET_YES != plc->file_save)
1025 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1026 memcpy (&place_pub_hash_ascii.encoding,
1027 GNUNET_h2s_full (&plc->pub_key_hash), sizeof (place_pub_hash_ascii));
1030 GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64,
1031 dir_social, DIR_SEPARATOR,
1032 "files", DIR_SEPARATOR,
1033 place_pub_hash_ascii.encoding, DIR_SEPARATOR,
1034 GNUNET_ntohll (msg->message_id));
1035 char *fn_part = NULL;
1036 GNUNET_asprintf (&fn_part, "%s.part", fn);
1038 rename (fn_part, fn);
1041 GNUNET_free (fn_part);
1046 * Initialize place data structure.
1049 place_init (struct Place *plc)
1051 plc->slicer = GNUNET_PSYC_slicer_create ();
1056 * Add a place to the @e places hash map.
1061 * @return #GNUNET_OK if the place was added
1062 * #GNUNET_NO if the place already exists in the hash map
1063 * #GNUNET_SYSERR on error
1066 place_add (const struct PlaceEnterRequest *ereq)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Adding place to hashmap:\n");
1071 struct EgoPlacePublicKey ego_place_pub_key = {
1072 .ego_pub_key = ereq->ego_pub_key,
1073 .place_pub_key = ereq->place_pub_key,
1075 struct GNUNET_HashCode ego_place_pub_hash;
1076 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1081 struct GNUNET_MessageHeader *
1082 place_msg = GNUNET_CONTAINER_multihashmap_get (places, &ego_place_pub_hash);
1083 if (NULL != place_msg)
1086 place_msg = GNUNET_copy_message (&ereq->header);
1087 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places, &ego_place_pub_hash, place_msg,
1088 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1091 GNUNET_free (place_msg);
1092 return GNUNET_SYSERR;
1099 * Add a place to the @e app_places hash map.
1106 * @return #GNUNET_OK if the place was added
1107 * #GNUNET_NO if the place already exists in the hash map
1108 * #GNUNET_SYSERR on error
1111 app_place_add (const char *app_id,
1112 const struct PlaceEnterRequest *ereq)
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 "Adding app place to hashmap:\n");
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 " app_id = %s\n", app_id);
1119 struct GNUNET_HashCode app_id_hash;
1120 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1122 struct EgoPlacePublicKey ego_place_pub_key = {
1123 .ego_pub_key = ereq->ego_pub_key,
1124 .place_pub_key = ereq->place_pub_key,
1126 struct GNUNET_HashCode ego_place_pub_hash;
1127 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1132 struct GNUNET_CONTAINER_MultiHashMap *
1133 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1134 if (NULL == app_places)
1136 app_places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1137 GNUNET_CONTAINER_multihashmap_put (apps_places, &app_id_hash, app_places,
1138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1141 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (app_places, &ego_place_pub_hash))
1144 if (GNUNET_SYSERR == place_add (ereq))
1145 return GNUNET_SYSERR;
1147 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (app_places, &ego_place_pub_hash, NULL,
1148 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1151 return GNUNET_SYSERR;
1154 struct GNUNET_HashCode place_pub_hash;
1155 GNUNET_CRYPTO_hash (&ereq->place_pub_key, sizeof (ereq->place_pub_key), &place_pub_hash);
1157 struct GNUNET_CONTAINER_MultiHashMap *
1158 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &place_pub_hash);
1159 if (NULL == place_apps)
1161 place_apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1162 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places_apps, &place_pub_hash, place_apps,
1163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1169 size_t app_id_size = strlen (app_id) + 1;
1170 void *app_id_value = GNUNET_malloc (app_id_size);
1171 memcpy (app_id_value, app_id, app_id_size);
1173 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (place_apps, &app_id_hash, app_id_value,
1174 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1184 * Save place entry message to disk.
1192 app_place_save (const char *app_id,
1193 const struct PlaceEnterRequest *ereq)
1195 app_place_add (app_id, ereq);
1197 if (NULL == dir_places)
1198 return GNUNET_SYSERR;
1200 struct GNUNET_HashCode ego_pub_hash;
1201 struct GNUNET_HashCode place_pub_hash;
1202 GNUNET_CRYPTO_hash (&ereq->ego_pub_key, sizeof (ereq->ego_pub_key),
1204 GNUNET_CRYPTO_hash (&ereq->place_pub_key, sizeof (ereq->place_pub_key),
1207 struct GNUNET_CRYPTO_HashAsciiEncoded ego_pub_hash_ascii;
1208 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1209 memcpy (&ego_pub_hash_ascii.encoding,
1210 GNUNET_h2s_full (&ego_pub_hash), sizeof (ego_pub_hash_ascii));
1211 memcpy (&place_pub_hash_ascii.encoding,
1212 GNUNET_h2s_full (&place_pub_hash), sizeof (place_pub_hash_ascii));
1214 char *filename = NULL;
1215 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
1216 dir_social, DIR_SEPARATOR,
1217 "places", DIR_SEPARATOR,
1218 ego_pub_hash_ascii.encoding, DIR_SEPARATOR,
1219 place_pub_hash_ascii.encoding);
1220 int ret = GNUNET_DISK_directory_create_for_file (filename);
1221 if (GNUNET_OK != ret
1222 || 0 > GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size),
1223 GNUNET_DISK_PERM_USER_READ
1224 | GNUNET_DISK_PERM_USER_WRITE))
1227 ret = GNUNET_SYSERR;
1229 GNUNET_free (filename);
1231 if (ret == GNUNET_OK)
1233 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1234 dir_social, DIR_SEPARATOR,
1235 "apps", DIR_SEPARATOR,
1236 app_id, DIR_SEPARATOR,
1237 ego_pub_hash_ascii.encoding, DIR_SEPARATOR,
1238 place_pub_hash_ascii.encoding);
1239 ret = GNUNET_DISK_directory_create_for_file (filename);
1240 if (GNUNET_OK != ret
1241 || 0 > GNUNET_DISK_fn_write (filename, "", 0,
1242 GNUNET_DISK_PERM_USER_READ
1243 | GNUNET_DISK_PERM_USER_WRITE))
1246 ret = GNUNET_SYSERR;
1248 GNUNET_free (filename);
1255 app_place_remove (const char *app_id,
1256 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1257 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key)
1259 struct GNUNET_HashCode ego_pub_hash;
1260 struct GNUNET_HashCode place_pub_hash;
1261 GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash);
1262 GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash);
1264 struct GNUNET_CRYPTO_HashAsciiEncoded ego_pub_hash_ascii;
1265 struct GNUNET_CRYPTO_HashAsciiEncoded place_pub_hash_ascii;
1266 memcpy (&ego_pub_hash_ascii.encoding,
1267 GNUNET_h2s_full (&ego_pub_hash), sizeof (ego_pub_hash_ascii));
1268 memcpy (&place_pub_hash_ascii.encoding,
1269 GNUNET_h2s_full (&place_pub_hash), sizeof (place_pub_hash_ascii));
1271 char *app_place_filename = NULL;
1272 GNUNET_asprintf (&app_place_filename,
1273 "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1274 dir_social, DIR_SEPARATOR,
1275 "apps", DIR_SEPARATOR,
1276 app_id, DIR_SEPARATOR,
1277 ego_pub_hash_ascii.encoding, DIR_SEPARATOR,
1278 place_pub_hash_ascii.encoding);
1280 struct GNUNET_HashCode app_id_hash;
1281 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1283 struct GNUNET_CONTAINER_MultiHashMap *
1284 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1286 if (NULL != app_places)
1287 GNUNET_CONTAINER_multihashmap_remove (app_places, &place_pub_hash, NULL);
1289 struct GNUNET_CONTAINER_MultiHashMap *
1290 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &place_pub_hash);
1291 if (NULL != place_apps)
1293 void *app_id_value = GNUNET_CONTAINER_multihashmap_get (place_apps, &app_id_hash);
1294 if (NULL != app_id_value)
1296 GNUNET_CONTAINER_multihashmap_remove (place_apps, &app_id_hash, app_id_value);
1297 GNUNET_free (app_id_value);
1301 int ret = GNUNET_OK;
1303 if (0 != unlink (app_place_filename))
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 "Error removing app place file: %s: %s\n",
1308 app_place_filename, strerror (errno), errno);
1309 ret = GNUNET_SYSERR;
1311 GNUNET_free (app_place_filename);
1318 * Enter place as host.
1322 * @param[out] ret_hst
1323 * Returned Host struct.
1325 * @return #GNUNET_YES if the host entered the place just now,
1326 * #GNUNET_NO if the place is already entered,
1327 * #GNUNET_SYSERR if place_pub_key was set
1328 * but its private key was not found
1331 host_enter (const struct HostEnterRequest *hreq, struct Host **ret_hst)
1333 int ret = GNUNET_NO;
1334 struct GNUNET_HashCode place_pub_hash;
1335 GNUNET_CRYPTO_hash (&hreq->place_pub_key, sizeof (hreq->place_pub_key),
1337 struct Host *hst = GNUNET_CONTAINER_multihashmap_get (hosts, &place_pub_hash);
1341 hst = GNUNET_new (struct Host);
1342 hst->policy = hreq->policy;
1343 hst->join_reqs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1344 hst->relay_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1346 struct Place *plc = &hst->plc;
1348 plc->is_host = GNUNET_YES;
1349 plc->pub_key = hreq->place_pub_key;
1350 plc->pub_key_hash = place_pub_hash;
1352 GNUNET_CONTAINER_multihashmap_put (hosts, &plc->pub_key_hash, plc,
1353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1354 hst->master = GNUNET_PSYC_master_start (cfg, &hreq->place_key, hst->policy,
1355 &psyc_master_started,
1356 &psyc_recv_join_request,
1357 &psyc_recv_message, NULL, hst);
1358 plc->channel = GNUNET_PSYC_master_get_channel (hst->master);
1362 if (NULL != ret_hst)
1368 const struct MsgProcRequest *
1369 msg_proc_parse (const struct GNUNET_MessageHeader *msg,
1371 const char **method_prefix,
1372 struct GNUNET_HashCode *method_hash)
1374 const struct MsgProcRequest *mpreq = (const struct MsgProcRequest *) msg;
1375 uint8_t method_size = ntohs (mpreq->header.size) - sizeof (*mpreq);
1376 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &mpreq[1],
1377 method_size, 1, method_prefix);
1379 if (0 == offset || offset != method_size || *method_prefix == NULL)
1381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1382 "offset = %u, method_size = %u, method_name = %s\n",
1383 offset, method_size, *method_prefix);
1387 GNUNET_CRYPTO_hash (*method_prefix, method_size, method_hash);
1388 *flags = ntohl (mpreq->flags);
1394 * Handle a client setting message proccesing flags for a method prefix.
1397 client_recv_msg_proc_set (void *cls, struct GNUNET_SERVER_Client *client,
1398 const struct GNUNET_MessageHeader *msg)
1401 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
1402 GNUNET_assert (NULL != ctx);
1403 struct Place *plc = ctx->plc;
1405 const char *method_prefix = NULL;
1407 struct GNUNET_HashCode method_hash;
1408 const struct MsgProcRequest *
1409 mpreq = msg_proc_parse (msg, &flags, &method_prefix, &method_hash);
1411 if (NULL == mpreq) {
1413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1417 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1418 place_recv_relay_method,
1419 place_recv_relay_modifier,
1420 place_recv_relay_data,
1421 place_recv_relay_eom);
1422 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1423 place_recv_save_method,
1425 place_recv_save_data,
1426 place_recv_save_eom);
1428 if (flags & GNUNET_SOCIAL_MSG_PROC_RELAY)
1430 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1431 place_recv_relay_method,
1432 place_recv_relay_modifier,
1433 place_recv_relay_data,
1434 place_recv_relay_eom,
1437 if (flags & GNUNET_SOCIAL_MSG_PROC_SAVE)
1439 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1440 place_recv_save_method,
1442 place_recv_save_data,
1443 place_recv_save_eom,
1447 /** @todo Save flags to be able to resume relaying/saving after restart */
1449 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1454 * Handle a connecting client requesting to clear all relay rules.
1457 client_recv_msg_proc_clear (void *cls, struct GNUNET_SERVER_Client *client,
1458 const struct GNUNET_MessageHeader *msg)
1461 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
1462 GNUNET_assert (NULL != ctx);
1463 struct Place *plc = ctx->plc;
1464 if (GNUNET_YES != plc->is_host) {
1466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1469 struct Host *hst = (struct Host *) plc;
1471 GNUNET_PSYC_slicer_clear (plc->slicer);
1473 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1478 * Handle a connecting client entering a place as host.
1481 client_recv_host_enter (void *cls, struct GNUNET_SERVER_Client *client,
1482 const struct GNUNET_MessageHeader *msg)
1484 struct HostEnterRequest *hreq
1485 = (struct HostEnterRequest *) GNUNET_copy_message (msg);
1487 uint8_t app_id_size = ntohs (hreq->header.size) - sizeof (*hreq);
1488 const char *app_id = NULL;
1489 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &hreq[1],
1490 app_id_size, 1, &app_id);
1491 if (0 == offset || offset != app_id_size || app_id == NULL)
1493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1494 "offset = %u, app_id_size = %u, app_id = %s\n",
1495 offset, app_id_size, app_id);
1497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1501 struct Host *hst = NULL;
1502 struct Place *plc = NULL;
1503 int ret = GNUNET_OK;
1505 struct GNUNET_CRYPTO_EddsaPublicKey empty_pub_key;
1506 memset (&empty_pub_key, 0, sizeof (empty_pub_key));
1508 if (0 == memcmp (&hreq->place_pub_key, &empty_pub_key, sizeof (empty_pub_key)))
1509 { // no public key set: create new private key & save the place
1510 struct GNUNET_CRYPTO_EddsaPrivateKey *
1511 place_key = GNUNET_CRYPTO_eddsa_key_create ();
1512 hreq->place_key = *place_key;
1513 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &hreq->place_pub_key);
1514 GNUNET_CRYPTO_eddsa_key_clear (place_key);
1515 GNUNET_free (place_key);
1517 app_place_save (app_id, (const struct PlaceEnterRequest *) hreq);
1520 switch (host_enter (hreq, &hst))
1529 client_send_host_enter_ack (client, hst, GNUNET_OK);
1533 ret = GNUNET_SYSERR;
1536 if (ret != GNUNET_SYSERR)
1539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1540 "%p Client connected as host to place %s.\n",
1541 hst, GNUNET_h2s (&plc->pub_key_hash));
1543 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1544 cli->client = client;
1545 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1547 struct Client *ctx = GNUNET_new (struct Client);
1549 GNUNET_SERVER_client_set_user_context (client, ctx);
1552 GNUNET_CRYPTO_eddsa_key_clear (&hreq->place_key);
1554 GNUNET_SERVER_receive_done (client, ret);
1559 * Enter place as guest.
1563 * @param[out] ret_gst
1564 * Returned Guest struct.
1566 * @return #GNUNET_YES if the guest entered the place just now,
1567 * #GNUNET_NO if the place is already entered,
1568 * #GNUNET_SYSERR on error.
1571 guest_enter (const struct GuestEnterRequest *greq, struct Guest **ret_gst)
1573 int ret = GNUNET_NO;
1574 uint16_t greq_size = ntohs (greq->header.size);
1576 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key = greq->ego_pub_key;
1577 struct GNUNET_HashCode ego_pub_hash;
1578 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
1579 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
1582 return GNUNET_SYSERR;
1584 struct GNUNET_HashCode place_pub_hash;
1585 GNUNET_CRYPTO_hash (&greq->place_pub_key, sizeof (greq->place_pub_key),
1588 struct GNUNET_CONTAINER_MultiHashMap *
1589 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests, &place_pub_hash);
1590 struct Guest *gst = NULL;
1592 if (NULL != plc_gst)
1593 gst = GNUNET_CONTAINER_multihashmap_get (plc_gst, &ego_pub_hash);
1595 if (NULL == gst || NULL == gst->slave)
1597 gst = GNUNET_new (struct Guest);
1598 gst->origin = greq->origin;
1599 gst->relay_count = ntohl (greq->relay_count);
1602 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1603 const char *app_id = (const char *) &greq[1];
1604 const char *p = app_id;
1606 len = strnlen (app_id, remaining);
1607 if (len == remaining)
1610 return GNUNET_SYSERR;
1613 remaining -= len + 1;
1615 const struct GNUNET_PeerIdentity *relays = NULL;
1616 uint16_t relay_size = gst->relay_count * sizeof (*relays);
1617 if (remaining < relay_size)
1620 return GNUNET_SYSERR;
1623 relays = (const struct GNUNET_PeerIdentity *) p;
1625 remaining -= relay_size;
1627 struct GNUNET_PSYC_Message *join_msg = NULL;
1628 uint16_t join_msg_size = 0;
1630 if (sizeof (struct GNUNET_MessageHeader) <= remaining)
1632 join_msg = (struct GNUNET_PSYC_Message *) p;
1633 join_msg_size = ntohs (join_msg->header.size);
1635 remaining -= join_msg_size;
1639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1640 "%u + %u + %u != %u\n",
1641 sizeof (*greq), relay_size, join_msg_size, greq_size);
1644 return GNUNET_SYSERR;
1648 gst->relays = GNUNET_malloc (relay_size);
1649 memcpy (gst->relays, relays, relay_size);
1652 gst->join_flags = ntohl (greq->flags);
1654 struct Place *plc = &gst->plc;
1656 plc->is_host = GNUNET_NO;
1657 plc->pub_key = greq->place_pub_key;
1658 plc->pub_key_hash = place_pub_hash;
1659 plc->ego_pub_key = ego_pub_key;
1660 plc->ego_pub_hash = ego_pub_hash;
1661 plc->ego_key = ego->key;
1663 if (NULL == plc_gst)
1665 plc_gst = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1666 (void) GNUNET_CONTAINER_multihashmap_put (place_guests, &plc->pub_key_hash, plc_gst,
1667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1669 (void) GNUNET_CONTAINER_multihashmap_put (plc_gst, &plc->ego_pub_hash, gst,
1670 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1671 (void) GNUNET_CONTAINER_multihashmap_put (guests, &plc->pub_key_hash, gst,
1672 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1674 = GNUNET_PSYC_slave_join (cfg, &plc->pub_key, &plc->ego_key,
1675 gst->join_flags, &gst->origin,
1676 gst->relay_count, gst->relays,
1677 &psyc_recv_message, NULL,
1678 &psyc_slave_connected,
1679 &psyc_recv_join_dcsn,
1681 plc->channel = GNUNET_PSYC_slave_get_channel (gst->slave);
1685 if (NULL != ret_gst)
1692 * Handle a connecting client entering a place as guest.
1695 client_recv_guest_enter (void *cls, struct GNUNET_SERVER_Client *client,
1696 const struct GNUNET_MessageHeader *msg)
1698 const struct GuestEnterRequest *
1699 greq = (const struct GuestEnterRequest *) msg;
1701 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1702 const char *app_id = NULL;
1703 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &greq[1],
1704 remaining, 1, &app_id);
1708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1712 struct Guest *gst = NULL;
1713 struct Place *plc = NULL;
1715 switch (guest_enter (greq, &gst))
1719 app_place_save (app_id, (const struct PlaceEnterRequest *) greq);
1726 struct GNUNET_PSYC_CountersResultMessage res;
1727 res.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
1728 res.header.size = htons (sizeof (res));
1729 res.result_code = htonl (GNUNET_OK);
1730 res.max_message_id = GNUNET_htonll (plc->max_message_id);
1732 client_send_msg (client, &res.header);
1733 if (NULL != gst->join_dcsn)
1734 client_send_msg (client, &gst->join_dcsn->header);
1740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "%p Client connected as guest to place %s.\n",
1746 gst, GNUNET_h2s (&plc->pub_key_hash));
1748 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1749 cli->client = client;
1750 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1752 struct Client *ctx = GNUNET_new (struct Client);
1754 GNUNET_SERVER_client_set_user_context (client, ctx);
1755 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1759 struct GuestEnterByNameClosure
1761 struct GNUNET_SERVER_Client *client;
1764 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1765 struct GNUNET_MessageHeader *join_msg;
1770 * Result of a GNS name lookup for entering a place.
1772 * @see GNUNET_SOCIAL_guest_enter_by_name
1775 gns_result_guest_enter (void *cls, uint32_t rd_count,
1776 const struct GNUNET_GNSRECORD_Data *rd)
1778 struct GuestEnterByNameClosure *gcls = cls;
1779 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1780 "%p GNS result: %u records.\n", gcls->client, rd_count);
1782 const struct GNUNET_GNSRECORD_PlaceData *
1783 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
1785 if (0 == rd_count || rd->data_size < sizeof (*rec))
1788 GNUNET_SERVER_receive_done (gcls->client, GNUNET_SYSERR);
1792 uint16_t relay_count = ntohl (rec->relay_count);
1793 struct GNUNET_PeerIdentity *relays = NULL;
1795 if (0 < relay_count)
1797 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
1799 relays = (struct GNUNET_PeerIdentity *) &rec[1];
1804 GNUNET_break_op (0);
1808 uint16_t app_id_size = strlen (gcls->app_id) + 1;
1809 uint16_t relay_size = relay_count * sizeof (*relays);
1810 uint16_t join_msg_size = 0;
1811 if (NULL != gcls->join_msg)
1812 join_msg_size = ntohs (gcls->join_msg->size);
1813 uint16_t greq_size = sizeof (struct GuestEnterRequest)
1814 + app_id_size + relay_size + join_msg_size;
1815 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1816 greq->header.size = htons (greq_size);
1817 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1818 greq->ego_pub_key = gcls->ego_pub_key;
1819 greq->place_pub_key = rec->place_pub_key;
1820 greq->origin = rec->origin;
1821 greq->relay_count = rec->relay_count;
1824 memcpy (p, gcls->app_id, app_id_size);
1826 memcpy (p, relays, relay_size);
1828 memcpy (p, gcls->join_msg, join_msg_size);
1830 client_recv_guest_enter (NULL, gcls->client, &greq->header);
1832 GNUNET_free (gcls->app_id);
1833 if (NULL != gcls->password)
1834 GNUNET_free (gcls->password);
1835 if (NULL != gcls->join_msg)
1836 GNUNET_free (gcls->join_msg);
1843 * Handle a connecting client entering a place as guest using a GNS address.
1845 * Look up GNS address and generate a GuestEnterRequest from that.
1848 client_recv_guest_enter_by_name (void *cls, struct GNUNET_SERVER_Client *client,
1849 const struct GNUNET_MessageHeader *msg)
1851 const struct GuestEnterByNameRequest *
1852 greq = (const struct GuestEnterByNameRequest *) msg;
1854 struct GuestEnterByNameClosure *gcls = GNUNET_malloc (sizeof (*gcls));
1855 gcls->client = client;
1856 gcls->ego_pub_key = greq->ego_pub_key;
1858 const char *p = (const char *) &greq[1];
1859 const char *app_id = NULL, *password = NULL, *gns_name = NULL;
1860 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1861 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 3,
1866 remaining -= offset;
1868 if (0 != offset && sizeof (*gcls->join_msg) <= remaining)
1870 gcls->join_msg = GNUNET_copy_message ((struct GNUNET_MessageHeader *) p);
1871 remaining -= ntohs (gcls->join_msg->size);
1874 if (0 == offset || 0 != remaining)
1876 if (NULL != gcls->join_msg)
1877 GNUNET_free (gcls->join_msg);
1879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1883 uint16_t app_id_size = strlen (app_id) + 1;
1884 gcls->app_id = GNUNET_malloc (app_id_size);
1885 memcpy (gcls->app_id, app_id, app_id_size);
1887 uint16_t password_size = strlen (password);
1888 if (0 < password_size++)
1890 gcls->password = GNUNET_malloc (password_size);
1891 memcpy (gcls->password, password, password_size);
1894 GNUNET_GNS_lookup (gns, gns_name, &greq->ego_pub_key,
1895 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
1896 NULL, gns_result_guest_enter, gcls);
1901 app_notify_place (struct GNUNET_MessageHeader *msg,
1902 struct GNUNET_SERVER_Client *client)
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "%p Sending place notification of type %u to client.\n",
1906 client, ntohs (msg->type));
1908 uint16_t msg_size = ntohs (msg->size);
1909 struct AppPlaceMessage amsg;
1910 amsg.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
1911 amsg.header.size = htons (sizeof (amsg));
1912 // FIXME: also notify about not entered places
1913 amsg.place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
1915 switch (ntohs (msg->type))
1917 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
1918 if (msg_size < sizeof (struct HostEnterRequest))
1920 struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
1921 amsg.is_host = GNUNET_YES;
1922 amsg.ego_pub_key = hreq->ego_pub_key;
1923 amsg.place_pub_key = hreq->place_pub_key;
1926 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
1927 if (msg_size < sizeof (struct GuestEnterRequest))
1929 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) msg;
1930 amsg.is_host = GNUNET_NO;
1931 amsg.ego_pub_key = greq->ego_pub_key;
1932 amsg.place_pub_key = greq->place_pub_key;
1939 client_send_msg (client, &amsg.header);
1944 app_notify_ego (struct Ego *ego, struct GNUNET_SERVER_Client *client)
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "%p Sending ego notification to client: %s\n",
1950 size_t name_size = strlen (ego->name) + 1;
1951 struct AppEgoMessage *emsg = GNUNET_malloc (sizeof (*emsg) + name_size);
1952 emsg->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO);
1953 emsg->header.size = htons (sizeof (*emsg) + name_size);
1955 GNUNET_CRYPTO_ecdsa_key_get_public (&ego->key, &emsg->ego_pub_key);
1956 memcpy (&emsg[1], ego->name, name_size);
1958 client_send_msg (client, &emsg->header);
1964 app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key, void *value)
1966 struct GNUNET_MessageHeader *
1967 msg = GNUNET_CONTAINER_multihashmap_get (places, key);
1969 app_notify_place (msg, cls);
1975 ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
1977 app_notify_ego (value, cls);
1983 * Handle application connection.
1986 client_recv_app_connect (void *cls, struct GNUNET_SERVER_Client *client,
1987 const struct GNUNET_MessageHeader *msg)
1989 const struct AppConnectRequest *creq
1990 = (const struct AppConnectRequest *) msg;
1992 uint8_t app_id_size = ntohs (creq->header.size) - sizeof (*creq);
1993 const char *app_id = NULL;
1994 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
1995 app_id_size, 1, &app_id);
1996 if (0 == offset || offset != app_id_size)
1999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2003 struct GNUNET_HashCode app_id_hash;
2004 GNUNET_CRYPTO_hash (app_id, app_id_size, &app_id_hash);
2006 GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
2008 struct GNUNET_CONTAINER_MultiHashMap *
2009 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
2010 if (NULL != app_places)
2011 GNUNET_CONTAINER_multihashmap_iterate (app_places, app_place_entry_notify, client);
2013 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
2014 cli->client = client;
2015 struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
2018 app = GNUNET_malloc (sizeof (*app));
2019 (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
2020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2022 GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "%p Application %s connected.\n", app, app_id);
2027 struct Client *ctx = GNUNET_new (struct Client);
2028 ctx->app_id = GNUNET_malloc (app_id_size);
2029 memcpy (ctx->app_id, app_id, app_id_size);
2031 GNUNET_SERVER_client_set_user_context (client, ctx);
2032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2037 * Handle application detach request.
2040 client_recv_app_detach (void *cls, struct GNUNET_SERVER_Client *client,
2041 const struct GNUNET_MessageHeader *msg)
2044 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2045 GNUNET_assert (NULL != ctx);
2047 struct Place *plc = ctx->plc;
2049 const struct AppDetachRequest *req
2050 = (const struct AppDetachRequest *) msg;
2052 int ret = app_place_remove (ctx->app_id, &plc->ego_pub_key, &req->place_pub_key);
2053 client_send_result (client, req->op_id, ret, NULL, 0);
2055 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2060 app_places_entry_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
2062 struct Place *plc = cls;
2063 const char *app_id = value;
2064 app_place_remove (app_id, &plc->ego_pub_key, &plc->pub_key);
2070 * Handle application detach request.
2073 client_recv_place_leave (void *cls, struct GNUNET_SERVER_Client *client,
2074 const struct GNUNET_MessageHeader *msg)
2077 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2078 GNUNET_assert (NULL != ctx);
2079 struct Place *plc = ctx->plc;
2081 /* FIXME: remove all app subscriptions and leave this place */
2083 struct GNUNET_CONTAINER_MultiHashMap *
2084 place_apps = GNUNET_CONTAINER_multihashmap_get (places_apps, &plc->pub_key_hash);
2085 if (NULL != place_apps)
2087 GNUNET_CONTAINER_multihashmap_iterate (place_apps, app_places_entry_remove, plc);
2090 /* FIXME: disconnect from the network, but keep local connection for history access */
2092 /* Disconnect all clients connected to the place */
2093 struct ClientListItem *cli = plc->clients_head, *next;
2096 GNUNET_CONTAINER_DLL_remove (plc->clients_head, plc->clients_tail, cli);
2097 GNUNET_SERVER_client_disconnect (cli->client);
2103 if (GNUNET_YES != plc->is_disconnected)
2105 plc->is_disconnected = GNUNET_YES;
2106 if (NULL != plc->tmit_msgs_head)
2107 { /* Send pending messages to PSYC before cleanup. */
2108 psyc_transmit_message (plc);
2112 cleanup_place (plc);
2118 struct JoinDecisionClosure
2120 int32_t is_admitted;
2121 struct GNUNET_PSYC_Message *msg;
2126 * Iterator callback for responding to join requests.
2129 psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
2132 struct JoinDecisionClosure *jcls = cls;
2133 struct GNUNET_PSYC_JoinHandle *jh = value;
2134 // FIXME: add relays
2135 GNUNET_PSYC_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
2141 * Handle an entry decision from a host client.
2144 client_recv_join_decision (void *cls, struct GNUNET_SERVER_Client *client,
2145 const struct GNUNET_MessageHeader *msg)
2148 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2149 GNUNET_assert (NULL != ctx);
2150 struct Place *plc = ctx->plc;
2151 if (GNUNET_YES != plc->is_host) {
2153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2156 struct Host *hst = (struct Host *) plc;
2158 struct GNUNET_PSYC_JoinDecisionMessage *
2159 dcsn = (struct GNUNET_PSYC_JoinDecisionMessage *) msg;
2160 struct JoinDecisionClosure jcls;
2161 jcls.is_admitted = ntohl (dcsn->is_admitted);
2163 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (msg->size))
2164 ? (struct GNUNET_PSYC_Message *) &dcsn[1]
2167 struct GNUNET_HashCode slave_pub_hash;
2168 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "%p Got join decision (%d) from client for place %s..\n",
2173 hst, jcls.is_admitted, GNUNET_h2s (&plc->pub_key_hash));
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "%p ..and slave %s.\n",
2176 hst, GNUNET_h2s (&slave_pub_hash));
2178 GNUNET_CONTAINER_multihashmap_get_multiple (hst->join_reqs, &slave_pub_hash,
2179 &psyc_send_join_decision, &jcls);
2180 GNUNET_CONTAINER_multihashmap_remove_all (hst->join_reqs, &slave_pub_hash);
2181 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2186 * Send acknowledgement to a client.
2188 * Sent after a message fragment has been passed on to multicast.
2190 * @param plc The place struct for the client.
2193 send_message_ack (struct Place *plc, struct GNUNET_SERVER_Client *client)
2195 struct GNUNET_MessageHeader res;
2196 res.size = htons (sizeof (res));
2197 res.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
2198 client_send_msg (client, &res);
2203 * Proceed to the next message part in the transmission queue.
2206 * Place where the transmission is going on.
2208 * Currently transmitted message.
2210 * Currently transmitted message fragment.
2212 * @return @a tmit_frag, or NULL if reached the end of fragment.
2214 static struct FragmentTransmitQueue *
2215 psyc_transmit_queue_next_part (struct Place *plc,
2216 struct MessageTransmitQueue *tmit_msg,
2217 struct FragmentTransmitQueue *tmit_frag)
2219 uint16_t psize = ntohs (tmit_frag->next_part->size);
2220 if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
2223 tmit_frag->next_part
2224 = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
2226 else /* Reached end of current fragment. */
2228 if (NULL != tmit_frag->client)
2229 send_message_ack (plc, tmit_frag->client);
2230 GNUNET_CONTAINER_DLL_remove (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2231 GNUNET_free (tmit_frag);
2239 * Proceed to next message in transmission queue.
2242 * Place where the transmission is going on.
2244 * Currently transmitted message.
2246 * @return The next message in queue, or NULL if queue is empty.
2248 static struct MessageTransmitQueue *
2249 psyc_transmit_queue_next_msg (struct Place *plc,
2250 struct MessageTransmitQueue *tmit_msg)
2252 GNUNET_CONTAINER_DLL_remove (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2253 GNUNET_free (tmit_msg);
2254 return plc->tmit_msgs_head;
2259 * Callback for data transmission to PSYC.
2262 psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2264 struct Place *plc = cls;
2265 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2266 GNUNET_assert (NULL != tmit_msg);
2267 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2268 if (NULL == tmit_frag)
2269 { /* Rest of the message have not arrived yet, pause transmission */
2273 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277 "%p psyc_transmit_notify_data: nothing to send.\n", plc);
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 "%p psyc_transmit_notify_data()\n", plc);
2284 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2286 uint16_t ptype = ntohs (pmsg->type);
2287 uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
2292 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2293 if (*data_size < pdata_size)
2295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2296 "%p psyc_transmit_notify_data: buffer size too small for data.\n", plc);
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2301 "%p psyc_transmit_notify_data: sending %u bytes.\n",
2304 *data_size = pdata_size;
2305 memcpy (data, &pmsg[1], *data_size);
2309 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2314 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2316 ret = GNUNET_SYSERR;
2320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2321 "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
2323 ret = GNUNET_SYSERR;
2326 if (GNUNET_SYSERR == ret && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL != ptype)
2329 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2330 plc->is_disconnected = GNUNET_YES;
2331 GNUNET_SERVER_client_disconnect (tmit_frag->client);
2332 GNUNET_SCHEDULER_add_now (&schedule_cleanup_place, plc);
2337 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2338 if (NULL != tmit_frag)
2340 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2341 ptype = ntohs (pmsg->type);
2344 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2347 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2348 ret = GNUNET_SYSERR;
2353 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2354 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2355 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2359 if (NULL == tmit_msg->frags_head
2360 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2361 { /* Reached end of current message. */
2362 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2366 if (ret != GNUNET_NO)
2368 if (NULL != tmit_msg)
2370 psyc_transmit_message (plc);
2372 else if (GNUNET_YES == plc->is_disconnected)
2374 /* FIXME: handle partial message (when still in_transmit) */
2375 cleanup_place (plc);
2383 * Callback for modifier transmission to PSYC.
2386 psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2387 uint8_t *oper, uint32_t *full_value_size)
2389 struct Place *plc = cls;
2390 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2391 GNUNET_assert (NULL != tmit_msg);
2392 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2393 if (NULL == tmit_frag)
2394 { /* Rest of the message have not arrived yet, pause transmission */
2398 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 "%p psyc_transmit_notify_mod()\n", plc);
2409 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2411 uint16_t ptype = ntohs (pmsg->type);
2416 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
2420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2421 "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
2422 ret = GNUNET_SYSERR;
2425 struct GNUNET_PSYC_MessageModifier *
2426 pmod = (struct GNUNET_PSYC_MessageModifier *) tmit_frag->next_part;
2427 uint16_t mod_size = ntohs (pmod->header.size) - sizeof (*pmod);
2429 if (*data_size < mod_size)
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2437 *full_value_size = ntohl (pmod->value_size);
2439 *data_size = mod_size;
2440 memcpy (data, &pmod[1], mod_size);
2445 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
2449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2450 "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
2451 ret = GNUNET_SYSERR;
2454 uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
2455 if (*data_size < mod_size)
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2463 "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc, mod_size);
2465 *data_size = mod_size;
2466 memcpy (data, &pmsg[1], *data_size);
2471 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2472 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2473 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2480 "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
2482 ret = GNUNET_SYSERR;
2485 if (GNUNET_SYSERR == ret)
2488 ret = GNUNET_SYSERR;
2489 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2490 plc->is_disconnected = GNUNET_YES;
2491 GNUNET_SERVER_client_disconnect (tmit_frag->client);
2492 GNUNET_SCHEDULER_add_now (&schedule_cleanup_place, plc);
2496 if (GNUNET_YES != ret)
2497 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2499 if (NULL == tmit_msg->frags_head
2500 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2501 { /* Reached end of current message. */
2502 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2509 * Callback for data transmission from a host to PSYC.
2512 host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2514 int ret = psyc_transmit_notify_data (cls, data_size, data);
2516 if (GNUNET_NO != ret)
2518 struct Host *hst = cls;
2519 hst->tmit_handle = NULL;
2526 * Callback for the transmit functions of multicast.
2529 guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2531 int ret = psyc_transmit_notify_data (cls, data_size, data);
2533 if (GNUNET_NO != ret)
2535 struct Guest *gst = cls;
2536 gst->tmit_handle = NULL;
2543 * Callback for modifier transmission from a host to PSYC.
2546 host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2547 uint8_t *oper, uint32_t *full_value_size)
2549 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2550 oper, full_value_size);
2551 if (GNUNET_SYSERR == ret)
2553 struct Host *hst = cls;
2554 hst->tmit_handle = NULL;
2561 * Callback for modifier transmission from a guest to PSYC.
2564 guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2565 uint8_t *oper, uint32_t *full_value_size)
2567 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2568 oper, full_value_size);
2569 if (GNUNET_SYSERR == ret)
2571 struct Guest *gst = cls;
2572 gst->tmit_handle = NULL;
2579 * Get method part of next message from transmission queue.
2582 * Next item in message transmission queue.
2584 * The malloc'd message method is returned here.
2586 * @return #GNUNET_OK on success
2587 * #GNUNET_NO if there are no more messages in queue.
2588 * #GNUNET_SYSERR if the next message is malformed.
2590 static struct GNUNET_PSYC_MessageMethod *
2591 psyc_transmit_queue_next_method (struct Place *plc)
2593 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2594 if (NULL == tmit_msg)
2597 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2598 if (NULL == tmit_frag)
2604 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2606 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type))
2608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2609 "%p psyc_transmit_queue_next_method: unexpected message part of type %u.\n",
2610 plc, NULL != pmsg ? ntohs (pmsg->type) : 0);
2615 uint16_t psize = ntohs (pmsg->size);
2616 struct GNUNET_PSYC_MessageMethod *
2617 pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);
2619 if (psize < sizeof (*pmeth) + 1 || '\0' != *((char *) pmeth + psize - 1))
2621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2622 "%p psyc_transmit_queue_next_method: invalid method name.\n",
2623 plc, ntohs (pmsg->type));
2624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2625 "%u <= %u || NUL != %u\n",
2626 sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
2628 GNUNET_free (pmeth);
2632 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2638 * Transmit the next message in queue from the host to the PSYC channel.
2641 psyc_master_transmit_message (struct Host *hst)
2643 struct Place *plc = &hst->plc;
2645 if (NULL == hst->tmit_handle)
2647 struct GNUNET_PSYC_MessageMethod *
2648 pmeth = psyc_transmit_queue_next_method (plc);
2650 return GNUNET_SYSERR;
2653 = GNUNET_PSYC_master_transmit (hst->master, (const char *) &pmeth[1],
2654 &host_transmit_notify_mod,
2655 &host_transmit_notify_data, hst,
2657 GNUNET_free (pmeth);
2661 GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
2668 * Transmit the next message in queue from a guest to the PSYC channel.
2671 psyc_slave_transmit_message (struct Guest *gst)
2673 struct Place *plc = &gst->plc;
2675 if (NULL == gst->tmit_handle)
2677 struct GNUNET_PSYC_MessageMethod *
2678 pmeth = psyc_transmit_queue_next_method (plc);
2680 return GNUNET_SYSERR;
2683 = GNUNET_PSYC_slave_transmit (gst->slave, (const char *) &pmeth[1],
2684 &guest_transmit_notify_mod,
2685 &guest_transmit_notify_data, gst,
2687 GNUNET_free (pmeth);
2691 GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
2698 * Transmit a message to PSYC.
2701 psyc_transmit_message (struct Place *plc)
2705 ? psyc_master_transmit_message ((struct Host *) plc)
2706 : psyc_slave_transmit_message ((struct Guest *) plc);
2711 * Queue message parts for sending to PSYC.
2713 * @param plc Place to send to.
2714 * @param client Client the message originates from.
2715 * @param data_size Size of @a data.
2716 * @param data Concatenated message parts.
2717 * @param first_ptype First message part type in @a data.
2718 * @param last_ptype Last message part type in @a data.
2720 static struct MessageTransmitQueue *
2721 psyc_transmit_queue_message (struct Place *plc,
2722 struct GNUNET_SERVER_Client *client,
2725 uint16_t first_ptype, uint16_t last_ptype,
2726 struct MessageTransmitQueue *tmit_msg)
2728 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
2730 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
2731 GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2733 else if (NULL == tmit_msg)
2738 struct FragmentTransmitQueue *
2739 tmit_frag = GNUNET_malloc (sizeof (*tmit_frag) + data_size);
2740 memcpy (&tmit_frag[1], data, data_size);
2741 tmit_frag->next_part = (struct GNUNET_MessageHeader *) &tmit_frag[1];
2742 tmit_frag->client = client;
2743 tmit_frag->size = data_size;
2745 GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2746 tmit_msg->client = client;
2752 * Cancel transmission of current message to PSYC.
2754 * @param plc Place to send to.
2755 * @param client Client the message originates from.
2758 psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVER_Client *client)
2760 uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2762 struct GNUNET_MessageHeader msg;
2763 msg.size = htons (sizeof (msg));
2764 msg.type = htons (type);
2766 psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
2767 psyc_transmit_message (plc);
2769 /* FIXME: cleanup */
2774 * Handle an incoming message from a client, to be transmitted to the place.
2777 client_recv_psyc_message (void *cls, struct GNUNET_SERVER_Client *client,
2778 const struct GNUNET_MessageHeader *msg)
2781 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2782 GNUNET_assert (NULL != ctx);
2783 struct Place *plc = ctx->plc;
2784 int ret = GNUNET_SYSERR;
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2787 "%p Received message from client.\n", plc);
2788 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);
2790 if (GNUNET_YES != plc->is_ready)
2792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2793 "%p Place is not ready yet, disconnecting client.\n", plc);
2795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2799 uint16_t size = ntohs (msg->size);
2800 uint16_t psize = size - sizeof (*msg);
2801 if (psize < sizeof (struct GNUNET_MessageHeader)
2802 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < psize)
2804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2805 "%p Received message with invalid payload size (%u) from client.\n",
2808 psyc_transmit_cancel (plc, client);
2809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2813 uint16_t first_ptype = 0, last_ptype = 0;
2815 == GNUNET_PSYC_receive_check_parts (psize, (const char *) &msg[1],
2816 &first_ptype, &last_ptype))
2818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2819 "%p Received invalid message part from client.\n", plc);
2821 psyc_transmit_cancel (plc, client);
2822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2826 "%p Received message with first part type %u and last part type %u.\n",
2827 plc, first_ptype, last_ptype);
2830 = psyc_transmit_queue_message (plc, client, psize, &msg[1],
2831 first_ptype, last_ptype, ctx->tmit_msg);
2832 if (NULL != ctx->tmit_msg)
2834 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
2835 ctx->tmit_msg = NULL;
2836 ret = psyc_transmit_message (plc);
2839 if (GNUNET_OK != ret)
2841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2842 "%p Received invalid message part from client.\n", plc);
2844 psyc_transmit_cancel (plc, client);
2845 ret = GNUNET_SYSERR;
2847 GNUNET_SERVER_receive_done (client, ret);
2852 * A historic message arrived from PSYC.
2855 psyc_recv_history_message (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
2857 struct OperationClosure *opcls = cls;
2858 struct Place *plc = opcls->plc;
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2861 "%p Received historic message #%" PRId64 " (flags: %x)\n",
2862 plc, GNUNET_ntohll (msg->message_id), ntohl (msg->flags));
2864 uint16_t size = ntohs (msg->header.size);
2866 struct GNUNET_OperationResultMessage *
2867 res = GNUNET_malloc (sizeof (*res) + size);
2868 res->header.size = htons (sizeof (*res) + size);
2869 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
2870 res->op_id = opcls->op_id;
2871 res->result_code = GNUNET_htonll (GNUNET_OK);
2873 memcpy (&res[1], msg, size);
2875 /** @todo FIXME: send only to requesting client */
2876 place_send_msg (plc, &res->header);
2881 * Result of message history replay from PSYC.
2884 psyc_recv_history_result (void *cls, int64_t result,
2885 const void *err_msg, uint16_t err_msg_size)
2887 struct OperationClosure *opcls = cls;
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2889 "%p History replay #%" PRIu64 ": "
2890 "PSYCstore returned %" PRId64 " (%.*s)\n",
2891 opcls->plc, GNUNET_ntohll (opcls->op_id), result, err_msg_size, err_msg);
2893 // FIXME: place might have been destroyed
2894 client_send_result (opcls->client, opcls->op_id, result, err_msg, err_msg_size);
2899 * Client requests channel history.
2902 client_recv_history_replay (void *cls, struct GNUNET_SERVER_Client *client,
2903 const struct GNUNET_MessageHeader *msg)
2906 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
2907 GNUNET_assert (NULL != ctx);
2908 struct Place *plc = ctx->plc;
2910 const struct GNUNET_PSYC_HistoryRequestMessage *
2911 req = (const struct GNUNET_PSYC_HistoryRequestMessage *) msg;
2912 uint16_t size = ntohs (msg->size);
2913 const char *method_prefix = (const char *) &req[1];
2915 if (size < sizeof (*req) + 1
2916 || '\0' != method_prefix[size - sizeof (*req) - 1])
2918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2919 "%p History replay #%" PRIu64 ": "
2920 "invalid method prefix. size: %u < %u?\n",
2921 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
2923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2927 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
2928 opcls->client = client;
2930 opcls->op_id = req->op_id;
2931 opcls->flags = ntohl (req->flags);
2933 if (0 == req->message_limit)
2934 GNUNET_PSYC_channel_history_replay (plc->channel,
2935 GNUNET_ntohll (req->start_message_id),
2936 GNUNET_ntohll (req->end_message_id),
2937 method_prefix, opcls->flags,
2938 psyc_recv_history_message, NULL,
2939 psyc_recv_history_result, opcls);
2941 GNUNET_PSYC_channel_history_replay_latest (plc->channel,
2942 GNUNET_ntohll (req->message_limit),
2943 method_prefix, opcls->flags,
2944 psyc_recv_history_message, NULL,
2945 psyc_recv_history_result, opcls);
2947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2952 * A state variable part arrived from PSYC.
2955 psyc_recv_state_var (void *cls,
2956 const struct GNUNET_MessageHeader *mod,
2959 uint32_t value_size,
2960 uint32_t full_value_size)
2962 struct OperationClosure *opcls = cls;
2963 struct Place *plc = opcls->plc;
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2966 "%p Received state variable %s from PSYC\n",
2969 uint16_t size = ntohs (mod->size);
2971 struct GNUNET_OperationResultMessage *
2972 res = GNUNET_malloc (sizeof (*res) + size);
2973 res->header.size = htons (sizeof (*res) + size);
2974 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
2975 res->op_id = opcls->op_id;
2976 res->result_code = GNUNET_htonll (GNUNET_OK);
2978 memcpy (&res[1], mod, size);
2980 /** @todo FIXME: send only to requesting client */
2981 place_send_msg (plc, &res->header);
2986 * Result of retrieving state variable from PSYC.
2989 psyc_recv_state_result (void *cls, int64_t result,
2990 const void *err_msg, uint16_t err_msg_size)
2992 struct OperationClosure *opcls = cls;
2993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2994 "%p State get #%" PRIu64 ": "
2995 "PSYCstore returned %" PRId64 " (%.*s)\n",
2996 opcls->plc, GNUNET_ntohll (opcls->op_id), result, err_msg_size, err_msg);
2998 // FIXME: place might have been destroyed
2999 client_send_result (opcls->client, opcls->op_id, result, err_msg, err_msg_size);
3004 * Client requests channel history.
3007 client_recv_state_get (void *cls, struct GNUNET_SERVER_Client *client,
3008 const struct GNUNET_MessageHeader *msg)
3011 ctx = GNUNET_SERVER_client_get_user_context (client, struct Client);
3012 GNUNET_assert (NULL != ctx);
3013 struct Place *plc = ctx->plc;
3015 const struct GNUNET_PSYC_StateRequestMessage *
3016 req = (const struct GNUNET_PSYC_StateRequestMessage *) msg;
3017 uint16_t size = ntohs (msg->size);
3018 const char *name = (const char *) &req[1];
3020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3021 "%p State get #%" PRIu64 ": %s\n",
3022 plc, GNUNET_ntohll (req->op_id), name);
3024 if (size < sizeof (*req) + 1
3025 || '\0' != name[size - sizeof (*req) - 1])
3027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3028 "%p State get #%" PRIu64 ": "
3029 "invalid name. size: %u < %u?\n",
3030 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3036 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3037 opcls->client = client;
3039 opcls->op_id = req->op_id;
3041 switch (ntohs (msg->type))
3043 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET:
3044 GNUNET_PSYC_channel_state_get (plc->channel, name,
3045 psyc_recv_state_var,
3046 psyc_recv_state_result, opcls);
3049 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX:
3050 GNUNET_PSYC_channel_state_get_prefix (plc->channel, name,
3051 psyc_recv_state_var,
3052 psyc_recv_state_result, opcls);
3059 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3064 namestore_recv_records_store_result (void *cls, int32_t result,
3065 const char *err_msg)
3067 struct OperationClosure *ocls = cls;
3068 client_send_result (ocls->client, ocls->op_id, result, err_msg,
3069 (NULL != err_msg) ? strlen (err_msg) : 0);
3075 * Handle request to add PLACE record to GNS zone.
3078 client_recv_zone_add_place (void *cls, struct GNUNET_SERVER_Client *client,
3079 const struct GNUNET_MessageHeader *msg)
3081 const struct ZoneAddPlaceRequest *preq
3082 = (const struct ZoneAddPlaceRequest *) msg;
3084 uint16_t remaining = ntohs (preq->header.size) - sizeof (*preq);
3085 const char *p = (const char *) &preq[1];
3086 const char *name = NULL, *password = NULL;
3087 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 2,
3089 remaining -= offset;
3091 const struct GNUNET_PeerIdentity *
3092 relays = (const struct GNUNET_PeerIdentity *) p;
3093 uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);
3095 if (0 == offset || remaining != relay_size)
3098 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3099 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3103 struct GNUNET_GNSRECORD_Data rd = { };
3104 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
3105 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3106 rd.expiration_time = GNUNET_ntohll (preq->expiration_time);
3108 struct GNUNET_GNSRECORD_PlaceData *
3109 rec = GNUNET_malloc (sizeof (*rec) + relay_size);
3110 rec->place_pub_key = preq->place_pub_key;
3111 rec->origin = this_peer;
3112 rec->relay_count = preq->relay_count;
3113 memcpy (&rec[1], relays, relay_size);
3116 rd.data_size = sizeof (*rec) + relay_size;
3118 struct GNUNET_HashCode ego_pub_hash;
3119 GNUNET_CRYPTO_hash (&preq->ego_pub_key, sizeof (preq->ego_pub_key), &ego_pub_hash);
3120 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3123 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3127 struct OperationClosure *ocls = GNUNET_malloc (sizeof (*ocls));
3128 ocls->client = client;
3129 ocls->op_id = preq->op_id;
3130 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3132 namestore_recv_records_store_result, ocls);
3133 /** @todo refresh stored records later */
3135 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3140 * Handle request to add PLACE record to GNS zone.
3143 client_recv_zone_add_nym (void *cls, struct GNUNET_SERVER_Client *client,
3144 const struct GNUNET_MessageHeader *msg)
3146 const struct ZoneAddNymRequest *nreq
3147 = (const struct ZoneAddNymRequest *) msg;
3149 uint16_t name_size = ntohs (nreq->header.size) - sizeof (*nreq);
3150 const char *name = NULL;
3151 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &nreq[1],
3152 name_size, 1, &name);
3153 if (0 == offset || offset != name_size)
3156 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3157 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3161 struct GNUNET_GNSRECORD_Data rd = { };
3162 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
3163 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3164 rd.expiration_time = GNUNET_ntohll (nreq->expiration_time);
3165 rd.data = &nreq->nym_pub_key;
3166 rd.data_size = sizeof (nreq->nym_pub_key);
3168 struct GNUNET_HashCode ego_pub_hash;
3169 GNUNET_CRYPTO_hash (&nreq->ego_pub_key, sizeof (nreq->ego_pub_key), &ego_pub_hash);
3170 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3173 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3177 struct OperationClosure *ocls = GNUNET_malloc (sizeof (*ocls));
3178 ocls->client = client;
3179 ocls->op_id = nreq->op_id;
3180 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3182 namestore_recv_records_store_result, ocls);
3183 /** @todo refresh stored records later */
3185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3189 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3190 { client_recv_host_enter, NULL,
3191 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER, 0 },
3193 { client_recv_guest_enter, NULL,
3194 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER, 0 },
3196 { client_recv_guest_enter_by_name, NULL,
3197 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME, 0 },
3199 { client_recv_join_decision, NULL,
3200 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION, 0 },
3202 { client_recv_psyc_message, NULL,
3203 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE, 0 },
3205 { client_recv_history_replay, NULL,
3206 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY, 0 },
3208 { client_recv_state_get, NULL,
3209 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET, 0 },
3211 { client_recv_state_get, NULL,
3212 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX, 0 },
3214 { client_recv_zone_add_place, NULL,
3215 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE, 0 },
3217 { client_recv_zone_add_nym, NULL,
3218 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM, 0 },
3220 { client_recv_app_connect, NULL,
3221 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT, 0 },
3223 { client_recv_app_detach, NULL,
3224 GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH, 0 },
3226 { client_recv_place_leave, NULL,
3227 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE, 0 },
3229 { client_recv_msg_proc_set, NULL,
3230 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET, 0 },
3232 { client_recv_msg_proc_clear, NULL,
3233 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR, 0 },
3235 { NULL, NULL, 0, 0 }
3240 path_basename (const char *path)
3242 const char *basename = strrchr (path, DIR_SEPARATOR);
3243 if (NULL != basename)
3246 if (NULL == basename || '\0' == basename)
3253 struct PlaceLoadClosure
3256 const char *ego_pub_hash_str;
3260 /** Load a place file */
3262 file_place_load (void *cls, const char *filename)
3265 uint64_t file_size = 0;
3267 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3268 || file_size < sizeof (struct HostEnterRequest))
3271 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
3272 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
3273 if (read_size < 0 || read_size < sizeof (*ereq))
3276 uint16_t ereq_size = ntohs (ereq->header.size);
3277 if (read_size != ereq_size)
3280 switch (ntohs (ereq->header.type))
3282 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3283 if (ereq_size < sizeof (struct HostEnterRequest))
3285 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3286 host_enter (hreq, NULL);
3289 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3290 if (ereq_size < sizeof (struct GuestEnterRequest))
3292 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3293 guest_enter (greq, NULL);
3300 app_place_add (app_id, ereq);
3305 /** Load an ego place file */
3307 file_ego_place_load (void *cls, const char *place_filename)
3309 struct PlaceLoadClosure *plcls = cls;
3311 const char *place_pub_hash_str = path_basename (place_filename);
3312 if (NULL == place_pub_hash_str)
3318 char *filename = NULL;
3319 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3320 dir_social, DIR_SEPARATOR,
3321 "places", DIR_SEPARATOR,
3322 plcls->ego_pub_hash_str, DIR_SEPARATOR,
3323 place_pub_hash_str);
3325 struct PlaceEnterRequest ereq[GNUNET_SERVER_MAX_MESSAGE_SIZE];
3327 int read_size = GNUNET_DISK_fn_read (filename, &ereq,
3328 GNUNET_SERVER_MAX_MESSAGE_SIZE);
3329 GNUNET_free (filename);
3331 if (read_size < (ssize_t) sizeof (ereq))
3334 app_place_add (plcls->app_id, ereq);
3340 * Read @e place_pub_hash_str entries in @a dir_ego
3343 * Data directory of an application ego.
3344 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_hash_str/
3347 scan_app_ego_dir (void *cls, const char *dir_ego)
3349 struct PlaceLoadClosure *plcls = cls;
3350 plcls->ego_pub_hash_str = path_basename (dir_ego);
3352 if (NULL != plcls->ego_pub_hash_str)
3353 GNUNET_DISK_directory_scan (dir_ego, file_ego_place_load, plcls);
3359 * Read @e ego_pub_hash_str entries in @a dir_app
3362 * Data directory of an application.
3363 * $GNUNET_DATA_HOME/social/apps/$app_id/
3366 scan_app_dir (void *cls, const char *dir_app)
3368 if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
3371 struct PlaceLoadClosure plcls;
3372 plcls.app_id = path_basename (dir_app);
3374 if (NULL != plcls.app_id)
3375 GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);
3382 identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
3383 void **ctx, const char *name)
3385 if (NULL == id_ego) // end of initial list of egos
3388 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
3389 GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);
3391 struct GNUNET_HashCode ego_pub_hash;
3392 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
3394 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3397 GNUNET_free (ego->name);
3398 if (NULL == name) // deleted
3400 GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
3407 ego = GNUNET_malloc (sizeof (*ego));
3411 ego->key = *(GNUNET_IDENTITY_ego_get_private_key (id_ego));
3412 size_t name_size = strlen (name) + 1;
3413 ego->name = GNUNET_malloc (name_size);
3414 memcpy (ego->name, name, name_size);
3416 GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
3417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3420 // FIXME: notify clients about changed ego
3425 * Connected to core service.
3428 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
3430 this_peer = *my_identity;
3435 * Initialize the PSYC service.
3437 * @param cls Closure.
3438 * @param server The initialized server.
3439 * @param c Configuration to use.
3442 run (void *cls, struct GNUNET_SERVER_Handle *server,
3443 const struct GNUNET_CONFIGURATION_Handle *c)
3447 hosts = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3448 guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3449 place_guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3451 egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3452 apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3453 places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3454 apps_places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3455 places_apps = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3457 core = GNUNET_CORE_connect (cfg, NULL, core_connected, NULL, NULL,
3458 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
3459 id = GNUNET_IDENTITY_connect (cfg, &identity_recv_ego, NULL);
3460 gns = GNUNET_GNS_connect (cfg);
3461 namestore = GNUNET_NAMESTORE_connect (cfg);
3462 stats = GNUNET_STATISTICS_create ("social", cfg);
3465 GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
3468 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3469 "social", "DATA_HOME");
3473 GNUNET_asprintf (&dir_places, "%s%c%s",
3474 dir_social, DIR_SEPARATOR, "places");
3475 GNUNET_asprintf (&dir_apps, "%s%c%s",
3476 dir_social, DIR_SEPARATOR, "apps");
3478 GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);
3480 nc = GNUNET_SERVER_notification_context_create (server, 1);
3481 GNUNET_SERVER_add_handlers (server, handlers);
3482 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
3483 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3484 &shutdown_task, NULL);
3489 * The main function for the service.
3491 * @param argc number of arguments from the command line
3492 * @param argv command line arguments
3493 * @return 0 ok, 1 on error
3496 main (int argc, char *const *argv)
3498 return (GNUNET_OK ==
3499 GNUNET_SERVICE_run (argc, argv, "social",
3500 GNUNET_SERVICE_OPTION_NONE,
3501 &run, NULL)) ? 0 : 1;
3504 /* end of gnunet-service-social.c */