2 This file is part of GNUnet.
3 Copyright (C) 2013, 2016 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 dv/gnunet-service-dv.c
23 * @brief the distance vector service, primarily handles gossip of nearby
24 * peers and sending/receiving DV messages from core and decapsulating
27 * @author Christian Grothoff
28 * @author Nathan Evans
31 #include "gnunet_util_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_set_service.h"
38 #include "gnunet_ats_service.h"
44 * How often do we establish the consensu?
46 #define GNUNET_DV_CONSENSUS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
49 * Maximum number of messages we queue per peer.
51 #define MAX_QUEUE_SIZE 16
54 * Maximum number of messages we queue towards the clients/plugin.
56 #define MAX_QUEUE_SIZE_PLUGIN 1024
59 * The default fisheye depth, from how many hops away will
62 #define DEFAULT_FISHEYE_DEPTH 3
65 * How many hops is a direct neighbor away?
67 #define DIRECT_NEIGHBOR_COST 1
70 GNUNET_NETWORK_STRUCT_BEGIN
73 * Information about a peer DV can route to. These entries are what
74 * we use as the binary format to establish consensus to create our
75 * routing table and as the address format in the HELLOs.
81 * Identity of the peer we can reach.
83 struct GNUNET_PeerIdentity peer;
86 * How many hops (1-3) is this peer away? in network byte order
88 uint32_t distance GNUNET_PACKED;
94 * Message exchanged between DV services (via core), requesting a
95 * message to be routed.
100 * Type: #GNUNET_MESSAGE_TYPE_DV_ROUTE
102 struct GNUNET_MessageHeader header;
105 * Expected (remaining) distance. Must be always smaller than
106 * #DEFAULT_FISHEYE_DEPTH, should be zero at the target. Must
107 * be decremented by one at each hop. Peers must not forward
108 * these messages further once the counter has reached zero.
110 uint32_t distance GNUNET_PACKED;
113 * The (actual) target of the message (this peer, if distance is zero).
115 struct GNUNET_PeerIdentity target;
118 * The (actual) sender of the message.
120 struct GNUNET_PeerIdentity sender;
124 GNUNET_NETWORK_STRUCT_END
128 * Information about a direct neighbor (core-level, excluding
129 * DV-links, only DV-enabled peers).
131 struct DirectNeighbor
135 * Identity of the peer.
137 struct GNUNET_PeerIdentity peer;
140 * Session ID we use whenever we create a set union with
141 * this neighbor; constructed from the XOR of our peer
142 * IDs and then salted with "DV-SALT" to avoid conflicts
143 * with other applications.
145 struct GNUNET_HashCode real_session_id;
148 * Transmit handle to core service.
150 struct GNUNET_MQ_Handle *mq;
153 * Routing table of the neighbor, NULL if not yet established.
154 * Keys are peer identities, values are 'struct Target' entries.
155 * Note that the distances in the targets are from the point-of-view
156 * of the peer, not from us!
158 struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table;
161 * Updated routing table of the neighbor, under construction,
162 * NULL if we are not currently building it.
163 * Keys are peer identities, values are 'struct Target' entries.
164 * Note that the distances in the targets are from the point-of-view
165 * of the other peer, not from us!
167 struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table_consensus;
170 * Our current (exposed) routing table as a set.
172 struct GNUNET_SET_Handle *my_set;
175 * Handle for our current active set union operation.
177 struct GNUNET_SET_OperationHandle *set_op;
180 * Handle used if we are listening for this peer, waiting for the
181 * other peer to initiate construction of the set union. NULL if
182 * we ar the initiating peer.
184 struct GNUNET_SET_ListenHandle *listen_handle;
187 * ID of the task we use to (periodically) update our consensus
188 * with this peer. Used if we are the initiating peer.
190 struct GNUNET_SCHEDULER_Task *initiate_task;
193 * At what offset are we, with respect to inserting our own routes
194 * into the consensus?
196 unsigned int consensus_insertion_offset;
199 * At what distance are we, with respect to inserting our own routes
200 * into the consensus?
202 unsigned int consensus_insertion_distance;
205 * Elements in consensus
207 unsigned int consensus_elements;
210 * Direct one hop route
212 struct Route *direct_route;
215 * Flag set within 'check_target_removed' to trigger full global route refresh.
220 * Our distance to this peer, 0 for unknown.
225 * The network this peer is in
227 enum GNUNET_ATS_Network_Type network;
230 * Is this neighbor connected at the core level?
238 * A route includes information about the next hop,
239 * the target, and the ultimate distance to the
246 * Which peer do we need to forward the message to?
248 struct DirectNeighbor *next_hop;
251 * What would be the target, and how far is it away?
253 struct Target target;
256 * Offset of this target in the respective consensus set.
258 unsigned int set_offset;
264 * Set of targets we bring to a consensus; all targets in a set have a
265 * distance equal to the sets distance (which is implied by the array
272 * Array of targets in the set, may include NULL entries if a
273 * neighbor has disconnected; the targets are allocated with the
274 * respective container (all_routes), not here.
276 struct Route **targets;
279 * Size of the @e targets array.
281 unsigned int array_length;
287 * Peermap of all of our neighbors; processing these usually requires
288 * first checking to see if the peer is core-connected and if the
289 * distance is 1, in which case they are direct neighbors.
291 static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
294 * Hashmap with all routes that we currently support; contains
295 * routing information for all peers from distance 2
296 * up to distance #DEFAULT_FISHEYE_DEPTH.
298 static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
301 * Array of consensus sets we expose to the outside world. Sets
302 * are structured by the distance to the target.
304 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH];
307 * Handle to the core service api.
309 static struct GNUNET_CORE_Handle *core_api;
312 * The identity of our peer.
314 static struct GNUNET_PeerIdentity my_identity;
317 * The configuration for this service.
319 static const struct GNUNET_CONFIGURATION_Handle *cfg;
322 * The client, the DV plugin connected to us (or an event monitor).
323 * Hopefully this client will never change, although if the plugin
324 * dies and returns for some reason it may happen.
326 static struct GNUNET_NotificationContext *nc;
329 * Handle for the statistics service.
331 static struct GNUNET_STATISTICS_Handle *stats;
334 * Handle to ATS service.
336 static struct GNUNET_ATS_PerformanceHandle *ats;
339 * Task scheduled to refresh routes based on direct neighbours.
341 static struct GNUNET_SCHEDULER_Task *rr_task;
344 * #GNUNET_YES if we are shutting down.
346 static int in_shutdown;
349 * Start creating a new DV set union by initiating the connection.
351 * @param cls the 'struct DirectNeighbor' of the peer we're building
352 * a routing consensus with
355 initiate_set_union (void *cls);
359 * Start creating a new DV set union construction, our neighbour has
360 * asked for it (callback for listening peer).
362 * @param cls the 'struct DirectNeighbor' of the peer we're building
363 * a routing consensus with
364 * @param other_peer the other peer
365 * @param context_msg message with application specific information from
367 * @param request request from the other peer, use GNUNET_SET_accept
368 * to accept it, otherwise the request will be refused
369 * Note that we don't use a return value here, as it is also
370 * necessary to specify the set we want to do the operation with,
371 * whith sometimes can be derived from the context message.
372 * Also necessary to specify the timeout.
375 listen_set_union (void *cls,
376 const struct GNUNET_PeerIdentity *other_peer,
377 const struct GNUNET_MessageHeader *context_msg,
378 struct GNUNET_SET_Request *request);
382 * Forward a message from another peer to the plugin.
384 * @param message the message to send to the plugin
385 * @param origin the original sender of the message
386 * @param distance distance to the original sender of the message
389 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
390 const struct GNUNET_PeerIdentity *origin,
393 struct GNUNET_DV_ReceivedMessage *received_msg;
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Delivering message from peer `%s' at distance %u\n",
399 (unsigned int) distance);
400 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
401 ntohs (message->size);
402 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
404 GNUNET_break (0); /* too big */
407 received_msg = GNUNET_malloc (size);
408 received_msg->header.size = htons (size);
409 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
410 received_msg->distance = htonl (distance);
411 received_msg->sender = *origin;
412 GNUNET_memcpy (&received_msg[1], message, ntohs (message->size));
413 GNUNET_notification_context_broadcast (nc,
414 &received_msg->header,
416 GNUNET_free (received_msg);
421 * Forward a control message to the plugin.
423 * @param message the message to send to the plugin
426 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
428 GNUNET_notification_context_broadcast (nc,
435 * Send a DISTANCE_CHANGED message to the plugin.
437 * @param peer peer with a changed distance
438 * @param distance new distance to the peer
439 * @param network network used by the neighbor
442 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
444 enum GNUNET_ATS_Network_Type network)
446 struct GNUNET_DV_DistanceUpdateMessage du_msg;
448 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
452 du_msg.header.size = htons (sizeof (du_msg));
453 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
454 du_msg.distance = htonl (distance);
456 du_msg.network = htonl ((uint32_t) network);
457 send_control_to_plugin (&du_msg.header);
462 * Give a CONNECT message to the plugin.
464 * @param target peer that connected
465 * @param distance distance to the target
466 * @param network the network the next hop is located in
469 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
471 enum GNUNET_ATS_Network_Type network)
473 struct GNUNET_DV_ConnectMessage cm;
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Delivering CONNECT about peer %s with distance %u\n",
477 GNUNET_i2s (target), distance);
478 cm.header.size = htons (sizeof (cm));
479 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
480 cm.distance = htonl (distance);
481 cm.network = htonl ((uint32_t) network);
483 send_control_to_plugin (&cm.header);
488 * Give a DISCONNECT message to the plugin.
490 * @param target peer that disconnected
493 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
495 struct GNUNET_DV_DisconnectMessage dm;
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Delivering DISCONNECT about peer `%s'\n",
499 GNUNET_i2s (target));
500 dm.header.size = htons (sizeof (dm));
501 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
502 dm.reserved = htonl (0);
504 send_control_to_plugin (&dm.header);
509 * Forward the given payload to the given target.
511 * @param target where to send the message
512 * @param distance distance to the @a sender
513 * @param sender original sender of the message
514 * @param actual_target ultimate recipient for the message
515 * @param payload payload of the message
518 forward_payload (struct DirectNeighbor *target,
520 const struct GNUNET_PeerIdentity *sender,
521 const struct GNUNET_PeerIdentity *actual_target,
522 const struct GNUNET_MessageHeader *payload)
524 struct GNUNET_MQ_Envelope *env;
525 struct RouteMessage *rm;
527 if ( (GNUNET_MQ_get_length (target->mq) >= MAX_QUEUE_SIZE) &&
528 (0 != memcmp (sender,
530 sizeof (struct GNUNET_PeerIdentity))) )
532 /* not _our_ client and queue is full, drop */
533 GNUNET_STATISTICS_update (stats,
534 "# messages dropped",
539 if (sizeof (struct RouteMessage) + ntohs (payload->size)
540 >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
545 env = GNUNET_MQ_msg_nested_mh (rm,
546 GNUNET_MESSAGE_TYPE_DV_ROUTE,
548 rm->distance = htonl (distance);
549 rm->target = *actual_target;
550 rm->sender = *sender;
551 GNUNET_MQ_send (target->mq,
557 * Find a free slot for storing a 'route' in the 'consensi'
558 * set at the given distance.
560 * @param distance distance to use for the set slot
563 get_consensus_slot (uint32_t distance)
565 struct ConsensusSet *cs;
568 GNUNET_assert (distance < DEFAULT_FISHEYE_DEPTH);
569 cs = &consensi[distance];
571 while ( (i < cs->array_length) &&
572 (NULL != cs->targets[i]) ) i++;
573 if (i == cs->array_length)
575 GNUNET_array_grow (cs->targets,
577 cs->array_length * 2 + 2);
584 * Allocate a slot in the consensus set for a route.
586 * @param route route to initialize
587 * @param distance which consensus set to use
590 allocate_route (struct Route *route,
595 if (distance >= DEFAULT_FISHEYE_DEPTH)
597 route->target.distance = htonl (distance);
598 route->set_offset = UINT_MAX; /* invalid slot */
601 i = get_consensus_slot (distance);
602 route->set_offset = i;
603 consensi[distance].targets[i] = route;
604 route->target.distance = htonl (distance);
609 * Release a slot in the consensus set for a route.
611 * @param route route to release the slot from
614 release_route (struct Route *route)
616 if (UINT_MAX == route->set_offset)
618 GNUNET_assert (ntohl (route->target.distance) < DEFAULT_FISHEYE_DEPTH);
619 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
620 route->set_offset = UINT_MAX; /* indicate invalid slot */
625 * Move a route from one consensus set to another.
627 * @param route route to move
628 * @param new_distance new distance for the route (destination set)
631 move_route (struct Route *route,
632 uint32_t new_distance)
634 release_route (route);
635 allocate_route (route, new_distance);
640 * Initialize this neighbors 'my_set' and when done give
641 * it to the pending set operation for execution.
643 * Add a single element to the set per call:
645 * If we reached the last element of a consensus element: increase distance
648 * @param cls the neighbor for which we are building the set
651 build_set (void *cls)
653 struct DirectNeighbor *neighbor = cls;
654 struct GNUNET_SET_Element element;
655 struct Target *target;
659 /* skip over NULL entries */
660 while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
661 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
662 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
663 neighbor->consensus_insertion_offset++;
664 while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
665 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
667 /* If we reached the last element of a consensus array element: increase distance and start with next array */
668 neighbor->consensus_insertion_offset = 0;
669 neighbor->consensus_insertion_distance++;
670 /* skip over NULL entries */
671 while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
672 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
673 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
674 neighbor->consensus_insertion_offset++;
676 if (DEFAULT_FISHEYE_DEPTH == neighbor->consensus_insertion_distance)
678 /* we have added all elements to the set, run the operation */
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Finished building my SET for peer `%s' with %u elements, committing\n",
681 GNUNET_i2s (&neighbor->peer),
682 neighbor->consensus_elements);
683 GNUNET_SET_commit (neighbor->set_op,
685 GNUNET_SET_destroy (neighbor->my_set);
686 neighbor->my_set = NULL;
690 route = consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset];
691 GNUNET_assert (NULL != route);
692 target = &route->target;
693 GNUNET_assert (ntohl (target->distance) < DEFAULT_FISHEYE_DEPTH);
694 element.size = sizeof (struct Target);
695 element.element_type = htons (0);
696 element.data = target;
698 /* Find next non-NULL entry */
699 neighbor->consensus_insertion_offset++;
700 if ( (0 != memcmp (&target->peer,
702 sizeof (my_identity))) &&
703 (0 != memcmp (&target->peer,
705 sizeof (struct GNUNET_PeerIdentity))) )
707 /* Add target if it is not the neighbor or this peer */
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Adding peer `%s' with distance %u to SET\n",
710 GNUNET_i2s (&target->peer),
711 ntohl (target->distance) + 1);
712 GNUNET_SET_add_element (neighbor->my_set,
714 &build_set, neighbor);
715 neighbor->consensus_elements++;
718 build_set (neighbor);
723 * A peer is now connected to us at distance 1. Initiate DV exchange.
725 * @param neighbor entry for the neighbor at distance 1
728 handle_direct_connect (struct DirectNeighbor *neighbor)
731 struct GNUNET_HashCode h1;
732 struct GNUNET_HashCode h2;
733 struct GNUNET_HashCode session_id;
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Direct connection to %s established, routing table exchange begins.\n",
737 GNUNET_i2s (&neighbor->peer));
738 GNUNET_STATISTICS_update (stats,
739 "# peers connected (1-hop)",
741 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
745 GNUNET_assert (GNUNET_YES ==
746 GNUNET_CONTAINER_multipeermap_remove (all_routes,
749 send_disconnect_to_plugin (&neighbor->peer);
750 release_route (route);
754 neighbor->direct_route = GNUNET_new (struct Route);
755 neighbor->direct_route->next_hop = neighbor;
756 neighbor->direct_route->target.peer = neighbor->peer;
757 allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Adding direct route to %s\n",
761 GNUNET_i2s (&neighbor->direct_route->target.peer));
764 /* construct session ID seed as XOR of both peer's identities */
765 GNUNET_CRYPTO_hash (&my_identity,
766 sizeof (my_identity),
768 GNUNET_CRYPTO_hash (&neighbor->peer,
769 sizeof (struct GNUNET_PeerIdentity),
771 GNUNET_CRYPTO_hash_xor (&h1,
774 /* make sure session ID is unique across applications by salting it with 'DV' */
775 GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
776 GCRY_MD_SHA512, GCRY_MD_SHA256,
778 &session_id, sizeof (session_id),
780 if (0 < memcmp (&neighbor->peer,
782 sizeof (struct GNUNET_PeerIdentity)))
784 if (NULL != neighbor->listen_handle)
789 neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
794 if (NULL != neighbor->listen_handle)
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Starting SET listen operation with peer `%s'\n",
802 GNUNET_i2s (&neighbor->peer));
803 neighbor->listen_handle = GNUNET_SET_listen (cfg,
804 GNUNET_SET_OPERATION_UNION,
805 &neighbor->real_session_id,
814 * Method called whenever a peer connects.
817 * @param peer peer identity this notification is about
818 * @param mq message queue for sending data to @a peer
819 * @return our `struct DirectNeighbour` for this peer
822 handle_core_connect (void *cls,
823 const struct GNUNET_PeerIdentity *peer,
824 struct GNUNET_MQ_Handle *mq)
826 struct DirectNeighbor *neighbor;
828 /* Check for connect to self message */
829 if (0 == memcmp (&my_identity,
831 sizeof (struct GNUNET_PeerIdentity)))
833 /* check if entry exists */
834 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
836 if (NULL != neighbor)
838 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != neighbor->network);
839 GNUNET_break (GNUNET_YES != neighbor->connected);
840 neighbor->connected = GNUNET_YES;
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Core connected to %s (distance %u)\n",
844 (unsigned int) neighbor->distance);
845 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
847 handle_direct_connect (neighbor);
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Core connected to %s (distance unknown)\n",
853 neighbor = GNUNET_new (struct DirectNeighbor);
854 neighbor->peer = *peer;
855 GNUNET_assert (GNUNET_YES ==
856 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
860 neighbor->connected = GNUNET_YES;
861 neighbor->distance = 0; /* unknown */
862 neighbor->network = GNUNET_ATS_NET_UNSPECIFIED;
868 * Called for each 'target' in a neighbor table to free the associated memory.
871 * @param key key of the value
872 * @param value value to free
873 * @return #GNUNET_OK to continue to iterate
876 free_targets (void *cls,
877 const struct GNUNET_PeerIdentity *key,
886 * Add a new route to the given @a target via the given @a neighbor.
888 * @param target the target of the route
889 * @param neighbor the next hop for communicating with the @a target
892 add_new_route (struct Target *target,
893 struct DirectNeighbor *neighbor)
897 route = GNUNET_new (struct Route);
898 route->next_hop = neighbor;
899 route->target.peer = target->peer;
900 allocate_route (route, ntohl (target->distance) + 1);
901 GNUNET_assert (GNUNET_YES ==
902 GNUNET_CONTAINER_multipeermap_put (all_routes,
905 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
906 send_connect_to_plugin (&route->target.peer,
907 ntohl (route->target.distance),
913 * Multipeerhmap iterator for checking if a given route is
914 * (now) useful to this peer.
916 * @param cls the direct neighbor for the given route
917 * @param key key value stored under
918 * @param value a 'struct Target' that may or may not be useful; not that
919 * the distance in 'target' does not include the first hop yet
920 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
923 check_possible_route (void *cls,
924 const struct GNUNET_PeerIdentity *key,
927 struct DirectNeighbor *neighbor = cls;
928 struct Target *target = value;
932 GNUNET_CONTAINER_multipeermap_contains (direct_neighbors,
934 return GNUNET_YES; /* direct route, do not care about alternatives */
935 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
939 /* we have an existing route, check how it compares with going via 'target' */
940 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
942 /* via 'target' is cheaper than the existing route; switch to alternative route! */
943 move_route (route, ntohl (target->distance) + 1);
944 route->next_hop = neighbor;
945 send_distance_change_to_plugin (&target->peer,
946 ntohl (target->distance) + 1,
949 return GNUNET_YES; /* got a route to this target already */
951 if (ntohl (target->distance) >= DEFAULT_FISHEYE_DEPTH)
952 return GNUNET_YES; /* distance is too large to be interesting */
953 add_new_route (target, neighbor);
959 * Multipeermap iterator for finding routes that were previously
960 * "hidden" due to a better route (called after a disconnect event).
963 * @param key peer identity of the given direct neighbor
964 * @param value a `struct DirectNeighbor` to check for additional routes
965 * @return #GNUNET_YES to continue iteration
968 refresh_routes (void *cls,
969 const struct GNUNET_PeerIdentity *key,
972 struct DirectNeighbor *neighbor = value;
974 if ( (GNUNET_YES != neighbor->connected) ||
975 (DIRECT_NEIGHBOR_COST != neighbor->distance) )
977 if (NULL != neighbor->neighbor_table)
978 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
979 &check_possible_route,
986 * Task to run #refresh_routes() on all direct neighbours.
991 refresh_routes_task (void *cls)
994 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1001 * Asynchronously run #refresh_routes() at the next opportunity
1002 * on all direct neighbours.
1005 schedule_refresh_routes ()
1007 if (NULL == rr_task)
1008 rr_task = GNUNET_SCHEDULER_add_now (&refresh_routes_task,
1014 * Multipeermap iterator for freeing routes that go via a particular
1015 * neighbor that disconnected and is thus no longer available.
1017 * @param cls the direct neighbor that is now unavailable
1018 * @param key key value stored under
1019 * @param value a `struct Route` that may or may not go via neighbor
1021 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1024 cull_routes (void *cls,
1025 const struct GNUNET_PeerIdentity *key,
1028 struct DirectNeighbor *neighbor = cls;
1029 struct Route *route = value;
1031 if (route->next_hop != neighbor)
1032 return GNUNET_YES; /* not affected */
1033 GNUNET_assert (GNUNET_YES ==
1034 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1035 release_route (route);
1036 send_disconnect_to_plugin (&route->target.peer);
1037 GNUNET_free (route);
1043 * Handle the case that a direct connection to a peer is
1044 * disrupted. Remove all routes via that peer and
1045 * stop the consensus with it.
1047 * @param neighbor peer that was disconnected (or at least is no
1048 * longer at distance 1)
1051 handle_direct_disconnect (struct DirectNeighbor *neighbor)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Culling routes via %s due to direct disconnect\n",
1055 GNUNET_i2s (&neighbor->peer));
1056 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1059 if (NULL != neighbor->direct_route)
1061 release_route (neighbor->direct_route);
1062 GNUNET_free (neighbor->direct_route);
1063 neighbor->direct_route = NULL;
1065 if (NULL != neighbor->neighbor_table_consensus)
1067 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1070 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1071 neighbor->neighbor_table_consensus = NULL;
1073 if (NULL != neighbor->neighbor_table)
1075 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1078 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1079 neighbor->neighbor_table = NULL;
1081 if (NULL != neighbor->set_op)
1083 GNUNET_SET_operation_cancel (neighbor->set_op);
1084 neighbor->set_op = NULL;
1086 if (NULL != neighbor->my_set)
1088 GNUNET_SET_destroy (neighbor->my_set);
1089 neighbor->my_set = NULL;
1091 if (NULL != neighbor->listen_handle)
1093 GNUNET_SET_listen_cancel (neighbor->listen_handle);
1094 neighbor->listen_handle = NULL;
1096 if (NULL != neighbor->initiate_task)
1098 GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1099 neighbor->initiate_task = NULL;
1105 * Function that is called with QoS information about an address; used
1106 * to update our current distance to another peer.
1108 * @param cls closure
1109 * @param address the address
1110 * @param active #GNUNET_YES if this address is actively used
1111 * to maintain a connection to a peer;
1112 * #GNUNET_NO if the address is not actively used;
1113 * #GNUNET_SYSERR if this address is no longer available for ATS
1114 * @param bandwidth_out assigned outbound bandwidth for the connection
1115 * @param bandwidth_in assigned inbound bandwidth for the connection
1116 * @param prop performance data for the address (as far as known)
1119 handle_ats_update (void *cls,
1120 const struct GNUNET_HELLO_Address *address,
1122 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1123 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1124 const struct GNUNET_ATS_Properties *prop)
1126 struct DirectNeighbor *neighbor;
1128 enum GNUNET_ATS_Network_Type network;
1130 if (NULL == address)
1132 /* ATS service temporarily disconnected */
1136 if (GNUNET_YES != active)
1138 // FIXME: handle disconnect/inactive case too!
1141 distance = prop->distance;
1142 network = prop->scope;
1143 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
1144 /* check if entry exists */
1145 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1147 if (NULL != neighbor)
1149 neighbor->network = network;
1150 if (neighbor->distance == distance)
1151 return; /* nothing new to see here, move along */
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "ATS says distance to %s is now %u\n",
1154 GNUNET_i2s (&address->peer),
1155 (unsigned int) distance);
1156 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1157 (DIRECT_NEIGHBOR_COST == distance) )
1158 return; /* no change */
1159 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1161 neighbor->distance = distance;
1162 GNUNET_STATISTICS_update (stats,
1163 "# peers connected (1-hop)",
1165 handle_direct_disconnect (neighbor);
1166 schedule_refresh_routes ();
1169 neighbor->distance = distance;
1170 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1172 if (GNUNET_YES != neighbor->connected)
1174 handle_direct_connect (neighbor);
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "ATS says distance to %s is now %u\n",
1179 GNUNET_i2s (&address->peer),
1180 (unsigned int) distance);
1181 neighbor = GNUNET_new (struct DirectNeighbor);
1182 neighbor->peer = address->peer;
1183 GNUNET_assert (GNUNET_YES ==
1184 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1187 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1188 neighbor->connected = GNUNET_NO; /* not yet */
1189 neighbor->distance = distance;
1190 neighbor->network = network;
1195 * Check if a target was removed from the set of the other peer; if so,
1196 * if we also used it for our route, we need to remove it from our
1197 * 'all_routes' set (and later check if an alternative path now exists).
1199 * @param cls the `struct DirectNeighbor`
1200 * @param key peer identity for the target
1201 * @param value a `struct Target` previously reachable via the given neighbor
1204 check_target_removed (void *cls,
1205 const struct GNUNET_PeerIdentity *key,
1208 struct DirectNeighbor *neighbor = cls;
1209 struct Target *new_target;
1210 struct Route *current_route;
1212 new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
1214 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1216 if (NULL != new_target)
1218 /* target was in old set, is in new set */
1219 if ( (NULL != current_route) &&
1220 (current_route->next_hop == neighbor) &&
1221 (current_route->target.distance != new_target->distance) )
1223 /* need to recalculate routes due to distance change */
1224 neighbor->target_removed = GNUNET_YES;
1228 /* target was revoked, check if it was used */
1229 if ( (NULL == current_route) ||
1230 (current_route->next_hop != neighbor) )
1232 /* didn't matter, wasn't used */
1235 /* remove existing route */
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Lost route to %s\n",
1238 GNUNET_i2s (¤t_route->target.peer));
1239 GNUNET_assert (GNUNET_YES ==
1240 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
1241 send_disconnect_to_plugin (¤t_route->target.peer);
1242 release_route (current_route);
1243 GNUNET_free (current_route);
1244 neighbor->target_removed = GNUNET_YES;
1250 * Check if a target was added to the set of the other peer; if it
1251 * was added or impoves the existing route, do the needed updates.
1253 * @param cls the `struct DirectNeighbor`
1254 * @param key peer identity for the target
1255 * @param value a `struct Target` now reachable via the given neighbor
1258 check_target_added (void *cls,
1259 const struct GNUNET_PeerIdentity *key,
1262 struct DirectNeighbor *neighbor = cls;
1263 struct Target *target = value;
1264 struct Route *current_route;
1266 /* target was revoked, check if it was used */
1267 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1269 if (NULL != current_route)
1272 if (current_route->next_hop == neighbor)
1274 /* we had the same route before, no change in target */
1275 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1277 /* but distance changed! */
1278 if (ntohl (target->distance) + 1 > DEFAULT_FISHEYE_DEPTH)
1280 /* distance increased beyond what is allowed, kill route */
1281 GNUNET_assert (GNUNET_YES ==
1282 GNUNET_CONTAINER_multipeermap_remove (all_routes,
1285 send_disconnect_to_plugin (key);
1286 release_route (current_route);
1287 GNUNET_free (current_route);
1291 /* distance decreased, update route */
1292 move_route (current_route,
1293 ntohl (target->distance) + 1);
1294 send_distance_change_to_plugin (&target->peer,
1295 ntohl (target->distance) + 1,
1301 if (ntohl (current_route->target.distance) <= ntohl (target->distance) + 1)
1303 /* alternative, shorter route exists, ignore */
1306 /* new route is better than the existing one, take over! */
1307 /* NOTE: minor security issue: malicious peers may advertise
1308 very short routes to take over longer paths; as we don't
1309 check that the shorter routes actually work, a malicious
1310 direct neighbor can use this to DoS our long routes */
1312 move_route (current_route, ntohl (target->distance) + 1);
1313 current_route->next_hop = neighbor;
1314 send_distance_change_to_plugin (&target->peer,
1315 ntohl (target->distance) + 1,
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "Discovered new route to %s using %u hops\n",
1322 GNUNET_i2s (&target->peer),
1323 (unsigned int) (ntohl (target->distance) + 1));
1324 current_route = GNUNET_new (struct Route);
1325 current_route->next_hop = neighbor;
1326 current_route->target.peer = target->peer;
1327 allocate_route (current_route, ntohl (target->distance) + 1);
1328 GNUNET_assert (GNUNET_YES ==
1329 GNUNET_CONTAINER_multipeermap_put (all_routes,
1330 ¤t_route->target.peer,
1332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1334 send_connect_to_plugin (¤t_route->target.peer,
1335 ntohl (current_route->target.distance),
1342 * Callback for set operation results. Called for each element
1343 * in the result set.
1344 * We have learned a new route from the other peer. Add it to the
1345 * route set we're building.
1347 * @param cls the `struct DirectNeighbor` we're building the consensus with
1348 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
1349 * @param current_size current set size
1350 * @param status see `enum GNUNET_SET_Status`
1353 handle_set_union_result (void *cls,
1354 const struct GNUNET_SET_Element *element,
1355 uint64_t current_size,
1356 enum GNUNET_SET_Status status)
1358 struct DirectNeighbor *neighbor = cls;
1359 struct DirectNeighbor *dn;
1360 struct Target *target;
1361 const struct Target *ctarget;
1366 case GNUNET_SET_STATUS_OK:
1367 status_str = "GNUNET_SET_STATUS_OK";
1369 case GNUNET_SET_STATUS_FAILURE:
1370 status_str = "GNUNET_SET_STATUS_FAILURE";
1372 case GNUNET_SET_STATUS_HALF_DONE:
1373 status_str = "GNUNET_SET_STATUS_HALF_DONE";
1375 case GNUNET_SET_STATUS_DONE:
1376 status_str = "GNUNET_SET_STATUS_DONE";
1379 status_str = "UNDEFINED";
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Got SET union result: %s\n",
1388 case GNUNET_SET_STATUS_OK:
1389 if (sizeof (struct Target) != element->size)
1391 GNUNET_break_op (0);
1394 ctarget = element->data;
1396 (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1397 &ctarget->peer))) &&
1398 (DIRECT_NEIGHBOR_COST == dn->distance) )
1400 /* this is a direct neighbor of ours, we do not care about routes
1404 target = GNUNET_new (struct Target);
1405 GNUNET_memcpy (target, element->data, sizeof (struct Target));
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Received information about peer `%s' with distance %u from SET\n",
1408 GNUNET_i2s (&target->peer),
1409 ntohl (target->distance) + 1);
1411 if (NULL == neighbor->neighbor_table_consensus)
1412 neighbor->neighbor_table_consensus
1413 = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1415 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
1418 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1420 GNUNET_break_op (0);
1421 GNUNET_free (target);
1424 case GNUNET_SET_STATUS_FAILURE:
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1426 "Failed to establish DV union, will try again later\n");
1427 neighbor->set_op = NULL;
1428 if (NULL != neighbor->neighbor_table_consensus)
1430 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1433 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1434 neighbor->neighbor_table_consensus = NULL;
1436 if (0 < memcmp (&neighbor->peer,
1438 sizeof (struct GNUNET_PeerIdentity)))
1439 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1440 &initiate_set_union,
1443 case GNUNET_SET_STATUS_HALF_DONE:
1445 case GNUNET_SET_STATUS_DONE:
1446 /* we got all of our updates; integrate routing table! */
1447 neighbor->target_removed = GNUNET_NO;
1448 if (NULL == neighbor->neighbor_table_consensus)
1449 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1450 if (NULL != neighbor->neighbor_table)
1451 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1452 &check_target_removed,
1454 if (GNUNET_YES == neighbor->target_removed)
1456 /* check if we got an alternative for the removed routes */
1457 schedule_refresh_routes ();
1459 /* add targets that appeared (and check for improved routes) */
1460 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1461 &check_target_added,
1463 if (NULL != neighbor->neighbor_table)
1465 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1468 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1469 neighbor->neighbor_table = NULL;
1471 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1472 neighbor->neighbor_table_consensus = NULL;
1474 /* operation done, schedule next run! */
1475 neighbor->set_op = NULL;
1476 if (0 < memcmp (&neighbor->peer,
1478 sizeof (struct GNUNET_PeerIdentity)))
1479 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1480 &initiate_set_union,
1491 * Start creating a new DV set union construction, our neighbour has
1492 * asked for it (callback for listening peer).
1494 * @param cls the 'struct DirectNeighbor' of the peer we're building
1495 * a routing consensus with
1496 * @param other_peer the other peer
1497 * @param context_msg message with application specific information from
1499 * @param request request from the other peer, use GNUNET_SET_accept
1500 * to accept it, otherwise the request will be refused
1501 * Note that we don't use a return value here, as it is also
1502 * necessary to specify the set we want to do the operation with,
1503 * whith sometimes can be derived from the context message.
1504 * Also necessary to specify the timeout.
1507 listen_set_union (void *cls,
1508 const struct GNUNET_PeerIdentity *other_peer,
1509 const struct GNUNET_MessageHeader *context_msg,
1510 struct GNUNET_SET_Request *request)
1512 struct DirectNeighbor *neighbor = cls;
1514 if (NULL == request)
1515 return; /* why??? */
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Starting to create consensus with %s\n",
1518 GNUNET_i2s (&neighbor->peer));
1519 if (NULL != neighbor->set_op)
1521 GNUNET_SET_operation_cancel (neighbor->set_op);
1522 neighbor->set_op = NULL;
1524 if (NULL != neighbor->my_set)
1526 GNUNET_SET_destroy (neighbor->my_set);
1527 neighbor->my_set = NULL;
1529 neighbor->my_set = GNUNET_SET_create (cfg,
1530 GNUNET_SET_OPERATION_UNION);
1531 neighbor->set_op = GNUNET_SET_accept (request,
1532 GNUNET_SET_RESULT_ADDED,
1533 (struct GNUNET_SET_Option[]) { 0 },
1534 &handle_set_union_result,
1536 neighbor->consensus_insertion_offset = 0;
1537 neighbor->consensus_insertion_distance = 0;
1538 neighbor->consensus_elements = 0;
1539 build_set (neighbor);
1544 * Start creating a new DV set union by initiating the connection.
1546 * @param cls the `struct DirectNeighbor *` of the peer we're building
1547 * a routing consensus with
1550 initiate_set_union (void *cls)
1552 struct DirectNeighbor *neighbor = cls;
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "Initiating SET union with peer `%s'\n",
1556 GNUNET_i2s (&neighbor->peer));
1557 neighbor->initiate_task = NULL;
1558 neighbor->my_set = GNUNET_SET_create (cfg,
1559 GNUNET_SET_OPERATION_UNION);
1560 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1561 &neighbor->real_session_id,
1563 GNUNET_SET_RESULT_ADDED,
1564 (struct GNUNET_SET_Option[]) { 0 },
1565 &handle_set_union_result,
1567 neighbor->consensus_insertion_offset = 0;
1568 neighbor->consensus_insertion_distance = 0;
1569 neighbor->consensus_elements = 0;
1570 build_set (neighbor);
1575 * Check that @a rm is well-formed.
1577 * @param cls closure
1578 * @param rm the message
1579 * @return #GNUNET_OK if @a rm is well-formed.
1582 check_dv_route_message (void *cls,
1583 const struct RouteMessage *rm)
1585 const struct GNUNET_MessageHeader *payload;
1587 if (ntohs (rm->header.size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1589 GNUNET_break_op (0);
1590 return GNUNET_SYSERR;
1592 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1593 if (ntohs (rm->header.size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1595 GNUNET_break_op (0);
1596 return GNUNET_SYSERR;
1603 * Core handler for DV data messages. Whatever this message
1604 * contains all we really have to do is rip it out of its
1605 * DV layering and give it to our pal the DV plugin to report
1608 * @param cls closure
1609 * @param rm the message
1612 handle_dv_route_message (void *cls,
1613 const struct RouteMessage *rm)
1615 struct DirectNeighbor *neighbor = cls;
1616 const struct GNUNET_MessageHeader *payload;
1617 struct Route *route;
1618 struct DirectNeighbor *nneighbor;
1619 struct DirectNeighbor *dn;
1620 struct Target *target;
1627 distance = ntohl (rm->distance);
1628 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1629 strncpy (prev, GNUNET_i2s (&neighbor->peer), 4);
1630 strncpy (me, GNUNET_i2s (&my_identity), 4);
1631 strncpy (src, GNUNET_i2s (&rm->sender), 4);
1632 strncpy (dst, GNUNET_i2s (&rm->target), 4);
1633 prev[4] = me[4] = src[4] = dst[4] = '\0';
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
1636 (unsigned int) (ntohs (rm->header.size) - sizeof (struct RouteMessage)),
1637 ntohs (payload->type),
1642 (unsigned int) distance + 1);
1644 if (0 == memcmp (&rm->target,
1646 sizeof (struct GNUNET_PeerIdentity)))
1649 (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1651 (DIRECT_NEIGHBOR_COST == dn->distance))
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1654 "Discarding DV message, as %s is a direct neighbor\n",
1655 GNUNET_i2s (&rm->sender));
1656 GNUNET_STATISTICS_update (stats,
1657 "# messages discarded (direct neighbor)",
1661 /* message is for me, check reverse route! */
1662 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1664 if ( (NULL == route) &&
1665 (distance < DEFAULT_FISHEYE_DEPTH) )
1667 /* don't have reverse route yet, learn it! */
1668 target = GNUNET_new (struct Target);
1669 target->peer = rm->sender;
1670 target->distance = htonl (distance);
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 "Learning sender %s at distance %u from delivery!\n",
1673 GNUNET_i2s (&rm->sender),
1674 (unsigned int) distance + 1);
1675 if (NULL == neighbor->neighbor_table)
1676 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1678 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1681 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1683 GNUNET_break_op (0);
1684 GNUNET_free (target);
1687 add_new_route (target, neighbor);
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Delivering %u bytes from %s to myself!\n",
1691 ntohs (payload->size),
1692 GNUNET_i2s (&rm->sender));
1693 send_data_to_plugin (payload,
1698 if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1700 (NULL == GNUNET_CONTAINER_multipeermap_get (all_routes,
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Learning sender %s at distance %u from forwarding!\n",
1705 GNUNET_i2s (&rm->sender),
1707 target = GNUNET_new (struct Target);
1708 target->peer = rm->sender;
1709 target->distance = htonl (distance);
1710 if (NULL == neighbor->neighbor_table)
1711 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1713 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1716 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1718 GNUNET_break_op (0);
1719 GNUNET_free (target);
1722 add_new_route (target, neighbor);
1725 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1729 nneighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1731 if (NULL == nneighbor)
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "No route to %s, not routing %u bytes!\n",
1735 GNUNET_i2s (&rm->target),
1736 ntohs (payload->size));
1737 GNUNET_STATISTICS_update (stats,
1738 "# messages discarded (no route)",
1745 nneighbor = route->next_hop;
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "Forwarding message to %s\n",
1749 GNUNET_i2s (&nneighbor->peer));
1750 forward_payload (nneighbor,
1759 * Check that @a msg is well-formed
1761 * @param cls identification of the client
1762 * @param message the actual message
1763 * @return #GNUNET_OK if @a msg is well-formed
1766 check_dv_send_message (void *cls,
1767 const struct GNUNET_DV_SendMessage *msg)
1769 const struct GNUNET_MessageHeader *payload;
1771 if (ntohs (msg->header.size) < sizeof (struct GNUNET_DV_SendMessage) +
1772 sizeof (struct GNUNET_MessageHeader))
1775 return GNUNET_SYSERR;
1777 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1778 if (ntohs (msg->header.size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1781 return GNUNET_SYSERR;
1788 * Service server's handler for message send requests (which come
1789 * bubbling up to us through the DV plugin).
1791 * @param cls identification of the client
1792 * @param message the actual message
1795 handle_dv_send_message (void *cls,
1796 const struct GNUNET_DV_SendMessage *msg)
1798 struct GNUNET_SERVICE_Client *client = cls;
1799 struct Route *route;
1800 const struct GNUNET_MessageHeader *payload;
1802 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1803 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1807 /* got disconnected */
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "No route to %s, dropping local message of type %u\n",
1810 GNUNET_i2s (&msg->target),
1811 ntohs (payload->type));
1812 GNUNET_STATISTICS_update (stats,
1813 "# local messages discarded (no route)",
1815 GNUNET_SERVICE_client_continue (client);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "Forwarding %u bytes of type %u to %s\n",
1820 ntohs (payload->size),
1821 ntohs (payload->type),
1822 GNUNET_i2s (&msg->target));
1824 forward_payload (route->next_hop,
1825 0 /* first hop, distance is zero */,
1829 GNUNET_SERVICE_client_continue (client);
1834 * Cleanup all of the data structures associated with a given neighbor.
1836 * @param neighbor neighbor to clean up
1839 cleanup_neighbor (struct DirectNeighbor *neighbor)
1841 handle_direct_disconnect (neighbor);
1842 GNUNET_assert (GNUNET_YES ==
1843 GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1846 GNUNET_free (neighbor);
1851 * Method called whenever a given peer disconnects.
1853 * @param cls closure
1854 * @param peer peer identity this notification is about
1855 * @param internal_cls the corresponding `struct DirectNeighbor`
1858 handle_core_disconnect (void *cls,
1859 const struct GNUNET_PeerIdentity *peer,
1862 struct DirectNeighbor *neighbor = internal_cls;
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865 "Received core peer disconnect message for peer `%s'!\n",
1867 /* Check for disconnect from self message */
1868 if (NULL == neighbor)
1870 GNUNET_break (GNUNET_YES == neighbor->connected);
1871 neighbor->connected = GNUNET_NO;
1872 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1874 GNUNET_STATISTICS_update (stats,
1875 "# peers connected (1-hop)",
1879 cleanup_neighbor (neighbor);
1880 if (GNUNET_YES == in_shutdown)
1882 schedule_refresh_routes ();
1887 * Multipeermap iterator for freeing routes. Should never be called.
1890 * @param key key value stored under
1891 * @param value the route to be freed
1892 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1895 free_route (void *cls,
1896 const struct GNUNET_PeerIdentity *key,
1899 struct Route *route = value;
1902 GNUNET_assert (GNUNET_YES ==
1903 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1904 release_route (route);
1905 send_disconnect_to_plugin (&route->target.peer);
1906 GNUNET_free (route);
1912 * Multipeermap iterator for freeing direct neighbors. Should never be called.
1915 * @param key key value stored under
1916 * @param value the direct neighbor to be freed
1917 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1920 free_direct_neighbors (void *cls,
1921 const struct GNUNET_PeerIdentity *key,
1924 struct DirectNeighbor *neighbor = value;
1926 cleanup_neighbor (neighbor);
1932 * Task run during shutdown.
1937 shutdown_task (void *cls)
1941 in_shutdown = GNUNET_YES;
1942 GNUNET_assert (NULL != core_api);
1943 GNUNET_CORE_disconnect (core_api);
1945 GNUNET_ATS_performance_done (ats);
1947 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1948 &free_direct_neighbors,
1950 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1953 GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
1954 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1955 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1957 GNUNET_notification_context_destroy (nc);
1959 for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
1961 GNUNET_array_grow (consensi[i].targets,
1962 consensi[i].array_length,
1965 if (NULL != rr_task)
1967 GNUNET_SCHEDULER_cancel (rr_task);
1974 * Notify newly connected client about an existing route.
1976 * @param cls the `struct GNUNET_SERVICE_Client *`
1977 * @param key peer identity
1978 * @param value the `struct Route *`
1979 * @return #GNUNET_OK (continue to iterate)
1982 notify_client_about_route (void *cls,
1983 const struct GNUNET_PeerIdentity *key,
1986 struct GNUNET_SERVICE_Client *client = cls;
1987 struct Route *route = value;
1988 struct GNUNET_MQ_Envelope *env;
1989 struct GNUNET_DV_ConnectMessage *cm;
1991 env = GNUNET_MQ_msg (cm,
1992 GNUNET_MESSAGE_TYPE_DV_CONNECT);
1993 cm->distance = htonl (route->target.distance);
1994 cm->peer = route->target.peer;
1995 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
2002 * Handle START-message. This is the first message sent to us
2003 * by the client (can only be one!).
2005 * @param cls closure (always NULL)
2006 * @param client identification of the client
2007 * @param message the actual message
2010 handle_start (void *cls,
2011 const struct GNUNET_MessageHeader *message)
2013 struct GNUNET_SERVICE_Client *client = cls;
2015 GNUNET_notification_context_add (nc,
2016 GNUNET_SERVICE_client_get_mq (client));
2017 GNUNET_SERVICE_client_continue (client);
2018 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2019 ¬ify_client_about_route,
2025 * Called on core init.
2028 * @param identity this peer's identity
2031 core_init (void *cls,
2032 const struct GNUNET_PeerIdentity *identity)
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 GNUNET_i2s (identity));
2037 my_identity = *identity;
2042 * Process dv requests.
2044 * @param cls closure
2045 * @param c configuration to use
2046 * @param service the initialized service
2050 const struct GNUNET_CONFIGURATION_Handle *c,
2051 struct GNUNET_SERVICE_Handle *service)
2053 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2054 GNUNET_MQ_hd_var_size (dv_route_message,
2055 GNUNET_MESSAGE_TYPE_DV_ROUTE,
2056 struct RouteMessage,
2058 GNUNET_MQ_handler_end ()
2060 in_shutdown = GNUNET_NO;
2062 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128,
2064 all_routes = GNUNET_CONTAINER_multipeermap_create (65536,
2066 core_api = GNUNET_CORE_connect (cfg,
2069 &handle_core_connect,
2070 &handle_core_disconnect,
2073 if (NULL == core_api)
2075 ats = GNUNET_ATS_performance_init (cfg,
2080 GNUNET_CORE_disconnect (core_api);
2084 nc = GNUNET_notification_context_create (MAX_QUEUE_SIZE_PLUGIN);
2085 stats = GNUNET_STATISTICS_create ("dv",
2087 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2093 * Callback called when a client connects to the service.
2095 * @param cls closure for the service
2096 * @param c the new client that connected to the service
2097 * @param mq the message queue used to send messages to the client
2101 client_connect_cb (void *cls,
2102 struct GNUNET_SERVICE_Client *c,
2103 struct GNUNET_MQ_Handle *mq)
2110 * Callback called when a client disconnected from the service
2112 * @param cls closure for the service
2113 * @param c the client that disconnected
2114 * @param internal_cls should be equal to @a c
2117 client_disconnect_cb (void *cls,
2118 struct GNUNET_SERVICE_Client *c,
2121 GNUNET_assert (c == internal_cls);
2126 * Define "main" method using service macro.
2130 GNUNET_SERVICE_OPTION_NONE,
2133 &client_disconnect_cb,
2135 GNUNET_MQ_hd_fixed_size (start,
2136 GNUNET_MESSAGE_TYPE_DV_START,
2137 struct GNUNET_MessageHeader,
2139 GNUNET_MQ_hd_var_size (dv_send_message,
2140 GNUNET_MESSAGE_TYPE_DV_SEND,
2141 struct GNUNET_DV_SendMessage,
2143 GNUNET_MQ_handler_end ());
2146 /* end of gnunet-service-dv.c */