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,
666 struct GNUNET_CONTAINER_HeapNode *node,
667 void *element, GNUNET_CONTAINER_HeapCostType cost)
669 struct FindDestinationContext *fdc = cls;
670 struct DistantNeighbor *dn = element;
672 if (fdc->tid != dn->our_id)
680 * We've been given a target ID based on the random numbers that
681 * we assigned to our DV-neighborhood. Find the entry for the
682 * respective neighbor.
685 find_specific_id (void *cls,
686 const GNUNET_HashCode *key,
689 struct FindIDContext *fdc = cls;
690 struct DistantNeighbor *dn = value;
692 if (memcmp(&dn->referrer->identity, fdc->via, sizeof(struct GNUNET_PeerIdentity)) == 0)
694 fdc->tid = dn->referrer_id;
701 * Find a distant peer whose referrer_id matches what we're
702 * looking for. For looking up a peer we've gossipped about
703 * but is now disconnected. Need to do this because we don't
704 * want to remove those that may be accessible via a different
707 static int find_distant_peer (void *cls,
708 const GNUNET_HashCode * key,
711 struct FindDestinationContext *fdc = cls;
712 struct DistantNeighbor *distant = value;
714 if (fdc->tid == distant->referrer_id)
723 * Function called to notify a client about the socket
724 * begin ready to queue more data. "buf" will be
725 * NULL and "size" zero if the socket was closed for
726 * writing in the meantime.
729 * @param size number of bytes available in buf
730 * @param buf where the callee should write the message
731 * @return number of bytes written to buf
733 size_t transmit_to_plugin (void *cls,
734 size_t size, void *buf)
737 struct PendingMessage *reply;
743 /* client disconnected */
744 #if DEBUG_DV_MESSAGES
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: %s buffer was NULL (client disconnect?)\n", my_short_id, "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,
757 memcpy (&cbuf[off], reply->msg, msize);
762 if (plugin_pending_head != NULL)
763 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
764 ntohs(plugin_pending_head->msg->size),
765 GNUNET_TIME_UNIT_FOREVER_REL,
766 &transmit_to_plugin, NULL);
772 * Send a message to the dv plugin.
774 * @param sender the direct sender of the message
775 * @param message the message to send to the plugin
776 * (may be an encapsulated type)
777 * @param message_size the size of the message to be sent
778 * @param distant_neighbor the original sender of the message
779 * @param cost the cost to the original sender of the message
781 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
782 const struct GNUNET_MessageHeader *message,
784 struct GNUNET_PeerIdentity *distant_neighbor,
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, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
798 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
800 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
801 sender_address = GNUNET_malloc(sender_address_len);
802 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
803 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
807 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
808 sender_address = GNUNET_malloc(sender_address_len);
809 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
812 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
813 received_msg = GNUNET_malloc(size);
814 received_msg->header.size = htons(size);
815 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
816 received_msg->distance = htonl(cost);
817 received_msg->msg_len = htonl(message_size);
818 /* Set the sender in this message to be the original sender! */
819 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
820 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
821 memcpy(&received_msg[1], sender_address, sender_address_len);
822 GNUNET_free(sender_address);
823 /* Copy the actual message after the sender */
824 packed_msg_start = (char *)&received_msg[1];
825 packed_msg_start = &packed_msg_start[sender_address_len];
826 memcpy(packed_msg_start, message, message_size);
827 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
828 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
829 memcpy(&pending_message[1], received_msg, size);
830 GNUNET_free(received_msg);
832 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
834 if (client_handle != NULL)
836 if (plugin_transmit_handle == NULL)
838 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
839 size, GNUNET_TIME_UNIT_FOREVER_REL,
840 &transmit_to_plugin, NULL);
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
849 /* Declare here so retry_core_send is aware of it */
850 size_t core_transmit_notify (void *cls,
851 size_t size, void *buf);
854 * Try to send another message from our core sending list
857 try_core_send (void *cls,
858 const struct GNUNET_SCHEDULER_TaskContext *tc)
860 struct PendingMessage *pending;
861 pending = core_pending_head;
863 if (core_transmit_handle != NULL)
864 return; /* Message send already in progress */
866 if ((pending != NULL) && (coreAPI != NULL))
867 core_transmit_handle = GNUNET_CORE_notify_transmit_ready (coreAPI, pending->importance, pending->timeout, &pending->recipient, pending->msg_size, &core_transmit_notify, NULL);
871 * Function called to notify a client about the socket
872 * being ready to queue more data. "buf" will be
873 * NULL and "size" zero if the socket was closed for
874 * writing in the meantime.
876 * @param cls closure (NULL)
877 * @param size number of bytes available in buf
878 * @param buf where the callee should write the message
879 * @return number of bytes written to buf
881 size_t core_transmit_notify (void *cls,
882 size_t size, void *buf)
885 struct PendingMessage *pending;
886 struct PendingMessage *client_reply;
892 /* client disconnected */
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
899 core_transmit_handle = NULL;
901 pending = core_pending_head;
902 if ( (pending != NULL) &&
903 (size >= (msize = ntohs (pending->msg->size))))
906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
908 GNUNET_CONTAINER_DLL_remove (core_pending_head,
911 if (pending->send_result != NULL) /* Will only be non-null if a real client asked for this send */
913 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
914 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
915 memcpy(&client_reply[1], pending->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
916 GNUNET_free(pending->send_result);
918 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
919 if (client_handle != NULL)
921 if (plugin_transmit_handle == NULL)
923 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
924 sizeof(struct GNUNET_DV_SendResultMessage),
925 GNUNET_TIME_UNIT_FOREVER_REL,
926 &transmit_to_plugin, NULL);
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
934 memcpy (&cbuf[off], pending->msg, msize);
935 GNUNET_free (pending);
938 /*reply = core_pending_head;*/
940 GNUNET_SCHEDULER_add_now(&try_core_send, NULL);
942 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);*/
949 * Send a DV data message via DV.
951 * @param sender the original sender of the message
952 * @param recipient the next hop recipient, may be our direct peer, maybe not
953 * @param send_context the send context
956 send_message_via (const struct GNUNET_PeerIdentity *sender,
957 const struct GNUNET_PeerIdentity *recipient,
958 struct DV_SendContext *send_context)
960 p2p_dv_MESSAGE_Data *toSend;
961 unsigned int msg_size;
962 unsigned int recipient_id;
963 unsigned int sender_id;
964 struct DistantNeighbor *source;
965 struct PendingMessage *pending_message;
966 struct FindIDContext find_context;
971 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
973 find_context.dest = send_context->distant_peer;
974 find_context.via = recipient;
975 find_context.tid = 0;
976 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
977 &find_specific_id, &find_context);
979 if (find_context.tid == 0)
981 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
982 /* target unknown to us, drop! */
983 return GNUNET_SYSERR;
985 recipient_id = find_context.tid;
987 if (0 == (memcmp (&my_identity,
988 sender, sizeof (struct GNUNET_PeerIdentity))))
991 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
992 &sender->hashPubKey);
994 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: send_message_via found %s, myself in extended peer list???\n", my_short_id, GNUNET_i2s(&source->identity));
998 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
999 &sender->hashPubKey);
1002 /* sender unknown to us, drop! */
1003 return GNUNET_SYSERR;
1005 sender_id = source->our_id;
1008 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1009 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1010 pending_message->send_result = send_context->send_result;
1011 memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
1012 pending_message->msg_size = msg_size;
1013 pending_message->importance = send_context->importance;
1014 pending_message->timeout = send_context->timeout;
1015 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1016 toSend->header.size = htons (msg_size);
1017 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1018 toSend->sender = htonl (sender_id);
1019 toSend->recipient = htonl (recipient_id);
1020 #if DEBUG_DV_MESSAGES
1021 toSend->uid = send_context->uid; /* Still sent around in network byte order */
1023 toSend->uid = htonl(0);
1026 memcpy (&toSend[1], send_context->message, send_context->message_size);
1029 memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
1030 shortname[4] = '\0';
1031 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(recipient), msg_size);
1034 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1039 GNUNET_SCHEDULER_add_now(try_core_send, NULL);
1045 * Given a FindLeastCostContext, and a set
1046 * of peers that match the target, return the cheapest.
1048 * @param cls closure, a struct FindLeastCostContext
1049 * @param key the key identifying the target peer
1050 * @param value the target peer
1052 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1055 find_least_cost_peer (void *cls,
1056 const GNUNET_HashCode *key,
1059 struct FindLeastCostContext *find_context = cls;
1060 struct DistantNeighbor *dn = value;
1062 if (dn->cost < find_context->least_cost)
1064 find_context->target = dn;
1066 if (dn->cost == DIRECT_NEIGHBOR_COST)
1072 * Send a DV data message via DV.
1074 * @param recipient the ultimate recipient of this message
1075 * @param sender the original sender of the message
1076 * @param specific_neighbor the specific neighbor to send this message via
1077 * @param message the packed message
1078 * @param message_size size of the message
1079 * @param importance what priority to send this message with
1080 * @param uid the unique identifier of this message (or 0 for none)
1081 * @param timeout how long to possibly delay sending this message
1084 send_message (const struct GNUNET_PeerIdentity * recipient,
1085 const struct GNUNET_PeerIdentity * sender,
1086 const struct DistantNeighbor * specific_neighbor,
1087 const struct GNUNET_MessageHeader * message,
1088 size_t message_size,
1089 unsigned int importance,
1091 struct GNUNET_TIME_Relative timeout)
1093 p2p_dv_MESSAGE_Data *toSend;
1094 unsigned int msg_size;
1096 unsigned int recipient_id;
1097 unsigned int sender_id;
1098 struct DistantNeighbor *target;
1099 struct DistantNeighbor *source;
1100 struct PendingMessage *pending_message;
1101 struct FindLeastCostContext find_least_ctx;
1102 #if DEBUG_DV_PEER_NUMBERS
1103 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1104 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1105 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1107 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1109 find_least_ctx.least_cost = -1;
1110 find_least_ctx.target = NULL;
1112 * Need to find the least cost peer, lest the transport selection keep
1113 * picking the same DV route for the same destination which results
1114 * in messages looping forever. Relatively cheap, we don't iterate
1115 * over all known peers, just those that apply.
1117 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1118 &recipient->hashPubKey, &find_least_cost_peer, &find_least_ctx);
1119 target = find_least_ctx.target;
1123 /* target unknown to us, drop! */
1124 return GNUNET_SYSERR;
1126 recipient_id = target->referrer_id;
1128 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1129 &sender->hashPubKey);
1132 if (0 != (memcmp (&my_identity,
1133 sender, sizeof (struct GNUNET_PeerIdentity))))
1135 /* sender unknown to us, drop! */
1136 return GNUNET_SYSERR;
1138 sender_id = 0; /* 0 == us */
1142 /* find out the number that we use when we gossip about
1144 sender_id = source->our_id;
1147 #if DEBUG_DV_PEER_NUMBERS
1148 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1149 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1150 encPeerFrom.encoding[4] = '\0';
1151 encPeerVia.encoding[4] = '\0';
1153 if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1158 cost = target->cost;
1159 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1160 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1161 pending_message->send_result = NULL;
1162 pending_message->importance = importance;
1163 pending_message->timeout = timeout;
1164 memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1165 pending_message->msg_size = msg_size;
1166 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1167 toSend->header.size = htons (msg_size);
1168 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1169 toSend->sender = htonl (sender_id);
1170 toSend->recipient = htonl (recipient_id);
1171 #if DEBUG_DV_MESSAGES
1172 toSend->uid = htonl(uid);
1174 toSend->uid = htonl(0);
1177 #if DEBUG_DV_PEER_NUMBERS
1178 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1179 encPeerTo.encoding[4] = '\0';
1180 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Sending DATA message. Sender id %u, source %s, destination %s, via %s\n", GNUNET_i2s(&my_identity), sender_id, &encPeerFrom, &encPeerTo, &encPeerVia);
1182 memcpy (&toSend[1], message, message_size);
1183 if ((source != NULL) && (source->pkey == NULL)) /* Test our hypothesis about message failures! */
1185 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: Sending message, but anticipate recipient will not know sender!!!\n\n\n", my_short_id);
1187 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send size %d to destination `%s'\n", "DV SEND MESSAGE", msg_size, GNUNET_i2s(recipient));
1195 GNUNET_SCHEDULER_add_now(try_core_send, NULL);
1200 struct CheckPeerContext
1205 struct DistantNeighbor *peer;
1208 * Sender id to search for
1210 unsigned int sender_id;
1214 * Iterator over hash map entries.
1216 * @param cls closure
1217 * @param key current key code
1218 * @param value value in the hash map
1219 * @return GNUNET_YES if we should continue to
1223 int checkPeerID (void *cls,
1224 const GNUNET_HashCode * key,
1227 struct CheckPeerContext *ctx = cls;
1228 struct DistantNeighbor *distant = value;
1230 if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1232 ctx->peer = distant;
1242 * Handler for messages parsed out by the tokenizer from
1243 * DV DATA received for this peer.
1246 * @param client the TokenizedMessageContext which contains message information
1247 * @param message the actual message
1249 void tokenized_message_handler (void *cls,
1251 const struct GNUNET_MessageHeader *message)
1253 struct TokenizedMessageContext *ctx = client;
1254 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1255 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1256 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1257 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1259 #if DEBUG_DV_MESSAGES
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "%s: Receives %s message for me, uid %u, size %d, type %d cost %u from %s!\n", my_short_id, "DV DATA", ctx->uid, ntohs(message->size), ntohs(message->type), ctx->distant->cost, GNUNET_i2s(&ctx->distant->identity));
1263 GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1264 send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1269 struct DelayedMessageContext
1271 struct GNUNET_PeerIdentity dest;
1272 struct GNUNET_PeerIdentity sender;
1273 struct GNUNET_MessageHeader *message;
1274 size_t message_size;
1278 void send_message_delayed (void *cls,
1279 const struct GNUNET_SCHEDULER_TaskContext *tc)
1281 struct DelayedMessageContext *msg_ctx = cls;
1282 if (msg_ctx != NULL)
1284 send_message(&msg_ctx->dest,
1288 msg_ctx->message_size,
1289 default_dv_priority,
1291 GNUNET_TIME_relative_get_forever());
1292 GNUNET_free(msg_ctx->message);
1293 GNUNET_free(msg_ctx);
1299 * Get distance information from 'atsi'.
1301 * @param atsi performance data
1302 * @return connected transport distance
1305 get_atsi_distance (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1307 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
1308 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE) )
1310 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
1313 /* FIXME: we do not have distance data? Assume direct neighbor. */
1314 return DIRECT_NEIGHBOR_COST;
1316 return ntohl (atsi->value);
1320 * Find latency information in 'atsi'.
1322 * @param atsi performance data
1323 * @return connection latency
1325 static struct GNUNET_TIME_Relative
1326 get_atsi_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1328 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
1329 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY) )
1331 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
1334 /* how can we not have latency data? */
1335 return GNUNET_TIME_UNIT_SECONDS;
1337 /* FIXME: Multiply by GNUNET_TIME_UNIT_MILLISECONDS (1) to get as a GNUNET_TIME_Relative */
1338 return GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, ntohl (atsi->value));
1342 * Core handler for dv data messages. Whatever this message
1343 * contains all we really have to do is rip it out of its
1344 * DV layering and give it to our pal the DV plugin to report
1347 * @param cls closure
1348 * @param peer peer which sent the message (immediate sender)
1349 * @param message the message
1350 * @param atsi transport ATS information (latency, distance, etc.)
1353 handle_dv_data_message (void *cls,
1354 const struct GNUNET_PeerIdentity * peer,
1355 const struct GNUNET_MessageHeader * message,
1356 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1358 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1359 const struct GNUNET_MessageHeader *packed_message;
1360 struct DirectNeighbor *dn;
1361 struct DistantNeighbor *pos;
1362 unsigned int sid; /* Sender id */
1363 unsigned int tid; /* Target id */
1364 struct GNUNET_PeerIdentity *original_sender;
1365 struct GNUNET_PeerIdentity *destination;
1366 struct FindDestinationContext fdc;
1367 struct TokenizedMessageContext tkm_ctx;
1371 struct DelayedMessageContext *delayed_context;
1374 struct CheckPeerContext checkPeerCtx;
1376 #if DEBUG_DV_MESSAGES
1380 size_t packed_message_size;
1382 uint32_t distance; /* Distance information */
1383 struct GNUNET_TIME_Relative latency; /* Latency information */
1385 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "%s: Receives DATA message from %s size %d, packed size %d!\n", my_short_id, GNUNET_i2s(peer) , ntohs(incoming->header.size), packed_message_size);
1391 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "`%s': Message sizes don't add up, total size %u, expected at least %u!\n", "dv service", ntohs(incoming->header.size), sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader));
1397 return GNUNET_SYSERR;
1400 /* Iterate over ATS_Information to get distance and latency */
1401 latency = get_atsi_latency(atsi);
1402 distance = get_atsi_distance(atsi);
1403 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1408 sid = ntohl (incoming->sender);
1412 checkPeerCtx.sender_id = sid;
1413 checkPeerCtx.peer = NULL;
1414 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1415 pos = checkPeerCtx.peer;
1419 pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1423 pos = dn->referee_head;
1424 while ((NULL != pos) && (pos->referrer_id != sid))
1430 #if DEBUG_DV_MESSAGES
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "%s: unknown sender (%u), Message uid %u from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1433 pos = dn->referee_head;
1434 while ((NULL != pos) && (pos->referrer_id != sid))
1436 sender_id = strdup(GNUNET_i2s(&pos->identity));
1437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1438 GNUNET_free(sender_id);
1444 for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
1446 if (dn->pending_messages[i].sender_id == 0)
1453 if (found_pos == -1)
1455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1456 "%s: Too many unknown senders (%u), ignoring message! Message uid %llu from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1460 dn->pending_messages[found_pos].message = GNUNET_malloc(ntohs (message->size));
1461 memcpy(dn->pending_messages[found_pos].message, message, ntohs(message->size));
1462 dn->pending_messages[found_pos].distance = distance;
1463 dn->pending_messages[found_pos].latency = latency;
1464 memcpy(&dn->pending_messages[found_pos].sender, peer, sizeof(struct GNUNET_PeerIdentity));
1465 dn->pending_messages[found_pos].sender_id = sid;
1467 /* unknown sender */
1470 original_sender = &pos->identity;
1471 tid = ntohl (incoming->recipient);
1475 cbuf = (char *)&incoming[1];
1477 tkm_ctx.peer = peer;
1478 tkm_ctx.distant = pos;
1479 tkm_ctx.uid = ntohl(incoming->uid);
1480 if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1483 packed_message_size,
1488 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1494 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1497 /* FIXME: this is the *only* per-request operation we have in DV
1498 that is O(n) in relation to the number of connected peers; a
1499 hash-table lookup could easily solve this (minor performance
1503 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1504 &find_destination, &fdc);
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1511 if (fdc.dest == NULL)
1513 #if DEBUG_DV_MESSAGES
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1519 destination = &fdc.dest->identity;
1521 if (0 == memcmp (destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1523 /* FIXME: create stat: routing loop-discard! */
1525 #if DEBUG_DV_MESSAGES
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "%s: DROPPING MESSAGE uid %u type %d, routing loop! Message immediately from %s!\n", my_short_id, ntohl(incoming->uid), ntohs(packed_message->type), GNUNET_i2s(&dn->identity));
1532 /* At this point we have a message, and we need to forward it on to the
1535 #if DEBUG_DV_MESSAGES
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "%s: FORWARD %s message for %s, uid %u, size %d type %d, cost %u!\n", my_short_id, "DV DATA", GNUNET_i2s(destination), ntohl(incoming->uid), ntohs(packed_message->size), ntohs(packed_message->type), pos->cost);
1541 if (GNUNET_TIME_absolute_get_duration(pos->last_gossip).abs_value < GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2).abs_value)
1543 delayed_context = GNUNET_malloc(sizeof(struct DelayedMessageContext));
1544 memcpy(&delayed_context->dest, destination, sizeof(struct GNUNET_PeerIdentity));
1545 memcpy(&delayed_context->sender, original_sender, sizeof(struct GNUNET_PeerIdentity));
1546 delayed_context->message = GNUNET_malloc(packed_message_size);
1547 memcpy(delayed_context->message, packed_message, packed_message_size);
1548 delayed_context->message_size = packed_message_size;
1549 delayed_context->uid = ntohl(incoming->uid);
1550 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 2500), &send_message_delayed, delayed_context);
1556 ret = send_message(destination,
1560 packed_message_size,
1561 default_dv_priority,
1562 ntohl(incoming->uid),
1563 GNUNET_TIME_relative_get_forever());
1565 if (ret != GNUNET_SYSERR)
1569 #if DEBUG_MESSAGE_DROP
1570 char *direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "%s: DROPPING MESSAGE type %d, forwarding failed! Message immediately from %s!\n", GNUNET_i2s(&my_identity), ntohs(((struct GNUNET_MessageHeader *)&incoming[1])->type), direct_id);
1573 GNUNET_free (direct_id);
1575 return GNUNET_SYSERR;
1581 * Iterator over hash map entries.
1583 * @param cls closure (NULL)
1584 * @param key current key code
1585 * @param value value in the hash map (DistantNeighbor)
1586 * @return GNUNET_YES if we should continue to
1590 int print_neighbors (void *cls,
1591 const GNUNET_HashCode * key,
1594 struct DistantNeighbor *distant_neighbor = abs_value;
1595 char my_shortname[5];
1596 char referrer_shortname[5];
1597 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1598 my_shortname[4] = '\0';
1599 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1600 referrer_shortname[4] = '\0';
1602 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`%s' %s: Peer `%s', distance %d, referrer `%s' pkey: %s\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname, distant_neighbor->pkey == NULL ? "no" : "yes");
1608 * Scheduled task which gossips about known direct peers to other connected
1609 * peers. Will run until called with reason shutdown.
1612 neighbor_send_task (void *cls,
1613 const struct GNUNET_SCHEDULER_TaskContext *tc)
1615 struct NeighborSendContext *send_context = cls;
1616 #if DEBUG_DV_GOSSIP_SEND
1617 char * encPeerAbout;
1620 struct DistantNeighbor *about;
1621 struct DirectNeighbor *to;
1622 struct FastGossipNeighborList *about_list;
1624 p2p_dv_MESSAGE_NeighborInfo *message;
1625 struct PendingMessage *pending_message;
1627 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "%s: Called with reason shutdown, shutting down!\n",
1632 GNUNET_i2s(&my_identity));
1637 if (send_context->fast_gossip_list_head != NULL)
1639 about_list = send_context->fast_gossip_list_head;
1640 about = about_list->about;
1641 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1642 send_context->fast_gossip_list_tail,
1644 GNUNET_free(about_list);
1648 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1649 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1650 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1651 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1652 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1653 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1654 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1655 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1656 * asynchronously enough to avoid major problems (-;
1658 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1660 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1662 to = send_context->toNeighbor;
1664 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1666 (about->hidden == GNUNET_NO) &&
1669 (0 != memcmp (&about->identity,
1670 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1671 (about->pkey != NULL))
1673 #if DEBUG_DV_GOSSIP_SEND
1674 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1675 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1678 GNUNET_i2s(&my_identity),
1679 encPeerAbout, about->our_id, encPeerTo);
1680 GNUNET_free(encPeerAbout);
1681 GNUNET_free(encPeerTo);
1683 about->last_gossip = GNUNET_TIME_absolute_get();
1684 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1685 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1686 pending_message->importance = default_dv_priority;
1687 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1688 memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1689 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1690 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1691 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1692 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1693 message->cost = htonl (about->cost);
1694 message->neighbor_id = htonl (about->our_id);
1696 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1697 memcpy (&message->neighbor,
1698 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1700 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1705 GNUNET_SCHEDULER_add_now(try_core_send, NULL);
1706 /*if (core_transmit_handle == NULL)
1707 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, GNUNET_TIME_relative_get_forever(), &to->identity, sizeof(p2p_dv_MESSAGE_NeighborInfo), &core_transmit_notify, NULL);*/
1711 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1713 #if DEBUG_DV_PEER_NUMBERS
1714 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1716 send_context->task = GNUNET_SCHEDULER_add_now(&neighbor_send_task, send_context);
1720 #if DEBUG_DV_PEER_NUMBERS
1721 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1723 send_context->task = GNUNET_SCHEDULER_add_delayed(GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1731 * Handle START-message. This is the first message sent to us
1732 * by the client (can only be one!).
1734 * @param cls closure (always NULL)
1735 * @param client identification of the client
1736 * @param message the actual message
1739 handle_start (void *cls,
1740 struct GNUNET_SERVER_Client *client,
1741 const struct GNUNET_MessageHeader *message)
1745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1746 "Received `%s' request from client\n", "START");
1749 client_handle = client;
1751 GNUNET_SERVER_client_keep(client_handle);
1752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1757 * Iterate over hash map entries for a distant neighbor,
1758 * if direct neighbor matches context call send message
1760 * @param cls closure, a DV_SendContext
1761 * @param key current key code
1762 * @param value value in the hash map
1763 * @return GNUNET_YES if we should continue to
1767 int send_iterator (void *cls,
1768 const GNUNET_HashCode * key,
1771 struct DV_SendContext *send_context = cls;
1772 struct DistantNeighbor *distant_neighbor = abs_value;
1774 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1776 send_message_via(&my_identity, distant_neighbor, send_context);
1784 * Service server's handler for message send requests (which come
1785 * bubbling up to us through the DV plugin).
1787 * @param cls closure
1788 * @param client identification of the client
1789 * @param message the actual message
1791 void handle_dv_send_message (void *cls,
1792 struct GNUNET_SERVER_Client * client,
1793 const struct GNUNET_MessageHeader * message)
1795 struct GNUNET_DV_SendMessage *send_msg;
1796 struct GNUNET_DV_SendResultMessage *send_result_msg;
1797 struct PendingMessage *pending_message;
1799 size_t message_size;
1800 struct GNUNET_PeerIdentity *destination;
1801 struct GNUNET_PeerIdentity *direct;
1802 struct GNUNET_MessageHeader *message_buf;
1805 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1806 struct DV_SendContext *send_context;
1807 #if DEBUG_DV_MESSAGES
1809 struct GNUNET_MessageHeader *packed_message;
1812 if (client_handle == NULL)
1814 client_handle = client;
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1818 else if (client_handle != client)
1820 client_handle = client;
1821 /* What should we do in this case, assert fail or just log the warning? */
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 "%s: Setting client handle (was a different client!)!\n", "dv");
1828 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1829 send_msg = (struct GNUNET_DV_SendMessage *)message;
1831 address_len = ntohl(send_msg->addrlen);
1832 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1833 message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1834 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1835 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1836 message_buf = GNUNET_malloc(message_size);
1838 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1839 offset = 0; /* Offset starts at zero */
1841 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1842 offset += sizeof(struct GNUNET_PeerIdentity);
1844 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1845 offset += sizeof(struct GNUNET_PeerIdentity);
1848 memcpy(message_buf, &temp_pos[offset], message_size);
1849 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1851 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1852 dest_hash.encoding[4] = '\0';
1853 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: asked to send message to `%s', but address is for `%s'!", "DV SERVICE", GNUNET_i2s(&send_msg->target), (const char *)&dest_hash.encoding);
1856 #if DEBUG_DV_MESSAGES
1857 cbuf = (char *)message_buf;
1859 while(offset < message_size)
1861 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1862 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(packed_message->type), GNUNET_i2s(destination));
1863 offset += ntohs(packed_message->size);
1865 /*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));*/
1867 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1868 dest_hash.encoding[4] = '\0';
1869 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1871 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1872 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1873 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1874 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1876 send_context->importance = ntohl(send_msg->priority);
1877 send_context->timeout = send_msg->timeout;
1878 send_context->direct_peer = direct;
1879 send_context->distant_peer = destination;
1880 send_context->message = message_buf;
1881 send_context->message_size = message_size;
1882 send_context->send_result = send_result_msg;
1883 #if DEBUG_DV_MESSAGES
1884 send_context->uid = send_msg->uid;
1887 if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1889 send_result_msg->result = htons(1);
1890 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1891 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1892 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1893 GNUNET_free(send_result_msg);
1895 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1897 if (client_handle != NULL)
1899 if (plugin_transmit_handle == NULL)
1901 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1902 sizeof(struct GNUNET_DV_SendResultMessage),
1903 GNUNET_TIME_UNIT_FOREVER_REL,
1904 &transmit_to_plugin, NULL);
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1911 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1912 dest_hash.encoding[4] = '\0';
1913 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s DV SEND failed to send message to destination `%s' via `%s'\n", my_short_id, (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1916 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1918 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1920 send_result_msg->result = htons(1);
1921 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1922 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1923 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1924 GNUNET_free(send_result_msg);
1926 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1928 if (client_handle != NULL)
1930 if (plugin_transmit_handle == NULL)
1932 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1933 sizeof(struct GNUNET_DV_SendResultMessage),
1934 GNUNET_TIME_UNIT_FOREVER_REL,
1935 &transmit_to_plugin, NULL);
1939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1942 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1943 dest_hash.encoding[4] = '\0';
1944 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s DV SEND failed to send message to destination `%s' via `%s'\n", my_short_id, (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1947 GNUNET_free(message_buf);
1948 GNUNET_free(send_context);
1949 GNUNET_free(direct);
1950 GNUNET_free(destination);
1952 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1955 /** Forward declarations **/
1956 static int handle_dv_gossip_message (void *cls,
1957 const struct GNUNET_PeerIdentity *peer,
1958 const struct GNUNET_MessageHeader *message,
1959 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
1961 static int handle_dv_disconnect_message (void *cls,
1962 const struct GNUNET_PeerIdentity *peer,
1963 const struct GNUNET_MessageHeader *message,
1964 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
1965 /** End forward declarations **/
1969 * List of handlers for the messages understood by this
1972 * Hmm... will we need to register some handlers with core and
1973 * some handlers with our server here? Because core should be
1974 * getting the incoming DV messages (from whichever lower level
1975 * transport) and then our server should be getting messages
1976 * from the dv_plugin, right?
1978 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1979 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1980 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1981 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1985 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1986 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1987 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1992 * Free a DistantNeighbor node, including removing it
1993 * from the referer's list.
1996 distant_neighbor_free (struct DistantNeighbor *referee)
1998 struct DirectNeighbor *referrer;
2000 referrer = referee->referrer;
2001 if (referrer != NULL)
2003 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
2004 referrer->referee_tail, referee);
2006 GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
2007 GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
2008 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
2009 &referee->identity.hashPubKey);
2010 GNUNET_free_non_null (referee->pkey);
2011 GNUNET_free (referee);
2015 * Free a DirectNeighbor node, including removing it
2016 * from the referer's list.
2019 direct_neighbor_free (struct DirectNeighbor *direct)
2021 struct NeighborSendContext *send_context;
2022 struct FastGossipNeighborList *about_list;
2023 struct FastGossipNeighborList *prev_about;
2025 send_context = direct->send_context;
2027 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2028 GNUNET_SCHEDULER_cancel(send_context->task);
2030 about_list = send_context->fast_gossip_list_head;
2031 while (about_list != NULL)
2033 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
2034 prev_about = about_list;
2035 about_list = about_list->next;
2036 GNUNET_free(prev_about);
2038 GNUNET_free(send_context);
2039 GNUNET_free(direct);
2043 * Multihashmap iterator for sending out disconnect messages
2046 * @param cls the peer that was disconnected
2047 * @param key key value stored under
2048 * @param value the direct neighbor to send disconnect to
2050 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2052 static int schedule_disconnect_messages (void *cls,
2053 const GNUNET_HashCode * key,
2056 struct DisconnectContext *disconnect_context = cls;
2057 struct DirectNeighbor *disconnected = disconnect_context->direct;
2058 struct DirectNeighbor *notify = value;
2059 struct PendingMessage *pending_message;
2060 p2p_dv_MESSAGE_Disconnect *disconnect_message;
2062 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2063 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
2065 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
2066 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
2067 pending_message->importance = default_dv_priority;
2068 pending_message->timeout = GNUNET_TIME_relative_get_forever();
2069 memcpy(&pending_message->recipient, ¬ify->identity, sizeof(struct GNUNET_PeerIdentity));
2070 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
2071 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
2072 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
2073 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
2074 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
2076 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
2081 GNUNET_SCHEDULER_add_now(try_core_send, NULL);
2082 /*if (core_transmit_handle == NULL)
2083 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, GNUNET_TIME_relative_get_forever(), ¬ify->identity, sizeof(p2p_dv_MESSAGE_Disconnect), &core_transmit_notify, NULL);*/
2089 * Multihashmap iterator for freeing extended neighbors.
2092 * @param key key value stored under
2093 * @param value the distant neighbor to be freed
2095 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2097 static int free_extended_neighbors (void *cls,
2098 const GNUNET_HashCode * key,
2101 struct DistantNeighbor *distant = value;
2102 distant_neighbor_free(distant);
2107 * Multihashmap iterator for freeing direct neighbors.
2110 * @param key key value stored under
2111 * @param value the direct neighbor to be freed
2113 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2115 static int free_direct_neighbors (void *cls,
2116 const GNUNET_HashCode * key,
2119 struct DirectNeighbor *direct = value;
2120 direct_neighbor_free(direct);
2126 * Task run during shutdown.
2132 shutdown_task (void *cls,
2133 const struct GNUNET_SCHEDULER_TaskContext *tc)
2136 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2137 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2139 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2140 GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2141 GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2142 GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2144 GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2145 GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2147 GNUNET_CORE_disconnect (coreAPI);
2149 GNUNET_PEERINFO_disconnect(peerinfo_handle);
2150 GNUNET_SERVER_mst_destroy(coreMST);
2151 GNUNET_free_non_null(my_short_id);
2153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2158 * To be called on core init/fail.
2160 void core_init (void *cls,
2161 struct GNUNET_CORE_Handle * server,
2162 const struct GNUNET_PeerIdentity *identity,
2163 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2168 GNUNET_SCHEDULER_cancel(cleanup_task);
2169 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2174 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2176 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2177 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2182 #if PKEY_NO_NEIGHBOR_ON_ADD
2184 * Iterator over hash map entries.
2186 * @param cls closure
2187 * @param key current key code
2188 * @param value value in the hash map
2189 * @return GNUNET_YES if we should continue to
2193 static int add_pkey_to_extended (void *cls,
2194 const GNUNET_HashCode * key,
2197 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2198 struct DistantNeighbor *distant_neighbor = abs_value;
2200 if (distant_neighbor->pkey == NULL)
2202 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2203 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2211 * Iterator over hash map entries.
2213 * @param cls closure
2214 * @param key current key code
2215 * @param value value in the hash map
2216 * @return GNUNET_YES if we should continue to
2220 static int update_matching_neighbors (void *cls,
2221 const GNUNET_HashCode * key,
2224 struct NeighborUpdateInfo * update_info = cls;
2225 struct DistantNeighbor *distant_neighbor = value;
2227 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2229 /* same referrer, cost change! */
2230 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2231 update_info->neighbor->max_loc, update_info->cost);
2232 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2233 update_info->neighbor->min_loc, update_info->cost);
2234 update_info->neighbor->last_activity = update_info->now;
2235 update_info->neighbor->cost = update_info->cost;
2236 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2245 * Iterate over all current direct peers, add DISTANT newly connected
2246 * peer to the fast gossip list for that peer so we get DV routing
2247 * information out as fast as possible!
2249 * @param cls the newly connected neighbor we will gossip about
2250 * @param key the hashcode of the peer
2251 * @param value the direct neighbor we should gossip to
2253 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2255 static int add_distant_all_direct_neighbors (void *cls,
2256 const GNUNET_HashCode * key,
2259 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2260 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2261 struct NeighborSendContext *send_context = direct->send_context;
2262 struct FastGossipNeighborList *gossip_entry;
2268 if (distant == NULL)
2273 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2275 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2279 if (distant->hidden == GNUNET_YES)
2280 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2282 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2283 gossip_entry->about = distant;
2285 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2286 send_context->fast_gossip_list_tail,
2287 send_context->fast_gossip_list_tail,
2290 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2291 encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2294 GNUNET_i2s(&my_identity),
2295 encPeerAbout, distant->our_id, encPeerTo);
2296 GNUNET_free(encPeerAbout);
2297 GNUNET_free(encPeerTo);
2299 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2300 GNUNET_SCHEDULER_cancel(send_context->task);*/
2302 send_context->task = GNUNET_SCHEDULER_add_now(&neighbor_send_task, send_context);
2307 * Callback for hello address creation.
2309 * @param cls closure, a struct HelloContext
2310 * @param max maximum number of bytes that can be written to buf
2311 * @param buf where to write the address information
2313 * @return number of bytes written, 0 to signal the
2314 * end of the iteration.
2317 generate_hello_address (void *cls, size_t max, void *buf)
2319 struct HelloContext *hello_context = cls;
2325 if (hello_context->addresses_to_add == 0)
2328 /* Hello "address" will be concatenation of distant peer and direct peer identities */
2329 size = 2 * sizeof(struct GNUNET_PeerIdentity);
2330 GNUNET_assert(max >= size);
2332 addr_buffer = GNUNET_malloc(size);
2334 /* Copy the distant peer identity to buffer */
2335 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
2336 offset += sizeof(struct GNUNET_PeerIdentity);
2337 /* Copy the direct peer identity to buffer */
2338 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
2339 ret = GNUNET_HELLO_add_address ("dv",
2340 GNUNET_TIME_relative_to_absolute
2341 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2344 hello_context->addresses_to_add--;
2346 GNUNET_free(addr_buffer);
2352 * Handles when a peer is either added due to being newly connected
2353 * or having been gossiped about, also called when the cost for a neighbor
2354 * needs to be updated.
2356 * @param peer identity of the peer whose info is being added/updated
2357 * @param pkey public key of the peer whose info is being added/updated
2358 * @param referrer_peer_id id to use when sending to 'peer'
2359 * @param referrer if this is a gossiped peer, who did we hear it from?
2360 * @param cost the cost of communicating with this peer via 'referrer'
2362 * @return the added neighbor, the updated neighbor or NULL (neighbor
2365 static struct DistantNeighbor *
2366 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer,
2367 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2368 unsigned int referrer_peer_id,
2369 struct DirectNeighbor *referrer, unsigned int cost)
2371 struct DistantNeighbor *neighbor;
2372 struct DistantNeighbor *max;
2373 struct GNUNET_TIME_Absolute now;
2374 struct NeighborUpdateInfo *neighbor_update;
2375 struct HelloContext *hello_context;
2376 struct GNUNET_HELLO_Message *hello_msg;
2377 unsigned int our_id;
2382 #if DEBUG_DV_PEER_NUMBERS
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2388 now = GNUNET_TIME_absolute_get ();
2389 neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2391 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2392 neighbor_update->neighbor = neighbor;
2393 neighbor_update->cost = cost;
2394 neighbor_update->now = now;
2395 neighbor_update->referrer = referrer;
2396 neighbor_update->referrer_peer_id = referrer_peer_id;
2398 if (neighbor != NULL)
2401 memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2403 our_id = neighbor->our_id;
2409 memcpy(&our_id, peer, sizeof(unsigned int));
2411 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2415 /* Either we do not know this peer, or we already do but via a different immediate peer */
2416 if ((neighbor == NULL) ||
2417 (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2419 &update_matching_neighbors,
2420 neighbor_update) != GNUNET_SYSERR))
2424 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2426 distant_neighbor_free(neighbor);
2428 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2434 if (cost > fisheye_depth)
2437 GNUNET_free(neighbor_update);
2441 #if DEBUG_DV_PEER_NUMBERS
2442 encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2445 GNUNET_free(encAbout);
2448 if (max_table_size <=
2449 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2451 /* remove most expensive entry */
2452 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2453 GNUNET_assert(max != NULL);
2454 if (cost > max->cost)
2456 /* new entry most expensive, don't create */
2457 GNUNET_free(neighbor_update);
2462 /* only free if this is not a direct connection;
2463 we could theoretically have more direct
2464 connections than DV entries allowed total! */
2465 distant_neighbor_free (max);
2469 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2470 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2471 referrer->referee_tail, neighbor);
2472 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2474 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2476 neighbor->referrer = referrer;
2477 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2478 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2480 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2481 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2484 neighbor->pkey = pkey;
2486 neighbor->last_activity = now;
2487 neighbor->cost = cost;
2488 neighbor->referrer_id = referrer_peer_id;
2489 neighbor->our_id = our_id;
2491 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2494 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2496 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2497 if (referrer_peer_id != 0)
2499 for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
2501 if (referrer->pending_messages[i].sender_id == referrer_peer_id) /* We have a queued message from just learned about peer! */
2503 #if DEBUG_DV_MESSAGES
2504 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: learned about peer %llu from which we have a previous unknown message, processing!\n", my_short_id, referrer_peer_id);
2506 struct GNUNET_TRANSPORT_ATS_Information atsi[3];
2507 atsi[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2508 atsi[0].value = htonl (referrer->pending_messages[i].distance);
2509 atsi[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2510 atsi[1].value = htonl ((uint32_t)referrer->pending_messages[i].latency.rel_value);
2511 atsi[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2512 atsi[2].value = htonl (0);
2513 handle_dv_data_message(NULL,
2514 &referrer->pending_messages[i].sender,
2515 referrer->pending_messages[i].message,
2516 (const struct GNUNET_TRANSPORT_ATS_Information *)&atsi);
2517 GNUNET_free(referrer->pending_messages[i].message);
2518 referrer->pending_messages[i].sender_id = 0;
2522 if ((cost != DIRECT_NEIGHBOR_COST) && (neighbor->pkey != NULL))
2524 /* Added neighbor, now send HELLO to transport */
2525 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2526 hello_context->direct_peer = &referrer->identity;
2527 memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2528 hello_context->addresses_to_add = 1;
2529 hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2530 GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2531 addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2532 addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2534 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: GIVING HELLO size %d for %s via %s to TRANSPORT\n", my_short_id, GNUNET_HELLO_size(hello_msg), addr2, addr1);
2538 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2539 GNUNET_free(hello_context);
2540 GNUNET_free(hello_msg);
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2548 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2556 GNUNET_free(neighbor_update);
2562 * Core handler for dv disconnect messages. These will be used
2563 * by us to tell transport via the dv plugin that a peer can
2564 * no longer be contacted by us via a certain address. We should
2565 * then propagate these messages on, given that the distance to
2566 * the peer indicates we would have gossiped about it to others.
2568 * @param cls closure
2569 * @param peer peer which sent the message (immediate sender)
2570 * @param message the message
2571 * @param atsi performance data
2573 static int handle_dv_disconnect_message (void *cls,
2574 const struct GNUNET_PeerIdentity *peer,
2575 const struct GNUNET_MessageHeader *message,
2576 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2578 struct DirectNeighbor *referrer;
2579 struct DistantNeighbor *distant;
2580 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2582 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2584 return GNUNET_SYSERR; /* invalid message */
2587 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2589 if (referrer == NULL)
2592 distant = referrer->referee_head;
2593 while (distant != NULL)
2595 if (distant->referrer_id == ntohl(enc_message->peer_id))
2597 distant_neighbor_free(distant);
2598 distant = referrer->referee_head;
2601 distant = distant->next;
2609 * Core handler for dv gossip messages. These will be used
2610 * by us to create a HELLO message for the newly peer containing
2611 * which direct peer we can connect through, and what the cost
2612 * is. This HELLO will then be scheduled for validation by the
2613 * transport service so that it can be used by all others.
2615 * @param cls closure
2616 * @param peer peer which sent the message (immediate sender)
2617 * @param message the message
2618 * @param atsi performance data
2621 handle_dv_gossip_message (void *cls,
2622 const struct GNUNET_PeerIdentity *peer,
2623 const struct GNUNET_MessageHeader *message,
2624 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2626 struct DirectNeighbor *referrer;
2627 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2629 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2631 return GNUNET_SYSERR; /* invalid message */
2634 #if DEBUG_DV_GOSSIP_RECEIPT
2635 char * encPeerAbout;
2638 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2639 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "%s: Received %s message from peer %s about peer %s id %u distance %d!\n", GNUNET_i2s(&my_identity), "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl(enc_message->neighbor_id), ntohl (enc_message->cost) + 1);
2642 GNUNET_free(encPeerAbout);
2643 GNUNET_free(encPeerFrom);
2646 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2648 if (referrer == NULL)
2651 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2652 ntohl (enc_message->neighbor_id),
2653 referrer, ntohl (enc_message->cost) + 1);
2660 * Iterate over all currently known peers, add them to the
2661 * fast gossip list for this peer so we get DV routing information
2662 * out as fast as possible!
2664 * @param cls the direct neighbor we will gossip to
2665 * @param key the hashcode of the peer
2666 * @param value the distant neighbor we should add to the list
2668 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2671 add_all_extended_peers (void *cls,
2672 const GNUNET_HashCode * key,
2675 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2676 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2677 struct FastGossipNeighborList *gossip_entry;
2679 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2680 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2683 if (distant->hidden == GNUNET_YES)
2684 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2686 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2687 gossip_entry->about = distant;
2689 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2690 send_context->fast_gossip_list_tail,
2691 send_context->fast_gossip_list_tail,
2699 * Iterator over hash map entries.
2701 * @param cls closure
2702 * @param key current key code
2703 * @param value value in the hash map
2704 * @return GNUNET_YES if we should continue to
2709 gossip_all_to_all_iterator (void *cls,
2710 const GNUNET_HashCode * key,
2713 struct DirectNeighbor *direct = abs_value;
2715 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2717 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2718 GNUNET_SCHEDULER_cancel(direct->send_context->task);
2720 direct->send_context->task = GNUNET_SCHEDULER_add_now(&neighbor_send_task, direct->send_context);
2725 * Task run during shutdown.
2731 gossip_all_to_all (void *cls,
2732 const struct GNUNET_SCHEDULER_TaskContext *tc)
2734 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2736 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2743 * Iterate over all current direct peers, add newly connected peer
2744 * to the fast gossip list for that peer so we get DV routing
2745 * information out as fast as possible!
2747 * @param cls the newly connected neighbor we will gossip about
2748 * @param key the hashcode of the peer
2749 * @param value the direct neighbor we should gossip to
2751 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2754 add_all_direct_neighbors (void *cls,
2755 const GNUNET_HashCode * key,
2758 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2759 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2760 struct DistantNeighbor *distant;
2761 struct NeighborSendContext *send_context = direct->send_context;
2762 struct FastGossipNeighborList *gossip_entry;
2766 distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2767 if (distant == NULL)
2772 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2774 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2778 if (distant->hidden == GNUNET_YES)
2779 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2781 direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2783 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: adding peer %s to fast send list for %s\n", my_short_id, GNUNET_i2s(&distant->identity), direct_id);
2785 GNUNET_free(direct_id);
2786 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2787 gossip_entry->about = distant;
2789 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2790 send_context->fast_gossip_list_tail,
2791 send_context->fast_gossip_list_tail,
2793 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2794 GNUNET_SCHEDULER_cancel(send_context->task);
2796 send_context->task = GNUNET_SCHEDULER_add_now(&neighbor_send_task, send_context);
2797 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2798 //neighbor_send_task(send_context, &tc);
2803 * Type of an iterator over the hosts. Note that each
2804 * host will be called with each available protocol.
2806 * @param cls closure
2807 * @param peer id of the peer, NULL for last call
2808 * @param hello hello message for the peer (can be NULL)
2809 * @param err_msg NULL if successful, otherwise contains error message
2812 process_peerinfo (void *cls,
2813 const struct GNUNET_PeerIdentity *peer,
2814 const struct GNUNET_HELLO_Message *hello,
2815 const char *err_msg)
2817 struct PeerIteratorContext *peerinfo_iterator = cls;
2818 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2819 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2820 #if DEBUG_DV_PEER_NUMBERS
2824 if (err_msg != NULL)
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2827 _("Error in communication with PEERINFO service\n"));
2832 if (distant->pkey == NULL)
2835 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2837 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2838 &peerinfo_iterator->neighbor->identity,
2839 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2845 GNUNET_free(peerinfo_iterator);
2850 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2853 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2855 if (distant->pkey == NULL)
2857 distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2858 memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2861 sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2864 GNUNET_STATISTICS_update (stats, "# distant peers gossiped to direct neighbors", sent, GNUNET_NO);
2866 #if DEBUG_DV_PEER_NUMBERS
2867 neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2868 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2870 sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2873 GNUNET_STATISTICS_update (stats, "# direct peers gossiped to direct neighbors", sent, GNUNET_NO);
2875 #if DEBUG_DV_PEER_NUMBERS
2876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2877 GNUNET_free(neighbor_pid);
2879 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(&neighbor_send_task, neighbor->send_context);
2885 * Method called whenever a peer connects.
2887 * @param cls closure
2888 * @param peer peer identity this notification is about
2889 * @param atsi performance data
2892 handle_core_connect (void *cls,
2893 const struct GNUNET_PeerIdentity * peer,
2894 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2896 struct DirectNeighbor *neighbor;
2897 struct DistantNeighbor *about;
2898 struct PeerIteratorContext *peerinfo_iterator;
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2902 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2906 /* Check for connect to self message */
2907 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
2910 distance = get_atsi_distance (atsi);
2911 if ((distance == DIRECT_NEIGHBOR_COST) &&
2912 (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2914 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2915 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2916 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2917 neighbor->send_context->toNeighbor = neighbor;
2918 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2920 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2922 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2923 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2924 peerinfo_iterator->distant = about;
2925 peerinfo_iterator->neighbor = neighbor;
2926 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2928 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2932 if ((about != NULL) && (about->pkey == NULL))
2935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2938 else if (about != NULL)
2940 GNUNET_free(peerinfo_iterator);
2945 about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2946 if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2948 sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2951 GNUNET_STATISTICS_update (stats, "# direct peers gossiped to new direct neighbors", sent, GNUNET_NO);
2955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2956 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2963 * Method called whenever a given peer disconnects.
2965 * @param cls closure
2966 * @param peer peer identity this notification is about
2968 void handle_core_disconnect (void *cls,
2969 const struct GNUNET_PeerIdentity * peer)
2971 struct DirectNeighbor *neighbor;
2972 struct DistantNeighbor *referee;
2973 struct FindDestinationContext fdc;
2974 struct DisconnectContext disconnect_context;
2975 struct PendingMessage *pending_pos;
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2979 "%s: Receives core peer disconnect message!\n", "dv");
2982 /* Check for disconnect from self message */
2983 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
2987 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2989 if (neighbor == NULL)
2994 pending_pos = core_pending_head;
2995 while (NULL != pending_pos)
2997 if (0 == memcmp(&pending_pos->recipient, &neighbor->identity, sizeof(struct GNUNET_PeerIdentity)))
2999 GNUNET_CONTAINER_DLL_remove(core_pending_head, core_pending_tail, pending_pos);
3000 pending_pos = core_pending_head;
3003 pending_pos = pending_pos->next;
3006 while (NULL != (referee = neighbor->referee_head))
3007 distant_neighbor_free (referee);
3012 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
3014 if (fdc.dest != NULL)
3016 disconnect_context.direct = neighbor;
3017 disconnect_context.distant = fdc.dest;
3018 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
3021 GNUNET_assert (neighbor->referee_tail == NULL);
3022 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
3023 &peer->hashPubKey, neighbor))
3027 if ((neighbor->send_context != NULL) &&
3028 (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
3029 GNUNET_SCHEDULER_cancel(neighbor->send_context->task);
3030 GNUNET_free (neighbor);
3035 * Process dv requests.
3037 * @param cls closure
3038 * @param server the initialized server
3039 * @param c configuration to use
3043 struct GNUNET_SERVER_Handle *server,
3044 const struct GNUNET_CONFIGURATION_Handle *c)
3046 unsigned long long max_hosts;
3049 /* FIXME: Read from config, or calculate, or something other than this! */
3050 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
3051 max_table_size = DEFAULT_DV_SIZE;
3052 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
3054 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
3055 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
3057 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
3058 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
3061 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
3062 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
3065 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3067 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
3069 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
3070 extended_neighbors =
3071 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
3073 GNUNET_SERVER_add_handlers (server, plugin_handlers);
3075 GNUNET_CORE_connect (cfg,
3077 NULL, /* FIXME: anything we want to pass around? */
3079 &handle_core_connect,
3080 &handle_core_disconnect,
3088 if (coreAPI == NULL)
3091 coreMST = GNUNET_SERVER_mst_create (&tokenized_message_handler,
3094 peerinfo_handle = GNUNET_PEERINFO_connect(cfg);
3096 if (peerinfo_handle == NULL)
3098 GNUNET_CORE_disconnect(coreAPI);
3102 /* Scheduled the task to clean up when shutdown is called */
3103 cleanup_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3110 * The main function for the dv service.
3112 * @param argc number of arguments from the command line
3113 * @param argv command line arguments
3114 * @return 0 ok, 1 on error
3117 main (int argc, char *const *argv)
3119 return (GNUNET_OK ==
3120 GNUNET_SERVICE_run (argc,
3123 GNUNET_SERVICE_OPTION_NONE,
3124 &run, NULL)) ? 0 : 1;