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