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