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