31676ba3a94326c161d8f7464c1801f071d041a0
[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   GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
441   if (NULL != set->elements)
442   {
443     // free all elements in the hashtable, before destroying the table
444     GNUNET_CONTAINER_multihashmap_iterate (set->elements,
445                                            destroy_elements_iterator, NULL);
446     GNUNET_CONTAINER_multihashmap_destroy (set->elements);
447     set->elements = NULL;
448   }
449   GNUNET_free (set);
450 }
451
452
453 /**
454  * Clean up after a client has disconnected
455  *
456  * @param cls closure, unused
457  * @param client the client to clean up after
458  */
459 static void
460 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
461 {
462   struct Set *set;
463   struct Listener *listener;
464
465   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466               "client disconnected, cleaning up\n");
467   set = set_get (client);
468   if (NULL != set)
469   {
470     set->client = NULL;
471     set_destroy (set);
472     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473                 "(client's set destroyed)\n");
474   }
475   listener = listener_get (client);
476   if (NULL != listener)
477   {
478     listener->client = NULL;
479     listener_destroy (listener);
480     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481                 "(client's listener destroyed)\n");
482   }
483 }
484
485
486 /**
487  * Destroy an incoming request from a remote peer
488  *
489  * @param incoming remote request to destroy
490  */
491 static void
492 incoming_destroy (struct Operation *incoming)
493 {
494   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
495   GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
496   if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
497   {
498     GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
499     incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
500   }
501   GNUNET_assert (NULL != incoming->state);
502   GNUNET_free (incoming->state);
503   // make sure that the tunnel end handler will not
504   // destroy us again
505   incoming->vt = NULL;
506   incoming->state = NULL;
507   if (NULL != incoming->mq)
508   {
509     GNUNET_MQ_destroy (incoming->mq);
510     incoming->mq = NULL;
511   }
512   if (NULL != incoming->channel)
513   {
514     GNUNET_CADET_channel_destroy (incoming->channel);
515     incoming->channel = NULL;
516   }
517 }
518
519
520 /**
521  * remove & free state of the operation from the incoming list
522  *
523  * @param incoming the element to remove
524  */
525 static void
526 incoming_retire (struct Operation *incoming)
527 {
528   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
529   incoming->is_incoming = GNUNET_NO;
530   GNUNET_assert (NULL != incoming->state);
531   GNUNET_free (incoming->state);
532   incoming->state = NULL;
533   GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
534 }
535
536
537 /**
538  * Find a listener that is interested in the given operation type
539  * and application id.
540  *
541  * @param op operation type to look for
542  * @param app_id application id to look for
543  * @return a matching listener, or NULL if no listener matches the
544  *         given operation and application id
545  */
546 static struct Listener *
547 listener_get_by_target (enum GNUNET_SET_OperationType op,
548                         const struct GNUNET_HashCode *app_id)
549 {
550   struct Listener *l;
551
552   for (l = listeners_head; NULL != l; l = l->next)
553   {
554     if (l->operation != op)
555       continue;
556     if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
557       continue;
558     return l;
559   }
560   return NULL;
561 }
562
563
564 /**
565  * Suggest the given request to the listener. The listening client can then
566  * accept or reject the remote request.
567  *
568  * @param incoming the incoming peer with the request to suggest
569  * @param listener the listener to suggest the request to
570  */
571 static void
572 incoming_suggest (struct Operation *incoming, struct Listener *listener)
573 {
574   struct GNUNET_MQ_Envelope *mqm;
575   struct GNUNET_SET_RequestMessage *cmsg;
576
577   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
578   GNUNET_assert (NULL != incoming->state);
579   GNUNET_assert (NULL != incoming->spec);
580   GNUNET_assert (0 == incoming->state->suggest_id);
581   incoming->state->suggest_id = suggest_id++;
582
583   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
584   GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
585   incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
586
587   mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
588                                  incoming->spec->context_msg);
589   GNUNET_assert (NULL != mqm);
590   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
591               incoming->state->suggest_id);
592   cmsg->accept_id = htonl (incoming->state->suggest_id);
593   cmsg->peer_id = incoming->spec->peer;
594   GNUNET_MQ_send (listener->client_mq, mqm);
595 }
596
597
598 /**
599  * Handle a request for a set operation from
600  * another peer.
601  *
602  * This msg is expected as the first and only msg handled through the
603  * non-operation bound virtual table, acceptance of this operation replaces
604  * our virtual table and subsequent msgs would be routed differently.
605  *
606  * @param op the operation state
607  * @param mh the received message
608  * @return #GNUNET_OK if the channel should be kept alive,
609  *         #GNUNET_SYSERR to destroy the channel
610  */
611 static int
612 handle_incoming_msg (struct Operation *op,
613                      const struct GNUNET_MessageHeader *mh)
614 {
615   const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
616   struct Listener *listener;
617   struct OperationSpecification *spec;
618
619   GNUNET_assert (GNUNET_YES == op->is_incoming);
620
621   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got op request\n");
622
623   if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
624   {
625     GNUNET_break_op (0);
626     return GNUNET_SYSERR;
627   }
628
629   /* double operation request */
630   if (NULL != op->spec)
631   {
632     GNUNET_break_op (0);
633     return GNUNET_SYSERR;
634   }
635
636   spec = GNUNET_new (struct OperationSpecification);
637   spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
638   // for simplicity we just backup the context msg instead of rebuilding it later on
639   if (NULL != spec->context_msg)
640     spec->context_msg = GNUNET_copy_message (spec->context_msg);
641   spec->operation = ntohl (msg->operation);
642   spec->app_id = msg->app_id;
643   spec->salt = ntohl (msg->salt);
644   spec->peer = op->state->peer;
645   spec->remote_element_count = ntohl (msg->element_count);
646
647   op->spec = spec;
648
649   if ( (NULL != spec->context_msg) &&
650        (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
651   {
652     GNUNET_break_op (0);
653     return GNUNET_SYSERR;
654   }
655
656   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
657               ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
658   listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
659   if (NULL == listener)
660   {
661     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662                 "no listener matches incoming request, waiting with timeout\n");
663     return GNUNET_OK;
664   }
665   incoming_suggest (op, listener);
666   return GNUNET_OK;
667 }
668
669
670 /**
671  * Send the next element of a set to the set's client.  The next element is given by
672  * the set's current hashmap iterator.  The set's iterator will be set to NULL if there
673  * are no more elements in the set.  The caller must ensure that the set's iterator is
674  * valid.
675  *
676  * @param set set that should send its next element to its client
677  */
678 static void
679 send_client_element (struct Set *set)
680 {
681   int ret;
682   struct ElementEntry *ee;
683   struct GNUNET_MQ_Envelope *ev;
684
685   GNUNET_assert (NULL != set->iter);
686   ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
687   if (GNUNET_NO == ret)
688   {
689     ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
690     GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
691     set->iter = NULL;
692   }
693   else
694   {
695     struct GNUNET_SET_IterResponseMessage *msg;
696
697     GNUNET_assert (NULL != ee);
698     ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
699     memcpy (&msg[1], ee->element.data, ee->element.size);
700     msg->element_type = ee->element.type;
701   }
702   GNUNET_MQ_send (set->client_mq, ev);
703 }
704
705
706 /**
707  * Called when a client wants to iterate the elements of a set.
708  *
709  * @param cls unused
710  * @param client client that sent the message
711  * @param m message sent by the client
712  */
713 static void
714 handle_client_iterate (void *cls,
715                        struct GNUNET_SERVER_Client *client,
716                        const struct GNUNET_MessageHeader *m)
717 {
718   struct Set *set;
719
720   // iterate over a non existing set
721   set = set_get (client);
722   if (NULL == set)
723   {
724     GNUNET_break (0);
725     GNUNET_SERVER_client_disconnect (client);
726     return;
727   }
728
729   // only one concurrent iterate-action per set
730   if (NULL != set->iter)
731   {
732     GNUNET_break (0);
733     GNUNET_SERVER_client_disconnect (client);
734     return;
735   }
736   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
737               GNUNET_CONTAINER_multihashmap_size (set->elements));
738   GNUNET_SERVER_receive_done (client, GNUNET_OK);
739   set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
740   send_client_element (set);
741 }
742
743
744 /**
745  * Called when a client wants to create a new set.
746  *
747  * @param cls unused
748  * @param client client that sent the message
749  * @param m message sent by the client
750  */
751 static void
752 handle_client_create_set (void *cls,
753                           struct GNUNET_SERVER_Client *client,
754                           const struct GNUNET_MessageHeader *m)
755 {
756   const struct GNUNET_SET_CreateMessage *msg;
757   struct Set *set;
758
759   msg = (const struct GNUNET_SET_CreateMessage *) m;
760   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761               "client created new set (operation %u)\n",
762               ntohs (msg->operation));
763
764   // max. one set per client!
765   if (NULL != set_get (client))
766   {
767     GNUNET_break (0);
768     GNUNET_SERVER_client_disconnect (client);
769     return;
770   }
771
772   set = GNUNET_new (struct Set);
773
774   switch (ntohs (msg->operation))
775   {
776   case GNUNET_SET_OPERATION_INTERSECTION:
777     set->vt = _GSS_intersection_vt ();
778     break;
779   case GNUNET_SET_OPERATION_UNION:
780     set->vt = _GSS_union_vt ();
781     break;
782   default:
783     GNUNET_free (set);
784     GNUNET_break (0);
785     GNUNET_SERVER_client_disconnect (client);
786     return;
787   }
788
789   set->state = set->vt->create ();
790   set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
791   set->client = client;
792   set->client_mq = GNUNET_MQ_queue_for_server_client (client);
793   GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
794   GNUNET_SERVER_receive_done (client, GNUNET_OK);
795 }
796
797
798 /**
799  * Called when a client wants to create a new listener.
800  *
801  * @param cls unused
802  * @param client client that sent the message
803  * @param m message sent by the client
804  */
805 static void
806 handle_client_listen (void *cls,
807                       struct GNUNET_SERVER_Client *client,
808                       const struct GNUNET_MessageHeader *m)
809 {
810   const struct GNUNET_SET_ListenMessage *msg;
811   struct Listener *listener;
812   struct Operation *op;
813
814   msg = (const struct GNUNET_SET_ListenMessage *) m;
815   /* max. one per client! */
816   if (NULL != listener_get (client))
817   {
818     GNUNET_break (0);
819     GNUNET_SERVER_client_disconnect (client);
820     return;
821   }
822
823   listener = GNUNET_new (struct Listener);
824   listener->client = client;
825   listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
826   listener->app_id = msg->app_id;
827   listener->operation = ntohl (msg->operation);
828   GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
829   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830               "new listener created (op %u, app %s)\n",
831               listener->operation,
832               GNUNET_h2s (&listener->app_id));
833
834   /* check for incoming requests the listener is interested in */
835   for (op = incoming_head; NULL != op; op = op->next)
836   {
837     if (NULL == op->spec)
838     {
839       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840                   "request has no spec yet\n");
841       continue;
842     }
843     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844                 "considering (op: %u, app: %s, suggest: %u)\n",
845                 op->spec->operation,
846                 GNUNET_h2s (&op->spec->app_id),
847                 op->state->suggest_id);
848
849     /* don't consider the incoming request if it has been already suggested to a listener */
850     if (0 != op->state->suggest_id)
851       continue;
852     if (listener->operation != op->spec->operation)
853       continue;
854     if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
855       continue;
856     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857                 "request suggested\n");
858     incoming_suggest (op, listener);
859   }
860   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861               "considered all incoming requests\n");
862   GNUNET_SERVER_receive_done (client, GNUNET_OK);
863 }
864
865
866 /**
867  * Called when the listening client rejects an operation
868  * request by another peer.
869  *
870  * @param cls unused
871  * @param client client that sent the message
872  * @param m message sent by the client
873  */
874 static void
875 handle_client_reject (void *cls,
876                       struct GNUNET_SERVER_Client *client,
877                       const struct GNUNET_MessageHeader *m)
878 {
879   struct Operation *incoming;
880   const struct GNUNET_SET_AcceptRejectMessage *msg;
881
882   msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
883   GNUNET_break (0 == ntohl (msg->request_id));
884
885   // no matching incoming operation for this reject
886   incoming = get_incoming (ntohl (msg->accept_reject_id));
887   if (NULL == incoming)
888   {
889     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
890     return;
891   }
892   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893               "peer request rejected by client\n");
894
895   GNUNET_CADET_channel_destroy (incoming->channel);
896   //channel destruction handler called immediately upon destruction
897   GNUNET_SERVER_receive_done (client, GNUNET_OK);
898 }
899
900
901 /**
902  * Called when a client wants to add/remove an element to/from a
903  * set it inhabits.
904  *
905  * @param cls unused
906  * @param client client that sent the message
907  * @param m message sent by the client
908  */
909 static void
910 handle_client_add_remove (void *cls,
911                           struct GNUNET_SERVER_Client *client,
912                           const struct GNUNET_MessageHeader *m)
913 {
914   struct Set *set;
915   const struct GNUNET_SET_ElementMessage *msg;
916   struct GNUNET_SET_Element el;
917   struct ElementEntry *ee;
918
919   // client without a set requested an operation
920   set = set_get (client);
921   if (NULL == set)
922   {
923     GNUNET_break (0);
924     GNUNET_SERVER_client_disconnect (client);
925     return;
926   }
927   GNUNET_SERVER_receive_done (client, GNUNET_OK);
928   msg = (const struct GNUNET_SET_ElementMessage *) m;
929   el.size = ntohs (m->size) - sizeof *msg;
930   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931               "client ins/rem element of size %u\n", el.size);
932   el.data = &msg[1];
933   if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
934   {
935     struct GNUNET_HashCode hash;
936
937     GNUNET_CRYPTO_hash (el.data, el.size, &hash);
938     ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
939     if (NULL == ee)
940     {
941       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
942                   "client tried to remove non-existing element\n");
943       return;
944     }
945     if (GNUNET_YES == ee->removed)
946     {
947       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
948                   "client tried to remove element twice\n");
949       return;
950     }
951     ee->removed = GNUNET_YES;
952     ee->generation_removed = set->current_generation;
953     set->vt->remove (set->state, ee);
954   }
955   else
956   {
957     struct ElementEntry *ee_dup;
958
959     ee = GNUNET_malloc (el.size + sizeof *ee);
960     ee->element.size = el.size;
961     memcpy (&ee[1], el.data, el.size);
962     ee->element.data = &ee[1];
963     ee->generation_added = set->current_generation;
964     ee->remote = GNUNET_NO;
965     GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
966     ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
967                                                 &ee->element_hash);
968     if (NULL != ee_dup)
969     {
970       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
971                   "element inserted twice, ignoring\n");
972       GNUNET_free (ee);
973       return;
974     }
975     GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
976                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
977     set->vt->add (set->state, ee);
978   }
979 }
980
981
982 /**
983  * Called when a client wants to evaluate a set operation with another peer.
984  *
985  * @param cls unused
986  * @param client client that sent the message
987  * @param m message sent by the client
988  */
989 static void
990 handle_client_evaluate (void *cls,
991                         struct GNUNET_SERVER_Client *client,
992                         const struct GNUNET_MessageHeader *m)
993 {
994   struct Set *set;
995   const struct GNUNET_SET_EvaluateMessage *msg;
996   struct OperationSpecification *spec;
997   struct Operation *op;
998
999   set = set_get (client);
1000   if (NULL == set)
1001   {
1002     GNUNET_break (0);
1003     GNUNET_SERVER_client_disconnect (client);
1004     return;
1005   }
1006
1007   msg = (const struct GNUNET_SET_EvaluateMessage *) m;
1008   spec = GNUNET_new (struct OperationSpecification);
1009   spec->operation = set->operation;
1010   spec->app_id = msg->app_id;
1011   spec->salt = ntohl (msg->salt);
1012   spec->peer = msg->target_peer;
1013   spec->set = set;
1014   spec->result_mode = ntohs (msg->result_mode);
1015   spec->client_request_id = ntohl (msg->request_id);
1016   spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
1017
1018   // for simplicity we just backup the context msg instead of rebuilding it later on
1019   if (NULL != spec->context_msg)
1020     spec->context_msg = GNUNET_copy_message (spec->context_msg);
1021
1022   op = GNUNET_new (struct Operation);
1023   op->spec = spec;
1024   op->generation_created = set->current_generation++;
1025   op->vt = set->vt;
1026   GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1027
1028   op->channel = GNUNET_CADET_channel_create (cadet, op, &msg->target_peer,
1029                                             GNUNET_APPLICATION_TYPE_SET,
1030                                             GNUNET_CADET_OPTION_RELIABLE);
1031
1032   op->mq = GNUNET_CADET_mq_create (op->channel);
1033
1034   set->vt->evaluate (op);
1035   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1036 }
1037
1038
1039 /**
1040  * Handle an ack from a client, and send the next element.
1041  *
1042  * @param cls unused
1043  * @param client the client
1044  * @param m the message
1045  */
1046 static void
1047 handle_client_iter_ack (void *cls,
1048                    struct GNUNET_SERVER_Client *client,
1049                    const struct GNUNET_MessageHeader *m)
1050 {
1051   struct Set *set;
1052
1053   // client without a set requested an operation
1054   set = set_get (client);
1055   if (NULL == set)
1056   {
1057     GNUNET_break (0);
1058     GNUNET_SERVER_client_disconnect (client);
1059     return;
1060   }
1061
1062   // client sent an ack, but we were not expecting one
1063   if (NULL == set->iter)
1064   {
1065     GNUNET_break (0);
1066     GNUNET_SERVER_client_disconnect (client);
1067     return;
1068   }
1069
1070   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1071   send_client_element (set);
1072 }
1073
1074
1075 /**
1076  * Handle a request from the client to
1077  * cancel a running set operation.
1078  *
1079  * @param cls unused
1080  * @param client the client
1081  * @param mh the message
1082  */
1083 static void
1084 handle_client_cancel (void *cls,
1085                       struct GNUNET_SERVER_Client *client,
1086                       const struct GNUNET_MessageHeader *mh)
1087 {
1088   const struct GNUNET_SET_CancelMessage *msg =
1089       (const struct GNUNET_SET_CancelMessage *) mh;
1090   struct Set *set;
1091   struct Operation *op;
1092   int found;
1093
1094   // client without a set requested an operation
1095   set = set_get (client);
1096   if (NULL == set)
1097   {
1098     GNUNET_break (0);
1099     GNUNET_SERVER_client_disconnect (client);
1100     return;
1101   }
1102
1103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104               "client requested cancel for op %u\n",
1105               ntohl (msg->request_id));
1106
1107   found = GNUNET_NO;
1108   for (op = set->ops_head; NULL != op; op = op->next)
1109   {
1110     if (op->spec->client_request_id == ntohl (msg->request_id))
1111     {
1112       found = GNUNET_YES;
1113       break;
1114     }
1115   }
1116
1117   /* It may happen that the operation was destroyed due to
1118    * the other peer disconnecting.  The client may not know about this
1119    * yet and try to cancel the (non non-existent) operation.
1120    */
1121   if (GNUNET_NO != found)
1122     _GSS_operation_destroy (op,
1123                             GNUNET_YES);
1124   else
1125     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126                 "client canceled non-existent op\n");
1127
1128
1129   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1130 }
1131
1132
1133 /**
1134  * Handle a request from the client to accept
1135  * a set operation that came from a remote peer.
1136  * We forward the accept to the associated operation for handling
1137  *
1138  * @param cls unused
1139  * @param client the client
1140  * @param mh the message
1141  */
1142 static void
1143 handle_client_accept (void *cls,
1144                       struct GNUNET_SERVER_Client *client,
1145                       const struct GNUNET_MessageHeader *mh)
1146 {
1147   struct Set *set;
1148   const struct GNUNET_SET_AcceptRejectMessage *msg;
1149   struct Operation *op;
1150
1151   msg = (const struct GNUNET_SET_AcceptRejectMessage *) mh;
1152
1153   // client without a set requested an operation
1154   set = set_get (client);
1155
1156   if (NULL == set)
1157   {
1158     GNUNET_break (0);
1159     GNUNET_SERVER_client_disconnect (client);
1160     return;
1161   }
1162
1163   op = get_incoming (ntohl (msg->accept_reject_id));
1164
1165   /* it is not an error if the set op does not exist -- it may
1166    * have been destroyed when the partner peer disconnected. */
1167   if (NULL == op)
1168   {
1169     struct GNUNET_SET_ResultMessage *result_message;
1170     struct GNUNET_MQ_Envelope *ev;
1171     ev = GNUNET_MQ_msg (result_message, GNUNET_MESSAGE_TYPE_SET_RESULT);
1172     result_message->request_id = msg->request_id;
1173     result_message->element_type = 0;
1174     result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1175     GNUNET_MQ_send (set->client_mq, ev);
1176     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1177     return;
1178   }
1179
1180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181               "client accepting %u\n",
1182               ntohl (msg->accept_reject_id));
1183
1184   GNUNET_assert (GNUNET_YES == op->is_incoming);
1185
1186
1187   op->spec->set = set;
1188
1189   incoming_retire (op);
1190
1191   GNUNET_assert (NULL != op->spec->set);
1192   GNUNET_assert (NULL != op->spec->set->vt);
1193
1194   GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1195
1196   op->spec->client_request_id = ntohl (msg->request_id);
1197   op->spec->result_mode = ntohs (msg->result_mode);
1198   op->generation_created = set->current_generation++;
1199   op->vt = op->spec->set->vt;
1200   GNUNET_assert (NULL != op->vt->accept);
1201   set->vt->accept (op);
1202   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1203 }
1204
1205
1206 /**
1207  * Called to clean up, after a shutdown has been requested.
1208  *
1209  * @param cls closure
1210  * @param tc context information (why was this task triggered now)
1211  */
1212 static void
1213 shutdown_task (void *cls,
1214                const struct GNUNET_SCHEDULER_TaskContext *tc)
1215 {
1216   while (NULL != incoming_head)
1217     incoming_destroy (incoming_head);
1218
1219   while (NULL != listeners_head)
1220     listener_destroy (listeners_head);
1221
1222   while (NULL != sets_head)
1223     set_destroy (sets_head);
1224
1225   /* it's important to destroy cadet at the end, as all channels
1226    * must be destroyed before the cadet handle! */
1227   if (NULL != cadet)
1228   {
1229     GNUNET_CADET_disconnect (cadet);
1230     cadet = NULL;
1231   }
1232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233               "handled shutdown request\n");
1234 }
1235
1236
1237 /**
1238  * Timeout happens iff:
1239  *  - we suggested an operation to our listener,
1240  *    but did not receive a response in time
1241  *  - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1242  *  - shutdown (obviously)
1243  *
1244  * @param cls channel context
1245  * @param tc context information (why was this task triggered now)
1246  */
1247 static void
1248 incoming_timeout_cb (void *cls,
1249                      const struct GNUNET_SCHEDULER_TaskContext *tc)
1250 {
1251   struct Operation *incoming = cls;
1252
1253   incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1254   GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1255   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1256     return;
1257   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258               "remote peer timed out\n");
1259   incoming_destroy (incoming);
1260 }
1261
1262
1263 /**
1264  * Terminates an incoming operation in case we have not yet received an
1265  * operation request. Called by the channel destruction handler.
1266  *
1267  * @param op the channel context
1268  */
1269 static void
1270 handle_incoming_disconnect (struct Operation *op)
1271 {
1272   GNUNET_assert (GNUNET_YES == op->is_incoming);
1273   /* channel is already dead, incoming_destroy must not
1274    * destroy it ... */
1275   op->channel = NULL;
1276   incoming_destroy (op);
1277   op->vt = NULL;
1278 }
1279
1280
1281 /**
1282  * Method called whenever another peer has added us to a channel
1283  * the other peer initiated.
1284  * Only called (once) upon reception of data with a message type which was
1285  * subscribed to in GNUNET_CADET_connect().
1286  *
1287  * The channel context represents the operation itself and gets added to a DLL,
1288  * from where it gets looked up when our local listener client responds
1289  * to a proposed/suggested operation or connects and associates with this operation.
1290  *
1291  * @param cls closure
1292  * @param channel new handle to the channel
1293  * @param initiator peer that started the channel
1294  * @param port Port this channel is for.
1295  * @param options Unused.
1296  * @return initial channel context for the channel
1297  *         (can be NULL -- that's not an error)
1298  */
1299 static void *
1300 channel_new_cb (void *cls,
1301                struct GNUNET_CADET_Channel *channel,
1302                const struct GNUNET_PeerIdentity *initiator,
1303                uint32_t port, enum GNUNET_CADET_ChannelOption options)
1304 {
1305   struct Operation *incoming;
1306   static const struct SetVT incoming_vt = {
1307     .msg_handler = handle_incoming_msg,
1308     .peer_disconnect = handle_incoming_disconnect
1309   };
1310
1311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312               "new incoming channel\n");
1313
1314   if (GNUNET_APPLICATION_TYPE_SET != port)
1315   {
1316     GNUNET_break (0);
1317     GNUNET_CADET_channel_destroy (channel);
1318     return NULL;
1319   }
1320
1321   incoming = GNUNET_new (struct Operation);
1322   incoming->is_incoming = GNUNET_YES;
1323   incoming->state = GNUNET_new (struct OperationState);
1324   incoming->state->peer = *initiator;
1325   incoming->channel = channel;
1326   incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
1327   incoming->vt = &incoming_vt;
1328   incoming->state->timeout_task =
1329       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1330                                     &incoming_timeout_cb, incoming);
1331   GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
1332
1333   return incoming;
1334 }
1335
1336
1337 /**
1338  * Function called whenever a channel is destroyed.  Should clean up
1339  * any associated state.  It must NOT call
1340  * GNUNET_CADET_channel_destroy() on the channel.
1341  *
1342  * The peer_disconnect function is part of a a virtual table set initially either
1343  * when a peer creates a new channel with us (channel_new_cb), or once we create
1344  * a new channel ourselves (evaluate).
1345  *
1346  * Once we know the exact type of operation (union/intersection), the vt is
1347  * replaced with an operation specific instance (_GSS_[op]_vt).
1348  *
1349  * @param cls closure (set from GNUNET_CADET_connect())
1350  * @param channel connection to the other end (henceforth invalid)
1351  * @param channel_ctx place where local state associated
1352  *                   with the channel is stored
1353  */
1354 static void
1355 channel_end_cb (void *cls,
1356                 const struct GNUNET_CADET_Channel *channel, void *channel_ctx)
1357 {
1358   struct Operation *op = channel_ctx;
1359
1360   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361               "channel end cb called\n");
1362   op->channel = NULL;
1363   /* the vt can be null if a client already requested canceling op. */
1364   if (NULL != op->vt)
1365   {
1366     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367                 "calling peer disconnect due to channel end\n");
1368     op->vt->peer_disconnect (op);
1369   }
1370
1371   if (GNUNET_YES == op->keep)
1372     return;
1373
1374   /* cadet will never call us with the context again! */
1375   GNUNET_free (channel_ctx);
1376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377               "channel end cb finished\n");
1378 }
1379
1380
1381 /**
1382  * Functions with this signature are called whenever a message is
1383  * received via a cadet channel.
1384  *
1385  * The msg_handler is a virtual table set in initially either when a peer
1386  * creates a new channel with us (channel_new_cb), or once we create a new channel
1387  * ourselves (evaluate).
1388  *
1389  * Once we know the exact type of operation (union/intersection), the vt is
1390  * replaced with an operation specific instance (_GSS_[op]_vt).
1391  *
1392  * @param cls Closure (set from GNUNET_CADET_connect()).
1393  * @param channel Connection to the other end.
1394  * @param channel_ctx Place to store local state associated with the channel.
1395  * @param message The actual message.
1396  * @return #GNUNET_OK to keep the channel open,
1397  *         #GNUNET_SYSERR to close it (signal serious error).
1398  */
1399 static int
1400 dispatch_p2p_message (void *cls,
1401                       struct GNUNET_CADET_Channel *channel,
1402                       void **channel_ctx,
1403                       const struct GNUNET_MessageHeader *message)
1404 {
1405   struct Operation *op = *channel_ctx;
1406   int ret;
1407
1408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1409               "dispatching cadet message (type: %u)\n",
1410               ntohs (message->type));
1411   /* do this before the handler, as the handler might kill the channel */
1412   GNUNET_CADET_receive_done (channel);
1413   if (NULL != op->vt)
1414     ret = op->vt->msg_handler (op, message);
1415   else
1416     ret = GNUNET_SYSERR;
1417   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418               "handled cadet message (type: %u)\n",
1419               ntohs (message->type));
1420   return ret;
1421 }
1422
1423
1424 /**
1425  * Function called by the service's run
1426  * method to run service-specific setup code.
1427  *
1428  * @param cls closure
1429  * @param server the initialized server
1430  * @param cfg configuration to use
1431  */
1432 static void
1433 run (void *cls, struct GNUNET_SERVER_Handle *server,
1434      const struct GNUNET_CONFIGURATION_Handle *cfg)
1435 {
1436   static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1437     {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1438         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1439     {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1440     {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1441     {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1442         sizeof (struct GNUNET_SET_CreateMessage)},
1443     {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1444         sizeof (struct GNUNET_MessageHeader)},
1445     {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1446     {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1447         sizeof (struct GNUNET_SET_ListenMessage)},
1448     {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1449         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1450     {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1451     {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1452         sizeof (struct GNUNET_SET_CancelMessage)},
1453     {NULL, NULL, 0, 0}
1454   };
1455   static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1456     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1457     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1458     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1459     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1460     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1461     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1462     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1463     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1464     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART, 0},
1465     {NULL, 0, 0}
1466   };
1467   static const uint32_t cadet_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1468
1469   configuration = cfg;
1470   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1471                                 &shutdown_task, NULL);
1472   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1473   GNUNET_SERVER_add_handlers (server, server_handlers);
1474
1475   cadet = GNUNET_CADET_connect (cfg, NULL, channel_new_cb, channel_end_cb,
1476                               cadet_handlers, cadet_ports);
1477   if (NULL == cadet)
1478   {
1479     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1480                 _("Could not connect to cadet service\n"));
1481     return;
1482   }
1483 }
1484
1485
1486 /**
1487  * The main function for the set service.
1488  *
1489  * @param argc number of arguments from the command line
1490  * @param argv command line arguments
1491  * @return 0 ok, 1 on error
1492  */
1493 int
1494 main (int argc, char *const *argv)
1495 {
1496   int ret;
1497
1498   ret = GNUNET_SERVICE_run (argc, argv, "set",
1499                             GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1500   return (GNUNET_OK == ret) ? 0 : 1;
1501 }
1502
1503 /* end of gnunet-service-set.c */
1504