aboutsummaryrefslogtreecommitdiff
path: root/src/social/gnunet-service-social.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/social/gnunet-service-social.c')
-rw-r--r--src/social/gnunet-service-social.c3760
1 files changed, 3760 insertions, 0 deletions
diff --git a/src/social/gnunet-service-social.c b/src/social/gnunet-service-social.c
new file mode 100644
index 0000000..33fabae
--- /dev/null
+++ b/src/social/gnunet-service-social.c
@@ -0,0 +1,3760 @@
1/*
2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
4 *
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.
9 *
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.
14 *
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file social/gnunet-service-social.c
23 * @brief Social service
24 * @author Gabor X Toth
25 */
26
27#include <inttypes.h>
28#include <strings.h>
29
30#include "platform.h"
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"
41#include "social.h"
42
43
44/**
45 * Handle to our current configuration.
46 */
47static const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49/**
50 * Service handle.
51 */
52static struct GNUNET_SERVICE_Handle *service;
53
54/* Handles to other services */
55static struct GNUNET_IDENTITY_Handle *id;
56static struct GNUNET_GNS_Handle *gns;
57static struct GNUNET_NAMESTORE_Handle *namestore;
58static struct GNUNET_STATISTICS_Handle *stats;
59
60/**
61 * ID of this peer.
62 */
63static struct GNUNET_PeerIdentity this_peer;
64
65/**
66 * All connected hosts.
67 * H(place_pub_key) -> struct Host
68 */
69static struct GNUNET_CONTAINER_MultiHashMap *hosts;
70
71/**
72 * All connected guests.
73 * H(place_pub_key) -> struct Guest
74 */
75static struct GNUNET_CONTAINER_MultiHashMap *guests;
76
77/**
78 * Connected guests per place.
79 * H(place_pub_key) -> ego_pub_key -> struct Guest
80 */
81static struct GNUNET_CONTAINER_MultiHashMap *place_guests;
82
83/**
84 * Places entered as host or guest.
85 * H(place_pub_key) -> struct HostEnterRequest OR struct GuestEnterRequest
86 */
87static struct GNUNET_CONTAINER_MultiHashMap *places;
88
89/**
90 * Places entered per application.
91 * H(app_id) -> H(place_pub_key) -> NULL
92 */
93static struct GNUNET_CONTAINER_MultiHashMap *apps_places;
94
95/**
96 * Application subscriptions per place.
97 * H(place_pub_key) -> H(app_id)
98 */
99//static struct GNUNET_CONTAINER_MultiHashMap *places_apps;
100
101/**
102 * Connected applications.
103 * H(app_id) -> struct Application
104 */
105static struct GNUNET_CONTAINER_MultiHashMap *apps;
106
107/**
108 * All egos.
109 * H(ego_pub_key) -> struct Ego
110 */
111static struct GNUNET_CONTAINER_MultiHashMap *egos;
112
113/**
114 * Directory for storing social data.
115 * Default: $GNUNET_DATA_HOME/social
116 */
117static char *dir_social;
118
119/**
120 * Directory for storing place data.
121 * $dir_social/places
122 */
123static char *dir_places;
124
125/**
126 * Directory for storing app data.
127 * $dir_social/apps
128 */
129static char *dir_apps;
130
131
132/**
133 * Message fragment transmission queue.
134 */
135struct FragmentTransmitQueue
136{
137 struct FragmentTransmitQueue *prev;
138 struct FragmentTransmitQueue *next;
139
140 struct GNUNET_SERVICE_Client *client;
141
142 /**
143 * Pointer to the next message part inside the data after this struct.
144 */
145 struct GNUNET_MessageHeader *next_part;
146
147 /**
148 * Size of message.
149 */
150 uint16_t size;
151
152 /**
153 * @see enum GNUNET_PSYC_MessageState
154 */
155 uint8_t state;
156
157 /* Followed by one or more message parts. */
158};
159
160
161/**
162 * Message transmission queue.
163 */
164struct MessageTransmitQueue
165{
166 struct MessageTransmitQueue *prev;
167 struct MessageTransmitQueue *next;
168
169 struct FragmentTransmitQueue *frags_head;
170 struct FragmentTransmitQueue *frags_tail;
171
172 struct GNUNET_SERVICE_Client *client;
173};
174
175/**
176 * List of connected clients.
177 */
178struct ClientListItem
179{
180 struct ClientListItem *prev;
181 struct ClientListItem *next;
182
183 struct GNUNET_SERVICE_Client *client;
184};
185
186
187/**
188 * Common part of the client context for both a host and guest.
189 */
190struct Place
191{
192 struct ClientListItem *clients_head;
193 struct ClientListItem *clients_tail;
194
195 struct MessageTransmitQueue *tmit_msgs_head;
196 struct MessageTransmitQueue *tmit_msgs_tail;
197
198 struct GNUNET_PSYC_Channel *channel;
199
200 /**
201 * Private key of home in case of a host.
202 */
203 struct GNUNET_CRYPTO_EddsaPublicKey key;
204
205 /**
206 * Public key of place.
207 */
208 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
209
210 /**
211 * Hash of @a pub_key.
212 */
213 struct GNUNET_HashCode pub_key_hash;
214
215 /**
216 * Private key of ego.
217 */
218 struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
219
220 /**
221 * Public key of ego.
222 */
223 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
224
225 /**
226 * Hash of @a ego_pub_key.
227 */
228 struct GNUNET_HashCode ego_pub_hash;
229
230 /**
231 * Slicer for processing incoming messages.
232 */
233 struct GNUNET_PSYC_Slicer *slicer;
234
235 /**
236 * Last message ID received for the place.
237 * 0 if there is no such message.
238 */
239 uint64_t max_message_id;
240
241 /**
242 * Offset where the file is currently being written.
243 */
244 uint64_t file_offset;
245
246 /**
247 * Whether or not to save the file (#GNUNET_YES or #GNUNET_NO)
248 */
249 uint8_t file_save;
250
251 /**
252 * Is this place ready to receive messages from client?
253 * #GNUNET_YES or #GNUNET_NO
254 */
255 uint8_t is_ready;
256
257 /**
258 * Is the client disconnecting?
259 * #GNUNET_YES or #GNUNET_NO
260 */
261 uint8_t is_disconnecting;
262
263 /**
264 * Is this a host (#GNUNET_YES), or guest (#GNUNET_NO)?
265 */
266 uint8_t is_host;
267
268 union {
269 struct Host *host;
270 struct Guest *guest;
271 };
272};
273
274
275/**
276 * Client context for a host.
277 */
278struct Host
279{
280 /**
281 * Place struct common for Host and Guest
282 */
283 struct Place place;
284
285 /**
286 * Handle for the multicast origin.
287 */
288 struct GNUNET_PSYC_Master *master;
289
290 /**
291 * Transmit handle for multicast.
292 */
293 struct GNUNET_PSYC_MasterTransmitHandle *tmit_handle;
294
295 /**
296 * Incoming join requests.
297 * guest_key -> struct GNUNET_PSYC_JoinHandle *
298 */
299 struct GNUNET_CONTAINER_MultiHashMap *join_reqs;
300
301 /**
302 * Messages being relayed.
303 */
304 struct GNUNET_CONTAINER_MultiHashMap *relay_msgs;
305
306 /**
307 * @see enum GNUNET_PSYC_Policy
308 */
309 enum GNUNET_PSYC_Policy policy;
310};
311
312
313/**
314 * Client context for a guest.
315 */
316struct Guest
317{
318 /**
319 * Place struct common for Host and Guest.
320 */
321 struct Place place;
322
323 /**
324 * Handle for the PSYC slave.
325 */
326 struct GNUNET_PSYC_Slave *slave;
327
328 /**
329 * Transmit handle for multicast.
330 */
331 struct GNUNET_PSYC_SlaveTransmitHandle *tmit_handle;
332
333 /**
334 * Peer identity of the origin.
335 */
336 struct GNUNET_PeerIdentity origin;
337
338 /**
339 * Number of items in @a relays.
340 */
341 uint32_t relay_count;
342
343 /**
344 * Relays that multicast can use to connect.
345 */
346 struct GNUNET_PeerIdentity *relays;
347
348 /**
349 * Join request to be transmitted to the master on join.
350 */
351 struct GNUNET_MessageHeader *join_req; // FIXME: not used!
352
353 /**
354 * Join decision received from PSYC.
355 */
356 struct GNUNET_PSYC_JoinDecisionMessage *join_dcsn;
357
358 /**
359 * Join flags for the PSYC service.
360 */
361 enum GNUNET_PSYC_SlaveJoinFlags join_flags;
362};
363
364
365/**
366 * Context for a client.
367 */
368struct Client
369{
370 /**
371 * Client handle.
372 */
373 struct GNUNET_SERVICE_Client *client;
374
375 /**
376 * Place where the client entered.
377 */
378 struct Place *place;
379
380 /**
381 * Message queue for the message currently being transmitted
382 * by this client.
383 */
384 struct MessageTransmitQueue *tmit_msg;
385
386 /**
387 * ID for application clients.
388 */
389 char *app_id;
390};
391
392
393struct Application
394{
395 struct ClientListItem *clients_head;
396 struct ClientListItem *clients_tail;
397};
398
399
400struct Ego {
401 struct GNUNET_CRYPTO_EcdsaPrivateKey key;
402 char *name;
403};
404
405
406struct OperationClosure
407{
408 struct Client *client;
409 uint64_t op_id;
410 uint32_t flags;
411};
412
413
414static int
415psyc_transmit_message (struct Place *plc);
416
417
418/**
419 * Clean up place data structures after a client disconnected.
420 *
421 * @param cls the `struct Place` to clean up
422 */
423static void
424cleanup_place (void *cls);
425
426
427static struct MessageTransmitQueue *
428psyc_transmit_queue_message (struct Place *plc,
429 struct GNUNET_SERVICE_Client *client,
430 size_t data_size,
431 const void *data,
432 uint16_t first_ptype, uint16_t last_ptype,
433 struct MessageTransmitQueue *tmit_msg);
434
435
436static int
437place_entry_cleanup (void *cls,
438 const struct GNUNET_HashCode *key,
439 void *value)
440{
441 struct Place *plc = value;
442
443 cleanup_place (plc);
444 return GNUNET_YES;
445}
446
447
448/**
449 * Task run during shutdown.
450 *
451 * @param cls unused
452 */
453static void
454shutdown_task (void *cls)
455{
456 GNUNET_CONTAINER_multihashmap_iterate (hosts, place_entry_cleanup, NULL);
457 GNUNET_CONTAINER_multihashmap_iterate (guests, place_entry_cleanup, NULL);
458
459 if (NULL != id)
460 {
461 GNUNET_IDENTITY_disconnect (id);
462 id = NULL;
463 }
464 if (NULL != namestore)
465 {
466 GNUNET_NAMESTORE_disconnect (namestore);
467 namestore = NULL;
468 }
469 if (NULL != gns)
470 {
471 GNUNET_GNS_disconnect (gns);
472 gns = NULL;
473 }
474 if (NULL != stats)
475 {
476 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
477 stats = NULL;
478 }
479}
480
481
482/**
483 * Clean up host data structures after a client disconnected.
484 */
485static void
486cleanup_host (struct Host *hst)
487{
488 struct Place *plc = &hst->place;
489
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);
493}
494
495
496/**
497 * Clean up guest data structures after a client disconnected.
498 */
499static void
500cleanup_guest (struct Guest *gst)
501{
502 struct Place *plc = &gst->place;
503 struct GNUNET_CONTAINER_MultiHashMap *
504 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests,
505 &plc->pub_key_hash);
506 if (NULL != plc_gst)
507 {
508 GNUNET_CONTAINER_multihashmap_remove (plc_gst, &plc->ego_pub_hash, gst);
509
510 if (0 == GNUNET_CONTAINER_multihashmap_size (plc_gst))
511 {
512 GNUNET_CONTAINER_multihashmap_remove (place_guests, &plc->pub_key_hash,
513 plc_gst);
514 GNUNET_CONTAINER_multihashmap_destroy (plc_gst);
515 }
516 }
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);
523}
524
525
526/**
527 * Clean up place data structures after a client disconnected.
528 *
529 * @param cls the `struct Place` to clean up
530 */
531static void
532cleanup_place (void *cls)
533{
534 struct Place *plc = cls;
535
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "cleaning up place %s\n",
538 GNUNET_h2s (&plc->pub_key_hash));
539
540 (GNUNET_YES == plc->is_host)
541 ? cleanup_host ((struct Host *) plc)
542 : cleanup_guest ((struct Guest *) plc);
543
544 GNUNET_PSYC_slicer_destroy (plc->slicer);
545 GNUNET_free (plc);
546}
547
548
549/**
550 * Called whenever a client is disconnected.
551 * Frees our resources associated with that client.
552 *
553 * @param cls closure
554 * @param client identification of the client
555 * @param app_ctx must match @a client
556 */
557static void
558client_notify_disconnect (void *cls,
559 struct GNUNET_SERVICE_Client *client,
560 void *app_ctx)
561{
562 struct Client *c = app_ctx;
563 struct Place *plc = c->place;
564
565 if (NULL != c->app_id)
566 GNUNET_free (c->app_id);
567
568 GNUNET_free (c);
569
570 if (NULL == plc)
571 return; // application client, nothing to do
572
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));
577
578 struct ClientListItem *cli = plc->clients_head;
579 while (NULL != cli)
580 {
581 if (cli->client == client)
582 {
583 GNUNET_CONTAINER_DLL_remove (plc->clients_head,
584 plc->clients_tail,
585 cli);
586 GNUNET_free (cli);
587 break;
588 }
589 cli = cli->next;
590 }
591 if (GNUNET_YES == plc->is_disconnecting)
592 {
593 GNUNET_PSYC_slicer_destroy (plc->slicer);
594 GNUNET_free (plc);
595 }
596}
597
598
599/**
600 * A new client connected.
601 *
602 * @param cls NULL
603 * @param client client to add
604 * @param mq message queue for @a client
605 * @return @a client
606 */
607static void *
608client_notify_connect (void *cls,
609 struct GNUNET_SERVICE_Client *client,
610 struct GNUNET_MQ_Handle *mq)
611{
612 struct Client *c = GNUNET_new (struct Client);
613
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Client %p connected with queue %p\n",
616 client,
617 mq);
618 c->client = client;
619 return c;
620}
621
622
623/**
624 * Send message to all clients connected to a place and
625 * takes care of freeing @env.
626 */
627static void
628place_send_msg (const struct Place *plc,
629 struct GNUNET_MQ_Envelope *env)
630{
631 struct ClientListItem *cli = plc->clients_head;
632
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "%p Sending message to clients of place.\n", plc);
635 while (NULL != cli)
636 {
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Sending message to client %p\n",
639 cli);
640 GNUNET_MQ_send_copy (GNUNET_SERVICE_client_get_mq (cli->client),
641 env);
642 cli = cli->next;
643 }
644 GNUNET_MQ_discard (env);
645}
646
647
648static void
649place_send_leave_ack (struct Place *plc)
650{
651 struct GNUNET_MQ_Envelope *env;
652
653 for (struct ClientListItem *cli = plc->clients_head;
654 NULL != cli;
655 cli = cli->next)
656 {
657 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK);
658 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (cli->client),
659 env);
660 }
661}
662
663
664/**
665 * Send a result code back to the client.
666 *
667 * @param client
668 * Client that should receive the result code.
669 * @param result_code
670 * Code to transmit.
671 * @param op_id
672 * Operation ID in network byte order.
673 * @param data
674 * Data payload or NULL.
675 * @param data_size
676 * Size of @a data.
677 */
678static void
679client_send_result (struct GNUNET_SERVICE_Client *client, uint64_t op_id,
680 int64_t result_code, const void *data, uint16_t data_size)
681{
682 struct GNUNET_MQ_Envelope *env;
683 struct GNUNET_OperationResultMessage *res;
684
685 env = GNUNET_MQ_msg_extra (res,
686 data_size,
687 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
688 res->result_code = GNUNET_htonll (result_code);
689 res->op_id = op_id;
690 if (0 < data_size)
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);
697}
698
699
700static void
701client_send_host_enter_ack (struct GNUNET_SERVICE_Client *client,
702 struct Host *hst, uint32_t result)
703{
704 struct GNUNET_MQ_Envelope *env;
705 struct HostEnterAck *hack;
706 struct Place *plc = &hst->place;
707
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;
713
714 if (NULL != client)
715 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
716 env);
717 else
718 place_send_msg (plc, env);
719}
720
721
722/**
723 * Called after a PSYC master is started.
724 */
725static void
726psyc_master_started (void *cls, int result, uint64_t max_message_id)
727{
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;
732
733 client_send_host_enter_ack (NULL, hst, result);
734}
735
736
737/**
738 * Called when a PSYC master receives a join request.
739 */
740static void
741psyc_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)
746{
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));
754}
755
756
757/**
758 * Called after a PSYC slave is connected.
759 */
760static void
761psyc_slave_connected (void *cls, int result, uint64_t max_message_id)
762{
763 struct GNUNET_PSYC_CountersResultMessage *res;
764 struct GNUNET_MQ_Envelope *env;
765 struct Guest *gst = cls;
766 struct Place *plc = &gst->place;
767
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);
772 res->result_code =
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);
776}
777
778
779static void
780slave_parted_after_join_decision (void *cls)
781{
782 struct Guest *gst = cls;
783
784 GNUNET_assert (NULL != gst->join_dcsn);
785 place_send_msg (&gst->place, GNUNET_MQ_msg_copy (&gst->join_dcsn->header));
786}
787
788
789/**
790 * Called when a PSYC slave receives a join decision.
791 */
792static void
793psyc_recv_join_dcsn (void *cls,
794 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
795 int is_admitted,
796 const struct GNUNET_PSYC_Message *join_msg)
797{
798 struct Guest *gst = cls;
799
800 gst->join_dcsn = GNUNET_malloc (dcsn->header.size);
801 GNUNET_memcpy (gst->join_dcsn,
802 dcsn,
803 dcsn->header.size);
804 if (GNUNET_NO == is_admitted)
805 {
806 GNUNET_PSYC_slave_part (gst->slave,
807 GNUNET_NO,
808 &slave_parted_after_join_decision,
809 gst);
810 gst->slave = NULL;
811 return;
812 }
813 place_send_msg (&gst->place, GNUNET_MQ_msg_copy (&gst->join_dcsn->header));
814}
815
816
817/**
818 * Called when a PSYC master or slave receives a message.
819 */
820static void
821psyc_recv_message (void *cls,
822 const struct GNUNET_PSYC_MessageHeader *msg)
823{
824 struct Place *plc = cls;
825
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);
830 GNUNET_free (str);
831
832 GNUNET_PSYC_slicer_message (plc->slicer, msg);
833
834 place_send_msg (plc, GNUNET_MQ_msg_copy (&msg->header));
835}
836
837
838/**
839 * Relay a message part received from a guest to the the place.
840 *
841 * @param hst
842 * Host.
843 * @param pmsg
844 * Message part.
845 * @param nym_pub_key
846 * Nym the message is received from.
847 */
848static void
849host_relay_message_part (struct Host *hst,
850 const struct GNUNET_MessageHeader *pmsg,
851 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key)
852{
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);
856
857 struct MessageTransmitQueue *
858 tmit_msg = GNUNET_CONTAINER_multihashmap_get (hst->relay_msgs, &nym_pub_hash);
859
860 uint16_t ptype = ntohs (pmsg->type);
861
862 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
863 {
864 /* FIXME: last message was unfinished, cancel & remove from queue */
865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
866 "FIXME: last message was unfinished.\n");
867 }
868
869 tmit_msg = psyc_transmit_queue_message (&hst->place, NULL, ntohs (pmsg->size),
870 pmsg, ptype, ptype, tmit_msg);
871
872 switch (ptype)
873 {
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));
878 break;
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));
883 break;
884 }
885}
886
887
888/**
889 * Received a method to be relayed from a guest.
890 */
891static void
892place_recv_relay_method (void *cls,
893 const struct GNUNET_PSYC_MessageHeader *msg,
894 const struct GNUNET_PSYC_MessageMethod *meth,
895 uint64_t message_id,
896 const char *method_name)
897{
898 struct Place *plc = cls;
899
900 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
901 && GNUNET_YES == plc->is_host)
902 {
903 struct Host *hst = cls;
904 host_relay_message_part (hst, &meth->header, &msg->slave_pub_key);
905 }
906}
907
908
909/**
910 * Received a modifier to be relayed from a guest.
911 */
912static void
913place_recv_relay_modifier (void *cls,
914 const struct GNUNET_PSYC_MessageHeader *msg,
915 const struct GNUNET_MessageHeader *pmsg,
916 uint64_t message_id,
917 enum GNUNET_PSYC_Operator oper,
918 const char *name,
919 const void *value,
920 uint16_t value_size,
921 uint16_t full_value_size)
922{
923 struct Place *plc = cls;
924
925 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
926 && GNUNET_YES == plc->is_host)
927 {
928 struct Host *hst = cls;
929 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
930 }
931}
932
933/**
934 * Received a data fragment to be relayed from a guest.
935 */
936static void
937place_recv_relay_data (void *cls,
938 const struct GNUNET_PSYC_MessageHeader *msg,
939 const struct GNUNET_MessageHeader *pmsg,
940 uint64_t message_id,
941 const void *data,
942 uint16_t data_size)
943{
944 struct Place *plc = cls;
945
946 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
947 && GNUNET_YES == plc->is_host)
948 {
949 struct Host *hst = cls;
950 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
951 }
952}
953
954
955/**
956 * Received end of message to be relayed from a guest.
957 */
958static void
959place_recv_relay_eom (void *cls,
960 const struct GNUNET_PSYC_MessageHeader *msg,
961 const struct GNUNET_MessageHeader *pmsg,
962 uint64_t message_id,
963 uint8_t is_cancelled)
964{
965 struct Place *plc = cls;
966
967 if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
968 && GNUNET_YES == plc->is_host)
969 {
970 struct Host *hst = cls;
971 host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
972 }
973}
974
975
976/**
977 * Received a method to be saved to disk.
978 *
979 * Create a new file for writing the data part of the message into,
980 * if the file does not yet exist.
981 */
982static void
983place_recv_save_method (void *cls,
984 const struct GNUNET_PSYC_MessageHeader *msg,
985 const struct GNUNET_PSYC_MessageMethod *meth,
986 uint64_t message_id,
987 const char *method_name)
988{
989 struct Place *plc = cls;
990 plc->file_offset = 0;
991 plc->file_save = GNUNET_NO;
992
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);
1001
1002 /* save if does not already exist */
1003 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1004 {
1005 if (0 == GNUNET_DISK_fn_write (filename, NULL, 0,
1006 GNUNET_DISK_PERM_USER_READ
1007 | GNUNET_DISK_PERM_USER_WRITE))
1008 {
1009 plc->file_save = GNUNET_YES;
1010 }
1011 else
1012 {
1013 GNUNET_break (0);
1014 }
1015 }
1016 GNUNET_free (filename);
1017}
1018
1019
1020/**
1021 * Received a data fragment to be saved to disk.
1022 *
1023 * Append data fragment to the file.
1024 */
1025static void
1026place_recv_save_data (void *cls,
1027 const struct GNUNET_PSYC_MessageHeader *msg,
1028 const struct GNUNET_MessageHeader *pmsg,
1029 uint64_t message_id,
1030 const void *data,
1031 uint16_t data_size)
1032{
1033 struct Place *plc = cls;
1034 if (GNUNET_YES != plc->file_save)
1035 return;
1036
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))
1046 {
1047 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "create", filename);
1048 GNUNET_free (filename);
1049 return;
1050 }
1051
1052 struct GNUNET_DISK_FileHandle *
1053 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
1054 GNUNET_DISK_PERM_NONE);
1055 if (NULL != fh)
1056 {
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);
1062 return;
1063 }
1064 GNUNET_DISK_file_write (fh, data, data_size);
1065 GNUNET_DISK_file_close (fh);
1066 GNUNET_free (filename);
1067 }
1068 else
1069 {
1070 GNUNET_free (filename);
1071 GNUNET_break (0);
1072 }
1073 plc->file_offset += data_size;
1074}
1075
1076
1077/**
1078 * Received end of message to be saved to disk.
1079 *
1080 * Remove .part ending from the filename.
1081 */
1082static void
1083place_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)
1088{
1089 struct Place *plc = cls;
1090 if (GNUNET_YES != plc->file_save)
1091 return;
1092
1093 char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&plc->pub_key);
1094 char *fn = NULL;
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);
1103
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);
1108 }
1109
1110 GNUNET_free (fn);
1111 GNUNET_free (fn_part);
1112}
1113
1114
1115/**
1116 * Initialize place data structure.
1117 */
1118static void
1119place_init (struct Place *plc)
1120{
1121 plc->slicer = GNUNET_PSYC_slicer_create ();
1122}
1123
1124
1125/**
1126 * Add a place to the @e places hash map.
1127 *
1128 * @param ereq
1129 * Entry request.
1130 *
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
1134 */
1135static int
1136place_add (const struct PlaceEnterRequest *ereq)
1137{
1138 struct EgoPlacePublicKey ego_place_pub_key = {
1139 .ego_pub_key = ereq->ego_pub_key,
1140 .place_pub_key = ereq->place_pub_key,
1141 };
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);
1144
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1147
1148 struct GNUNET_MessageHeader *
1149 place_msg = GNUNET_CONTAINER_multihashmap_get (places, &ego_place_pub_hash);
1150 if (NULL != place_msg)
1151 return GNUNET_NO;
1152
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))
1156 {
1157 GNUNET_break (0);
1158 GNUNET_free (place_msg);
1159 return GNUNET_SYSERR;
1160 }
1161
1162 return GNUNET_OK;
1163}
1164
1165/**
1166 * Add a place to the @e app_places hash map.
1167 *
1168 * @param app_id
1169 * Application ID.
1170 * @param ereq
1171 * Entry request.
1172 *
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
1176 */
1177static int
1178app_place_add (const char *app_id,
1179 const struct PlaceEnterRequest *ereq)
1180{
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);
1185
1186 struct GNUNET_HashCode app_id_hash;
1187 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1188
1189 struct EgoPlacePublicKey ego_place_pub_key = {
1190 .ego_pub_key = ereq->ego_pub_key,
1191 .place_pub_key = ereq->place_pub_key,
1192 };
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);
1195
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 " ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));
1198
1199 struct GNUNET_CONTAINER_MultiHashMap *
1200 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1201 if (NULL == app_places)
1202 {
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);
1206 }
1207
1208 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (app_places, &ego_place_pub_hash))
1209 return GNUNET_NO;
1210
1211 if (GNUNET_SYSERR == place_add (ereq))
1212 {
1213 return GNUNET_SYSERR;
1214 }
1215
1216 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (app_places, &ego_place_pub_hash, NULL,
1217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1218 {
1219 GNUNET_break (0);
1220 return GNUNET_SYSERR;
1221 }
1222 return GNUNET_OK;
1223}
1224
1225
1226/**
1227 * Save place entry message to disk.
1228 *
1229 * @param app_id
1230 * Application ID.
1231 * @param ereq
1232 * Entry request message.
1233 */
1234static int
1235app_place_save (const char *app_id,
1236 const struct PlaceEnterRequest *ereq)
1237{
1238 if (GNUNET_SYSERR == app_place_add (app_id, ereq))
1239 {
1240 GNUNET_assert (0);
1241 }
1242
1243 if (NULL == dir_places)
1244 return GNUNET_SYSERR;
1245
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,
1253 place_pub_str);
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))
1259 {
1260 GNUNET_break (0);
1261 ret = GNUNET_SYSERR;
1262 }
1263 GNUNET_free (filename);
1264
1265 if (ret == GNUNET_OK)
1266 {
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,
1272 place_pub_str);
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))
1278 {
1279 GNUNET_break (0);
1280 ret = GNUNET_SYSERR;
1281 }
1282 GNUNET_free (filename);
1283 }
1284 GNUNET_free (ego_pub_str);
1285 GNUNET_free (place_pub_str);
1286 return ret;
1287}
1288
1289
1290int
1291app_place_remove (const char *app_id,
1292 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1293 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key)
1294{
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);
1299
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,
1309 place_pub_str);
1310 GNUNET_free (ego_pub_str);
1311 GNUNET_free (place_pub_str);
1312
1313 struct GNUNET_HashCode app_id_hash;
1314 GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);
1315
1316 struct GNUNET_CONTAINER_MultiHashMap *
1317 app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
1318
1319 if (NULL != app_places)
1320 GNUNET_CONTAINER_multihashmap_remove (app_places, &place_pub_hash, NULL);
1321
1322 int ret = GNUNET_OK;
1323
1324 if (0 != unlink (app_place_filename))
1325 {
1326 GNUNET_break (0);
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;
1331 }
1332 GNUNET_free (app_place_filename);
1333
1334 return ret;
1335}
1336
1337
1338/**
1339 * Enter place as host.
1340 *
1341 * @param hreq
1342 * Host entry request.
1343 * @param[out] ret_hst
1344 * Returned Host struct.
1345 *
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
1350 */
1351static int
1352host_enter (const struct HostEnterRequest *hreq, struct Host **ret_hst)
1353{
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),
1357 &place_pub_hash);
1358 struct Host *hst = GNUNET_CONTAINER_multihashmap_get (hosts, &place_pub_hash);
1359
1360 if (NULL == hst)
1361 {
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);
1366
1367 struct Place *plc = &hst->place;
1368 place_init (plc);
1369 plc->is_host = GNUNET_YES;
1370 plc->pub_key = hreq->place_pub_key;
1371 plc->pub_key_hash = place_pub_hash;
1372
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);
1380 ret = GNUNET_YES;
1381 }
1382
1383 if (NULL != ret_hst)
1384 *ret_hst = hst;
1385 return ret;
1386}
1387
1388
1389static int
1390msg_proc_parse (const struct MsgProcRequest *mpreq,
1391 uint32_t *flags,
1392 const char **method_prefix,
1393 struct GNUNET_HashCode *method_hash)
1394{
1395 ssize_t method_size = ntohs (mpreq->header.size) - sizeof (*mpreq);
1396 uint16_t offset;
1397
1398 if (method_size < 0)
1399 {
1400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1401 "MsgProcRequest has invalid size\n");
1402 return GNUNET_SYSERR;
1403 }
1404
1405 offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &mpreq[1],
1406 method_size,
1407 1,
1408 method_prefix);
1409 if (0 == offset || offset != method_size || *method_prefix == NULL)
1410 {
1411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1412 "MsgProcRequest contains invalid method\n");
1413 return GNUNET_SYSERR;
1414 }
1415 GNUNET_CRYPTO_hash (*method_prefix, (size_t) method_size, method_hash);
1416 *flags = ntohl (mpreq->flags);
1417 return GNUNET_OK;
1418}
1419
1420
1421void
1422app_notify_place (const struct GNUNET_MessageHeader *msg,
1423 struct GNUNET_SERVICE_Client *client)
1424{
1425 struct AppPlaceMessage *amsg;
1426 struct GNUNET_MQ_Envelope *env;
1427 uint16_t msg_size = ntohs (msg->size);
1428
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))
1433 {
1434 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
1435 {
1436 struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
1437 if (msg_size < sizeof (struct HostEnterRequest))
1438 return;
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),
1447 env);
1448 break;
1449 }
1450 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
1451 {
1452 if (msg_size < sizeof (struct GuestEnterRequest))
1453 return;
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),
1463 env);
1464 break;
1465 }
1466 default:
1467 return;
1468 }
1469}
1470
1471
1472void
1473app_notify_place_end (struct GNUNET_SERVICE_Client *client)
1474{
1475 struct GNUNET_MQ_Envelope *env;
1476
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "%p Sending end of place list notification to client\n",
1479 client);
1480 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END);
1481 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1482 env);
1483}
1484
1485
1486void
1487app_notify_ego (struct Ego *ego, struct GNUNET_SERVICE_Client *client)
1488{
1489 struct AppEgoMessage *emsg;
1490 struct GNUNET_MQ_Envelope *env;
1491 size_t name_size = strlen (ego->name) + 1;
1492
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "%p Sending ego notification to client: %s\n",
1495 client, ego->name);
1496 env = GNUNET_MQ_msg_extra (emsg,
1497 name_size,
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),
1502 env);
1503}
1504
1505
1506void
1507app_notify_ego_end (struct GNUNET_SERVICE_Client *client)
1508{
1509 struct GNUNET_MQ_Envelope *env;
1510
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "%p Sending end of ego list notification to client\n",
1513 client);
1514 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END);
1515 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1516 env);
1517}
1518
1519
1520int
1521app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key, void *value)
1522{
1523 struct GNUNET_MessageHeader *
1524 msg = GNUNET_CONTAINER_multihashmap_get (places, key);
1525 if (NULL != msg)
1526 app_notify_place (msg, cls);
1527 return GNUNET_YES;
1528}
1529
1530
1531int
1532ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
1533{
1534 app_notify_ego (value, cls);
1535 return GNUNET_YES;
1536}
1537
1538
1539static int
1540check_client_msg_proc_set (void *cls,
1541 const struct MsgProcRequest *mpreq)
1542{
1543 return GNUNET_OK;
1544}
1545
1546
1547/**
1548 * Handle a client setting message proccesing flags for a method prefix.
1549 */
1550static void
1551handle_client_msg_proc_set (void *cls,
1552 const struct MsgProcRequest *mpreq)
1553{
1554 struct Client *c = cls;
1555 struct GNUNET_SERVICE_Client *client = c->client;
1556 struct Place *plc = c->place;
1557 if (NULL == plc)
1558 {
1559 GNUNET_break (0);
1560 GNUNET_SERVICE_client_drop (client);
1561 return;
1562 }
1563
1564 const char *method_prefix = NULL;
1565 uint32_t flags = 0;
1566 struct GNUNET_HashCode method_hash;
1567
1568 if (GNUNET_OK !=
1569 msg_proc_parse (mpreq, &flags, &method_prefix, &method_hash))
1570 {
1571 GNUNET_break (0);
1572 GNUNET_SERVICE_client_drop (client);
1573 return;
1574 }
1575#if 0
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,
1583 NULL,
1584 place_recv_save_data,
1585 place_recv_save_eom);
1586#endif
1587 if (flags & GNUNET_SOCIAL_MSG_PROC_RELAY)
1588 {
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,
1594 plc);
1595 }
1596 if (flags & GNUNET_SOCIAL_MSG_PROC_SAVE)
1597 {
1598 GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
1599 place_recv_save_method,
1600 NULL,
1601 place_recv_save_data,
1602 place_recv_save_eom,
1603 plc);
1604 }
1605
1606 /** @todo Save flags to be able to resume relaying/saving after restart */
1607
1608 GNUNET_SERVICE_client_continue (client);
1609}
1610
1611
1612/**
1613 * Handle a connecting client requesting to clear all relay rules.
1614 */
1615static void
1616handle_client_msg_proc_clear (void *cls,
1617 const struct GNUNET_MessageHeader *msg)
1618{
1619 struct Client *c = cls;
1620 struct GNUNET_SERVICE_Client *client = c->client;
1621 struct Place *plc = c->place;
1622 if (NULL == plc)
1623 {
1624 GNUNET_break (0);
1625 GNUNET_SERVICE_client_drop (client);
1626 return;
1627 }
1628
1629 GNUNET_PSYC_slicer_clear (plc->slicer);
1630
1631 GNUNET_SERVICE_client_continue (client);
1632}
1633
1634
1635static int
1636check_client_host_enter (void *cls,
1637 const struct HostEnterRequest *hr)
1638{
1639 return GNUNET_OK;
1640}
1641
1642
1643/**
1644 * Handle a connecting client entering a place as host.
1645 */
1646static void
1647handle_client_host_enter (void *cls,
1648 const struct HostEnterRequest *hr)
1649{
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);
1654
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)
1660 {
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);
1664 GNUNET_break (0);
1665 GNUNET_SERVICE_client_drop (client);
1666 return;
1667 }
1668
1669 struct Host *hst = NULL;
1670 struct Place *plc = NULL;
1671 int ret = GNUNET_OK;
1672
1673 struct GNUNET_CRYPTO_EddsaPublicKey empty_pub_key;
1674 memset (&empty_pub_key, 0, sizeof (empty_pub_key));
1675
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);
1684
1685 app_place_save (app_id, (const struct PlaceEnterRequest *) hreq);
1686 }
1687
1688 switch (host_enter (hreq, &hst))
1689 {
1690 case GNUNET_YES:
1691 plc = c->place = &hst->place;
1692 plc->host = hst;
1693 break;
1694
1695 case GNUNET_NO:
1696 {
1697 plc = c->place = &hst->place;
1698 plc->host = hst;
1699 client_send_host_enter_ack (client, hst, GNUNET_OK);
1700 break;
1701 }
1702 case GNUNET_SYSERR:
1703 ret = GNUNET_SYSERR;
1704 }
1705
1706 if (ret != GNUNET_SYSERR)
1707 {
1708
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));
1712
1713 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1714 cli->client = client;
1715 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1716 c->place = plc;
1717 app_notify_place (&hreq->header, client);
1718 }
1719
1720 GNUNET_CRYPTO_eddsa_key_clear (&hreq->place_key);
1721 GNUNET_free (hreq);
1722
1723 if (GNUNET_OK == ret)
1724 GNUNET_SERVICE_client_continue (client);
1725 else
1726 GNUNET_SERVICE_client_drop (client);
1727}
1728
1729
1730/**
1731 * Enter place as guest.
1732 *
1733 * @param greq
1734 * Guest entry request.
1735 * @param[out] ret_gst
1736 * Returned Guest struct.
1737 *
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.
1741 */
1742static int
1743guest_enter (const struct GuestEnterRequest *greq, struct Guest **ret_gst)
1744{
1745 int ret = GNUNET_NO;
1746 uint16_t greq_size = ntohs (greq->header.size);
1747
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);
1752
1753 if (NULL == ego)
1754 {
1755 return GNUNET_SYSERR;
1756 }
1757
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),
1762 &place_pub_hash);
1763
1764 struct GNUNET_CONTAINER_MultiHashMap *
1765 plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests, &place_pub_hash);
1766 struct Guest *gst = NULL;
1767 int new_guest;
1768
1769 if (NULL != plc_gst)
1770 gst = GNUNET_CONTAINER_multihashmap_get (plc_gst, &ego_pub_hash);
1771
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1773 "plc_gst = %p, gst = %p\n",
1774 plc_gst,
1775 gst);
1776
1777 if (NULL == gst)
1778 {
1779 gst = GNUNET_new (struct Guest);
1780 new_guest = GNUNET_YES;
1781 }
1782 else new_guest = GNUNET_NO;
1783
1784 if (NULL == gst->slave)
1785 {
1786 gst->origin = greq->origin;
1787 gst->relay_count = ntohl (greq->relay_count);
1788
1789 uint16_t len;
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;
1793
1794 len = strnlen (app_id, remaining);
1795 if (len == remaining)
1796 {
1797 GNUNET_free (gst);
1798 GNUNET_break (0);
1799 return GNUNET_SYSERR;
1800 }
1801 p += len + 1;
1802 remaining -= len + 1;
1803
1804 const struct GNUNET_PeerIdentity *relays = NULL;
1805 uint16_t relay_size = gst->relay_count * sizeof (*relays);
1806 if (remaining < relay_size)
1807 {
1808 GNUNET_free (gst);
1809 GNUNET_break (0);
1810 return GNUNET_SYSERR;
1811 }
1812 if (0 < relay_size)
1813 relays = (const struct GNUNET_PeerIdentity *) p;
1814 p += relay_size;
1815 remaining -= relay_size;
1816
1817 struct GNUNET_PSYC_Message *join_msg = NULL;
1818 uint16_t join_msg_size = 0;
1819
1820 if (sizeof (struct GNUNET_MessageHeader) <= remaining)
1821 {
1822 join_msg = (struct GNUNET_PSYC_Message *) p;
1823 join_msg_size = ntohs (join_msg->header.size);
1824 p += join_msg_size;
1825 remaining -= join_msg_size;
1826 }
1827 if (0 != remaining)
1828 {
1829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1830 "%zu + %u + %u != %u\n",
1831 sizeof (*greq), relay_size, join_msg_size, greq_size);
1832 GNUNET_free (gst);
1833 GNUNET_break (0);
1834 return GNUNET_SYSERR;
1835 }
1836 if (0 < relay_size)
1837 {
1838 gst->relays = GNUNET_malloc (relay_size);
1839 GNUNET_memcpy (gst->relays, relays, relay_size);
1840 }
1841
1842 gst->join_flags = ntohl (greq->flags);
1843
1844 struct Place *plc = &gst->place;
1845 place_init (plc);
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;
1852
1853 if (NULL == plc_gst)
1854 {
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);
1858 }
1859 if (GNUNET_YES == new_guest)
1860 {
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);
1865
1866 }
1867 gst->slave
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,
1874 gst, join_msg);
1875 plc->channel = GNUNET_PSYC_slave_get_channel (gst->slave);
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "slave entered channel %p\n",
1878 plc->channel);
1879 ret = GNUNET_YES;
1880 }
1881
1882 // TODO: explain to automatic code scanners why free(gst) not necessary
1883 if (NULL != ret_gst)
1884 *ret_gst = gst;
1885 return ret;
1886}
1887
1888
1889static int
1890client_guest_enter (struct Client *c,
1891 const struct GuestEnterRequest *greq)
1892{
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;
1904
1905 if (0 == offset)
1906 {
1907 return GNUNET_SYSERR;
1908 }
1909 switch (guest_enter (greq, &gst))
1910 {
1911 case GNUNET_YES:
1912 {
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));
1917 plc->guest = gst;
1918 app_place_save (app_id, (const struct PlaceEnterRequest *) greq);
1919 app_notify_place (&greq->header, client);
1920 break;
1921 }
1922 case GNUNET_NO:
1923 {
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));
1928 plc->guest = gst;
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),
1934 env);
1935 if (NULL != gst->join_dcsn)
1936 {
1937 env = GNUNET_MQ_msg_copy (&gst->join_dcsn->header);
1938 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1939 env);
1940 }
1941 break;
1942 }
1943 case GNUNET_SYSERR:
1944 {
1945 return GNUNET_SYSERR;
1946 }
1947 }
1948
1949 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
1950 cli->client = client;
1951 GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
1952 return GNUNET_OK;
1953}
1954
1955
1956static int
1957check_client_guest_enter (void *cls,
1958 const struct GuestEnterRequest *greq)
1959{
1960 return GNUNET_OK;
1961}
1962
1963
1964/**
1965 * Handle a connecting client entering a place as guest.
1966 */
1967static void
1968handle_client_guest_enter (void *cls,
1969 const struct GuestEnterRequest *greq)
1970{
1971 struct Client *c = cls;
1972
1973 if (GNUNET_SYSERR == client_guest_enter (c, greq))
1974 {
1975 GNUNET_break (0);
1976 GNUNET_SERVICE_client_drop (c->client);
1977 return;
1978 }
1979 GNUNET_SERVICE_client_continue (c->client);
1980}
1981
1982
1983struct GuestEnterByNameClosure
1984{
1985 struct Client *client;
1986 char *app_id;
1987 char *password;
1988 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1989 struct GNUNET_MessageHeader *join_msg;
1990};
1991
1992
1993/**
1994 * Result of a GNS name lookup for entering a place.
1995 *
1996 * @see GNUNET_SOCIAL_guest_enter_by_name
1997 */
1998static void
1999gns_result_guest_enter (void *cls, uint32_t rd_count,
2000 const struct GNUNET_GNSRECORD_Data *rd)
2001{
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",
2006 c, rd_count);
2007
2008 const struct GNUNET_GNSRECORD_PlaceData *
2009 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
2010
2011 if (0 == rd_count || rd->data_size < sizeof (*rec))
2012 {
2013 GNUNET_break (0);
2014 GNUNET_SERVICE_client_drop (c->client);
2015 return;
2016 }
2017
2018 uint16_t relay_count = ntohl (rec->relay_count);
2019 struct GNUNET_PeerIdentity *relays = NULL;
2020
2021 if (0 < relay_count)
2022 {
2023 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
2024 {
2025 relays = (struct GNUNET_PeerIdentity *) &rec[1];
2026 }
2027 else
2028 {
2029 relay_count = 0;
2030 GNUNET_break_op (0);
2031 }
2032 }
2033
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;
2048
2049 void *p = &greq[1];
2050 GNUNET_memcpy (p, gcls->app_id, app_id_size);
2051 p += app_id_size;
2052 GNUNET_memcpy (p, relays, relay_size);
2053 p += relay_size;
2054 GNUNET_memcpy (p, gcls->join_msg, join_msg_size);
2055
2056 client_guest_enter (c, greq);
2057
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);
2063 GNUNET_free (gcls);
2064 GNUNET_free (greq);
2065}
2066
2067
2068static int
2069check_client_guest_enter_by_name (void *cls,
2070 const struct GuestEnterByNameRequest *greq)
2071{
2072 return GNUNET_OK;
2073}
2074
2075
2076/**
2077 * Handle a connecting client entering a place as guest using a GNS address.
2078 *
2079 * Look up GNS address and generate a GuestEnterRequest from that.
2080 */
2081static void
2082handle_client_guest_enter_by_name (void *cls,
2083 const struct GuestEnterByNameRequest *greq)
2084{
2085 struct Client *c = cls;
2086 struct GNUNET_SERVICE_Client *client = c->client;
2087
2088 struct GuestEnterByNameClosure *gcls = GNUNET_malloc (sizeof (*gcls));
2089 gcls->client = c;
2090 gcls->ego_pub_key = greq->ego_pub_key;
2091
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,
2096 &app_id,
2097 &gns_name,
2098 &password);
2099 p += offset;
2100 remaining -= offset;
2101
2102 if (0 != offset && sizeof (*gcls->join_msg) <= remaining)
2103 {
2104 gcls->join_msg = GNUNET_copy_message ((struct GNUNET_MessageHeader *) p);
2105 remaining -= ntohs (gcls->join_msg->size);
2106 }
2107
2108 if (0 == offset || 0 != remaining)
2109 {
2110 if (NULL != gcls->join_msg)
2111 GNUNET_free (gcls->join_msg);
2112 GNUNET_free (gcls);
2113 GNUNET_break (0);
2114 GNUNET_SERVICE_client_drop (client);
2115 return;
2116 }
2117
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);
2121
2122 uint16_t password_size = strlen (password);
2123 if (0 < password_size++)
2124 {
2125 gcls->password = GNUNET_malloc (password_size);
2126 GNUNET_memcpy (gcls->password, password, password_size);
2127 }
2128
2129 GNUNET_GNS_lookup (gns, gns_name,
2130 &greq->ego_pub_key,
2131 GNUNET_GNSRECORD_TYPE_PLACE,
2132 GNUNET_GNS_LO_DEFAULT,
2133 &gns_result_guest_enter, gcls);
2134 GNUNET_SERVICE_client_continue (client);
2135}
2136
2137
2138static int
2139check_client_app_connect (void *cls,
2140 const struct AppConnectRequest *creq)
2141{
2142 return GNUNET_OK;
2143}
2144
2145
2146/**
2147 * Handle application connection.
2148 */
2149static void
2150handle_client_app_connect (void *cls,
2151 const struct AppConnectRequest *creq)
2152{
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;
2157 uint16_t offset;
2158
2159 if (app_id_size < 0)
2160 {
2161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2162 "AppConnectRequest has invalid size\n");
2163 GNUNET_break (0);
2164 GNUNET_SERVICE_client_drop (client);
2165 return;
2166 }
2167
2168 offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
2169 (size_t) app_id_size,
2170 1,
2171 &app_id);
2172 if (0 == offset || offset != app_id_size)
2173 {
2174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2175 "AppConnectRequest contains invalid app ID\n");
2176 GNUNET_break (0);
2177 GNUNET_SERVICE_client_drop (client);
2178 return;
2179 }
2180
2181 struct GNUNET_HashCode app_id_hash;
2182 GNUNET_CRYPTO_hash (app_id, (size_t) app_id_size, &app_id_hash);
2183
2184 GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
2185 app_notify_ego_end (client);
2186
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);
2192
2193 struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
2194 cli->client = client;
2195 struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
2196 &app_id_hash);
2197 if (NULL == app) {
2198 app = GNUNET_malloc (sizeof (*app));
2199 (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
2200 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2201 }
2202 GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);
2203
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "%p Application %s connected.\n", app, app_id);
2206
2207 c->app_id = GNUNET_malloc ((size_t) app_id_size);
2208 GNUNET_memcpy (c->app_id, app_id, (size_t) app_id_size);
2209
2210 GNUNET_SERVICE_client_continue (client);
2211}
2212
2213
2214/**
2215 * Handle application detach request.
2216 */
2217static void
2218handle_client_app_detach (void *cls,
2219 const struct AppDetachRequest *req)
2220{
2221 struct Client *c = cls;
2222 struct GNUNET_SERVICE_Client *client = c->client;
2223
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);
2226
2227 GNUNET_SERVICE_client_continue (client);
2228}
2229
2230
2231static void
2232place_leave_cb (void *cls)
2233{
2234 struct Place *plc = cls;
2235
2236 place_send_leave_ack (plc);
2237 (GNUNET_YES == plc->is_host)
2238 ? cleanup_host ((struct Host *) plc)
2239 : cleanup_guest ((struct Guest *) plc);
2240}
2241
2242
2243/**
2244 * Handle application leave request.
2245 */
2246static void
2247handle_client_place_leave (void *cls,
2248 const struct GNUNET_MessageHeader *msg)
2249{
2250 struct Client *c = cls;
2251 struct GNUNET_SERVICE_Client *client = c->client;
2252 struct Place *plc = c->place;
2253
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));
2258 if (NULL == plc)
2259 {
2260 GNUNET_break (0);
2261 GNUNET_SERVICE_client_drop (client);
2262 return;
2263 }
2264
2265 if (GNUNET_YES != plc->is_disconnecting)
2266 {
2267 plc->is_disconnecting = GNUNET_YES;
2268 if (plc->is_host)
2269 {
2270 struct Host *host = plc->host;
2271 GNUNET_assert (NULL != host);
2272 GNUNET_PSYC_master_stop (host->master, GNUNET_NO, &place_leave_cb, plc);
2273 }
2274 else
2275 {
2276 struct Guest *guest = plc->guest;
2277 GNUNET_assert (NULL != guest);
2278 GNUNET_PSYC_slave_part (guest->slave, GNUNET_NO, &place_leave_cb, plc);
2279 }
2280 }
2281 else
2282 {
2283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2284 "got leave request but place is already leaving\n");
2285 }
2286 GNUNET_SERVICE_client_continue (client);
2287}
2288
2289
2290struct JoinDecisionClosure
2291{
2292 int32_t is_admitted;
2293 struct GNUNET_PSYC_Message *msg;
2294};
2295
2296
2297/**
2298 * Iterator callback for responding to join requests.
2299 */
2300static int
2301psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
2302 void *value)
2303{
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);
2308 return GNUNET_YES;
2309}
2310
2311
2312static int
2313check_client_join_decision (void *cls,
2314 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2315{
2316 return GNUNET_OK;
2317}
2318
2319
2320/**
2321 * Handle an entry decision from a host client.
2322 */
2323static void
2324handle_client_join_decision (void *cls,
2325 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
2326{
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)
2331 {
2332 GNUNET_break (0);
2333 GNUNET_SERVICE_client_drop (client);
2334 return;
2335 }
2336 struct Host *hst = plc->host;
2337
2338 struct JoinDecisionClosure jcls;
2339 jcls.is_admitted = ntohl (dcsn->is_admitted);
2340 jcls.msg
2341 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (dcsn->header.size))
2342 ? (struct GNUNET_PSYC_Message *) &dcsn[1]
2343 : NULL;
2344
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "jcls.msg = %p\n",
2347 jcls.msg);
2348 struct GNUNET_HashCode slave_pub_hash;
2349 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
2350 &slave_pub_hash);
2351
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));
2358
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);
2362
2363 GNUNET_SERVICE_client_continue (client);
2364}
2365
2366
2367/**
2368 * Send acknowledgement to a client.
2369 *
2370 * Sent after a message fragment has been passed on to multicast.
2371 *
2372 * @param plc The place struct for the client.
2373 */
2374static void
2375send_message_ack (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2376{
2377 struct GNUNET_MQ_Envelope *env;
2378
2379 env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
2380 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
2381 env);
2382}
2383
2384
2385/**
2386 * Proceed to the next message part in the transmission queue.
2387 *
2388 * @param plc
2389 * Place where the transmission is going on.
2390 * @param tmit_msg
2391 * Currently transmitted message.
2392 * @param tmit_frag
2393 * Currently transmitted message fragment.
2394 *
2395 * @return @a tmit_frag, or NULL if reached the end of fragment.
2396 */
2397static struct FragmentTransmitQueue *
2398psyc_transmit_queue_next_part (struct Place *plc,
2399 struct MessageTransmitQueue *tmit_msg,
2400 struct FragmentTransmitQueue *tmit_frag)
2401{
2402 uint16_t psize = ntohs (tmit_frag->next_part->size);
2403 if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
2404 < tmit_frag->size)
2405 {
2406 tmit_frag->next_part
2407 = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
2408 }
2409 else /* Reached end of current fragment. */
2410 {
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);
2415 tmit_frag = NULL;
2416 }
2417 return tmit_frag;
2418}
2419
2420
2421/**
2422 * Proceed to next message in transmission queue.
2423 *
2424 * @param plc
2425 * Place where the transmission is going on.
2426 * @param tmit_msg
2427 * Currently transmitted message.
2428 *
2429 * @return The next message in queue, or NULL if queue is empty.
2430 */
2431static struct MessageTransmitQueue *
2432psyc_transmit_queue_next_msg (struct Place *plc,
2433 struct MessageTransmitQueue *tmit_msg)
2434{
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;
2438}
2439
2440
2441/**
2442 * Callback for data transmission to PSYC.
2443 */
2444static int
2445psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2446{
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 */
2453 *data_size = 0;
2454 return GNUNET_NO;
2455 }
2456 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2457 if (NULL == pmsg)
2458 {
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 "%p psyc_transmit_notify_data: nothing to send.\n", plc);
2461 *data_size = 0;
2462 return GNUNET_NO;
2463 }
2464
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);
2468
2469 uint16_t ptype = ntohs (pmsg->type);
2470 uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
2471 int ret;
2472
2473 switch (ptype)
2474 {
2475 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2476 if (*data_size < pdata_size)
2477 {
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 "%p psyc_transmit_notify_data: buffer size too small for data.\n", plc);
2480 *data_size = 0;
2481 return GNUNET_NO;
2482 }
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2484 "%p psyc_transmit_notify_data: sending %u bytes.\n",
2485 plc, pdata_size);
2486
2487 *data_size = pdata_size;
2488 GNUNET_memcpy (data, &pmsg[1], *data_size);
2489 ret = GNUNET_NO;
2490 break;
2491
2492 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2493 *data_size = 0;
2494 ret = GNUNET_YES;
2495 break;
2496
2497 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2498 *data_size = 0;
2499 ret = GNUNET_SYSERR;
2500 break;
2501
2502 default:
2503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2504 "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
2505 plc, ptype);
2506 ret = GNUNET_SYSERR;
2507 }
2508
2509 if (GNUNET_SYSERR == ret && GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL != ptype)
2510 {
2511 *data_size = 0;
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);
2515 return ret;
2516 }
2517 else
2518 {
2519 tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2520 if (NULL != tmit_frag)
2521 {
2522 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2523 ptype = ntohs (pmsg->type);
2524 switch (ptype)
2525 {
2526 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2527 ret = GNUNET_YES;
2528 break;
2529 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2530 ret = GNUNET_SYSERR;
2531 break;
2532 }
2533 switch (ptype)
2534 {
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);
2538 }
2539 }
2540
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);
2545 }
2546 }
2547
2548 if (ret != GNUNET_NO)
2549 {
2550 if (NULL != tmit_msg)
2551 {
2552 psyc_transmit_message (plc);
2553 }
2554 /* FIXME: handle partial message (when still in_transmit) */
2555 }
2556 return ret;
2557}
2558
2559
2560/**
2561 * Callback for modifier transmission to PSYC.
2562 */
2563static int
2564psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2565 uint8_t *oper, uint32_t *full_value_size)
2566{
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 */
2573 *data_size = 0;
2574 return GNUNET_NO;
2575 }
2576 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2577 if (NULL == pmsg)
2578 {
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2580 "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
2581 *data_size = 0;
2582 return GNUNET_NO;
2583 }
2584
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);
2588
2589 uint16_t ptype = ntohs (pmsg->type);
2590 int ret;
2591
2592 switch (ptype)
2593 {
2594 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
2595 {
2596 if (NULL == oper)
2597 {
2598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2599 "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
2600 ret = GNUNET_SYSERR;
2601 break;
2602 }
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);
2606
2607 if (*data_size < mod_size)
2608 {
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2611 *data_size = 0;
2612 return GNUNET_NO;
2613 }
2614
2615 *full_value_size = ntohl (pmod->value_size);
2616 *oper = pmod->oper;
2617 *data_size = mod_size;
2618 GNUNET_memcpy (data, &pmod[1], mod_size);
2619 ret = GNUNET_NO;
2620 break;
2621 }
2622
2623 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
2624 {
2625 if (NULL != oper)
2626 {
2627 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2628 "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
2629 ret = GNUNET_SYSERR;
2630 break;
2631 }
2632 uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
2633 if (*data_size < mod_size)
2634 {
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2636 "%p psyc_transmit_notify_mod: buffer size too small for data.\n", plc);
2637 *data_size = 0;
2638 return GNUNET_NO;
2639 }
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc, mod_size);
2642
2643 *data_size = mod_size;
2644 GNUNET_memcpy (data, &pmsg[1], *data_size);
2645 ret = GNUNET_NO;
2646 break;
2647 }
2648
2649 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
2650 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
2651 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
2652 *data_size = 0;
2653 ret = GNUNET_YES;
2654 break;
2655
2656 default:
2657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2658 "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
2659 plc, ptype);
2660 ret = GNUNET_SYSERR;
2661 }
2662
2663 if (GNUNET_SYSERR == ret)
2664 {
2665 *data_size = 0;
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);
2670 }
2671 else
2672 {
2673 if (GNUNET_YES != ret)
2674 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2675
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);
2680 }
2681 }
2682 return ret;
2683}
2684
2685/**
2686 * Callback for data transmission from a host to PSYC.
2687 */
2688static int
2689host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2690{
2691 int ret = psyc_transmit_notify_data (cls, data_size, data);
2692
2693 if (GNUNET_NO != ret)
2694 {
2695 struct Host *hst = cls;
2696 hst->tmit_handle = NULL;
2697 }
2698 return ret;
2699}
2700
2701
2702/**
2703 * Callback for the transmit functions of multicast.
2704 */
2705static int
2706guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
2707{
2708 int ret = psyc_transmit_notify_data (cls, data_size, data);
2709
2710 if (GNUNET_NO != ret)
2711 {
2712 struct Guest *gst = cls;
2713 gst->tmit_handle = NULL;
2714 }
2715 return ret;
2716}
2717
2718
2719/**
2720 * Callback for modifier transmission from a host to PSYC.
2721 */
2722static int
2723host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2724 uint8_t *oper, uint32_t *full_value_size)
2725{
2726 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2727 oper, full_value_size);
2728 if (GNUNET_SYSERR == ret)
2729 {
2730 struct Host *hst = cls;
2731 hst->tmit_handle = NULL;
2732 }
2733 return ret;
2734}
2735
2736
2737/**
2738 * Callback for modifier transmission from a guest to PSYC.
2739 */
2740static int
2741guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
2742 uint8_t *oper, uint32_t *full_value_size)
2743{
2744 int ret = psyc_transmit_notify_mod (cls, data_size, data,
2745 oper, full_value_size);
2746 if (GNUNET_SYSERR == ret)
2747 {
2748 struct Guest *gst = cls;
2749 gst->tmit_handle = NULL;
2750 }
2751 return ret;
2752}
2753
2754
2755/**
2756 * Get method part of next message from transmission queue.
2757 *
2758 * @param plc
2759 * Place
2760 *
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.
2764 */
2765static struct GNUNET_PSYC_MessageMethod *
2766psyc_transmit_queue_next_method (struct Place *plc)
2767{
2768 struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
2769 if (NULL == tmit_msg)
2770 return GNUNET_NO;
2771
2772 struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
2773 if (NULL == tmit_frag)
2774 {
2775 GNUNET_break (0);
2776 return GNUNET_NO;
2777 }
2778
2779 struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
2780 if (NULL == pmsg
2781 || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type))
2782 {
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);
2786 GNUNET_break (0);
2787 return NULL;
2788 }
2789
2790 uint16_t psize = ntohs (pmsg->size);
2791 struct GNUNET_PSYC_MessageMethod *
2792 pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);
2793
2794 if (psize < sizeof (*pmeth) + 1 || '\0' != *((char *) pmeth + psize - 1))
2795 {
2796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2797 "%p psyc_transmit_queue_next_method: invalid method name.\n",
2798 plc);
2799 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2800 "%zu <= %u || NUL != %u\n",
2801 sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
2802 GNUNET_break (0);
2803 GNUNET_free (pmeth);
2804 return NULL;
2805 }
2806
2807 psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
2808 return pmeth;
2809}
2810
2811
2812/**
2813 * Transmit the next message in queue from the host to the PSYC channel.
2814 */
2815static int
2816psyc_master_transmit_message (struct Host *hst)
2817{
2818 struct Place *plc = &hst->place;
2819
2820 if (NULL == hst->tmit_handle)
2821 {
2822 struct GNUNET_PSYC_MessageMethod *
2823 pmeth = psyc_transmit_queue_next_method (plc);
2824 if (NULL == pmeth)
2825 return GNUNET_SYSERR;
2826
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,
2832 pmeth->flags);
2833 if (NULL != hst->tmit_handle)
2834 hst->tmit_handle = tmit_handle;
2835 GNUNET_free (pmeth);
2836 }
2837 else
2838 {
2839 GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
2840 }
2841 return GNUNET_OK;
2842}
2843
2844
2845/**
2846 * Transmit the next message in queue from a guest to the PSYC channel.
2847 */
2848static int
2849psyc_slave_transmit_message (struct Guest *gst)
2850{
2851 struct Place *plc = &gst->place;
2852
2853 if (NULL == gst->tmit_handle)
2854 {
2855 struct GNUNET_PSYC_MessageMethod *
2856 pmeth = psyc_transmit_queue_next_method (plc);
2857 if (NULL == pmeth)
2858 return GNUNET_SYSERR;
2859
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,
2865 pmeth->flags);
2866 if (NULL != gst->tmit_handle)
2867 gst->tmit_handle = tmit_handle;
2868 GNUNET_free (pmeth);
2869 }
2870 else
2871 {
2872 GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
2873 }
2874 return GNUNET_OK;
2875}
2876
2877
2878/**
2879 * Transmit a message to PSYC.
2880 */
2881static int
2882psyc_transmit_message (struct Place *plc)
2883{
2884 return
2885 (plc->is_host)
2886 ? psyc_master_transmit_message ((struct Host *) plc)
2887 : psyc_slave_transmit_message ((struct Guest *) plc);
2888}
2889
2890
2891/**
2892 * Queue message parts for sending to PSYC.
2893 *
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.
2900 */
2901static struct MessageTransmitQueue *
2902psyc_transmit_queue_message (struct Place *plc,
2903 struct GNUNET_SERVICE_Client *client,
2904 size_t data_size,
2905 const void *data,
2906 uint16_t first_ptype, uint16_t last_ptype,
2907 struct MessageTransmitQueue *tmit_msg)
2908{
2909 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
2910 {
2911 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
2912 GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail, tmit_msg);
2913 }
2914 else if (NULL == tmit_msg)
2915 {
2916 return NULL;
2917 }
2918
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;
2925
2926 GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail, tmit_frag);
2927 tmit_msg->client = client;
2928 return tmit_msg;
2929}
2930
2931
2932///**
2933// * Cancel transmission of current message to PSYC.
2934// *
2935// * @param plc Place to send to.
2936// * @param client Client the message originates from.
2937// */
2938//static void
2939//psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVICE_Client *client)
2940//{
2941// uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2942//
2943// struct GNUNET_MessageHeader msg;
2944// msg.size = htons (sizeof (msg));
2945// msg.type = htons (type);
2946//
2947// psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
2948// psyc_transmit_message (plc);
2949//
2950// /* FIXME: cleanup */
2951//}
2952
2953
2954static int
2955check_client_psyc_message (void *cls,
2956 const struct GNUNET_MessageHeader *msg)
2957{
2958 return GNUNET_OK;
2959}
2960
2961
2962/**
2963 * Handle an incoming message from a client, to be transmitted to the place.
2964 */
2965static void
2966handle_client_psyc_message (void *cls,
2967 const struct GNUNET_MessageHeader *msg)
2968{
2969 struct Client *c = cls;
2970 struct GNUNET_SERVICE_Client *client = c->client;
2971 struct Place *plc = c->place;
2972 int ret;
2973
2974 if (NULL == plc)
2975 {
2976 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2977 "received PSYC message for non-existing client %p\n",
2978 client);
2979 GNUNET_break (0);
2980 GNUNET_SERVICE_client_drop (client);
2981 return;
2982 }
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);
2986
2987 if (GNUNET_YES != plc->is_ready)
2988 {
2989 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2990 "%p Place is not ready yet, disconnecting client.\n", plc);
2991 GNUNET_break (0);
2992 GNUNET_SERVICE_client_drop (client);
2993 return;
2994 }
2995
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)
3000 {
3001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3002 "%p Received message with invalid payload size (%u) from client.\n",
3003 plc, psize);
3004 GNUNET_break (0);
3005 GNUNET_SERVICE_client_drop (client);
3006 return;
3007 }
3008
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))
3014 {
3015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3016 "%p Received invalid message part from client.\n", plc);
3017 GNUNET_break (0);
3018 GNUNET_SERVICE_client_drop (client);
3019 return;
3020 }
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);
3024
3025 c->tmit_msg
3026 = psyc_transmit_queue_message (plc, client, psize, &msg[1],
3027 first_ptype, last_ptype, c->tmit_msg);
3028 if (NULL != c->tmit_msg)
3029 {
3030 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
3031 c->tmit_msg = NULL;
3032 ret = psyc_transmit_message (plc);
3033 }
3034 else
3035 {
3036 ret = GNUNET_SYSERR;
3037 }
3038 if (GNUNET_OK != ret)
3039 {
3040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3041 "%p Received invalid message part from client.\n", plc);
3042 GNUNET_break (0);
3043 GNUNET_SERVICE_client_drop (client);
3044 return;
3045 }
3046 GNUNET_SERVICE_client_continue (client);
3047}
3048
3049
3050/**
3051 * A historic message arrived from PSYC.
3052 */
3053static void
3054psyc_recv_history_message (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
3055{
3056 struct OperationClosure *opcls = cls;
3057 struct Client *c = opcls->client;
3058 struct Place *plc = c->place;
3059
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));
3063
3064 uint16_t size = ntohs (msg->header.size);
3065
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);
3072
3073 GNUNET_memcpy (&res[1], msg, size);
3074
3075 /** @todo FIXME: send only to requesting client */
3076 place_send_msg (plc, GNUNET_MQ_msg_copy (&res->header));
3077
3078 GNUNET_free (res);
3079}
3080
3081
3082/**
3083 * Result of message history replay from PSYC.
3084 */
3085static void
3086psyc_recv_history_result (void *cls, int64_t result,
3087 const void *err_msg, uint16_t err_msg_size)
3088{
3089 struct OperationClosure *opcls = cls;
3090 struct Client *c = opcls->client;
3091 struct Place *plc = c->place;
3092
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);
3098
3099 // FIXME: place might have been destroyed
3100 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3101}
3102
3103
3104static int
3105check_client_history_replay (void *cls,
3106 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3107{
3108 return GNUNET_OK;
3109}
3110
3111
3112/**
3113 * Client requests channel history.
3114 */
3115static void
3116handle_client_history_replay (void *cls,
3117 const struct GNUNET_PSYC_HistoryRequestMessage *req)
3118{
3119 struct Client *c = cls;
3120 struct GNUNET_SERVICE_Client *client = c->client;
3121 struct Place *plc = c->place;
3122 if (NULL == plc)
3123 {
3124 GNUNET_break (0);
3125 GNUNET_SERVICE_client_drop (client);
3126 return;
3127 }
3128
3129 uint16_t size = ntohs (req->header.size);
3130 const char *method_prefix = (const char *) &req[1];
3131
3132 if (size < sizeof (*req) + 1
3133 || '\0' != method_prefix[size - sizeof (*req) - 1])
3134 {
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);
3139 GNUNET_break (0);
3140 GNUNET_SERVICE_client_drop (client);
3141 return;
3142 }
3143
3144 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3145 opcls->client = c;
3146 opcls->op_id = req->op_id;
3147 opcls->flags = ntohl (req->flags);
3148
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);
3156 else
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);
3162
3163 GNUNET_SERVICE_client_continue (client);
3164}
3165
3166
3167/**
3168 * A state variable part arrived from PSYC.
3169 */
3170void
3171psyc_recv_state_var (void *cls,
3172 const struct GNUNET_MessageHeader *mod,
3173 const char *name,
3174 const void *value,
3175 uint32_t value_size,
3176 uint32_t full_value_size)
3177{
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);
3184
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3186 "%p Received state variable %s from PSYC\n",
3187 plc, name);
3188 env = GNUNET_MQ_msg_extra (result_msg,
3189 size,
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);
3196}
3197
3198
3199/**
3200 * Result of retrieving state variable from PSYC.
3201 */
3202static void
3203psyc_recv_state_result (void *cls, int64_t result,
3204 const void *err_msg, uint16_t err_msg_size)
3205{
3206 struct OperationClosure *opcls = cls;
3207 struct Client *c = opcls->client;
3208 struct Place *plc = c->place;
3209
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);
3215
3216 // FIXME: place might have been destroyed
3217 client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
3218}
3219
3220
3221static int
3222check_client_state_get (void *cls,
3223 const struct GNUNET_PSYC_StateRequestMessage *req)
3224{
3225 return GNUNET_OK;
3226}
3227
3228
3229/**
3230 * Client requests channel history.
3231 */
3232static void
3233handle_client_state_get (void *cls,
3234 const struct GNUNET_PSYC_StateRequestMessage *req)
3235{
3236 struct Client *c = cls;
3237 struct GNUNET_SERVICE_Client *client = c->client;
3238 struct Place *plc = c->place;
3239 if (NULL == plc)
3240 {
3241 GNUNET_break (0);
3242 GNUNET_SERVICE_client_drop (client);
3243 return;
3244 }
3245
3246 uint16_t size = ntohs (req->header.size);
3247 const char *name = (const char *) &req[1];
3248
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 "%p State get #%" PRIu64 ": %s\n",
3251 plc, GNUNET_ntohll (req->op_id), name);
3252
3253 if (size < sizeof (*req) + 1
3254 || '\0' != name[size - sizeof (*req) - 1])
3255 {
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);
3260 GNUNET_break (0);
3261 GNUNET_SERVICE_client_drop (client);
3262 return;
3263 }
3264
3265 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3266 opcls->client = c;
3267 opcls->op_id = req->op_id;
3268
3269 switch (ntohs (req->header.type))
3270 {
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);
3275 break;
3276
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);
3281 break;
3282
3283 default:
3284 GNUNET_assert (0);
3285 }
3286
3287 GNUNET_SERVICE_client_continue (client);
3288}
3289
3290
3291#define check_client_state_get_prefix check_client_state_get
3292#define handle_client_state_get_prefix handle_client_state_get
3293
3294
3295static void
3296namestore_recv_records_store_result (void *cls, int32_t result,
3297 const char *err_msg)
3298{
3299 struct OperationClosure *opcls = cls;
3300 struct Client *c = opcls->client;
3301
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);
3306}
3307
3308
3309static int
3310check_client_zone_add_place (void *cls,
3311 const struct ZoneAddPlaceRequest *preq)
3312{
3313 return GNUNET_OK;
3314}
3315
3316
3317/**
3318 * Handle request to add PLACE record to GNS zone.
3319 */
3320static void
3321handle_client_zone_add_place (void *cls,
3322 const struct ZoneAddPlaceRequest *preq)
3323{
3324 struct Client *c = cls;
3325 struct GNUNET_SERVICE_Client *client = c->client;
3326
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,
3331 &name, &password);
3332 remaining -= offset;
3333 p += offset;
3334 const struct GNUNET_PeerIdentity *
3335 relays = (const struct GNUNET_PeerIdentity *) p;
3336 uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);
3337
3338 if (0 == offset || remaining != relay_size)
3339 {
3340 GNUNET_break (0);
3341 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3342 GNUNET_SERVICE_client_drop (client);
3343 return;
3344 }
3345
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);
3350
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);
3357
3358 rd.data = rec;
3359 rd.data_size = sizeof (*rec) + relay_size;
3360
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);
3364 if (NULL == ego)
3365 {
3366 client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
3367 }
3368 else
3369 {
3370 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3371 opcls->client = c;
3372 opcls->op_id = preq->op_id;
3373 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3374 name, 1, &rd,
3375 namestore_recv_records_store_result, opcls);
3376 /** @todo refresh stored records later */
3377 }
3378 GNUNET_SERVICE_client_continue (client);
3379}
3380
3381
3382static int
3383check_client_zone_add_nym (void *cls,
3384 const struct ZoneAddNymRequest *nreq)
3385{
3386 return GNUNET_OK;
3387}
3388
3389
3390/**
3391 * Handle request to add PLACE record to GNS zone.
3392 */
3393static void
3394handle_client_zone_add_nym (void *cls,
3395 const struct ZoneAddNymRequest *nreq)
3396{
3397 struct Client *c = cls;
3398 struct GNUNET_SERVICE_Client *client = c->client;
3399
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)
3405 {
3406 GNUNET_break (0);
3407 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3408 GNUNET_SERVICE_client_continue (client);
3409 return;
3410 }
3411
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);
3418
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);
3422 if (NULL == ego)
3423 {
3424 client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
3425 }
3426 else
3427 {
3428 struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
3429 opcls->client = c;
3430 opcls->op_id = nreq->op_id;
3431 GNUNET_NAMESTORE_records_store (namestore, &ego->key,
3432 name, 1, &rd,
3433 namestore_recv_records_store_result, opcls);
3434 /** @todo refresh stored records later */
3435 }
3436 GNUNET_SERVICE_client_continue (client);
3437}
3438
3439
3440const char *
3441path_basename (const char *path)
3442{
3443 const char *basename = strrchr (path, DIR_SEPARATOR);
3444 if (NULL != basename)
3445 basename++;
3446
3447 if (NULL == basename || '\0' == *basename)
3448 return NULL;
3449
3450 return basename;
3451}
3452
3453
3454struct PlaceLoadClosure
3455{
3456 const char *app_id;
3457 const char *ego_pub_str;
3458};
3459
3460
3461/** Load a place file */
3462int
3463file_place_load (void *cls, const char *place_filename)
3464{
3465 struct PlaceLoadClosure *plcls = cls;
3466
3467 const char *place_pub_str = path_basename (place_filename);
3468 if (NULL == place_pub_str)
3469 {
3470 GNUNET_break (0);
3471 return GNUNET_OK;
3472 }
3473
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,
3479 place_pub_str);
3480
3481 uint64_t file_size = 0;
3482 if (GNUNET_OK !=
3483 GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES)
3484 || file_size < sizeof (struct PlaceEnterRequest))
3485 {
3486 GNUNET_free (filename);
3487 return GNUNET_OK;
3488 }
3489
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))
3494 {
3495 GNUNET_free (ereq);
3496 return GNUNET_OK;
3497 }
3498
3499 uint16_t ereq_size = ntohs (ereq->header.size);
3500 if (read_size != ereq_size)
3501 {
3502 GNUNET_free (ereq);
3503 return GNUNET_OK;
3504 }
3505
3506 switch (ntohs (ereq->header.type))
3507 {
3508 case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
3509 if (ereq_size < sizeof (struct HostEnterRequest))
3510 {
3511 GNUNET_free (ereq);
3512 return GNUNET_OK;
3513 }
3514 struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
3515 host_enter (hreq, NULL);
3516 break;
3517
3518 case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
3519 if (ereq_size < sizeof (struct GuestEnterRequest))
3520 {
3521 GNUNET_free (ereq);
3522 return GNUNET_OK;
3523 }
3524 struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
3525 guest_enter (greq, NULL);
3526 break;
3527
3528 default:
3529 GNUNET_free (ereq);
3530 return GNUNET_OK;
3531 }
3532
3533 if (GNUNET_SYSERR == app_place_add (plcls->app_id, ereq))
3534 {
3535 GNUNET_assert (0);
3536 }
3537 GNUNET_free (ereq);
3538 return GNUNET_OK;
3539}
3540
3541
3542/**
3543 * Read @e place_pub_str entries in @a dir_ego
3544 *
3545 * @param dir_ego
3546 * Data directory of an application ego.
3547 * $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_str/
3548 */
3549int
3550scan_app_ego_dir (void *cls, const char *dir_ego)
3551{
3552 struct PlaceLoadClosure *plcls = cls;
3553 plcls->ego_pub_str = path_basename (dir_ego);
3554
3555 if (NULL != plcls->ego_pub_str)
3556 GNUNET_DISK_directory_scan (dir_ego, file_place_load, plcls);
3557
3558 return GNUNET_OK;
3559}
3560
3561/**
3562 * Read @e ego_pub_str entries in @a dir_app
3563 *
3564 * @param dir_app
3565 * Data directory of an application.
3566 * $GNUNET_DATA_HOME/social/apps/$app_id/
3567 */
3568int
3569scan_app_dir (void *cls, const char *dir_app)
3570{
3571 if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
3572 return GNUNET_OK;
3573
3574 struct PlaceLoadClosure plcls;
3575 plcls.app_id = path_basename (dir_app);
3576
3577 if (NULL != plcls.app_id)
3578 GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);
3579
3580 return GNUNET_OK;
3581}
3582
3583
3584static void
3585identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
3586 void **ctx, const char *name)
3587{
3588 if (NULL == id_ego) // end of initial list of egos
3589 return;
3590
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3592 "social service received ego %s\n",
3593 name);
3594
3595 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
3596 GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);
3597
3598 struct GNUNET_HashCode ego_pub_hash;
3599 GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
3600
3601 struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
3602 if (NULL == ego && NULL == name)
3603 {
3604 // an ego that is none of our business has been deleted
3605 return;
3606 }
3607 if (NULL != ego)
3608 {
3609 // one of our egos has been changed
3610 GNUNET_free (ego->name);
3611 if (NULL == name)
3612 {
3613 // one of our egos has been deleted
3614 GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
3615 GNUNET_free (ego);
3616 return;
3617 }
3618 }
3619 else
3620 {
3621 ego = GNUNET_malloc (sizeof (*ego));
3622 }
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);
3627
3628 GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
3629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3630
3631 // FIXME: notify clients about changed ego
3632}
3633
3634
3635/**
3636 * Initialize the PSYC service.
3637 *
3638 * @param cls Closure.
3639 * @param server The initialized server.
3640 * @param c Configuration to use.
3641 */
3642static void
3643run (void *cls,
3644 const struct GNUNET_CONFIGURATION_Handle *c,
3645 struct GNUNET_SERVICE_Handle *svc)
3646{
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "starting social service\n");
3649
3650 cfg = c;
3651 service = svc;
3652 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
3653
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);
3657
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);
3663
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);
3668
3669 if (GNUNET_OK !=
3670 GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
3671 &dir_social))
3672 {
3673 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3674 "social", "DATA_HOME");
3675 GNUNET_break (0);
3676 return;
3677 }
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");
3682
3683 GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);
3684
3685 GNUNET_SCHEDULER_add_shutdown (shutdown_task, NULL);
3686}
3687
3688
3689/**
3690 * Define "main" method using service macro.
3691 */
3692GNUNET_SERVICE_MAIN
3693("social",
3694 GNUNET_SERVICE_OPTION_NONE,
3695 run,
3696 client_notify_connect,
3697 client_notify_disconnect,
3698 NULL,
3699 GNUNET_MQ_hd_var_size (client_host_enter,
3700 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER,
3701 struct HostEnterRequest,
3702 NULL),
3703 GNUNET_MQ_hd_var_size (client_guest_enter,
3704 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER,
3705 struct GuestEnterRequest,
3706 NULL),
3707 GNUNET_MQ_hd_var_size (client_guest_enter_by_name,
3708 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME,
3709 struct GuestEnterByNameRequest,
3710 NULL),
3711 GNUNET_MQ_hd_var_size (client_join_decision,
3712 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
3713 struct GNUNET_PSYC_JoinDecisionMessage,
3714 NULL),
3715 GNUNET_MQ_hd_var_size (client_psyc_message,
3716 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
3717 struct GNUNET_MessageHeader,
3718 NULL),
3719 GNUNET_MQ_hd_var_size (client_history_replay,
3720 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY,
3721 struct GNUNET_PSYC_HistoryRequestMessage,
3722 NULL),
3723 GNUNET_MQ_hd_var_size (client_state_get,
3724 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
3725 struct GNUNET_PSYC_StateRequestMessage,
3726 NULL),
3727 GNUNET_MQ_hd_var_size (client_state_get_prefix,
3728 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
3729 struct GNUNET_PSYC_StateRequestMessage,
3730 NULL),
3731 GNUNET_MQ_hd_var_size (client_zone_add_place,
3732 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE,
3733 struct ZoneAddPlaceRequest,
3734 NULL),
3735 GNUNET_MQ_hd_var_size (client_zone_add_nym,
3736 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM,
3737 struct ZoneAddNymRequest,
3738 NULL),
3739 GNUNET_MQ_hd_var_size (client_app_connect,
3740 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT,
3741 struct AppConnectRequest,
3742 NULL),
3743 GNUNET_MQ_hd_fixed_size (client_app_detach,
3744 GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH,
3745 struct AppDetachRequest,
3746 NULL),
3747 GNUNET_MQ_hd_fixed_size (client_place_leave,
3748 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE,
3749 struct GNUNET_MessageHeader,
3750 NULL),
3751 GNUNET_MQ_hd_var_size (client_msg_proc_set,
3752 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET,
3753 struct MsgProcRequest,
3754 NULL),
3755 GNUNET_MQ_hd_fixed_size (client_msg_proc_clear,
3756 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR,
3757 struct GNUNET_MessageHeader,
3758 NULL));
3759
3760/* end of gnunet-service-social.c */