2 This file is part of GNUnet.
3 Copyright (C) 2001-2013, 2017 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file cadet/gnunet-service-cadet.c
23 * @brief GNUnet CADET service with encryption
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
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.
36 #include "gnunet_util_lib.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet-service-cadet.h"
40 #include "gnunet-service-cadet_channel.h"
41 #include "gnunet-service-cadet_connection.h"
42 #include "gnunet-service-cadet_core.h"
43 #include "gnunet-service-cadet_dht.h"
44 #include "gnunet-service-cadet_hello.h"
45 #include "gnunet-service-cadet_tunnels.h"
46 #include "gnunet-service-cadet_peer.h"
47 #include "gnunet-service-cadet_paths.h"
49 #define LOG(level, ...) GNUNET_log (level, __VA_ARGS__)
53 * Struct containing information about a client of the service
60 struct CadetClient *next;
65 struct CadetClient *prev;
68 * Tunnels that belong to this client, indexed by local id,
69 * value is a `struct CadetChannel`.
71 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
74 * Handle to communicate with the client
76 struct GNUNET_MQ_Handle *mq;
81 struct GNUNET_SERVICE_Client *client;
84 * Ports that this client has declared interest in.
85 * Indexed by port, contains `struct OpenPort`
87 struct GNUNET_CONTAINER_MultiHashMap *ports;
90 * Channel ID to use for the next incoming channel for this client.
91 * Wraps around (in theory).
93 struct GNUNET_CADET_ClientChannelNumber next_ccn;
96 * ID of the client, mainly for debug messages. Purely internal to this file.
102 /******************************************************************************/
103 /*********************** GLOBAL VARIABLES ****************************/
104 /******************************************************************************/
106 /****************************** Global variables ******************************/
109 * Handle to our configuration.
111 const struct GNUNET_CONFIGURATION_Handle *cfg;
114 * Handle to the statistics service.
116 struct GNUNET_STATISTICS_Handle *stats;
119 * Handle to communicate with ATS.
121 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
126 struct GNUNET_PeerIdentity my_full_id;
131 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
134 * Signal that shutdown is happening: prevent recovery measures.
139 * DLL with all the clients, head.
141 static struct CadetClient *clients_head;
144 * DLL with all the clients, tail.
146 static struct CadetClient *clients_tail;
149 * Next ID to assign to a client.
151 static unsigned int next_client_id;
154 * All ports clients of this peer have opened. Maps from
155 * a hashed port to a `struct OpenPort`.
157 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
160 * Map from ports to channels where the ports were closed at the
161 * time we got the inbound connection.
162 * Indexed by h_port, contains `struct CadetChannel`.
164 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
167 * Map from PIDs to `struct CadetPeer` entries.
169 struct GNUNET_CONTAINER_MultiPeerMap *peers;
172 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
173 * hash codes to `struct CadetConnection` objects.
175 struct GNUNET_CONTAINER_MultiShortmap *connections;
178 * How many messages are needed to trigger an AXOLOTL ratchet advance.
180 unsigned long long ratchet_messages;
183 * How long until we trigger a ratched advance due to time.
185 struct GNUNET_TIME_Relative ratchet_time;
188 * How frequently do we send KEEPALIVE messages on idle connections?
190 struct GNUNET_TIME_Relative keepalive_period;
193 * Set to non-zero values to create random drops to test retransmissions.
195 unsigned long long drop_percent;
199 * Send a message to a client.
201 * @param c client to get the message
202 * @param env envelope with the message
205 GSC_send_to_client (struct CadetClient *c,
206 struct GNUNET_MQ_Envelope *env)
208 GNUNET_MQ_send (c->mq,
214 * Return identifier for a client as a string.
216 * @param c client to identify
217 * @return string for debugging
220 GSC_2s (struct CadetClient *c)
224 GNUNET_snprintf (buf,
233 * Lookup channel of client @a c by @a ccn.
235 * @param c client to look in
236 * @param ccn channel ID to look up
237 * @return NULL if no such channel exists
239 static struct CadetChannel *
240 lookup_channel (struct CadetClient *c,
241 struct GNUNET_CADET_ClientChannelNumber ccn)
243 return GNUNET_CONTAINER_multihashmap32_get (c->channels,
244 ntohl (ccn.channel_of_client));
249 * Obtain the next LID to use for incoming connections to
252 * @param c client handle
254 static struct GNUNET_CADET_ClientChannelNumber
255 client_get_next_ccn (struct CadetClient *c)
257 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
259 /* increment until we have a free one... */
264 ccn.channel_of_client
265 = htonl (1 + (ntohl (ccn.channel_of_client)));
266 if (ntohl (ccn.channel_of_client) >=
267 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
268 ccn.channel_of_client = htonl (0);
270 c->next_ccn.channel_of_client
271 = htonl (1 + (ntohl (ccn.channel_of_client)));
277 * Bind incoming channel to this client, and notify client about
278 * incoming connection. Caller is responsible for notifying the other
279 * peer about our acceptance of the channel.
281 * @param c client to bind to
282 * @param ch channel to be bound
283 * @param dest peer that establishes the connection
284 * @param port port number
285 * @param options options
286 * @return local channel number assigned to the new client
288 struct GNUNET_CADET_ClientChannelNumber
289 GSC_bind (struct CadetClient *c,
290 struct CadetChannel *ch,
291 struct CadetPeer *dest,
292 const struct GNUNET_HashCode *port,
295 struct GNUNET_MQ_Envelope *env;
296 struct GNUNET_CADET_LocalChannelCreateMessage *cm;
297 struct GNUNET_CADET_ClientChannelNumber ccn;
299 ccn = client_get_next_ccn (c);
300 GNUNET_assert (GNUNET_YES ==
301 GNUNET_CONTAINER_multihashmap32_put (c->channels,
303 ccn.channel_of_client),
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",
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);
318 cm->opt = htonl (options);
319 cm->peer = *GCP_get_id (dest);
320 GSC_send_to_client (c,
327 * Callback invoked on all peers to destroy all tunnels
328 * that may still exist.
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)
336 destroy_tunnels_now (void *cls,
337 const struct GNUNET_PeerIdentity *pid,
340 struct CadetPeer *cp = value;
341 struct CadetTunnel *t = GCP_get_tunnel (cp,
345 GCT_destroy_tunnel_now (t);
351 * Callback invoked on all peers to destroy all tunnels
352 * that may still exist.
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)
360 destroy_paths_now (void *cls,
361 const struct GNUNET_PeerIdentity *pid,
364 struct CadetPeer *cp = value;
366 GCP_drop_owned_paths (cp);
372 * Shutdown everything once the clients have disconnected.
379 GNUNET_STATISTICS_destroy (stats,
383 /* Destroy tunnels. Note that all channels must be destroyed first! */
384 GCP_iterate_all (&destroy_tunnels_now,
386 /* All tunnels, channels, connections and CORE must be down before this point. */
387 GCP_iterate_all (&destroy_paths_now,
389 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
390 GCP_destroy_all_peers ();
391 if (NULL != open_ports)
393 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
396 if (NULL != loose_channels)
398 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
399 loose_channels = NULL;
403 GNUNET_CONTAINER_multipeermap_destroy (peers);
406 if (NULL != connections)
408 GNUNET_CONTAINER_multishortmap_destroy (connections);
413 GNUNET_ATS_connectivity_done (ats_ch);
418 GNUNET_free_non_null (my_private_key);
419 my_private_key = NULL;
424 * Task run during shutdown.
429 shutdown_task (void *cls)
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 shutting_down = GNUNET_YES;
435 if (NULL == clients_head)
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.
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)
450 bind_loose_channel (void *cls,
451 const struct GNUNET_HashCode *port,
454 struct OpenPort *op = cls;
455 struct CadetChannel *ch = value;
460 GNUNET_assert (GNUNET_YES ==
461 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
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
474 * @param cls Identification of the client.
475 * @param pmsg The actual message.
478 handle_port_open (void *cls,
479 const struct GNUNET_CADET_PortMessage *pmsg)
481 struct CadetClient *c = cls;
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "Open port %s requested by %s\n",
486 GNUNET_h2s (&pmsg->port),
488 if (NULL == c->ports)
489 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
491 op = GNUNET_new (struct OpenPort);
493 op->port = pmsg->port;
494 GCCH_hash_port (&op->h_port,
498 GNUNET_CONTAINER_multihashmap_put (c->ports,
501 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
504 GNUNET_SERVICE_client_drop (c->client);
507 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
510 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
511 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
515 GNUNET_SERVICE_client_continue (c->client);
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
525 * @param cls Identification of the client.
526 * @param pmsg The actual message.
529 handle_port_close (void *cls,
530 const struct GNUNET_CADET_PortMessage *pmsg)
532 struct CadetClient *c = cls;
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "Closing port %s as requested by %s\n",
537 GNUNET_h2s (&pmsg->port),
539 if (NULL == c->ports)
541 /* Client closed a port despite _never_ having opened one? */
543 GNUNET_SERVICE_client_drop (c->client);
546 op = GNUNET_CONTAINER_multihashmap_get (c->ports,
551 GNUNET_SERVICE_client_drop (c->client);
554 GNUNET_assert (GNUNET_YES ==
555 GNUNET_CONTAINER_multihashmap_remove (c->ports,
558 GNUNET_assert (GNUNET_YES ==
559 GNUNET_CONTAINER_multihashmap_remove (open_ports,
563 GNUNET_SERVICE_client_continue (c->client);
568 * Handler for requests for us creating a new channel to another peer and port.
570 * @param cls Identification of the client.
571 * @param tcm The actual message.
574 handle_channel_create (void *cls,
575 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
577 struct CadetClient *c = cls;
578 struct CadetChannel *ch;
580 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
582 /* Channel ID not in allowed range. */
584 GNUNET_SERVICE_client_drop (c->client);
587 ch = lookup_channel (c,
591 /* Channel ID already in use. Not allowed. */
593 GNUNET_SERVICE_client_drop (c->client);
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),
603 ch = GCCH_channel_local_new (c,
612 GNUNET_SERVICE_client_drop (c->client);
615 GNUNET_assert (GNUNET_YES ==
616 GNUNET_CONTAINER_multihashmap32_put (c->channels,
621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
623 GNUNET_SERVICE_client_continue (c->client);
628 * Handler for requests of destroying an existing channel.
630 * @param cls client identification of the client
631 * @param msg the actual message
634 handle_channel_destroy (void *cls,
636 GNUNET_CADET_LocalChannelDestroyMessage *msg)
638 struct CadetClient *c = cls;
639 struct CadetChannel *ch;
641 ch = lookup_channel (c,
645 /* Client attempted to destroy unknown channel.
646 Can happen if the other side went down at the same time.*/
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "%s tried to destroy unknown channel %X\n",
650 (uint32_t) ntohl (msg->ccn.channel_of_client));
651 GNUNET_SERVICE_client_continue (c->client);
654 LOG (GNUNET_ERROR_TYPE_DEBUG,
655 "%s is destroying %s\n",
658 GNUNET_assert (GNUNET_YES ==
659 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
664 GCCH_channel_local_destroy (ch,
667 GNUNET_SERVICE_client_continue (c->client);
672 * Check for client traffic data message is well-formed.
674 * @param cls identification of the client
675 * @param msg the actual message
676 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
679 check_local_data (void *cls,
680 const struct GNUNET_CADET_LocalData *msg)
683 size_t payload_claimed_size;
685 struct GNUNET_MessageHeader pa;
687 /* FIXME: what is the format we shall allow for @a msg?
688 ONE payload item or multiple? Seems current cadet_api
689 at least in theory allows more than one. Next-gen
690 cadet_api will likely no more, so we could then
691 simplify this mess again. *//* Sanity check for message size */payload_size = ntohs (msg->header.size) - sizeof(*msg);
692 buf = (const char *) &msg[1];
693 while (payload_size >= sizeof(struct GNUNET_MessageHeader))
695 /* need to memcpy() for alignment */
699 payload_claimed_size = ntohs (pa.size);
700 if ((payload_size < payload_claimed_size) ||
701 (payload_claimed_size < sizeof(struct GNUNET_MessageHeader)) ||
702 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size))
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "Local data of %u total size had sub-message %u at %u with %u bytes\n",
707 ntohs (msg->header.size),
709 (unsigned int) (buf - (const char *) &msg[1]),
710 (unsigned int) payload_claimed_size);
711 return GNUNET_SYSERR;
713 payload_size -= payload_claimed_size;
714 buf += payload_claimed_size;
716 if (0 != payload_size)
719 return GNUNET_SYSERR;
726 * Handler for client payload traffic to be send on a channel to
729 * @param cls identification of the client
730 * @param msg the actual message
733 handle_local_data (void *cls,
734 const struct GNUNET_CADET_LocalData *msg)
736 struct CadetClient *c = cls;
737 struct CadetChannel *ch;
741 ch = lookup_channel (c,
745 /* Channel does not exist (anymore) */
746 LOG (GNUNET_ERROR_TYPE_WARNING,
747 "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
748 (unsigned int) ntohl (msg->ccn.channel_of_client));
749 GNUNET_SERVICE_client_continue (c->client);
752 payload_size = ntohs (msg->header.size) - sizeof(*msg);
753 GNUNET_STATISTICS_update (stats,
754 "# payload received from clients",
757 buf = (const char *) &msg[1];
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Received %u bytes payload from %s for %s\n",
760 (unsigned int) payload_size,
764 GCCH_handle_local_data (ch,
770 GNUNET_SERVICE_client_drop (c->client);
773 GNUNET_SERVICE_client_continue (c->client);
778 * Handler for client's ACKs for payload traffic.
780 * @param cls identification of the client.
781 * @param msg The actual message.
784 handle_local_ack (void *cls,
785 const struct GNUNET_CADET_LocalAck *msg)
787 struct CadetClient *c = cls;
788 struct CadetChannel *ch;
790 ch = lookup_channel (c,
794 /* Channel does not exist (anymore) */
795 LOG (GNUNET_ERROR_TYPE_WARNING,
796 "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
797 (unsigned int) ntohl (msg->ccn.channel_of_client));
798 GNUNET_SERVICE_client_continue (c->client);
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Got a local ACK from %s for %s\n",
805 GCCH_handle_local_ack (ch,
807 GNUNET_SERVICE_client_continue (c->client);
812 * Iterator over all peers to send a monitoring client info about each peer.
814 * @param cls Closure ().
815 * @param peer Peer ID (tunnel remote peer).
816 * @param value Peer info.
817 * @return #GNUNET_YES, to keep iterating.
820 get_all_peers_iterator (void *cls,
821 const struct GNUNET_PeerIdentity *peer,
824 struct CadetClient *c = cls;
825 struct CadetPeer *p = value;
826 struct GNUNET_MQ_Envelope *env;
827 struct GNUNET_CADET_LocalInfoPeers *msg;
829 env = GNUNET_MQ_msg (msg,
830 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
831 msg->destination = *peer;
832 msg->paths = htons (GCP_count_paths (p));
833 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
835 msg->best_path_length = htonl (0); // FIXME: get length of shortest known path!
836 GNUNET_MQ_send (c->mq,
843 * Handler for client's INFO PEERS request.
845 * @param cls Identification of the client.
846 * @param message The actual message.
849 handle_get_peers (void *cls,
850 const struct GNUNET_MessageHeader *message)
852 struct CadetClient *c = cls;
853 struct GNUNET_MQ_Envelope *env;
854 struct GNUNET_MessageHeader *reply;
856 GCP_iterate_all (&get_all_peers_iterator,
858 env = GNUNET_MQ_msg (reply,
859 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END);
860 GNUNET_MQ_send (c->mq,
862 GNUNET_SERVICE_client_continue (c->client);
867 * Iterator over all paths of a peer to build an InfoPeer message.
868 * Message contains blocks of peers, first not included.
870 * @param cls message queue for transmission
871 * @param path Path itself
872 * @param off offset of the peer on @a path
873 * @return #GNUNET_YES if should keep iterating.
874 * #GNUNET_NO otherwise.
877 path_info_iterator (void *cls,
878 struct CadetPeerPath *path,
881 struct GNUNET_MQ_Handle *mq = cls;
882 struct GNUNET_MQ_Envelope *env;
883 struct GNUNET_CADET_LocalInfoPath *resp;
884 struct GNUNET_PeerIdentity *id;
886 unsigned int path_length;
888 path_length = GCPP_get_length (path);
889 path_size = sizeof(struct GNUNET_PeerIdentity) * path_length;
890 if (sizeof(*resp) + path_size > UINT16_MAX)
892 /* try just giving the relevant path */
893 path_length = GNUNET_MIN ((UINT16_MAX - sizeof(*resp)) / sizeof(struct
894 GNUNET_PeerIdentity),
896 path_size = sizeof(struct GNUNET_PeerIdentity) * path_length;
898 if (sizeof(*resp) + path_size > UINT16_MAX)
900 LOG (GNUNET_ERROR_TYPE_WARNING,
901 "Path of %u entries is too long for info message\n",
905 env = GNUNET_MQ_msg_extra (resp,
907 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH);
908 id = (struct GNUNET_PeerIdentity *) &resp[1];
910 /* Don't copy first peer. First peer is always the local one. Last
911 * peer is always the destination (leave as 0, EOL).
913 for (unsigned int i = 0; i < path_length; i++)
914 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
916 resp->off = htonl (off);
924 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH request.
926 * @param cls Identification of the client.
927 * @param msg The actual message.
930 handle_show_path (void *cls,
931 const struct GNUNET_CADET_RequestPathInfoMessage *msg)
933 struct CadetClient *c = cls;
935 struct GNUNET_MQ_Envelope *env;
936 struct GNUNET_MessageHeader *resp;
938 p = GCP_get (&msg->peer,
941 GCP_iterate_indirect_paths (p,
944 env = GNUNET_MQ_msg (resp,
945 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END);
946 GNUNET_MQ_send (c->mq,
948 GNUNET_SERVICE_client_continue (c->client);
953 * Iterator over all tunnels to send a monitoring client info about each tunnel.
955 * @param cls Closure ().
956 * @param peer Peer ID (tunnel remote peer).
957 * @param value a `struct CadetPeer`
958 * @return #GNUNET_YES, to keep iterating.
961 get_all_tunnels_iterator (void *cls,
962 const struct GNUNET_PeerIdentity *peer,
965 struct CadetClient *c = cls;
966 struct CadetPeer *p = value;
967 struct GNUNET_MQ_Envelope *env;
968 struct GNUNET_CADET_LocalInfoTunnel *msg;
969 struct CadetTunnel *t;
971 t = GCP_get_tunnel (p,
975 env = GNUNET_MQ_msg (msg,
976 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
977 msg->destination = *peer;
978 msg->channels = htonl (GCT_count_channels (t));
979 msg->connections = htonl (GCT_count_any_connections (t));
980 msg->cstate = htons (0);
981 msg->estate = htons ((uint16_t) GCT_get_estate (t));
982 GNUNET_MQ_send (c->mq,
989 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS request.
991 * @param cls client Identification of the client.
992 * @param message The actual message.
995 handle_info_tunnels (void *cls,
996 const struct GNUNET_MessageHeader *message)
998 struct CadetClient *c = cls;
999 struct GNUNET_MQ_Envelope *env;
1000 struct GNUNET_MessageHeader *reply;
1002 GCP_iterate_all (&get_all_tunnels_iterator,
1004 env = GNUNET_MQ_msg (reply,
1005 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END);
1006 GNUNET_MQ_send (c->mq,
1008 GNUNET_SERVICE_client_continue (c->client);
1013 * Callback called when a client connects to the service.
1015 * @param cls closure for the service
1016 * @param client the new client that connected to the service
1017 * @param mq the message queue used to send messages to the client
1021 client_connect_cb (void *cls,
1022 struct GNUNET_SERVICE_Client *client,
1023 struct GNUNET_MQ_Handle *mq)
1025 struct CadetClient *c;
1027 c = GNUNET_new (struct CadetClient);
1030 c->id = next_client_id++; /* overflow not important: just for debug */
1032 = GNUNET_CONTAINER_multihashmap32_create (32);
1033 GNUNET_CONTAINER_DLL_insert (clients_head,
1036 GNUNET_STATISTICS_update (stats,
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1048 * A channel was destroyed by the other peer. Tell our client.
1050 * @param c client that lost a channel
1051 * @param ccn channel identification number for the client
1052 * @param ch the channel object
1055 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1056 struct GNUNET_CADET_ClientChannelNumber ccn,
1057 struct CadetChannel *ch)
1059 struct GNUNET_MQ_Envelope *env;
1060 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1062 env = GNUNET_MQ_msg (tdm,
1063 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1065 GSC_send_to_client (c,
1067 GNUNET_assert (GNUNET_YES ==
1068 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1070 ccn.channel_of_client),
1076 * A client that created a loose channel that was not bound to a port
1077 * disconnected, drop it from the #loose_channels list.
1079 * @param h_port the hashed port the channel was trying to bind to
1080 * @param ch the channel that was lost
1083 GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
1084 struct CadetChannel *ch)
1086 GNUNET_assert (GNUNET_YES ==
1087 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
1094 * Iterator for deleting each channel whose client endpoint disconnected.
1096 * @param cls Closure (client that has disconnected).
1097 * @param key The local channel id in host byte order
1098 * @param value The value stored at the key (channel to destroy).
1099 * @return #GNUNET_OK, keep iterating.
1102 channel_destroy_iterator (void *cls,
1106 struct CadetClient *c = cls;
1107 struct GNUNET_CADET_ClientChannelNumber ccn;
1108 struct CadetChannel *ch = value;
1110 LOG (GNUNET_ERROR_TYPE_DEBUG,
1111 "Destroying %s, due to %s disconnecting.\n",
1114 ccn.channel_of_client = htonl (key);
1115 GCCH_channel_local_destroy (ch,
1118 GNUNET_assert (GNUNET_YES ==
1119 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1127 * Remove client's ports from the global hashmap on disconnect.
1129 * @param cls the `struct CadetClient`
1130 * @param port the port.
1131 * @param value the `struct OpenPort` to remove
1132 * @return #GNUNET_OK, keep iterating.
1135 client_release_ports (void *cls,
1136 const struct GNUNET_HashCode *port,
1139 struct CadetClient *c = cls;
1140 struct OpenPort *op = value;
1142 GNUNET_assert (c == op->c);
1143 LOG (GNUNET_ERROR_TYPE_DEBUG,
1144 "Closing port %s due to %s disconnect.\n",
1147 GNUNET_assert (GNUNET_YES ==
1148 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1151 GNUNET_assert (GNUNET_YES ==
1152 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1161 * Callback called when a client disconnected from the service
1163 * @param cls closure for the service
1164 * @param client the client that disconnected
1165 * @param internal_cls should be equal to @a c
1168 client_disconnect_cb (void *cls,
1169 struct GNUNET_SERVICE_Client *client,
1172 struct CadetClient *c = internal_cls;
1174 GNUNET_assert (c->client == client);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "%s is disconnecting.\n",
1178 if (NULL != c->channels)
1180 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1181 &channel_destroy_iterator,
1183 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels));
1184 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1186 if (NULL != c->ports)
1188 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1189 &client_release_ports,
1191 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1193 GNUNET_CONTAINER_DLL_remove (clients_head,
1196 GNUNET_STATISTICS_update (stats,
1201 if ((NULL == clients_head) &&
1202 (GNUNET_YES == shutting_down))
1208 * Setup CADET internals.
1210 * @param cls closure
1211 * @param server the initialized server
1212 * @param c configuration to use
1216 const struct GNUNET_CONFIGURATION_Handle *c,
1217 struct GNUNET_SERVICE_Handle *service)
1221 GNUNET_CONFIGURATION_get_value_number (c,
1226 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1229 "needs to be a number");
1230 ratchet_messages = 64;
1233 GNUNET_CONFIGURATION_get_value_time (c,
1238 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1241 "need delay value");
1242 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1245 GNUNET_CONFIGURATION_get_value_time (c,
1247 "REFRESH_CONNECTION_TIME",
1250 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1252 "REFRESH_CONNECTION_TIME",
1253 "need delay value");
1254 keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1257 GNUNET_CONFIGURATION_get_value_number (c,
1266 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1267 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1268 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1269 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1270 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1272 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1273 if (NULL == my_private_key)
1276 GNUNET_SCHEDULER_shutdown ();
1279 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1280 &my_full_id.public_key);
1281 stats = GNUNET_STATISTICS_create ("cadet",
1283 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1285 ats_ch = GNUNET_ATS_connectivity_init (c);
1286 /* FIXME: optimize code to allow GNUNET_YES here! */
1287 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1289 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1291 peers = GNUNET_CONTAINER_multipeermap_create (16,
1293 connections = GNUNET_CONTAINER_multishortmap_create (256,
1298 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1299 "CADET started for peer %s\n",
1300 GNUNET_i2s (&my_full_id));
1305 * Define "main" method using service macro.
1309 GNUNET_SERVICE_OPTION_NONE,
1312 &client_disconnect_cb,
1314 GNUNET_MQ_hd_fixed_size (port_open,
1315 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1316 struct GNUNET_CADET_PortMessage,
1318 GNUNET_MQ_hd_fixed_size (port_close,
1319 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1320 struct GNUNET_CADET_PortMessage,
1322 GNUNET_MQ_hd_fixed_size (channel_create,
1323 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1324 struct GNUNET_CADET_LocalChannelCreateMessage,
1326 GNUNET_MQ_hd_fixed_size (channel_destroy,
1327 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1328 struct GNUNET_CADET_LocalChannelDestroyMessage,
1330 GNUNET_MQ_hd_var_size (local_data,
1331 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1332 struct GNUNET_CADET_LocalData,
1334 GNUNET_MQ_hd_fixed_size (local_ack,
1335 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1336 struct GNUNET_CADET_LocalAck,
1338 GNUNET_MQ_hd_fixed_size (get_peers,
1339 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS,
1340 struct GNUNET_MessageHeader,
1342 GNUNET_MQ_hd_fixed_size (show_path,
1343 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH,
1344 struct GNUNET_CADET_RequestPathInfoMessage,
1346 GNUNET_MQ_hd_fixed_size (info_tunnels,
1347 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS,
1348 struct GNUNET_MessageHeader,
1350 GNUNET_MQ_handler_end ());
1352 /* end of gnunet-service-cadet-new.c */