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_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->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 status see `enum GNUNET_SET_Status`
1352 handle_set_union_result (void *cls,
1353 const struct GNUNET_SET_Element *element,
1354 enum GNUNET_SET_Status status)
1356 struct DirectNeighbor *neighbor = cls;
1357 struct DirectNeighbor *dn;
1358 struct Target *target;
1359 const struct Target *ctarget;
1364 case GNUNET_SET_STATUS_OK:
1365 status_str = "GNUNET_SET_STATUS_OK";
1367 case GNUNET_SET_STATUS_FAILURE:
1368 status_str = "GNUNET_SET_STATUS_FAILURE";
1370 case GNUNET_SET_STATUS_HALF_DONE:
1371 status_str = "GNUNET_SET_STATUS_HALF_DONE";
1373 case GNUNET_SET_STATUS_DONE:
1374 status_str = "GNUNET_SET_STATUS_DONE";
1377 status_str = "UNDEFINED";
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Got SET union result: %s\n",
1386 case GNUNET_SET_STATUS_OK:
1387 if (sizeof (struct Target) != element->size)
1389 GNUNET_break_op (0);
1392 ctarget = element->data;
1394 (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1395 &ctarget->peer))) &&
1396 (DIRECT_NEIGHBOR_COST == dn->distance) )
1398 /* this is a direct neighbor of ours, we do not care about routes
1402 target = GNUNET_new (struct Target);
1403 GNUNET_memcpy (target, element->data, sizeof (struct Target));
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Received information about peer `%s' with distance %u from SET\n",
1406 GNUNET_i2s (&target->peer),
1407 ntohl (target->distance) + 1);
1409 if (NULL == neighbor->neighbor_table_consensus)
1410 neighbor->neighbor_table_consensus
1411 = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1413 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
1416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1418 GNUNET_break_op (0);
1419 GNUNET_free (target);
1422 case GNUNET_SET_STATUS_FAILURE:
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Failed to establish DV union, will try again later\n");
1425 neighbor->set_op = NULL;
1426 if (NULL != neighbor->neighbor_table_consensus)
1428 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1431 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1432 neighbor->neighbor_table_consensus = NULL;
1434 if (0 < memcmp (&neighbor->peer,
1436 sizeof (struct GNUNET_PeerIdentity)))
1437 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1438 &initiate_set_union,
1441 case GNUNET_SET_STATUS_HALF_DONE:
1443 case GNUNET_SET_STATUS_DONE:
1444 /* we got all of our updates; integrate routing table! */
1445 neighbor->target_removed = GNUNET_NO;
1446 if (NULL == neighbor->neighbor_table_consensus)
1447 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1448 if (NULL != neighbor->neighbor_table)
1449 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1450 &check_target_removed,
1452 if (GNUNET_YES == neighbor->target_removed)
1454 /* check if we got an alternative for the removed routes */
1455 schedule_refresh_routes ();
1457 /* add targets that appeared (and check for improved routes) */
1458 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1459 &check_target_added,
1461 if (NULL != neighbor->neighbor_table)
1463 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1466 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1467 neighbor->neighbor_table = NULL;
1469 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1470 neighbor->neighbor_table_consensus = NULL;
1472 /* operation done, schedule next run! */
1473 neighbor->set_op = NULL;
1474 if (0 < memcmp (&neighbor->peer,
1476 sizeof (struct GNUNET_PeerIdentity)))
1477 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1478 &initiate_set_union,
1489 * Start creating a new DV set union construction, our neighbour has
1490 * asked for it (callback for listening peer).
1492 * @param cls the 'struct DirectNeighbor' of the peer we're building
1493 * a routing consensus with
1494 * @param other_peer the other peer
1495 * @param context_msg message with application specific information from
1497 * @param request request from the other peer, use GNUNET_SET_accept
1498 * to accept it, otherwise the request will be refused
1499 * Note that we don't use a return value here, as it is also
1500 * necessary to specify the set we want to do the operation with,
1501 * whith sometimes can be derived from the context message.
1502 * Also necessary to specify the timeout.
1505 listen_set_union (void *cls,
1506 const struct GNUNET_PeerIdentity *other_peer,
1507 const struct GNUNET_MessageHeader *context_msg,
1508 struct GNUNET_SET_Request *request)
1510 struct DirectNeighbor *neighbor = cls;
1512 if (NULL == request)
1513 return; /* why??? */
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Starting to create consensus with %s\n",
1516 GNUNET_i2s (&neighbor->peer));
1517 if (NULL != neighbor->set_op)
1519 GNUNET_SET_operation_cancel (neighbor->set_op);
1520 neighbor->set_op = NULL;
1522 if (NULL != neighbor->my_set)
1524 GNUNET_SET_destroy (neighbor->my_set);
1525 neighbor->my_set = NULL;
1527 neighbor->my_set = GNUNET_SET_create (cfg,
1528 GNUNET_SET_OPERATION_UNION);
1529 neighbor->set_op = GNUNET_SET_accept (request,
1530 GNUNET_SET_RESULT_ADDED,
1531 &handle_set_union_result,
1533 neighbor->consensus_insertion_offset = 0;
1534 neighbor->consensus_insertion_distance = 0;
1535 neighbor->consensus_elements = 0;
1536 build_set (neighbor);
1541 * Start creating a new DV set union by initiating the connection.
1543 * @param cls the `struct DirectNeighbor *` of the peer we're building
1544 * a routing consensus with
1547 initiate_set_union (void *cls)
1549 struct DirectNeighbor *neighbor = cls;
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Initiating SET union with peer `%s'\n",
1553 GNUNET_i2s (&neighbor->peer));
1554 neighbor->initiate_task = NULL;
1555 neighbor->my_set = GNUNET_SET_create (cfg,
1556 GNUNET_SET_OPERATION_UNION);
1557 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1558 &neighbor->real_session_id,
1560 GNUNET_SET_RESULT_ADDED,
1561 &handle_set_union_result,
1563 neighbor->consensus_insertion_offset = 0;
1564 neighbor->consensus_insertion_distance = 0;
1565 neighbor->consensus_elements = 0;
1566 build_set (neighbor);
1571 * Check that @a rm is well-formed.
1573 * @param cls closure
1574 * @param rm the message
1575 * @return #GNUNET_OK if @a rm is well-formed.
1578 check_dv_route_message (void *cls,
1579 const struct RouteMessage *rm)
1581 const struct GNUNET_MessageHeader *payload;
1583 if (ntohs (rm->header.size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1585 GNUNET_break_op (0);
1586 return GNUNET_SYSERR;
1588 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1589 if (ntohs (rm->header.size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1591 GNUNET_break_op (0);
1592 return GNUNET_SYSERR;
1599 * Core handler for DV data messages. Whatever this message
1600 * contains all we really have to do is rip it out of its
1601 * DV layering and give it to our pal the DV plugin to report
1604 * @param cls closure
1605 * @param rm the message
1608 handle_dv_route_message (void *cls,
1609 const struct RouteMessage *rm)
1611 struct DirectNeighbor *neighbor = cls;
1612 const struct GNUNET_MessageHeader *payload;
1613 struct Route *route;
1614 struct DirectNeighbor *nneighbor;
1615 struct DirectNeighbor *dn;
1616 struct Target *target;
1623 distance = ntohl (rm->distance);
1624 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1625 strncpy (prev, GNUNET_i2s (&neighbor->peer), 4);
1626 strncpy (me, GNUNET_i2s (&my_identity), 4);
1627 strncpy (src, GNUNET_i2s (&rm->sender), 4);
1628 strncpy (dst, GNUNET_i2s (&rm->target), 4);
1629 prev[4] = me[4] = src[4] = dst[4] = '\0';
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
1632 (unsigned int) (ntohs (rm->header.size) - sizeof (struct RouteMessage)),
1633 ntohs (payload->type),
1638 (unsigned int) distance + 1);
1640 if (0 == memcmp (&rm->target,
1642 sizeof (struct GNUNET_PeerIdentity)))
1645 (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1647 (DIRECT_NEIGHBOR_COST == dn->distance))
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "Discarding DV message, as %s is a direct neighbor\n",
1651 GNUNET_i2s (&rm->sender));
1652 GNUNET_STATISTICS_update (stats,
1653 "# messages discarded (direct neighbor)",
1657 /* message is for me, check reverse route! */
1658 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1660 if ( (NULL == route) &&
1661 (distance < DEFAULT_FISHEYE_DEPTH) )
1663 /* don't have reverse route yet, learn it! */
1664 target = GNUNET_new (struct Target);
1665 target->peer = rm->sender;
1666 target->distance = htonl (distance);
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Learning sender %s at distance %u from delivery!\n",
1669 GNUNET_i2s (&rm->sender),
1670 (unsigned int) distance + 1);
1671 if (NULL == neighbor->neighbor_table)
1672 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1674 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1679 GNUNET_break_op (0);
1680 GNUNET_free (target);
1683 add_new_route (target, neighbor);
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1686 "Delivering %u bytes from %s to myself!\n",
1687 ntohs (payload->size),
1688 GNUNET_i2s (&rm->sender));
1689 send_data_to_plugin (payload,
1694 if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1696 (NULL == GNUNET_CONTAINER_multipeermap_get (all_routes,
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 "Learning sender %s at distance %u from forwarding!\n",
1701 GNUNET_i2s (&rm->sender),
1703 target = GNUNET_new (struct Target);
1704 target->peer = rm->sender;
1705 target->distance = htonl (distance);
1706 if (NULL == neighbor->neighbor_table)
1707 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1709 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1714 GNUNET_break_op (0);
1715 GNUNET_free (target);
1718 add_new_route (target, neighbor);
1721 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1725 nneighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1727 if (NULL == nneighbor)
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "No route to %s, not routing %u bytes!\n",
1731 GNUNET_i2s (&rm->target),
1732 ntohs (payload->size));
1733 GNUNET_STATISTICS_update (stats,
1734 "# messages discarded (no route)",
1741 nneighbor = route->next_hop;
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Forwarding message to %s\n",
1745 GNUNET_i2s (&nneighbor->peer));
1746 forward_payload (nneighbor,
1755 * Service server's handler for message send requests (which come
1756 * bubbling up to us through the DV plugin).
1758 * @param cls closure
1759 * @param client identification of the client
1760 * @param message the actual message
1763 handle_dv_send_message (void *cls,
1764 struct GNUNET_SERVER_Client *client,
1765 const struct GNUNET_MessageHeader *message)
1767 struct Route *route;
1768 const struct GNUNET_DV_SendMessage *msg;
1769 const struct GNUNET_MessageHeader *payload;
1771 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1777 msg = (const struct GNUNET_DV_SendMessage *) message;
1778 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1779 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1785 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1789 /* got disconnected */
1790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791 "No route to %s, dropping local message of type %u\n",
1792 GNUNET_i2s (&msg->target),
1793 ntohs (payload->type));
1794 GNUNET_STATISTICS_update (stats,
1795 "# local messages discarded (no route)",
1797 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1801 "Forwarding %u bytes of type %u to %s\n",
1802 ntohs (payload->size),
1803 ntohs (payload->type),
1804 GNUNET_i2s (&msg->target));
1806 forward_payload (route->next_hop,
1807 0 /* first hop, distance is zero */,
1811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1816 * Cleanup all of the data structures associated with a given neighbor.
1818 * @param neighbor neighbor to clean up
1821 cleanup_neighbor (struct DirectNeighbor *neighbor)
1823 handle_direct_disconnect (neighbor);
1824 GNUNET_assert (GNUNET_YES ==
1825 GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1828 GNUNET_free (neighbor);
1833 * Method called whenever a given peer disconnects.
1835 * @param cls closure
1836 * @param peer peer identity this notification is about
1837 * @param internal_cls the corresponding `struct DirectNeighbor`
1840 handle_core_disconnect (void *cls,
1841 const struct GNUNET_PeerIdentity *peer,
1844 struct DirectNeighbor *neighbor = internal_cls;
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1847 "Received core peer disconnect message for peer `%s'!\n",
1849 /* Check for disconnect from self message */
1850 if (NULL == neighbor)
1852 GNUNET_break (GNUNET_YES == neighbor->connected);
1853 neighbor->connected = GNUNET_NO;
1854 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1856 GNUNET_STATISTICS_update (stats,
1857 "# peers connected (1-hop)",
1861 cleanup_neighbor (neighbor);
1862 if (GNUNET_YES == in_shutdown)
1864 schedule_refresh_routes ();
1869 * Multipeermap iterator for freeing routes. Should never be called.
1872 * @param key key value stored under
1873 * @param value the route to be freed
1874 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1877 free_route (void *cls,
1878 const struct GNUNET_PeerIdentity *key,
1881 struct Route *route = value;
1884 GNUNET_assert (GNUNET_YES ==
1885 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1886 release_route (route);
1887 send_disconnect_to_plugin (&route->target.peer);
1888 GNUNET_free (route);
1894 * Multipeermap iterator for freeing direct neighbors. Should never be called.
1897 * @param key key value stored under
1898 * @param value the direct neighbor to be freed
1899 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1902 free_direct_neighbors (void *cls,
1903 const struct GNUNET_PeerIdentity *key,
1906 struct DirectNeighbor *neighbor = value;
1908 cleanup_neighbor (neighbor);
1914 * Task run during shutdown.
1919 shutdown_task (void *cls)
1923 in_shutdown = GNUNET_YES;
1924 GNUNET_assert (NULL != core_api);
1925 GNUNET_CORE_disconnecT (core_api);
1927 GNUNET_ATS_performance_done (ats);
1929 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1930 &free_direct_neighbors,
1932 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1935 GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
1936 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1937 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1939 GNUNET_SERVER_notification_context_destroy (nc);
1941 for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
1943 GNUNET_array_grow (consensi[i].targets,
1944 consensi[i].array_length,
1947 if (NULL != rr_task)
1949 GNUNET_SCHEDULER_cancel (rr_task);
1956 * Notify newly connected client about an existing route.
1958 * @param cls the `struct GNUNET_SERVER_Client *`
1959 * @param key peer identity
1960 * @param value the `struct Route *`
1961 * @return #GNUNET_OK (continue to iterate)
1964 notify_client_about_route (void *cls,
1965 const struct GNUNET_PeerIdentity *key,
1968 struct GNUNET_SERVER_Client *client = cls;
1969 struct Route *route = value;
1970 struct GNUNET_DV_ConnectMessage cm;
1972 memset (&cm, 0, sizeof (cm));
1973 cm.header.size = htons (sizeof (cm));
1974 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1975 cm.distance = htonl (route->target.distance);
1976 cm.peer = route->target.peer;
1978 GNUNET_SERVER_notification_context_unicast (nc,
1987 * Handle START-message. This is the first message sent to us
1988 * by the client (can only be one!).
1990 * @param cls closure (always NULL)
1991 * @param client identification of the client
1992 * @param message the actual message
1995 handle_start (void *cls,
1996 struct GNUNET_SERVER_Client *client,
1997 const struct GNUNET_MessageHeader *message)
1999 GNUNET_SERVER_notification_context_add (nc, client);
2000 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2001 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2002 ¬ify_client_about_route,
2008 * Called on core init.
2011 * @param identity this peer's identity
2014 core_init (void *cls,
2015 const struct GNUNET_PeerIdentity *identity)
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 GNUNET_i2s (identity));
2020 my_identity = *identity;
2025 * Process dv requests.
2027 * @param cls closure
2028 * @param server the initialized server
2029 * @param c configuration to use
2033 struct GNUNET_SERVER_Handle *server,
2034 const struct GNUNET_CONFIGURATION_Handle *c)
2036 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2037 GNUNET_MQ_hd_var_size (dv_route_message,
2038 GNUNET_MESSAGE_TYPE_DV_ROUTE,
2039 struct RouteMessage,
2041 GNUNET_MQ_handler_end ()
2043 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2044 {&handle_start, NULL,
2045 GNUNET_MESSAGE_TYPE_DV_START,
2046 sizeof (struct GNUNET_MessageHeader) },
2047 { &handle_dv_send_message, NULL,
2048 GNUNET_MESSAGE_TYPE_DV_SEND,
2052 in_shutdown = GNUNET_NO;
2054 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128,
2056 all_routes = GNUNET_CONTAINER_multipeermap_create (65536,
2058 core_api = GNUNET_CORE_connecT (cfg,
2061 &handle_core_connect,
2062 &handle_core_disconnect,
2065 if (NULL == core_api)
2067 ats = GNUNET_ATS_performance_init (cfg,
2072 GNUNET_CORE_disconnecT (core_api);
2076 nc = GNUNET_SERVER_notification_context_create (server,
2077 MAX_QUEUE_SIZE_PLUGIN);
2078 stats = GNUNET_STATISTICS_create ("dv",
2080 GNUNET_SERVER_add_handlers (server,
2082 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2088 * The main function for the dv service.
2090 * @param argc number of arguments from the command line
2091 * @param argv command line arguments
2092 * @return 0 ok, 1 on error
2098 return (GNUNET_OK ==
2099 GNUNET_SERVICE_run (argc,
2102 GNUNET_SERVICE_OPTION_NONE,
2107 /* end of gnunet-service-dv.c */