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