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