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 recovery 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 *cm;
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 %s from %s on open port %s (%u), assigning ccn %X\n",
300 ntohl (ccn.channel_of_client));
301 /* notify local client about incoming connection! */
302 env = GNUNET_MQ_msg (cm,
303 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
306 cm->opt = htonl (options);
307 cm->peer = *GCP_get_id (dest);
308 GSC_send_to_client (c,
315 * Callback invoked on all peers to destroy all tunnels
316 * that may still exist.
319 * @param pid identify of a peer
320 * @param value a `struct CadetPeer` that may still have a tunnel
321 * @return #GNUNET_OK (iterate over all entries)
324 destroy_tunnels_now (void *cls,
325 const struct GNUNET_PeerIdentity *pid,
328 struct CadetPeer *cp = value;
329 struct CadetTunnel *t = GCP_get_tunnel (cp,
333 GCT_destroy_tunnel_now (t);
339 * Callback invoked on all peers to destroy all tunnels
340 * that may still exist.
343 * @param pid identify of a peer
344 * @param value a `struct CadetPeer` that may still have a tunnel
345 * @return #GNUNET_OK (iterate over all entries)
348 destroy_paths_now (void *cls,
349 const struct GNUNET_PeerIdentity *pid,
352 struct CadetPeer *cp = value;
354 GCP_drop_owned_paths (cp);
360 * Shutdown everything once the clients have disconnected.
367 GNUNET_STATISTICS_destroy (stats,
371 if (NULL != open_ports)
373 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
376 if (NULL != loose_channels)
378 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
379 loose_channels = NULL;
381 /* Destroy tunnels. Note that all channels must be destroyed first! */
382 GCP_iterate_all (&destroy_tunnels_now,
384 /* All tunnels, channels, connections and CORE must be down before this point. */
385 GCP_iterate_all (&destroy_paths_now,
387 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
388 GCP_destroy_all_peers ();
391 GNUNET_CONTAINER_multipeermap_destroy (peers);
394 if (NULL != connections)
396 GNUNET_CONTAINER_multishortmap_destroy (connections);
401 GNUNET_ATS_connectivity_done (ats_ch);
406 GNUNET_free_non_null (my_private_key);
407 my_private_key = NULL;
412 * Task run during shutdown.
417 shutdown_task (void *cls)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 shutting_down = GNUNET_YES;
423 if (NULL == clients_head)
429 * We had a remote connection @a value to port @a port before
430 * client @a cls opened port @a port. Bind them now.
432 * @param cls the `struct CadetClient`
433 * @param port the port
434 * @param value the `struct CadetChannel`
435 * @return #GNUNET_YES (iterate over all such channels)
438 bind_loose_channel (void *cls,
439 const struct GNUNET_HashCode *port,
442 struct CadetClient *c = cls;
443 struct CadetChannel *ch = value;
447 GNUNET_assert (GNUNET_YES ==
448 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
456 * Handle port open request. Creates a mapping from the
457 * port to the respective client and checks whether we have
458 * loose channels trying to bind to the port. If so, those
461 * @param cls Identification of the client.
462 * @param pmsg The actual message.
465 handle_port_open (void *cls,
466 const struct GNUNET_CADET_PortMessage *pmsg)
468 struct CadetClient *c = cls;
470 LOG (GNUNET_ERROR_TYPE_DEBUG,
471 "Open port %s requested by %s\n",
472 GNUNET_h2s (&pmsg->port),
474 if (NULL == c->ports)
475 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
478 GNUNET_CONTAINER_multihashmap_put (c->ports,
481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
484 GNUNET_SERVICE_client_drop (c->client);
487 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
491 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
495 GNUNET_SERVICE_client_continue (c->client);
500 * Handler for port close requests. Marks this port as closed
501 * (unless of course we have another client with the same port
502 * open). Note that existing channels accepted on the port are
505 * @param cls Identification of the client.
506 * @param pmsg The actual message.
509 handle_port_close (void *cls,
510 const struct GNUNET_CADET_PortMessage *pmsg)
512 struct CadetClient *c = cls;
514 LOG (GNUNET_ERROR_TYPE_DEBUG,
515 "Closing port %s as requested by %s\n",
516 GNUNET_h2s (&pmsg->port),
519 GNUNET_CONTAINER_multihashmap_remove (c->ports,
524 GNUNET_SERVICE_client_drop (c->client);
527 GNUNET_assert (GNUNET_YES ==
528 GNUNET_CONTAINER_multihashmap_remove (open_ports,
531 GNUNET_SERVICE_client_continue (c->client);
536 * Handler for requests for us creating a new channel to another peer and port.
538 * @param cls Identification of the client.
539 * @param tcm The actual message.
542 handle_channel_create (void *cls,
543 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
545 struct CadetClient *c = cls;
546 struct CadetChannel *ch;
548 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
550 /* Channel ID not in allowed range. */
552 GNUNET_SERVICE_client_drop (c->client);
555 ch = lookup_channel (c,
559 /* Channel ID already in use. Not allowed. */
561 GNUNET_SERVICE_client_drop (c->client);
564 LOG (GNUNET_ERROR_TYPE_DEBUG,
565 "New channel to %s at port %s requested by %s\n",
566 GNUNET_i2s (&tcm->peer),
567 GNUNET_h2s (&tcm->port),
571 ch = GCCH_channel_local_new (c,
580 GNUNET_SERVICE_client_drop (c->client);
583 GNUNET_assert (GNUNET_YES ==
584 GNUNET_CONTAINER_multihashmap32_put (c->channels,
585 ntohl (tcm->ccn.channel_of_client),
587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
589 GNUNET_SERVICE_client_continue (c->client);
594 * Handler for requests of destroying an existing channel.
596 * @param cls client identification of the client
597 * @param msg the actual message
600 handle_channel_destroy (void *cls,
601 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
603 struct CadetClient *c = cls;
604 struct CadetChannel *ch;
606 ch = lookup_channel (c,
610 /* Client attempted to destroy unknown channel */
612 GNUNET_SERVICE_client_drop (c->client);
615 LOG (GNUNET_ERROR_TYPE_INFO,
616 "%s is destroying %s\n",
619 GNUNET_assert (GNUNET_YES ==
620 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
621 ntohl (msg->ccn.channel_of_client),
623 GCCH_channel_local_destroy (ch,
626 GNUNET_SERVICE_client_continue (c->client);
631 * Check for client traffic data message is well-formed.
633 * @param cls identification of the client
634 * @param msg the actual message
635 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
638 check_data (void *cls,
639 const struct GNUNET_CADET_LocalData *msg)
642 size_t payload_claimed_size;
644 struct GNUNET_MessageHeader pa;
646 /* FIXME: what is the format we shall allow for @a msg?
647 ONE payload item or multiple? Seems current cadet_api
648 at least in theory allows more than one. Next-gen
649 cadet_api will likely no more, so we could then
650 simplify this mess again. */
651 /* Sanity check for message size */
652 payload_size = ntohs (msg->header.size) - sizeof (*msg);
653 buf = (const char *) &msg[1];
654 while (payload_size >= sizeof (struct GNUNET_MessageHeader))
656 /* need to memcpy() for alignment */
660 payload_claimed_size = ntohs (pa.size);
661 if ( (payload_size < payload_claimed_size) ||
662 (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
663 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Local data of %u total size had sub-message %u at %u with %u bytes\n",
668 ntohs (msg->header.size),
670 (unsigned int) (buf - (const char *) &msg[1]),
671 (unsigned int) payload_claimed_size);
672 return GNUNET_SYSERR;
674 payload_size -= payload_claimed_size;
675 buf += payload_claimed_size;
677 if (0 != payload_size)
680 return GNUNET_SYSERR;
687 * Handler for client payload traffic to be send on a channel to
690 * @param cls identification of the client
691 * @param msg the actual message
694 handle_data (void *cls,
695 const struct GNUNET_CADET_LocalData *msg)
697 struct CadetClient *c = cls;
698 struct CadetChannel *ch;
702 ch = lookup_channel (c,
706 /* Channel does not exist! */
708 GNUNET_SERVICE_client_drop (c->client);
711 payload_size = ntohs (msg->header.size) - sizeof (*msg);
712 buf = (const char *) &msg[1];
713 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "Received %u bytes payload from %s for %s\n",
715 (unsigned int) payload_size,
719 GCCH_handle_local_data (ch,
724 GNUNET_SERVICE_client_drop (c->client);
727 GNUNET_SERVICE_client_continue (c->client);
732 * Handler for client's ACKs for payload traffic.
734 * @param cls identification of the client.
735 * @param msg The actual message.
738 handle_ack (void *cls,
739 const struct GNUNET_CADET_LocalAck *msg)
741 struct CadetClient *c = cls;
742 struct CadetChannel *ch;
744 ch = lookup_channel (c,
748 /* Channel does not exist! */
750 GNUNET_SERVICE_client_drop (c->client);
753 LOG (GNUNET_ERROR_TYPE_DEBUG,
754 "Got a local ACK from %s for %s\n",
757 GCCH_handle_local_ack (ch,
759 GNUNET_SERVICE_client_continue (c->client);
764 * Iterator over all peers to send a monitoring client info about each peer.
766 * @param cls Closure ().
767 * @param peer Peer ID (tunnel remote peer).
768 * @param value Peer info.
769 * @return #GNUNET_YES, to keep iterating.
772 get_all_peers_iterator (void *cls,
773 const struct GNUNET_PeerIdentity *peer,
776 struct CadetClient *c = cls;
777 struct CadetPeer *p = value;
778 struct GNUNET_MQ_Envelope *env;
779 struct GNUNET_CADET_LocalInfoPeer *msg;
781 env = GNUNET_MQ_msg (msg,
782 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
783 msg->destination = *peer;
784 msg->paths = htons (GCP_count_paths (p));
785 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
787 GNUNET_MQ_send (c->mq,
794 * Handler for client's INFO PEERS request.
796 * @param cls Identification of the client.
797 * @param message The actual message.
800 handle_get_peers (void *cls,
801 const struct GNUNET_MessageHeader *message)
803 struct CadetClient *c = cls;
804 struct GNUNET_MQ_Envelope *env;
805 struct GNUNET_MessageHeader *reply;
807 GCP_iterate_all (&get_all_peers_iterator,
809 env = GNUNET_MQ_msg (reply,
810 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
811 GNUNET_MQ_send (c->mq,
813 GNUNET_SERVICE_client_continue (c->client);
818 * Iterator over all paths of a peer to build an InfoPeer message.
819 * Message contains blocks of peers, first not included.
821 * @param cls message queue for transmission
822 * @param path Path itself
823 * @param off offset of the peer on @a path
824 * @return #GNUNET_YES if should keep iterating.
825 * #GNUNET_NO otherwise.
828 path_info_iterator (void *cls,
829 struct CadetPeerPath *path,
832 struct GNUNET_MQ_Handle *mq = cls;
833 struct GNUNET_MQ_Envelope *env;
834 struct GNUNET_MessageHeader *resp;
835 struct GNUNET_PeerIdentity *id;
838 unsigned int path_length;
840 path_length = GCPP_get_length (path);
841 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
842 if (sizeof (*resp) + path_size > UINT16_MAX)
844 LOG (GNUNET_ERROR_TYPE_WARNING,
845 "Path of %u entries is too long for info message\n",
849 env = GNUNET_MQ_msg_extra (resp,
851 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
852 id = (struct GNUNET_PeerIdentity *) &resp[1];
854 /* Don't copy first peer. First peer is always the local one. Last
855 * peer is always the destination (leave as 0, EOL).
857 for (i = 0; i < off; i++)
858 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
867 * Handler for client's SHOW_PEER request.
869 * @param cls Identification of the client.
870 * @param msg The actual message.
873 handle_show_peer (void *cls,
874 const struct GNUNET_CADET_LocalInfo *msg)
876 struct CadetClient *c = cls;
878 struct GNUNET_MQ_Envelope *env;
879 struct GNUNET_MessageHeader *resp;
881 p = GCP_get (&msg->peer,
884 GCP_iterate_paths (p,
887 /* Send message with 0/0 to indicate the end */
888 env = GNUNET_MQ_msg (resp,
889 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
890 GNUNET_MQ_send (c->mq,
892 GNUNET_SERVICE_client_continue (c->client);
897 * Iterator over all tunnels to send a monitoring client info about each tunnel.
899 * @param cls Closure ().
900 * @param peer Peer ID (tunnel remote peer).
901 * @param value a `struct CadetPeer`
902 * @return #GNUNET_YES, to keep iterating.
905 get_all_tunnels_iterator (void *cls,
906 const struct GNUNET_PeerIdentity *peer,
909 struct CadetClient *c = cls;
910 struct CadetPeer *p = value;
911 struct GNUNET_MQ_Envelope *env;
912 struct GNUNET_CADET_LocalInfoTunnel *msg;
913 struct CadetTunnel *t;
915 t = GCP_get_tunnel (p,
919 env = GNUNET_MQ_msg (msg,
920 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
921 msg->destination = *peer;
922 msg->channels = htonl (GCT_count_channels (t));
923 msg->connections = htonl (GCT_count_any_connections (t));
924 msg->cstate = htons (0);
925 msg->estate = htons ((uint16_t) GCT_get_estate (t));
926 GNUNET_MQ_send (c->mq,
933 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
935 * @param cls client Identification of the client.
936 * @param message The actual message.
939 handle_info_tunnels (void *cls,
940 const struct GNUNET_MessageHeader *message)
942 struct CadetClient *c = cls;
943 struct GNUNET_MQ_Envelope *env;
944 struct GNUNET_MessageHeader *reply;
946 GCP_iterate_all (&get_all_tunnels_iterator,
948 env = GNUNET_MQ_msg (reply,
949 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
950 GNUNET_MQ_send (c->mq,
952 GNUNET_SERVICE_client_continue (c->client);
957 * Update the message with information about the connection.
959 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
960 * @param c a connection about which we should store information in @a cls
963 iter_connection (void *cls,
964 struct CadetConnection *c)
966 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
967 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
969 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
970 h[msg->connections++] = *(GCC_get_id (c));
975 * Update the message with information about the channel.
977 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
978 * @param ch a channel about which we should store information in @a cls
981 iter_channel (void *cls,
982 struct CadetChannel *ch)
984 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
985 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
986 struct GNUNET_CADET_ChannelTunnelNumber *chn
987 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
989 chn[msg->channels++] = GCCH_get_id (ch);
994 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
996 * @param cls Identification of the client.
997 * @param msg The actual message.
1000 handle_info_tunnel (void *cls,
1001 const struct GNUNET_CADET_LocalInfo *msg)
1003 struct CadetClient *c = cls;
1004 struct GNUNET_MQ_Envelope *env;
1005 struct GNUNET_CADET_LocalInfoTunnel *resp;
1006 struct CadetTunnel *t;
1007 struct CadetPeer *p;
1011 p = GCP_get (&msg->peer,
1013 t = GCP_get_tunnel (p,
1017 /* We don't know the tunnel */
1018 struct GNUNET_MQ_Envelope *env;
1019 struct GNUNET_CADET_LocalInfoTunnel *warn;
1021 LOG (GNUNET_ERROR_TYPE_INFO,
1022 "Tunnel to %s unknown\n",
1023 GNUNET_i2s_full (&msg->peer));
1024 env = GNUNET_MQ_msg (warn,
1025 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1026 warn->destination = msg->peer;
1027 GNUNET_MQ_send (c->mq,
1029 GNUNET_SERVICE_client_continue (c->client);
1033 /* Initialize context */
1034 ch_n = GCT_count_channels (t);
1035 c_n = GCT_count_any_connections (t);
1036 env = GNUNET_MQ_msg_extra (resp,
1037 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1038 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1039 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1040 resp->destination = msg->peer;
1041 /* Do not reorder! #iter_channel needs counters in HBO! */
1042 GCT_iterate_connections (t,
1045 GCT_iterate_channels (t,
1048 resp->connections = htonl (resp->connections);
1049 resp->channels = htonl (resp->channels);
1050 resp->cstate = htons (0);
1051 resp->estate = htons (GCT_get_estate (t));
1052 GNUNET_MQ_send (c->mq,
1054 GNUNET_SERVICE_client_continue (c->client);
1059 * Iterator over all peers to dump info for each peer.
1061 * @param cls Closure (unused).
1062 * @param peer Peer ID (tunnel remote peer).
1063 * @param value Peer info.
1065 * @return #GNUNET_YES, to keep iterating.
1068 show_peer_iterator (void *cls,
1069 const struct GNUNET_PeerIdentity *peer,
1072 struct CadetPeer *p = value;
1073 struct CadetTunnel *t;
1075 t = GCP_get_tunnel (p,
1079 GNUNET_ERROR_TYPE_ERROR);
1080 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1086 * Handler for client's INFO_DUMP request.
1088 * @param cls Identification of the client.
1089 * @param message The actual message.
1092 handle_info_dump (void *cls,
1093 const struct GNUNET_MessageHeader *message)
1095 struct CadetClient *c = cls;
1097 LOG (GNUNET_ERROR_TYPE_INFO,
1098 "Received dump info request from client %u\n",
1101 LOG (GNUNET_ERROR_TYPE_ERROR,
1102 "*************************** DUMP START ***************************\n");
1103 for (struct CadetClient *ci = clients_head;
1107 LOG (GNUNET_ERROR_TYPE_ERROR,
1108 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1113 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1115 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1117 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1118 GCP_iterate_all (&show_peer_iterator,
1121 LOG (GNUNET_ERROR_TYPE_ERROR,
1122 "**************************** DUMP END ****************************\n");
1124 GNUNET_SERVICE_client_continue (c->client);
1130 * Callback called when a client connects to the service.
1132 * @param cls closure for the service
1133 * @param client the new client that connected to the service
1134 * @param mq the message queue used to send messages to the client
1138 client_connect_cb (void *cls,
1139 struct GNUNET_SERVICE_Client *client,
1140 struct GNUNET_MQ_Handle *mq)
1142 struct CadetClient *c;
1144 c = GNUNET_new (struct CadetClient);
1147 c->id = next_client_id++; /* overflow not important: just for debug */
1149 = GNUNET_CONTAINER_multihashmap32_create (32);
1150 GNUNET_CONTAINER_DLL_insert (clients_head,
1153 GNUNET_STATISTICS_update (stats,
1157 LOG (GNUNET_ERROR_TYPE_DEBUG,
1165 * A channel was destroyed by the other peer. Tell our client.
1167 * @param c client that lost a channel
1168 * @param ccn channel identification number for the client
1169 * @param ch the channel object
1172 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1173 struct GNUNET_CADET_ClientChannelNumber ccn,
1174 struct CadetChannel *ch)
1176 struct GNUNET_MQ_Envelope *env;
1177 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1179 env = GNUNET_MQ_msg (tdm,
1180 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1182 GSC_send_to_client (c,
1184 GNUNET_assert (GNUNET_YES ==
1185 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1186 ntohl (ccn.channel_of_client),
1192 * Iterator for deleting each channel whose client endpoint disconnected.
1194 * @param cls Closure (client that has disconnected).
1195 * @param key The local channel id in host byte order
1196 * @param value The value stored at the key (channel to destroy).
1197 * @return #GNUNET_OK, keep iterating.
1200 channel_destroy_iterator (void *cls,
1204 struct CadetClient *c = cls;
1205 struct GNUNET_CADET_ClientChannelNumber ccn;
1206 struct CadetChannel *ch = value;
1208 LOG (GNUNET_ERROR_TYPE_DEBUG,
1209 "Destroying %s, due to %s disconnecting.\n",
1212 GNUNET_assert (GNUNET_YES ==
1213 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1216 ccn.channel_of_client = htonl (key);
1217 GCCH_channel_local_destroy (ch,
1225 * Remove client's ports from the global hashmap on disconnect.
1227 * @param cls Closure (unused).
1228 * @param key the port.
1229 * @param value the `struct CadetClient` to remove
1230 * @return #GNUNET_OK, keep iterating.
1233 client_release_ports (void *cls,
1234 const struct GNUNET_HashCode *key,
1237 struct CadetClient *c = value;
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "Closing port %s due to %s disconnect.\n",
1243 GNUNET_assert (GNUNET_YES ==
1244 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1247 GNUNET_assert (GNUNET_YES ==
1248 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1256 * Callback called when a client disconnected from the service
1258 * @param cls closure for the service
1259 * @param client the client that disconnected
1260 * @param internal_cls should be equal to @a c
1263 client_disconnect_cb (void *cls,
1264 struct GNUNET_SERVICE_Client *client,
1267 struct CadetClient *c = internal_cls;
1269 GNUNET_assert (c->client == client);
1270 LOG (GNUNET_ERROR_TYPE_DEBUG,
1271 "%s is disconnecting.\n",
1273 if (NULL != c->channels)
1275 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1276 &channel_destroy_iterator,
1278 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1280 if (NULL != c->ports)
1282 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1283 &client_release_ports,
1285 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1287 GNUNET_CONTAINER_DLL_remove (clients_head,
1290 GNUNET_STATISTICS_update (stats,
1295 if ( (NULL == clients_head) &&
1296 (GNUNET_YES == shutting_down) )
1302 * Setup CADET internals.
1304 * @param cls closure
1305 * @param server the initialized server
1306 * @param c configuration to use
1310 const struct GNUNET_CONFIGURATION_Handle *c,
1311 struct GNUNET_SERVICE_Handle *service)
1315 GNUNET_CONFIGURATION_get_value_number (c,
1320 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1323 "needs to be a number");
1324 ratchet_messages = 64;
1327 GNUNET_CONFIGURATION_get_value_time (c,
1332 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1335 "need delay value");
1336 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1339 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1340 if (NULL == my_private_key)
1343 GNUNET_SCHEDULER_shutdown ();
1346 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1347 &my_full_id.public_key);
1348 stats = GNUNET_STATISTICS_create ("cadet",
1350 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1352 ats_ch = GNUNET_ATS_connectivity_init (c);
1353 /* FIXME: optimize code to allow GNUNET_YES here! */
1354 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1356 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1358 peers = GNUNET_CONTAINER_multipeermap_create (16,
1360 connections = GNUNET_CONTAINER_multishortmap_create (256,
1365 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1366 "CADET started for peer %s\n",
1367 GNUNET_i2s (&my_full_id));
1373 * Define "main" method using service macro.
1377 GNUNET_SERVICE_OPTION_NONE,
1380 &client_disconnect_cb,
1382 GNUNET_MQ_hd_fixed_size (port_open,
1383 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1384 struct GNUNET_CADET_PortMessage,
1386 GNUNET_MQ_hd_fixed_size (port_close,
1387 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1388 struct GNUNET_CADET_PortMessage,
1390 GNUNET_MQ_hd_fixed_size (channel_create,
1391 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1392 struct GNUNET_CADET_LocalChannelCreateMessage,
1394 GNUNET_MQ_hd_fixed_size (channel_destroy,
1395 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1396 struct GNUNET_CADET_LocalChannelDestroyMessage,
1398 GNUNET_MQ_hd_var_size (data,
1399 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1400 struct GNUNET_CADET_LocalData,
1402 GNUNET_MQ_hd_fixed_size (ack,
1403 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1404 struct GNUNET_CADET_LocalAck,
1406 GNUNET_MQ_hd_fixed_size (get_peers,
1407 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1408 struct GNUNET_MessageHeader,
1410 GNUNET_MQ_hd_fixed_size (show_peer,
1411 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1412 struct GNUNET_CADET_LocalInfo,
1414 GNUNET_MQ_hd_fixed_size (info_tunnels,
1415 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1416 struct GNUNET_MessageHeader,
1418 GNUNET_MQ_hd_fixed_size (info_tunnel,
1419 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1420 struct GNUNET_CADET_LocalInfo,
1422 GNUNET_MQ_hd_fixed_size (info_dump,
1423 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1424 struct GNUNET_MessageHeader,
1426 GNUNET_MQ_handler_end ());
1428 /* end of gnunet-service-cadet-new.c */