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 const 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_SERVER_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_SERVER_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_SERVER_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->cth)
1061 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1062 neighbor->cth = NULL;
1065 if (NULL != neighbor->direct_route)
1067 release_route (neighbor->direct_route);
1068 GNUNET_free (neighbor->direct_route);
1069 neighbor->direct_route = NULL;
1072 if (NULL != neighbor->neighbor_table_consensus)
1074 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1077 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1078 neighbor->neighbor_table_consensus = NULL;
1080 if (NULL != neighbor->neighbor_table)
1082 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1085 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1086 neighbor->neighbor_table = NULL;
1088 if (NULL != neighbor->set_op)
1090 GNUNET_SET_operation_cancel (neighbor->set_op);
1091 neighbor->set_op = NULL;
1093 if (NULL != neighbor->my_set)
1095 GNUNET_SET_destroy (neighbor->my_set);
1096 neighbor->my_set = NULL;
1098 if (NULL != neighbor->listen_handle)
1100 GNUNET_SET_listen_cancel (neighbor->listen_handle);
1101 neighbor->listen_handle = NULL;
1103 if (NULL != neighbor->initiate_task)
1105 GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1106 neighbor->initiate_task = NULL;
1112 * Function that is called with QoS information about an address; used
1113 * to update our current distance to another peer.
1115 * @param cls closure
1116 * @param address the address
1117 * @param active #GNUNET_YES if this address is actively used
1118 * to maintain a connection to a peer;
1119 * #GNUNET_NO if the address is not actively used;
1120 * #GNUNET_SYSERR if this address is no longer available for ATS
1121 * @param bandwidth_out assigned outbound bandwidth for the connection
1122 * @param bandwidth_in assigned inbound bandwidth for the connection
1123 * @param prop performance data for the address (as far as known)
1126 handle_ats_update (void *cls,
1127 const struct GNUNET_HELLO_Address *address,
1129 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1130 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1131 const struct GNUNET_ATS_Properties *prop)
1133 struct DirectNeighbor *neighbor;
1135 enum GNUNET_ATS_Network_Type network;
1137 if (NULL == address)
1139 /* ATS service temporarily disconnected */
1143 if (GNUNET_YES != active)
1145 // FIXME: handle disconnect/inactive case too!
1148 distance = prop->distance;
1149 network = prop->scope;
1150 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
1151 /* check if entry exists */
1152 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1154 if (NULL != neighbor)
1156 neighbor->network = network;
1157 if (neighbor->distance == distance)
1158 return; /* nothing new to see here, move along */
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "ATS says distance to %s is now %u\n",
1161 GNUNET_i2s (&address->peer),
1162 (unsigned int) distance);
1163 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1164 (DIRECT_NEIGHBOR_COST == distance) )
1165 return; /* no change */
1166 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1168 neighbor->distance = distance;
1169 GNUNET_STATISTICS_update (stats,
1170 "# peers connected (1-hop)",
1172 handle_direct_disconnect (neighbor);
1173 schedule_refresh_routes ();
1176 neighbor->distance = distance;
1177 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1179 if (GNUNET_YES != neighbor->connected)
1181 handle_direct_connect (neighbor);
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "ATS says distance to %s is now %u\n",
1186 GNUNET_i2s (&address->peer),
1187 (unsigned int) distance);
1188 neighbor = GNUNET_new (struct DirectNeighbor);
1189 neighbor->peer = address->peer;
1190 GNUNET_assert (GNUNET_YES ==
1191 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1195 neighbor->connected = GNUNET_NO; /* not yet */
1196 neighbor->distance = distance;
1197 neighbor->network = network;
1202 * Check if a target was removed from the set of the other peer; if so,
1203 * if we also used it for our route, we need to remove it from our
1204 * 'all_routes' set (and later check if an alternative path now exists).
1206 * @param cls the `struct DirectNeighbor`
1207 * @param key peer identity for the target
1208 * @param value a `struct Target` previously reachable via the given neighbor
1211 check_target_removed (void *cls,
1212 const struct GNUNET_PeerIdentity *key,
1215 struct DirectNeighbor *neighbor = cls;
1216 struct Target *new_target;
1217 struct Route *current_route;
1219 new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
1221 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1223 if (NULL != new_target)
1225 /* target was in old set, is in new set */
1226 if ( (NULL != current_route) &&
1227 (current_route->next_hop == neighbor) &&
1228 (current_route->target.distance != new_target->distance) )
1230 /* need to recalculate routes due to distance change */
1231 neighbor->target_removed = GNUNET_YES;
1235 /* target was revoked, check if it was used */
1236 if ( (NULL == current_route) ||
1237 (current_route->next_hop != neighbor) )
1239 /* didn't matter, wasn't used */
1242 /* remove existing route */
1243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1244 "Lost route to %s\n",
1245 GNUNET_i2s (¤t_route->target.peer));
1246 GNUNET_assert (GNUNET_YES ==
1247 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
1248 send_disconnect_to_plugin (¤t_route->target.peer);
1249 release_route (current_route);
1250 GNUNET_free (current_route);
1251 neighbor->target_removed = GNUNET_YES;
1257 * Check if a target was added to the set of the other peer; if it
1258 * was added or impoves the existing route, do the needed updates.
1260 * @param cls the `struct DirectNeighbor`
1261 * @param key peer identity for the target
1262 * @param value a `struct Target` now reachable via the given neighbor
1265 check_target_added (void *cls,
1266 const struct GNUNET_PeerIdentity *key,
1269 struct DirectNeighbor *neighbor = cls;
1270 struct Target *target = value;
1271 struct Route *current_route;
1273 /* target was revoked, check if it was used */
1274 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1276 if (NULL != current_route)
1279 if (current_route->next_hop == neighbor)
1281 /* we had the same route before, no change in target */
1282 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1284 /* but distance changed! */
1285 if (ntohl (target->distance) + 1 > DEFAULT_FISHEYE_DEPTH)
1287 /* distance increased beyond what is allowed, kill route */
1288 GNUNET_assert (GNUNET_YES ==
1289 GNUNET_CONTAINER_multipeermap_remove (all_routes,
1292 send_disconnect_to_plugin (key);
1293 release_route (current_route);
1294 GNUNET_free (current_route);
1298 /* distance decreased, update route */
1299 move_route (current_route,
1300 ntohl (target->distance) + 1);
1301 send_distance_change_to_plugin (&target->peer,
1302 ntohl (target->distance) + 1,
1308 if (ntohl (current_route->target.distance) <= ntohl (target->distance) + 1)
1310 /* alternative, shorter route exists, ignore */
1313 /* new route is better than the existing one, take over! */
1314 /* NOTE: minor security issue: malicious peers may advertise
1315 very short routes to take over longer paths; as we don't
1316 check that the shorter routes actually work, a malicious
1317 direct neighbor can use this to DoS our long routes */
1319 move_route (current_route, ntohl (target->distance) + 1);
1320 current_route->next_hop = neighbor;
1321 send_distance_change_to_plugin (&target->peer,
1322 ntohl (target->distance) + 1,
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 "Discovered new route to %s using %u hops\n",
1329 GNUNET_i2s (&target->peer),
1330 (unsigned int) (ntohl (target->distance) + 1));
1331 current_route = GNUNET_new (struct Route);
1332 current_route->next_hop = neighbor;
1333 current_route->target.peer = target->peer;
1334 allocate_route (current_route, ntohl (target->distance) + 1);
1335 GNUNET_assert (GNUNET_YES ==
1336 GNUNET_CONTAINER_multipeermap_put (all_routes,
1337 ¤t_route->target.peer,
1339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1341 send_connect_to_plugin (¤t_route->target.peer,
1342 ntohl (current_route->target.distance),
1349 * Callback for set operation results. Called for each element
1350 * in the result set.
1351 * We have learned a new route from the other peer. Add it to the
1352 * route set we're building.
1354 * @param cls the `struct DirectNeighbor` we're building the consensus with
1355 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
1356 * @param status see `enum GNUNET_SET_Status`
1359 handle_set_union_result (void *cls,
1360 const struct GNUNET_SET_Element *element,
1361 enum GNUNET_SET_Status status)
1363 struct DirectNeighbor *neighbor = cls;
1364 struct DirectNeighbor *dn;
1365 struct Target *target;
1370 case GNUNET_SET_STATUS_OK:
1371 status_str = "GNUNET_SET_STATUS_OK";
1373 case GNUNET_SET_STATUS_FAILURE:
1374 status_str = "GNUNET_SET_STATUS_FAILURE";
1376 case GNUNET_SET_STATUS_HALF_DONE:
1377 status_str = "GNUNET_SET_STATUS_HALF_DONE";
1379 case GNUNET_SET_STATUS_DONE:
1380 status_str = "GNUNET_SET_STATUS_DONE";
1383 status_str = "UNDEFINED";
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Got SET union result: %s\n",
1392 case GNUNET_SET_STATUS_OK:
1393 if (sizeof (struct Target) != element->size)
1395 GNUNET_break_op (0);
1398 if ( (NULL != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors, &((struct Target *) element->data)->peer))) && (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 &handle_set_union_result,
1535 neighbor->consensus_insertion_offset = 0;
1536 neighbor->consensus_insertion_distance = 0;
1537 neighbor->consensus_elements = 0;
1538 build_set (neighbor);
1543 * Start creating a new DV set union by initiating the connection.
1545 * @param cls the `struct DirectNeighbor *` of the peer we're building
1546 * a routing consensus with
1549 initiate_set_union (void *cls)
1551 struct DirectNeighbor *neighbor = cls;
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "Initiating SET union with peer `%s'\n",
1555 GNUNET_i2s (&neighbor->peer));
1556 neighbor->initiate_task = NULL;
1557 neighbor->my_set = GNUNET_SET_create (cfg,
1558 GNUNET_SET_OPERATION_UNION);
1559 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1560 &neighbor->real_session_id,
1562 GNUNET_SET_RESULT_ADDED,
1563 &handle_set_union_result,
1565 neighbor->consensus_insertion_offset = 0;
1566 neighbor->consensus_insertion_distance = 0;
1567 neighbor->consensus_elements = 0;
1568 build_set (neighbor);
1573 * Check that @a rm is well-formed.
1575 * @param cls closure
1576 * @param rm the message
1577 * @return #GNUNET_OK if @a rm is well-formed.
1580 check_dv_route_message (void *cls,
1581 const struct RouteMessage *rm)
1583 const struct GNUNET_MessageHeader *payload;
1585 if (ntohs (rm->header.size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1587 GNUNET_break_op (0);
1588 return GNUNET_SYSERR;
1590 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1591 if (ntohs (rm->header.size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1593 GNUNET_break_op (0);
1594 return GNUNET_SYSERR;
1601 * Core handler for DV data messages. Whatever this message
1602 * contains all we really have to do is rip it out of its
1603 * DV layering and give it to our pal the DV plugin to report
1606 * @param cls closure
1607 * @param rm the message
1610 handle_dv_route_message (void *cls,
1611 const struct RouteMessage *rm)
1613 const struct GNUNET_MessageHeader *payload;
1614 struct Route *route;
1615 struct DirectNeighbor *neighbor;
1616 struct DirectNeighbor *dn;
1617 struct Target *target;
1624 distance = ntohl (rm->distance);
1625 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1626 strncpy (prev, GNUNET_i2s (peer), 4);
1627 strncpy (me, GNUNET_i2s (&my_identity), 4);
1628 strncpy (src, GNUNET_i2s (&rm->sender), 4);
1629 strncpy (dst, GNUNET_i2s (&rm->target), 4);
1630 prev[4] = me[4] = src[4] = dst[4] = '\0';
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1632 "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
1633 (unsigned int) (ntohs (rm->header.size) - sizeof (struct RouteMessage)),
1634 ntohs (payload->type),
1639 (unsigned int) distance + 1);
1641 if (0 == memcmp (&rm->target,
1643 sizeof (struct GNUNET_PeerIdentity)))
1646 (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1648 (DIRECT_NEIGHBOR_COST == dn->distance))
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Discarding DV message, as %s is a direct neighbor\n",
1652 GNUNET_i2s (&rm->sender));
1653 GNUNET_STATISTICS_update (stats,
1654 "# messages discarded (direct neighbor)",
1658 /* message is for me, check reverse route! */
1659 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1661 if ( (NULL == route) &&
1662 (distance < DEFAULT_FISHEYE_DEPTH) )
1664 /* don't have reverse route yet, learn it! */
1665 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1667 if (NULL == neighbor)
1672 target = GNUNET_new (struct Target);
1673 target->peer = rm->sender;
1674 target->distance = htonl (distance);
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Learning sender %s at distance %u from delivery!\n",
1677 GNUNET_i2s (&rm->sender),
1678 (unsigned int) distance + 1);
1679 if (NULL == neighbor->neighbor_table)
1680 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1682 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1687 GNUNET_break_op (0);
1688 GNUNET_free (target);
1691 add_new_route (target, neighbor);
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 "Delivering %u bytes from %s to myself!\n",
1695 ntohs (payload->size),
1696 GNUNET_i2s (&rm->sender));
1697 send_data_to_plugin (payload,
1702 if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1704 (NULL == GNUNET_CONTAINER_multipeermap_get (all_routes,
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "Learning sender %s at distance %u from forwarding!\n",
1709 GNUNET_i2s (&rm->sender),
1711 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1713 if (NULL == neighbor)
1718 target = GNUNET_new (struct Target);
1719 target->peer = rm->sender;
1720 target->distance = htonl (distance);
1721 if (NULL == neighbor->neighbor_table)
1722 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1724 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1729 GNUNET_break_op (0);
1730 GNUNET_free (target);
1733 add_new_route (target, neighbor);
1736 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1740 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1742 if (NULL == neighbor)
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "No route to %s, not routing %u bytes!\n",
1746 GNUNET_i2s (&rm->target),
1747 ntohs (payload->size));
1748 GNUNET_STATISTICS_update (stats,
1749 "# messages discarded (no route)",
1756 neighbor = route->next_hop;
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Forwarding message to %s\n",
1760 GNUNET_i2s (neighbor->peer));
1761 forward_payload (neighbor,
1770 * Service server's handler for message send requests (which come
1771 * bubbling up to us through the DV plugin).
1773 * @param cls closure
1774 * @param client identification of the client
1775 * @param message the actual message
1778 handle_dv_send_message (void *cls,
1779 struct GNUNET_SERVER_Client *client,
1780 const struct GNUNET_MessageHeader *message)
1782 struct Route *route;
1783 const struct GNUNET_DV_SendMessage *msg;
1784 const struct GNUNET_MessageHeader *payload;
1786 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1792 msg = (const struct GNUNET_DV_SendMessage *) message;
1793 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1794 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1800 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1804 /* got disconnected */
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 "No route to %s, dropping local message of type %u\n",
1807 GNUNET_i2s (&msg->target),
1808 ntohs (payload->type));
1809 GNUNET_STATISTICS_update (stats,
1810 "# local messages discarded (no route)",
1812 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "Forwarding %u bytes of type %u to %s\n",
1817 ntohs (payload->size),
1818 ntohs (payload->type),
1819 GNUNET_i2s (&msg->target));
1821 forward_payload (route->next_hop,
1822 0 /* first hop, distance is zero */,
1826 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1831 * Cleanup all of the data structures associated with a given neighbor.
1833 * @param neighbor neighbor to clean up
1836 cleanup_neighbor (struct DirectNeighbor *neighbor)
1838 handle_direct_disconnect (neighbor);
1839 GNUNET_assert (GNUNET_YES ==
1840 GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1843 GNUNET_free (neighbor);
1848 * Method called whenever a given peer disconnects.
1850 * @param cls closure
1851 * @param peer peer identity this notification is about
1852 * @param internal_cls the corresponding `struct DirectNeighbor`
1855 handle_core_disconnect (void *cls,
1856 const struct GNUNET_PeerIdentity *peer,
1859 struct DirectNeighbor *neighbor = internal_cls;
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Received core peer disconnect message for peer `%s'!\n",
1864 /* Check for disconnect from self message */
1865 if (NULL == neighbor)
1867 GNUNET_break (GNUNET_YES == neighbor->connected);
1868 neighbor->connected = GNUNET_NO;
1869 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1871 GNUNET_STATISTICS_update (stats,
1872 "# peers connected (1-hop)",
1876 cleanup_neighbor (neighbor);
1877 if (GNUNET_YES == in_shutdown)
1879 schedule_refresh_routes ();
1884 * Multipeermap iterator for freeing routes. Should never be called.
1887 * @param key key value stored under
1888 * @param value the route to be freed
1889 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1892 free_route (void *cls,
1893 const struct GNUNET_PeerIdentity *key,
1896 struct Route *route = value;
1899 GNUNET_assert (GNUNET_YES ==
1900 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1901 release_route (route);
1902 send_disconnect_to_plugin (&route->target.peer);
1903 GNUNET_free (route);
1909 * Multipeermap iterator for freeing direct neighbors. Should never be called.
1912 * @param key key value stored under
1913 * @param value the direct neighbor to be freed
1914 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1917 free_direct_neighbors (void *cls,
1918 const struct GNUNET_PeerIdentity *key,
1921 struct DirectNeighbor *neighbor = value;
1923 cleanup_neighbor (neighbor);
1929 * Task run during shutdown.
1934 shutdown_task (void *cls)
1938 in_shutdown = GNUNET_YES;
1939 GNUNET_assert (NULL != core_api);
1940 GNUNET_CORE_disconnecT (core_api);
1942 GNUNET_ATS_performance_done (ats);
1944 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1945 &free_direct_neighbors,
1947 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1950 GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
1951 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1952 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1954 GNUNET_SERVER_notification_context_destroy (nc);
1956 for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
1958 GNUNET_array_grow (consensi[i].targets,
1959 consensi[i].array_length,
1962 if (NULL != rr_task)
1964 GNUNET_SCHEDULER_cancel (rr_task);
1971 * Notify newly connected client about an existing route.
1973 * @param cls the `struct GNUNET_SERVER_Client *`
1974 * @param key peer identity
1975 * @param value the `struct Route *`
1976 * @return #GNUNET_OK (continue to iterate)
1979 notify_client_about_route (void *cls,
1980 const struct GNUNET_PeerIdentity *key,
1983 struct GNUNET_SERVER_Client *client = cls;
1984 struct Route *route = value;
1985 struct GNUNET_DV_ConnectMessage cm;
1987 memset (&cm, 0, sizeof (cm));
1988 cm.header.size = htons (sizeof (cm));
1989 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1990 cm.distance = htonl (route->target.distance);
1991 cm.peer = route->target.peer;
1993 GNUNET_SERVER_notification_context_unicast (nc,
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 struct GNUNET_SERVER_Client *client,
2012 const struct GNUNET_MessageHeader *message)
2014 GNUNET_SERVER_notification_context_add (nc, client);
2015 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2016 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2017 ¬ify_client_about_route,
2023 * Called on core init.
2026 * @param identity this peer's identity
2029 core_init (void *cls,
2030 const struct GNUNET_PeerIdentity *identity)
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 GNUNET_i2s (identity));
2035 my_identity = *identity;
2040 * Process dv requests.
2042 * @param cls closure
2043 * @param server the initialized server
2044 * @param c configuration to use
2048 struct GNUNET_SERVER_Handle *server,
2049 const struct GNUNET_CONFIGURATION_Handle *c)
2051 GNUNET_MQ_hd_var_size (dv_route_message,
2052 GNUNET_MESSAGE_TYPE_DV_ROUTE,
2053 struct RouteMessage);
2054 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2055 make_dv_route_message_handler (NULL),
2056 GNUNET_MQ_handler_end ()
2058 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2059 {&handle_start, NULL,
2060 GNUNET_MESSAGE_TYPE_DV_START,
2061 sizeof (struct GNUNET_MessageHeader) },
2062 { &handle_dv_send_message, NULL,
2063 GNUNET_MESSAGE_TYPE_DV_SEND,
2067 in_shutdown = GNUNET_NO;
2069 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128,
2071 all_routes = GNUNET_CONTAINER_multipeermap_create (65536,
2073 core_api = GNUNET_CORE_connecT (cfg,
2076 &handle_core_connect,
2077 &handle_core_disconnect,
2080 if (NULL == core_api)
2082 ats = GNUNET_ATS_performance_init (cfg,
2087 GNUNET_CORE_disconnecT (core_api);
2091 nc = GNUNET_SERVER_notification_context_create (server,
2092 MAX_QUEUE_SIZE_PLUGIN);
2093 stats = GNUNET_STATISTICS_create ("dv",
2095 GNUNET_SERVER_add_handlers (server,
2097 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2103 * The main function for the dv service.
2105 * @param argc number of arguments from the command line
2106 * @param argv command line arguments
2107 * @return 0 ok, 1 on error
2113 return (GNUNET_OK ==
2114 GNUNET_SERVICE_run (argc,
2117 GNUNET_SERVICE_OPTION_NONE,
2122 /* end of gnunet-service-dv.c */