2 This file is part of GNUnet.
3 (C) 2009 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_client_lib.h"
33 #include "gnunet_getopt_lib.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_service_lib.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_signal_lib.h"
39 #include "gnunet_util_lib.h"
40 #include "gnunet_hello_lib.h"
41 #include "gnunet_peerinfo_service.h"
42 #include "gnunet_crypto_lib.h"
43 #include "gnunet_statistics_service.h"
47 * For testing mostly, remember only the
48 * shortest path to a distant neighbor.
50 #define AT_MOST_ONE GNUNET_NO
52 #define USE_PEER_ID GNUNET_YES
55 * How many outstanding messages (unknown sender) will we allow per peer?
57 #define MAX_OUTSTANDING_MESSAGES 5
60 * How often do we check about sending out more peer information (if
61 * we are connected to no peers previously).
63 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
66 * How long do we wait at most between sending out information?
68 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
71 * How long can we have not heard from a peer and
72 * still have it in our tables?
74 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
77 * Priority for gossip.
79 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
82 * How often should we check if expiration time has elapsed for
85 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
88 * How long to allow a message to be delayed?
90 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
93 * Priority to use for DV data messages.
98 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
100 #define DIRECT_NEIGHBOR_COST 1
103 * The default number of direct connections to store in DV (max)
105 #define DEFAULT_DIRECT_CONNECTIONS 50
108 * The default size of direct + extended peers in DV (max)
110 #define DEFAULT_DV_SIZE 100
113 * The default fisheye depth, from how many hops away will
116 #define DEFAULT_FISHEYE_DEPTH 4
119 * Linked list of messages to send to clients.
121 struct PendingMessage
124 * Pointer to next item in the list
126 struct PendingMessage *next;
129 * Pointer to previous item in the list
131 struct PendingMessage *prev;
134 * The PeerIdentity to send to
136 struct GNUNET_PeerIdentity recipient;
139 * The result of message sending.
141 struct GNUNET_DV_SendResultMessage *send_result;
144 * Message importance level.
146 unsigned int importance;
151 unsigned int msg_size;
154 * How long to wait before sending message.
156 struct GNUNET_TIME_Relative timeout;
159 * Actual message to be sent; // avoid allocation
161 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
165 struct FastGossipNeighborList
168 * Next element of DLL
170 struct FastGossipNeighborList *next;
173 * Prev element of DLL
175 struct FastGossipNeighborList *prev;
178 * The neighbor to gossip about
180 struct DistantNeighbor *about;
184 * Context created whenever a direct peer connects to us,
185 * used to gossip other peers to it.
187 struct NeighborSendContext
190 * The peer we will gossip to.
192 struct DirectNeighbor *toNeighbor;
195 * The task associated with this context.
197 GNUNET_SCHEDULER_TaskIdentifier task;
200 * Head of DLL of peers to gossip about
201 * as fast as possible to this peer, for initial
204 struct FastGossipNeighborList *fast_gossip_list_head;
207 * Tail of DLL of peers to gossip about
208 * as fast as possible to this peer, for initial
211 struct FastGossipNeighborList *fast_gossip_list_tail;
217 * Struct to hold information for updating existing neighbors
219 struct NeighborUpdateInfo
227 * The existing neighbor
229 struct DistantNeighbor *neighbor;
232 * The referrer of the possibly existing peer
234 struct DirectNeighbor *referrer;
237 * The time we heard about this peer
239 struct GNUNET_TIME_Absolute now;
242 * Peer id this peer uses to refer to neighbor.
244 unsigned int referrer_peer_id;
249 * Struct to store a single message received with
252 struct UnknownSenderMessage
255 * Message sender (immediate)
257 struct GNUNET_PeerIdentity sender;
260 * The actual message received
262 struct GNUNET_MessageHeader *message;
265 * Latency of connection
267 struct GNUNET_TIME_Relative latency;
270 * Distance to destination
281 * Struct where actual neighbor information is stored,
282 * referenced by min_heap and max_heap. Freeing dealt
283 * with when items removed from hashmap.
285 struct DirectNeighbor
288 * Identity of neighbor.
290 struct GNUNET_PeerIdentity identity;
293 * PublicKey of neighbor.
295 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
298 * Head of DLL of nodes that this direct neighbor referred to us.
300 struct DistantNeighbor *referee_head;
303 * Tail of DLL of nodes that this direct neighbor referred to us.
305 struct DistantNeighbor *referee_tail;
308 * The sending context for gossiping peers to this neighbor.
310 struct NeighborSendContext *send_context;
313 * Is this one of the direct neighbors that we are "hiding"
319 * Save messages immediately from this direct neighbor from a
320 * distan peer we don't know on the chance that it will be
321 * gossiped about and we can deliver the message.
323 struct UnknownSenderMessage pending_messages[MAX_OUTSTANDING_MESSAGES];
328 * Struct where actual neighbor information is stored,
329 * referenced by min_heap and max_heap. Freeing dealt
330 * with when items removed from hashmap.
332 struct DistantNeighbor
335 * We keep distant neighbor's of the same referrer in a DLL.
337 struct DistantNeighbor *next;
340 * We keep distant neighbor's of the same referrer in a DLL.
342 struct DistantNeighbor *prev;
347 struct GNUNET_CONTAINER_HeapNode *min_loc;
352 struct GNUNET_CONTAINER_HeapNode *max_loc;
355 * Identity of referrer (next hop towards 'neighbor').
357 struct DirectNeighbor *referrer;
360 * Identity of neighbor.
362 struct GNUNET_PeerIdentity identity;
365 * PublicKey of neighbor.
367 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
370 * Last time we received routing information from this peer
372 struct GNUNET_TIME_Absolute last_activity;
375 * Last time we sent routing information about this peer
377 struct GNUNET_TIME_Absolute last_gossip;
380 * Cost to neighbor, used for actual distance vector computations
385 * Random identifier *we* use for this peer, to be used as shortcut
386 * instead of sending full peer id for each message
391 * Random identifier the *referrer* uses for this peer.
393 unsigned int referrer_id;
396 * Is this one of the direct neighbors that we are "hiding"
403 struct PeerIteratorContext
406 * The actual context, to be freed later.
408 struct GNUNET_PEERINFO_IteratorContext *ic;
411 * The neighbor about which we are concerned.
413 struct DirectNeighbor *neighbor;
416 * The distant neighbor entry for this direct neighbor.
418 struct DistantNeighbor *distant;
423 * Context used for creating hello messages when
424 * gossips are received.
429 * Identity of distant neighbor.
431 struct GNUNET_PeerIdentity distant_peer;
434 * Identity of direct neighbor, via which we send this message.
436 const struct GNUNET_PeerIdentity *direct_peer;
439 * How many addresses do we need to add (always starts at 1, then set to 0)
441 int addresses_to_add;
445 struct DV_SendContext
448 * The distant peer (should always match)
450 struct GNUNET_PeerIdentity *distant_peer;
453 * The direct peer, we need to verify the referrer of.
455 struct GNUNET_PeerIdentity *direct_peer;
458 * The message to be sent
460 struct GNUNET_MessageHeader *message;
463 * The pre-built send result message. Simply needs to be queued
464 * and freed once send has been called!
466 struct GNUNET_DV_SendResultMessage *send_result;
469 * The size of the message being sent, may be larger
470 * than message->header.size because it's multiple
471 * messages packed into one!
476 * How important is this message?
478 unsigned int importance;
481 * Timeout for this message
483 struct GNUNET_TIME_Relative timeout;
486 * Unique ID for DV message
491 struct FindDestinationContext
494 struct DistantNeighbor *dest;
500 struct GNUNET_PeerIdentity *dest;
501 const struct GNUNET_PeerIdentity *via;
504 struct DisconnectContext
507 * Distant neighbor to get pid from.
509 struct DistantNeighbor *distant;
512 * Direct neighbor that disconnected.
514 struct DirectNeighbor *direct;
517 struct TokenizedMessageContext
520 * Immediate sender of this message
522 const struct GNUNET_PeerIdentity *peer;
525 * Distant sender of the message
527 struct DistantNeighbor *distant;
530 * Uid for this set of messages
536 * Context for finding the least cost peer to send to.
537 * Transport selection can only go so far.
539 struct FindLeastCostContext
541 struct DistantNeighbor *target;
542 unsigned int least_cost;
546 * Handle to the core service api.
548 static struct GNUNET_CORE_Handle *coreAPI;
551 * Stream tokenizer to handle messages coming in from core.
553 static struct GNUNET_SERVER_MessageStreamTokenizer *coreMST;
556 * The identity of our peer.
558 static struct GNUNET_PeerIdentity my_identity;
561 * The configuration for this service.
563 static const struct GNUNET_CONFIGURATION_Handle *cfg;
567 * The client, the DV plugin connected to us. Hopefully
568 * this client will never change, although if the plugin dies
569 * and returns for some reason it may happen.
571 static struct GNUNET_SERVER_Client *client_handle;
574 * Task to run when we shut down, cleaning up all our trash
576 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
578 static size_t default_dv_priority = 0;
580 static char *my_short_id;
583 * Transmit handle to the plugin.
585 static struct GNUNET_CONNECTION_TransmitHandle *plugin_transmit_handle;
588 * Head of DLL for client messages
590 static struct PendingMessage *plugin_pending_head;
593 * Tail of DLL for client messages
595 static struct PendingMessage *plugin_pending_tail;
598 * Handle to the peerinfo service
600 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
603 * Transmit handle to core service.
605 static struct GNUNET_CORE_TransmitHandle *core_transmit_handle;
608 * Head of DLL for core messages
610 static struct PendingMessage *core_pending_head;
613 * Tail of DLL for core messages
615 static struct PendingMessage *core_pending_tail;
618 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
619 * directly connected peers.
621 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
624 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
625 * peers connected via DV (extended neighborhood). Does ALSO
626 * include any peers that are in 'direct_neighbors'; for those
627 * peers, the cost will be zero and the referrer all zeros.
629 static struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
632 * We use the min heap (min refers to cost) to prefer
633 * gossipping about peers with small costs.
635 static struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
638 * We use the max heap (max refers to cost) for general
639 * iterations over all peers and to remove the most costly
640 * connection if we have too many.
642 static struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
645 * Handle for the statistics service.
647 struct GNUNET_STATISTICS_Handle *stats;
650 * How far out to keep peers we learn about.
652 static unsigned long long fisheye_depth;
655 * How many peers to store at most.
657 static unsigned long long max_table_size;
660 * We've been given a target ID based on the random numbers that
661 * we assigned to our DV-neighborhood. Find the entry for the
662 * respective neighbor.
665 find_destination (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
666 void *element, GNUNET_CONTAINER_HeapCostType cost)
668 struct FindDestinationContext *fdc = cls;
669 struct DistantNeighbor *dn = element;
671 if (fdc->tid != dn->our_id)
679 * We've been given a target ID based on the random numbers that
680 * we assigned to our DV-neighborhood. Find the entry for the
681 * respective neighbor.
684 find_specific_id (void *cls, const GNUNET_HashCode * key, void *value)
686 struct FindIDContext *fdc = cls;
687 struct DistantNeighbor *dn = value;
690 (&dn->referrer->identity, fdc->via,
691 sizeof (struct GNUNET_PeerIdentity)) == 0)
693 fdc->tid = dn->referrer_id;
700 * Find a distant peer whose referrer_id matches what we're
701 * looking for. For looking up a peer we've gossipped about
702 * but is now disconnected. Need to do this because we don't
703 * want to remove those that may be accessible via a different
707 find_distant_peer (void *cls, const GNUNET_HashCode * key, void *value)
709 struct FindDestinationContext *fdc = cls;
710 struct DistantNeighbor *distant = value;
712 if (fdc->tid == distant->referrer_id)
721 * Function called to notify a client about the socket
722 * begin ready to queue more data. "buf" will be
723 * NULL and "size" zero if the socket was closed for
724 * writing in the meantime.
727 * @param size number of bytes available in buf
728 * @param buf where the callee should write the message
729 * @return number of bytes written to buf
732 transmit_to_plugin (void *cls, size_t size, void *buf)
735 struct PendingMessage *reply;
741 /* client disconnected */
742 #if DEBUG_DV_MESSAGES
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "%s: %s buffer was NULL (client disconnect?)\n", my_short_id,
745 "transmit_to_plugin");
749 plugin_transmit_handle = NULL;
751 while ((NULL != (reply = plugin_pending_head)) &&
752 (size >= off + (msize = ntohs (reply->msg->size))))
754 GNUNET_CONTAINER_DLL_remove (plugin_pending_head, plugin_pending_tail,
756 memcpy (&cbuf[off], reply->msg, msize);
761 if (plugin_pending_head != NULL)
762 plugin_transmit_handle =
763 GNUNET_SERVER_notify_transmit_ready (client_handle,
764 ntohs (plugin_pending_head->msg->
766 GNUNET_TIME_UNIT_FOREVER_REL,
767 &transmit_to_plugin, NULL);
773 * Send a message to the dv plugin.
775 * @param sender the direct sender of the message
776 * @param message the message to send to the plugin
777 * (may be an encapsulated type)
778 * @param message_size the size of the message to be sent
779 * @param distant_neighbor the original sender of the message
780 * @param cost the cost to the original sender of the message
783 send_to_plugin (const struct GNUNET_PeerIdentity *sender,
784 const struct GNUNET_MessageHeader *message, size_t message_size,
785 struct GNUNET_PeerIdentity *distant_neighbor, size_t cost)
787 struct GNUNET_DV_MessageReceived *received_msg;
788 struct PendingMessage *pending_message;
789 char *sender_address;
790 size_t sender_address_len;
791 char *packed_msg_start;
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "send_to_plugin called with peer %s as sender\n",
797 GNUNET_i2s (distant_neighbor));
800 if (memcmp (sender, distant_neighbor, sizeof (struct GNUNET_PeerIdentity)) !=
803 sender_address_len = sizeof (struct GNUNET_PeerIdentity) * 2;
804 sender_address = GNUNET_malloc (sender_address_len);
805 memcpy (sender_address, distant_neighbor,
806 sizeof (struct GNUNET_PeerIdentity));
807 memcpy (&sender_address[sizeof (struct GNUNET_PeerIdentity)], sender,
808 sizeof (struct GNUNET_PeerIdentity));
812 sender_address_len = sizeof (struct GNUNET_PeerIdentity);
813 sender_address = GNUNET_malloc (sender_address_len);
814 memcpy (sender_address, sender, sizeof (struct GNUNET_PeerIdentity));
818 sizeof (struct GNUNET_DV_MessageReceived) + sender_address_len +
820 received_msg = GNUNET_malloc (size);
821 received_msg->header.size = htons (size);
822 received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
823 received_msg->distance = htonl (cost);
824 received_msg->msg_len = htonl (message_size);
825 /* Set the sender in this message to be the original sender! */
826 memcpy (&received_msg->sender, distant_neighbor,
827 sizeof (struct GNUNET_PeerIdentity));
828 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
829 memcpy (&received_msg[1], sender_address, sender_address_len);
830 GNUNET_free (sender_address);
831 /* Copy the actual message after the sender */
832 packed_msg_start = (char *) &received_msg[1];
833 packed_msg_start = &packed_msg_start[sender_address_len];
834 memcpy (packed_msg_start, message, message_size);
835 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + size);
836 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
837 memcpy (&pending_message[1], received_msg, size);
838 GNUNET_free (received_msg);
840 GNUNET_CONTAINER_DLL_insert_after (plugin_pending_head, plugin_pending_tail,
841 plugin_pending_tail, pending_message);
843 if (client_handle != NULL)
845 if (plugin_transmit_handle == NULL)
847 plugin_transmit_handle =
848 GNUNET_SERVER_notify_transmit_ready (client_handle, size,
849 GNUNET_TIME_UNIT_FOREVER_REL,
850 &transmit_to_plugin, NULL);
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
860 /* Declare here so retry_core_send is aware of it */
862 core_transmit_notify (void *cls, size_t size, void *buf);
865 * Try to send another message from our core sending list
868 try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
870 struct PendingMessage *pending;
872 pending = core_pending_head;
874 if (core_transmit_handle != NULL)
875 return; /* Message send already in progress */
877 if ((pending != NULL) && (coreAPI != NULL))
878 core_transmit_handle =
879 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
884 &core_transmit_notify, NULL);
889 * Function called to notify a client about the socket
890 * being ready to queue more data. "buf" will be
891 * NULL and "size" zero if the socket was closed for
892 * writing in the meantime.
894 * @param cls closure (NULL)
895 * @param size number of bytes available in buf
896 * @param buf where the callee should write the message
897 * @return number of bytes written to buf
900 core_transmit_notify (void *cls, size_t size, void *buf)
903 struct PendingMessage *pending;
904 struct PendingMessage *client_reply;
910 /* client disconnected */
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
917 core_transmit_handle = NULL;
919 pending = core_pending_head;
920 if ((pending != NULL) && (size >= (msize = ntohs (pending->msg->size))))
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "`%s' : transmit_notify (core) called with size %d\n",
925 "dv service", msize);
927 GNUNET_CONTAINER_DLL_remove (core_pending_head, core_pending_tail, pending);
928 if (pending->send_result != NULL) /* Will only be non-null if a real client asked for this send */
931 GNUNET_malloc (sizeof (struct PendingMessage) +
932 sizeof (struct GNUNET_DV_SendResultMessage));
933 client_reply->msg = (struct GNUNET_MessageHeader *) &client_reply[1];
934 memcpy (&client_reply[1], pending->send_result,
935 sizeof (struct GNUNET_DV_SendResultMessage));
936 GNUNET_free (pending->send_result);
938 GNUNET_CONTAINER_DLL_insert_after (plugin_pending_head,
940 plugin_pending_tail, client_reply);
941 if (client_handle != NULL)
943 if (plugin_transmit_handle == NULL)
945 plugin_transmit_handle =
946 GNUNET_SERVER_notify_transmit_ready (client_handle,
948 GNUNET_DV_SendResultMessage),
949 GNUNET_TIME_UNIT_FOREVER_REL,
950 &transmit_to_plugin, NULL);
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Failed to queue message for plugin, must be one in progress already!!\n");
959 memcpy (&cbuf[off], pending->msg, msize);
960 GNUNET_free (pending);
963 /*reply = core_pending_head; */
965 GNUNET_SCHEDULER_add_now (&try_core_send, NULL);
967 * core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, GNUNET_YES, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL); */
974 * Send a DV data message via DV.
976 * @param sender the original sender of the message
977 * @param recipient the next hop recipient, may be our direct peer, maybe not
978 * @param send_context the send context
981 send_message_via (const struct GNUNET_PeerIdentity *sender,
982 const struct GNUNET_PeerIdentity *recipient,
983 struct DV_SendContext *send_context)
985 p2p_dv_MESSAGE_Data *toSend;
986 unsigned int msg_size;
987 unsigned int recipient_id;
988 unsigned int sender_id;
989 struct DistantNeighbor *source;
990 struct PendingMessage *pending_message;
991 struct FindIDContext find_context;
997 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
999 find_context.dest = send_context->distant_peer;
1000 find_context.via = recipient;
1001 find_context.tid = 0;
1002 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1004 distant_peer->hashPubKey,
1005 &find_specific_id, &find_context);
1007 if (find_context.tid == 0)
1009 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1010 "%s: find_specific_id failed to find peer!\n", my_short_id);
1011 /* target unknown to us, drop! */
1012 return GNUNET_SYSERR;
1014 recipient_id = find_context.tid;
1016 if (0 == (memcmp (&my_identity, sender, sizeof (struct GNUNET_PeerIdentity))))
1020 GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1021 &sender->hashPubKey);
1023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1024 "%s: send_message_via found %s, myself in extended peer list???\n",
1025 my_short_id, GNUNET_i2s (&source->identity));
1030 GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1031 &sender->hashPubKey);
1034 /* sender unknown to us, drop! */
1035 return GNUNET_SYSERR;
1037 sender_id = source->our_id;
1040 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1041 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1042 pending_message->send_result = send_context->send_result;
1043 memcpy (&pending_message->recipient, recipient,
1044 sizeof (struct GNUNET_PeerIdentity));
1045 pending_message->msg_size = msg_size;
1046 pending_message->importance = send_context->importance;
1047 pending_message->timeout = send_context->timeout;
1048 toSend = (p2p_dv_MESSAGE_Data *) pending_message->msg;
1049 toSend->header.size = htons (msg_size);
1050 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1051 toSend->sender = htonl (sender_id);
1052 toSend->recipient = htonl (recipient_id);
1053 #if DEBUG_DV_MESSAGES
1054 toSend->uid = send_context->uid; /* Still sent around in network byte order */
1056 toSend->uid = htonl (0);
1059 memcpy (&toSend[1], send_context->message, send_context->message_size);
1062 memcpy (&shortname, GNUNET_i2s (send_context->distant_peer), 4);
1063 shortname[4] = '\0';
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "%s: Notifying core of send to destination `%s' via `%s' size %u\n",
1066 "DV", &shortname, GNUNET_i2s (recipient), msg_size);
1069 GNUNET_CONTAINER_DLL_insert_after (core_pending_head, core_pending_tail,
1070 core_pending_tail, pending_message);
1072 GNUNET_SCHEDULER_add_now (try_core_send, NULL);
1078 * Given a FindLeastCostContext, and a set
1079 * of peers that match the target, return the cheapest.
1081 * @param cls closure, a struct FindLeastCostContext
1082 * @param key the key identifying the target peer
1083 * @param value the target peer
1085 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1088 find_least_cost_peer (void *cls, const GNUNET_HashCode * key, void *value)
1090 struct FindLeastCostContext *find_context = cls;
1091 struct DistantNeighbor *dn = value;
1093 if (dn->cost < find_context->least_cost)
1095 find_context->target = dn;
1097 if (dn->cost == DIRECT_NEIGHBOR_COST)
1103 * Send a DV data message via DV.
1105 * @param recipient the ultimate recipient of this message
1106 * @param sender the original sender of the message
1107 * @param specific_neighbor the specific neighbor to send this message via
1108 * @param message the packed message
1109 * @param message_size size of the message
1110 * @param importance what priority to send this message with
1111 * @param uid the unique identifier of this message (or 0 for none)
1112 * @param timeout how long to possibly delay sending this message
1115 send_message (const struct GNUNET_PeerIdentity *recipient,
1116 const struct GNUNET_PeerIdentity *sender,
1117 const struct DistantNeighbor *specific_neighbor,
1118 const struct GNUNET_MessageHeader *message, size_t message_size,
1119 unsigned int importance, unsigned int uid,
1120 struct GNUNET_TIME_Relative timeout)
1122 p2p_dv_MESSAGE_Data *toSend;
1123 unsigned int msg_size;
1125 unsigned int recipient_id;
1126 unsigned int sender_id;
1127 struct DistantNeighbor *target;
1128 struct DistantNeighbor *source;
1129 struct PendingMessage *pending_message;
1130 struct FindLeastCostContext find_least_ctx;
1132 #if DEBUG_DV_PEER_NUMBERS
1133 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1134 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1135 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1137 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1139 find_least_ctx.least_cost = -1;
1140 find_least_ctx.target = NULL;
1142 * Need to find the least cost peer, lest the transport selection keep
1143 * picking the same DV route for the same destination which results
1144 * in messages looping forever. Relatively cheap, we don't iterate
1145 * over all known peers, just those that apply.
1147 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1148 &recipient->hashPubKey,
1149 &find_least_cost_peer,
1151 target = find_least_ctx.target;
1155 /* target unknown to us, drop! */
1156 return GNUNET_SYSERR;
1158 recipient_id = target->referrer_id;
1161 GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1162 &sender->hashPubKey);
1166 (memcmp (&my_identity, sender, sizeof (struct GNUNET_PeerIdentity))))
1168 /* sender unknown to us, drop! */
1169 return GNUNET_SYSERR;
1171 sender_id = 0; /* 0 == us */
1175 /* find out the number that we use when we gossip about
1177 sender_id = source->our_id;
1180 #if DEBUG_DV_PEER_NUMBERS
1181 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1182 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey,
1184 encPeerFrom.encoding[4] = '\0';
1185 encPeerVia.encoding[4] = '\0';
1187 if ((sender_id != 0) &&
1189 memcmp (&source->identity, &target->referrer->identity,
1190 sizeof (struct GNUNET_PeerIdentity))))
1195 cost = target->cost;
1196 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1197 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1198 pending_message->send_result = NULL;
1199 pending_message->importance = importance;
1200 pending_message->timeout = timeout;
1201 memcpy (&pending_message->recipient, &target->referrer->identity,
1202 sizeof (struct GNUNET_PeerIdentity));
1203 pending_message->msg_size = msg_size;
1204 toSend = (p2p_dv_MESSAGE_Data *) pending_message->msg;
1205 toSend->header.size = htons (msg_size);
1206 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1207 toSend->sender = htonl (sender_id);
1208 toSend->recipient = htonl (recipient_id);
1209 #if DEBUG_DV_MESSAGES
1210 toSend->uid = htonl (uid);
1212 toSend->uid = htonl (0);
1215 #if DEBUG_DV_PEER_NUMBERS
1216 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1217 encPeerTo.encoding[4] = '\0';
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "%s: Sending DATA message. Sender id %u, source %s, destination %s, via %s\n",
1220 GNUNET_i2s (&my_identity), sender_id, &encPeerFrom, &encPeerTo,
1223 memcpy (&toSend[1], message, message_size);
1224 if ((source != NULL) && (source->pkey == NULL)) /* Test our hypothesis about message failures! */
1226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1227 "%s: Sending message, but anticipate recipient will not know sender!!!\n\n\n",
1230 GNUNET_CONTAINER_DLL_insert_after (core_pending_head, core_pending_tail,
1231 core_pending_tail, pending_message);
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "%s: Notifying core of send size %d to destination `%s'\n",
1235 "DV SEND MESSAGE", msg_size, GNUNET_i2s (recipient));
1238 GNUNET_SCHEDULER_add_now (try_core_send, NULL);
1243 struct CheckPeerContext
1248 struct DistantNeighbor *peer;
1251 * Sender id to search for
1253 unsigned int sender_id;
1257 * Iterator over hash map entries.
1259 * @param cls closure
1260 * @param key current key code
1261 * @param value value in the hash map
1262 * @return GNUNET_YES if we should continue to
1267 checkPeerID (void *cls, const GNUNET_HashCode * key, void *value)
1269 struct CheckPeerContext *ctx = cls;
1270 struct DistantNeighbor *distant = value;
1272 if (memcmp (key, &ctx->sender_id, sizeof (unsigned int)) == 0)
1274 ctx->peer = distant;
1284 * Handler for messages parsed out by the tokenizer from
1285 * DV DATA received for this peer.
1288 * @param client the TokenizedMessageContext which contains message information
1289 * @param message the actual message
1292 tokenized_message_handler (void *cls, void *client,
1293 const struct GNUNET_MessageHeader *message)
1295 struct TokenizedMessageContext *ctx = client;
1297 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1298 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1299 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1300 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA))
1302 #if DEBUG_DV_MESSAGES
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 "%s: Receives %s message for me, uid %u, size %d, type %d cost %u from %s!\n",
1305 my_short_id, "DV DATA", ctx->uid, ntohs (message->size),
1306 ntohs (message->type), ctx->distant->cost,
1307 GNUNET_i2s (&ctx->distant->identity));
1309 GNUNET_assert (memcmp
1310 (ctx->peer, &ctx->distant->identity,
1311 sizeof (struct GNUNET_PeerIdentity)) != 0);
1312 send_to_plugin (ctx->peer, message, ntohs (message->size),
1313 &ctx->distant->identity, ctx->distant->cost);
1318 struct DelayedMessageContext
1320 struct GNUNET_PeerIdentity dest;
1321 struct GNUNET_PeerIdentity sender;
1322 struct GNUNET_MessageHeader *message;
1323 size_t message_size;
1328 send_message_delayed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1330 struct DelayedMessageContext *msg_ctx = cls;
1332 if (msg_ctx != NULL)
1334 send_message (&msg_ctx->dest, &msg_ctx->sender, NULL, msg_ctx->message,
1335 msg_ctx->message_size, default_dv_priority, msg_ctx->uid,
1336 GNUNET_TIME_relative_get_forever ());
1337 GNUNET_free (msg_ctx->message);
1338 GNUNET_free (msg_ctx);
1344 * Get distance information from 'atsi'.
1346 * @param atsi performance data
1347 * @return connected transport distance
1350 get_atsi_distance (const struct GNUNET_ATS_Information *atsi)
1352 while ((ntohl (atsi->type) != GNUNET_ATS_ARRAY_TERMINATOR) &&
1353 (ntohl (atsi->type) != GNUNET_ATS_QUALITY_NET_DISTANCE))
1355 if (ntohl (atsi->type) == GNUNET_ATS_ARRAY_TERMINATOR)
1358 /* FIXME: we do not have distance data? Assume direct neighbor. */
1359 return DIRECT_NEIGHBOR_COST;
1361 return ntohl (atsi->value);
1365 * Find latency information in 'atsi'.
1367 * @param atsi performance data
1368 * @return connection latency
1370 static struct GNUNET_TIME_Relative
1371 get_atsi_latency (const struct GNUNET_ATS_Information *atsi)
1373 while ((ntohl (atsi->type) != GNUNET_ATS_ARRAY_TERMINATOR) &&
1374 (ntohl (atsi->type) != GNUNET_ATS_QUALITY_NET_DELAY))
1376 if (ntohl (atsi->type) == GNUNET_ATS_ARRAY_TERMINATOR)
1379 /* how can we not have latency data? */
1380 return GNUNET_TIME_UNIT_SECONDS;
1382 /* FIXME: Multiply by GNUNET_TIME_UNIT_MILLISECONDS (1) to get as a GNUNET_TIME_Relative */
1383 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1384 ntohl (atsi->value));
1388 * Core handler for dv data messages. Whatever this message
1389 * contains all we really have to do is rip it out of its
1390 * DV layering and give it to our pal the DV plugin to report
1393 * @param cls closure
1394 * @param peer peer which sent the message (immediate sender)
1395 * @param message the message
1396 * @param atsi transport ATS information (latency, distance, etc.)
1399 handle_dv_data_message (void *cls, const struct GNUNET_PeerIdentity *peer,
1400 const struct GNUNET_MessageHeader *message,
1401 const struct GNUNET_ATS_Information *atsi)
1403 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1404 const struct GNUNET_MessageHeader *packed_message;
1405 struct DirectNeighbor *dn;
1406 struct DistantNeighbor *pos;
1407 unsigned int sid; /* Sender id */
1408 unsigned int tid; /* Target id */
1409 struct GNUNET_PeerIdentity *original_sender;
1410 struct GNUNET_PeerIdentity *destination;
1411 struct FindDestinationContext fdc;
1412 struct TokenizedMessageContext tkm_ctx;
1417 struct DelayedMessageContext *delayed_context;
1420 struct CheckPeerContext checkPeerCtx;
1422 #if DEBUG_DV_MESSAGES
1426 size_t packed_message_size;
1428 uint32_t distance; /* Distance information */
1429 struct GNUNET_TIME_Relative latency; /* Latency information */
1431 packed_message_size =
1432 ntohs (incoming->header.size) - sizeof (p2p_dv_MESSAGE_Data);
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "%s: Receives DATA message from %s size %d, packed size %d!\n",
1436 my_short_id, GNUNET_i2s (peer), ntohs (incoming->header.size),
1437 packed_message_size);
1440 if (ntohs (incoming->header.size) <
1441 sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 "`%s': Message sizes don't add up, total size %u, expected at least %u!\n",
1446 "dv service", ntohs (incoming->header.size),
1447 sizeof (p2p_dv_MESSAGE_Data) +
1448 sizeof (struct GNUNET_MessageHeader));
1450 return GNUNET_SYSERR;
1453 /* Iterate over ATS_Information to get distance and latency */
1454 latency = get_atsi_latency (atsi);
1455 distance = get_atsi_distance (atsi);
1456 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
1460 sid = ntohl (incoming->sender);
1464 checkPeerCtx.sender_id = sid;
1465 checkPeerCtx.peer = NULL;
1466 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &checkPeerID,
1468 pos = checkPeerCtx.peer;
1473 GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1477 pos = dn->referee_head;
1478 while ((NULL != pos) && (pos->referrer_id != sid))
1484 #if DEBUG_DV_MESSAGES
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 "%s: unknown sender (%u), Message uid %u from %s!\n",
1487 my_short_id, ntohl (incoming->sender), ntohl (incoming->uid),
1488 GNUNET_i2s (&dn->identity));
1489 pos = dn->referee_head;
1490 while ((NULL != pos) && (pos->referrer_id != sid))
1492 sender_id = GNUNET_strdup (GNUNET_i2s (&pos->identity));
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n",
1494 pos->referrer_id, sender_id);
1495 GNUNET_free (sender_id);
1501 for (i = 0; i < MAX_OUTSTANDING_MESSAGES; i++)
1503 if (dn->pending_messages[i].sender_id == 0)
1510 if (found_pos == -1)
1512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1513 "%s: Too many unknown senders (%u), ignoring message! Message uid %llu from %s!\n",
1514 my_short_id, ntohl (incoming->sender), ntohl (incoming->uid),
1515 GNUNET_i2s (&dn->identity));
1519 dn->pending_messages[found_pos].message =
1520 GNUNET_malloc (ntohs (message->size));
1521 memcpy (dn->pending_messages[found_pos].message, message,
1522 ntohs (message->size));
1523 dn->pending_messages[found_pos].distance = distance;
1524 dn->pending_messages[found_pos].latency = latency;
1525 memcpy (&dn->pending_messages[found_pos].sender, peer,
1526 sizeof (struct GNUNET_PeerIdentity));
1527 dn->pending_messages[found_pos].sender_id = sid;
1529 /* unknown sender */
1532 original_sender = &pos->identity;
1533 tid = ntohl (incoming->recipient);
1537 cbuf = (char *) &incoming[1];
1539 tkm_ctx.peer = peer;
1540 tkm_ctx.distant = pos;
1541 tkm_ctx.uid = ntohl (incoming->uid);
1543 GNUNET_SERVER_mst_receive (coreMST, &tkm_ctx, cbuf, packed_message_size,
1544 GNUNET_NO, GNUNET_NO))
1546 GNUNET_break_op (0);
1547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1548 "%s: %s Received corrupt data, discarding!", my_short_id,
1555 packed_message = (struct GNUNET_MessageHeader *) &incoming[1];
1558 /* FIXME: this is the *only* per-request operation we have in DV
1559 * that is O(n) in relation to the number of connected peers; a
1560 * hash-table lookup could easily solve this (minor performance
1564 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap, &find_destination, &fdc);
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1571 if (fdc.dest == NULL)
1573 #if DEBUG_DV_MESSAGES
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n",
1576 my_short_id, "DV DATA", ntohl (incoming->uid), tid);
1580 destination = &fdc.dest->identity;
1582 if (0 == memcmp (destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1584 /* FIXME: create stat: routing loop-discard! */
1586 #if DEBUG_DV_MESSAGES
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "%s: DROPPING MESSAGE uid %u type %d, routing loop! Message immediately from %s!\n",
1589 my_short_id, ntohl (incoming->uid),
1590 ntohs (packed_message->type), GNUNET_i2s (&dn->identity));
1595 /* At this point we have a message, and we need to forward it on to the
1598 #if DEBUG_DV_MESSAGES
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "%s: FORWARD %s message for %s, uid %u, size %d type %d, cost %u!\n",
1601 my_short_id, "DV DATA", GNUNET_i2s (destination),
1602 ntohl (incoming->uid), ntohs (packed_message->size),
1603 ntohs (packed_message->type), pos->cost);
1607 if (GNUNET_TIME_absolute_get_duration (pos->last_gossip).abs_value <
1608 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2).abs_value)
1610 delayed_context = GNUNET_malloc (sizeof (struct DelayedMessageContext));
1611 memcpy (&delayed_context->dest, destination,
1612 sizeof (struct GNUNET_PeerIdentity));
1613 memcpy (&delayed_context->sender, original_sender,
1614 sizeof (struct GNUNET_PeerIdentity));
1615 delayed_context->message = GNUNET_malloc (packed_message_size);
1616 memcpy (delayed_context->message, packed_message, packed_message_size);
1617 delayed_context->message_size = packed_message_size;
1618 delayed_context->uid = ntohl (incoming->uid);
1619 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1620 (GNUNET_TIME_UNIT_MILLISECONDS, 2500),
1621 &send_message_delayed, delayed_context);
1628 send_message (destination, original_sender, NULL, packed_message,
1629 packed_message_size, default_dv_priority,
1630 ntohl (incoming->uid),
1631 GNUNET_TIME_relative_get_forever ());
1633 if (ret != GNUNET_SYSERR)
1637 #if DEBUG_MESSAGE_DROP
1638 char *direct_id = GNUNET_strdup (GNUNET_i2s (&dn->identity));
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1641 "%s: DROPPING MESSAGE type %d, forwarding failed! Message immediately from %s!\n",
1642 GNUNET_i2s (&my_identity),
1643 ntohs (((struct GNUNET_MessageHeader *) &incoming[1])->type),
1645 GNUNET_free (direct_id);
1647 return GNUNET_SYSERR;
1653 * Iterator over hash map entries.
1655 * @param cls closure (NULL)
1656 * @param key current key code
1657 * @param value value in the hash map (DistantNeighbor)
1658 * @return GNUNET_YES if we should continue to
1663 print_neighbors (void *cls, const GNUNET_HashCode * key, void *abs_value)
1665 struct DistantNeighbor *distant_neighbor = abs_value;
1666 char my_shortname[5];
1667 char referrer_shortname[5];
1669 memcpy (&my_shortname, GNUNET_i2s (&my_identity), 4);
1670 my_shortname[4] = '\0';
1671 memcpy (&referrer_shortname,
1672 GNUNET_i2s (&distant_neighbor->referrer->identity), 4);
1673 referrer_shortname[4] = '\0';
1675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1676 "`%s' %s: Peer `%s', distance %d, referrer `%s' pkey: %s\n",
1677 &my_shortname, "DV", GNUNET_i2s (&distant_neighbor->identity),
1678 distant_neighbor->cost, &referrer_shortname,
1679 distant_neighbor->pkey == NULL ? "no" : "yes");
1685 * Scheduled task which gossips about known direct peers to other connected
1686 * peers. Will run until called with reason shutdown.
1689 neighbor_send_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1691 struct NeighborSendContext *send_context = cls;
1693 #if DEBUG_DV_GOSSIP_SEND
1697 struct DistantNeighbor *about;
1698 struct DirectNeighbor *to;
1699 struct FastGossipNeighborList *about_list;
1701 p2p_dv_MESSAGE_NeighborInfo *message;
1702 struct PendingMessage *pending_message;
1704 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "%s: Called with reason shutdown, shutting down!\n",
1709 GNUNET_i2s (&my_identity));
1714 if (send_context->fast_gossip_list_head != NULL)
1716 about_list = send_context->fast_gossip_list_head;
1717 about = about_list->about;
1718 GNUNET_CONTAINER_DLL_remove (send_context->fast_gossip_list_head,
1719 send_context->fast_gossip_list_tail,
1721 GNUNET_free (about_list);
1725 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1726 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1727 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1728 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1729 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1730 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1731 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1732 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1733 * asynchronously enough to avoid major problems (-;
1735 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1737 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1739 to = send_context->toNeighbor;
1741 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1743 (about->hidden == GNUNET_NO) &&
1747 memcmp (&about->identity, &to->identity,
1748 sizeof (struct GNUNET_PeerIdentity))) && (about->pkey != NULL))
1750 #if DEBUG_DV_GOSSIP_SEND
1751 encPeerAbout = GNUNET_strdup (GNUNET_i2s (&about->identity));
1752 encPeerTo = GNUNET_strdup (GNUNET_i2s (&to->identity));
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1755 GNUNET_i2s (&my_identity), encPeerAbout, about->our_id,
1757 GNUNET_free (encPeerAbout);
1758 GNUNET_free (encPeerTo);
1760 about->last_gossip = GNUNET_TIME_absolute_get ();
1762 GNUNET_malloc (sizeof (struct PendingMessage) +
1763 sizeof (p2p_dv_MESSAGE_NeighborInfo));
1764 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1765 pending_message->importance = default_dv_priority;
1766 pending_message->timeout = GNUNET_TIME_relative_get_forever ();
1767 memcpy (&pending_message->recipient, &to->identity,
1768 sizeof (struct GNUNET_PeerIdentity));
1769 pending_message->msg_size = sizeof (p2p_dv_MESSAGE_NeighborInfo);
1770 message = (p2p_dv_MESSAGE_NeighborInfo *) pending_message->msg;
1771 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1772 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1773 message->cost = htonl (about->cost);
1774 message->neighbor_id = htonl (about->our_id);
1776 memcpy (&message->pkey, about->pkey,
1777 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1778 memcpy (&message->neighbor, &about->identity,
1779 sizeof (struct GNUNET_PeerIdentity));
1781 GNUNET_CONTAINER_DLL_insert_after (core_pending_head, core_pending_tail,
1782 core_pending_tail, pending_message);
1784 GNUNET_SCHEDULER_add_now (try_core_send, NULL);
1785 /*if (core_transmit_handle == NULL)
1786 * core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, GNUNET_YES, default_dv_priority, GNUNET_TIME_relative_get_forever(), &to->identity, sizeof(p2p_dv_MESSAGE_NeighborInfo), &core_transmit_notify, NULL); */
1790 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1792 #if DEBUG_DV_PEER_NUMBERS
1793 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1794 "DV SERVICE: still in fast send mode\n");
1796 send_context->task =
1797 GNUNET_SCHEDULER_add_now (&neighbor_send_task, send_context);
1801 #if DEBUG_DV_PEER_NUMBERS
1802 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1803 "DV SERVICE: entering slow send mode\n");
1805 send_context->task =
1806 GNUNET_SCHEDULER_add_delayed (GNUNET_DV_DEFAULT_SEND_INTERVAL,
1807 &neighbor_send_task, send_context);
1815 * Handle START-message. This is the first message sent to us
1816 * by the client (can only be one!).
1818 * @param cls closure (always NULL)
1819 * @param client identification of the client
1820 * @param message the actual message
1823 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
1824 const struct GNUNET_MessageHeader *message)
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' request from client\n",
1832 client_handle = client;
1834 GNUNET_SERVER_client_keep (client_handle);
1835 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1840 * Iterate over hash map entries for a distant neighbor,
1841 * if direct neighbor matches context call send message
1843 * @param cls closure, a DV_SendContext
1844 * @param key current key code
1845 * @param value value in the hash map
1846 * @return GNUNET_YES if we should continue to
1851 send_iterator (void *cls, const GNUNET_HashCode * key, void *abs_value)
1853 struct DV_SendContext *send_context = cls;
1854 struct DistantNeighbor *distant_neighbor = abs_value;
1856 if (memcmp (distant_neighbor->referrer, send_context->direct_peer, sizeof (struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1858 send_message_via (&my_identity, distant_neighbor, send_context);
1866 * Service server's handler for message send requests (which come
1867 * bubbling up to us through the DV plugin).
1869 * @param cls closure
1870 * @param client identification of the client
1871 * @param message the actual message
1874 handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
1875 const struct GNUNET_MessageHeader *message)
1877 struct GNUNET_DV_SendMessage *send_msg;
1878 struct GNUNET_DV_SendResultMessage *send_result_msg;
1879 struct PendingMessage *pending_message;
1881 size_t message_size;
1882 struct GNUNET_PeerIdentity *destination;
1883 struct GNUNET_PeerIdentity *direct;
1884 struct GNUNET_MessageHeader *message_buf;
1887 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1888 struct DV_SendContext *send_context;
1890 #if DEBUG_DV_MESSAGES
1892 struct GNUNET_MessageHeader *packed_message;
1895 if (client_handle == NULL)
1897 client_handle = client;
1898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1899 "%s: Setting initial client handle, never received `%s' message?\n",
1902 else if (client_handle != client)
1904 client_handle = client;
1905 /* What should we do in this case, assert fail or just log the warning? */
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "%s: Setting client handle (was a different client!)!\n", "dv");
1912 GNUNET_assert (ntohs (message->size) > sizeof (struct GNUNET_DV_SendMessage));
1913 send_msg = (struct GNUNET_DV_SendMessage *) message;
1915 address_len = ntohl (send_msg->addrlen);
1916 GNUNET_assert (address_len == sizeof (struct GNUNET_PeerIdentity) * 2);
1918 ntohs (message->size) - sizeof (struct GNUNET_DV_SendMessage) -
1920 destination = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1921 direct = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1922 message_buf = GNUNET_malloc (message_size);
1924 temp_pos = (char *) &send_msg[1]; /* Set pointer to end of message */
1925 offset = 0; /* Offset starts at zero */
1927 memcpy (destination, &temp_pos[offset], sizeof (struct GNUNET_PeerIdentity));
1928 offset += sizeof (struct GNUNET_PeerIdentity);
1930 memcpy (direct, &temp_pos[offset], sizeof (struct GNUNET_PeerIdentity));
1931 offset += sizeof (struct GNUNET_PeerIdentity);
1934 memcpy (message_buf, &temp_pos[offset], message_size);
1936 (&send_msg->target, destination,
1937 sizeof (struct GNUNET_PeerIdentity)) != 0)
1939 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1940 dest_hash.encoding[4] = '\0';
1941 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1942 "%s: asked to send message to `%s', but address is for `%s'!",
1943 "DV SERVICE", GNUNET_i2s (&send_msg->target),
1944 (const char *) &dest_hash.encoding);
1947 #if DEBUG_DV_MESSAGES
1948 cbuf = (char *) message_buf;
1950 while (offset < message_size)
1952 packed_message = (struct GNUNET_MessageHeader *) &cbuf[offset];
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1954 "%s: DV PLUGIN SEND uid %u type %d to %s\n", my_short_id,
1955 ntohl (send_msg->uid), ntohs (packed_message->type),
1956 GNUNET_i2s (destination));
1957 offset += ntohs (packed_message->size);
1959 /*GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: DV PLUGIN SEND uid %u type %d to %s\n", my_short_id, ntohl(send_msg->uid), ntohs(message_buf->type), GNUNET_i2s(destination)); */
1961 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1962 dest_hash.encoding[4] = '\0';
1963 send_context = GNUNET_malloc (sizeof (struct DV_SendContext));
1965 send_result_msg = GNUNET_malloc (sizeof (struct GNUNET_DV_SendResultMessage));
1966 send_result_msg->header.size =
1967 htons (sizeof (struct GNUNET_DV_SendResultMessage));
1968 send_result_msg->header.type =
1969 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1970 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1972 send_context->importance = ntohl (send_msg->priority);
1973 send_context->timeout = send_msg->timeout;
1974 send_context->direct_peer = direct;
1975 send_context->distant_peer = destination;
1976 send_context->message = message_buf;
1977 send_context->message_size = message_size;
1978 send_context->send_result = send_result_msg;
1979 #if DEBUG_DV_MESSAGES
1980 send_context->uid = send_msg->uid;
1983 if (send_message_via (&my_identity, direct, send_context) != GNUNET_YES)
1985 send_result_msg->result = htons (1);
1987 GNUNET_malloc (sizeof (struct PendingMessage) +
1988 sizeof (struct GNUNET_DV_SendResultMessage));
1989 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1990 memcpy (&pending_message[1], send_result_msg,
1991 sizeof (struct GNUNET_DV_SendResultMessage));
1992 GNUNET_free (send_result_msg);
1994 GNUNET_CONTAINER_DLL_insert_after (plugin_pending_head, plugin_pending_tail,
1995 plugin_pending_tail, pending_message);
1997 if (client_handle != NULL)
1999 if (plugin_transmit_handle == NULL)
2001 plugin_transmit_handle =
2002 GNUNET_SERVER_notify_transmit_ready (client_handle,
2004 GNUNET_DV_SendResultMessage),
2005 GNUNET_TIME_UNIT_FOREVER_REL,
2006 &transmit_to_plugin, NULL);
2010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2011 "Failed to queue message for plugin, must be one in progress already!!\n");
2014 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
2015 dest_hash.encoding[4] = '\0';
2016 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2017 "%s DV SEND failed to send message to destination `%s' via `%s'\n",
2018 my_short_id, (const char *) &dest_hash.encoding,
2019 GNUNET_i2s (direct));
2022 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
2024 if (GNUNET_SYSERR !=
2025 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
2026 &destination->hashPubKey,
2027 &send_iterator, send_context))
2029 send_result_msg->result = htons (1);
2031 GNUNET_malloc (sizeof (struct PendingMessage) +
2032 sizeof (struct GNUNET_DV_SendResultMessage));
2033 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
2034 memcpy (&pending_message[1], send_result_msg,
2035 sizeof (struct GNUNET_DV_SendResultMessage));
2036 GNUNET_free (send_result_msg);
2038 GNUNET_CONTAINER_DLL_insert_after (plugin_pending_head, plugin_pending_tail,
2039 plugin_pending_tail, pending_message);
2041 if (client_handle != NULL)
2043 if (plugin_transmit_handle == NULL)
2045 plugin_transmit_handle =
2046 GNUNET_SERVER_notify_transmit_ready (client_handle,
2048 GNUNET_DV_SendResultMessage),
2049 GNUNET_TIME_UNIT_FOREVER_REL,
2050 &transmit_to_plugin, NULL);
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055 "Failed to queue message for plugin, must be one in progress already!!\n");
2058 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
2059 dest_hash.encoding[4] = '\0';
2060 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2061 "%s DV SEND failed to send message to destination `%s' via `%s'\n",
2062 my_short_id, (const char *) &dest_hash.encoding,
2063 GNUNET_i2s (direct));
2066 GNUNET_free (message_buf);
2067 GNUNET_free (send_context);
2068 GNUNET_free (direct);
2069 GNUNET_free (destination);
2071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2074 /** Forward declarations **/
2076 handle_dv_gossip_message (void *cls, const struct GNUNET_PeerIdentity *peer,
2077 const struct GNUNET_MessageHeader *message,
2078 const struct GNUNET_ATS_Information *atsi);
2081 handle_dv_disconnect_message (void *cls, const struct GNUNET_PeerIdentity *peer,
2082 const struct GNUNET_MessageHeader *message,
2083 const struct GNUNET_ATS_Information
2085 /** End forward declarations **/
2089 * List of handlers for the messages understood by this
2092 * Hmm... will we need to register some handlers with core and
2093 * some handlers with our server here? Because core should be
2094 * getting the incoming DV messages (from whichever lower level
2095 * transport) and then our server should be getting messages
2096 * from the dv_plugin, right?
2098 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2099 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
2100 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
2101 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
2105 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2106 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
2107 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
2112 * Free a DistantNeighbor node, including removing it
2113 * from the referer's list.
2116 distant_neighbor_free (struct DistantNeighbor *referee)
2118 struct DirectNeighbor *referrer;
2120 referrer = referee->referrer;
2121 if (referrer != NULL)
2123 GNUNET_CONTAINER_DLL_remove (referrer->referee_head, referrer->referee_tail,
2126 GNUNET_CONTAINER_heap_remove_node (referee->max_loc);
2127 GNUNET_CONTAINER_heap_remove_node (referee->min_loc);
2128 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
2129 &referee->identity.hashPubKey);
2130 GNUNET_free_non_null (referee->pkey);
2131 GNUNET_free (referee);
2135 * Free a DirectNeighbor node, including removing it
2136 * from the referer's list.
2139 direct_neighbor_free (struct DirectNeighbor *direct)
2141 struct NeighborSendContext *send_context;
2142 struct FastGossipNeighborList *about_list;
2143 struct FastGossipNeighborList *prev_about;
2145 send_context = direct->send_context;
2147 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2148 GNUNET_SCHEDULER_cancel (send_context->task);
2150 about_list = send_context->fast_gossip_list_head;
2151 while (about_list != NULL)
2153 GNUNET_CONTAINER_DLL_remove (send_context->fast_gossip_list_head,
2154 send_context->fast_gossip_list_tail,
2156 prev_about = about_list;
2157 about_list = about_list->next;
2158 GNUNET_free (prev_about);
2160 GNUNET_free (send_context);
2161 GNUNET_free (direct);
2165 * Multihashmap iterator for sending out disconnect messages
2168 * @param cls the peer that was disconnected
2169 * @param key key value stored under
2170 * @param value the direct neighbor to send disconnect to
2172 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2175 schedule_disconnect_messages (void *cls, const GNUNET_HashCode * key,
2178 struct DisconnectContext *disconnect_context = cls;
2179 struct DirectNeighbor *disconnected = disconnect_context->direct;
2180 struct DirectNeighbor *notify = value;
2181 struct PendingMessage *pending_message;
2182 p2p_dv_MESSAGE_Disconnect *disconnect_message;
2185 (¬ify->identity, &disconnected->identity,
2186 sizeof (struct GNUNET_PeerIdentity)) == 0)
2187 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
2190 GNUNET_malloc (sizeof (struct PendingMessage) +
2191 sizeof (p2p_dv_MESSAGE_Disconnect));
2192 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
2193 pending_message->importance = default_dv_priority;
2194 pending_message->timeout = GNUNET_TIME_relative_get_forever ();
2195 memcpy (&pending_message->recipient, ¬ify->identity,
2196 sizeof (struct GNUNET_PeerIdentity));
2197 pending_message->msg_size = sizeof (p2p_dv_MESSAGE_Disconnect);
2198 disconnect_message = (p2p_dv_MESSAGE_Disconnect *) pending_message->msg;
2199 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
2200 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
2201 disconnect_message->peer_id = htonl (disconnect_context->distant->our_id);
2203 GNUNET_CONTAINER_DLL_insert_after (core_pending_head, core_pending_tail,
2204 core_pending_tail, pending_message);
2206 GNUNET_SCHEDULER_add_now (try_core_send, NULL);
2207 /*if (core_transmit_handle == NULL)
2208 * core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, GNUNET_YES, default_dv_priority, GNUNET_TIME_relative_get_forever(), ¬ify->identity, sizeof(p2p_dv_MESSAGE_Disconnect), &core_transmit_notify, NULL); */
2214 * Multihashmap iterator for freeing extended neighbors.
2217 * @param key key value stored under
2218 * @param value the distant neighbor to be freed
2220 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2223 free_extended_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
2225 struct DistantNeighbor *distant = value;
2227 distant_neighbor_free (distant);
2232 * Multihashmap iterator for freeing direct neighbors.
2235 * @param key key value stored under
2236 * @param value the direct neighbor to be freed
2238 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2241 free_direct_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
2243 struct DirectNeighbor *direct = value;
2245 direct_neighbor_free (direct);
2251 * Task run during shutdown.
2257 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2261 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &print_neighbors,
2264 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors,
2265 &free_extended_neighbors, NULL);
2266 GNUNET_CONTAINER_multihashmap_destroy (extended_neighbors);
2267 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
2268 &free_direct_neighbors, NULL);
2269 GNUNET_CONTAINER_multihashmap_destroy (direct_neighbors);
2271 GNUNET_CONTAINER_heap_destroy (neighbor_max_heap);
2272 GNUNET_CONTAINER_heap_destroy (neighbor_min_heap);
2274 GNUNET_CORE_disconnect (coreAPI);
2276 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2277 GNUNET_SERVER_mst_destroy (coreMST);
2278 GNUNET_free_non_null (my_short_id);
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2285 * To be called on core init/fail.
2288 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2289 const struct GNUNET_PeerIdentity *identity)
2294 GNUNET_SCHEDULER_cancel (cleanup_task);
2295 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300 "%s: Core connection initialized, I am peer: %s\n", "dv",
2301 GNUNET_i2s (identity));
2303 memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
2304 my_short_id = GNUNET_strdup (GNUNET_i2s (&my_identity));
2309 #if PKEY_NO_NEIGHBOR_ON_ADD
2311 * Iterator over hash map entries.
2313 * @param cls closure
2314 * @param key current key code
2315 * @param value value in the hash map
2316 * @return GNUNET_YES if we should continue to
2321 add_pkey_to_extended (void *cls, const GNUNET_HashCode * key, void *abs_value)
2323 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2324 struct DistantNeighbor *distant_neighbor = abs_value;
2326 if (distant_neighbor->pkey == NULL)
2328 distant_neighbor->pkey =
2329 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2330 memcpy (distant_neighbor->pkey, pkey,
2331 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2339 * Iterator over hash map entries.
2341 * @param cls closure
2342 * @param key current key code
2343 * @param value value in the hash map
2344 * @return GNUNET_YES if we should continue to
2349 update_matching_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
2351 struct NeighborUpdateInfo *update_info = cls;
2352 struct DistantNeighbor *distant_neighbor = value;
2354 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2356 /* same referrer, cost change! */
2357 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2358 update_info->neighbor->max_loc,
2360 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2361 update_info->neighbor->min_loc,
2363 update_info->neighbor->last_activity = update_info->now;
2364 update_info->neighbor->cost = update_info->cost;
2365 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2374 * Iterate over all current direct peers, add DISTANT newly connected
2375 * peer to the fast gossip list for that peer so we get DV routing
2376 * information out as fast as possible!
2378 * @param cls the newly connected neighbor we will gossip about
2379 * @param key the hashcode of the peer
2380 * @param value the direct neighbor we should gossip to
2382 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2385 add_distant_all_direct_neighbors (void *cls, const GNUNET_HashCode * key,
2388 struct DirectNeighbor *direct = (struct DirectNeighbor *) value;
2389 struct DistantNeighbor *distant = (struct DistantNeighbor *) cls;
2390 struct NeighborSendContext *send_context = direct->send_context;
2391 struct FastGossipNeighborList *gossip_entry;
2398 if (distant == NULL)
2404 (&direct->identity, &distant->identity,
2405 sizeof (struct GNUNET_PeerIdentity)) == 0)
2407 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2411 if (distant->hidden == GNUNET_YES)
2412 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2414 gossip_entry = GNUNET_malloc (sizeof (struct FastGossipNeighborList));
2415 gossip_entry->about = distant;
2417 GNUNET_CONTAINER_DLL_insert_after (send_context->fast_gossip_list_head,
2418 send_context->fast_gossip_list_tail,
2419 send_context->fast_gossip_list_tail,
2422 encPeerAbout = GNUNET_strdup (GNUNET_i2s (&distant->identity));
2423 encPeerTo = GNUNET_strdup (GNUNET_i2s (&direct->identity));
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2427 GNUNET_i2s (&my_identity), encPeerAbout, distant->our_id,
2429 GNUNET_free (encPeerAbout);
2430 GNUNET_free (encPeerTo);
2432 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2433 * GNUNET_SCHEDULER_cancel(send_context->task); */
2435 send_context->task =
2436 GNUNET_SCHEDULER_add_now (&neighbor_send_task, send_context);
2441 * Callback for hello address creation.
2443 * @param cls closure, a struct HelloContext
2444 * @param max maximum number of bytes that can be written to buf
2445 * @param buf where to write the address information
2447 * @return number of bytes written, 0 to signal the
2448 * end of the iteration.
2451 generate_hello_address (void *cls, size_t max, void *buf)
2453 struct HelloContext *hello_context = cls;
2459 if (hello_context->addresses_to_add == 0)
2462 /* Hello "address" will be concatenation of distant peer and direct peer identities */
2463 size = 2 * sizeof (struct GNUNET_PeerIdentity);
2464 GNUNET_assert (max >= size);
2466 addr_buffer = GNUNET_malloc (size);
2468 /* Copy the distant peer identity to buffer */
2469 memcpy (addr_buffer, &hello_context->distant_peer,
2470 sizeof (struct GNUNET_PeerIdentity));
2471 offset += sizeof (struct GNUNET_PeerIdentity);
2472 /* Copy the direct peer identity to buffer */
2473 memcpy (&addr_buffer[offset], hello_context->direct_peer,
2474 sizeof (struct GNUNET_PeerIdentity));
2476 GNUNET_HELLO_add_address ("dv",
2477 GNUNET_TIME_relative_to_absolute
2478 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2481 hello_context->addresses_to_add--;
2483 GNUNET_free (addr_buffer);
2489 * Handles when a peer is either added due to being newly connected
2490 * or having been gossiped about, also called when the cost for a neighbor
2491 * needs to be updated.
2493 * @param peer identity of the peer whose info is being added/updated
2494 * @param pkey public key of the peer whose info is being added/updated
2495 * @param referrer_peer_id id to use when sending to 'peer'
2496 * @param referrer if this is a gossiped peer, who did we hear it from?
2497 * @param cost the cost of communicating with this peer via 'referrer'
2499 * @return the added neighbor, the updated neighbor or NULL (neighbor
2502 static struct DistantNeighbor *
2503 addUpdateNeighbor (const struct GNUNET_PeerIdentity *peer,
2504 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2505 unsigned int referrer_peer_id,
2506 struct DirectNeighbor *referrer, unsigned int cost)
2508 struct DistantNeighbor *neighbor;
2509 struct DistantNeighbor *max;
2510 struct GNUNET_TIME_Absolute now;
2511 struct NeighborUpdateInfo *neighbor_update;
2512 struct HelloContext *hello_context;
2513 struct GNUNET_HELLO_Message *hello_msg;
2514 unsigned int our_id;
2519 #if DEBUG_DV_PEER_NUMBERS
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s Received sender id (%u)!\n",
2523 "DV SERVICE", referrer_peer_id);
2526 now = GNUNET_TIME_absolute_get ();
2528 GNUNET_CONTAINER_multihashmap_get (extended_neighbors, &peer->hashPubKey);
2529 neighbor_update = GNUNET_malloc (sizeof (struct NeighborUpdateInfo));
2530 neighbor_update->neighbor = neighbor;
2531 neighbor_update->cost = cost;
2532 neighbor_update->now = now;
2533 neighbor_update->referrer = referrer;
2534 neighbor_update->referrer_peer_id = referrer_peer_id;
2536 if (neighbor != NULL)
2539 memcpy (&our_id, &neighbor->identity, sizeof (unsigned int));
2541 our_id = neighbor->our_id;
2547 memcpy (&our_id, peer, sizeof (unsigned int));
2550 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
2555 /* Either we do not know this peer, or we already do but via a different immediate peer */
2556 if ((neighbor == NULL) ||
2557 (GNUNET_CONTAINER_multihashmap_get_multiple
2558 (extended_neighbors, &peer->hashPubKey, &update_matching_neighbors,
2559 neighbor_update) != GNUNET_SYSERR))
2562 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2564 distant_neighbor_free (neighbor);
2566 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2572 if (cost > fisheye_depth)
2575 GNUNET_free (neighbor_update);
2579 #if DEBUG_DV_PEER_NUMBERS
2580 encAbout = GNUNET_strdup (GNUNET_i2s (peer));
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 "%s: %s Chose NEW id (%u) for peer %s!\n",
2583 GNUNET_i2s (&my_identity), "DV SERVICE", our_id, encAbout);
2584 GNUNET_free (encAbout);
2587 if (max_table_size <=
2588 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2590 /* remove most expensive entry */
2591 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2592 GNUNET_assert (max != NULL);
2593 if (cost > max->cost)
2595 /* new entry most expensive, don't create */
2596 GNUNET_free (neighbor_update);
2601 /* only free if this is not a direct connection;
2602 * we could theoretically have more direct
2603 * connections than DV entries allowed total! */
2604 distant_neighbor_free (max);
2608 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2609 GNUNET_CONTAINER_DLL_insert (referrer->referee_head, referrer->referee_tail,
2612 GNUNET_CONTAINER_heap_insert (neighbor_max_heap, neighbor, cost);
2614 GNUNET_CONTAINER_heap_insert (neighbor_min_heap, neighbor, cost);
2615 neighbor->referrer = referrer;
2616 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2617 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2620 GNUNET_malloc (sizeof
2621 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2622 memcpy (neighbor->pkey, pkey,
2623 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2626 neighbor->pkey = pkey;
2628 neighbor->last_activity = now;
2629 neighbor->cost = cost;
2630 neighbor->referrer_id = referrer_peer_id;
2631 neighbor->our_id = our_id;
2634 DIRECT_NEIGHBOR_COST)
2635 ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2638 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2640 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2641 if (referrer_peer_id != 0)
2643 for (i = 0; i < MAX_OUTSTANDING_MESSAGES; i++)
2645 if (referrer->pending_messages[i].sender_id == referrer_peer_id) /* We have a queued message from just learned about peer! */
2647 #if DEBUG_DV_MESSAGES
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "%s: learned about peer %llu from which we have a previous unknown message, processing!\n",
2650 my_short_id, referrer_peer_id);
2652 struct GNUNET_ATS_Information atsi[3];
2654 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2655 atsi[0].value = htonl (referrer->pending_messages[i].distance);
2656 atsi[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2658 htonl ((uint32_t) referrer->pending_messages[i].
2660 atsi[2].type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
2661 atsi[2].value = htonl (0);
2662 handle_dv_data_message (NULL, &referrer->pending_messages[i].sender,
2663 referrer->pending_messages[i].message,
2664 (const struct GNUNET_ATS_Information
2666 GNUNET_free (referrer->pending_messages[i].message);
2667 referrer->pending_messages[i].sender_id = 0;
2671 if ((cost != DIRECT_NEIGHBOR_COST) && (neighbor->pkey != NULL))
2673 /* Added neighbor, now send HELLO to transport */
2674 hello_context = GNUNET_malloc (sizeof (struct HelloContext));
2675 hello_context->direct_peer = &referrer->identity;
2676 memcpy (&hello_context->distant_peer, peer,
2677 sizeof (struct GNUNET_PeerIdentity));
2678 hello_context->addresses_to_add = 1;
2680 GNUNET_HELLO_create (pkey, &generate_hello_address, hello_context);
2681 GNUNET_assert (memcmp
2682 (hello_context->direct_peer, &hello_context->distant_peer,
2683 sizeof (struct GNUNET_PeerIdentity)) != 0);
2684 addr1 = GNUNET_strdup (GNUNET_i2s (hello_context->direct_peer));
2685 addr2 = GNUNET_strdup (GNUNET_i2s (&hello_context->distant_peer));
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2688 "%s: GIVING HELLO size %d for %s via %s to TRANSPORT\n",
2689 my_short_id, GNUNET_HELLO_size (hello_msg), addr2, addr1);
2691 GNUNET_free (addr1);
2692 GNUNET_free (addr2);
2693 send_to_plugin (hello_context->direct_peer,
2694 GNUNET_HELLO_get_header (hello_msg),
2695 GNUNET_HELLO_size (hello_msg),
2696 &hello_context->distant_peer, cost);
2697 GNUNET_free (hello_context);
2698 GNUNET_free (hello_msg);
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv",
2707 GNUNET_i2s (peer), cost, referrer_peer_id);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Size of extended_neighbors is %d\n",
2712 "dv", GNUNET_CONTAINER_multihashmap_size (extended_neighbors));
2715 GNUNET_free (neighbor_update);
2721 * Core handler for dv disconnect messages. These will be used
2722 * by us to tell transport via the dv plugin that a peer can
2723 * no longer be contacted by us via a certain address. We should
2724 * then propagate these messages on, given that the distance to
2725 * the peer indicates we would have gossiped about it to others.
2727 * @param cls closure
2728 * @param peer peer which sent the message (immediate sender)
2729 * @param message the message
2730 * @param atsi performance data
2733 handle_dv_disconnect_message (void *cls, const struct GNUNET_PeerIdentity *peer,
2734 const struct GNUNET_MessageHeader *message,
2735 const struct GNUNET_ATS_Information
2738 struct DirectNeighbor *referrer;
2739 struct DistantNeighbor *distant;
2740 p2p_dv_MESSAGE_Disconnect *enc_message =
2741 (p2p_dv_MESSAGE_Disconnect *) message;
2743 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2745 return GNUNET_SYSERR; /* invalid message */
2749 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2750 if (referrer == NULL)
2753 distant = referrer->referee_head;
2754 while (distant != NULL)
2756 if (distant->referrer_id == ntohl (enc_message->peer_id))
2758 distant_neighbor_free (distant);
2759 distant = referrer->referee_head;
2762 distant = distant->next;
2770 * Core handler for dv gossip messages. These will be used
2771 * by us to create a HELLO message for the newly peer containing
2772 * which direct peer we can connect through, and what the cost
2773 * is. This HELLO will then be scheduled for validation by the
2774 * transport service so that it can be used by all others.
2776 * @param cls closure
2777 * @param peer peer which sent the message (immediate sender)
2778 * @param message the message
2779 * @param atsi performance data
2782 handle_dv_gossip_message (void *cls, const struct GNUNET_PeerIdentity *peer,
2783 const struct GNUNET_MessageHeader *message,
2784 const struct GNUNET_ATS_Information *atsi)
2786 struct DirectNeighbor *referrer;
2787 p2p_dv_MESSAGE_NeighborInfo *enc_message =
2788 (p2p_dv_MESSAGE_NeighborInfo *) message;
2790 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2792 return GNUNET_SYSERR; /* invalid message */
2795 #if DEBUG_DV_GOSSIP_RECEIPT
2799 encPeerAbout = GNUNET_strdup (GNUNET_i2s (&enc_message->neighbor));
2800 encPeerFrom = GNUNET_strdup (GNUNET_i2s (peer));
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2802 "%s: Received %s message from peer %s about peer %s id %u distance %d!\n",
2803 GNUNET_i2s (&my_identity), "DV GOSSIP", encPeerFrom, encPeerAbout,
2804 ntohl (enc_message->neighbor_id), ntohl (enc_message->cost) + 1);
2805 GNUNET_free (encPeerAbout);
2806 GNUNET_free (encPeerFrom);
2810 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2811 if (referrer == NULL)
2814 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2815 ntohl (enc_message->neighbor_id), referrer,
2816 ntohl (enc_message->cost) + 1);
2823 * Iterate over all currently known peers, add them to the
2824 * fast gossip list for this peer so we get DV routing information
2825 * out as fast as possible!
2827 * @param cls the direct neighbor we will gossip to
2828 * @param key the hashcode of the peer
2829 * @param value the distant neighbor we should add to the list
2831 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2834 add_all_extended_peers (void *cls, const GNUNET_HashCode * key, void *value)
2836 struct NeighborSendContext *send_context = (struct NeighborSendContext *) cls;
2837 struct DistantNeighbor *distant = (struct DistantNeighbor *) value;
2838 struct FastGossipNeighborList *gossip_entry;
2841 (&send_context->toNeighbor->identity, &distant->identity,
2842 sizeof (struct GNUNET_PeerIdentity)) == 0)
2843 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2846 if (distant->hidden == GNUNET_YES)
2847 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2849 gossip_entry = GNUNET_malloc (sizeof (struct FastGossipNeighborList));
2850 gossip_entry->about = distant;
2852 GNUNET_CONTAINER_DLL_insert_after (send_context->fast_gossip_list_head,
2853 send_context->fast_gossip_list_tail,
2854 send_context->fast_gossip_list_tail,
2862 * Iterator over hash map entries.
2864 * @param cls closure
2865 * @param key current key code
2866 * @param value value in the hash map
2867 * @return GNUNET_YES if we should continue to
2872 gossip_all_to_all_iterator (void *cls, const GNUNET_HashCode * key,
2875 struct DirectNeighbor *direct = abs_value;
2877 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors,
2878 &add_all_extended_peers,
2879 direct->send_context);
2881 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2882 GNUNET_SCHEDULER_cancel (direct->send_context->task);
2884 direct->send_context->task =
2885 GNUNET_SCHEDULER_add_now (&neighbor_send_task, direct->send_context);
2890 * Task run during shutdown.
2896 gossip_all_to_all (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2898 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
2899 &gossip_all_to_all_iterator, NULL);
2901 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2902 (GNUNET_TIME_UNIT_SECONDS, 5),
2903 &gossip_all_to_all, NULL);
2908 * Iterate over all current direct peers, add newly connected peer
2909 * to the fast gossip list for that peer so we get DV routing
2910 * information out as fast as possible!
2912 * @param cls the newly connected neighbor we will gossip about
2913 * @param key the hashcode of the peer
2914 * @param value the direct neighbor we should gossip to
2916 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2919 add_all_direct_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
2921 struct DirectNeighbor *direct = (struct DirectNeighbor *) value;
2922 struct DirectNeighbor *to = (struct DirectNeighbor *) cls;
2923 struct DistantNeighbor *distant;
2924 struct NeighborSendContext *send_context = direct->send_context;
2925 struct FastGossipNeighborList *gossip_entry;
2930 GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2931 &to->identity.hashPubKey);
2932 if (distant == NULL)
2938 (&direct->identity, &to->identity,
2939 sizeof (struct GNUNET_PeerIdentity)) == 0)
2941 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2945 if (distant->hidden == GNUNET_YES)
2946 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2948 direct_id = GNUNET_strdup (GNUNET_i2s (&direct->identity));
2950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2951 "%s: adding peer %s to fast send list for %s\n", my_short_id,
2952 GNUNET_i2s (&distant->identity), direct_id);
2954 GNUNET_free (direct_id);
2955 gossip_entry = GNUNET_malloc (sizeof (struct FastGossipNeighborList));
2956 gossip_entry->about = distant;
2958 GNUNET_CONTAINER_DLL_insert_after (send_context->fast_gossip_list_head,
2959 send_context->fast_gossip_list_tail,
2960 send_context->fast_gossip_list_tail,
2962 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2963 GNUNET_SCHEDULER_cancel (send_context->task);
2965 send_context->task =
2966 GNUNET_SCHEDULER_add_now (&neighbor_send_task, send_context);
2967 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2968 //neighbor_send_task(send_context, &tc);
2973 * Type of an iterator over the hosts. Note that each
2974 * host will be called with each available protocol.
2976 * @param cls closure
2977 * @param peer id of the peer, NULL for last call
2978 * @param hello hello message for the peer (can be NULL)
2979 * @param err_msg NULL if successful, otherwise contains error message
2982 process_peerinfo (void *cls, const struct GNUNET_PeerIdentity *peer,
2983 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
2985 struct PeerIteratorContext *peerinfo_iterator = cls;
2986 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2987 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2989 #if DEBUG_DV_PEER_NUMBERS
2994 if (err_msg != NULL)
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2997 _("Error in communication with PEERINFO service\n"));
3002 if (distant->pkey == NULL)
3005 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3006 "Failed to get peerinfo information for this peer, retrying!\n");
3008 peerinfo_iterator->ic =
3009 GNUNET_PEERINFO_iterate (peerinfo_handle,
3010 &peerinfo_iterator->neighbor->identity,
3011 GNUNET_TIME_relative_multiply
3012 (GNUNET_TIME_UNIT_SECONDS, 3),
3013 &process_peerinfo, peerinfo_iterator);
3017 GNUNET_free (peerinfo_iterator);
3023 (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity) != 0))
3026 if ((hello != NULL) &&
3027 (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
3029 if (distant->pkey == NULL)
3032 GNUNET_malloc (sizeof
3033 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3034 memcpy (distant->pkey, &neighbor->pkey,
3035 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3039 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors,
3040 &add_all_extended_peers,
3041 neighbor->send_context);
3044 GNUNET_STATISTICS_update (stats,
3045 "# distant peers gossiped to direct neighbors",
3048 #if DEBUG_DV_PEER_NUMBERS
3049 neighbor_pid = GNUNET_strdup (GNUNET_i2s (&neighbor->identity));
3050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3051 "%s: Gossipped %d extended peers to %s\n",
3052 GNUNET_i2s (&my_identity), sent, neighbor_pid);
3055 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
3056 &add_all_direct_neighbors,
3060 GNUNET_STATISTICS_update (stats,
3061 "# direct peers gossiped to direct neighbors",
3064 #if DEBUG_DV_PEER_NUMBERS
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3066 "%s: Gossipped about %s to %d direct peers\n",
3067 GNUNET_i2s (&my_identity), neighbor_pid, sent);
3068 GNUNET_free (neighbor_pid);
3070 neighbor->send_context->task =
3071 GNUNET_SCHEDULER_add_now (&neighbor_send_task, neighbor->send_context);
3077 * Method called whenever a peer connects.
3079 * @param cls closure
3080 * @param peer peer identity this notification is about
3081 * @param atsi performance data
3084 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3085 const struct GNUNET_ATS_Information *atsi)
3087 struct DirectNeighbor *neighbor;
3088 struct DistantNeighbor *about;
3089 struct PeerIteratorContext *peerinfo_iterator;
3094 /* Check for connect to self message */
3095 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
3098 distance = get_atsi_distance (atsi);
3099 if ((distance == DIRECT_NEIGHBOR_COST) &&
3100 (GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey)
3103 peerinfo_iterator = GNUNET_malloc (sizeof (struct PeerIteratorContext));
3104 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
3105 neighbor->send_context =
3106 GNUNET_malloc (sizeof (struct NeighborSendContext));
3107 neighbor->send_context->toNeighbor = neighbor;
3108 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
3110 GNUNET_assert (GNUNET_SYSERR !=
3111 GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
3114 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3115 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
3116 peerinfo_iterator->distant = about;
3117 peerinfo_iterator->neighbor = neighbor;
3118 peerinfo_iterator->ic =
3119 GNUNET_PEERINFO_iterate (peerinfo_handle, peer,
3120 GNUNET_TIME_relative_multiply
3121 (GNUNET_TIME_UNIT_SECONDS, 3),
3122 &process_peerinfo, peerinfo_iterator);
3124 if ((about != NULL) && (about->pkey == NULL))
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3128 "Newly added peer %s has NULL pkey!\n", GNUNET_i2s (peer));
3131 else if (about != NULL)
3133 GNUNET_free (peerinfo_iterator);
3139 GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
3141 if ((GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey)
3142 == NULL) && (about != NULL))
3145 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
3146 &add_distant_all_direct_neighbors,
3149 GNUNET_STATISTICS_update (stats,
3150 "# direct peers gossiped to new direct neighbors",
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3155 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n",
3156 "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s (peer));
3163 * Method called whenever a given peer disconnects.
3165 * @param cls closure
3166 * @param peer peer identity this notification is about
3169 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3171 struct DirectNeighbor *neighbor;
3172 struct DistantNeighbor *referee;
3173 struct FindDestinationContext fdc;
3174 struct DisconnectContext disconnect_context;
3175 struct PendingMessage *pending_pos;
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3179 "%s: Receives core peer disconnect message!\n", "dv");
3182 /* Check for disconnect from self message */
3183 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
3187 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
3189 if (neighbor == NULL)
3194 pending_pos = core_pending_head;
3195 while (NULL != pending_pos)
3198 memcmp (&pending_pos->recipient, &neighbor->identity,
3199 sizeof (struct GNUNET_PeerIdentity)))
3201 GNUNET_CONTAINER_DLL_remove (core_pending_head, core_pending_tail,
3203 pending_pos = core_pending_head;
3206 pending_pos = pending_pos->next;
3209 while (NULL != (referee = neighbor->referee_head))
3210 distant_neighbor_free (referee);
3215 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer,
3218 if (fdc.dest != NULL)
3220 disconnect_context.direct = neighbor;
3221 disconnect_context.distant = fdc.dest;
3222 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors,
3223 &schedule_disconnect_messages,
3224 &disconnect_context);
3227 GNUNET_assert (neighbor->referee_tail == NULL);
3229 GNUNET_CONTAINER_multihashmap_remove (direct_neighbors, &peer->hashPubKey,
3234 if ((neighbor->send_context != NULL) &&
3235 (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
3236 GNUNET_SCHEDULER_cancel (neighbor->send_context->task);
3237 GNUNET_free (neighbor);
3242 * Process dv requests.
3244 * @param cls closure
3245 * @param server the initialized server
3246 * @param c configuration to use
3249 run (void *cls, struct GNUNET_SERVER_Handle *server,
3250 const struct GNUNET_CONFIGURATION_Handle *c)
3252 unsigned long long max_hosts;
3256 /* FIXME: Read from config, or calculate, or something other than this! */
3257 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
3258 max_table_size = DEFAULT_DV_SIZE;
3259 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
3261 if (GNUNET_CONFIGURATION_have_value (cfg, "dv", "max_direct_connections"))
3262 GNUNET_assert (GNUNET_OK ==
3263 GNUNET_CONFIGURATION_get_value_number (cfg, "dv",
3264 "max_direct_connections",
3267 if (GNUNET_CONFIGURATION_have_value (cfg, "dv", "max_total_connections"))
3268 GNUNET_assert (GNUNET_OK ==
3269 GNUNET_CONFIGURATION_get_value_number (cfg, "dv",
3270 "max_total_connections",
3274 if (GNUNET_CONFIGURATION_have_value (cfg, "dv", "fisheye_depth"))
3275 GNUNET_assert (GNUNET_OK ==
3276 GNUNET_CONFIGURATION_get_value_number (cfg, "dv",
3281 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3283 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
3285 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
3286 extended_neighbors =
3287 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
3289 GNUNET_SERVER_add_handlers (server, plugin_handlers);
3290 coreAPI = GNUNET_CORE_connect (cfg, 1, NULL, /* FIXME: anything we want to pass around? */
3291 &core_init, &handle_core_connect,
3292 &handle_core_disconnect, NULL, GNUNET_NO,
3293 NULL, GNUNET_NO, core_handlers);
3295 if (coreAPI == NULL)
3298 coreMST = GNUNET_SERVER_mst_create (&tokenized_message_handler, NULL);
3300 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
3302 if (peerinfo_handle == NULL)
3304 GNUNET_CORE_disconnect (coreAPI);
3308 /* Scheduled the task to clean up when shutdown is called */
3310 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3311 &shutdown_task, NULL);
3316 * The main function for the dv service.
3318 * @param argc number of arguments from the command line
3319 * @param argv command line arguments
3320 * @return 0 ok, 1 on error
3323 main (int argc, char *const *argv)
3325 return (GNUNET_OK ==
3326 GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
3327 &run, NULL)) ? 0 : 1;