- use correct hash type
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_local.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  * Iterator for deleting each channel whose client endpoint disconnected.
172  *
173  * @param cls Closure (client that has disconnected).
174  * @param key The local channel id (used to access the hashmap).
175  * @param value The value stored at the key (channel to destroy).
176  *
177  * @return GNUNET_OK, keep iterating.
178  */
179 static int
180 channel_destroy_iterator (void *cls,
181                           uint32_t key,
182                           void *value)
183 {
184   struct CadetChannel *ch = value;
185   struct CadetClient *c = cls;
186
187   LOG (GNUNET_ERROR_TYPE_DEBUG,
188        " Channel %s destroy, due to client %s shutdown.\n",
189        GCCH_2s (ch), GML_2s (c));
190
191   GCCH_handle_local_destroy (ch, c, key < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
192   return GNUNET_OK;
193 }
194
195
196 /**
197  * Unregister data and free memory for a client.
198  *
199  * @param c Client to destroy. No longer valid after call.
200  */
201 static void
202 client_destroy (struct CadetClient *c)
203 {
204   LOG (GNUNET_ERROR_TYPE_INFO, "  client destroy: %p/%u\n", c, c->id);
205   GNUNET_SERVER_client_drop (c->handle);
206   c->shutting_down = GNUNET_YES;
207
208   if (NULL != c->own_channels)
209   {
210     GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
211                                              &channel_destroy_iterator, c);
212     GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
213   }
214   if (NULL != c->incoming_channels)
215   {
216     GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
217                                              &channel_destroy_iterator, c);
218     GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
219   }
220   if (NULL != c->ports)
221   {
222     GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
223                                              &client_release_ports, c);
224     GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
225   }
226
227   GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
228   GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
229   GNUNET_SERVER_client_set_user_context (c->handle, NULL);
230   GNUNET_free (c);
231 }
232
233 /**
234  * Create a client record, register data and initialize memory.
235  *
236  * @param client Client's handle.
237  */
238 static struct CadetClient *
239 client_new (struct GNUNET_SERVER_Client *client)
240 {
241   struct CadetClient *c;
242
243   GNUNET_SERVER_client_keep (client);
244   GNUNET_SERVER_notification_context_add (nc, client);
245
246   c = GNUNET_new (struct CadetClient);
247   c->handle = client;
248   c->id = next_client_id++; /* overflow not important: just for debug */
249   c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
250
251   c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
252   c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
253
254   GNUNET_SERVER_client_set_user_context (client, c);
255   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
256   GNUNET_STATISTICS_update (stats, "# clients", +1, GNUNET_NO);
257
258   LOG (GNUNET_ERROR_TYPE_INFO, "  client created: %p/%u\n", c, c->id);
259
260   return c;
261 }
262
263
264 /******************************************************************************/
265 /********************************  HANDLES  ***********************************/
266 /******************************************************************************/
267
268 /**
269  * Handler for client connection.
270  *
271  * @param cls Closure (unused).
272  * @param client Client handler.
273  */
274 static void
275 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
276 {
277   LOG (GNUNET_ERROR_TYPE_INFO, "Client connected: %p\n", client);
278   if (NULL == client)
279     return;
280
281   (void) client_new (client);
282 }
283
284
285 /**
286  * Handler for client disconnection
287  *
288  * @param cls closure
289  * @param client identification of the client; NULL
290  *        for the last call when the server is destroyed
291  */
292 static void
293 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
294 {
295   struct CadetClient *c;
296
297   LOG (GNUNET_ERROR_TYPE_INFO, "Client disconnected: %p\n", client);
298
299   c = GML_client_get (client);
300   if (NULL != c)
301   {
302     LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
303                 c->id, c);
304     client_destroy (c);
305   }
306   else
307   {
308     LOG (GNUNET_ERROR_TYPE_DEBUG, " disconnecting client's context NULL\n");
309   }
310   return;
311 }
312
313
314 /**
315  * Handler for new clients
316  *
317  * @param cls closure
318  * @param client identification of the client
319  * @param message the actual message, which includes messages the client wants
320  */
321 static void
322 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
323                    const struct GNUNET_MessageHeader *message)
324 {
325   struct GNUNET_CADET_ClientConnect *cc_msg;
326   struct CadetClient *c;
327   unsigned int size;
328   uint32_t *p;
329   unsigned int i;
330
331   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
332   LOG (GNUNET_ERROR_TYPE_INFO, "new client registering %p\n", client);
333
334   /* Check data sanity */
335   size = ntohs (message->size);
336   if (size < sizeof (struct GNUNET_CADET_ClientConnect))
337   {
338     GNUNET_break (0);
339     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
340     return;
341   }
342   size -= sizeof (struct GNUNET_CADET_ClientConnect); /* Array size */
343   if (0 != (size % sizeof (uint32_t)))
344   {
345     GNUNET_break (0);
346     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
347     return;
348   }
349   size /= sizeof (uint32_t); /* Number of ports */
350   cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
351
352   /* Retrieve client structure */
353   c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
354   if (NULL == c)
355   {
356     GNUNET_break (0);
357     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
358     return;
359   }
360
361   LOG (GNUNET_ERROR_TYPE_INFO, "  client %u has %u ports\n", c-> id, size);
362   if (size > 0)
363   {
364     uint32_t u32;
365
366     p = (uint32_t *) &cc_msg[1];
367     c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
368     for (i = 0; i < size; i++)
369     {
370       u32 = ntohl (p[i]);
371       LOG (GNUNET_ERROR_TYPE_INFO, "    port: %u\n", u32);
372
373       /* store in client's hashmap */
374       GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
375                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
376       /* store in global hashmap */
377       /* FIXME only allow one client to have the port open,
378        *       have a backup hashmap with waiting clients */
379       GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
380                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
381     }
382   }
383
384   GNUNET_SERVER_receive_done (client, GNUNET_OK);
385   LOG (GNUNET_ERROR_TYPE_DEBUG, "new regitering processed\n");
386 }
387
388
389 /**
390  * Handler for requests of new tunnels
391  *
392  * @param cls Closure.
393  * @param client Identification of the client.
394  * @param message The actual message.
395  */
396 static void
397 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
398                        const struct GNUNET_MessageHeader *message)
399 {
400   struct CadetClient *c;
401
402   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
403   LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
404
405   /* Sanity check for client registration */
406   if (NULL == (c = GML_client_get (client)))
407   {
408     GNUNET_break (0);
409     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
410     return;
411   }
412   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
413
414   /* Message size sanity check */
415   if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
416   {
417     GNUNET_break (0);
418     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
419     return;
420   }
421
422   if (GNUNET_OK !=
423       GCCH_handle_local_create (c,
424                                 (struct GNUNET_CADET_ChannelMessage *) message))
425   {
426     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
427     return;
428   }
429
430   GNUNET_SERVER_receive_done (client, GNUNET_OK);
431   return;
432 }
433
434
435 /**
436  * Handler for requests of deleting tunnels
437  *
438  * @param cls closure
439  * @param client identification of the client
440  * @param message the actual message
441  */
442 static void
443 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
444                         const struct GNUNET_MessageHeader *message)
445 {
446   struct GNUNET_CADET_ChannelMessage *msg;
447   struct CadetClient *c;
448   struct CadetChannel *ch;
449   CADET_ChannelNumber chid;
450
451   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
452
453   /* Sanity check for client registration */
454   if (NULL == (c = GML_client_get (client)))
455   {
456     GNUNET_break (0);
457     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
458     return;
459   }
460   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
461
462   /* Message sanity check */
463   if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
464   {
465     GNUNET_break (0);
466     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
467     return;
468   }
469
470   msg = (struct GNUNET_CADET_ChannelMessage *) message;
471
472   /* Retrieve tunnel */
473   chid = ntohl (msg->channel_id);
474   ch = GML_channel_get (c, chid);
475
476   LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n",
477        c->id, chid);
478
479   if (NULL == ch)
480   {
481     LOG (GNUNET_ERROR_TYPE_WARNING, "  channel %X not found\n", chid);
482     GNUNET_STATISTICS_update (stats,
483                               "# client destroy messages on unknown channel",
484                               1, GNUNET_NO);
485     GNUNET_SERVER_receive_done (client, GNUNET_OK);
486     return;
487   }
488
489   GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
490
491   GNUNET_SERVER_receive_done (client, GNUNET_OK);
492   return;
493 }
494
495
496 /**
497  * Handler for client traffic
498  *
499  * @param cls closure
500  * @param client identification of the client
501  * @param message the actual message
502  */
503 static void
504 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
505              const struct GNUNET_MessageHeader *message)
506 {
507   const struct GNUNET_MessageHeader *payload;
508   struct GNUNET_CADET_LocalData *msg;
509   struct CadetClient *c;
510   struct CadetChannel *ch;
511   CADET_ChannelNumber chid;
512   size_t message_size;
513   size_t payload_size;
514   size_t payload_claimed_size;
515   int fwd;
516
517   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
518   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
519   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
520
521   /* Sanity check for client registration */
522   if (NULL == (c = GML_client_get (client)))
523   {
524     GNUNET_break (0);
525     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
526     return;
527   }
528
529   /* Sanity check for message size */
530   message_size = ntohs (message->size);
531   if (sizeof (struct GNUNET_CADET_LocalData)
532       + sizeof (struct GNUNET_MessageHeader) > message_size
533       || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
534   {
535     GNUNET_break (0);
536     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
537     return;
538   }
539
540   /* Sanity check for payload size */
541   payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
542   msg = (struct GNUNET_CADET_LocalData *) message;
543   payload = (struct GNUNET_MessageHeader *) &msg[1];
544   payload_claimed_size = ntohs (payload->size);
545   if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
546       || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
547       || payload_claimed_size > payload_size)
548   {
549     LOG (GNUNET_ERROR_TYPE_WARNING,
550          "client claims to send %u bytes in %u payload\n",
551          payload_claimed_size, payload_size);
552     GNUNET_break (0);
553     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554     return;
555   }
556
557   chid = ntohl (msg->id);
558   LOG (GNUNET_ERROR_TYPE_DEBUG, "  %u bytes (%u payload) by client %u\n",
559        payload_size, payload_claimed_size, c->id);
560
561   /* Channel exists? */
562   fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
563   ch = GML_channel_get (c, chid);
564   if (NULL == ch)
565   {
566     GNUNET_STATISTICS_update (stats,
567                               "# client data messages on unknown channel",
568                               1, GNUNET_NO);
569     GNUNET_SERVER_receive_done (client, GNUNET_OK);
570     return;
571   }
572
573   if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
574   {
575     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
576     return;
577   }
578
579   LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
580   GNUNET_SERVER_receive_done (client, GNUNET_OK);
581
582   return;
583 }
584
585
586 /**
587  * Handler for client's ACKs for payload traffic.
588  *
589  * @param cls Closure (unused).
590  * @param client Identification of the client.
591  * @param message The actual message.
592  */
593 static void
594 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
595             const struct GNUNET_MessageHeader *message)
596 {
597   struct GNUNET_CADET_LocalAck *msg;
598   struct CadetChannel *ch;
599   struct CadetClient *c;
600   CADET_ChannelNumber chid;
601   int fwd;
602
603   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
604   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
605
606   /* Sanity check for client registration */
607   if (NULL == (c = GML_client_get (client)))
608   {
609     GNUNET_break (0);
610     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
611     return;
612   }
613   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
614
615   msg = (struct GNUNET_CADET_LocalAck *) message;
616
617   /* Channel exists? */
618   chid = ntohl (msg->channel_id);
619   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
620   ch = GML_channel_get (c, chid);
621   LOG (GNUNET_ERROR_TYPE_DEBUG, "   -- ch %p\n", ch);
622   if (NULL == ch)
623   {
624     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
625     LOG (GNUNET_ERROR_TYPE_DEBUG, "  for client %u.\n", c->id);
626     GNUNET_STATISTICS_update (stats,
627                               "# client ack messages on unknown channel",
628                               1, GNUNET_NO);
629     GNUNET_SERVER_receive_done (client, GNUNET_OK);
630     return;
631   }
632
633   /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
634   /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
635   fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
636
637   GCCH_handle_local_ack (ch, fwd);
638   GNUNET_SERVER_receive_done (client, GNUNET_OK);
639
640   return;
641 }
642
643
644 /**
645  * Iterator over all peers to send a monitoring client info about each peer.
646  *
647  * @param cls Closure ().
648  * @param peer Peer ID (tunnel remote peer).
649  * @param value Peer info.
650  *
651  * @return #GNUNET_YES, to keep iterating.
652  */
653 static int
654 get_all_peers_iterator (void *cls,
655                         const struct GNUNET_PeerIdentity * peer,
656                         void *value)
657 {
658   struct GNUNET_SERVER_Client *client = cls;
659   struct CadetPeer *p = value;
660   struct GNUNET_CADET_LocalInfoPeer msg;
661
662   msg.header.size = htons (sizeof (msg));
663   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
664   msg.destination = *peer;
665   msg.paths = htons (GCP_count_paths (p));
666   msg.tunnel = htons (NULL != GCP_get_tunnel (p));
667
668   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
669        GNUNET_i2s (peer));
670
671   GNUNET_SERVER_notification_context_unicast (nc, client,
672                                               &msg.header, GNUNET_NO);
673   return GNUNET_YES;
674 }
675
676
677 /**
678  * Iterator over all peers to dump info for each peer.
679  *
680  * @param cls Closure (unused).
681  * @param peer Peer ID (tunnel remote peer).
682  * @param value Peer info.
683  *
684  * @return #GNUNET_YES, to keep iterating.
685  */
686 static int
687 show_peer_iterator (void *cls,
688                         const struct GNUNET_PeerIdentity * peer,
689                         void *value)
690 {
691   struct CadetPeer *p = value;
692   struct CadetTunnel *t;
693
694   GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
695
696   t = GCP_get_tunnel (p);
697   if (NULL != t)
698     GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
699
700   LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
701
702   return GNUNET_YES;
703 }
704
705
706 /**
707  * Iterator over all paths of a peer to build an InfoPeer message.
708  *
709  * Message contains blocks of peers, first not included.
710  *
711  * @param cls Closure (message to build).
712  * @param peer Peer this path is towards.
713  * @param path Path itself
714  * @return #GNUNET_YES if should keep iterating.
715  *         #GNUNET_NO otherwise.
716  */
717 static int
718 path_info_iterator (void *cls,
719                     struct CadetPeer *peer,
720                     struct CadetPeerPath *path)
721 {
722   struct GNUNET_CADET_LocalInfoPeer *resp = cls;
723   struct GNUNET_PeerIdentity *id;
724   uint16_t msg_size;
725   uint16_t path_size;
726   unsigned int i;
727
728   msg_size = ntohs (resp->header.size);
729   path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
730
731   LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
732   if (msg_size + path_size > UINT16_MAX)
733   {
734     LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
735     return GNUNET_NO;
736   }
737
738   i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
739   i = i / sizeof (struct GNUNET_PeerIdentity);
740
741   /* Set id to the address of the first free peer slot. */
742   id = (struct GNUNET_PeerIdentity *) &resp[1];
743   id = &id[i];
744
745   /* Don't copy first peers.
746    * First peer is always the local one.
747    * Last peer is always the destination (leave as 0, EOL).
748    */
749   for (i = 0; i < path->length - 1; i++)
750   {
751     GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
752     LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
753   }
754
755   resp->header.size = htons (msg_size + path_size);
756
757   return GNUNET_YES;
758 }
759
760
761 /**
762  * Handler for client's INFO PEERS request.
763  *
764  * @param cls Closure (unused).
765  * @param client Identification of the client.
766  * @param message The actual message.
767  */
768 static void
769 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
770                     const struct GNUNET_MessageHeader *message)
771 {
772   struct CadetClient *c;
773   struct GNUNET_MessageHeader reply;
774
775   /* Sanity check for client registration */
776   if (NULL == (c = GML_client_get (client)))
777   {
778     GNUNET_break (0);
779     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
780     return;
781   }
782
783   LOG (GNUNET_ERROR_TYPE_DEBUG,
784        "Received get peers request from client %u (%p)\n",
785        c->id, client);
786
787   GCP_iterate_all (get_all_peers_iterator, client);
788   reply.size = htons (sizeof (reply));
789   reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
790   GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
791
792   LOG (GNUNET_ERROR_TYPE_DEBUG,
793        "Get peers request from client %u completed\n", c->id);
794   GNUNET_SERVER_receive_done (client, GNUNET_OK);
795 }
796
797
798 /**
799  * Handler for client's SHOW_PEER request.
800  *
801  * @param cls Closure (unused).
802  * @param client Identification of the client.
803  * @param message The actual message.
804  */
805 void
806 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
807                   const struct GNUNET_MessageHeader *message)
808 {
809   const struct GNUNET_CADET_LocalInfo *msg;
810   struct GNUNET_CADET_LocalInfoPeer *resp;
811   struct CadetPeer *p;
812   struct CadetClient *c;
813   unsigned char cbuf[64 * 1024];
814
815   /* Sanity check for client registration */
816   if (NULL == (c = GML_client_get (client)))
817   {
818     GNUNET_break (0);
819     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
820     return;
821   }
822
823   msg = (struct GNUNET_CADET_LocalInfo *) message;
824   resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
825   LOG (GNUNET_ERROR_TYPE_INFO,
826        "Received peer info request from client %u for peer %s\n",
827        c->id, GNUNET_i2s_full (&msg->peer));
828
829   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
830   resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
831   resp->destination = msg->peer;
832   p = GCP_get (&msg->peer, GNUNET_NO);
833   if (NULL == p)
834   {
835     /* We don't know the peer */
836
837     LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
838          GNUNET_i2s_full (&msg->peer));
839     resp->paths = htons (0);
840     resp->tunnel = htons (NULL != GCP_get_tunnel (p));
841
842     GNUNET_SERVER_notification_context_unicast (nc, client,
843                                                 &resp->header,
844                                                 GNUNET_NO);
845     GNUNET_SERVER_receive_done (client, GNUNET_OK);
846     return;
847   }
848
849   resp->paths = htons (GCP_count_paths (p));
850   resp->tunnel = htons (NULL != GCP_get_tunnel (p));
851   GCP_iterate_paths (p, &path_info_iterator, resp);
852
853   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
854                                               &resp->header, GNUNET_NO);
855
856   LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
857   GNUNET_SERVER_receive_done (client, GNUNET_OK);
858 }
859
860
861 /**
862  * Iterator over all tunnels to send a monitoring client info about each tunnel.
863  *
864  * @param cls Closure ().
865  * @param peer Peer ID (tunnel remote peer).
866  * @param value Tunnel info.
867  *
868  * @return #GNUNET_YES, to keep iterating.
869  */
870 static int
871 get_all_tunnels_iterator (void *cls,
872                           const struct GNUNET_PeerIdentity * peer,
873                           void *value)
874 {
875   struct GNUNET_SERVER_Client *client = cls;
876   struct CadetTunnel *t = value;
877   struct GNUNET_CADET_LocalInfoTunnel msg;
878
879   msg.header.size = htons (sizeof (msg));
880   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
881   msg.destination = *peer;
882   msg.channels = htonl (GCT_count_channels (t));
883   msg.connections = htonl (GCT_count_any_connections (t));
884   msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
885   msg.estate = htons ((uint16_t) GCT_get_estate (t));
886
887   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
888        GNUNET_i2s (peer));
889
890   GNUNET_SERVER_notification_context_unicast (nc, client,
891                                               &msg.header, GNUNET_NO);
892   return GNUNET_YES;
893 }
894
895
896 /**
897  * Handler for client's INFO TUNNELS request.
898  *
899  * @param cls Closure (unused).
900  * @param client Identification of the client.
901  * @param message The actual message.
902  */
903 static void
904 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
905                     const struct GNUNET_MessageHeader *message)
906 {
907   struct CadetClient *c;
908   struct GNUNET_MessageHeader reply;
909
910   /* Sanity check for client registration */
911   if (NULL == (c = GML_client_get (client)))
912   {
913     GNUNET_break (0);
914     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
915     return;
916   }
917
918   LOG (GNUNET_ERROR_TYPE_DEBUG,
919        "Received get tunnels request from client %u (%p)\n",
920        c->id, client);
921
922   GCT_iterate_all (get_all_tunnels_iterator, client);
923   reply.size = htons (sizeof (reply));
924   reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
925   GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
926
927   LOG (GNUNET_ERROR_TYPE_DEBUG,
928        "Get tunnels request from client %u completed\n", c->id);
929   GNUNET_SERVER_receive_done (client, GNUNET_OK);
930 }
931
932
933 static void
934 iter_connection (void *cls, struct CadetConnection *c)
935 {
936   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
937   struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
938
939   h[msg->connections] = *(GCC_get_id (c));
940   msg->connections++;
941 }
942
943 static void
944 iter_channel (void *cls, struct CadetChannel *ch)
945 {
946   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
947   struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
948   CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
949
950   chn[msg->channels] = htonl (GCCH_get_id (ch));
951   msg->channels++;
952 }
953
954
955 /**
956  * Handler for client's SHOW_TUNNEL request.
957  *
958  * @param cls Closure (unused).
959  * @param client Identification of the client.
960  * @param message The actual message.
961  */
962 void
963 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
964                     const struct GNUNET_MessageHeader *message)
965 {
966   const struct GNUNET_CADET_LocalInfo *msg;
967   struct GNUNET_CADET_LocalInfoTunnel *resp;
968   struct CadetClient *c;
969   struct CadetTunnel *t;
970   unsigned int ch_n;
971   unsigned int c_n;
972   size_t size;
973
974   /* Sanity check for client registration */
975   if (NULL == (c = GML_client_get (client)))
976   {
977     GNUNET_break (0);
978     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
979     return;
980   }
981
982   msg = (struct GNUNET_CADET_LocalInfo *) message;
983   LOG (GNUNET_ERROR_TYPE_DEBUG,
984        "Received tunnel info request from client %u for tunnel %s\n",
985        c->id, GNUNET_i2s_full(&msg->peer));
986
987   t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
988   if (NULL == t)
989   {
990     /* We don't know the tunnel */
991     struct GNUNET_CADET_LocalInfoTunnel warn;
992
993     LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
994          GNUNET_i2s_full(&msg->peer), sizeof (warn));
995     warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
996     warn.header.size = htons (sizeof (warn));
997     warn.destination = msg->peer;
998     warn.channels = htonl (0);
999     warn.connections = htonl (0);
1000     warn.cstate = htons (0);
1001     warn.estate = htons (0);
1002
1003     GNUNET_SERVER_notification_context_unicast (nc, client,
1004                                                 &warn.header,
1005                                                 GNUNET_NO);
1006     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1007     return;
1008   }
1009
1010   /* Initialize context */
1011   ch_n = GCT_count_channels (t);
1012   c_n = GCT_count_any_connections (t);
1013
1014   size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1015   size += c_n * sizeof (struct GNUNET_CADET_Hash);
1016   size += ch_n * sizeof (CADET_ChannelNumber);
1017
1018   resp = GNUNET_malloc (size);
1019   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1020   resp->header.size = htons (size);
1021   resp->destination = msg->peer;
1022   /* Do not interleave with iterators, iter_channel needs conn in HBO */
1023   GCT_iterate_connections (t, &iter_connection, resp);
1024   GCT_iterate_channels (t, &iter_channel, resp);
1025   resp->connections = htonl (resp->connections);
1026   resp->channels = htonl (resp->channels);
1027   /* Do not interleave end */
1028   resp->cstate = htons (GCT_get_cstate (t));
1029   resp->estate = htons (GCT_get_estate (t));
1030   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1031                                               &resp->header, GNUNET_NO);
1032   GNUNET_free (resp);
1033
1034   LOG (GNUNET_ERROR_TYPE_DEBUG,
1035        "Show tunnel request from client %u completed. %u conn, %u ch\n",
1036        c->id, c_n, ch_n);
1037   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1038 }
1039
1040
1041 /**
1042  * Handler for client's INFO_DUMP request.
1043  *
1044  * @param cls Closure (unused).
1045  * @param client Identification of the client.
1046  * @param message The actual message.
1047  */
1048 void
1049 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1050                   const struct GNUNET_MessageHeader *message)
1051 {
1052   struct CadetClient *c;
1053
1054   /* Sanity check for client registration */
1055   if (NULL == (c = GML_client_get (client)))
1056   {
1057     GNUNET_break (0);
1058     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1059     return;
1060   }
1061
1062   LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1063        c->id);
1064   LOG (GNUNET_ERROR_TYPE_ERROR,
1065        "*************************** DUMP START ***************************\n");
1066
1067   for (c = clients_head; NULL != c; c = c->next)
1068   {
1069     LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
1070          c->id, c, c->handle);
1071     if (NULL != c->ports)
1072       LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
1073            GNUNET_CONTAINER_multihashmap32_size (c->ports));
1074     else
1075       LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n");
1076     LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
1077          GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
1078     LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
1079          GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
1080   }
1081   LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1082   GCP_iterate_all (&show_peer_iterator, NULL);
1083
1084   LOG (GNUNET_ERROR_TYPE_ERROR,
1085        "**************************** DUMP END ****************************\n");
1086
1087   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1088 }
1089
1090
1091 /**
1092  * Functions to handle messages from clients
1093  */
1094 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1095   {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1096   {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1097    sizeof (struct GNUNET_CADET_ChannelMessage)},
1098   {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1099    sizeof (struct GNUNET_CADET_ChannelMessage)},
1100   {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1101   {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1102    sizeof (struct GNUNET_CADET_LocalAck)},
1103   {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1104    sizeof (struct GNUNET_MessageHeader)},
1105   {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1106    sizeof (struct GNUNET_CADET_LocalInfo)},
1107   {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1108    sizeof (struct GNUNET_MessageHeader)},
1109   {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1110    sizeof (struct GNUNET_CADET_LocalInfo)},
1111   {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1112    sizeof (struct GNUNET_MessageHeader)},
1113   {NULL, NULL, 0, 0}
1114 };
1115
1116
1117
1118 /******************************************************************************/
1119 /********************************    API    ***********************************/
1120 /******************************************************************************/
1121
1122 /**
1123  * Initialize server subsystem.
1124  *
1125  * @param handle Server handle.
1126  */
1127 void
1128 GML_init (struct GNUNET_SERVER_Handle *handle)
1129 {
1130   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1131   server_handle = handle;
1132   GNUNET_SERVER_suspend (server_handle);
1133   ports = GNUNET_CONTAINER_multihashmap32_create (32);
1134 }
1135
1136
1137 /**
1138  * Install server (service) handlers and start listening to clients.
1139  */
1140 void
1141 GML_start (void)
1142 {
1143   GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1144   GNUNET_SERVER_connect_notify (server_handle,  &handle_client_connect, NULL);
1145   GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1146                                    NULL);
1147   nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1148
1149   clients_head = NULL;
1150   clients_tail = NULL;
1151   next_client_id = 0;
1152   GNUNET_SERVER_resume (server_handle);
1153 }
1154
1155
1156 /**
1157  * Shutdown server.
1158  */
1159 void
1160 GML_shutdown (void)
1161 {
1162   struct CadetClient *c;
1163
1164   for (c = clients_head; NULL != clients_head; c = clients_head)
1165     client_destroy (c);
1166
1167   if (nc != NULL)
1168   {
1169     GNUNET_SERVER_notification_context_destroy (nc);
1170     nc = NULL;
1171   }
1172
1173 }
1174
1175
1176 /**
1177  * Get a channel from a client.
1178  *
1179  * @param c Client to check.
1180  * @param chid Channel ID, must be local (> 0x800...).
1181  *
1182  * @return non-NULL if channel exists in the clients lists
1183  */
1184 struct CadetChannel *
1185 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1186 {
1187   struct GNUNET_CONTAINER_MultiHashMap32 *map;
1188
1189   if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1190   {
1191     GNUNET_break_op (0);
1192     LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1193     return NULL;
1194   }
1195
1196   if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1197     map = c->incoming_channels;
1198   else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1199     map = c->own_channels;
1200   else
1201   {
1202     GNUNET_break (0);
1203     map = NULL;
1204   }
1205   if (NULL == map)
1206   {
1207     GNUNET_break (0);
1208     LOG (GNUNET_ERROR_TYPE_DEBUG,
1209          "Client %s does no t have a valid map for CHID %X\n",
1210          GML_2s (c), chid);
1211     return NULL;
1212   }
1213   return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1214 }
1215
1216
1217 /**
1218  * Add a channel to a client
1219  *
1220  * @param client Client.
1221  * @param chid Channel ID.
1222  * @param ch Channel.
1223  */
1224 void
1225 GML_channel_add (struct CadetClient *client,
1226                  uint32_t chid,
1227                  struct CadetChannel *ch)
1228 {
1229   if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1230     GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1231                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1232   else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1233     GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1234                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1235   else
1236     GNUNET_break (0);
1237 }
1238
1239
1240 /**
1241  * Remove a channel from a client.
1242  *
1243  * @param client Client.
1244  * @param chid Channel ID.
1245  * @param ch Channel.
1246  */
1247 void
1248 GML_channel_remove (struct CadetClient *client,
1249                     uint32_t chid,
1250                     struct CadetChannel *ch)
1251 {
1252   if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1253     GNUNET_break (GNUNET_YES ==
1254                   GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1255                                                           chid, ch));
1256   else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1257     GNUNET_break (GNUNET_YES ==
1258                   GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1259                                                           chid, ch));
1260   else
1261     GNUNET_break (0);
1262 }
1263
1264
1265 /**
1266  * Get the tunnel's next free local channel ID.
1267  *
1268  * @param c Client.
1269  *
1270  * @return LID of a channel free to use.
1271  */
1272 CADET_ChannelNumber
1273 GML_get_next_chid (struct CadetClient *c)
1274 {
1275   CADET_ChannelNumber chid;
1276
1277   while (NULL != GML_channel_get (c, c->next_chid))
1278   {
1279     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1280     c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1281   }
1282   chid = c->next_chid;
1283   c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1284
1285   return chid;
1286 }
1287
1288
1289 /**
1290  * Check if client has registered with the service and has not disconnected
1291  *
1292  * @param client the client to check
1293  *
1294  * @return non-NULL if client exists in the global DLL
1295  */
1296 struct CadetClient *
1297 GML_client_get (struct GNUNET_SERVER_Client *client)
1298 {
1299   if (NULL == client)
1300     return NULL;
1301   return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1302 }
1303
1304 /**
1305  * Find a client that has opened a port
1306  *
1307  * @param port Port to check.
1308  *
1309  * @return non-NULL if a client has the port.
1310  */
1311 struct CadetClient *
1312 GML_client_get_by_port (uint32_t port)
1313 {
1314   return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1315 }
1316
1317
1318 /**
1319  * Deletes a channel from a client (either owner or destination).
1320  *
1321  * @param c Client whose tunnel to delete.
1322  * @param ch Channel which should be deleted.
1323  * @param id Channel ID.
1324  */
1325 void
1326 GML_client_delete_channel (struct CadetClient *c,
1327                            struct CadetChannel *ch,
1328                            CADET_ChannelNumber id)
1329 {
1330   int res;
1331
1332   if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1333   {
1334     res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1335                                                   id, ch);
1336     if (GNUNET_YES != res)
1337       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1338   }
1339   else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1340   {
1341     res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1342                                                   id, ch);
1343     if (GNUNET_YES != res)
1344       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1345   }
1346   else
1347   {
1348     GNUNET_break (0);
1349   }
1350 }
1351
1352 /**
1353  * Build a local ACK message and send it to a local client, if needed.
1354  *
1355  * If the client was already allowed to send data, do nothing.
1356  *
1357  * @param c Client to whom send the ACK.
1358  * @param id Channel ID to use
1359  */
1360 void
1361 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1362 {
1363   struct GNUNET_CADET_LocalAck msg;
1364
1365   LOG (GNUNET_ERROR_TYPE_DEBUG,
1366               "send local %s ack on %X towards %p\n",
1367               id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1368
1369   msg.header.size = htons (sizeof (msg));
1370   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1371   msg.channel_id = htonl (id);
1372   GNUNET_SERVER_notification_context_unicast (nc,
1373                                               c->handle,
1374                                               &msg.header,
1375                                               GNUNET_NO);
1376
1377 }
1378
1379
1380
1381 /**
1382  * Notify the client that a new incoming channel was created.
1383  *
1384  * @param c Client to notify.
1385  * @param id Channel ID.
1386  * @param port Channel's destination port.
1387  * @param opt Options (bit array).
1388  * @param peer Origin peer.
1389  */
1390 void
1391 GML_send_channel_create (struct CadetClient *c,
1392                          uint32_t id, uint32_t port, uint32_t opt,
1393                          const struct GNUNET_PeerIdentity *peer)
1394 {
1395   struct GNUNET_CADET_ChannelMessage msg;
1396
1397   msg.header.size = htons (sizeof (msg));
1398   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1399   msg.channel_id = htonl (id);
1400   msg.port = htonl (port);
1401   msg.opt = htonl (opt);
1402   msg.peer = *peer;
1403   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1404                                               &msg.header, GNUNET_NO);
1405 }
1406
1407
1408 /**
1409  * Build a local channel NACK message and send it to a local client.
1410  *
1411  * @param c Client to whom send the NACK.
1412  * @param id Channel ID to use
1413  */
1414 void
1415 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1416 {
1417   struct GNUNET_CADET_LocalAck msg;
1418
1419   LOG (GNUNET_ERROR_TYPE_DEBUG,
1420        "send local nack on %X towards %p\n",
1421        id, c);
1422
1423   msg.header.size = htons (sizeof (msg));
1424   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1425   msg.channel_id = htonl (id);
1426   GNUNET_SERVER_notification_context_unicast (nc,
1427                                               c->handle,
1428                                               &msg.header,
1429                                               GNUNET_NO);
1430
1431 }
1432
1433 /**
1434  * Notify a client that a channel is no longer valid.
1435  *
1436  * @param c Client.
1437  * @param id ID of the channel that is destroyed.
1438  */
1439 void
1440 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1441 {
1442   struct GNUNET_CADET_ChannelMessage msg;
1443
1444   if (NULL == c)
1445   {
1446     GNUNET_break (0);
1447     return;
1448   }
1449   if (GNUNET_YES == c->shutting_down)
1450     return;
1451   msg.header.size = htons (sizeof (msg));
1452   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1453   msg.channel_id = htonl (id);
1454   msg.port = htonl (0);
1455   memset (&msg.peer, 0, sizeof (msg.peer));
1456   msg.opt = htonl (0);
1457   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1458                                               &msg.header, GNUNET_NO);
1459 }
1460
1461
1462 /**
1463  * Modify the cadet message ID from global to local and send to client.
1464  *
1465  * @param c Client to send to.
1466  * @param msg Message to modify and send.
1467  * @param id Channel ID to use (c can be both owner and client).
1468  */
1469 void
1470 GML_send_data (struct CadetClient *c,
1471                const struct GNUNET_CADET_Data *msg,
1472                CADET_ChannelNumber id)
1473 {
1474   struct GNUNET_CADET_LocalData *copy;
1475   uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1476   char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1477
1478   if (size < sizeof (struct GNUNET_MessageHeader))
1479   {
1480     GNUNET_break_op (0);
1481     return;
1482   }
1483   if (NULL == c)
1484   {
1485     GNUNET_break (0);
1486     return;
1487   }
1488   copy = (struct GNUNET_CADET_LocalData *) cbuf;
1489   memcpy (&copy[1], &msg[1], size);
1490   copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1491   copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1492   copy->id = htonl (id);
1493   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1494                                               &copy->header, GNUNET_NO);
1495 }
1496
1497
1498 /**
1499  * Get the static string to represent a client.
1500  *
1501  * @param c Client.
1502  *
1503  * @return Static string for the client.
1504  */
1505 const char *
1506 GML_2s (const struct CadetClient *c)
1507 {
1508   static char buf[32];
1509
1510   SPRINTF (buf, "%u", c->id);
1511   return buf;
1512 }