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