glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013, 2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file cadet/gnunet-service-cadet.c
18  * @brief GNUnet CADET service with encryption
19  * @author Bartlomiej Polot
20  * @author Christian Grothoff
21  *
22  * Dictionary:
23  * - peer: other cadet instance. If there is direct connection it's a neighbor.
24  * - path: series of directly connected peer from one peer to another.
25  * - connection: path which is being used in a tunnel.
26  * - tunnel: encrypted connection to a peer, neighbor or not.
27  * - channel: logical link between two clients, on the same or different peers.
28  *            have properties like reliability.
29  */
30
31 #include "platform.h"
32 #include "gnunet_util_lib.h"
33 #include "cadet.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet-service-cadet.h"
36 #include "gnunet-service-cadet_channel.h"
37 #include "gnunet-service-cadet_connection.h"
38 #include "gnunet-service-cadet_core.h"
39 #include "gnunet-service-cadet_dht.h"
40 #include "gnunet-service-cadet_hello.h"
41 #include "gnunet-service-cadet_tunnels.h"
42 #include "gnunet-service-cadet_peer.h"
43 #include "gnunet-service-cadet_paths.h"
44
45 #define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
46
47
48 /**
49  * Struct containing information about a client of the service
50  */
51 struct CadetClient
52 {
53   /**
54    * Linked list next
55    */
56   struct CadetClient *next;
57
58   /**
59    * Linked list prev
60    */
61   struct CadetClient *prev;
62
63   /**
64    * Tunnels that belong to this client, indexed by local id,
65    * value is a `struct CadetChannel`.
66    */
67   struct GNUNET_CONTAINER_MultiHashMap32 *channels;
68
69   /**
70    * Handle to communicate with the client
71    */
72   struct GNUNET_MQ_Handle *mq;
73
74   /**
75    * Client handle.
76    */
77   struct GNUNET_SERVICE_Client *client;
78
79   /**
80    * Ports that this client has declared interest in.
81    * Indexed by port, contains `struct OpenPort`
82    */
83   struct GNUNET_CONTAINER_MultiHashMap *ports;
84
85   /**
86    * Channel ID to use for the next incoming channel for this client.
87    * Wraps around (in theory).
88    */
89   struct GNUNET_CADET_ClientChannelNumber next_ccn;
90
91   /**
92    * ID of the client, mainly for debug messages. Purely internal to this file.
93    */
94   unsigned int id;
95 };
96
97
98 /******************************************************************************/
99 /***********************      GLOBAL VARIABLES     ****************************/
100 /******************************************************************************/
101
102 /****************************** Global variables ******************************/
103
104 /**
105  * Handle to our configuration.
106  */
107 const struct GNUNET_CONFIGURATION_Handle *cfg;
108
109 /**
110  * Handle to the statistics service.
111  */
112 struct GNUNET_STATISTICS_Handle *stats;
113
114 /**
115  * Handle to communicate with ATS.
116  */
117 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
118
119 /**
120  * Local peer own ID.
121  */
122 struct GNUNET_PeerIdentity my_full_id;
123
124 /**
125  * Own private key.
126  */
127 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
128
129 /**
130  * Signal that shutdown is happening: prevent recovery measures.
131  */
132 int shutting_down;
133
134 /**
135  * DLL with all the clients, head.
136  */
137 static struct CadetClient *clients_head;
138
139 /**
140  * DLL with all the clients, tail.
141  */
142 static struct CadetClient *clients_tail;
143
144 /**
145  * Next ID to assign to a client.
146  */
147 static unsigned int next_client_id;
148
149 /**
150  * All ports clients of this peer have opened.  Maps from
151  * a hashed port to a `struct OpenPort`.
152  */
153 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
154
155 /**
156  * Map from ports to channels where the ports were closed at the
157  * time we got the inbound connection.
158  * Indexed by h_port, contains `struct CadetChannel`.
159  */
160 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
161
162 /**
163  * Map from PIDs to `struct CadetPeer` entries.
164  */
165 struct GNUNET_CONTAINER_MultiPeerMap *peers;
166
167 /**
168  * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
169  * hash codes to `struct CadetConnection` objects.
170  */
171 struct GNUNET_CONTAINER_MultiShortmap *connections;
172
173 /**
174  * How many messages are needed to trigger an AXOLOTL ratchet advance.
175  */
176 unsigned long long ratchet_messages;
177
178 /**
179  * How long until we trigger a ratched advance due to time.
180  */
181 struct GNUNET_TIME_Relative ratchet_time;
182
183 /**
184  * How frequently do we send KEEPALIVE messages on idle connections?
185  */
186 struct GNUNET_TIME_Relative keepalive_period;
187
188 /**
189  * Set to non-zero values to create random drops to test retransmissions.
190  */
191 unsigned long long drop_percent;
192
193
194 /**
195  * Send a message to a client.
196  *
197  * @param c client to get the message
198  * @param env envelope with the message
199  */
200 void
201 GSC_send_to_client (struct CadetClient *c,
202                     struct GNUNET_MQ_Envelope *env)
203 {
204   GNUNET_MQ_send (c->mq,
205                   env);
206 }
207
208
209 /**
210  * Return identifier for a client as a string.
211  *
212  * @param c client to identify
213  * @return string for debugging
214  */
215 const char *
216 GSC_2s (struct CadetClient *c)
217 {
218   static char buf[32];
219
220   GNUNET_snprintf (buf,
221                    sizeof (buf),
222                    "Client(%u)",
223                    c->id);
224   return buf;
225 }
226
227
228 /**
229  * Lookup channel of client @a c by @a ccn.
230  *
231  * @param c client to look in
232  * @param ccn channel ID to look up
233  * @return NULL if no such channel exists
234  */
235 static struct CadetChannel *
236 lookup_channel (struct CadetClient *c,
237                 struct GNUNET_CADET_ClientChannelNumber ccn)
238 {
239   return GNUNET_CONTAINER_multihashmap32_get (c->channels,
240                                               ntohl (ccn.channel_of_client));
241 }
242
243
244 /**
245  * Obtain the next LID to use for incoming connections to
246  * the given client.
247  *
248  * @param c client handle
249  */
250 static struct GNUNET_CADET_ClientChannelNumber
251 client_get_next_ccn (struct CadetClient *c)
252 {
253   struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
254
255   /* increment until we have a free one... */
256   while (NULL !=
257          lookup_channel (c,
258                          ccn))
259   {
260     ccn.channel_of_client
261       = htonl (1 + (ntohl (ccn.channel_of_client)));
262     if (ntohl (ccn.channel_of_client) >=
263         GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
264       ccn.channel_of_client = htonl (0);
265   }
266   c->next_ccn.channel_of_client
267     = htonl (1 + (ntohl (ccn.channel_of_client)));
268   return ccn;
269 }
270
271
272 /**
273  * Bind incoming channel to this client, and notify client about
274  * incoming connection.  Caller is responsible for notifying the other
275  * peer about our acceptance of the channel.
276  *
277  * @param c client to bind to
278  * @param ch channel to be bound
279  * @param dest peer that establishes the connection
280  * @param port port number
281  * @param options options
282  * @return local channel number assigned to the new client
283  */
284 struct GNUNET_CADET_ClientChannelNumber
285 GSC_bind (struct CadetClient *c,
286           struct CadetChannel *ch,
287           struct CadetPeer *dest,
288           const struct GNUNET_HashCode *port,
289           uint32_t options)
290 {
291   struct GNUNET_MQ_Envelope *env;
292   struct GNUNET_CADET_LocalChannelCreateMessage *cm;
293   struct GNUNET_CADET_ClientChannelNumber ccn;
294
295   ccn = client_get_next_ccn (c);
296   GNUNET_assert (GNUNET_YES ==
297                  GNUNET_CONTAINER_multihashmap32_put (c->channels,
298                                                       ntohl (ccn.channel_of_client),
299                                                       ch,
300                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
301   LOG (GNUNET_ERROR_TYPE_DEBUG,
302        "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n",
303        GCCH_2s (ch),
304        GCP_2s (dest),
305        GNUNET_h2s (port),
306        (uint32_t) ntohl (options),
307        (uint32_t) ntohl (ccn.channel_of_client));
308   /* notify local client about incoming connection! */
309   env = GNUNET_MQ_msg (cm,
310                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
311   cm->ccn = ccn;
312   cm->port = *port;
313   cm->opt = htonl (options);
314   cm->peer = *GCP_get_id (dest);
315   GSC_send_to_client (c,
316                       env);
317   return ccn;
318 }
319
320
321 /**
322  * Callback invoked on all peers to destroy all tunnels
323  * that may still exist.
324  *
325  * @param cls NULL
326  * @param pid identify of a peer
327  * @param value a `struct CadetPeer` that may still have a tunnel
328  * @return #GNUNET_OK (iterate over all entries)
329  */
330 static int
331 destroy_tunnels_now (void *cls,
332                      const struct GNUNET_PeerIdentity *pid,
333                      void *value)
334 {
335   struct CadetPeer *cp = value;
336   struct CadetTunnel *t = GCP_get_tunnel (cp,
337                                           GNUNET_NO);
338
339   if (NULL != t)
340     GCT_destroy_tunnel_now (t);
341   return GNUNET_OK;
342 }
343
344
345 /**
346  * Callback invoked on all peers to destroy all tunnels
347  * that may still exist.
348  *
349  * @param cls NULL
350  * @param pid identify of a peer
351  * @param value a `struct CadetPeer` that may still have a tunnel
352  * @return #GNUNET_OK (iterate over all entries)
353  */
354 static int
355 destroy_paths_now (void *cls,
356                    const struct GNUNET_PeerIdentity *pid,
357                    void *value)
358 {
359   struct CadetPeer *cp = value;
360
361   GCP_drop_owned_paths (cp);
362   return GNUNET_OK;
363 }
364
365
366 /**
367  * Shutdown everything once the clients have disconnected.
368  */
369 static void
370 shutdown_rest ()
371 {
372   if (NULL != stats)
373   {
374     GNUNET_STATISTICS_destroy (stats,
375                                GNUNET_NO);
376     stats = NULL;
377   }
378   if (NULL != open_ports)
379   {
380     GNUNET_CONTAINER_multihashmap_destroy (open_ports);
381     open_ports = NULL;
382   }
383   if (NULL != loose_channels)
384   {
385     GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
386     loose_channels = NULL;
387   }
388   /* Destroy tunnels.  Note that all channels must be destroyed first! */
389   GCP_iterate_all (&destroy_tunnels_now,
390                    NULL);
391   /* All tunnels, channels, connections and CORE must be down before this point. */
392   GCP_iterate_all (&destroy_paths_now,
393                    NULL);
394   /* All paths, tunnels, channels, connections and CORE must be down before this point. */
395   GCP_destroy_all_peers ();
396   if (NULL != peers)
397   {
398     GNUNET_CONTAINER_multipeermap_destroy (peers);
399     peers = NULL;
400   }
401   if (NULL != connections)
402   {
403     GNUNET_CONTAINER_multishortmap_destroy (connections);
404     connections = NULL;
405   }
406   if (NULL != ats_ch)
407   {
408     GNUNET_ATS_connectivity_done (ats_ch);
409     ats_ch = NULL;
410   }
411   GCD_shutdown ();
412   GCH_shutdown ();
413   GNUNET_free_non_null (my_private_key);
414   my_private_key = NULL;
415 }
416
417
418 /**
419  * Task run during shutdown.
420  *
421  * @param cls unused
422  */
423 static void
424 shutdown_task (void *cls)
425 {
426   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427               "Shutting down\n");
428   shutting_down = GNUNET_YES;
429   GCO_shutdown ();
430   if (NULL == clients_head)
431     shutdown_rest ();
432 }
433
434
435 /**
436  * We had a remote connection @a value to port @a h_port before
437  * client @a cls opened port @a port.  Bind them now.
438  *
439  * @param cls the `struct CadetClient`
440  * @param h_port the hashed port
441  * @param value the `struct CadetChannel`
442  * @return #GNUNET_YES (iterate over all such channels)
443  */
444 static int
445 bind_loose_channel (void *cls,
446                     const struct GNUNET_HashCode *port,
447                     void *value)
448 {
449   struct OpenPort *op = cls;
450   struct CadetChannel *ch = value;
451
452   GCCH_bind (ch,
453              op->c,
454              &op->port);
455   GNUNET_assert (GNUNET_YES ==
456                  GNUNET_CONTAINER_multihashmap_remove (loose_channels,
457                                                        &op->h_port,
458                                                        ch));
459   return GNUNET_YES;
460 }
461
462
463 /**
464  * Handle port open request.  Creates a mapping from the
465  * port to the respective client and checks whether we have
466  * loose channels trying to bind to the port.  If so, those
467  * are bound.
468  *
469  * @param cls Identification of the client.
470  * @param pmsg The actual message.
471  */
472 static void
473 handle_port_open (void *cls,
474                   const struct GNUNET_CADET_PortMessage *pmsg)
475 {
476   struct CadetClient *c = cls;
477   struct OpenPort *op;
478
479   LOG (GNUNET_ERROR_TYPE_DEBUG,
480        "Open port %s requested by %s\n",
481        GNUNET_h2s (&pmsg->port),
482        GSC_2s (c));
483   if (NULL == c->ports)
484     c->ports = GNUNET_CONTAINER_multihashmap_create (4,
485                                                      GNUNET_NO);
486   op = GNUNET_new (struct OpenPort);
487   op->c = c;
488   op->port = pmsg->port;
489   GCCH_hash_port (&op->h_port,
490                   &pmsg->port,
491                   &my_full_id);
492   if (GNUNET_OK !=
493       GNUNET_CONTAINER_multihashmap_put (c->ports,
494                                          &op->port,
495                                          op,
496                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
497   {
498     GNUNET_break (0);
499     GNUNET_SERVICE_client_drop (c->client);
500     return;
501   }
502   (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
503                                             &op->h_port,
504                                             op,
505                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
506   GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
507                                               &op->h_port,
508                                               &bind_loose_channel,
509                                               op);
510   GNUNET_SERVICE_client_continue (c->client);
511 }
512
513
514 /**
515  * Handler for port close requests.  Marks this port as closed
516  * (unless of course we have another client with the same port
517  * open).  Note that existing channels accepted on the port are
518  * not affected.
519  *
520  * @param cls Identification of the client.
521  * @param pmsg The actual message.
522  */
523 static void
524 handle_port_close (void *cls,
525                    const struct GNUNET_CADET_PortMessage *pmsg)
526 {
527   struct CadetClient *c = cls;
528   struct OpenPort *op;
529
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Closing port %s as requested by %s\n",
532        GNUNET_h2s (&pmsg->port),
533        GSC_2s (c));
534   if (NULL == c->ports)
535   {
536     /* Client closed a port despite _never_ having opened one? */
537     GNUNET_break (0);
538     GNUNET_SERVICE_client_drop (c->client);
539     return;
540   }
541   op = GNUNET_CONTAINER_multihashmap_get (c->ports,
542                                           &pmsg->port);
543   if (NULL == op)
544   {
545     GNUNET_break (0);
546     GNUNET_SERVICE_client_drop (c->client);
547     return;
548   }
549   GNUNET_assert (GNUNET_YES ==
550                  GNUNET_CONTAINER_multihashmap_remove (c->ports,
551                                                        &op->port,
552                                                        op));
553   GNUNET_assert (GNUNET_YES ==
554                  GNUNET_CONTAINER_multihashmap_remove (open_ports,
555                                                        &op->h_port,
556                                                        op));
557   GNUNET_free (op);
558   GNUNET_SERVICE_client_continue (c->client);
559 }
560
561
562 /**
563  * Handler for requests for us creating a new channel to another peer and port.
564  *
565  * @param cls Identification of the client.
566  * @param tcm The actual message.
567  */
568 static void
569 handle_channel_create (void *cls,
570                        const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
571 {
572   struct CadetClient *c = cls;
573   struct CadetChannel *ch;
574
575   if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
576   {
577     /* Channel ID not in allowed range. */
578     GNUNET_break (0);
579     GNUNET_SERVICE_client_drop (c->client);
580     return;
581   }
582   ch = lookup_channel (c,
583                        tcm->ccn);
584   if (NULL != ch)
585   {
586     /* Channel ID already in use. Not allowed. */
587     GNUNET_break (0);
588     GNUNET_SERVICE_client_drop (c->client);
589     return;
590   }
591   LOG (GNUNET_ERROR_TYPE_DEBUG,
592        "New channel to %s at port %s requested by %s\n",
593        GNUNET_i2s (&tcm->peer),
594        GNUNET_h2s (&tcm->port),
595        GSC_2s (c));
596
597   /* Create channel */
598   ch = GCCH_channel_local_new (c,
599                                tcm->ccn,
600                                GCP_get (&tcm->peer,
601                                         GNUNET_YES),
602                                &tcm->port,
603                                ntohl (tcm->opt));
604   if (NULL == ch)
605   {
606     GNUNET_break (0);
607     GNUNET_SERVICE_client_drop (c->client);
608     return;
609   }
610   GNUNET_assert (GNUNET_YES ==
611                  GNUNET_CONTAINER_multihashmap32_put (c->channels,
612                                                       ntohl (tcm->ccn.channel_of_client),
613                                                       ch,
614                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
615
616   GNUNET_SERVICE_client_continue (c->client);
617 }
618
619
620 /**
621  * Handler for requests of destroying an existing channel.
622  *
623  * @param cls client identification of the client
624  * @param msg the actual message
625  */
626 static void
627 handle_channel_destroy (void *cls,
628                         const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
629 {
630   struct CadetClient *c = cls;
631   struct CadetChannel *ch;
632
633   ch = lookup_channel (c,
634                        msg->ccn);
635   if (NULL == ch)
636   {
637     /* Client attempted to destroy unknown channel.
638        Can happen if the other side went down at the same time.*/
639     LOG (GNUNET_ERROR_TYPE_DEBUG,
640          "%s tried to destroy unknown channel %X\n",
641          GSC_2s(c),
642          (uint32_t) ntohl (msg->ccn.channel_of_client));
643     GNUNET_SERVICE_client_continue (c->client);
644     return;
645   }
646   LOG (GNUNET_ERROR_TYPE_DEBUG,
647        "%s is destroying %s\n",
648        GSC_2s(c),
649        GCCH_2s (ch));
650   GNUNET_assert (GNUNET_YES ==
651                  GNUNET_CONTAINER_multihashmap32_remove (c->channels,
652                                                          ntohl (msg->ccn.channel_of_client),
653                                                          ch));
654   GCCH_channel_local_destroy (ch,
655                               c,
656                               msg->ccn);
657   GNUNET_SERVICE_client_continue (c->client);
658 }
659
660
661 /**
662  * Check for client traffic data message is well-formed.
663  *
664  * @param cls identification of the client
665  * @param msg the actual message
666  * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
667  */
668 static int
669 check_local_data (void *cls,
670                   const struct GNUNET_CADET_LocalData *msg)
671 {
672   size_t payload_size;
673   size_t payload_claimed_size;
674   const char *buf;
675   struct GNUNET_MessageHeader pa;
676
677   /* FIXME: what is the format we shall allow for @a msg?
678      ONE payload item or multiple? Seems current cadet_api
679      at least in theory allows more than one. Next-gen
680      cadet_api will likely no more, so we could then
681      simplify this mess again. */
682   /* Sanity check for message size */
683   payload_size = ntohs (msg->header.size) - sizeof (*msg);
684   buf = (const char *) &msg[1];
685   while (payload_size >= sizeof (struct GNUNET_MessageHeader))
686   {
687     /* need to memcpy() for alignment */
688     GNUNET_memcpy (&pa,
689                    buf,
690                    sizeof (pa));
691     payload_claimed_size = ntohs (pa.size);
692     if ( (payload_size < payload_claimed_size) ||
693          (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
694          (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
695     {
696       GNUNET_break (0);
697       LOG (GNUNET_ERROR_TYPE_DEBUG,
698            "Local data of %u total size had sub-message %u at %u with %u bytes\n",
699            ntohs (msg->header.size),
700            ntohs (pa.type),
701            (unsigned int) (buf - (const char *) &msg[1]),
702            (unsigned int) payload_claimed_size);
703       return GNUNET_SYSERR;
704     }
705     payload_size -= payload_claimed_size;
706     buf += payload_claimed_size;
707   }
708   if (0 != payload_size)
709   {
710     GNUNET_break_op (0);
711     return GNUNET_SYSERR;
712   }
713   return GNUNET_OK;
714 }
715
716
717 /**
718  * Handler for client payload traffic to be send on a channel to
719  * another peer.
720  *
721  * @param cls identification of the client
722  * @param msg the actual message
723  */
724 static void
725 handle_local_data (void *cls,
726                    const struct GNUNET_CADET_LocalData *msg)
727 {
728   struct CadetClient *c = cls;
729   struct CadetChannel *ch;
730   size_t payload_size;
731   const char *buf;
732
733   ch = lookup_channel (c,
734                        msg->ccn);
735   if (NULL == ch)
736   {
737     /* Channel does not exist (anymore) */
738     LOG (GNUNET_ERROR_TYPE_WARNING,
739          "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
740          (unsigned int) ntohl (msg->ccn.channel_of_client));
741     GNUNET_SERVICE_client_continue (c->client);
742     return;
743   }
744   payload_size = ntohs (msg->header.size) - sizeof (*msg);
745   GNUNET_STATISTICS_update (stats,
746                             "# payload received from clients",
747                             payload_size,
748                             GNUNET_NO);
749   buf = (const char *) &msg[1];
750   LOG (GNUNET_ERROR_TYPE_DEBUG,
751        "Received %u bytes payload from %s for %s\n",
752        (unsigned int) payload_size,
753        GSC_2s (c),
754        GCCH_2s (ch));
755   if (GNUNET_OK !=
756       GCCH_handle_local_data (ch,
757                               msg->ccn,
758                               buf,
759                               payload_size))
760   {
761     GNUNET_break (0);
762     GNUNET_SERVICE_client_drop (c->client);
763     return;
764   }
765   GNUNET_SERVICE_client_continue (c->client);
766 }
767
768
769 /**
770  * Handler for client's ACKs for payload traffic.
771  *
772  * @param cls identification of the client.
773  * @param msg The actual message.
774  */
775 static void
776 handle_local_ack (void *cls,
777                   const struct GNUNET_CADET_LocalAck *msg)
778 {
779   struct CadetClient *c = cls;
780   struct CadetChannel *ch;
781
782   ch = lookup_channel (c,
783                        msg->ccn);
784   if (NULL == ch)
785   {
786     /* Channel does not exist (anymore) */
787     LOG (GNUNET_ERROR_TYPE_WARNING,
788          "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
789          (unsigned int) ntohl (msg->ccn.channel_of_client));
790     GNUNET_SERVICE_client_continue (c->client);
791     return;
792   }
793   LOG (GNUNET_ERROR_TYPE_DEBUG,
794        "Got a local ACK from %s for %s\n",
795        GSC_2s(c),
796        GCCH_2s (ch));
797   GCCH_handle_local_ack (ch,
798                          msg->ccn);
799   GNUNET_SERVICE_client_continue (c->client);
800 }
801
802
803 /**
804  * Iterator over all peers to send a monitoring client info about each peer.
805  *
806  * @param cls Closure ().
807  * @param peer Peer ID (tunnel remote peer).
808  * @param value Peer info.
809  * @return #GNUNET_YES, to keep iterating.
810  */
811 static int
812 get_all_peers_iterator (void *cls,
813                         const struct GNUNET_PeerIdentity *peer,
814                         void *value)
815 {
816   struct CadetClient *c = cls;
817   struct CadetPeer *p = value;
818   struct GNUNET_MQ_Envelope *env;
819   struct GNUNET_CADET_LocalInfoPeer *msg;
820
821   env = GNUNET_MQ_msg (msg,
822                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
823   msg->destination = *peer;
824   msg->paths = htons (GCP_count_paths (p));
825   msg->tunnel = htons (NULL != GCP_get_tunnel (p,
826                                                GNUNET_NO));
827   GNUNET_MQ_send (c->mq,
828                   env);
829   return GNUNET_YES;
830 }
831
832
833 /**
834  * Handler for client's INFO PEERS request.
835  *
836  * @param cls Identification of the client.
837  * @param message The actual message.
838  */
839 static void
840 handle_get_peers (void *cls,
841                   const struct GNUNET_MessageHeader *message)
842 {
843   struct CadetClient *c = cls;
844   struct GNUNET_MQ_Envelope *env;
845   struct GNUNET_MessageHeader *reply;
846
847   GCP_iterate_all (&get_all_peers_iterator,
848                    c);
849   env = GNUNET_MQ_msg (reply,
850                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
851   GNUNET_MQ_send (c->mq,
852                   env);
853   GNUNET_SERVICE_client_continue (c->client);
854 }
855
856
857 /**
858  * Iterator over all paths of a peer to build an InfoPeer message.
859  * Message contains blocks of peers, first not included.
860  *
861  * @param cls message queue for transmission
862  * @param path Path itself
863  * @param off offset of the peer on @a path
864  * @return #GNUNET_YES if should keep iterating.
865  *         #GNUNET_NO otherwise.
866  */
867 static int
868 path_info_iterator (void *cls,
869                     struct CadetPeerPath *path,
870                     unsigned int off)
871 {
872   struct GNUNET_MQ_Handle *mq = cls;
873   struct GNUNET_MQ_Envelope *env;
874   struct GNUNET_MessageHeader *resp;
875   struct GNUNET_PeerIdentity *id;
876   uint16_t path_size;
877   unsigned int i;
878   unsigned int path_length;
879
880   path_length = GCPP_get_length (path);
881   path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
882   if (sizeof (*resp) + path_size > UINT16_MAX)
883   {
884     LOG (GNUNET_ERROR_TYPE_WARNING,
885          "Path of %u entries is too long for info message\n",
886          path_length);
887     return GNUNET_YES;
888   }
889   env = GNUNET_MQ_msg_extra (resp,
890                              path_size,
891                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
892   id = (struct GNUNET_PeerIdentity *) &resp[1];
893
894   /* Don't copy first peer.  First peer is always the local one.  Last
895    * peer is always the destination (leave as 0, EOL).
896    */
897   for (i = 0; i < off; i++)
898     id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
899                                                   i + 1));
900   GNUNET_MQ_send (mq,
901                   env);
902   return GNUNET_YES;
903 }
904
905
906 /**
907  * Handler for client's SHOW_PEER request.
908  *
909  * @param cls Identification of the client.
910  * @param msg The actual message.
911  */
912 static void
913 handle_show_peer (void *cls,
914                   const struct GNUNET_CADET_LocalInfo *msg)
915 {
916   struct CadetClient *c = cls;
917   struct CadetPeer *p;
918   struct GNUNET_MQ_Envelope *env;
919   struct GNUNET_MessageHeader *resp;
920
921   p = GCP_get (&msg->peer,
922                GNUNET_NO);
923   if (NULL != p)
924     GCP_iterate_paths (p,
925                        &path_info_iterator,
926                        c->mq);
927   /* Send message with 0/0 to indicate the end */
928   env = GNUNET_MQ_msg (resp,
929                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
930   GNUNET_MQ_send (c->mq,
931                   env);
932   GNUNET_SERVICE_client_continue (c->client);
933 }
934
935
936 /**
937  * Iterator over all tunnels to send a monitoring client info about each tunnel.
938  *
939  * @param cls Closure ().
940  * @param peer Peer ID (tunnel remote peer).
941  * @param value a `struct CadetPeer`
942  * @return #GNUNET_YES, to keep iterating.
943  */
944 static int
945 get_all_tunnels_iterator (void *cls,
946                           const struct GNUNET_PeerIdentity *peer,
947                           void *value)
948 {
949   struct CadetClient *c = cls;
950   struct CadetPeer *p = value;
951   struct GNUNET_MQ_Envelope *env;
952   struct GNUNET_CADET_LocalInfoTunnel *msg;
953   struct CadetTunnel *t;
954
955   t = GCP_get_tunnel (p,
956                       GNUNET_NO);
957   if (NULL == t)
958     return GNUNET_YES;
959   env = GNUNET_MQ_msg (msg,
960                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
961   msg->destination = *peer;
962   msg->channels = htonl (GCT_count_channels (t));
963   msg->connections = htonl (GCT_count_any_connections (t));
964   msg->cstate = htons (0);
965   msg->estate = htons ((uint16_t) GCT_get_estate (t));
966   GNUNET_MQ_send (c->mq,
967                   env);
968   return GNUNET_YES;
969 }
970
971
972 /**
973  * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
974  *
975  * @param cls client Identification of the client.
976  * @param message The actual message.
977  */
978 static void
979 handle_info_tunnels (void *cls,
980                      const struct GNUNET_MessageHeader *message)
981 {
982   struct CadetClient *c = cls;
983   struct GNUNET_MQ_Envelope *env;
984   struct GNUNET_MessageHeader *reply;
985
986   GCP_iterate_all (&get_all_tunnels_iterator,
987                    c);
988   env = GNUNET_MQ_msg (reply,
989                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
990   GNUNET_MQ_send (c->mq,
991                   env);
992   GNUNET_SERVICE_client_continue (c->client);
993 }
994
995
996 /**
997  * Update the message with information about the connection.
998  *
999  * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1000  * @param ct a connection about which we should store information in @a cls
1001  */
1002 static void
1003 iter_connection (void *cls,
1004                  struct CadetTConnection *ct)
1005 {
1006   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1007   struct CadetConnection *cc = ct->cc;
1008   struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
1009
1010   h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1011   h[msg->connections++] = *(GCC_get_id (cc));
1012 }
1013
1014
1015 /**
1016  * Update the message with information about the channel.
1017  *
1018  * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1019  * @param ch a channel about which we should store information in @a cls
1020  */
1021 static void
1022 iter_channel (void *cls,
1023               struct CadetChannel *ch)
1024 {
1025   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1026   struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1027   struct GNUNET_CADET_ChannelTunnelNumber *chn
1028     = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
1029
1030   chn[msg->channels++] = GCCH_get_id (ch);
1031 }
1032
1033
1034 /**
1035  * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
1036  *
1037  * @param cls Identification of the client.
1038  * @param msg The actual message.
1039  */
1040 static void
1041 handle_info_tunnel (void *cls,
1042                     const struct GNUNET_CADET_LocalInfo *msg)
1043 {
1044   struct CadetClient *c = cls;
1045   struct GNUNET_MQ_Envelope *env;
1046   struct GNUNET_CADET_LocalInfoTunnel *resp;
1047   struct CadetTunnel *t;
1048   struct CadetPeer *p;
1049   unsigned int ch_n;
1050   unsigned int c_n;
1051
1052   p = GCP_get (&msg->peer,
1053                GNUNET_NO);
1054   t = GCP_get_tunnel (p,
1055                       GNUNET_NO);
1056   if (NULL == t)
1057   {
1058     /* We don't know the tunnel */
1059     struct GNUNET_MQ_Envelope *env;
1060     struct GNUNET_CADET_LocalInfoTunnel *warn;
1061
1062     LOG (GNUNET_ERROR_TYPE_INFO,
1063          "Tunnel to %s unknown\n",
1064          GNUNET_i2s_full (&msg->peer));
1065     env = GNUNET_MQ_msg (warn,
1066                          GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1067     warn->destination = msg->peer;
1068     GNUNET_MQ_send (c->mq,
1069                     env);
1070     GNUNET_SERVICE_client_continue (c->client);
1071     return;
1072   }
1073
1074   /* Initialize context */
1075   ch_n = GCT_count_channels (t);
1076   c_n = GCT_count_any_connections (t);
1077   env = GNUNET_MQ_msg_extra (resp,
1078                              c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1079                              ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1080                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1081   resp->destination = msg->peer;
1082   /* Do not reorder! #iter_channel needs counters in HBO! */
1083   GCT_iterate_connections (t,
1084                            &iter_connection,
1085                            resp);
1086   GCT_iterate_channels (t,
1087                         &iter_channel,
1088                         resp);
1089   resp->connections = htonl (resp->connections);
1090   resp->channels = htonl (resp->channels);
1091   resp->cstate = htons (0);
1092   resp->estate = htons (GCT_get_estate (t));
1093   GNUNET_MQ_send (c->mq,
1094                   env);
1095   GNUNET_SERVICE_client_continue (c->client);
1096 }
1097
1098
1099 /**
1100  * Iterator over all peers to dump info for each peer.
1101  *
1102  * @param cls Closure (unused).
1103  * @param peer Peer ID (tunnel remote peer).
1104  * @param value Peer info.
1105  *
1106  * @return #GNUNET_YES, to keep iterating.
1107  */
1108 static int
1109 show_peer_iterator (void *cls,
1110                     const struct GNUNET_PeerIdentity *peer,
1111                     void *value)
1112 {
1113   struct CadetPeer *p = value;
1114   struct CadetTunnel *t;
1115
1116   t = GCP_get_tunnel (p,
1117                       GNUNET_NO);
1118   if (NULL != t)
1119     GCT_debug (t,
1120                GNUNET_ERROR_TYPE_ERROR);
1121   LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1122   return GNUNET_YES;
1123 }
1124
1125
1126 /**
1127  * Handler for client's INFO_DUMP request.
1128  *
1129  * @param cls Identification of the client.
1130  * @param message The actual message.
1131  */
1132 static void
1133 handle_info_dump (void *cls,
1134                   const struct GNUNET_MessageHeader *message)
1135 {
1136   struct CadetClient *c = cls;
1137
1138   LOG (GNUNET_ERROR_TYPE_INFO,
1139        "Received dump info request from client %u\n",
1140        c->id);
1141
1142   LOG (GNUNET_ERROR_TYPE_ERROR,
1143        "*************************** DUMP START ***************************\n");
1144   for (struct CadetClient *ci = clients_head;
1145        NULL != ci;
1146        ci = ci->next)
1147   {
1148     LOG (GNUNET_ERROR_TYPE_ERROR,
1149          "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1150          ci->id,
1151          ci,
1152          ci->client,
1153          (NULL != c->ports)
1154          ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1155          : 0,
1156          GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1157   }
1158   LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1159   GCP_iterate_all (&show_peer_iterator,
1160                    NULL);
1161
1162   LOG (GNUNET_ERROR_TYPE_ERROR,
1163        "**************************** DUMP END ****************************\n");
1164
1165   GNUNET_SERVICE_client_continue (c->client);
1166 }
1167
1168
1169
1170 /**
1171  * Callback called when a client connects to the service.
1172  *
1173  * @param cls closure for the service
1174  * @param client the new client that connected to the service
1175  * @param mq the message queue used to send messages to the client
1176  * @return @a c
1177  */
1178 static void *
1179 client_connect_cb (void *cls,
1180                    struct GNUNET_SERVICE_Client *client,
1181                    struct GNUNET_MQ_Handle *mq)
1182 {
1183   struct CadetClient *c;
1184
1185   c = GNUNET_new (struct CadetClient);
1186   c->client = client;
1187   c->mq = mq;
1188   c->id = next_client_id++; /* overflow not important: just for debug */
1189   c->channels
1190     = GNUNET_CONTAINER_multihashmap32_create (32);
1191   GNUNET_CONTAINER_DLL_insert (clients_head,
1192                                clients_tail,
1193                                c);
1194   GNUNET_STATISTICS_update (stats,
1195                             "# clients",
1196                             +1,
1197                             GNUNET_NO);
1198   LOG (GNUNET_ERROR_TYPE_DEBUG,
1199        "%s connected\n",
1200        GSC_2s (c));
1201   return c;
1202 }
1203
1204
1205 /**
1206  * A channel was destroyed by the other peer. Tell our client.
1207  *
1208  * @param c client that lost a channel
1209  * @param ccn channel identification number for the client
1210  * @param ch the channel object
1211  */
1212 void
1213 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1214                                    struct GNUNET_CADET_ClientChannelNumber ccn,
1215                                    struct CadetChannel *ch)
1216 {
1217   struct GNUNET_MQ_Envelope *env;
1218   struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1219
1220   env = GNUNET_MQ_msg (tdm,
1221                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1222   tdm->ccn = ccn;
1223   GSC_send_to_client (c,
1224                       env);
1225   GNUNET_assert (GNUNET_YES ==
1226                  GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1227                                                          ntohl (ccn.channel_of_client),
1228                                                          ch));
1229 }
1230
1231
1232 /**
1233  * A client that created a loose channel that was not bound to a port
1234  * disconnected, drop it from the #loose_channels list.
1235  *
1236  * @param h_port the hashed port the channel was trying to bind to
1237  * @param ch the channel that was lost
1238  */
1239 void
1240 GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
1241                         struct CadetChannel *ch)
1242 {
1243   GNUNET_assert (GNUNET_YES ==
1244                  GNUNET_CONTAINER_multihashmap_remove (loose_channels,
1245                                                        h_port,
1246                                                        ch));
1247 }
1248
1249
1250 /**
1251  * Iterator for deleting each channel whose client endpoint disconnected.
1252  *
1253  * @param cls Closure (client that has disconnected).
1254  * @param key The local channel id in host byte order
1255  * @param value The value stored at the key (channel to destroy).
1256  * @return #GNUNET_OK, keep iterating.
1257  */
1258 static int
1259 channel_destroy_iterator (void *cls,
1260                           uint32_t key,
1261                           void *value)
1262 {
1263   struct CadetClient *c = cls;
1264   struct GNUNET_CADET_ClientChannelNumber ccn;
1265   struct CadetChannel *ch = value;
1266
1267   LOG (GNUNET_ERROR_TYPE_DEBUG,
1268        "Destroying %s, due to %s disconnecting.\n",
1269        GCCH_2s (ch),
1270        GSC_2s (c));
1271   ccn.channel_of_client = htonl (key);
1272   GCCH_channel_local_destroy (ch,
1273                               c,
1274                               ccn);
1275   GNUNET_assert (GNUNET_YES ==
1276                  GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1277                                                          key,
1278                                                          ch));
1279   return GNUNET_OK;
1280 }
1281
1282
1283 /**
1284  * Remove client's ports from the global hashmap on disconnect.
1285  *
1286  * @param cls the `struct CadetClient`
1287  * @param port the port.
1288  * @param value the `struct OpenPort` to remove
1289  * @return #GNUNET_OK, keep iterating.
1290  */
1291 static int
1292 client_release_ports (void *cls,
1293                       const struct GNUNET_HashCode *port,
1294                       void *value)
1295 {
1296   struct CadetClient *c = cls;
1297   struct OpenPort *op = value;
1298
1299   GNUNET_assert (c == op->c);
1300   LOG (GNUNET_ERROR_TYPE_DEBUG,
1301        "Closing port %s due to %s disconnect.\n",
1302        GNUNET_h2s (port),
1303        GSC_2s (c));
1304   GNUNET_assert (GNUNET_YES ==
1305                  GNUNET_CONTAINER_multihashmap_remove (open_ports,
1306                                                        &op->h_port,
1307                                                        op));
1308   GNUNET_assert (GNUNET_YES ==
1309                  GNUNET_CONTAINER_multihashmap_remove (c->ports,
1310                                                        port,
1311                                                        op));
1312   GNUNET_free (op);
1313   return GNUNET_OK;
1314 }
1315
1316
1317 /**
1318  * Callback called when a client disconnected from the service
1319  *
1320  * @param cls closure for the service
1321  * @param client the client that disconnected
1322  * @param internal_cls should be equal to @a c
1323  */
1324 static void
1325 client_disconnect_cb (void *cls,
1326                       struct GNUNET_SERVICE_Client *client,
1327                       void *internal_cls)
1328 {
1329   struct CadetClient *c = internal_cls;
1330
1331   GNUNET_assert (c->client == client);
1332   LOG (GNUNET_ERROR_TYPE_DEBUG,
1333        "%s is disconnecting.\n",
1334        GSC_2s (c));
1335   if (NULL != c->channels)
1336   {
1337     GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1338                                              &channel_destroy_iterator,
1339                                              c);
1340     GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels));
1341     GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1342   }
1343   if (NULL != c->ports)
1344   {
1345     GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1346                                            &client_release_ports,
1347                                            c);
1348     GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1349   }
1350   GNUNET_CONTAINER_DLL_remove (clients_head,
1351                                clients_tail,
1352                                c);
1353   GNUNET_STATISTICS_update (stats,
1354                             "# clients",
1355                             -1,
1356                             GNUNET_NO);
1357   GNUNET_free (c);
1358   if ( (NULL == clients_head) &&
1359        (GNUNET_YES == shutting_down) )
1360     shutdown_rest ();
1361 }
1362
1363
1364 /**
1365  * Setup CADET internals.
1366  *
1367  * @param cls closure
1368  * @param server the initialized server
1369  * @param c configuration to use
1370  */
1371 static void
1372 run (void *cls,
1373      const struct GNUNET_CONFIGURATION_Handle *c,
1374      struct GNUNET_SERVICE_Handle *service)
1375 {
1376   cfg = c;
1377   if (GNUNET_OK !=
1378       GNUNET_CONFIGURATION_get_value_number (c,
1379                                              "CADET",
1380                                              "RATCHET_MESSAGES",
1381                                              &ratchet_messages))
1382   {
1383     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1384                                "CADET",
1385                                "RATCHET_MESSAGES",
1386                                "needs to be a number");
1387     ratchet_messages = 64;
1388   }
1389   if (GNUNET_OK !=
1390       GNUNET_CONFIGURATION_get_value_time (c,
1391                                            "CADET",
1392                                            "RATCHET_TIME",
1393                                            &ratchet_time))
1394   {
1395     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1396                                "CADET",
1397                                "RATCHET_TIME",
1398                                "need delay value");
1399     ratchet_time = GNUNET_TIME_UNIT_HOURS;
1400   }
1401   if (GNUNET_OK !=
1402       GNUNET_CONFIGURATION_get_value_time (c,
1403                                            "CADET",
1404                                            "REFRESH_CONNECTION_TIME",
1405                                            &keepalive_period))
1406   {
1407     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1408                                "CADET",
1409                                "REFRESH_CONNECTION_TIME",
1410                                "need delay value");
1411     keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1412   }
1413   if (GNUNET_OK !=
1414       GNUNET_CONFIGURATION_get_value_number (c,
1415                                              "CADET",
1416                                              "DROP_PERCENT",
1417                                              &drop_percent))
1418   {
1419     drop_percent = 0;
1420   }
1421   else
1422   {
1423     LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1424     LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1425     LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1426     LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1427     LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1428   }
1429   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1430   if (NULL == my_private_key)
1431   {
1432     GNUNET_break (0);
1433     GNUNET_SCHEDULER_shutdown ();
1434     return;
1435   }
1436   GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1437                                       &my_full_id.public_key);
1438   stats = GNUNET_STATISTICS_create ("cadet",
1439                                     c);
1440   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1441                                  NULL);
1442   ats_ch = GNUNET_ATS_connectivity_init (c);
1443   /* FIXME: optimize code to allow GNUNET_YES here! */
1444   open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1445                                                      GNUNET_NO);
1446   loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1447                                                          GNUNET_NO);
1448   peers = GNUNET_CONTAINER_multipeermap_create (16,
1449                                                 GNUNET_YES);
1450   connections = GNUNET_CONTAINER_multishortmap_create (256,
1451                                                        GNUNET_YES);
1452   GCH_init (c);
1453   GCD_init (c);
1454   GCO_init (c);
1455   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1456               "CADET started for peer %s\n",
1457               GNUNET_i2s (&my_full_id));
1458
1459 }
1460
1461
1462 /**
1463  * Define "main" method using service macro.
1464  */
1465 GNUNET_SERVICE_MAIN
1466 ("cadet",
1467  GNUNET_SERVICE_OPTION_NONE,
1468  &run,
1469  &client_connect_cb,
1470  &client_disconnect_cb,
1471  NULL,
1472  GNUNET_MQ_hd_fixed_size (port_open,
1473                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1474                           struct GNUNET_CADET_PortMessage,
1475                           NULL),
1476  GNUNET_MQ_hd_fixed_size (port_close,
1477                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1478                           struct GNUNET_CADET_PortMessage,
1479                           NULL),
1480  GNUNET_MQ_hd_fixed_size (channel_create,
1481                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1482                           struct GNUNET_CADET_LocalChannelCreateMessage,
1483                           NULL),
1484  GNUNET_MQ_hd_fixed_size (channel_destroy,
1485                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1486                           struct GNUNET_CADET_LocalChannelDestroyMessage,
1487                           NULL),
1488  GNUNET_MQ_hd_var_size (local_data,
1489                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1490                         struct GNUNET_CADET_LocalData,
1491                         NULL),
1492  GNUNET_MQ_hd_fixed_size (local_ack,
1493                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1494                           struct GNUNET_CADET_LocalAck,
1495                           NULL),
1496  GNUNET_MQ_hd_fixed_size (get_peers,
1497                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1498                           struct GNUNET_MessageHeader,
1499                           NULL),
1500  GNUNET_MQ_hd_fixed_size (show_peer,
1501                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1502                           struct GNUNET_CADET_LocalInfo,
1503                           NULL),
1504  GNUNET_MQ_hd_fixed_size (info_tunnels,
1505                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1506                           struct GNUNET_MessageHeader,
1507                           NULL),
1508  GNUNET_MQ_hd_fixed_size (info_tunnel,
1509                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1510                           struct GNUNET_CADET_LocalInfo,
1511                           NULL),
1512  GNUNET_MQ_hd_fixed_size (info_dump,
1513                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1514                           struct GNUNET_MessageHeader,
1515                           NULL),
1516  GNUNET_MQ_handler_end ());
1517
1518 /* end of gnunet-service-cadet-new.c */