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_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 * 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 * Session ID we use whenever we create a set union with
174 * this neighbor; constructed from the XOR of our peer
175 * IDs and then salted with "DV-SALT" to avoid conflicts
176 * with other applications.
178 struct GNUNET_HashCode real_session_id;
181 * Head of linked list of messages to send to this peer.
183 struct PendingMessage *pm_head;
186 * Tail of linked list of messages to send to this peer.
188 struct PendingMessage *pm_tail;
191 * Transmit handle to core service.
193 struct GNUNET_CORE_TransmitHandle *cth;
196 * Routing table of the neighbor, NULL if not yet established.
197 * Keys are peer identities, values are 'struct Target' entries.
198 * Note that the distances in the targets are from the point-of-view
199 * of the peer, not from us!
201 struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table;
204 * Updated routing table of the neighbor, under construction,
205 * NULL if we are not currently building it.
206 * Keys are peer identities, values are 'struct Target' entries.
207 * Note that the distances in the targets are from the point-of-view
208 * of the peer, not from us!
210 struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table_consensus;
213 * Our current (exposed) routing table as a set.
215 struct GNUNET_SET_Handle *my_set;
218 * Handle for our current active set union operation.
220 struct GNUNET_SET_OperationHandle *set_op;
223 * Handle used if we are listening for this peer, waiting for the
224 * other peer to initiate construction of the set union. NULL if
225 * we ar the initiating peer.
227 struct GNUNET_SET_ListenHandle *listen_handle;
230 * ID of the task we use to (periodically) update our consensus
231 * with this peer. Used if we are the initiating peer.
233 GNUNET_SCHEDULER_TaskIdentifier initiate_task;
236 * At what offset are we, with respect to inserting our own routes
237 * into the consensus?
239 unsigned int consensus_insertion_offset;
242 * At what distance are we, with respect to inserting our own routes
243 * into the consensus?
245 unsigned int consensus_insertion_distance;
248 * Number of messages currently in the 'pm_XXXX'-DLL.
250 unsigned int pm_queue_size;
253 * Elements in consensus
255 unsigned int consensus_elements;
258 * Direct one hop route
260 struct Route *direct_route;
263 * Flag set within 'check_target_removed' to trigger full global route refresh.
268 * Our distance to this peer, 0 for unknown.
273 * The network this peer is in
278 * Is this neighbor connected at the core level?
286 * A route includes information about the next hop,
287 * the target, and the ultimate distance to the
294 * Which peer do we need to forward the message to?
296 struct DirectNeighbor *next_hop;
299 * What would be the target, and how far is it away?
301 struct Target target;
304 * Offset of this target in the respective consensus set.
306 unsigned int set_offset;
312 * Set of targets we bring to a consensus; all targets in a set have a
313 * distance equal to the sets distance (which is implied by the array
320 * Array of targets in the set, may include NULL entries if a
321 * neighbor has disconnected; the targets are allocated with the
322 * respective container (all_routes), not here.
324 struct Route **targets;
327 * Size of the @e targets array.
329 unsigned int array_length;
335 * Peermap of all of our neighbors; processing these usually requires
336 * first checking to see if the peer is core-connected and if the
337 * distance is 1, in which case they are direct neighbors.
339 static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
342 * Hashmap with all routes that we currently support; contains
343 * routing information for all peers from distance 2
344 * up to distance DEFAULT_FISHEYE_DEPTH.
346 static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
349 * Array of consensus sets we expose to the outside world. Sets
350 * are structured by the distance to the target.
352 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
355 * Handle to the core service api.
357 static struct GNUNET_CORE_Handle *core_api;
360 * The identity of our peer.
362 static struct GNUNET_PeerIdentity my_identity;
365 * The configuration for this service.
367 static const struct GNUNET_CONFIGURATION_Handle *cfg;
370 * The client, the DV plugin connected to us (or an event monitor).
371 * Hopefully this client will never change, although if the plugin
372 * dies and returns for some reason it may happen.
374 static struct GNUNET_SERVER_NotificationContext *nc;
377 * Handle for the statistics service.
379 static struct GNUNET_STATISTICS_Handle *stats;
382 * Handle to ATS service.
384 static struct GNUNET_ATS_PerformanceHandle *ats;
387 * Task scheduled to refresh routes based on direct neighbours.
389 static GNUNET_SCHEDULER_TaskIdentifier rr_task;
392 * #GNUNET_YES if we are shutting down.
394 static int in_shutdown;
397 * Start creating a new DV set union by initiating the connection.
399 * @param cls the 'struct DirectNeighbor' of the peer we're building
400 * a routing consensus with
401 * @param tc scheduler context
404 initiate_set_union (void *cls,
405 const struct GNUNET_SCHEDULER_TaskContext *tc);
409 * Start creating a new DV set union construction, our neighbour has
410 * asked for it (callback for listening peer).
412 * @param cls the 'struct DirectNeighbor' of the peer we're building
413 * a routing consensus with
414 * @param other_peer the other peer
415 * @param context_msg message with application specific information from
417 * @param request request from the other peer, use GNUNET_SET_accept
418 * to accept it, otherwise the request will be refused
419 * Note that we don't use a return value here, as it is also
420 * necessary to specify the set we want to do the operation with,
421 * whith sometimes can be derived from the context message.
422 * Also necessary to specify the timeout.
425 listen_set_union (void *cls,
426 const struct GNUNET_PeerIdentity *other_peer,
427 const struct GNUNET_MessageHeader *context_msg,
428 struct GNUNET_SET_Request *request);
432 * Forward a message from another peer to the plugin.
434 * @param message the message to send to the plugin
435 * @param origin the original sender of the message
436 * @param distance distance to the original sender of the message
439 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
440 const struct GNUNET_PeerIdentity *origin,
443 struct GNUNET_DV_ReceivedMessage *received_msg;
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Delivering message from peer `%s'\n",
448 GNUNET_i2s (origin));
449 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
450 ntohs (message->size);
451 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
453 GNUNET_break (0); /* too big */
456 received_msg = GNUNET_malloc (size);
457 received_msg->header.size = htons (size);
458 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
459 received_msg->distance = htonl (distance);
460 received_msg->sender = *origin;
461 memcpy (&received_msg[1], message, ntohs (message->size));
462 GNUNET_SERVER_notification_context_broadcast (nc,
463 &received_msg->header,
465 GNUNET_free (received_msg);
470 * Forward a control message to the plugin.
472 * @param message the message to send to the plugin
475 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
477 GNUNET_SERVER_notification_context_broadcast (nc,
484 * Give an (N)ACK message to the plugin, we transmitted a message for it.
486 * @param target peer that received the message
487 * @param uid plugin-chosen UID for the message
488 * @param nack GNUNET_NO to send ACK, GNUNET_YES to send NACK
491 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
495 struct GNUNET_DV_AckMessage ack_msg;
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Delivering ACK for message to peer `%s'\n",
499 GNUNET_i2s (target));
500 ack_msg.header.size = htons (sizeof (ack_msg));
501 ack_msg.header.type = htons ((GNUNET_YES == nack)
502 ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
503 : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
504 ack_msg.uid = htonl (uid);
505 ack_msg.target = *target;
506 send_control_to_plugin (&ack_msg.header);
511 * Send a DISTANCE_CHANGED message to the plugin.
513 * @param peer peer with a changed distance
514 * @param distance new distance to the peer
517 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
520 struct GNUNET_DV_DistanceUpdateMessage du_msg;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
525 du_msg.header.size = htons (sizeof (du_msg));
526 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
527 du_msg.distance = htonl (distance);
529 send_control_to_plugin (&du_msg.header);
534 * Give a CONNECT message to the plugin.
536 * @param target peer that connected
537 * @param distance distance to the target
538 * @param network the network the next hop is located in
541 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
542 uint32_t distance, uint32_t network)
544 struct GNUNET_DV_ConnectMessage cm;
546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
547 "Delivering CONNECT about peer `%s' with distance %u\n",
548 GNUNET_i2s (target), distance);
549 cm.header.size = htons (sizeof (cm));
550 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
551 cm.distance = htonl (distance);
552 cm.network = htonl (network);
554 send_control_to_plugin (&cm.header);
559 * Give a DISCONNECT message to the plugin.
561 * @param target peer that disconnected
564 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
566 struct GNUNET_DV_DisconnectMessage dm;
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Delivering DISCONNECT about peer `%s'\n",
570 GNUNET_i2s (target));
571 dm.header.size = htons (sizeof (dm));
572 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
573 dm.reserved = htonl (0);
575 send_control_to_plugin (&dm.header);
580 * Function called to transfer a message to another peer
583 * @param cls closure with the direct neighbor
584 * @param size number of bytes available in buf
585 * @param buf where the callee should write the message
586 * @return number of bytes written to buf
589 core_transmit_notify (void *cls, size_t size, void *buf)
591 struct DirectNeighbor *dn = cls;
593 struct PendingMessage *pending;
600 /* peer disconnected */
604 while ( (NULL != (pending = dn->pm_head)) &&
605 (size >= off + (msize = ntohs (pending->msg->size))))
608 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
611 memcpy (&cbuf[off], pending->msg, msize);
612 if (0 != pending->uid)
613 send_ack_to_plugin (&pending->ultimate_target,
616 GNUNET_free (pending);
619 GNUNET_assert (NULL != core_api);
620 if (NULL != dn->pm_head)
622 GNUNET_CORE_notify_transmit_ready (core_api,
623 GNUNET_YES /* cork */,
625 GNUNET_TIME_UNIT_FOREVER_REL,
628 &core_transmit_notify, dn);
634 * Forward the given payload to the given target.
636 * @param target where to send the message
637 * @param uid unique ID for the message
638 * @param ultimate_target ultimate recipient for the message
639 * @param distance expected (remaining) distance to the target
640 * @param sender original sender of the message
641 * @param payload payload of the message
644 forward_payload (struct DirectNeighbor *target,
647 const struct GNUNET_PeerIdentity *sender,
648 const struct GNUNET_PeerIdentity *ultimate_target,
649 const struct GNUNET_MessageHeader *payload)
651 struct PendingMessage *pm;
652 struct RouteMessage *rm;
655 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
656 (0 != memcmp (sender,
658 sizeof (struct GNUNET_PeerIdentity))) )
660 GNUNET_break (0 == uid);
663 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
664 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
669 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
670 pm->ultimate_target = *ultimate_target;
672 pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
673 rm = (struct RouteMessage *) &pm[1];
674 rm->header.size = htons ((uint16_t) msize);
675 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
676 rm->distance = htonl (distance);
677 rm->target = target->peer;
678 rm->sender = *sender;
679 memcpy (&rm[1], payload, ntohs (payload->size));
680 GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
683 target->pm_queue_size++;
684 GNUNET_assert (NULL != core_api);
685 if (NULL == target->cth)
686 target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
687 GNUNET_YES /* cork */,
689 GNUNET_TIME_UNIT_FOREVER_REL,
692 &core_transmit_notify, target);
697 * Find a free slot for storing a 'route' in the 'consensi'
698 * set at the given distance.
700 * @param distance distance to use for the set slot
703 get_consensus_slot (uint32_t distance)
705 struct ConsensusSet *cs;
708 cs = &consensi[distance];
710 while ( (i < cs->array_length) &&
711 (NULL != cs->targets[i]) ) i++;
712 if (i == cs->array_length)
714 GNUNET_array_grow (cs->targets,
716 cs->array_length * 2 + 2);
723 * Allocate a slot in the consensus set for a route.
725 * @param route route to initialize
726 * @param distance which consensus set to use
729 allocate_route (struct Route *route,
734 i = get_consensus_slot (distance);
735 route->set_offset = i;
736 consensi[distance].targets[i] = route;
737 route->target.distance = htonl (distance);
742 * Release a slot in the consensus set for a route.
744 * @param route route to release the slot from
747 release_route (struct Route *route)
749 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
750 route->set_offset = UINT_MAX; /* indicate invalid slot */
755 * Move a route from one consensus set to another.
757 * @param route route to move
758 * @param new_distance new distance for the route (destination set)
761 move_route (struct Route *route,
762 uint32_t new_distance)
766 release_route (route);
767 i = get_consensus_slot (new_distance);
768 route->set_offset = i;
769 consensi[new_distance].targets[i] = route;
770 route->target.distance = htonl (new_distance);
775 * Initialize this neighbors 'my_set' and when done give
776 * it to the pending set operation for execution.
778 * Add a single element to the set per call:
780 * If we reached the last element of a consensus element: increase distance
783 * @param cls the neighbor for which we are building the set
786 build_set (void *cls)
788 struct DirectNeighbor *neighbor = cls;
789 struct GNUNET_SET_Element element;
790 struct Target *target;
792 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
793 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
795 /* If we reached the last element of a consensus array element: increase distance and start with next array */
796 neighbor->consensus_insertion_offset = 0;
797 neighbor->consensus_insertion_distance++;
798 /* skip over NULL entries */
799 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
800 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
801 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
802 neighbor->consensus_insertion_offset++;
804 if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
806 /* we have added all elements to the set, run the operation */
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Finished building my SET for peer `%s' with %u elements, committing\n",
809 GNUNET_i2s(&neighbor->peer),
810 neighbor->consensus_elements);
811 GNUNET_SET_commit (neighbor->set_op,
813 GNUNET_SET_destroy (neighbor->my_set);
814 neighbor->my_set = NULL;
818 target = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]->target;
819 element.size = sizeof (struct Target);
820 element.type = htons (0); /* do we need this? */
821 element.data = target;
823 /* Find next non-NULL entry */
824 neighbor->consensus_insertion_offset++;
825 /* skip over NULL entries */
826 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
827 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
828 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
830 neighbor->consensus_insertion_offset++;
833 if ( (0 != memcmp(&target->peer, &my_identity, sizeof (my_identity))) &&
834 (0 != memcmp(&target->peer, &neighbor->peer, sizeof (neighbor->peer))) )
836 /* Add target if it is not the neighbor or this peer */
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Adding peer `%s' with distance %u to SET\n",
839 GNUNET_i2s (&target->peer),
840 ntohl (target->distance));
841 GNUNET_SET_add_element (neighbor->my_set,
843 &build_set, neighbor);
844 neighbor->consensus_elements++;
852 * A peer is now connected to us at distance 1. Initiate DV exchange.
854 * @param neighbor entry for the neighbor at distance 1
857 handle_direct_connect (struct DirectNeighbor *neighbor)
860 struct GNUNET_HashCode h1;
861 struct GNUNET_HashCode h2;
862 struct GNUNET_HashCode session_id;
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "Direct connection to %s established, routing table exchange begins.\n",
866 GNUNET_i2s (&neighbor->peer));
867 GNUNET_STATISTICS_update (stats,
868 "# peers connected (1-hop)",
870 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
874 GNUNET_assert (GNUNET_YES ==
875 GNUNET_CONTAINER_multipeermap_remove (all_routes,
878 send_disconnect_to_plugin (&neighbor->peer);
879 release_route (route);
883 neighbor->direct_route = GNUNET_new (struct Route);
884 neighbor->direct_route->next_hop = neighbor;
885 neighbor->direct_route->target.peer= neighbor->peer;
886 neighbor->direct_route->target.distance = DIRECT_NEIGHBOR_COST;
887 allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Adding direct route to %s\n",
891 GNUNET_i2s (&neighbor->direct_route->target.peer));
894 /* construct session ID seed as XOR of both peer's identities */
895 GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &h1);
896 GNUNET_CRYPTO_hash (&neighbor->peer, sizeof (struct GNUNET_PeerIdentity), &h2);
897 GNUNET_CRYPTO_hash_xor (&h1,
900 /* make sure session ID is unique across applications by salting it with 'DV' */
901 GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
902 GCRY_MD_SHA512, GCRY_MD_SHA256,
904 &session_id, sizeof (session_id),
906 if (0 < memcmp (&neighbor->peer,
908 sizeof (struct GNUNET_PeerIdentity)))
910 if (NULL != neighbor->listen_handle)
915 neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
920 if (NULL != neighbor->listen_handle)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Starting SET listen operation with peer `%s'\n",
928 GNUNET_i2s(&neighbor->peer));
929 neighbor->listen_handle = GNUNET_SET_listen (cfg,
930 GNUNET_SET_OPERATION_UNION,
931 &neighbor->real_session_id,
940 * Method called whenever a peer connects.
943 * @param peer peer identity this notification is about
946 handle_core_connect (void *cls,
947 const struct GNUNET_PeerIdentity *peer)
949 struct DirectNeighbor *neighbor;
951 /* Check for connect to self message */
952 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
954 /* check if entry exists */
955 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
957 if (NULL != neighbor)
959 GNUNET_break (GNUNET_YES != neighbor->connected);
960 neighbor->connected = GNUNET_YES;
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Core connected to %s (distance %u)\n",
964 (unsigned int) neighbor->distance);
965 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
967 handle_direct_connect (neighbor);
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
971 "Core connected to %s (distance unknown)\n",
973 neighbor = GNUNET_new (struct DirectNeighbor);
974 neighbor->peer = *peer;
975 GNUNET_assert (GNUNET_YES ==
976 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
979 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
980 neighbor->connected = GNUNET_YES;
981 neighbor->distance = 0; /* unknown */
982 neighbor->network = GNUNET_ATS_NET_UNSPECIFIED;
987 * Called for each 'target' in a neighbor table to free the associated memory.
990 * @param key key of the value
991 * @param value value to free
992 * @return #GNUNET_OK to continue to iterate
995 free_targets (void *cls,
996 const struct GNUNET_PeerIdentity *key,
1005 * Multipeerhmap iterator for checking if a given route is
1006 * (now) useful to this peer.
1008 * @param cls the direct neighbor for the given route
1009 * @param key key value stored under
1010 * @param value a 'struct Target' that may or may not be useful; not that
1011 * the distance in 'target' does not include the first hop yet
1012 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1015 check_possible_route (void *cls,
1016 const struct GNUNET_PeerIdentity *key,
1019 struct DirectNeighbor *neighbor = cls;
1020 struct Target *target = value;
1021 struct Route *route;
1023 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1027 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
1029 /* this 'target' is cheaper than the existing route; switch to alternative route! */
1030 move_route (route, ntohl (target->distance) + 1);
1031 route->next_hop = neighbor;
1032 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1034 return GNUNET_YES; /* got a route to this target already */
1036 route = GNUNET_new (struct Route);
1037 route->next_hop = neighbor;
1038 route->target.distance = htonl (ntohl (target->distance) + 1);
1039 route->target.peer = target->peer;
1040 allocate_route (route, ntohl (route->target.distance));
1041 GNUNET_assert (GNUNET_YES ==
1042 GNUNET_CONTAINER_multipeermap_put (all_routes,
1043 &route->target.peer,
1045 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1046 send_connect_to_plugin (&route->target.peer, ntohl (target->distance),
1053 * Multipeermap iterator for finding routes that were previously
1054 * "hidden" due to a better route (called after a disconnect event).
1057 * @param key peer identity of the given direct neighbor
1058 * @param value a `struct DirectNeighbor` to check for additional routes
1059 * @return #GNUNET_YES to continue iteration
1062 refresh_routes (void *cls,
1063 const struct GNUNET_PeerIdentity *key,
1066 struct DirectNeighbor *neighbor = value;
1068 if ( (GNUNET_YES != neighbor->connected) ||
1069 (DIRECT_NEIGHBOR_COST != neighbor->distance) )
1071 if (NULL != neighbor->neighbor_table)
1072 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1073 &check_possible_route,
1080 * Task to run #refresh_routes() on all direct neighbours.
1086 refresh_routes_task (void *cls,
1087 const struct GNUNET_SCHEDULER_TaskContext *tc)
1089 rr_task = GNUNET_SCHEDULER_NO_TASK;
1090 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1097 * Asynchronously run #refresh_routes() at the next opportunity
1098 * on all direct neighbours.
1101 schedule_refresh_routes ()
1103 if (GNUNET_SCHEDULER_NO_TASK == rr_task)
1104 rr_task = GNUNET_SCHEDULER_add_now (&refresh_routes_task,
1110 * Get distance information from 'atsi'.
1112 * @param atsi performance data
1113 * @param atsi_count number of entries in atsi
1114 * @return connected transport distance
1117 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
1118 uint32_t atsi_count)
1122 for (i = 0; i < atsi_count; i++)
1123 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
1124 return (0 == ntohl (atsi[i].value)) ? DIRECT_NEIGHBOR_COST : ntohl (atsi[i].value); // FIXME: 0 check should not be required once ATS is fixed!
1125 /* If we do not have explicit distance data, assume direct neighbor. */
1126 return DIRECT_NEIGHBOR_COST;
1131 * Get network information from 'atsi'.
1133 * @param atsi performance data
1134 * @param atsi_count number of entries in atsi
1135 * @return connected transport network
1138 get_atsi_network (const struct GNUNET_ATS_Information *atsi,
1139 uint32_t atsi_count)
1143 for (i = 0; i < atsi_count; i++)
1144 if (ntohl (atsi[i].type) == GNUNET_ATS_NETWORK_TYPE)
1145 return ntohl (atsi[i].value);
1146 return GNUNET_ATS_NET_UNSPECIFIED;
1150 * Multipeermap iterator for freeing routes that go via a particular
1151 * neighbor that disconnected and is thus no longer available.
1153 * @param cls the direct neighbor that is now unavailable
1154 * @param key key value stored under
1155 * @param value a `struct Route` that may or may not go via neighbor
1157 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1160 cull_routes (void *cls,
1161 const struct GNUNET_PeerIdentity *key,
1164 struct DirectNeighbor *neighbor = cls;
1165 struct Route *route = value;
1167 if (route->next_hop != neighbor)
1168 return GNUNET_YES; /* not affected */
1169 GNUNET_assert (GNUNET_YES ==
1170 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1171 release_route (route);
1172 send_disconnect_to_plugin (&route->target.peer);
1173 GNUNET_free (route);
1179 * Handle the case that a direct connection to a peer is
1180 * disrupted. Remove all routes via that peer and
1181 * stop the consensus with it.
1183 * @param neighbor peer that was disconnected (or at least is no
1184 * longer at distance 1)
1187 handle_direct_disconnect (struct DirectNeighbor *neighbor)
1189 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1192 if (NULL != neighbor->cth)
1194 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1195 neighbor->cth = NULL;
1198 if (NULL != neighbor->direct_route)
1200 release_route (neighbor->direct_route);
1201 GNUNET_free (neighbor->direct_route);
1202 neighbor->direct_route = NULL;
1205 if (NULL != neighbor->neighbor_table_consensus)
1207 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1210 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1211 neighbor->neighbor_table_consensus = NULL;
1213 if (NULL != neighbor->neighbor_table)
1215 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1218 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1219 neighbor->neighbor_table = NULL;
1221 if (NULL != neighbor->set_op)
1223 GNUNET_SET_operation_cancel (neighbor->set_op);
1224 neighbor->set_op = NULL;
1226 if (NULL != neighbor->my_set)
1228 GNUNET_SET_destroy (neighbor->my_set);
1229 neighbor->my_set = NULL;
1231 if (NULL != neighbor->listen_handle)
1233 GNUNET_SET_listen_cancel (neighbor->listen_handle);
1234 neighbor->listen_handle = NULL;
1236 if (GNUNET_SCHEDULER_NO_TASK != neighbor->initiate_task)
1238 GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1239 neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1245 * Function that is called with QoS information about an address; used
1246 * to update our current distance to another peer.
1248 * @param cls closure
1249 * @param address the address
1250 * @param active is this address in active use
1251 * @param bandwidth_out assigned outbound bandwidth for the connection
1252 * @param bandwidth_in assigned inbound bandwidth for the connection
1253 * @param ats performance data for the address (as far as known)
1254 * @param ats_count number of performance records in @a ats
1257 handle_ats_update (void *cls,
1258 const struct GNUNET_HELLO_Address *address,
1260 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1261 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1262 const struct GNUNET_ATS_Information *ats,
1265 struct DirectNeighbor *neighbor;
1267 uint32_t network = GNUNET_ATS_NET_UNSPECIFIED;
1269 if (GNUNET_NO == active)
1271 distance = get_atsi_distance (ats, ats_count);
1272 network = get_atsi_network (ats, ats_count);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "ATS says distance to %s is %u\n",
1277 GNUNET_i2s (&address->peer),
1278 (unsigned int) distance);*/
1279 /* check if entry exists */
1280 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1282 if (NULL != neighbor)
1284 if (GNUNET_ATS_NET_UNSPECIFIED != network)
1285 neighbor->network = network;
1287 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1288 (DIRECT_NEIGHBOR_COST == distance) )
1289 return; /* no change */
1290 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1292 neighbor->distance = distance;
1293 GNUNET_STATISTICS_update (stats,
1294 "# peers connected (1-hop)",
1296 handle_direct_disconnect (neighbor);
1297 schedule_refresh_routes ();
1300 neighbor->distance = distance;
1301 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1303 if (GNUNET_YES != neighbor->connected)
1305 handle_direct_connect (neighbor);
1308 neighbor = GNUNET_new (struct DirectNeighbor);
1309 neighbor->peer = address->peer;
1310 GNUNET_assert (GNUNET_YES ==
1311 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1315 neighbor->connected = GNUNET_NO; /* not yet */
1316 neighbor->distance = distance;
1317 neighbor->network = network;
1322 * Check if a target was removed from the set of the other peer; if so,
1323 * if we also used it for our route, we need to remove it from our
1324 * 'all_routes' set (and later check if an alternative path now exists).
1326 * @param cls the `struct DirectNeighbor`
1327 * @param key peer identity for the target
1328 * @param value a `struct Target` previously reachable via the given neighbor
1331 check_target_removed (void *cls,
1332 const struct GNUNET_PeerIdentity *key,
1335 struct DirectNeighbor *neighbor = cls;
1336 struct Target *new_target;
1337 struct Route *current_route;
1339 new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
1341 if (NULL == new_target)
1343 /* target was revoked, check if it was used */
1344 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1346 if ( (NULL == current_route) ||
1347 (current_route->next_hop != neighbor) )
1349 /* didn't matter, wasn't used */
1352 /* remove existing route */
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Lost route to %s\n",
1355 GNUNET_i2s (¤t_route->target.peer));
1356 GNUNET_assert (GNUNET_YES ==
1357 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
1358 send_disconnect_to_plugin (¤t_route->target.peer);
1359 GNUNET_free (current_route);
1360 neighbor->target_removed = GNUNET_YES;
1368 * Check if a target was added to the set of the other peer; if it
1369 * was added or impoves the existing route, do the needed updates.
1371 * @param cls the `struct DirectNeighbor`
1372 * @param key peer identity for the target
1373 * @param value a `struct Target` now reachable via the given neighbor
1376 check_target_added (void *cls,
1377 const struct GNUNET_PeerIdentity *key,
1380 struct DirectNeighbor *neighbor = cls;
1381 struct Target *target = value;
1382 struct Route *current_route;
1384 /* target was revoked, check if it was used */
1385 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1387 if (NULL != current_route)
1390 if (current_route->next_hop == neighbor)
1392 /* we had the same route before, no change */
1393 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1395 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1396 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1400 if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
1402 /* alternative, shorter route exists, ignore */
1405 /* new route is better than the existing one, take over! */
1406 /* NOTE: minor security issue: malicious peers may advertise
1407 very short routes to take over longer paths; as we don't
1408 check that the shorter routes actually work, a malicious
1409 direct neighbor can use this to DoS our long routes */
1410 current_route->next_hop = neighbor;
1411 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1412 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417 "Discovered new route to %s using %u hops\n",
1418 GNUNET_i2s (&target->peer),
1419 (unsigned int) (ntohl (target->distance) + 1));
1420 current_route = GNUNET_new (struct Route);
1421 current_route->next_hop = neighbor;
1422 current_route->target.peer = target->peer;
1423 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1424 GNUNET_assert (GNUNET_YES ==
1425 GNUNET_CONTAINER_multipeermap_put (all_routes,
1426 ¤t_route->target.peer,
1428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1430 send_connect_to_plugin (¤t_route->target.peer,
1431 ntohl (current_route->target.distance),
1438 * Callback for set operation results. Called for each element
1439 * in the result set.
1440 * We have learned a new route from the other peer. Add it to the
1441 * route set we're building.
1443 * @param cls the `struct DirectNeighbor` we're building the consensus with
1444 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
1445 * @param status see enum GNUNET_SET_Status
1448 handle_set_union_result (void *cls,
1449 const struct GNUNET_SET_Element *element,
1450 enum GNUNET_SET_Status status)
1452 struct DirectNeighbor *neighbor = cls;
1453 struct Target *target;
1457 case GNUNET_SET_STATUS_OK:
1458 status_str = "GNUNET_SET_STATUS_OK";
1460 case GNUNET_SET_STATUS_TIMEOUT:
1461 status_str = "GNUNET_SET_STATUS_TIMEOUT";
1463 case GNUNET_SET_STATUS_FAILURE:
1464 status_str = "GNUNET_SET_STATUS_FAILURE";
1466 case GNUNET_SET_STATUS_HALF_DONE:
1467 status_str = "GNUNET_SET_STATUS_HALF_DONE";
1469 case GNUNET_SET_STATUS_DONE:
1470 status_str = "GNUNET_SET_STATUS_DONE";
1473 status_str = "UNDEFINED";
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "Got SET union result: %s\n",
1482 case GNUNET_SET_STATUS_OK:
1483 if (sizeof (struct Target) != element->size)
1485 GNUNET_break_op (0);
1488 target = GNUNET_new (struct Target);
1489 memcpy (target, element->data, sizeof (struct Target));
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Received information about peer `%s' with distance %u\n",
1492 GNUNET_i2s (&target->peer), ntohl(target->distance) + 1);
1493 if (NULL == neighbor->neighbor_table_consensus)
1494 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1496 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
1499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1501 GNUNET_break_op (0);
1502 GNUNET_free (target);
1505 case GNUNET_SET_STATUS_TIMEOUT:
1506 case GNUNET_SET_STATUS_FAILURE:
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Failed to establish DV union, will try again later\n");
1509 neighbor->set_op = NULL;
1510 if (NULL != neighbor->neighbor_table_consensus)
1512 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1515 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1516 neighbor->neighbor_table_consensus = NULL;
1518 if (0 < memcmp (&neighbor->peer,
1520 sizeof (struct GNUNET_PeerIdentity)))
1521 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1522 &initiate_set_union,
1525 case GNUNET_SET_STATUS_HALF_DONE:
1527 case GNUNET_SET_STATUS_DONE:
1528 /* we got all of our updates; integrate routing table! */
1529 neighbor->target_removed = GNUNET_NO;
1530 if (NULL == neighbor->neighbor_table_consensus)
1531 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1532 if (NULL != neighbor->neighbor_table)
1533 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1534 &check_target_removed,
1536 if (GNUNET_YES == neighbor->target_removed)
1538 /* check if we got an alternative for the removed routes */
1539 schedule_refresh_routes ();
1541 /* add targets that appeared (and check for improved routes) */
1542 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1543 &check_target_added,
1545 if (NULL != neighbor->neighbor_table)
1547 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1550 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1551 neighbor->neighbor_table = NULL;
1553 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1554 neighbor->neighbor_table_consensus = NULL;
1556 /* operation done, schedule next run! */
1557 neighbor->set_op = NULL;
1558 if (0 < memcmp (&neighbor->peer,
1560 sizeof (struct GNUNET_PeerIdentity)))
1561 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1562 &initiate_set_union,
1573 * Start creating a new DV set union construction, our neighbour has
1574 * asked for it (callback for listening peer).
1576 * @param cls the 'struct DirectNeighbor' of the peer we're building
1577 * a routing consensus with
1578 * @param other_peer the other peer
1579 * @param context_msg message with application specific information from
1581 * @param request request from the other peer, use GNUNET_SET_accept
1582 * to accept it, otherwise the request will be refused
1583 * Note that we don't use a return value here, as it is also
1584 * necessary to specify the set we want to do the operation with,
1585 * whith sometimes can be derived from the context message.
1586 * Also necessary to specify the timeout.
1589 listen_set_union (void *cls,
1590 const struct GNUNET_PeerIdentity *other_peer,
1591 const struct GNUNET_MessageHeader *context_msg,
1592 struct GNUNET_SET_Request *request)
1594 struct DirectNeighbor *neighbor = cls;
1596 if (NULL == request)
1597 return; /* why??? */
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Starting to create consensus with %s\n",
1600 GNUNET_i2s (&neighbor->peer));
1601 if (NULL != neighbor->set_op)
1603 GNUNET_SET_operation_cancel (neighbor->set_op);
1604 neighbor->set_op = NULL;
1606 if (NULL != neighbor->my_set)
1608 GNUNET_SET_destroy (neighbor->my_set);
1609 neighbor->my_set = NULL;
1611 neighbor->my_set = GNUNET_SET_create (cfg,
1612 GNUNET_SET_OPERATION_UNION);
1613 neighbor->set_op = GNUNET_SET_accept (request,
1614 GNUNET_SET_RESULT_ADDED,
1615 &handle_set_union_result,
1617 neighbor->consensus_insertion_offset = 0;
1618 neighbor->consensus_insertion_distance = 0;
1619 neighbor->consensus_elements = 0;
1620 build_set (neighbor);
1625 * Start creating a new DV set union by initiating the connection.
1627 * @param cls the 'struct DirectNeighbor' of the peer we're building
1628 * a routing consensus with
1629 * @param tc scheduler context
1632 initiate_set_union (void *cls,
1633 const struct GNUNET_SCHEDULER_TaskContext *tc)
1635 struct DirectNeighbor *neighbor = cls;
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Initiating SET union with peer `%s'\n",
1639 GNUNET_i2s (&neighbor->peer));
1640 neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1641 neighbor->my_set = GNUNET_SET_create (cfg,
1642 GNUNET_SET_OPERATION_UNION);
1643 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1644 &neighbor->real_session_id,
1646 0 /* FIXME: salt */,
1647 GNUNET_SET_RESULT_ADDED,
1648 &handle_set_union_result,
1650 neighbor->consensus_insertion_offset = 0;
1651 neighbor->consensus_insertion_distance = 0;
1652 neighbor->consensus_elements = 0;
1653 build_set (neighbor);
1658 * Core handler for DV data messages. Whatever this message
1659 * contains all we really have to do is rip it out of its
1660 * DV layering and give it to our pal the DV plugin to report
1663 * @param cls closure
1664 * @param peer peer which sent the message (immediate sender)
1665 * @param message the message
1666 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the other peer violated the protocol
1669 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1670 const struct GNUNET_MessageHeader *message)
1672 const struct RouteMessage *rm;
1673 const struct GNUNET_MessageHeader *payload;
1674 struct Route *route;
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Handling DV message\n");
1678 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1680 GNUNET_break_op (0);
1681 return GNUNET_SYSERR;
1683 rm = (const struct RouteMessage *) message;
1684 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1685 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1687 GNUNET_break_op (0);
1688 return GNUNET_SYSERR;
1690 if (0 == memcmp (&rm->target,
1692 sizeof (struct GNUNET_PeerIdentity)))
1694 /* message is for me, check reverse route! */
1695 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1699 /* don't have reverse route, drop */
1700 GNUNET_STATISTICS_update (stats,
1701 "# message discarded (no reverse route)",
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Delivering %u bytes to myself!\n",
1707 ntohs (payload->size));
1708 send_data_to_plugin (payload,
1710 ntohl (route->target.distance));
1713 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1717 GNUNET_STATISTICS_update (stats,
1718 "# messages discarded (no route)",
1722 if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
1724 GNUNET_STATISTICS_update (stats,
1725 "# messages discarded (target too far)",
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "Forwarding message to %s\n",
1731 GNUNET_i2s (&rm->target));
1732 forward_payload (route->next_hop,
1733 ntohl (route->target.distance),
1743 * Service server's handler for message send requests (which come
1744 * bubbling up to us through the DV plugin).
1746 * @param cls closure
1747 * @param client identification of the client
1748 * @param message the actual message
1751 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1752 const struct GNUNET_MessageHeader *message)
1754 struct Route *route;
1755 const struct GNUNET_DV_SendMessage *msg;
1756 const struct GNUNET_MessageHeader *payload;
1758 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1764 msg = (const struct GNUNET_DV_SendMessage *) message;
1765 GNUNET_break (0 != ntohl (msg->uid));
1766 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1767 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1773 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1777 /* got disconnected */
1778 GNUNET_STATISTICS_update (stats,
1779 "# local messages discarded (no route)",
1781 send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1782 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Forwarding %u bytes to %s\n",
1787 ntohs (payload->size),
1788 GNUNET_i2s (&msg->target));
1790 forward_payload (route->next_hop,
1791 ntohl (route->target.distance),
1796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1801 * Cleanup all of the data structures associated with a given neighbor.
1803 * @param neighbor neighbor to clean up
1806 cleanup_neighbor (struct DirectNeighbor *neighbor)
1808 struct PendingMessage *pending;
1810 while (NULL != (pending = neighbor->pm_head))
1812 neighbor->pm_queue_size--;
1813 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1816 GNUNET_free (pending);
1818 handle_direct_disconnect (neighbor);
1819 GNUNET_assert (GNUNET_YES ==
1820 GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1823 GNUNET_free (neighbor);
1828 * Method called whenever a given peer disconnects.
1830 * @param cls closure
1831 * @param peer peer identity this notification is about
1834 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1836 struct DirectNeighbor *neighbor;
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 "Received core peer disconnect message for peer `%s'!\n",
1841 /* Check for disconnect from self message */
1842 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1845 GNUNET_CONTAINER_multipeermap_get (direct_neighbors, peer);
1846 if (NULL == neighbor)
1851 GNUNET_break (GNUNET_YES == neighbor->connected);
1852 neighbor->connected = GNUNET_NO;
1853 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1856 GNUNET_STATISTICS_update (stats,
1857 "# peers connected (1-hop)",
1860 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;
1907 cleanup_neighbor (neighbor);
1913 * Task run during shutdown.
1919 shutdown_task (void *cls,
1920 const struct GNUNET_SCHEDULER_TaskContext *tc)
1924 in_shutdown = GNUNET_YES;
1925 GNUNET_assert (NULL != core_api);
1926 GNUNET_CORE_disconnect (core_api);
1928 GNUNET_ATS_performance_done (ats);
1930 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1931 &free_direct_neighbors, NULL);
1932 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1934 GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
1935 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1936 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1938 GNUNET_SERVER_notification_context_destroy (nc);
1940 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1942 GNUNET_array_grow (consensi[i].targets,
1943 consensi[i].array_length,
1946 if (GNUNET_SCHEDULER_NO_TASK != rr_task)
1948 GNUNET_SCHEDULER_cancel (rr_task);
1949 rr_task = GNUNET_SCHEDULER_NO_TASK;
1955 * Notify newly connected client about an existing route.
1957 * @param cls the `struct GNUNET_SERVER_Client *`
1958 * @param key peer identity
1959 * @param value the `struct Route *`
1960 * @return #GNUNET_OK (continue to iterate)
1963 add_route (void *cls,
1964 const struct GNUNET_PeerIdentity *key,
1967 struct GNUNET_SERVER_Client *client = cls;
1968 struct Route *route = value;
1969 struct GNUNET_DV_ConnectMessage cm;
1971 memset (&cm, 0, sizeof (cm));
1972 cm.header.size = htons (sizeof (cm));
1973 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1974 cm.distance = htonl (route->target.distance);
1975 cm.peer = route->target.peer;
1977 GNUNET_SERVER_notification_context_unicast (nc,
1986 * Handle START-message. This is the first message sent to us
1987 * by the client (can only be one!).
1989 * @param cls closure (always NULL)
1990 * @param client identification of the client
1991 * @param message the actual message
1994 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1995 const struct GNUNET_MessageHeader *message)
1997 GNUNET_SERVER_notification_context_add (nc, client);
1998 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1999 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2006 * Called on core init.
2009 * @param identity this peer's identity
2012 core_init (void *cls,
2013 const struct GNUNET_PeerIdentity *identity)
2015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 GNUNET_i2s (identity));
2018 my_identity = *identity;
2023 * Process dv requests.
2025 * @param cls closure
2026 * @param server the initialized server
2027 * @param c configuration to use
2030 run (void *cls, struct GNUNET_SERVER_Handle *server,
2031 const struct GNUNET_CONFIGURATION_Handle *c)
2033 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2034 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
2037 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2038 {&handle_start, NULL,
2039 GNUNET_MESSAGE_TYPE_DV_START,
2040 sizeof (struct GNUNET_MessageHeader) },
2041 { &handle_dv_send_message, NULL,
2042 GNUNET_MESSAGE_TYPE_DV_SEND,
2046 in_shutdown = GNUNET_NO;
2048 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2049 all_routes = GNUNET_CONTAINER_multipeermap_create (65536, GNUNET_NO);
2050 core_api = GNUNET_CORE_connect (cfg, NULL,
2052 &handle_core_connect,
2053 &handle_core_disconnect,
2058 if (NULL == core_api)
2060 ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
2063 GNUNET_CORE_disconnect (core_api);
2067 nc = GNUNET_SERVER_notification_context_create (server,
2068 MAX_QUEUE_SIZE_PLUGIN);
2069 stats = GNUNET_STATISTICS_create ("dv", cfg);
2070 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2071 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2072 &shutdown_task, NULL);
2077 * The main function for the dv service.
2079 * @param argc number of arguments from the command line
2080 * @param argv command line arguments
2081 * @return 0 ok, 1 on error
2084 main (int argc, char *const *argv)
2086 return (GNUNET_OK ==
2087 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
2088 &run, NULL)) ? 0 : 1;
2091 /* end of gnunet-service-dv.c */