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