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