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_consensus_service.h"
41 * How often do we establish the consensu?
43 #define GNUNET_DV_CONSENSUS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5))
46 * Maximum number of messages we queue per peer.
48 #define MAX_QUEUE_SIZE 16
51 * The default fisheye depth, from how many hops away will
54 #define DEFAULT_FISHEYE_DEPTH 3
57 * How many hops is a direct neighbor away?
59 #define DIRECT_NEIGHBOR_COST 1
62 GNUNET_NETWORK_STRUCT_BEGIN
65 * Information about a peer DV can route to. These entries are what
66 * we use as the binary format to establish consensus to create our
67 * routing table and as the address format in the HELLOs.
73 * Identity of the peer we can reach.
75 struct GNUNET_PeerIdentity peer;
78 * How many hops (1-3) is this peer away?
80 uint32_t distance GNUNET_PACKED;
86 * Message exchanged between DV services (via core), requesting a
87 * message to be routed.
92 * Type: GNUNET_MESSAGE_TYPE_DV_ROUTE
94 struct GNUNET_MessageHeader header;
97 * Expected (remaining) distance. Must be always smaller than
98 * DEFAULT_FISHEYE_DEPTH, should be zero at the target. Must
99 * be decremented by one at each hop. Peers must not forward
100 * these messages further once the counter has reached zero.
102 uint32_t distance GNUNET_PACKED;
105 * The (actual) target of the message (this peer, if distance is zero).
107 struct GNUNET_PeerIdentity target;
110 * The (actual) sender of the message.
112 struct GNUNET_PeerIdentity sender;
116 GNUNET_NETWORK_STRUCT_END
120 * Linked list of messages to send to clients.
122 struct PendingMessage
125 * Pointer to next item in the list
127 struct PendingMessage *next;
130 * Pointer to previous item in the list
132 struct PendingMessage *prev;
135 * Actual message to be sent, allocated after this struct.
137 const struct GNUNET_MessageHeader *msg;
140 * Ultimate target for the message.
142 struct GNUNET_PeerIdentity ultimate_target;
145 * Unique ID of the message.
153 * Information about a direct neighbor (core-level, excluding
154 * DV-links, only DV-enabled peers).
156 struct DirectNeighbor
160 * Identity of the peer.
162 struct GNUNET_PeerIdentity peer;
165 * Head of linked list of messages to send to this peer.
167 struct PendingMessage *pm_head;
170 * Tail of linked list of messages to send to this peer.
172 struct PendingMessage *pm_tail;
175 * Transmit handle to core service.
177 struct GNUNET_CORE_TransmitHandle *cth;
180 * Routing table of the neighbor, NULL if not yet established.
181 * Keys are peer identities, values are 'struct Target' entries.
182 * Note that the distances in the targets are from the point-of-view
183 * of the peer, not from us!
185 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table;
188 * Updated routing table of the neighbor, under construction,
189 * NULL if we are not currently building it.
190 * Keys are peer identities, values are 'struct Target' entries.
191 * Note that the distances in the targets are from the point-of-view
192 * of the peer, not from us!
194 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table_consensus;
197 * Active consensus, if we are currently synchronizing the
200 struct GNUNET_CONSENSUS_Handle *consensus;
203 * ID of the task we use to (periodically) update our consensus
206 GNUNET_SCHEDULER_TaskIdentifier consensus_task;
209 * At what offset are we, with respect to inserting our own routes
210 * into the consensus?
212 unsigned int consensus_insertion_offset;
215 * At what distance are we, with respect to inserting our own routes
216 * into the consensus?
218 unsigned int consensus_insertion_distance;
221 * Number of messages currently in the 'pm_XXXX'-DLL.
223 unsigned int pm_queue_size;
229 * A route includes information about the next hop,
230 * the target, and the ultimate distance to the
237 * Which peer do we need to forward the message to?
239 struct DirectNeighbor *next_hop;
242 * What would be the target, and how far is it away?
244 struct Target target;
247 * Offset of this target in the respective consensus set.
249 unsigned int set_offset;
255 * Set of targets we bring to a consensus; all targets in a set have a
256 * distance equal to the sets distance (which is implied by the array
263 * Array of targets in the set, may include NULL entries if a
264 * neighbor has disconnected; the targets are allocated with the
265 * respective container (all_routes), not here.
267 struct Route **targets;
270 * Size of the 'targets' array.
272 unsigned int array_length;
278 * Hashmap of all of our direct neighbors (no DV routing).
280 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
283 * Hashmap with all routes that we currently support; contains
284 * routing information for all peers from distance 2
285 * up to distance DEFAULT_FISHEYE_DEPTH.
287 static struct GNUNET_CONTAINER_MultiHashMap *all_routes;
290 * Array of consensus sets we expose to the outside world. Sets
291 * are structured by the distance to the target.
293 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
296 * Handle to the core service api.
298 static struct GNUNET_CORE_Handle *core_api;
301 * The identity of our peer.
303 static struct GNUNET_PeerIdentity my_identity;
306 * The configuration for this service.
308 static const struct GNUNET_CONFIGURATION_Handle *cfg;
311 * The client, the DV plugin connected to us. Hopefully
312 * this client will never change, although if the plugin dies
313 * and returns for some reason it may happen.
315 static struct GNUNET_SERVER_Client *client_handle;
318 * Transmit handle to the plugin.
320 static struct GNUNET_SERVER_TransmitHandle *plugin_transmit_handle;
323 * Head of DLL for client messages
325 static struct PendingMessage *plugin_pending_head;
328 * Tail of DLL for client messages
330 static struct PendingMessage *plugin_pending_tail;
333 * Handle for the statistics service.
335 struct GNUNET_STATISTICS_Handle *stats;
339 * Get distance information from 'atsi'.
341 * @param atsi performance data
342 * @param atsi_count number of entries in atsi
343 * @return connected transport distance
346 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
347 unsigned int atsi_count)
351 for (i = 0; i < atsi_count; i++)
352 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
353 return ntohl (atsi->value);
354 /* FIXME: we do not have distance data? Assume direct neighbor. */
355 return DIRECT_NEIGHBOR_COST;
360 * Function called to notify a client about the socket
361 * begin ready to queue more data. "buf" will be
362 * NULL and "size" zero if the socket was closed for
363 * writing in the meantime.
366 * @param size number of bytes available in buf
367 * @param buf where the callee should write the message
368 * @return number of bytes written to buf
371 transmit_to_plugin (void *cls, size_t size, void *buf)
374 struct PendingMessage *reply;
378 plugin_transmit_handle = NULL;
381 /* client disconnected */
385 while ( (NULL != (reply = plugin_pending_head)) &&
386 (size >= off + (msize = ntohs (reply->msg->size))))
388 GNUNET_CONTAINER_DLL_remove (plugin_pending_head, plugin_pending_tail,
390 memcpy (&cbuf[off], reply->msg, msize);
394 if (NULL != plugin_pending_head)
395 plugin_transmit_handle =
396 GNUNET_SERVER_notify_transmit_ready (client_handle,
398 GNUNET_TIME_UNIT_FOREVER_REL,
399 &transmit_to_plugin, NULL);
405 * Forward a message from another peer to the plugin.
407 * @param message the message to send to the plugin
408 * @param distant_neighbor the original sender of the message
409 * @param distnace distance to the original sender of the message
412 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
413 const struct GNUNET_PeerIdentity *distant_neighbor,
416 struct GNUNET_DV_ReceivedMessage *received_msg;
417 struct PendingMessage *pending_message;
420 if (NULL == client_handle)
422 GNUNET_STATISTICS_update (stats,
423 "# messages discarded (no plugin)",
425 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
426 _("Refusing to queue messages, DV plugin not active.\n"));
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Delivering message from peer `%s'\n",
431 GNUNET_i2s (distant_neighbor));
432 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
433 ntohs (message->size);
434 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
436 GNUNET_break (0); /* too big */
439 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + size);
440 received_msg = (struct GNUNET_DV_ReceivedMessage *) &pending_message[1];
441 received_msg->header.size = htons (size);
442 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
443 received_msg->distance = htonl (distance);
444 received_msg->sender = *distant_neighbor;
445 memcpy (&received_msg[1], message, ntohs (message->size));
446 GNUNET_CONTAINER_DLL_insert_tail (plugin_pending_head,
449 if (NULL == plugin_transmit_handle)
450 plugin_transmit_handle =
451 GNUNET_SERVER_notify_transmit_ready (client_handle, size,
452 GNUNET_TIME_UNIT_FOREVER_REL,
453 &transmit_to_plugin, NULL);
458 * Forward a control message to the plugin.
460 * @param message the message to send to the plugin
461 * @param distant_neighbor the original sender of the message
462 * @param distnace distance to the original sender of the message
465 send_control_to_plugin (const struct GNUNET_MessageHeader *message)
467 struct PendingMessage *pending_message;
470 if (NULL == client_handle)
472 GNUNET_STATISTICS_update (stats,
473 "# control messages discarded (no plugin)",
475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
476 _("Refusing to queue messages, DV plugin not active.\n"));
479 size = ntohs (message->size);
480 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + size);
481 memcpy (&pending_message[1], message, size);
482 GNUNET_CONTAINER_DLL_insert_tail (plugin_pending_head,
485 if (NULL == plugin_transmit_handle)
486 plugin_transmit_handle =
487 GNUNET_SERVER_notify_transmit_ready (client_handle, size,
488 GNUNET_TIME_UNIT_FOREVER_REL,
489 &transmit_to_plugin, NULL);
494 * Give an ACK message to the plugin, we transmitted a message for it.
496 * @param target peer that received the message
497 * @param uid plugin-chosen UID for the message
500 send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
503 struct GNUNET_DV_AckMessage ack_msg;
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "Delivering ACK for message to peer `%s'\n",
507 GNUNET_i2s (target));
508 ack_msg.header.size = htons (sizeof (ack_msg));
509 ack_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
510 ack_msg.uid = htonl (uid);
511 ack_msg.target = *target;
512 send_control_to_plugin (&ack_msg.header);
517 * Give a CONNECT message to the plugin.
519 * @param target peer that connected
520 * @param distance distance to the target
523 send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
526 struct GNUNET_DV_ConnectMessage cm;
528 if (NULL == client_handle)
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Delivering CONNECT about peer `%s'\n",
532 GNUNET_i2s (target));
533 cm.header.size = htons (sizeof (cm));
534 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
535 cm.distance = htonl (distance);
537 send_control_to_plugin (&cm.header);
542 * Give a DISCONNECT message to the plugin.
544 * @param target peer that disconnected
547 send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
549 struct GNUNET_DV_DisconnectMessage dm;
551 if (NULL == client_handle)
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "Delivering DISCONNECT about peer `%s'\n",
555 GNUNET_i2s (target));
556 dm.header.size = htons (sizeof (dm));
557 dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
558 dm.reserved = htonl (0);
560 send_control_to_plugin (&dm.header);
565 * Function called to transfer a message to another peer
568 * @param cls closure with the direct neighbor
569 * @param size number of bytes available in buf
570 * @param buf where the callee should write the message
571 * @return number of bytes written to buf
574 core_transmit_notify (void *cls, size_t size, void *buf)
576 struct DirectNeighbor *dn = cls;
578 struct PendingMessage *pending;
585 /* peer disconnected */
589 pending = dn->pm_head;
591 while ( (NULL != (pending = dn->pm_head)) &&
592 (size >= off + (msize = ntohs (pending->msg->size))))
595 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
598 memcpy (&cbuf[off], pending->msg, msize);
599 send_ack_to_plugin (&pending->ultimate_target,
601 GNUNET_free (pending);
604 if (NULL != dn->pm_head)
606 GNUNET_CORE_notify_transmit_ready (core_api,
607 GNUNET_YES /* cork */,
609 GNUNET_TIME_UNIT_FOREVER_REL,
612 &core_transmit_notify, dn);
618 * Forward the given payload to the given target.
620 * @param target where to send the message
621 * @param distance expected (remaining) distance to the target
622 * @param sender original sender of the message
623 * @param payload payload of the message
626 forward_payload (struct DirectNeighbor *target,
628 const struct GNUNET_PeerIdentity *sender,
629 const struct GNUNET_MessageHeader *payload)
631 struct PendingMessage *pm;
632 struct RouteMessage *rm;
635 if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
636 (0 != memcmp (sender,
638 sizeof (struct GNUNET_PeerIdentity))) )
640 msize = sizeof (struct RouteMessage) + ntohs (payload->size);
641 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
646 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
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 = 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[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 = new_distance;
747 * Method called whenever a peer connects.
750 * @param peer peer identity this notification is about
751 * @param atsi performance data
752 * @param atsi_count number of entries in atsi
755 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
756 const struct GNUNET_ATS_Information *atsi,
757 unsigned int atsi_count)
759 struct DirectNeighbor *neighbor;
763 /* Check for connect to self message */
764 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
766 distance = get_atsi_distance (atsi, atsi_count);
767 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
769 if (NULL != neighbor)
774 if (DIRECT_NEIGHBOR_COST != distance)
775 return; /* is a DV-neighbor */
776 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
777 neighbor->peer = *peer;
778 GNUNET_assert (GNUNET_YES ==
779 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
782 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
783 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
787 send_disconnect_to_plugin (peer);
788 release_route (route);
791 route->next_hop = neighbor;
792 // FIXME: begin exchange_routing_information!
798 * Core handler for DV data messages. Whatever this message
799 * contains all we really have to do is rip it out of its
800 * DV layering and give it to our pal the DV plugin to report
804 * @param peer peer which sent the message (immediate sender)
805 * @param message the message
806 * @param atsi transport ATS information (latency, distance, etc.)
807 * @param atsi_count number of entries in atsi
808 * @return GNUNET_OK on success, GNUNET_SYSERR if the other peer violated the protocol
811 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
812 const struct GNUNET_MessageHeader *message,
813 const struct GNUNET_ATS_Information *atsi,
814 unsigned int atsi_count)
816 const struct RouteMessage *rm;
817 const struct GNUNET_MessageHeader *payload;
820 if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
823 return GNUNET_SYSERR;
825 rm = (const struct RouteMessage *) message;
826 payload = (const struct GNUNET_MessageHeader *) &rm[1];
827 if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
830 return GNUNET_SYSERR;
832 if (0 == memcmp (&rm->target,
834 sizeof (struct GNUNET_PeerIdentity)))
836 /* message is for me, check reverse route! */
837 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
838 &rm->sender.hashPubKey);
841 /* don't have reverse route, drop */
842 GNUNET_STATISTICS_update (stats,
843 "# message discarded (no reverse route)",
847 send_data_to_plugin (payload,
849 route->target.distance);
852 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
853 &rm->target.hashPubKey);
856 GNUNET_STATISTICS_update (stats,
857 "# messages discarded (no route)",
861 if (route->target.distance > ntohl (rm->distance) + 1)
863 GNUNET_STATISTICS_update (stats,
864 "# messages discarded (target too far)",
868 forward_payload (route->next_hop,
869 route->target.distance,
877 * Service server's handler for message send requests (which come
878 * bubbling up to us through the DV plugin).
881 * @param client identification of the client
882 * @param message the actual message
885 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
886 const struct GNUNET_MessageHeader *message)
889 const struct GNUNET_DV_SendMessage *msg;
890 const struct GNUNET_MessageHeader *payload;
892 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
898 msg = (const struct GNUNET_DV_SendMessage *) message;
899 payload = (const struct GNUNET_MessageHeader *) &msg[1];
900 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
906 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
907 &msg->target.hashPubKey);
910 /* got disconnected, send ACK anyway?
911 FIXME: What we really want is an 'NACK' here... */
912 GNUNET_STATISTICS_update (stats,
913 "# local messages discarded (no route)",
915 send_ack_to_plugin (&msg->target, htonl (msg->uid));
916 GNUNET_SERVER_receive_done (client, GNUNET_OK);
919 // FIXME: flow control (send ACK only once message has left the queue...)
920 send_ack_to_plugin (&msg->target, htonl (msg->uid));
921 forward_payload (route->next_hop,
922 route->target.distance,
925 GNUNET_SERVER_receive_done (client, GNUNET_OK);
930 * Multihashmap iterator for freeing routes that go via a particular
931 * neighbor that disconnected and is thus no longer available.
933 * @param cls the direct neighbor that is now unavailable
934 * @param key key value stored under
935 * @param value a 'struct Route' that may or may not go via neighbor
937 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
940 cull_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
942 struct DirectNeighbor *neighbor = cls;
943 struct Route *route = value;
945 if (route->next_hop != neighbor)
946 return GNUNET_YES; /* not affected */
947 GNUNET_assert (GNUNET_YES ==
948 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
949 release_route (route);
950 send_disconnect_to_plugin (&route->target.peer);
957 * Multihashmap iterator for checking if a given route is
958 * (now) useful to this peer.
960 * @param cls the direct neighbor for the given route
961 * @param key key value stored under
962 * @param value a 'struct Target' that may or may not be useful; not that
963 * the distance in 'target' does not include the first hop yet
964 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
967 check_possible_route (void *cls, const struct GNUNET_HashCode * key, void *value)
969 struct DirectNeighbor *neighbor = cls;
970 struct Target *target = value;
973 route = GNUNET_CONTAINER_multihashmap_get (all_routes,
977 if (route->target.distance > target->distance + 1)
979 /* this 'target' is cheaper than the existing route; switch to alternative route! */
980 move_route (route, target->distance + 1);
981 route->next_hop = neighbor;
982 // FIXME: notify plugin about distance update?
984 return GNUNET_YES; /* got a route to this target already */
986 route = GNUNET_malloc (sizeof (struct Route));
987 route->next_hop = neighbor;
988 route->target.distance = target->distance + 1;
989 route->target.peer = target->peer;
990 allocate_route (route, route->target.distance);
991 GNUNET_assert (GNUNET_YES ==
992 GNUNET_CONTAINER_multihashmap_put (all_routes,
993 &route->target.peer.hashPubKey,
995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
996 send_connect_to_plugin (&route->target.peer, target->distance);
1002 * Multihashmap iterator for finding routes that were previously
1003 * "hidden" due to a better route (called after a disconnect event).
1006 * @param key peer identity of the given direct neighbor
1007 * @param value a 'struct DirectNeighbor' to check for additional routes
1008 * @return GNUNET_YES to continue iteration
1011 refresh_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
1013 struct DirectNeighbor *neighbor = value;
1015 if (NULL != neighbor->neighbor_table)
1016 GNUNET_CONTAINER_multihashmap_iterate (neighbor->neighbor_table,
1017 &check_possible_route,
1024 * Cleanup all of the data structures associated with a given neighbor.
1026 * @param neighbor neighbor to clean up
1029 cleanup_neighbor (struct DirectNeighbor *neighbor)
1031 struct PendingMessage *pending;
1033 while (NULL != (pending = neighbor->pm_head))
1035 neighbor->pm_queue_size--;
1036 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
1039 GNUNET_free (pending);
1041 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1044 if (NULL != neighbor->cth)
1046 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
1047 neighbor->cth = NULL;
1049 if (GNUNET_SCHEDULER_NO_TASK != neighbor->consensus_task)
1051 GNUNET_SCHEDULER_cancel (neighbor->consensus_task);
1052 neighbor->consensus_task = GNUNET_SCHEDULER_NO_TASK;
1054 if (NULL != neighbor->consensus)
1056 GNUNET_CONSENSUS_destroy (neighbor->consensus);
1057 neighbor->consensus = NULL;
1059 GNUNET_assert (GNUNET_YES ==
1060 GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
1061 &neighbor->peer.hashPubKey,
1063 GNUNET_free (neighbor);
1068 * Method called whenever a given peer disconnects.
1070 * @param cls closure
1071 * @param peer peer identity this notification is about
1074 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1076 struct DirectNeighbor *neighbor;
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Received core peer disconnect message for peer `%s'!\n",
1081 /* Check for disconnect from self message */
1082 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1085 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
1086 if (NULL == neighbor)
1088 /* must have been a DV-neighbor, ignore */
1091 cleanup_neighbor (neighbor);
1092 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1099 * Multihashmap iterator for freeing routes. Should never be called.
1102 * @param key key value stored under
1103 * @param value the route to be freed
1105 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1108 free_route (void *cls, const struct GNUNET_HashCode * key, void *value)
1110 struct Route *route = value;
1113 GNUNET_assert (GNUNET_YES ==
1114 GNUNET_CONTAINER_multihashmap_remove (all_routes, key, value));
1115 release_route (route);
1116 send_disconnect_to_plugin (&route->target.peer);
1117 GNUNET_free (route);
1123 * Multihashmap iterator for freeing direct neighbors. Should never be called.
1126 * @param key key value stored under
1127 * @param value the direct neighbor to be freed
1129 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1132 free_direct_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
1134 struct DirectNeighbor *neighbor = value;
1137 cleanup_neighbor (neighbor);
1143 * Task run during shutdown.
1149 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1151 struct PendingMessage *pending;
1154 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
1155 &free_direct_neighbors, NULL);
1156 GNUNET_CONTAINER_multihashmap_destroy (direct_neighbors);
1157 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
1159 GNUNET_CONTAINER_multihashmap_destroy (all_routes);
1160 GNUNET_CORE_disconnect (core_api);
1162 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1164 while (NULL != (pending = plugin_pending_head))
1166 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
1167 plugin_pending_tail,
1169 GNUNET_free (pending);
1171 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
1172 GNUNET_array_grow (consensi[i].targets,
1173 consensi[i].array_length,
1179 * Handle START-message. This is the first message sent to us
1180 * by the client (can only be one!).
1182 * @param cls closure (always NULL)
1183 * @param client identification of the client
1184 * @param message the actual message
1187 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1188 const struct GNUNET_MessageHeader *message)
1190 if (NULL != client_handle)
1192 /* forcefully drop old client */
1193 GNUNET_SERVER_client_disconnect (client_handle);
1194 GNUNET_SERVER_client_drop (client_handle);
1196 client_handle = client;
1197 GNUNET_SERVER_client_keep (client_handle);
1198 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1203 * Called on core init.
1206 * @param server legacy
1207 * @param identity this peer's identity
1210 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1211 const struct GNUNET_PeerIdentity *identity)
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 GNUNET_i2s (identity));
1216 my_identity = *identity;
1221 * Process dv requests.
1223 * @param cls closure
1224 * @param server the initialized server
1225 * @param c configuration to use
1228 run (void *cls, struct GNUNET_SERVER_Handle *server,
1229 const struct GNUNET_CONFIGURATION_Handle *c)
1231 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1232 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
1235 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1236 {&handle_start, NULL,
1237 GNUNET_MESSAGE_TYPE_DV_START,
1238 sizeof (struct GNUNET_MessageHeader) },
1239 { &handle_dv_send_message, NULL,
1240 GNUNET_MESSAGE_TYPE_DV_SEND,
1246 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1247 all_routes = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
1248 core_api = GNUNET_CORE_connect (cfg, NULL,
1250 &handle_core_connect,
1251 &handle_core_disconnect,
1256 if (NULL == core_api)
1258 stats = GNUNET_STATISTICS_create ("dv", cfg);
1259 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1260 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1261 &shutdown_task, NULL);
1266 * The main function for the dv service.
1268 * @param argc number of arguments from the command line
1269 * @param argv command line arguments
1270 * @return 0 ok, 1 on error
1273 main (int argc, char *const *argv)
1275 return (GNUNET_OK ==
1276 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
1277 &run, NULL)) ? 0 : 1;
1280 /* end of gnunet-service-dv.c */