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
71 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
74 * Tunnels this client has accepted, indexed by incoming local id
76 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
79 * Channel ID for the next incoming channel.
81 struct GNUNET_CADET_ClientChannelNumber next_chid;
84 * Handle to communicate with the client
86 struct GNUNET_MQ_Handle *mq;
91 struct GNUNET_SERVICE_Client *client;
94 * Ports that this client has declared interest in.
95 * Indexed by port, contains *Client.
97 struct GNUNET_CONTAINER_MultiHashMap *ports;
100 * Whether the client is active or shutting down (don't send confirmations
101 * to a client that is shutting down).
106 * ID of the client, mainly for debug messages
111 /******************************************************************************/
112 /*********************** GLOBAL VARIABLES ****************************/
113 /******************************************************************************/
115 /****************************** Global variables ******************************/
118 * Handle to the statistics service.
120 struct GNUNET_STATISTICS_Handle *stats;
123 * Handle to communicate with ATS.
125 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
130 struct GNUNET_PeerIdentity my_full_id;
135 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
138 * Signal that shutdown is happening: prevent recover measures.
143 * DLL with all the clients, head.
145 static struct CadetClient *clients_head;
148 * DLL with all the clients, tail.
150 static struct CadetClient *clients_tail;
153 * Next ID to assign to a client.
155 static unsigned int next_client_id;
158 * All ports clients of this peer have opened.
160 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
163 * Map from ports to channels where the ports were closed at the
164 * time we got the inbound connection.
165 * Indexed by port, contains `struct CadetChannel`.
167 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
170 * Map from PIDs to `struct CadetPeer` entries.
172 struct GNUNET_CONTAINER_MultiPeerMap *peers;
175 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
176 * hash codes to `struct CadetConnection` objects.
178 struct GNUNET_CONTAINER_MultiShortmap *connections;
181 * How many messages are needed to trigger an AXOLOTL ratchet advance.
183 unsigned long long ratchet_messages;
186 * How long until we trigger a ratched advance due to time.
188 struct GNUNET_TIME_Relative ratchet_time;
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)
219 return "Client(NULL)";
220 GNUNET_snprintf (buf,
229 * Obtain the next LID to use for incoming connections to
232 * @param c client handle
234 static struct GNUNET_CADET_ClientChannelNumber
235 client_get_next_lid (struct CadetClient *c)
237 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_chid;
239 /* increment until we have a free one... */
241 GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels,
242 ntohl (ccn.channel_of_client)))
244 ccn.channel_of_client
245 = htonl (1 + (ntohl (ccn.channel_of_client)));
246 if (ntohl (ccn.channel_of_client) >=
247 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
248 ccn.channel_of_client = htonl (0);
250 c->next_chid.channel_of_client
251 = htonl (1 + (ntohl (ccn.channel_of_client)));
257 * Bind incoming channel to this client, and notify client
258 * about incoming connection.
260 * @param c client to bind to
261 * @param ch channel to be bound
262 * @param dest peer that establishes the connection
263 * @param port port number
264 * @param options options
265 * @return local channel number assigned to the new client
267 struct GNUNET_CADET_ClientChannelNumber
268 GSC_bind (struct CadetClient *c,
269 struct CadetChannel *ch,
270 struct CadetPeer *dest,
271 const struct GNUNET_HashCode *port,
274 struct GNUNET_MQ_Envelope *env;
275 struct GNUNET_CADET_ChannelOpenMessageMessage *msg;
276 struct GNUNET_CADET_ClientChannelNumber lid;
278 lid = client_get_next_lid (c);
279 GNUNET_assert (GNUNET_YES ==
280 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
281 ntohl (lid.channel_of_client),
283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
285 /* notify local client about incoming connection! */
286 env = GNUNET_MQ_msg (msg,
287 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
288 msg->channel_id = lid;
290 msg->opt = htonl (options);
291 msg->peer = *GCP_get_id (dest);
292 GSC_send_to_client (c,
298 /******************************************************************************/
299 /************************ MAIN FUNCTIONS ****************************/
300 /******************************************************************************/
303 * Task run during shutdown.
308 shutdown_task (void *cls)
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 shutting_down = GNUNET_YES;
316 GNUNET_STATISTICS_destroy (stats,
320 if (NULL != open_ports)
322 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
325 if (NULL != loose_channels)
327 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
328 loose_channels = NULL;
330 /* All channels, connections and CORE must be down before this point. */
331 GCP_destroy_all_peers ();
334 GNUNET_CONTAINER_multipeermap_destroy (peers);
337 if (NULL != connections)
339 GNUNET_CONTAINER_multishortmap_destroy (connections);
344 GNUNET_ATS_connectivity_done (ats_ch);
349 GNUNET_free_non_null (my_private_key);
350 my_private_key = NULL;
355 * We had a remote connection @a value to port @a port before
356 * client @a cls opened port @a port. Bind them now.
358 * @param cls the `struct CadetClient`
359 * @param port the port
360 * @param value the `struct CadetChannel`
361 * @return #GNUNET_YES (iterate over all such channels)
364 bind_loose_channel (void *cls,
365 const struct GNUNET_HashCode *port,
368 struct CadetClient *c = cls;
369 struct CadetChannel *ch = value;
373 GNUNET_assert (GNUNET_YES ==
374 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
382 * Handler for port open requests.
384 * @param cls Identification of the client.
385 * @param pmsg The actual message.
388 handle_port_open (void *cls,
389 const struct GNUNET_CADET_PortMessage *pmsg)
391 struct CadetClient *c = cls;
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "Open port %s requested by client %u\n",
395 GNUNET_h2s (&pmsg->port),
397 if (NULL == c->ports)
398 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
401 GNUNET_CONTAINER_multihashmap_put (c->ports,
404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
407 GNUNET_SERVICE_client_drop (c->client);
410 /* store in global hashmap */
411 /* FIXME only allow one client to have the port open,
412 * have a backup hashmap with waiting clients */
413 GNUNET_CONTAINER_multihashmap_put (open_ports,
416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
417 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
421 GNUNET_SERVICE_client_continue (c->client);
426 * Handler for port close requests.
428 * @param cls Identification of the client.
429 * @param pmsg The actual message.
432 handle_port_close (void *cls,
433 const struct GNUNET_CADET_PortMessage *pmsg)
435 struct CadetClient *c = cls;
437 LOG (GNUNET_ERROR_TYPE_DEBUG,
438 "Open port %s requested by client %u\n",
439 GNUNET_h2s (&pmsg->port),
442 GNUNET_CONTAINER_multihashmap_remove (c->ports,
447 GNUNET_SERVICE_client_drop (c->client);
450 GNUNET_assert (GNUNET_YES ==
451 GNUNET_CONTAINER_multihashmap_remove (open_ports,
455 GNUNET_SERVICE_client_continue (c->client);
460 * Handler for requests of new channels.
462 * @param cls Identification of the client.
463 * @param ccm The actual message.
466 handle_channel_create (void *cls,
467 const struct GNUNET_CADET_ChannelOpenMessageMessage *ccm)
469 struct CadetClient *c = cls;
470 struct CadetChannel *ch;
471 struct GNUNET_CADET_ClientChannelNumber chid;
472 struct CadetPeer *dst;
474 chid = ccm->channel_id;
475 if (ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
477 /* Channel ID not in allowed range. */
479 GNUNET_SERVICE_client_drop (c->client);
482 ch = GNUNET_CONTAINER_multihashmap32_get (c->own_channels,
483 ntohl (chid.channel_of_client));
486 /* Channel ID already in use. Not allowed. */
488 GNUNET_SERVICE_client_drop (c->client);
492 dst = GCP_get (&ccm->peer,
496 ch = GCCH_channel_local_new (c,
504 GNUNET_SERVICE_client_drop (c->client);
507 GNUNET_assert (GNUNET_YES ==
508 GNUNET_CONTAINER_multihashmap32_put (c->own_channels,
509 ntohl (chid.channel_of_client),
511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "New channel %s to %s at port %s requested by client %u\n",
516 GNUNET_i2s (&ccm->peer),
517 GNUNET_h2s (&ccm->port),
519 GNUNET_SERVICE_client_continue (c->client);
524 * Return the map which we use for client @a c for a channel ID of @a chid
526 * @param c client to find map for
527 * @param chid chid to find map for
528 * @return applicable map we use
530 static struct GNUNET_CONTAINER_MultiHashMap32 *
531 get_map_by_chid (struct CadetClient *c,
532 struct GNUNET_CADET_ClientChannelNumber chid)
534 return (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
536 : c->incoming_channels;
541 * Handler for requests of deleting tunnels
543 * @param cls client identification of the client
544 * @param msg the actual message
547 handle_channel_destroy (void *cls,
548 const struct GNUNET_CADET_ChannelDestroyMessage *msg)
550 struct CadetClient *c = cls;
551 struct GNUNET_CADET_ClientChannelNumber chid;
552 struct GNUNET_CONTAINER_MultiHashMap32 *map;
553 struct CadetChannel *ch;
555 /* Retrieve tunnel */
556 chid = msg->channel_id;
557 map = get_map_by_chid (c,
559 ch = GNUNET_CONTAINER_multihashmap32_get (map,
560 ntohl (chid.channel_of_client));
563 /* Client attempted to destroy unknown channel */
565 GNUNET_SERVICE_client_drop (c->client);
568 LOG (GNUNET_ERROR_TYPE_INFO,
569 "Client %u is destroying channel %s\n",
572 GNUNET_assert (GNUNET_YES ==
573 GNUNET_CONTAINER_multihashmap32_remove (map,
574 ntohl (chid.channel_of_client),
576 GCCH_channel_local_destroy (ch);
577 GNUNET_SERVICE_client_continue (c->client);
582 * Check for client traffic data message is well-formed
584 * @param cls identification of the client
585 * @param msg the actual message
586 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
589 check_data (void *cls,
590 const struct GNUNET_CADET_LocalData *msg)
592 const struct GNUNET_MessageHeader *payload;
594 size_t payload_claimed_size;
596 /* Sanity check for message size */
597 payload_size = ntohs (msg->header.size) - sizeof (*msg);
598 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
599 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
602 return GNUNET_SYSERR;
604 payload = (struct GNUNET_MessageHeader *) &msg[1];
605 payload_claimed_size = ntohs (payload->size);
606 if (payload_size != payload_claimed_size)
609 return GNUNET_SYSERR;
616 * Handler for client traffic
618 * @param cls identification of the client
619 * @param msg the actual message
622 handle_data (void *cls,
623 const struct GNUNET_CADET_LocalData *msg)
625 struct CadetClient *c = cls;
626 struct GNUNET_CONTAINER_MultiHashMap32 *map;
627 struct GNUNET_CADET_ClientChannelNumber chid;
628 struct CadetChannel *ch;
629 const struct GNUNET_MessageHeader *payload;
631 chid = msg->channel_id;
632 map = get_map_by_chid (c,
634 ch = GNUNET_CONTAINER_multihashmap32_get (map,
635 ntohl (chid.channel_of_client));
638 /* Channel does not exist! */
640 GNUNET_SERVICE_client_drop (c->client);
644 payload = (const struct GNUNET_MessageHeader *) &msg[1];
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Received %u bytes payload from client %u for channel %s\n",
647 ntohs (payload->size),
651 GCCH_handle_local_data (ch,
654 GNUNET_SERVICE_client_drop (c->client);
657 GNUNET_SERVICE_client_continue (c->client);
662 * Handler for client's ACKs for payload traffic.
664 * @param cls identification of the client.
665 * @param msg The actual message.
668 handle_ack (void *cls,
669 const struct GNUNET_CADET_LocalAck *msg)
671 struct CadetClient *c = cls;
672 struct GNUNET_CONTAINER_MultiHashMap32 *map;
673 struct GNUNET_CADET_ClientChannelNumber chid;
674 struct CadetChannel *ch;
676 chid = msg->channel_id;
677 map = get_map_by_chid (c,
679 ch = GNUNET_CONTAINER_multihashmap32_get (map,
680 ntohl (chid.channel_of_client));
683 /* Channel does not exist! */
685 GNUNET_SERVICE_client_drop (c->client);
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Got a local ACK from client %u for channel %s\n",
692 GCCH_handle_local_ack (ch);
693 GNUNET_SERVICE_client_continue (c->client);
698 * Iterator over all peers to send a monitoring client info about each peer.
700 * @param cls Closure ().
701 * @param peer Peer ID (tunnel remote peer).
702 * @param value Peer info.
703 * @return #GNUNET_YES, to keep iterating.
706 get_all_peers_iterator (void *cls,
707 const struct GNUNET_PeerIdentity *peer,
710 struct CadetClient *c = cls;
711 struct CadetPeer *p = value;
712 struct GNUNET_MQ_Envelope *env;
713 struct GNUNET_CADET_LocalInfoPeer *msg;
715 env = GNUNET_MQ_msg (msg,
716 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
717 msg->destination = *peer;
718 msg->paths = htons (GCP_count_paths (p));
719 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
721 GNUNET_MQ_send (c->mq,
728 * Handler for client's INFO PEERS request.
730 * @param cls Identification of the client.
731 * @param message The actual message.
734 handle_get_peers (void *cls,
735 const struct GNUNET_MessageHeader *message)
737 struct CadetClient *c = cls;
738 struct GNUNET_MQ_Envelope *env;
739 struct GNUNET_MessageHeader *reply;
741 GCP_iterate_all (&get_all_peers_iterator,
743 env = GNUNET_MQ_msg (reply,
744 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
745 GNUNET_MQ_send (c->mq,
747 GNUNET_SERVICE_client_continue (c->client);
752 * Iterator over all paths of a peer to build an InfoPeer message.
753 * Message contains blocks of peers, first not included.
755 * @param cls message queue for transmission
756 * @param path Path itself
757 * @param off offset of the peer on @a path
758 * @return #GNUNET_YES if should keep iterating.
759 * #GNUNET_NO otherwise.
762 path_info_iterator (void *cls,
763 struct CadetPeerPath *path,
766 struct GNUNET_MQ_Handle *mq = cls;
767 struct GNUNET_MQ_Envelope *env;
768 struct GNUNET_MessageHeader *resp;
769 struct GNUNET_PeerIdentity *id;
772 unsigned int path_length;
774 path_length = GCPP_get_length (path);
775 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
776 if (sizeof (*resp) + path_size > UINT16_MAX)
778 LOG (GNUNET_ERROR_TYPE_WARNING,
779 "Path of %u entries is too long for info message\n",
783 env = GNUNET_MQ_msg_extra (resp,
785 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
786 id = (struct GNUNET_PeerIdentity *) &resp[1];
788 /* Don't copy first peer. First peer is always the local one. Last
789 * peer is always the destination (leave as 0, EOL).
791 for (i = 0; i < off; i++)
792 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
801 * Handler for client's SHOW_PEER request.
803 * @param cls Identification of the client.
804 * @param msg The actual message.
807 handle_show_peer (void *cls,
808 const struct GNUNET_CADET_LocalInfo *msg)
810 struct CadetClient *c = cls;
812 struct GNUNET_MQ_Envelope *env;
813 struct GNUNET_MessageHeader *resp;
815 p = GCP_get (&msg->peer,
818 GCP_iterate_paths (p,
821 /* Send message with 0/0 to indicate the end */
822 env = GNUNET_MQ_msg (resp,
823 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
824 GNUNET_MQ_send (c->mq,
826 GNUNET_SERVICE_client_continue (c->client);
831 * Iterator over all tunnels to send a monitoring client info about each tunnel.
833 * @param cls Closure ().
834 * @param peer Peer ID (tunnel remote peer).
835 * @param value a `struct CadetPeer`
836 * @return #GNUNET_YES, to keep iterating.
839 get_all_tunnels_iterator (void *cls,
840 const struct GNUNET_PeerIdentity *peer,
843 struct CadetClient *c = cls;
844 struct CadetPeer *p = value;
845 struct GNUNET_MQ_Envelope *env;
846 struct GNUNET_CADET_LocalInfoTunnel *msg;
847 struct CadetTunnel *t;
849 t = GCP_get_tunnel (p,
853 env = GNUNET_MQ_msg (msg,
854 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
855 msg->destination = *peer;
856 msg->channels = htonl (GCT_count_channels (t));
857 msg->connections = htonl (GCT_count_any_connections (t));
858 msg->cstate = htons (0);
859 msg->estate = htons ((uint16_t) GCT_get_estate (t));
860 GNUNET_MQ_send (c->mq,
867 * Handler for client's INFO TUNNELS request.
869 * @param cls client Identification of the client.
870 * @param message The actual message.
873 handle_get_tunnels (void *cls,
874 const struct GNUNET_MessageHeader *message)
876 struct CadetClient *c = cls;
877 struct GNUNET_MQ_Envelope *env;
878 struct GNUNET_MessageHeader *reply;
880 GCP_iterate_all (&get_all_tunnels_iterator,
882 env = GNUNET_MQ_msg (reply,
883 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
884 GNUNET_MQ_send (c->mq,
886 GNUNET_SERVICE_client_continue (c->client);
894 iter_connection (void *cls,
895 struct CadetConnection *c)
897 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
898 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
900 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
901 h[msg->connections++] = *(GCC_get_id (c));
909 iter_channel (void *cls,
910 struct CadetChannel *ch)
912 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
913 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
914 struct GNUNET_CADET_ChannelTunnelNumber *chn
915 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
917 chn[msg->channels++] = GCCH_get_id (ch);
922 * Handler for client's SHOW_TUNNEL request.
924 * @param cls Identification of the client.
925 * @param msg The actual message.
928 handle_show_tunnel (void *cls,
929 const struct GNUNET_CADET_LocalInfo *msg)
931 struct CadetClient *c = cls;
932 struct GNUNET_MQ_Envelope *env;
933 struct GNUNET_CADET_LocalInfoTunnel *resp;
934 struct CadetTunnel *t;
939 p = GCP_get (&msg->peer,
941 t = GCP_get_tunnel (p,
945 /* We don't know the tunnel */
946 struct GNUNET_MQ_Envelope *env;
947 struct GNUNET_CADET_LocalInfoTunnel *warn;
949 LOG (GNUNET_ERROR_TYPE_INFO,
950 "Tunnel to %s unknown\n",
951 GNUNET_i2s_full (&msg->peer));
952 env = GNUNET_MQ_msg (warn,
953 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
954 warn->destination = msg->peer;
955 GNUNET_MQ_send (c->mq,
957 GNUNET_SERVICE_client_continue (c->client);
961 /* Initialize context */
962 ch_n = GCT_count_channels (t);
963 c_n = GCT_count_any_connections (t);
964 env = GNUNET_MQ_msg_extra (resp,
965 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
966 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
967 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
968 resp->destination = msg->peer;
969 /* Do not reorder! #iter_channel needs counters in HBO! */
970 GCT_iterate_connections (t,
973 GCT_iterate_channels (t,
976 resp->connections = htonl (resp->connections);
977 resp->channels = htonl (resp->channels);
978 resp->cstate = htons (0);
979 resp->estate = htons (GCT_get_estate (t));
980 GNUNET_MQ_send (c->mq,
982 GNUNET_SERVICE_client_continue (c->client);
987 * Iterator over all peers to dump info for each peer.
989 * @param cls Closure (unused).
990 * @param peer Peer ID (tunnel remote peer).
991 * @param value Peer info.
993 * @return #GNUNET_YES, to keep iterating.
996 show_peer_iterator (void *cls,
997 const struct GNUNET_PeerIdentity *peer,
1000 struct CadetPeer *p = value;
1001 struct CadetTunnel *t;
1003 t = GCP_get_tunnel (p,
1007 GNUNET_ERROR_TYPE_ERROR);
1008 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1014 * Handler for client's INFO_DUMP request.
1016 * @param cls Identification of the client.
1017 * @param message The actual message.
1020 handle_info_dump (void *cls,
1021 const struct GNUNET_MessageHeader *message)
1023 struct CadetClient *c = cls;
1025 LOG (GNUNET_ERROR_TYPE_INFO,
1026 "Received dump info request from client %u\n",
1029 LOG (GNUNET_ERROR_TYPE_ERROR,
1030 "*************************** DUMP START ***************************\n");
1031 for (struct CadetClient *ci = clients_head; NULL != ci; ci = ci->next)
1033 LOG (GNUNET_ERROR_TYPE_ERROR,
1034 "Client %u (%p), handle: %p, ports: %u, own channels: %u, incoming channels: %u\n",
1039 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1041 GNUNET_CONTAINER_multihashmap32_size (ci->own_channels),
1042 GNUNET_CONTAINER_multihashmap32_size (ci->incoming_channels));
1044 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1045 GCP_iterate_all (&show_peer_iterator,
1048 LOG (GNUNET_ERROR_TYPE_ERROR,
1049 "**************************** DUMP END ****************************\n");
1051 GNUNET_SERVICE_client_continue (c->client);
1057 * Callback called when a client connects to the service.
1059 * @param cls closure for the service
1060 * @param client the new client that connected to the service
1061 * @param mq the message queue used to send messages to the client
1065 client_connect_cb (void *cls,
1066 struct GNUNET_SERVICE_Client *client,
1067 struct GNUNET_MQ_Handle *mq)
1069 struct CadetClient *c;
1071 c = GNUNET_new (struct CadetClient);
1074 c->id = next_client_id++; /* overflow not important: just for debug */
1076 = GNUNET_CONTAINER_multihashmap32_create (32);
1077 c->incoming_channels
1078 = GNUNET_CONTAINER_multihashmap32_create (32);
1079 GNUNET_CONTAINER_DLL_insert (clients_head,
1082 GNUNET_STATISTICS_update (stats,
1091 * Iterator for deleting each channel whose client endpoint disconnected.
1093 * @param cls Closure (client that has disconnected).
1094 * @param key The local channel id (used to access the hashmap).
1095 * @param value The value stored at the key (channel to destroy).
1096 * @return #GNUNET_OK, keep iterating.
1099 own_channel_destroy_iterator (void *cls,
1103 struct CadetClient *c = cls;
1104 struct CadetChannel *ch = value;
1106 GNUNET_assert (GNUNET_YES ==
1107 GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1110 LOG (GNUNET_ERROR_TYPE_DEBUG,
1111 "Destroying own channel %s, due to client %u shutdown.\n",
1114 GCCH_channel_local_destroy (ch);
1120 * Iterator for deleting each channel whose client endpoint disconnected.
1122 * @param cls Closure (client that has disconnected).
1123 * @param key The local channel id (used to access the hashmap).
1124 * @param value The value stored at the key (channel to destroy).
1125 * @return #GNUNET_OK, keep iterating.
1128 incoming_channel_destroy_iterator (void *cls,
1132 struct CadetChannel *ch = value;
1133 struct CadetClient *c = cls;
1135 GNUNET_assert (GNUNET_YES ==
1136 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Destroying incoming channel %s due to client %u shutdown.\n",
1144 GCCH_channel_incoming_destroy (ch);
1150 * Remove client's ports from the global hashmap on disconnect.
1152 * @param cls Closure (unused).
1154 * @param value the `struct CadetClient` to remove
1155 * @return #GNUNET_OK, keep iterating.
1158 client_release_ports (void *cls,
1159 const struct GNUNET_HashCode *key,
1162 struct CadetClient *c = value;
1164 GNUNET_assert (GNUNET_YES ==
1165 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1168 GNUNET_assert (GNUNET_YES ==
1169 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1177 * Callback called when a client disconnected from the service
1179 * @param cls closure for the service
1180 * @param client the client that disconnected
1181 * @param internal_cls should be equal to @a c
1184 client_disconnect_cb (void *cls,
1185 struct GNUNET_SERVICE_Client *client,
1188 struct CadetClient *c = internal_cls;
1190 GNUNET_assert (c->client == client);
1191 c->shutting_down = GNUNET_YES;
1192 if (NULL != c->own_channels)
1194 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
1195 &own_channel_destroy_iterator,
1197 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
1199 if (NULL != c->incoming_channels)
1201 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
1202 &incoming_channel_destroy_iterator,
1204 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
1206 if (NULL != c->ports)
1208 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1209 &client_release_ports,
1211 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1213 GNUNET_CONTAINER_DLL_remove (clients_head,
1216 GNUNET_STATISTICS_update (stats,
1225 * Setup CADET internals.
1227 * @param cls closure
1228 * @param server the initialized server
1229 * @param c configuration to use
1233 const struct GNUNET_CONFIGURATION_Handle *c,
1234 struct GNUNET_SERVICE_Handle *service)
1237 GNUNET_CONFIGURATION_get_value_number (c,
1242 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1245 "needs to be a number");
1246 ratchet_messages = 64;
1249 GNUNET_CONFIGURATION_get_value_time (c,
1254 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1257 "need delay value");
1258 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1261 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1262 if (NULL == my_private_key)
1265 GNUNET_SCHEDULER_shutdown ();
1268 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1269 &my_full_id.public_key);
1270 stats = GNUNET_STATISTICS_create ("cadet",
1272 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1274 ats_ch = GNUNET_ATS_connectivity_init (c);
1275 /* FIXME: optimize code to allow GNUNET_YES here! */
1276 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1278 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1280 peers = GNUNET_CONTAINER_multipeermap_create (16,
1282 connections = GNUNET_CONTAINER_multishortmap_create (256,
1287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1288 "CADET starting at peer %s\n",
1289 GNUNET_i2s (&my_full_id));
1295 * Define "main" method using service macro.
1299 GNUNET_SERVICE_OPTION_NONE,
1302 &client_disconnect_cb,
1304 GNUNET_MQ_hd_fixed_size (port_open,
1305 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1306 struct GNUNET_CADET_PortMessage,
1308 GNUNET_MQ_hd_fixed_size (port_close,
1309 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1310 struct GNUNET_CADET_PortMessage,
1312 GNUNET_MQ_hd_fixed_size (channel_create,
1313 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1314 struct GNUNET_CADET_ChannelOpenMessageMessage,
1316 GNUNET_MQ_hd_fixed_size (channel_destroy,
1317 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1318 struct GNUNET_CADET_ChannelDestroyMessage,
1320 GNUNET_MQ_hd_var_size (data,
1321 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1322 struct GNUNET_CADET_LocalData,
1324 GNUNET_MQ_hd_fixed_size (ack,
1325 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1326 struct GNUNET_CADET_LocalAck,
1328 GNUNET_MQ_hd_fixed_size (get_peers,
1329 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1330 struct GNUNET_MessageHeader,
1332 GNUNET_MQ_hd_fixed_size (show_peer,
1333 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1334 struct GNUNET_CADET_LocalInfo,
1336 GNUNET_MQ_hd_fixed_size (get_tunnels,
1337 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1338 struct GNUNET_MessageHeader,
1340 GNUNET_MQ_hd_fixed_size (show_tunnel,
1341 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1342 struct GNUNET_CADET_LocalInfo,
1344 GNUNET_MQ_hd_fixed_size (info_dump,
1345 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1346 struct GNUNET_MessageHeader,
1348 GNUNET_MQ_handler_end ());
1350 /* end of gnunet-service-cadet-new.c */