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 * Callback invoked on all peers to destroy all tunnels
315 * that may still exist.
318 * @param pid identify of a peer
319 * @param value a `struct CadetPeer` that may still have a tunnel
320 * @return #GNUNET_OK (iterate over all entries)
323 destroy_tunnels_now (void *cls,
324 const struct GNUNET_PeerIdentity *pid,
327 struct CadetPeer *cp = value;
328 struct CadetTunnel *t = GCP_get_tunnel (cp,
332 GCT_destroy_tunnel_now (t);
338 * Task run during shutdown.
343 shutdown_task (void *cls)
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 shutting_down = GNUNET_YES;
351 GNUNET_STATISTICS_destroy (stats,
355 if (NULL != open_ports)
357 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
360 if (NULL != loose_channels)
362 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
363 loose_channels = NULL;
365 /* Destroy tunnels. Note that all channels must be destroyed first! */
366 GCP_iterate_all (&destroy_tunnels_now,
368 /* All tunnels, channels, connections and CORE must be down before this point. */
369 GCP_destroy_all_peers ();
372 GNUNET_CONTAINER_multipeermap_destroy (peers);
375 if (NULL != connections)
377 GNUNET_CONTAINER_multishortmap_destroy (connections);
382 GNUNET_ATS_connectivity_done (ats_ch);
387 GNUNET_free_non_null (my_private_key);
388 my_private_key = NULL;
393 * We had a remote connection @a value to port @a port before
394 * client @a cls opened port @a port. Bind them now.
396 * @param cls the `struct CadetClient`
397 * @param port the port
398 * @param value the `struct CadetChannel`
399 * @return #GNUNET_YES (iterate over all such channels)
402 bind_loose_channel (void *cls,
403 const struct GNUNET_HashCode *port,
406 struct CadetClient *c = cls;
407 struct CadetChannel *ch = value;
411 GNUNET_assert (GNUNET_YES ==
412 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
420 * Handle port open request. Creates a mapping from the
421 * port to the respective client and checks whether we have
422 * loose channels trying to bind to the port. If so, those
425 * @param cls Identification of the client.
426 * @param pmsg The actual message.
429 handle_port_open (void *cls,
430 const struct GNUNET_CADET_PortMessage *pmsg)
432 struct CadetClient *c = cls;
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 "Open port %s requested by client %s\n",
436 GNUNET_h2s (&pmsg->port),
438 if (NULL == c->ports)
439 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
442 GNUNET_CONTAINER_multihashmap_put (c->ports,
445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
448 GNUNET_SERVICE_client_drop (c->client);
451 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
455 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
459 GNUNET_SERVICE_client_continue (c->client);
464 * Handler for port close requests. Marks this port as closed
465 * (unless of course we have another client with the same port
466 * open). Note that existing channels accepted on the port are
469 * @param cls Identification of the client.
470 * @param pmsg The actual message.
473 handle_port_close (void *cls,
474 const struct GNUNET_CADET_PortMessage *pmsg)
476 struct CadetClient *c = cls;
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Closing port %s as requested by client %s\n",
480 GNUNET_h2s (&pmsg->port),
483 GNUNET_CONTAINER_multihashmap_remove (c->ports,
488 GNUNET_SERVICE_client_drop (c->client);
491 GNUNET_assert (GNUNET_YES ==
492 GNUNET_CONTAINER_multihashmap_remove (open_ports,
495 GNUNET_SERVICE_client_continue (c->client);
500 * Handler for requests for us creating a new channel to another peer and port.
502 * @param cls Identification of the client.
503 * @param tcm The actual message.
506 handle_channel_create (void *cls,
507 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
509 struct CadetClient *c = cls;
510 struct CadetChannel *ch;
512 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
514 /* Channel ID not in allowed range. */
516 GNUNET_SERVICE_client_drop (c->client);
519 ch = lookup_channel (c,
523 /* Channel ID already in use. Not allowed. */
525 GNUNET_SERVICE_client_drop (c->client);
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 "New channel to %s at port %s requested by client %s\n",
530 GNUNET_i2s (&tcm->peer),
531 GNUNET_h2s (&tcm->port),
535 ch = GCCH_channel_local_new (c,
544 GNUNET_SERVICE_client_drop (c->client);
547 GNUNET_assert (GNUNET_YES ==
548 GNUNET_CONTAINER_multihashmap32_put (c->channels,
549 ntohl (tcm->ccn.channel_of_client),
551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
553 GNUNET_SERVICE_client_continue (c->client);
558 * Handler for requests of destroying an existing channel.
560 * @param cls client identification of the client
561 * @param msg the actual message
564 handle_channel_destroy (void *cls,
565 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
567 struct CadetClient *c = cls;
568 struct CadetChannel *ch;
570 ch = lookup_channel (c,
574 /* Client attempted to destroy unknown channel */
576 GNUNET_SERVICE_client_drop (c->client);
579 LOG (GNUNET_ERROR_TYPE_INFO,
580 "Client %s is destroying channel %s\n",
583 GNUNET_assert (GNUNET_YES ==
584 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
585 ntohl (msg->ccn.channel_of_client),
587 GCCH_channel_local_destroy (ch);
588 GNUNET_SERVICE_client_continue (c->client);
593 * Check for client traffic data message is well-formed.
595 * @param cls identification of the client
596 * @param msg the actual message
597 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
600 check_data (void *cls,
601 const struct GNUNET_CADET_LocalData *msg)
603 const struct GNUNET_MessageHeader *payload;
605 size_t payload_claimed_size;
607 /* Sanity check for message size */
608 payload_size = ntohs (msg->header.size) - sizeof (*msg);
609 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
610 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
613 return GNUNET_SYSERR;
615 payload = (struct GNUNET_MessageHeader *) &msg[1];
616 payload_claimed_size = ntohs (payload->size);
617 if (payload_size != payload_claimed_size)
620 return GNUNET_SYSERR;
627 * Handler for client payload traffic to be send on a channel to
630 * @param cls identification of the client
631 * @param msg the actual message
634 handle_data (void *cls,
635 const struct GNUNET_CADET_LocalData *msg)
637 struct CadetClient *c = cls;
638 struct CadetChannel *ch;
639 const struct GNUNET_MessageHeader *payload;
641 ch = lookup_channel (c,
645 /* Channel does not exist! */
647 GNUNET_SERVICE_client_drop (c->client);
651 payload = (const struct GNUNET_MessageHeader *) &msg[1];
652 LOG (GNUNET_ERROR_TYPE_DEBUG,
653 "Received %u bytes payload from client %s for channel %s\n",
654 ntohs (payload->size),
658 GCCH_handle_local_data (ch,
661 GNUNET_SERVICE_client_drop (c->client);
664 GNUNET_SERVICE_client_continue (c->client);
669 * Handler for client's ACKs for payload traffic.
671 * @param cls identification of the client.
672 * @param msg The actual message.
675 handle_ack (void *cls,
676 const struct GNUNET_CADET_LocalAck *msg)
678 struct CadetClient *c = cls;
679 struct CadetChannel *ch;
681 ch = lookup_channel (c,
685 /* Channel does not exist! */
687 GNUNET_SERVICE_client_drop (c->client);
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "Got a local ACK from client %s for channel %s\n",
694 GCCH_handle_local_ack (ch);
695 GNUNET_SERVICE_client_continue (c->client);
700 * Iterator over all peers to send a monitoring client info about each peer.
702 * @param cls Closure ().
703 * @param peer Peer ID (tunnel remote peer).
704 * @param value Peer info.
705 * @return #GNUNET_YES, to keep iterating.
708 get_all_peers_iterator (void *cls,
709 const struct GNUNET_PeerIdentity *peer,
712 struct CadetClient *c = cls;
713 struct CadetPeer *p = value;
714 struct GNUNET_MQ_Envelope *env;
715 struct GNUNET_CADET_LocalInfoPeer *msg;
717 env = GNUNET_MQ_msg (msg,
718 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
719 msg->destination = *peer;
720 msg->paths = htons (GCP_count_paths (p));
721 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
723 GNUNET_MQ_send (c->mq,
730 * Handler for client's INFO PEERS request.
732 * @param cls Identification of the client.
733 * @param message The actual message.
736 handle_get_peers (void *cls,
737 const struct GNUNET_MessageHeader *message)
739 struct CadetClient *c = cls;
740 struct GNUNET_MQ_Envelope *env;
741 struct GNUNET_MessageHeader *reply;
743 GCP_iterate_all (&get_all_peers_iterator,
745 env = GNUNET_MQ_msg (reply,
746 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
747 GNUNET_MQ_send (c->mq,
749 GNUNET_SERVICE_client_continue (c->client);
754 * Iterator over all paths of a peer to build an InfoPeer message.
755 * Message contains blocks of peers, first not included.
757 * @param cls message queue for transmission
758 * @param path Path itself
759 * @param off offset of the peer on @a path
760 * @return #GNUNET_YES if should keep iterating.
761 * #GNUNET_NO otherwise.
764 path_info_iterator (void *cls,
765 struct CadetPeerPath *path,
768 struct GNUNET_MQ_Handle *mq = cls;
769 struct GNUNET_MQ_Envelope *env;
770 struct GNUNET_MessageHeader *resp;
771 struct GNUNET_PeerIdentity *id;
774 unsigned int path_length;
776 path_length = GCPP_get_length (path);
777 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
778 if (sizeof (*resp) + path_size > UINT16_MAX)
780 LOG (GNUNET_ERROR_TYPE_WARNING,
781 "Path of %u entries is too long for info message\n",
785 env = GNUNET_MQ_msg_extra (resp,
787 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
788 id = (struct GNUNET_PeerIdentity *) &resp[1];
790 /* Don't copy first peer. First peer is always the local one. Last
791 * peer is always the destination (leave as 0, EOL).
793 for (i = 0; i < off; i++)
794 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
803 * Handler for client's SHOW_PEER request.
805 * @param cls Identification of the client.
806 * @param msg The actual message.
809 handle_show_peer (void *cls,
810 const struct GNUNET_CADET_LocalInfo *msg)
812 struct CadetClient *c = cls;
814 struct GNUNET_MQ_Envelope *env;
815 struct GNUNET_MessageHeader *resp;
817 p = GCP_get (&msg->peer,
820 GCP_iterate_paths (p,
823 /* Send message with 0/0 to indicate the end */
824 env = GNUNET_MQ_msg (resp,
825 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
826 GNUNET_MQ_send (c->mq,
828 GNUNET_SERVICE_client_continue (c->client);
833 * Iterator over all tunnels to send a monitoring client info about each tunnel.
835 * @param cls Closure ().
836 * @param peer Peer ID (tunnel remote peer).
837 * @param value a `struct CadetPeer`
838 * @return #GNUNET_YES, to keep iterating.
841 get_all_tunnels_iterator (void *cls,
842 const struct GNUNET_PeerIdentity *peer,
845 struct CadetClient *c = cls;
846 struct CadetPeer *p = value;
847 struct GNUNET_MQ_Envelope *env;
848 struct GNUNET_CADET_LocalInfoTunnel *msg;
849 struct CadetTunnel *t;
851 t = GCP_get_tunnel (p,
855 env = GNUNET_MQ_msg (msg,
856 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
857 msg->destination = *peer;
858 msg->channels = htonl (GCT_count_channels (t));
859 msg->connections = htonl (GCT_count_any_connections (t));
860 msg->cstate = htons (0);
861 msg->estate = htons ((uint16_t) GCT_get_estate (t));
862 GNUNET_MQ_send (c->mq,
869 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
871 * @param cls client Identification of the client.
872 * @param message The actual message.
875 handle_info_tunnels (void *cls,
876 const struct GNUNET_MessageHeader *message)
878 struct CadetClient *c = cls;
879 struct GNUNET_MQ_Envelope *env;
880 struct GNUNET_MessageHeader *reply;
882 GCP_iterate_all (&get_all_tunnels_iterator,
884 env = GNUNET_MQ_msg (reply,
885 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
886 GNUNET_MQ_send (c->mq,
888 GNUNET_SERVICE_client_continue (c->client);
893 * Update the message with information about the connection.
895 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
896 * @param c a connection about which we should store information in @a cls
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));
911 * Update the message with information about the channel.
913 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
914 * @param ch a channel about which we should store information in @a cls
917 iter_channel (void *cls,
918 struct CadetChannel *ch)
920 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
921 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
922 struct GNUNET_CADET_ChannelTunnelNumber *chn
923 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
925 chn[msg->channels++] = GCCH_get_id (ch);
930 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
932 * @param cls Identification of the client.
933 * @param msg The actual message.
936 handle_info_tunnel (void *cls,
937 const struct GNUNET_CADET_LocalInfo *msg)
939 struct CadetClient *c = cls;
940 struct GNUNET_MQ_Envelope *env;
941 struct GNUNET_CADET_LocalInfoTunnel *resp;
942 struct CadetTunnel *t;
947 p = GCP_get (&msg->peer,
949 t = GCP_get_tunnel (p,
953 /* We don't know the tunnel */
954 struct GNUNET_MQ_Envelope *env;
955 struct GNUNET_CADET_LocalInfoTunnel *warn;
957 LOG (GNUNET_ERROR_TYPE_INFO,
958 "Tunnel to %s unknown\n",
959 GNUNET_i2s_full (&msg->peer));
960 env = GNUNET_MQ_msg (warn,
961 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
962 warn->destination = msg->peer;
963 GNUNET_MQ_send (c->mq,
965 GNUNET_SERVICE_client_continue (c->client);
969 /* Initialize context */
970 ch_n = GCT_count_channels (t);
971 c_n = GCT_count_any_connections (t);
972 env = GNUNET_MQ_msg_extra (resp,
973 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
974 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
975 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
976 resp->destination = msg->peer;
977 /* Do not reorder! #iter_channel needs counters in HBO! */
978 GCT_iterate_connections (t,
981 GCT_iterate_channels (t,
984 resp->connections = htonl (resp->connections);
985 resp->channels = htonl (resp->channels);
986 resp->cstate = htons (0);
987 resp->estate = htons (GCT_get_estate (t));
988 GNUNET_MQ_send (c->mq,
990 GNUNET_SERVICE_client_continue (c->client);
995 * Iterator over all peers to dump info for each peer.
997 * @param cls Closure (unused).
998 * @param peer Peer ID (tunnel remote peer).
999 * @param value Peer info.
1001 * @return #GNUNET_YES, to keep iterating.
1004 show_peer_iterator (void *cls,
1005 const struct GNUNET_PeerIdentity *peer,
1008 struct CadetPeer *p = value;
1009 struct CadetTunnel *t;
1011 t = GCP_get_tunnel (p,
1015 GNUNET_ERROR_TYPE_ERROR);
1016 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1022 * Handler for client's INFO_DUMP request.
1024 * @param cls Identification of the client.
1025 * @param message The actual message.
1028 handle_info_dump (void *cls,
1029 const struct GNUNET_MessageHeader *message)
1031 struct CadetClient *c = cls;
1033 LOG (GNUNET_ERROR_TYPE_INFO,
1034 "Received dump info request from client %u\n",
1037 LOG (GNUNET_ERROR_TYPE_ERROR,
1038 "*************************** DUMP START ***************************\n");
1039 for (struct CadetClient *ci = clients_head;
1043 LOG (GNUNET_ERROR_TYPE_ERROR,
1044 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1049 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1051 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1053 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1054 GCP_iterate_all (&show_peer_iterator,
1057 LOG (GNUNET_ERROR_TYPE_ERROR,
1058 "**************************** DUMP END ****************************\n");
1060 GNUNET_SERVICE_client_continue (c->client);
1066 * Callback called when a client connects to the service.
1068 * @param cls closure for the service
1069 * @param client the new client that connected to the service
1070 * @param mq the message queue used to send messages to the client
1074 client_connect_cb (void *cls,
1075 struct GNUNET_SERVICE_Client *client,
1076 struct GNUNET_MQ_Handle *mq)
1078 struct CadetClient *c;
1080 c = GNUNET_new (struct CadetClient);
1083 c->id = next_client_id++; /* overflow not important: just for debug */
1085 = GNUNET_CONTAINER_multihashmap32_create (32);
1086 GNUNET_CONTAINER_DLL_insert (clients_head,
1089 GNUNET_STATISTICS_update (stats,
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "Client %s connected\n",
1101 * Iterator for deleting each channel whose client endpoint disconnected.
1103 * @param cls Closure (client that has disconnected).
1104 * @param key The local channel id in host byte order
1105 * @param value The value stored at the key (channel to destroy).
1106 * @return #GNUNET_OK, keep iterating.
1109 channel_destroy_iterator (void *cls,
1113 struct CadetClient *c = cls;
1114 struct CadetChannel *ch = value;
1116 GNUNET_assert (GNUNET_YES ==
1117 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1120 LOG (GNUNET_ERROR_TYPE_DEBUG,
1121 "Destroying channel %s, due to client %s disconnecting.\n",
1124 if (key < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1125 GCCH_channel_local_destroy (ch);
1127 GCCH_channel_incoming_destroy (ch);
1133 * Remove client's ports from the global hashmap on disconnect.
1135 * @param cls Closure (unused).
1136 * @param key the port.
1137 * @param value the `struct CadetClient` to remove
1138 * @return #GNUNET_OK, keep iterating.
1141 client_release_ports (void *cls,
1142 const struct GNUNET_HashCode *key,
1145 struct CadetClient *c = value;
1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "Closing port %s due to client %s disconnect.\n",
1151 GNUNET_assert (GNUNET_YES ==
1152 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1155 GNUNET_assert (GNUNET_YES ==
1156 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1164 * Callback called when a client disconnected from the service
1166 * @param cls closure for the service
1167 * @param client the client that disconnected
1168 * @param internal_cls should be equal to @a c
1171 client_disconnect_cb (void *cls,
1172 struct GNUNET_SERVICE_Client *client,
1175 struct CadetClient *c = internal_cls;
1177 GNUNET_assert (c->client == client);
1178 LOG (GNUNET_ERROR_TYPE_DEBUG,
1179 "Client %s is disconnecting.\n",
1181 if (NULL != c->channels)
1183 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1184 &channel_destroy_iterator,
1186 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1188 if (NULL != c->ports)
1190 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1191 &client_release_ports,
1193 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1195 GNUNET_CONTAINER_DLL_remove (clients_head,
1198 GNUNET_STATISTICS_update (stats,
1207 * Setup CADET internals.
1209 * @param cls closure
1210 * @param server the initialized server
1211 * @param c configuration to use
1215 const struct GNUNET_CONFIGURATION_Handle *c,
1216 struct GNUNET_SERVICE_Handle *service)
1220 GNUNET_CONFIGURATION_get_value_number (c,
1225 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1228 "needs to be a number");
1229 ratchet_messages = 64;
1232 GNUNET_CONFIGURATION_get_value_time (c,
1237 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1240 "need delay value");
1241 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1244 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1245 if (NULL == my_private_key)
1248 GNUNET_SCHEDULER_shutdown ();
1251 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1252 &my_full_id.public_key);
1253 stats = GNUNET_STATISTICS_create ("cadet",
1255 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1257 ats_ch = GNUNET_ATS_connectivity_init (c);
1258 /* FIXME: optimize code to allow GNUNET_YES here! */
1259 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1261 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1263 peers = GNUNET_CONTAINER_multipeermap_create (16,
1265 connections = GNUNET_CONTAINER_multishortmap_create (256,
1270 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1271 "CADET started for peer %s\n",
1272 GNUNET_i2s (&my_full_id));
1278 * Define "main" method using service macro.
1282 GNUNET_SERVICE_OPTION_NONE,
1285 &client_disconnect_cb,
1287 GNUNET_MQ_hd_fixed_size (port_open,
1288 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1289 struct GNUNET_CADET_PortMessage,
1291 GNUNET_MQ_hd_fixed_size (port_close,
1292 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1293 struct GNUNET_CADET_PortMessage,
1295 GNUNET_MQ_hd_fixed_size (channel_create,
1296 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1297 struct GNUNET_CADET_LocalChannelCreateMessage,
1299 GNUNET_MQ_hd_fixed_size (channel_destroy,
1300 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1301 struct GNUNET_CADET_LocalChannelDestroyMessage,
1303 GNUNET_MQ_hd_var_size (data,
1304 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1305 struct GNUNET_CADET_LocalData,
1307 GNUNET_MQ_hd_fixed_size (ack,
1308 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1309 struct GNUNET_CADET_LocalAck,
1311 GNUNET_MQ_hd_fixed_size (get_peers,
1312 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1313 struct GNUNET_MessageHeader,
1315 GNUNET_MQ_hd_fixed_size (show_peer,
1316 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1317 struct GNUNET_CADET_LocalInfo,
1319 GNUNET_MQ_hd_fixed_size (info_tunnels,
1320 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1321 struct GNUNET_MessageHeader,
1323 GNUNET_MQ_hd_fixed_size (info_tunnel,
1324 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1325 struct GNUNET_CADET_LocalInfo,
1327 GNUNET_MQ_hd_fixed_size (info_dump,
1328 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1329 struct GNUNET_MessageHeader,
1331 GNUNET_MQ_handler_end ());
1333 /* end of gnunet-service-cadet-new.c */