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