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_dht.h"
44 #include "gnunet-service-cadet-new_hello.h"
45 #include "gnunet-service-cadet-new_tunnels.h"
46 #include "gnunet-service-cadet-new_peer.h"
47 #include "gnunet-service-cadet-new_paths.h"
49 #define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
53 * Struct containing information about a client of the service
60 struct CadetClient *next;
65 struct CadetClient *prev;
68 * Tunnels that belong to this client, indexed by local id
70 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
73 * Tunnels this client has accepted, indexed by incoming local id
75 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
78 * Channel ID for the next incoming channel.
80 struct GNUNET_CADET_ClientChannelNumber next_chid;
83 * Handle to communicate with the client
85 struct GNUNET_MQ_Handle *mq;
90 struct GNUNET_SERVICE_Client *client;
93 * Ports that this client has declared interest in.
94 * Indexed by port, contains *Client.
96 struct GNUNET_CONTAINER_MultiHashMap *ports;
99 * Whether the client is active or shutting down (don't send confirmations
100 * to a client that is shutting down).
105 * ID of the client, mainly for debug messages
110 /******************************************************************************/
111 /*********************** GLOBAL VARIABLES ****************************/
112 /******************************************************************************/
114 /****************************** Global variables ******************************/
117 * Handle to the statistics service.
119 struct GNUNET_STATISTICS_Handle *stats;
122 * Handle to communicate with ATS.
124 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
129 struct GNUNET_PeerIdentity my_full_id;
134 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
137 * Signal that shutdown is happening: prevent recover measures.
142 * DLL with all the clients, head.
144 static struct CadetClient *clients_head;
147 * DLL with all the clients, tail.
149 static struct CadetClient *clients_tail;
152 * Next ID to assign to a client.
154 static unsigned int next_client_id;
157 * All ports clients of this peer have opened.
159 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
162 * Map from ports to channels where the ports were closed at the
163 * time we got the inbound connection.
164 * Indexed by port, contains `struct CadetChannel`.
166 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
169 * Map from PIDs to `struct CadetPeer` entries.
171 struct GNUNET_CONTAINER_MultiPeerMap *peers;
174 * Map from expanded connection hash codes to `struct CadetConnection` objects.
176 struct GNUNET_CONTAINER_MultiHashMap *connections;
179 * How many messages are needed to trigger an AXOLOTL ratchet advance.
181 unsigned long long ratchet_messages;
184 * How long until we trigger a ratched advance due to time.
186 struct GNUNET_TIME_Relative ratchet_time;
191 * Send a message to a client.
193 * @param c client to get the message
194 * @param env envelope with the message
197 GSC_send_to_client (struct CadetClient *c,
198 struct GNUNET_MQ_Envelope *env)
200 GNUNET_MQ_send (c->mq,
206 * Return identifier for a client as a string.
208 * @param c client to identify
209 * @return string for debugging
212 GSC_2s (struct CadetClient *c)
217 return "Client(NULL)";
218 GNUNET_snprintf (buf,
227 * Obtain the next LID to use for incoming connections to
230 * @param c client handle
232 static struct GNUNET_CADET_ClientChannelNumber
233 client_get_next_lid (struct CadetClient *c)
235 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_chid;
237 /* increment until we have a free one... */
239 GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels,
240 ntohl (ccn.channel_of_client)))
242 ccn.channel_of_client
243 = htonl (1 + (ntohl (ccn.channel_of_client)));
244 if (ntohl (ccn.channel_of_client) >=
245 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
246 ccn.channel_of_client = htonl (0);
248 c->next_chid.channel_of_client
249 = htonl (1 + (ntohl (ccn.channel_of_client)));
255 * Bind incoming channel to this client, and notify client
256 * about incoming connection.
258 * @param c client to bind to
259 * @param ch channel to be bound
260 * @param dest peer that establishes the connection
261 * @param port port number
262 * @param options options
263 * @return local channel number assigned to the new client
265 struct GNUNET_CADET_ClientChannelNumber
266 GSC_bind (struct CadetClient *c,
267 struct CadetChannel *ch,
268 struct CadetPeer *dest,
269 const struct GNUNET_HashCode *port,
272 struct GNUNET_MQ_Envelope *env;
273 struct GNUNET_CADET_ChannelCreateMessage *msg;
274 struct GNUNET_CADET_ClientChannelNumber lid;
276 lid = client_get_next_lid (c);
277 GNUNET_assert (GNUNET_YES ==
278 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
279 ntohl (lid.channel_of_client),
281 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
283 /* notify local client about incoming connection! */
284 env = GNUNET_MQ_msg (msg,
285 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
286 msg->channel_id = lid;
288 msg->opt = htonl (options);
289 msg->peer = *GCP_get_id (dest);
290 GSC_send_to_client (c,
296 /******************************************************************************/
297 /************************ MAIN FUNCTIONS ****************************/
298 /******************************************************************************/
301 * Task run during shutdown.
306 shutdown_task (void *cls)
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 shutting_down = GNUNET_YES;
314 GNUNET_STATISTICS_destroy (stats,
318 if (NULL != open_ports)
320 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
323 if (NULL != loose_channels)
325 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
326 loose_channels = NULL;
328 /* All channels, connections and CORE must be down before this point. */
329 GCP_destroy_all_peers ();
332 GNUNET_CONTAINER_multipeermap_destroy (peers);
335 if (NULL != connections)
337 GNUNET_CONTAINER_multihashmap_destroy (connections);
342 GNUNET_ATS_connectivity_done (ats_ch);
347 GNUNET_free_non_null (my_private_key);
348 my_private_key = NULL;
353 * We had a remote connection @a value to port @a port before
354 * client @a cls opened port @a port. Bind them now.
356 * @param cls the `struct CadetClient`
357 * @param port the port
358 * @param value the `struct CadetChannel`
359 * @return #GNUNET_YES (iterate over all such channels)
362 bind_loose_channel (void *cls,
363 const struct GNUNET_HashCode *port,
366 struct CadetClient *c = cls;
367 struct CadetChannel *ch = value;
371 GNUNET_assert (GNUNET_YES ==
372 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
380 * Handler for port open requests.
382 * @param cls Identification of the client.
383 * @param pmsg The actual message.
386 handle_port_open (void *cls,
387 const struct GNUNET_CADET_PortMessage *pmsg)
389 struct CadetClient *c = cls;
391 LOG (GNUNET_ERROR_TYPE_DEBUG,
392 "Open port %s requested by client %u\n",
393 GNUNET_h2s (&pmsg->port),
395 if (NULL == c->ports)
396 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
399 GNUNET_CONTAINER_multihashmap_put (c->ports,
402 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
405 GNUNET_SERVICE_client_drop (c->client);
408 /* store in global hashmap */
409 /* FIXME only allow one client to have the port open,
410 * have a backup hashmap with waiting clients */
411 GNUNET_CONTAINER_multihashmap_put (open_ports,
414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
415 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
419 GNUNET_SERVICE_client_continue (c->client);
424 * Handler for port close requests.
426 * @param cls Identification of the client.
427 * @param pmsg The actual message.
430 handle_port_close (void *cls,
431 const struct GNUNET_CADET_PortMessage *pmsg)
433 struct CadetClient *c = cls;
435 LOG (GNUNET_ERROR_TYPE_DEBUG,
436 "Open port %s requested by client %u\n",
437 GNUNET_h2s (&pmsg->port),
440 GNUNET_CONTAINER_multihashmap_remove (c->ports,
445 GNUNET_SERVICE_client_drop (c->client);
448 GNUNET_assert (GNUNET_YES ==
449 GNUNET_CONTAINER_multihashmap_remove (open_ports,
453 GNUNET_SERVICE_client_continue (c->client);
458 * Handler for requests of new channels.
460 * @param cls Identification of the client.
461 * @param ccm The actual message.
464 handle_channel_create (void *cls,
465 const struct GNUNET_CADET_ChannelCreateMessage *ccm)
467 struct CadetClient *c = cls;
468 struct CadetChannel *ch;
469 struct GNUNET_CADET_ClientChannelNumber chid;
470 struct CadetPeer *dst;
472 chid = ccm->channel_id;
473 if (ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
475 /* Channel ID not in allowed range. */
477 GNUNET_SERVICE_client_drop (c->client);
480 ch = GNUNET_CONTAINER_multihashmap32_get (c->own_channels,
481 ntohl (chid.channel_of_client));
484 /* Channel ID already in use. Not allowed. */
486 GNUNET_SERVICE_client_drop (c->client);
490 dst = GCP_get (&ccm->peer,
494 ch = GCCH_channel_local_new (c,
502 GNUNET_SERVICE_client_drop (c->client);
505 GNUNET_assert (GNUNET_YES ==
506 GNUNET_CONTAINER_multihashmap32_put (c->own_channels,
507 ntohl (chid.channel_of_client),
509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "New channel %s to %s at port %s requested by client %u\n",
514 GNUNET_i2s (&ccm->peer),
515 GNUNET_h2s (&ccm->port),
517 GNUNET_SERVICE_client_continue (c->client);
522 * Return the map which we use for client @a c for a channel ID of @a chid
524 * @param c client to find map for
525 * @param chid chid to find map for
526 * @return applicable map we use
528 static struct GNUNET_CONTAINER_MultiHashMap32 *
529 get_map_by_chid (struct CadetClient *c,
530 struct GNUNET_CADET_ClientChannelNumber chid)
532 return (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
534 : c->incoming_channels;
539 * Handler for requests of deleting tunnels
541 * @param cls client identification of the client
542 * @param msg the actual message
545 handle_channel_destroy (void *cls,
546 const struct GNUNET_CADET_ChannelDestroyMessage *msg)
548 struct CadetClient *c = cls;
549 struct GNUNET_CADET_ClientChannelNumber chid;
550 struct GNUNET_CONTAINER_MultiHashMap32 *map;
551 struct CadetChannel *ch;
553 /* Retrieve tunnel */
554 chid = msg->channel_id;
555 map = get_map_by_chid (c,
557 ch = GNUNET_CONTAINER_multihashmap32_get (map,
558 ntohl (chid.channel_of_client));
561 /* Client attempted to destroy unknown channel */
563 GNUNET_SERVICE_client_drop (c->client);
566 LOG (GNUNET_ERROR_TYPE_INFO,
567 "Client %u is destroying channel %s\n",
570 GNUNET_assert (GNUNET_YES ==
571 GNUNET_CONTAINER_multihashmap32_remove (map,
572 ntohl (chid.channel_of_client),
574 GCCH_channel_local_destroy (ch);
575 GNUNET_SERVICE_client_continue (c->client);
580 * Check for client traffic data message is well-formed
582 * @param cls identification of the client
583 * @param msg the actual message
584 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
587 check_data (void *cls,
588 const struct GNUNET_CADET_LocalData *msg)
590 const struct GNUNET_MessageHeader *payload;
592 size_t payload_claimed_size;
594 /* Sanity check for message size */
595 payload_size = ntohs (msg->header.size) - sizeof (*msg);
596 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
597 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
600 return GNUNET_SYSERR;
602 payload = (struct GNUNET_MessageHeader *) &msg[1];
603 payload_claimed_size = ntohs (payload->size);
604 if (payload_size != payload_claimed_size)
607 return GNUNET_SYSERR;
614 * Handler for client traffic
616 * @param cls identification of the client
617 * @param msg the actual message
620 handle_data (void *cls,
621 const struct GNUNET_CADET_LocalData *msg)
623 struct CadetClient *c = cls;
624 struct GNUNET_CONTAINER_MultiHashMap32 *map;
625 struct GNUNET_CADET_ClientChannelNumber chid;
626 struct CadetChannel *ch;
627 const struct GNUNET_MessageHeader *payload;
630 map = get_map_by_chid (c,
632 ch = GNUNET_CONTAINER_multihashmap32_get (map,
633 ntohl (chid.channel_of_client));
636 /* Channel does not exist! */
638 GNUNET_SERVICE_client_drop (c->client);
642 payload = (const struct GNUNET_MessageHeader *) &msg[1];
643 LOG (GNUNET_ERROR_TYPE_DEBUG,
644 "Received %u bytes payload from client %u for channel %s\n",
645 ntohs (payload->size),
649 GCCH_handle_local_data (ch,
652 GNUNET_SERVICE_client_drop (c->client);
655 GNUNET_SERVICE_client_continue (c->client);
660 * Handler for client's ACKs for payload traffic.
662 * @param cls identification of the client.
663 * @param msg The actual message.
666 handle_ack (void *cls,
667 const struct GNUNET_CADET_LocalAck *msg)
669 struct CadetClient *c = cls;
670 struct GNUNET_CONTAINER_MultiHashMap32 *map;
671 struct GNUNET_CADET_ClientChannelNumber chid;
672 struct CadetChannel *ch;
674 chid = msg->channel_id;
675 map = get_map_by_chid (c,
677 ch = GNUNET_CONTAINER_multihashmap32_get (map,
678 ntohl (chid.channel_of_client));
681 /* Channel does not exist! */
683 GNUNET_SERVICE_client_drop (c->client);
686 LOG (GNUNET_ERROR_TYPE_DEBUG,
687 "Got a local ACK from client %u for channel %s\n",
690 GCCH_handle_local_ack (ch);
691 GNUNET_SERVICE_client_continue (c->client);
696 * Iterator over all peers to send a monitoring client info about each peer.
698 * @param cls Closure ().
699 * @param peer Peer ID (tunnel remote peer).
700 * @param value Peer info.
701 * @return #GNUNET_YES, to keep iterating.
704 get_all_peers_iterator (void *cls,
705 const struct GNUNET_PeerIdentity *peer,
708 struct CadetClient *c = cls;
709 struct CadetPeer *p = value;
710 struct GNUNET_MQ_Envelope *env;
711 struct GNUNET_CADET_LocalInfoPeer *msg;
713 env = GNUNET_MQ_msg (msg,
714 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
715 msg->destination = *peer;
716 msg->paths = htons (GCP_count_paths (p));
717 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
719 GNUNET_MQ_send (c->mq,
726 * Handler for client's INFO PEERS request.
728 * @param cls Identification of the client.
729 * @param message The actual message.
732 handle_get_peers (void *cls,
733 const struct GNUNET_MessageHeader *message)
735 struct CadetClient *c = cls;
736 struct GNUNET_MQ_Envelope *env;
737 struct GNUNET_MessageHeader *reply;
739 GCP_iterate_all (&get_all_peers_iterator,
741 env = GNUNET_MQ_msg (reply,
742 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
743 GNUNET_MQ_send (c->mq,
745 GNUNET_SERVICE_client_continue (c->client);
750 * Iterator over all paths of a peer to build an InfoPeer message.
751 * Message contains blocks of peers, first not included.
753 * @param cls message queue for transmission
754 * @param path Path itself
755 * @param off offset of the peer on @a path
756 * @return #GNUNET_YES if should keep iterating.
757 * #GNUNET_NO otherwise.
760 path_info_iterator (void *cls,
761 struct CadetPeerPath *path,
764 struct GNUNET_MQ_Handle *mq = cls;
765 struct GNUNET_MQ_Envelope *env;
766 struct GNUNET_MessageHeader *resp;
767 struct GNUNET_PeerIdentity *id;
770 unsigned int path_length;
772 path_length = GCPP_get_length (path);
773 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
774 if (sizeof (*resp) + path_size > UINT16_MAX)
776 LOG (GNUNET_ERROR_TYPE_WARNING,
777 "Path of %u entries is too long for info message\n",
781 env = GNUNET_MQ_msg_extra (resp,
783 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
784 id = (struct GNUNET_PeerIdentity *) &resp[1];
786 /* Don't copy first peer. First peer is always the local one. Last
787 * peer is always the destination (leave as 0, EOL).
789 for (i = 0; i < off; i++)
790 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
799 * Handler for client's SHOW_PEER request.
801 * @param cls Identification of the client.
802 * @param msg The actual message.
805 handle_show_peer (void *cls,
806 const struct GNUNET_CADET_LocalInfo *msg)
808 struct CadetClient *c = cls;
810 struct GNUNET_MQ_Envelope *env;
811 struct GNUNET_MessageHeader *resp;
813 p = GCP_get (&msg->peer,
816 GCP_iterate_paths (p,
819 /* Send message with 0/0 to indicate the end */
820 env = GNUNET_MQ_msg (resp,
821 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
822 GNUNET_MQ_send (c->mq,
824 GNUNET_SERVICE_client_continue (c->client);
829 * Iterator over all tunnels to send a monitoring client info about each tunnel.
831 * @param cls Closure ().
832 * @param peer Peer ID (tunnel remote peer).
833 * @param value a `struct CadetPeer`
834 * @return #GNUNET_YES, to keep iterating.
837 get_all_tunnels_iterator (void *cls,
838 const struct GNUNET_PeerIdentity *peer,
841 struct CadetClient *c = cls;
842 struct CadetPeer *p = value;
843 struct GNUNET_MQ_Envelope *env;
844 struct GNUNET_CADET_LocalInfoTunnel *msg;
845 struct CadetTunnel *t;
847 t = GCP_get_tunnel (p,
851 env = GNUNET_MQ_msg (msg,
852 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
853 msg->destination = *peer;
854 msg->channels = htonl (GCT_count_channels (t));
855 msg->connections = htonl (GCT_count_any_connections (t));
856 msg->cstate = htons ((uint16_t) GCT_get_cstate (t));
857 msg->estate = htons ((uint16_t) GCT_get_estate (t));
858 GNUNET_MQ_send (c->mq,
865 * Handler for client's INFO TUNNELS request.
867 * @param cls client Identification of the client.
868 * @param message The actual message.
871 handle_get_tunnels (void *cls,
872 const struct GNUNET_MessageHeader *message)
874 struct CadetClient *c = cls;
875 struct GNUNET_MQ_Envelope *env;
876 struct GNUNET_MessageHeader *reply;
878 GCP_iterate_all (&get_all_tunnels_iterator,
880 env = GNUNET_MQ_msg (reply,
881 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
882 GNUNET_MQ_send (c->mq,
884 GNUNET_SERVICE_client_continue (c->client);
892 iter_connection (void *cls,
893 struct CadetConnection *c)
895 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
896 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
898 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
899 h[msg->connections++] = *(GCC_get_id (c));
907 iter_channel (void *cls,
908 struct CadetChannel *ch)
910 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
911 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
912 struct GCT_ChannelTunnelNumber *chn
913 = (struct GCT_ChannelTunnelNumber *) &h[msg->connections];
915 chn[msg->channels++] = GCCH_get_id (ch);
920 * Handler for client's SHOW_TUNNEL request.
922 * @param cls Identification of the client.
923 * @param msg The actual message.
926 handle_show_tunnel (void *cls,
927 const struct GNUNET_CADET_LocalInfo *msg)
929 struct CadetClient *c = cls;
930 struct GNUNET_MQ_Envelope *env;
931 struct GNUNET_CADET_LocalInfoTunnel *resp;
932 struct CadetTunnel *t;
937 p = GCP_get (&msg->peer,
939 t = GCP_get_tunnel (p,
943 /* We don't know the tunnel */
944 struct GNUNET_MQ_Envelope *env;
945 struct GNUNET_CADET_LocalInfoTunnel *warn;
947 LOG (GNUNET_ERROR_TYPE_INFO,
948 "Tunnel to %s unknown\n",
949 GNUNET_i2s_full (&msg->peer));
950 env = GNUNET_MQ_msg (warn,
951 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
952 warn->destination = msg->peer;
953 GNUNET_MQ_send (c->mq,
955 GNUNET_SERVICE_client_continue (c->client);
959 /* Initialize context */
960 ch_n = GCT_count_channels (t);
961 c_n = GCT_count_any_connections (t);
962 env = GNUNET_MQ_msg_extra (resp,
963 c_n * sizeof (struct GNUNET_CADET_Hash) +
964 ch_n * sizeof (struct GCT_ChannelTunnelNumber),
965 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
966 resp->destination = msg->peer;
967 /* Do not reorder! #iter_channel needs counters in HBO! */
968 GCT_iterate_connections (t,
971 GCT_iterate_channels (t,
974 resp->connections = htonl (resp->connections);
975 resp->channels = htonl (resp->channels);
976 resp->cstate = htons (GCT_get_cstate (t));
977 resp->estate = htons (GCT_get_estate (t));
978 GNUNET_MQ_send (c->mq,
980 GNUNET_SERVICE_client_continue (c->client);
985 * Iterator over all peers to dump info for each peer.
987 * @param cls Closure (unused).
988 * @param peer Peer ID (tunnel remote peer).
989 * @param value Peer info.
991 * @return #GNUNET_YES, to keep iterating.
994 show_peer_iterator (void *cls,
995 const struct GNUNET_PeerIdentity *peer,
998 struct CadetPeer *p = value;
999 struct CadetTunnel *t;
1001 t = GCP_get_tunnel (p,
1005 GNUNET_ERROR_TYPE_ERROR);
1006 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1012 * Handler for client's INFO_DUMP request.
1014 * @param cls Identification of the client.
1015 * @param message The actual message.
1018 handle_info_dump (void *cls,
1019 const struct GNUNET_MessageHeader *message)
1021 struct CadetClient *c = cls;
1023 LOG (GNUNET_ERROR_TYPE_INFO,
1024 "Received dump info request from client %u\n",
1027 LOG (GNUNET_ERROR_TYPE_ERROR,
1028 "*************************** DUMP START ***************************\n");
1029 for (struct CadetClient *ci = clients_head; NULL != ci; ci = ci->next)
1031 LOG (GNUNET_ERROR_TYPE_ERROR,
1032 "Client %u (%p), handle: %p, ports: %u, own channels: %u, incoming channels: %u\n",
1037 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1039 GNUNET_CONTAINER_multihashmap32_size (ci->own_channels),
1040 GNUNET_CONTAINER_multihashmap32_size (ci->incoming_channels));
1042 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1043 GCP_iterate_all (&show_peer_iterator,
1046 LOG (GNUNET_ERROR_TYPE_ERROR,
1047 "**************************** DUMP END ****************************\n");
1049 GNUNET_SERVICE_client_continue (c->client);
1055 * Callback called when a client connects to the service.
1057 * @param cls closure for the service
1058 * @param client the new client that connected to the service
1059 * @param mq the message queue used to send messages to the client
1063 client_connect_cb (void *cls,
1064 struct GNUNET_SERVICE_Client *client,
1065 struct GNUNET_MQ_Handle *mq)
1067 struct CadetClient *c;
1069 c = GNUNET_new (struct CadetClient);
1072 c->id = next_client_id++; /* overflow not important: just for debug */
1074 = GNUNET_CONTAINER_multihashmap32_create (32);
1075 c->incoming_channels
1076 = GNUNET_CONTAINER_multihashmap32_create (32);
1077 GNUNET_CONTAINER_DLL_insert (clients_head,
1080 GNUNET_STATISTICS_update (stats,
1089 * Iterator for deleting each channel whose client endpoint disconnected.
1091 * @param cls Closure (client that has disconnected).
1092 * @param key The local channel id (used to access the hashmap).
1093 * @param value The value stored at the key (channel to destroy).
1094 * @return #GNUNET_OK, keep iterating.
1097 own_channel_destroy_iterator (void *cls,
1101 struct CadetClient *c = cls;
1102 struct CadetChannel *ch = value;
1104 GNUNET_assert (GNUNET_YES ==
1105 GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1108 LOG (GNUNET_ERROR_TYPE_DEBUG,
1109 "Destroying own channel %s, due to client %u shutdown.\n",
1112 GCCH_channel_local_destroy (ch);
1118 * Iterator for deleting each channel whose client endpoint disconnected.
1120 * @param cls Closure (client that has disconnected).
1121 * @param key The local channel id (used to access the hashmap).
1122 * @param value The value stored at the key (channel to destroy).
1123 * @return #GNUNET_OK, keep iterating.
1126 incoming_channel_destroy_iterator (void *cls,
1130 struct CadetChannel *ch = value;
1131 struct CadetClient *c = cls;
1133 GNUNET_assert (GNUNET_YES ==
1134 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 "Destroying incoming channel %s due to client %u shutdown.\n",
1142 GCCH_channel_incoming_destroy (ch);
1148 * Remove client's ports from the global hashmap on disconnect.
1150 * @param cls Closure (unused).
1152 * @param value the `struct CadetClient` to remove
1153 * @return #GNUNET_OK, keep iterating.
1156 client_release_ports (void *cls,
1157 const struct GNUNET_HashCode *key,
1160 struct CadetClient *c = value;
1162 GNUNET_assert (GNUNET_YES ==
1163 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1166 GNUNET_assert (GNUNET_YES ==
1167 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1175 * Callback called when a client disconnected from the service
1177 * @param cls closure for the service
1178 * @param client the client that disconnected
1179 * @param internal_cls should be equal to @a c
1182 client_disconnect_cb (void *cls,
1183 struct GNUNET_SERVICE_Client *client,
1186 struct CadetClient *c = internal_cls;
1188 GNUNET_assert (c->client == client);
1189 c->shutting_down = GNUNET_YES;
1190 if (NULL != c->own_channels)
1192 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
1193 &own_channel_destroy_iterator,
1195 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
1197 if (NULL != c->incoming_channels)
1199 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
1200 &incoming_channel_destroy_iterator,
1202 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
1204 if (NULL != c->ports)
1206 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1207 &client_release_ports,
1209 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1211 GNUNET_CONTAINER_DLL_remove (clients_head,
1214 GNUNET_STATISTICS_update (stats,
1223 * Setup CADET internals.
1225 * @param cls closure
1226 * @param server the initialized server
1227 * @param c configuration to use
1231 const struct GNUNET_CONFIGURATION_Handle *c,
1232 struct GNUNET_SERVICE_Handle *service)
1235 GNUNET_CONFIGURATION_get_value_number (c,
1240 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1243 "needs to be a number");
1244 ratchet_messages = 64;
1247 GNUNET_CONFIGURATION_get_value_time (c,
1252 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1255 "need delay value");
1256 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1259 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1260 if (NULL == my_private_key)
1263 GNUNET_SCHEDULER_shutdown ();
1266 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1267 &my_full_id.public_key);
1268 stats = GNUNET_STATISTICS_create ("cadet",
1270 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1272 ats_ch = GNUNET_ATS_connectivity_init (c);
1273 /* FIXME: optimize code to allow GNUNET_YES here! */
1274 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1276 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1278 peers = GNUNET_CONTAINER_multipeermap_create (16,
1280 connections = GNUNET_CONTAINER_multihashmap_create (256,
1284 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1285 "CADET starting at peer %s\n",
1286 GNUNET_i2s (&my_full_id));
1292 * Define "main" method using service macro.
1296 GNUNET_SERVICE_OPTION_NONE,
1299 &client_disconnect_cb,
1301 GNUNET_MQ_hd_fixed_size (port_open,
1302 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1303 struct GNUNET_CADET_PortMessage,
1305 GNUNET_MQ_hd_fixed_size (port_close,
1306 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1307 struct GNUNET_CADET_PortMessage,
1309 GNUNET_MQ_hd_fixed_size (channel_create,
1310 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1311 struct GNUNET_CADET_ChannelCreateMessage,
1313 GNUNET_MQ_hd_fixed_size (channel_destroy,
1314 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1315 struct GNUNET_CADET_ChannelDestroyMessage,
1317 GNUNET_MQ_hd_var_size (data,
1318 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1319 struct GNUNET_CADET_LocalData,
1321 GNUNET_MQ_hd_fixed_size (ack,
1322 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1323 struct GNUNET_CADET_LocalAck,
1325 GNUNET_MQ_hd_fixed_size (get_peers,
1326 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1327 struct GNUNET_MessageHeader,
1329 GNUNET_MQ_hd_fixed_size (show_peer,
1330 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1331 struct GNUNET_CADET_LocalInfo,
1333 GNUNET_MQ_hd_fixed_size (get_tunnels,
1334 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1335 struct GNUNET_MessageHeader,
1337 GNUNET_MQ_hd_fixed_size (show_tunnel,
1338 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1339 struct GNUNET_CADET_LocalInfo,
1341 GNUNET_MQ_hd_fixed_size (info_dump,
1342 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1343 struct GNUNET_MessageHeader,
1345 GNUNET_MQ_handler_end ());
1347 /* end of gnunet-service-cadet-new.c */