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