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