20cf1592b42a2a1945c2255688474076f7cb6ff4
[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   LOG (GNUNET_ERROR_TYPE_DEBUG, "  for channel %X\n", chid);
475   ch = GML_channel_get (c, chid);
476   if (NULL == ch)
477   {
478     LOG (GNUNET_ERROR_TYPE_DEBUG, "  channel %X not found\n", chid);
479     GNUNET_STATISTICS_update (stats,
480                               "# client destroy messages on unknown channel",
481                               1, GNUNET_NO);
482     GNUNET_SERVER_receive_done (client, GNUNET_OK);
483     return;
484   }
485
486   GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
487
488   GNUNET_SERVER_receive_done (client, GNUNET_OK);
489   return;
490 }
491
492
493 /**
494  * Handler for client traffic
495  *
496  * @param cls closure
497  * @param client identification of the client
498  * @param message the actual message
499  */
500 static void
501 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
502              const struct GNUNET_MessageHeader *message)
503 {
504   const struct GNUNET_MessageHeader *payload;
505   struct GNUNET_CADET_LocalData *msg;
506   struct CadetClient *c;
507   struct CadetChannel *ch;
508   CADET_ChannelNumber chid;
509   size_t message_size;
510   size_t payload_size;
511   size_t payload_claimed_size;
512   int fwd;
513
514   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
515   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
516   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
517
518   /* Sanity check for client registration */
519   if (NULL == (c = GML_client_get (client)))
520   {
521     GNUNET_break (0);
522     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
523     return;
524   }
525
526   /* Sanity check for message size */
527   message_size = ntohs (message->size);
528   if (sizeof (struct GNUNET_CADET_LocalData)
529       + sizeof (struct GNUNET_MessageHeader) > message_size
530       || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
531   {
532     GNUNET_break (0);
533     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
534     return;
535   }
536
537   /* Sanity check for payload size */
538   payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
539   msg = (struct GNUNET_CADET_LocalData *) message;
540   payload = (struct GNUNET_MessageHeader *) &msg[1];
541   payload_claimed_size = ntohs (payload->size);
542   if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
543       || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
544       || payload_claimed_size > payload_size)
545   {
546     LOG (GNUNET_ERROR_TYPE_WARNING,
547          "client claims to send %u bytes in %u payload\n",
548          payload_claimed_size, payload_size);
549     GNUNET_break (0);
550     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
551     return;
552   }
553
554   chid = ntohl (msg->id);
555   LOG (GNUNET_ERROR_TYPE_DEBUG, "  %u bytes (%u payload) by client %u\n",
556        payload_size, payload_claimed_size, c->id);
557
558   /* Channel exists? */
559   fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
560   ch = GML_channel_get (c, chid);
561   if (NULL == ch)
562   {
563     GNUNET_STATISTICS_update (stats,
564                               "# client data messages on unknown channel",
565                               1, GNUNET_NO);
566     GNUNET_SERVER_receive_done (client, GNUNET_OK);
567     return;
568   }
569
570   if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
571   {
572     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
573     return;
574   }
575
576   LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
577   GNUNET_SERVER_receive_done (client, GNUNET_OK);
578
579   return;
580 }
581
582
583 /**
584  * Handler for client's ACKs for payload traffic.
585  *
586  * @param cls Closure (unused).
587  * @param client Identification of the client.
588  * @param message The actual message.
589  */
590 static void
591 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
592             const struct GNUNET_MessageHeader *message)
593 {
594   struct GNUNET_CADET_LocalAck *msg;
595   struct CadetChannel *ch;
596   struct CadetClient *c;
597   CADET_ChannelNumber chid;
598   int fwd;
599
600   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
601   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
602
603   /* Sanity check for client registration */
604   if (NULL == (c = GML_client_get (client)))
605   {
606     GNUNET_break (0);
607     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608     return;
609   }
610   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
611
612   msg = (struct GNUNET_CADET_LocalAck *) message;
613
614   /* Channel exists? */
615   chid = ntohl (msg->channel_id);
616   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
617   ch = GML_channel_get (c, chid);
618   LOG (GNUNET_ERROR_TYPE_DEBUG, "   -- ch %p\n", ch);
619   if (NULL == ch)
620   {
621     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
622     LOG (GNUNET_ERROR_TYPE_DEBUG, "  for client %u.\n", c->id);
623     GNUNET_STATISTICS_update (stats,
624                               "# client ack messages on unknown channel",
625                               1, GNUNET_NO);
626     GNUNET_SERVER_receive_done (client, GNUNET_OK);
627     return;
628   }
629
630   /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
631   /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
632   fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
633
634   GCCH_handle_local_ack (ch, fwd);
635   GNUNET_SERVER_receive_done (client, GNUNET_OK);
636
637   return;
638 }
639
640
641 /**
642  * Iterator over all peers to send a monitoring client info about each peer.
643  *
644  * @param cls Closure ().
645  * @param peer Peer ID (tunnel remote peer).
646  * @param value Peer info.
647  *
648  * @return #GNUNET_YES, to keep iterating.
649  */
650 static int
651 get_all_peers_iterator (void *cls,
652                         const struct GNUNET_PeerIdentity * peer,
653                         void *value)
654 {
655   struct GNUNET_SERVER_Client *client = cls;
656   struct CadetPeer *p = value;
657   struct GNUNET_CADET_LocalInfoPeer msg;
658
659   msg.header.size = htons (sizeof (msg));
660   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
661   msg.destination = *peer;
662   msg.paths = htons (GCP_count_paths (p));
663   msg.tunnel = htons (NULL != GCP_get_tunnel (p));
664
665   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
666        GNUNET_i2s (peer));
667
668   GNUNET_SERVER_notification_context_unicast (nc, client,
669                                               &msg.header, GNUNET_NO);
670   return GNUNET_YES;
671 }
672
673
674 /**
675  * Iterator over all peers to dump info for each peer.
676  *
677  * @param cls Closure (unused).
678  * @param peer Peer ID (tunnel remote peer).
679  * @param value Peer info.
680  *
681  * @return #GNUNET_YES, to keep iterating.
682  */
683 static int
684 show_peer_iterator (void *cls,
685                         const struct GNUNET_PeerIdentity * peer,
686                         void *value)
687 {
688   struct CadetPeer *p = value;
689   struct CadetTunnel *t;
690
691   GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
692
693   t = GCP_get_tunnel (p);
694   if (NULL != t)
695     GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
696
697   LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
698
699   return GNUNET_YES;
700 }
701
702
703 /**
704  * Iterator over all paths of a peer to build an InfoPeer message.
705  *
706  * Message contains blocks of peers, first not included.
707  *
708  * @param cls Closure (message to build).
709  * @param peer Peer this path is towards.
710  * @param path Path itself
711  * @return #GNUNET_YES if should keep iterating.
712  *         #GNUNET_NO otherwise.
713  */
714 static int
715 path_info_iterator (void *cls,
716                     struct CadetPeer *peer,
717                     struct CadetPeerPath *path)
718 {
719   struct GNUNET_CADET_LocalInfoPeer *resp = cls;
720   struct GNUNET_PeerIdentity *id;
721   uint16_t msg_size;
722   uint16_t path_size;
723   unsigned int i;
724
725   msg_size = ntohs (resp->header.size);
726   path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
727
728   LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
729   if (msg_size + path_size > UINT16_MAX)
730   {
731     LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
732     return GNUNET_NO;
733   }
734
735   i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
736   i = i / sizeof (struct GNUNET_PeerIdentity);
737
738   /* Set id to the address of the first free peer slot. */
739   id = (struct GNUNET_PeerIdentity *) &resp[1];
740   id = &id[i];
741
742   /* Don't copy first peers.
743    * First peer is always the local one.
744    * Last peer is always the destination (leave as 0, EOL).
745    */
746   for (i = 0; i < path->length - 1; i++)
747   {
748     GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
749     LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
750   }
751
752   resp->header.size = htons (msg_size + path_size);
753
754   return GNUNET_YES;
755 }
756
757
758 /**
759  * Handler for client's INFO PEERS request.
760  *
761  * @param cls Closure (unused).
762  * @param client Identification of the client.
763  * @param message The actual message.
764  */
765 static void
766 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
767                     const struct GNUNET_MessageHeader *message)
768 {
769   struct CadetClient *c;
770   struct GNUNET_MessageHeader reply;
771
772   /* Sanity check for client registration */
773   if (NULL == (c = GML_client_get (client)))
774   {
775     GNUNET_break (0);
776     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
777     return;
778   }
779
780   LOG (GNUNET_ERROR_TYPE_DEBUG,
781        "Received get peers request from client %u (%p)\n",
782        c->id, client);
783
784   GCP_iterate_all (get_all_peers_iterator, client);
785   reply.size = htons (sizeof (reply));
786   reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
787   GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
788
789   LOG (GNUNET_ERROR_TYPE_DEBUG,
790        "Get peers request from client %u completed\n", c->id);
791   GNUNET_SERVER_receive_done (client, GNUNET_OK);
792 }
793
794
795 /**
796  * Handler for client's SHOW_PEER request.
797  *
798  * @param cls Closure (unused).
799  * @param client Identification of the client.
800  * @param message The actual message.
801  */
802 void
803 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
804                   const struct GNUNET_MessageHeader *message)
805 {
806   const struct GNUNET_CADET_LocalInfo *msg;
807   struct GNUNET_CADET_LocalInfoPeer *resp;
808   struct CadetPeer *p;
809   struct CadetClient *c;
810   unsigned char cbuf[64 * 1024];
811
812   /* Sanity check for client registration */
813   if (NULL == (c = GML_client_get (client)))
814   {
815     GNUNET_break (0);
816     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
817     return;
818   }
819
820   msg = (struct GNUNET_CADET_LocalInfo *) message;
821   resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
822   LOG (GNUNET_ERROR_TYPE_INFO,
823        "Received peer info request from client %u for peer %s\n",
824        c->id, GNUNET_i2s_full (&msg->peer));
825
826   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
827   resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
828   resp->destination = msg->peer;
829   p = GCP_get (&msg->peer, GNUNET_NO);
830   if (NULL == p)
831   {
832     /* We don't know the peer */
833
834     LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
835          GNUNET_i2s_full (&msg->peer));
836     resp->paths = htons (0);
837     resp->tunnel = htons (NULL != GCP_get_tunnel (p));
838
839     GNUNET_SERVER_notification_context_unicast (nc, client,
840                                                 &resp->header,
841                                                 GNUNET_NO);
842     GNUNET_SERVER_receive_done (client, GNUNET_OK);
843     return;
844   }
845
846   resp->paths = htons (GCP_count_paths (p));
847   resp->tunnel = htons (NULL != GCP_get_tunnel (p));
848   GCP_iterate_paths (p, &path_info_iterator, resp);
849
850   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
851                                               &resp->header, GNUNET_NO);
852
853   LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
854   GNUNET_SERVER_receive_done (client, GNUNET_OK);
855 }
856
857
858 /**
859  * Iterator over all tunnels to send a monitoring client info about each tunnel.
860  *
861  * @param cls Closure ().
862  * @param peer Peer ID (tunnel remote peer).
863  * @param value Tunnel info.
864  *
865  * @return #GNUNET_YES, to keep iterating.
866  */
867 static int
868 get_all_tunnels_iterator (void *cls,
869                           const struct GNUNET_PeerIdentity * peer,
870                           void *value)
871 {
872   struct GNUNET_SERVER_Client *client = cls;
873   struct CadetTunnel *t = value;
874   struct GNUNET_CADET_LocalInfoTunnel msg;
875
876   msg.header.size = htons (sizeof (msg));
877   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
878   msg.destination = *peer;
879   msg.channels = htonl (GCT_count_channels (t));
880   msg.connections = htonl (GCT_count_any_connections (t));
881   msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
882   msg.estate = htons ((uint16_t) GCT_get_estate (t));
883
884   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
885        GNUNET_i2s (peer));
886
887   GNUNET_SERVER_notification_context_unicast (nc, client,
888                                               &msg.header, GNUNET_NO);
889   return GNUNET_YES;
890 }
891
892
893 /**
894  * Handler for client's INFO TUNNELS request.
895  *
896  * @param cls Closure (unused).
897  * @param client Identification of the client.
898  * @param message The actual message.
899  */
900 static void
901 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
902                     const struct GNUNET_MessageHeader *message)
903 {
904   struct CadetClient *c;
905   struct GNUNET_MessageHeader reply;
906
907   /* Sanity check for client registration */
908   if (NULL == (c = GML_client_get (client)))
909   {
910     GNUNET_break (0);
911     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
912     return;
913   }
914
915   LOG (GNUNET_ERROR_TYPE_DEBUG,
916        "Received get tunnels request from client %u (%p)\n",
917        c->id, client);
918
919   GCT_iterate_all (get_all_tunnels_iterator, client);
920   reply.size = htons (sizeof (reply));
921   reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
922   GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
923
924   LOG (GNUNET_ERROR_TYPE_DEBUG,
925        "Get tunnels request from client %u completed\n", c->id);
926   GNUNET_SERVER_receive_done (client, GNUNET_OK);
927 }
928
929
930 static void
931 iter_connection (void *cls, struct CadetConnection *c)
932 {
933   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
934   struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
935
936   h[msg->connections] = *(GCC_get_id (c));
937   msg->connections++;
938 }
939
940 static void
941 iter_channel (void *cls, struct CadetChannel *ch)
942 {
943   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
944   struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
945   CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
946
947   chn[msg->channels] = htonl (GCCH_get_id (ch));
948   msg->channels++;
949 }
950
951
952 /**
953  * Handler for client's SHOW_TUNNEL request.
954  *
955  * @param cls Closure (unused).
956  * @param client Identification of the client.
957  * @param message The actual message.
958  */
959 void
960 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
961                     const struct GNUNET_MessageHeader *message)
962 {
963   const struct GNUNET_CADET_LocalInfo *msg;
964   struct GNUNET_CADET_LocalInfoTunnel *resp;
965   struct CadetClient *c;
966   struct CadetTunnel *t;
967   unsigned int ch_n;
968   unsigned int c_n;
969   size_t size;
970
971   /* Sanity check for client registration */
972   if (NULL == (c = GML_client_get (client)))
973   {
974     GNUNET_break (0);
975     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
976     return;
977   }
978
979   msg = (struct GNUNET_CADET_LocalInfo *) message;
980   LOG (GNUNET_ERROR_TYPE_DEBUG,
981        "Received tunnel info request from client %u for tunnel %s\n",
982        c->id, GNUNET_i2s_full(&msg->peer));
983
984   t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
985   if (NULL == t)
986   {
987     /* We don't know the tunnel */
988     struct GNUNET_CADET_LocalInfoTunnel warn;
989
990     LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
991          GNUNET_i2s_full(&msg->peer), sizeof (warn));
992     warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
993     warn.header.size = htons (sizeof (warn));
994     warn.destination = msg->peer;
995     warn.channels = htonl (0);
996     warn.connections = htonl (0);
997     warn.cstate = htons (0);
998     warn.estate = htons (0);
999
1000     GNUNET_SERVER_notification_context_unicast (nc, client,
1001                                                 &warn.header,
1002                                                 GNUNET_NO);
1003     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1004     return;
1005   }
1006
1007   /* Initialize context */
1008   ch_n = GCT_count_channels (t);
1009   c_n = GCT_count_any_connections (t);
1010
1011   size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1012   size += c_n * sizeof (struct GNUNET_CADET_Hash);
1013   size += ch_n * sizeof (CADET_ChannelNumber);
1014
1015   resp = GNUNET_malloc (size);
1016   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1017   resp->header.size = htons (size);
1018   GCT_iterate_connections (t, &iter_connection, resp);
1019   GCT_iterate_channels (t, &iter_channel, resp);
1020   /* Do not interleave with iterators, iter_channel needs conn in HBO */
1021   resp->destination = msg->peer;
1022   resp->connections = htonl (resp->connections);
1023   resp->channels = htonl (resp->channels);
1024   resp->cstate = htons (GCT_get_cstate (t));
1025   resp->estate = htons (GCT_get_estate (t));
1026   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1027                                               &resp->header, GNUNET_NO);
1028   GNUNET_free (resp);
1029
1030   LOG (GNUNET_ERROR_TYPE_DEBUG,
1031        "Show tunnel request from client %u completed. %u conn, %u ch\n",
1032        c->id, c_n, ch_n);
1033   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1034 }
1035
1036
1037 /**
1038  * Handler for client's INFO_DUMP request.
1039  *
1040  * @param cls Closure (unused).
1041  * @param client Identification of the client.
1042  * @param message The actual message.
1043  */
1044 void
1045 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1046                   const struct GNUNET_MessageHeader *message)
1047 {
1048   struct CadetClient *c;
1049
1050   /* Sanity check for client registration */
1051   if (NULL == (c = GML_client_get (client)))
1052   {
1053     GNUNET_break (0);
1054     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1055     return;
1056   }
1057
1058   LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1059        c->id);
1060   LOG (GNUNET_ERROR_TYPE_ERROR,
1061        "*************************** DUMP START ***************************\n");
1062
1063   for (c = clients_head; NULL != c; c = c->next)
1064   {
1065     LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
1066          c->id, c, c->handle);
1067     if (NULL != c->ports)
1068       LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
1069            GNUNET_CONTAINER_multihashmap32_size (c->ports));
1070     else
1071       LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n");
1072     LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
1073          GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
1074     LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
1075          GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
1076   }
1077   LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1078   GCP_iterate_all (&show_peer_iterator, NULL);
1079
1080   LOG (GNUNET_ERROR_TYPE_ERROR,
1081        "**************************** DUMP END ****************************\n");
1082
1083   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1084 }
1085
1086
1087 /**
1088  * Functions to handle messages from clients
1089  */
1090 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1091   {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1092   {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1093    sizeof (struct GNUNET_CADET_ChannelMessage)},
1094   {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1095    sizeof (struct GNUNET_CADET_ChannelMessage)},
1096   {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1097   {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1098    sizeof (struct GNUNET_CADET_LocalAck)},
1099   {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1100    sizeof (struct GNUNET_MessageHeader)},
1101   {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1102    sizeof (struct GNUNET_CADET_LocalInfo)},
1103   {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1104    sizeof (struct GNUNET_MessageHeader)},
1105   {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1106    sizeof (struct GNUNET_CADET_LocalInfo)},
1107   {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1108    sizeof (struct GNUNET_MessageHeader)},
1109   {NULL, NULL, 0, 0}
1110 };
1111
1112
1113
1114 /******************************************************************************/
1115 /********************************    API    ***********************************/
1116 /******************************************************************************/
1117
1118 /**
1119  * Initialize server subsystem.
1120  *
1121  * @param handle Server handle.
1122  */
1123 void
1124 GML_init (struct GNUNET_SERVER_Handle *handle)
1125 {
1126   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1127   server_handle = handle;
1128   GNUNET_SERVER_suspend (server_handle);
1129   ports = GNUNET_CONTAINER_multihashmap32_create (32);
1130 }
1131
1132
1133 /**
1134  * Install server (service) handlers and start listening to clients.
1135  */
1136 void
1137 GML_start (void)
1138 {
1139   GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1140   GNUNET_SERVER_connect_notify (server_handle,  &handle_client_connect, NULL);
1141   GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1142                                    NULL);
1143   nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1144
1145   clients_head = NULL;
1146   clients_tail = NULL;
1147   next_client_id = 0;
1148   GNUNET_SERVER_resume (server_handle);
1149 }
1150
1151
1152 /**
1153  * Shutdown server.
1154  */
1155 void
1156 GML_shutdown (void)
1157 {
1158   struct CadetClient *c;
1159
1160   for (c = clients_head; NULL != clients_head; c = clients_head)
1161     client_destroy (c);
1162
1163   if (nc != NULL)
1164   {
1165     GNUNET_SERVER_notification_context_destroy (nc);
1166     nc = NULL;
1167   }
1168
1169 }
1170
1171
1172 /**
1173  * Get a channel from a client.
1174  *
1175  * @param c Client to check.
1176  * @param chid Channel ID, must be local (> 0x800...).
1177  *
1178  * @return non-NULL if channel exists in the clients lists
1179  */
1180 struct CadetChannel *
1181 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1182 {
1183   struct GNUNET_CONTAINER_MultiHashMap32 *map;
1184
1185   if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1186   {
1187     GNUNET_break_op (0);
1188     LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1189     return NULL;
1190   }
1191
1192   if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1193     map = c->incoming_channels;
1194   else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1195     map = c->own_channels;
1196   else
1197   {
1198     GNUNET_break (0);
1199     map = NULL;
1200   }
1201   if (NULL == map)
1202   {
1203     GNUNET_break (0);
1204     LOG (GNUNET_ERROR_TYPE_DEBUG,
1205          "Client %s does no t have a valid map for CHID %X\n",
1206          GML_2s (c), chid);
1207     return NULL;
1208   }
1209   return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1210 }
1211
1212
1213 /**
1214  * Add a channel to a client
1215  *
1216  * @param client Client.
1217  * @param chid Channel ID.
1218  * @param ch Channel.
1219  */
1220 void
1221 GML_channel_add (struct CadetClient *client,
1222                  uint32_t chid,
1223                  struct CadetChannel *ch)
1224 {
1225   if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1226     GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1227                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1228   else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1229     GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1230                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1231   else
1232     GNUNET_break (0);
1233 }
1234
1235
1236 /**
1237  * Remove a channel from a client.
1238  *
1239  * @param client Client.
1240  * @param chid Channel ID.
1241  * @param ch Channel.
1242  */
1243 void
1244 GML_channel_remove (struct CadetClient *client,
1245                     uint32_t chid,
1246                     struct CadetChannel *ch)
1247 {
1248   if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1249     GNUNET_break (GNUNET_YES ==
1250                   GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1251                                                           chid, ch));
1252   else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1253     GNUNET_break (GNUNET_YES ==
1254                   GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1255                                                           chid, ch));
1256   else
1257     GNUNET_break (0);
1258 }
1259
1260
1261 /**
1262  * Get the tunnel's next free local channel ID.
1263  *
1264  * @param c Client.
1265  *
1266  * @return LID of a channel free to use.
1267  */
1268 CADET_ChannelNumber
1269 GML_get_next_chid (struct CadetClient *c)
1270 {
1271   CADET_ChannelNumber chid;
1272
1273   while (NULL != GML_channel_get (c, c->next_chid))
1274   {
1275     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1276     c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1277   }
1278   chid = c->next_chid;
1279   c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1280
1281   return chid;
1282 }
1283
1284
1285 /**
1286  * Check if client has registered with the service and has not disconnected
1287  *
1288  * @param client the client to check
1289  *
1290  * @return non-NULL if client exists in the global DLL
1291  */
1292 struct CadetClient *
1293 GML_client_get (struct GNUNET_SERVER_Client *client)
1294 {
1295   return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1296 }
1297
1298 /**
1299  * Find a client that has opened a port
1300  *
1301  * @param port Port to check.
1302  *
1303  * @return non-NULL if a client has the port.
1304  */
1305 struct CadetClient *
1306 GML_client_get_by_port (uint32_t port)
1307 {
1308   return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1309 }
1310
1311
1312 /**
1313  * Deletes a channel from a client (either owner or destination).
1314  *
1315  * @param c Client whose tunnel to delete.
1316  * @param ch Channel which should be deleted.
1317  * @param id Channel ID.
1318  */
1319 void
1320 GML_client_delete_channel (struct CadetClient *c,
1321                            struct CadetChannel *ch,
1322                            CADET_ChannelNumber id)
1323 {
1324   int res;
1325
1326   if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1327   {
1328     res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1329                                                   id, ch);
1330     if (GNUNET_YES != res)
1331       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1332   }
1333   else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1334   {
1335     res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1336                                                   id, ch);
1337     if (GNUNET_YES != res)
1338       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1339   }
1340   else
1341   {
1342     GNUNET_break (0);
1343   }
1344 }
1345
1346 /**
1347  * Build a local ACK message and send it to a local client, if needed.
1348  *
1349  * If the client was already allowed to send data, do nothing.
1350  *
1351  * @param c Client to whom send the ACK.
1352  * @param id Channel ID to use
1353  */
1354 void
1355 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1356 {
1357   struct GNUNET_CADET_LocalAck msg;
1358
1359   LOG (GNUNET_ERROR_TYPE_DEBUG,
1360               "send local %s ack on %X towards %p\n",
1361               id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1362
1363   msg.header.size = htons (sizeof (msg));
1364   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1365   msg.channel_id = htonl (id);
1366   GNUNET_SERVER_notification_context_unicast (nc,
1367                                               c->handle,
1368                                               &msg.header,
1369                                               GNUNET_NO);
1370
1371 }
1372
1373
1374
1375 /**
1376  * Notify the client that a new incoming channel was created.
1377  *
1378  * @param c Client to notify.
1379  * @param id Channel ID.
1380  * @param port Channel's destination port.
1381  * @param opt Options (bit array).
1382  * @param peer Origin peer.
1383  */
1384 void
1385 GML_send_channel_create (struct CadetClient *c,
1386                          uint32_t id, uint32_t port, uint32_t opt,
1387                          const struct GNUNET_PeerIdentity *peer)
1388 {
1389   struct GNUNET_CADET_ChannelMessage msg;
1390
1391   msg.header.size = htons (sizeof (msg));
1392   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1393   msg.channel_id = htonl (id);
1394   msg.port = htonl (port);
1395   msg.opt = htonl (opt);
1396   msg.peer = *peer;
1397   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1398                                               &msg.header, GNUNET_NO);
1399 }
1400
1401
1402 /**
1403  * Build a local channel NACK message and send it to a local client.
1404  *
1405  * @param c Client to whom send the NACK.
1406  * @param id Channel ID to use
1407  */
1408 void
1409 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1410 {
1411   struct GNUNET_CADET_LocalAck msg;
1412
1413   LOG (GNUNET_ERROR_TYPE_DEBUG,
1414        "send local nack on %X towards %p\n",
1415        id, c);
1416
1417   msg.header.size = htons (sizeof (msg));
1418   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1419   msg.channel_id = htonl (id);
1420   GNUNET_SERVER_notification_context_unicast (nc,
1421                                               c->handle,
1422                                               &msg.header,
1423                                               GNUNET_NO);
1424
1425 }
1426
1427 /**
1428  * Notify a client that a channel is no longer valid.
1429  *
1430  * @param c Client.
1431  * @param id ID of the channel that is destroyed.
1432  */
1433 void
1434 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1435 {
1436   struct GNUNET_CADET_ChannelMessage msg;
1437
1438   if (NULL == c)
1439   {
1440     GNUNET_break (0);
1441     return;
1442   }
1443   if (GNUNET_YES == c->shutting_down)
1444     return;
1445   msg.header.size = htons (sizeof (msg));
1446   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1447   msg.channel_id = htonl (id);
1448   msg.port = htonl (0);
1449   memset (&msg.peer, 0, sizeof (msg.peer));
1450   msg.opt = htonl (0);
1451   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1452                                               &msg.header, GNUNET_NO);
1453 }
1454
1455
1456 /**
1457  * Modify the cadet message ID from global to local and send to client.
1458  *
1459  * @param c Client to send to.
1460  * @param msg Message to modify and send.
1461  * @param id Channel ID to use (c can be both owner and client).
1462  */
1463 void
1464 GML_send_data (struct CadetClient *c,
1465                const struct GNUNET_CADET_Data *msg,
1466                CADET_ChannelNumber id)
1467 {
1468   struct GNUNET_CADET_LocalData *copy;
1469   uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1470   char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1471
1472   if (size < sizeof (struct GNUNET_MessageHeader))
1473   {
1474     GNUNET_break_op (0);
1475     return;
1476   }
1477   if (NULL == c)
1478   {
1479     GNUNET_break (0);
1480     return;
1481   }
1482   copy = (struct GNUNET_CADET_LocalData *) cbuf;
1483   memcpy (&copy[1], &msg[1], size);
1484   copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1485   copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1486   copy->id = htonl (id);
1487   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1488                                               &copy->header, GNUNET_NO);
1489 }
1490
1491
1492 /**
1493  * Get the static string to represent a client.
1494  *
1495  * @param c Client.
1496  *
1497  * @return Static string for the client.
1498  */
1499 const char *
1500 GML_2s (const struct CadetClient *c)
1501 {
1502   static char buf[32];
1503
1504   SPRINTF (buf, "%u", c->id);
1505   return buf;
1506 }