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
32 #include "gnunet_util_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_consensus_service.h"
42 * How often do we establish the consensu?
44 #define GNUNET_DV_CONSENSUS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5))
47 * The default fisheye depth, from how many hops away will
50 #define DEFAULT_FISHEYE_DEPTH 3
53 * How many hops is a direct neighbor away?
55 #define DIRECT_NEIGHBOR_COST 1
57 GNUNET_NETWORK_STRUCT_BEGIN
60 * Information about a peer DV can route to. These entries are what
61 * we use as the binary format to establish consensus to create our
62 * routing table and as the address format in the HELLOs.
68 * Identity of the peer we can reach.
70 struct GNUNET_PeerIdentity peer;
73 * How many hops (1-3) is this peer away?
75 uint32_t distance GNUNET_PACKED;
81 * Message exchanged between DV services (via core), requesting a
82 * message to be routed.
87 * Type: GNUNET_MESSAGE_TYPE_DV_ROUTE
89 struct GNUNET_MessageHeader header;
92 * Expected (remaining) distance. Must be always smaller than
93 * DEFAULT_FISHEYE_DEPTH, should be zero at the target. Must
94 * be decremented by one at each hop. Peers must not forward
95 * these messages further once the counter has reached zero.
97 uint32_t distance GNUNET_PACKED;
100 * The (actual) target of the message (this peer, if distance is zero).
102 struct GNUNET_PeerIdentity target;
106 GNUNET_NETWORK_STRUCT_END
110 * Linked list of messages to send to clients.
112 struct PendingMessage
115 * Pointer to next item in the list
117 struct PendingMessage *next;
120 * Pointer to previous item in the list
122 struct PendingMessage *prev;
125 * Actual message to be sent, allocated after this struct.
127 const struct GNUNET_MessageHeader *msg;
130 * Ultimate target for the message.
132 struct GNUNET_PeerIdentity ultimate_target;
135 * Unique ID of the message.
143 * Information about a direct neighbor (core-level, excluding
144 * DV-links, only DV-enabled peers).
146 struct DirectNeighbor
150 * Identity of the peer.
152 struct GNUNET_PeerIdentity peer;
155 * Head of linked list of messages to send to this peer.
157 struct PendingMessage *pm_head;
160 * Tail of linked list of messages to send to this peer.
162 struct PendingMessage *pm_tail;
165 * Transmit handle to core service.
167 struct GNUNET_CORE_TransmitHandle *cth;
172 * A route includes information about the next hop,
173 * the target, and the ultimate distance to the
180 * Which peer do we need to forward the message to?
182 struct DirectNeighbor *next_hop;
185 * What would be the target, and how far is it away?
187 struct Target target;
190 * Offset of this target in the respective consensus set.
192 unsigned int set_offset;
198 * Routing neighbors are neighbors that we exchange
199 * routing information with; that is, their distance
200 * must be strictly less than the DEFAULT_FISHEYE_DEPTH;
201 * they can also be direct neighbors.
203 struct RoutingNeighbor
207 * Which peer is this, and how do we talk to it?
212 * Routing table of the neighbor, NULL if not yet established.
214 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table;
217 * Updated routing table of the neighbor, under construction,
218 * NULL if we are not currently building it.
220 struct GNUNET_CONTAINER_MultiHashMap *neighbor_table_consensus;
223 * Active consensus, if we are currently synchronizing the
226 struct GNUNET_CONSENSUS_Handle *consensus;
229 * At what offset are we, with respect to inserting our own routes
230 * into the consensus?
232 unsigned int consensus_insertion_offset;
235 * At what distance are we, with respect to inserting our own routes
236 * into the consensus?
238 unsigned int consensus_insertion_distance;
244 * Set of targets we bring to a consensus; all targets in a set have a
245 * distance equal to the sets distance (which is implied by the array
252 * Array of targets in the set, may include NULL
253 * entries if a neighbor has disconnected; the
254 * targets are allocated with the respective
255 * 'struct Route', not here.
257 struct Target **targets;
260 * Size of the 'targets' array.
262 unsigned int array_length;
268 * Hashmap of all of our direct neighbors (no DV routing).
270 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
273 * Hashmap of all of the neighbors we exchange routing information
274 * with (peers up to DEFAULT_FISHEYE_DEPTH - 1 distance from us).
276 static struct GNUNET_CONTAINER_MultiHashMap *routing_neighbors;
279 * Hashmap with all routes that we currently support; contains
280 * routing information for all peers up to distance DEFAULT_FISHEYE_DEPTH.
282 static struct GNUNET_CONTAINER_MultiHashMap *all_routes;
285 * Array of consensus sets we expose to the outside world. Sets
286 * are structured by the distance to the target.
288 static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH - 1];
291 * ID of the task we use to (periodically) update our consensus
294 static GNUNET_SCHEDULER_Task consensus_task;
297 * Handle to the core service api.
299 static struct GNUNET_CORE_Handle *core_api;
302 * The identity of our peer.
304 static struct GNUNET_PeerIdentity my_identity;
307 * The configuration for this service.
309 static const struct GNUNET_CONFIGURATION_Handle *cfg;
312 * The client, the DV plugin connected to us. Hopefully
313 * this client will never change, although if the plugin dies
314 * and returns for some reason it may happen.
316 static struct GNUNET_SERVER_Client *client_handle;
319 * Transmit handle to the plugin.
321 static struct GNUNET_SERVER_TransmitHandle *plugin_transmit_handle;
324 * Head of DLL for client messages
326 static struct PendingMessage *plugin_pending_head;
329 * Tail of DLL for client messages
331 static struct PendingMessage *plugin_pending_tail;
334 * Handle for the statistics service.
336 struct GNUNET_STATISTICS_Handle *stats;
339 * How far out to keep peers we learn about.
341 static unsigned long long fisheye_depth;
345 * Get distance information from 'atsi'.
347 * @param atsi performance data
348 * @param atsi_count number of entries in atsi
349 * @return connected transport distance
352 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
353 unsigned int atsi_count)
357 for (i = 0; i < atsi_count; i++)
358 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
359 return ntohl (atsi->value);
360 /* FIXME: we do not have distance data? Assume direct neighbor. */
361 return DIRECT_NEIGHBOR_COST;
366 * Function called to notify a client about the socket
367 * begin ready to queue more data. "buf" will be
368 * NULL and "size" zero if the socket was closed for
369 * writing in the meantime.
372 * @param size number of bytes available in buf
373 * @param buf where the callee should write the message
374 * @return number of bytes written to buf
377 transmit_to_plugin (void *cls, size_t size, void *buf)
380 struct PendingMessage *reply;
384 plugin_transmit_handle = NULL;
387 /* client disconnected */
391 while ( (NULL != (reply = plugin_pending_head)) &&
392 (size >= off + (msize = ntohs (reply->msg->size))))
394 GNUNET_CONTAINER_DLL_remove (plugin_pending_head, plugin_pending_tail,
396 memcpy (&cbuf[off], reply->msg, msize);
400 if (NULL != plugin_pending_head)
401 plugin_transmit_handle =
402 GNUNET_SERVER_notify_transmit_ready (client_handle,
404 GNUNET_TIME_UNIT_FOREVER_REL,
405 &transmit_to_plugin, NULL);
411 * Forward a message from another peer to the plugin.
413 * @param message the message to send to the plugin
414 * @param distant_neighbor the original sender of the message
415 * @param distnace distance to the original sender of the message
418 send_data_to_plugin (const struct GNUNET_MessageHeader *message,
419 struct GNUNET_PeerIdentity *distant_neighbor,
422 struct GNUNET_DV_ReceivedMessage *received_msg;
423 struct PendingMessage *pending_message;
426 if (NULL == client_handle)
428 GNUNET_STATISTICS_update (stats,
429 "# messages discarded (no plugin)",
431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432 _("Refusing to queue messages, DV plugin not active.\n"));
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Delivering message from peer `%s'\n",
437 GNUNET_i2s (distant_neighbor));
438 size = sizeof (struct GNUNET_DV_ReceivedMessage) +
439 ntohs (message->size);
440 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
442 GNUNET_break (0); /* too big */
445 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + size);
446 received_msg = (struct GNUNET_DV_ReceivedMessage *) &pending_message[1];
447 received_msg->header.size = htons (size);
448 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
449 received_msg->distance = htonl (distance);
450 received_msg->sender = *distant_neighbor;
451 memcpy (&received_msg[1], message, ntohs (message->size));
452 GNUNET_CONTAINER_DLL_insert_tail (plugin_pending_head,
455 if (NULL == plugin_transmit_handle)
456 plugin_transmit_handle =
457 GNUNET_SERVER_notify_transmit_ready (client_handle, size,
458 GNUNET_TIME_UNIT_FOREVER_REL,
459 &transmit_to_plugin, NULL);
464 * Give an ACK message to the plugin, we transmitted a message for it.
466 * @param target peer that received the message
467 * @param uid plugin-chosen UID for the message
470 send_ack_to_plugin (struct GNUNET_PeerIdentity *target,
473 struct GNUNET_DV_AckMessage *ack_msg;
474 struct PendingMessage *pending_message;
477 if (NULL == client_handle)
479 GNUNET_STATISTICS_update (stats,
480 "# acks discarded (no plugin)",
482 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
483 _("Refusing to queue messages, DV plugin not active.\n"));
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Delivering ACK for message to peer `%s'\n",
488 GNUNET_i2s (target));
489 size = sizeof (struct GNUNET_DV_AckMessage);
490 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + size);
491 ack_msg = (struct GNUNET_DV_AckMessage *) &pending_message[1];
492 ack_msg->header.size = htons (size);
493 ack_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
494 ack_msg->uid = htonl (uid);
495 ack_msg->target = *target;
496 GNUNET_CONTAINER_DLL_insert_tail (plugin_pending_head,
499 if (NULL == plugin_transmit_handle)
500 plugin_transmit_handle =
501 GNUNET_SERVER_notify_transmit_ready (client_handle, size,
502 GNUNET_TIME_UNIT_FOREVER_REL,
503 &transmit_to_plugin, NULL);
508 * Function called to transfer a message to another peer
511 * @param cls closure with the direct neighbor
512 * @param size number of bytes available in buf
513 * @param buf where the callee should write the message
514 * @return number of bytes written to buf
517 core_transmit_notify (void *cls, size_t size, void *buf)
519 struct DirectNeighbor *dn = cls;
521 struct PendingMessage *pending;
528 /* peer disconnected */
532 pending = dn->pm_head;
534 while ( (NULL != (pending = dn->pm_head)) &&
535 (size >= off + (msize = ntohs (pending->msg->size))))
537 GNUNET_CONTAINER_DLL_remove (dn->pm_head,
540 memcpy (&cbuf[off], pending->msg, msize);
541 send_ack_to_plugin (&pending->ultimate_target,
543 GNUNET_free (pending);
546 if (NULL != dn->pm_head)
548 GNUNET_CORE_notify_transmit_ready (core_api,
549 GNUNET_YES /* cork */,
551 GNUNET_TIME_UNIT_FOREVER_REL,
554 &core_transmit_notify, dn);
560 * Method called whenever a peer connects.
563 * @param peer peer identity this notification is about
564 * @param atsi performance data
565 * @param atsi_count number of entries in atsi
568 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
569 const struct GNUNET_ATS_Information *atsi,
570 unsigned int atsi_count)
572 struct DirectNeighbor *neighbor;
575 /* Check for connect to self message */
576 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
578 distance = get_atsi_distance (atsi, atsi_count);
579 neighbor = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
581 if (NULL != neighbor)
586 if (DIRECT_NEIGHBOR_COST != distance)
587 return; /* is a DV-neighbor */
589 GNUNET_break (0); // FIXME...
595 * Core handler for dv data messages. Whatever this message
596 * contains all we really have to do is rip it out of its
597 * DV layering and give it to our pal the DV plugin to report
601 * @param peer peer which sent the message (immediate sender)
602 * @param message the message
603 * @param atsi transport ATS information (latency, distance, etc.)
604 * @param atsi_count number of entries in atsi
607 handle_dv_route_message (void *cls, const struct GNUNET_PeerIdentity *peer,
608 const struct GNUNET_MessageHeader *message,
609 const struct GNUNET_ATS_Information *atsi,
610 unsigned int atsi_count)
612 GNUNET_break (0); // FIXME
618 * Service server's handler for message send requests (which come
619 * bubbling up to us through the DV plugin).
622 * @param client identification of the client
623 * @param message the actual message
626 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
627 const struct GNUNET_MessageHeader *message)
629 GNUNET_break (0); // FIXME
630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
635 * Multihashmap iterator for freeing routes that go via a particular
636 * neighbor that disconnected and is thus no longer available.
638 * @param cls the direct neighbor that is now unavailable
639 * @param key key value stored under
640 * @param value a 'struct Route' that may or may not go via neighbor
642 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
645 cull_routes (void *cls, const struct GNUNET_HashCode * key, void *value)
647 struct DirectNeighbor *neighbor = cls;
648 struct Route *route = value;
650 if (route->next_hop != neighbor)
651 return GNUNET_YES; /* not affected */
653 /* FIXME: destroy route! */
661 * Multihashmap iterator for freeing routes that go via a particular
662 * neighbor that disconnected and is thus no longer available.
664 * @param cls the direct neighbor that is now unavailable
665 * @param key key value stored under
666 * @param value a 'struct Route' that may or may not go via neighbor
668 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
671 cull_routing_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
673 struct DirectNeighbor *neighbor = cls;
674 struct RoutingNeighbor *rn = value;
676 if (rn->route.next_hop != neighbor)
677 return GNUNET_YES; /* not affected */
679 /* FIXME: destroy routing neighbor! */
687 * Method called whenever a given peer disconnects.
690 * @param peer peer identity this notification is about
693 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
695 struct DirectNeighbor *neighbor;
696 struct PendingMessage *pending;
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Received core peer disconnect message for peer `%s'!\n",
701 /* Check for disconnect from self message */
702 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
705 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
706 if (NULL == neighbor)
708 /* must have been a DV-neighbor, ignore */
711 while (NULL != (pending = neighbor->pm_head))
713 GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
716 GNUNET_free (pending);
718 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
721 GNUNET_CONTAINER_multihashmap_iterate (routing_neighbors,
722 &cull_routing_neighbors,
724 if (NULL != neighbor->cth)
726 GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
727 neighbor->cth = NULL;
729 GNUNET_assert (GNUNET_YES ==
730 GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
733 GNUNET_free (neighbor);
739 * Multihashmap iterator for freeing routes. Should never be called.
742 * @param key key value stored under
743 * @param value the route to be freed
745 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
748 free_route (void *cls, const struct GNUNET_HashCode * key, void *value)
751 // FIXME: notify client about disconnect
757 * Multihashmap iterator for freeing routing neighbors. Should never be called.
760 * @param key key value stored under
761 * @param value the distant neighbor to be freed
763 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
766 free_routing_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
768 struct RoutingNeighbor *router = value;
771 // FIXME: release resources
777 * Multihashmap iterator for freeing direct neighbors. Should never be called.
780 * @param key key value stored under
781 * @param value the direct neighbor to be freed
783 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
786 free_direct_neighbors (void *cls, const struct GNUNET_HashCode * key, void *value)
788 struct DirectNeighbor *dn = value;
791 // FIXME: release resources, ...
797 * Task run during shutdown.
803 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
805 struct PendingMessage *pending;
808 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
809 &free_direct_neighbors, NULL);
810 GNUNET_CONTAINER_multihashmap_destroy (direct_neighbors);
811 GNUNET_CONTAINER_multihashmap_iterate (routing_neighbors,
812 &free_routing_neighbors, NULL);
813 GNUNET_CONTAINER_multihashmap_destroy (routing_neighbors);
814 GNUNET_CONTAINER_multihashmap_iterate (all_routes,
816 GNUNET_CONTAINER_multihashmap_destroy (all_routes);
817 GNUNET_CORE_disconnect (core_api);
819 while (NULL != (pending = plugin_pending_head))
821 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
824 GNUNET_free (pending);
826 for (i=0;i<DEFAULT_FISHEYE_DEPTH - 1;i++)
827 GNUNET_array_grow (consensi[i].targets,
828 consensi[i].array_length,
834 * Handle START-message. This is the first message sent to us
835 * by the client (can only be one!).
837 * @param cls closure (always NULL)
838 * @param client identification of the client
839 * @param message the actual message
842 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
843 const struct GNUNET_MessageHeader *message)
845 if (NULL != client_handle)
847 /* forcefully drop old client */
848 GNUNET_SERVER_client_disconnect (client_handle);
849 GNUNET_SERVER_client_drop (client_handle);
851 client_handle = client;
852 GNUNET_SERVER_client_keep (client_handle);
853 GNUNET_SERVER_receive_done (client, GNUNET_OK);
858 * Called on core init.
861 * @param server legacy
862 * @param identity this peer's identity
865 core_init (void *cls, struct GNUNET_CORE_Handle *server,
866 const struct GNUNET_PeerIdentity *identity)
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 GNUNET_i2s (identity));
871 my_identity = *identity;
876 * Process dv requests.
879 * @param server the initialized server
880 * @param c configuration to use
883 run (void *cls, struct GNUNET_SERVER_Handle *server,
884 const struct GNUNET_CONFIGURATION_Handle *c)
886 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
887 {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
890 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
891 {&handle_start, NULL,
892 GNUNET_MESSAGE_TYPE_DV_START,
893 sizeof (struct GNUNET_MessageHeader) },
894 { &handle_dv_send_message, NULL,
895 GNUNET_MESSAGE_TYPE_DV_SEND,
901 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
902 routing_neighbors = GNUNET_CONTAINER_multihashmap_create (128 * 128, GNUNET_NO);
903 all_routes = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
904 core_api = GNUNET_CORE_connect (cfg, NULL,
906 &handle_core_connect,
907 &handle_core_disconnect,
912 if (NULL == core_api)
915 GNUNET_SERVER_add_handlers (server, plugin_handlers);
916 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
917 &shutdown_task, NULL);
922 * The main function for the dv service.
924 * @param argc number of arguments from the command line
925 * @param argv command line arguments
926 * @return 0 ok, 1 on error
929 main (int argc, char *const *argv)
932 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
933 &run, NULL)) ? 0 : 1;