2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_consensus_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 * Linked list of messages to send to clients.
130 struct PendingMessage
133 * Pointer to next item in the list
135 struct PendingMessage *next;
138 * Pointer to previous item in the list
140 struct PendingMessage *prev;
143 * Actual message to be sent, allocated after this struct.
145 const struct GNUNET_MessageHeader *msg;
148 * Ultimate target for the message.
150 struct GNUNET_PeerIdentity ultimate_target;
153 * Unique ID of the message.
161 * Information about a direct neighbor (core-level, excluding
162 * DV-links, only DV-enabled peers).
164 struct DirectNeighbor
168 * Identity of the peer.
170 struct GNUNET_PeerIdentity peer;
173 * Head of linked list of messages to send to this peer.
175 struct PendingMessage *pm_head;
178 * Tail of linked list of messages to send to this peer.
180 struct PendingMessage *pm_tail;
183 * Transmit handle to core service.
185 struct GNUNET_CORE_TransmitHandle *cth;
188 * Routing table of the neighbor, NULL if not yet established.
189 * Keys are peer identities, values are 'struct Target' entries.
190 * Note that the distances in the targets are from the point-of-view
191 * of the peer, not from us!
193 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table;
196 * Updated routing table of the neighbor, under construction,
197 * NULL if we are not currently building it.
198 * Keys are peer identities, values are 'struct Target' entries.
199 * Note that the distances in the targets are from the point-of-view
200 * of the peer, not from us!
202 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table_consensus;
205 * Active consensus, if we are currently synchronizing the
208 struct GNUNET_CONSENSUS_Handle *consensus;
211 * ID of the task we use to (periodically) update our consensus
214 GNUNET_SCHEDULER_TaskIdentifier consensus_task;
217 * At what offset are we, with respect to inserting our own routes
218 * into the consensus?
220 unsigned int consensus_insertion_offset;
223 * At what distance are we, with respect to inserting our own routes
224 * into the consensus?
226 unsigned int consensus_insertion_distance;
229 * Number of messages currently in the 'pm_XXXX'-DLL.
231 unsigned int pm_queue_size;
234 * Flag set within 'check_target_removed' to trigger full global route refresh.
239 * Our distance to this peer, 0 for unknown.
244 * Is this neighbor connected at the core level?
252 * A route includes information about the next hop,
253 * the target, and the ultimate distance to the
260 * Which peer do we need to forward the message to?
262 struct DirectNeighbor *next_hop;
265 * What would be the target, and how far is it away?
267 struct Target target;
270 * Offset of this target in the respective consensus set.
272 unsigned int set_offset;
278 * Set of targets we bring to a consensus; all targets in a set have a
279 * distance equal to the sets distance (which is implied by the array
286 * Array of targets in the set, may include NULL entries if a
287 * neighbor has disconnected; the targets are allocated with the
288 * respective container (all_routes), not here.
290 struct Route **targets;
293 * Size of the 'targets' array.
295 unsigned int array_length;
301 * Hashmap of all of our neighbors; processing these usually requires
302 * first checking to see if the peer is core-connected and if the
303 * distance is 1, in which case they are direct neighbors.
305 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
308 * Hashmap with all routes that we currently support; contains
309 * routing information for all peers from distance 2
310 * up to distance DEFAULT_FISHEYE_DEPTH.
312 static struct GNUNET_CONTAINER_MultiHashMap *all_routes;
315 * Array of consensus sets we expose to the outside world. Sets
316 * are structured by the distance to the target.
318 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
321 * Handle to the core service api.
323 static struct GNUNET_CORE_Handle *core_api;
326 * The identity of our peer.
328 static struct GNUNET_PeerIdentity my_identity;
331 * The configuration for this service.
333 static const struct GNUNET_CONFIGURATION_Handle *cfg;
336 * The client, the DV plugin connected to us (or an event monitor).
337 * Hopefully this client will never change, although if the plugin
338 * dies and returns for some reason it may happen.
340 static struct GNUNET_SERVER_NotificationContext *nc;
343 * Handle for the statistics service.
345 static struct GNUNET_STATISTICS_Handle *stats;
348 * Handle to ATS service.
350 static struct GNUNET_ATS_PerformanceHandle *ats;
354 * Start creating a new consensus from scratch.
356 * @param cls the 'struct DirectNeighbor' of the peer we're building
357 * a routing consensus with
358 * @param tc scheduler context
361 start_consensus (void *cls,
362 const struct GNUNET_SCHEDULER_TaskContext *tc);
366 * Forward a message from another peer to the plugin.
368 * @param message the message to send to the plugin
369 * @param origin the original sender of the message
370 * @param distance distance to the original sender of the message
373 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
374 const struct GNUNET_PeerIdentity *origin,
377 struct GNUNET_DV_ReceivedMessage *received_msg;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Delivering message from peer `%s'\n",
382 GNUNET_i2s (origin));
383 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
384 ntohs (message->size);
385 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
387 GNUNET_break (0); /* too big */
390 received_msg = GNUNET_malloc (size);
391 received_msg->header.size = htons (size);
392 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
393 received_msg->distance = htonl (distance);
394 received_msg->sender = *origin;
395 memcpy (&received_msg[1], message, ntohs (message->size));
396 GNUNET_SERVER_notification_context_broadcast (nc,
397 &received_msg->header,
399 GNUNET_free (received_msg);
404 * Forward a control message to the plugin.
406 * @param message the message to send to the plugin
409 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
411 GNUNET_SERVER_notification_context_broadcast (nc,
418 * Give an (N)ACK message to the plugin, we transmitted a message for it.
420 * @param target peer that received the message
421 * @param uid plugin-chosen UID for the message
422 * @param nack GNUNET_NO to send ACK, GNUNET_YES to send NACK
425 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
429 struct GNUNET_DV_AckMessage ack_msg;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Delivering ACK for message to peer `%s'\n",
433 GNUNET_i2s (target));
434 ack_msg.header.size = htons (sizeof (ack_msg));
435 ack_msg.header.type = htons ((GNUNET_YES == nack)
436 ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
437 : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
438 ack_msg.uid = htonl (uid);
439 ack_msg.target = *target;
440 send_control_to_plugin (&ack_msg.header);
445 * Send a DISTANCE_CHANGED message to the plugin.
447 * @param peer peer with a changed distance
448 * @param distance new distance to the peer
451 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
454 struct GNUNET_DV_DistanceUpdateMessage du_msg;
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
459 du_msg.header.size = htons (sizeof (du_msg));
460 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
461 du_msg.distance = htonl (distance);
463 send_control_to_plugin (&du_msg.header);
468 * Give a CONNECT message to the plugin.
470 * @param target peer that connected
471 * @param distance distance to the target
474 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
477 struct GNUNET_DV_ConnectMessage cm;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Delivering CONNECT about peer `%s'\n",
481 GNUNET_i2s (target));
482 cm.header.size = htons (sizeof (cm));
483 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
484 cm.distance = htonl (distance);
486 send_control_to_plugin (&cm.header);
491 * Give a DISCONNECT message to the plugin.
493 * @param target peer that disconnected
496 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
498 struct GNUNET_DV_DisconnectMessage dm;
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Delivering DISCONNECT about peer `%s'\n",
502 GNUNET_i2s (target));
503 dm.header.size = htons (sizeof (dm));
504 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
505 dm.reserved = htonl (0);
507 send_control_to_plugin (&dm.header);
512 * Function called to transfer a message to another peer
515 * @param cls closure with the direct neighbor
516 * @param size number of bytes available in buf
517 * @param buf where the callee should write the message
518 * @return number of bytes written to buf
521 core_transmit_notify (void *cls, size_t size, void *buf)
523 struct DirectNeighbor *dn = cls;
525 struct PendingMessage *pending;
532 /* peer disconnected */
536 pending = dn->pm_head;
538 while ( (NULL != (pending = dn->pm_head)) &&
539 (size >= off + (msize = ntohs (pending->msg->size))))
542 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
545 memcpy (&cbuf[off], pending->msg, msize);
546 if (0 != pending->uid)
547 send_ack_to_plugin (&pending->ultimate_target,
550 GNUNET_free (pending);
553 if (NULL != dn->pm_head)
555 GNUNET_CORE_notify_transmit_ready (core_api,
556 GNUNET_YES /* cork */,
558 GNUNET_TIME_UNIT_FOREVER_REL,
561 &core_transmit_notify, dn);
567 * Forward the given payload to the given target.
569 * @param target where to send the message
570 * @param uid unique ID for the message
571 * @param ultimate_target ultimate recipient for the message
572 * @param distance expected (remaining) distance to the target
573 * @param sender original sender of the message
574 * @param payload payload of the message
577 forward_payload (struct DirectNeighbor *target,
580 const struct GNUNET_PeerIdentity *sender,
581 const struct GNUNET_PeerIdentity *ultimate_target,
582 const struct GNUNET_MessageHeader *payload)
584 struct PendingMessage *pm;
585 struct RouteMessage *rm;
588 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
589 (0 != memcmp (sender,
591 sizeof (struct GNUNET_PeerIdentity))) )
593 GNUNET_break (0 == uid);
596 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
597 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
602 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
603 pm->ultimate_target = *ultimate_target;
605 pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
606 rm = (struct RouteMessage *) &pm[1];
607 rm->header.size = htons ((uint16_t) msize);
608 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
609 rm->distance = htonl (distance);
610 rm->target = target->peer;
611 rm->sender = *sender;
612 memcpy (&rm[1], payload, ntohs (payload->size));
613 GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
616 target->pm_queue_size++;
617 if (NULL == target->cth)
618 target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
619 GNUNET_YES /* cork */,
621 GNUNET_TIME_UNIT_FOREVER_REL,
624 &core_transmit_notify, target);
629 * Find a free slot for storing a 'route' in the 'consensi'
630 * set at the given distance.
632 * @param distance distance to use for the set slot
635 get_consensus_slot (uint32_t distance)
637 struct ConsensusSet *cs;
640 cs = &consensi[distance];
642 while ( (i < cs->array_length) &&
643 (NULL != cs->targets[i]) ) i++;
644 if (i == cs->array_length)
645 GNUNET_array_grow (cs->targets,
647 cs->array_length * 2 + 2);
653 * Allocate a slot in the consensus set for a route.
655 * @param route route to initialize
656 * @param distance which consensus set to use
659 allocate_route (struct Route *route,
664 i = get_consensus_slot (distance);
665 route->set_offset = i;
666 consensi[distance].targets[i] = route;
667 route->target.distance = htonl (distance);
672 * Release a slot in the consensus set for a route.
674 * @param route route to release the slot from
677 release_route (struct Route *route)
679 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
680 route->set_offset = UINT_MAX; /* indicate invalid slot */
685 * Move a route from one consensus set to another.
687 * @param route route to move
688 * @param new_distance new distance for the route (destination set)
691 move_route (struct Route *route,
692 uint32_t new_distance)
696 release_route (route);
697 i = get_consensus_slot (new_distance);
698 route->set_offset = i;
699 consensi[new_distance].targets[i] = route;
700 route->target.distance = htonl (new_distance);
705 * A peer is now connected to us at distance 1. Initiate DV exchange.
707 * @param neighbor entry for the neighbor at distance 1
710 handle_direct_connect (struct DirectNeighbor *neighbor)
714 GNUNET_STATISTICS_update (stats,
715 "# peers connected (1-hop)",
717 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
718 &neighbor->peer.hashPubKey);
721 send_disconnect_to_plugin (&neighbor->peer);
722 release_route (route);
725 neighbor->consensus_task = GNUNET_SCHEDULER_add_now (&start_consensus,
731 * Method called whenever a peer connects.
734 * @param peer peer identity this notification is about
737 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
739 struct DirectNeighbor *neighbor;
741 /* Check for connect to self message */
742 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
744 /* check if entry exists */
745 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
747 if (NULL != neighbor)
749 GNUNET_break (GNUNET_YES != neighbor->connected);
750 neighbor->connected = GNUNET_YES;
751 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
753 handle_direct_connect (neighbor);
756 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
757 neighbor->peer = *peer;
758 GNUNET_assert (GNUNET_YES ==
759 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
762 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
763 neighbor->connected = GNUNET_YES;
764 neighbor->distance = 0; /* unknown */
769 * Called for each 'target' in a neighbor table to free the associated memory.
772 * @param key key of the value
773 * @param value value to free
774 * @return GNUNET_OK to continue to iterate
777 free_targets (void *cls,
778 const struct GNUNET_HashCode *key,
787 * Multihashmap iterator for checking if a given route is
788 * (now) useful to this peer.
790 * @param cls the direct neighbor for the given route
791 * @param key key value stored under
792 * @param value a 'struct Target' that may or may not be useful; not that
793 * the distance in 'target' does not include the first hop yet
794 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
797 check_possible_route (void *cls, const struct GNUNET_HashCode * key, void *value)
799 struct DirectNeighbor *neighbor = cls;
800 struct Target *target = value;
803 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
807 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
809 /* this 'target' is cheaper than the existing route; switch to alternative route! */
810 move_route (route, ntohl (target->distance) + 1);
811 route->next_hop = neighbor;
812 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
814 return GNUNET_YES; /* got a route to this target already */
816 route = GNUNET_malloc (sizeof (struct Route));
817 route->next_hop = neighbor;
818 route->target.distance = htonl (ntohl (target->distance) + 1);
819 route->target.peer = target->peer;
820 allocate_route (route, ntohl (route->target.distance));
821 GNUNET_assert (GNUNET_YES ==
822 GNUNET_CONTAINER_multihashmap_put (all_routes,
823 &route->target.peer.hashPubKey,
825 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
826 send_connect_to_plugin (&route->target.peer, ntohl (target->distance));
832 * Multihashmap iterator for finding routes that were previously
833 * "hidden" due to a better route (called after a disconnect event).
836 * @param key peer identity of the given direct neighbor
837 * @param value a 'struct DirectNeighbor' to check for additional routes
838 * @return GNUNET_YES to continue iteration
841 refresh_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
843 struct DirectNeighbor *neighbor = value;
845 if ( (GNUNET_YES != neighbor->connected) ||
846 (DIRECT_NEIGHBOR_COST != neighbor->distance) )
848 if (NULL != neighbor->neighbor_table)
849 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
850 &check_possible_route,
857 * Get distance information from 'atsi'.
859 * @param atsi performance data
860 * @param atsi_count number of entries in atsi
861 * @return connected transport distance
864 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
869 for (i = 0; i < atsi_count; i++)
870 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
871 return ntohl (atsi->value);
872 /* If we do not have explicit distance data, assume direct neighbor. */
873 return DIRECT_NEIGHBOR_COST;
878 * Multihashmap iterator for freeing routes that go via a particular
879 * neighbor that disconnected and is thus no longer available.
881 * @param cls the direct neighbor that is now unavailable
882 * @param key key value stored under
883 * @param value a 'struct Route' that may or may not go via neighbor
885 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
888 cull_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
890 struct DirectNeighbor *neighbor = cls;
891 struct Route *route = value;
893 if (route->next_hop != neighbor)
894 return GNUNET_YES; /* not affected */
895 GNUNET_assert (GNUNET_YES ==
896 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
897 release_route (route);
898 send_disconnect_to_plugin (&route->target.peer);
905 * Handle the case that a direct connection to a peer is
906 * disrupted. Remove all routes via that peer and
907 * stop the consensus with it.
909 * @param neighbor peer that was disconnected (or at least is no
910 * longer at distance 1)
913 handle_direct_disconnect (struct DirectNeighbor *neighbor)
915 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
918 if (NULL != neighbor->cth)
920 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
921 neighbor->cth = NULL;
923 if (NULL != neighbor->neighbor_table_consensus)
925 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
928 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table_consensus);
929 neighbor->neighbor_table_consensus = NULL;
931 if (NULL != neighbor->neighbor_table)
933 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
936 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table);
937 neighbor->neighbor_table = NULL;
939 if (GNUNET_SCHEDULER_NO_TASK != neighbor->consensus_task)
941 GNUNET_SCHEDULER_cancel (neighbor->consensus_task);
942 neighbor->consensus_task = GNUNET_SCHEDULER_NO_TASK;
944 if (NULL != neighbor->consensus)
946 GNUNET_CONSENSUS_destroy (neighbor->consensus);
947 neighbor->consensus = NULL;
953 * Function that is called with QoS information about an address; used
954 * to update our current distance to another peer.
957 * @param address the address
958 * @param bandwidth_out assigned outbound bandwidth for the connection
959 * @param bandwidth_in assigned inbound bandwidth for the connection
960 * @param ats performance data for the address (as far as known)
961 * @param ats_count number of performance records in 'ats'
964 handle_ats_update (void *cls,
965 const struct GNUNET_HELLO_Address *address,
967 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
968 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
969 const struct GNUNET_ATS_Information *ats,
972 struct DirectNeighbor *neighbor;
975 if (GNUNET_NO == active)
977 distance = get_atsi_distance (ats, ats_count);
978 /* check if entry exists */
979 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
980 &address->peer.hashPubKey);
981 if (NULL != neighbor)
983 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
984 (DIRECT_NEIGHBOR_COST == distance) )
985 return; /* no change */
986 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
988 neighbor->distance = distance;
989 GNUNET_STATISTICS_update (stats,
990 "# peers connected (1-hop)",
992 handle_direct_disconnect (neighbor);
993 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
998 neighbor->distance = distance;
999 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1001 if (GNUNET_YES != neighbor->connected)
1003 handle_direct_connect (neighbor);
1006 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
1007 neighbor->peer = address->peer;
1008 GNUNET_assert (GNUNET_YES ==
1009 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
1010 &address->peer.hashPubKey,
1012 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1013 neighbor->connected = GNUNET_NO; /* not yet */
1014 neighbor->distance = distance;
1019 * Check if a target was removed from the set of the other peer; if so,
1020 * if we also used it for our route, we need to remove it from our
1021 * 'all_routes' set (and later check if an alternative path now exists).
1023 * @param cls the 'struct DirectNeighbor'
1024 * @param key peer identity for the target
1025 * @param value a 'struct Target' previously reachable via the given neighbor
1028 check_target_removed (void *cls,
1029 const struct GNUNET_HashCode *key,
1032 struct DirectNeighbor *neighbor = cls;
1033 struct Target *new_target;
1034 struct Route *current_route;
1036 new_target = GNUNET_CONTAINER_multihashmap_get (neighbor->neighbor_table_consensus,
1038 if (NULL == new_target)
1040 /* target was revoked, check if it was used */
1041 current_route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1043 if ( (NULL == current_route) ||
1044 (current_route->next_hop != neighbor) )
1046 /* didn't matter, wasn't used */
1049 /* remove existing route */
1050 GNUNET_assert (GNUNET_YES ==
1051 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, current_route));
1052 send_disconnect_to_plugin (¤t_route->target.peer);
1053 GNUNET_free (current_route);
1054 neighbor->target_removed = GNUNET_YES;
1062 * Check if a target was added to the set of the other peer; if it
1063 * was added or impoves the existing route, do the needed updates.
1065 * @param cls the 'struct DirectNeighbor'
1066 * @param key peer identity for the target
1067 * @param value a 'struct Target' now reachable via the given neighbor
1070 check_target_added (void *cls,
1071 const struct GNUNET_HashCode *key,
1074 struct DirectNeighbor *neighbor = cls;
1075 struct Target *target = value;
1076 struct Route *current_route;
1078 /* target was revoked, check if it was used */
1079 current_route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1081 if (NULL != current_route)
1084 if (current_route->next_hop == neighbor)
1086 /* we had the same route before, no change */
1087 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1089 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1090 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1094 if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
1096 /* alternative, shorter route exists, ignore */
1099 /* new route is better than the existing one, take over! */
1100 /* NOTE: minor security issue: malicious peers may advertise
1101 very short routes to take over longer paths; as we don't
1102 check that the shorter routes actually work, a malicious
1103 direct neighbor can use this to DoS our long routes */
1104 current_route->next_hop = neighbor;
1105 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1106 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1110 current_route = GNUNET_malloc (sizeof (struct Route));
1111 current_route->next_hop = neighbor;
1112 current_route->target.peer = target->peer;
1113 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1114 GNUNET_assert (GNUNET_YES ==
1115 GNUNET_CONTAINER_multihashmap_put (all_routes,
1116 ¤t_route->target.peer.hashPubKey,
1118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1119 send_connect_to_plugin (¤t_route->target.peer,
1120 ntohl (current_route->target.distance));
1127 * The consensus has concluded, clean up and schedule the next one.
1129 * @param cls the 'struct GNUNET_DirectNeighbor' with which we created the consensus
1132 consensus_done_cb (void *cls)
1134 struct DirectNeighbor *neighbor = cls;
1136 GNUNET_CONSENSUS_destroy (neighbor->consensus);
1137 neighbor->consensus = NULL;
1138 /* remove targets that disappeared */
1139 neighbor->target_removed = GNUNET_NO;
1140 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1141 &check_target_removed,
1143 if (GNUNET_YES == neighbor->target_removed)
1145 /* check if we got an alternative for the removed routes */
1146 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1150 /* add targets that appeared (and check for improved routes) */
1151 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1152 &check_target_added,
1154 if (NULL != neighbor->neighbor_table)
1156 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1159 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table);
1160 neighbor->neighbor_table = NULL;
1162 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1163 neighbor->neighbor_table_consensus = NULL;
1164 neighbor->consensus_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1171 * We inserted the last element into the consensus, get ready to
1172 * insert the next element into the consensus or conclude if
1175 * @param cls the 'struct DirectNeighbor' of the peer we're building
1176 * a routing consensus with
1177 * @param success GNUNET_OK if the last element was added successfully,
1178 * GNUNET_SYSERR if we failed
1181 insert_next_element (void *cls,
1184 struct DirectNeighbor *neighbor = cls;
1185 struct GNUNET_CONSENSUS_Element element;
1187 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
1188 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
1190 neighbor->consensus_insertion_offset = 0;
1191 neighbor->consensus_insertion_distance++;
1192 /* skip over NULL entries */
1193 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
1194 (consensi[neighbor->consensus_insertion_distance].array_length < neighbor->consensus_insertion_offset) &&
1195 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
1196 neighbor->consensus_insertion_offset++;
1198 if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
1200 /* we're done, conclude! */
1201 GNUNET_CONSENSUS_conclude (neighbor->consensus,
1202 GNUNET_DV_CONSENSUS_FREQUENCY,
1207 element.size = sizeof (struct Target);
1208 element.data = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset++]->target;
1210 /* skip over NULL entries */
1211 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
1212 (consensi[neighbor->consensus_insertion_distance].array_length < neighbor->consensus_insertion_offset) &&
1213 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
1214 neighbor->consensus_insertion_offset++;
1215 GNUNET_CONSENSUS_insert (neighbor->consensus,
1217 &insert_next_element,
1223 * We have learned a new route from the other peer. Add it to the
1224 * route set we're building.
1226 * @param cls the 'struct DirectNeighbor' we're building the consensus with
1227 * @param element the new element we have learned
1228 * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
1229 * GNUNET_SYSERR if the element should be ignored and not be propagated
1232 learn_route_cb (void *cls,
1233 const struct GNUNET_CONSENSUS_Element *element)
1235 struct DirectNeighbor *neighbor = cls;
1236 struct Target *target;
1238 if (NULL == element)
1240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1241 "Failed to establish DV consensus, will try again later\n");
1242 GNUNET_CONSENSUS_destroy (neighbor->consensus);
1243 if (NULL != neighbor->neighbor_table_consensus)
1245 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1248 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table_consensus);
1249 neighbor->neighbor_table_consensus = NULL;
1251 neighbor->consensus = NULL;
1252 neighbor->consensus_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1255 return GNUNET_SYSERR;
1257 if (sizeof (struct Target) != element->size)
1259 GNUNET_break_op (0);
1260 return GNUNET_SYSERR;
1262 target = GNUNET_malloc (sizeof (struct Target));
1263 memcpy (target, element->data, sizeof (struct Target));
1265 GNUNET_CONTAINER_multihashmap_put (neighbor->neighbor_table_consensus,
1266 &target->peer.hashPubKey,
1268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1270 GNUNET_break_op (0);
1271 GNUNET_free (target);
1272 return GNUNET_SYSERR;
1279 * Start creating a new consensus from scratch.
1281 * @param cls the 'struct DirectNeighbor' of the peer we're building
1282 * a routing consensus with
1283 * @param tc scheduler context
1286 start_consensus (void *cls,
1287 const struct GNUNET_SCHEDULER_TaskContext *tc)
1289 struct DirectNeighbor *neighbor = cls;
1290 struct GNUNET_HashCode session_id;
1291 struct GNUNET_HashCode real_session_id;
1293 neighbor->consensus_task = GNUNET_SCHEDULER_NO_TASK;
1294 neighbor->consensus_insertion_offset = 0;
1295 neighbor->consensus_insertion_distance = 0;
1296 GNUNET_assert (NULL == neighbor->neighbor_table_consensus);
1297 GNUNET_assert (NULL == neighbor->consensus);
1298 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1299 /* construct session ID seed as XOR of both peer's identities */
1300 GNUNET_CRYPTO_hash_xor (&my_identity.hashPubKey,
1301 &neighbor->peer.hashPubKey,
1303 /* make sure session ID is unique across applications by salting it with 'DV' */
1304 GNUNET_CRYPTO_hkdf (&real_session_id, sizeof (real_session_id),
1305 GCRY_MD_SHA512, GCRY_MD_SHA256,
1307 &session_id, sizeof (session_id),
1309 neighbor->consensus = GNUNET_CONSENSUS_create (cfg,
1315 if (NULL == neighbor->consensus)
1317 neighbor->consensus_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1322 insert_next_element (neighbor, GNUNET_OK);
1327 * Core handler for DV data messages. Whatever this message
1328 * contains all we really have to do is rip it out of its
1329 * DV layering and give it to our pal the DV plugin to report
1332 * @param cls closure
1333 * @param peer peer which sent the message (immediate sender)
1334 * @param message the message
1335 * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
1338 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1339 const struct GNUNET_MessageHeader *message)
1341 const struct RouteMessage *rm;
1342 const struct GNUNET_MessageHeader *payload;
1343 struct Route *route;
1345 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1347 GNUNET_break_op (0);
1348 return GNUNET_SYSERR;
1350 rm = (const struct RouteMessage *) message;
1351 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1352 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1354 GNUNET_break_op (0);
1355 return GNUNET_SYSERR;
1357 if (0 == memcmp (&rm->target,
1359 sizeof (struct GNUNET_PeerIdentity)))
1361 /* message is for me, check reverse route! */
1362 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1363 &rm->sender.hashPubKey);
1366 /* don't have reverse route, drop */
1367 GNUNET_STATISTICS_update (stats,
1368 "# message discarded (no reverse route)",
1372 send_data_to_plugin (payload,
1374 ntohl (route->target.distance));
1377 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1378 &rm->target.hashPubKey);
1381 GNUNET_STATISTICS_update (stats,
1382 "# messages discarded (no route)",
1386 if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
1388 GNUNET_STATISTICS_update (stats,
1389 "# messages discarded (target too far)",
1393 forward_payload (route->next_hop,
1394 ntohl (route->target.distance),
1404 * Service server's handler for message send requests (which come
1405 * bubbling up to us through the DV plugin).
1407 * @param cls closure
1408 * @param client identification of the client
1409 * @param message the actual message
1412 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1413 const struct GNUNET_MessageHeader *message)
1415 struct Route *route;
1416 const struct GNUNET_DV_SendMessage *msg;
1417 const struct GNUNET_MessageHeader *payload;
1419 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1425 msg = (const struct GNUNET_DV_SendMessage *) message;
1426 GNUNET_break (0 != ntohl (msg->uid));
1427 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1428 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1434 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1435 &msg->target.hashPubKey);
1438 /* got disconnected */
1439 GNUNET_STATISTICS_update (stats,
1440 "# local messages discarded (no route)",
1442 send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1443 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1446 forward_payload (route->next_hop,
1447 ntohl (route->target.distance),
1452 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1457 * Cleanup all of the data structures associated with a given neighbor.
1459 * @param neighbor neighbor to clean up
1462 cleanup_neighbor (struct DirectNeighbor *neighbor)
1464 struct PendingMessage *pending;
1466 while (NULL != (pending = neighbor->pm_head))
1468 neighbor->pm_queue_size--;
1469 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1472 GNUNET_free (pending);
1474 handle_direct_disconnect (neighbor);
1475 GNUNET_assert (GNUNET_YES ==
1476 GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
1477 &neighbor->peer.hashPubKey,
1479 GNUNET_free (neighbor);
1484 * Method called whenever a given peer disconnects.
1486 * @param cls closure
1487 * @param peer peer identity this notification is about
1490 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1492 struct DirectNeighbor *neighbor;
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Received core peer disconnect message for peer `%s'!\n",
1497 /* Check for disconnect from self message */
1498 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1501 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
1502 if (NULL == neighbor)
1507 GNUNET_break (GNUNET_YES == neighbor->connected);
1508 neighbor->connected = GNUNET_NO;
1509 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1511 GNUNET_STATISTICS_update (stats,
1512 "# peers connected (1-hop)",
1515 cleanup_neighbor (neighbor);
1516 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1523 * Multihashmap iterator for freeing routes. Should never be called.
1526 * @param key key value stored under
1527 * @param value the route to be freed
1529 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1532 free_route (void *cls, const struct GNUNET_HashCode * key, void *value)
1534 struct Route *route = value;
1537 GNUNET_assert (GNUNET_YES ==
1538 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
1539 release_route (route);
1540 send_disconnect_to_plugin (&route->target.peer);
1541 GNUNET_free (route);
1547 * Multihashmap iterator for freeing direct neighbors. Should never be called.
1550 * @param key key value stored under
1551 * @param value the direct neighbor to be freed
1553 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1556 free_direct_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
1558 struct DirectNeighbor *neighbor = value;
1561 cleanup_neighbor (neighbor);
1567 * Task run during shutdown.
1573 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 GNUNET_CORE_disconnect (core_api);
1579 GNUNET_ATS_performance_done (ats);
1581 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1582 &free_direct_neighbors, NULL);
1583 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1585 GNUNET_CONTAINER_multihashmap_destroy (direct_neighbors);
1586 GNUNET_CONTAINER_multihashmap_destroy (all_routes);
1587 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1589 GNUNET_SERVER_notification_context_destroy (nc);
1591 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1592 GNUNET_array_grow (consensi[i].targets,
1593 consensi[i].array_length,
1599 * Notify newly connected client about an existing route.
1601 * @param cls the 'struct GNUNET_SERVER_Client'
1602 * @param key peer identity
1603 * @param value the XXX.
1604 * @return GNUNET_OK (continue to iterate)
1607 add_route (void *cls,
1608 const struct GNUNET_HashCode *key,
1611 struct GNUNET_SERVER_Client *client = cls;
1612 struct Route *route = value;
1613 struct GNUNET_DV_ConnectMessage cm;
1615 cm.header.size = htons (sizeof (cm));
1616 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1617 cm.distance = htonl (route->target.distance);
1618 cm.peer = route->target.peer;
1620 GNUNET_SERVER_notification_context_unicast (nc,
1629 * Handle START-message. This is the first message sent to us
1630 * by the client (can only be one!).
1632 * @param cls closure (always NULL)
1633 * @param client identification of the client
1634 * @param message the actual message
1637 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1638 const struct GNUNET_MessageHeader *message)
1640 GNUNET_SERVER_notification_context_add (nc, client);
1641 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1642 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1649 * Called on core init.
1652 * @param server legacy
1653 * @param identity this peer's identity
1656 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1657 const struct GNUNET_PeerIdentity *identity)
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 GNUNET_i2s (identity));
1662 my_identity = *identity;
1667 * Process dv requests.
1669 * @param cls closure
1670 * @param server the initialized server
1671 * @param c configuration to use
1674 run (void *cls, struct GNUNET_SERVER_Handle *server,
1675 const struct GNUNET_CONFIGURATION_Handle *c)
1677 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1678 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
1681 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1682 {&handle_start, NULL,
1683 GNUNET_MESSAGE_TYPE_DV_START,
1684 sizeof (struct GNUNET_MessageHeader) },
1685 { &handle_dv_send_message, NULL,
1686 GNUNET_MESSAGE_TYPE_DV_SEND,
1692 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1693 all_routes = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
1694 core_api = GNUNET_CORE_connect (cfg, NULL,
1696 &handle_core_connect,
1697 &handle_core_disconnect,
1702 if (NULL == core_api)
1704 ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
1707 GNUNET_CORE_disconnect (core_api);
1710 nc = GNUNET_SERVER_notification_context_create (server,
1711 MAX_QUEUE_SIZE_PLUGIN);
1712 stats = GNUNET_STATISTICS_create ("dv", cfg);
1713 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1714 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1715 &shutdown_task, NULL);
1720 * The main function for the dv service.
1722 * @param argc number of arguments from the command line
1723 * @param argv command line arguments
1724 * @return 0 ok, 1 on error
1727 main (int argc, char *const *argv)
1729 return (GNUNET_OK ==
1730 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
1731 &run, NULL)) ? 0 : 1;
1734 /* end of gnunet-service-dv.c */