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 recover 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;
188 * Send a message to a client.
190 * @param c client to get the message
191 * @param env envelope with the message
194 GSC_send_to_client (struct CadetClient *c,
195 struct GNUNET_MQ_Envelope *env)
197 GNUNET_MQ_send (c->mq,
203 * Return identifier for a client as a string.
205 * @param c client to identify
206 * @return string for debugging
209 GSC_2s (struct CadetClient *c)
213 GNUNET_snprintf (buf,
222 * Lookup channel of client @a c by @a ccn.
224 * @param c client to look in
225 * @param ccn channel ID to look up
226 * @return NULL if no such channel exists
228 static struct CadetChannel *
229 lookup_channel (struct CadetClient *c,
230 struct GNUNET_CADET_ClientChannelNumber ccn)
232 return GNUNET_CONTAINER_multihashmap32_get (c->channels,
233 ntohl (ccn.channel_of_client));
238 * Obtain the next LID to use for incoming connections to
241 * @param c client handle
243 static struct GNUNET_CADET_ClientChannelNumber
244 client_get_next_ccn (struct CadetClient *c)
246 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
248 /* increment until we have a free one... */
253 ccn.channel_of_client
254 = htonl (1 + (ntohl (ccn.channel_of_client)));
255 if (ntohl (ccn.channel_of_client) >=
256 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
257 ccn.channel_of_client = htonl (0);
259 c->next_ccn.channel_of_client
260 = htonl (1 + (ntohl (ccn.channel_of_client)));
266 * Bind incoming channel to this client, and notify client about
267 * incoming connection. Caller is responsible for notifying the other
268 * peer about our acceptance of the channel.
270 * @param c client to bind to
271 * @param ch channel to be bound
272 * @param dest peer that establishes the connection
273 * @param port port number
274 * @param options options
275 * @return local channel number assigned to the new client
277 struct GNUNET_CADET_ClientChannelNumber
278 GSC_bind (struct CadetClient *c,
279 struct CadetChannel *ch,
280 struct CadetPeer *dest,
281 const struct GNUNET_HashCode *port,
284 struct GNUNET_MQ_Envelope *env;
285 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
286 struct GNUNET_CADET_ClientChannelNumber ccn;
288 ccn = client_get_next_ccn (c);
289 GNUNET_assert (GNUNET_YES ==
290 GNUNET_CONTAINER_multihashmap32_put (c->channels,
291 ntohl (ccn.channel_of_client),
293 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
294 LOG (GNUNET_ERROR_TYPE_DEBUG,
295 "Accepting incoming channel %s from %s on open port %s (%u)\n",
300 /* notify local client about incoming connection! */
301 env = GNUNET_MQ_msg (msg,
302 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
305 msg->opt = htonl (options);
306 msg->peer = *GCP_get_id (dest);
307 GSC_send_to_client (c,
314 * Task run during shutdown.
319 shutdown_task (void *cls)
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 shutting_down = GNUNET_YES;
327 GNUNET_STATISTICS_destroy (stats,
331 if (NULL != open_ports)
333 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
336 if (NULL != loose_channels)
338 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
339 loose_channels = NULL;
341 /* All channels, connections and CORE must be down before this point. */
342 GCP_destroy_all_peers ();
345 GNUNET_CONTAINER_multipeermap_destroy (peers);
348 if (NULL != connections)
350 GNUNET_CONTAINER_multishortmap_destroy (connections);
355 GNUNET_ATS_connectivity_done (ats_ch);
360 GNUNET_free_non_null (my_private_key);
361 my_private_key = NULL;
366 * We had a remote connection @a value to port @a port before
367 * client @a cls opened port @a port. Bind them now.
369 * @param cls the `struct CadetClient`
370 * @param port the port
371 * @param value the `struct CadetChannel`
372 * @return #GNUNET_YES (iterate over all such channels)
375 bind_loose_channel (void *cls,
376 const struct GNUNET_HashCode *port,
379 struct CadetClient *c = cls;
380 struct CadetChannel *ch = value;
384 GNUNET_assert (GNUNET_YES ==
385 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
393 * Handle port open request. Creates a mapping from the
394 * port to the respective client and checks whether we have
395 * loose channels trying to bind to the port. If so, those
398 * @param cls Identification of the client.
399 * @param pmsg The actual message.
402 handle_port_open (void *cls,
403 const struct GNUNET_CADET_PortMessage *pmsg)
405 struct CadetClient *c = cls;
407 LOG (GNUNET_ERROR_TYPE_DEBUG,
408 "Open port %s requested by client %s\n",
409 GNUNET_h2s (&pmsg->port),
411 if (NULL == c->ports)
412 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
415 GNUNET_CONTAINER_multihashmap_put (c->ports,
418 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
421 GNUNET_SERVICE_client_drop (c->client);
424 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
427 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
428 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
432 GNUNET_SERVICE_client_continue (c->client);
437 * Handler for port close requests. Marks this port as closed
438 * (unless of course we have another client with the same port
439 * open). Note that existing channels accepted on the port are
442 * @param cls Identification of the client.
443 * @param pmsg The actual message.
446 handle_port_close (void *cls,
447 const struct GNUNET_CADET_PortMessage *pmsg)
449 struct CadetClient *c = cls;
451 LOG (GNUNET_ERROR_TYPE_DEBUG,
452 "Closing port %s as requested by client %s\n",
453 GNUNET_h2s (&pmsg->port),
456 GNUNET_CONTAINER_multihashmap_remove (c->ports,
461 GNUNET_SERVICE_client_drop (c->client);
464 GNUNET_assert (GNUNET_YES ==
465 GNUNET_CONTAINER_multihashmap_remove (open_ports,
468 GNUNET_SERVICE_client_continue (c->client);
473 * Handler for requests for us creating a new channel to another peer and port.
475 * @param cls Identification of the client.
476 * @param tcm The actual message.
479 handle_channel_create (void *cls,
480 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
482 struct CadetClient *c = cls;
483 struct CadetChannel *ch;
485 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
487 /* Channel ID not in allowed range. */
489 GNUNET_SERVICE_client_drop (c->client);
492 ch = lookup_channel (c,
496 /* Channel ID already in use. Not allowed. */
498 GNUNET_SERVICE_client_drop (c->client);
503 ch = GCCH_channel_local_new (c,
512 GNUNET_SERVICE_client_drop (c->client);
515 GNUNET_assert (GNUNET_YES ==
516 GNUNET_CONTAINER_multihashmap32_put (c->channels,
517 ntohl (tcm->ccn.channel_of_client),
519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "New channel %s to %s at port %s requested by client %s\n",
524 GNUNET_i2s (&tcm->peer),
525 GNUNET_h2s (&tcm->port),
527 GNUNET_SERVICE_client_continue (c->client);
532 * Handler for requests of destroying an existing channel.
534 * @param cls client identification of the client
535 * @param msg the actual message
538 handle_channel_destroy (void *cls,
539 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
541 struct CadetClient *c = cls;
542 struct CadetChannel *ch;
544 ch = lookup_channel (c,
548 /* Client attempted to destroy unknown channel */
550 GNUNET_SERVICE_client_drop (c->client);
553 LOG (GNUNET_ERROR_TYPE_INFO,
554 "Client %s is destroying channel %s\n",
557 GNUNET_assert (GNUNET_YES ==
558 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
559 ntohl (msg->ccn.channel_of_client),
561 GCCH_channel_local_destroy (ch);
562 GNUNET_SERVICE_client_continue (c->client);
567 * Check for client traffic data message is well-formed.
569 * @param cls identification of the client
570 * @param msg the actual message
571 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
574 check_data (void *cls,
575 const struct GNUNET_CADET_LocalData *msg)
577 const struct GNUNET_MessageHeader *payload;
579 size_t payload_claimed_size;
581 /* Sanity check for message size */
582 payload_size = ntohs (msg->header.size) - sizeof (*msg);
583 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
584 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
587 return GNUNET_SYSERR;
589 payload = (struct GNUNET_MessageHeader *) &msg[1];
590 payload_claimed_size = ntohs (payload->size);
591 if (payload_size != payload_claimed_size)
594 return GNUNET_SYSERR;
601 * Handler for client payload traffic to be send on a channel to
604 * @param cls identification of the client
605 * @param msg the actual message
608 handle_data (void *cls,
609 const struct GNUNET_CADET_LocalData *msg)
611 struct CadetClient *c = cls;
612 struct CadetChannel *ch;
613 const struct GNUNET_MessageHeader *payload;
615 ch = lookup_channel (c,
619 /* Channel does not exist! */
621 GNUNET_SERVICE_client_drop (c->client);
625 payload = (const struct GNUNET_MessageHeader *) &msg[1];
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Received %u bytes payload from client %s for channel %s\n",
628 ntohs (payload->size),
632 GCCH_handle_local_data (ch,
635 GNUNET_SERVICE_client_drop (c->client);
638 GNUNET_SERVICE_client_continue (c->client);
643 * Handler for client's ACKs for payload traffic.
645 * @param cls identification of the client.
646 * @param msg The actual message.
649 handle_ack (void *cls,
650 const struct GNUNET_CADET_LocalAck *msg)
652 struct CadetClient *c = cls;
653 struct CadetChannel *ch;
655 ch = lookup_channel (c,
659 /* Channel does not exist! */
661 GNUNET_SERVICE_client_drop (c->client);
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "Got a local ACK from client %s for channel %s\n",
668 GCCH_handle_local_ack (ch);
669 GNUNET_SERVICE_client_continue (c->client);
674 * Iterator over all peers to send a monitoring client info about each peer.
676 * @param cls Closure ().
677 * @param peer Peer ID (tunnel remote peer).
678 * @param value Peer info.
679 * @return #GNUNET_YES, to keep iterating.
682 get_all_peers_iterator (void *cls,
683 const struct GNUNET_PeerIdentity *peer,
686 struct CadetClient *c = cls;
687 struct CadetPeer *p = value;
688 struct GNUNET_MQ_Envelope *env;
689 struct GNUNET_CADET_LocalInfoPeer *msg;
691 env = GNUNET_MQ_msg (msg,
692 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
693 msg->destination = *peer;
694 msg->paths = htons (GCP_count_paths (p));
695 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
697 GNUNET_MQ_send (c->mq,
704 * Handler for client's INFO PEERS request.
706 * @param cls Identification of the client.
707 * @param message The actual message.
710 handle_get_peers (void *cls,
711 const struct GNUNET_MessageHeader *message)
713 struct CadetClient *c = cls;
714 struct GNUNET_MQ_Envelope *env;
715 struct GNUNET_MessageHeader *reply;
717 GCP_iterate_all (&get_all_peers_iterator,
719 env = GNUNET_MQ_msg (reply,
720 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
721 GNUNET_MQ_send (c->mq,
723 GNUNET_SERVICE_client_continue (c->client);
728 * Iterator over all paths of a peer to build an InfoPeer message.
729 * Message contains blocks of peers, first not included.
731 * @param cls message queue for transmission
732 * @param path Path itself
733 * @param off offset of the peer on @a path
734 * @return #GNUNET_YES if should keep iterating.
735 * #GNUNET_NO otherwise.
738 path_info_iterator (void *cls,
739 struct CadetPeerPath *path,
742 struct GNUNET_MQ_Handle *mq = cls;
743 struct GNUNET_MQ_Envelope *env;
744 struct GNUNET_MessageHeader *resp;
745 struct GNUNET_PeerIdentity *id;
748 unsigned int path_length;
750 path_length = GCPP_get_length (path);
751 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
752 if (sizeof (*resp) + path_size > UINT16_MAX)
754 LOG (GNUNET_ERROR_TYPE_WARNING,
755 "Path of %u entries is too long for info message\n",
759 env = GNUNET_MQ_msg_extra (resp,
761 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
762 id = (struct GNUNET_PeerIdentity *) &resp[1];
764 /* Don't copy first peer. First peer is always the local one. Last
765 * peer is always the destination (leave as 0, EOL).
767 for (i = 0; i < off; i++)
768 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
777 * Handler for client's SHOW_PEER request.
779 * @param cls Identification of the client.
780 * @param msg The actual message.
783 handle_show_peer (void *cls,
784 const struct GNUNET_CADET_LocalInfo *msg)
786 struct CadetClient *c = cls;
788 struct GNUNET_MQ_Envelope *env;
789 struct GNUNET_MessageHeader *resp;
791 p = GCP_get (&msg->peer,
794 GCP_iterate_paths (p,
797 /* Send message with 0/0 to indicate the end */
798 env = GNUNET_MQ_msg (resp,
799 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
800 GNUNET_MQ_send (c->mq,
802 GNUNET_SERVICE_client_continue (c->client);
807 * Iterator over all tunnels to send a monitoring client info about each tunnel.
809 * @param cls Closure ().
810 * @param peer Peer ID (tunnel remote peer).
811 * @param value a `struct CadetPeer`
812 * @return #GNUNET_YES, to keep iterating.
815 get_all_tunnels_iterator (void *cls,
816 const struct GNUNET_PeerIdentity *peer,
819 struct CadetClient *c = cls;
820 struct CadetPeer *p = value;
821 struct GNUNET_MQ_Envelope *env;
822 struct GNUNET_CADET_LocalInfoTunnel *msg;
823 struct CadetTunnel *t;
825 t = GCP_get_tunnel (p,
829 env = GNUNET_MQ_msg (msg,
830 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
831 msg->destination = *peer;
832 msg->channels = htonl (GCT_count_channels (t));
833 msg->connections = htonl (GCT_count_any_connections (t));
834 msg->cstate = htons (0);
835 msg->estate = htons ((uint16_t) GCT_get_estate (t));
836 GNUNET_MQ_send (c->mq,
843 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
845 * @param cls client Identification of the client.
846 * @param message The actual message.
849 handle_info_tunnels (void *cls,
850 const struct GNUNET_MessageHeader *message)
852 struct CadetClient *c = cls;
853 struct GNUNET_MQ_Envelope *env;
854 struct GNUNET_MessageHeader *reply;
856 GCP_iterate_all (&get_all_tunnels_iterator,
858 env = GNUNET_MQ_msg (reply,
859 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
860 GNUNET_MQ_send (c->mq,
862 GNUNET_SERVICE_client_continue (c->client);
867 * Update the message with information about the connection.
869 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
870 * @param c a connection about which we should store information in @a cls
873 iter_connection (void *cls,
874 struct CadetConnection *c)
876 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
877 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
879 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
880 h[msg->connections++] = *(GCC_get_id (c));
885 * Update the message with information about the channel.
887 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
888 * @param ch a channel about which we should store information in @a cls
891 iter_channel (void *cls,
892 struct CadetChannel *ch)
894 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
895 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
896 struct GNUNET_CADET_ChannelTunnelNumber *chn
897 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
899 chn[msg->channels++] = GCCH_get_id (ch);
904 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
906 * @param cls Identification of the client.
907 * @param msg The actual message.
910 handle_info_tunnel (void *cls,
911 const struct GNUNET_CADET_LocalInfo *msg)
913 struct CadetClient *c = cls;
914 struct GNUNET_MQ_Envelope *env;
915 struct GNUNET_CADET_LocalInfoTunnel *resp;
916 struct CadetTunnel *t;
921 p = GCP_get (&msg->peer,
923 t = GCP_get_tunnel (p,
927 /* We don't know the tunnel */
928 struct GNUNET_MQ_Envelope *env;
929 struct GNUNET_CADET_LocalInfoTunnel *warn;
931 LOG (GNUNET_ERROR_TYPE_INFO,
932 "Tunnel to %s unknown\n",
933 GNUNET_i2s_full (&msg->peer));
934 env = GNUNET_MQ_msg (warn,
935 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
936 warn->destination = msg->peer;
937 GNUNET_MQ_send (c->mq,
939 GNUNET_SERVICE_client_continue (c->client);
943 /* Initialize context */
944 ch_n = GCT_count_channels (t);
945 c_n = GCT_count_any_connections (t);
946 env = GNUNET_MQ_msg_extra (resp,
947 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
948 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
949 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
950 resp->destination = msg->peer;
951 /* Do not reorder! #iter_channel needs counters in HBO! */
952 GCT_iterate_connections (t,
955 GCT_iterate_channels (t,
958 resp->connections = htonl (resp->connections);
959 resp->channels = htonl (resp->channels);
960 resp->cstate = htons (0);
961 resp->estate = htons (GCT_get_estate (t));
962 GNUNET_MQ_send (c->mq,
964 GNUNET_SERVICE_client_continue (c->client);
969 * Iterator over all peers to dump info for each peer.
971 * @param cls Closure (unused).
972 * @param peer Peer ID (tunnel remote peer).
973 * @param value Peer info.
975 * @return #GNUNET_YES, to keep iterating.
978 show_peer_iterator (void *cls,
979 const struct GNUNET_PeerIdentity *peer,
982 struct CadetPeer *p = value;
983 struct CadetTunnel *t;
985 t = GCP_get_tunnel (p,
989 GNUNET_ERROR_TYPE_ERROR);
990 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
996 * Handler for client's INFO_DUMP request.
998 * @param cls Identification of the client.
999 * @param message The actual message.
1002 handle_info_dump (void *cls,
1003 const struct GNUNET_MessageHeader *message)
1005 struct CadetClient *c = cls;
1007 LOG (GNUNET_ERROR_TYPE_INFO,
1008 "Received dump info request from client %u\n",
1011 LOG (GNUNET_ERROR_TYPE_ERROR,
1012 "*************************** DUMP START ***************************\n");
1013 for (struct CadetClient *ci = clients_head;
1017 LOG (GNUNET_ERROR_TYPE_ERROR,
1018 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1023 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1025 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1027 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1028 GCP_iterate_all (&show_peer_iterator,
1031 LOG (GNUNET_ERROR_TYPE_ERROR,
1032 "**************************** DUMP END ****************************\n");
1034 GNUNET_SERVICE_client_continue (c->client);
1040 * Callback called when a client connects to the service.
1042 * @param cls closure for the service
1043 * @param client the new client that connected to the service
1044 * @param mq the message queue used to send messages to the client
1048 client_connect_cb (void *cls,
1049 struct GNUNET_SERVICE_Client *client,
1050 struct GNUNET_MQ_Handle *mq)
1052 struct CadetClient *c;
1054 c = GNUNET_new (struct CadetClient);
1057 c->id = next_client_id++; /* overflow not important: just for debug */
1059 = GNUNET_CONTAINER_multihashmap32_create (32);
1060 GNUNET_CONTAINER_DLL_insert (clients_head,
1063 GNUNET_STATISTICS_update (stats,
1067 LOG (GNUNET_ERROR_TYPE_DEBUG,
1068 "Client %s connected\n",
1075 * Iterator for deleting each channel whose client endpoint disconnected.
1077 * @param cls Closure (client that has disconnected).
1078 * @param key The local channel id in host byte order
1079 * @param value The value stored at the key (channel to destroy).
1080 * @return #GNUNET_OK, keep iterating.
1083 channel_destroy_iterator (void *cls,
1087 struct CadetClient *c = cls;
1088 struct CadetChannel *ch = value;
1090 GNUNET_assert (GNUNET_YES ==
1091 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1094 LOG (GNUNET_ERROR_TYPE_DEBUG,
1095 "Destroying channel %s, due to client %s disconnecting.\n",
1098 if (key < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1099 GCCH_channel_local_destroy (ch);
1101 GCCH_channel_incoming_destroy (ch);
1107 * Remove client's ports from the global hashmap on disconnect.
1109 * @param cls Closure (unused).
1110 * @param key the port.
1111 * @param value the `struct CadetClient` to remove
1112 * @return #GNUNET_OK, keep iterating.
1115 client_release_ports (void *cls,
1116 const struct GNUNET_HashCode *key,
1119 struct CadetClient *c = value;
1121 LOG (GNUNET_ERROR_TYPE_DEBUG,
1122 "Closing port %s due to client %s disconnect.\n",
1125 GNUNET_assert (GNUNET_YES ==
1126 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1129 GNUNET_assert (GNUNET_YES ==
1130 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1138 * Callback called when a client disconnected from the service
1140 * @param cls closure for the service
1141 * @param client the client that disconnected
1142 * @param internal_cls should be equal to @a c
1145 client_disconnect_cb (void *cls,
1146 struct GNUNET_SERVICE_Client *client,
1149 struct CadetClient *c = internal_cls;
1151 GNUNET_assert (c->client == client);
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "Client %s is disconnecting.\n",
1155 if (NULL != c->channels)
1157 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1158 &channel_destroy_iterator,
1160 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1162 if (NULL != c->ports)
1164 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1165 &client_release_ports,
1167 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1169 GNUNET_CONTAINER_DLL_remove (clients_head,
1172 GNUNET_STATISTICS_update (stats,
1181 * Setup CADET internals.
1183 * @param cls closure
1184 * @param server the initialized server
1185 * @param c configuration to use
1189 const struct GNUNET_CONFIGURATION_Handle *c,
1190 struct GNUNET_SERVICE_Handle *service)
1194 GNUNET_CONFIGURATION_get_value_number (c,
1199 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1202 "needs to be a number");
1203 ratchet_messages = 64;
1206 GNUNET_CONFIGURATION_get_value_time (c,
1211 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1214 "need delay value");
1215 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1218 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1219 if (NULL == my_private_key)
1222 GNUNET_SCHEDULER_shutdown ();
1225 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1226 &my_full_id.public_key);
1227 stats = GNUNET_STATISTICS_create ("cadet",
1229 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1231 ats_ch = GNUNET_ATS_connectivity_init (c);
1232 /* FIXME: optimize code to allow GNUNET_YES here! */
1233 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1235 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1237 peers = GNUNET_CONTAINER_multipeermap_create (16,
1239 connections = GNUNET_CONTAINER_multishortmap_create (256,
1244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1245 "CADET started for peer %s\n",
1246 GNUNET_i2s (&my_full_id));
1252 * Define "main" method using service macro.
1256 GNUNET_SERVICE_OPTION_NONE,
1259 &client_disconnect_cb,
1261 GNUNET_MQ_hd_fixed_size (port_open,
1262 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1263 struct GNUNET_CADET_PortMessage,
1265 GNUNET_MQ_hd_fixed_size (port_close,
1266 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1267 struct GNUNET_CADET_PortMessage,
1269 GNUNET_MQ_hd_fixed_size (channel_create,
1270 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1271 struct GNUNET_CADET_LocalChannelCreateMessage,
1273 GNUNET_MQ_hd_fixed_size (channel_destroy,
1274 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1275 struct GNUNET_CADET_LocalChannelDestroyMessage,
1277 GNUNET_MQ_hd_var_size (data,
1278 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1279 struct GNUNET_CADET_LocalData,
1281 GNUNET_MQ_hd_fixed_size (ack,
1282 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1283 struct GNUNET_CADET_LocalAck,
1285 GNUNET_MQ_hd_fixed_size (get_peers,
1286 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1287 struct GNUNET_MessageHeader,
1289 GNUNET_MQ_hd_fixed_size (show_peer,
1290 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1291 struct GNUNET_CADET_LocalInfo,
1293 GNUNET_MQ_hd_fixed_size (info_tunnels,
1294 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1295 struct GNUNET_MessageHeader,
1297 GNUNET_MQ_hd_fixed_size (info_tunnel,
1298 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1299 struct GNUNET_CADET_LocalInfo,
1301 GNUNET_MQ_hd_fixed_size (info_dump,
1302 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1303 struct GNUNET_MessageHeader,
1305 GNUNET_MQ_handler_end ());
1307 /* end of gnunet-service-cadet-new.c */