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