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 * Callback invoked on all peers to destroy all tunnels
339 * that may still exist.
342 * @param pid identify of a peer
343 * @param value a `struct CadetPeer` that may still have a tunnel
344 * @return #GNUNET_OK (iterate over all entries)
347 destroy_paths_now (void *cls,
348 const struct GNUNET_PeerIdentity *pid,
351 struct CadetPeer *cp = value;
353 GCP_drop_owned_paths (cp);
359 * Task run during shutdown.
364 shutdown_task (void *cls)
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 shutting_down = GNUNET_YES;
372 GNUNET_STATISTICS_destroy (stats,
376 if (NULL != open_ports)
378 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
381 if (NULL != loose_channels)
383 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
384 loose_channels = NULL;
386 /* Destroy tunnels. Note that all channels must be destroyed first! */
387 GCP_iterate_all (&destroy_tunnels_now,
389 /* All tunnels, channels, connections and CORE must be down before this point. */
390 GCP_iterate_all (&destroy_paths_now,
392 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
393 GCP_destroy_all_peers ();
396 GNUNET_CONTAINER_multipeermap_destroy (peers);
399 if (NULL != connections)
401 GNUNET_CONTAINER_multishortmap_destroy (connections);
406 GNUNET_ATS_connectivity_done (ats_ch);
411 GNUNET_free_non_null (my_private_key);
412 my_private_key = NULL;
417 * We had a remote connection @a value to port @a port before
418 * client @a cls opened port @a port. Bind them now.
420 * @param cls the `struct CadetClient`
421 * @param port the port
422 * @param value the `struct CadetChannel`
423 * @return #GNUNET_YES (iterate over all such channels)
426 bind_loose_channel (void *cls,
427 const struct GNUNET_HashCode *port,
430 struct CadetClient *c = cls;
431 struct CadetChannel *ch = value;
435 GNUNET_assert (GNUNET_YES ==
436 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
444 * Handle port open request. Creates a mapping from the
445 * port to the respective client and checks whether we have
446 * loose channels trying to bind to the port. If so, those
449 * @param cls Identification of the client.
450 * @param pmsg The actual message.
453 handle_port_open (void *cls,
454 const struct GNUNET_CADET_PortMessage *pmsg)
456 struct CadetClient *c = cls;
458 LOG (GNUNET_ERROR_TYPE_DEBUG,
459 "Open port %s requested by client %s\n",
460 GNUNET_h2s (&pmsg->port),
462 if (NULL == c->ports)
463 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
466 GNUNET_CONTAINER_multihashmap_put (c->ports,
469 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
472 GNUNET_SERVICE_client_drop (c->client);
475 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
479 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
483 GNUNET_SERVICE_client_continue (c->client);
488 * Handler for port close requests. Marks this port as closed
489 * (unless of course we have another client with the same port
490 * open). Note that existing channels accepted on the port are
493 * @param cls Identification of the client.
494 * @param pmsg The actual message.
497 handle_port_close (void *cls,
498 const struct GNUNET_CADET_PortMessage *pmsg)
500 struct CadetClient *c = cls;
502 LOG (GNUNET_ERROR_TYPE_DEBUG,
503 "Closing port %s as requested by client %s\n",
504 GNUNET_h2s (&pmsg->port),
507 GNUNET_CONTAINER_multihashmap_remove (c->ports,
512 GNUNET_SERVICE_client_drop (c->client);
515 GNUNET_assert (GNUNET_YES ==
516 GNUNET_CONTAINER_multihashmap_remove (open_ports,
519 GNUNET_SERVICE_client_continue (c->client);
524 * Handler for requests for us creating a new channel to another peer and port.
526 * @param cls Identification of the client.
527 * @param tcm The actual message.
530 handle_channel_create (void *cls,
531 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
533 struct CadetClient *c = cls;
534 struct CadetChannel *ch;
536 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
538 /* Channel ID not in allowed range. */
540 GNUNET_SERVICE_client_drop (c->client);
543 ch = lookup_channel (c,
547 /* Channel ID already in use. Not allowed. */
549 GNUNET_SERVICE_client_drop (c->client);
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "New channel to %s at port %s requested by client %s\n",
554 GNUNET_i2s (&tcm->peer),
555 GNUNET_h2s (&tcm->port),
559 ch = GCCH_channel_local_new (c,
568 GNUNET_SERVICE_client_drop (c->client);
571 GNUNET_assert (GNUNET_YES ==
572 GNUNET_CONTAINER_multihashmap32_put (c->channels,
573 ntohl (tcm->ccn.channel_of_client),
575 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
577 GNUNET_SERVICE_client_continue (c->client);
582 * Handler for requests of destroying an existing channel.
584 * @param cls client identification of the client
585 * @param msg the actual message
588 handle_channel_destroy (void *cls,
589 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
591 struct CadetClient *c = cls;
592 struct CadetChannel *ch;
594 ch = lookup_channel (c,
598 /* Client attempted to destroy unknown channel */
600 GNUNET_SERVICE_client_drop (c->client);
603 LOG (GNUNET_ERROR_TYPE_INFO,
604 "Client %s is destroying channel %s\n",
607 GNUNET_assert (GNUNET_YES ==
608 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
609 ntohl (msg->ccn.channel_of_client),
611 GCCH_channel_local_destroy (ch);
612 GNUNET_SERVICE_client_continue (c->client);
617 * Check for client traffic data message is well-formed.
619 * @param cls identification of the client
620 * @param msg the actual message
621 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
624 check_data (void *cls,
625 const struct GNUNET_CADET_LocalData *msg)
627 const struct GNUNET_MessageHeader *payload;
629 size_t payload_claimed_size;
631 /* Sanity check for message size */
632 payload_size = ntohs (msg->header.size) - sizeof (*msg);
633 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
634 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
637 return GNUNET_SYSERR;
639 payload = (struct GNUNET_MessageHeader *) &msg[1];
640 payload_claimed_size = ntohs (payload->size);
641 if (payload_size != payload_claimed_size)
644 return GNUNET_SYSERR;
651 * Handler for client payload traffic to be send on a channel to
654 * @param cls identification of the client
655 * @param msg the actual message
658 handle_data (void *cls,
659 const struct GNUNET_CADET_LocalData *msg)
661 struct CadetClient *c = cls;
662 struct CadetChannel *ch;
663 const struct GNUNET_MessageHeader *payload;
665 ch = lookup_channel (c,
669 /* Channel does not exist! */
671 GNUNET_SERVICE_client_drop (c->client);
675 payload = (const struct GNUNET_MessageHeader *) &msg[1];
676 LOG (GNUNET_ERROR_TYPE_DEBUG,
677 "Received %u bytes payload from client %s for channel %s\n",
678 ntohs (payload->size),
682 GCCH_handle_local_data (ch,
685 GNUNET_SERVICE_client_drop (c->client);
688 GNUNET_SERVICE_client_continue (c->client);
693 * Handler for client's ACKs for payload traffic.
695 * @param cls identification of the client.
696 * @param msg The actual message.
699 handle_ack (void *cls,
700 const struct GNUNET_CADET_LocalAck *msg)
702 struct CadetClient *c = cls;
703 struct CadetChannel *ch;
705 ch = lookup_channel (c,
709 /* Channel does not exist! */
711 GNUNET_SERVICE_client_drop (c->client);
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Got a local ACK from client %s for channel %s\n",
718 GCCH_handle_local_ack (ch);
719 GNUNET_SERVICE_client_continue (c->client);
724 * Iterator over all peers to send a monitoring client info about each peer.
726 * @param cls Closure ().
727 * @param peer Peer ID (tunnel remote peer).
728 * @param value Peer info.
729 * @return #GNUNET_YES, to keep iterating.
732 get_all_peers_iterator (void *cls,
733 const struct GNUNET_PeerIdentity *peer,
736 struct CadetClient *c = cls;
737 struct CadetPeer *p = value;
738 struct GNUNET_MQ_Envelope *env;
739 struct GNUNET_CADET_LocalInfoPeer *msg;
741 env = GNUNET_MQ_msg (msg,
742 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
743 msg->destination = *peer;
744 msg->paths = htons (GCP_count_paths (p));
745 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
747 GNUNET_MQ_send (c->mq,
754 * Handler for client's INFO PEERS request.
756 * @param cls Identification of the client.
757 * @param message The actual message.
760 handle_get_peers (void *cls,
761 const struct GNUNET_MessageHeader *message)
763 struct CadetClient *c = cls;
764 struct GNUNET_MQ_Envelope *env;
765 struct GNUNET_MessageHeader *reply;
767 GCP_iterate_all (&get_all_peers_iterator,
769 env = GNUNET_MQ_msg (reply,
770 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
771 GNUNET_MQ_send (c->mq,
773 GNUNET_SERVICE_client_continue (c->client);
778 * Iterator over all paths of a peer to build an InfoPeer message.
779 * Message contains blocks of peers, first not included.
781 * @param cls message queue for transmission
782 * @param path Path itself
783 * @param off offset of the peer on @a path
784 * @return #GNUNET_YES if should keep iterating.
785 * #GNUNET_NO otherwise.
788 path_info_iterator (void *cls,
789 struct CadetPeerPath *path,
792 struct GNUNET_MQ_Handle *mq = cls;
793 struct GNUNET_MQ_Envelope *env;
794 struct GNUNET_MessageHeader *resp;
795 struct GNUNET_PeerIdentity *id;
798 unsigned int path_length;
800 path_length = GCPP_get_length (path);
801 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
802 if (sizeof (*resp) + path_size > UINT16_MAX)
804 LOG (GNUNET_ERROR_TYPE_WARNING,
805 "Path of %u entries is too long for info message\n",
809 env = GNUNET_MQ_msg_extra (resp,
811 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
812 id = (struct GNUNET_PeerIdentity *) &resp[1];
814 /* Don't copy first peer. First peer is always the local one. Last
815 * peer is always the destination (leave as 0, EOL).
817 for (i = 0; i < off; i++)
818 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
827 * Handler for client's SHOW_PEER request.
829 * @param cls Identification of the client.
830 * @param msg The actual message.
833 handle_show_peer (void *cls,
834 const struct GNUNET_CADET_LocalInfo *msg)
836 struct CadetClient *c = cls;
838 struct GNUNET_MQ_Envelope *env;
839 struct GNUNET_MessageHeader *resp;
841 p = GCP_get (&msg->peer,
844 GCP_iterate_paths (p,
847 /* Send message with 0/0 to indicate the end */
848 env = GNUNET_MQ_msg (resp,
849 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
850 GNUNET_MQ_send (c->mq,
852 GNUNET_SERVICE_client_continue (c->client);
857 * Iterator over all tunnels to send a monitoring client info about each tunnel.
859 * @param cls Closure ().
860 * @param peer Peer ID (tunnel remote peer).
861 * @param value a `struct CadetPeer`
862 * @return #GNUNET_YES, to keep iterating.
865 get_all_tunnels_iterator (void *cls,
866 const struct GNUNET_PeerIdentity *peer,
869 struct CadetClient *c = cls;
870 struct CadetPeer *p = value;
871 struct GNUNET_MQ_Envelope *env;
872 struct GNUNET_CADET_LocalInfoTunnel *msg;
873 struct CadetTunnel *t;
875 t = GCP_get_tunnel (p,
879 env = GNUNET_MQ_msg (msg,
880 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
881 msg->destination = *peer;
882 msg->channels = htonl (GCT_count_channels (t));
883 msg->connections = htonl (GCT_count_any_connections (t));
884 msg->cstate = htons (0);
885 msg->estate = htons ((uint16_t) GCT_get_estate (t));
886 GNUNET_MQ_send (c->mq,
893 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
895 * @param cls client Identification of the client.
896 * @param message The actual message.
899 handle_info_tunnels (void *cls,
900 const struct GNUNET_MessageHeader *message)
902 struct CadetClient *c = cls;
903 struct GNUNET_MQ_Envelope *env;
904 struct GNUNET_MessageHeader *reply;
906 GCP_iterate_all (&get_all_tunnels_iterator,
908 env = GNUNET_MQ_msg (reply,
909 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
910 GNUNET_MQ_send (c->mq,
912 GNUNET_SERVICE_client_continue (c->client);
917 * Update the message with information about the connection.
919 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
920 * @param c a connection about which we should store information in @a cls
923 iter_connection (void *cls,
924 struct CadetConnection *c)
926 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
927 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
929 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
930 h[msg->connections++] = *(GCC_get_id (c));
935 * Update the message with information about the channel.
937 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
938 * @param ch a channel about which we should store information in @a cls
941 iter_channel (void *cls,
942 struct CadetChannel *ch)
944 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
945 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
946 struct GNUNET_CADET_ChannelTunnelNumber *chn
947 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
949 chn[msg->channels++] = GCCH_get_id (ch);
954 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
956 * @param cls Identification of the client.
957 * @param msg The actual message.
960 handle_info_tunnel (void *cls,
961 const struct GNUNET_CADET_LocalInfo *msg)
963 struct CadetClient *c = cls;
964 struct GNUNET_MQ_Envelope *env;
965 struct GNUNET_CADET_LocalInfoTunnel *resp;
966 struct CadetTunnel *t;
971 p = GCP_get (&msg->peer,
973 t = GCP_get_tunnel (p,
977 /* We don't know the tunnel */
978 struct GNUNET_MQ_Envelope *env;
979 struct GNUNET_CADET_LocalInfoTunnel *warn;
981 LOG (GNUNET_ERROR_TYPE_INFO,
982 "Tunnel to %s unknown\n",
983 GNUNET_i2s_full (&msg->peer));
984 env = GNUNET_MQ_msg (warn,
985 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
986 warn->destination = msg->peer;
987 GNUNET_MQ_send (c->mq,
989 GNUNET_SERVICE_client_continue (c->client);
993 /* Initialize context */
994 ch_n = GCT_count_channels (t);
995 c_n = GCT_count_any_connections (t);
996 env = GNUNET_MQ_msg_extra (resp,
997 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
998 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
999 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1000 resp->destination = msg->peer;
1001 /* Do not reorder! #iter_channel needs counters in HBO! */
1002 GCT_iterate_connections (t,
1005 GCT_iterate_channels (t,
1008 resp->connections = htonl (resp->connections);
1009 resp->channels = htonl (resp->channels);
1010 resp->cstate = htons (0);
1011 resp->estate = htons (GCT_get_estate (t));
1012 GNUNET_MQ_send (c->mq,
1014 GNUNET_SERVICE_client_continue (c->client);
1019 * Iterator over all peers to dump info for each peer.
1021 * @param cls Closure (unused).
1022 * @param peer Peer ID (tunnel remote peer).
1023 * @param value Peer info.
1025 * @return #GNUNET_YES, to keep iterating.
1028 show_peer_iterator (void *cls,
1029 const struct GNUNET_PeerIdentity *peer,
1032 struct CadetPeer *p = value;
1033 struct CadetTunnel *t;
1035 t = GCP_get_tunnel (p,
1039 GNUNET_ERROR_TYPE_ERROR);
1040 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1046 * Handler for client's INFO_DUMP request.
1048 * @param cls Identification of the client.
1049 * @param message The actual message.
1052 handle_info_dump (void *cls,
1053 const struct GNUNET_MessageHeader *message)
1055 struct CadetClient *c = cls;
1057 LOG (GNUNET_ERROR_TYPE_INFO,
1058 "Received dump info request from client %u\n",
1061 LOG (GNUNET_ERROR_TYPE_ERROR,
1062 "*************************** DUMP START ***************************\n");
1063 for (struct CadetClient *ci = clients_head;
1067 LOG (GNUNET_ERROR_TYPE_ERROR,
1068 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1073 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1075 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1077 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1078 GCP_iterate_all (&show_peer_iterator,
1081 LOG (GNUNET_ERROR_TYPE_ERROR,
1082 "**************************** DUMP END ****************************\n");
1084 GNUNET_SERVICE_client_continue (c->client);
1090 * Callback called when a client connects to the service.
1092 * @param cls closure for the service
1093 * @param client the new client that connected to the service
1094 * @param mq the message queue used to send messages to the client
1098 client_connect_cb (void *cls,
1099 struct GNUNET_SERVICE_Client *client,
1100 struct GNUNET_MQ_Handle *mq)
1102 struct CadetClient *c;
1104 c = GNUNET_new (struct CadetClient);
1107 c->id = next_client_id++; /* overflow not important: just for debug */
1109 = GNUNET_CONTAINER_multihashmap32_create (32);
1110 GNUNET_CONTAINER_DLL_insert (clients_head,
1113 GNUNET_STATISTICS_update (stats,
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Client %s connected\n",
1125 * A channel was destroyed by the other peer. Tell our client.
1127 * @param c client that lost a channel
1128 * @param ccn channel identification number for the client
1129 * @param ch the channel object
1132 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1133 struct GNUNET_CADET_ClientChannelNumber ccn,
1134 struct CadetChannel *ch)
1136 struct GNUNET_MQ_Envelope *env;
1137 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1139 env = GNUNET_MQ_msg (tdm,
1140 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1142 GSC_send_to_client (c,
1144 GNUNET_assert (GNUNET_YES ==
1145 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1146 ntohl (ccn.channel_of_client),
1152 * Iterator for deleting each channel whose client endpoint disconnected.
1154 * @param cls Closure (client that has disconnected).
1155 * @param key The local channel id in host byte order
1156 * @param value The value stored at the key (channel to destroy).
1157 * @return #GNUNET_OK, keep iterating.
1160 channel_destroy_iterator (void *cls,
1164 struct CadetClient *c = cls;
1165 struct CadetChannel *ch = value;
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "Destroying channel %s, due to client %s disconnecting.\n",
1171 GNUNET_assert (GNUNET_YES ==
1172 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1175 if (key < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1176 GCCH_channel_local_destroy (ch);
1178 GCCH_channel_incoming_destroy (ch);
1184 * Remove client's ports from the global hashmap on disconnect.
1186 * @param cls Closure (unused).
1187 * @param key the port.
1188 * @param value the `struct CadetClient` to remove
1189 * @return #GNUNET_OK, keep iterating.
1192 client_release_ports (void *cls,
1193 const struct GNUNET_HashCode *key,
1196 struct CadetClient *c = value;
1198 LOG (GNUNET_ERROR_TYPE_DEBUG,
1199 "Closing port %s due to client %s disconnect.\n",
1202 GNUNET_assert (GNUNET_YES ==
1203 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1206 GNUNET_assert (GNUNET_YES ==
1207 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1215 * Callback called when a client disconnected from the service
1217 * @param cls closure for the service
1218 * @param client the client that disconnected
1219 * @param internal_cls should be equal to @a c
1222 client_disconnect_cb (void *cls,
1223 struct GNUNET_SERVICE_Client *client,
1226 struct CadetClient *c = internal_cls;
1228 GNUNET_assert (c->client == client);
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "Client %s is disconnecting.\n",
1232 if (NULL != c->channels)
1234 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1235 &channel_destroy_iterator,
1237 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1239 if (NULL != c->ports)
1241 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1242 &client_release_ports,
1244 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1246 GNUNET_CONTAINER_DLL_remove (clients_head,
1249 GNUNET_STATISTICS_update (stats,
1258 * Setup CADET internals.
1260 * @param cls closure
1261 * @param server the initialized server
1262 * @param c configuration to use
1266 const struct GNUNET_CONFIGURATION_Handle *c,
1267 struct GNUNET_SERVICE_Handle *service)
1271 GNUNET_CONFIGURATION_get_value_number (c,
1276 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1279 "needs to be a number");
1280 ratchet_messages = 64;
1283 GNUNET_CONFIGURATION_get_value_time (c,
1288 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1291 "need delay value");
1292 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1295 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1296 if (NULL == my_private_key)
1299 GNUNET_SCHEDULER_shutdown ();
1302 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1303 &my_full_id.public_key);
1304 stats = GNUNET_STATISTICS_create ("cadet",
1306 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1308 ats_ch = GNUNET_ATS_connectivity_init (c);
1309 /* FIXME: optimize code to allow GNUNET_YES here! */
1310 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1312 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1314 peers = GNUNET_CONTAINER_multipeermap_create (16,
1316 connections = GNUNET_CONTAINER_multishortmap_create (256,
1321 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1322 "CADET started for peer %s\n",
1323 GNUNET_i2s (&my_full_id));
1329 * Define "main" method using service macro.
1333 GNUNET_SERVICE_OPTION_NONE,
1336 &client_disconnect_cb,
1338 GNUNET_MQ_hd_fixed_size (port_open,
1339 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1340 struct GNUNET_CADET_PortMessage,
1342 GNUNET_MQ_hd_fixed_size (port_close,
1343 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1344 struct GNUNET_CADET_PortMessage,
1346 GNUNET_MQ_hd_fixed_size (channel_create,
1347 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1348 struct GNUNET_CADET_LocalChannelCreateMessage,
1350 GNUNET_MQ_hd_fixed_size (channel_destroy,
1351 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1352 struct GNUNET_CADET_LocalChannelDestroyMessage,
1354 GNUNET_MQ_hd_var_size (data,
1355 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1356 struct GNUNET_CADET_LocalData,
1358 GNUNET_MQ_hd_fixed_size (ack,
1359 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1360 struct GNUNET_CADET_LocalAck,
1362 GNUNET_MQ_hd_fixed_size (get_peers,
1363 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1364 struct GNUNET_MessageHeader,
1366 GNUNET_MQ_hd_fixed_size (show_peer,
1367 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1368 struct GNUNET_CADET_LocalInfo,
1370 GNUNET_MQ_hd_fixed_size (info_tunnels,
1371 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1372 struct GNUNET_MessageHeader,
1374 GNUNET_MQ_hd_fixed_size (info_tunnel,
1375 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1376 struct GNUNET_CADET_LocalInfo,
1378 GNUNET_MQ_hd_fixed_size (info_dump,
1379 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1380 struct GNUNET_MessageHeader,
1382 GNUNET_MQ_handler_end ());
1384 /* end of gnunet-service-cadet-new.c */