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_MultiHashMap *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_MultiHashMap *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 * Flag set within 'check_target_removed' to trigger full global route refresh.
258 * Our distance to this peer, 0 for unknown.
263 * Is this neighbor connected at the core level?
271 * A route includes information about the next hop,
272 * the target, and the ultimate distance to the
279 * Which peer do we need to forward the message to?
281 struct DirectNeighbor *next_hop;
284 * What would be the target, and how far is it away?
286 struct Target target;
289 * Offset of this target in the respective consensus set.
291 unsigned int set_offset;
297 * Set of targets we bring to a consensus; all targets in a set have a
298 * distance equal to the sets distance (which is implied by the array
305 * Array of targets in the set, may include NULL entries if a
306 * neighbor has disconnected; the targets are allocated with the
307 * respective container (all_routes), not here.
309 struct Route **targets;
312 * Size of the 'targets' array.
314 unsigned int array_length;
320 * Hashmap of all of our neighbors; processing these usually requires
321 * first checking to see if the peer is core-connected and if the
322 * distance is 1, in which case they are direct neighbors.
324 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
327 * Hashmap with all routes that we currently support; contains
328 * routing information for all peers from distance 2
329 * up to distance DEFAULT_FISHEYE_DEPTH.
331 static struct GNUNET_CONTAINER_MultiHashMap *all_routes;
334 * Array of consensus sets we expose to the outside world. Sets
335 * are structured by the distance to the target.
337 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
340 * Handle to the core service api.
342 static struct GNUNET_CORE_Handle *core_api;
345 * The identity of our peer.
347 static struct GNUNET_PeerIdentity my_identity;
350 * The configuration for this service.
352 static const struct GNUNET_CONFIGURATION_Handle *cfg;
355 * The client, the DV plugin connected to us (or an event monitor).
356 * Hopefully this client will never change, although if the plugin
357 * dies and returns for some reason it may happen.
359 static struct GNUNET_SERVER_NotificationContext *nc;
362 * Handle for the statistics service.
364 static struct GNUNET_STATISTICS_Handle *stats;
367 * Handle to ATS service.
369 static struct GNUNET_ATS_PerformanceHandle *ats;
373 * Start creating a new DV set union by initiating the connection.
375 * @param cls the 'struct DirectNeighbor' of the peer we're building
376 * a routing consensus with
377 * @param tc scheduler context
380 initiate_set_union (void *cls,
381 const struct GNUNET_SCHEDULER_TaskContext *tc);
385 * Start creating a new DV set union construction, our neighbour has
386 * asked for it (callback for listening peer).
388 * @param cls the 'struct DirectNeighbor' of the peer we're building
389 * a routing consensus with
390 * @param other_peer the other peer
391 * @param context_msg message with application specific information from
393 * @param request request from the other peer, use GNUNET_SET_accept
394 * to accept it, otherwise the request will be refused
395 * Note that we don't use a return value here, as it is also
396 * necessary to specify the set we want to do the operation with,
397 * whith sometimes can be derived from the context message.
398 * Also necessary to specify the timeout.
401 listen_set_union (void *cls,
402 const struct GNUNET_PeerIdentity *other_peer,
403 const struct GNUNET_MessageHeader *context_msg,
404 struct GNUNET_SET_Request *request);
408 * Forward a message from another peer to the plugin.
410 * @param message the message to send to the plugin
411 * @param origin the original sender of the message
412 * @param distance distance to the original sender of the message
415 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
416 const struct GNUNET_PeerIdentity *origin,
419 struct GNUNET_DV_ReceivedMessage *received_msg;
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Delivering message from peer `%s'\n",
424 GNUNET_i2s (origin));
425 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
426 ntohs (message->size);
427 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
429 GNUNET_break (0); /* too big */
432 received_msg = GNUNET_malloc (size);
433 received_msg->header.size = htons (size);
434 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
435 received_msg->distance = htonl (distance);
436 received_msg->sender = *origin;
437 memcpy (&received_msg[1], message, ntohs (message->size));
438 GNUNET_SERVER_notification_context_broadcast (nc,
439 &received_msg->header,
441 GNUNET_free (received_msg);
446 * Forward a control message to the plugin.
448 * @param message the message to send to the plugin
451 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
453 GNUNET_SERVER_notification_context_broadcast (nc,
460 * Give an (N)ACK message to the plugin, we transmitted a message for it.
462 * @param target peer that received the message
463 * @param uid plugin-chosen UID for the message
464 * @param nack GNUNET_NO to send ACK, GNUNET_YES to send NACK
467 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
471 struct GNUNET_DV_AckMessage ack_msg;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Delivering ACK for message to peer `%s'\n",
475 GNUNET_i2s (target));
476 ack_msg.header.size = htons (sizeof (ack_msg));
477 ack_msg.header.type = htons ((GNUNET_YES == nack)
478 ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
479 : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
480 ack_msg.uid = htonl (uid);
481 ack_msg.target = *target;
482 send_control_to_plugin (&ack_msg.header);
487 * Send a DISTANCE_CHANGED message to the plugin.
489 * @param peer peer with a changed distance
490 * @param distance new distance to the peer
493 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
496 struct GNUNET_DV_DistanceUpdateMessage du_msg;
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
501 du_msg.header.size = htons (sizeof (du_msg));
502 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
503 du_msg.distance = htonl (distance);
505 send_control_to_plugin (&du_msg.header);
510 * Give a CONNECT message to the plugin.
512 * @param target peer that connected
513 * @param distance distance to the target
516 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
519 struct GNUNET_DV_ConnectMessage cm;
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Delivering CONNECT about peer `%s'\n",
523 GNUNET_i2s (target));
524 cm.header.size = htons (sizeof (cm));
525 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
526 cm.distance = htonl (distance);
528 send_control_to_plugin (&cm.header);
533 * Give a DISCONNECT message to the plugin.
535 * @param target peer that disconnected
538 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
540 struct GNUNET_DV_DisconnectMessage dm;
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Delivering DISCONNECT about peer `%s'\n",
544 GNUNET_i2s (target));
545 dm.header.size = htons (sizeof (dm));
546 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
547 dm.reserved = htonl (0);
549 send_control_to_plugin (&dm.header);
554 * Function called to transfer a message to another peer
557 * @param cls closure with the direct neighbor
558 * @param size number of bytes available in buf
559 * @param buf where the callee should write the message
560 * @return number of bytes written to buf
563 core_transmit_notify (void *cls, size_t size, void *buf)
565 struct DirectNeighbor *dn = cls;
567 struct PendingMessage *pending;
574 /* peer disconnected */
578 pending = dn->pm_head;
580 while ( (NULL != (pending = dn->pm_head)) &&
581 (size >= off + (msize = ntohs (pending->msg->size))))
584 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
587 memcpy (&cbuf[off], pending->msg, msize);
588 if (0 != pending->uid)
589 send_ack_to_plugin (&pending->ultimate_target,
592 GNUNET_free (pending);
595 if (NULL != dn->pm_head)
597 GNUNET_CORE_notify_transmit_ready (core_api,
598 GNUNET_YES /* cork */,
600 GNUNET_TIME_UNIT_FOREVER_REL,
603 &core_transmit_notify, dn);
609 * Forward the given payload to the given target.
611 * @param target where to send the message
612 * @param uid unique ID for the message
613 * @param ultimate_target ultimate recipient for the message
614 * @param distance expected (remaining) distance to the target
615 * @param sender original sender of the message
616 * @param payload payload of the message
619 forward_payload (struct DirectNeighbor *target,
622 const struct GNUNET_PeerIdentity *sender,
623 const struct GNUNET_PeerIdentity *ultimate_target,
624 const struct GNUNET_MessageHeader *payload)
626 struct PendingMessage *pm;
627 struct RouteMessage *rm;
630 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
631 (0 != memcmp (sender,
633 sizeof (struct GNUNET_PeerIdentity))) )
635 GNUNET_break (0 == uid);
638 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
639 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
644 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
645 pm->ultimate_target = *ultimate_target;
647 pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
648 rm = (struct RouteMessage *) &pm[1];
649 rm->header.size = htons ((uint16_t) msize);
650 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
651 rm->distance = htonl (distance);
652 rm->target = target->peer;
653 rm->sender = *sender;
654 memcpy (&rm[1], payload, ntohs (payload->size));
655 GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
658 target->pm_queue_size++;
659 if (NULL == target->cth)
660 target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
661 GNUNET_YES /* cork */,
663 GNUNET_TIME_UNIT_FOREVER_REL,
666 &core_transmit_notify, target);
671 * Find a free slot for storing a 'route' in the 'consensi'
672 * set at the given distance.
674 * @param distance distance to use for the set slot
677 get_consensus_slot (uint32_t distance)
679 struct ConsensusSet *cs;
682 cs = &consensi[distance];
684 while ( (i < cs->array_length) &&
685 (NULL != cs->targets[i]) ) i++;
686 if (i == cs->array_length)
687 GNUNET_array_grow (cs->targets,
689 cs->array_length * 2 + 2);
695 * Allocate a slot in the consensus set for a route.
697 * @param route route to initialize
698 * @param distance which consensus set to use
701 allocate_route (struct Route *route,
706 i = get_consensus_slot (distance);
707 route->set_offset = i;
708 consensi[distance].targets[i] = route;
709 route->target.distance = htonl (distance);
714 * Release a slot in the consensus set for a route.
716 * @param route route to release the slot from
719 release_route (struct Route *route)
721 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
722 route->set_offset = UINT_MAX; /* indicate invalid slot */
727 * Move a route from one consensus set to another.
729 * @param route route to move
730 * @param new_distance new distance for the route (destination set)
733 move_route (struct Route *route,
734 uint32_t new_distance)
738 release_route (route);
739 i = get_consensus_slot (new_distance);
740 route->set_offset = i;
741 consensi[new_distance].targets[i] = route;
742 route->target.distance = htonl (new_distance);
747 * Initialize this neighbors 'my_set' and when done give
748 * it to the pending set operation for execution.
750 * @param cls the neighbor for which we are building the set
753 build_set (void *cls)
755 struct DirectNeighbor *neighbor = cls;
756 struct GNUNET_SET_Element element;
758 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
759 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
761 neighbor->consensus_insertion_offset = 0;
762 neighbor->consensus_insertion_distance++;
763 /* skip over NULL entries */
764 while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
765 (consensi[neighbor->consensus_insertion_distance].array_length < neighbor->consensus_insertion_offset) &&
766 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
767 neighbor->consensus_insertion_offset++;
769 if (DEFAULT_FISHEYE_DEPTH - 1 == neighbor->consensus_insertion_distance)
771 /* we have added all elements to the set, run the operation */
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "Finished building my SET, committing\n");
774 GNUNET_SET_commit (neighbor->set_op,
776 GNUNET_SET_destroy (neighbor->my_set);
777 neighbor->my_set = NULL;
780 element.size = sizeof (struct Target);
781 element.type = htons (0); /* do we need this? */
782 element.data = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset++]->target;
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++;
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Adding element to SET\n");
791 GNUNET_SET_add_element (neighbor->my_set,
793 &build_set, neighbor);
799 * A peer is now connected to us at distance 1. Initiate DV exchange.
801 * @param neighbor entry for the neighbor at distance 1
804 handle_direct_connect (struct DirectNeighbor *neighbor)
807 struct GNUNET_HashCode session_id;
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Direct connection to %s established, routing table exchange begins.\n",
811 GNUNET_i2s (&neighbor->peer));
812 GNUNET_STATISTICS_update (stats,
813 "# peers connected (1-hop)",
815 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
816 &neighbor->peer.hashPubKey);
819 send_disconnect_to_plugin (&neighbor->peer);
820 release_route (route);
823 /* construct session ID seed as XOR of both peer's identities */
824 GNUNET_CRYPTO_hash_xor (&my_identity.hashPubKey,
825 &neighbor->peer.hashPubKey,
827 /* make sure session ID is unique across applications by salting it with 'DV' */
828 GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
829 GCRY_MD_SHA512, GCRY_MD_SHA256,
831 &session_id, sizeof (session_id),
833 if (1 == GNUNET_CRYPTO_hash_cmp (&neighbor->peer.hashPubKey,
834 &my_identity.hashPubKey))
836 neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Starting SET listen operation\n");
843 neighbor->listen_handle = GNUNET_SET_listen (cfg,
844 GNUNET_SET_OPERATION_UNION,
845 &neighbor->real_session_id,
853 * Method called whenever a peer connects.
856 * @param peer peer identity this notification is about
859 handle_core_connect (void *cls,
860 const struct GNUNET_PeerIdentity *peer)
862 struct DirectNeighbor *neighbor;
864 /* Check for connect to self message */
865 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
867 /* check if entry exists */
868 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
870 if (NULL != neighbor)
872 GNUNET_break (GNUNET_YES != neighbor->connected);
873 neighbor->connected = GNUNET_YES;
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 "Core connected to %s (distance %u)\n",
877 (unsigned int) neighbor->distance);
878 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
880 handle_direct_connect (neighbor);
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Core connected to %s (distance unknown)\n",
886 neighbor = GNUNET_new (struct DirectNeighbor);
887 neighbor->peer = *peer;
888 GNUNET_assert (GNUNET_YES ==
889 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
893 neighbor->connected = GNUNET_YES;
894 neighbor->distance = 0; /* unknown */
899 * Called for each 'target' in a neighbor table to free the associated memory.
902 * @param key key of the value
903 * @param value value to free
904 * @return GNUNET_OK to continue to iterate
907 free_targets (void *cls,
908 const struct GNUNET_HashCode *key,
917 * Multihashmap iterator for checking if a given route is
918 * (now) useful to this peer.
920 * @param cls the direct neighbor for the given route
921 * @param key key value stored under
922 * @param value a 'struct Target' that may or may not be useful; not that
923 * the distance in 'target' does not include the first hop yet
924 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
927 check_possible_route (void *cls,
928 const struct GNUNET_HashCode *key,
931 struct DirectNeighbor *neighbor = cls;
932 struct Target *target = value;
935 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
939 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
941 /* this 'target' is cheaper than the existing route; switch to alternative route! */
942 move_route (route, ntohl (target->distance) + 1);
943 route->next_hop = neighbor;
944 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
946 return GNUNET_YES; /* got a route to this target already */
948 route = GNUNET_new (struct Route);
949 route->next_hop = neighbor;
950 route->target.distance = htonl (ntohl (target->distance) + 1);
951 route->target.peer = target->peer;
952 allocate_route (route, ntohl (route->target.distance));
953 GNUNET_assert (GNUNET_YES ==
954 GNUNET_CONTAINER_multihashmap_put (all_routes,
955 &route->target.peer.hashPubKey,
957 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
958 send_connect_to_plugin (&route->target.peer, ntohl (target->distance));
964 * Multihashmap iterator for finding routes that were previously
965 * "hidden" due to a better route (called after a disconnect event).
968 * @param key peer identity of the given direct neighbor
969 * @param value a 'struct DirectNeighbor' to check for additional routes
970 * @return GNUNET_YES to continue iteration
973 refresh_routes (void *cls,
974 const struct GNUNET_HashCode *key,
977 struct DirectNeighbor *neighbor = value;
979 if ( (GNUNET_YES != neighbor->connected) ||
980 (DIRECT_NEIGHBOR_COST != neighbor->distance) )
982 if (NULL != neighbor->neighbor_table)
983 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
984 &check_possible_route,
991 * Get distance information from 'atsi'.
993 * @param atsi performance data
994 * @param atsi_count number of entries in atsi
995 * @return connected transport distance
998 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
1003 for (i = 0; i < atsi_count; i++)
1004 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
1005 return (0 == ntohl (atsi->value)) ? DIRECT_NEIGHBOR_COST : ntohl (atsi->value); // FIXME: 0 check should not be required once ATS is fixed!
1006 /* If we do not have explicit distance data, assume direct neighbor. */
1007 return DIRECT_NEIGHBOR_COST;
1012 * Multihashmap iterator for freeing routes that go via a particular
1013 * neighbor that disconnected and is thus no longer available.
1015 * @param cls the direct neighbor that is now unavailable
1016 * @param key key value stored under
1017 * @param value a 'struct Route' that may or may not go via neighbor
1019 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1022 cull_routes (void *cls,
1023 const struct GNUNET_HashCode *key,
1026 struct DirectNeighbor *neighbor = cls;
1027 struct Route *route = value;
1029 if (route->next_hop != neighbor)
1030 return GNUNET_YES; /* not affected */
1031 GNUNET_assert (GNUNET_YES ==
1032 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
1033 release_route (route);
1034 send_disconnect_to_plugin (&route->target.peer);
1035 GNUNET_free (route);
1041 * Handle the case that a direct connection to a peer is
1042 * disrupted. Remove all routes via that peer and
1043 * stop the consensus with it.
1045 * @param neighbor peer that was disconnected (or at least is no
1046 * longer at distance 1)
1049 handle_direct_disconnect (struct DirectNeighbor *neighbor)
1051 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1054 if (NULL != neighbor->cth)
1056 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1057 neighbor->cth = NULL;
1059 if (NULL != neighbor->neighbor_table_consensus)
1061 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1064 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table_consensus);
1065 neighbor->neighbor_table_consensus = NULL;
1067 if (NULL != neighbor->neighbor_table)
1069 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1072 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table);
1073 neighbor->neighbor_table = NULL;
1075 if (NULL != neighbor->set_op)
1077 GNUNET_SET_operation_cancel (neighbor->set_op);
1078 neighbor->set_op = NULL;
1080 if (NULL != neighbor->my_set)
1082 GNUNET_SET_destroy (neighbor->my_set);
1083 neighbor->my_set = NULL;
1085 if (NULL != neighbor->listen_handle)
1087 GNUNET_SET_listen_cancel (neighbor->listen_handle);
1088 neighbor->listen_handle = NULL;
1090 if (GNUNET_SCHEDULER_NO_TASK != neighbor->initiate_task)
1092 GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1093 neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1099 * Function that is called with QoS information about an address; used
1100 * to update our current distance to another peer.
1102 * @param cls closure
1103 * @param address the address
1104 * @param active is this address in active use
1105 * @param bandwidth_out assigned outbound bandwidth for the connection
1106 * @param bandwidth_in assigned inbound bandwidth for the connection
1107 * @param ats performance data for the address (as far as known)
1108 * @param ats_count number of performance records in 'ats'
1111 handle_ats_update (void *cls,
1112 const struct GNUNET_HELLO_Address *address,
1114 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1115 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1116 const struct GNUNET_ATS_Information *ats,
1119 struct DirectNeighbor *neighbor;
1122 if (GNUNET_NO == active)
1124 distance = get_atsi_distance (ats, ats_count);
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "ATS says distance to %s is %u\n",
1127 GNUNET_i2s (&address->peer),
1128 (unsigned int) distance);
1129 /* check if entry exists */
1130 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1131 &address->peer.hashPubKey);
1132 if (NULL != neighbor)
1134 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1135 (DIRECT_NEIGHBOR_COST == distance) )
1136 return; /* no change */
1137 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1139 neighbor->distance = distance;
1140 GNUNET_STATISTICS_update (stats,
1141 "# peers connected (1-hop)",
1143 handle_direct_disconnect (neighbor);
1144 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1149 neighbor->distance = distance;
1150 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1152 if (GNUNET_YES != neighbor->connected)
1154 handle_direct_connect (neighbor);
1157 neighbor = GNUNET_new (struct DirectNeighbor);
1158 neighbor->peer = address->peer;
1159 GNUNET_assert (GNUNET_YES ==
1160 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
1161 &address->peer.hashPubKey,
1163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1164 neighbor->connected = GNUNET_NO; /* not yet */
1165 neighbor->distance = distance;
1170 * Check if a target was removed from the set of the other peer; if so,
1171 * if we also used it for our route, we need to remove it from our
1172 * 'all_routes' set (and later check if an alternative path now exists).
1174 * @param cls the 'struct DirectNeighbor'
1175 * @param key peer identity for the target
1176 * @param value a 'struct Target' previously reachable via the given neighbor
1179 check_target_removed (void *cls,
1180 const struct GNUNET_HashCode *key,
1183 struct DirectNeighbor *neighbor = cls;
1184 struct Target *new_target;
1185 struct Route *current_route;
1187 new_target = GNUNET_CONTAINER_multihashmap_get (neighbor->neighbor_table_consensus,
1189 if (NULL == new_target)
1191 /* target was revoked, check if it was used */
1192 current_route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1194 if ( (NULL == current_route) ||
1195 (current_route->next_hop != neighbor) )
1197 /* didn't matter, wasn't used */
1200 /* remove existing route */
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Lost route to %s\n",
1203 GNUNET_i2s (¤t_route->target.peer));
1204 GNUNET_assert (GNUNET_YES ==
1205 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, current_route));
1206 send_disconnect_to_plugin (¤t_route->target.peer);
1207 GNUNET_free (current_route);
1208 neighbor->target_removed = GNUNET_YES;
1216 * Check if a target was added to the set of the other peer; if it
1217 * was added or impoves the existing route, do the needed updates.
1219 * @param cls the 'struct DirectNeighbor'
1220 * @param key peer identity for the target
1221 * @param value a 'struct Target' now reachable via the given neighbor
1224 check_target_added (void *cls,
1225 const struct GNUNET_HashCode *key,
1228 struct DirectNeighbor *neighbor = cls;
1229 struct Target *target = value;
1230 struct Route *current_route;
1232 /* target was revoked, check if it was used */
1233 current_route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1235 if (NULL != current_route)
1238 if (current_route->next_hop == neighbor)
1240 /* we had the same route before, no change */
1241 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1243 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1244 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1248 if (ntohl (current_route->target.distance) >= ntohl (target->distance) + 1)
1250 /* alternative, shorter route exists, ignore */
1253 /* new route is better than the existing one, take over! */
1254 /* NOTE: minor security issue: malicious peers may advertise
1255 very short routes to take over longer paths; as we don't
1256 check that the shorter routes actually work, a malicious
1257 direct neighbor can use this to DoS our long routes */
1258 current_route->next_hop = neighbor;
1259 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1260 send_distance_change_to_plugin (&target->peer, ntohl (target->distance) + 1);
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Discovered new route to %s using %u hops\n",
1266 GNUNET_i2s (&target->peer),
1267 (unsigned int) (ntohl (target->distance) + 1));
1268 current_route = GNUNET_new (struct Route);
1269 current_route->next_hop = neighbor;
1270 current_route->target.peer = target->peer;
1271 current_route->target.distance = htonl (ntohl (target->distance) + 1);
1272 GNUNET_assert (GNUNET_YES ==
1273 GNUNET_CONTAINER_multihashmap_put (all_routes,
1274 ¤t_route->target.peer.hashPubKey,
1276 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1277 send_connect_to_plugin (¤t_route->target.peer,
1278 ntohl (current_route->target.distance));
1284 * Callback for set operation results. Called for each element
1285 * in the result set.
1286 * We have learned a new route from the other peer. Add it to the
1287 * route set we're building.
1289 * @param cls the 'struct DirectNeighbor' we're building the consensus with
1290 * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
1291 * @param status see enum GNUNET_SET_Status
1294 handle_set_union_result (void *cls,
1295 const struct GNUNET_SET_Element *element,
1296 enum GNUNET_SET_Status status)
1298 struct DirectNeighbor *neighbor = cls;
1299 struct Target *target;
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Got SET union result: %d\n",
1306 case GNUNET_SET_STATUS_OK:
1307 if (sizeof (struct Target) != element->size)
1309 GNUNET_break_op (0);
1312 target = GNUNET_new (struct Target);
1313 memcpy (target, element->data, sizeof (struct Target));
1315 GNUNET_CONTAINER_multihashmap_put (neighbor->neighbor_table_consensus,
1316 &target->peer.hashPubKey,
1318 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1320 GNUNET_break_op (0);
1321 GNUNET_free (target);
1324 case GNUNET_SET_STATUS_TIMEOUT:
1325 case GNUNET_SET_STATUS_FAILURE:
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Failed to establish DV union, will try again later\n");
1328 neighbor->set_op = NULL;
1329 if (NULL != neighbor->neighbor_table_consensus)
1331 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1334 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table_consensus);
1335 neighbor->neighbor_table_consensus = NULL;
1337 if (1 == GNUNET_CRYPTO_hash_cmp (&neighbor->peer.hashPubKey,
1338 &my_identity.hashPubKey))
1339 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1340 &initiate_set_union,
1343 case GNUNET_SET_STATUS_HALF_DONE:
1344 /* we got all of our updates; integrate routing table! */
1345 neighbor->target_removed = GNUNET_NO;
1346 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1347 &check_target_removed,
1349 if (GNUNET_YES == neighbor->target_removed)
1351 /* check if we got an alternative for the removed routes */
1352 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1356 /* add targets that appeared (and check for improved routes) */
1357 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table_consensus,
1358 &check_target_added,
1360 if (NULL != neighbor->neighbor_table)
1362 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1365 GNUNET_CONTAINER_multihashmap_destroy (neighbor->neighbor_table);
1366 neighbor->neighbor_table = NULL;
1368 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1369 neighbor->neighbor_table_consensus = NULL;
1371 case GNUNET_SET_STATUS_DONE:
1372 /* operation done, schedule next run! */
1373 neighbor->set_op = NULL;
1374 if (1 == GNUNET_CRYPTO_hash_cmp (&neighbor->peer.hashPubKey,
1375 &my_identity.hashPubKey))
1376 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1377 &initiate_set_union,
1388 * Start creating a new DV set union construction, our neighbour has
1389 * asked for it (callback for listening peer).
1391 * @param cls the 'struct DirectNeighbor' of the peer we're building
1392 * a routing consensus with
1393 * @param other_peer the other peer
1394 * @param context_msg message with application specific information from
1396 * @param request request from the other peer, use GNUNET_SET_accept
1397 * to accept it, otherwise the request will be refused
1398 * Note that we don't use a return value here, as it is also
1399 * necessary to specify the set we want to do the operation with,
1400 * whith sometimes can be derived from the context message.
1401 * Also necessary to specify the timeout.
1404 listen_set_union (void *cls,
1405 const struct GNUNET_PeerIdentity *other_peer,
1406 const struct GNUNET_MessageHeader *context_msg,
1407 struct GNUNET_SET_Request *request)
1409 struct DirectNeighbor *neighbor = cls;
1411 if (NULL == request)
1412 return; /* why??? */
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Starting to create consensus with %s!\n",
1415 GNUNET_i2s (&neighbor->peer));
1416 if (NULL != neighbor->set_op)
1418 GNUNET_SET_operation_cancel (neighbor->set_op);
1419 neighbor->set_op = NULL;
1421 if (NULL != neighbor->my_set)
1423 GNUNET_SET_destroy (neighbor->my_set);
1424 neighbor->my_set = NULL;
1426 neighbor->my_set = GNUNET_SET_create (cfg,
1427 GNUNET_SET_OPERATION_UNION);
1428 neighbor->set_op = GNUNET_SET_accept (request,
1429 GNUNET_SET_RESULT_ADDED,
1430 &handle_set_union_result,
1432 build_set (neighbor);
1437 * Start creating a new DV set union by initiating the connection.
1439 * @param cls the 'struct DirectNeighbor' of the peer we're building
1440 * a routing consensus with
1441 * @param tc scheduler context
1444 initiate_set_union (void *cls,
1445 const struct GNUNET_SCHEDULER_TaskContext *tc)
1447 struct DirectNeighbor *neighbor = cls;
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Initiating SET union\n");
1451 neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
1452 neighbor->my_set = GNUNET_SET_create (cfg,
1453 GNUNET_SET_OPERATION_UNION);
1454 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1455 &neighbor->real_session_id,
1457 0 /* FIXME: salt */,
1458 GNUNET_SET_RESULT_ADDED,
1459 &handle_set_union_result,
1461 build_set (neighbor);
1466 * Core handler for DV data messages. Whatever this message
1467 * contains all we really have to do is rip it out of its
1468 * DV layering and give it to our pal the DV plugin to report
1471 * @param cls closure
1472 * @param peer peer which sent the message (immediate sender)
1473 * @param message the message
1474 * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
1477 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1478 const struct GNUNET_MessageHeader *message)
1480 const struct RouteMessage *rm;
1481 const struct GNUNET_MessageHeader *payload;
1482 struct Route *route;
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 "Handling DV message\n");
1486 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1488 GNUNET_break_op (0);
1489 return GNUNET_SYSERR;
1491 rm = (const struct RouteMessage *) message;
1492 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1493 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1495 GNUNET_break_op (0);
1496 return GNUNET_SYSERR;
1498 if (0 == memcmp (&rm->target,
1500 sizeof (struct GNUNET_PeerIdentity)))
1502 /* message is for me, check reverse route! */
1503 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1504 &rm->sender.hashPubKey);
1507 /* don't have reverse route, drop */
1508 GNUNET_STATISTICS_update (stats,
1509 "# message discarded (no reverse route)",
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Delivering %u bytes to myself!\n",
1515 ntohs (payload->size));
1516 send_data_to_plugin (payload,
1518 ntohl (route->target.distance));
1521 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1522 &rm->target.hashPubKey);
1525 GNUNET_STATISTICS_update (stats,
1526 "# messages discarded (no route)",
1530 if (ntohl (route->target.distance) > ntohl (rm->distance) + 1)
1532 GNUNET_STATISTICS_update (stats,
1533 "# messages discarded (target too far)",
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538 "Forwarding message to %s\n",
1539 GNUNET_i2s (&rm->target));
1540 forward_payload (route->next_hop,
1541 ntohl (route->target.distance),
1551 * Service server's handler for message send requests (which come
1552 * bubbling up to us through the DV plugin).
1554 * @param cls closure
1555 * @param client identification of the client
1556 * @param message the actual message
1559 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1560 const struct GNUNET_MessageHeader *message)
1562 struct Route *route;
1563 const struct GNUNET_DV_SendMessage *msg;
1564 const struct GNUNET_MessageHeader *payload;
1566 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1572 msg = (const struct GNUNET_DV_SendMessage *) message;
1573 GNUNET_break (0 != ntohl (msg->uid));
1574 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1575 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1581 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
1582 &msg->target.hashPubKey);
1585 /* got disconnected */
1586 GNUNET_STATISTICS_update (stats,
1587 "# local messages discarded (no route)",
1589 send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1590 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "Forwarding %u bytes to %s\n",
1595 ntohs (payload->size),
1596 GNUNET_i2s (&msg->target));
1598 forward_payload (route->next_hop,
1599 ntohl (route->target.distance),
1604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1609 * Cleanup all of the data structures associated with a given neighbor.
1611 * @param neighbor neighbor to clean up
1614 cleanup_neighbor (struct DirectNeighbor *neighbor)
1616 struct PendingMessage *pending;
1618 while (NULL != (pending = neighbor->pm_head))
1620 neighbor->pm_queue_size--;
1621 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1624 GNUNET_free (pending);
1626 handle_direct_disconnect (neighbor);
1627 GNUNET_assert (GNUNET_YES ==
1628 GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
1629 &neighbor->peer.hashPubKey,
1631 GNUNET_free (neighbor);
1636 * Method called whenever a given peer disconnects.
1638 * @param cls closure
1639 * @param peer peer identity this notification is about
1642 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1644 struct DirectNeighbor *neighbor;
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1647 "Received core peer disconnect message for peer `%s'!\n",
1649 /* Check for disconnect from self message */
1650 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1653 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
1654 if (NULL == neighbor)
1659 GNUNET_break (GNUNET_YES == neighbor->connected);
1660 neighbor->connected = GNUNET_NO;
1661 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1663 GNUNET_STATISTICS_update (stats,
1664 "# peers connected (1-hop)",
1667 cleanup_neighbor (neighbor);
1668 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1675 * Multihashmap iterator for freeing routes. Should never be called.
1678 * @param key key value stored under
1679 * @param value the route to be freed
1681 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1684 free_route (void *cls, const struct GNUNET_HashCode * key, void *value)
1686 struct Route *route = value;
1689 GNUNET_assert (GNUNET_YES ==
1690 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
1691 release_route (route);
1692 send_disconnect_to_plugin (&route->target.peer);
1693 GNUNET_free (route);
1699 * Multihashmap iterator for freeing direct neighbors. Should never be called.
1702 * @param key key value stored under
1703 * @param value the direct neighbor to be freed
1705 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1708 free_direct_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
1710 struct DirectNeighbor *neighbor = value;
1713 cleanup_neighbor (neighbor);
1719 * Task run during shutdown.
1725 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1729 GNUNET_CORE_disconnect (core_api);
1731 GNUNET_ATS_performance_done (ats);
1733 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1734 &free_direct_neighbors, NULL);
1735 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1737 GNUNET_CONTAINER_multihashmap_destroy (direct_neighbors);
1738 GNUNET_CONTAINER_multihashmap_destroy (all_routes);
1739 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1741 GNUNET_SERVER_notification_context_destroy (nc);
1743 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1744 GNUNET_array_grow (consensi[i].targets,
1745 consensi[i].array_length,
1751 * Notify newly connected client about an existing route.
1753 * @param cls the 'struct GNUNET_SERVER_Client'
1754 * @param key peer identity
1755 * @param value the XXX.
1756 * @return GNUNET_OK (continue to iterate)
1759 add_route (void *cls,
1760 const struct GNUNET_HashCode *key,
1763 struct GNUNET_SERVER_Client *client = cls;
1764 struct Route *route = value;
1765 struct GNUNET_DV_ConnectMessage cm;
1767 cm.header.size = htons (sizeof (cm));
1768 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
1769 cm.distance = htonl (route->target.distance);
1770 cm.peer = route->target.peer;
1772 GNUNET_SERVER_notification_context_unicast (nc,
1781 * Handle START-message. This is the first message sent to us
1782 * by the client (can only be one!).
1784 * @param cls closure (always NULL)
1785 * @param client identification of the client
1786 * @param message the actual message
1789 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1790 const struct GNUNET_MessageHeader *message)
1792 GNUNET_SERVER_notification_context_add (nc, client);
1793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1794 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1801 * Called on core init.
1804 * @param server legacy
1805 * @param identity this peer's identity
1808 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1809 const struct GNUNET_PeerIdentity *identity)
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 GNUNET_i2s (identity));
1814 my_identity = *identity;
1819 * Process dv requests.
1821 * @param cls closure
1822 * @param server the initialized server
1823 * @param c configuration to use
1826 run (void *cls, struct GNUNET_SERVER_Handle *server,
1827 const struct GNUNET_CONFIGURATION_Handle *c)
1829 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1830 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
1833 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1834 {&handle_start, NULL,
1835 GNUNET_MESSAGE_TYPE_DV_START,
1836 sizeof (struct GNUNET_MessageHeader) },
1837 { &handle_dv_send_message, NULL,
1838 GNUNET_MESSAGE_TYPE_DV_SEND,
1844 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1845 all_routes = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
1846 core_api = GNUNET_CORE_connect (cfg, NULL,
1848 &handle_core_connect,
1849 &handle_core_disconnect,
1854 if (NULL == core_api)
1856 ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
1859 GNUNET_CORE_disconnect (core_api);
1862 nc = GNUNET_SERVER_notification_context_create (server,
1863 MAX_QUEUE_SIZE_PLUGIN);
1864 stats = GNUNET_STATISTICS_create ("dv", cfg);
1865 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1866 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1867 &shutdown_task, NULL);
1872 * The main function for the dv service.
1874 * @param argc number of arguments from the command line
1875 * @param argv command line arguments
1876 * @return 0 ok, 1 on error
1879 main (int argc, char *const *argv)
1881 return (GNUNET_OK ==
1882 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
1883 &run, NULL)) ? 0 : 1;
1886 /* end of gnunet-service-dv.c */