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-new.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-new.h"
41 #include "gnunet-service-cadet-new_channel.h"
42 #include "gnunet-service-cadet-new_connection.h"
43 #include "gnunet-service-cadet-new_core.h"
44 #include "gnunet-service-cadet-new_dht.h"
45 #include "gnunet-service-cadet-new_hello.h"
46 #include "gnunet-service-cadet-new_tunnels.h"
47 #include "gnunet-service-cadet-new_peer.h"
48 #include "gnunet-service-cadet-new_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 *Client.
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.
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.
156 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
159 * Map from ports to channels where the ports were closed at the
160 * time we got the inbound connection.
161 * Indexed by port, contains `struct CadetChannel`.
163 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
166 * Map from PIDs to `struct CadetPeer` entries.
168 struct GNUNET_CONTAINER_MultiPeerMap *peers;
171 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
172 * hash codes to `struct CadetConnection` objects.
174 struct GNUNET_CONTAINER_MultiShortmap *connections;
177 * How many messages are needed to trigger an AXOLOTL ratchet advance.
179 unsigned long long ratchet_messages;
182 * How long until we trigger a ratched advance due to time.
184 struct GNUNET_TIME_Relative ratchet_time;
187 * How frequently do we send KEEPALIVE messages on idle connections?
189 struct GNUNET_TIME_Relative keepalive_period;
192 * Set to non-zero values to create random drops to test retransmissions.
194 unsigned long long drop_percent;
198 * Send a message to a client.
200 * @param c client to get the message
201 * @param env envelope with the message
204 GSC_send_to_client (struct CadetClient *c,
205 struct GNUNET_MQ_Envelope *env)
207 GNUNET_MQ_send (c->mq,
213 * Return identifier for a client as a string.
215 * @param c client to identify
216 * @return string for debugging
219 GSC_2s (struct CadetClient *c)
223 GNUNET_snprintf (buf,
232 * Lookup channel of client @a c by @a ccn.
234 * @param c client to look in
235 * @param ccn channel ID to look up
236 * @return NULL if no such channel exists
238 static struct CadetChannel *
239 lookup_channel (struct CadetClient *c,
240 struct GNUNET_CADET_ClientChannelNumber ccn)
242 return GNUNET_CONTAINER_multihashmap32_get (c->channels,
243 ntohl (ccn.channel_of_client));
248 * Obtain the next LID to use for incoming connections to
251 * @param c client handle
253 static struct GNUNET_CADET_ClientChannelNumber
254 client_get_next_ccn (struct CadetClient *c)
256 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
258 /* increment until we have a free one... */
263 ccn.channel_of_client
264 = htonl (1 + (ntohl (ccn.channel_of_client)));
265 if (ntohl (ccn.channel_of_client) >=
266 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
267 ccn.channel_of_client = htonl (0);
269 c->next_ccn.channel_of_client
270 = htonl (1 + (ntohl (ccn.channel_of_client)));
276 * Bind incoming channel to this client, and notify client about
277 * incoming connection. Caller is responsible for notifying the other
278 * peer about our acceptance of the channel.
280 * @param c client to bind to
281 * @param ch channel to be bound
282 * @param dest peer that establishes the connection
283 * @param port port number
284 * @param options options
285 * @return local channel number assigned to the new client
287 struct GNUNET_CADET_ClientChannelNumber
288 GSC_bind (struct CadetClient *c,
289 struct CadetChannel *ch,
290 struct CadetPeer *dest,
291 const struct GNUNET_HashCode *port,
294 struct GNUNET_MQ_Envelope *env;
295 struct GNUNET_CADET_LocalChannelCreateMessage *cm;
296 struct GNUNET_CADET_ClientChannelNumber ccn;
298 ccn = client_get_next_ccn (c);
299 GNUNET_assert (GNUNET_YES ==
300 GNUNET_CONTAINER_multihashmap32_put (c->channels,
301 ntohl (ccn.channel_of_client),
303 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n",
310 ntohl (ccn.channel_of_client));
311 /* notify local client about incoming connection! */
312 env = GNUNET_MQ_msg (cm,
313 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
316 cm->opt = htonl (options);
317 cm->peer = *GCP_get_id (dest);
318 GSC_send_to_client (c,
325 * Callback invoked on all peers to destroy all tunnels
326 * that may still exist.
329 * @param pid identify of a peer
330 * @param value a `struct CadetPeer` that may still have a tunnel
331 * @return #GNUNET_OK (iterate over all entries)
334 destroy_tunnels_now (void *cls,
335 const struct GNUNET_PeerIdentity *pid,
338 struct CadetPeer *cp = value;
339 struct CadetTunnel *t = GCP_get_tunnel (cp,
343 GCT_destroy_tunnel_now (t);
349 * Callback invoked on all peers to destroy all tunnels
350 * that may still exist.
353 * @param pid identify of a peer
354 * @param value a `struct CadetPeer` that may still have a tunnel
355 * @return #GNUNET_OK (iterate over all entries)
358 destroy_paths_now (void *cls,
359 const struct GNUNET_PeerIdentity *pid,
362 struct CadetPeer *cp = value;
364 GCP_drop_owned_paths (cp);
370 * Shutdown everything once the clients have disconnected.
377 GNUNET_STATISTICS_destroy (stats,
381 if (NULL != open_ports)
383 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
386 if (NULL != loose_channels)
388 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
389 loose_channels = NULL;
391 /* Destroy tunnels. Note that all channels must be destroyed first! */
392 GCP_iterate_all (&destroy_tunnels_now,
394 /* All tunnels, channels, connections and CORE must be down before this point. */
395 GCP_iterate_all (&destroy_paths_now,
397 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
398 GCP_destroy_all_peers ();
401 GNUNET_CONTAINER_multipeermap_destroy (peers);
404 if (NULL != connections)
406 GNUNET_CONTAINER_multishortmap_destroy (connections);
411 GNUNET_ATS_connectivity_done (ats_ch);
416 GNUNET_free_non_null (my_private_key);
417 my_private_key = NULL;
422 * Task run during shutdown.
427 shutdown_task (void *cls)
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 shutting_down = GNUNET_YES;
433 if (NULL == clients_head)
439 * We had a remote connection @a value to port @a port before
440 * client @a cls opened port @a port. Bind them now.
442 * @param cls the `struct CadetClient`
443 * @param port the port
444 * @param value the `struct CadetChannel`
445 * @return #GNUNET_YES (iterate over all such channels)
448 bind_loose_channel (void *cls,
449 const struct GNUNET_HashCode *port,
452 struct CadetClient *c = cls;
453 struct CadetChannel *ch = value;
457 GNUNET_assert (GNUNET_YES ==
458 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
466 * Handle port open request. Creates a mapping from the
467 * port to the respective client and checks whether we have
468 * loose channels trying to bind to the port. If so, those
471 * @param cls Identification of the client.
472 * @param pmsg The actual message.
475 handle_port_open (void *cls,
476 const struct GNUNET_CADET_PortMessage *pmsg)
478 struct CadetClient *c = cls;
480 LOG (GNUNET_ERROR_TYPE_DEBUG,
481 "Open port %s requested by %s\n",
482 GNUNET_h2s (&pmsg->port),
484 if (NULL == c->ports)
485 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
488 GNUNET_CONTAINER_multihashmap_put (c->ports,
491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
494 GNUNET_SERVICE_client_drop (c->client);
497 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
501 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
505 GNUNET_SERVICE_client_continue (c->client);
510 * Handler for port close requests. Marks this port as closed
511 * (unless of course we have another client with the same port
512 * open). Note that existing channels accepted on the port are
515 * @param cls Identification of the client.
516 * @param pmsg The actual message.
519 handle_port_close (void *cls,
520 const struct GNUNET_CADET_PortMessage *pmsg)
522 struct CadetClient *c = cls;
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
525 "Closing port %s as requested by %s\n",
526 GNUNET_h2s (&pmsg->port),
529 GNUNET_CONTAINER_multihashmap_remove (c->ports,
534 GNUNET_SERVICE_client_drop (c->client);
537 GNUNET_assert (GNUNET_YES ==
538 GNUNET_CONTAINER_multihashmap_remove (open_ports,
541 GNUNET_SERVICE_client_continue (c->client);
546 * Handler for requests for us creating a new channel to another peer and port.
548 * @param cls Identification of the client.
549 * @param tcm The actual message.
552 handle_channel_create (void *cls,
553 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
555 struct CadetClient *c = cls;
556 struct CadetChannel *ch;
558 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
560 /* Channel ID not in allowed range. */
562 GNUNET_SERVICE_client_drop (c->client);
565 ch = lookup_channel (c,
569 /* Channel ID already in use. Not allowed. */
571 GNUNET_SERVICE_client_drop (c->client);
574 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 "New channel to %s at port %s requested by %s\n",
576 GNUNET_i2s (&tcm->peer),
577 GNUNET_h2s (&tcm->port),
581 ch = GCCH_channel_local_new (c,
590 GNUNET_SERVICE_client_drop (c->client);
593 GNUNET_assert (GNUNET_YES ==
594 GNUNET_CONTAINER_multihashmap32_put (c->channels,
595 ntohl (tcm->ccn.channel_of_client),
597 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
599 GNUNET_SERVICE_client_continue (c->client);
604 * Handler for requests of destroying an existing channel.
606 * @param cls client identification of the client
607 * @param msg the actual message
610 handle_channel_destroy (void *cls,
611 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
613 struct CadetClient *c = cls;
614 struct CadetChannel *ch;
616 ch = lookup_channel (c,
620 /* Client attempted to destroy unknown channel.
621 Can happen if the other side went down at the same time.*/
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
623 "%s tried to destroy unknown channel %X\n",
625 ntohl (msg->ccn.channel_of_client));
628 LOG (GNUNET_ERROR_TYPE_DEBUG,
629 "%s is destroying %s\n",
632 GNUNET_assert (GNUNET_YES ==
633 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
634 ntohl (msg->ccn.channel_of_client),
636 GCCH_channel_local_destroy (ch,
639 GNUNET_SERVICE_client_continue (c->client);
644 * Check for client traffic data message is well-formed.
646 * @param cls identification of the client
647 * @param msg the actual message
648 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
651 check_local_data (void *cls,
652 const struct GNUNET_CADET_LocalData *msg)
655 size_t payload_claimed_size;
657 struct GNUNET_MessageHeader pa;
659 /* FIXME: what is the format we shall allow for @a msg?
660 ONE payload item or multiple? Seems current cadet_api
661 at least in theory allows more than one. Next-gen
662 cadet_api will likely no more, so we could then
663 simplify this mess again. */
664 /* Sanity check for message size */
665 payload_size = ntohs (msg->header.size) - sizeof (*msg);
666 buf = (const char *) &msg[1];
667 while (payload_size >= sizeof (struct GNUNET_MessageHeader))
669 /* need to memcpy() for alignment */
673 payload_claimed_size = ntohs (pa.size);
674 if ( (payload_size < payload_claimed_size) ||
675 (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
676 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "Local data of %u total size had sub-message %u at %u with %u bytes\n",
681 ntohs (msg->header.size),
683 (unsigned int) (buf - (const char *) &msg[1]),
684 (unsigned int) payload_claimed_size);
685 return GNUNET_SYSERR;
687 payload_size -= payload_claimed_size;
688 buf += payload_claimed_size;
690 if (0 != payload_size)
693 return GNUNET_SYSERR;
700 * Handler for client payload traffic to be send on a channel to
703 * @param cls identification of the client
704 * @param msg the actual message
707 handle_local_data (void *cls,
708 const struct GNUNET_CADET_LocalData *msg)
710 struct CadetClient *c = cls;
711 struct CadetChannel *ch;
715 ch = lookup_channel (c,
719 /* Channel does not exist! */
721 GNUNET_SERVICE_client_drop (c->client);
724 payload_size = ntohs (msg->header.size) - sizeof (*msg);
725 buf = (const char *) &msg[1];
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "Received %u bytes payload from %s for %s\n",
728 (unsigned int) payload_size,
732 GCCH_handle_local_data (ch,
737 GNUNET_SERVICE_client_drop (c->client);
740 GNUNET_SERVICE_client_continue (c->client);
745 * Handler for client's ACKs for payload traffic.
747 * @param cls identification of the client.
748 * @param msg The actual message.
751 handle_local_ack (void *cls,
752 const struct GNUNET_CADET_LocalAck *msg)
754 struct CadetClient *c = cls;
755 struct CadetChannel *ch;
757 ch = lookup_channel (c,
761 /* Channel does not exist! */
763 GNUNET_SERVICE_client_drop (c->client);
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "Got a local ACK from %s for %s\n",
770 GCCH_handle_local_ack (ch,
772 GNUNET_SERVICE_client_continue (c->client);
777 * Iterator over all peers to send a monitoring client info about each peer.
779 * @param cls Closure ().
780 * @param peer Peer ID (tunnel remote peer).
781 * @param value Peer info.
782 * @return #GNUNET_YES, to keep iterating.
785 get_all_peers_iterator (void *cls,
786 const struct GNUNET_PeerIdentity *peer,
789 struct CadetClient *c = cls;
790 struct CadetPeer *p = value;
791 struct GNUNET_MQ_Envelope *env;
792 struct GNUNET_CADET_LocalInfoPeer *msg;
794 env = GNUNET_MQ_msg (msg,
795 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
796 msg->destination = *peer;
797 msg->paths = htons (GCP_count_paths (p));
798 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
800 GNUNET_MQ_send (c->mq,
807 * Handler for client's INFO PEERS request.
809 * @param cls Identification of the client.
810 * @param message The actual message.
813 handle_get_peers (void *cls,
814 const struct GNUNET_MessageHeader *message)
816 struct CadetClient *c = cls;
817 struct GNUNET_MQ_Envelope *env;
818 struct GNUNET_MessageHeader *reply;
820 GCP_iterate_all (&get_all_peers_iterator,
822 env = GNUNET_MQ_msg (reply,
823 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
824 GNUNET_MQ_send (c->mq,
826 GNUNET_SERVICE_client_continue (c->client);
831 * Iterator over all paths of a peer to build an InfoPeer message.
832 * Message contains blocks of peers, first not included.
834 * @param cls message queue for transmission
835 * @param path Path itself
836 * @param off offset of the peer on @a path
837 * @return #GNUNET_YES if should keep iterating.
838 * #GNUNET_NO otherwise.
841 path_info_iterator (void *cls,
842 struct CadetPeerPath *path,
845 struct GNUNET_MQ_Handle *mq = cls;
846 struct GNUNET_MQ_Envelope *env;
847 struct GNUNET_MessageHeader *resp;
848 struct GNUNET_PeerIdentity *id;
851 unsigned int path_length;
853 path_length = GCPP_get_length (path);
854 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
855 if (sizeof (*resp) + path_size > UINT16_MAX)
857 LOG (GNUNET_ERROR_TYPE_WARNING,
858 "Path of %u entries is too long for info message\n",
862 env = GNUNET_MQ_msg_extra (resp,
864 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
865 id = (struct GNUNET_PeerIdentity *) &resp[1];
867 /* Don't copy first peer. First peer is always the local one. Last
868 * peer is always the destination (leave as 0, EOL).
870 for (i = 0; i < off; i++)
871 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
880 * Handler for client's SHOW_PEER request.
882 * @param cls Identification of the client.
883 * @param msg The actual message.
886 handle_show_peer (void *cls,
887 const struct GNUNET_CADET_LocalInfo *msg)
889 struct CadetClient *c = cls;
891 struct GNUNET_MQ_Envelope *env;
892 struct GNUNET_MessageHeader *resp;
894 p = GCP_get (&msg->peer,
897 GCP_iterate_paths (p,
900 /* Send message with 0/0 to indicate the end */
901 env = GNUNET_MQ_msg (resp,
902 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
903 GNUNET_MQ_send (c->mq,
905 GNUNET_SERVICE_client_continue (c->client);
910 * Iterator over all tunnels to send a monitoring client info about each tunnel.
912 * @param cls Closure ().
913 * @param peer Peer ID (tunnel remote peer).
914 * @param value a `struct CadetPeer`
915 * @return #GNUNET_YES, to keep iterating.
918 get_all_tunnels_iterator (void *cls,
919 const struct GNUNET_PeerIdentity *peer,
922 struct CadetClient *c = cls;
923 struct CadetPeer *p = value;
924 struct GNUNET_MQ_Envelope *env;
925 struct GNUNET_CADET_LocalInfoTunnel *msg;
926 struct CadetTunnel *t;
928 t = GCP_get_tunnel (p,
932 env = GNUNET_MQ_msg (msg,
933 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
934 msg->destination = *peer;
935 msg->channels = htonl (GCT_count_channels (t));
936 msg->connections = htonl (GCT_count_any_connections (t));
937 msg->cstate = htons (0);
938 msg->estate = htons ((uint16_t) GCT_get_estate (t));
939 GNUNET_MQ_send (c->mq,
946 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
948 * @param cls client Identification of the client.
949 * @param message The actual message.
952 handle_info_tunnels (void *cls,
953 const struct GNUNET_MessageHeader *message)
955 struct CadetClient *c = cls;
956 struct GNUNET_MQ_Envelope *env;
957 struct GNUNET_MessageHeader *reply;
959 GCP_iterate_all (&get_all_tunnels_iterator,
961 env = GNUNET_MQ_msg (reply,
962 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
963 GNUNET_MQ_send (c->mq,
965 GNUNET_SERVICE_client_continue (c->client);
970 * Update the message with information about the connection.
972 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
973 * @param ct a connection about which we should store information in @a cls
976 iter_connection (void *cls,
977 struct CadetTConnection *ct)
979 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
980 struct CadetConnection *cc = ct->cc;
981 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
983 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
984 h[msg->connections++] = *(GCC_get_id (cc));
989 * Update the message with information about the channel.
991 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
992 * @param ch a channel about which we should store information in @a cls
995 iter_channel (void *cls,
996 struct CadetChannel *ch)
998 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
999 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1000 struct GNUNET_CADET_ChannelTunnelNumber *chn
1001 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
1003 chn[msg->channels++] = GCCH_get_id (ch);
1008 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
1010 * @param cls Identification of the client.
1011 * @param msg The actual message.
1014 handle_info_tunnel (void *cls,
1015 const struct GNUNET_CADET_LocalInfo *msg)
1017 struct CadetClient *c = cls;
1018 struct GNUNET_MQ_Envelope *env;
1019 struct GNUNET_CADET_LocalInfoTunnel *resp;
1020 struct CadetTunnel *t;
1021 struct CadetPeer *p;
1025 p = GCP_get (&msg->peer,
1027 t = GCP_get_tunnel (p,
1031 /* We don't know the tunnel */
1032 struct GNUNET_MQ_Envelope *env;
1033 struct GNUNET_CADET_LocalInfoTunnel *warn;
1035 LOG (GNUNET_ERROR_TYPE_INFO,
1036 "Tunnel to %s unknown\n",
1037 GNUNET_i2s_full (&msg->peer));
1038 env = GNUNET_MQ_msg (warn,
1039 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1040 warn->destination = msg->peer;
1041 GNUNET_MQ_send (c->mq,
1043 GNUNET_SERVICE_client_continue (c->client);
1047 /* Initialize context */
1048 ch_n = GCT_count_channels (t);
1049 c_n = GCT_count_any_connections (t);
1050 env = GNUNET_MQ_msg_extra (resp,
1051 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1052 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1053 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1054 resp->destination = msg->peer;
1055 /* Do not reorder! #iter_channel needs counters in HBO! */
1056 GCT_iterate_connections (t,
1059 GCT_iterate_channels (t,
1062 resp->connections = htonl (resp->connections);
1063 resp->channels = htonl (resp->channels);
1064 resp->cstate = htons (0);
1065 resp->estate = htons (GCT_get_estate (t));
1066 GNUNET_MQ_send (c->mq,
1068 GNUNET_SERVICE_client_continue (c->client);
1073 * Iterator over all peers to dump info for each peer.
1075 * @param cls Closure (unused).
1076 * @param peer Peer ID (tunnel remote peer).
1077 * @param value Peer info.
1079 * @return #GNUNET_YES, to keep iterating.
1082 show_peer_iterator (void *cls,
1083 const struct GNUNET_PeerIdentity *peer,
1086 struct CadetPeer *p = value;
1087 struct CadetTunnel *t;
1089 t = GCP_get_tunnel (p,
1093 GNUNET_ERROR_TYPE_ERROR);
1094 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1100 * Handler for client's INFO_DUMP request.
1102 * @param cls Identification of the client.
1103 * @param message The actual message.
1106 handle_info_dump (void *cls,
1107 const struct GNUNET_MessageHeader *message)
1109 struct CadetClient *c = cls;
1111 LOG (GNUNET_ERROR_TYPE_INFO,
1112 "Received dump info request from client %u\n",
1115 LOG (GNUNET_ERROR_TYPE_ERROR,
1116 "*************************** DUMP START ***************************\n");
1117 for (struct CadetClient *ci = clients_head;
1121 LOG (GNUNET_ERROR_TYPE_ERROR,
1122 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1127 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1129 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1131 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1132 GCP_iterate_all (&show_peer_iterator,
1135 LOG (GNUNET_ERROR_TYPE_ERROR,
1136 "**************************** DUMP END ****************************\n");
1138 GNUNET_SERVICE_client_continue (c->client);
1144 * Callback called when a client connects to the service.
1146 * @param cls closure for the service
1147 * @param client the new client that connected to the service
1148 * @param mq the message queue used to send messages to the client
1152 client_connect_cb (void *cls,
1153 struct GNUNET_SERVICE_Client *client,
1154 struct GNUNET_MQ_Handle *mq)
1156 struct CadetClient *c;
1158 c = GNUNET_new (struct CadetClient);
1161 c->id = next_client_id++; /* overflow not important: just for debug */
1163 = GNUNET_CONTAINER_multihashmap32_create (32);
1164 GNUNET_CONTAINER_DLL_insert (clients_head,
1167 GNUNET_STATISTICS_update (stats,
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1179 * A channel was destroyed by the other peer. Tell our client.
1181 * @param c client that lost a channel
1182 * @param ccn channel identification number for the client
1183 * @param ch the channel object
1186 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1187 struct GNUNET_CADET_ClientChannelNumber ccn,
1188 struct CadetChannel *ch)
1190 struct GNUNET_MQ_Envelope *env;
1191 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1193 env = GNUNET_MQ_msg (tdm,
1194 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1196 GSC_send_to_client (c,
1198 GNUNET_assert (GNUNET_YES ==
1199 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1200 ntohl (ccn.channel_of_client),
1206 * A client that created a loose channel that was not bound to a port
1207 * disconnected, drop it from the #loose_channels list.
1209 * @param port the port the channel was trying to bind to
1210 * @param ch the channel that was lost
1213 GSC_drop_loose_channel (const struct GNUNET_HashCode *port,
1214 struct CadetChannel *ch)
1216 GNUNET_assert (GNUNET_YES ==
1217 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
1224 * Iterator for deleting each channel whose client endpoint disconnected.
1226 * @param cls Closure (client that has disconnected).
1227 * @param key The local channel id in host byte order
1228 * @param value The value stored at the key (channel to destroy).
1229 * @return #GNUNET_OK, keep iterating.
1232 channel_destroy_iterator (void *cls,
1236 struct CadetClient *c = cls;
1237 struct GNUNET_CADET_ClientChannelNumber ccn;
1238 struct CadetChannel *ch = value;
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "Destroying %s, due to %s disconnecting.\n",
1244 ccn.channel_of_client = htonl (key);
1245 GCCH_channel_local_destroy (ch,
1248 GNUNET_assert (GNUNET_YES ==
1249 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1257 * Remove client's ports from the global hashmap on disconnect.
1259 * @param cls Closure (unused).
1260 * @param key the port.
1261 * @param value the `struct CadetClient` to remove
1262 * @return #GNUNET_OK, keep iterating.
1265 client_release_ports (void *cls,
1266 const struct GNUNET_HashCode *key,
1269 struct CadetClient *c = value;
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 "Closing port %s due to %s disconnect.\n",
1275 GNUNET_assert (GNUNET_YES ==
1276 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1279 GNUNET_assert (GNUNET_YES ==
1280 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1288 * Callback called when a client disconnected from the service
1290 * @param cls closure for the service
1291 * @param client the client that disconnected
1292 * @param internal_cls should be equal to @a c
1295 client_disconnect_cb (void *cls,
1296 struct GNUNET_SERVICE_Client *client,
1299 struct CadetClient *c = internal_cls;
1301 GNUNET_assert (c->client == client);
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "%s is disconnecting.\n",
1305 if (NULL != c->channels)
1307 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1308 &channel_destroy_iterator,
1310 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels));
1311 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1313 if (NULL != c->ports)
1315 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1316 &client_release_ports,
1318 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1320 GNUNET_CONTAINER_DLL_remove (clients_head,
1323 GNUNET_STATISTICS_update (stats,
1328 if ( (NULL == clients_head) &&
1329 (GNUNET_YES == shutting_down) )
1335 * Setup CADET internals.
1337 * @param cls closure
1338 * @param server the initialized server
1339 * @param c configuration to use
1343 const struct GNUNET_CONFIGURATION_Handle *c,
1344 struct GNUNET_SERVICE_Handle *service)
1348 GNUNET_CONFIGURATION_get_value_number (c,
1353 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1356 "needs to be a number");
1357 ratchet_messages = 64;
1360 GNUNET_CONFIGURATION_get_value_time (c,
1365 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1368 "need delay value");
1369 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1372 GNUNET_CONFIGURATION_get_value_time (c,
1374 "REFRESH_CONNECTION_TIME",
1377 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1379 "REFRESH_CONNECTION_TIME",
1380 "need delay value");
1381 keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1384 GNUNET_CONFIGURATION_get_value_number (c,
1393 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1394 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1395 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1396 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1397 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1399 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1400 if (NULL == my_private_key)
1403 GNUNET_SCHEDULER_shutdown ();
1406 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1407 &my_full_id.public_key);
1408 stats = GNUNET_STATISTICS_create ("cadet",
1410 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1412 ats_ch = GNUNET_ATS_connectivity_init (c);
1413 /* FIXME: optimize code to allow GNUNET_YES here! */
1414 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1416 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1418 peers = GNUNET_CONTAINER_multipeermap_create (16,
1420 connections = GNUNET_CONTAINER_multishortmap_create (256,
1425 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1426 "CADET started for peer %s\n",
1427 GNUNET_i2s (&my_full_id));
1433 * Define "main" method using service macro.
1437 GNUNET_SERVICE_OPTION_NONE,
1440 &client_disconnect_cb,
1442 GNUNET_MQ_hd_fixed_size (port_open,
1443 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1444 struct GNUNET_CADET_PortMessage,
1446 GNUNET_MQ_hd_fixed_size (port_close,
1447 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1448 struct GNUNET_CADET_PortMessage,
1450 GNUNET_MQ_hd_fixed_size (channel_create,
1451 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1452 struct GNUNET_CADET_LocalChannelCreateMessage,
1454 GNUNET_MQ_hd_fixed_size (channel_destroy,
1455 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1456 struct GNUNET_CADET_LocalChannelDestroyMessage,
1458 GNUNET_MQ_hd_var_size (local_data,
1459 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1460 struct GNUNET_CADET_LocalData,
1462 GNUNET_MQ_hd_fixed_size (local_ack,
1463 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1464 struct GNUNET_CADET_LocalAck,
1466 GNUNET_MQ_hd_fixed_size (get_peers,
1467 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1468 struct GNUNET_MessageHeader,
1470 GNUNET_MQ_hd_fixed_size (show_peer,
1471 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1472 struct GNUNET_CADET_LocalInfo,
1474 GNUNET_MQ_hd_fixed_size (info_tunnels,
1475 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1476 struct GNUNET_MessageHeader,
1478 GNUNET_MQ_hd_fixed_size (info_tunnel,
1479 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1480 struct GNUNET_CADET_LocalInfo,
1482 GNUNET_MQ_hd_fixed_size (info_dump,
1483 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1484 struct GNUNET_MessageHeader,
1486 GNUNET_MQ_handler_end ());
1488 /* end of gnunet-service-cadet-new.c */