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