- more message size checks
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_local.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet_statistics_service.h"
26
27 #include "cadet.h"
28 #include "cadet_protocol.h" /* GNUNET_CADET_Data is shared */
29
30 #include "gnunet-service-cadet_local.h"
31 #include "gnunet-service-cadet_channel.h"
32
33 /* INFO DEBUG */
34 #include "gnunet-service-cadet_tunnel.h"
35 #include "gnunet-service-cadet_peer.h"
36
37 #define LOG(level, ...) GNUNET_log_from(level,"cadet-loc",__VA_ARGS__)
38
39 /******************************************************************************/
40 /********************************   STRUCTS  **********************************/
41 /******************************************************************************/
42
43 /**
44  * Struct containing information about a client of the service
45  *
46  * TODO: add a list of 'waiting' ports
47  */
48 struct CadetClient
49 {
50     /**
51      * Linked list next
52      */
53   struct CadetClient *next;
54
55     /**
56      * Linked list prev
57      */
58   struct CadetClient *prev;
59
60     /**
61      * Tunnels that belong to this client, indexed by local id
62      */
63   struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
64
65     /**
66      * Tunnels this client has accepted, indexed by incoming local id
67      */
68   struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
69
70     /**
71      * Channel ID for the next incoming channel.
72      */
73   CADET_ChannelNumber next_chid;
74
75     /**
76      * Handle to communicate with the client
77      */
78   struct GNUNET_SERVER_Client *handle;
79
80     /**
81      * Ports that this client has declared interest in.
82      * Indexed by port, contains *Client.
83      */
84   struct GNUNET_CONTAINER_MultiHashMap32 *ports;
85
86     /**
87      * Whether the client is active or shutting down (don't send confirmations
88      * to a client that is shutting down.
89      */
90   int shutting_down;
91
92     /**
93      * ID of the client, mainly for debug messages
94      */
95   unsigned int id;
96 };
97
98 /******************************************************************************/
99 /*******************************   GLOBALS  ***********************************/
100 /******************************************************************************/
101
102 /**
103  * Global handle to the statistics service.
104  */
105 extern struct GNUNET_STATISTICS_Handle *stats;
106
107 /**
108  * Handle to server lib.
109  */
110 static struct GNUNET_SERVER_Handle *server_handle;
111
112 /**
113  * DLL with all the clients, head.
114  */
115 static struct CadetClient *clients_head;
116
117 /**
118  * DLL with all the clients, tail.
119  */
120 static struct CadetClient *clients_tail;
121
122 /**
123  * Next ID to assign to a client.
124  */
125 unsigned int next_client_id;
126
127 /**
128  * All ports clients of this peer have opened.
129  */
130 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
131
132 /**
133  * Notification context, to send messages to local clients.
134  */
135 static struct GNUNET_SERVER_NotificationContext *nc;
136
137
138 /******************************************************************************/
139 /********************************   STATIC  ***********************************/
140 /******************************************************************************/
141
142 /**
143  * Remove client's ports from the global hashmap on disconnect.
144  *
145  * @param cls Closure (unused).
146  * @param key Port.
147  * @param value Client structure.
148  *
149  * @return GNUNET_OK, keep iterating.
150  */
151 static int
152 client_release_ports (void *cls,
153                       uint32_t key,
154                       void *value)
155 {
156   int res;
157
158   res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
159   if (GNUNET_YES != res)
160   {
161     GNUNET_break (0);
162     LOG (GNUNET_ERROR_TYPE_WARNING,
163                 "Port %u by client %p was not registered.\n",
164                 key, value);
165   }
166   return GNUNET_OK;
167 }
168
169
170
171 /******************************************************************************/
172 /********************************  HANDLES  ***********************************/
173 /******************************************************************************/
174
175
176 /**
177  * Handler for client connection.
178  *
179  * @param cls Closure (unused).
180  * @param client Client handler.
181  */
182 static void
183 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
184 {
185   struct CadetClient *c;
186
187   LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
188   if (NULL == client)
189     return;
190   c = GNUNET_new (struct CadetClient);
191   c->handle = client;
192   c->id = next_client_id++; /* overflow not important: just for debug */
193   c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
194   GNUNET_SERVER_client_keep (client);
195   GNUNET_SERVER_client_set_user_context (client, c);
196   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
197 }
198
199
200 /**
201  * Iterator for deleting each channel whose client endpoint disconnected.
202  *
203  * @param cls Closure (client that has disconnected).
204  * @param key The local channel id (used to access the hashmap).
205  * @param value The value stored at the key (channel to destroy).
206  *
207  * @return GNUNET_OK, keep iterating.
208  */
209 static int
210 channel_destroy_iterator (void *cls,
211                           uint32_t key,
212                           void *value)
213 {
214   struct CadetChannel *ch = value;
215   struct CadetClient *c = cls;
216
217   LOG (GNUNET_ERROR_TYPE_DEBUG,
218               " Channel %s destroy, due to client %s shutdown.\n",
219               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, "  by client %u\n", c->id);
527
528   /* Channel exists? */
529   fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
530   ch = GML_channel_get (c, chid);
531   if (NULL == ch)
532   {
533     GNUNET_STATISTICS_update (stats,
534                               "# client data messages on unknown channel",
535                               1, GNUNET_NO);
536     GNUNET_SERVER_receive_done (client, GNUNET_OK);
537     return;
538   }
539
540   if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
541   {
542     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543     return;
544   }
545
546   LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
547   GNUNET_SERVER_receive_done (client, GNUNET_OK);
548
549   return;
550 }
551
552
553 /**
554  * Handler for client's ACKs for payload traffic.
555  *
556  * @param cls Closure (unused).
557  * @param client Identification of the client.
558  * @param message The actual message.
559  */
560 static void
561 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
562             const struct GNUNET_MessageHeader *message)
563 {
564   struct GNUNET_CADET_LocalAck *msg;
565   struct CadetChannel *ch;
566   struct CadetClient *c;
567   CADET_ChannelNumber chid;
568   int fwd;
569
570   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
571   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
572
573   /* Sanity check for client registration */
574   if (NULL == (c = GML_client_get (client)))
575   {
576     GNUNET_break (0);
577     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578     return;
579   }
580   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
581
582   msg = (struct GNUNET_CADET_LocalAck *) message;
583
584   /* Channel exists? */
585   chid = ntohl (msg->channel_id);
586   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
587   ch = GML_channel_get (c, chid);
588   LOG (GNUNET_ERROR_TYPE_DEBUG, "   -- ch %p\n", ch);
589   if (NULL == ch)
590   {
591     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
592     LOG (GNUNET_ERROR_TYPE_DEBUG, "  for client %u.\n", c->id);
593     GNUNET_STATISTICS_update (stats,
594                               "# client ack messages on unknown channel",
595                               1, GNUNET_NO);
596     GNUNET_SERVER_receive_done (client, GNUNET_OK);
597     return;
598   }
599
600   /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
601   /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
602   fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
603
604   GCCH_handle_local_ack (ch, fwd);
605   GNUNET_SERVER_receive_done (client, GNUNET_OK);
606
607   return;
608 }
609
610
611 /**
612  * Iterator over all peers to send a monitoring client info about each peer.
613  *
614  * @param cls Closure ().
615  * @param peer Peer ID (tunnel remote peer).
616  * @param value Peer info.
617  *
618  * @return #GNUNET_YES, to keep iterating.
619  */
620 static int
621 get_all_peers_iterator (void *cls,
622                         const struct GNUNET_PeerIdentity * peer,
623                         void *value)
624 {
625   struct GNUNET_SERVER_Client *client = cls;
626   struct CadetPeer *p = value;
627   struct GNUNET_CADET_LocalInfoPeer msg;
628
629   msg.header.size = htons (sizeof (msg));
630   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
631   msg.destination = *peer;
632   msg.paths = htons (GCP_count_paths (p));
633   msg.tunnel = htons (NULL != GCP_get_tunnel (p));
634
635   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
636        GNUNET_i2s (peer));
637
638   GNUNET_SERVER_notification_context_unicast (nc, client,
639                                               &msg.header, GNUNET_NO);
640   return GNUNET_YES;
641 }
642
643
644 /**
645  * Iterator over all peers to dump info for each peer.
646  *
647  * @param cls Closure (unused).
648  * @param peer Peer ID (tunnel remote peer).
649  * @param value Peer info.
650  *
651  * @return #GNUNET_YES, to keep iterating.
652  */
653 static int
654 show_peer_iterator (void *cls,
655                         const struct GNUNET_PeerIdentity * peer,
656                         void *value)
657 {
658   struct CadetPeer *p = value;
659   struct CadetTunnel *t;
660
661   GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
662
663   t = GCP_get_tunnel (p);
664   if (NULL != t)
665     GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
666
667   LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
668
669   return GNUNET_YES;
670 }
671
672
673 /**
674  * Handler for client's INFO PEERS request.
675  *
676  * @param cls Closure (unused).
677  * @param client Identification of the client.
678  * @param message The actual message.
679  */
680 static void
681 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
682                     const struct GNUNET_MessageHeader *message)
683 {
684   struct CadetClient *c;
685   struct GNUNET_MessageHeader reply;
686
687   /* Sanity check for client registration */
688   if (NULL == (c = GML_client_get (client)))
689   {
690     GNUNET_break (0);
691     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
692     return;
693   }
694
695   LOG (GNUNET_ERROR_TYPE_DEBUG,
696        "Received get peers request from client %u (%p)\n",
697        c->id, client);
698
699   GCP_iterate_all (get_all_peers_iterator, client);
700   reply.size = htons (sizeof (reply));
701   reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
702   GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
703
704   LOG (GNUNET_ERROR_TYPE_DEBUG,
705        "Get peers request from client %u completed\n", c->id);
706   GNUNET_SERVER_receive_done (client, GNUNET_OK);
707 }
708
709
710 /**
711  * Handler for client's SHOW_PEER request.
712  *
713  * @param cls Closure (unused).
714  * @param client Identification of the client.
715  * @param message The actual message.
716  */
717 void
718 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
719                   const struct GNUNET_MessageHeader *message)
720 {
721   const struct GNUNET_CADET_LocalInfo *msg;
722   struct GNUNET_CADET_LocalInfoPeer *resp;
723   struct CadetPeer *p;
724   struct CadetClient *c;
725   size_t size;
726
727   /* Sanity check for client registration */
728   if (NULL == (c = GML_client_get (client)))
729   {
730     GNUNET_break (0);
731     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
732     return;
733   }
734
735   msg = (struct GNUNET_CADET_LocalInfo *) message;
736   LOG (GNUNET_ERROR_TYPE_INFO,
737        "Received peer info request from client %u for peer %s\n",
738        c->id, GNUNET_i2s_full (&msg->peer));
739
740   p = GCP_get (&msg->peer);
741   if (NULL == p)
742   {
743     /* We don't know the peer */
744     struct GNUNET_CADET_LocalInfoPeer warn;
745
746     LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown %u\n",
747          GNUNET_i2s_full (&msg->peer), sizeof (warn));
748     warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
749     warn.header.size = htons (sizeof (warn));
750     warn.destination = msg->peer;
751     warn.paths = htons (0);
752     warn.tunnel = htons (NULL != GCP_get_tunnel (p));
753
754     GNUNET_SERVER_notification_context_unicast (nc, client,
755                                                 &warn.header,
756                                                 GNUNET_NO);
757     GNUNET_SERVER_receive_done (client, GNUNET_OK);
758     return;
759   }
760
761   size = sizeof (struct GNUNET_CADET_LocalInfoPeer);
762 //   size += c_n * sizeof (struct GNUNET_CADET_Hash);
763
764   resp = GNUNET_malloc (size);
765   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
766   resp->header.size = htons (size);
767   resp->destination = msg->peer;
768   resp->paths = htons (0);
769   resp->tunnel = htons (0);
770
771   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
772                                               &resp->header, GNUNET_NO);
773   GNUNET_free (resp);
774
775   LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
776   GNUNET_SERVER_receive_done (client, GNUNET_OK);
777 }
778
779
780 /**
781  * Iterator over all tunnels to send a monitoring client info about each tunnel.
782  *
783  * @param cls Closure ().
784  * @param peer Peer ID (tunnel remote peer).
785  * @param value Tunnel info.
786  *
787  * @return #GNUNET_YES, to keep iterating.
788  */
789 static int
790 get_all_tunnels_iterator (void *cls,
791                           const struct GNUNET_PeerIdentity * peer,
792                           void *value)
793 {
794   struct GNUNET_SERVER_Client *client = cls;
795   struct CadetTunnel *t = value;
796   struct GNUNET_CADET_LocalInfoTunnel msg;
797
798   msg.header.size = htons (sizeof (msg));
799   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
800   msg.destination = *peer;
801   msg.channels = htonl (GCT_count_channels (t));
802   msg.connections = htonl (GCT_count_any_connections (t));
803   msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
804   msg.estate = htons ((uint16_t) GCT_get_estate (t));
805
806   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
807        GNUNET_i2s (peer));
808
809   GNUNET_SERVER_notification_context_unicast (nc, client,
810                                               &msg.header, GNUNET_NO);
811   return GNUNET_YES;
812 }
813
814
815 /**
816  * Handler for client's INFO TUNNELS request.
817  *
818  * @param cls Closure (unused).
819  * @param client Identification of the client.
820  * @param message The actual message.
821  */
822 static void
823 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
824                     const struct GNUNET_MessageHeader *message)
825 {
826   struct CadetClient *c;
827   struct GNUNET_MessageHeader reply;
828
829   /* Sanity check for client registration */
830   if (NULL == (c = GML_client_get (client)))
831   {
832     GNUNET_break (0);
833     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
834     return;
835   }
836
837   LOG (GNUNET_ERROR_TYPE_DEBUG,
838        "Received get tunnels request from client %u (%p)\n",
839        c->id, client);
840
841   GCT_iterate_all (get_all_tunnels_iterator, client);
842   reply.size = htons (sizeof (reply));
843   reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
844   GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
845
846   LOG (GNUNET_ERROR_TYPE_DEBUG,
847        "Get tunnels request from client %u completed\n", c->id);
848   GNUNET_SERVER_receive_done (client, GNUNET_OK);
849 }
850
851
852 static void
853 iter_connection (void *cls, struct CadetConnection *c)
854 {
855   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
856   struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
857
858   h[msg->connections] = *(GCC_get_id (c));
859   msg->connections++;
860 }
861
862 static void
863 iter_channel (void *cls, struct CadetChannel *ch)
864 {
865   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
866   struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
867   CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
868
869   chn[msg->channels] = GCCH_get_id (ch);
870   msg->channels++;
871 }
872
873
874 /**
875  * Handler for client's SHOW_TUNNEL request.
876  *
877  * @param cls Closure (unused).
878  * @param client Identification of the client.
879  * @param message The actual message.
880  */
881 void
882 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
883                     const struct GNUNET_MessageHeader *message)
884 {
885   const struct GNUNET_CADET_LocalInfo *msg;
886   struct GNUNET_CADET_LocalInfoTunnel *resp;
887   struct CadetClient *c;
888   struct CadetTunnel *t;
889   unsigned int ch_n;
890   unsigned int c_n;
891   size_t size;
892
893   /* Sanity check for client registration */
894   if (NULL == (c = GML_client_get (client)))
895   {
896     GNUNET_break (0);
897     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
898     return;
899   }
900
901   msg = (struct GNUNET_CADET_LocalInfo *) message;
902   LOG (GNUNET_ERROR_TYPE_INFO,
903        "Received tunnel info request from client %u for tunnel %s\n",
904        c->id, GNUNET_i2s_full(&msg->peer));
905
906   t = GCP_get_tunnel (GCP_get (&msg->peer));
907   if (NULL == t)
908   {
909     /* We don't know the tunnel */
910     struct GNUNET_CADET_LocalInfoTunnel warn;
911
912     LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
913          GNUNET_i2s_full(&msg->peer), sizeof (warn));
914     warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
915     warn.header.size = htons (sizeof (warn));
916     warn.destination = msg->peer;
917     warn.channels = htonl (0);
918     warn.connections = htonl (0);
919     warn.cstate = htons (0);
920     warn.estate = htons (0);
921
922     GNUNET_SERVER_notification_context_unicast (nc, client,
923                                                 &warn.header,
924                                                 GNUNET_NO);
925     GNUNET_SERVER_receive_done (client, GNUNET_OK);
926     return;
927   }
928
929   /* Initialize context */
930   ch_n = GCT_count_channels (t);
931   c_n = GCT_count_any_connections (t);
932
933   size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
934   size += c_n * sizeof (struct GNUNET_CADET_Hash);
935   size += ch_n * sizeof (CADET_ChannelNumber);
936
937   resp = GNUNET_malloc (size);
938   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
939   resp->header.size = htons (size);
940   GCT_iterate_connections (t, &iter_connection, resp);
941   GCT_iterate_channels (t, &iter_channel, resp);
942   /* Do not interleave with iterators, iter_channel needs conn in HBO */
943   resp->destination = msg->peer;
944   resp->connections = htonl (resp->connections);
945   resp->channels = htonl (resp->channels);
946   resp->cstate = htons (GCT_get_cstate (t));
947   resp->estate = htons (GCT_get_estate (t));
948   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
949                                               &resp->header, GNUNET_NO);
950   GNUNET_free (resp);
951
952   LOG (GNUNET_ERROR_TYPE_INFO,
953        "Show tunnel request from client %u completed. %u conn, %u ch\n",
954        c->id, c_n, ch_n);
955   GNUNET_SERVER_receive_done (client, GNUNET_OK);
956 }
957
958
959 /**
960  * Handler for client's INFO_DUMP request.
961  *
962  * @param cls Closure (unused).
963  * @param client Identification of the client.
964  * @param message The actual message.
965  */
966 void
967 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
968                   const struct GNUNET_MessageHeader *message)
969 {
970   struct CadetClient *c;
971
972   /* Sanity check for client registration */
973   if (NULL == (c = GML_client_get (client)))
974   {
975     GNUNET_break (0);
976     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
977     return;
978   }
979
980   LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
981        c->id);
982
983   LOG (GNUNET_ERROR_TYPE_ERROR,
984        "*************************** DUMP START ***************************\n");
985
986   GCP_iterate_all (&show_peer_iterator, NULL);
987
988   LOG (GNUNET_ERROR_TYPE_ERROR,
989        "**************************** DUMP END ****************************\n");
990
991   GNUNET_SERVER_receive_done (client, GNUNET_OK);
992 }
993
994
995 /**
996  * Functions to handle messages from clients
997  */
998 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
999   {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1000   {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1001    sizeof (struct GNUNET_CADET_ChannelMessage)},
1002   {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1003    sizeof (struct GNUNET_CADET_ChannelMessage)},
1004   {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1005   {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1006    sizeof (struct GNUNET_CADET_LocalAck)},
1007   {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1008    sizeof (struct GNUNET_MessageHeader)},
1009   {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1010    sizeof (struct GNUNET_CADET_LocalInfo)},
1011   {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1012    sizeof (struct GNUNET_MessageHeader)},
1013   {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1014    sizeof (struct GNUNET_CADET_LocalInfo)},
1015   {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1016    sizeof (struct GNUNET_MessageHeader)},
1017   {NULL, NULL, 0, 0}
1018 };
1019
1020
1021
1022 /******************************************************************************/
1023 /********************************    API    ***********************************/
1024 /******************************************************************************/
1025
1026 /**
1027  * Initialize server subsystem.
1028  *
1029  * @param handle Server handle.
1030  */
1031 void
1032 GML_init (struct GNUNET_SERVER_Handle *handle)
1033 {
1034   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1035   server_handle = handle;
1036   GNUNET_SERVER_suspend (server_handle);
1037   ports = GNUNET_CONTAINER_multihashmap32_create (32);
1038 }
1039
1040
1041 /**
1042  * Install server (service) handlers and start listening to clients.
1043  */
1044 void
1045 GML_start (void)
1046 {
1047   GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1048   GNUNET_SERVER_connect_notify (server_handle,  &handle_client_connect, NULL);
1049   GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1050                                    NULL);
1051   nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1052
1053   clients_head = NULL;
1054   clients_tail = NULL;
1055   next_client_id = 0;
1056   GNUNET_SERVER_resume (server_handle);
1057 }
1058
1059
1060 /**
1061  * Shutdown server.
1062  */
1063 void
1064 GML_shutdown (void)
1065 {
1066   if (nc != NULL)
1067   {
1068     GNUNET_SERVER_notification_context_destroy (nc);
1069     nc = NULL;
1070   }
1071 }
1072
1073
1074 /**
1075  * Get a channel from a client.
1076  *
1077  * @param c Client to check.
1078  * @param chid Channel ID, must be local (> 0x800...).
1079  *
1080  * @return non-NULL if channel exists in the clients lists
1081  */
1082 struct CadetChannel *
1083 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1084 {
1085   struct GNUNET_CONTAINER_MultiHashMap32 *map;
1086
1087   if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1088   {
1089     GNUNET_break_op (0);
1090     LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1091     return NULL;
1092   }
1093
1094   if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1095     map = c->incoming_channels;
1096   else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1097     map = c->own_channels;
1098   else
1099   {
1100     GNUNET_break (0);
1101     map = NULL;
1102   }
1103   if (NULL == map)
1104   {
1105     GNUNET_break (0);
1106     LOG (GNUNET_ERROR_TYPE_DEBUG,
1107          "Client %s does no t have a valid map for CHID %X\n",
1108          GML_2s (c), chid);
1109     return NULL;
1110   }
1111   return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1112 }
1113
1114
1115 /**
1116  * Add a channel to a client
1117  *
1118  * @param client Client.
1119  * @param chid Channel ID.
1120  * @param ch Channel.
1121  */
1122 void
1123 GML_channel_add (struct CadetClient *client,
1124                  uint32_t chid,
1125                  struct CadetChannel *ch)
1126 {
1127   if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1128     GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1129                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1130   else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1131     GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1132                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1133   else
1134     GNUNET_break (0);
1135 }
1136
1137
1138 /**
1139  * Remove a channel from a client.
1140  *
1141  * @param client Client.
1142  * @param chid Channel ID.
1143  * @param ch Channel.
1144  */
1145 void
1146 GML_channel_remove (struct CadetClient *client,
1147                     uint32_t chid,
1148                     struct CadetChannel *ch)
1149 {
1150   if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1151     GNUNET_break (GNUNET_YES ==
1152                   GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1153                                                           chid, ch));
1154   else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1155     GNUNET_break (GNUNET_YES ==
1156                   GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1157                                                           chid, ch));
1158   else
1159     GNUNET_break (0);
1160 }
1161
1162
1163 /**
1164  * Get the tunnel's next free local channel ID.
1165  *
1166  * @param c Client.
1167  *
1168  * @return LID of a channel free to use.
1169  */
1170 CADET_ChannelNumber
1171 GML_get_next_chid (struct CadetClient *c)
1172 {
1173   CADET_ChannelNumber chid;
1174
1175   while (NULL != GML_channel_get (c, c->next_chid))
1176   {
1177     LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1178     c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1179   }
1180   chid = c->next_chid;
1181   c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1182
1183   return chid;
1184 }
1185
1186
1187 /**
1188  * Check if client has registered with the service and has not disconnected
1189  *
1190  * @param client the client to check
1191  *
1192  * @return non-NULL if client exists in the global DLL
1193  */
1194 struct CadetClient *
1195 GML_client_get (struct GNUNET_SERVER_Client *client)
1196 {
1197   return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1198 }
1199
1200 /**
1201  * Find a client that has opened a port
1202  *
1203  * @param port Port to check.
1204  *
1205  * @return non-NULL if a client has the port.
1206  */
1207 struct CadetClient *
1208 GML_client_get_by_port (uint32_t port)
1209 {
1210   return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1211 }
1212
1213
1214 /**
1215  * Deletes a channel from a client (either owner or destination).
1216  *
1217  * @param c Client whose tunnel to delete.
1218  * @param ch Channel which should be deleted.
1219  * @param id Channel ID.
1220  */
1221 void
1222 GML_client_delete_channel (struct CadetClient *c,
1223                            struct CadetChannel *ch,
1224                            CADET_ChannelNumber id)
1225 {
1226   int res;
1227
1228   if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1229   {
1230     res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1231                                                   id, ch);
1232     if (GNUNET_YES != res)
1233       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1234   }
1235   else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1236   {
1237     res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1238                                                   id, ch);
1239     if (GNUNET_YES != res)
1240       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1241   }
1242   else
1243   {
1244     GNUNET_break (0);
1245   }
1246 }
1247
1248 /**
1249  * Build a local ACK message and send it to a local client, if needed.
1250  *
1251  * If the client was already allowed to send data, do nothing.
1252  *
1253  * @param c Client to whom send the ACK.
1254  * @param id Channel ID to use
1255  */
1256 void
1257 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1258 {
1259   struct GNUNET_CADET_LocalAck msg;
1260
1261   LOG (GNUNET_ERROR_TYPE_DEBUG,
1262               "send local %s ack on %X towards %p\n",
1263               id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1264
1265   msg.header.size = htons (sizeof (msg));
1266   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1267   msg.channel_id = htonl (id);
1268   GNUNET_SERVER_notification_context_unicast (nc,
1269                                               c->handle,
1270                                               &msg.header,
1271                                               GNUNET_NO);
1272
1273 }
1274
1275
1276
1277 /**
1278  * Notify the client that a new incoming channel was created.
1279  *
1280  * @param c Client to notify.
1281  * @param id Channel ID.
1282  * @param port Channel's destination port.
1283  * @param opt Options (bit array).
1284  * @param peer Origin peer.
1285  */
1286 void
1287 GML_send_channel_create (struct CadetClient *c,
1288                          uint32_t id, uint32_t port, uint32_t opt,
1289                          const struct GNUNET_PeerIdentity *peer)
1290 {
1291   struct GNUNET_CADET_ChannelMessage msg;
1292
1293   msg.header.size = htons (sizeof (msg));
1294   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1295   msg.channel_id = htonl (id);
1296   msg.port = htonl (port);
1297   msg.opt = htonl (opt);
1298   msg.peer = *peer;
1299   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1300                                               &msg.header, GNUNET_NO);
1301 }
1302
1303
1304 /**
1305  * Build a local channel NACK message and send it to a local client.
1306  *
1307  * @param c Client to whom send the NACK.
1308  * @param id Channel ID to use
1309  */
1310 void
1311 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1312 {
1313   struct GNUNET_CADET_LocalAck msg;
1314
1315   LOG (GNUNET_ERROR_TYPE_DEBUG,
1316        "send local nack on %X towards %p\n",
1317        id, c);
1318
1319   msg.header.size = htons (sizeof (msg));
1320   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1321   msg.channel_id = htonl (id);
1322   GNUNET_SERVER_notification_context_unicast (nc,
1323                                               c->handle,
1324                                               &msg.header,
1325                                               GNUNET_NO);
1326
1327 }
1328
1329 /**
1330  * Notify a client that a channel is no longer valid.
1331  *
1332  * @param c Client.
1333  * @param id ID of the channel that is destroyed.
1334  */
1335 void
1336 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1337 {
1338   struct GNUNET_CADET_ChannelMessage msg;
1339
1340   if (NULL == c)
1341   {
1342     GNUNET_break (0);
1343     return;
1344   }
1345   if (GNUNET_YES == c->shutting_down)
1346     return;
1347   msg.header.size = htons (sizeof (msg));
1348   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1349   msg.channel_id = htonl (id);
1350   msg.port = htonl (0);
1351   memset (&msg.peer, 0, sizeof (msg.peer));
1352   msg.opt = htonl (0);
1353   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1354                                               &msg.header, GNUNET_NO);
1355 }
1356
1357
1358 /**
1359  * Modify the cadet message ID from global to local and send to client.
1360  *
1361  * @param c Client to send to.
1362  * @param msg Message to modify and send.
1363  * @param id Channel ID to use (c can be both owner and client).
1364  */
1365 void
1366 GML_send_data (struct CadetClient *c,
1367                const struct GNUNET_CADET_Data *msg,
1368                CADET_ChannelNumber id)
1369 {
1370   struct GNUNET_CADET_LocalData *copy;
1371   uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1372   char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1373
1374   if (size < sizeof (struct GNUNET_MessageHeader))
1375   {
1376     GNUNET_break_op (0);
1377     return;
1378   }
1379   if (NULL == c)
1380   {
1381     GNUNET_break (0);
1382     return;
1383   }
1384   copy = (struct GNUNET_CADET_LocalData *) cbuf;
1385   memcpy (&copy[1], &msg[1], size);
1386   copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1387   copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1388   copy->id = htonl (id);
1389   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1390                                               &copy->header, GNUNET_NO);
1391 }
1392
1393
1394 /**
1395  * Get the static string to represent a client.
1396  *
1397  * @param c Client.
1398  *
1399  * @return Static string for the client.
1400  */
1401 const char *
1402 GML_2s (const struct CadetClient *c)
1403 {
1404   static char buf[32];
1405
1406   SPRINTF (buf, "%u", c->id);
1407   return buf;
1408 }