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