- fixed tunnel context
[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 2, 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  * Peer that has connected to us, but is not yet evaluating a set operation.
32  * Once the peer has sent a request, and the client has
33  * accepted or rejected it, this information will be deleted.
34  */
35 struct Incoming
36 {
37   /**
38    * Incoming peers are held in a linked list
39    */
40   struct Incoming *next;
41
42   /**
43    * Incoming peers are held in a linked list
44    */
45   struct Incoming *prev;
46
47   /**
48    * Detail information about the operation.
49    */
50   struct OperationSpecification *spec;
51
52   /**
53    * The identity of the requesting peer.
54    */
55   struct GNUNET_PeerIdentity peer;
56
57   /**
58    * Tunnel to the peer.
59    */
60   struct GNUNET_MESH_Tunnel *tunnel;
61
62   /**
63    * Unique request id for the request from
64    * a remote peer, sent to the client, which will
65    * accept or reject the request.
66    * Set to '0' iff the request has not been
67    * suggested yet.
68    */
69   uint32_t suggest_id;
70
71   /**
72    * Timeout task, if the incoming peer has not been accepted
73    * after the timeout, it will be disconnected.
74    */
75   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
76 };
77
78
79 /**
80  * Configuration of our local peer.
81  * (Not declared 'static' as also needed in gnunet-service-set_union.c)
82  */
83 const struct GNUNET_CONFIGURATION_Handle *configuration;
84
85 /**
86  * Handle to the mesh service, used
87  * to listen for and connect to remote peers.
88  * (Not declared 'static' as also needed in gnunet-service-set_union.c)
89  */
90 struct GNUNET_MESH_Handle *mesh;
91
92 /**
93  * Sets are held in a doubly linked list.
94  */
95 static struct Set *sets_head;
96
97 /**
98  * Sets are held in a doubly linked list.
99  */
100 static struct Set *sets_tail;
101
102 /**
103  * Listeners are held in a doubly linked list.
104  */
105 static struct Listener *listeners_head;
106
107 /**
108  * Listeners are held in a doubly linked list.
109  */
110 static struct Listener *listeners_tail;
111
112 /**
113  * Incoming sockets from remote peers are
114  * held in a doubly linked list.
115  */
116 static struct Incoming *incoming_head;
117
118 /**
119  * Incoming sockets from remote peers are
120  * held in a doubly linked list.
121  */
122 static struct Incoming *incoming_tail;
123
124 /**
125  * Counter for allocating unique IDs for clients,
126  * used to identify incoming operation requests from remote peers,
127  * that the client can choose to accept or refuse.
128  */
129 static uint32_t suggest_id = 1;
130
131
132 /**
133  * Get set that is owned by the given client, if any.
134  *
135  * @param client client to look for
136  * @return set that the client owns, NULL if the client
137  *         does not own a set
138  */
139 static struct Set *
140 set_get (struct GNUNET_SERVER_Client *client)
141 {
142   struct Set *set;
143
144   for (set = sets_head; NULL != set; set = set->next)
145     if (set->client == client)
146       return set;
147   return NULL;
148 }
149
150
151 /**
152  * Get the listener associated with the given client, if any.
153  *
154  * @param client the client
155  * @return listener associated with the client, NULL
156  *         if there isn't any
157  */
158 static struct Listener *
159 listener_get (struct GNUNET_SERVER_Client *client)
160 {
161   struct Listener *listener;
162
163   for (listener = listeners_head; NULL != listener; listener = listener->next)
164     if (listener->client == client)
165       return listener;
166   return NULL;
167 }
168
169
170 /**
171  * Get the incoming socket associated with the given id.
172  *
173  * @param id id to look for
174  * @return the incoming socket associated with the id,
175  *         or NULL if there is none
176  */
177 static struct Incoming *
178 get_incoming (uint32_t id)
179 {
180   struct Incoming *incoming;
181
182   for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
183     if (incoming->suggest_id == id)
184       return incoming;
185   return NULL;
186 }
187
188
189 /**
190  * Destroy a listener, free all resources associated with it.
191  *
192  * @param listener listener to destroy
193  */
194 static void
195 listener_destroy (struct Listener *listener)
196 {
197   /* If the client is not dead yet, destroy it.
198    * The client's destroy callback will destroy the listener again. */
199   if (NULL != listener->client)
200   {
201     GNUNET_SERVER_client_disconnect (listener->client);
202     listener->client = NULL;
203     return;
204   }
205   if (NULL != listener->client_mq)
206   {
207     GNUNET_MQ_destroy (listener->client_mq);
208     listener->client_mq = NULL;
209   }
210   GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
211   GNUNET_free (listener);
212 }
213
214
215 /**
216  * Destroy a set, and free all resources associated with it.
217  *
218  * @param set the set to destroy
219  */
220 static void
221 set_destroy (struct Set *set)
222 {
223   /* If the client is not dead yet, destroy it.
224    * The client's destroy callback will destroy the set again. */
225   if (NULL != set->client)
226   {
227     GNUNET_SERVER_client_disconnect (set->client);
228     set->client = NULL;
229     return;
230   }
231   switch (set->operation)
232   {
233     case GNUNET_SET_OPERATION_INTERSECTION:
234       GNUNET_assert (0);
235       break;
236     case GNUNET_SET_OPERATION_UNION:
237       _GSS_union_set_destroy (set);
238       break;
239     default:
240       GNUNET_assert (0);
241       break;
242   }
243   GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
244   GNUNET_free (set);
245 }
246
247
248 /**
249  * Clean up after a client after it is
250  * disconnected (either by us or by itself)
251  *
252  * @param cls closure, unused
253  * @param client the client to clean up after
254  */
255 static void
256 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
257 {
258   struct Set *set;
259   struct Listener *listener;
260
261   set = set_get (client);
262   if (NULL != set)
263   {
264     set->client = NULL;
265     set_destroy (set);
266   }
267   listener = listener_get (client);
268   if (NULL != listener)
269   {
270     listener->client = NULL;
271     listener_destroy (listener);
272   }
273 }
274
275
276 /**
277  * Destroy an incoming request from a remote peer
278  *
279  * @param incoming remote request to destroy
280  */
281 static void
282 incoming_destroy (struct Incoming *incoming)
283 {
284   if (NULL != incoming->tunnel)
285   {
286     struct GNUNET_MESH_Tunnel *t = incoming->tunnel;
287     incoming->tunnel = NULL;
288     GNUNET_MESH_tunnel_destroy (t);
289     return;
290   }
291   GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
292   GNUNET_free (incoming);
293 }
294
295
296 static struct Listener *
297 listener_get_by_target (enum GNUNET_SET_OperationType op,
298                         const struct GNUNET_HashCode *app_id)
299 {
300   struct Listener *l;
301
302   for (l = listeners_head; NULL != l; l = l->next)
303   {
304     if (l->operation != op)
305       continue;
306     if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
307       continue;
308     return l;
309   }
310   return NULL;
311 }
312
313
314 /**
315  * Suggest the given request to the listener,
316  * who can accept or reject the request.
317  *
318  * @param incoming the incoming peer with the request to suggest
319  * @param listener the listener to suggest the request to
320  */
321 static void
322 incoming_suggest (struct Incoming *incoming, struct Listener *listener)
323 {
324   struct GNUNET_MQ_Envelope *mqm;
325   struct GNUNET_SET_RequestMessage *cmsg;
326
327   GNUNET_assert (0 == incoming->suggest_id);
328   GNUNET_assert (NULL != incoming->spec);
329   incoming->suggest_id = suggest_id++;
330
331   GNUNET_SCHEDULER_cancel (incoming->timeout_task);
332   mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
333                                  incoming->spec->context_msg);
334   GNUNET_assert (NULL != mqm);
335   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "suggesting request with accept id %u\n", incoming->suggest_id);
336   cmsg->accept_id = htonl (incoming->suggest_id);
337   cmsg->peer_id = incoming->spec->peer;
338   GNUNET_MQ_send (listener->client_mq, mqm);
339
340 }
341
342
343 /**
344  * Handle a request for a set operation from
345  * another peer.
346  *
347  * @param cls the incoming socket
348  * @param tunnel the tunnel that sent the message
349  * @param tunnel_ctx the tunnel context
350  * @param mh the message
351  */
352 static int
353 handle_p2p_operation_request (void *cls,
354                               struct GNUNET_MESH_Tunnel *tunnel,
355                               void **tunnel_ctx,
356                               const struct GNUNET_MessageHeader *mh)
357 {
358   struct TunnelContext *tc = *tunnel_ctx;
359   struct Incoming *incoming;
360   const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
361   struct Listener *listener;
362   struct OperationSpecification *spec;
363
364   if (CONTEXT_INCOMING != tc->type)
365   {
366     /* unexpected request */
367     GNUNET_break_op (0);
368     /* kill the tunnel, cleaner will be called */
369     return GNUNET_SYSERR;
370   }
371
372   incoming = tc->data.incoming;
373
374   if (NULL != incoming->spec)
375   {
376     /* double operation request */
377     GNUNET_break_op (0);
378     return GNUNET_SYSERR;
379   }
380
381   spec = GNUNET_new (struct OperationSpecification);
382   spec->context_msg = 
383       GNUNET_copy_message (GNUNET_MQ_extract_nested_mh (msg));
384   spec->operation = ntohl (msg->operation);
385   spec->app_id = msg->app_id;
386   spec->salt = ntohl (msg->salt);
387   spec->peer = incoming->peer;
388
389   incoming->spec = spec;
390
391   if ( (NULL != spec->context_msg) &&
392        (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
393   {
394     GNUNET_break_op (0);
395     return GNUNET_SYSERR;
396   }
397
398   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received P2P operation request (op %u, app %s)\n",
399               ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
400   listener = listener_get_by_target (ntohs (msg->operation), &msg->app_id);
401   if (NULL == listener)
402   {
403     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
404                 "no listener matches incoming request, waiting with timeout\n");
405     return GNUNET_OK;
406   }
407   incoming_suggest (incoming, listener);
408   return GNUNET_OK;
409 }
410
411
412 /**
413  * Called when a client wants to create a new set.
414  *
415  * @param cls unused
416  * @param client client that sent the message
417  * @param m message sent by the client
418  */
419 static void
420 handle_client_create (void *cls,
421                       struct GNUNET_SERVER_Client *client,
422                       const struct GNUNET_MessageHeader *m)
423 {
424   struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
425   struct Set *set;
426
427   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client created new set (operation %u)\n",
428               ntohs (msg->operation));
429
430   if (NULL != set_get (client))
431   {
432     GNUNET_break (0);
433     GNUNET_SERVER_client_disconnect (client);
434     return;
435   }
436
437   set = NULL;
438
439   switch (ntohs (msg->operation))
440   {
441     case GNUNET_SET_OPERATION_INTERSECTION:
442       //set = _GSS_intersection_set_create ();
443       break;
444     case GNUNET_SET_OPERATION_UNION:
445       set = _GSS_union_set_create ();
446       break;
447     default:
448       GNUNET_break (0);
449       GNUNET_SERVER_client_disconnect (client);
450       return;
451   }
452
453   GNUNET_assert (NULL != set);
454
455   set->client = client;
456   set->client_mq = GNUNET_MQ_queue_for_server_client (client);
457   GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
458   GNUNET_SERVER_receive_done (client, GNUNET_OK);
459 }
460
461
462 /**
463  * Called when a client wants to create a new listener.
464  *
465  * @param cls unused
466  * @param client client that sent the message
467  * @param m message sent by the client
468  */
469 static void
470 handle_client_listen (void *cls,
471                       struct GNUNET_SERVER_Client *client,
472                       const struct GNUNET_MessageHeader *m)
473 {
474   struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
475   struct Listener *listener;
476   struct Incoming *incoming;
477
478   if (NULL != listener_get (client))
479   {
480     GNUNET_break (0);
481     GNUNET_SERVER_client_disconnect (client);
482     return;
483   }
484   listener = GNUNET_new (struct Listener);
485   listener->client = client;
486   listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
487   listener->app_id = msg->app_id;
488   listener->operation = ntohs (msg->operation);
489   GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
490   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new listener created (op %u, app %s)\n",
491               listener->operation, GNUNET_h2s (&listener->app_id));
492   for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
493   {
494     if ( (NULL == incoming->spec) ||
495          (0 != incoming->suggest_id) )
496       continue;
497     if (listener->operation != incoming->spec->operation)
498       continue;
499     if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &incoming->spec->app_id))
500       continue;
501     incoming_suggest (incoming, listener);
502   }
503   GNUNET_SERVER_receive_done (client, GNUNET_OK);
504 }
505
506
507 /**
508  * Called when a client wants to remove an element
509  * from the set it inhabits.
510  *
511  * @param cls unused
512  * @param client client that sent the message
513  * @param m message sent by the client
514  */
515 static void
516 handle_client_remove (void *cls,
517                       struct GNUNET_SERVER_Client *client,
518                       const struct GNUNET_MessageHeader *m)
519 {
520   struct Set *set;
521
522   set = set_get (client);
523   if (NULL == set)
524   {
525     GNUNET_break (0);
526     GNUNET_SERVER_client_disconnect (client);
527     return;
528   }
529   switch (set->operation)
530   {
531     case GNUNET_SET_OPERATION_UNION:
532       _GSS_union_remove ((struct GNUNET_SET_ElementMessage *) m, set);
533       break;
534     case GNUNET_SET_OPERATION_INTERSECTION:
535       //_GSS_intersection_remove ((struct GNUNET_SET_ElementMessage *) m, set);
536       break;
537     default:
538       GNUNET_assert (0);
539       break;
540   }
541
542   GNUNET_SERVER_receive_done (client, GNUNET_OK);
543 }
544
545
546
547 /**
548  * Called when the client wants to reject an operation
549  * request from another peer.
550  *
551  * @param cls unused
552  * @param client client that sent the message
553  * @param m message sent by the client
554  */
555 static void
556 handle_client_reject (void *cls,
557                       struct GNUNET_SERVER_Client *client,
558                       const struct GNUNET_MessageHeader *m)
559 {
560   struct Incoming *incoming;
561   struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) m;
562
563   GNUNET_break (0 == ntohl (msg->request_id));
564
565   incoming = get_incoming (ntohl (msg->accept_reject_id));
566   if (NULL == incoming)
567   {
568     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
569     return;
570   }
571   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "peer request rejected by client\n");
572   GNUNET_MESH_tunnel_destroy (incoming->tunnel);
573   GNUNET_SERVER_receive_done (client, GNUNET_OK);
574 }
575
576
577
578 /**
579  * Called when a client wants to add an element to a
580  * set it inhabits.
581  *
582  * @param cls unused
583  * @param client client that sent the message
584  * @param m message sent by the client
585  */
586 static void
587 handle_client_add (void *cls,
588                    struct GNUNET_SERVER_Client *client,
589                    const struct GNUNET_MessageHeader *m)
590 {
591   struct Set *set;
592
593   set = set_get (client);
594   if (NULL == set)
595   {
596     GNUNET_break (0);
597     GNUNET_SERVER_client_disconnect (client);
598     return;
599   }
600   switch (set->operation)
601   {
602     case GNUNET_SET_OPERATION_UNION:
603       _GSS_union_add ((struct GNUNET_SET_ElementMessage *) m, set);
604       break;
605     case GNUNET_SET_OPERATION_INTERSECTION:
606       //_GSS_intersection_add ((struct GNUNET_SET_ElementMessage *) m, set);
607       break;
608     default:
609       GNUNET_assert (0);
610       break;
611   }
612
613   GNUNET_SERVER_receive_done (client, GNUNET_OK);
614 }
615
616
617 /**
618  * Called when a client wants to evaluate a set operation with another peer.
619  *
620  * @param cls unused
621  * @param client client that sent the message
622  * @param m message sent by the client
623  */
624 static void
625 handle_client_evaluate (void *cls,
626                         struct GNUNET_SERVER_Client *client,
627                         const struct GNUNET_MessageHeader *m)
628 {
629   struct Set *set;
630   struct TunnelContext *tc;
631   struct GNUNET_MESH_Tunnel *tunnel;
632   struct GNUNET_SET_EvaluateMessage *msg;
633   struct OperationSpecification *spec;
634
635   set = set_get (client);
636   if (NULL == set)
637   {
638     GNUNET_break (0);
639     GNUNET_SERVER_client_disconnect (client);
640     return;
641   }
642
643   msg = (struct GNUNET_SET_EvaluateMessage *) m;
644   tc = GNUNET_new (struct TunnelContext);
645   spec = GNUNET_new (struct OperationSpecification);
646   spec->operation = set->operation;
647   spec->app_id = msg->app_id;
648   spec->salt = ntohl (msg->salt);
649   spec->peer = msg->target_peer;
650
651   tunnel = GNUNET_MESH_tunnel_create (mesh, tc, &msg->target_peer,
652                                       GNUNET_APPLICATION_TYPE_SET);
653
654   switch (set->operation)
655   {
656     case GNUNET_SET_OPERATION_INTERSECTION:
657       tc->type = CONTEXT_OPERATION_INTERSECTION;
658       //_GSS_intersection_evaluate ((struct GNUNET_SET_EvaluateMessage *) m, set);
659       break;
660     case GNUNET_SET_OPERATION_UNION:
661       tc->type = CONTEXT_OPERATION_UNION;
662       tc->data.union_op =
663           _GSS_union_evaluate (spec, tunnel);
664       break;
665     default:
666       GNUNET_assert (0);
667       break;
668   }
669
670   GNUNET_SERVER_receive_done (client, GNUNET_OK);
671 }
672
673
674 /**
675  * Handle an ack from a client.
676  *
677  * @param cls unused
678  * @param client the client
679  * @param m the message
680  */
681 static void
682 handle_client_ack (void *cls,
683                    struct GNUNET_SERVER_Client *client,
684                    const struct GNUNET_MessageHeader *m)
685 {
686   /* FIXME: implement */
687   GNUNET_SERVER_receive_done (client, GNUNET_OK);
688 }
689
690
691 /**
692  * Handle a request from the client to accept
693  * a set operation that came from a remote peer.
694  *
695  * @param cls unused
696  * @param client the client
697  * @param mh the message
698  */
699 static void
700 handle_client_accept (void *cls,
701                       struct GNUNET_SERVER_Client *client,
702                       const struct GNUNET_MessageHeader *mh)
703 {
704   struct Set *set;
705   struct Incoming *incoming;
706   struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
707   struct GNUNET_MESH_Tunnel *tunnel;
708   struct TunnelContext *tc;
709   struct OperationSpecification *spec;
710
711   incoming = get_incoming (ntohl (msg->accept_reject_id));
712
713   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client accepting %u\n", ntohl (msg->accept_reject_id));
714
715   if (NULL == incoming)
716   {
717
718     GNUNET_break (0);
719     GNUNET_SERVER_client_disconnect (client);
720     return;
721   }
722
723   set = set_get (client);
724
725   if (NULL == set)
726   {
727     GNUNET_break (0);
728     GNUNET_SERVER_client_disconnect (client);
729     return;
730   }
731
732   tc = GNUNET_new (struct TunnelContext);
733   tunnel = GNUNET_MESH_tunnel_create (mesh, tc, &incoming->spec->peer,
734                                       GNUNET_APPLICATION_TYPE_SET);
735   spec = GNUNET_new (struct OperationSpecification);
736
737   switch (set->operation)
738   {
739     case GNUNET_SET_OPERATION_INTERSECTION:
740       tc->type = CONTEXT_OPERATION_INTERSECTION;
741       // _GSS_intersection_accept (msg, set, incoming);
742       break;
743     case GNUNET_SET_OPERATION_UNION:
744       tc->type = CONTEXT_OPERATION_UNION;
745       tc->data.union_op = _GSS_union_accept (spec, tunnel);
746       break;
747     default:
748       GNUNET_assert (0);
749       break;
750   }
751
752   /* note: _GSS_*_accept has to make sure the tunnel and mq are set to NULL,
753    * otherwise they will be destroyed and disconnected */
754   incoming_destroy (incoming);
755   GNUNET_SERVER_receive_done (client, GNUNET_OK);
756 }
757
758
759 /**
760  * Called to clean up, after a shutdown has been requested.
761  *
762  * @param cls closure
763  * @param tc context information (why was this task triggered now)
764  */
765 static void
766 shutdown_task (void *cls,
767                const struct GNUNET_SCHEDULER_TaskContext *tc)
768 {
769   if (NULL != mesh)
770   {
771     GNUNET_MESH_disconnect (mesh);
772     mesh = NULL;
773   }
774
775   while (NULL != incoming_head)
776     incoming_destroy (incoming_head);
777
778   while (NULL != listeners_head)
779     listener_destroy (listeners_head);
780
781   while (NULL != sets_head)
782     set_destroy (sets_head);
783
784   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
785 }
786
787
788
789 /**
790  * Signature of the main function of a task.
791  *
792  * @param cls closure
793  * @param tc context information (why was this task triggered now)
794  */
795 static void
796 incoming_timeout_cb (void *cls,
797                      const struct GNUNET_SCHEDULER_TaskContext *tc)
798 {
799   struct Incoming *incoming = cls;
800
801   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "remote peer timed out\n");
802   incoming_destroy (incoming);
803 }
804
805
806 /**
807  * Method called whenever another peer has added us to a tunnel
808  * the other peer initiated.
809  * Only called (once) upon reception of data with a message type which was
810  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
811  * causes te tunnel to be ignored and no further notifications are sent about
812  * the same tunnel.
813  *
814  * @param cls closure
815  * @param tunnel new handle to the tunnel
816  * @param initiator peer that started the tunnel
817  * @param port Port this tunnel is for.
818  * @return initial tunnel context for the tunnel
819  *         (can be NULL -- that's not an error)
820  */
821 static void *
822 tunnel_new_cb (void *cls,
823                struct GNUNET_MESH_Tunnel *tunnel,
824                const struct GNUNET_PeerIdentity *initiator,
825                uint32_t port)
826 {
827   struct Incoming *incoming;
828   struct TunnelContext *tc;
829
830   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new incoming tunnel\n");
831
832   GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
833   tc = GNUNET_new (struct TunnelContext);
834   incoming = GNUNET_new (struct Incoming);
835   incoming->peer = *initiator;
836   incoming->tunnel = tunnel;
837   tc->data.incoming = incoming;
838   tc->type = CONTEXT_INCOMING;
839   incoming->timeout_task = 
840       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
841   GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
842
843   return tc;
844 }
845
846
847 /**
848  * Function called whenever a tunnel is destroyed.  Should clean up
849  * any associated state.
850  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
851  * the tunnel.
852  *
853  * @param cls closure (set from GNUNET_MESH_connect)
854  * @param tunnel connection to the other end (henceforth invalid)
855  * @param tunnel_ctx place where local state associated
856  *                   with the tunnel is stored
857  */
858 static void
859 tunnel_end_cb (void *cls,
860                const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
861 {
862   struct TunnelContext *ctx = tunnel_ctx;
863
864   switch (ctx->type)
865   {
866     case CONTEXT_INCOMING:
867       incoming_destroy (ctx->data.incoming);
868       break;
869     case CONTEXT_OPERATION_UNION:
870       _GSS_union_operation_destroy (ctx->data.union_op);
871       break;
872     case CONTEXT_OPERATION_INTERSECTION:
873       GNUNET_assert (0);
874       /* FIXME: cfuchs */
875       break;
876     default:
877       GNUNET_assert (0);
878   }
879
880   GNUNET_free (tunnel_ctx);
881 }
882
883
884 /**
885  * Function called by the service's run
886  * method to run service-specific setup code.
887  *
888  * @param cls closure
889  * @param server the initialized server
890  * @param cfg configuration to use
891  */
892 static void
893 run (void *cls, struct GNUNET_SERVER_Handle *server,
894      const struct GNUNET_CONFIGURATION_Handle *cfg)
895 {
896   static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
897     {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
898     {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
899     {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
900     {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
901     {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
902     {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
903     {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT, 0},
904     {handle_client_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
905     {NULL, NULL, 0, 0}
906   };
907   static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
908     {handle_p2p_operation_request,
909       GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
910     /* messages for the union operation */
911     {_GSS_union_handle_p2p_message,
912       GNUNET_MESSAGE_TYPE_SET_P2P_IBF, 0},
913     {_GSS_union_handle_p2p_message,
914       GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
915     {_GSS_union_handle_p2p_message,
916       GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
917     {_GSS_union_handle_p2p_message,
918       GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
919     {_GSS_union_handle_p2p_message,
920       GNUNET_MESSAGE_TYPE_SET_P2P_SE, 0},
921     /* FIXME: messages for intersection operation */
922     {NULL, 0, 0}
923   };
924   static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
925
926   configuration = cfg;
927   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
928                                 &shutdown_task, NULL);
929   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
930   GNUNET_SERVER_add_handlers (server, server_handlers);
931
932   mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
933                               mesh_handlers, mesh_ports);
934   if (NULL == mesh)
935   {
936     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not connect to mesh\n");
937     return;
938   }
939
940   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "service started\n");
941 }
942
943
944 /**
945  * The main function for the set service.
946  *
947  * @param argc number of arguments from the command line
948  * @param argv command line arguments
949  * @return 0 ok, 1 on error
950  */
951 int
952 main (int argc, char *const *argv)
953 {
954   int ret;
955   ret = GNUNET_SERVICE_run (argc, argv, "set",
956                             GNUNET_SERVICE_OPTION_NONE, &run, NULL);
957   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
958   return (GNUNET_OK == ret) ? 0 : 1;
959 }
960