2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * Next target for the message (a neighbour of ours).
150 struct GNUNET_PeerIdentity next_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 other 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 struct GNUNET_SCHEDULER_Task * initiate_task;
236 * At what offset are we, with respect to inserting our own routes
237 * into the consensus?
239 unsigned int consensus_insertion_offset;
242 * At what distance are we, with respect to inserting our own routes
243 * into the consensus?
245 unsigned int consensus_insertion_distance;
248 * Number of messages currently in the 'pm_XXXX'-DLL.
250 unsigned int pm_queue_size;
253 * Elements in consensus
255 unsigned int consensus_elements;
258 * Direct one hop route
260 struct Route *direct_route;
263 * Flag set within 'check_target_removed' to trigger full global route refresh.
268 * Our distance to this peer, 0 for unknown.
273 * The network this peer is in
275 enum GNUNET_ATS_Network_Type network;
278 * Is this neighbor connected at the core level?
286 * A route includes information about the next hop,
287 * the target, and the ultimate distance to the
294 * Which peer do we need to forward the message to?
296 struct DirectNeighbor *next_hop;
299 * What would be the target, and how far is it away?
301 struct Target target;
304 * Offset of this target in the respective consensus set.
306 unsigned int set_offset;
312 * Set of targets we bring to a consensus; all targets in a set have a
313 * distance equal to the sets distance (which is implied by the array
320 * Array of targets in the set, may include NULL entries if a
321 * neighbor has disconnected; the targets are allocated with the
322 * respective container (all_routes), not here.
324 struct Route **targets;
327 * Size of the @e targets array.
329 unsigned int array_length;
335 * Peermap of all of our neighbors; processing these usually requires
336 * first checking to see if the peer is core-connected and if the
337 * distance is 1, in which case they are direct neighbors.
339 static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
342 * Hashmap with all routes that we currently support; contains
343 * routing information for all peers from distance 2
344 * up to distance #DEFAULT_FISHEYE_DEPTH.
346 static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
349 * Array of consensus sets we expose to the outside world. Sets
350 * are structured by the distance to the target.
352 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH];
355 * Handle to the core service api.
357 static struct GNUNET_CORE_Handle *core_api;
360 * The identity of our peer.
362 static struct GNUNET_PeerIdentity my_identity;
365 * The configuration for this service.
367 static const struct GNUNET_CONFIGURATION_Handle *cfg;
370 * The client, the DV plugin connected to us (or an event monitor).
371 * Hopefully this client will never change, although if the plugin
372 * dies and returns for some reason it may happen.
374 static struct GNUNET_SERVER_NotificationContext *nc;
377 * Handle for the statistics service.
379 static struct GNUNET_STATISTICS_Handle *stats;
382 * Handle to ATS service.
384 static struct GNUNET_ATS_PerformanceHandle *ats;
387 * Task scheduled to refresh routes based on direct neighbours.
389 static struct GNUNET_SCHEDULER_Task * rr_task;
392 * #GNUNET_YES if we are shutting down.
394 static int in_shutdown;
397 * Start creating a new DV set union by initiating the connection.
399 * @param cls the 'struct DirectNeighbor' of the peer we're building
400 * a routing consensus with
401 * @param tc scheduler context
404 initiate_set_union (void *cls,
405 const struct GNUNET_SCHEDULER_TaskContext *tc);
409 * Start creating a new DV set union construction, our neighbour has
410 * asked for it (callback for listening peer).
412 * @param cls the 'struct DirectNeighbor' of the peer we're building
413 * a routing consensus with
414 * @param other_peer the other peer
415 * @param context_msg message with application specific information from
417 * @param request request from the other peer, use GNUNET_SET_accept
418 * to accept it, otherwise the request will be refused
419 * Note that we don't use a return value here, as it is also
420 * necessary to specify the set we want to do the operation with,
421 * whith sometimes can be derived from the context message.
422 * Also necessary to specify the timeout.
425 listen_set_union (void *cls,
426 const struct GNUNET_PeerIdentity *other_peer,
427 const struct GNUNET_MessageHeader *context_msg,
428 struct GNUNET_SET_Request *request);
432 * Forward a message from another peer to the plugin.
434 * @param message the message to send to the plugin
435 * @param origin the original sender of the message
436 * @param distance distance to the original sender of the message
439 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
440 const struct GNUNET_PeerIdentity *origin,
443 struct GNUNET_DV_ReceivedMessage *received_msg;
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Delivering message from peer `%s' at distance %u\n",
449 (unsigned int) distance);
450 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
451 ntohs (message->size);
452 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
454 GNUNET_break (0); /* too big */
457 received_msg = GNUNET_malloc (size);
458 received_msg->header.size = htons (size);
459 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
460 received_msg->distance = htonl (distance);
461 received_msg->sender = *origin;
462 memcpy (&received_msg[1], message, ntohs (message->size));
463 GNUNET_SERVER_notification_context_broadcast (nc,
464 &received_msg->header,
466 GNUNET_free (received_msg);
471 * Forward a control message to the plugin.
473 * @param message the message to send to the plugin
476 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
478 GNUNET_SERVER_notification_context_broadcast (nc,
485 * Give an (N)ACK message to the plugin, we transmitted a message for it.
487 * @param target peer that received the message
488 * @param uid plugin-chosen UID for the message
489 * @param nack #GNUNET_NO to send ACK, #GNUNET_YES to send NACK
492 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
496 struct GNUNET_DV_AckMessage ack_msg;
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Delivering ACK for message to peer `%s'\n",
500 GNUNET_i2s (target));
501 ack_msg.header.size = htons (sizeof (ack_msg));
502 ack_msg.header.type = htons ((GNUNET_YES == nack)
503 ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
504 : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
505 ack_msg.uid = htonl (uid);
506 ack_msg.target = *target;
507 send_control_to_plugin (&ack_msg.header);
512 * Send a DISTANCE_CHANGED message to the plugin.
514 * @param peer peer with a changed distance
515 * @param distance new distance to the peer
516 * @param network network used by the neighbor
519 send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
521 enum GNUNET_ATS_Network_Type network)
523 struct GNUNET_DV_DistanceUpdateMessage du_msg;
525 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
529 du_msg.header.size = htons (sizeof (du_msg));
530 du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
531 du_msg.distance = htonl (distance);
533 du_msg.network = htonl ((uint32_t) network);
534 send_control_to_plugin (&du_msg.header);
539 * Give a CONNECT message to the plugin.
541 * @param target peer that connected
542 * @param distance distance to the target
543 * @param network the network the next hop is located in
546 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
548 enum GNUNET_ATS_Network_Type network)
550 struct GNUNET_DV_ConnectMessage cm;
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Delivering CONNECT about peer %s with distance %u\n",
554 GNUNET_i2s (target), distance);
555 cm.header.size = htons (sizeof (cm));
556 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
557 cm.distance = htonl (distance);
558 cm.network = htonl ((uint32_t) network);
560 send_control_to_plugin (&cm.header);
565 * Give a DISCONNECT message to the plugin.
567 * @param target peer that disconnected
570 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
572 struct GNUNET_DV_DisconnectMessage dm;
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Delivering DISCONNECT about peer `%s'\n",
576 GNUNET_i2s (target));
577 dm.header.size = htons (sizeof (dm));
578 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
579 dm.reserved = htonl (0);
581 send_control_to_plugin (&dm.header);
586 * Function called to transfer a message to another peer
589 * @param cls closure with the direct neighbor
590 * @param size number of bytes available in buf
591 * @param buf where the callee should write the message
592 * @return number of bytes written to buf
595 core_transmit_notify (void *cls, size_t size, void *buf)
597 struct DirectNeighbor *dn = cls;
599 struct PendingMessage *pending;
606 /* client disconnected */
610 while ( (NULL != (pending = dn->pm_head)) &&
611 (size >= off + (msize = ntohs (pending->msg->size))))
614 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
617 memcpy (&cbuf[off], pending->msg, msize);
618 if (0 != pending->uid)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Acking transmission of %u bytes to %s with plugin\n",
623 GNUNET_i2s (&pending->next_target));
624 send_ack_to_plugin (&pending->next_target,
628 GNUNET_free (pending);
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Transmitting total of %u bytes to %s\n",
634 GNUNET_i2s (&dn->peer));
635 GNUNET_assert (NULL != core_api);
638 GNUNET_CORE_notify_transmit_ready (core_api,
639 GNUNET_YES /* cork */,
640 GNUNET_CORE_PRIO_BEST_EFFORT,
641 GNUNET_TIME_UNIT_FOREVER_REL,
644 &core_transmit_notify, dn);
650 * Forward the given payload to the given target.
652 * @param target where to send the message
653 * @param distance distance to the @a sender
654 * @param uid unique ID for the message
655 * @param sender original sender of the message
656 * @param actual_target ultimate recipient for the message
657 * @param payload payload of the message
660 forward_payload (struct DirectNeighbor *target,
663 const struct GNUNET_PeerIdentity *sender,
664 const struct GNUNET_PeerIdentity *actual_target,
665 const struct GNUNET_MessageHeader *payload)
667 struct PendingMessage *pm;
668 struct RouteMessage *rm;
671 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
673 (0 != memcmp (sender,
675 sizeof (struct GNUNET_PeerIdentity))) )
677 /* not _our_ client and queue is full, drop */
678 GNUNET_STATISTICS_update (stats,
679 "# messages dropped",
683 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
684 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
689 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
690 pm->next_target = target->peer;
692 pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
693 rm = (struct RouteMessage *) &pm[1];
694 rm->header.size = htons ((uint16_t) msize);
695 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
696 rm->distance = htonl (distance);
697 rm->target = *actual_target;
698 rm->sender = *sender;
699 memcpy (&rm[1], payload, ntohs (payload->size));
700 GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
703 target->pm_queue_size++;
704 GNUNET_assert (NULL != core_api);
705 if (NULL == target->cth)
706 target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
707 GNUNET_YES /* cork */,
708 GNUNET_CORE_PRIO_BEST_EFFORT,
709 GNUNET_TIME_UNIT_FOREVER_REL,
712 &core_transmit_notify, target);
717 * Find a free slot for storing a 'route' in the 'consensi'
718 * set at the given distance.
720 * @param distance distance to use for the set slot
723 get_consensus_slot (uint32_t distance)
725 struct ConsensusSet *cs;
728 GNUNET_assert (distance < DEFAULT_FISHEYE_DEPTH);
729 cs = &consensi[distance];
731 while ( (i < cs->array_length) &&
732 (NULL != cs->targets[i]) ) i++;
733 if (i == cs->array_length)
735 GNUNET_array_grow (cs->targets,
737 cs->array_length * 2 + 2);
744 * Allocate a slot in the consensus set for a route.
746 * @param route route to initialize
747 * @param distance which consensus set to use
750 allocate_route (struct Route *route,
755 if (distance >= DEFAULT_FISHEYE_DEPTH)
757 route->target.distance = htonl (distance);
758 route->set_offset = UINT_MAX; /* invalid slot */
761 i = get_consensus_slot (distance);
762 route->set_offset = i;
763 consensi[distance].targets[i] = route;
764 route->target.distance = htonl (distance);
769 * Release a slot in the consensus set for a route.
771 * @param route route to release the slot from
774 release_route (struct Route *route)
776 if (UINT_MAX == route->set_offset)
778 GNUNET_assert (ntohl (route->target.distance) < DEFAULT_FISHEYE_DEPTH);
779 consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
780 route->set_offset = UINT_MAX; /* indicate invalid slot */
785 * Move a route from one consensus set to another.
787 * @param route route to move
788 * @param new_distance new distance for the route (destination set)
791 move_route (struct Route *route,
792 uint32_t new_distance)
794 release_route (route);
795 allocate_route (route, new_distance);
800 * Initialize this neighbors 'my_set' and when done give
801 * it to the pending set operation for execution.
803 * Add a single element to the set per call:
805 * If we reached the last element of a consensus element: increase distance
808 * @param cls the neighbor for which we are building the set
811 build_set (void *cls)
813 struct DirectNeighbor *neighbor = cls;
814 struct GNUNET_SET_Element element;
815 struct Target *target;
819 /* skip over NULL entries */
820 while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
821 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
822 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
823 neighbor->consensus_insertion_offset++;
824 while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
825 (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
827 /* If we reached the last element of a consensus array element: increase distance and start with next array */
828 neighbor->consensus_insertion_offset = 0;
829 neighbor->consensus_insertion_distance++;
830 /* skip over NULL entries */
831 while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
832 (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
833 (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
834 neighbor->consensus_insertion_offset++;
836 if (DEFAULT_FISHEYE_DEPTH == neighbor->consensus_insertion_distance)
838 /* we have added all elements to the set, run the operation */
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Finished building my SET for peer `%s' with %u elements, committing\n",
841 GNUNET_i2s (&neighbor->peer),
842 neighbor->consensus_elements);
843 GNUNET_SET_commit (neighbor->set_op,
845 GNUNET_SET_destroy (neighbor->my_set);
846 neighbor->my_set = NULL;
850 route = consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset];
851 GNUNET_assert (NULL != route);
852 target = &route->target;
853 GNUNET_assert (ntohl (target->distance) < DEFAULT_FISHEYE_DEPTH);
854 element.size = sizeof (struct Target);
855 element.element_type = htons (0);
856 element.data = target;
858 /* Find next non-NULL entry */
859 neighbor->consensus_insertion_offset++;
860 if ( (0 != memcmp (&target->peer, &my_identity, sizeof (my_identity))) &&
861 (0 != memcmp (&target->peer, &neighbor->peer, sizeof (neighbor->peer))) )
863 /* Add target if it is not the neighbor or this peer */
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "Adding peer `%s' with distance %u to SET\n",
866 GNUNET_i2s (&target->peer),
867 ntohl (target->distance) + 1);
868 GNUNET_SET_add_element (neighbor->my_set,
870 &build_set, neighbor);
871 neighbor->consensus_elements++;
874 build_set (neighbor);
879 * A peer is now connected to us at distance 1. Initiate DV exchange.
881 * @param neighbor entry for the neighbor at distance 1
884 handle_direct_connect (struct DirectNeighbor *neighbor)
887 struct GNUNET_HashCode h1;
888 struct GNUNET_HashCode h2;
889 struct GNUNET_HashCode session_id;
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Direct connection to %s established, routing table exchange begins.\n",
893 GNUNET_i2s (&neighbor->peer));
894 GNUNET_STATISTICS_update (stats,
895 "# peers connected (1-hop)",
897 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
901 GNUNET_assert (GNUNET_YES ==
902 GNUNET_CONTAINER_multipeermap_remove (all_routes,
905 send_disconnect_to_plugin (&neighbor->peer);
906 release_route (route);
910 neighbor->direct_route = GNUNET_new (struct Route);
911 neighbor->direct_route->next_hop = neighbor;
912 neighbor->direct_route->target.peer = neighbor->peer;
913 allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Adding direct route to %s\n",
917 GNUNET_i2s (&neighbor->direct_route->target.peer));
920 /* construct session ID seed as XOR of both peer's identities */
921 GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &h1);
922 GNUNET_CRYPTO_hash (&neighbor->peer, sizeof (struct GNUNET_PeerIdentity), &h2);
923 GNUNET_CRYPTO_hash_xor (&h1,
926 /* make sure session ID is unique across applications by salting it with 'DV' */
927 GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
928 GCRY_MD_SHA512, GCRY_MD_SHA256,
930 &session_id, sizeof (session_id),
932 if (0 < memcmp (&neighbor->peer,
934 sizeof (struct GNUNET_PeerIdentity)))
936 if (NULL != neighbor->listen_handle)
941 neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
946 if (NULL != neighbor->listen_handle)
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Starting SET listen operation with peer `%s'\n",
954 GNUNET_i2s(&neighbor->peer));
955 neighbor->listen_handle = GNUNET_SET_listen (cfg,
956 GNUNET_SET_OPERATION_UNION,
957 &neighbor->real_session_id,
966 * Method called whenever a peer connects.
969 * @param peer peer identity this notification is about
972 handle_core_connect (void *cls,
973 const struct GNUNET_PeerIdentity *peer)
975 struct DirectNeighbor *neighbor;
977 /* Check for connect to self message */
978 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
980 /* check if entry exists */
981 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
983 if (NULL != neighbor)
985 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != neighbor->network);
986 GNUNET_break (GNUNET_YES != neighbor->connected);
987 neighbor->connected = GNUNET_YES;
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "Core connected to %s (distance %u)\n",
991 (unsigned int) neighbor->distance);
992 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
994 handle_direct_connect (neighbor);
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "Core connected to %s (distance unknown)\n",
1000 neighbor = GNUNET_new (struct DirectNeighbor);
1001 neighbor->peer = *peer;
1002 GNUNET_assert (GNUNET_YES ==
1003 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1006 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1007 neighbor->connected = GNUNET_YES;
1008 neighbor->distance = 0; /* unknown */
1009 neighbor->network = GNUNET_ATS_NET_UNSPECIFIED;
1014 * Called for each 'target' in a neighbor table to free the associated memory.
1017 * @param key key of the value
1018 * @param value value to free
1019 * @return #GNUNET_OK to continue to iterate
1022 free_targets (void *cls,
1023 const struct GNUNET_PeerIdentity *key,
1026 GNUNET_free (value);
1032 * Add a new route to the given @a target via the given @a neighbor.
1034 * @param target the target of the route
1035 * @param neighbor the next hop for communicating with the @a target
1038 add_new_route (struct Target *target,
1039 struct DirectNeighbor *neighbor)
1041 struct Route *route;
1043 route = GNUNET_new (struct Route);
1044 route->next_hop = neighbor;
1045 route->target.peer = target->peer;
1046 allocate_route (route, ntohl (target->distance) + 1);
1047 GNUNET_assert (GNUNET_YES ==
1048 GNUNET_CONTAINER_multipeermap_put (all_routes,
1049 &route->target.peer,
1051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1052 send_connect_to_plugin (&route->target.peer,
1053 ntohl (route->target.distance),
1059 * Multipeerhmap iterator for checking if a given route is
1060 * (now) useful to this peer.
1062 * @param cls the direct neighbor for the given route
1063 * @param key key value stored under
1064 * @param value a 'struct Target' that may or may not be useful; not that
1065 * the distance in 'target' does not include the first hop yet
1066 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1069 check_possible_route (void *cls,
1070 const struct GNUNET_PeerIdentity *key,
1073 struct DirectNeighbor *neighbor = cls;
1074 struct Target *target = value;
1075 struct Route *route;
1078 GNUNET_CONTAINER_multipeermap_contains (direct_neighbors,
1080 return GNUNET_YES; /* direct route, do not care about alternatives */
1081 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1085 /* we have an existing route, check how it compares with going via 'target' */
1086 if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
1088 /* via 'target' is cheaper than the existing route; switch to alternative route! */
1089 move_route (route, ntohl (target->distance) + 1);
1090 route->next_hop = neighbor;
1091 send_distance_change_to_plugin (&target->peer,
1092 ntohl (target->distance) + 1,
1095 return GNUNET_YES; /* got a route to this target already */
1097 if (ntohl (target->distance) >= DEFAULT_FISHEYE_DEPTH)
1098 return GNUNET_YES; /* distance is too large to be interesting */
1099 add_new_route (target, neighbor);
1105 * Multipeermap iterator for finding routes that were previously
1106 * "hidden" due to a better route (called after a disconnect event).
1109 * @param key peer identity of the given direct neighbor
1110 * @param value a `struct DirectNeighbor` to check for additional routes
1111 * @return #GNUNET_YES to continue iteration
1114 refresh_routes (void *cls,
1115 const struct GNUNET_PeerIdentity *key,
1118 struct DirectNeighbor *neighbor = value;
1120 if ( (GNUNET_YES != neighbor->connected) ||
1121 (DIRECT_NEIGHBOR_COST != neighbor->distance) )
1123 if (NULL != neighbor->neighbor_table)
1124 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1125 &check_possible_route,
1132 * Task to run #refresh_routes() on all direct neighbours.
1138 refresh_routes_task (void *cls,
1139 const struct GNUNET_SCHEDULER_TaskContext *tc)
1142 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
1149 * Asynchronously run #refresh_routes() at the next opportunity
1150 * on all direct neighbours.
1153 schedule_refresh_routes ()
1155 if (NULL == rr_task)
1156 rr_task = GNUNET_SCHEDULER_add_now (&refresh_routes_task,
1162 * Multipeermap iterator for freeing routes that go via a particular
1163 * neighbor that disconnected and is thus no longer available.
1165 * @param cls the direct neighbor that is now unavailable
1166 * @param key key value stored under
1167 * @param value a `struct Route` that may or may not go via neighbor
1169 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
1172 cull_routes (void *cls,
1173 const struct GNUNET_PeerIdentity *key,
1176 struct DirectNeighbor *neighbor = cls;
1177 struct Route *route = value;
1179 if (route->next_hop != neighbor)
1180 return GNUNET_YES; /* not affected */
1181 GNUNET_assert (GNUNET_YES ==
1182 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
1183 release_route (route);
1184 send_disconnect_to_plugin (&route->target.peer);
1185 GNUNET_free (route);
1191 * Handle the case that a direct connection to a peer is
1192 * disrupted. Remove all routes via that peer and
1193 * stop the consensus with it.
1195 * @param neighbor peer that was disconnected (or at least is no
1196 * longer at distance 1)
1199 handle_direct_disconnect (struct DirectNeighbor *neighbor)
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Culling routes via %s due to direct disconnect\n",
1203 GNUNET_i2s (&neighbor->peer));
1204 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
1207 if (NULL != neighbor->cth)
1209 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1210 neighbor->cth = NULL;
1213 if (NULL != neighbor->direct_route)
1215 release_route (neighbor->direct_route);
1216 GNUNET_free (neighbor->direct_route);
1217 neighbor->direct_route = NULL;
1220 if (NULL != neighbor->neighbor_table_consensus)
1222 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1225 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1226 neighbor->neighbor_table_consensus = NULL;
1228 if (NULL != neighbor->neighbor_table)
1230 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1233 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1234 neighbor->neighbor_table = NULL;
1236 if (NULL != neighbor->set_op)
1238 GNUNET_SET_operation_cancel (neighbor->set_op);
1239 neighbor->set_op = NULL;
1241 if (NULL != neighbor->my_set)
1243 GNUNET_SET_destroy (neighbor->my_set);
1244 neighbor->my_set = NULL;
1246 if (NULL != neighbor->listen_handle)
1248 GNUNET_SET_listen_cancel (neighbor->listen_handle);
1249 neighbor->listen_handle = NULL;
1251 if (NULL != neighbor->initiate_task)
1253 GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
1254 neighbor->initiate_task = NULL;
1260 * Function that is called with QoS information about an address; used
1261 * to update our current distance to another peer.
1263 * @param cls closure
1264 * @param address the address
1265 * @param active #GNUNET_YES if this address is actively used
1266 * to maintain a connection to a peer;
1267 * #GNUNET_NO if the address is not actively used;
1268 * #GNUNET_SYSERR if this address is no longer available for ATS
1269 * @param bandwidth_out assigned outbound bandwidth for the connection
1270 * @param bandwidth_in assigned inbound bandwidth for the connection
1271 * @param prop performance data for the address (as far as known)
1274 handle_ats_update (void *cls,
1275 const struct GNUNET_HELLO_Address *address,
1277 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1278 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1279 const struct GNUNET_ATS_Properties *prop)
1281 struct DirectNeighbor *neighbor;
1283 enum GNUNET_ATS_Network_Type network;
1285 if (NULL == address)
1287 /* ATS service temporarily disconnected */
1291 if (GNUNET_YES != active)
1293 // FIXME: handle disconnect/inactive case too!
1296 distance = prop->distance;
1297 network = prop->scope;
1298 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
1299 /* check if entry exists */
1300 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1302 if (NULL != neighbor)
1304 neighbor->network = network;
1305 if (neighbor->distance == distance)
1306 return; /* nothing new to see here, move along */
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "ATS says distance to %s is now %u\n",
1309 GNUNET_i2s (&address->peer),
1310 (unsigned int) distance);
1311 if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
1312 (DIRECT_NEIGHBOR_COST == distance) )
1313 return; /* no change */
1314 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
1316 neighbor->distance = distance;
1317 GNUNET_STATISTICS_update (stats,
1318 "# peers connected (1-hop)",
1320 handle_direct_disconnect (neighbor);
1321 schedule_refresh_routes ();
1324 neighbor->distance = distance;
1325 if (DIRECT_NEIGHBOR_COST != neighbor->distance)
1327 if (GNUNET_YES != neighbor->connected)
1329 handle_direct_connect (neighbor);
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 "ATS says distance to %s is now %u\n",
1334 GNUNET_i2s (&address->peer),
1335 (unsigned int) distance);
1336 neighbor = GNUNET_new (struct DirectNeighbor);
1337 neighbor->peer = address->peer;
1338 GNUNET_assert (GNUNET_YES ==
1339 GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
1342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1343 neighbor->connected = GNUNET_NO; /* not yet */
1344 neighbor->distance = distance;
1345 neighbor->network = network;
1350 * Check if a target was removed from the set of the other peer; if so,
1351 * if we also used it for our route, we need to remove it from our
1352 * 'all_routes' set (and later check if an alternative path now exists).
1354 * @param cls the `struct DirectNeighbor`
1355 * @param key peer identity for the target
1356 * @param value a `struct Target` previously reachable via the given neighbor
1359 check_target_removed (void *cls,
1360 const struct GNUNET_PeerIdentity *key,
1363 struct DirectNeighbor *neighbor = cls;
1364 struct Target *new_target;
1365 struct Route *current_route;
1367 new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
1369 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1371 if (NULL != new_target)
1373 /* target was in old set, is in new set */
1374 if ( (NULL != current_route) &&
1375 (current_route->next_hop == neighbor) &&
1376 (current_route->target.distance != new_target->distance) )
1378 /* need to recalculate routes due to distance change */
1379 neighbor->target_removed = GNUNET_YES;
1383 /* target was revoked, check if it was used */
1384 if ( (NULL == current_route) ||
1385 (current_route->next_hop != neighbor) )
1387 /* didn't matter, wasn't used */
1390 /* remove existing route */
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Lost route to %s\n",
1393 GNUNET_i2s (¤t_route->target.peer));
1394 GNUNET_assert (GNUNET_YES ==
1395 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
1396 send_disconnect_to_plugin (¤t_route->target.peer);
1397 release_route (current_route);
1398 GNUNET_free (current_route);
1399 neighbor->target_removed = GNUNET_YES;
1405 * Check if a target was added to the set of the other peer; if it
1406 * was added or impoves the existing route, do the needed updates.
1408 * @param cls the `struct DirectNeighbor`
1409 * @param key peer identity for the target
1410 * @param value a `struct Target` now reachable via the given neighbor
1413 check_target_added (void *cls,
1414 const struct GNUNET_PeerIdentity *key,
1417 struct DirectNeighbor *neighbor = cls;
1418 struct Target *target = value;
1419 struct Route *current_route;
1421 /* target was revoked, check if it was used */
1422 current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1424 if (NULL != current_route)
1427 if (current_route->next_hop == neighbor)
1429 /* we had the same route before, no change in target */
1430 if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
1432 /* but distance changed! */
1433 if (ntohl (target->distance) + 1 > DEFAULT_FISHEYE_DEPTH)
1435 /* distance increased beyond what is allowed, kill route */
1436 GNUNET_assert (GNUNET_YES ==
1437 GNUNET_CONTAINER_multipeermap_remove (all_routes,
1440 send_disconnect_to_plugin (key);
1441 release_route (current_route);
1442 GNUNET_free (current_route);
1446 /* distance decreased, update route */
1447 move_route (current_route,
1448 ntohl (target->distance) + 1);
1449 send_distance_change_to_plugin (&target->peer,
1450 ntohl (target->distance) + 1,
1456 if (ntohl (current_route->target.distance) <= ntohl (target->distance) + 1)
1458 /* alternative, shorter route exists, ignore */
1461 /* new route is better than the existing one, take over! */
1462 /* NOTE: minor security issue: malicious peers may advertise
1463 very short routes to take over longer paths; as we don't
1464 check that the shorter routes actually work, a malicious
1465 direct neighbor can use this to DoS our long routes */
1467 move_route (current_route, ntohl (target->distance) + 1);
1468 current_route->next_hop = neighbor;
1469 send_distance_change_to_plugin (&target->peer,
1470 ntohl (target->distance) + 1,
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Discovered new route to %s using %u hops\n",
1477 GNUNET_i2s (&target->peer),
1478 (unsigned int) (ntohl (target->distance) + 1));
1479 current_route = GNUNET_new (struct Route);
1480 current_route->next_hop = neighbor;
1481 current_route->target.peer = target->peer;
1482 allocate_route (current_route, ntohl (target->distance) + 1);
1483 GNUNET_assert (GNUNET_YES ==
1484 GNUNET_CONTAINER_multipeermap_put (all_routes,
1485 ¤t_route->target.peer,
1487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1489 send_connect_to_plugin (¤t_route->target.peer,
1490 ntohl (current_route->target.distance),
1497 * Callback for set operation results. Called for each element
1498 * in the result set.
1499 * We have learned a new route from the other peer. Add it to the
1500 * route set we're building.
1502 * @param cls the `struct DirectNeighbor` we're building the consensus with
1503 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
1504 * @param status see `enum GNUNET_SET_Status`
1507 handle_set_union_result (void *cls,
1508 const struct GNUNET_SET_Element *element,
1509 enum GNUNET_SET_Status status)
1511 struct DirectNeighbor *neighbor = cls;
1512 struct DirectNeighbor *dn;
1513 struct Target *target;
1518 case GNUNET_SET_STATUS_OK:
1519 status_str = "GNUNET_SET_STATUS_OK";
1521 case GNUNET_SET_STATUS_FAILURE:
1522 status_str = "GNUNET_SET_STATUS_FAILURE";
1524 case GNUNET_SET_STATUS_HALF_DONE:
1525 status_str = "GNUNET_SET_STATUS_HALF_DONE";
1527 case GNUNET_SET_STATUS_DONE:
1528 status_str = "GNUNET_SET_STATUS_DONE";
1531 status_str = "UNDEFINED";
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "Got SET union result: %s\n",
1540 case GNUNET_SET_STATUS_OK:
1541 if (sizeof (struct Target) != element->size)
1543 GNUNET_break_op (0);
1546 if ( (NULL != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors, &((struct Target *) element->data)->peer))) && (DIRECT_NEIGHBOR_COST == dn->distance) )
1548 /* this is a direct neighbor of ours, we do not care about routes
1552 target = GNUNET_new (struct Target);
1553 memcpy (target, element->data, sizeof (struct Target));
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "Received information about peer `%s' with distance %u from SET\n",
1556 GNUNET_i2s (&target->peer),
1557 ntohl (target->distance) + 1);
1559 if (NULL == neighbor->neighbor_table_consensus)
1560 neighbor->neighbor_table_consensus
1561 = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1563 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
1566 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1568 GNUNET_break_op (0);
1569 GNUNET_free (target);
1572 case GNUNET_SET_STATUS_FAILURE:
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Failed to establish DV union, will try again later\n");
1575 neighbor->set_op = NULL;
1576 if (NULL != neighbor->neighbor_table_consensus)
1578 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1581 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
1582 neighbor->neighbor_table_consensus = NULL;
1584 if (0 < memcmp (&neighbor->peer,
1586 sizeof (struct GNUNET_PeerIdentity)))
1587 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1588 &initiate_set_union,
1591 case GNUNET_SET_STATUS_HALF_DONE:
1593 case GNUNET_SET_STATUS_DONE:
1594 /* we got all of our updates; integrate routing table! */
1595 neighbor->target_removed = GNUNET_NO;
1596 if (NULL == neighbor->neighbor_table_consensus)
1597 neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1598 if (NULL != neighbor->neighbor_table)
1599 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1600 &check_target_removed,
1602 if (GNUNET_YES == neighbor->target_removed)
1604 /* check if we got an alternative for the removed routes */
1605 schedule_refresh_routes ();
1607 /* add targets that appeared (and check for improved routes) */
1608 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
1609 &check_target_added,
1611 if (NULL != neighbor->neighbor_table)
1613 GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
1616 GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
1617 neighbor->neighbor_table = NULL;
1619 neighbor->neighbor_table = neighbor->neighbor_table_consensus;
1620 neighbor->neighbor_table_consensus = NULL;
1622 /* operation done, schedule next run! */
1623 neighbor->set_op = NULL;
1624 if (0 < memcmp (&neighbor->peer,
1626 sizeof (struct GNUNET_PeerIdentity)))
1627 neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
1628 &initiate_set_union,
1639 * Start creating a new DV set union construction, our neighbour has
1640 * asked for it (callback for listening peer).
1642 * @param cls the 'struct DirectNeighbor' of the peer we're building
1643 * a routing consensus with
1644 * @param other_peer the other peer
1645 * @param context_msg message with application specific information from
1647 * @param request request from the other peer, use GNUNET_SET_accept
1648 * to accept it, otherwise the request will be refused
1649 * Note that we don't use a return value here, as it is also
1650 * necessary to specify the set we want to do the operation with,
1651 * whith sometimes can be derived from the context message.
1652 * Also necessary to specify the timeout.
1655 listen_set_union (void *cls,
1656 const struct GNUNET_PeerIdentity *other_peer,
1657 const struct GNUNET_MessageHeader *context_msg,
1658 struct GNUNET_SET_Request *request)
1660 struct DirectNeighbor *neighbor = cls;
1662 if (NULL == request)
1663 return; /* why??? */
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Starting to create consensus with %s\n",
1666 GNUNET_i2s (&neighbor->peer));
1667 if (NULL != neighbor->set_op)
1669 GNUNET_SET_operation_cancel (neighbor->set_op);
1670 neighbor->set_op = NULL;
1672 if (NULL != neighbor->my_set)
1674 GNUNET_SET_destroy (neighbor->my_set);
1675 neighbor->my_set = NULL;
1677 neighbor->my_set = GNUNET_SET_create (cfg,
1678 GNUNET_SET_OPERATION_UNION);
1679 neighbor->set_op = GNUNET_SET_accept (request,
1680 GNUNET_SET_RESULT_ADDED,
1681 &handle_set_union_result,
1683 neighbor->consensus_insertion_offset = 0;
1684 neighbor->consensus_insertion_distance = 0;
1685 neighbor->consensus_elements = 0;
1686 build_set (neighbor);
1691 * Start creating a new DV set union by initiating the connection.
1693 * @param cls the `struct DirectNeighbor *` of the peer we're building
1694 * a routing consensus with
1695 * @param tc scheduler context
1698 initiate_set_union (void *cls,
1699 const struct GNUNET_SCHEDULER_TaskContext *tc)
1701 struct DirectNeighbor *neighbor = cls;
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Initiating SET union with peer `%s'\n",
1705 GNUNET_i2s (&neighbor->peer));
1706 neighbor->initiate_task = NULL;
1707 neighbor->my_set = GNUNET_SET_create (cfg,
1708 GNUNET_SET_OPERATION_UNION);
1709 neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
1710 &neighbor->real_session_id,
1712 GNUNET_SET_RESULT_ADDED,
1713 &handle_set_union_result,
1715 neighbor->consensus_insertion_offset = 0;
1716 neighbor->consensus_insertion_distance = 0;
1717 neighbor->consensus_elements = 0;
1718 build_set (neighbor);
1723 * Core handler for DV data messages. Whatever this message
1724 * contains all we really have to do is rip it out of its
1725 * DV layering and give it to our pal the DV plugin to report
1728 * @param cls closure
1729 * @param peer peer which sent the message (immediate sender)
1730 * @param message the message
1731 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the other peer violated the protocol
1734 handle_dv_route_message (void *cls,
1735 const struct GNUNET_PeerIdentity *peer,
1736 const struct GNUNET_MessageHeader *message)
1738 const struct RouteMessage *rm;
1739 const struct GNUNET_MessageHeader *payload;
1740 struct Route *route;
1741 struct DirectNeighbor *neighbor;
1742 struct DirectNeighbor *dn;
1743 struct Target *target;
1750 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
1752 GNUNET_break_op (0);
1753 return GNUNET_SYSERR;
1755 rm = (const struct RouteMessage *) message;
1756 distance = ntohl (rm->distance);
1757 payload = (const struct GNUNET_MessageHeader *) &rm[1];
1758 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
1760 GNUNET_break_op (0);
1761 return GNUNET_SYSERR;
1763 strncpy (prev, GNUNET_i2s (peer), 4);
1764 strncpy (me, GNUNET_i2s (&my_identity), 4);
1765 strncpy (src, GNUNET_i2s (&rm->sender), 4);
1766 strncpy (dst, GNUNET_i2s (&rm->target), 4);
1767 prev[4] = me[4] = src[4] = dst[4] = '\0';
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
1770 ntohs (message->size) - sizeof (struct RouteMessage),
1771 ntohs (payload->type),
1774 (unsigned int) distance + 1);
1776 if (0 == memcmp (&rm->target,
1778 sizeof (struct GNUNET_PeerIdentity)))
1781 != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1782 &rm->sender))) && (DIRECT_NEIGHBOR_COST == dn->distance))
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1785 "Discarding DV message, as %s is a direct neighbor\n",
1786 GNUNET_i2s (&rm->sender));
1787 GNUNET_STATISTICS_update (stats,
1788 "# messages discarded (direct neighbor)",
1792 /* message is for me, check reverse route! */
1793 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1795 if ( (NULL == route) &&
1796 (distance < DEFAULT_FISHEYE_DEPTH) )
1798 /* don't have reverse route yet, learn it! */
1799 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1801 if (NULL == neighbor)
1806 target = GNUNET_new (struct Target);
1807 target->peer = rm->sender;
1808 target->distance = htonl (distance);
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1810 "Learning sender %s at distance %u from delivery!\n",
1811 GNUNET_i2s (&rm->sender),
1812 (unsigned int) distance + 1);
1813 if (NULL == neighbor->neighbor_table)
1814 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1816 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1821 GNUNET_break_op (0);
1822 GNUNET_free (target);
1825 add_new_route (target, neighbor);
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "Delivering %u bytes from %s to myself!\n",
1829 ntohs (payload->size),
1830 GNUNET_i2s (&rm->sender));
1831 send_data_to_plugin (payload,
1836 if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1838 (NULL == GNUNET_CONTAINER_multipeermap_get (all_routes,
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Learning sender %s at distance %u from forwarding!\n",
1843 GNUNET_i2s (&rm->sender),
1845 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1847 if (NULL == neighbor)
1852 target = GNUNET_new (struct Target);
1853 target->peer = rm->sender;
1854 target->distance = htonl (distance);
1855 if (NULL == neighbor->neighbor_table)
1856 neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1858 GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
1861 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1863 GNUNET_break_op (0);
1864 GNUNET_free (target);
1867 add_new_route (target, neighbor);
1870 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1874 neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
1876 if (NULL == neighbor)
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "No route to %s, not routing %u bytes!\n",
1880 GNUNET_i2s (&rm->target),
1881 ntohs (payload->size));
1882 GNUNET_STATISTICS_update (stats,
1883 "# messages discarded (no route)",
1890 neighbor = route->next_hop;
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1893 "Forwarding message to %s\n",
1894 GNUNET_i2s (&neighbor->peer));
1895 forward_payload (neighbor,
1906 * Service server's handler for message send requests (which come
1907 * bubbling up to us through the DV plugin).
1909 * @param cls closure
1910 * @param client identification of the client
1911 * @param message the actual message
1914 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1915 const struct GNUNET_MessageHeader *message)
1917 struct Route *route;
1918 const struct GNUNET_DV_SendMessage *msg;
1919 const struct GNUNET_MessageHeader *payload;
1921 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
1924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1927 msg = (const struct GNUNET_DV_SendMessage *) message;
1928 GNUNET_break (0 != ntohl (msg->uid));
1929 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1930 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1936 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1940 /* got disconnected */
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "No route to %s, dropping local message of type %u\n",
1943 GNUNET_i2s (&msg->target),
1944 ntohs (payload->type));
1945 GNUNET_STATISTICS_update (stats,
1946 "# local messages discarded (no route)",
1948 send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
1949 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953 "Forwarding %u bytes of type %u to %s\n",
1954 ntohs (payload->size),
1955 ntohs (payload->type),
1956 GNUNET_i2s (&msg->target));
1958 forward_payload (route->next_hop,
1959 0 /* first hop, distance is zero */,
1964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1969 * Cleanup all of the data structures associated with a given neighbor.
1971 * @param neighbor neighbor to clean up
1974 cleanup_neighbor (struct DirectNeighbor *neighbor)
1976 struct PendingMessage *pending;
1978 while (NULL != (pending = neighbor->pm_head))
1980 neighbor->pm_queue_size--;
1981 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1984 GNUNET_free (pending);
1986 handle_direct_disconnect (neighbor);
1987 GNUNET_assert (GNUNET_YES ==
1988 GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
1991 GNUNET_free (neighbor);
1996 * Method called whenever a given peer disconnects.
1998 * @param cls closure
1999 * @param peer peer identity this notification is about
2002 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
2004 struct DirectNeighbor *neighbor;
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "Received core peer disconnect message for peer `%s'!\n",
2009 /* Check for disconnect from self message */
2010 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
2013 GNUNET_CONTAINER_multipeermap_get (direct_neighbors, peer);
2014 if (NULL == neighbor)
2019 GNUNET_break (GNUNET_YES == neighbor->connected);
2020 neighbor->connected = GNUNET_NO;
2021 if (DIRECT_NEIGHBOR_COST == neighbor->distance)
2024 GNUNET_STATISTICS_update (stats,
2025 "# peers connected (1-hop)",
2028 cleanup_neighbor (neighbor);
2030 if (GNUNET_YES == in_shutdown)
2032 schedule_refresh_routes ();
2037 * Multipeermap iterator for freeing routes. Should never be called.
2040 * @param key key value stored under
2041 * @param value the route to be freed
2042 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
2045 free_route (void *cls,
2046 const struct GNUNET_PeerIdentity *key,
2049 struct Route *route = value;
2052 GNUNET_assert (GNUNET_YES ==
2053 GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
2054 release_route (route);
2055 send_disconnect_to_plugin (&route->target.peer);
2056 GNUNET_free (route);
2062 * Multipeermap iterator for freeing direct neighbors. Should never be called.
2065 * @param key key value stored under
2066 * @param value the direct neighbor to be freed
2067 * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
2070 free_direct_neighbors (void *cls,
2071 const struct GNUNET_PeerIdentity *key,
2074 struct DirectNeighbor *neighbor = value;
2076 cleanup_neighbor (neighbor);
2082 * Task run during shutdown.
2088 shutdown_task (void *cls,
2089 const struct GNUNET_SCHEDULER_TaskContext *tc)
2093 in_shutdown = GNUNET_YES;
2094 GNUNET_assert (NULL != core_api);
2095 GNUNET_CORE_disconnect (core_api);
2097 GNUNET_ATS_performance_done (ats);
2099 GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
2100 &free_direct_neighbors, NULL);
2101 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2103 GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
2104 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
2105 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
2107 GNUNET_SERVER_notification_context_destroy (nc);
2109 for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
2111 GNUNET_array_grow (consensi[i].targets,
2112 consensi[i].array_length,
2115 if (NULL != rr_task)
2117 GNUNET_SCHEDULER_cancel (rr_task);
2124 * Notify newly connected client about an existing route.
2126 * @param cls the `struct GNUNET_SERVER_Client *`
2127 * @param key peer identity
2128 * @param value the `struct Route *`
2129 * @return #GNUNET_OK (continue to iterate)
2132 notify_client_about_route (void *cls,
2133 const struct GNUNET_PeerIdentity *key,
2136 struct GNUNET_SERVER_Client *client = cls;
2137 struct Route *route = value;
2138 struct GNUNET_DV_ConnectMessage cm;
2140 memset (&cm, 0, sizeof (cm));
2141 cm.header.size = htons (sizeof (cm));
2142 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
2143 cm.distance = htonl (route->target.distance);
2144 cm.peer = route->target.peer;
2146 GNUNET_SERVER_notification_context_unicast (nc,
2155 * Handle START-message. This is the first message sent to us
2156 * by the client (can only be one!).
2158 * @param cls closure (always NULL)
2159 * @param client identification of the client
2160 * @param message the actual message
2163 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
2164 const struct GNUNET_MessageHeader *message)
2166 GNUNET_SERVER_notification_context_add (nc, client);
2167 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2168 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2169 ¬ify_client_about_route,
2175 * Called on core init.
2178 * @param identity this peer's identity
2181 core_init (void *cls,
2182 const struct GNUNET_PeerIdentity *identity)
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186 GNUNET_i2s (identity));
2187 my_identity = *identity;
2192 * Process dv requests.
2194 * @param cls closure
2195 * @param server the initialized server
2196 * @param c configuration to use
2199 run (void *cls, struct GNUNET_SERVER_Handle *server,
2200 const struct GNUNET_CONFIGURATION_Handle *c)
2202 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2203 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
2206 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2207 {&handle_start, NULL,
2208 GNUNET_MESSAGE_TYPE_DV_START,
2209 sizeof (struct GNUNET_MessageHeader) },
2210 { &handle_dv_send_message, NULL,
2211 GNUNET_MESSAGE_TYPE_DV_SEND,
2215 in_shutdown = GNUNET_NO;
2217 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2218 all_routes = GNUNET_CONTAINER_multipeermap_create (65536, GNUNET_NO);
2219 core_api = GNUNET_CORE_connect (cfg, NULL,
2221 &handle_core_connect,
2222 &handle_core_disconnect,
2227 if (NULL == core_api)
2229 ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
2232 GNUNET_CORE_disconnect (core_api);
2236 nc = GNUNET_SERVER_notification_context_create (server,
2237 MAX_QUEUE_SIZE_PLUGIN);
2238 stats = GNUNET_STATISTICS_create ("dv", cfg);
2239 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2240 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2241 &shutdown_task, NULL);
2246 * The main function for the dv service.
2248 * @param argc number of arguments from the command line
2249 * @param argv command line arguments
2250 * @return 0 ok, 1 on error
2253 main (int argc, char *const *argv)
2255 return (GNUNET_OK ==
2256 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
2257 &run, NULL)) ? 0 : 1;
2260 /* end of gnunet-service-dv.c */