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 * - distance updates are not properly communicate to US by core/transport/ats
34 #include "gnunet_util_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_hello_lib.h"
38 #include "gnunet_peerinfo_service.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet_consensus_service.h"
46 * How often do we establish the consensu?
48 #define GNUNET_DV_CONSENSUS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
51 * Maximum number of messages we queue per peer.
53 #define MAX_QUEUE_SIZE 16
56 * Maximum number of messages we queue towards the clients/plugin.
58 #define MAX_QUEUE_SIZE_PLUGIN 1024
61 * The default fisheye depth, from how many hops away will
64 #define DEFAULT_FISHEYE_DEPTH 3
67 * How many hops is a direct neighbor away?
69 #define DIRECT_NEIGHBOR_COST 1
72 GNUNET_NETWORK_STRUCT_BEGIN
75 * Information about a peer DV can route to. These entries are what
76 * we use as the binary format to establish consensus to create our
77 * routing table and as the address format in the HELLOs.
83 * Identity of the peer we can reach.
85 struct GNUNET_PeerIdentity peer;
88 * How many hops (1-3) is this peer away? in network byte order
90 uint32_t distance GNUNET_PACKED;
96 * Message exchanged between DV services (via core), requesting a
97 * message to be routed.
102 * Type: GNUNET_MESSAGE_TYPE_DV_ROUTE
104 struct GNUNET_MessageHeader header;
107 * Expected (remaining) distance. Must be always smaller than
108 * DEFAULT_FISHEYE_DEPTH, should be zero at the target. Must
109 * be decremented by one at each hop. Peers must not forward
110 * these messages further once the counter has reached zero.
112 uint32_t distance GNUNET_PACKED;
115 * The (actual) target of the message (this peer, if distance is zero).
117 struct GNUNET_PeerIdentity target;
120 * The (actual) sender of the message.
122 struct GNUNET_PeerIdentity sender;
126 GNUNET_NETWORK_STRUCT_END
130 * Linked list of messages to send to clients.
132 struct PendingMessage
135 * Pointer to next item in the list
137 struct PendingMessage *next;
140 * Pointer to previous item in the list
142 struct PendingMessage *prev;
145 * Actual message to be sent, allocated after this struct.
147 const struct GNUNET_MessageHeader *msg;
150 * Ultimate target for the message.
152 struct GNUNET_PeerIdentity ultimate_target;
155 * Unique ID of the message.
163 * Information about a direct neighbor (core-level, excluding
164 * DV-links, only DV-enabled peers).
166 struct DirectNeighbor
170 * Identity of the peer.
172 struct GNUNET_PeerIdentity peer;
175 * Head of linked list of messages to send to this peer.
177 struct PendingMessage *pm_head;
180 * Tail of linked list of messages to send to this peer.
182 struct PendingMessage *pm_tail;
185 * Transmit handle to core service.
187 struct GNUNET_CORE_TransmitHandle *cth;
190 * Routing table of the neighbor, NULL if not yet established.
191 * Keys are peer identities, values are 'struct Target' entries.
192 * Note that the distances in the targets are from the point-of-view
193 * of the peer, not from us!
195 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table;
198 * Updated routing table of the neighbor, under construction,
199 * NULL if we are not currently building it.
200 * Keys are peer identities, values are 'struct Target' entries.
201 * Note that the distances in the targets are from the point-of-view
202 * of the peer, not from us!
204 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table_consensus;
207 * Active consensus, if we are currently synchronizing the
210 struct GNUNET_CONSENSUS_Handle *consensus;
213 * ID of the task we use to (periodically) update our consensus
216 GNUNET_SCHEDULER_TaskIdentifier consensus_task;
219 * At what offset are we, with respect to inserting our own routes
220 * into the consensus?
222 unsigned int consensus_insertion_offset;
225 * At what distance are we, with respect to inserting our own routes
226 * into the consensus?
228 unsigned int consensus_insertion_distance;
231 * Number of messages currently in the 'pm_XXXX'-DLL.
233 unsigned int pm_queue_size;
236 * Flag set within 'check_target_removed' to trigger full global route refresh.
244 * A route includes information about the next hop,
245 * the target, and the ultimate distance to the
252 * Which peer do we need to forward the message to?
254 struct DirectNeighbor *next_hop;
257 * What would be the target, and how far is it away?
259 struct Target target;
262 * Offset of this target in the respective consensus set.
264 unsigned int set_offset;
270 * Set of targets we bring to a consensus; all targets in a set have a
271 * distance equal to the sets distance (which is implied by the array
278 * Array of targets in the set, may include NULL entries if a
279 * neighbor has disconnected; the targets are allocated with the
280 * respective container (all_routes), not here.
282 struct Route **targets;
285 * Size of the 'targets' array.
287 unsigned int array_length;
293 * Hashmap of all of our direct neighbors (no DV routing).
295 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
298 * Hashmap with all routes that we currently support; contains
299 * routing information for all peers from distance 2
300 * up to distance DEFAULT_FISHEYE_DEPTH.
302 static struct GNUNET_CONTAINER_MultiHashMap *all_routes;
305 * Array of consensus sets we expose to the outside world. Sets
306 * are structured by the distance to the target.
308 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
311 * Handle to the core service api.
313 static struct GNUNET_CORE_Handle *core_api;
316 * The identity of our peer.
318 static struct GNUNET_PeerIdentity my_identity;
321 * The configuration for this service.
323 static const struct GNUNET_CONFIGURATION_Handle *cfg;
326 * The client, the DV plugin connected to us (or an event monitor).
327 * Hopefully this client will never change, although if the plugin
328 * dies and returns for some reason it may happen.
330 static struct GNUNET_SERVER_NotificationContext *nc;
333 * Handle for the statistics service.
335 struct GNUNET_STATISTICS_Handle *stats;
339 * Get distance information from 'atsi'.
341 * @param atsi performance data
342 * @param atsi_count number of entries in atsi
343 * @return connected transport distance
346 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
347 unsigned int atsi_count)
351 for (i = 0; i < atsi_count; i++)
352 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
353 return ntohl (atsi->value);
354 /* FIXME: we do not have distance data? Assume direct neighbor. */
355 return DIRECT_NEIGHBOR_COST;
360 * Forward a message from another peer to the plugin.
362 * @param message the message to send to the plugin
363 * @param origin the original sender of the message
364 * @param distance distance to the original sender of the message
367 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
368 const struct GNUNET_PeerIdentity *origin,
371 struct GNUNET_DV_ReceivedMessage *received_msg;
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Delivering message from peer `%s'\n",
376 GNUNET_i2s (origin));
377 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
378 ntohs (message->size);
379 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
381 GNUNET_break (0); /* too big */
384 received_msg = GNUNET_malloc (size);
385 received_msg->header.size = htons (size);
386 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
387 received_msg->distance = htonl (distance);
388 received_msg->sender = *origin;
389 memcpy (&received_msg[1], message, ntohs (message->size));
390 GNUNET_SERVER_notification_context_broadcast (nc,
391 &received_msg->header,
393 GNUNET_free (received_msg);
398 * Forward a control message to the plugin.
400 * @param message the message to send to the plugin
403 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
405 GNUNET_SERVER_notification_context_broadcast (nc,
412 * Give an (N)ACK message to the plugin, we transmitted a message for it.
414 * @param target peer that received the message
415 * @param uid plugin-chosen UID for the message
416 * @param nack GNUNET_NO to send ACK, GNUNET_YES to send NACK
419 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
423 struct GNUNET_DV_AckMessage ack_msg;
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Delivering ACK for message to peer `%s'\n",
427 GNUNET_i2s (target));
428 ack_msg.header.size = htons (sizeof (ack_msg));
429 ack_msg.header.type = htons ((GNUNET_YES == nack)
430 ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
431 : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
432 ack_msg.uid = htonl (uid);
433 ack_msg.target = *target;
434 send_control_to_plugin (&ack_msg.header);
439 * Send a DISTANCE_CHANGED message to the plugin.
441 * @param peer peer with a changed distance
442 * @param distance new distance to the peer
445 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
448 struct GNUNET_DV_DistanceUpdateMessage du_msg;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
453 du_msg.header.size = htons (sizeof (du_msg));
454 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
455 du_msg.distance = htonl (distance);
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
468 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
471 struct GNUNET_DV_ConnectMessage cm;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Delivering CONNECT about peer `%s'\n",
475 GNUNET_i2s (target));
476 cm.header.size = htons (sizeof (cm));
477 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
478 cm.distance = htonl (distance);
480 send_control_to_plugin (&cm.header);
485 * Give a DISCONNECT message to the plugin.
487 * @param target peer that disconnected
490 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
492 struct GNUNET_DV_DisconnectMessage dm;
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Delivering DISCONNECT about peer `%s'\n",
496 GNUNET_i2s (target));
497 dm.header.size = htons (sizeof (dm));
498 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
499 dm.reserved = htonl (0);
501 send_control_to_plugin (&dm.header);
506 * Function called to transfer a message to another peer
509 * @param cls closure with the direct neighbor
510 * @param size number of bytes available in buf
511 * @param buf where the callee should write the message
512 * @return number of bytes written to buf
515 core_transmit_notify (void *cls, size_t size, void *buf)
517 struct DirectNeighbor *dn = cls;
519 struct PendingMessage *pending;
526 /* peer disconnected */
530 pending = dn->pm_head;
532 while ( (NULL != (pending = dn->pm_head)) &&
533 (size >= off + (msize = ntohs (pending->msg->size))))
536 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
539 memcpy (&cbuf[off], pending->msg, msize);
540 if (0 != pending->uid)
541 send_ack_to_plugin (&pending->ultimate_target,
544 GNUNET_free (pending);
547 if (NULL != dn->pm_head)
549 GNUNET_CORE_notify_transmit_ready (core_api,
550 GNUNET_YES /* cork */,
552 GNUNET_TIME_UNIT_FOREVER_REL,
555 &core_transmit_notify, dn);
561 * Forward the given payload to the given target.
563 * @param target where to send the message
564 * @param uid unique ID for the message
565 * @param ultimate_target ultimate recipient for the message
566 * @param distance expected (remaining) distance to the target
567 * @param sender original sender of the message
568 * @param payload payload of the message
571 forward_payload (struct DirectNeighbor *target,
574 const struct GNUNET_PeerIdentity *sender,
575 const struct GNUNET_PeerIdentity *ultimate_target,
576 const struct GNUNET_MessageHeader *payload)
578 struct PendingMessage *pm;
579 struct RouteMessage *rm;
582 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
583 (0 != memcmp (sender,
585 sizeof (struct GNUNET_PeerIdentity))) )
587 GNUNET_break (0 == uid);
590 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
591 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
596 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
597 pm->ultimate_target = *ultimate_target;
599 pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
600 rm = (struct RouteMessage *) &pm[1];
601 rm->header.size = htons ((uint16_t) msize);
602 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
603 rm->distance = htonl (distance);
604 rm->target = target->peer;
605 rm->sender = *sender;
606 memcpy (&rm[1], payload, ntohs (payload->size));
607 GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
610 target->pm_queue_size++;
611 if (NULL == target->cth)
612 target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
613 GNUNET_YES /* cork */,
615 GNUNET_TIME_UNIT_FOREVER_REL,
618 &core_transmit_notify, target);
623 * Find a free slot for storing a 'route' in the 'consensi'
624 * set at the given distance.
626 * @param distance distance to use for the set slot
629 get_consensus_slot (uint32_t distance)
631 struct ConsensusSet *cs;
634 cs = &consensi[distance];
636 while ( (i < cs->array_length) &&
637 (NULL != cs->targets[i]) ) i++;
638 if (i == cs->array_length)
639 GNUNET_array_grow (cs->targets,
641 cs->array_length * 2 + 2);
647 * Allocate a slot in the consensus set for a route.
649 * @param route route to initialize
650 * @param distance which consensus set to use
653 allocate_route (struct Route *route,
658 i = get_consensus_slot (distance);
659 route->set_offset = i;
660 consensi[distance].targets[i] = route;
661 route->target.distance = htonl (distance);
666 * Release a slot in the consensus set for a route.
668 * @param route route to release the slot from
671 release_route (struct Route *route)
673 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
674 route->set_offset = UINT_MAX; /* indicate invalid slot */
679 * Move a route from one consensus set to another.
681 * @param route route to move
682 * @param new_distance new distance for the route (destination set)
685 move_route (struct Route *route,
686 uint32_t new_distance)
690 release_route (route);
691 i = get_consensus_slot (new_distance);
692 route->set_offset = i;
693 consensi[new_distance].targets[i] = route;
694 route->target.distance = htonl (new_distance);
699 * Start creating a new consensus from scratch.
701 * @param cls the 'struct DirectNeighbor' of the peer we're building
702 * a routing consensus with
703 * @param tc scheduler context
706 start_consensus (void *cls,
707 const struct GNUNET_SCHEDULER_TaskContext *tc);
711 * Method called whenever a peer connects.
714 * @param peer peer identity this notification is about
717 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
719 struct DirectNeighbor *neighbor;
723 /* Check for connect to self message */
724 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
726 fprintf (stderr, "FIX ATS DATA: %s:%u!\n", __FILE__, __LINE__);
727 distance = get_atsi_distance (NULL, 0);
728 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
730 if (NULL != neighbor)
735 if (DIRECT_NEIGHBOR_COST != distance)
736 return; /* is a DV-neighbor */
737 GNUNET_STATISTICS_update (stats,
738 "# peers connected (1-hop)",
740 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
741 neighbor->peer = *peer;
742 GNUNET_assert (GNUNET_YES ==
743 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
746 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
747 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
751 send_disconnect_to_plugin (peer);
752 release_route (route);
755 neighbor->consensus_task = GNUNET_SCHEDULER_add_now (&start_consensus,
761 * Called for each 'target' in a neighbor table to free the associated memory.
764 * @param key key of the value
765 * @param value value to free
766 * @return GNUNET_OK to continue to iterate
769 free_targets (void *cls,
770 const struct GNUNET_HashCode *key,
779 * Multihashmap iterator for checking if a given route is
780 * (now) useful to this peer.
782 * @param cls the direct neighbor for the given route
783 * @param key key value stored under
784 * @param value a 'struct Target' that may or may not be useful; not that
785 * the distance in 'target' does not include the first hop yet
786 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
789 check_possible_route (void *cls, const struct GNUNET_HashCode * key, void *value)
791 struct DirectNeighbor *neighbor = cls;
792 struct Target *target = value;
795 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
799 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
801 /* this 'target' is cheaper than the existing route; switch to alternative route! */
802 move_route (route, ntohl (target->distance) + 1);
803 route->next_hop = neighbor;
804 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
806 return GNUNET_YES; /* got a route to this target already */
808 route = GNUNET_malloc (sizeof (struct Route));
809 route->next_hop = neighbor;
810 route->target.distance = htonl (ntohl (target->distance) + 1);
811 route->target.peer = target->peer;
812 allocate_route (route, ntohl (route->target.distance));
813 GNUNET_assert (GNUNET_YES ==
814 GNUNET_CONTAINER_multihashmap_put (all_routes,
815 &route->target.peer.hashPubKey,
817 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
818 send_connect_to_plugin (&route->target.peer, ntohl (target->distance));
824 * Multihashmap iterator for finding routes that were previously
825 * "hidden" due to a better route (called after a disconnect event).
828 * @param key peer identity of the given direct neighbor
829 * @param value a 'struct DirectNeighbor' to check for additional routes
830 * @return GNUNET_YES to continue iteration
833 refresh_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
835 struct DirectNeighbor *neighbor = value;
837 if (NULL != neighbor->neighbor_table)
838 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
839 &check_possible_route,
846 * Check if a target was removed from the set of the other peer; if so,
847 * if we also used it for our route, we need to remove it from our
848 * 'all_routes' set (and later check if an alternative path now exists).
850 * @param cls the 'struct DirectNeighbor'
851 * @param key peer identity for the target
852 * @param value a 'struct Target' previously reachable via the given neighbor
855 check_target_removed (void *cls,
856 const struct GNUNET_HashCode *key,
859 struct DirectNeighbor *neighbor = cls;
860 struct Target *new_target;
861 struct Route *current_route;
863 new_target = GNUNET_CONTAINER_multihashmap_get (neighbor->neighbor_table_consensus,
865 if (NULL == new_target)
867 /* target was revoked, check if it was used */
868 current_route = GNUNET_CONTAINER_multihashmap_get (all_routes,
870 if ( (NULL == current_route) ||
871 (current_route->next_hop != neighbor) )
873 /* didn't matter, wasn't used */
876 /* remove existing route */
877 GNUNET_assert (GNUNET_YES ==
878 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, current_route));
879 send_disconnect_to_plugin (¤t_route->target.peer);
880 GNUNET_free (current_route);
881 neighbor->target_removed = GNUNET_YES;
889 * Check if a target was added to the set of the other peer; if it
890 * was added or impoves the existing route, do the needed updates.
892 * @param cls the 'struct DirectNeighbor'
893 * @param key peer identity for the target
894 * @param value a 'struct Target' now reachable via the given neighbor
897 check_target_added (void *cls,
898 const struct GNUNET_HashCode *key,
901 struct DirectNeighbor *neighbor = cls;
902 struct Target *target = value;
903 struct Route *current_route;
905 /* target was revoked, check if it was used */
906 current_route = GNUNET_CONTAINER_multihashmap_get (all_routes,
908 if (NULL != current_route)
911 if (current_route->next_hop == neighbor)
913 /* we had the same route before, no change */
914 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
916 current_route->target.distance = htonl (ntohl (target->distance) + 1);
917 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
921 if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
923 /* alternative, shorter route exists, ignore */
926 /* new route is better than the existing one, take over! */
927 /* NOTE: minor security issue: malicious peers may advertise
928 very short routes to take over longer paths; as we don't
929 check that the shorter routes actually work, a malicious
930 direct neighbor can use this to DoS our long routes */
931 current_route->next_hop = neighbor;
932 current_route->target.distance = htonl (ntohl (target->distance) + 1);
933 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
937 current_route = GNUNET_malloc (sizeof (struct Route));
938 current_route->next_hop = neighbor;
939 current_route->target.peer = target->peer;
940 current_route->target.distance = htonl (ntohl (target->distance) + 1);
941 GNUNET_assert (GNUNET_YES ==
942 GNUNET_CONTAINER_multihashmap_put (all_routes,
943 ¤t_route->target.peer.hashPubKey,
945 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
946 send_connect_to_plugin (¤t_route->target.peer,
947 ntohl (current_route->target.distance));
954 * The consensus has concluded, clean up and schedule the next one.
956 * @param cls the 'struct GNUNET_DirectNeighbor' with which we created the consensus
960 consensus_done_cb (void *cls)
962 struct DirectNeighbor *neighbor = cls;
964 GNUNET_CONSENSUS_destroy (neighbor->consensus);
965 neighbor->consensus = NULL;
966 /* remove targets that disappeared */
967 neighbor->target_removed = GNUNET_NO;
968 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
969 &check_target_removed,
971 if (GNUNET_YES == neighbor->target_removed)
973 /* check if we got an alternative for the removed routes */
974 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
978 /* add targets that appeared (and check for improved routes) */
979 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
982 if (NULL != neighbor->neighbor_table)
984 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
987 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table);
988 neighbor->neighbor_table = NULL;
990 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
991 neighbor->neighbor_table_consensus = NULL;
992 neighbor->consensus_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
999 * We inserted the last element into the consensus, get ready to
1000 * insert the next element into the consensus or conclude if
1003 * @param cls the 'struct DirectNeighbor' of the peer we're building
1004 * a routing consensus with
1005 * @param success GNUNET_OK if the last element was added successfully,
1006 * GNUNET_SYSERR if we failed
1009 insert_next_element (void *cls,
1012 struct DirectNeighbor *neighbor = cls;
1013 struct GNUNET_CONSENSUS_Element element;
1015 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
1016 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
1018 neighbor->consensus_insertion_offset = 0;
1019 neighbor->consensus_insertion_distance++;
1020 /* skip over NULL entries */
1021 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
1022 (consensi[neighbor->consensus_insertion_distance].array_length < neighbor->consensus_insertion_offset) &&
1023 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
1024 neighbor->consensus_insertion_offset++;
1026 if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
1028 /* we're done, conclude! */
1029 GNUNET_CONSENSUS_conclude (neighbor->consensus,
1030 GNUNET_DV_CONSENSUS_FREQUENCY,
1035 element.size = sizeof (struct Target);
1036 element.data = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset++]->target;
1038 /* skip over NULL entries */
1039 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
1040 (consensi[neighbor->consensus_insertion_distance].array_length < neighbor->consensus_insertion_offset) &&
1041 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
1042 neighbor->consensus_insertion_offset++;
1043 GNUNET_CONSENSUS_insert (neighbor->consensus,
1045 &insert_next_element,
1051 * We have learned a new route from the other peer. Add it to the
1052 * route set we're building.
1054 * @param cls the 'struct DirectNeighbor' we're building the consensus with
1055 * @param element the new element we have learned
1056 * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
1057 * GNUNET_SYSERR if the element should be ignored and not be propagated
1060 learn_route_cb (void *cls,
1061 const struct GNUNET_CONSENSUS_Element *element)
1063 struct DirectNeighbor *neighbor = cls;
1064 struct Target *target;
1066 if (NULL == element)
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1069 "Failed to establish DV consensus, will try again later\n");
1070 GNUNET_CONSENSUS_destroy (neighbor->consensus);
1071 if (NULL != neighbor->neighbor_table_consensus)
1073 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1076 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table_consensus);
1077 neighbor->neighbor_table_consensus = NULL;
1079 neighbor->consensus = NULL;
1080 neighbor->consensus_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1083 return GNUNET_SYSERR;
1085 if (sizeof (struct Target) != element->size)
1087 GNUNET_break_op (0);
1088 return GNUNET_SYSERR;
1090 target = GNUNET_malloc (sizeof (struct Target));
1091 memcpy (target, element->data, sizeof (struct Target));
1093 GNUNET_CONTAINER_multihashmap_put (neighbor->neighbor_table_consensus,
1094 &target->peer.hashPubKey,
1096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1098 GNUNET_break_op (0);
1099 GNUNET_free (target);
1100 return GNUNET_SYSERR;
1107 * Start creating a new consensus from scratch.
1109 * @param cls the 'struct DirectNeighbor' of the peer we're building
1110 * a routing consensus with
1111 * @param tc scheduler context
1114 start_consensus (void *cls,
1115 const struct GNUNET_SCHEDULER_TaskContext *tc)
1117 struct DirectNeighbor *neighbor = cls;
1118 struct GNUNET_HashCode session_id;
1119 struct GNUNET_HashCode real_session_id;
1121 neighbor->consensus_task = GNUNET_SCHEDULER_NO_TASK;
1122 neighbor->consensus_insertion_offset = 0;
1123 neighbor->consensus_insertion_distance = 0;
1124 GNUNET_assert (NULL == neighbor->neighbor_table_consensus);
1125 GNUNET_assert (NULL == neighbor->consensus);
1126 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1127 /* construct session ID seed as XOR of both peer's identities */
1128 GNUNET_CRYPTO_hash_xor (&my_identity.hashPubKey,
1129 &neighbor->peer.hashPubKey,
1131 /* make sure session ID is unique across applications by salting it with 'DV' */
1132 GNUNET_CRYPTO_hkdf (&real_session_id, sizeof (real_session_id),
1133 GCRY_MD_SHA512, GCRY_MD_SHA256,
1135 &session_id, sizeof (session_id),
1137 neighbor->consensus = GNUNET_CONSENSUS_create (cfg,
1143 if (NULL == neighbor->consensus)
1145 neighbor->consensus_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1150 insert_next_element (neighbor, GNUNET_OK);
1155 * Core handler for DV data messages. Whatever this message
1156 * contains all we really have to do is rip it out of its
1157 * DV layering and give it to our pal the DV plugin to report
1160 * @param cls closure
1161 * @param peer peer which sent the message (immediate sender)
1162 * @param message the message
1163 * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
1166 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1167 const struct GNUNET_MessageHeader *message)
1169 const struct RouteMessage *rm;
1170 const struct GNUNET_MessageHeader *payload;
1171 struct Route *route;
1173 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1175 GNUNET_break_op (0);
1176 return GNUNET_SYSERR;
1178 rm = (const struct RouteMessage *) message;
1179 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1180 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1182 GNUNET_break_op (0);
1183 return GNUNET_SYSERR;
1185 if (0 == memcmp (&rm->target,
1187 sizeof (struct GNUNET_PeerIdentity)))
1189 /* message is for me, check reverse route! */
1190 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1191 &rm->sender.hashPubKey);
1194 /* don't have reverse route, drop */
1195 GNUNET_STATISTICS_update (stats,
1196 "# message discarded (no reverse route)",
1200 send_data_to_plugin (payload,
1202 ntohl (route->target.distance));
1205 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1206 &rm->target.hashPubKey);
1209 GNUNET_STATISTICS_update (stats,
1210 "# messages discarded (no route)",
1214 if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
1216 GNUNET_STATISTICS_update (stats,
1217 "# messages discarded (target too far)",
1221 forward_payload (route->next_hop,
1222 ntohl (route->target.distance),
1232 * Service server's handler for message send requests (which come
1233 * bubbling up to us through the DV plugin).
1235 * @param cls closure
1236 * @param client identification of the client
1237 * @param message the actual message
1240 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1241 const struct GNUNET_MessageHeader *message)
1243 struct Route *route;
1244 const struct GNUNET_DV_SendMessage *msg;
1245 const struct GNUNET_MessageHeader *payload;
1247 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1250 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1253 msg = (const struct GNUNET_DV_SendMessage *) message;
1254 GNUNET_break (0 != ntohl (msg->uid));
1255 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1256 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1262 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1263 &msg->target.hashPubKey);
1266 /* got disconnected */
1267 GNUNET_STATISTICS_update (stats,
1268 "# local messages discarded (no route)",
1270 send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1274 forward_payload (route->next_hop,
1275 ntohl (route->target.distance),
1280 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1285 * Multihashmap iterator for freeing routes that go via a particular
1286 * neighbor that disconnected and is thus no longer available.
1288 * @param cls the direct neighbor that is now unavailable
1289 * @param key key value stored under
1290 * @param value a 'struct Route' that may or may not go via neighbor
1292 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1295 cull_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
1297 struct DirectNeighbor *neighbor = cls;
1298 struct Route *route = value;
1300 if (route->next_hop != neighbor)
1301 return GNUNET_YES; /* not affected */
1302 GNUNET_assert (GNUNET_YES ==
1303 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
1304 release_route (route);
1305 send_disconnect_to_plugin (&route->target.peer);
1306 GNUNET_free (route);
1312 * Cleanup all of the data structures associated with a given neighbor.
1314 * @param neighbor neighbor to clean up
1317 cleanup_neighbor (struct DirectNeighbor *neighbor)
1319 struct PendingMessage *pending;
1321 while (NULL != (pending = neighbor->pm_head))
1323 neighbor->pm_queue_size--;
1324 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1327 GNUNET_free (pending);
1329 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1332 if (NULL != neighbor->cth)
1334 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1335 neighbor->cth = NULL;
1337 if (NULL != neighbor->neighbor_table_consensus)
1339 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1342 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table_consensus);
1343 neighbor->neighbor_table_consensus = NULL;
1345 if (NULL != neighbor->neighbor_table)
1347 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1350 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table);
1351 neighbor->neighbor_table = NULL;
1353 if (GNUNET_SCHEDULER_NO_TASK != neighbor->consensus_task)
1355 GNUNET_SCHEDULER_cancel (neighbor->consensus_task);
1356 neighbor->consensus_task = GNUNET_SCHEDULER_NO_TASK;
1358 if (NULL != neighbor->consensus)
1360 GNUNET_CONSENSUS_destroy (neighbor->consensus);
1361 neighbor->consensus = NULL;
1363 GNUNET_assert (GNUNET_YES ==
1364 GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
1365 &neighbor->peer.hashPubKey,
1367 GNUNET_free (neighbor);
1372 * Method called whenever a given peer disconnects.
1374 * @param cls closure
1375 * @param peer peer identity this notification is about
1378 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1380 struct DirectNeighbor *neighbor;
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "Received core peer disconnect message for peer `%s'!\n",
1385 /* Check for disconnect from self message */
1386 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1389 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
1390 if (NULL == neighbor)
1392 /* must have been a DV-neighbor, ignore */
1395 GNUNET_STATISTICS_update (stats,
1396 "# peers connected (1-hop)",
1398 cleanup_neighbor (neighbor);
1399 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1406 * Multihashmap iterator for freeing routes. Should never be called.
1409 * @param key key value stored under
1410 * @param value the route to be freed
1412 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1415 free_route (void *cls, const struct GNUNET_HashCode * key, void *value)
1417 struct Route *route = value;
1420 GNUNET_assert (GNUNET_YES ==
1421 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
1422 release_route (route);
1423 send_disconnect_to_plugin (&route->target.peer);
1424 GNUNET_free (route);
1430 * Multihashmap iterator for freeing direct neighbors. Should never be called.
1433 * @param key key value stored under
1434 * @param value the direct neighbor to be freed
1436 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1439 free_direct_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
1441 struct DirectNeighbor *neighbor = value;
1444 cleanup_neighbor (neighbor);
1450 * Task run during shutdown.
1456 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1460 GNUNET_CORE_disconnect (core_api);
1462 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1463 &free_direct_neighbors, NULL);
1464 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1466 GNUNET_CONTAINER_multihashmap_destroy (direct_neighbors);
1467 GNUNET_CONTAINER_multihashmap_destroy (all_routes);
1468 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1470 GNUNET_SERVER_notification_context_destroy (nc);
1472 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1473 GNUNET_array_grow (consensi[i].targets,
1474 consensi[i].array_length,
1480 * Notify newly connected client about an existing route.
1482 * @param cls the 'struct GNUNET_SERVER_Client'
1483 * @param key peer identity
1484 * @param value the XXX.
1485 * @return GNUNET_OK (continue to iterate)
1488 add_route (void *cls,
1489 const struct GNUNET_HashCode *key,
1492 struct GNUNET_SERVER_Client *client = cls;
1493 struct Route *route = value;
1494 struct GNUNET_DV_ConnectMessage cm;
1496 cm.header.size = htons (sizeof (cm));
1497 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1498 cm.distance = htonl (route->target.distance);
1499 cm.peer = route->target.peer;
1501 GNUNET_SERVER_notification_context_unicast (nc,
1510 * Handle START-message. This is the first message sent to us
1511 * by the client (can only be one!).
1513 * @param cls closure (always NULL)
1514 * @param client identification of the client
1515 * @param message the actual message
1518 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1519 const struct GNUNET_MessageHeader *message)
1521 GNUNET_SERVER_notification_context_add (nc, client);
1522 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1523 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1530 * Called on core init.
1533 * @param server legacy
1534 * @param identity this peer's identity
1537 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1538 const struct GNUNET_PeerIdentity *identity)
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 GNUNET_i2s (identity));
1543 my_identity = *identity;
1548 * Process dv requests.
1550 * @param cls closure
1551 * @param server the initialized server
1552 * @param c configuration to use
1555 run (void *cls, struct GNUNET_SERVER_Handle *server,
1556 const struct GNUNET_CONFIGURATION_Handle *c)
1558 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1559 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
1562 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1563 {&handle_start, NULL,
1564 GNUNET_MESSAGE_TYPE_DV_START,
1565 sizeof (struct GNUNET_MessageHeader) },
1566 { &handle_dv_send_message, NULL,
1567 GNUNET_MESSAGE_TYPE_DV_SEND,
1573 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1574 all_routes = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
1575 core_api = GNUNET_CORE_connect (cfg, NULL,
1577 &handle_core_connect,
1578 &handle_core_disconnect,
1583 if (NULL == core_api)
1585 nc = GNUNET_SERVER_notification_context_create (server,
1586 MAX_QUEUE_SIZE_PLUGIN);
1587 stats = GNUNET_STATISTICS_create ("dv", cfg);
1588 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1589 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1590 &shutdown_task, NULL);
1595 * The main function for the dv service.
1597 * @param argc number of arguments from the command line
1598 * @param argv command line arguments
1599 * @return 0 ok, 1 on error
1602 main (int argc, char *const *argv)
1604 return (GNUNET_OK ==
1605 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
1606 &run, NULL)) ? 0 : 1;
1609 /* end of gnunet-service-dv.c */