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