src: for every AGPL3.0 file, add SPDX identifier.
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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   if (NULL == c->ports)
540   {
541     /* Client closed a port despite _never_ having opened one? */
542     GNUNET_break (0);
543     GNUNET_SERVICE_client_drop (c->client);
544     return;
545   }
546   op = GNUNET_CONTAINER_multihashmap_get (c->ports,
547                                           &pmsg->port);
548   if (NULL == op)
549   {
550     GNUNET_break (0);
551     GNUNET_SERVICE_client_drop (c->client);
552     return;
553   }
554   GNUNET_assert (GNUNET_YES ==
555                  GNUNET_CONTAINER_multihashmap_remove (c->ports,
556                                                        &op->port,
557                                                        op));
558   GNUNET_assert (GNUNET_YES ==
559                  GNUNET_CONTAINER_multihashmap_remove (open_ports,
560                                                        &op->h_port,
561                                                        op));
562   GNUNET_free (op);
563   GNUNET_SERVICE_client_continue (c->client);
564 }
565
566
567 /**
568  * Handler for requests for us creating a new channel to another peer and port.
569  *
570  * @param cls Identification of the client.
571  * @param tcm The actual message.
572  */
573 static void
574 handle_channel_create (void *cls,
575                        const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
576 {
577   struct CadetClient *c = cls;
578   struct CadetChannel *ch;
579
580   if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
581   {
582     /* Channel ID not in allowed range. */
583     GNUNET_break (0);
584     GNUNET_SERVICE_client_drop (c->client);
585     return;
586   }
587   ch = lookup_channel (c,
588                        tcm->ccn);
589   if (NULL != ch)
590   {
591     /* Channel ID already in use. Not allowed. */
592     GNUNET_break (0);
593     GNUNET_SERVICE_client_drop (c->client);
594     return;
595   }
596   LOG (GNUNET_ERROR_TYPE_DEBUG,
597        "New channel to %s at port %s requested by %s\n",
598        GNUNET_i2s (&tcm->peer),
599        GNUNET_h2s (&tcm->port),
600        GSC_2s (c));
601
602   /* Create channel */
603   ch = GCCH_channel_local_new (c,
604                                tcm->ccn,
605                                GCP_get (&tcm->peer,
606                                         GNUNET_YES),
607                                &tcm->port,
608                                ntohl (tcm->opt));
609   if (NULL == ch)
610   {
611     GNUNET_break (0);
612     GNUNET_SERVICE_client_drop (c->client);
613     return;
614   }
615   GNUNET_assert (GNUNET_YES ==
616                  GNUNET_CONTAINER_multihashmap32_put (c->channels,
617                                                       ntohl (tcm->ccn.channel_of_client),
618                                                       ch,
619                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
620
621   GNUNET_SERVICE_client_continue (c->client);
622 }
623
624
625 /**
626  * Handler for requests of destroying an existing channel.
627  *
628  * @param cls client identification of the client
629  * @param msg the actual message
630  */
631 static void
632 handle_channel_destroy (void *cls,
633                         const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
634 {
635   struct CadetClient *c = cls;
636   struct CadetChannel *ch;
637
638   ch = lookup_channel (c,
639                        msg->ccn);
640   if (NULL == ch)
641   {
642     /* Client attempted to destroy unknown channel.
643        Can happen if the other side went down at the same time.*/
644     LOG (GNUNET_ERROR_TYPE_DEBUG,
645          "%s tried to destroy unknown channel %X\n",
646          GSC_2s(c),
647          (uint32_t) ntohl (msg->ccn.channel_of_client));
648     GNUNET_SERVICE_client_continue (c->client);
649     return;
650   }
651   LOG (GNUNET_ERROR_TYPE_DEBUG,
652        "%s is destroying %s\n",
653        GSC_2s(c),
654        GCCH_2s (ch));
655   GNUNET_assert (GNUNET_YES ==
656                  GNUNET_CONTAINER_multihashmap32_remove (c->channels,
657                                                          ntohl (msg->ccn.channel_of_client),
658                                                          ch));
659   GCCH_channel_local_destroy (ch,
660                               c,
661                               msg->ccn);
662   GNUNET_SERVICE_client_continue (c->client);
663 }
664
665
666 /**
667  * Check for client traffic data message is well-formed.
668  *
669  * @param cls identification of the client
670  * @param msg the actual message
671  * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
672  */
673 static int
674 check_local_data (void *cls,
675                   const struct GNUNET_CADET_LocalData *msg)
676 {
677   size_t payload_size;
678   size_t payload_claimed_size;
679   const char *buf;
680   struct GNUNET_MessageHeader pa;
681
682   /* FIXME: what is the format we shall allow for @a msg?
683      ONE payload item or multiple? Seems current cadet_api
684      at least in theory allows more than one. Next-gen
685      cadet_api will likely no more, so we could then
686      simplify this mess again. */
687   /* Sanity check for message size */
688   payload_size = ntohs (msg->header.size) - sizeof (*msg);
689   buf = (const char *) &msg[1];
690   while (payload_size >= sizeof (struct GNUNET_MessageHeader))
691   {
692     /* need to memcpy() for alignment */
693     GNUNET_memcpy (&pa,
694                    buf,
695                    sizeof (pa));
696     payload_claimed_size = ntohs (pa.size);
697     if ( (payload_size < payload_claimed_size) ||
698          (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
699          (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
700     {
701       GNUNET_break (0);
702       LOG (GNUNET_ERROR_TYPE_DEBUG,
703            "Local data of %u total size had sub-message %u at %u with %u bytes\n",
704            ntohs (msg->header.size),
705            ntohs (pa.type),
706            (unsigned int) (buf - (const char *) &msg[1]),
707            (unsigned int) payload_claimed_size);
708       return GNUNET_SYSERR;
709     }
710     payload_size -= payload_claimed_size;
711     buf += payload_claimed_size;
712   }
713   if (0 != payload_size)
714   {
715     GNUNET_break_op (0);
716     return GNUNET_SYSERR;
717   }
718   return GNUNET_OK;
719 }
720
721
722 /**
723  * Handler for client payload traffic to be send on a channel to
724  * another peer.
725  *
726  * @param cls identification of the client
727  * @param msg the actual message
728  */
729 static void
730 handle_local_data (void *cls,
731                    const struct GNUNET_CADET_LocalData *msg)
732 {
733   struct CadetClient *c = cls;
734   struct CadetChannel *ch;
735   size_t payload_size;
736   const char *buf;
737
738   ch = lookup_channel (c,
739                        msg->ccn);
740   if (NULL == ch)
741   {
742     /* Channel does not exist (anymore) */
743     LOG (GNUNET_ERROR_TYPE_WARNING,
744          "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
745          (unsigned int) ntohl (msg->ccn.channel_of_client));
746     GNUNET_SERVICE_client_continue (c->client);
747     return;
748   }
749   payload_size = ntohs (msg->header.size) - sizeof (*msg);
750   GNUNET_STATISTICS_update (stats,
751                             "# payload received from clients",
752                             payload_size,
753                             GNUNET_NO);
754   buf = (const char *) &msg[1];
755   LOG (GNUNET_ERROR_TYPE_DEBUG,
756        "Received %u bytes payload from %s for %s\n",
757        (unsigned int) payload_size,
758        GSC_2s (c),
759        GCCH_2s (ch));
760   if (GNUNET_OK !=
761       GCCH_handle_local_data (ch,
762                               msg->ccn,
763                               buf,
764                               payload_size))
765   {
766     GNUNET_break (0);
767     GNUNET_SERVICE_client_drop (c->client);
768     return;
769   }
770   GNUNET_SERVICE_client_continue (c->client);
771 }
772
773
774 /**
775  * Handler for client's ACKs for payload traffic.
776  *
777  * @param cls identification of the client.
778  * @param msg The actual message.
779  */
780 static void
781 handle_local_ack (void *cls,
782                   const struct GNUNET_CADET_LocalAck *msg)
783 {
784   struct CadetClient *c = cls;
785   struct CadetChannel *ch;
786
787   ch = lookup_channel (c,
788                        msg->ccn);
789   if (NULL == ch)
790   {
791     /* Channel does not exist (anymore) */
792     LOG (GNUNET_ERROR_TYPE_WARNING,
793          "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
794          (unsigned int) ntohl (msg->ccn.channel_of_client));
795     GNUNET_SERVICE_client_continue (c->client);
796     return;
797   }
798   LOG (GNUNET_ERROR_TYPE_DEBUG,
799        "Got a local ACK from %s for %s\n",
800        GSC_2s(c),
801        GCCH_2s (ch));
802   GCCH_handle_local_ack (ch,
803                          msg->ccn);
804   GNUNET_SERVICE_client_continue (c->client);
805 }
806
807
808 /**
809  * Iterator over all peers to send a monitoring client info about each peer.
810  *
811  * @param cls Closure ().
812  * @param peer Peer ID (tunnel remote peer).
813  * @param value Peer info.
814  * @return #GNUNET_YES, to keep iterating.
815  */
816 static int
817 get_all_peers_iterator (void *cls,
818                         const struct GNUNET_PeerIdentity *peer,
819                         void *value)
820 {
821   struct CadetClient *c = cls;
822   struct CadetPeer *p = value;
823   struct GNUNET_MQ_Envelope *env;
824   struct GNUNET_CADET_LocalInfoPeer *msg;
825
826   env = GNUNET_MQ_msg (msg,
827                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);  
828   msg->destination = *peer;
829   msg->paths = htons (GCP_count_paths (p));
830   msg->tunnel = htons (NULL != GCP_get_tunnel (p,
831                                                GNUNET_NO));
832   GNUNET_MQ_send (c->mq,
833                   env);
834   return GNUNET_YES;
835 }
836
837
838 /**
839  * Handler for client's INFO PEERS request.
840  *
841  * @param cls Identification of the client.
842  * @param message The actual message.
843  */
844 static void
845 handle_get_peers (void *cls,
846                   const struct GNUNET_MessageHeader *message)
847 {
848   struct CadetClient *c = cls;
849   struct GNUNET_MQ_Envelope *env;
850   struct GNUNET_MessageHeader *reply;
851
852   GCP_iterate_all (&get_all_peers_iterator,
853                    c);
854   env = GNUNET_MQ_msg (reply,
855                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
856   GNUNET_MQ_send (c->mq,
857                   env);
858   GNUNET_SERVICE_client_continue (c->client);
859 }
860
861
862 /**
863  * Iterator over all paths of a peer to build an InfoPeer message.
864  * Message contains blocks of peers, first not included.
865  *
866  * @param cls message queue for transmission
867  * @param path Path itself
868  * @param off offset of the peer on @a path
869  * @return #GNUNET_YES if should keep iterating.
870  *         #GNUNET_NO otherwise.
871  */
872 static int
873 path_info_iterator (void *cls,
874                     struct CadetPeerPath *path,
875                     unsigned int off)
876 {
877   struct GNUNET_MQ_Handle *mq = cls;
878   struct GNUNET_MQ_Envelope *env;
879   struct GNUNET_CADET_LocalInfoPeer *resp;
880   struct GNUNET_PeerIdentity *id;
881   uint16_t path_size;
882   unsigned int i;
883   unsigned int path_length;
884
885   path_length = GCPP_get_length (path);
886   path_size = sizeof (struct GNUNET_PeerIdentity) * path_length;
887   if (sizeof (*resp) + path_size > UINT16_MAX)
888   {
889     LOG (GNUNET_ERROR_TYPE_WARNING,
890          "Path of %u entries is too long for info message\n",
891          path_length);
892     return GNUNET_YES;
893   }
894   env = GNUNET_MQ_msg_extra (resp,
895                              path_size,
896                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
897   
898   
899   resp->offset = htons(off);
900   resp->finished_with_paths = htons(0);
901   
902   id = (struct GNUNET_PeerIdentity *) &resp[1];
903
904   /* Don't copy first peer.  First peer is always the local one.  Last
905    * peer is always the destination (leave as 0, EOL).
906    */
907   for (i = 0; i <= off; i++)
908     id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
909                                                   i));
910   GNUNET_MQ_send (mq,
911                   env);
912   return GNUNET_YES;
913 }
914
915 /**
916  * Getting summary information about the number of paths and if a tunnel exists, 
917  * and the indirect paths to a peer, if there are ones.
918  *
919  * @param cls Closure ().
920  * @param peer Peer ID (tunnel remote peer).
921  * @param value Peer info.
922  * @return #GNUNET_YES, to keep iterating.
923  */
924 static void
925 get_peer_info (void *cls,
926                 const struct GNUNET_PeerIdentity *peer,
927                 struct CadetPeer *p)
928 {
929   struct CadetClient *c = cls;
930   struct GNUNET_MQ_Envelope *env;
931   struct GNUNET_CADET_LocalInfoPeer *msg;
932   
933   env = GNUNET_MQ_msg (msg,
934                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
935   msg->offset = htons(0);
936   msg->destination = *peer;
937   msg->paths = htons (GCP_count_paths (p));
938   msg->tunnel = htons (NULL != GCP_get_tunnel (p,
939                                                GNUNET_NO));
940   msg->finished_with_paths = htons(0);
941   GNUNET_MQ_send (c->mq,
942                   env);
943   GCP_iterate_indirect_paths (p,
944                               &path_info_iterator,
945                               c->mq);  
946 }
947
948
949 /**
950  * Handler for client's SHOW_PEER request.
951  *
952  * @param cls Identification of the client.
953  * @param msg The actual message.
954  */
955 static void
956 handle_show_peer (void *cls,
957                   const struct GNUNET_CADET_LocalInfo *msg)
958 {
959   struct CadetClient *c = cls;
960   struct CadetPeer *p;
961   struct GNUNET_MQ_Envelope *env;
962   struct GNUNET_CADET_LocalInfoPeer *resp;
963
964   p = GCP_get (&msg->peer,
965                GNUNET_NO);
966   if (NULL != p){
967     get_peer_info(c, &(msg->peer), p);  
968   }
969   
970   
971   env = GNUNET_MQ_msg (resp,
972                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
973   resp->finished_with_paths = htons(1);
974   resp->destination = msg->peer;
975   
976   GNUNET_MQ_send (c->mq,
977                   env);
978   
979   GNUNET_SERVICE_client_continue (c->client);
980 }
981
982
983 /**
984  * Iterator over all tunnels to send a monitoring client info about each tunnel.
985  *
986  * @param cls Closure ().
987  * @param peer Peer ID (tunnel remote peer).
988  * @param value a `struct CadetPeer`
989  * @return #GNUNET_YES, to keep iterating.
990  */
991 static int
992 get_all_tunnels_iterator (void *cls,
993                           const struct GNUNET_PeerIdentity *peer,
994                           void *value)
995 {
996   struct CadetClient *c = cls;
997   struct CadetPeer *p = value;
998   struct GNUNET_MQ_Envelope *env;
999   struct GNUNET_CADET_LocalInfoTunnel *msg;
1000   struct CadetTunnel *t;
1001
1002   t = GCP_get_tunnel (p,
1003                       GNUNET_NO);
1004   if (NULL == t)
1005     return GNUNET_YES;
1006   env = GNUNET_MQ_msg (msg,
1007                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1008   msg->destination = *peer;
1009   msg->channels = htonl (GCT_count_channels (t));
1010   msg->connections = htonl (GCT_count_any_connections (t));
1011   msg->cstate = htons (0);
1012   msg->estate = htons ((uint16_t) GCT_get_estate (t));
1013   GNUNET_MQ_send (c->mq,
1014                   env);
1015   return GNUNET_YES;
1016 }
1017
1018
1019 /**
1020  * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
1021  *
1022  * @param cls client Identification of the client.
1023  * @param message The actual message.
1024  */
1025 static void
1026 handle_info_tunnels (void *cls,
1027                      const struct GNUNET_MessageHeader *message)
1028 {
1029   struct CadetClient *c = cls;
1030   struct GNUNET_MQ_Envelope *env;
1031   struct GNUNET_MessageHeader *reply;
1032
1033   GCP_iterate_all (&get_all_tunnels_iterator,
1034                    c);
1035   env = GNUNET_MQ_msg (reply,
1036                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1037   GNUNET_MQ_send (c->mq,
1038                   env);
1039   GNUNET_SERVICE_client_continue (c->client);
1040 }
1041
1042
1043 /**
1044  * Update the message with information about the connection.
1045  *
1046  * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1047  * @param ct a connection about which we should store information in @a cls
1048  */
1049 static void
1050 iter_connection (void *cls,
1051                  struct CadetTConnection *ct)
1052 {
1053   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1054   struct CadetConnection *cc = ct->cc;
1055   struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
1056
1057   h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1058   h[msg->connections++] = *(GCC_get_id (cc));
1059 }
1060
1061
1062 /**
1063  * Update the message with information about the channel.
1064  *
1065  * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1066  * @param ch a channel about which we should store information in @a cls
1067  */
1068 static void
1069 iter_channel (void *cls,
1070               struct CadetChannel *ch)
1071 {
1072   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1073   struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1074   struct GNUNET_CADET_ChannelTunnelNumber *chn
1075     = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
1076
1077   chn[msg->channels++] = GCCH_get_id (ch);
1078 }
1079
1080
1081 /**
1082  * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
1083  *
1084  * @param cls Identification of the client.
1085  * @param msg The actual message.
1086  */
1087 static void
1088 handle_info_tunnel (void *cls,
1089                     const struct GNUNET_CADET_LocalInfo *msg)
1090 {
1091   struct CadetClient *c = cls;
1092   struct GNUNET_MQ_Envelope *env;
1093   struct GNUNET_CADET_LocalInfoTunnel *resp;
1094   struct CadetTunnel *t;
1095   struct CadetPeer *p;
1096   unsigned int ch_n;
1097   unsigned int c_n;
1098
1099   p = GCP_get (&msg->peer,
1100                GNUNET_NO);
1101   t = GCP_get_tunnel (p,
1102                       GNUNET_NO);
1103   if (NULL == t)
1104   {
1105     /* We don't know the tunnel */
1106     struct GNUNET_MQ_Envelope *env;
1107     struct GNUNET_CADET_LocalInfoTunnel *warn;
1108
1109     LOG (GNUNET_ERROR_TYPE_INFO,
1110          "Tunnel to %s unknown\n",
1111          GNUNET_i2s_full (&msg->peer));
1112     env = GNUNET_MQ_msg (warn,
1113                          GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1114     warn->destination = msg->peer;
1115     GNUNET_MQ_send (c->mq,
1116                     env);
1117     GNUNET_SERVICE_client_continue (c->client);
1118     return;
1119   }
1120
1121   /* Initialize context */
1122   ch_n = GCT_count_channels (t);
1123   c_n = GCT_count_any_connections (t);
1124   env = GNUNET_MQ_msg_extra (resp,
1125                              c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1126                              ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1127                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1128   resp->destination = msg->peer;
1129   /* Do not reorder! #iter_channel needs counters in HBO! */
1130   GCT_iterate_connections (t,
1131                            &iter_connection,
1132                            resp);
1133   GCT_iterate_channels (t,
1134                         &iter_channel,
1135                         resp);
1136   resp->connections = htonl (resp->connections);
1137   resp->channels = htonl (resp->channels);
1138   resp->cstate = htons (0);
1139   resp->estate = htons (GCT_get_estate (t));
1140   GNUNET_MQ_send (c->mq,
1141                   env);
1142   GNUNET_SERVICE_client_continue (c->client);
1143 }
1144
1145
1146 /**
1147  * Iterator over all peers to dump info for each peer.
1148  *
1149  * @param cls Closure (unused).
1150  * @param peer Peer ID (tunnel remote peer).
1151  * @param value Peer info.
1152  *
1153  * @return #GNUNET_YES, to keep iterating.
1154  */
1155 static int
1156 show_peer_iterator (void *cls,
1157                     const struct GNUNET_PeerIdentity *peer,
1158                     void *value)
1159 {
1160   struct CadetPeer *p = value;
1161   struct CadetTunnel *t;
1162
1163   t = GCP_get_tunnel (p,
1164                       GNUNET_NO);
1165   if (NULL != t)
1166     GCT_debug (t,
1167                GNUNET_ERROR_TYPE_ERROR);
1168   LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1169   return GNUNET_YES;
1170 }
1171
1172
1173 /**
1174  * Handler for client's INFO_DUMP request.
1175  *
1176  * @param cls Identification of the client.
1177  * @param message The actual message.
1178  */
1179 static void
1180 handle_info_dump (void *cls,
1181                   const struct GNUNET_MessageHeader *message)
1182 {
1183   struct CadetClient *c = cls;
1184
1185   LOG (GNUNET_ERROR_TYPE_INFO,
1186        "Received dump info request from client %u\n",
1187        c->id);
1188
1189   LOG (GNUNET_ERROR_TYPE_ERROR,
1190        "*************************** DUMP START ***************************\n");
1191   for (struct CadetClient *ci = clients_head;
1192        NULL != ci;
1193        ci = ci->next)
1194   {
1195     LOG (GNUNET_ERROR_TYPE_ERROR,
1196          "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1197          ci->id,
1198          ci,
1199          ci->client,
1200          (NULL != c->ports)
1201          ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1202          : 0,
1203          GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1204   }
1205   LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1206   GCP_iterate_all (&show_peer_iterator,
1207                    NULL);
1208
1209   LOG (GNUNET_ERROR_TYPE_ERROR,
1210        "**************************** DUMP END ****************************\n");
1211
1212   GNUNET_SERVICE_client_continue (c->client);
1213 }
1214
1215
1216
1217 /**
1218  * Callback called when a client connects to the service.
1219  *
1220  * @param cls closure for the service
1221  * @param client the new client that connected to the service
1222  * @param mq the message queue used to send messages to the client
1223  * @return @a c
1224  */
1225 static void *
1226 client_connect_cb (void *cls,
1227                    struct GNUNET_SERVICE_Client *client,
1228                    struct GNUNET_MQ_Handle *mq)
1229 {
1230   struct CadetClient *c;
1231
1232   c = GNUNET_new (struct CadetClient);
1233   c->client = client;
1234   c->mq = mq;
1235   c->id = next_client_id++; /* overflow not important: just for debug */
1236   c->channels
1237     = GNUNET_CONTAINER_multihashmap32_create (32);
1238   GNUNET_CONTAINER_DLL_insert (clients_head,
1239                                clients_tail,
1240                                c);
1241   GNUNET_STATISTICS_update (stats,
1242                             "# clients",
1243                             +1,
1244                             GNUNET_NO);
1245   LOG (GNUNET_ERROR_TYPE_DEBUG,
1246        "%s connected\n",
1247        GSC_2s (c));
1248   return c;
1249 }
1250
1251
1252 /**
1253  * A channel was destroyed by the other peer. Tell our client.
1254  *
1255  * @param c client that lost a channel
1256  * @param ccn channel identification number for the client
1257  * @param ch the channel object
1258  */
1259 void
1260 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1261                                    struct GNUNET_CADET_ClientChannelNumber ccn,
1262                                    struct CadetChannel *ch)
1263 {
1264   struct GNUNET_MQ_Envelope *env;
1265   struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1266
1267   env = GNUNET_MQ_msg (tdm,
1268                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1269   tdm->ccn = ccn;
1270   GSC_send_to_client (c,
1271                       env);
1272   GNUNET_assert (GNUNET_YES ==
1273                  GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1274                                                          ntohl (ccn.channel_of_client),
1275                                                          ch));
1276 }
1277
1278
1279 /**
1280  * A client that created a loose channel that was not bound to a port
1281  * disconnected, drop it from the #loose_channels list.
1282  *
1283  * @param h_port the hashed port the channel was trying to bind to
1284  * @param ch the channel that was lost
1285  */
1286 void
1287 GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
1288                         struct CadetChannel *ch)
1289 {
1290   GNUNET_assert (GNUNET_YES ==
1291                  GNUNET_CONTAINER_multihashmap_remove (loose_channels,
1292                                                        h_port,
1293                                                        ch));
1294 }
1295
1296
1297 /**
1298  * Iterator for deleting each channel whose client endpoint disconnected.
1299  *
1300  * @param cls Closure (client that has disconnected).
1301  * @param key The local channel id in host byte order
1302  * @param value The value stored at the key (channel to destroy).
1303  * @return #GNUNET_OK, keep iterating.
1304  */
1305 static int
1306 channel_destroy_iterator (void *cls,
1307                           uint32_t key,
1308                           void *value)
1309 {
1310   struct CadetClient *c = cls;
1311   struct GNUNET_CADET_ClientChannelNumber ccn;
1312   struct CadetChannel *ch = value;
1313
1314   LOG (GNUNET_ERROR_TYPE_DEBUG,
1315        "Destroying %s, due to %s disconnecting.\n",
1316        GCCH_2s (ch),
1317        GSC_2s (c));
1318   ccn.channel_of_client = htonl (key);
1319   GCCH_channel_local_destroy (ch,
1320                               c,
1321                               ccn);
1322   GNUNET_assert (GNUNET_YES ==
1323                  GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1324                                                          key,
1325                                                          ch));
1326   return GNUNET_OK;
1327 }
1328
1329
1330 /**
1331  * Remove client's ports from the global hashmap on disconnect.
1332  *
1333  * @param cls the `struct CadetClient`
1334  * @param port the port.
1335  * @param value the `struct OpenPort` to remove
1336  * @return #GNUNET_OK, keep iterating.
1337  */
1338 static int
1339 client_release_ports (void *cls,
1340                       const struct GNUNET_HashCode *port,
1341                       void *value)
1342 {
1343   struct CadetClient *c = cls;
1344   struct OpenPort *op = value;
1345
1346   GNUNET_assert (c == op->c);
1347   LOG (GNUNET_ERROR_TYPE_DEBUG,
1348        "Closing port %s due to %s disconnect.\n",
1349        GNUNET_h2s (port),
1350        GSC_2s (c));
1351   GNUNET_assert (GNUNET_YES ==
1352                  GNUNET_CONTAINER_multihashmap_remove (open_ports,
1353                                                        &op->h_port,
1354                                                        op));
1355   GNUNET_assert (GNUNET_YES ==
1356                  GNUNET_CONTAINER_multihashmap_remove (c->ports,
1357                                                        port,
1358                                                        op));
1359   GNUNET_free (op);
1360   return GNUNET_OK;
1361 }
1362
1363
1364 /**
1365  * Callback called when a client disconnected from the service
1366  *
1367  * @param cls closure for the service
1368  * @param client the client that disconnected
1369  * @param internal_cls should be equal to @a c
1370  */
1371 static void
1372 client_disconnect_cb (void *cls,
1373                       struct GNUNET_SERVICE_Client *client,
1374                       void *internal_cls)
1375 {
1376   struct CadetClient *c = internal_cls;
1377
1378   GNUNET_assert (c->client == client);
1379   LOG (GNUNET_ERROR_TYPE_DEBUG,
1380        "%s is disconnecting.\n",
1381        GSC_2s (c));
1382   if (NULL != c->channels)
1383   {
1384     GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1385                                              &channel_destroy_iterator,
1386                                              c);
1387     GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels));
1388     GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1389   }
1390   if (NULL != c->ports)
1391   {
1392     GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1393                                            &client_release_ports,
1394                                            c);
1395     GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1396   }
1397   GNUNET_CONTAINER_DLL_remove (clients_head,
1398                                clients_tail,
1399                                c);
1400   GNUNET_STATISTICS_update (stats,
1401                             "# clients",
1402                             -1,
1403                             GNUNET_NO);
1404   GNUNET_free (c);
1405   if ( (NULL == clients_head) &&
1406        (GNUNET_YES == shutting_down) )
1407     shutdown_rest ();
1408 }
1409
1410
1411 /**
1412  * Setup CADET internals.
1413  *
1414  * @param cls closure
1415  * @param server the initialized server
1416  * @param c configuration to use
1417  */
1418 static void
1419 run (void *cls,
1420      const struct GNUNET_CONFIGURATION_Handle *c,
1421      struct GNUNET_SERVICE_Handle *service)
1422 {
1423   cfg = c;
1424   if (GNUNET_OK !=
1425       GNUNET_CONFIGURATION_get_value_number (c,
1426                                              "CADET",
1427                                              "RATCHET_MESSAGES",
1428                                              &ratchet_messages))
1429   {
1430     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1431                                "CADET",
1432                                "RATCHET_MESSAGES",
1433                                "needs to be a number");
1434     ratchet_messages = 64;
1435   }
1436   if (GNUNET_OK !=
1437       GNUNET_CONFIGURATION_get_value_time (c,
1438                                            "CADET",
1439                                            "RATCHET_TIME",
1440                                            &ratchet_time))
1441   {
1442     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1443                                "CADET",
1444                                "RATCHET_TIME",
1445                                "need delay value");
1446     ratchet_time = GNUNET_TIME_UNIT_HOURS;
1447   }
1448   if (GNUNET_OK !=
1449       GNUNET_CONFIGURATION_get_value_time (c,
1450                                            "CADET",
1451                                            "REFRESH_CONNECTION_TIME",
1452                                            &keepalive_period))
1453   {
1454     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1455                                "CADET",
1456                                "REFRESH_CONNECTION_TIME",
1457                                "need delay value");
1458     keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1459   }
1460   if (GNUNET_OK !=
1461       GNUNET_CONFIGURATION_get_value_number (c,
1462                                              "CADET",
1463                                              "DROP_PERCENT",
1464                                              &drop_percent))
1465   {
1466     drop_percent = 0;
1467   }
1468   else
1469   {
1470     LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1471     LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1472     LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1473     LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1474     LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1475   }
1476   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1477   if (NULL == my_private_key)
1478   {
1479     GNUNET_break (0);
1480     GNUNET_SCHEDULER_shutdown ();
1481     return;
1482   }
1483   GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1484                                       &my_full_id.public_key);
1485   stats = GNUNET_STATISTICS_create ("cadet",
1486                                     c);
1487   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1488                                  NULL);
1489   ats_ch = GNUNET_ATS_connectivity_init (c);
1490   /* FIXME: optimize code to allow GNUNET_YES here! */
1491   open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1492                                                      GNUNET_NO);
1493   loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1494                                                          GNUNET_NO);
1495   peers = GNUNET_CONTAINER_multipeermap_create (16,
1496                                                 GNUNET_YES);
1497   connections = GNUNET_CONTAINER_multishortmap_create (256,
1498                                                        GNUNET_YES);
1499   GCH_init (c);
1500   GCD_init (c);
1501   GCO_init (c);
1502   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1503               "CADET started for peer %s\n",
1504               GNUNET_i2s (&my_full_id));
1505
1506 }
1507
1508
1509 /**
1510  * Define "main" method using service macro.
1511  */
1512 GNUNET_SERVICE_MAIN
1513 ("cadet",
1514  GNUNET_SERVICE_OPTION_NONE,
1515  &run,
1516  &client_connect_cb,
1517  &client_disconnect_cb,
1518  NULL,
1519  GNUNET_MQ_hd_fixed_size (port_open,
1520                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1521                           struct GNUNET_CADET_PortMessage,
1522                           NULL),
1523  GNUNET_MQ_hd_fixed_size (port_close,
1524                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1525                           struct GNUNET_CADET_PortMessage,
1526                           NULL),
1527  GNUNET_MQ_hd_fixed_size (channel_create,
1528                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1529                           struct GNUNET_CADET_LocalChannelCreateMessage,
1530                           NULL),
1531  GNUNET_MQ_hd_fixed_size (channel_destroy,
1532                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1533                           struct GNUNET_CADET_LocalChannelDestroyMessage,
1534                           NULL),
1535  GNUNET_MQ_hd_var_size (local_data,
1536                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1537                         struct GNUNET_CADET_LocalData,
1538                         NULL),
1539  GNUNET_MQ_hd_fixed_size (local_ack,
1540                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1541                           struct GNUNET_CADET_LocalAck,
1542                           NULL),
1543  GNUNET_MQ_hd_fixed_size (get_peers,
1544                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1545                           struct GNUNET_MessageHeader,
1546                           NULL),
1547  GNUNET_MQ_hd_fixed_size (show_peer,
1548                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1549                           struct GNUNET_CADET_LocalInfo,
1550                           NULL),
1551  GNUNET_MQ_hd_fixed_size (info_tunnels,
1552                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1553                           struct GNUNET_MessageHeader,
1554                           NULL),
1555  GNUNET_MQ_hd_fixed_size (info_tunnel,
1556                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1557                           struct GNUNET_CADET_LocalInfo,
1558                           NULL),
1559  GNUNET_MQ_hd_fixed_size (info_dump,
1560                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1561                           struct GNUNET_MessageHeader,
1562                           NULL),
1563  GNUNET_MQ_handler_end ());
1564
1565 /* end of gnunet-service-cadet-new.c */