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 our configuration.
120 const struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Handle to the statistics service.
125 struct GNUNET_STATISTICS_Handle *stats;
128 * Handle to communicate with ATS.
130 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
135 struct GNUNET_PeerIdentity my_full_id;
140 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
143 * Signal that shutdown is happening: prevent recover measures.
148 * DLL with all the clients, head.
150 static struct CadetClient *clients_head;
153 * DLL with all the clients, tail.
155 static struct CadetClient *clients_tail;
158 * Next ID to assign to a client.
160 static unsigned int next_client_id;
163 * All ports clients of this peer have opened.
165 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
168 * Map from ports to channels where the ports were closed at the
169 * time we got the inbound connection.
170 * Indexed by port, contains `struct CadetChannel`.
172 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
175 * Map from PIDs to `struct CadetPeer` entries.
177 struct GNUNET_CONTAINER_MultiPeerMap *peers;
180 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
181 * hash codes to `struct CadetConnection` objects.
183 struct GNUNET_CONTAINER_MultiShortmap *connections;
186 * How many messages are needed to trigger an AXOLOTL ratchet advance.
188 unsigned long long ratchet_messages;
191 * How long until we trigger a ratched advance due to time.
193 struct GNUNET_TIME_Relative ratchet_time;
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)
224 return "Client(NULL)";
225 GNUNET_snprintf (buf,
234 * Obtain the next LID to use for incoming connections to
237 * @param c client handle
239 static struct GNUNET_CADET_ClientChannelNumber
240 client_get_next_lid (struct CadetClient *c)
242 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_chid;
244 /* increment until we have a free one... */
246 GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels,
247 ntohl (ccn.channel_of_client)))
249 ccn.channel_of_client
250 = htonl (1 + (ntohl (ccn.channel_of_client)));
251 if (ntohl (ccn.channel_of_client) >=
252 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
253 ccn.channel_of_client = htonl (0);
255 c->next_chid.channel_of_client
256 = htonl (1 + (ntohl (ccn.channel_of_client)));
262 * Bind incoming channel to this client, and notify client
263 * about incoming connection.
265 * @param c client to bind to
266 * @param ch channel to be bound
267 * @param dest peer that establishes the connection
268 * @param port port number
269 * @param options options
270 * @return local channel number assigned to the new client
272 struct GNUNET_CADET_ClientChannelNumber
273 GSC_bind (struct CadetClient *c,
274 struct CadetChannel *ch,
275 struct CadetPeer *dest,
276 const struct GNUNET_HashCode *port,
279 struct GNUNET_MQ_Envelope *env;
280 struct GNUNET_CADET_TunnelCreateMessage *msg;
281 struct GNUNET_CADET_ClientChannelNumber lid;
283 lid = client_get_next_lid (c);
284 GNUNET_assert (GNUNET_YES ==
285 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
286 ntohl (lid.channel_of_client),
288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
290 /* notify local client about incoming connection! */
291 env = GNUNET_MQ_msg (msg,
292 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE);
293 msg->channel_id = lid;
295 msg->opt = htonl (options);
296 msg->peer = *GCP_get_id (dest);
297 GSC_send_to_client (c,
303 /******************************************************************************/
304 /************************ MAIN FUNCTIONS ****************************/
305 /******************************************************************************/
308 * Task run during shutdown.
313 shutdown_task (void *cls)
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 shutting_down = GNUNET_YES;
321 GNUNET_STATISTICS_destroy (stats,
325 if (NULL != open_ports)
327 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
330 if (NULL != loose_channels)
332 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
333 loose_channels = NULL;
335 /* All channels, connections and CORE must be down before this point. */
336 GCP_destroy_all_peers ();
339 GNUNET_CONTAINER_multipeermap_destroy (peers);
342 if (NULL != connections)
344 GNUNET_CONTAINER_multishortmap_destroy (connections);
349 GNUNET_ATS_connectivity_done (ats_ch);
354 GNUNET_free_non_null (my_private_key);
355 my_private_key = NULL;
360 * We had a remote connection @a value to port @a port before
361 * client @a cls opened port @a port. Bind them now.
363 * @param cls the `struct CadetClient`
364 * @param port the port
365 * @param value the `struct CadetChannel`
366 * @return #GNUNET_YES (iterate over all such channels)
369 bind_loose_channel (void *cls,
370 const struct GNUNET_HashCode *port,
373 struct CadetClient *c = cls;
374 struct CadetChannel *ch = value;
378 GNUNET_assert (GNUNET_YES ==
379 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
387 * Handler for port open requests.
389 * @param cls Identification of the client.
390 * @param pmsg The actual message.
393 handle_port_open (void *cls,
394 const struct GNUNET_CADET_PortMessage *pmsg)
396 struct CadetClient *c = cls;
398 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 "Open port %s requested by client %u\n",
400 GNUNET_h2s (&pmsg->port),
402 if (NULL == c->ports)
403 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
406 GNUNET_CONTAINER_multihashmap_put (c->ports,
409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
412 GNUNET_SERVICE_client_drop (c->client);
415 /* store in global hashmap */
416 /* FIXME only allow one client to have the port open,
417 * have a backup hashmap with waiting clients */
418 GNUNET_CONTAINER_multihashmap_put (open_ports,
421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
422 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
426 GNUNET_SERVICE_client_continue (c->client);
431 * Handler for port close requests.
433 * @param cls Identification of the client.
434 * @param pmsg The actual message.
437 handle_port_close (void *cls,
438 const struct GNUNET_CADET_PortMessage *pmsg)
440 struct CadetClient *c = cls;
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Open port %s requested by client %u\n",
444 GNUNET_h2s (&pmsg->port),
447 GNUNET_CONTAINER_multihashmap_remove (c->ports,
452 GNUNET_SERVICE_client_drop (c->client);
455 GNUNET_assert (GNUNET_YES ==
456 GNUNET_CONTAINER_multihashmap_remove (open_ports,
460 GNUNET_SERVICE_client_continue (c->client);
465 * Handler for requests of new channels.
467 * @param cls Identification of the client.
468 * @param tcm The actual message.
471 handle_tunnel_create (void *cls,
472 const struct GNUNET_CADET_TunnelCreateMessage *tcm)
474 struct CadetClient *c = cls;
475 struct CadetChannel *ch;
476 struct GNUNET_CADET_ClientChannelNumber chid;
477 struct CadetPeer *dst;
479 chid = tcm->channel_id;
480 if (ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
482 /* Channel ID not in allowed range. */
484 GNUNET_SERVICE_client_drop (c->client);
487 ch = GNUNET_CONTAINER_multihashmap32_get (c->own_channels,
488 ntohl (chid.channel_of_client));
491 /* Channel ID already in use. Not allowed. */
493 GNUNET_SERVICE_client_drop (c->client);
497 dst = GCP_get (&tcm->peer,
501 ch = GCCH_channel_local_new (c,
509 GNUNET_SERVICE_client_drop (c->client);
512 GNUNET_assert (GNUNET_YES ==
513 GNUNET_CONTAINER_multihashmap32_put (c->own_channels,
514 ntohl (chid.channel_of_client),
516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "New channel %s to %s at port %s requested by client %u\n",
521 GNUNET_i2s (&tcm->peer),
522 GNUNET_h2s (&tcm->port),
524 GNUNET_SERVICE_client_continue (c->client);
529 * Return the map which we use for client @a c for a channel ID of @a chid
531 * @param c client to find map for
532 * @param chid chid to find map for
533 * @return applicable map we use
535 static struct GNUNET_CONTAINER_MultiHashMap32 *
536 get_map_by_chid (struct CadetClient *c,
537 struct GNUNET_CADET_ClientChannelNumber chid)
539 return (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
541 : c->incoming_channels;
546 * Handler for requests of deleting tunnels
548 * @param cls client identification of the client
549 * @param msg the actual message
552 handle_tunnel_destroy (void *cls,
553 const struct GNUNET_CADET_TunnelDestroyMessage *msg)
555 struct CadetClient *c = cls;
556 struct GNUNET_CADET_ClientChannelNumber chid;
557 struct GNUNET_CONTAINER_MultiHashMap32 *map;
558 struct CadetChannel *ch;
560 /* Retrieve tunnel */
561 chid = msg->channel_id;
562 map = get_map_by_chid (c,
564 ch = GNUNET_CONTAINER_multihashmap32_get (map,
565 ntohl (chid.channel_of_client));
568 /* Client attempted to destroy unknown channel */
570 GNUNET_SERVICE_client_drop (c->client);
573 LOG (GNUNET_ERROR_TYPE_INFO,
574 "Client %u is destroying channel %s\n",
577 GNUNET_assert (GNUNET_YES ==
578 GNUNET_CONTAINER_multihashmap32_remove (map,
579 ntohl (chid.channel_of_client),
581 GCCH_channel_local_destroy (ch);
582 GNUNET_SERVICE_client_continue (c->client);
587 * Check for client traffic data message is well-formed
589 * @param cls identification of the client
590 * @param msg the actual message
591 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
594 check_data (void *cls,
595 const struct GNUNET_CADET_LocalData *msg)
597 const struct GNUNET_MessageHeader *payload;
599 size_t payload_claimed_size;
601 /* Sanity check for message size */
602 payload_size = ntohs (msg->header.size) - sizeof (*msg);
603 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
604 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
607 return GNUNET_SYSERR;
609 payload = (struct GNUNET_MessageHeader *) &msg[1];
610 payload_claimed_size = ntohs (payload->size);
611 if (payload_size != payload_claimed_size)
614 return GNUNET_SYSERR;
621 * Handler for client traffic
623 * @param cls identification of the client
624 * @param msg the actual message
627 handle_data (void *cls,
628 const struct GNUNET_CADET_LocalData *msg)
630 struct CadetClient *c = cls;
631 struct GNUNET_CONTAINER_MultiHashMap32 *map;
632 struct GNUNET_CADET_ClientChannelNumber chid;
633 struct CadetChannel *ch;
634 const struct GNUNET_MessageHeader *payload;
636 chid = msg->channel_id;
637 map = get_map_by_chid (c,
639 ch = GNUNET_CONTAINER_multihashmap32_get (map,
640 ntohl (chid.channel_of_client));
643 /* Channel does not exist! */
645 GNUNET_SERVICE_client_drop (c->client);
649 payload = (const struct GNUNET_MessageHeader *) &msg[1];
650 LOG (GNUNET_ERROR_TYPE_DEBUG,
651 "Received %u bytes payload from client %u for channel %s\n",
652 ntohs (payload->size),
656 GCCH_handle_local_data (ch,
659 GNUNET_SERVICE_client_drop (c->client);
662 GNUNET_SERVICE_client_continue (c->client);
667 * Handler for client's ACKs for payload traffic.
669 * @param cls identification of the client.
670 * @param msg The actual message.
673 handle_ack (void *cls,
674 const struct GNUNET_CADET_LocalAck *msg)
676 struct CadetClient *c = cls;
677 struct GNUNET_CONTAINER_MultiHashMap32 *map;
678 struct GNUNET_CADET_ClientChannelNumber chid;
679 struct CadetChannel *ch;
681 chid = msg->channel_id;
682 map = get_map_by_chid (c,
684 ch = GNUNET_CONTAINER_multihashmap32_get (map,
685 ntohl (chid.channel_of_client));
688 /* Channel does not exist! */
690 GNUNET_SERVICE_client_drop (c->client);
693 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Got a local ACK from client %u for channel %s\n",
697 GCCH_handle_local_ack (ch);
698 GNUNET_SERVICE_client_continue (c->client);
703 * Iterator over all peers to send a monitoring client info about each peer.
705 * @param cls Closure ().
706 * @param peer Peer ID (tunnel remote peer).
707 * @param value Peer info.
708 * @return #GNUNET_YES, to keep iterating.
711 get_all_peers_iterator (void *cls,
712 const struct GNUNET_PeerIdentity *peer,
715 struct CadetClient *c = cls;
716 struct CadetPeer *p = value;
717 struct GNUNET_MQ_Envelope *env;
718 struct GNUNET_CADET_LocalInfoPeer *msg;
720 env = GNUNET_MQ_msg (msg,
721 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
722 msg->destination = *peer;
723 msg->paths = htons (GCP_count_paths (p));
724 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
726 GNUNET_MQ_send (c->mq,
733 * Handler for client's INFO PEERS request.
735 * @param cls Identification of the client.
736 * @param message The actual message.
739 handle_get_peers (void *cls,
740 const struct GNUNET_MessageHeader *message)
742 struct CadetClient *c = cls;
743 struct GNUNET_MQ_Envelope *env;
744 struct GNUNET_MessageHeader *reply;
746 GCP_iterate_all (&get_all_peers_iterator,
748 env = GNUNET_MQ_msg (reply,
749 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
750 GNUNET_MQ_send (c->mq,
752 GNUNET_SERVICE_client_continue (c->client);
757 * Iterator over all paths of a peer to build an InfoPeer message.
758 * Message contains blocks of peers, first not included.
760 * @param cls message queue for transmission
761 * @param path Path itself
762 * @param off offset of the peer on @a path
763 * @return #GNUNET_YES if should keep iterating.
764 * #GNUNET_NO otherwise.
767 path_info_iterator (void *cls,
768 struct CadetPeerPath *path,
771 struct GNUNET_MQ_Handle *mq = cls;
772 struct GNUNET_MQ_Envelope *env;
773 struct GNUNET_MessageHeader *resp;
774 struct GNUNET_PeerIdentity *id;
777 unsigned int path_length;
779 path_length = GCPP_get_length (path);
780 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
781 if (sizeof (*resp) + path_size > UINT16_MAX)
783 LOG (GNUNET_ERROR_TYPE_WARNING,
784 "Path of %u entries is too long for info message\n",
788 env = GNUNET_MQ_msg_extra (resp,
790 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
791 id = (struct GNUNET_PeerIdentity *) &resp[1];
793 /* Don't copy first peer. First peer is always the local one. Last
794 * peer is always the destination (leave as 0, EOL).
796 for (i = 0; i < off; i++)
797 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
806 * Handler for client's SHOW_PEER request.
808 * @param cls Identification of the client.
809 * @param msg The actual message.
812 handle_show_peer (void *cls,
813 const struct GNUNET_CADET_LocalInfo *msg)
815 struct CadetClient *c = cls;
817 struct GNUNET_MQ_Envelope *env;
818 struct GNUNET_MessageHeader *resp;
820 p = GCP_get (&msg->peer,
823 GCP_iterate_paths (p,
826 /* Send message with 0/0 to indicate the end */
827 env = GNUNET_MQ_msg (resp,
828 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
829 GNUNET_MQ_send (c->mq,
831 GNUNET_SERVICE_client_continue (c->client);
836 * Iterator over all tunnels to send a monitoring client info about each tunnel.
838 * @param cls Closure ().
839 * @param peer Peer ID (tunnel remote peer).
840 * @param value a `struct CadetPeer`
841 * @return #GNUNET_YES, to keep iterating.
844 get_all_tunnels_iterator (void *cls,
845 const struct GNUNET_PeerIdentity *peer,
848 struct CadetClient *c = cls;
849 struct CadetPeer *p = value;
850 struct GNUNET_MQ_Envelope *env;
851 struct GNUNET_CADET_LocalInfoTunnel *msg;
852 struct CadetTunnel *t;
854 t = GCP_get_tunnel (p,
858 env = GNUNET_MQ_msg (msg,
859 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
860 msg->destination = *peer;
861 msg->channels = htonl (GCT_count_channels (t));
862 msg->connections = htonl (GCT_count_any_connections (t));
863 msg->cstate = htons (0);
864 msg->estate = htons ((uint16_t) GCT_get_estate (t));
865 GNUNET_MQ_send (c->mq,
872 * Handler for client's INFO TUNNELS request.
874 * @param cls client Identification of the client.
875 * @param message The actual message.
878 handle_get_tunnels (void *cls,
879 const struct GNUNET_MessageHeader *message)
881 struct CadetClient *c = cls;
882 struct GNUNET_MQ_Envelope *env;
883 struct GNUNET_MessageHeader *reply;
885 GCP_iterate_all (&get_all_tunnels_iterator,
887 env = GNUNET_MQ_msg (reply,
888 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
889 GNUNET_MQ_send (c->mq,
891 GNUNET_SERVICE_client_continue (c->client);
899 iter_connection (void *cls,
900 struct CadetConnection *c)
902 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
903 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
905 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
906 h[msg->connections++] = *(GCC_get_id (c));
914 iter_channel (void *cls,
915 struct CadetChannel *ch)
917 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
918 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
919 struct GNUNET_CADET_ChannelTunnelNumber *chn
920 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
922 chn[msg->channels++] = GCCH_get_id (ch);
927 * Handler for client's SHOW_TUNNEL request.
929 * @param cls Identification of the client.
930 * @param msg The actual message.
933 handle_show_tunnel (void *cls,
934 const struct GNUNET_CADET_LocalInfo *msg)
936 struct CadetClient *c = cls;
937 struct GNUNET_MQ_Envelope *env;
938 struct GNUNET_CADET_LocalInfoTunnel *resp;
939 struct CadetTunnel *t;
944 p = GCP_get (&msg->peer,
946 t = GCP_get_tunnel (p,
950 /* We don't know the tunnel */
951 struct GNUNET_MQ_Envelope *env;
952 struct GNUNET_CADET_LocalInfoTunnel *warn;
954 LOG (GNUNET_ERROR_TYPE_INFO,
955 "Tunnel to %s unknown\n",
956 GNUNET_i2s_full (&msg->peer));
957 env = GNUNET_MQ_msg (warn,
958 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
959 warn->destination = msg->peer;
960 GNUNET_MQ_send (c->mq,
962 GNUNET_SERVICE_client_continue (c->client);
966 /* Initialize context */
967 ch_n = GCT_count_channels (t);
968 c_n = GCT_count_any_connections (t);
969 env = GNUNET_MQ_msg_extra (resp,
970 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
971 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
972 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
973 resp->destination = msg->peer;
974 /* Do not reorder! #iter_channel needs counters in HBO! */
975 GCT_iterate_connections (t,
978 GCT_iterate_channels (t,
981 resp->connections = htonl (resp->connections);
982 resp->channels = htonl (resp->channels);
983 resp->cstate = htons (0);
984 resp->estate = htons (GCT_get_estate (t));
985 GNUNET_MQ_send (c->mq,
987 GNUNET_SERVICE_client_continue (c->client);
992 * Iterator over all peers to dump info for each peer.
994 * @param cls Closure (unused).
995 * @param peer Peer ID (tunnel remote peer).
996 * @param value Peer info.
998 * @return #GNUNET_YES, to keep iterating.
1001 show_peer_iterator (void *cls,
1002 const struct GNUNET_PeerIdentity *peer,
1005 struct CadetPeer *p = value;
1006 struct CadetTunnel *t;
1008 t = GCP_get_tunnel (p,
1012 GNUNET_ERROR_TYPE_ERROR);
1013 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1019 * Handler for client's INFO_DUMP request.
1021 * @param cls Identification of the client.
1022 * @param message The actual message.
1025 handle_info_dump (void *cls,
1026 const struct GNUNET_MessageHeader *message)
1028 struct CadetClient *c = cls;
1030 LOG (GNUNET_ERROR_TYPE_INFO,
1031 "Received dump info request from client %u\n",
1034 LOG (GNUNET_ERROR_TYPE_ERROR,
1035 "*************************** DUMP START ***************************\n");
1036 for (struct CadetClient *ci = clients_head; NULL != ci; ci = ci->next)
1038 LOG (GNUNET_ERROR_TYPE_ERROR,
1039 "Client %u (%p), handle: %p, ports: %u, own channels: %u, incoming channels: %u\n",
1044 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1046 GNUNET_CONTAINER_multihashmap32_size (ci->own_channels),
1047 GNUNET_CONTAINER_multihashmap32_size (ci->incoming_channels));
1049 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1050 GCP_iterate_all (&show_peer_iterator,
1053 LOG (GNUNET_ERROR_TYPE_ERROR,
1054 "**************************** DUMP END ****************************\n");
1056 GNUNET_SERVICE_client_continue (c->client);
1062 * Callback called when a client connects to the service.
1064 * @param cls closure for the service
1065 * @param client the new client that connected to the service
1066 * @param mq the message queue used to send messages to the client
1070 client_connect_cb (void *cls,
1071 struct GNUNET_SERVICE_Client *client,
1072 struct GNUNET_MQ_Handle *mq)
1074 struct CadetClient *c;
1076 c = GNUNET_new (struct CadetClient);
1079 c->id = next_client_id++; /* overflow not important: just for debug */
1081 = GNUNET_CONTAINER_multihashmap32_create (32);
1082 c->incoming_channels
1083 = GNUNET_CONTAINER_multihashmap32_create (32);
1084 GNUNET_CONTAINER_DLL_insert (clients_head,
1087 GNUNET_STATISTICS_update (stats,
1096 * Iterator for deleting each channel whose client endpoint disconnected.
1098 * @param cls Closure (client that has disconnected).
1099 * @param key The local channel id (used to access the hashmap).
1100 * @param value The value stored at the key (channel to destroy).
1101 * @return #GNUNET_OK, keep iterating.
1104 own_channel_destroy_iterator (void *cls,
1108 struct CadetClient *c = cls;
1109 struct CadetChannel *ch = value;
1111 GNUNET_assert (GNUNET_YES ==
1112 GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Destroying own channel %s, due to client %u shutdown.\n",
1119 GCCH_channel_local_destroy (ch);
1125 * Iterator for deleting each channel whose client endpoint disconnected.
1127 * @param cls Closure (client that has disconnected).
1128 * @param key The local channel id (used to access the hashmap).
1129 * @param value The value stored at the key (channel to destroy).
1130 * @return #GNUNET_OK, keep iterating.
1133 incoming_channel_destroy_iterator (void *cls,
1137 struct CadetChannel *ch = value;
1138 struct CadetClient *c = cls;
1140 GNUNET_assert (GNUNET_YES ==
1141 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1145 LOG (GNUNET_ERROR_TYPE_DEBUG,
1146 "Destroying incoming channel %s due to client %u shutdown.\n",
1149 GCCH_channel_incoming_destroy (ch);
1155 * Remove client's ports from the global hashmap on disconnect.
1157 * @param cls Closure (unused).
1159 * @param value the `struct CadetClient` to remove
1160 * @return #GNUNET_OK, keep iterating.
1163 client_release_ports (void *cls,
1164 const struct GNUNET_HashCode *key,
1167 struct CadetClient *c = value;
1169 GNUNET_assert (GNUNET_YES ==
1170 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1173 GNUNET_assert (GNUNET_YES ==
1174 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1182 * Callback called when a client disconnected from the service
1184 * @param cls closure for the service
1185 * @param client the client that disconnected
1186 * @param internal_cls should be equal to @a c
1189 client_disconnect_cb (void *cls,
1190 struct GNUNET_SERVICE_Client *client,
1193 struct CadetClient *c = internal_cls;
1195 GNUNET_assert (c->client == client);
1196 c->shutting_down = GNUNET_YES;
1197 if (NULL != c->own_channels)
1199 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
1200 &own_channel_destroy_iterator,
1202 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
1204 if (NULL != c->incoming_channels)
1206 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
1207 &incoming_channel_destroy_iterator,
1209 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
1211 if (NULL != c->ports)
1213 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1214 &client_release_ports,
1216 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1218 GNUNET_CONTAINER_DLL_remove (clients_head,
1221 GNUNET_STATISTICS_update (stats,
1230 * Setup CADET internals.
1232 * @param cls closure
1233 * @param server the initialized server
1234 * @param c configuration to use
1238 const struct GNUNET_CONFIGURATION_Handle *c,
1239 struct GNUNET_SERVICE_Handle *service)
1243 GNUNET_CONFIGURATION_get_value_number (c,
1248 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1251 "needs to be a number");
1252 ratchet_messages = 64;
1255 GNUNET_CONFIGURATION_get_value_time (c,
1260 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1263 "need delay value");
1264 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1267 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1268 if (NULL == my_private_key)
1271 GNUNET_SCHEDULER_shutdown ();
1274 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1275 &my_full_id.public_key);
1276 stats = GNUNET_STATISTICS_create ("cadet",
1278 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1280 ats_ch = GNUNET_ATS_connectivity_init (c);
1281 /* FIXME: optimize code to allow GNUNET_YES here! */
1282 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1284 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1286 peers = GNUNET_CONTAINER_multipeermap_create (16,
1288 connections = GNUNET_CONTAINER_multishortmap_create (256,
1293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1294 "CADET starting at peer %s\n",
1295 GNUNET_i2s (&my_full_id));
1301 * Define "main" method using service macro.
1305 GNUNET_SERVICE_OPTION_NONE,
1308 &client_disconnect_cb,
1310 GNUNET_MQ_hd_fixed_size (port_open,
1311 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1312 struct GNUNET_CADET_PortMessage,
1314 GNUNET_MQ_hd_fixed_size (port_close,
1315 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1316 struct GNUNET_CADET_PortMessage,
1318 GNUNET_MQ_hd_fixed_size (tunnel_create,
1319 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE,
1320 struct GNUNET_CADET_TunnelCreateMessage,
1322 GNUNET_MQ_hd_fixed_size (tunnel_destroy,
1323 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY,
1324 struct GNUNET_CADET_TunnelDestroyMessage,
1326 GNUNET_MQ_hd_var_size (data,
1327 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1328 struct GNUNET_CADET_LocalData,
1330 GNUNET_MQ_hd_fixed_size (ack,
1331 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1332 struct GNUNET_CADET_LocalAck,
1334 GNUNET_MQ_hd_fixed_size (get_peers,
1335 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1336 struct GNUNET_MessageHeader,
1338 GNUNET_MQ_hd_fixed_size (show_peer,
1339 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1340 struct GNUNET_CADET_LocalInfo,
1342 GNUNET_MQ_hd_fixed_size (get_tunnels,
1343 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1344 struct GNUNET_MessageHeader,
1346 GNUNET_MQ_hd_fixed_size (show_tunnel,
1347 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1348 struct GNUNET_CADET_LocalInfo,
1350 GNUNET_MQ_hd_fixed_size (info_dump,
1351 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1352 struct GNUNET_MessageHeader,
1354 GNUNET_MQ_handler_end ());
1356 /* end of gnunet-service-cadet-new.c */