db377bfcff5cc68c83e887da76ca4bcf0a271265
[oweals/gnunet.git] / src / set / gnunet-service-set.c
1 /*
2       This file is part of GNUnet
3       (C) 2013 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
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       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file set/gnunet-service-set.c
23  * @brief two-peer set operations
24  * @author Florian Dold
25  */
26 #include "gnunet-service-set.h"
27 #include "set_protocol.h"
28
29
30 /**
31  * State of an operation where the peer has connected to us, but is not yet
32  * evaluating a set operation.  Once the peer has sent a concrete request, and
33  * the client has accepted or rejected it, this information will be deleted
34  * and replaced by the real set operation state.
35  */
36 struct OperationState
37 {
38   /**
39    * The identity of the requesting peer.  Needs to
40    * be stored here as the op spec might not have been created yet.
41    */
42   struct GNUNET_PeerIdentity peer;
43
44   /**
45    * Timeout task, if the incoming peer has not been accepted
46    * after the timeout, it will be disconnected.
47    */
48   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
49
50   /**
51    * Unique request id for the request from a remote peer, sent to the
52    * client, which will accept or reject the request.  Set to '0' iff
53    * the request has not been suggested yet.
54    */
55   uint32_t suggest_id;
56
57 };
58
59
60 /**
61  * A listener is inhabited by a client, and waits for evaluation
62  * requests from remote peers.
63  */
64 struct Listener
65 {
66   /**
67    * Listeners are held in a doubly linked list.
68    */
69   struct Listener *next;
70
71   /**
72    * Listeners are held in a doubly linked list.
73    */
74   struct Listener *prev;
75
76   /**
77    * Client that owns the listener.
78    * Only one client may own a listener.
79    */
80   struct GNUNET_SERVER_Client *client;
81
82   /**
83    * Message queue for the client
84    */
85   struct GNUNET_MQ_Handle *client_mq;
86
87   /**
88    * Application ID for the operation, used to distinguish
89    * multiple operations of the same type with the same peer.
90    */
91   struct GNUNET_HashCode app_id;
92
93   /**
94    * The type of the operation.
95    */
96   enum GNUNET_SET_OperationType operation;
97 };
98
99
100 /**
101  * Configuration of our local peer.
102  */
103 static const struct GNUNET_CONFIGURATION_Handle *configuration;
104
105 /**
106  * Handle to the cadet service, used to listen for and connect to
107  * remote peers.
108  */
109 static struct GNUNET_CADET_Handle *cadet;
110
111 /**
112  * Sets are held in a doubly linked list.
113  */
114 static struct Set *sets_head;
115
116 /**
117  * Sets are held in a doubly linked list.
118  */
119 static struct Set *sets_tail;
120
121 /**
122  * Listeners are held in a doubly linked list.
123  */
124 static struct Listener *listeners_head;
125
126 /**
127  * Listeners are held in a doubly linked list.
128  */
129 static struct Listener *listeners_tail;
130
131 /**
132  * Incoming sockets from remote peers are held in a doubly linked
133  * list.
134  */
135 static struct Operation *incoming_head;
136
137 /**
138  * Incoming sockets from remote peers are held in a doubly linked
139  * list.
140  */
141 static struct Operation *incoming_tail;
142
143 /**
144  * Counter for allocating unique IDs for clients, used to identify
145  * incoming operation requests from remote peers, that the client can
146  * choose to accept or refuse.
147  */
148 static uint32_t suggest_id = 1;
149
150
151 /**
152  * Get set that is owned by the given client, if any.
153  *
154  * @param client client to look for
155  * @return set that the client owns, NULL if the client
156  *         does not own a set
157  */
158 static struct Set *
159 set_get (struct GNUNET_SERVER_Client *client)
160 {
161   struct Set *set;
162
163   for (set = sets_head; NULL != set; set = set->next)
164     if (set->client == client)
165       return set;
166   return NULL;
167 }
168
169
170 /**
171  * Get the listener associated with the given client, if any.
172  *
173  * @param client the client
174  * @return listener associated with the client, NULL
175  *         if there isn't any
176  */
177 static struct Listener *
178 listener_get (struct GNUNET_SERVER_Client *client)
179 {
180   struct Listener *listener;
181
182   for (listener = listeners_head; NULL != listener; listener = listener->next)
183     if (listener->client == client)
184       return listener;
185   return NULL;
186 }
187
188
189 /**
190  * Get the incoming socket associated with the given id.
191  *
192  * @param id id to look for
193  * @return the incoming socket associated with the id,
194  *         or NULL if there is none
195  */
196 static struct Operation *
197 get_incoming (uint32_t id)
198 {
199   struct Operation *op;
200
201   for (op = incoming_head; NULL != op; op = op->next)
202     if (op->state->suggest_id == id)
203     {
204       // FIXME: remove this assertion once the corresponding bug is gone!
205       GNUNET_assert (GNUNET_YES == op->is_incoming);
206       return op;
207     }
208   return NULL;
209 }
210
211
212 /**
213  * Destroy a listener, free all resources associated with it.
214  *
215  * @param listener listener to destroy
216  */
217 static void
218 listener_destroy (struct Listener *listener)
219 {
220   /* If the client is not dead yet, destroy it.
221    * The client's destroy callback will destroy the listener again. */
222   if (NULL != listener->client)
223   {
224     struct GNUNET_SERVER_Client *client = listener->client;
225
226     listener->client = NULL;
227     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228                 "disconnecting listener client\n");
229     GNUNET_SERVER_client_disconnect (client);
230     return;
231   }
232   if (NULL != listener->client_mq)
233   {
234     GNUNET_MQ_destroy (listener->client_mq);
235     listener->client_mq = NULL;
236   }
237   GNUNET_CONTAINER_DLL_remove (listeners_head,
238                                listeners_tail,
239                                listener);
240   GNUNET_free (listener);
241 }
242
243
244 /**
245  * Context for the #garbage_collect_cb().
246  */
247 struct GarbageContext
248 {
249
250   /**
251    * Map for which we are garbage collecting removed elements.
252    */
253   struct GNUNET_CONTAINER_MultiHashMap *map;
254
255   /**
256    * Lowest generation for which an operation is still pending.
257    */
258   unsigned int min_op_generation;
259
260   /**
261    * Largest generation for which an operation is still pending.
262    */
263   unsigned int max_op_generation;
264
265 };
266
267
268 /**
269  * Function invoked to check if an element can be removed from
270  * the set's history because it is no longer needed.
271  *
272  * @param cls the `struct GarbageContext *`
273  * @param key key of the element in the map
274  * @param value the `struct ElementEntry *`
275  * @return #GNUNET_OK (continue to iterate)
276  */
277 static int
278 garbage_collect_cb (void *cls,
279                     const struct GNUNET_HashCode *key,
280                     void *value)
281 {
282   struct GarbageContext *gc = cls;
283   struct ElementEntry *ee = value;
284
285   if (GNUNET_YES != ee->removed)
286     return GNUNET_OK;
287   if ( (gc->max_op_generation < ee->generation_added) ||
288        (ee->generation_removed > gc->min_op_generation) )
289   {
290     GNUNET_assert (GNUNET_YES ==
291                    GNUNET_CONTAINER_multihashmap_remove (gc->map,
292                                                          key,
293                                                          ee));
294     GNUNET_free (ee);
295   }
296   return GNUNET_OK;
297 }
298
299
300 /**
301  * Collect and destroy elements that are not needed anymore, because
302  * their lifetime (as determined by their generation) does not overlap
303  * with any active set operation.
304  *
305  * @param set set to garbage collect
306  */
307 static void
308 collect_generation_garbage (struct Set *set)
309 {
310   struct Operation *op;
311   struct GarbageContext gc;
312
313   gc.min_op_generation = UINT_MAX;
314   gc.max_op_generation = 0;
315   for (op = set->ops_head; NULL != op; op = op->next)
316   {
317     gc.min_op_generation = GNUNET_MIN (gc.min_op_generation,
318                                        op->generation_created);
319     gc.max_op_generation = GNUNET_MAX (gc.max_op_generation,
320                                        op->generation_created);
321   }
322   gc.map = set->elements;
323   GNUNET_CONTAINER_multihashmap_iterate (set->elements,
324                                          &garbage_collect_cb,
325                                          &gc);
326 }
327
328
329 /**
330  * Destroy the given operation.  Call the implementation-specific
331  * cancel function of the operation.  Disconnects from the remote
332  * peer.  Does not disconnect the client, as there may be multiple
333  * operations per set.
334  *
335  * @param op operation to destroy
336  * @param gc #GNUNET_YES to perform garbage collection on the set
337  */
338 void
339 _GSS_operation_destroy (struct Operation *op,
340                         int gc)
341 {
342   struct Set *set;
343   struct GNUNET_CADET_Channel *channel;
344
345   if (NULL == op->vt)
346   {
347     /* already in #_GSS_operation_destroy() */
348     return;
349   }
350   GNUNET_assert (GNUNET_NO == op->is_incoming);
351   GNUNET_assert (NULL != op->spec);
352   set = op->spec->set;
353   GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
354                                op->spec->set->ops_tail,
355                                op);
356   op->vt->cancel (op);
357   op->vt = NULL;
358   if (NULL != op->spec)
359   {
360     if (NULL != op->spec->context_msg)
361     {
362       GNUNET_free (op->spec->context_msg);
363       op->spec->context_msg = NULL;
364     }
365     GNUNET_free (op->spec);
366     op->spec = NULL;
367   }
368   if (NULL != op->mq)
369   {
370     GNUNET_MQ_destroy (op->mq);
371     op->mq = NULL;
372   }
373   if (NULL != (channel = op->channel))
374   {
375     op->channel = NULL;
376     GNUNET_CADET_channel_destroy (channel);
377   }
378   if (GNUNET_YES == gc)
379     collect_generation_garbage (set);
380   /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL,
381    * there was a channel end handler that will free 'op' on the call stack. */
382 }
383
384
385 /**
386  * Iterator over hash map entries to free element entries.
387  *
388  * @param cls closure
389  * @param key current key code
390  * @param value a `struct ElementEntry *` to be free'd
391  * @return #GNUNET_YES (continue to iterate)
392  */
393 static int
394 destroy_elements_iterator (void *cls,
395                            const struct GNUNET_HashCode *key,
396                            void *value)
397 {
398   struct ElementEntry *ee = value;
399
400   GNUNET_free (ee);
401   return GNUNET_YES;
402 }
403
404
405 /**
406  * Destroy a set, and free all resources and operations associated with it.
407  *
408  * @param set the set to destroy
409  */
410 static void
411 set_destroy (struct Set *set)
412 {
413   if (NULL != set->client)
414   {
415     /* If the client is not dead yet, destroy it.  The client's destroy
416      * callback will call `set_destroy()` again in this case.  We do
417      * this so that the channel end handler still has a valid set handle
418      * to destroy. */
419     struct GNUNET_SERVER_Client *client = set->client;
420
421     set->client = NULL;
422     GNUNET_SERVER_client_disconnect (client);
423     return;
424   }
425   GNUNET_assert (NULL != set->state);
426   while (NULL != set->ops_head)
427     _GSS_operation_destroy (set->ops_head, GNUNET_NO);
428   set->vt->destroy_set (set->state);
429   set->state = NULL;
430   if (NULL != set->client_mq)
431   {
432     GNUNET_MQ_destroy (set->client_mq);
433     set->client_mq = NULL;
434   }
435   if (NULL != set->iter)
436   {
437     GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
438     set->iter = NULL;
439   }
440   if (NULL != set->elements)
441   {
442     GNUNET_CONTAINER_multihashmap_iterate (set->elements,
443                                            &destroy_elements_iterator,
444                                            NULL);
445     GNUNET_CONTAINER_multihashmap_destroy (set->elements);
446     set->elements = NULL;
447   }
448   GNUNET_CONTAINER_DLL_remove (sets_head,
449                                sets_tail,
450                                set);
451   GNUNET_free (set);
452 }
453
454
455 /**
456  * Clean up after a client has disconnected
457  *
458  * @param cls closure, unused
459  * @param client the client to clean up after
460  */
461 static void
462 handle_client_disconnect (void *cls,
463                           struct GNUNET_SERVER_Client *client)
464 {
465   struct Set *set;
466   struct Listener *listener;
467
468   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469               "client disconnected, cleaning up\n");
470   set = set_get (client);
471   if (NULL != set)
472   {
473     set->client = NULL;
474     set_destroy (set);
475     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476                 "Client's set destroyed\n");
477   }
478   listener = listener_get (client);
479   if (NULL != listener)
480   {
481     listener->client = NULL;
482     listener_destroy (listener);
483     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484                 "Client's listener destroyed\n");
485   }
486 }
487
488
489 /**
490  * Destroy an incoming request from a remote peer
491  *
492  * @param incoming remote request to destroy
493  */
494 static void
495 incoming_destroy (struct Operation *incoming)
496 {
497   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
498   GNUNET_CONTAINER_DLL_remove (incoming_head,
499                                incoming_tail,
500                                incoming);
501   if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
502   {
503     GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
504     incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
505   }
506   GNUNET_assert (NULL != incoming->state);
507   GNUNET_free (incoming->state);
508   /* make sure that the tunnel end handler will not destroy us again */
509   incoming->vt = NULL;
510   incoming->state = NULL;
511   if (NULL != incoming->mq)
512   {
513     GNUNET_MQ_destroy (incoming->mq);
514     incoming->mq = NULL;
515   }
516   if (NULL != incoming->channel)
517   {
518     GNUNET_CADET_channel_destroy (incoming->channel);
519     incoming->channel = NULL;
520   }
521 }
522
523
524 /**
525  * Find a listener that is interested in the given operation type
526  * and application id.
527  *
528  * @param op operation type to look for
529  * @param app_id application id to look for
530  * @return a matching listener, or NULL if no listener matches the
531  *         given operation and application id
532  */
533 static struct Listener *
534 listener_get_by_target (enum GNUNET_SET_OperationType op,
535                         const struct GNUNET_HashCode *app_id)
536 {
537   struct Listener *l;
538
539   for (l = listeners_head; NULL != l; l = l->next)
540   {
541     if (l->operation != op)
542       continue;
543     if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
544       continue;
545     return l;
546   }
547   return NULL;
548 }
549
550
551 /**
552  * Suggest the given request to the listener. The listening client can then
553  * accept or reject the remote request.
554  *
555  * @param incoming the incoming peer with the request to suggest
556  * @param listener the listener to suggest the request to
557  */
558 static void
559 incoming_suggest (struct Operation *incoming, struct Listener *listener)
560 {
561   struct GNUNET_MQ_Envelope *mqm;
562   struct GNUNET_SET_RequestMessage *cmsg;
563
564   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
565   GNUNET_assert (NULL != incoming->state);
566   GNUNET_assert (NULL != incoming->spec);
567   GNUNET_assert (0 == incoming->state->suggest_id);
568   incoming->state->suggest_id = suggest_id++;
569
570   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
571   GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
572   incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
573
574   mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
575                                  incoming->spec->context_msg);
576   GNUNET_assert (NULL != mqm);
577   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
578               incoming->state->suggest_id);
579   cmsg->accept_id = htonl (incoming->state->suggest_id);
580   cmsg->peer_id = incoming->spec->peer;
581   GNUNET_MQ_send (listener->client_mq, mqm);
582 }
583
584
585 /**
586  * Handle a request for a set operation from
587  * another peer.
588  *
589  * This msg is expected as the first and only msg handled through the
590  * non-operation bound virtual table, acceptance of this operation replaces
591  * our virtual table and subsequent msgs would be routed differently.
592  *
593  * @param op the operation state
594  * @param mh the received message
595  * @return #GNUNET_OK if the channel should be kept alive,
596  *         #GNUNET_SYSERR to destroy the channel
597  */
598 static int
599 handle_incoming_msg (struct Operation *op,
600                      const struct GNUNET_MessageHeader *mh)
601 {
602   const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
603   struct Listener *listener;
604   struct OperationSpecification *spec;
605
606   GNUNET_assert (GNUNET_YES == op->is_incoming);
607
608   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got op request\n");
609
610   if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
611   {
612     GNUNET_break_op (0);
613     return GNUNET_SYSERR;
614   }
615
616   /* double operation request */
617   if (NULL != op->spec)
618   {
619     GNUNET_break_op (0);
620     return GNUNET_SYSERR;
621   }
622
623   spec = GNUNET_new (struct OperationSpecification);
624   spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
625   // for simplicity we just backup the context msg instead of rebuilding it later on
626   if (NULL != spec->context_msg)
627     spec->context_msg = GNUNET_copy_message (spec->context_msg);
628   spec->operation = ntohl (msg->operation);
629   spec->app_id = msg->app_id;
630   spec->salt = ntohl (msg->salt);
631   spec->peer = op->state->peer;
632   spec->remote_element_count = ntohl (msg->element_count);
633
634   op->spec = spec;
635
636   if ( (NULL != spec->context_msg) &&
637        (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
638   {
639     GNUNET_break_op (0);
640     return GNUNET_SYSERR;
641   }
642
643   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
644               ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
645   listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
646   if (NULL == listener)
647   {
648     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649                 "no listener matches incoming request, waiting with timeout\n");
650     return GNUNET_OK;
651   }
652   incoming_suggest (op, listener);
653   return GNUNET_OK;
654 }
655
656
657 /**
658  * Send the next element of a set to the set's client.  The next element is given by
659  * the set's current hashmap iterator.  The set's iterator will be set to NULL if there
660  * are no more elements in the set.  The caller must ensure that the set's iterator is
661  * valid.
662  *
663  * @param set set that should send its next element to its client
664  */
665 static void
666 send_client_element (struct Set *set)
667 {
668   int ret;
669   struct ElementEntry *ee;
670   struct GNUNET_MQ_Envelope *ev;
671
672   GNUNET_assert (NULL != set->iter);
673   ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
674   if (GNUNET_NO == ret)
675   {
676     ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
677     GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
678     set->iter = NULL;
679   }
680   else
681   {
682     struct GNUNET_SET_IterResponseMessage *msg;
683
684     GNUNET_assert (NULL != ee);
685     ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
686     memcpy (&msg[1], ee->element.data, ee->element.size);
687     msg->element_type = ee->element.type;
688   }
689   GNUNET_MQ_send (set->client_mq, ev);
690 }
691
692
693 /**
694  * Called when a client wants to iterate the elements of a set.
695  *
696  * @param cls unused
697  * @param client client that sent the message
698  * @param m message sent by the client
699  */
700 static void
701 handle_client_iterate (void *cls,
702                        struct GNUNET_SERVER_Client *client,
703                        const struct GNUNET_MessageHeader *m)
704 {
705   struct Set *set;
706
707   // iterate over a non existing set
708   set = set_get (client);
709   if (NULL == set)
710   {
711     GNUNET_break (0);
712     GNUNET_SERVER_client_disconnect (client);
713     return;
714   }
715
716   // only one concurrent iterate-action per set
717   if (NULL != set->iter)
718   {
719     GNUNET_break (0);
720     GNUNET_SERVER_client_disconnect (client);
721     return;
722   }
723   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
724               GNUNET_CONTAINER_multihashmap_size (set->elements));
725   GNUNET_SERVER_receive_done (client, GNUNET_OK);
726   set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
727   send_client_element (set);
728 }
729
730
731 /**
732  * Called when a client wants to create a new set.
733  *
734  * @param cls unused
735  * @param client client that sent the message
736  * @param m message sent by the client
737  */
738 static void
739 handle_client_create_set (void *cls,
740                           struct GNUNET_SERVER_Client *client,
741                           const struct GNUNET_MessageHeader *m)
742 {
743   const struct GNUNET_SET_CreateMessage *msg;
744   struct Set *set;
745
746   msg = (const struct GNUNET_SET_CreateMessage *) m;
747   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748               "client created new set (operation %u)\n",
749               ntohs (msg->operation));
750
751   // max. one set per client!
752   if (NULL != set_get (client))
753   {
754     GNUNET_break (0);
755     GNUNET_SERVER_client_disconnect (client);
756     return;
757   }
758
759   set = GNUNET_new (struct Set);
760
761   switch (ntohs (msg->operation))
762   {
763   case GNUNET_SET_OPERATION_INTERSECTION:
764     set->vt = _GSS_intersection_vt ();
765     break;
766   case GNUNET_SET_OPERATION_UNION:
767     set->vt = _GSS_union_vt ();
768     break;
769   default:
770     GNUNET_free (set);
771     GNUNET_break (0);
772     GNUNET_SERVER_client_disconnect (client);
773     return;
774   }
775
776   set->state = set->vt->create ();
777   set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
778   set->client = client;
779   set->client_mq = GNUNET_MQ_queue_for_server_client (client);
780   GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
781   GNUNET_SERVER_receive_done (client, GNUNET_OK);
782 }
783
784
785 /**
786  * Called when a client wants to create a new listener.
787  *
788  * @param cls unused
789  * @param client client that sent the message
790  * @param m message sent by the client
791  */
792 static void
793 handle_client_listen (void *cls,
794                       struct GNUNET_SERVER_Client *client,
795                       const struct GNUNET_MessageHeader *m)
796 {
797   const struct GNUNET_SET_ListenMessage *msg;
798   struct Listener *listener;
799   struct Operation *op;
800
801   msg = (const struct GNUNET_SET_ListenMessage *) m;
802   /* max. one per client! */
803   if (NULL != listener_get (client))
804   {
805     GNUNET_break (0);
806     GNUNET_SERVER_client_disconnect (client);
807     return;
808   }
809
810   listener = GNUNET_new (struct Listener);
811   listener->client = client;
812   listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
813   listener->app_id = msg->app_id;
814   listener->operation = ntohl (msg->operation);
815   GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
816   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817               "new listener created (op %u, app %s)\n",
818               listener->operation,
819               GNUNET_h2s (&listener->app_id));
820
821   /* check for incoming requests the listener is interested in */
822   for (op = incoming_head; NULL != op; op = op->next)
823   {
824     if (NULL == op->spec)
825     {
826       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827                   "request has no spec yet\n");
828       continue;
829     }
830     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831                 "considering (op: %u, app: %s, suggest: %u)\n",
832                 op->spec->operation,
833                 GNUNET_h2s (&op->spec->app_id),
834                 op->state->suggest_id);
835
836     /* don't consider the incoming request if it has been already suggested to a listener */
837     if (0 != op->state->suggest_id)
838       continue;
839     if (listener->operation != op->spec->operation)
840       continue;
841     if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
842       continue;
843     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844                 "request suggested\n");
845     incoming_suggest (op, listener);
846   }
847   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848               "considered all incoming requests\n");
849   GNUNET_SERVER_receive_done (client, GNUNET_OK);
850 }
851
852
853 /**
854  * Called when the listening client rejects an operation
855  * request by another peer.
856  *
857  * @param cls unused
858  * @param client client that sent the message
859  * @param m message sent by the client
860  */
861 static void
862 handle_client_reject (void *cls,
863                       struct GNUNET_SERVER_Client *client,
864                       const struct GNUNET_MessageHeader *m)
865 {
866   struct Operation *incoming;
867   const struct GNUNET_SET_AcceptRejectMessage *msg;
868
869   msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
870   GNUNET_break (0 == ntohl (msg->request_id));
871
872   // no matching incoming operation for this reject
873   incoming = get_incoming (ntohl (msg->accept_reject_id));
874   if (NULL == incoming)
875   {
876     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
877     return;
878   }
879   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880               "peer request rejected by client\n");
881
882   GNUNET_CADET_channel_destroy (incoming->channel);
883   //channel destruction handler called immediately upon destruction
884   GNUNET_SERVER_receive_done (client, GNUNET_OK);
885 }
886
887
888 /**
889  * Called when a client wants to add/remove an element to/from a
890  * set it inhabits.
891  *
892  * @param cls unused
893  * @param client client that sent the message
894  * @param m message sent by the client
895  */
896 static void
897 handle_client_add_remove (void *cls,
898                           struct GNUNET_SERVER_Client *client,
899                           const struct GNUNET_MessageHeader *m)
900 {
901   struct Set *set;
902   const struct GNUNET_SET_ElementMessage *msg;
903   struct GNUNET_SET_Element el;
904   struct ElementEntry *ee;
905
906   // client without a set requested an operation
907   set = set_get (client);
908   if (NULL == set)
909   {
910     GNUNET_break (0);
911     GNUNET_SERVER_client_disconnect (client);
912     return;
913   }
914   GNUNET_SERVER_receive_done (client, GNUNET_OK);
915   msg = (const struct GNUNET_SET_ElementMessage *) m;
916   el.size = ntohs (m->size) - sizeof *msg;
917   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918               "client ins/rem element of size %u\n", el.size);
919   el.data = &msg[1];
920   if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
921   {
922     struct GNUNET_HashCode hash;
923
924     GNUNET_CRYPTO_hash (el.data, el.size, &hash);
925     ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
926     if (NULL == ee)
927     {
928       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
929                   "client tried to remove non-existing element\n");
930       return;
931     }
932     if (GNUNET_YES == ee->removed)
933     {
934       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
935                   "client tried to remove element twice\n");
936       return;
937     }
938     ee->removed = GNUNET_YES;
939     ee->generation_removed = set->current_generation;
940     set->vt->remove (set->state, ee);
941   }
942   else
943   {
944     struct ElementEntry *ee_dup;
945
946     ee = GNUNET_malloc (el.size + sizeof *ee);
947     ee->element.size = el.size;
948     memcpy (&ee[1], el.data, el.size);
949     ee->element.data = &ee[1];
950     ee->generation_added = set->current_generation;
951     ee->remote = GNUNET_NO;
952     GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
953     ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
954                                                 &ee->element_hash);
955     if (NULL != ee_dup)
956     {
957       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958                   "element inserted twice, ignoring\n");
959       GNUNET_free (ee);
960       return;
961     }
962     GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
963                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
964     set->vt->add (set->state, ee);
965   }
966 }
967
968
969 /**
970  * Called when a client wants to evaluate a set operation with another peer.
971  *
972  * @param cls unused
973  * @param client client that sent the message
974  * @param m message sent by the client
975  */
976 static void
977 handle_client_evaluate (void *cls,
978                         struct GNUNET_SERVER_Client *client,
979                         const struct GNUNET_MessageHeader *m)
980 {
981   struct Set *set;
982   const struct GNUNET_SET_EvaluateMessage *msg;
983   struct OperationSpecification *spec;
984   struct Operation *op;
985
986   set = set_get (client);
987   if (NULL == set)
988   {
989     GNUNET_break (0);
990     GNUNET_SERVER_client_disconnect (client);
991     return;
992   }
993
994   msg = (const struct GNUNET_SET_EvaluateMessage *) m;
995   spec = GNUNET_new (struct OperationSpecification);
996   spec->operation = set->operation;
997   spec->app_id = msg->app_id;
998   spec->salt = ntohl (msg->salt);
999   spec->peer = msg->target_peer;
1000   spec->set = set;
1001   spec->result_mode = ntohs (msg->result_mode);
1002   spec->client_request_id = ntohl (msg->request_id);
1003   spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
1004
1005   // for simplicity we just backup the context msg instead of rebuilding it later on
1006   if (NULL != spec->context_msg)
1007     spec->context_msg = GNUNET_copy_message (spec->context_msg);
1008
1009   op = GNUNET_new (struct Operation);
1010   op->spec = spec;
1011   op->generation_created = set->current_generation++;
1012   op->vt = set->vt;
1013   GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1014
1015   op->channel = GNUNET_CADET_channel_create (cadet, op, &msg->target_peer,
1016                                             GNUNET_APPLICATION_TYPE_SET,
1017                                             GNUNET_CADET_OPTION_RELIABLE);
1018
1019   op->mq = GNUNET_CADET_mq_create (op->channel);
1020
1021   set->vt->evaluate (op);
1022   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1023 }
1024
1025
1026 /**
1027  * Handle an ack from a client, and send the next element.
1028  *
1029  * @param cls unused
1030  * @param client the client
1031  * @param m the message
1032  */
1033 static void
1034 handle_client_iter_ack (void *cls,
1035                    struct GNUNET_SERVER_Client *client,
1036                    const struct GNUNET_MessageHeader *m)
1037 {
1038   struct Set *set;
1039
1040   // client without a set requested an operation
1041   set = set_get (client);
1042   if (NULL == set)
1043   {
1044     GNUNET_break (0);
1045     GNUNET_SERVER_client_disconnect (client);
1046     return;
1047   }
1048
1049   // client sent an ack, but we were not expecting one
1050   if (NULL == set->iter)
1051   {
1052     GNUNET_break (0);
1053     GNUNET_SERVER_client_disconnect (client);
1054     return;
1055   }
1056
1057   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1058   send_client_element (set);
1059 }
1060
1061
1062 /**
1063  * Handle a request from the client to
1064  * cancel a running set operation.
1065  *
1066  * @param cls unused
1067  * @param client the client
1068  * @param mh the message
1069  */
1070 static void
1071 handle_client_cancel (void *cls,
1072                       struct GNUNET_SERVER_Client *client,
1073                       const struct GNUNET_MessageHeader *mh)
1074 {
1075   const struct GNUNET_SET_CancelMessage *msg =
1076       (const struct GNUNET_SET_CancelMessage *) mh;
1077   struct Set *set;
1078   struct Operation *op;
1079   int found;
1080
1081   // client without a set requested an operation
1082   set = set_get (client);
1083   if (NULL == set)
1084   {
1085     GNUNET_break (0);
1086     GNUNET_SERVER_client_disconnect (client);
1087     return;
1088   }
1089
1090   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091               "client requested cancel for op %u\n",
1092               ntohl (msg->request_id));
1093
1094   found = GNUNET_NO;
1095   for (op = set->ops_head; NULL != op; op = op->next)
1096   {
1097     if (op->spec->client_request_id == ntohl (msg->request_id))
1098     {
1099       found = GNUNET_YES;
1100       break;
1101     }
1102   }
1103
1104   /* It may happen that the operation was destroyed due to
1105    * the other peer disconnecting.  The client may not know about this
1106    * yet and try to cancel the (non non-existent) operation.
1107    */
1108   if (GNUNET_NO != found)
1109     _GSS_operation_destroy (op,
1110                             GNUNET_YES);
1111   else
1112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113                 "client canceled non-existent op\n");
1114
1115
1116   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1117 }
1118
1119
1120 /**
1121  * Handle a request from the client to accept
1122  * a set operation that came from a remote peer.
1123  * We forward the accept to the associated operation for handling
1124  *
1125  * @param cls unused
1126  * @param client the client
1127  * @param mh the message
1128  */
1129 static void
1130 handle_client_accept (void *cls,
1131                       struct GNUNET_SERVER_Client *client,
1132                       const struct GNUNET_MessageHeader *mh)
1133 {
1134   struct Set *set;
1135   const struct GNUNET_SET_AcceptRejectMessage *msg;
1136   struct Operation *op;
1137
1138   msg = (const struct GNUNET_SET_AcceptRejectMessage *) mh;
1139
1140   // client without a set requested an operation
1141   set = set_get (client);
1142
1143   if (NULL == set)
1144   {
1145     GNUNET_break (0);
1146     GNUNET_SERVER_client_disconnect (client);
1147     return;
1148   }
1149
1150   op = get_incoming (ntohl (msg->accept_reject_id));
1151
1152   /* it is not an error if the set op does not exist -- it may
1153    * have been destroyed when the partner peer disconnected. */
1154   if (NULL == op)
1155   {
1156     struct GNUNET_SET_ResultMessage *result_message;
1157     struct GNUNET_MQ_Envelope *ev;
1158     ev = GNUNET_MQ_msg (result_message, GNUNET_MESSAGE_TYPE_SET_RESULT);
1159     result_message->request_id = msg->request_id;
1160     result_message->element_type = 0;
1161     result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1162     GNUNET_MQ_send (set->client_mq, ev);
1163     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1164     return;
1165   }
1166
1167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168               "client accepting %u\n",
1169               ntohl (msg->accept_reject_id));
1170
1171   GNUNET_assert (GNUNET_YES == op->is_incoming);
1172
1173
1174   op->spec->set = set;
1175
1176   GNUNET_assert (GNUNET_YES == op->is_incoming);
1177   op->is_incoming = GNUNET_NO;
1178   GNUNET_assert (NULL != op->state);
1179   GNUNET_free (op->state);
1180   op->state = NULL;
1181   GNUNET_CONTAINER_DLL_remove (incoming_head,
1182                                incoming_tail,
1183                                op);
1184
1185   GNUNET_assert (NULL != op->spec->set);
1186   GNUNET_assert (NULL != op->spec->set->vt);
1187
1188   GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1189
1190   op->spec->client_request_id = ntohl (msg->request_id);
1191   op->spec->result_mode = ntohs (msg->result_mode);
1192   op->generation_created = set->current_generation++;
1193   op->vt = op->spec->set->vt;
1194   GNUNET_assert (NULL != op->vt->accept);
1195   set->vt->accept (op);
1196   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1197 }
1198
1199
1200 /**
1201  * Called to clean up, after a shutdown has been requested.
1202  *
1203  * @param cls closure
1204  * @param tc context information (why was this task triggered now)
1205  */
1206 static void
1207 shutdown_task (void *cls,
1208                const struct GNUNET_SCHEDULER_TaskContext *tc)
1209 {
1210   while (NULL != incoming_head)
1211     incoming_destroy (incoming_head);
1212
1213   while (NULL != listeners_head)
1214     listener_destroy (listeners_head);
1215
1216   while (NULL != sets_head)
1217     set_destroy (sets_head);
1218
1219   /* it's important to destroy cadet at the end, as all channels
1220    * must be destroyed before the cadet handle! */
1221   if (NULL != cadet)
1222   {
1223     GNUNET_CADET_disconnect (cadet);
1224     cadet = NULL;
1225   }
1226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227               "handled shutdown request\n");
1228 }
1229
1230
1231 /**
1232  * Timeout happens iff:
1233  *  - we suggested an operation to our listener,
1234  *    but did not receive a response in time
1235  *  - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1236  *  - shutdown (obviously)
1237  *
1238  * @param cls channel context
1239  * @param tc context information (why was this task triggered now)
1240  */
1241 static void
1242 incoming_timeout_cb (void *cls,
1243                      const struct GNUNET_SCHEDULER_TaskContext *tc)
1244 {
1245   struct Operation *incoming = cls;
1246
1247   incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1248   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1249   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1250     return;
1251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252               "remote peer timed out\n");
1253   incoming_destroy (incoming);
1254 }
1255
1256
1257 /**
1258  * Terminates an incoming operation in case we have not yet received an
1259  * operation request. Called by the channel destruction handler.
1260  *
1261  * @param op the channel context
1262  */
1263 static void
1264 handle_incoming_disconnect (struct Operation *op)
1265 {
1266   GNUNET_assert (GNUNET_YES == op->is_incoming);
1267   /* channel is already dead, incoming_destroy must not
1268    * destroy it ... */
1269   op->channel = NULL;
1270   incoming_destroy (op);
1271   op->vt = NULL;
1272 }
1273
1274
1275 /**
1276  * Method called whenever another peer has added us to a channel
1277  * the other peer initiated.
1278  * Only called (once) upon reception of data with a message type which was
1279  * subscribed to in GNUNET_CADET_connect().
1280  *
1281  * The channel context represents the operation itself and gets added to a DLL,
1282  * from where it gets looked up when our local listener client responds
1283  * to a proposed/suggested operation or connects and associates with this operation.
1284  *
1285  * @param cls closure
1286  * @param channel new handle to the channel
1287  * @param initiator peer that started the channel
1288  * @param port Port this channel is for.
1289  * @param options Unused.
1290  * @return initial channel context for the channel
1291  *         (can be NULL -- that's not an error)
1292  */
1293 static void *
1294 channel_new_cb (void *cls,
1295                struct GNUNET_CADET_Channel *channel,
1296                const struct GNUNET_PeerIdentity *initiator,
1297                uint32_t port, enum GNUNET_CADET_ChannelOption options)
1298 {
1299   struct Operation *incoming;
1300   static const struct SetVT incoming_vt = {
1301     .msg_handler = handle_incoming_msg,
1302     .peer_disconnect = handle_incoming_disconnect
1303   };
1304
1305   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306               "new incoming channel\n");
1307
1308   if (GNUNET_APPLICATION_TYPE_SET != port)
1309   {
1310     GNUNET_break (0);
1311     GNUNET_CADET_channel_destroy (channel);
1312     return NULL;
1313   }
1314
1315   incoming = GNUNET_new (struct Operation);
1316   incoming->is_incoming = GNUNET_YES;
1317   incoming->state = GNUNET_new (struct OperationState);
1318   incoming->state->peer = *initiator;
1319   incoming->channel = channel;
1320   incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
1321   incoming->vt = &incoming_vt;
1322   incoming->state->timeout_task =
1323       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1324                                     &incoming_timeout_cb, incoming);
1325   GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
1326
1327   return incoming;
1328 }
1329
1330
1331 /**
1332  * Function called whenever a channel is destroyed.  Should clean up
1333  * any associated state.  It must NOT call
1334  * GNUNET_CADET_channel_destroy() on the channel.
1335  *
1336  * The peer_disconnect function is part of a a virtual table set initially either
1337  * when a peer creates a new channel with us (channel_new_cb), or once we create
1338  * a new channel ourselves (evaluate).
1339  *
1340  * Once we know the exact type of operation (union/intersection), the vt is
1341  * replaced with an operation specific instance (_GSS_[op]_vt).
1342  *
1343  * @param cls closure (set from GNUNET_CADET_connect())
1344  * @param channel connection to the other end (henceforth invalid)
1345  * @param channel_ctx place where local state associated
1346  *                   with the channel is stored
1347  */
1348 static void
1349 channel_end_cb (void *cls,
1350                 const struct GNUNET_CADET_Channel *channel, void *channel_ctx)
1351 {
1352   struct Operation *op = channel_ctx;
1353
1354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355               "channel end cb called\n");
1356   op->channel = NULL;
1357   /* the vt can be null if a client already requested canceling op. */
1358   if (NULL != op->vt)
1359   {
1360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361                 "calling peer disconnect due to channel end\n");
1362     op->vt->peer_disconnect (op);
1363   }
1364
1365   if (GNUNET_YES == op->keep)
1366     return;
1367
1368   /* cadet will never call us with the context again! */
1369   GNUNET_free (channel_ctx);
1370   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371               "channel end cb finished\n");
1372 }
1373
1374
1375 /**
1376  * Functions with this signature are called whenever a message is
1377  * received via a cadet channel.
1378  *
1379  * The msg_handler is a virtual table set in initially either when a peer
1380  * creates a new channel with us (channel_new_cb), or once we create a new channel
1381  * ourselves (evaluate).
1382  *
1383  * Once we know the exact type of operation (union/intersection), the vt is
1384  * replaced with an operation specific instance (_GSS_[op]_vt).
1385  *
1386  * @param cls Closure (set from GNUNET_CADET_connect()).
1387  * @param channel Connection to the other end.
1388  * @param channel_ctx Place to store local state associated with the channel.
1389  * @param message The actual message.
1390  * @return #GNUNET_OK to keep the channel open,
1391  *         #GNUNET_SYSERR to close it (signal serious error).
1392  */
1393 static int
1394 dispatch_p2p_message (void *cls,
1395                       struct GNUNET_CADET_Channel *channel,
1396                       void **channel_ctx,
1397                       const struct GNUNET_MessageHeader *message)
1398 {
1399   struct Operation *op = *channel_ctx;
1400   int ret;
1401
1402   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403               "dispatching cadet message (type: %u)\n",
1404               ntohs (message->type));
1405   /* do this before the handler, as the handler might kill the channel */
1406   GNUNET_CADET_receive_done (channel);
1407   if (NULL != op->vt)
1408     ret = op->vt->msg_handler (op, message);
1409   else
1410     ret = GNUNET_SYSERR;
1411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412               "handled cadet message (type: %u)\n",
1413               ntohs (message->type));
1414   return ret;
1415 }
1416
1417
1418 /**
1419  * Function called by the service's run
1420  * method to run service-specific setup code.
1421  *
1422  * @param cls closure
1423  * @param server the initialized server
1424  * @param cfg configuration to use
1425  */
1426 static void
1427 run (void *cls, struct GNUNET_SERVER_Handle *server,
1428      const struct GNUNET_CONFIGURATION_Handle *cfg)
1429 {
1430   static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1431     {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1432         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1433     {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1434     {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1435     {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1436         sizeof (struct GNUNET_SET_CreateMessage)},
1437     {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1438         sizeof (struct GNUNET_MessageHeader)},
1439     {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1440     {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1441         sizeof (struct GNUNET_SET_ListenMessage)},
1442     {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1443         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1444     {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1445     {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1446         sizeof (struct GNUNET_SET_CancelMessage)},
1447     {NULL, NULL, 0, 0}
1448   };
1449   static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1450     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1451     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1452     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1453     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1454     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1455     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1456     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1457     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1458     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART, 0},
1459     {NULL, 0, 0}
1460   };
1461   static const uint32_t cadet_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1462
1463   configuration = cfg;
1464   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1465                                 &shutdown_task, NULL);
1466   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1467   GNUNET_SERVER_add_handlers (server, server_handlers);
1468
1469   cadet = GNUNET_CADET_connect (cfg, NULL, channel_new_cb, channel_end_cb,
1470                               cadet_handlers, cadet_ports);
1471   if (NULL == cadet)
1472   {
1473     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1474                 _("Could not connect to cadet service\n"));
1475     return;
1476   }
1477 }
1478
1479
1480 /**
1481  * The main function for the set service.
1482  *
1483  * @param argc number of arguments from the command line
1484  * @param argv command line arguments
1485  * @return 0 ok, 1 on error
1486  */
1487 int
1488 main (int argc, char *const *argv)
1489 {
1490   int ret;
1491
1492   ret = GNUNET_SERVICE_run (argc, argv, "set",
1493                             GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1494   return (GNUNET_OK == ret) ? 0 : 1;
1495 }
1496
1497 /* end of gnunet-service-set.c */
1498