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