2 This file is part of GNUnet.
3 Copyright (C) 2001-2013, 2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file cadet/gnunet-service-cadet-new.c
23 * @brief GNUnet CADET service with encryption
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
28 * - peer: other cadet instance. If there is direct connection it's a neighbor.
29 * - path: series of directly connected peer from one peer to another.
30 * - connection: path which is being used in a tunnel.
31 * - tunnel: encrypted connection to a peer, neighbor or not.
32 * - channel: logical link between two clients, on the same or different peers.
33 * have properties like reliability.
37 #include "gnunet_util_lib.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet-service-cadet-new.h"
41 #include "gnunet-service-cadet-new_channel.h"
42 #include "gnunet-service-cadet-new_connection.h"
43 #include "gnunet-service-cadet-new_dht.h"
44 #include "gnunet-service-cadet-new_hello.h"
45 #include "gnunet-service-cadet-new_tunnels.h"
46 #include "gnunet-service-cadet-new_peer.h"
47 #include "gnunet-service-cadet-new_paths.h"
49 #define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
53 * Struct containing information about a client of the service
60 struct CadetClient *next;
65 struct CadetClient *prev;
68 * Tunnels that belong to this client, indexed by local id
70 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
73 * Tunnels this client has accepted, indexed by incoming local id
75 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
78 * Channel ID for the next incoming channel.
80 struct GNUNET_CADET_ClientChannelNumber next_chid;
83 * Handle to communicate with the client
85 struct GNUNET_MQ_Handle *mq;
90 struct GNUNET_SERVICE_Client *client;
93 * Ports that this client has declared interest in.
94 * Indexed by port, contains *Client.
96 struct GNUNET_CONTAINER_MultiHashMap *ports;
99 * Whether the client is active or shutting down (don't send confirmations
100 * to a client that is shutting down).
105 * ID of the client, mainly for debug messages
110 /******************************************************************************/
111 /*********************** GLOBAL VARIABLES ****************************/
112 /******************************************************************************/
114 /****************************** Global variables ******************************/
117 * Handle to the statistics service.
119 struct GNUNET_STATISTICS_Handle *stats;
122 * Handle to communicate with ATS.
124 struct GNUNET_ATS_ConnectivityHandle *ats_ch;
129 struct GNUNET_PeerIdentity my_full_id;
134 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
137 * Signal that shutdown is happening: prevent recover measures.
142 * DLL with all the clients, head.
144 static struct CadetClient *clients_head;
147 * DLL with all the clients, tail.
149 static struct CadetClient *clients_tail;
152 * Next ID to assign to a client.
154 static unsigned int next_client_id;
157 * All ports clients of this peer have opened.
159 struct GNUNET_CONTAINER_MultiHashMap *open_ports;
162 * Map from ports to channels where the ports were closed at the
163 * time we got the inbound connection.
164 * Indexed by port, contains `struct CadetChannel`.
166 struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
169 * Map from PIDs to `struct CadetPeer` entries.
171 struct GNUNET_CONTAINER_MultiPeerMap *peers;
176 * Send a message to a client.
178 * @param c client to get the message
179 * @param env envelope with the message
182 GSC_send_to_client (struct CadetClient *c,
183 struct GNUNET_MQ_Envelope *env)
185 GNUNET_MQ_send (c->mq,
191 * Return identifier for a client as a string.
193 * @param c client to identify
194 * @return string for debugging
197 GSC_2s (struct CadetClient *c)
202 return "Client(NULL)";
203 GNUNET_snprintf (buf,
212 * Obtain the next LID to use for incoming connections to
215 * @param c client handle
217 static struct GNUNET_CADET_ClientChannelNumber
218 client_get_next_lid (struct CadetClient *c)
220 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_chid;
222 /* increment until we have a free one... */
224 GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels,
225 ntohl (ccn.channel_of_client)))
227 ccn.channel_of_client
228 = htonl (1 + (ntohl (ccn.channel_of_client)));
229 if (ntohl (ccn.channel_of_client) >=
230 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
231 ccn.channel_of_client = htonl (0);
233 c->next_chid.channel_of_client
234 = htonl (1 + (ntohl (ccn.channel_of_client)));
240 * Bind incoming channel to this client, and notify client
241 * about incoming connection.
243 * @param c client to bind to
244 * @param ch channel to be bound
245 * @param dest peer that establishes the connection
246 * @param port port number
247 * @param options options
248 * @return local channel number assigned to the new client
250 struct GNUNET_CADET_ClientChannelNumber
251 GSC_bind (struct CadetClient *c,
252 struct CadetChannel *ch,
253 struct CadetPeer *dest,
254 const struct GNUNET_HashCode *port,
257 struct GNUNET_MQ_Envelope *env;
258 struct GNUNET_CADET_ChannelCreateMessage *msg;
259 struct GNUNET_CADET_ClientChannelNumber lid;
261 lid = client_get_next_lid (c);
262 GNUNET_assert (GNUNET_YES ==
263 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
264 ntohl (lid.channel_of_client),
266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
268 /* notify local client about incoming connection! */
269 env = GNUNET_MQ_msg (msg,
270 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
271 msg->channel_id = lid;
273 msg->opt = htonl (options);
274 msg->peer = *GCP_get_id (dest);
275 GSC_send_to_client (c,
281 /******************************************************************************/
282 /************************ MAIN FUNCTIONS ****************************/
283 /******************************************************************************/
286 * Task run during shutdown.
291 shutdown_task (void *cls)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 shutting_down = GNUNET_YES;
299 GNUNET_STATISTICS_destroy (stats,
303 if (NULL != open_ports)
305 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
308 if (NULL != loose_channels)
310 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
311 loose_channels = NULL;
313 /* All channels, connections and CORE must be down before this point. */
314 GCP_destroy_all_peers ();
317 GNUNET_CONTAINER_multipeermap_destroy (peers);
322 GNUNET_ATS_connectivity_done (ats_ch);
327 GNUNET_free_non_null (my_private_key);
328 my_private_key = NULL;
333 * We had a remote connection @a value to port @a port before
334 * client @a cls opened port @a port. Bind them now.
336 * @param cls the `struct CadetClient`
337 * @param port the port
338 * @param value the `struct CadetChannel`
339 * @return #GNUNET_YES (iterate over all such channels)
342 bind_loose_channel (void *cls,
343 const struct GNUNET_HashCode *port,
346 struct CadetClient *c = cls;
347 struct CadetChannel *ch = value;
351 GNUNET_assert (GNUNET_YES ==
352 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
360 * Handler for port open requests.
362 * @param cls Identification of the client.
363 * @param pmsg The actual message.
366 handle_port_open (void *cls,
367 const struct GNUNET_CADET_PortMessage *pmsg)
369 struct CadetClient *c = cls;
371 LOG (GNUNET_ERROR_TYPE_DEBUG,
372 "Open port %s requested by client %u\n",
373 GNUNET_h2s (&pmsg->port),
375 if (NULL == c->ports)
376 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
379 GNUNET_CONTAINER_multihashmap_put (c->ports,
382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
385 GNUNET_SERVICE_client_drop (c->client);
388 /* store in global hashmap */
389 /* FIXME only allow one client to have the port open,
390 * have a backup hashmap with waiting clients */
391 GNUNET_CONTAINER_multihashmap_put (open_ports,
394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
395 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
399 GNUNET_SERVICE_client_continue (c->client);
404 * Handler for port close requests.
406 * @param cls Identification of the client.
407 * @param pmsg The actual message.
410 handle_port_close (void *cls,
411 const struct GNUNET_CADET_PortMessage *pmsg)
413 struct CadetClient *c = cls;
415 LOG (GNUNET_ERROR_TYPE_DEBUG,
416 "Open port %s requested by client %u\n",
417 GNUNET_h2s (&pmsg->port),
420 GNUNET_CONTAINER_multihashmap_remove (c->ports,
425 GNUNET_SERVICE_client_drop (c->client);
428 GNUNET_assert (GNUNET_YES ==
429 GNUNET_CONTAINER_multihashmap_remove (open_ports,
433 GNUNET_SERVICE_client_continue (c->client);
438 * Handler for requests of new channels.
440 * @param cls Identification of the client.
441 * @param ccm The actual message.
444 handle_channel_create (void *cls,
445 const struct GNUNET_CADET_ChannelCreateMessage *ccm)
447 struct CadetClient *c = cls;
448 struct CadetChannel *ch;
449 struct GNUNET_CADET_ClientChannelNumber chid;
450 struct CadetPeer *dst;
452 chid = ccm->channel_id;
453 if (ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
455 /* Channel ID not in allowed range. */
457 GNUNET_SERVICE_client_drop (c->client);
460 ch = GNUNET_CONTAINER_multihashmap32_get (c->own_channels,
461 ntohl (chid.channel_of_client));
464 /* Channel ID already in use. Not allowed. */
466 GNUNET_SERVICE_client_drop (c->client);
470 dst = GCP_get (&ccm->peer,
474 ch = GCCH_channel_local_new (c,
482 GNUNET_SERVICE_client_drop (c->client);
485 GNUNET_assert (GNUNET_YES ==
486 GNUNET_CONTAINER_multihashmap32_put (c->own_channels,
487 ntohl (chid.channel_of_client),
489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "New channel %s to %s at port %s requested by client %u\n",
494 GNUNET_i2s (&ccm->peer),
495 GNUNET_h2s (&ccm->port),
497 GNUNET_SERVICE_client_continue (c->client);
502 * Return the map which we use for client @a c for a channel ID of @a chid
504 * @param c client to find map for
505 * @param chid chid to find map for
506 * @return applicable map we use
508 static struct GNUNET_CONTAINER_MultiHashMap32 *
509 get_map_by_chid (struct CadetClient *c,
510 struct GNUNET_CADET_ClientChannelNumber chid)
512 return (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
514 : c->incoming_channels;
519 * Handler for requests of deleting tunnels
521 * @param cls client identification of the client
522 * @param msg the actual message
525 handle_channel_destroy (void *cls,
526 const struct GNUNET_CADET_ChannelDestroyMessage *msg)
528 struct CadetClient *c = cls;
529 struct GNUNET_CADET_ClientChannelNumber chid;
530 struct GNUNET_CONTAINER_MultiHashMap32 *map;
531 struct CadetChannel *ch;
533 /* Retrieve tunnel */
534 chid = msg->channel_id;
535 map = get_map_by_chid (c,
537 ch = GNUNET_CONTAINER_multihashmap32_get (map,
538 ntohl (chid.channel_of_client));
541 /* Client attempted to destroy unknown channel */
543 GNUNET_SERVICE_client_drop (c->client);
546 LOG (GNUNET_ERROR_TYPE_INFO,
547 "Client %u is destroying channel %s\n",
550 GNUNET_assert (GNUNET_YES ==
551 GNUNET_CONTAINER_multihashmap32_remove (map,
552 ntohl (chid.channel_of_client),
554 GCCH_channel_local_destroy (ch);
555 GNUNET_SERVICE_client_continue (c->client);
560 * Check for client traffic data message is well-formed
562 * @param cls identification of the client
563 * @param msg the actual message
564 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
567 check_data (void *cls,
568 const struct GNUNET_CADET_LocalData *msg)
570 const struct GNUNET_MessageHeader *payload;
572 size_t payload_claimed_size;
574 /* Sanity check for message size */
575 payload_size = ntohs (msg->header.size) - sizeof (*msg);
576 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
577 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
580 return GNUNET_SYSERR;
582 payload = (struct GNUNET_MessageHeader *) &msg[1];
583 payload_claimed_size = ntohs (payload->size);
584 if (payload_size != payload_claimed_size)
587 return GNUNET_SYSERR;
594 * Handler for client traffic
596 * @param cls identification of the client
597 * @param msg the actual message
600 handle_data (void *cls,
601 const struct GNUNET_CADET_LocalData *msg)
603 struct CadetClient *c = cls;
604 struct GNUNET_CONTAINER_MultiHashMap32 *map;
605 struct GNUNET_CADET_ClientChannelNumber chid;
606 struct CadetChannel *ch;
607 const struct GNUNET_MessageHeader *payload;
610 map = get_map_by_chid (c,
612 ch = GNUNET_CONTAINER_multihashmap32_get (map,
613 ntohl (chid.channel_of_client));
616 /* Channel does not exist! */
618 GNUNET_SERVICE_client_drop (c->client);
622 payload = (const struct GNUNET_MessageHeader *) &msg[1];
623 LOG (GNUNET_ERROR_TYPE_DEBUG,
624 "Received %u bytes payload from client %u for channel %s\n",
625 ntohs (payload->size),
629 GCCH_handle_local_data (ch,
632 GNUNET_SERVICE_client_drop (c->client);
635 GNUNET_SERVICE_client_continue (c->client);
640 * Handler for client's ACKs for payload traffic.
642 * @param cls identification of the client.
643 * @param msg The actual message.
646 handle_ack (void *cls,
647 const struct GNUNET_CADET_LocalAck *msg)
649 struct CadetClient *c = cls;
650 struct GNUNET_CONTAINER_MultiHashMap32 *map;
651 struct GNUNET_CADET_ClientChannelNumber chid;
652 struct CadetChannel *ch;
654 chid = msg->channel_id;
655 map = get_map_by_chid (c,
657 ch = GNUNET_CONTAINER_multihashmap32_get (map,
658 ntohl (chid.channel_of_client));
661 /* Channel does not exist! */
663 GNUNET_SERVICE_client_drop (c->client);
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Got a local ACK from client %u for channel %s\n",
670 GCCH_handle_local_ack (ch);
671 GNUNET_SERVICE_client_continue (c->client);
676 * Iterator over all peers to send a monitoring client info about each peer.
678 * @param cls Closure ().
679 * @param peer Peer ID (tunnel remote peer).
680 * @param value Peer info.
681 * @return #GNUNET_YES, to keep iterating.
684 get_all_peers_iterator (void *cls,
685 const struct GNUNET_PeerIdentity *peer,
688 struct CadetClient *c = cls;
689 struct CadetPeer *p = value;
690 struct GNUNET_MQ_Envelope *env;
691 struct GNUNET_CADET_LocalInfoPeer *msg;
693 env = GNUNET_MQ_msg (msg,
694 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
695 msg->destination = *peer;
696 msg->paths = htons (GCP_count_paths (p));
697 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
699 GNUNET_MQ_send (c->mq,
706 * Handler for client's INFO PEERS request.
708 * @param cls Identification of the client.
709 * @param message The actual message.
712 handle_get_peers (void *cls,
713 const struct GNUNET_MessageHeader *message)
715 struct CadetClient *c = cls;
716 struct GNUNET_MQ_Envelope *env;
717 struct GNUNET_MessageHeader *reply;
719 GCP_iterate_all (&get_all_peers_iterator,
721 env = GNUNET_MQ_msg (reply,
722 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
723 GNUNET_MQ_send (c->mq,
725 GNUNET_SERVICE_client_continue (c->client);
730 * Iterator over all paths of a peer to build an InfoPeer message.
731 * Message contains blocks of peers, first not included.
733 * @param cls message queue for transmission
734 * @param path Path itself
735 * @param off offset of the peer on @a path
736 * @return #GNUNET_YES if should keep iterating.
737 * #GNUNET_NO otherwise.
740 path_info_iterator (void *cls,
741 struct CadetPeerPath *path,
744 struct GNUNET_MQ_Handle *mq = cls;
745 struct GNUNET_MQ_Envelope *env;
746 struct GNUNET_MessageHeader *resp;
747 struct GNUNET_PeerIdentity *id;
750 unsigned int path_length;
752 path_length = GCPP_get_length (path);
753 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
754 if (sizeof (*resp) + path_size > UINT16_MAX)
756 LOG (GNUNET_ERROR_TYPE_WARNING,
757 "Path of %u entries is too long for info message\n",
761 env = GNUNET_MQ_msg_extra (resp,
763 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
764 id = (struct GNUNET_PeerIdentity *) &resp[1];
766 /* Don't copy first peer. First peer is always the local one. Last
767 * peer is always the destination (leave as 0, EOL).
769 for (i = 0; i < off; i++)
770 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
779 * Handler for client's SHOW_PEER request.
781 * @param cls Identification of the client.
782 * @param msg The actual message.
785 handle_show_peer (void *cls,
786 const struct GNUNET_CADET_LocalInfo *msg)
788 struct CadetClient *c = cls;
790 struct GNUNET_MQ_Envelope *env;
791 struct GNUNET_MessageHeader *resp;
793 p = GCP_get (&msg->peer,
796 GCP_iterate_paths (p,
799 /* Send message with 0/0 to indicate the end */
800 env = GNUNET_MQ_msg (resp,
801 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
802 GNUNET_MQ_send (c->mq,
804 GNUNET_SERVICE_client_continue (c->client);
809 * Iterator over all tunnels to send a monitoring client info about each tunnel.
811 * @param cls Closure ().
812 * @param peer Peer ID (tunnel remote peer).
813 * @param value a `struct CadetPeer`
814 * @return #GNUNET_YES, to keep iterating.
817 get_all_tunnels_iterator (void *cls,
818 const struct GNUNET_PeerIdentity *peer,
821 struct CadetClient *c = cls;
822 struct CadetPeer *p = value;
823 struct GNUNET_MQ_Envelope *env;
824 struct GNUNET_CADET_LocalInfoTunnel *msg;
825 struct CadetTunnel *t;
827 t = GCP_get_tunnel (p,
831 env = GNUNET_MQ_msg (msg,
832 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
833 msg->destination = *peer;
834 msg->channels = htonl (GCT_count_channels (t));
835 msg->connections = htonl (GCT_count_any_connections (t));
836 msg->cstate = htons ((uint16_t) GCT_get_cstate (t));
837 msg->estate = htons ((uint16_t) GCT_get_estate (t));
838 GNUNET_MQ_send (c->mq,
845 * Handler for client's INFO TUNNELS request.
847 * @param cls client Identification of the client.
848 * @param message The actual message.
851 handle_get_tunnels (void *cls,
852 const struct GNUNET_MessageHeader *message)
854 struct CadetClient *c = cls;
855 struct GNUNET_MQ_Envelope *env;
856 struct GNUNET_MessageHeader *reply;
858 GCP_iterate_all (&get_all_tunnels_iterator,
860 env = GNUNET_MQ_msg (reply,
861 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
862 GNUNET_MQ_send (c->mq,
864 GNUNET_SERVICE_client_continue (c->client);
872 iter_connection (void *cls,
873 struct CadetConnection *c)
875 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
876 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
878 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
879 h[msg->connections++] = *(GCC_get_id (c));
887 iter_channel (void *cls,
888 struct CadetChannel *ch)
890 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
891 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
892 struct GCT_ChannelTunnelNumber *chn
893 = (struct GCT_ChannelTunnelNumber *) &h[msg->connections];
895 chn[msg->channels++] = GCCH_get_id (ch);
900 * Handler for client's SHOW_TUNNEL request.
902 * @param cls Identification of the client.
903 * @param msg The actual message.
906 handle_show_tunnel (void *cls,
907 const struct GNUNET_CADET_LocalInfo *msg)
909 struct CadetClient *c = cls;
910 struct GNUNET_MQ_Envelope *env;
911 struct GNUNET_CADET_LocalInfoTunnel *resp;
912 struct CadetTunnel *t;
917 p = GCP_get (&msg->peer,
919 t = GCP_get_tunnel (p,
923 /* We don't know the tunnel */
924 struct GNUNET_MQ_Envelope *env;
925 struct GNUNET_CADET_LocalInfoTunnel *warn;
927 LOG (GNUNET_ERROR_TYPE_INFO,
928 "Tunnel to %s unknown\n",
929 GNUNET_i2s_full (&msg->peer));
930 env = GNUNET_MQ_msg (warn,
931 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
932 warn->destination = msg->peer;
933 GNUNET_MQ_send (c->mq,
935 GNUNET_SERVICE_client_continue (c->client);
939 /* Initialize context */
940 ch_n = GCT_count_channels (t);
941 c_n = GCT_count_any_connections (t);
942 env = GNUNET_MQ_msg_extra (resp,
943 c_n * sizeof (struct GNUNET_CADET_Hash) +
944 ch_n * sizeof (struct GCT_ChannelTunnelNumber),
945 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
946 resp->destination = msg->peer;
947 /* Do not reorder! #iter_channel needs counters in HBO! */
948 GCT_iterate_connections (t,
951 GCT_iterate_channels (t,
954 resp->connections = htonl (resp->connections);
955 resp->channels = htonl (resp->channels);
956 resp->cstate = htons (GCT_get_cstate (t));
957 resp->estate = htons (GCT_get_estate (t));
958 GNUNET_MQ_send (c->mq,
960 GNUNET_SERVICE_client_continue (c->client);
965 * Iterator over all peers to dump info for each peer.
967 * @param cls Closure (unused).
968 * @param peer Peer ID (tunnel remote peer).
969 * @param value Peer info.
971 * @return #GNUNET_YES, to keep iterating.
974 show_peer_iterator (void *cls,
975 const struct GNUNET_PeerIdentity *peer,
978 struct CadetPeer *p = value;
979 struct CadetTunnel *t;
981 t = GCP_get_tunnel (p,
985 GNUNET_ERROR_TYPE_ERROR);
986 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
992 * Handler for client's INFO_DUMP request.
994 * @param cls Identification of the client.
995 * @param message The actual message.
998 handle_info_dump (void *cls,
999 const struct GNUNET_MessageHeader *message)
1001 struct CadetClient *c = cls;
1003 LOG (GNUNET_ERROR_TYPE_INFO,
1004 "Received dump info request from client %u\n",
1007 LOG (GNUNET_ERROR_TYPE_ERROR,
1008 "*************************** DUMP START ***************************\n");
1009 for (struct CadetClient *ci = clients_head; NULL != ci; ci = ci->next)
1011 LOG (GNUNET_ERROR_TYPE_ERROR,
1012 "Client %u (%p), handle: %p, ports: %u, own channels: %u, incoming channels: %u\n",
1017 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1019 GNUNET_CONTAINER_multihashmap32_size (ci->own_channels),
1020 GNUNET_CONTAINER_multihashmap32_size (ci->incoming_channels));
1022 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1023 GCP_iterate_all (&show_peer_iterator,
1026 LOG (GNUNET_ERROR_TYPE_ERROR,
1027 "**************************** DUMP END ****************************\n");
1029 GNUNET_SERVICE_client_continue (c->client);
1035 * Callback called when a client connects to the service.
1037 * @param cls closure for the service
1038 * @param client the new client that connected to the service
1039 * @param mq the message queue used to send messages to the client
1043 client_connect_cb (void *cls,
1044 struct GNUNET_SERVICE_Client *client,
1045 struct GNUNET_MQ_Handle *mq)
1047 struct CadetClient *c;
1049 c = GNUNET_new (struct CadetClient);
1052 c->id = next_client_id++; /* overflow not important: just for debug */
1054 = GNUNET_CONTAINER_multihashmap32_create (32);
1055 c->incoming_channels
1056 = GNUNET_CONTAINER_multihashmap32_create (32);
1057 GNUNET_CONTAINER_DLL_insert (clients_head,
1060 GNUNET_STATISTICS_update (stats,
1069 * Iterator for deleting each channel whose client endpoint disconnected.
1071 * @param cls Closure (client that has disconnected).
1072 * @param key The local channel id (used to access the hashmap).
1073 * @param value The value stored at the key (channel to destroy).
1074 * @return #GNUNET_OK, keep iterating.
1077 own_channel_destroy_iterator (void *cls,
1081 struct CadetClient *c = cls;
1082 struct CadetChannel *ch = value;
1084 GNUNET_assert (GNUNET_YES ==
1085 GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1088 LOG (GNUNET_ERROR_TYPE_DEBUG,
1089 "Destroying own channel %s, due to client %u shutdown.\n",
1092 GCCH_channel_local_destroy (ch);
1098 * Iterator for deleting each channel whose client endpoint disconnected.
1100 * @param cls Closure (client that has disconnected).
1101 * @param key The local channel id (used to access the hashmap).
1102 * @param value The value stored at the key (channel to destroy).
1103 * @return #GNUNET_OK, keep iterating.
1106 incoming_channel_destroy_iterator (void *cls,
1110 struct CadetChannel *ch = value;
1111 struct CadetClient *c = cls;
1113 GNUNET_assert (GNUNET_YES ==
1114 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Destroying incoming channel %s due to client %u shutdown.\n",
1122 GCCH_channel_incoming_destroy (ch);
1128 * Remove client's ports from the global hashmap on disconnect.
1130 * @param cls Closure (unused).
1132 * @param value the `struct CadetClient` to remove
1133 * @return #GNUNET_OK, keep iterating.
1136 client_release_ports (void *cls,
1137 const struct GNUNET_HashCode *key,
1140 struct CadetClient *c = value;
1142 GNUNET_assert (GNUNET_YES ==
1143 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1146 GNUNET_assert (GNUNET_YES ==
1147 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1155 * Callback called when a client disconnected from the service
1157 * @param cls closure for the service
1158 * @param client the client that disconnected
1159 * @param internal_cls should be equal to @a c
1162 client_disconnect_cb (void *cls,
1163 struct GNUNET_SERVICE_Client *client,
1166 struct CadetClient *c = internal_cls;
1168 GNUNET_assert (c->client == client);
1169 c->shutting_down = GNUNET_YES;
1170 if (NULL != c->own_channels)
1172 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
1173 &own_channel_destroy_iterator,
1175 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
1177 if (NULL != c->incoming_channels)
1179 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
1180 &incoming_channel_destroy_iterator,
1182 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
1184 if (NULL != c->ports)
1186 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1187 &client_release_ports,
1189 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1191 GNUNET_CONTAINER_DLL_remove (clients_head,
1194 GNUNET_STATISTICS_update (stats,
1203 * Setup CADET internals.
1205 * @param cls closure
1206 * @param server the initialized server
1207 * @param c configuration to use
1211 const struct GNUNET_CONFIGURATION_Handle *c,
1212 struct GNUNET_SERVICE_Handle *service)
1214 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1215 if (NULL == my_private_key)
1218 GNUNET_SCHEDULER_shutdown ();
1221 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1222 &my_full_id.public_key);
1223 stats = GNUNET_STATISTICS_create ("cadet",
1225 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1227 ats_ch = GNUNET_ATS_connectivity_init (c);
1228 /* FIXME: optimize code to allow GNUNET_YES here! */
1229 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1231 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1233 peers = GNUNET_CONTAINER_multipeermap_create (16,
1237 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1238 "CADET starting at peer %s\n",
1239 GNUNET_i2s (&my_full_id));
1245 * Define "main" method using service macro.
1249 GNUNET_SERVICE_OPTION_NONE,
1252 &client_disconnect_cb,
1254 GNUNET_MQ_hd_fixed_size (port_open,
1255 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1256 struct GNUNET_CADET_PortMessage,
1258 GNUNET_MQ_hd_fixed_size (port_close,
1259 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1260 struct GNUNET_CADET_PortMessage,
1262 GNUNET_MQ_hd_fixed_size (channel_create,
1263 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1264 struct GNUNET_CADET_ChannelCreateMessage,
1266 GNUNET_MQ_hd_fixed_size (channel_destroy,
1267 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1268 struct GNUNET_CADET_ChannelDestroyMessage,
1270 GNUNET_MQ_hd_var_size (data,
1271 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1272 struct GNUNET_CADET_LocalData,
1274 GNUNET_MQ_hd_fixed_size (ack,
1275 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1276 struct GNUNET_CADET_LocalAck,
1278 GNUNET_MQ_hd_fixed_size (get_peers,
1279 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1280 struct GNUNET_MessageHeader,
1282 GNUNET_MQ_hd_fixed_size (show_peer,
1283 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1284 struct GNUNET_CADET_LocalInfo,
1286 GNUNET_MQ_hd_fixed_size (get_tunnels,
1287 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1288 struct GNUNET_MessageHeader,
1290 GNUNET_MQ_hd_fixed_size (show_tunnel,
1291 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1292 struct GNUNET_CADET_LocalInfo,
1294 GNUNET_MQ_hd_fixed_size (info_dump,
1295 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1296 struct GNUNET_MessageHeader,
1298 GNUNET_MQ_handler_end ());
1300 /* end of gnunet-service-cadet-new.c */