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