specify proper access control rules for conversation service
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_local.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 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet_statistics_service.h"
26
27 #include "mesh.h"
28 #include "mesh_protocol.h" /* GNUNET_MESH_Data is shared */
29
30 #include "gnunet-service-mesh_local.h"
31 #include "gnunet-service-mesh_channel.h"
32
33 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
34
35 /******************************************************************************/
36 /********************************   STRUCTS  **********************************/
37 /******************************************************************************/
38
39 /**
40  * Struct containing information about a client of the service
41  *
42  * TODO: add a list of 'waiting' ports
43  */
44 struct MeshClient
45 {
46     /**
47      * Linked list next
48      */
49   struct MeshClient *next;
50
51     /**
52      * Linked list prev
53      */
54   struct MeshClient *prev;
55
56     /**
57      * Tunnels that belong to this client, indexed by local id
58      */
59   struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
60
61     /**
62      * Tunnels this client has accepted, indexed by incoming local id
63      */
64   struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
65
66     /**
67      * Channel ID for the next incoming channel.
68      */
69   MESH_ChannelNumber next_chid;
70
71     /**
72      * Handle to communicate with the client
73      */
74   struct GNUNET_SERVER_Client *handle;
75
76     /**
77      * Ports that this client has declared interest in.
78      * Indexed by port, contains *Client.
79      */
80   struct GNUNET_CONTAINER_MultiHashMap32 *ports;
81
82     /**
83      * Whether the client is active or shutting down (don't send confirmations
84      * to a client that is shutting down.
85      */
86   int shutting_down;
87
88     /**
89      * ID of the client, mainly for debug messages
90      */
91   unsigned int id;
92 };
93
94 /******************************************************************************/
95 /*******************************   GLOBALS  ***********************************/
96 /******************************************************************************/
97
98 /**
99  * Global handle to the statistics service.
100  */
101 extern struct GNUNET_STATISTICS_Handle *stats;
102
103 /**
104  * Handle to server lib.
105  */
106 static struct GNUNET_SERVER_Handle *server_handle;
107
108 /**
109  * DLL with all the clients, head.
110  */
111 static struct MeshClient *clients_head;
112
113 /**
114  * DLL with all the clients, tail.
115  */
116 static struct MeshClient *clients_tail;
117
118 /**
119  * Next ID to assign to a client.
120  */
121 unsigned int next_client_id;
122
123 /**
124  * All ports clients of this peer have opened.
125  */
126 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
127
128 /**
129  * Notification context, to send messages to local clients.
130  */
131 static struct GNUNET_SERVER_NotificationContext *nc;
132
133
134 /******************************************************************************/
135 /********************************   STATIC  ***********************************/
136 /******************************************************************************/
137
138 /**
139  * Remove client's ports from the global hashmap on disconnect.
140  *
141  * @param cls Closure (unused).
142  * @param key Port.
143  * @param value Client structure.
144  *
145  * @return GNUNET_OK, keep iterating.
146  */
147 static int
148 client_release_ports (void *cls,
149                       uint32_t key,
150                       void *value)
151 {
152   int res;
153
154   res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
155   if (GNUNET_YES != res)
156   {
157     GNUNET_break (0);
158     LOG (GNUNET_ERROR_TYPE_WARNING,
159                 "Port %u by client %p was not registered.\n",
160                 key, value);
161   }
162   return GNUNET_OK;
163 }
164
165
166
167 /******************************************************************************/
168 /********************************  HANDLES  ***********************************/
169 /******************************************************************************/
170
171
172 /**
173  * Handler for client connection.
174  *
175  * @param cls Closure (unused).
176  * @param client Client handler.
177  */
178 static void
179 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
180 {
181   struct MeshClient *c;
182
183   LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
184   if (NULL == client)
185     return;
186   c = GNUNET_new (struct MeshClient);
187   c->handle = client;
188   c->id = next_client_id++; /* overflow not important: just for debug */
189   c->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
190   GNUNET_SERVER_client_keep (client);
191   GNUNET_SERVER_client_set_user_context (client, c);
192   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
193 }
194
195
196 /**
197  * Iterator for deleting each channel whose client endpoint disconnected.
198  *
199  * @param cls Closure (client that has disconnected).
200  * @param key The local channel id (used to access the hashmap).
201  * @param value The value stored at the key (channel to destroy).
202  *
203  * @return GNUNET_OK, keep iterating.
204  */
205 static int
206 channel_destroy_iterator (void *cls,
207                           uint32_t key,
208                           void *value)
209 {
210   struct MeshChannel *ch = value;
211   struct MeshClient *c = cls;
212
213   LOG (GNUNET_ERROR_TYPE_DEBUG,
214               " Channel %s destroy, due to client %s shutdown.\n",
215               GMCH_2s (ch), GML_2s (c));
216
217   GMCH_handle_local_destroy (ch, c, key < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
218   return GNUNET_OK;
219 }
220
221 /**
222  * Handler for client disconnection
223  *
224  * @param cls closure
225  * @param client identification of the client; NULL
226  *        for the last call when the server is destroyed
227  */
228 static void
229 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
230 {
231   struct MeshClient *c;
232
233   LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
234   if (client == NULL)
235   {
236     LOG (GNUNET_ERROR_TYPE_DEBUG, "   (SERVER DOWN)\n");
237     return;
238   }
239
240   c = GML_client_get (client);
241   if (NULL != c)
242   {
243     LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
244                 c->id, c);
245     GNUNET_SERVER_client_drop (c->handle);
246     c->shutting_down = GNUNET_YES;
247     if (NULL != c->own_channels)
248     {
249       GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
250                                                &channel_destroy_iterator, c);
251       GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
252     }
253
254     if (NULL != c->incoming_channels)
255     {
256       GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
257                                                &channel_destroy_iterator, c);
258       GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
259     }
260
261     if (NULL != c->ports)
262     {
263       GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
264                                                &client_release_ports, c);
265       GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
266     }
267     GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
268     GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
269     LOG (GNUNET_ERROR_TYPE_DEBUG, "  client free (%p)\n", c);
270     GNUNET_free (c);
271   }
272   else
273   {
274     LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
275   }
276   LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
277   return;
278 }
279
280
281 /**
282  * Handler for new clients
283  *
284  * @param cls closure
285  * @param client identification of the client
286  * @param message the actual message, which includes messages the client wants
287  */
288 static void
289 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
290                    const struct GNUNET_MessageHeader *message)
291 {
292   struct GNUNET_MESH_ClientConnect *cc_msg;
293   struct MeshClient *c;
294   unsigned int size;
295   uint32_t *p;
296   unsigned int i;
297
298   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nnew client connected %p\n", client);
299
300   /* Check data sanity */
301   size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
302   cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
303   if (0 != (size % sizeof (uint32_t)))
304   {
305     GNUNET_break (0);
306     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
307     return;
308   }
309   size /= sizeof (uint32_t);
310
311   /* Initialize new client structure */
312   c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
313   LOG (GNUNET_ERROR_TYPE_DEBUG, "  client id %u\n", c->id);
314   LOG (GNUNET_ERROR_TYPE_DEBUG, "  client has %u ports\n", size);
315   if (size > 0)
316   {
317     uint32_t u32;
318
319     p = (uint32_t *) &cc_msg[1];
320     c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
321     for (i = 0; i < size; i++)
322     {
323       u32 = ntohl (p[i]);
324       LOG (GNUNET_ERROR_TYPE_DEBUG, "    port: %u\n", u32);
325
326       /* store in client's hashmap */
327       GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
328                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
329       /* store in global hashmap */
330       /* FIXME only allow one client to have the port open,
331        *       have a backup hashmap with waiting clients */
332       GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
333                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
334     }
335   }
336
337   c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
338   c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
339   GNUNET_SERVER_notification_context_add (nc, client);
340   GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
341
342   GNUNET_SERVER_receive_done (client, GNUNET_OK);
343   LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
344 }
345
346
347 /**
348  * Handler for requests of new tunnels
349  *
350  * @param cls Closure.
351  * @param client Identification of the client.
352  * @param message The actual message.
353  */
354 static void
355 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
356                        const struct GNUNET_MessageHeader *message)
357 {
358   struct MeshClient *c;
359
360   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
361   LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
362
363   /* Sanity check for client registration */
364   if (NULL == (c = GML_client_get (client)))
365   {
366     GNUNET_break (0);
367     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
368     return;
369   }
370   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
371
372   /* Message size sanity check */
373   if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
374   {
375     GNUNET_break (0);
376     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
377     return;
378   }
379
380   if (GNUNET_OK !=
381       GMCH_handle_local_create (c,
382                                 (struct GNUNET_MESH_ChannelMessage *) message))
383   {
384     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
385     return;
386   }
387
388   GNUNET_SERVER_receive_done (client, GNUNET_OK);
389   return;
390 }
391
392
393 /**
394  * Handler for requests of deleting tunnels
395  *
396  * @param cls closure
397  * @param client identification of the client
398  * @param message the actual message
399  */
400 static void
401 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
402                         const struct GNUNET_MessageHeader *message)
403 {
404   struct GNUNET_MESH_ChannelMessage *msg;
405   struct MeshClient *c;
406   struct MeshChannel *ch;
407   MESH_ChannelNumber chid;
408
409   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot a DESTROY CHANNEL from client!\n");
410
411   /* Sanity check for client registration */
412   if (NULL == (c = GML_client_get (client)))
413   {
414     GNUNET_break (0);
415     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
416     return;
417   }
418   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
419
420   /* Message sanity check */
421   if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
422   {
423     GNUNET_break (0);
424     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
425     return;
426   }
427
428   msg = (struct GNUNET_MESH_ChannelMessage *) message;
429
430   /* Retrieve tunnel */
431   chid = ntohl (msg->channel_id);
432   LOG (GNUNET_ERROR_TYPE_DEBUG, "  for channel %X\n", chid);
433   ch = GML_channel_get (c, chid);
434   if (NULL == ch)
435   {
436     LOG (GNUNET_ERROR_TYPE_DEBUG, "  channel %X not found\n", chid);
437     GNUNET_STATISTICS_update (stats,
438                               "# client destroy messages on unknown channel",
439                               1, GNUNET_NO);
440     GNUNET_SERVER_receive_done (client, GNUNET_OK);
441     return;
442   }
443
444   GMCH_handle_local_destroy (ch, c, chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
445
446   GNUNET_SERVER_receive_done (client, GNUNET_OK);
447   return;
448 }
449
450
451 /**
452  * Handler for client traffic
453  *
454  * @param cls closure
455  * @param client identification of the client
456  * @param message the actual message
457  */
458 static void
459 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
460              const struct GNUNET_MessageHeader *message)
461 {
462   struct GNUNET_MESH_LocalData *msg;
463   struct MeshClient *c;
464   struct MeshChannel *ch;
465   MESH_ChannelNumber chid;
466   size_t size;
467   int fwd;
468
469   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot data from a client!\n");
470
471   /* Sanity check for client registration */
472   if (NULL == (c = GML_client_get (client)))
473   {
474     GNUNET_break (0);
475     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
476     return;
477   }
478   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
479
480   msg = (struct GNUNET_MESH_LocalData *) message;
481
482   /* Sanity check for message size */
483   size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
484   if (size < sizeof (struct GNUNET_MessageHeader))
485   {
486     GNUNET_break (0);
487     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
488     return;
489   }
490
491   /* Channel exists? */
492   chid = ntohl (msg->id);
493   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
494   fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
495   ch = GML_channel_get (c, chid);
496   if (NULL == ch)
497   {
498     GNUNET_STATISTICS_update (stats,
499                               "# client data messages on unknown channel",
500                               1, GNUNET_NO);
501     GNUNET_SERVER_receive_done (client, GNUNET_OK);
502     return;
503   }
504
505   if (GNUNET_OK !=
506       GMCH_handle_local_data (ch, c,
507                               (struct GNUNET_MessageHeader *)&msg[1], fwd))
508   {
509     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
510     return;
511   }
512
513   LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
514   GNUNET_SERVER_receive_done (client, GNUNET_OK);
515
516   return;
517 }
518
519
520 /**
521  * Handler for client's ACKs for payload traffic.
522  *
523  * @param cls Closure (unused).
524  * @param client Identification of the client.
525  * @param message The actual message.
526  */
527 static void
528 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
529             const struct GNUNET_MessageHeader *message)
530 {
531   struct GNUNET_MESH_LocalAck *msg;
532   struct MeshChannel *ch;
533   struct MeshClient *c;
534   MESH_ChannelNumber chid;
535   int fwd;
536
537   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
538   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
539
540   /* Sanity check for client registration */
541   if (NULL == (c = GML_client_get (client)))
542   {
543     GNUNET_break (0);
544     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
545     return;
546   }
547   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
548
549   msg = (struct GNUNET_MESH_LocalAck *) message;
550
551   /* Channel exists? */
552   chid = ntohl (msg->channel_id);
553   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
554   ch = GML_channel_get (c, chid);
555   LOG (GNUNET_ERROR_TYPE_DEBUG, "   -- ch %p\n", ch);
556   if (NULL == ch)
557   {
558     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
559     LOG (GNUNET_ERROR_TYPE_DEBUG, "  for client %u.\n", c->id);
560     GNUNET_STATISTICS_update (stats,
561                               "# client ack messages on unknown channel",
562                               1, GNUNET_NO);
563     GNUNET_SERVER_receive_done (client, GNUNET_OK);
564     return;
565   }
566
567   /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
568   /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
569   fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
570
571   GMCH_handle_local_ack (ch, fwd);
572   GNUNET_SERVER_receive_done (client, GNUNET_OK);
573
574   return;
575 }
576
577
578 /*
579  * Iterator over all tunnels to send a monitoring client info about each tunnel.
580  *
581  * @param cls Closure (client handle).
582  * @param key Key (hashed tunnel ID, unused).
583  * @param value Tunnel info.
584  *
585  * @return #GNUNET_YES, to keep iterating.
586  */
587 // static int
588 // monitor_all_tunnels_iterator (void *cls,
589 //                               const struct GNUNET_HashCode * key,
590 //                               void *value)
591 // {
592 //   struct GNUNET_SERVER_Client *client = cls;
593 //   struct MeshChannel *ch = value;
594 //   struct GNUNET_MESH_LocalMonitor *msg;
595 //
596 //   msg = GNUNET_new (struct GNUNET_MESH_LocalMonitor);
597 //   msg->channel_id = htonl (ch->gid);
598 //   msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
599 //   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
600 //
601 //   LOG (GNUNET_ERROR_TYPE_INFO,
602 //               "*  sending info about tunnel %s\n",
603 //               GNUNET_i2s (&msg->owner));
604 //
605 //   GNUNET_SERVER_notification_context_unicast (nc, client,
606 //                                               &msg->header, GNUNET_NO);
607 //   return GNUNET_YES;
608 // }
609
610
611 /**
612  * Handler for client's MONITOR request.
613  *
614  * @param cls Closure (unused).
615  * @param client Identification of the client.
616  * @param message The actual message.
617  */
618 static void
619 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
620                     const struct GNUNET_MessageHeader *message)
621 {
622   struct MeshClient *c;
623
624   /* Sanity check for client registration */
625   if (NULL == (c = GML_client_get (client)))
626   {
627     GNUNET_break (0);
628     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
629     return;
630   }
631
632   LOG (GNUNET_ERROR_TYPE_INFO,
633               "Received get tunnels request from client %u\n",
634               c->id);
635 //   GNUNET_CONTAINER_multihashmap_iterate (tunnels,
636 //                                          monitor_all_tunnels_iterator,
637 //                                          client);
638   LOG (GNUNET_ERROR_TYPE_INFO,
639               "Get tunnels request from client %u completed\n",
640               c->id);
641   GNUNET_SERVER_receive_done (client, GNUNET_OK);
642 }
643
644
645 /**
646  * Handler for client's MONITOR_TUNNEL request.
647  *
648  * @param cls Closure (unused).
649  * @param client Identification of the client.
650  * @param message The actual message.
651  */
652 void
653 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
654                     const struct GNUNET_MessageHeader *message)
655 {
656   const struct GNUNET_MESH_LocalMonitor *msg;
657   struct GNUNET_MESH_LocalMonitor *resp;
658   struct MeshClient *c;
659   struct MeshChannel *ch;
660
661   /* Sanity check for client registration */
662   if (NULL == (c = GML_client_get (client)))
663   {
664     GNUNET_break (0);
665     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
666     return;
667   }
668
669   msg = (struct GNUNET_MESH_LocalMonitor *) message;
670   LOG (GNUNET_ERROR_TYPE_INFO,
671               "Received tunnel info request from client %u for tunnel %s[%X]\n",
672               c->id,
673               &msg->owner,
674               ntohl (msg->channel_id));
675 //   ch = channel_get (&msg->owner, ntohl (msg->channel_id));
676   ch = NULL; // FIXME
677   if (NULL == ch)
678   {
679     /* We don't know the tunnel */
680     struct GNUNET_MESH_LocalMonitor warn;
681
682     warn = *msg;
683     GNUNET_SERVER_notification_context_unicast (nc, client,
684                                                 &warn.header,
685                                                 GNUNET_NO);
686     GNUNET_SERVER_receive_done (client, GNUNET_OK);
687     return;
688   }
689
690   /* Initialize context */
691   resp = GNUNET_new (struct GNUNET_MESH_LocalMonitor);
692   *resp = *msg;
693   resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
694   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
695                                               &resp->header, GNUNET_NO);
696   GNUNET_free (resp);
697
698   LOG (GNUNET_ERROR_TYPE_INFO,
699               "Monitor tunnel request from client %u completed\n",
700               c->id);
701   GNUNET_SERVER_receive_done (client, GNUNET_OK);
702 }
703
704
705 /**
706  * Functions to handle messages from clients
707  */
708 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
709   {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
710   {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
711    sizeof (struct GNUNET_MESH_ChannelMessage)},
712   {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
713    sizeof (struct GNUNET_MESH_ChannelMessage)},
714   {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
715   {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
716    sizeof (struct GNUNET_MESH_LocalAck)},
717   {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
718    sizeof (struct GNUNET_MessageHeader)},
719   {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
720    sizeof (struct GNUNET_MESH_LocalMonitor)},
721   {NULL, NULL, 0, 0}
722 };
723
724
725
726 /******************************************************************************/
727 /********************************    API    ***********************************/
728 /******************************************************************************/
729
730 /**
731  * Initialize server subsystem.
732  *
733  * @param handle Server handle.
734  */
735 void
736 GML_init (struct GNUNET_SERVER_Handle *handle)
737 {
738   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
739   server_handle = handle;
740   GNUNET_SERVER_suspend (server_handle);
741   ports = GNUNET_CONTAINER_multihashmap32_create (32);
742 }
743
744
745 /**
746  * Install server (service) handlers and start listening to clients.
747  */
748 void
749 GML_start (void)
750 {
751   GNUNET_SERVER_add_handlers (server_handle, client_handlers);
752   GNUNET_SERVER_connect_notify (server_handle,  &handle_client_connect, NULL);
753   GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
754                                    NULL);
755   nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
756
757   clients_head = NULL;
758   clients_tail = NULL;
759   next_client_id = 0;
760   GNUNET_SERVER_resume (server_handle);
761 }
762
763
764 /**
765  * Shutdown server.
766  */
767 void
768 GML_shutdown (void)
769 {
770   if (nc != NULL)
771   {
772     GNUNET_SERVER_notification_context_destroy (nc);
773     nc = NULL;
774   }
775 }
776
777
778 /**
779  * Get a channel from a client.
780  *
781  * @param c Client to check.
782  * @param chid Channel ID, must be local (> 0x800...).
783  *
784  * @return non-NULL if channel exists in the clients lists
785  */
786 struct MeshChannel *
787 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
788 {
789   struct GNUNET_CONTAINER_MultiHashMap32 *map;
790
791   if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
792   {
793     GNUNET_break_op (0);
794     LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
795     return NULL;
796   }
797
798   if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
799     map = c->incoming_channels;
800   else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
801     map = c->own_channels;
802   else
803   {
804     GNUNET_break (0);
805     map = NULL;
806   }
807   if (NULL == map)
808   {
809     GNUNET_break (0);
810     LOG (GNUNET_ERROR_TYPE_DEBUG,
811          "Client %s does no t have a valid map for CHID %X\n",
812          GML_2s (c), chid);
813     return NULL;
814   }
815   return GNUNET_CONTAINER_multihashmap32_get (map, chid);
816 }
817
818
819 /**
820  * Add a channel to a client
821  *
822  * @param client Client.
823  * @param chid Channel ID.
824  * @param ch Channel.
825  */
826 void
827 GML_channel_add (struct MeshClient *client,
828                  uint32_t chid,
829                  struct MeshChannel *ch)
830 {
831   if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
832     GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
833                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
834   else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
835     GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
836                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
837   else
838     GNUNET_break (0);
839 }
840
841
842 /**
843  * Remove a channel from a client.
844  *
845  * @param client Client.
846  * @param chid Channel ID.
847  * @param ch Channel.
848  */
849 void
850 GML_channel_remove (struct MeshClient *client,
851                     uint32_t chid,
852                     struct MeshChannel *ch)
853 {
854   if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
855     GNUNET_break (GNUNET_YES ==
856                   GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
857                                                           chid, ch));
858   else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
859     GNUNET_break (GNUNET_YES ==
860                   GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
861                                                           chid, ch));
862   else
863     GNUNET_break (0);
864 }
865
866
867 /**
868  * Get the tunnel's next free local channel ID.
869  *
870  * @param c Client.
871  *
872  * @return LID of a channel free to use.
873  */
874 MESH_ChannelNumber
875 GML_get_next_chid (struct MeshClient *c)
876 {
877   MESH_ChannelNumber chid;
878
879   while (NULL != GML_channel_get (c, c->next_chid))
880   {
881     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
882     c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
883   }
884   chid = c->next_chid;
885   c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
886
887   return chid;
888 }
889
890
891 /**
892  * Check if client has registered with the service and has not disconnected
893  *
894  * @param client the client to check
895  *
896  * @return non-NULL if client exists in the global DLL
897  */
898 struct MeshClient *
899 GML_client_get (struct GNUNET_SERVER_Client *client)
900 {
901   return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
902 }
903
904 /**
905  * Find a client that has opened a port
906  *
907  * @param port Port to check.
908  *
909  * @return non-NULL if a client has the port.
910  */
911 struct MeshClient *
912 GML_client_get_by_port (uint32_t port)
913 {
914   return GNUNET_CONTAINER_multihashmap32_get (ports, port);
915 }
916
917
918 /**
919  * Deletes a channel from a client (either owner or destination).
920  *
921  * @param c Client whose tunnel to delete.
922  * @param ch Channel which should be deleted.
923  * @param id Channel ID.
924  */
925 void
926 GML_client_delete_channel (struct MeshClient *c,
927                            struct MeshChannel *ch,
928                            MESH_ChannelNumber id)
929 {
930   int res;
931
932   if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
933   {
934     res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
935                                                   id, ch);
936     if (GNUNET_YES != res)
937       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
938   }
939   else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
940   {
941     res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
942                                                   id, ch);
943     if (GNUNET_YES != res)
944       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
945   }
946   else
947   {
948     GNUNET_break (0);
949   }
950 }
951
952 /**
953  * Build a local ACK message and send it to a local client, if needed.
954  *
955  * If the client was already allowed to send data, do nothing.
956  *
957  * @param c Client to whom send the ACK.
958  * @param id Channel ID to use
959  */
960 void
961 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
962 {
963   struct GNUNET_MESH_LocalAck msg;
964
965   LOG (GNUNET_ERROR_TYPE_DEBUG,
966               "send local %s ack on %X towards %p\n",
967               id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
968
969   msg.header.size = htons (sizeof (msg));
970   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
971   msg.channel_id = htonl (id);
972   GNUNET_SERVER_notification_context_unicast (nc,
973                                               c->handle,
974                                               &msg.header,
975                                               GNUNET_NO);
976
977 }
978
979
980 /**
981  * Build a local channel NACK message and send it to a local client.
982  *
983  * @param c Client to whom send the NACK.
984  * @param id Channel ID to use
985  */
986 void
987 GML_send_nack (struct MeshClient *c, MESH_ChannelNumber id)
988 {
989   struct GNUNET_MESH_LocalAck msg;
990
991   LOG (GNUNET_ERROR_TYPE_DEBUG,
992               "send local nack on %X towards %p\n",
993               id, c);
994
995   msg.header.size = htons (sizeof (msg));
996   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK);
997   msg.channel_id = htonl (id);
998   GNUNET_SERVER_notification_context_unicast (nc,
999                                               c->handle,
1000                                               &msg.header,
1001                                               GNUNET_NO);
1002
1003 }
1004
1005
1006 /**
1007  * Notify the client that a new incoming channel was created.
1008  *
1009  * @param c Client to notify.
1010  * @param id Channel ID.
1011  * @param port Channel's destination port.
1012  * @param opt Options (bit array).
1013  * @param peer Origin peer.
1014  */
1015 void
1016 GML_send_channel_create (struct MeshClient *c,
1017                          uint32_t id, uint32_t port, uint32_t opt,
1018                          const struct GNUNET_PeerIdentity *peer)
1019 {
1020   struct GNUNET_MESH_ChannelMessage msg;
1021
1022   msg.header.size = htons (sizeof (msg));
1023   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1024   msg.channel_id = htonl (id);
1025   msg.port = htonl (port);
1026   msg.opt = htonl (opt);
1027   msg.peer = *peer;
1028   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1029                                               &msg.header, GNUNET_NO);
1030 }
1031
1032
1033 /**
1034  * Notify a client that a channel is no longer valid.
1035  *
1036  * @param c Client.
1037  * @param id ID of the channel that is destroyed.
1038  */
1039 void
1040 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1041 {
1042   struct GNUNET_MESH_ChannelMessage msg;
1043
1044   if (NULL == c)
1045   {
1046     GNUNET_break (0);
1047     return;
1048   }
1049   if (GNUNET_YES == c->shutting_down)
1050     return;
1051   msg.header.size = htons (sizeof (msg));
1052   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1053   msg.channel_id = htonl (id);
1054   msg.port = htonl (0);
1055   memset (&msg.peer, 0, sizeof (msg.peer));
1056   msg.opt = htonl (0);
1057   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1058                                               &msg.header, GNUNET_NO);
1059 }
1060
1061
1062 /**
1063  * Modify the mesh message ID from global to local and send to client.
1064  *
1065  * @param c Client to send to.
1066  * @param msg Message to modify and send.
1067  * @param id Channel ID to use (c can be both owner and client).
1068  */
1069 void
1070 GML_send_data (struct MeshClient *c,
1071                const struct GNUNET_MESH_Data *msg,
1072                MESH_ChannelNumber id)
1073 {
1074   struct GNUNET_MESH_LocalData *copy;
1075   uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1076   char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1077
1078   if (size < sizeof (struct GNUNET_MessageHeader))
1079   {
1080     GNUNET_break_op (0);
1081     return;
1082   }
1083   if (NULL == c)
1084   {
1085     GNUNET_break (0);
1086     return;
1087   }
1088   copy = (struct GNUNET_MESH_LocalData *) cbuf;
1089   memcpy (&copy[1], &msg[1], size);
1090   copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1091   copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1092   copy->id = htonl (id);
1093   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1094                                               &copy->header, GNUNET_NO);
1095 }
1096
1097
1098 /**
1099  * Get the static string to represent a client.
1100  *
1101  * @param c Client.
1102  *
1103  * @return Static string for the client.
1104  */
1105 const char *
1106 GML_2s (const struct MeshClient *c)
1107 {
1108   static char buf[32];
1109
1110   sprintf (buf, "%u", c->id);
1111   return buf;
1112 }