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
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.
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.
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.
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.
37 #include "gnunet_util_lib.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"
50 #define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
54 * Struct containing information about a client of the service
61 struct CadetClient *next;
66 struct CadetClient *prev;
69 * Tunnels that belong to this client, indexed by local id,
70 * value is a `struct CadetChannel`.
72 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
75 * Handle to communicate with the client
77 struct GNUNET_MQ_Handle *mq;
82 struct GNUNET_SERVICE_Client *client;
85 * Ports that this client has declared interest in.
86 * Indexed by port, contains `struct OpenPort`
88 struct GNUNET_CONTAINER_MultiHashMap *ports;
91 * Channel ID to use for the next incoming channel for this client.
92 * Wraps around (in theory).
94 struct GNUNET_CADET_ClientChannelNumber next_ccn;
97 * ID of the client, mainly for debug messages. Purely internal to this file.
103 /******************************************************************************/
104 /*********************** GLOBAL VARIABLES ****************************/
105 /******************************************************************************/
107 /****************************** Global variables ******************************/
110 * Handle to our configuration.
112 const struct GNUNET_CONFIGURATION_Handle *cfg;
115 * Handle to the statistics service.
117 struct GNUNET_STATISTICS_Handle *stats;
120 * Handle to communicate with ATS.
122 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
127 struct GNUNET_PeerIdentity my_full_id;
132 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
135 * Signal that shutdown is happening: prevent recovery measures.
140 * DLL with all the clients, head.
142 static struct CadetClient *clients_head;
145 * DLL with all the clients, tail.
147 static struct CadetClient *clients_tail;
150 * Next ID to assign to a client.
152 static unsigned int next_client_id;
155 * All ports clients of this peer have opened. Maps from
156 * a hashed port to a `struct OpenPort`.
158 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
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`.
165 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
168 * Map from PIDs to `struct CadetPeer` entries.
170 struct GNUNET_CONTAINER_MultiPeerMap *peers;
173 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
174 * hash codes to `struct CadetConnection` objects.
176 struct GNUNET_CONTAINER_MultiShortmap *connections;
179 * How many messages are needed to trigger an AXOLOTL ratchet advance.
181 unsigned long long ratchet_messages;
184 * How long until we trigger a ratched advance due to time.
186 struct GNUNET_TIME_Relative ratchet_time;
189 * How frequently do we send KEEPALIVE messages on idle connections?
191 struct GNUNET_TIME_Relative keepalive_period;
194 * Set to non-zero values to create random drops to test retransmissions.
196 unsigned long long drop_percent;
200 * Send a message to a client.
202 * @param c client to get the message
203 * @param env envelope with the message
206 GSC_send_to_client (struct CadetClient *c,
207 struct GNUNET_MQ_Envelope *env)
209 GNUNET_MQ_send (c->mq,
215 * Return identifier for a client as a string.
217 * @param c client to identify
218 * @return string for debugging
221 GSC_2s (struct CadetClient *c)
225 GNUNET_snprintf (buf,
234 * Lookup channel of client @a c by @a ccn.
236 * @param c client to look in
237 * @param ccn channel ID to look up
238 * @return NULL if no such channel exists
240 static struct CadetChannel *
241 lookup_channel (struct CadetClient *c,
242 struct GNUNET_CADET_ClientChannelNumber ccn)
244 return GNUNET_CONTAINER_multihashmap32_get (c->channels,
245 ntohl (ccn.channel_of_client));
250 * Obtain the next LID to use for incoming connections to
253 * @param c client handle
255 static struct GNUNET_CADET_ClientChannelNumber
256 client_get_next_ccn (struct CadetClient *c)
258 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
260 /* increment until we have a free one... */
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);
271 c->next_ccn.channel_of_client
272 = htonl (1 + (ntohl (ccn.channel_of_client)));
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.
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
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,
296 struct GNUNET_MQ_Envelope *env;
297 struct GNUNET_CADET_LocalChannelCreateMessage *cm;
298 struct GNUNET_CADET_ClientChannelNumber ccn;
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),
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 if (NULL != open_ports)
385 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
388 if (NULL != loose_channels)
390 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
391 loose_channels = NULL;
393 /* Destroy tunnels. Note that all channels must be destroyed first! */
394 GCP_iterate_all (&destroy_tunnels_now,
396 /* All tunnels, channels, connections and CORE must be down before this point. */
397 GCP_iterate_all (&destroy_paths_now,
399 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
400 GCP_destroy_all_peers ();
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,
617 ntohl (tcm->ccn.channel_of_client),
619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
621 GNUNET_SERVICE_client_continue (c->client);
626 * Handler for requests of destroying an existing channel.
628 * @param cls client identification of the client
629 * @param msg the actual message
632 handle_channel_destroy (void *cls,
633 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
635 struct CadetClient *c = cls;
636 struct CadetChannel *ch;
638 ch = lookup_channel (c,
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",
647 (uint32_t) ntohl (msg->ccn.channel_of_client));
648 GNUNET_SERVICE_client_continue (c->client);
651 LOG (GNUNET_ERROR_TYPE_DEBUG,
652 "%s is destroying %s\n",
655 GNUNET_assert (GNUNET_YES ==
656 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
657 ntohl (msg->ccn.channel_of_client),
659 GCCH_channel_local_destroy (ch,
662 GNUNET_SERVICE_client_continue (c->client);
667 * Check for client traffic data message is well-formed.
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
674 check_local_data (void *cls,
675 const struct GNUNET_CADET_LocalData *msg)
678 size_t payload_claimed_size;
680 struct GNUNET_MessageHeader pa;
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))
692 /* need to memcpy() for alignment */
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) )
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),
706 (unsigned int) (buf - (const char *) &msg[1]),
707 (unsigned int) payload_claimed_size);
708 return GNUNET_SYSERR;
710 payload_size -= payload_claimed_size;
711 buf += payload_claimed_size;
713 if (0 != payload_size)
716 return GNUNET_SYSERR;
723 * Handler for client payload traffic to be send on a channel to
726 * @param cls identification of the client
727 * @param msg the actual message
730 handle_local_data (void *cls,
731 const struct GNUNET_CADET_LocalData *msg)
733 struct CadetClient *c = cls;
734 struct CadetChannel *ch;
738 ch = lookup_channel (c,
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);
749 payload_size = ntohs (msg->header.size) - sizeof (*msg);
750 GNUNET_STATISTICS_update (stats,
751 "# payload received from clients",
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,
761 GCCH_handle_local_data (ch,
766 GNUNET_SERVICE_client_drop (c->client);
769 GNUNET_SERVICE_client_continue (c->client);
774 * Handler for client's ACKs for payload traffic.
776 * @param cls identification of the client.
777 * @param msg The actual message.
780 handle_local_ack (void *cls,
781 const struct GNUNET_CADET_LocalAck *msg)
783 struct CadetClient *c = cls;
784 struct CadetChannel *ch;
786 ch = lookup_channel (c,
790 /* Channel does not exist (anymore) */
791 LOG (GNUNET_ERROR_TYPE_WARNING,
792 "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
793 (unsigned int) ntohl (msg->ccn.channel_of_client));
794 GNUNET_SERVICE_client_continue (c->client);
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 "Got a local ACK from %s for %s\n",
801 GCCH_handle_local_ack (ch,
803 GNUNET_SERVICE_client_continue (c->client);
808 * Iterator over all peers to send a monitoring client info about each peer.
810 * @param cls Closure ().
811 * @param peer Peer ID (tunnel remote peer).
812 * @param value Peer info.
813 * @return #GNUNET_YES, to keep iterating.
816 get_all_peers_iterator (void *cls,
817 const struct GNUNET_PeerIdentity *peer,
820 struct CadetClient *c = cls;
821 struct CadetPeer *p = value;
822 struct GNUNET_MQ_Envelope *env;
823 struct GNUNET_CADET_LocalInfoPeer *msg;
825 env = GNUNET_MQ_msg (msg,
826 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
827 msg->destination = *peer;
828 msg->paths = htons (GCP_count_paths (p));
829 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
831 GNUNET_MQ_send (c->mq,
838 * Handler for client's INFO PEERS request.
840 * @param cls Identification of the client.
841 * @param message The actual message.
844 handle_get_peers (void *cls,
845 const struct GNUNET_MessageHeader *message)
847 struct CadetClient *c = cls;
848 struct GNUNET_MQ_Envelope *env;
849 struct GNUNET_MessageHeader *reply;
851 GCP_iterate_all (&get_all_peers_iterator,
853 env = GNUNET_MQ_msg (reply,
854 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
855 GNUNET_MQ_send (c->mq,
857 GNUNET_SERVICE_client_continue (c->client);
862 * Iterator over all paths of a peer to build an InfoPeer message.
863 * Message contains blocks of peers, first not included.
865 * @param cls message queue for transmission
866 * @param path Path itself
867 * @param off offset of the peer on @a path
868 * @return #GNUNET_YES if should keep iterating.
869 * #GNUNET_NO otherwise.
872 path_info_iterator (void *cls,
873 struct CadetPeerPath *path,
876 struct GNUNET_MQ_Handle *mq = cls;
877 struct GNUNET_MQ_Envelope *env;
878 struct GNUNET_MessageHeader *resp;
879 struct GNUNET_PeerIdentity *id;
882 unsigned int path_length;
884 path_length = GCPP_get_length (path);
885 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
886 if (sizeof (*resp) + path_size > UINT16_MAX)
888 LOG (GNUNET_ERROR_TYPE_WARNING,
889 "Path of %u entries is too long for info message\n",
893 env = GNUNET_MQ_msg_extra (resp,
895 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
896 id = (struct GNUNET_PeerIdentity *) &resp[1];
898 /* Don't copy first peer. First peer is always the local one. Last
899 * peer is always the destination (leave as 0, EOL).
901 for (i = 0; i < off; i++)
902 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
911 * Handler for client's SHOW_PEER request.
913 * @param cls Identification of the client.
914 * @param msg The actual message.
917 handle_show_peer (void *cls,
918 const struct GNUNET_CADET_LocalInfo *msg)
920 struct CadetClient *c = cls;
922 struct GNUNET_MQ_Envelope *env;
923 struct GNUNET_MessageHeader *resp;
925 p = GCP_get (&msg->peer,
928 GCP_iterate_paths (p,
931 /* Send message with 0/0 to indicate the end */
932 env = GNUNET_MQ_msg (resp,
933 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
934 GNUNET_MQ_send (c->mq,
936 GNUNET_SERVICE_client_continue (c->client);
941 * Iterator over all tunnels to send a monitoring client info about each tunnel.
943 * @param cls Closure ().
944 * @param peer Peer ID (tunnel remote peer).
945 * @param value a `struct CadetPeer`
946 * @return #GNUNET_YES, to keep iterating.
949 get_all_tunnels_iterator (void *cls,
950 const struct GNUNET_PeerIdentity *peer,
953 struct CadetClient *c = cls;
954 struct CadetPeer *p = value;
955 struct GNUNET_MQ_Envelope *env;
956 struct GNUNET_CADET_LocalInfoTunnel *msg;
957 struct CadetTunnel *t;
959 t = GCP_get_tunnel (p,
963 env = GNUNET_MQ_msg (msg,
964 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
965 msg->destination = *peer;
966 msg->channels = htonl (GCT_count_channels (t));
967 msg->connections = htonl (GCT_count_any_connections (t));
968 msg->cstate = htons (0);
969 msg->estate = htons ((uint16_t) GCT_get_estate (t));
970 GNUNET_MQ_send (c->mq,
977 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
979 * @param cls client Identification of the client.
980 * @param message The actual message.
983 handle_info_tunnels (void *cls,
984 const struct GNUNET_MessageHeader *message)
986 struct CadetClient *c = cls;
987 struct GNUNET_MQ_Envelope *env;
988 struct GNUNET_MessageHeader *reply;
990 GCP_iterate_all (&get_all_tunnels_iterator,
992 env = GNUNET_MQ_msg (reply,
993 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
994 GNUNET_MQ_send (c->mq,
996 GNUNET_SERVICE_client_continue (c->client);
1001 * Update the message with information about the connection.
1003 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1004 * @param ct a connection about which we should store information in @a cls
1007 iter_connection (void *cls,
1008 struct CadetTConnection *ct)
1010 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1011 struct CadetConnection *cc = ct->cc;
1012 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
1014 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1015 h[msg->connections++] = *(GCC_get_id (cc));
1020 * Update the message with information about the channel.
1022 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1023 * @param ch a channel about which we should store information in @a cls
1026 iter_channel (void *cls,
1027 struct CadetChannel *ch)
1029 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1030 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1031 struct GNUNET_CADET_ChannelTunnelNumber *chn
1032 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
1034 chn[msg->channels++] = GCCH_get_id (ch);
1039 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
1041 * @param cls Identification of the client.
1042 * @param msg The actual message.
1045 handle_info_tunnel (void *cls,
1046 const struct GNUNET_CADET_LocalInfo *msg)
1048 struct CadetClient *c = cls;
1049 struct GNUNET_MQ_Envelope *env;
1050 struct GNUNET_CADET_LocalInfoTunnel *resp;
1051 struct CadetTunnel *t;
1052 struct CadetPeer *p;
1056 p = GCP_get (&msg->peer,
1058 t = GCP_get_tunnel (p,
1062 /* We don't know the tunnel */
1063 struct GNUNET_MQ_Envelope *env;
1064 struct GNUNET_CADET_LocalInfoTunnel *warn;
1066 LOG (GNUNET_ERROR_TYPE_INFO,
1067 "Tunnel to %s unknown\n",
1068 GNUNET_i2s_full (&msg->peer));
1069 env = GNUNET_MQ_msg (warn,
1070 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1071 warn->destination = msg->peer;
1072 GNUNET_MQ_send (c->mq,
1074 GNUNET_SERVICE_client_continue (c->client);
1078 /* Initialize context */
1079 ch_n = GCT_count_channels (t);
1080 c_n = GCT_count_any_connections (t);
1081 env = GNUNET_MQ_msg_extra (resp,
1082 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1083 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1084 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1085 resp->destination = msg->peer;
1086 /* Do not reorder! #iter_channel needs counters in HBO! */
1087 GCT_iterate_connections (t,
1090 GCT_iterate_channels (t,
1093 resp->connections = htonl (resp->connections);
1094 resp->channels = htonl (resp->channels);
1095 resp->cstate = htons (0);
1096 resp->estate = htons (GCT_get_estate (t));
1097 GNUNET_MQ_send (c->mq,
1099 GNUNET_SERVICE_client_continue (c->client);
1104 * Iterator over all peers to dump info for each peer.
1106 * @param cls Closure (unused).
1107 * @param peer Peer ID (tunnel remote peer).
1108 * @param value Peer info.
1110 * @return #GNUNET_YES, to keep iterating.
1113 show_peer_iterator (void *cls,
1114 const struct GNUNET_PeerIdentity *peer,
1117 struct CadetPeer *p = value;
1118 struct CadetTunnel *t;
1120 t = GCP_get_tunnel (p,
1124 GNUNET_ERROR_TYPE_ERROR);
1125 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1131 * Handler for client's INFO_DUMP request.
1133 * @param cls Identification of the client.
1134 * @param message The actual message.
1137 handle_info_dump (void *cls,
1138 const struct GNUNET_MessageHeader *message)
1140 struct CadetClient *c = cls;
1142 LOG (GNUNET_ERROR_TYPE_INFO,
1143 "Received dump info request from client %u\n",
1146 LOG (GNUNET_ERROR_TYPE_ERROR,
1147 "*************************** DUMP START ***************************\n");
1148 for (struct CadetClient *ci = clients_head;
1152 LOG (GNUNET_ERROR_TYPE_ERROR,
1153 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1158 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1160 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1162 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1163 GCP_iterate_all (&show_peer_iterator,
1166 LOG (GNUNET_ERROR_TYPE_ERROR,
1167 "**************************** DUMP END ****************************\n");
1169 GNUNET_SERVICE_client_continue (c->client);
1175 * Callback called when a client connects to the service.
1177 * @param cls closure for the service
1178 * @param client the new client that connected to the service
1179 * @param mq the message queue used to send messages to the client
1183 client_connect_cb (void *cls,
1184 struct GNUNET_SERVICE_Client *client,
1185 struct GNUNET_MQ_Handle *mq)
1187 struct CadetClient *c;
1189 c = GNUNET_new (struct CadetClient);
1192 c->id = next_client_id++; /* overflow not important: just for debug */
1194 = GNUNET_CONTAINER_multihashmap32_create (32);
1195 GNUNET_CONTAINER_DLL_insert (clients_head,
1198 GNUNET_STATISTICS_update (stats,
1202 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 * A channel was destroyed by the other peer. Tell our client.
1212 * @param c client that lost a channel
1213 * @param ccn channel identification number for the client
1214 * @param ch the channel object
1217 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1218 struct GNUNET_CADET_ClientChannelNumber ccn,
1219 struct CadetChannel *ch)
1221 struct GNUNET_MQ_Envelope *env;
1222 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1224 env = GNUNET_MQ_msg (tdm,
1225 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1227 GSC_send_to_client (c,
1229 GNUNET_assert (GNUNET_YES ==
1230 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1231 ntohl (ccn.channel_of_client),
1237 * A client that created a loose channel that was not bound to a port
1238 * disconnected, drop it from the #loose_channels list.
1240 * @param h_port the hashed port the channel was trying to bind to
1241 * @param ch the channel that was lost
1244 GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
1245 struct CadetChannel *ch)
1247 GNUNET_assert (GNUNET_YES ==
1248 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
1255 * Iterator for deleting each channel whose client endpoint disconnected.
1257 * @param cls Closure (client that has disconnected).
1258 * @param key The local channel id in host byte order
1259 * @param value The value stored at the key (channel to destroy).
1260 * @return #GNUNET_OK, keep iterating.
1263 channel_destroy_iterator (void *cls,
1267 struct CadetClient *c = cls;
1268 struct GNUNET_CADET_ClientChannelNumber ccn;
1269 struct CadetChannel *ch = value;
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 "Destroying %s, due to %s disconnecting.\n",
1275 ccn.channel_of_client = htonl (key);
1276 GCCH_channel_local_destroy (ch,
1279 GNUNET_assert (GNUNET_YES ==
1280 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1288 * Remove client's ports from the global hashmap on disconnect.
1290 * @param cls the `struct CadetClient`
1291 * @param port the port.
1292 * @param value the `struct OpenPort` to remove
1293 * @return #GNUNET_OK, keep iterating.
1296 client_release_ports (void *cls,
1297 const struct GNUNET_HashCode *port,
1300 struct CadetClient *c = cls;
1301 struct OpenPort *op = value;
1303 GNUNET_assert (c == op->c);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG,
1305 "Closing port %s due to %s disconnect.\n",
1308 GNUNET_assert (GNUNET_YES ==
1309 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1312 GNUNET_assert (GNUNET_YES ==
1313 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1322 * Callback called when a client disconnected from the service
1324 * @param cls closure for the service
1325 * @param client the client that disconnected
1326 * @param internal_cls should be equal to @a c
1329 client_disconnect_cb (void *cls,
1330 struct GNUNET_SERVICE_Client *client,
1333 struct CadetClient *c = internal_cls;
1335 GNUNET_assert (c->client == client);
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "%s is disconnecting.\n",
1339 if (NULL != c->channels)
1341 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1342 &channel_destroy_iterator,
1344 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels));
1345 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1347 if (NULL != c->ports)
1349 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1350 &client_release_ports,
1352 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1354 GNUNET_CONTAINER_DLL_remove (clients_head,
1357 GNUNET_STATISTICS_update (stats,
1362 if ( (NULL == clients_head) &&
1363 (GNUNET_YES == shutting_down) )
1369 * Setup CADET internals.
1371 * @param cls closure
1372 * @param server the initialized server
1373 * @param c configuration to use
1377 const struct GNUNET_CONFIGURATION_Handle *c,
1378 struct GNUNET_SERVICE_Handle *service)
1382 GNUNET_CONFIGURATION_get_value_number (c,
1387 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1390 "needs to be a number");
1391 ratchet_messages = 64;
1394 GNUNET_CONFIGURATION_get_value_time (c,
1399 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1402 "need delay value");
1403 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1406 GNUNET_CONFIGURATION_get_value_time (c,
1408 "REFRESH_CONNECTION_TIME",
1411 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1413 "REFRESH_CONNECTION_TIME",
1414 "need delay value");
1415 keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1418 GNUNET_CONFIGURATION_get_value_number (c,
1427 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1428 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1429 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1430 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1431 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1433 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1434 if (NULL == my_private_key)
1437 GNUNET_SCHEDULER_shutdown ();
1440 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1441 &my_full_id.public_key);
1442 stats = GNUNET_STATISTICS_create ("cadet",
1444 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1446 ats_ch = GNUNET_ATS_connectivity_init (c);
1447 /* FIXME: optimize code to allow GNUNET_YES here! */
1448 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1450 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1452 peers = GNUNET_CONTAINER_multipeermap_create (16,
1454 connections = GNUNET_CONTAINER_multishortmap_create (256,
1459 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1460 "CADET started for peer %s\n",
1461 GNUNET_i2s (&my_full_id));
1467 * Define "main" method using service macro.
1471 GNUNET_SERVICE_OPTION_NONE,
1474 &client_disconnect_cb,
1476 GNUNET_MQ_hd_fixed_size (port_open,
1477 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1478 struct GNUNET_CADET_PortMessage,
1480 GNUNET_MQ_hd_fixed_size (port_close,
1481 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1482 struct GNUNET_CADET_PortMessage,
1484 GNUNET_MQ_hd_fixed_size (channel_create,
1485 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1486 struct GNUNET_CADET_LocalChannelCreateMessage,
1488 GNUNET_MQ_hd_fixed_size (channel_destroy,
1489 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1490 struct GNUNET_CADET_LocalChannelDestroyMessage,
1492 GNUNET_MQ_hd_var_size (local_data,
1493 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1494 struct GNUNET_CADET_LocalData,
1496 GNUNET_MQ_hd_fixed_size (local_ack,
1497 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1498 struct GNUNET_CADET_LocalAck,
1500 GNUNET_MQ_hd_fixed_size (get_peers,
1501 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1502 struct GNUNET_MessageHeader,
1504 GNUNET_MQ_hd_fixed_size (show_peer,
1505 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1506 struct GNUNET_CADET_LocalInfo,
1508 GNUNET_MQ_hd_fixed_size (info_tunnels,
1509 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1510 struct GNUNET_MessageHeader,
1512 GNUNET_MQ_hd_fixed_size (info_tunnel,
1513 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1514 struct GNUNET_CADET_LocalInfo,
1516 GNUNET_MQ_hd_fixed_size (info_dump,
1517 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1518 struct GNUNET_MessageHeader,
1520 GNUNET_MQ_handler_end ());
1522 /* end of gnunet-service-cadet-new.c */