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