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