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 * Is this neighbor connected at the core level?
281 * A route includes information about the next hop,
282 * the target, and the ultimate distance to the
289 * Which peer do we need to forward the message to?
291 struct DirectNeighbor *next_hop;
294 * What would be the target, and how far is it away?
296 struct Target target;
299 * Offset of this target in the respective consensus set.
301 unsigned int set_offset;
307 * Set of targets we bring to a consensus; all targets in a set have a
308 * distance equal to the sets distance (which is implied by the array
315 * Array of targets in the set, may include NULL entries if a
316 * neighbor has disconnected; the targets are allocated with the
317 * respective container (all_routes), not here.
319 struct Route **targets;
322 * Size of the 'targets' array.
324 unsigned int array_length;
330 * Peermap of all of our neighbors; processing these usually requires
331 * first checking to see if the peer is core-connected and if the
332 * distance is 1, in which case they are direct neighbors.
334 static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
337 * Hashmap with all routes that we currently support; contains
338 * routing information for all peers from distance 2
339 * up to distance DEFAULT_FISHEYE_DEPTH.
341 static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
344 * Array of consensus sets we expose to the outside world. Sets
345 * are structured by the distance to the target.
347 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
350 * Handle to the core service api.
352 static struct GNUNET_CORE_Handle *core_api;
355 * The identity of our peer.
357 static struct GNUNET_PeerIdentity my_identity;
360 * The configuration for this service.
362 static const struct GNUNET_CONFIGURATION_Handle *cfg;
365 * The client, the DV plugin connected to us (or an event monitor).
366 * Hopefully this client will never change, although if the plugin
367 * dies and returns for some reason it may happen.
369 static struct GNUNET_SERVER_NotificationContext *nc;
372 * Handle for the statistics service.
374 static struct GNUNET_STATISTICS_Handle *stats;
377 * Handle to ATS service.
379 static struct GNUNET_ATS_PerformanceHandle *ats;
383 * Start creating a new DV set union by initiating the connection.
385 * @param cls the 'struct DirectNeighbor' of the peer we're building
386 * a routing consensus with
387 * @param tc scheduler context
390 initiate_set_union (void *cls,
391 const struct GNUNET_SCHEDULER_TaskContext *tc);
395 * Start creating a new DV set union construction, our neighbour has
396 * asked for it (callback for listening peer).
398 * @param cls the 'struct DirectNeighbor' of the peer we're building
399 * a routing consensus with
400 * @param other_peer the other peer
401 * @param context_msg message with application specific information from
403 * @param request request from the other peer, use GNUNET_SET_accept
404 * to accept it, otherwise the request will be refused
405 * Note that we don't use a return value here, as it is also
406 * necessary to specify the set we want to do the operation with,
407 * whith sometimes can be derived from the context message.
408 * Also necessary to specify the timeout.
411 listen_set_union (void *cls,
412 const struct GNUNET_PeerIdentity *other_peer,
413 const struct GNUNET_MessageHeader *context_msg,
414 struct GNUNET_SET_Request *request);
418 * Forward a message from another peer to the plugin.
420 * @param message the message to send to the plugin
421 * @param origin the original sender of the message
422 * @param distance distance to the original sender of the message
425 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
426 const struct GNUNET_PeerIdentity *origin,
429 struct GNUNET_DV_ReceivedMessage *received_msg;
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Delivering message from peer `%s'\n",
434 GNUNET_i2s (origin));
435 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
436 ntohs (message->size);
437 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
439 GNUNET_break (0); /* too big */
442 received_msg = GNUNET_malloc (size);
443 received_msg->header.size = htons (size);
444 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
445 received_msg->distance = htonl (distance);
446 received_msg->sender = *origin;
447 memcpy (&received_msg[1], message, ntohs (message->size));
448 GNUNET_SERVER_notification_context_broadcast (nc,
449 &received_msg->header,
451 GNUNET_free (received_msg);
456 * Forward a control message to the plugin.
458 * @param message the message to send to the plugin
461 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
463 GNUNET_SERVER_notification_context_broadcast (nc,
470 * Give an (N)ACK message to the plugin, we transmitted a message for it.
472 * @param target peer that received the message
473 * @param uid plugin-chosen UID for the message
474 * @param nack GNUNET_NO to send ACK, GNUNET_YES to send NACK
477 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
481 struct GNUNET_DV_AckMessage ack_msg;
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Delivering ACK for message to peer `%s'\n",
485 GNUNET_i2s (target));
486 ack_msg.header.size = htons (sizeof (ack_msg));
487 ack_msg.header.type = htons ((GNUNET_YES == nack)
488 ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
489 : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
490 ack_msg.uid = htonl (uid);
491 ack_msg.target = *target;
492 send_control_to_plugin (&ack_msg.header);
497 * Send a DISTANCE_CHANGED message to the plugin.
499 * @param peer peer with a changed distance
500 * @param distance new distance to the peer
503 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
506 struct GNUNET_DV_DistanceUpdateMessage du_msg;
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
511 du_msg.header.size = htons (sizeof (du_msg));
512 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
513 du_msg.distance = htonl (distance);
515 send_control_to_plugin (&du_msg.header);
520 * Give a CONNECT message to the plugin.
522 * @param target peer that connected
523 * @param distance distance to the target
526 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
529 struct GNUNET_DV_ConnectMessage cm;
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Delivering CONNECT about peer `%s'\n",
533 GNUNET_i2s (target));
534 cm.header.size = htons (sizeof (cm));
535 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
536 cm.distance = htonl (distance);
538 send_control_to_plugin (&cm.header);
543 * Give a DISCONNECT message to the plugin.
545 * @param target peer that disconnected
548 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
550 struct GNUNET_DV_DisconnectMessage dm;
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Delivering DISCONNECT about peer `%s'\n",
554 GNUNET_i2s (target));
555 dm.header.size = htons (sizeof (dm));
556 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
557 dm.reserved = htonl (0);
559 send_control_to_plugin (&dm.header);
564 * Function called to transfer a message to another peer
567 * @param cls closure with the direct neighbor
568 * @param size number of bytes available in buf
569 * @param buf where the callee should write the message
570 * @return number of bytes written to buf
573 core_transmit_notify (void *cls, size_t size, void *buf)
575 struct DirectNeighbor *dn = cls;
577 struct PendingMessage *pending;
584 /* peer disconnected */
588 pending = dn->pm_head;
590 while ( (NULL != (pending = dn->pm_head)) &&
591 (size >= off + (msize = ntohs (pending->msg->size))))
594 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
597 memcpy (&cbuf[off], pending->msg, msize);
598 if (0 != pending->uid)
599 send_ack_to_plugin (&pending->ultimate_target,
602 GNUNET_free (pending);
605 if (NULL != dn->pm_head)
607 GNUNET_CORE_notify_transmit_ready (core_api,
608 GNUNET_YES /* cork */,
610 GNUNET_TIME_UNIT_FOREVER_REL,
613 &core_transmit_notify, dn);
619 * Forward the given payload to the given target.
621 * @param target where to send the message
622 * @param uid unique ID for the message
623 * @param ultimate_target ultimate recipient for the message
624 * @param distance expected (remaining) distance to the target
625 * @param sender original sender of the message
626 * @param payload payload of the message
629 forward_payload (struct DirectNeighbor *target,
632 const struct GNUNET_PeerIdentity *sender,
633 const struct GNUNET_PeerIdentity *ultimate_target,
634 const struct GNUNET_MessageHeader *payload)
636 struct PendingMessage *pm;
637 struct RouteMessage *rm;
640 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
641 (0 != memcmp (sender,
643 sizeof (struct GNUNET_PeerIdentity))) )
645 GNUNET_break (0 == uid);
648 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
649 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
654 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
655 pm->ultimate_target = *ultimate_target;
657 pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
658 rm = (struct RouteMessage *) &pm[1];
659 rm->header.size = htons ((uint16_t) msize);
660 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
661 rm->distance = htonl (distance);
662 rm->target = target->peer;
663 rm->sender = *sender;
664 memcpy (&rm[1], payload, ntohs (payload->size));
665 GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
668 target->pm_queue_size++;
669 if (NULL == target->cth)
670 target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
671 GNUNET_YES /* cork */,
673 GNUNET_TIME_UNIT_FOREVER_REL,
676 &core_transmit_notify, target);
681 * Find a free slot for storing a 'route' in the 'consensi'
682 * set at the given distance.
684 * @param distance distance to use for the set slot
687 get_consensus_slot (uint32_t distance)
689 struct ConsensusSet *cs;
692 cs = &consensi[distance];
694 while ( (i < cs->array_length) &&
695 (NULL != cs->targets[i]) ) i++;
696 if (i == cs->array_length)
698 GNUNET_array_grow (cs->targets,
700 cs->array_length * 2 + 2);
707 * Allocate a slot in the consensus set for a route.
709 * @param route route to initialize
710 * @param distance which consensus set to use
713 allocate_route (struct Route *route,
718 i = get_consensus_slot (distance);
719 route->set_offset = i;
720 consensi[distance].targets[i] = route;
721 route->target.distance = htonl (distance);
726 * Release a slot in the consensus set for a route.
728 * @param route route to release the slot from
731 release_route (struct Route *route)
733 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
734 route->set_offset = UINT_MAX; /* indicate invalid slot */
739 * Move a route from one consensus set to another.
741 * @param route route to move
742 * @param new_distance new distance for the route (destination set)
745 move_route (struct Route *route,
746 uint32_t new_distance)
750 release_route (route);
751 i = get_consensus_slot (new_distance);
752 route->set_offset = i;
753 consensi[new_distance].targets[i] = route;
754 route->target.distance = htonl (new_distance);
759 * Initialize this neighbors 'my_set' and when done give
760 * it to the pending set operation for execution.
762 * Add a single element to the set per call:
764 * If we reached the last element of a consensus element: increase distance
767 * @param cls the neighbor for which we are building the set
770 build_set (void *cls)
772 struct DirectNeighbor *neighbor = cls;
773 struct GNUNET_SET_Element element;
774 struct Target *target;
777 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
778 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
780 /* If we reached the last element of a consensus array element: increase distance and start with next array */
781 neighbor->consensus_insertion_offset = 0;
782 neighbor->consensus_insertion_distance++;
784 /* skip over NULL entries */
785 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
786 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
787 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
788 neighbor->consensus_insertion_offset++;
790 if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
792 /* we have added all elements to the set, run the operation */
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 "Finished building my SET for peer `%s' with %u elements, committing\n",
795 GNUNET_i2s(&neighbor->peer),
796 neighbor->consensus_elements);
797 GNUNET_SET_commit (neighbor->set_op,
799 GNUNET_SET_destroy (neighbor->my_set);
800 neighbor->my_set = NULL;
804 target = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]->target;
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Adding peer `%s' with distance %u to SET\n",
807 GNUNET_i2s (&target->peer),
808 ntohl (target->distance));
809 element.size = sizeof (struct Target);
810 element.type = htons (0); /* do we need this? */
811 element.data = target;
812 neighbor->consensus_elements++;
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Adding element to SET\n");
815 GNUNET_SET_add_element (neighbor->my_set,
817 &build_set, neighbor);
819 /* Find next non-NULL entry */
820 neighbor->consensus_insertion_offset++;
821 /* skip over NULL entries */
822 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
823 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
824 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
825 neighbor->consensus_insertion_offset++;
830 * A peer is now connected to us at distance 1. Initiate DV exchange.
832 * @param neighbor entry for the neighbor at distance 1
835 handle_direct_connect (struct DirectNeighbor *neighbor)
838 struct GNUNET_HashCode h1;
839 struct GNUNET_HashCode h2;
840 struct GNUNET_HashCode session_id;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Direct connection to %s established, routing table exchange begins.\n",
844 GNUNET_i2s (&neighbor->peer));
845 GNUNET_STATISTICS_update (stats,
846 "# peers connected (1-hop)",
848 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
852 send_disconnect_to_plugin (&neighbor->peer);
853 release_route (route);
857 neighbor->direct_route = GNUNET_new (struct Route);
858 neighbor->direct_route->next_hop = neighbor;
859 neighbor->direct_route->target.peer= neighbor->peer;
860 neighbor->direct_route->target.distance = DIRECT_NEIGHBOR_COST;
861 allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Adding direct route to %s\n",
865 GNUNET_i2s (&neighbor->direct_route->target.peer));
868 /* construct session ID seed as XOR of both peer's identities */
869 GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &h1);
870 GNUNET_CRYPTO_hash (&neighbor->peer, sizeof (struct GNUNET_PeerIdentity), &h2);
871 GNUNET_CRYPTO_hash_xor (&h1,
874 /* make sure session ID is unique across applications by salting it with 'DV' */
875 GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
876 GCRY_MD_SHA512, GCRY_MD_SHA256,
878 &session_id, sizeof (session_id),
880 if (0 < memcmp (&neighbor->peer,
882 sizeof (struct GNUNET_PeerIdentity)))
884 neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Starting SET listen operation\n");
891 neighbor->listen_handle = GNUNET_SET_listen (cfg,
892 GNUNET_SET_OPERATION_UNION,
893 &neighbor->real_session_id,
901 * Method called whenever a peer connects.
904 * @param peer peer identity this notification is about
907 handle_core_connect (void *cls,
908 const struct GNUNET_PeerIdentity *peer)
910 struct DirectNeighbor *neighbor;
912 /* Check for connect to self message */
913 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
915 /* check if entry exists */
916 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
918 if (NULL != neighbor)
920 GNUNET_break (GNUNET_YES != neighbor->connected);
921 neighbor->connected = GNUNET_YES;
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Core connected to %s (distance %u)\n",
925 (unsigned int) neighbor->distance);
926 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
928 handle_direct_connect (neighbor);
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Core connected to %s (distance unknown)\n",
934 neighbor = GNUNET_new (struct DirectNeighbor);
935 neighbor->peer = *peer;
936 GNUNET_assert (GNUNET_YES ==
937 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
941 neighbor->connected = GNUNET_YES;
942 neighbor->distance = 0; /* unknown */
947 * Called for each 'target' in a neighbor table to free the associated memory.
950 * @param key key of the value
951 * @param value value to free
952 * @return GNUNET_OK to continue to iterate
955 free_targets (void *cls,
956 const struct GNUNET_PeerIdentity *key,
965 * Multipeerhmap iterator for checking if a given route is
966 * (now) useful to this peer.
968 * @param cls the direct neighbor for the given route
969 * @param key key value stored under
970 * @param value a 'struct Target' that may or may not be useful; not that
971 * the distance in 'target' does not include the first hop yet
972 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
975 check_possible_route (void *cls,
976 const struct GNUNET_PeerIdentity *key,
979 struct DirectNeighbor *neighbor = cls;
980 struct Target *target = value;
983 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
987 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
989 /* this 'target' is cheaper than the existing route; switch to alternative route! */
990 move_route (route, ntohl (target->distance) + 1);
991 route->next_hop = neighbor;
992 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
994 return GNUNET_YES; /* got a route to this target already */
996 route = GNUNET_new (struct Route);
997 route->next_hop = neighbor;
998 route->target.distance = htonl (ntohl (target->distance) + 1);
999 route->target.peer = target->peer;
1000 allocate_route (route, ntohl (route->target.distance));
1001 GNUNET_assert (GNUNET_YES ==
1002 GNUNET_CONTAINER_multipeermap_put (all_routes,
1003 &route->target.peer,
1005 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1006 send_connect_to_plugin (&route->target.peer, ntohl (target->distance));
1012 * Multipeermap iterator for finding routes that were previously
1013 * "hidden" due to a better route (called after a disconnect event).
1016 * @param key peer identity of the given direct neighbor
1017 * @param value a 'struct DirectNeighbor' to check for additional routes
1018 * @return GNUNET_YES to continue iteration
1021 refresh_routes (void *cls,
1022 const struct GNUNET_PeerIdentity *key,
1025 struct DirectNeighbor *neighbor = value;
1027 if ( (GNUNET_YES != neighbor->connected) ||
1028 (DIRECT_NEIGHBOR_COST != neighbor->distance) )
1030 if (NULL != neighbor->neighbor_table)
1031 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1032 &check_possible_route,
1039 * Get distance information from 'atsi'.
1041 * @param atsi performance data
1042 * @param atsi_count number of entries in atsi
1043 * @return connected transport distance
1046 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
1047 uint32_t atsi_count)
1051 for (i = 0; i < atsi_count; i++)
1052 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
1053 return (0 == ntohl (atsi->value)) ? DIRECT_NEIGHBOR_COST : ntohl (atsi->value); // FIXME: 0 check should not be required once ATS is fixed!
1054 /* If we do not have explicit distance data, assume direct neighbor. */
1055 return DIRECT_NEIGHBOR_COST;
1060 * Multipeermap iterator for freeing routes that go via a particular
1061 * neighbor that disconnected and is thus no longer available.
1063 * @param cls the direct neighbor that is now unavailable
1064 * @param key key value stored under
1065 * @param value a 'struct Route' that may or may not go via neighbor
1067 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1070 cull_routes (void *cls,
1071 const struct GNUNET_PeerIdentity *key,
1074 struct DirectNeighbor *neighbor = cls;
1075 struct Route *route = value;
1077 if (route->next_hop != neighbor)
1078 return GNUNET_YES; /* not affected */
1079 GNUNET_assert (GNUNET_YES ==
1080 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1081 release_route (route);
1082 send_disconnect_to_plugin (&route->target.peer);
1083 GNUNET_free (route);
1089 * Handle the case that a direct connection to a peer is
1090 * disrupted. Remove all routes via that peer and
1091 * stop the consensus with it.
1093 * @param neighbor peer that was disconnected (or at least is no
1094 * longer at distance 1)
1097 handle_direct_disconnect (struct DirectNeighbor *neighbor)
1099 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1102 if (NULL != neighbor->cth)
1104 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1105 neighbor->cth = NULL;
1108 if (NULL != neighbor->direct_route)
1110 release_route (neighbor->direct_route);
1111 GNUNET_free (neighbor->direct_route);
1112 neighbor->direct_route = NULL;
1115 if (NULL != neighbor->neighbor_table_consensus)
1117 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1120 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1121 neighbor->neighbor_table_consensus = NULL;
1123 if (NULL != neighbor->neighbor_table)
1125 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1128 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1129 neighbor->neighbor_table = NULL;
1131 if (NULL != neighbor->set_op)
1133 GNUNET_SET_operation_cancel (neighbor->set_op);
1134 neighbor->set_op = NULL;
1136 if (NULL != neighbor->my_set)
1138 GNUNET_SET_destroy (neighbor->my_set);
1139 neighbor->my_set = NULL;
1141 if (NULL != neighbor->listen_handle)
1143 GNUNET_SET_listen_cancel (neighbor->listen_handle);
1144 neighbor->listen_handle = NULL;
1146 if (GNUNET_SCHEDULER_NO_TASK != neighbor->initiate_task)
1148 GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1149 neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1155 * Function that is called with QoS information about an address; used
1156 * to update our current distance to another peer.
1158 * @param cls closure
1159 * @param address the address
1160 * @param active is this address in active use
1161 * @param bandwidth_out assigned outbound bandwidth for the connection
1162 * @param bandwidth_in assigned inbound bandwidth for the connection
1163 * @param ats performance data for the address (as far as known)
1164 * @param ats_count number of performance records in 'ats'
1167 handle_ats_update (void *cls,
1168 const struct GNUNET_HELLO_Address *address,
1170 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1171 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1172 const struct GNUNET_ATS_Information *ats,
1175 struct DirectNeighbor *neighbor;
1178 if (GNUNET_NO == active)
1180 distance = get_atsi_distance (ats, ats_count);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "ATS says distance to %s is %u\n",
1184 GNUNET_i2s (&address->peer),
1185 (unsigned int) distance);*/
1186 /* check if entry exists */
1187 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1189 if (NULL != neighbor)
1191 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1192 (DIRECT_NEIGHBOR_COST == distance) )
1193 return; /* no change */
1194 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1196 neighbor->distance = distance;
1197 GNUNET_STATISTICS_update (stats,
1198 "# peers connected (1-hop)",
1200 handle_direct_disconnect (neighbor);
1201 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1206 neighbor->distance = distance;
1207 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1209 if (GNUNET_YES != neighbor->connected)
1211 handle_direct_connect (neighbor);
1214 neighbor = GNUNET_new (struct DirectNeighbor);
1215 neighbor->peer = address->peer;
1216 GNUNET_assert (GNUNET_YES ==
1217 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1221 neighbor->connected = GNUNET_NO; /* not yet */
1222 neighbor->distance = distance;
1227 * Check if a target was removed from the set of the other peer; if so,
1228 * if we also used it for our route, we need to remove it from our
1229 * 'all_routes' set (and later check if an alternative path now exists).
1231 * @param cls the 'struct DirectNeighbor'
1232 * @param key peer identity for the target
1233 * @param value a 'struct Target' previously reachable via the given neighbor
1236 check_target_removed (void *cls,
1237 const struct GNUNET_PeerIdentity *key,
1240 struct DirectNeighbor *neighbor = cls;
1241 struct Target *new_target;
1242 struct Route *current_route;
1244 new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
1246 if (NULL == new_target)
1248 /* target was revoked, check if it was used */
1249 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1251 if ( (NULL == current_route) ||
1252 (current_route->next_hop != neighbor) )
1254 /* didn't matter, wasn't used */
1257 /* remove existing route */
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Lost route to %s\n",
1260 GNUNET_i2s (¤t_route->target.peer));
1261 GNUNET_assert (GNUNET_YES ==
1262 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
1263 send_disconnect_to_plugin (¤t_route->target.peer);
1264 GNUNET_free (current_route);
1265 neighbor->target_removed = GNUNET_YES;
1273 * Check if a target was added to the set of the other peer; if it
1274 * was added or impoves the existing route, do the needed updates.
1276 * @param cls the 'struct DirectNeighbor'
1277 * @param key peer identity for the target
1278 * @param value a 'struct Target' now reachable via the given neighbor
1281 check_target_added (void *cls,
1282 const struct GNUNET_PeerIdentity *key,
1285 struct DirectNeighbor *neighbor = cls;
1286 struct Target *target = value;
1287 struct Route *current_route;
1289 /* target was revoked, check if it was used */
1290 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1292 if (NULL != current_route)
1295 if (current_route->next_hop == neighbor)
1297 /* we had the same route before, no change */
1298 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1300 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1301 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1305 if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
1307 /* alternative, shorter route exists, ignore */
1310 /* new route is better than the existing one, take over! */
1311 /* NOTE: minor security issue: malicious peers may advertise
1312 very short routes to take over longer paths; as we don't
1313 check that the shorter routes actually work, a malicious
1314 direct neighbor can use this to DoS our long routes */
1315 current_route->next_hop = neighbor;
1316 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1317 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Discovered new route to %s using %u hops\n",
1323 GNUNET_i2s (&target->peer),
1324 (unsigned int) (ntohl (target->distance) + 1));
1325 current_route = GNUNET_new (struct Route);
1326 current_route->next_hop = neighbor;
1327 current_route->target.peer = target->peer;
1328 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1329 GNUNET_assert (GNUNET_YES ==
1330 GNUNET_CONTAINER_multipeermap_put (all_routes,
1331 ¤t_route->target.peer,
1333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1334 send_connect_to_plugin (¤t_route->target.peer,
1335 ntohl (current_route->target.distance));
1341 * Callback for set operation results. Called for each element
1342 * in the result set.
1343 * We have learned a new route from the other peer. Add it to the
1344 * route set we're building.
1346 * @param cls the 'struct DirectNeighbor' we're building the consensus with
1347 * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
1348 * @param status see enum GNUNET_SET_Status
1351 handle_set_union_result (void *cls,
1352 const struct GNUNET_SET_Element *element,
1353 enum GNUNET_SET_Status status)
1355 struct DirectNeighbor *neighbor = cls;
1356 struct Target *target;
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 "Got SET union result: %d\n",
1363 case GNUNET_SET_STATUS_OK:
1364 if (sizeof (struct Target) != element->size)
1366 GNUNET_break_op (0);
1369 target = GNUNET_new (struct Target);
1370 memcpy (target, element->data, sizeof (struct Target));
1372 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
1375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1377 GNUNET_break_op (0);
1378 GNUNET_free (target);
1381 case GNUNET_SET_STATUS_TIMEOUT:
1382 case GNUNET_SET_STATUS_FAILURE:
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Failed to establish DV union, will try again later\n");
1385 neighbor->set_op = NULL;
1386 if (NULL != neighbor->neighbor_table_consensus)
1388 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1391 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1392 neighbor->neighbor_table_consensus = NULL;
1394 if (0 < memcmp (&neighbor->peer,
1396 sizeof (struct GNUNET_PeerIdentity)))
1397 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1398 &initiate_set_union,
1401 case GNUNET_SET_STATUS_HALF_DONE:
1402 /* we got all of our updates; integrate routing table! */
1403 neighbor->target_removed = GNUNET_NO;
1404 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1405 &check_target_removed,
1407 if (GNUNET_YES == neighbor->target_removed)
1409 /* check if we got an alternative for the removed routes */
1410 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1414 /* add targets that appeared (and check for improved routes) */
1415 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1416 &check_target_added,
1418 if (NULL != neighbor->neighbor_table)
1420 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1423 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1424 neighbor->neighbor_table = NULL;
1426 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1427 neighbor->neighbor_table_consensus = NULL;
1429 case GNUNET_SET_STATUS_DONE:
1430 /* operation done, schedule next run! */
1431 neighbor->set_op = NULL;
1432 if (0 < memcmp (&neighbor->peer,
1434 sizeof (struct GNUNET_PeerIdentity)))
1435 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1436 &initiate_set_union,
1447 * Start creating a new DV set union construction, our neighbour has
1448 * asked for it (callback for listening peer).
1450 * @param cls the 'struct DirectNeighbor' of the peer we're building
1451 * a routing consensus with
1452 * @param other_peer the other peer
1453 * @param context_msg message with application specific information from
1455 * @param request request from the other peer, use GNUNET_SET_accept
1456 * to accept it, otherwise the request will be refused
1457 * Note that we don't use a return value here, as it is also
1458 * necessary to specify the set we want to do the operation with,
1459 * whith sometimes can be derived from the context message.
1460 * Also necessary to specify the timeout.
1463 listen_set_union (void *cls,
1464 const struct GNUNET_PeerIdentity *other_peer,
1465 const struct GNUNET_MessageHeader *context_msg,
1466 struct GNUNET_SET_Request *request)
1468 struct DirectNeighbor *neighbor = cls;
1470 if (NULL == request)
1471 return; /* why??? */
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "Starting to create consensus with %s!\n",
1474 GNUNET_i2s (&neighbor->peer));
1475 if (NULL != neighbor->set_op)
1477 GNUNET_SET_operation_cancel (neighbor->set_op);
1478 neighbor->set_op = NULL;
1480 if (NULL != neighbor->my_set)
1482 GNUNET_SET_destroy (neighbor->my_set);
1483 neighbor->my_set = NULL;
1485 neighbor->my_set = GNUNET_SET_create (cfg,
1486 GNUNET_SET_OPERATION_UNION);
1487 neighbor->set_op = GNUNET_SET_accept (request,
1488 GNUNET_SET_RESULT_ADDED,
1489 &handle_set_union_result,
1491 neighbor->consensus_insertion_offset = 0;
1492 neighbor->consensus_insertion_distance = 0;
1493 neighbor->consensus_elements = 0;
1494 build_set (neighbor);
1499 * Start creating a new DV set union by initiating the connection.
1501 * @param cls the 'struct DirectNeighbor' of the peer we're building
1502 * a routing consensus with
1503 * @param tc scheduler context
1506 initiate_set_union (void *cls,
1507 const struct GNUNET_SCHEDULER_TaskContext *tc)
1509 struct DirectNeighbor *neighbor = cls;
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Initiating SET union\n");
1513 neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1514 neighbor->my_set = GNUNET_SET_create (cfg,
1515 GNUNET_SET_OPERATION_UNION);
1516 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1517 &neighbor->real_session_id,
1519 0 /* FIXME: salt */,
1520 GNUNET_SET_RESULT_ADDED,
1521 &handle_set_union_result,
1523 neighbor->consensus_insertion_offset = 0;
1524 neighbor->consensus_insertion_distance = 0;
1525 neighbor->consensus_elements = 0;
1526 build_set (neighbor);
1531 * Core handler for DV data messages. Whatever this message
1532 * contains all we really have to do is rip it out of its
1533 * DV layering and give it to our pal the DV plugin to report
1536 * @param cls closure
1537 * @param peer peer which sent the message (immediate sender)
1538 * @param message the message
1539 * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
1542 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1543 const struct GNUNET_MessageHeader *message)
1545 const struct RouteMessage *rm;
1546 const struct GNUNET_MessageHeader *payload;
1547 struct Route *route;
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "Handling DV message\n");
1551 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1553 GNUNET_break_op (0);
1554 return GNUNET_SYSERR;
1556 rm = (const struct RouteMessage *) message;
1557 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1558 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1560 GNUNET_break_op (0);
1561 return GNUNET_SYSERR;
1563 if (0 == memcmp (&rm->target,
1565 sizeof (struct GNUNET_PeerIdentity)))
1567 /* message is for me, check reverse route! */
1568 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1572 /* don't have reverse route, drop */
1573 GNUNET_STATISTICS_update (stats,
1574 "# message discarded (no reverse route)",
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1579 "Delivering %u bytes to myself!\n",
1580 ntohs (payload->size));
1581 send_data_to_plugin (payload,
1583 ntohl (route->target.distance));
1586 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1590 GNUNET_STATISTICS_update (stats,
1591 "# messages discarded (no route)",
1595 if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
1597 GNUNET_STATISTICS_update (stats,
1598 "# messages discarded (target too far)",
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Forwarding message to %s\n",
1604 GNUNET_i2s (&rm->target));
1605 forward_payload (route->next_hop,
1606 ntohl (route->target.distance),
1616 * Service server's handler for message send requests (which come
1617 * bubbling up to us through the DV plugin).
1619 * @param cls closure
1620 * @param client identification of the client
1621 * @param message the actual message
1624 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1625 const struct GNUNET_MessageHeader *message)
1627 struct Route *route;
1628 const struct GNUNET_DV_SendMessage *msg;
1629 const struct GNUNET_MessageHeader *payload;
1631 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1637 msg = (const struct GNUNET_DV_SendMessage *) message;
1638 GNUNET_break (0 != ntohl (msg->uid));
1639 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1640 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1646 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1650 /* got disconnected */
1651 GNUNET_STATISTICS_update (stats,
1652 "# local messages discarded (no route)",
1654 send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1655 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "Forwarding %u bytes to %s\n",
1660 ntohs (payload->size),
1661 GNUNET_i2s (&msg->target));
1663 forward_payload (route->next_hop,
1664 ntohl (route->target.distance),
1669 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1674 * Cleanup all of the data structures associated with a given neighbor.
1676 * @param neighbor neighbor to clean up
1679 cleanup_neighbor (struct DirectNeighbor *neighbor)
1681 struct PendingMessage *pending;
1683 while (NULL != (pending = neighbor->pm_head))
1685 neighbor->pm_queue_size--;
1686 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1689 GNUNET_free (pending);
1691 handle_direct_disconnect (neighbor);
1692 GNUNET_assert (GNUNET_YES ==
1693 GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1696 GNUNET_free (neighbor);
1701 * Method called whenever a given peer disconnects.
1703 * @param cls closure
1704 * @param peer peer identity this notification is about
1707 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1709 struct DirectNeighbor *neighbor;
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Received core peer disconnect message for peer `%s'!\n",
1714 /* Check for disconnect from self message */
1715 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1718 GNUNET_CONTAINER_multipeermap_get (direct_neighbors, peer);
1719 if (NULL == neighbor)
1724 GNUNET_break (GNUNET_YES == neighbor->connected);
1725 neighbor->connected = GNUNET_NO;
1726 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1729 GNUNET_STATISTICS_update (stats,
1730 "# peers connected (1-hop)",
1733 cleanup_neighbor (neighbor);
1734 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1741 * Multipeermap iterator for freeing routes. Should never be called.
1744 * @param key key value stored under
1745 * @param value the route to be freed
1747 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1750 free_route (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1752 struct Route *route = value;
1755 GNUNET_assert (GNUNET_YES ==
1756 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1757 release_route (route);
1758 send_disconnect_to_plugin (&route->target.peer);
1759 GNUNET_free (route);
1765 * Multipeermap iterator for freeing direct neighbors. Should never be called.
1768 * @param key key value stored under
1769 * @param value the direct neighbor to be freed
1771 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1774 free_direct_neighbors (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1776 struct DirectNeighbor *neighbor = value;
1779 cleanup_neighbor (neighbor);
1785 * Task run during shutdown.
1791 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1795 GNUNET_CORE_disconnect (core_api);
1797 GNUNET_ATS_performance_done (ats);
1799 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1800 &free_direct_neighbors, NULL);
1801 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1803 GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
1804 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1805 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1807 GNUNET_SERVER_notification_context_destroy (nc);
1809 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1810 GNUNET_array_grow (consensi[i].targets,
1811 consensi[i].array_length,
1817 * Notify newly connected client about an existing route.
1819 * @param cls the 'struct GNUNET_SERVER_Client'
1820 * @param key peer identity
1821 * @param value the XXX.
1822 * @return GNUNET_OK (continue to iterate)
1825 add_route (void *cls,
1826 const struct GNUNET_PeerIdentity *key,
1829 struct GNUNET_SERVER_Client *client = cls;
1830 struct Route *route = value;
1831 struct GNUNET_DV_ConnectMessage cm;
1833 cm.header.size = htons (sizeof (cm));
1834 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1835 cm.distance = htonl (route->target.distance);
1836 cm.peer = route->target.peer;
1838 GNUNET_SERVER_notification_context_unicast (nc,
1847 * Handle START-message. This is the first message sent to us
1848 * by the client (can only be one!).
1850 * @param cls closure (always NULL)
1851 * @param client identification of the client
1852 * @param message the actual message
1855 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1856 const struct GNUNET_MessageHeader *message)
1858 GNUNET_SERVER_notification_context_add (nc, client);
1859 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1860 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1867 * Called on core init.
1870 * @param identity this peer's identity
1873 core_init (void *cls,
1874 const struct GNUNET_PeerIdentity *identity)
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 GNUNET_i2s (identity));
1879 my_identity = *identity;
1884 * Process dv requests.
1886 * @param cls closure
1887 * @param server the initialized server
1888 * @param c configuration to use
1891 run (void *cls, struct GNUNET_SERVER_Handle *server,
1892 const struct GNUNET_CONFIGURATION_Handle *c)
1894 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1895 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
1898 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1899 {&handle_start, NULL,
1900 GNUNET_MESSAGE_TYPE_DV_START,
1901 sizeof (struct GNUNET_MessageHeader) },
1902 { &handle_dv_send_message, NULL,
1903 GNUNET_MESSAGE_TYPE_DV_SEND,
1909 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1910 all_routes = GNUNET_CONTAINER_multipeermap_create (65536, GNUNET_NO);
1911 core_api = GNUNET_CORE_connect (cfg, NULL,
1913 &handle_core_connect,
1914 &handle_core_disconnect,
1919 if (NULL == core_api)
1921 ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
1924 GNUNET_CORE_disconnect (core_api);
1927 nc = GNUNET_SERVER_notification_context_create (server,
1928 MAX_QUEUE_SIZE_PLUGIN);
1929 stats = GNUNET_STATISTICS_create ("dv", cfg);
1930 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1931 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1932 &shutdown_task, NULL);
1937 * The main function for the dv service.
1939 * @param argc number of arguments from the command line
1940 * @param argv command line arguments
1941 * @return 0 ok, 1 on error
1944 main (int argc, char *const *argv)
1946 return (GNUNET_OK ==
1947 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
1948 &run, NULL)) ? 0 : 1;
1951 /* end of gnunet-service-dv.c */