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;
193 * Send a message to a client.
195 * @param c client to get the message
196 * @param env envelope with the message
199 GSC_send_to_client (struct CadetClient *c,
200 struct GNUNET_MQ_Envelope *env)
202 GNUNET_MQ_send (c->mq,
208 * Return identifier for a client as a string.
210 * @param c client to identify
211 * @return string for debugging
214 GSC_2s (struct CadetClient *c)
218 GNUNET_snprintf (buf,
227 * Lookup channel of client @a c by @a ccn.
229 * @param c client to look in
230 * @param ccn channel ID to look up
231 * @return NULL if no such channel exists
233 static struct CadetChannel *
234 lookup_channel (struct CadetClient *c,
235 struct GNUNET_CADET_ClientChannelNumber ccn)
237 return GNUNET_CONTAINER_multihashmap32_get (c->channels,
238 ntohl (ccn.channel_of_client));
243 * Obtain the next LID to use for incoming connections to
246 * @param c client handle
248 static struct GNUNET_CADET_ClientChannelNumber
249 client_get_next_ccn (struct CadetClient *c)
251 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
253 /* increment until we have a free one... */
258 ccn.channel_of_client
259 = htonl (1 + (ntohl (ccn.channel_of_client)));
260 if (ntohl (ccn.channel_of_client) >=
261 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
262 ccn.channel_of_client = htonl (0);
264 c->next_ccn.channel_of_client
265 = htonl (1 + (ntohl (ccn.channel_of_client)));
271 * Bind incoming channel to this client, and notify client about
272 * incoming connection. Caller is responsible for notifying the other
273 * peer about our acceptance of the channel.
275 * @param c client to bind to
276 * @param ch channel to be bound
277 * @param dest peer that establishes the connection
278 * @param port port number
279 * @param options options
280 * @return local channel number assigned to the new client
282 struct GNUNET_CADET_ClientChannelNumber
283 GSC_bind (struct CadetClient *c,
284 struct CadetChannel *ch,
285 struct CadetPeer *dest,
286 const struct GNUNET_HashCode *port,
289 struct GNUNET_MQ_Envelope *env;
290 struct GNUNET_CADET_LocalChannelCreateMessage *cm;
291 struct GNUNET_CADET_ClientChannelNumber ccn;
293 ccn = client_get_next_ccn (c);
294 GNUNET_assert (GNUNET_YES ==
295 GNUNET_CONTAINER_multihashmap32_put (c->channels,
296 ntohl (ccn.channel_of_client),
298 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
299 LOG (GNUNET_ERROR_TYPE_DEBUG,
300 "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n",
305 ntohl (ccn.channel_of_client));
306 /* notify local client about incoming connection! */
307 env = GNUNET_MQ_msg (cm,
308 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
311 cm->opt = htonl (options);
312 cm->peer = *GCP_get_id (dest);
313 GSC_send_to_client (c,
320 * Callback invoked on all peers to destroy all tunnels
321 * that may still exist.
324 * @param pid identify of a peer
325 * @param value a `struct CadetPeer` that may still have a tunnel
326 * @return #GNUNET_OK (iterate over all entries)
329 destroy_tunnels_now (void *cls,
330 const struct GNUNET_PeerIdentity *pid,
333 struct CadetPeer *cp = value;
334 struct CadetTunnel *t = GCP_get_tunnel (cp,
338 GCT_destroy_tunnel_now (t);
344 * Callback invoked on all peers to destroy all tunnels
345 * that may still exist.
348 * @param pid identify of a peer
349 * @param value a `struct CadetPeer` that may still have a tunnel
350 * @return #GNUNET_OK (iterate over all entries)
353 destroy_paths_now (void *cls,
354 const struct GNUNET_PeerIdentity *pid,
357 struct CadetPeer *cp = value;
359 GCP_drop_owned_paths (cp);
365 * Shutdown everything once the clients have disconnected.
372 GNUNET_STATISTICS_destroy (stats,
376 if (NULL != open_ports)
378 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
381 if (NULL != loose_channels)
383 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
384 loose_channels = NULL;
386 /* Destroy tunnels. Note that all channels must be destroyed first! */
387 GCP_iterate_all (&destroy_tunnels_now,
389 /* All tunnels, channels, connections and CORE must be down before this point. */
390 GCP_iterate_all (&destroy_paths_now,
392 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
393 GCP_destroy_all_peers ();
396 GNUNET_CONTAINER_multipeermap_destroy (peers);
399 if (NULL != connections)
401 GNUNET_CONTAINER_multishortmap_destroy (connections);
406 GNUNET_ATS_connectivity_done (ats_ch);
411 GNUNET_free_non_null (my_private_key);
412 my_private_key = NULL;
417 * Task run during shutdown.
422 shutdown_task (void *cls)
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 shutting_down = GNUNET_YES;
428 if (NULL == clients_head)
434 * We had a remote connection @a value to port @a port before
435 * client @a cls opened port @a port. Bind them now.
437 * @param cls the `struct CadetClient`
438 * @param port the port
439 * @param value the `struct CadetChannel`
440 * @return #GNUNET_YES (iterate over all such channels)
443 bind_loose_channel (void *cls,
444 const struct GNUNET_HashCode *port,
447 struct CadetClient *c = cls;
448 struct CadetChannel *ch = value;
452 GNUNET_assert (GNUNET_YES ==
453 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
461 * Handle port open request. Creates a mapping from the
462 * port to the respective client and checks whether we have
463 * loose channels trying to bind to the port. If so, those
466 * @param cls Identification of the client.
467 * @param pmsg The actual message.
470 handle_port_open (void *cls,
471 const struct GNUNET_CADET_PortMessage *pmsg)
473 struct CadetClient *c = cls;
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Open port %s requested by %s\n",
477 GNUNET_h2s (&pmsg->port),
479 if (NULL == c->ports)
480 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
483 GNUNET_CONTAINER_multihashmap_put (c->ports,
486 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
489 GNUNET_SERVICE_client_drop (c->client);
492 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
495 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
496 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
500 GNUNET_SERVICE_client_continue (c->client);
505 * Handler for port close requests. Marks this port as closed
506 * (unless of course we have another client with the same port
507 * open). Note that existing channels accepted on the port are
510 * @param cls Identification of the client.
511 * @param pmsg The actual message.
514 handle_port_close (void *cls,
515 const struct GNUNET_CADET_PortMessage *pmsg)
517 struct CadetClient *c = cls;
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "Closing port %s as requested by %s\n",
521 GNUNET_h2s (&pmsg->port),
524 GNUNET_CONTAINER_multihashmap_remove (c->ports,
529 GNUNET_SERVICE_client_drop (c->client);
532 GNUNET_assert (GNUNET_YES ==
533 GNUNET_CONTAINER_multihashmap_remove (open_ports,
536 GNUNET_SERVICE_client_continue (c->client);
541 * Handler for requests for us creating a new channel to another peer and port.
543 * @param cls Identification of the client.
544 * @param tcm The actual message.
547 handle_channel_create (void *cls,
548 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
550 struct CadetClient *c = cls;
551 struct CadetChannel *ch;
553 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
555 /* Channel ID not in allowed range. */
557 GNUNET_SERVICE_client_drop (c->client);
560 ch = lookup_channel (c,
564 /* Channel ID already in use. Not allowed. */
566 GNUNET_SERVICE_client_drop (c->client);
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 "New channel to %s at port %s requested by %s\n",
571 GNUNET_i2s (&tcm->peer),
572 GNUNET_h2s (&tcm->port),
576 ch = GCCH_channel_local_new (c,
585 GNUNET_SERVICE_client_drop (c->client);
588 GNUNET_assert (GNUNET_YES ==
589 GNUNET_CONTAINER_multihashmap32_put (c->channels,
590 ntohl (tcm->ccn.channel_of_client),
592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
594 GNUNET_SERVICE_client_continue (c->client);
599 * Handler for requests of destroying an existing channel.
601 * @param cls client identification of the client
602 * @param msg the actual message
605 handle_channel_destroy (void *cls,
606 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
608 struct CadetClient *c = cls;
609 struct CadetChannel *ch;
611 ch = lookup_channel (c,
615 /* Client attempted to destroy unknown channel */
617 GNUNET_SERVICE_client_drop (c->client);
620 LOG (GNUNET_ERROR_TYPE_INFO,
621 "%s is destroying %s\n",
624 GNUNET_assert (GNUNET_YES ==
625 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
626 ntohl (msg->ccn.channel_of_client),
628 GCCH_channel_local_destroy (ch,
631 GNUNET_SERVICE_client_continue (c->client);
636 * Check for client traffic data message is well-formed.
638 * @param cls identification of the client
639 * @param msg the actual message
640 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
643 check_local_data (void *cls,
644 const struct GNUNET_CADET_LocalData *msg)
647 size_t payload_claimed_size;
649 struct GNUNET_MessageHeader pa;
651 /* FIXME: what is the format we shall allow for @a msg?
652 ONE payload item or multiple? Seems current cadet_api
653 at least in theory allows more than one. Next-gen
654 cadet_api will likely no more, so we could then
655 simplify this mess again. */
656 /* Sanity check for message size */
657 payload_size = ntohs (msg->header.size) - sizeof (*msg);
658 buf = (const char *) &msg[1];
659 while (payload_size >= sizeof (struct GNUNET_MessageHeader))
661 /* need to memcpy() for alignment */
665 payload_claimed_size = ntohs (pa.size);
666 if ( (payload_size < payload_claimed_size) ||
667 (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
668 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
671 LOG (GNUNET_ERROR_TYPE_DEBUG,
672 "Local data of %u total size had sub-message %u at %u with %u bytes\n",
673 ntohs (msg->header.size),
675 (unsigned int) (buf - (const char *) &msg[1]),
676 (unsigned int) payload_claimed_size);
677 return GNUNET_SYSERR;
679 payload_size -= payload_claimed_size;
680 buf += payload_claimed_size;
682 if (0 != payload_size)
685 return GNUNET_SYSERR;
692 * Handler for client payload traffic to be send on a channel to
695 * @param cls identification of the client
696 * @param msg the actual message
699 handle_local_data (void *cls,
700 const struct GNUNET_CADET_LocalData *msg)
702 struct CadetClient *c = cls;
703 struct CadetChannel *ch;
707 ch = lookup_channel (c,
711 /* Channel does not exist! */
713 GNUNET_SERVICE_client_drop (c->client);
716 payload_size = ntohs (msg->header.size) - sizeof (*msg);
717 buf = (const char *) &msg[1];
718 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "Received %u bytes payload from %s for %s\n",
720 (unsigned int) payload_size,
724 GCCH_handle_local_data (ch,
729 GNUNET_SERVICE_client_drop (c->client);
732 GNUNET_SERVICE_client_continue (c->client);
737 * Handler for client's ACKs for payload traffic.
739 * @param cls identification of the client.
740 * @param msg The actual message.
743 handle_local_ack (void *cls,
744 const struct GNUNET_CADET_LocalAck *msg)
746 struct CadetClient *c = cls;
747 struct CadetChannel *ch;
749 ch = lookup_channel (c,
753 /* Channel does not exist! */
755 GNUNET_SERVICE_client_drop (c->client);
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Got a local ACK from %s for %s\n",
762 GCCH_handle_local_ack (ch,
764 GNUNET_SERVICE_client_continue (c->client);
769 * Iterator over all peers to send a monitoring client info about each peer.
771 * @param cls Closure ().
772 * @param peer Peer ID (tunnel remote peer).
773 * @param value Peer info.
774 * @return #GNUNET_YES, to keep iterating.
777 get_all_peers_iterator (void *cls,
778 const struct GNUNET_PeerIdentity *peer,
781 struct CadetClient *c = cls;
782 struct CadetPeer *p = value;
783 struct GNUNET_MQ_Envelope *env;
784 struct GNUNET_CADET_LocalInfoPeer *msg;
786 env = GNUNET_MQ_msg (msg,
787 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
788 msg->destination = *peer;
789 msg->paths = htons (GCP_count_paths (p));
790 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
792 GNUNET_MQ_send (c->mq,
799 * Handler for client's INFO PEERS request.
801 * @param cls Identification of the client.
802 * @param message The actual message.
805 handle_get_peers (void *cls,
806 const struct GNUNET_MessageHeader *message)
808 struct CadetClient *c = cls;
809 struct GNUNET_MQ_Envelope *env;
810 struct GNUNET_MessageHeader *reply;
812 GCP_iterate_all (&get_all_peers_iterator,
814 env = GNUNET_MQ_msg (reply,
815 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
816 GNUNET_MQ_send (c->mq,
818 GNUNET_SERVICE_client_continue (c->client);
823 * Iterator over all paths of a peer to build an InfoPeer message.
824 * Message contains blocks of peers, first not included.
826 * @param cls message queue for transmission
827 * @param path Path itself
828 * @param off offset of the peer on @a path
829 * @return #GNUNET_YES if should keep iterating.
830 * #GNUNET_NO otherwise.
833 path_info_iterator (void *cls,
834 struct CadetPeerPath *path,
837 struct GNUNET_MQ_Handle *mq = cls;
838 struct GNUNET_MQ_Envelope *env;
839 struct GNUNET_MessageHeader *resp;
840 struct GNUNET_PeerIdentity *id;
843 unsigned int path_length;
845 path_length = GCPP_get_length (path);
846 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
847 if (sizeof (*resp) + path_size > UINT16_MAX)
849 LOG (GNUNET_ERROR_TYPE_WARNING,
850 "Path of %u entries is too long for info message\n",
854 env = GNUNET_MQ_msg_extra (resp,
856 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
857 id = (struct GNUNET_PeerIdentity *) &resp[1];
859 /* Don't copy first peer. First peer is always the local one. Last
860 * peer is always the destination (leave as 0, EOL).
862 for (i = 0; i < off; i++)
863 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
872 * Handler for client's SHOW_PEER request.
874 * @param cls Identification of the client.
875 * @param msg The actual message.
878 handle_show_peer (void *cls,
879 const struct GNUNET_CADET_LocalInfo *msg)
881 struct CadetClient *c = cls;
883 struct GNUNET_MQ_Envelope *env;
884 struct GNUNET_MessageHeader *resp;
886 p = GCP_get (&msg->peer,
889 GCP_iterate_paths (p,
892 /* Send message with 0/0 to indicate the end */
893 env = GNUNET_MQ_msg (resp,
894 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
895 GNUNET_MQ_send (c->mq,
897 GNUNET_SERVICE_client_continue (c->client);
902 * Iterator over all tunnels to send a monitoring client info about each tunnel.
904 * @param cls Closure ().
905 * @param peer Peer ID (tunnel remote peer).
906 * @param value a `struct CadetPeer`
907 * @return #GNUNET_YES, to keep iterating.
910 get_all_tunnels_iterator (void *cls,
911 const struct GNUNET_PeerIdentity *peer,
914 struct CadetClient *c = cls;
915 struct CadetPeer *p = value;
916 struct GNUNET_MQ_Envelope *env;
917 struct GNUNET_CADET_LocalInfoTunnel *msg;
918 struct CadetTunnel *t;
920 t = GCP_get_tunnel (p,
924 env = GNUNET_MQ_msg (msg,
925 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
926 msg->destination = *peer;
927 msg->channels = htonl (GCT_count_channels (t));
928 msg->connections = htonl (GCT_count_any_connections (t));
929 msg->cstate = htons (0);
930 msg->estate = htons ((uint16_t) GCT_get_estate (t));
931 GNUNET_MQ_send (c->mq,
938 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
940 * @param cls client Identification of the client.
941 * @param message The actual message.
944 handle_info_tunnels (void *cls,
945 const struct GNUNET_MessageHeader *message)
947 struct CadetClient *c = cls;
948 struct GNUNET_MQ_Envelope *env;
949 struct GNUNET_MessageHeader *reply;
951 GCP_iterate_all (&get_all_tunnels_iterator,
953 env = GNUNET_MQ_msg (reply,
954 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
955 GNUNET_MQ_send (c->mq,
957 GNUNET_SERVICE_client_continue (c->client);
962 * Update the message with information about the connection.
964 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
965 * @param c a connection about which we should store information in @a cls
968 iter_connection (void *cls,
969 struct CadetConnection *c)
971 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
972 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
974 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
975 h[msg->connections++] = *(GCC_get_id (c));
980 * Update the message with information about the channel.
982 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
983 * @param ch a channel about which we should store information in @a cls
986 iter_channel (void *cls,
987 struct CadetChannel *ch)
989 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
990 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
991 struct GNUNET_CADET_ChannelTunnelNumber *chn
992 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
994 chn[msg->channels++] = GCCH_get_id (ch);
999 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
1001 * @param cls Identification of the client.
1002 * @param msg The actual message.
1005 handle_info_tunnel (void *cls,
1006 const struct GNUNET_CADET_LocalInfo *msg)
1008 struct CadetClient *c = cls;
1009 struct GNUNET_MQ_Envelope *env;
1010 struct GNUNET_CADET_LocalInfoTunnel *resp;
1011 struct CadetTunnel *t;
1012 struct CadetPeer *p;
1016 p = GCP_get (&msg->peer,
1018 t = GCP_get_tunnel (p,
1022 /* We don't know the tunnel */
1023 struct GNUNET_MQ_Envelope *env;
1024 struct GNUNET_CADET_LocalInfoTunnel *warn;
1026 LOG (GNUNET_ERROR_TYPE_INFO,
1027 "Tunnel to %s unknown\n",
1028 GNUNET_i2s_full (&msg->peer));
1029 env = GNUNET_MQ_msg (warn,
1030 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1031 warn->destination = msg->peer;
1032 GNUNET_MQ_send (c->mq,
1034 GNUNET_SERVICE_client_continue (c->client);
1038 /* Initialize context */
1039 ch_n = GCT_count_channels (t);
1040 c_n = GCT_count_any_connections (t);
1041 env = GNUNET_MQ_msg_extra (resp,
1042 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1043 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1044 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1045 resp->destination = msg->peer;
1046 /* Do not reorder! #iter_channel needs counters in HBO! */
1047 GCT_iterate_connections (t,
1050 GCT_iterate_channels (t,
1053 resp->connections = htonl (resp->connections);
1054 resp->channels = htonl (resp->channels);
1055 resp->cstate = htons (0);
1056 resp->estate = htons (GCT_get_estate (t));
1057 GNUNET_MQ_send (c->mq,
1059 GNUNET_SERVICE_client_continue (c->client);
1064 * Iterator over all peers to dump info for each peer.
1066 * @param cls Closure (unused).
1067 * @param peer Peer ID (tunnel remote peer).
1068 * @param value Peer info.
1070 * @return #GNUNET_YES, to keep iterating.
1073 show_peer_iterator (void *cls,
1074 const struct GNUNET_PeerIdentity *peer,
1077 struct CadetPeer *p = value;
1078 struct CadetTunnel *t;
1080 t = GCP_get_tunnel (p,
1084 GNUNET_ERROR_TYPE_ERROR);
1085 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1091 * Handler for client's INFO_DUMP request.
1093 * @param cls Identification of the client.
1094 * @param message The actual message.
1097 handle_info_dump (void *cls,
1098 const struct GNUNET_MessageHeader *message)
1100 struct CadetClient *c = cls;
1102 LOG (GNUNET_ERROR_TYPE_INFO,
1103 "Received dump info request from client %u\n",
1106 LOG (GNUNET_ERROR_TYPE_ERROR,
1107 "*************************** DUMP START ***************************\n");
1108 for (struct CadetClient *ci = clients_head;
1112 LOG (GNUNET_ERROR_TYPE_ERROR,
1113 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1118 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1120 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1122 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1123 GCP_iterate_all (&show_peer_iterator,
1126 LOG (GNUNET_ERROR_TYPE_ERROR,
1127 "**************************** DUMP END ****************************\n");
1129 GNUNET_SERVICE_client_continue (c->client);
1135 * Callback called when a client connects to the service.
1137 * @param cls closure for the service
1138 * @param client the new client that connected to the service
1139 * @param mq the message queue used to send messages to the client
1143 client_connect_cb (void *cls,
1144 struct GNUNET_SERVICE_Client *client,
1145 struct GNUNET_MQ_Handle *mq)
1147 struct CadetClient *c;
1149 c = GNUNET_new (struct CadetClient);
1152 c->id = next_client_id++; /* overflow not important: just for debug */
1154 = GNUNET_CONTAINER_multihashmap32_create (32);
1155 GNUNET_CONTAINER_DLL_insert (clients_head,
1158 GNUNET_STATISTICS_update (stats,
1162 LOG (GNUNET_ERROR_TYPE_DEBUG,
1170 * A channel was destroyed by the other peer. Tell our client.
1172 * @param c client that lost a channel
1173 * @param ccn channel identification number for the client
1174 * @param ch the channel object
1177 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1178 struct GNUNET_CADET_ClientChannelNumber ccn,
1179 struct CadetChannel *ch)
1181 struct GNUNET_MQ_Envelope *env;
1182 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1184 env = GNUNET_MQ_msg (tdm,
1185 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1187 GSC_send_to_client (c,
1189 GNUNET_assert (GNUNET_YES ==
1190 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1191 ntohl (ccn.channel_of_client),
1197 * Iterator for deleting each channel whose client endpoint disconnected.
1199 * @param cls Closure (client that has disconnected).
1200 * @param key The local channel id in host byte order
1201 * @param value The value stored at the key (channel to destroy).
1202 * @return #GNUNET_OK, keep iterating.
1205 channel_destroy_iterator (void *cls,
1209 struct CadetClient *c = cls;
1210 struct GNUNET_CADET_ClientChannelNumber ccn;
1211 struct CadetChannel *ch = value;
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "Destroying %s, due to %s disconnecting.\n",
1217 GNUNET_assert (GNUNET_YES ==
1218 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1221 ccn.channel_of_client = htonl (key);
1222 GCCH_channel_local_destroy (ch,
1230 * Remove client's ports from the global hashmap on disconnect.
1232 * @param cls Closure (unused).
1233 * @param key the port.
1234 * @param value the `struct CadetClient` to remove
1235 * @return #GNUNET_OK, keep iterating.
1238 client_release_ports (void *cls,
1239 const struct GNUNET_HashCode *key,
1242 struct CadetClient *c = value;
1244 LOG (GNUNET_ERROR_TYPE_DEBUG,
1245 "Closing port %s due to %s disconnect.\n",
1248 GNUNET_assert (GNUNET_YES ==
1249 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1252 GNUNET_assert (GNUNET_YES ==
1253 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1261 * Callback called when a client disconnected from the service
1263 * @param cls closure for the service
1264 * @param client the client that disconnected
1265 * @param internal_cls should be equal to @a c
1268 client_disconnect_cb (void *cls,
1269 struct GNUNET_SERVICE_Client *client,
1272 struct CadetClient *c = internal_cls;
1274 GNUNET_assert (c->client == client);
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "%s is disconnecting.\n",
1278 if (NULL != c->channels)
1280 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1281 &channel_destroy_iterator,
1283 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1285 if (NULL != c->ports)
1287 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1288 &client_release_ports,
1290 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1292 GNUNET_CONTAINER_DLL_remove (clients_head,
1295 GNUNET_STATISTICS_update (stats,
1300 if ( (NULL == clients_head) &&
1301 (GNUNET_YES == shutting_down) )
1307 * Setup CADET internals.
1309 * @param cls closure
1310 * @param server the initialized server
1311 * @param c configuration to use
1315 const struct GNUNET_CONFIGURATION_Handle *c,
1316 struct GNUNET_SERVICE_Handle *service)
1320 GNUNET_CONFIGURATION_get_value_number (c,
1325 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1328 "needs to be a number");
1329 ratchet_messages = 64;
1332 GNUNET_CONFIGURATION_get_value_time (c,
1337 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1340 "need delay value");
1341 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1344 GNUNET_CONFIGURATION_get_value_time (c,
1346 "REFRESHC_CONNECTION_TIME",
1349 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1351 "REFRESH_CONNECTION_TIME",
1352 "need delay value");
1353 keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1356 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1357 if (NULL == my_private_key)
1360 GNUNET_SCHEDULER_shutdown ();
1363 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1364 &my_full_id.public_key);
1365 stats = GNUNET_STATISTICS_create ("cadet",
1367 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1369 ats_ch = GNUNET_ATS_connectivity_init (c);
1370 /* FIXME: optimize code to allow GNUNET_YES here! */
1371 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1373 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1375 peers = GNUNET_CONTAINER_multipeermap_create (16,
1377 connections = GNUNET_CONTAINER_multishortmap_create (256,
1382 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1383 "CADET started for peer %s\n",
1384 GNUNET_i2s (&my_full_id));
1390 * Define "main" method using service macro.
1394 GNUNET_SERVICE_OPTION_NONE,
1397 &client_disconnect_cb,
1399 GNUNET_MQ_hd_fixed_size (port_open,
1400 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1401 struct GNUNET_CADET_PortMessage,
1403 GNUNET_MQ_hd_fixed_size (port_close,
1404 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1405 struct GNUNET_CADET_PortMessage,
1407 GNUNET_MQ_hd_fixed_size (channel_create,
1408 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1409 struct GNUNET_CADET_LocalChannelCreateMessage,
1411 GNUNET_MQ_hd_fixed_size (channel_destroy,
1412 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1413 struct GNUNET_CADET_LocalChannelDestroyMessage,
1415 GNUNET_MQ_hd_var_size (local_data,
1416 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1417 struct GNUNET_CADET_LocalData,
1419 GNUNET_MQ_hd_fixed_size (local_ack,
1420 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1421 struct GNUNET_CADET_LocalAck,
1423 GNUNET_MQ_hd_fixed_size (get_peers,
1424 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1425 struct GNUNET_MessageHeader,
1427 GNUNET_MQ_hd_fixed_size (show_peer,
1428 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1429 struct GNUNET_CADET_LocalInfo,
1431 GNUNET_MQ_hd_fixed_size (info_tunnels,
1432 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1433 struct GNUNET_MessageHeader,
1435 GNUNET_MQ_hd_fixed_size (info_tunnel,
1436 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1437 struct GNUNET_CADET_LocalInfo,
1439 GNUNET_MQ_hd_fixed_size (info_dump,
1440 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1441 struct GNUNET_MessageHeader,
1443 GNUNET_MQ_handler_end ());
1445 /* end of gnunet-service-cadet-new.c */