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 it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your 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 * Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file social/gnunet-service-social.c
23 * @brief Social service
24 * @author Gabor X Toth
31 #include "gnunet_util_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_identity_service.h"
35 #include "gnunet_namestore_service.h"
36 #include "gnunet_gns_service.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_psyc_service.h"
39 #include "gnunet_psyc_util_lib.h"
40 #include "gnunet_social_service.h"
45 * Handle to our current configuration.
47 static const struct GNUNET_CONFIGURATION_Handle *cfg;
52 static struct GNUNET_SERVICE_Handle *service;
54 /* Handles to other services */
55 static struct GNUNET_IDENTITY_Handle *id;
56 static struct GNUNET_GNS_Handle *gns;
57 static struct GNUNET_NAMESTORE_Handle *namestore;
58 static struct GNUNET_STATISTICS_Handle *stats;
63 static struct GNUNET_PeerIdentity this_peer;
66 * All connected hosts.
67 * H(place_pub_key) -> struct Host
69 static struct GNUNET_CONTAINER_MultiHashMap *hosts;
72 * All connected guests.
73 * H(place_pub_key) -> struct Guest
75 static struct GNUNET_CONTAINER_MultiHashMap *guests;
78 * Connected guests per place.
79 * H(place_pub_key) -> ego_pub_key -> struct Guest
81 static struct GNUNET_CONTAINER_MultiHashMap *place_guests;
84 * Places entered as host or guest.
85 * H(place_pub_key) -> struct HostEnterRequest OR struct GuestEnterRequest
87 static struct GNUNET_CONTAINER_MultiHashMap *places;
90 * Places entered per application.
91 * H(app_id) -> H(place_pub_key) -> NULL
93 static struct GNUNET_CONTAINER_MultiHashMap *apps_places;
96 * Application subscriptions per place.
97 * H(place_pub_key) -> H(app_id)
99 //static struct GNUNET_CONTAINER_MultiHashMap *places_apps;
102 * Connected applications.
103 * H(app_id) -> struct Application
105 static struct GNUNET_CONTAINER_MultiHashMap *apps;
109 * H(ego_pub_key) -> struct Ego
111 static struct GNUNET_CONTAINER_MultiHashMap *egos;
114 * Directory for storing social data.
115 * Default: $GNUNET_DATA_HOME/social
117 static char *dir_social;
120 * Directory for storing place data.
123 static char *dir_places;
126 * Directory for storing app data.
129 static char *dir_apps;
133 * Message fragment transmission queue.
135 struct FragmentTransmitQueue
137 struct FragmentTransmitQueue *prev;
138 struct FragmentTransmitQueue *next;
140 struct GNUNET_SERVICE_Client *client;
143 * Pointer to the next message part inside the data after this struct.
145 struct GNUNET_MessageHeader *next_part;
153 * @see enum GNUNET_PSYC_MessageState
157 /* Followed by one or more message parts. */
162 * Message transmission queue.
164 struct MessageTransmitQueue
166 struct MessageTransmitQueue *prev;
167 struct MessageTransmitQueue *next;
169 struct FragmentTransmitQueue *frags_head;
170 struct FragmentTransmitQueue *frags_tail;
172 struct GNUNET_SERVICE_Client *client;
176 * List of connected clients.
178 struct ClientListItem
180 struct ClientListItem *prev;
181 struct ClientListItem *next;
183 struct GNUNET_SERVICE_Client *client;
188 * Common part of the client context for both a host and guest.
192 struct ClientListItem *clients_head;
193 struct ClientListItem *clients_tail;
195 struct MessageTransmitQueue *tmit_msgs_head;
196 struct MessageTransmitQueue *tmit_msgs_tail;
198 struct GNUNET_PSYC_Channel *channel;
201 * Private key of home in case of a host.
203 struct GNUNET_CRYPTO_EddsaPublicKey key;
206 * Public key of place.
208 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
211 * Hash of @a pub_key.
213 struct GNUNET_HashCode pub_key_hash;
216 * Private key of ego.
218 struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
223 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
226 * Hash of @a ego_pub_key.
228 struct GNUNET_HashCode ego_pub_hash;
231 * Slicer for processing incoming messages.
233 struct GNUNET_PSYC_Slicer *slicer;
236 * Last message ID received for the place.
237 * 0 if there is no such message.
239 uint64_t max_message_id;
242 * Offset where the file is currently being written.
244 uint64_t file_offset;
247 * Whether or not to save the file (#GNUNET_YES or #GNUNET_NO)
252 * Is this place ready to receive messages from client?
253 * #GNUNET_YES or #GNUNET_NO
258 * Is the client disconnecting?
259 * #GNUNET_YES or #GNUNET_NO
261 uint8_t is_disconnecting;
264 * Is this a host (#GNUNET_YES), or guest (#GNUNET_NO)?
276 * Client context for a host.
281 * Place struct common for Host and Guest
286 * Handle for the multicast origin.
288 struct GNUNET_PSYC_Master *master;
291 * Transmit handle for multicast.
293 struct GNUNET_PSYC_MasterTransmitHandle *tmit_handle;
296 * Incoming join requests.
297 * guest_key -> struct GNUNET_PSYC_JoinHandle *
299 struct GNUNET_CONTAINER_MultiHashMap *join_reqs;
302 * Messages being relayed.
304 struct GNUNET_CONTAINER_MultiHashMap *relay_msgs;
307 * @see enum GNUNET_PSYC_Policy
309 enum GNUNET_PSYC_Policy policy;
314 * Client context for a guest.
319 * Place struct common for Host and Guest.
324 * Handle for the PSYC slave.
326 struct GNUNET_PSYC_Slave *slave;
329 * Transmit handle for multicast.
331 struct GNUNET_PSYC_SlaveTransmitHandle *tmit_handle;
334 * Peer identity of the origin.
336 struct GNUNET_PeerIdentity origin;
339 * Number of items in @a relays.
341 uint32_t relay_count;
344 * Relays that multicast can use to connect.
346 struct GNUNET_PeerIdentity *relays;
349 * Join request to be transmitted to the master on join.
351 struct GNUNET_MessageHeader *join_req; // FIXME: not used!
354 * Join decision received from PSYC.
356 struct GNUNET_PSYC_JoinDecisionMessage *join_dcsn;
359 * Join flags for the PSYC service.
361 enum GNUNET_PSYC_SlaveJoinFlags join_flags;
366 * Context for a client.
373 struct GNUNET_SERVICE_Client *client;
376 * Place where the client entered.
381 * Message queue for the message currently being transmitted
384 struct MessageTransmitQueue *tmit_msg;
387 * ID for application clients.
395 struct ClientListItem *clients_head;
396 struct ClientListItem *clients_tail;
401 struct GNUNET_CRYPTO_EcdsaPrivateKey key;
406 struct OperationClosure
408 struct Client *client;
415 psyc_transmit_message (struct Place *plc);
419 * Clean up place data structures after a client disconnected.
421 * @param cls the `struct Place` to clean up
424 cleanup_place (void *cls);
427 static struct MessageTransmitQueue *
428 psyc_transmit_queue_message (struct Place *plc,
429 struct GNUNET_SERVICE_Client *client,
432 uint16_t first_ptype, uint16_t last_ptype,
433 struct MessageTransmitQueue *tmit_msg);
437 place_entry_cleanup (void *cls,
438 const struct GNUNET_HashCode *key,
441 struct Place *plc = value;
449 * Task run during shutdown.
454 shutdown_task (void *cls)
456 GNUNET_CONTAINER_multihashmap_iterate (hosts, place_entry_cleanup, NULL);
457 GNUNET_CONTAINER_multihashmap_iterate (guests, place_entry_cleanup, NULL);
461 GNUNET_IDENTITY_disconnect (id);
464 if (NULL != namestore)
466 GNUNET_NAMESTORE_disconnect (namestore);
471 GNUNET_GNS_disconnect (gns);
476 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
483 * Clean up host data structures after a client disconnected.
486 cleanup_host (struct Host *hst)
488 struct Place *plc = &hst->place;
490 GNUNET_CONTAINER_multihashmap_destroy (hst->join_reqs);
491 GNUNET_CONTAINER_multihashmap_destroy (hst->relay_msgs);
492 GNUNET_CONTAINER_multihashmap_remove (hosts, &plc->pub_key_hash, plc);
497 * Clean up guest data structures after a client disconnected.
500 cleanup_guest (struct Guest *gst)
502 struct Place *plc = &gst->place;
503 struct GNUNET_CONTAINER_MultiHashMap *
504 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests,
508 GNUNET_CONTAINER_multihashmap_remove (plc_gst, &plc->ego_pub_hash, gst);
510 if (0 == GNUNET_CONTAINER_multihashmap_size (plc_gst))
512 GNUNET_CONTAINER_multihashmap_remove (place_guests, &plc->pub_key_hash,
514 GNUNET_CONTAINER_multihashmap_destroy (plc_gst);
517 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, gst);
518 if (NULL != gst->join_req)
519 GNUNET_free (gst->join_req);
520 if (NULL != gst->relays)
521 GNUNET_free (gst->relays);
522 GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, plc);
527 * Clean up place data structures after a client disconnected.
529 * @param cls the `struct Place` to clean up
532 cleanup_place (void *cls)
534 struct Place *plc = cls;
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "cleaning up place %s\n",
538 GNUNET_h2s (&plc->pub_key_hash));
540 (GNUNET_YES == plc->is_host)
541 ? cleanup_host ((struct Host *) plc)
542 : cleanup_guest ((struct Guest *) plc);
544 GNUNET_PSYC_slicer_destroy (plc->slicer);
550 * Called whenever a client is disconnected.
551 * Frees our resources associated with that client.
554 * @param client identification of the client
555 * @param app_ctx must match @a client
558 client_notify_disconnect (void *cls,
559 struct GNUNET_SERVICE_Client *client,
562 struct Client *c = app_ctx;
563 struct Place *plc = c->place;
565 if (NULL != c->app_id)
566 GNUNET_free (c->app_id);
571 return; // application client, nothing to do
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "%p Client (%s) disconnected from place %s\n",
575 plc, (GNUNET_YES == plc->is_host) ? "host" : "guest",
576 GNUNET_h2s (&plc->pub_key_hash));
578 struct ClientListItem *cli = plc->clients_head;
581 if (cli->client == client)
583 GNUNET_CONTAINER_DLL_remove (plc->clients_head,
591 if (GNUNET_YES == plc->is_disconnecting)
593 GNUNET_PSYC_slicer_destroy (plc->slicer);
600 * A new client connected.
603 * @param client client to add
604 * @param mq message queue for @a client
608 client_notify_connect (void *cls,
609 struct GNUNET_SERVICE_Client *client,
610 struct GNUNET_MQ_Handle *mq)
612 struct Client *c = GNUNET_new (struct Client);
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Client %p connected with queue %p\n",
624 * Send message to all clients connected to a place and
625 * takes care of freeing @env.
628 place_send_msg (const struct Place *plc,
629 struct GNUNET_MQ_Envelope *env)
631 struct ClientListItem *cli = plc->clients_head;
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "%p Sending message to clients of place.\n", plc);
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Sending message to client %p\n",
640 GNUNET_MQ_send_copy (GNUNET_SERVICE_client_get_mq (cli->client),
644 GNUNET_MQ_discard (env);
649 place_send_leave_ack (struct Place *plc)
651 struct GNUNET_MQ_Envelope *env;
653 for (struct ClientListItem *cli = plc->clients_head;
657 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK);
658 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (cli->client),
665 * Send a result code back to the client.
668 * Client that should receive the result code.
672 * Operation ID in network byte order.
674 * Data payload or NULL.
679 client_send_result (struct GNUNET_SERVICE_Client *client, uint64_t op_id,
680 int64_t result_code, const void *data, uint16_t data_size)
682 struct GNUNET_MQ_Envelope *env;
683 struct GNUNET_OperationResultMessage *res;
685 env = GNUNET_MQ_msg_extra (res,
687 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
688 res->result_code = GNUNET_htonll (result_code);
691 GNUNET_memcpy (&res[1], data, data_size);
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "%p Sending result to client for operation #%" PRIu64 ": "
694 "%" PRId64 " (size: %u)\n",
695 client, GNUNET_ntohll (op_id), result_code, data_size);
696 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
701 client_send_host_enter_ack (struct GNUNET_SERVICE_Client *client,
702 struct Host *hst, uint32_t result)
704 struct GNUNET_MQ_Envelope *env;
705 struct HostEnterAck *hack;
706 struct Place *plc = &hst->place;
708 env = GNUNET_MQ_msg (hack,
709 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK);
710 hack->result_code = htonl (result);
711 hack->max_message_id = GNUNET_htonll (plc->max_message_id);
712 hack->place_pub_key = plc->pub_key;
715 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
718 place_send_msg (plc, env);
723 * Called after a PSYC master is started.
726 psyc_master_started (void *cls, int result, uint64_t max_message_id)
728 struct Host *hst = cls;
729 struct Place *plc = &hst->place;
730 plc->max_message_id = max_message_id;
731 plc->is_ready = GNUNET_YES;
733 client_send_host_enter_ack (NULL, hst, result);
738 * Called when a PSYC master receives a join request.
741 psyc_recv_join_request (void *cls,
742 const struct GNUNET_PSYC_JoinRequestMessage *req,
743 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
744 const struct GNUNET_PSYC_Message *join_msg,
745 struct GNUNET_PSYC_JoinHandle *jh)
747 struct Host *hst = cls;
748 struct GNUNET_HashCode slave_key_hash;
749 GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
750 GNUNET_CONTAINER_multihashmap_put (hst->join_reqs, &slave_key_hash, jh,
751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
752 place_send_msg (&hst->place,
753 GNUNET_MQ_msg_copy (&req->header));
758 * Called after a PSYC slave is connected.
761 psyc_slave_connected (void *cls, int result, uint64_t max_message_id)
763 struct GNUNET_PSYC_CountersResultMessage *res;
764 struct GNUNET_MQ_Envelope *env;
765 struct Guest *gst = cls;
766 struct Place *plc = &gst->place;
768 plc->max_message_id = max_message_id;
769 plc->is_ready = GNUNET_YES;
770 env = GNUNET_MQ_msg (res,
771 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
773 (result != GNUNET_SYSERR) ? htonl (GNUNET_OK) : htonl (GNUNET_SYSERR);
774 res->max_message_id = GNUNET_htonll (plc->max_message_id);
775 place_send_msg (plc, env);
780 slave_parted_after_join_decision (void *cls)
782 struct Guest *gst = cls;
784 GNUNET_assert (NULL != gst->join_dcsn);
785 place_send_msg (&gst->place, GNUNET_MQ_msg_copy (&gst->join_dcsn->header));
790 * Called when a PSYC slave receives a join decision.
793 psyc_recv_join_dcsn (void *cls,
794 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
796 const struct GNUNET_PSYC_Message *join_msg)
798 struct Guest *gst = cls;
800 gst->join_dcsn = GNUNET_malloc (dcsn->header.size);
801 GNUNET_memcpy (gst->join_dcsn,
804 if (GNUNET_NO == is_admitted)
806 GNUNET_PSYC_slave_part (gst->slave,
808 &slave_parted_after_join_decision,
813 place_send_msg (&gst->place, GNUNET_MQ_msg_copy (&gst->join_dcsn->header));
818 * Called when a PSYC master or slave receives a message.
821 psyc_recv_message (void *cls,
822 const struct GNUNET_PSYC_MessageHeader *msg)
824 struct Place *plc = cls;
826 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&msg->slave_pub_key);
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "%p Received PSYC message of size %u from %s.\n",
829 plc, ntohs (msg->header.size), str);
832 GNUNET_PSYC_slicer_message (plc->slicer, msg);
834 place_send_msg (plc, GNUNET_MQ_msg_copy (&msg->header));
839 * Relay a message part received from a guest to the the place.
846 * Nym the message is received from.
849 host_relay_message_part (struct Host *hst,
850 const struct GNUNET_MessageHeader *pmsg,
851 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key)
853 /* separate queue per nym */
854 struct GNUNET_HashCode nym_pub_hash;
855 GNUNET_CRYPTO_hash (nym_pub_key, sizeof (*nym_pub_key), &nym_pub_hash);
857 struct MessageTransmitQueue *
858 tmit_msg = GNUNET_CONTAINER_multihashmap_get (hst->relay_msgs, &nym_pub_hash);
860 uint16_t ptype = ntohs (pmsg->type);
862 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
864 /* FIXME: last message was unfinished, cancel & remove from queue */
865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
866 "FIXME: last message was unfinished.\n");
869 tmit_msg = psyc_transmit_queue_message (&hst->place, NULL, ntohs (pmsg->size),
870 pmsg, ptype, ptype, tmit_msg);
874 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
875 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_put
876 (hst->relay_msgs, &nym_pub_hash, tmit_msg,
877 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
879 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
880 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
881 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove
882 (hst->relay_msgs, &nym_pub_hash, tmit_msg));
889 * Received a method to be relayed from a guest.
892 place_recv_relay_method (void *cls,
893 const struct GNUNET_PSYC_MessageHeader *msg,
894 const struct GNUNET_PSYC_MessageMethod *meth,
896 const char *method_name)
898 struct Place *plc = cls;
900 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
901 && GNUNET_YES == plc->is_host)
903 struct Host *hst = cls;
904 host_relay_message_part (hst, &meth->header, &msg->slave_pub_key);
910 * Received a modifier to be relayed from a guest.
913 place_recv_relay_modifier (void *cls,
914 const struct GNUNET_PSYC_MessageHeader *msg,
915 const struct GNUNET_MessageHeader *pmsg,
917 enum GNUNET_PSYC_Operator oper,
921 uint16_t full_value_size)
923 struct Place *plc = cls;
925 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
926 && GNUNET_YES == plc->is_host)
928 struct Host *hst = cls;
929 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
934 * Received a data fragment to be relayed from a guest.
937 place_recv_relay_data (void *cls,
938 const struct GNUNET_PSYC_MessageHeader *msg,
939 const struct GNUNET_MessageHeader *pmsg,
944 struct Place *plc = cls;
946 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
947 && GNUNET_YES == plc->is_host)
949 struct Host *hst = cls;
950 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
956 * Received end of message to be relayed from a guest.
959 place_recv_relay_eom (void *cls,
960 const struct GNUNET_PSYC_MessageHeader *msg,
961 const struct GNUNET_MessageHeader *pmsg,
963 uint8_t is_cancelled)
965 struct Place *plc = cls;
967 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
968 && GNUNET_YES == plc->is_host)
970 struct Host *hst = cls;
971 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
977 * Received a method to be saved to disk.
979 * Create a new file for writing the data part of the message into,
980 * if the file does not yet exist.
983 place_recv_save_method (void *cls,
984 const struct GNUNET_PSYC_MessageHeader *msg,
985 const struct GNUNET_PSYC_MessageMethod *meth,
987 const char *method_name)
989 struct Place *plc = cls;
990 plc->file_offset = 0;
991 plc->file_save = GNUNET_NO;
993 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
994 char *filename = NULL;
995 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
996 dir_social, DIR_SEPARATOR,
997 "files", DIR_SEPARATOR,
998 place_pub_str, DIR_SEPARATOR,
999 GNUNET_ntohll (msg->message_id));
1000 GNUNET_free (place_pub_str);
1002 /* save if does not already exist */
1003 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1005 if (0 == GNUNET_DISK_fn_write (filename, NULL, 0,
1006 GNUNET_DISK_PERM_USER_READ
1007 | GNUNET_DISK_PERM_USER_WRITE))
1009 plc->file_save = GNUNET_YES;
1016 GNUNET_free (filename);
1021 * Received a data fragment to be saved to disk.
1023 * Append data fragment to the file.
1026 place_recv_save_data (void *cls,
1027 const struct GNUNET_PSYC_MessageHeader *msg,
1028 const struct GNUNET_MessageHeader *pmsg,
1029 uint64_t message_id,
1033 struct Place *plc = cls;
1034 if (GNUNET_YES != plc->file_save)
1037 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
1038 char *filename = NULL;
1039 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
1040 dir_social, DIR_SEPARATOR,
1041 "files", DIR_SEPARATOR,
1042 place_pub_str, DIR_SEPARATOR,
1043 GNUNET_ntohll (msg->message_id));
1044 GNUNET_free (place_pub_str);
1045 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
1047 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "create", filename);
1048 GNUNET_free (filename);
1052 struct GNUNET_DISK_FileHandle *
1053 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
1054 GNUNET_DISK_PERM_NONE);
1057 if (plc->file_offset != GNUNET_DISK_file_seek
1058 (fh, plc->file_offset, GNUNET_DISK_SEEK_SET)) {
1059 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "seek", filename);
1060 GNUNET_DISK_file_close (fh);
1061 GNUNET_free (filename);
1064 GNUNET_DISK_file_write (fh, data, data_size);
1065 GNUNET_DISK_file_close (fh);
1066 GNUNET_free (filename);
1070 GNUNET_free (filename);
1073 plc->file_offset += data_size;
1078 * Received end of message to be saved to disk.
1080 * Remove .part ending from the filename.
1083 place_recv_save_eom (void *cls,
1084 const struct GNUNET_PSYC_MessageHeader *msg,
1085 const struct GNUNET_MessageHeader *pmsg,
1086 uint64_t message_id,
1087 uint8_t is_cancelled)
1089 struct Place *plc = cls;
1090 if (GNUNET_YES != plc->file_save)
1093 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
1095 GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64,
1096 dir_social, DIR_SEPARATOR,
1097 "files", DIR_SEPARATOR,
1098 place_pub_str, DIR_SEPARATOR,
1099 GNUNET_ntohll (msg->message_id));
1100 GNUNET_free (place_pub_str);
1101 char *fn_part = NULL;
1102 GNUNET_asprintf (&fn_part, "%s.part", fn);
1104 if (rename (fn_part, fn)) {
1105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1106 "Failed to rename %s into %s: %s (%d)\n",
1107 fn_part, fn, strerror (errno), errno);
1111 GNUNET_free (fn_part);
1116 * Initialize place data structure.
1119 place_init (struct Place *plc)
1121 plc->slicer = GNUNET_PSYC_slicer_create ();
1126 * Add a place to the @e places hash map.
1131 * @return #GNUNET_OK if the place was added
1132 * #GNUNET_NO if the place already exists in the hash map
1133 * #GNUNET_SYSERR on error
1136 place_add (const struct PlaceEnterRequest *ereq)
1138 struct EgoPlacePublicKey ego_place_pub_key = {
1139 .ego_pub_key = ereq->ego_pub_key,
1140 .place_pub_key = ereq->place_pub_key,
1142 struct GNUNET_HashCode ego_place_pub_hash;
1143 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1148 struct GNUNET_MessageHeader *
1149 place_msg = GNUNET_CONTAINER_multihashmap_get (places, &ego_place_pub_hash);
1150 if (NULL != place_msg)
1153 place_msg = GNUNET_copy_message (&ereq->header);
1154 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places, &ego_place_pub_hash, place_msg,
1155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1158 GNUNET_free (place_msg);
1159 return GNUNET_SYSERR;
1166 * Add a place to the @e app_places hash map.
1173 * @return #GNUNET_OK if the place was added
1174 * #GNUNET_NO if the place already exists in the hash map
1175 * #GNUNET_SYSERR on error
1178 app_place_add (const char *app_id,
1179 const struct PlaceEnterRequest *ereq)
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Adding app place to hashmap:\n");
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 " app_id = %s\n", app_id);
1186 struct GNUNET_HashCode app_id_hash;
1187 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1189 struct EgoPlacePublicKey ego_place_pub_key = {
1190 .ego_pub_key = ereq->ego_pub_key,
1191 .place_pub_key = ereq->place_pub_key,
1193 struct GNUNET_HashCode ego_place_pub_hash;
1194 GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key), &ego_place_pub_hash);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1199 struct GNUNET_CONTAINER_MultiHashMap *
1200 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1201 if (NULL == app_places)
1203 app_places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1204 GNUNET_CONTAINER_multihashmap_put (apps_places, &app_id_hash, app_places,
1205 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1208 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (app_places, &ego_place_pub_hash))
1211 if (GNUNET_SYSERR == place_add (ereq))
1213 return GNUNET_SYSERR;
1216 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (app_places, &ego_place_pub_hash, NULL,
1217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1220 return GNUNET_SYSERR;
1227 * Save place entry message to disk.
1232 * Entry request message.
1235 app_place_save (const char *app_id,
1236 const struct PlaceEnterRequest *ereq)
1238 if (GNUNET_SYSERR == app_place_add (app_id, ereq))
1243 if (NULL == dir_places)
1244 return GNUNET_SYSERR;
1246 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ereq->ego_pub_key);
1247 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&ereq->place_pub_key);
1248 char *filename = NULL;
1249 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
1250 dir_social, DIR_SEPARATOR,
1251 "places", DIR_SEPARATOR,
1252 ego_pub_str, DIR_SEPARATOR,
1254 int ret = GNUNET_DISK_directory_create_for_file (filename);
1255 if (GNUNET_OK != ret
1256 || 0 > GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size),
1257 GNUNET_DISK_PERM_USER_READ
1258 | GNUNET_DISK_PERM_USER_WRITE))
1261 ret = GNUNET_SYSERR;
1263 GNUNET_free (filename);
1265 if (ret == GNUNET_OK)
1267 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1268 dir_social, DIR_SEPARATOR,
1269 "apps", DIR_SEPARATOR,
1270 app_id, DIR_SEPARATOR,
1271 ego_pub_str, DIR_SEPARATOR,
1273 ret = GNUNET_DISK_directory_create_for_file (filename);
1274 if (GNUNET_OK != ret
1275 || 0 > GNUNET_DISK_fn_write (filename, "", 0,
1276 GNUNET_DISK_PERM_USER_READ
1277 | GNUNET_DISK_PERM_USER_WRITE))
1280 ret = GNUNET_SYSERR;
1282 GNUNET_free (filename);
1284 GNUNET_free (ego_pub_str);
1285 GNUNET_free (place_pub_str);
1291 app_place_remove (const char *app_id,
1292 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1293 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key)
1295 struct GNUNET_HashCode ego_pub_hash;
1296 struct GNUNET_HashCode place_pub_hash;
1297 GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash);
1298 GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash);
1300 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (ego_pub_key);
1301 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (place_pub_key);
1302 char *app_place_filename = NULL;
1303 GNUNET_asprintf (&app_place_filename,
1304 "%s%c" "%s%c" "%s%c" "%s%c" "%s",
1305 dir_social, DIR_SEPARATOR,
1306 "apps", DIR_SEPARATOR,
1307 app_id, DIR_SEPARATOR,
1308 ego_pub_str, DIR_SEPARATOR,
1310 GNUNET_free (ego_pub_str);
1311 GNUNET_free (place_pub_str);
1313 struct GNUNET_HashCode app_id_hash;
1314 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1316 struct GNUNET_CONTAINER_MultiHashMap *
1317 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1319 if (NULL != app_places)
1320 GNUNET_CONTAINER_multihashmap_remove (app_places, &place_pub_hash, NULL);
1322 int ret = GNUNET_OK;
1324 if (0 != unlink (app_place_filename))
1327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1328 "Error removing app place file: %s: %s (%d)\n",
1329 app_place_filename, strerror (errno), errno);
1330 ret = GNUNET_SYSERR;
1332 GNUNET_free (app_place_filename);
1339 * Enter place as host.
1342 * Host entry request.
1343 * @param[out] ret_hst
1344 * Returned Host struct.
1346 * @return #GNUNET_YES if the host entered the place just now,
1347 * #GNUNET_NO if the place is already entered,
1348 * #GNUNET_SYSERR if place_pub_key was set
1349 * but its private key was not found
1352 host_enter (const struct HostEnterRequest *hreq, struct Host **ret_hst)
1354 int ret = GNUNET_NO;
1355 struct GNUNET_HashCode place_pub_hash;
1356 GNUNET_CRYPTO_hash (&hreq->place_pub_key, sizeof (hreq->place_pub_key),
1358 struct Host *hst = GNUNET_CONTAINER_multihashmap_get (hosts, &place_pub_hash);
1362 hst = GNUNET_new (struct Host);
1363 hst->policy = hreq->policy;
1364 hst->join_reqs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1365 hst->relay_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1367 struct Place *plc = &hst->place;
1369 plc->is_host = GNUNET_YES;
1370 plc->pub_key = hreq->place_pub_key;
1371 plc->pub_key_hash = place_pub_hash;
1373 GNUNET_CONTAINER_multihashmap_put (hosts, &plc->pub_key_hash, plc,
1374 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1375 hst->master = GNUNET_PSYC_master_start (cfg, &hreq->place_key, hst->policy,
1376 &psyc_master_started,
1377 &psyc_recv_join_request,
1378 &psyc_recv_message, NULL, hst);
1379 plc->channel = GNUNET_PSYC_master_get_channel (hst->master);
1383 if (NULL != ret_hst)
1390 msg_proc_parse (const struct MsgProcRequest *mpreq,
1392 const char **method_prefix,
1393 struct GNUNET_HashCode *method_hash)
1395 ssize_t method_size = ntohs (mpreq->header.size) - sizeof (*mpreq);
1398 if (method_size < 0)
1400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1401 "MsgProcRequest has invalid size\n");
1402 return GNUNET_SYSERR;
1405 offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &mpreq[1],
1409 if (0 == offset || offset != method_size || *method_prefix == NULL)
1411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1412 "MsgProcRequest contains invalid method\n");
1413 return GNUNET_SYSERR;
1415 GNUNET_CRYPTO_hash (*method_prefix, (size_t) method_size, method_hash);
1416 *flags = ntohl (mpreq->flags);
1422 app_notify_place (const struct GNUNET_MessageHeader *msg,
1423 struct GNUNET_SERVICE_Client *client)
1425 struct AppPlaceMessage *amsg;
1426 struct GNUNET_MQ_Envelope *env;
1427 uint16_t msg_size = ntohs (msg->size);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "%p Sending place notification of type %u to client.\n",
1431 client, ntohs (msg->type));
1432 switch (ntohs (msg->type))
1434 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
1436 struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
1437 if (msg_size < sizeof (struct HostEnterRequest))
1439 env = GNUNET_MQ_msg (amsg,
1440 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
1441 // FIXME: also notify about not entered places
1442 amsg->place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
1443 amsg->is_host = GNUNET_YES;
1444 amsg->ego_pub_key = hreq->ego_pub_key;
1445 amsg->place_pub_key = hreq->place_pub_key;
1446 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1450 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
1452 if (msg_size < sizeof (struct GuestEnterRequest))
1454 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) msg;
1455 env = GNUNET_MQ_msg (amsg,
1456 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
1457 // FIXME: also notify about not entered places
1458 amsg->place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
1459 amsg->is_host = GNUNET_NO;
1460 amsg->ego_pub_key = greq->ego_pub_key;
1461 amsg->place_pub_key = greq->place_pub_key;
1462 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1473 app_notify_place_end (struct GNUNET_SERVICE_Client *client)
1475 struct GNUNET_MQ_Envelope *env;
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "%p Sending end of place list notification to client\n",
1480 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END);
1481 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1487 app_notify_ego (struct Ego *ego, struct GNUNET_SERVICE_Client *client)
1489 struct AppEgoMessage *emsg;
1490 struct GNUNET_MQ_Envelope *env;
1491 size_t name_size = strlen (ego->name) + 1;
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "%p Sending ego notification to client: %s\n",
1496 env = GNUNET_MQ_msg_extra (emsg,
1498 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO);
1499 GNUNET_CRYPTO_ecdsa_key_get_public (&ego->key, &emsg->ego_pub_key);
1500 GNUNET_memcpy (&emsg[1], ego->name, name_size);
1501 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1507 app_notify_ego_end (struct GNUNET_SERVICE_Client *client)
1509 struct GNUNET_MQ_Envelope *env;
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "%p Sending end of ego list notification to client\n",
1514 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END);
1515 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1521 app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key, void *value)
1523 struct GNUNET_MessageHeader *
1524 msg = GNUNET_CONTAINER_multihashmap_get (places, key);
1526 app_notify_place (msg, cls);
1532 ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
1534 app_notify_ego (value, cls);
1540 check_client_msg_proc_set (void *cls,
1541 const struct MsgProcRequest *mpreq)
1548 * Handle a client setting message proccesing flags for a method prefix.
1551 handle_client_msg_proc_set (void *cls,
1552 const struct MsgProcRequest *mpreq)
1554 struct Client *c = cls;
1555 struct GNUNET_SERVICE_Client *client = c->client;
1556 struct Place *plc = c->place;
1560 GNUNET_SERVICE_client_drop (client);
1564 const char *method_prefix = NULL;
1566 struct GNUNET_HashCode method_hash;
1569 msg_proc_parse (mpreq, &flags, &method_prefix, &method_hash))
1572 GNUNET_SERVICE_client_drop (client);
1576 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1577 place_recv_relay_method,
1578 place_recv_relay_modifier,
1579 place_recv_relay_data,
1580 place_recv_relay_eom);
1581 GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
1582 place_recv_save_method,
1584 place_recv_save_data,
1585 place_recv_save_eom);
1587 if (flags & GNUNET_SOCIAL_MSG_PROC_RELAY)
1589 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1590 place_recv_relay_method,
1591 place_recv_relay_modifier,
1592 place_recv_relay_data,
1593 place_recv_relay_eom,
1596 if (flags & GNUNET_SOCIAL_MSG_PROC_SAVE)
1598 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1599 place_recv_save_method,
1601 place_recv_save_data,
1602 place_recv_save_eom,
1606 /** @todo Save flags to be able to resume relaying/saving after restart */
1608 GNUNET_SERVICE_client_continue (client);
1613 * Handle a connecting client requesting to clear all relay rules.
1616 handle_client_msg_proc_clear (void *cls,
1617 const struct GNUNET_MessageHeader *msg)
1619 struct Client *c = cls;
1620 struct GNUNET_SERVICE_Client *client = c->client;
1621 struct Place *plc = c->place;
1625 GNUNET_SERVICE_client_drop (client);
1629 GNUNET_PSYC_slicer_clear (plc->slicer);
1631 GNUNET_SERVICE_client_continue (client);
1636 check_client_host_enter (void *cls,
1637 const struct HostEnterRequest *hr)
1644 * Handle a connecting client entering a place as host.
1647 handle_client_host_enter (void *cls,
1648 const struct HostEnterRequest *hr)
1650 struct Client *c = cls;
1651 struct GNUNET_SERVICE_Client *client = c->client;
1652 struct HostEnterRequest *
1653 hreq = (struct HostEnterRequest *) GNUNET_copy_message (&hr->header);
1655 uint8_t app_id_size = ntohs (hreq->header.size) - sizeof (*hreq);
1656 const char *app_id = NULL;
1657 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &hreq[1],
1658 app_id_size, 1, &app_id);
1659 if (0 == offset || offset != app_id_size || app_id == NULL)
1661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1662 "offset = %u, app_id_size = %u, app_id = %s\n",
1663 offset, app_id_size, app_id);
1665 GNUNET_SERVICE_client_drop (client);
1669 struct Host *hst = NULL;
1670 struct Place *plc = NULL;
1671 int ret = GNUNET_OK;
1673 struct GNUNET_CRYPTO_EddsaPublicKey empty_pub_key;
1674 memset (&empty_pub_key, 0, sizeof (empty_pub_key));
1676 if (0 == memcmp (&hreq->place_pub_key, &empty_pub_key, sizeof (empty_pub_key)))
1677 { // no public key set: create new private key & save the place
1678 struct GNUNET_CRYPTO_EddsaPrivateKey *
1679 place_key = GNUNET_CRYPTO_eddsa_key_create ();
1680 hreq->place_key = *place_key;
1681 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &hreq->place_pub_key);
1682 GNUNET_CRYPTO_eddsa_key_clear (place_key);
1683 GNUNET_free (place_key);
1685 app_place_save (app_id, (const struct PlaceEnterRequest *) hreq);
1688 switch (host_enter (hreq, &hst))
1691 plc = c->place = &hst->place;
1697 plc = c->place = &hst->place;
1699 client_send_host_enter_ack (client, hst, GNUNET_OK);
1703 ret = GNUNET_SYSERR;
1706 if (ret != GNUNET_SYSERR)
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "%p Client connected as host to place %s.\n",
1711 hst, GNUNET_h2s (&plc->pub_key_hash));
1713 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1714 cli->client = client;
1715 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1717 app_notify_place (&hreq->header, client);
1720 GNUNET_CRYPTO_eddsa_key_clear (&hreq->place_key);
1723 if (GNUNET_OK == ret)
1724 GNUNET_SERVICE_client_continue (client);
1726 GNUNET_SERVICE_client_drop (client);
1731 * Enter place as guest.
1734 * Guest entry request.
1735 * @param[out] ret_gst
1736 * Returned Guest struct.
1738 * @return #GNUNET_YES if the guest entered the place just now,
1739 * #GNUNET_NO if the place is already entered,
1740 * #GNUNET_SYSERR on error.
1743 guest_enter (const struct GuestEnterRequest *greq, struct Guest **ret_gst)
1745 int ret = GNUNET_NO;
1746 uint16_t greq_size = ntohs (greq->header.size);
1748 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key = greq->ego_pub_key;
1749 struct GNUNET_HashCode ego_pub_hash;
1750 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
1751 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
1755 return GNUNET_SYSERR;
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "entering as guest\n");
1760 struct GNUNET_HashCode place_pub_hash;
1761 GNUNET_CRYPTO_hash (&greq->place_pub_key, sizeof (greq->place_pub_key),
1764 struct GNUNET_CONTAINER_MultiHashMap *
1765 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests, &place_pub_hash);
1766 struct Guest *gst = NULL;
1769 if (NULL != plc_gst)
1770 gst = GNUNET_CONTAINER_multihashmap_get (plc_gst, &ego_pub_hash);
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1773 "plc_gst = %p, gst = %p\n",
1779 gst = GNUNET_new (struct Guest);
1780 new_guest = GNUNET_YES;
1782 else new_guest = GNUNET_NO;
1784 if (NULL == gst->slave)
1786 gst->origin = greq->origin;
1787 gst->relay_count = ntohl (greq->relay_count);
1790 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1791 const char *app_id = (const char *) &greq[1];
1792 const char *p = app_id;
1794 len = strnlen (app_id, remaining);
1795 if (len == remaining)
1799 return GNUNET_SYSERR;
1802 remaining -= len + 1;
1804 const struct GNUNET_PeerIdentity *relays = NULL;
1805 uint16_t relay_size = gst->relay_count * sizeof (*relays);
1806 if (remaining < relay_size)
1810 return GNUNET_SYSERR;
1813 relays = (const struct GNUNET_PeerIdentity *) p;
1815 remaining -= relay_size;
1817 struct GNUNET_PSYC_Message *join_msg = NULL;
1818 uint16_t join_msg_size = 0;
1820 if (sizeof (struct GNUNET_MessageHeader) <= remaining)
1822 join_msg = (struct GNUNET_PSYC_Message *) p;
1823 join_msg_size = ntohs (join_msg->header.size);
1825 remaining -= join_msg_size;
1829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1830 "%zu + %u + %u != %u\n",
1831 sizeof (*greq), relay_size, join_msg_size, greq_size);
1834 return GNUNET_SYSERR;
1838 gst->relays = GNUNET_malloc (relay_size);
1839 GNUNET_memcpy (gst->relays, relays, relay_size);
1842 gst->join_flags = ntohl (greq->flags);
1844 struct Place *plc = &gst->place;
1846 plc->is_host = GNUNET_NO;
1847 plc->pub_key = greq->place_pub_key;
1848 plc->pub_key_hash = place_pub_hash;
1849 plc->ego_pub_key = ego_pub_key;
1850 plc->ego_pub_hash = ego_pub_hash;
1851 plc->ego_key = ego->key;
1853 if (NULL == plc_gst)
1855 plc_gst = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1856 (void) GNUNET_CONTAINER_multihashmap_put (place_guests, &plc->pub_key_hash, plc_gst,
1857 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1859 if (GNUNET_YES == new_guest)
1861 (void) GNUNET_CONTAINER_multihashmap_put (plc_gst, &plc->ego_pub_hash, gst,
1862 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1863 (void) GNUNET_CONTAINER_multihashmap_put (guests, &plc->pub_key_hash, gst,
1864 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1868 = GNUNET_PSYC_slave_join (cfg, &plc->pub_key, &plc->ego_key,
1869 gst->join_flags, &gst->origin,
1870 gst->relay_count, gst->relays,
1871 &psyc_recv_message, NULL,
1872 &psyc_slave_connected,
1873 &psyc_recv_join_dcsn,
1875 plc->channel = GNUNET_PSYC_slave_get_channel (gst->slave);
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "slave entered channel %p\n",
1882 // TODO: explain to automatic code scanners why free(gst) not necessary
1883 if (NULL != ret_gst)
1890 client_guest_enter (struct Client *c,
1891 const struct GuestEnterRequest *greq)
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "client_guest_enter\n");
1895 struct GNUNET_PSYC_CountersResultMessage *result_msg;
1896 struct GNUNET_MQ_Envelope *env;
1897 struct GNUNET_SERVICE_Client *client = c->client;
1898 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
1899 const char *app_id = NULL;
1900 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &greq[1],
1901 remaining, 1, &app_id);
1902 struct Guest *gst = NULL;
1903 struct Place *plc = NULL;
1907 return GNUNET_SYSERR;
1909 switch (guest_enter (greq, &gst))
1913 plc = c->place = &gst->place;
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "guest entered successfully to local place %s\n",
1916 GNUNET_h2s (&plc->pub_key_hash));
1918 app_place_save (app_id, (const struct PlaceEnterRequest *) greq);
1919 app_notify_place (&greq->header, client);
1924 plc = c->place = &gst->place;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "guest re-entered successfully to local place %s\n",
1927 GNUNET_h2s (&plc->pub_key_hash));
1929 env = GNUNET_MQ_msg (result_msg,
1930 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
1931 result_msg->result_code = htonl (GNUNET_OK);
1932 result_msg->max_message_id = GNUNET_htonll (plc->max_message_id);
1933 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1935 if (NULL != gst->join_dcsn)
1937 env = GNUNET_MQ_msg_copy (&gst->join_dcsn->header);
1938 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1945 return GNUNET_SYSERR;
1949 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1950 cli->client = client;
1951 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1957 check_client_guest_enter (void *cls,
1958 const struct GuestEnterRequest *greq)
1965 * Handle a connecting client entering a place as guest.
1968 handle_client_guest_enter (void *cls,
1969 const struct GuestEnterRequest *greq)
1971 struct Client *c = cls;
1973 if (GNUNET_SYSERR == client_guest_enter (c, greq))
1976 GNUNET_SERVICE_client_drop (c->client);
1979 GNUNET_SERVICE_client_continue (c->client);
1983 struct GuestEnterByNameClosure
1985 struct Client *client;
1988 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1989 struct GNUNET_MessageHeader *join_msg;
1994 * Result of a GNS name lookup for entering a place.
1996 * @see GNUNET_SOCIAL_guest_enter_by_name
1999 gns_result_guest_enter (void *cls, uint32_t rd_count,
2000 const struct GNUNET_GNSRECORD_Data *rd)
2002 struct GuestEnterByNameClosure *gcls = cls;
2003 struct Client *c = gcls->client;
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2005 "%p GNS result: %u records.\n",
2008 const struct GNUNET_GNSRECORD_PlaceData *
2009 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
2011 if (0 == rd_count || rd->data_size < sizeof (*rec))
2014 GNUNET_SERVICE_client_drop (c->client);
2018 uint16_t relay_count = ntohl (rec->relay_count);
2019 struct GNUNET_PeerIdentity *relays = NULL;
2021 if (0 < relay_count)
2023 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
2025 relays = (struct GNUNET_PeerIdentity *) &rec[1];
2030 GNUNET_break_op (0);
2034 uint16_t app_id_size = strlen (gcls->app_id) + 1;
2035 uint16_t relay_size = relay_count * sizeof (*relays);
2036 uint16_t join_msg_size = 0;
2037 if (NULL != gcls->join_msg)
2038 join_msg_size = ntohs (gcls->join_msg->size);
2039 uint16_t greq_size = sizeof (struct GuestEnterRequest)
2040 + app_id_size + relay_size + join_msg_size;
2041 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
2042 greq->header.size = htons (greq_size);
2043 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
2044 greq->ego_pub_key = gcls->ego_pub_key;
2045 greq->place_pub_key = rec->place_pub_key;
2046 greq->origin = rec->origin;
2047 greq->relay_count = rec->relay_count;
2050 GNUNET_memcpy (p, gcls->app_id, app_id_size);
2052 GNUNET_memcpy (p, relays, relay_size);
2054 GNUNET_memcpy (p, gcls->join_msg, join_msg_size);
2056 client_guest_enter (c, greq);
2058 GNUNET_free (gcls->app_id);
2059 if (NULL != gcls->password)
2060 GNUNET_free (gcls->password);
2061 if (NULL != gcls->join_msg)
2062 GNUNET_free (gcls->join_msg);
2069 check_client_guest_enter_by_name (void *cls,
2070 const struct GuestEnterByNameRequest *greq)
2077 * Handle a connecting client entering a place as guest using a GNS address.
2079 * Look up GNS address and generate a GuestEnterRequest from that.
2082 handle_client_guest_enter_by_name (void *cls,
2083 const struct GuestEnterByNameRequest *greq)
2085 struct Client *c = cls;
2086 struct GNUNET_SERVICE_Client *client = c->client;
2088 struct GuestEnterByNameClosure *gcls = GNUNET_malloc (sizeof (*gcls));
2090 gcls->ego_pub_key = greq->ego_pub_key;
2092 const char *p = (const char *) &greq[1];
2093 const char *app_id = NULL, *password = NULL, *gns_name = NULL;
2094 uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
2095 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 3,
2100 remaining -= offset;
2102 if (0 != offset && sizeof (*gcls->join_msg) <= remaining)
2104 gcls->join_msg = GNUNET_copy_message ((struct GNUNET_MessageHeader *) p);
2105 remaining -= ntohs (gcls->join_msg->size);
2108 if (0 == offset || 0 != remaining)
2110 if (NULL != gcls->join_msg)
2111 GNUNET_free (gcls->join_msg);
2114 GNUNET_SERVICE_client_drop (client);
2118 uint16_t app_id_size = strlen (app_id) + 1;
2119 gcls->app_id = GNUNET_malloc (app_id_size);
2120 GNUNET_memcpy (gcls->app_id, app_id, app_id_size);
2122 uint16_t password_size = strlen (password);
2123 if (0 < password_size++)
2125 gcls->password = GNUNET_malloc (password_size);
2126 GNUNET_memcpy (gcls->password, password, password_size);
2129 GNUNET_GNS_lookup (gns, gns_name,
2131 GNUNET_GNSRECORD_TYPE_PLACE,
2132 GNUNET_GNS_LO_DEFAULT,
2133 &gns_result_guest_enter, gcls);
2134 GNUNET_SERVICE_client_continue (client);
2139 check_client_app_connect (void *cls,
2140 const struct AppConnectRequest *creq)
2147 * Handle application connection.
2150 handle_client_app_connect (void *cls,
2151 const struct AppConnectRequest *creq)
2153 struct Client *c = cls;
2154 struct GNUNET_SERVICE_Client *client = c->client;
2155 ssize_t app_id_size = ntohs (creq->header.size) - sizeof (*creq);
2156 const char *app_id = NULL;
2159 if (app_id_size < 0)
2161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2162 "AppConnectRequest has invalid size\n");
2164 GNUNET_SERVICE_client_drop (client);
2168 offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
2169 (size_t) app_id_size,
2172 if (0 == offset || offset != app_id_size)
2174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2175 "AppConnectRequest contains invalid app ID\n");
2177 GNUNET_SERVICE_client_drop (client);
2181 struct GNUNET_HashCode app_id_hash;
2182 GNUNET_CRYPTO_hash (app_id, (size_t) app_id_size, &app_id_hash);
2184 GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
2185 app_notify_ego_end (client);
2187 struct GNUNET_CONTAINER_MultiHashMap *
2188 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
2189 if (NULL != app_places)
2190 GNUNET_CONTAINER_multihashmap_iterate (app_places, app_place_entry_notify, client);
2191 app_notify_place_end (client);
2193 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
2194 cli->client = client;
2195 struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
2198 app = GNUNET_malloc (sizeof (*app));
2199 (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
2200 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2202 GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "%p Application %s connected.\n", app, app_id);
2207 c->app_id = GNUNET_malloc ((size_t) app_id_size);
2208 GNUNET_memcpy (c->app_id, app_id, (size_t) app_id_size);
2210 GNUNET_SERVICE_client_continue (client);
2215 * Handle application detach request.
2218 handle_client_app_detach (void *cls,
2219 const struct AppDetachRequest *req)
2221 struct Client *c = cls;
2222 struct GNUNET_SERVICE_Client *client = c->client;
2224 int ret = app_place_remove (c->app_id, &req->ego_pub_key, &req->place_pub_key);
2225 client_send_result (client, req->op_id, ret, NULL, 0);
2227 GNUNET_SERVICE_client_continue (client);
2232 place_leave_cb (void *cls)
2234 struct Place *plc = cls;
2236 place_send_leave_ack (plc);
2237 (GNUNET_YES == plc->is_host)
2238 ? cleanup_host ((struct Host *) plc)
2239 : cleanup_guest ((struct Guest *) plc);
2244 * Handle application leave request.
2247 handle_client_place_leave (void *cls,
2248 const struct GNUNET_MessageHeader *msg)
2250 struct Client *c = cls;
2251 struct GNUNET_SERVICE_Client *client = c->client;
2252 struct Place *plc = c->place;
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "got leave request from %s for place %s",
2256 plc->is_host? "host" : "slave",
2257 GNUNET_h2s (&plc->pub_key_hash));
2261 GNUNET_SERVICE_client_drop (client);
2265 if (GNUNET_YES != plc->is_disconnecting)
2267 plc->is_disconnecting = GNUNET_YES;
2270 struct Host *host = plc->host;
2271 GNUNET_assert (NULL != host);
2272 GNUNET_PSYC_master_stop (host->master, GNUNET_NO, &place_leave_cb, plc);
2276 struct Guest *guest = plc->guest;
2277 GNUNET_assert (NULL != guest);
2278 GNUNET_PSYC_slave_part (guest->slave, GNUNET_NO, &place_leave_cb, plc);
2283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2284 "got leave request but place is already leaving\n");
2286 GNUNET_SERVICE_client_continue (client);
2290 struct JoinDecisionClosure
2292 int32_t is_admitted;
2293 struct GNUNET_PSYC_Message *msg;
2298 * Iterator callback for responding to join requests.
2301 psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
2304 struct JoinDecisionClosure *jcls = cls;
2305 struct GNUNET_PSYC_JoinHandle *jh = value;
2306 // FIXME: add relays
2307 GNUNET_PSYC_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
2313 check_client_join_decision (void *cls,
2314 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2321 * Handle an entry decision from a host client.
2324 handle_client_join_decision (void *cls,
2325 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2327 struct Client *c = cls;
2328 struct GNUNET_SERVICE_Client *client = c->client;
2329 struct Place *plc = c->place;
2330 if (NULL == plc || GNUNET_YES != plc->is_host)
2333 GNUNET_SERVICE_client_drop (client);
2336 struct Host *hst = plc->host;
2338 struct JoinDecisionClosure jcls;
2339 jcls.is_admitted = ntohl (dcsn->is_admitted);
2341 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (dcsn->header.size))
2342 ? (struct GNUNET_PSYC_Message *) &dcsn[1]
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 struct GNUNET_HashCode slave_pub_hash;
2349 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "%p Got join decision (%d) from client for place %s..\n",
2354 hst, jcls.is_admitted, GNUNET_h2s (&plc->pub_key_hash));
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "%p ..and slave %s.\n",
2357 hst, GNUNET_h2s (&slave_pub_hash));
2359 GNUNET_CONTAINER_multihashmap_get_multiple (hst->join_reqs, &slave_pub_hash,
2360 &psyc_send_join_decision, &jcls);
2361 GNUNET_CONTAINER_multihashmap_remove_all (hst->join_reqs, &slave_pub_hash);
2363 GNUNET_SERVICE_client_continue (client);
2368 * Send acknowledgement to a client.
2370 * Sent after a message fragment has been passed on to multicast.
2372 * @param plc The place struct for the client.
2375 send_message_ack (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2377 struct GNUNET_MQ_Envelope *env;
2379 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
2380 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
2386 * Proceed to the next message part in the transmission queue.
2389 * Place where the transmission is going on.
2391 * Currently transmitted message.
2393 * Currently transmitted message fragment.
2395 * @return @a tmit_frag, or NULL if reached the end of fragment.
2397 static struct FragmentTransmitQueue *
2398 psyc_transmit_queue_next_part (struct Place *plc,
2399 struct MessageTransmitQueue *tmit_msg,
2400 struct FragmentTransmitQueue *tmit_frag)
2402 uint16_t psize = ntohs (tmit_frag->next_part->size);
2403 if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
2406 tmit_frag->next_part
2407 = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
2409 else /* Reached end of current fragment. */
2411 if (NULL != tmit_frag->client)
2412 send_message_ack (plc, tmit_frag->client);
2413 GNUNET_CONTAINER_DLL_remove (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2414 GNUNET_free (tmit_frag);
2422 * Proceed to next message in transmission queue.
2425 * Place where the transmission is going on.
2427 * Currently transmitted message.
2429 * @return The next message in queue, or NULL if queue is empty.
2431 static struct MessageTransmitQueue *
2432 psyc_transmit_queue_next_msg (struct Place *plc,
2433 struct MessageTransmitQueue *tmit_msg)
2435 GNUNET_CONTAINER_DLL_remove (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2436 GNUNET_free (tmit_msg);
2437 return plc->tmit_msgs_head;
2442 * Callback for data transmission to PSYC.
2445 psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2447 struct Place *plc = cls;
2448 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2449 GNUNET_assert (NULL != tmit_msg);
2450 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2451 if (NULL == tmit_frag)
2452 { /* Rest of the message have not arrived yet, pause transmission */
2456 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 "%p psyc_transmit_notify_data: nothing to send.\n", plc);
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 "%p psyc_transmit_notify_data()\n", plc);
2467 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2469 uint16_t ptype = ntohs (pmsg->type);
2470 uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
2475 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2476 if (*data_size < pdata_size)
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 "%p psyc_transmit_notify_data: buffer size too small for data.\n", plc);
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2484 "%p psyc_transmit_notify_data: sending %u bytes.\n",
2487 *data_size = pdata_size;
2488 GNUNET_memcpy (data, &pmsg[1], *data_size);
2492 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2497 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2499 ret = GNUNET_SYSERR;
2503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2504 "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
2506 ret = GNUNET_SYSERR;
2509 if (GNUNET_SYSERR == ret && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL != ptype)
2512 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2513 GNUNET_SERVICE_client_drop (tmit_frag->client);
2514 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2519 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2520 if (NULL != tmit_frag)
2522 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2523 ptype = ntohs (pmsg->type);
2526 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2529 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2530 ret = GNUNET_SYSERR;
2535 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2536 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2537 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2541 if (NULL == tmit_msg->frags_head
2542 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2543 { /* Reached end of current message. */
2544 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2548 if (ret != GNUNET_NO)
2550 if (NULL != tmit_msg)
2552 psyc_transmit_message (plc);
2554 /* FIXME: handle partial message (when still in_transmit) */
2561 * Callback for modifier transmission to PSYC.
2564 psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2565 uint8_t *oper, uint32_t *full_value_size)
2567 struct Place *plc = cls;
2568 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2569 GNUNET_assert (NULL != tmit_msg);
2570 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2571 if (NULL == tmit_frag)
2572 { /* Rest of the message have not arrived yet, pause transmission */
2576 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2580 "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2586 "%p psyc_transmit_notify_mod()\n", plc);
2587 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
2589 uint16_t ptype = ntohs (pmsg->type);
2594 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
2598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2599 "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
2600 ret = GNUNET_SYSERR;
2603 struct GNUNET_PSYC_MessageModifier *
2604 pmod = (struct GNUNET_PSYC_MessageModifier *) tmit_frag->next_part;
2605 uint16_t mod_size = ntohs (pmod->header.size) - sizeof (*pmod);
2607 if (*data_size < mod_size)
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2615 *full_value_size = ntohl (pmod->value_size);
2617 *data_size = mod_size;
2618 GNUNET_memcpy (data, &pmod[1], mod_size);
2623 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
2627 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2628 "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
2629 ret = GNUNET_SYSERR;
2632 uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
2633 if (*data_size < mod_size)
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2636 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc, mod_size);
2643 *data_size = mod_size;
2644 GNUNET_memcpy (data, &pmsg[1], *data_size);
2649 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2650 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2651 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2658 "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
2660 ret = GNUNET_SYSERR;
2663 if (GNUNET_SYSERR == ret)
2666 ret = GNUNET_SYSERR;
2667 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2668 GNUNET_SERVICE_client_drop (tmit_frag->client);
2669 GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
2673 if (GNUNET_YES != ret)
2674 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2676 if (NULL == tmit_msg->frags_head
2677 && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
2678 { /* Reached end of current message. */
2679 tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
2686 * Callback for data transmission from a host to PSYC.
2689 host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2691 int ret = psyc_transmit_notify_data (cls, data_size, data);
2693 if (GNUNET_NO != ret)
2695 struct Host *hst = cls;
2696 hst->tmit_handle = NULL;
2703 * Callback for the transmit functions of multicast.
2706 guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2708 int ret = psyc_transmit_notify_data (cls, data_size, data);
2710 if (GNUNET_NO != ret)
2712 struct Guest *gst = cls;
2713 gst->tmit_handle = NULL;
2720 * Callback for modifier transmission from a host to PSYC.
2723 host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2724 uint8_t *oper, uint32_t *full_value_size)
2726 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2727 oper, full_value_size);
2728 if (GNUNET_SYSERR == ret)
2730 struct Host *hst = cls;
2731 hst->tmit_handle = NULL;
2738 * Callback for modifier transmission from a guest to PSYC.
2741 guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2742 uint8_t *oper, uint32_t *full_value_size)
2744 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2745 oper, full_value_size);
2746 if (GNUNET_SYSERR == ret)
2748 struct Guest *gst = cls;
2749 gst->tmit_handle = NULL;
2756 * Get method part of next message from transmission queue.
2761 * @return #GNUNET_OK on success
2762 * #GNUNET_NO if there are no more messages in queue.
2763 * #GNUNET_SYSERR if the next message is malformed.
2765 static struct GNUNET_PSYC_MessageMethod *
2766 psyc_transmit_queue_next_method (struct Place *plc)
2768 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2769 if (NULL == tmit_msg)
2772 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2773 if (NULL == tmit_frag)
2779 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2781 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type))
2783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2784 "%p psyc_transmit_queue_next_method: unexpected message part of type %u.\n",
2785 plc, NULL != pmsg ? ntohs (pmsg->type) : 0);
2790 uint16_t psize = ntohs (pmsg->size);
2791 struct GNUNET_PSYC_MessageMethod *
2792 pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);
2794 if (psize < sizeof (*pmeth) + 1 || '\0' != *((char *) pmeth + psize - 1))
2796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2797 "%p psyc_transmit_queue_next_method: invalid method name.\n",
2799 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2800 "%zu <= %u || NUL != %u\n",
2801 sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
2803 GNUNET_free (pmeth);
2807 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2813 * Transmit the next message in queue from the host to the PSYC channel.
2816 psyc_master_transmit_message (struct Host *hst)
2818 struct Place *plc = &hst->place;
2820 if (NULL == hst->tmit_handle)
2822 struct GNUNET_PSYC_MessageMethod *
2823 pmeth = psyc_transmit_queue_next_method (plc);
2825 return GNUNET_SYSERR;
2827 hst->tmit_handle = (void *) &hst->tmit_handle;
2828 struct GNUNET_PSYC_MasterTransmitHandle *
2829 tmit_handle = GNUNET_PSYC_master_transmit (hst->master, (const char *) &pmeth[1],
2830 &host_transmit_notify_mod,
2831 &host_transmit_notify_data, hst,
2833 if (NULL != hst->tmit_handle)
2834 hst->tmit_handle = tmit_handle;
2835 GNUNET_free (pmeth);
2839 GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
2846 * Transmit the next message in queue from a guest to the PSYC channel.
2849 psyc_slave_transmit_message (struct Guest *gst)
2851 struct Place *plc = &gst->place;
2853 if (NULL == gst->tmit_handle)
2855 struct GNUNET_PSYC_MessageMethod *
2856 pmeth = psyc_transmit_queue_next_method (plc);
2858 return GNUNET_SYSERR;
2860 gst->tmit_handle = (void *) &gst->tmit_handle;
2861 struct GNUNET_PSYC_SlaveTransmitHandle *
2862 tmit_handle = GNUNET_PSYC_slave_transmit (gst->slave, (const char *) &pmeth[1],
2863 &guest_transmit_notify_mod,
2864 &guest_transmit_notify_data, gst,
2866 if (NULL != gst->tmit_handle)
2867 gst->tmit_handle = tmit_handle;
2868 GNUNET_free (pmeth);
2872 GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
2879 * Transmit a message to PSYC.
2882 psyc_transmit_message (struct Place *plc)
2886 ? psyc_master_transmit_message ((struct Host *) plc)
2887 : psyc_slave_transmit_message ((struct Guest *) plc);
2892 * Queue message parts for sending to PSYC.
2894 * @param plc Place to send to.
2895 * @param client Client the message originates from.
2896 * @param data_size Size of @a data.
2897 * @param data Concatenated message parts.
2898 * @param first_ptype First message part type in @a data.
2899 * @param last_ptype Last message part type in @a data.
2901 static struct MessageTransmitQueue *
2902 psyc_transmit_queue_message (struct Place *plc,
2903 struct GNUNET_SERVICE_Client *client,
2906 uint16_t first_ptype, uint16_t last_ptype,
2907 struct MessageTransmitQueue *tmit_msg)
2909 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
2911 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
2912 GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2914 else if (NULL == tmit_msg)
2919 struct FragmentTransmitQueue *
2920 tmit_frag = GNUNET_malloc (sizeof (*tmit_frag) + data_size);
2921 GNUNET_memcpy (&tmit_frag[1], data, data_size);
2922 tmit_frag->next_part = (struct GNUNET_MessageHeader *) &tmit_frag[1];
2923 tmit_frag->client = client;
2924 tmit_frag->size = data_size;
2926 GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2927 tmit_msg->client = client;
2933 // * Cancel transmission of current message to PSYC.
2935 // * @param plc Place to send to.
2936 // * @param client Client the message originates from.
2939 //psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2941 // uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2943 // struct GNUNET_MessageHeader msg;
2944 // msg.size = htons (sizeof (msg));
2945 // msg.type = htons (type);
2947 // psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
2948 // psyc_transmit_message (plc);
2950 // /* FIXME: cleanup */
2955 check_client_psyc_message (void *cls,
2956 const struct GNUNET_MessageHeader *msg)
2963 * Handle an incoming message from a client, to be transmitted to the place.
2966 handle_client_psyc_message (void *cls,
2967 const struct GNUNET_MessageHeader *msg)
2969 struct Client *c = cls;
2970 struct GNUNET_SERVICE_Client *client = c->client;
2971 struct Place *plc = c->place;
2976 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2977 "received PSYC message for non-existing client %p\n",
2980 GNUNET_SERVICE_client_drop (client);
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 "%p Received message of type %d from client.\n", plc, ntohs (msg->type));
2985 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);
2987 if (GNUNET_YES != plc->is_ready)
2989 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2990 "%p Place is not ready yet, disconnecting client.\n", plc);
2992 GNUNET_SERVICE_client_drop (client);
2996 uint16_t size = ntohs (msg->size);
2997 uint16_t psize = size - sizeof (*msg);
2998 if (psize < sizeof (struct GNUNET_MessageHeader)
2999 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < psize)
3001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3002 "%p Received message with invalid payload size (%u) from client.\n",
3005 GNUNET_SERVICE_client_drop (client);
3009 uint16_t first_ptype = 0;
3010 uint16_t last_ptype = 0;
3011 if (GNUNET_SYSERR ==
3012 GNUNET_PSYC_receive_check_parts (psize, (const char *) &msg[1],
3013 &first_ptype, &last_ptype))
3015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3016 "%p Received invalid message part from client.\n", plc);
3018 GNUNET_SERVICE_client_drop (client);
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3022 "%p Received message with first part type %u and last part type %u.\n",
3023 plc, first_ptype, last_ptype);
3026 = psyc_transmit_queue_message (plc, client, psize, &msg[1],
3027 first_ptype, last_ptype, c->tmit_msg);
3028 if (NULL != c->tmit_msg)
3030 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
3032 ret = psyc_transmit_message (plc);
3036 ret = GNUNET_SYSERR;
3038 if (GNUNET_OK != ret)
3040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3041 "%p Received invalid message part from client.\n", plc);
3043 GNUNET_SERVICE_client_drop (client);
3046 GNUNET_SERVICE_client_continue (client);
3051 * A historic message arrived from PSYC.
3054 psyc_recv_history_message (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
3056 struct OperationClosure *opcls = cls;
3057 struct Client *c = opcls->client;
3058 struct Place *plc = c->place;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3061 "%p Received historic message #%" PRId64 " (flags: %x)\n",
3062 plc, GNUNET_ntohll (msg->message_id), ntohl (msg->flags));
3064 uint16_t size = ntohs (msg->header.size);
3066 struct GNUNET_OperationResultMessage *
3067 res = GNUNET_malloc (sizeof (*res) + size);
3068 res->header.size = htons (sizeof (*res) + size);
3069 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
3070 res->op_id = opcls->op_id;
3071 res->result_code = GNUNET_htonll (GNUNET_OK);
3073 GNUNET_memcpy (&res[1], msg, size);
3075 /** @todo FIXME: send only to requesting client */
3076 place_send_msg (plc, GNUNET_MQ_msg_copy (&res->header));
3083 * Result of message history replay from PSYC.
3086 psyc_recv_history_result (void *cls, int64_t result,
3087 const void *err_msg, uint16_t err_msg_size)
3089 struct OperationClosure *opcls = cls;
3090 struct Client *c = opcls->client;
3091 struct Place *plc = c->place;
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3094 "%p History replay #%" PRIu64 ": "
3095 "PSYCstore returned %" PRId64 " (%.*s)\n",
3096 plc, GNUNET_ntohll (opcls->op_id), result,
3097 err_msg_size, (const char *) err_msg);
3099 // FIXME: place might have been destroyed
3100 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3105 check_client_history_replay (void *cls,
3106 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3113 * Client requests channel history.
3116 handle_client_history_replay (void *cls,
3117 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3119 struct Client *c = cls;
3120 struct GNUNET_SERVICE_Client *client = c->client;
3121 struct Place *plc = c->place;
3125 GNUNET_SERVICE_client_drop (client);
3129 uint16_t size = ntohs (req->header.size);
3130 const char *method_prefix = (const char *) &req[1];
3132 if (size < sizeof (*req) + 1
3133 || '\0' != method_prefix[size - sizeof (*req) - 1])
3135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3136 "%p History replay #%" PRIu64 ": "
3137 "invalid method prefix. size: %u < %zu?\n",
3138 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3140 GNUNET_SERVICE_client_drop (client);
3144 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3146 opcls->op_id = req->op_id;
3147 opcls->flags = ntohl (req->flags);
3149 if (0 == req->message_limit)
3150 GNUNET_PSYC_channel_history_replay (plc->channel,
3151 GNUNET_ntohll (req->start_message_id),
3152 GNUNET_ntohll (req->end_message_id),
3153 method_prefix, opcls->flags,
3154 psyc_recv_history_message, NULL,
3155 psyc_recv_history_result, opcls);
3157 GNUNET_PSYC_channel_history_replay_latest (plc->channel,
3158 GNUNET_ntohll (req->message_limit),
3159 method_prefix, opcls->flags,
3160 psyc_recv_history_message, NULL,
3161 psyc_recv_history_result, opcls);
3163 GNUNET_SERVICE_client_continue (client);
3168 * A state variable part arrived from PSYC.
3171 psyc_recv_state_var (void *cls,
3172 const struct GNUNET_MessageHeader *mod,
3175 uint32_t value_size,
3176 uint32_t full_value_size)
3178 struct GNUNET_OperationResultMessage *result_msg;
3179 struct GNUNET_MQ_Envelope *env;
3180 struct OperationClosure *opcls = cls;
3181 struct Client *c = opcls->client;
3182 struct Place *plc = c->place;
3183 uint16_t size = ntohs (mod->size);
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3186 "%p Received state variable %s from PSYC\n",
3188 env = GNUNET_MQ_msg_extra (result_msg,
3190 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
3191 result_msg->op_id = opcls->op_id;
3192 result_msg->result_code = GNUNET_htonll (GNUNET_OK);
3193 GNUNET_memcpy (&result_msg[1], mod, size);
3194 /** @todo FIXME: send only to requesting client */
3195 place_send_msg (plc, env);
3200 * Result of retrieving state variable from PSYC.
3203 psyc_recv_state_result (void *cls, int64_t result,
3204 const void *err_msg, uint16_t err_msg_size)
3206 struct OperationClosure *opcls = cls;
3207 struct Client *c = opcls->client;
3208 struct Place *plc = c->place;
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3211 "%p State get #%" PRIu64 ": "
3212 "PSYCstore returned %" PRId64 " (%.*s)\n",
3213 plc, GNUNET_ntohll (opcls->op_id), result,
3214 err_msg_size, (const char *) err_msg);
3216 // FIXME: place might have been destroyed
3217 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3222 check_client_state_get (void *cls,
3223 const struct GNUNET_PSYC_StateRequestMessage *req)
3230 * Client requests channel history.
3233 handle_client_state_get (void *cls,
3234 const struct GNUNET_PSYC_StateRequestMessage *req)
3236 struct Client *c = cls;
3237 struct GNUNET_SERVICE_Client *client = c->client;
3238 struct Place *plc = c->place;
3242 GNUNET_SERVICE_client_drop (client);
3246 uint16_t size = ntohs (req->header.size);
3247 const char *name = (const char *) &req[1];
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 "%p State get #%" PRIu64 ": %s\n",
3251 plc, GNUNET_ntohll (req->op_id), name);
3253 if (size < sizeof (*req) + 1
3254 || '\0' != name[size - sizeof (*req) - 1])
3256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3257 "%p State get #%" PRIu64 ": "
3258 "invalid name. size: %u < %zu?\n",
3259 plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
3261 GNUNET_SERVICE_client_drop (client);
3265 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3267 opcls->op_id = req->op_id;
3269 switch (ntohs (req->header.type))
3271 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET:
3272 GNUNET_PSYC_channel_state_get (plc->channel, name,
3273 psyc_recv_state_var,
3274 psyc_recv_state_result, opcls);
3277 case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX:
3278 GNUNET_PSYC_channel_state_get_prefix (plc->channel, name,
3279 psyc_recv_state_var,
3280 psyc_recv_state_result, opcls);
3287 GNUNET_SERVICE_client_continue (client);
3291 #define check_client_state_get_prefix check_client_state_get
3292 #define handle_client_state_get_prefix handle_client_state_get
3296 namestore_recv_records_store_result (void *cls, int32_t result,
3297 const char *err_msg)
3299 struct OperationClosure *opcls = cls;
3300 struct Client *c = opcls->client;
3302 // FIXME: client might have been disconnected
3303 client_send_result (c->client, opcls->op_id, result, err_msg,
3304 (NULL != err_msg) ? strlen (err_msg) : 0);
3305 GNUNET_free (opcls);
3310 check_client_zone_add_place (void *cls,
3311 const struct ZoneAddPlaceRequest *preq)
3318 * Handle request to add PLACE record to GNS zone.
3321 handle_client_zone_add_place (void *cls,
3322 const struct ZoneAddPlaceRequest *preq)
3324 struct Client *c = cls;
3325 struct GNUNET_SERVICE_Client *client = c->client;
3327 uint16_t remaining = ntohs (preq->header.size) - sizeof (*preq);
3328 const char *p = (const char *) &preq[1];
3329 const char *name = NULL, *password = NULL;
3330 uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 2,
3332 remaining -= offset;
3334 const struct GNUNET_PeerIdentity *
3335 relays = (const struct GNUNET_PeerIdentity *) p;
3336 uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);
3338 if (0 == offset || remaining != relay_size)
3341 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3342 GNUNET_SERVICE_client_drop (client);
3346 struct GNUNET_GNSRECORD_Data rd = { };
3347 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
3348 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3349 rd.expiration_time = GNUNET_ntohll (preq->expiration_time);
3351 struct GNUNET_GNSRECORD_PlaceData *
3352 rec = GNUNET_malloc (sizeof (*rec) + relay_size);
3353 rec->place_pub_key = preq->place_pub_key;
3354 rec->origin = this_peer;
3355 rec->relay_count = preq->relay_count;
3356 GNUNET_memcpy (&rec[1], relays, relay_size);
3359 rd.data_size = sizeof (*rec) + relay_size;
3361 struct GNUNET_HashCode ego_pub_hash;
3362 GNUNET_CRYPTO_hash (&preq->ego_pub_key, sizeof (preq->ego_pub_key), &ego_pub_hash);
3363 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3366 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3370 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3372 opcls->op_id = preq->op_id;
3373 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3375 namestore_recv_records_store_result, opcls);
3376 /** @todo refresh stored records later */
3378 GNUNET_SERVICE_client_continue (client);
3383 check_client_zone_add_nym (void *cls,
3384 const struct ZoneAddNymRequest *nreq)
3391 * Handle request to add PLACE record to GNS zone.
3394 handle_client_zone_add_nym (void *cls,
3395 const struct ZoneAddNymRequest *nreq)
3397 struct Client *c = cls;
3398 struct GNUNET_SERVICE_Client *client = c->client;
3400 uint16_t name_size = ntohs (nreq->header.size) - sizeof (*nreq);
3401 const char *name = NULL;
3402 uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &nreq[1],
3403 name_size, 1, &name);
3404 if (0 == offset || offset != name_size)
3407 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3408 GNUNET_SERVICE_client_continue (client);
3412 struct GNUNET_GNSRECORD_Data rd = { };
3413 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
3414 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
3415 rd.expiration_time = GNUNET_ntohll (nreq->expiration_time);
3416 rd.data = &nreq->nym_pub_key;
3417 rd.data_size = sizeof (nreq->nym_pub_key);
3419 struct GNUNET_HashCode ego_pub_hash;
3420 GNUNET_CRYPTO_hash (&nreq->ego_pub_key, sizeof (nreq->ego_pub_key), &ego_pub_hash);
3421 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3424 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3428 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3430 opcls->op_id = nreq->op_id;
3431 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3433 namestore_recv_records_store_result, opcls);
3434 /** @todo refresh stored records later */
3436 GNUNET_SERVICE_client_continue (client);
3441 path_basename (const char *path)
3443 const char *basename = strrchr (path, DIR_SEPARATOR);
3444 if (NULL != basename)
3447 if (NULL == basename || '\0' == *basename)
3454 struct PlaceLoadClosure
3457 const char *ego_pub_str;
3461 /** Load a place file */
3463 file_place_load (void *cls, const char *place_filename)
3465 struct PlaceLoadClosure *plcls = cls;
3467 const char *place_pub_str = path_basename (place_filename);
3468 if (NULL == place_pub_str)
3474 char *filename = NULL;
3475 GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
3476 dir_social, DIR_SEPARATOR,
3477 "places", DIR_SEPARATOR,
3478 plcls->ego_pub_str, DIR_SEPARATOR,
3481 uint64_t file_size = 0;
3483 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3484 || file_size < sizeof (struct PlaceEnterRequest))
3486 GNUNET_free (filename);
3490 struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
3491 ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
3492 GNUNET_free (filename);
3493 if (read_size < 0 || read_size < sizeof (*ereq))
3499 uint16_t ereq_size = ntohs (ereq->header.size);
3500 if (read_size != ereq_size)
3506 switch (ntohs (ereq->header.type))
3508 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3509 if (ereq_size < sizeof (struct HostEnterRequest))
3514 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3515 host_enter (hreq, NULL);
3518 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3519 if (ereq_size < sizeof (struct GuestEnterRequest))
3524 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3525 guest_enter (greq, NULL);
3533 if (GNUNET_SYSERR == app_place_add (plcls->app_id, ereq))
3543 * Read @e place_pub_str entries in @a dir_ego
3546 * Data directory of an application ego.
3547 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_str/
3550 scan_app_ego_dir (void *cls, const char *dir_ego)
3552 struct PlaceLoadClosure *plcls = cls;
3553 plcls->ego_pub_str = path_basename (dir_ego);
3555 if (NULL != plcls->ego_pub_str)
3556 GNUNET_DISK_directory_scan (dir_ego, file_place_load, plcls);
3562 * Read @e ego_pub_str entries in @a dir_app
3565 * Data directory of an application.
3566 * $GNUNET_DATA_HOME/social/apps/$app_id/
3569 scan_app_dir (void *cls, const char *dir_app)
3571 if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
3574 struct PlaceLoadClosure plcls;
3575 plcls.app_id = path_basename (dir_app);
3577 if (NULL != plcls.app_id)
3578 GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);
3585 identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
3586 void **ctx, const char *name)
3588 if (NULL == id_ego) // end of initial list of egos
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3592 "social service received ego %s\n",
3595 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
3596 GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);
3598 struct GNUNET_HashCode ego_pub_hash;
3599 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
3601 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3602 if (NULL == ego && NULL == name)
3604 // an ego that is none of our business has been deleted
3609 // one of our egos has been changed
3610 GNUNET_free (ego->name);
3613 // one of our egos has been deleted
3614 GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
3621 ego = GNUNET_malloc (sizeof (*ego));
3623 ego->key = *(GNUNET_IDENTITY_ego_get_private_key (id_ego));
3624 size_t name_size = strlen (name) + 1;
3625 ego->name = GNUNET_malloc (name_size);
3626 GNUNET_memcpy (ego->name, name, name_size);
3628 GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
3629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3631 // FIXME: notify clients about changed ego
3636 * Initialize the PSYC service.
3638 * @param cls Closure.
3639 * @param server The initialized server.
3640 * @param c Configuration to use.
3644 const struct GNUNET_CONFIGURATION_Handle *c,
3645 struct GNUNET_SERVICE_Handle *svc)
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "starting social service\n");
3652 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
3654 hosts = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3655 guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
3656 place_guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3658 egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3659 apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3660 places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3661 apps_places = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3662 //places_apps = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
3664 id = GNUNET_IDENTITY_connect (cfg, &identity_recv_ego, NULL);
3665 gns = GNUNET_GNS_connect (cfg);
3666 namestore = GNUNET_NAMESTORE_connect (cfg);
3667 stats = GNUNET_STATISTICS_create ("social", cfg);
3670 GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
3673 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3674 "social", "DATA_HOME");
3678 GNUNET_asprintf (&dir_places, "%s%c%s",
3679 dir_social, DIR_SEPARATOR, "places");
3680 GNUNET_asprintf (&dir_apps, "%s%c%s",
3681 dir_social, DIR_SEPARATOR, "apps");
3683 GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);
3685 GNUNET_SCHEDULER_add_shutdown (shutdown_task, NULL);
3690 * Define "main" method using service macro.
3694 GNUNET_SERVICE_OPTION_NONE,
3696 client_notify_connect,
3697 client_notify_disconnect,
3699 GNUNET_MQ_hd_var_size (client_host_enter,
3700 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER,
3701 struct HostEnterRequest,
3703 GNUNET_MQ_hd_var_size (client_guest_enter,
3704 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER,
3705 struct GuestEnterRequest,
3707 GNUNET_MQ_hd_var_size (client_guest_enter_by_name,
3708 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME,
3709 struct GuestEnterByNameRequest,
3711 GNUNET_MQ_hd_var_size (client_join_decision,
3712 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
3713 struct GNUNET_PSYC_JoinDecisionMessage,
3715 GNUNET_MQ_hd_var_size (client_psyc_message,
3716 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
3717 struct GNUNET_MessageHeader,
3719 GNUNET_MQ_hd_var_size (client_history_replay,
3720 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY,
3721 struct GNUNET_PSYC_HistoryRequestMessage,
3723 GNUNET_MQ_hd_var_size (client_state_get,
3724 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
3725 struct GNUNET_PSYC_StateRequestMessage,
3727 GNUNET_MQ_hd_var_size (client_state_get_prefix,
3728 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
3729 struct GNUNET_PSYC_StateRequestMessage,
3731 GNUNET_MQ_hd_var_size (client_zone_add_place,
3732 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE,
3733 struct ZoneAddPlaceRequest,
3735 GNUNET_MQ_hd_var_size (client_zone_add_nym,
3736 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM,
3737 struct ZoneAddNymRequest,
3739 GNUNET_MQ_hd_var_size (client_app_connect,
3740 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT,
3741 struct AppConnectRequest,
3743 GNUNET_MQ_hd_fixed_size (client_app_detach,
3744 GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH,
3745 struct AppDetachRequest,
3747 GNUNET_MQ_hd_fixed_size (client_place_leave,
3748 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE,
3749 struct GNUNET_MessageHeader,
3751 GNUNET_MQ_hd_var_size (client_msg_proc_set,
3752 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET,
3753 struct MsgProcRequest,
3755 GNUNET_MQ_hd_fixed_size (client_msg_proc_clear,
3756 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR,
3757 struct GNUNET_MessageHeader,
3760 /* end of gnunet-service-social.c */