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