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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file cadet/gnunet-service-cadet.c
18 * @brief GNUnet CADET service with encryption
19 * @author Bartlomiej Polot
20 * @author Christian Grothoff
23 * - peer: other cadet instance. If there is direct connection it's a neighbor.
24 * - path: series of directly connected peer from one peer to another.
25 * - connection: path which is being used in a tunnel.
26 * - tunnel: encrypted connection to a peer, neighbor or not.
27 * - channel: logical link between two clients, on the same or different peers.
28 * have properties like reliability.
32 #include "gnunet_util_lib.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet-service-cadet.h"
36 #include "gnunet-service-cadet_channel.h"
37 #include "gnunet-service-cadet_connection.h"
38 #include "gnunet-service-cadet_core.h"
39 #include "gnunet-service-cadet_dht.h"
40 #include "gnunet-service-cadet_hello.h"
41 #include "gnunet-service-cadet_tunnels.h"
42 #include "gnunet-service-cadet_peer.h"
43 #include "gnunet-service-cadet_paths.h"
45 #define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
49 * Struct containing information about a client of the service
56 struct CadetClient *next;
61 struct CadetClient *prev;
64 * Tunnels that belong to this client, indexed by local id,
65 * value is a `struct CadetChannel`.
67 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
70 * Handle to communicate with the client
72 struct GNUNET_MQ_Handle *mq;
77 struct GNUNET_SERVICE_Client *client;
80 * Ports that this client has declared interest in.
81 * Indexed by port, contains `struct OpenPort`
83 struct GNUNET_CONTAINER_MultiHashMap *ports;
86 * Channel ID to use for the next incoming channel for this client.
87 * Wraps around (in theory).
89 struct GNUNET_CADET_ClientChannelNumber next_ccn;
92 * ID of the client, mainly for debug messages. Purely internal to this file.
98 /******************************************************************************/
99 /*********************** GLOBAL VARIABLES ****************************/
100 /******************************************************************************/
102 /****************************** Global variables ******************************/
105 * Handle to our configuration.
107 const struct GNUNET_CONFIGURATION_Handle *cfg;
110 * Handle to the statistics service.
112 struct GNUNET_STATISTICS_Handle *stats;
115 * Handle to communicate with ATS.
117 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
122 struct GNUNET_PeerIdentity my_full_id;
127 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
130 * Signal that shutdown is happening: prevent recovery measures.
135 * DLL with all the clients, head.
137 static struct CadetClient *clients_head;
140 * DLL with all the clients, tail.
142 static struct CadetClient *clients_tail;
145 * Next ID to assign to a client.
147 static unsigned int next_client_id;
150 * All ports clients of this peer have opened. Maps from
151 * a hashed port to a `struct OpenPort`.
153 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
156 * Map from ports to channels where the ports were closed at the
157 * time we got the inbound connection.
158 * Indexed by h_port, contains `struct CadetChannel`.
160 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
163 * Map from PIDs to `struct CadetPeer` entries.
165 struct GNUNET_CONTAINER_MultiPeerMap *peers;
168 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
169 * hash codes to `struct CadetConnection` objects.
171 struct GNUNET_CONTAINER_MultiShortmap *connections;
174 * How many messages are needed to trigger an AXOLOTL ratchet advance.
176 unsigned long long ratchet_messages;
179 * How long until we trigger a ratched advance due to time.
181 struct GNUNET_TIME_Relative ratchet_time;
184 * How frequently do we send KEEPALIVE messages on idle connections?
186 struct GNUNET_TIME_Relative keepalive_period;
189 * Set to non-zero values to create random drops to test retransmissions.
191 unsigned long long drop_percent;
195 * Send a message to a client.
197 * @param c client to get the message
198 * @param env envelope with the message
201 GSC_send_to_client (struct CadetClient *c,
202 struct GNUNET_MQ_Envelope *env)
204 GNUNET_MQ_send (c->mq,
210 * Return identifier for a client as a string.
212 * @param c client to identify
213 * @return string for debugging
216 GSC_2s (struct CadetClient *c)
220 GNUNET_snprintf (buf,
229 * Lookup channel of client @a c by @a ccn.
231 * @param c client to look in
232 * @param ccn channel ID to look up
233 * @return NULL if no such channel exists
235 static struct CadetChannel *
236 lookup_channel (struct CadetClient *c,
237 struct GNUNET_CADET_ClientChannelNumber ccn)
239 return GNUNET_CONTAINER_multihashmap32_get (c->channels,
240 ntohl (ccn.channel_of_client));
245 * Obtain the next LID to use for incoming connections to
248 * @param c client handle
250 static struct GNUNET_CADET_ClientChannelNumber
251 client_get_next_ccn (struct CadetClient *c)
253 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
255 /* increment until we have a free one... */
260 ccn.channel_of_client
261 = htonl (1 + (ntohl (ccn.channel_of_client)));
262 if (ntohl (ccn.channel_of_client) >=
263 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
264 ccn.channel_of_client = htonl (0);
266 c->next_ccn.channel_of_client
267 = htonl (1 + (ntohl (ccn.channel_of_client)));
273 * Bind incoming channel to this client, and notify client about
274 * incoming connection. Caller is responsible for notifying the other
275 * peer about our acceptance of the channel.
277 * @param c client to bind to
278 * @param ch channel to be bound
279 * @param dest peer that establishes the connection
280 * @param port port number
281 * @param options options
282 * @return local channel number assigned to the new client
284 struct GNUNET_CADET_ClientChannelNumber
285 GSC_bind (struct CadetClient *c,
286 struct CadetChannel *ch,
287 struct CadetPeer *dest,
288 const struct GNUNET_HashCode *port,
291 struct GNUNET_MQ_Envelope *env;
292 struct GNUNET_CADET_LocalChannelCreateMessage *cm;
293 struct GNUNET_CADET_ClientChannelNumber ccn;
295 ccn = client_get_next_ccn (c);
296 GNUNET_assert (GNUNET_YES ==
297 GNUNET_CONTAINER_multihashmap32_put (c->channels,
298 ntohl (ccn.channel_of_client),
300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
301 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n",
306 (uint32_t) ntohl (options),
307 (uint32_t) ntohl (ccn.channel_of_client));
308 /* notify local client about incoming connection! */
309 env = GNUNET_MQ_msg (cm,
310 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
313 cm->opt = htonl (options);
314 cm->peer = *GCP_get_id (dest);
315 GSC_send_to_client (c,
322 * Callback invoked on all peers to destroy all tunnels
323 * that may still exist.
326 * @param pid identify of a peer
327 * @param value a `struct CadetPeer` that may still have a tunnel
328 * @return #GNUNET_OK (iterate over all entries)
331 destroy_tunnels_now (void *cls,
332 const struct GNUNET_PeerIdentity *pid,
335 struct CadetPeer *cp = value;
336 struct CadetTunnel *t = GCP_get_tunnel (cp,
340 GCT_destroy_tunnel_now (t);
346 * Callback invoked on all peers to destroy all tunnels
347 * that may still exist.
350 * @param pid identify of a peer
351 * @param value a `struct CadetPeer` that may still have a tunnel
352 * @return #GNUNET_OK (iterate over all entries)
355 destroy_paths_now (void *cls,
356 const struct GNUNET_PeerIdentity *pid,
359 struct CadetPeer *cp = value;
361 GCP_drop_owned_paths (cp);
367 * Shutdown everything once the clients have disconnected.
374 GNUNET_STATISTICS_destroy (stats,
378 if (NULL != open_ports)
380 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
383 if (NULL != loose_channels)
385 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
386 loose_channels = NULL;
388 /* Destroy tunnels. Note that all channels must be destroyed first! */
389 GCP_iterate_all (&destroy_tunnels_now,
391 /* All tunnels, channels, connections and CORE must be down before this point. */
392 GCP_iterate_all (&destroy_paths_now,
394 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
395 GCP_destroy_all_peers ();
398 GNUNET_CONTAINER_multipeermap_destroy (peers);
401 if (NULL != connections)
403 GNUNET_CONTAINER_multishortmap_destroy (connections);
408 GNUNET_ATS_connectivity_done (ats_ch);
413 GNUNET_free_non_null (my_private_key);
414 my_private_key = NULL;
419 * Task run during shutdown.
424 shutdown_task (void *cls)
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 shutting_down = GNUNET_YES;
430 if (NULL == clients_head)
436 * We had a remote connection @a value to port @a h_port before
437 * client @a cls opened port @a port. Bind them now.
439 * @param cls the `struct CadetClient`
440 * @param h_port the hashed port
441 * @param value the `struct CadetChannel`
442 * @return #GNUNET_YES (iterate over all such channels)
445 bind_loose_channel (void *cls,
446 const struct GNUNET_HashCode *port,
449 struct OpenPort *op = cls;
450 struct CadetChannel *ch = value;
455 GNUNET_assert (GNUNET_YES ==
456 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
464 * Handle port open request. Creates a mapping from the
465 * port to the respective client and checks whether we have
466 * loose channels trying to bind to the port. If so, those
469 * @param cls Identification of the client.
470 * @param pmsg The actual message.
473 handle_port_open (void *cls,
474 const struct GNUNET_CADET_PortMessage *pmsg)
476 struct CadetClient *c = cls;
479 LOG (GNUNET_ERROR_TYPE_DEBUG,
480 "Open port %s requested by %s\n",
481 GNUNET_h2s (&pmsg->port),
483 if (NULL == c->ports)
484 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
486 op = GNUNET_new (struct OpenPort);
488 op->port = pmsg->port;
489 GCCH_hash_port (&op->h_port,
493 GNUNET_CONTAINER_multihashmap_put (c->ports,
496 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
499 GNUNET_SERVICE_client_drop (c->client);
502 (void) GNUNET_CONTAINER_multihashmap_put (open_ports,
505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
506 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
510 GNUNET_SERVICE_client_continue (c->client);
515 * Handler for port close requests. Marks this port as closed
516 * (unless of course we have another client with the same port
517 * open). Note that existing channels accepted on the port are
520 * @param cls Identification of the client.
521 * @param pmsg The actual message.
524 handle_port_close (void *cls,
525 const struct GNUNET_CADET_PortMessage *pmsg)
527 struct CadetClient *c = cls;
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Closing port %s as requested by %s\n",
532 GNUNET_h2s (&pmsg->port),
534 if (NULL == c->ports)
536 /* Client closed a port despite _never_ having opened one? */
538 GNUNET_SERVICE_client_drop (c->client);
541 op = GNUNET_CONTAINER_multihashmap_get (c->ports,
546 GNUNET_SERVICE_client_drop (c->client);
549 GNUNET_assert (GNUNET_YES ==
550 GNUNET_CONTAINER_multihashmap_remove (c->ports,
553 GNUNET_assert (GNUNET_YES ==
554 GNUNET_CONTAINER_multihashmap_remove (open_ports,
558 GNUNET_SERVICE_client_continue (c->client);
563 * Handler for requests for us creating a new channel to another peer and port.
565 * @param cls Identification of the client.
566 * @param tcm The actual message.
569 handle_channel_create (void *cls,
570 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
572 struct CadetClient *c = cls;
573 struct CadetChannel *ch;
575 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
577 /* Channel ID not in allowed range. */
579 GNUNET_SERVICE_client_drop (c->client);
582 ch = lookup_channel (c,
586 /* Channel ID already in use. Not allowed. */
588 GNUNET_SERVICE_client_drop (c->client);
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "New channel to %s at port %s requested by %s\n",
593 GNUNET_i2s (&tcm->peer),
594 GNUNET_h2s (&tcm->port),
598 ch = GCCH_channel_local_new (c,
607 GNUNET_SERVICE_client_drop (c->client);
610 GNUNET_assert (GNUNET_YES ==
611 GNUNET_CONTAINER_multihashmap32_put (c->channels,
612 ntohl (tcm->ccn.channel_of_client),
614 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
616 GNUNET_SERVICE_client_continue (c->client);
621 * Handler for requests of destroying an existing channel.
623 * @param cls client identification of the client
624 * @param msg the actual message
627 handle_channel_destroy (void *cls,
628 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
630 struct CadetClient *c = cls;
631 struct CadetChannel *ch;
633 ch = lookup_channel (c,
637 /* Client attempted to destroy unknown channel.
638 Can happen if the other side went down at the same time.*/
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "%s tried to destroy unknown channel %X\n",
642 (uint32_t) ntohl (msg->ccn.channel_of_client));
643 GNUNET_SERVICE_client_continue (c->client);
646 LOG (GNUNET_ERROR_TYPE_DEBUG,
647 "%s is destroying %s\n",
650 GNUNET_assert (GNUNET_YES ==
651 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
652 ntohl (msg->ccn.channel_of_client),
654 GCCH_channel_local_destroy (ch,
657 GNUNET_SERVICE_client_continue (c->client);
662 * Check for client traffic data message is well-formed.
664 * @param cls identification of the client
665 * @param msg the actual message
666 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
669 check_local_data (void *cls,
670 const struct GNUNET_CADET_LocalData *msg)
673 size_t payload_claimed_size;
675 struct GNUNET_MessageHeader pa;
677 /* FIXME: what is the format we shall allow for @a msg?
678 ONE payload item or multiple? Seems current cadet_api
679 at least in theory allows more than one. Next-gen
680 cadet_api will likely no more, so we could then
681 simplify this mess again. */
682 /* Sanity check for message size */
683 payload_size = ntohs (msg->header.size) - sizeof (*msg);
684 buf = (const char *) &msg[1];
685 while (payload_size >= sizeof (struct GNUNET_MessageHeader))
687 /* need to memcpy() for alignment */
691 payload_claimed_size = ntohs (pa.size);
692 if ( (payload_size < payload_claimed_size) ||
693 (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
694 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Local data of %u total size had sub-message %u at %u with %u bytes\n",
699 ntohs (msg->header.size),
701 (unsigned int) (buf - (const char *) &msg[1]),
702 (unsigned int) payload_claimed_size);
703 return GNUNET_SYSERR;
705 payload_size -= payload_claimed_size;
706 buf += payload_claimed_size;
708 if (0 != payload_size)
711 return GNUNET_SYSERR;
718 * Handler for client payload traffic to be send on a channel to
721 * @param cls identification of the client
722 * @param msg the actual message
725 handle_local_data (void *cls,
726 const struct GNUNET_CADET_LocalData *msg)
728 struct CadetClient *c = cls;
729 struct CadetChannel *ch;
733 ch = lookup_channel (c,
737 /* Channel does not exist (anymore) */
738 LOG (GNUNET_ERROR_TYPE_WARNING,
739 "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
740 (unsigned int) ntohl (msg->ccn.channel_of_client));
741 GNUNET_SERVICE_client_continue (c->client);
744 payload_size = ntohs (msg->header.size) - sizeof (*msg);
745 GNUNET_STATISTICS_update (stats,
746 "# payload received from clients",
749 buf = (const char *) &msg[1];
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Received %u bytes payload from %s for %s\n",
752 (unsigned int) payload_size,
756 GCCH_handle_local_data (ch,
762 GNUNET_SERVICE_client_drop (c->client);
765 GNUNET_SERVICE_client_continue (c->client);
770 * Handler for client's ACKs for payload traffic.
772 * @param cls identification of the client.
773 * @param msg The actual message.
776 handle_local_ack (void *cls,
777 const struct GNUNET_CADET_LocalAck *msg)
779 struct CadetClient *c = cls;
780 struct CadetChannel *ch;
782 ch = lookup_channel (c,
786 /* Channel does not exist (anymore) */
787 LOG (GNUNET_ERROR_TYPE_WARNING,
788 "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
789 (unsigned int) ntohl (msg->ccn.channel_of_client));
790 GNUNET_SERVICE_client_continue (c->client);
793 LOG (GNUNET_ERROR_TYPE_DEBUG,
794 "Got a local ACK from %s for %s\n",
797 GCCH_handle_local_ack (ch,
799 GNUNET_SERVICE_client_continue (c->client);
804 * Iterator over all peers to send a monitoring client info about each peer.
806 * @param cls Closure ().
807 * @param peer Peer ID (tunnel remote peer).
808 * @param value Peer info.
809 * @return #GNUNET_YES, to keep iterating.
812 get_all_peers_iterator (void *cls,
813 const struct GNUNET_PeerIdentity *peer,
816 struct CadetClient *c = cls;
817 struct CadetPeer *p = value;
818 struct GNUNET_MQ_Envelope *env;
819 struct GNUNET_CADET_LocalInfoPeer *msg;
821 env = GNUNET_MQ_msg (msg,
822 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
823 msg->destination = *peer;
824 msg->paths = htons (GCP_count_paths (p));
825 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
827 GNUNET_MQ_send (c->mq,
834 * Handler for client's INFO PEERS request.
836 * @param cls Identification of the client.
837 * @param message The actual message.
840 handle_get_peers (void *cls,
841 const struct GNUNET_MessageHeader *message)
843 struct CadetClient *c = cls;
844 struct GNUNET_MQ_Envelope *env;
845 struct GNUNET_MessageHeader *reply;
847 GCP_iterate_all (&get_all_peers_iterator,
849 env = GNUNET_MQ_msg (reply,
850 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
851 GNUNET_MQ_send (c->mq,
853 GNUNET_SERVICE_client_continue (c->client);
858 * Iterator over all paths of a peer to build an InfoPeer message.
859 * Message contains blocks of peers, first not included.
861 * @param cls message queue for transmission
862 * @param path Path itself
863 * @param off offset of the peer on @a path
864 * @return #GNUNET_YES if should keep iterating.
865 * #GNUNET_NO otherwise.
868 path_info_iterator (void *cls,
869 struct CadetPeerPath *path,
872 struct GNUNET_MQ_Handle *mq = cls;
873 struct GNUNET_MQ_Envelope *env;
874 struct GNUNET_MessageHeader *resp;
875 struct GNUNET_PeerIdentity *id;
878 unsigned int path_length;
880 path_length = GCPP_get_length (path);
881 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
882 if (sizeof (*resp) + path_size > UINT16_MAX)
884 LOG (GNUNET_ERROR_TYPE_WARNING,
885 "Path of %u entries is too long for info message\n",
889 env = GNUNET_MQ_msg_extra (resp,
891 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
892 id = (struct GNUNET_PeerIdentity *) &resp[1];
894 /* Don't copy first peer. First peer is always the local one. Last
895 * peer is always the destination (leave as 0, EOL).
897 for (i = 0; i < off; i++)
898 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
907 * Handler for client's SHOW_PEER request.
909 * @param cls Identification of the client.
910 * @param msg The actual message.
913 handle_show_peer (void *cls,
914 const struct GNUNET_CADET_LocalInfo *msg)
916 struct CadetClient *c = cls;
918 struct GNUNET_MQ_Envelope *env;
919 struct GNUNET_MessageHeader *resp;
921 p = GCP_get (&msg->peer,
924 GCP_iterate_paths (p,
927 /* Send message with 0/0 to indicate the end */
928 env = GNUNET_MQ_msg (resp,
929 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
930 GNUNET_MQ_send (c->mq,
932 GNUNET_SERVICE_client_continue (c->client);
937 * Iterator over all tunnels to send a monitoring client info about each tunnel.
939 * @param cls Closure ().
940 * @param peer Peer ID (tunnel remote peer).
941 * @param value a `struct CadetPeer`
942 * @return #GNUNET_YES, to keep iterating.
945 get_all_tunnels_iterator (void *cls,
946 const struct GNUNET_PeerIdentity *peer,
949 struct CadetClient *c = cls;
950 struct CadetPeer *p = value;
951 struct GNUNET_MQ_Envelope *env;
952 struct GNUNET_CADET_LocalInfoTunnel *msg;
953 struct CadetTunnel *t;
955 t = GCP_get_tunnel (p,
959 env = GNUNET_MQ_msg (msg,
960 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
961 msg->destination = *peer;
962 msg->channels = htonl (GCT_count_channels (t));
963 msg->connections = htonl (GCT_count_any_connections (t));
964 msg->cstate = htons (0);
965 msg->estate = htons ((uint16_t) GCT_get_estate (t));
966 GNUNET_MQ_send (c->mq,
973 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request.
975 * @param cls client Identification of the client.
976 * @param message The actual message.
979 handle_info_tunnels (void *cls,
980 const struct GNUNET_MessageHeader *message)
982 struct CadetClient *c = cls;
983 struct GNUNET_MQ_Envelope *env;
984 struct GNUNET_MessageHeader *reply;
986 GCP_iterate_all (&get_all_tunnels_iterator,
988 env = GNUNET_MQ_msg (reply,
989 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
990 GNUNET_MQ_send (c->mq,
992 GNUNET_SERVICE_client_continue (c->client);
997 * Update the message with information about the connection.
999 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1000 * @param ct a connection about which we should store information in @a cls
1003 iter_connection (void *cls,
1004 struct CadetTConnection *ct)
1006 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1007 struct CadetConnection *cc = ct->cc;
1008 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
1010 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1011 h[msg->connections++] = *(GCC_get_id (cc));
1016 * Update the message with information about the channel.
1018 * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update
1019 * @param ch a channel about which we should store information in @a cls
1022 iter_channel (void *cls,
1023 struct CadetChannel *ch)
1025 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
1026 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1027 struct GNUNET_CADET_ChannelTunnelNumber *chn
1028 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
1030 chn[msg->channels++] = GCCH_get_id (ch);
1035 * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request.
1037 * @param cls Identification of the client.
1038 * @param msg The actual message.
1041 handle_info_tunnel (void *cls,
1042 const struct GNUNET_CADET_LocalInfo *msg)
1044 struct CadetClient *c = cls;
1045 struct GNUNET_MQ_Envelope *env;
1046 struct GNUNET_CADET_LocalInfoTunnel *resp;
1047 struct CadetTunnel *t;
1048 struct CadetPeer *p;
1052 p = GCP_get (&msg->peer,
1054 t = GCP_get_tunnel (p,
1058 /* We don't know the tunnel */
1059 struct GNUNET_MQ_Envelope *env;
1060 struct GNUNET_CADET_LocalInfoTunnel *warn;
1062 LOG (GNUNET_ERROR_TYPE_INFO,
1063 "Tunnel to %s unknown\n",
1064 GNUNET_i2s_full (&msg->peer));
1065 env = GNUNET_MQ_msg (warn,
1066 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1067 warn->destination = msg->peer;
1068 GNUNET_MQ_send (c->mq,
1070 GNUNET_SERVICE_client_continue (c->client);
1074 /* Initialize context */
1075 ch_n = GCT_count_channels (t);
1076 c_n = GCT_count_any_connections (t);
1077 env = GNUNET_MQ_msg_extra (resp,
1078 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
1079 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
1080 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1081 resp->destination = msg->peer;
1082 /* Do not reorder! #iter_channel needs counters in HBO! */
1083 GCT_iterate_connections (t,
1086 GCT_iterate_channels (t,
1089 resp->connections = htonl (resp->connections);
1090 resp->channels = htonl (resp->channels);
1091 resp->cstate = htons (0);
1092 resp->estate = htons (GCT_get_estate (t));
1093 GNUNET_MQ_send (c->mq,
1095 GNUNET_SERVICE_client_continue (c->client);
1100 * Iterator over all peers to dump info for each peer.
1102 * @param cls Closure (unused).
1103 * @param peer Peer ID (tunnel remote peer).
1104 * @param value Peer info.
1106 * @return #GNUNET_YES, to keep iterating.
1109 show_peer_iterator (void *cls,
1110 const struct GNUNET_PeerIdentity *peer,
1113 struct CadetPeer *p = value;
1114 struct CadetTunnel *t;
1116 t = GCP_get_tunnel (p,
1120 GNUNET_ERROR_TYPE_ERROR);
1121 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1127 * Handler for client's INFO_DUMP request.
1129 * @param cls Identification of the client.
1130 * @param message The actual message.
1133 handle_info_dump (void *cls,
1134 const struct GNUNET_MessageHeader *message)
1136 struct CadetClient *c = cls;
1138 LOG (GNUNET_ERROR_TYPE_INFO,
1139 "Received dump info request from client %u\n",
1142 LOG (GNUNET_ERROR_TYPE_ERROR,
1143 "*************************** DUMP START ***************************\n");
1144 for (struct CadetClient *ci = clients_head;
1148 LOG (GNUNET_ERROR_TYPE_ERROR,
1149 "Client %u (%p), handle: %p, ports: %u, channels: %u\n",
1154 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1156 GNUNET_CONTAINER_multihashmap32_size (ci->channels));
1158 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1159 GCP_iterate_all (&show_peer_iterator,
1162 LOG (GNUNET_ERROR_TYPE_ERROR,
1163 "**************************** DUMP END ****************************\n");
1165 GNUNET_SERVICE_client_continue (c->client);
1171 * Callback called when a client connects to the service.
1173 * @param cls closure for the service
1174 * @param client the new client that connected to the service
1175 * @param mq the message queue used to send messages to the client
1179 client_connect_cb (void *cls,
1180 struct GNUNET_SERVICE_Client *client,
1181 struct GNUNET_MQ_Handle *mq)
1183 struct CadetClient *c;
1185 c = GNUNET_new (struct CadetClient);
1188 c->id = next_client_id++; /* overflow not important: just for debug */
1190 = GNUNET_CONTAINER_multihashmap32_create (32);
1191 GNUNET_CONTAINER_DLL_insert (clients_head,
1194 GNUNET_STATISTICS_update (stats,
1198 LOG (GNUNET_ERROR_TYPE_DEBUG,
1206 * A channel was destroyed by the other peer. Tell our client.
1208 * @param c client that lost a channel
1209 * @param ccn channel identification number for the client
1210 * @param ch the channel object
1213 GSC_handle_remote_channel_destroy (struct CadetClient *c,
1214 struct GNUNET_CADET_ClientChannelNumber ccn,
1215 struct CadetChannel *ch)
1217 struct GNUNET_MQ_Envelope *env;
1218 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1220 env = GNUNET_MQ_msg (tdm,
1221 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1223 GSC_send_to_client (c,
1225 GNUNET_assert (GNUNET_YES ==
1226 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1227 ntohl (ccn.channel_of_client),
1233 * A client that created a loose channel that was not bound to a port
1234 * disconnected, drop it from the #loose_channels list.
1236 * @param h_port the hashed port the channel was trying to bind to
1237 * @param ch the channel that was lost
1240 GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
1241 struct CadetChannel *ch)
1243 GNUNET_assert (GNUNET_YES ==
1244 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
1251 * Iterator for deleting each channel whose client endpoint disconnected.
1253 * @param cls Closure (client that has disconnected).
1254 * @param key The local channel id in host byte order
1255 * @param value The value stored at the key (channel to destroy).
1256 * @return #GNUNET_OK, keep iterating.
1259 channel_destroy_iterator (void *cls,
1263 struct CadetClient *c = cls;
1264 struct GNUNET_CADET_ClientChannelNumber ccn;
1265 struct CadetChannel *ch = value;
1267 LOG (GNUNET_ERROR_TYPE_DEBUG,
1268 "Destroying %s, due to %s disconnecting.\n",
1271 ccn.channel_of_client = htonl (key);
1272 GCCH_channel_local_destroy (ch,
1275 GNUNET_assert (GNUNET_YES ==
1276 GNUNET_CONTAINER_multihashmap32_remove (c->channels,
1284 * Remove client's ports from the global hashmap on disconnect.
1286 * @param cls the `struct CadetClient`
1287 * @param port the port.
1288 * @param value the `struct OpenPort` to remove
1289 * @return #GNUNET_OK, keep iterating.
1292 client_release_ports (void *cls,
1293 const struct GNUNET_HashCode *port,
1296 struct CadetClient *c = cls;
1297 struct OpenPort *op = value;
1299 GNUNET_assert (c == op->c);
1300 LOG (GNUNET_ERROR_TYPE_DEBUG,
1301 "Closing port %s due to %s disconnect.\n",
1304 GNUNET_assert (GNUNET_YES ==
1305 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1308 GNUNET_assert (GNUNET_YES ==
1309 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1318 * Callback called when a client disconnected from the service
1320 * @param cls closure for the service
1321 * @param client the client that disconnected
1322 * @param internal_cls should be equal to @a c
1325 client_disconnect_cb (void *cls,
1326 struct GNUNET_SERVICE_Client *client,
1329 struct CadetClient *c = internal_cls;
1331 GNUNET_assert (c->client == client);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "%s is disconnecting.\n",
1335 if (NULL != c->channels)
1337 GNUNET_CONTAINER_multihashmap32_iterate (c->channels,
1338 &channel_destroy_iterator,
1340 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels));
1341 GNUNET_CONTAINER_multihashmap32_destroy (c->channels);
1343 if (NULL != c->ports)
1345 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1346 &client_release_ports,
1348 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1350 GNUNET_CONTAINER_DLL_remove (clients_head,
1353 GNUNET_STATISTICS_update (stats,
1358 if ( (NULL == clients_head) &&
1359 (GNUNET_YES == shutting_down) )
1365 * Setup CADET internals.
1367 * @param cls closure
1368 * @param server the initialized server
1369 * @param c configuration to use
1373 const struct GNUNET_CONFIGURATION_Handle *c,
1374 struct GNUNET_SERVICE_Handle *service)
1378 GNUNET_CONFIGURATION_get_value_number (c,
1383 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1386 "needs to be a number");
1387 ratchet_messages = 64;
1390 GNUNET_CONFIGURATION_get_value_time (c,
1395 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1398 "need delay value");
1399 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1402 GNUNET_CONFIGURATION_get_value_time (c,
1404 "REFRESH_CONNECTION_TIME",
1407 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1409 "REFRESH_CONNECTION_TIME",
1410 "need delay value");
1411 keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1414 GNUNET_CONFIGURATION_get_value_number (c,
1423 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1424 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1425 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1426 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1427 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1429 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1430 if (NULL == my_private_key)
1433 GNUNET_SCHEDULER_shutdown ();
1436 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1437 &my_full_id.public_key);
1438 stats = GNUNET_STATISTICS_create ("cadet",
1440 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1442 ats_ch = GNUNET_ATS_connectivity_init (c);
1443 /* FIXME: optimize code to allow GNUNET_YES here! */
1444 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1446 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1448 peers = GNUNET_CONTAINER_multipeermap_create (16,
1450 connections = GNUNET_CONTAINER_multishortmap_create (256,
1455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1456 "CADET started for peer %s\n",
1457 GNUNET_i2s (&my_full_id));
1463 * Define "main" method using service macro.
1467 GNUNET_SERVICE_OPTION_NONE,
1470 &client_disconnect_cb,
1472 GNUNET_MQ_hd_fixed_size (port_open,
1473 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1474 struct GNUNET_CADET_PortMessage,
1476 GNUNET_MQ_hd_fixed_size (port_close,
1477 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1478 struct GNUNET_CADET_PortMessage,
1480 GNUNET_MQ_hd_fixed_size (channel_create,
1481 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1482 struct GNUNET_CADET_LocalChannelCreateMessage,
1484 GNUNET_MQ_hd_fixed_size (channel_destroy,
1485 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1486 struct GNUNET_CADET_LocalChannelDestroyMessage,
1488 GNUNET_MQ_hd_var_size (local_data,
1489 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1490 struct GNUNET_CADET_LocalData,
1492 GNUNET_MQ_hd_fixed_size (local_ack,
1493 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1494 struct GNUNET_CADET_LocalAck,
1496 GNUNET_MQ_hd_fixed_size (get_peers,
1497 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1498 struct GNUNET_MessageHeader,
1500 GNUNET_MQ_hd_fixed_size (show_peer,
1501 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1502 struct GNUNET_CADET_LocalInfo,
1504 GNUNET_MQ_hd_fixed_size (info_tunnels,
1505 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1506 struct GNUNET_MessageHeader,
1508 GNUNET_MQ_hd_fixed_size (info_tunnel,
1509 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1510 struct GNUNET_CADET_LocalInfo,
1512 GNUNET_MQ_hd_fixed_size (info_dump,
1513 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1514 struct GNUNET_MessageHeader,
1516 GNUNET_MQ_handler_end ());
1518 /* end of gnunet-service-cadet-new.c */