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 2, 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
30 * TODO: The gossip rates need to be worked out. Probably many other things
35 #include "gnunet_client_lib.h"
36 #include "gnunet_getopt_lib.h"
37 #include "gnunet_os_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_service_lib.h"
40 #include "gnunet_core_service.h"
41 #include "gnunet_signal_lib.h"
42 #include "gnunet_util_lib.h"
43 #include "gnunet_hello_lib.h"
44 #include "gnunet_peerinfo_service.h"
45 #include "gnunet_crypto_lib.h"
48 #define DEBUG_DV_PEER_NUMBERS GNUNET_YES
51 * DV Service Context stuff goes here...
55 * Handle to the core service api.
57 static struct GNUNET_CORE_Handle *coreAPI;
60 * The identity of our peer.
62 static struct GNUNET_PeerIdentity my_identity;
65 * The configuration for this service.
67 static const struct GNUNET_CONFIGURATION_Handle *cfg;
70 * The scheduler for this service.
72 static struct GNUNET_SCHEDULER_Handle *sched;
75 * How often do we check about sending out more peer information (if
76 * we are connected to no peers previously).
78 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50000)
81 * How long do we wait at most between sending out information?
83 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50000)
86 * How long can we have not heard from a peer and
87 * still have it in our tables?
89 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
92 * Priority for gossip.
94 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
97 * How often should we check if expiration time has elapsed for
100 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
103 * How long to allow a message to be delayed?
105 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
108 * Priority to use for DV data messages.
110 #define DV_PRIORITY 0
113 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
115 #define DIRECT_NEIGHBOR_COST 1
118 * The default number of direct connections to store in DV (max)
120 #define DEFAULT_DIRECT_CONNECTIONS 50
123 * The default size of direct + extended peers in DV (max)
125 #define DEFAULT_DV_SIZE 100
128 * The default fisheye depth, from how many hops away will
131 #define DEFAULT_FISHEYE_DEPTH 4
134 * The client, the DV plugin connected to us. Hopefully
135 * this client will never change, although if the plugin dies
136 * and returns for some reason it may happen.
138 static struct GNUNET_SERVER_Client * client_handle;
141 * Task to run when we shut down, cleaning up all our trash
143 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
146 * Task to run to gossip about peers. Will reschedule itself forever until shutdown!
148 GNUNET_SCHEDULER_TaskIdentifier gossip_task;
151 * Struct where neighbor information is stored.
153 struct DistantNeighbor *referees;
155 static size_t default_dv_priority = 0;
159 * Linked list of messages to send to clients.
161 struct PendingMessage
164 * Pointer to next item in the list
166 struct PendingMessage *next;
169 * Pointer to previous item in the list
171 struct PendingMessage *prev;
173 struct GNUNET_DV_SendResultMessage *send_result;
175 * Actual message to be sent; // avoid allocation
177 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
182 * Transmit handle to the plugin.
184 struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
187 * Head of DLL for client messages
189 struct PendingMessage *plugin_pending_head;
192 * Tail of DLL for client messages
194 struct PendingMessage *plugin_pending_tail;
197 * Handle to the peerinfo service
199 struct GNUNET_PEERINFO_Handle *peerinfo_handle;
202 * Transmit handle to core service.
204 struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
207 * Head of DLL for core messages
209 struct PendingMessage *core_pending_head;
212 * Tail of DLL for core messages
214 struct PendingMessage *core_pending_tail;
217 struct FastGossipNeighborList
220 * Next element of DLL
222 struct FastGossipNeighborList *next;
225 * Prev element of DLL
227 struct FastGossipNeighborList *prev;
230 * The neighbor to gossip about
232 struct DistantNeighbor *about;
236 * Context created whenever a direct peer connects to us,
237 * used to gossip other peers to it.
239 struct NeighborSendContext
242 * The peer we will gossip to.
244 struct DirectNeighbor *toNeighbor;
247 * The task associated with this context.
249 GNUNET_SCHEDULER_TaskIdentifier task;
252 * Head of DLL of peers to gossip about
253 * as fast as possible to this peer, for initial
256 struct FastGossipNeighborList *fast_gossip_list_head;
259 * Tail of DLL of peers to gossip about
260 * as fast as possible to this peer, for initial
263 struct FastGossipNeighborList *fast_gossip_list_tail;
269 * Struct to hold information for updating existing neighbors
271 struct NeighborUpdateInfo
279 * The existing neighbor
281 struct DistantNeighbor *neighbor;
284 * The referrer of the possibly existing peer
286 struct DirectNeighbor *referrer;
289 * The time we heard about this peer
291 struct GNUNET_TIME_Absolute now;
295 * Struct where actual neighbor information is stored,
296 * referenced by min_heap and max_heap. Freeing dealt
297 * with when items removed from hashmap.
299 struct DirectNeighbor
302 * Identity of neighbor.
304 struct GNUNET_PeerIdentity identity;
307 * PublicKey of neighbor.
309 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
312 * Head of DLL of nodes that this direct neighbor referred to us.
314 struct DistantNeighbor *referee_head;
317 * Tail of DLL of nodes that this direct neighbor referred to us.
319 struct DistantNeighbor *referee_tail;
322 * The sending context for gossiping peers to this neighbor.
324 struct NeighborSendContext *send_context;
327 * Is this one of the direct neighbors that we are "hiding"
335 * Struct where actual neighbor information is stored,
336 * referenced by min_heap and max_heap. Freeing dealt
337 * with when items removed from hashmap.
339 struct DistantNeighbor
342 * We keep distant neighbor's of the same referrer in a DLL.
344 struct DistantNeighbor *next;
347 * We keep distant neighbor's of the same referrer in a DLL.
349 struct DistantNeighbor *prev;
354 struct GNUNET_CONTAINER_HeapNode *min_loc;
359 struct GNUNET_CONTAINER_HeapNode *max_loc;
362 * Identity of referrer (next hop towards 'neighbor').
364 struct DirectNeighbor *referrer;
367 * Identity of neighbor.
369 struct GNUNET_PeerIdentity identity;
372 * PublicKey of neighbor.
374 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
377 * Last time we received routing information from this peer
379 struct GNUNET_TIME_Absolute last_activity;
382 * Cost to neighbor, used for actual distance vector computations
387 * Random identifier *we* use for this peer, to be used as shortcut
388 * instead of sending full peer id for each message
393 * Random identifier the *referrer* uses for this peer.
395 unsigned int referrer_id;
398 * Is this one of the direct neighbors that we are "hiding"
405 struct PeerIteratorContext
408 * The actual context, to be freed later.
410 struct GNUNET_PEERINFO_IteratorContext *ic;
413 * The neighbor about which we are concerned.
415 struct DirectNeighbor *neighbor;
420 * Context used for creating hello messages when
421 * gossips are received.
426 * Identity of distant neighbor.
428 struct GNUNET_PeerIdentity distant_peer;
431 * Identity of direct neighbor, via which we send this message.
433 const struct GNUNET_PeerIdentity *direct_peer;
436 * How many addresses do we need to add (always starts at 1, then set to 0)
438 int addresses_to_add;
442 struct DV_SendContext
445 * The distant peer (should always match)
447 struct GNUNET_PeerIdentity *distant_peer;
450 * The direct peer, we need to verify the referrer of.
452 struct GNUNET_PeerIdentity *direct_peer;
455 * The message to be sent
457 struct GNUNET_MessageHeader *message;
460 * The pre-built send result message. Simply needs to be queued
461 * and freed once send has been called!
463 struct GNUNET_DV_SendResultMessage *send_result;
466 * The size of the message being sent, may be larger
467 * than message->header.size because it's multiple
468 * messages packed into one!
473 * How important is this message?
475 unsigned int importance;
478 * Timeout for this message
480 struct GNUNET_TIME_Relative timeout;
486 struct GNUNET_DV_Context
489 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
490 * directly connected peers.
492 struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
495 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
496 * peers connected via DV (extended neighborhood). Does ALSO
497 * include any peers that are in 'direct_neighbors'; for those
498 * peers, the cost will be zero and the referrer all zeros.
500 struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
503 * We use the min heap (min refers to cost) to prefer
504 * gossipping about peers with small costs.
506 struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
509 * We use the max heap (max refers to cost) for general
510 * iterations over all peers and to remove the most costly
511 * connection if we have too many.
513 struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
515 unsigned long long fisheye_depth;
517 unsigned long long max_table_size;
519 unsigned int neighbor_id_loc;
525 static struct GNUNET_DV_Context ctx;
527 struct FindDestinationContext
530 struct DistantNeighbor *dest;
533 struct DisconnectContext
536 * Distant neighbor to get pid from.
538 struct DistantNeighbor *distant;
541 * Direct neighbor that disconnected.
543 struct DirectNeighbor *direct;
547 * We've been given a target ID based on the random numbers that
548 * we assigned to our DV-neighborhood. Find the entry for the
549 * respective neighbor.
552 find_destination (void *cls,
553 struct GNUNET_CONTAINER_HeapNode *node,
554 void *element, GNUNET_CONTAINER_HeapCostType cost)
556 struct FindDestinationContext *fdc = cls;
557 struct DistantNeighbor *dn = element;
559 if (fdc->tid != dn->our_id)
566 * Find a distant peer whose referrer_id matches what we're
567 * looking for. For looking up a peer we've gossipped about
568 * but is now disconnected. Need to do this because we don't
569 * want to remove those that may be accessible via a different
572 static int find_distant_peer (void *cls,
573 const GNUNET_HashCode * key,
576 struct FindDestinationContext *fdc = cls;
577 struct DistantNeighbor *distant = value;
579 if (fdc->tid == distant->referrer_id)
588 * Function called to notify a client about the socket
589 * begin ready to queue more data. "buf" will be
590 * NULL and "size" zero if the socket was closed for
591 * writing in the meantime.
594 * @param size number of bytes available in buf
595 * @param buf where the callee should write the message
596 * @return number of bytes written to buf
598 size_t transmit_to_plugin (void *cls,
599 size_t size, void *buf)
602 struct PendingMessage *reply;
608 /* client disconnected */
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
614 plugin_transmit_handle = NULL;
616 while ( (NULL != (reply = plugin_pending_head)) &&
617 (size >= off + (msize = ntohs (reply->msg->size))))
620 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (plugin) called with size %d\n", "dv service", msize);
622 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
625 memcpy (&cbuf[off], reply->msg, msize);
630 if (plugin_pending_head != NULL)
631 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
632 ntohs(plugin_pending_head->msg->size),
633 GNUNET_TIME_UNIT_FOREVER_REL,
634 &transmit_to_plugin, NULL);
640 * Send a message to the dv plugin.
642 * @param sender the direct sender of the message
643 * @param message the message to send to the plugin
644 * (may be an encapsulated type)
645 * @param message_size the size of the message to be sent
646 * @param distant_neighbor the original sender of the message
647 * @param cost the cost to the original sender of the message
649 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
650 const struct GNUNET_MessageHeader *message,
652 struct GNUNET_PeerIdentity *distant_neighbor,
655 struct GNUNET_DV_MessageReceived *received_msg;
656 struct PendingMessage *pending_message;
658 struct GNUNET_MessageHeader * packed_message_header;
659 struct GNUNET_HELLO_Message *hello_msg;
660 struct GNUNET_PeerIdentity hello_identity;
662 char *sender_address;
663 size_t sender_address_len;
664 char *packed_msg_start;
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
671 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
673 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
674 sender_address = GNUNET_malloc(sender_address_len);
675 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
676 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
680 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
681 sender_address = GNUNET_malloc(sender_address_len);
682 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
685 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
686 received_msg = GNUNET_malloc(size);
687 received_msg->header.size = htons(size);
688 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
689 received_msg->sender_address_len = htonl(sender_address_len);
690 received_msg->distance = htonl(cost);
691 received_msg->msg_len = htonl(message_size);
692 /* Set the sender in this message to be the original sender! */
693 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
694 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
695 memcpy(&received_msg[1], sender_address, sender_address_len);
696 GNUNET_free(sender_address);
697 /* Copy the actual message after the sender */
698 packed_msg_start = (char *)&received_msg[1];
699 packed_msg_start = &packed_msg_start[sender_address_len];
700 memcpy(packed_msg_start, message, message_size);
702 packed_message_header = (struct GNUNET_MessageHeader *)packed_msg_start;
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv service created received message. sender_address_len %lu, packed message len %d, total len %d\n", sender_address_len, ntohl(received_msg->msg_len), ntohs(received_msg->header.size));
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv packed message len %d, type %d\n", ntohs(packed_message_header->size), ntohs(packed_message_header->type));
705 if (ntohs(packed_message_header->type) == GNUNET_MESSAGE_TYPE_HELLO)
707 hello_msg = (struct GNUNET_HELLO_Message *)packed_message_header;
708 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello_msg, &hello_identity));
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity));
712 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
713 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
714 memcpy(&pending_message[1], received_msg, size);
715 GNUNET_free(received_msg);
717 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
719 if (client_handle != NULL)
721 if (plugin_transmit_handle == NULL)
723 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
724 size, GNUNET_TIME_UNIT_FOREVER_REL,
725 &transmit_to_plugin, NULL);
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
738 * Function called to notify a client about the socket
739 * being ready to queue more data. "buf" will be
740 * NULL and "size" zero if the socket was closed for
741 * writing in the meantime.
744 * @param size number of bytes available in buf
745 * @param buf where the callee should write the message
746 * @return number of bytes written to buf
748 size_t core_transmit_notify (void *cls,
749 size_t size, void *buf)
752 struct PendingMessage *reply;
753 struct PendingMessage *client_reply;
759 /* client disconnected */
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
766 core_transmit_handle = NULL;
768 while ( (NULL != (reply = core_pending_head)) &&
769 (size >= off + (msize = ntohs (reply->msg->size))))
772 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
774 GNUNET_CONTAINER_DLL_remove (core_pending_head,
777 if (reply->send_result != NULL) /* Will only be non-null if a real client asked for this send */
779 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
780 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
781 memcpy(&client_reply[1], reply->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
782 GNUNET_free(reply->send_result);
784 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
785 if (client_handle != NULL)
787 if (plugin_transmit_handle == NULL)
789 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
790 sizeof(struct GNUNET_DV_SendResultMessage),
791 GNUNET_TIME_UNIT_FOREVER_REL,
792 &transmit_to_plugin, NULL);
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
800 memcpy (&cbuf[off], reply->msg, msize);
809 * Send a DV data message via DV.
811 * @param sender the original sender of the message
812 * @param specific_neighbor the specific DistantNeighbor to use, complete with referrer!
813 * @param send_context the send context
816 send_message_via (const struct GNUNET_PeerIdentity * sender,
817 const struct DistantNeighbor * specific_neighbor,
818 struct DV_SendContext *send_context)
820 p2p_dv_MESSAGE_Data *toSend;
821 unsigned int msg_size;
823 unsigned int recipient_id;
824 unsigned int sender_id;
825 struct DistantNeighbor *source;
826 struct PendingMessage *pending_message;
831 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
833 if (specific_neighbor == NULL)
835 /* target unknown to us, drop! */
836 return GNUNET_SYSERR;
838 recipient_id = specific_neighbor->referrer_id;
840 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
841 &sender->hashPubKey);
844 if (0 != (memcmp (&my_identity,
845 sender, sizeof (struct GNUNET_PeerIdentity))))
847 /* sender unknown to us, drop! */
848 return GNUNET_SYSERR;
850 sender_id = 0; /* 0 == us */
854 /* find out the number that we use when we gossip about
856 sender_id = source->our_id;
859 cost = specific_neighbor->cost;
860 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
861 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
862 pending_message->send_result = send_context->send_result;
863 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
864 toSend->header.size = htons (msg_size);
865 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
866 toSend->sender = htonl (sender_id);
867 toSend->recipient = htonl (recipient_id);
868 memcpy (&toSend[1], send_context->message, send_context->message_size);
871 memcpy(&shortname, GNUNET_i2s(&specific_neighbor->identity), 4);
873 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(&specific_neighbor->referrer->identity), msg_size);
876 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
880 if (core_transmit_handle == NULL)
881 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, send_context->importance, send_context->timeout, &specific_neighbor->referrer->identity, msg_size, &core_transmit_notify, NULL);
883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
890 * Send a DV data message via DV.
892 * @param recipient the ultimate recipient of this message
893 * @param sender the original sender of the message
894 * @param specific_neighbor the specific neighbor to send this message via
895 * @param message the packed message
896 * @param message_size size of the message
897 * @param importance what priority to send this message with
898 * @param timeout how long to possibly delay sending this message
901 send_message (const struct GNUNET_PeerIdentity * recipient,
902 const struct GNUNET_PeerIdentity * sender,
903 const struct DistantNeighbor * specific_neighbor,
904 const struct GNUNET_MessageHeader * message,
906 unsigned int importance, struct GNUNET_TIME_Relative timeout)
908 p2p_dv_MESSAGE_Data *toSend;
909 unsigned int msg_size;
911 unsigned int recipient_id;
912 unsigned int sender_id;
913 struct DistantNeighbor *target;
914 struct DistantNeighbor *source;
915 struct PendingMessage *pending_message;
917 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
919 target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
920 &recipient->hashPubKey);
923 /* target unknown to us, drop! */
924 return GNUNET_SYSERR;
926 recipient_id = target->referrer_id;
928 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
929 &sender->hashPubKey);
932 if (0 != (memcmp (&my_identity,
933 sender, sizeof (struct GNUNET_PeerIdentity))))
935 /* sender unknown to us, drop! */
936 return GNUNET_SYSERR;
938 sender_id = 0; /* 0 == us */
942 /* find out the number that we use when we gossip about
944 sender_id = source->our_id;
948 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
949 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
950 pending_message->send_result = NULL;
951 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
952 toSend->header.size = htons (msg_size);
953 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
954 toSend->sender = htonl (sender_id);
955 toSend->recipient = htonl (recipient_id);
956 memcpy (&toSend[1], message, message_size);
958 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
963 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));
965 if (core_transmit_handle == NULL)
966 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
973 * Core handler for dv data messages. Whatever this message
974 * contains all we really have to do is rip it out of its
975 * DV layering and give it to our pal the DV plugin to report
979 * @param peer peer which sent the message (immediate sender)
980 * @param message the message
981 * @param latency the latency of the connection we received the message from
982 * @param distance the distance to the immediate peer
984 static int handle_dv_data_message (void *cls,
985 const struct GNUNET_PeerIdentity * peer,
986 const struct GNUNET_MessageHeader * message,
987 struct GNUNET_TIME_Relative latency,
990 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
991 const struct GNUNET_MessageHeader *packed_message;
992 struct DirectNeighbor *dn;
993 struct DistantNeighbor *pos;
994 unsigned int sid; /* Sender id */
995 unsigned int tid; /* Target id */
996 struct GNUNET_PeerIdentity original_sender;
997 struct GNUNET_PeerIdentity destination;
998 struct FindDestinationContext fdc;
1000 size_t packed_message_size;
1004 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "%s: Receives %s message size %d, packed message size %d!\n", "dv", "DV DATA", ntohs(incoming->header.size), packed_message_size);
1010 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "`%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));
1016 return GNUNET_SYSERR;
1019 dn = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "%s: dn NULL!\n", "dv");
1029 sid = ntohl (incoming->sender);
1030 pos = dn->referee_head;
1031 while ((NULL != pos) && (pos->referrer_id != sid))
1036 #if DEBUG_DV_PEER_NUMBERS
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "%s peer %s: unknown sender (%d)!\n", "DV SERVICE", GNUNET_i2s(&my_identity), ntohl(incoming->sender), GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors));
1040 /* unknown sender */
1043 original_sender = pos->identity;
1044 tid = ntohl (incoming->recipient);
1048 cbuf = (char *)&incoming[1];
1050 while(offset < packed_message_size)
1052 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "%s: Receives %s message for me, size %d type %d, cost %u!\n", "dv", "DV DATA", ntohs(packed_message->size), ntohs(packed_message->type), pos->cost);
1057 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1058 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1059 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1060 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1062 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
1064 offset += ntohs(packed_message->size);
1071 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1074 /* FIXME: this is the *only* per-request operation we have in DV
1075 that is O(n) in relation to the number of connected peers; a
1076 hash-table lookup could easily solve this (minor performance
1080 GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap,
1081 &find_destination, &fdc);
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1088 if (fdc.dest == NULL)
1092 destination = fdc.dest->identity;
1094 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1096 /* FIXME: create stat: routing loop-discard! */
1098 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1103 /* At this point we have a message, and we need to forward it on to the
1106 /* FIXME: Can't send message on, we have to behave.
1107 * We have to tell core we have a message for the next peer, and let
1108 * transport do transport selection on how to get this message to 'em */
1109 /*ret = send_message (&destination,
1111 packed_message, DV_PRIORITY, DV_DELAY);*/
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "%s: Sends message size %d on!\n", "dv", packed_message_size);
1116 ret = send_message(&destination, &original_sender, NULL, packed_message, packed_message_size, default_dv_priority, GNUNET_TIME_relative_get_forever());
1118 if (ret != GNUNET_SYSERR)
1121 return GNUNET_SYSERR;
1126 * Scheduled task which gossips about known direct peers to other connected
1127 * peers. Will run until called with reason shutdown.
1130 neighbor_send_task (void *cls,
1131 const struct GNUNET_SCHEDULER_TaskContext *tc)
1133 struct NeighborSendContext *send_context = cls;
1134 #if DEBUG_DV_GOSSIP_SEND
1135 char * encPeerAbout;
1138 struct DistantNeighbor *about;
1139 struct DirectNeighbor *to;
1140 struct FastGossipNeighborList *about_list;
1142 p2p_dv_MESSAGE_NeighborInfo *message;
1143 struct PendingMessage *pending_message;
1145 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "%s: Called with reason shutdown, shutting down!\n",
1150 GNUNET_i2s(&my_identity));
1152 send_context->task = GNUNET_SCHEDULER_NO_TASK;
1156 if (send_context->fast_gossip_list_head != NULL)
1158 about_list = send_context->fast_gossip_list_head;
1159 about = send_context->fast_gossip_list_head->about;
1160 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1161 send_context->fast_gossip_list_tail,
1163 GNUNET_free(about_list);
1167 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1168 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1169 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1170 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1171 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1172 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1173 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1174 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1175 * asynchronously enough to avoid major problems (-;
1177 about = GNUNET_CONTAINER_heap_walk_get_next (ctx.neighbor_min_heap);
1179 to = send_context->toNeighbor;
1181 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1183 (about->hidden == GNUNET_NO) &&
1186 (0 != memcmp (&about->identity,
1187 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1188 (about->pkey != NULL))
1190 #if DEBUG_DV_GOSSIP_SEND
1191 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1192 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1195 GNUNET_i2s(&my_identity),
1196 encPeerAbout, about->our_id, encPeerTo);
1197 GNUNET_free(encPeerAbout);
1198 GNUNET_free(encPeerTo);
1200 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1201 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1202 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1203 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1204 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1205 message->cost = htonl (about->cost);
1206 message->neighbor_id = htonl (about->our_id);
1208 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1209 memcpy (&message->neighbor,
1210 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1212 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1217 if (core_transmit_handle == NULL)
1218 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);
1222 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SERVICE: still in fast send mode\n");
1227 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SERVICE: entering slow send mode\n");
1234 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1242 * Handle START-message. This is the first message sent to us
1243 * by the client (can only be one!).
1245 * @param cls closure (always NULL)
1246 * @param client identification of the client
1247 * @param message the actual message
1250 handle_start (void *cls,
1251 struct GNUNET_SERVER_Client *client,
1252 const struct GNUNET_MessageHeader *message)
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Received `%s' request from client\n", "START");
1260 client_handle = client;
1262 GNUNET_SERVER_client_keep(client_handle);
1263 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1268 * Iterate over hash map entries for a distant neighbor,
1269 * if direct neighbor matches context call send message
1271 * @param cls closure, a DV_SendContext
1272 * @param key current key code
1273 * @param value value in the hash map
1274 * @return GNUNET_YES if we should continue to
1278 int send_iterator (void *cls,
1279 const GNUNET_HashCode * key,
1282 struct DV_SendContext *send_context = cls;
1283 struct DistantNeighbor *distant_neighbor = value;
1285 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1287 send_message_via(&my_identity, distant_neighbor, send_context);
1294 * Service server's handler for message send requests (which come
1295 * bubbling up to us through the DV plugin).
1297 * @param cls closure
1298 * @param client identification of the client
1299 * @param message the actual message
1301 void handle_dv_send_message (void *cls,
1302 struct GNUNET_SERVER_Client * client,
1303 const struct GNUNET_MessageHeader * message)
1305 struct GNUNET_DV_SendMessage *send_msg;
1306 struct GNUNET_DV_SendResultMessage *send_result_msg;
1307 struct PendingMessage *pending_message;
1309 size_t message_size;
1310 struct GNUNET_PeerIdentity *destination;
1311 struct GNUNET_PeerIdentity *direct;
1312 struct GNUNET_MessageHeader *message_buf;
1315 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1316 struct DV_SendContext *send_context;
1318 if (client_handle == NULL)
1320 client_handle = client;
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1324 else if (client_handle != client)
1326 client_handle = client;
1327 /* What should we do in this case, assert fail or just log the warning? */
1328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1329 "%s: Setting client handle (was a different client!)!\n", "dv");
1332 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1333 send_msg = (struct GNUNET_DV_SendMessage *)message;
1335 address_len = ntohl(send_msg->addrlen);
1336 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1337 message_size = ntohl(send_msg->msgbuf_size);
1339 GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size);
1340 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1341 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1342 message_buf = GNUNET_malloc(message_size);
1344 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1345 offset = 0; /* Offset starts at zero */
1347 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1348 offset += sizeof(struct GNUNET_PeerIdentity);
1350 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1351 offset += sizeof(struct GNUNET_PeerIdentity);
1354 memcpy(message_buf, &temp_pos[offset], message_size);
1355 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1357 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1358 dest_hash.encoding[4] = '\0';
1359 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);
1363 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1364 dest_hash.encoding[4] = '\0';
1365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d type %d, destination `%s' via `%s'\n", message_size, ntohs(message_buf->type), (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1367 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1369 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1370 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1371 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1372 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1374 send_context->importance = ntohl(send_msg->priority);
1375 send_context->timeout = send_msg->timeout;
1376 send_context->direct_peer = direct;
1377 send_context->distant_peer = destination;
1378 send_context->message = message_buf;
1379 send_context->message_size = message_size;
1380 send_context->send_result = send_result_msg;
1382 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1383 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1385 send_result_msg->result = htons(1);
1386 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1387 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1388 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1389 GNUNET_free(send_result_msg);
1391 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1393 if (client_handle != NULL)
1395 if (plugin_transmit_handle == NULL)
1397 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1398 sizeof(struct GNUNET_DV_SendResultMessage),
1399 GNUNET_TIME_UNIT_FOREVER_REL,
1400 &transmit_to_plugin, NULL);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1407 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SEND failed to send message to destination `%s' via `%s'\n", (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1414 GNUNET_free(message_buf);
1415 GNUNET_free(send_context);
1416 GNUNET_free(direct);
1417 GNUNET_free(destination);
1419 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1422 /** Forward declarations **/
1423 static int handle_dv_gossip_message (void *cls,
1424 const struct GNUNET_PeerIdentity *peer,
1425 const struct GNUNET_MessageHeader *message,
1426 struct GNUNET_TIME_Relative latency,
1429 static int handle_dv_disconnect_message (void *cls,
1430 const struct GNUNET_PeerIdentity *peer,
1431 const struct GNUNET_MessageHeader *message,
1432 struct GNUNET_TIME_Relative latency,
1434 /** End forward declarations **/
1438 * List of handlers for the messages understood by this
1441 * Hmm... will we need to register some handlers with core and
1442 * some handlers with our server here? Because core should be
1443 * getting the incoming DV messages (from whichever lower level
1444 * transport) and then our server should be getting messages
1445 * from the dv_plugin, right?
1447 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1448 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1449 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1450 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1454 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1455 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1456 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1461 * Free a DistantNeighbor node, including removing it
1462 * from the referer's list.
1465 distant_neighbor_free (struct DistantNeighbor *referee)
1467 struct DirectNeighbor *referrer;
1469 referrer = referee->referrer;
1470 if (referrer != NULL)
1472 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1473 referrer->referee_tail, referee);
1475 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_max_heap, referee->max_loc);
1476 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_min_heap, referee->min_loc);
1477 GNUNET_CONTAINER_multihashmap_remove_all (ctx.extended_neighbors,
1478 &referee->identity.hashPubKey);
1479 GNUNET_free (referee->pkey);
1480 GNUNET_free (referee);
1484 * Free a DirectNeighbor node, including removing it
1485 * from the referer's list.
1488 direct_neighbor_free (struct DirectNeighbor *direct)
1490 struct NeighborSendContext *send_context;
1491 struct FastGossipNeighborList *about_list;
1492 struct FastGossipNeighborList *prev_about;
1494 send_context = direct->send_context;
1496 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1497 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1499 about_list = send_context->fast_gossip_list_head;
1500 while (about_list != NULL)
1502 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1503 prev_about = about_list;
1504 about_list = about_list->next;
1505 GNUNET_free(prev_about);
1507 GNUNET_free(send_context);
1508 GNUNET_free(direct);
1512 * Multihashmap iterator for sending out disconnect messages
1515 * @param cls the peer that was disconnected
1516 * @param key key value stored under
1517 * @param value the direct neighbor to send disconnect to
1519 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1521 static int schedule_disconnect_messages (void *cls,
1522 const GNUNET_HashCode * key,
1525 struct DisconnectContext *disconnect_context = cls;
1526 struct DirectNeighbor *disconnected = disconnect_context->direct;
1527 struct DirectNeighbor *notify = value;
1528 struct PendingMessage *pending_message;
1529 p2p_dv_MESSAGE_Disconnect *disconnect_message;
1531 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1532 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
1534 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
1535 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1536 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
1537 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
1538 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
1539 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
1541 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1546 if (core_transmit_handle == NULL)
1547 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);
1553 * Multihashmap iterator for freeing extended neighbors.
1556 * @param key key value stored under
1557 * @param value the distant neighbor to be freed
1559 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1561 static int free_extended_neighbors (void *cls,
1562 const GNUNET_HashCode * key,
1565 struct DistantNeighbor *distant = value;
1566 distant_neighbor_free(distant);
1571 * Multihashmap iterator for freeing direct neighbors.
1574 * @param key key value stored under
1575 * @param value the direct neighbor to be freed
1577 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1579 static int free_direct_neighbors (void *cls,
1580 const GNUNET_HashCode * key,
1583 struct DirectNeighbor *direct = value;
1584 direct_neighbor_free(direct);
1589 * Task run during shutdown.
1595 shutdown_task (void *cls,
1596 const struct GNUNET_SCHEDULER_TaskContext *tc)
1599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
1601 GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &free_extended_neighbors, NULL);
1602 GNUNET_CONTAINER_multihashmap_destroy(ctx.extended_neighbors);
1603 GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &free_direct_neighbors, NULL);
1604 GNUNET_CONTAINER_multihashmap_destroy(ctx.direct_neighbors);
1606 GNUNET_CONTAINER_heap_destroy(ctx.neighbor_max_heap);
1607 GNUNET_CONTAINER_heap_destroy(ctx.neighbor_min_heap);
1609 GNUNET_CORE_disconnect (coreAPI);
1610 GNUNET_PEERINFO_disconnect(peerinfo_handle);
1612 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
1617 * To be called on core init/fail.
1619 void core_init (void *cls,
1620 struct GNUNET_CORE_Handle * server,
1621 const struct GNUNET_PeerIdentity *identity,
1622 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
1627 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
1628 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1633 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
1635 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
1640 * Iterator over hash map entries.
1642 * @param cls closure
1643 * @param key current key code
1644 * @param value value in the hash map
1645 * @return GNUNET_YES if we should continue to
1649 static int add_pkey_to_extended (void *cls,
1650 const GNUNET_HashCode * key,
1653 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
1654 struct DistantNeighbor *distant_neighbor = value;
1656 if (distant_neighbor->pkey == NULL)
1658 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1659 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1666 * Iterator over hash map entries.
1668 * @param cls closure
1669 * @param key current key code
1670 * @param value value in the hash map
1671 * @return GNUNET_YES if we should continue to
1675 static int update_matching_neighbors (void *cls,
1676 const GNUNET_HashCode * key,
1679 struct NeighborUpdateInfo * update_info = cls;
1680 struct DistantNeighbor *distant_neighbor = value;
1682 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
1684 /* same referrer, cost change! */
1685 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_max_heap,
1686 update_info->neighbor->max_loc, update_info->cost);
1687 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_min_heap,
1688 update_info->neighbor->min_loc, update_info->cost);
1689 update_info->neighbor->last_activity = update_info->now;
1690 update_info->neighbor->cost = update_info->cost;
1700 * Iterator over hash map entries.
1702 * @param cls closure (NULL)
1703 * @param key current key code
1704 * @param value value in the hash map (DistantNeighbor)
1705 * @return GNUNET_YES if we should continue to
1709 int print_neighbors (void *cls,
1710 const GNUNET_HashCode * key,
1713 struct DistantNeighbor *distant_neighbor = value;
1714 char my_shortname[5];
1715 char referrer_shortname[5];
1716 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1717 my_shortname[4] = '\0';
1718 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1719 referrer_shortname[4] = '\0';
1721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' %s: Peer `%s', distance %d, referrer `%s'\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname);
1728 * Handles when a peer is either added due to being newly connected
1729 * or having been gossiped about, also called when the cost for a neighbor
1730 * needs to be updated.
1732 * @param peer identity of the peer whose info is being added/updated
1733 * @param pkey public key of the peer whose info is being added/updated
1734 * @param referrer_peer_id id to use when sending to 'peer'
1735 * @param referrer if this is a gossiped peer, who did we hear it from?
1736 * @param cost the cost of communicating with this peer via 'referrer'
1739 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
1740 unsigned int referrer_peer_id,
1741 struct DirectNeighbor *referrer, unsigned int cost)
1743 struct DistantNeighbor *neighbor;
1744 struct DistantNeighbor *max;
1745 struct GNUNET_TIME_Absolute now;
1746 struct NeighborUpdateInfo *neighbor_update;
1747 unsigned int our_id;
1749 #if DEBUG_DV_PEER_NUMBERS
1750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1751 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
1754 now = GNUNET_TIME_absolute_get ();
1755 neighbor = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
1757 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
1758 neighbor_update->neighbor = neighbor;
1759 neighbor_update->cost = cost;
1760 neighbor_update->now = now;
1761 neighbor_update->referrer = referrer;
1764 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
1765 #if DEBUG_DV_PEER_NUMBERS
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "%s Chose NEW id (%u)!\n", "DV SERVICE", our_id);
1770 /* Either we do not know this peer, or we already do but via a different immediate peer */
1771 if ((neighbor == NULL) ||
1772 (GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1774 &update_matching_neighbors,
1775 neighbor_update) != GNUNET_SYSERR))
1778 if (cost > ctx.fisheye_depth)
1781 GNUNET_free(neighbor_update);
1784 if (ctx.max_table_size <=
1785 GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors))
1787 /* remove most expensive entry */
1788 max = GNUNET_CONTAINER_heap_peek (ctx.neighbor_max_heap);
1789 if (cost > max->cost)
1791 /* new entry most expensive, don't create */
1792 GNUNET_free(neighbor_update);
1797 /* only free if this is not a direct connection;
1798 we could theoretically have more direct
1799 connections than DV entries allowed total! */
1800 distant_neighbor_free (max);
1804 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
1805 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
1806 referrer->referee_tail, neighbor);
1807 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_max_heap,
1809 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_min_heap,
1811 neighbor->referrer = referrer;
1812 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
1813 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
1815 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1816 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1819 neighbor->pkey = pkey;
1821 neighbor->last_activity = now;
1822 neighbor->cost = cost;
1823 neighbor->referrer_id = referrer_peer_id;
1824 neighbor->our_id = our_id;
1826 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
1829 GNUNET_CONTAINER_multihashmap_put (ctx.extended_neighbors, &peer->hashPubKey,
1831 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1837 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors));
1844 GNUNET_free(neighbor_update);
1850 generate_hello_address (void *cls, size_t max, void *buf)
1852 struct HelloContext *hello_context = cls;
1858 if (hello_context->addresses_to_add == 0)
1861 /* Hello "address" will be concatenation of distant peer and direct peer identities */
1862 size = 2 * sizeof(struct GNUNET_PeerIdentity);
1863 GNUNET_assert(max >= size);
1865 addr_buffer = GNUNET_malloc(size);
1867 /* Copy the distant peer identity to buffer */
1868 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
1869 offset += sizeof(struct GNUNET_PeerIdentity);
1870 /* Copy the direct peer identity to buffer */
1871 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
1872 ret = GNUNET_HELLO_add_address ("dv",
1873 GNUNET_TIME_relative_to_absolute
1874 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
1877 hello_context->addresses_to_add--;
1879 GNUNET_free(addr_buffer);
1885 * Core handler for dv disconnect messages. These will be used
1886 * by us to tell transport via the dv plugin that a peer can
1887 * no longer be contacted by us via a certain address. We should
1888 * then propagate these messages on, given that the distance to
1889 * the peer indicates we would have gossiped about it to others.
1891 * @param cls closure
1892 * @param peer peer which sent the message (immediate sender)
1893 * @param message the message
1894 * @param latency the latency of the connection we received the message from
1895 * @param distance the distance to the immediate peer
1897 static int handle_dv_disconnect_message (void *cls,
1898 const struct GNUNET_PeerIdentity *peer,
1899 const struct GNUNET_MessageHeader *message,
1900 struct GNUNET_TIME_Relative latency,
1903 struct DirectNeighbor *referrer;
1904 struct DistantNeighbor *distant;
1905 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
1907 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
1909 return GNUNET_SYSERR; /* invalid message */
1912 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1914 if (referrer == NULL)
1917 distant = referrer->referee_head;
1918 while (distant != NULL)
1920 if (distant->referrer_id == ntohl(enc_message->peer_id))
1922 distant_neighbor_free(distant);
1931 * Core handler for dv gossip messages. These will be used
1932 * by us to create a HELLO message for the newly peer containing
1933 * which direct peer we can connect through, and what the cost
1934 * is. This HELLO will then be scheduled for validation by the
1935 * transport service so that it can be used by all others.
1937 * @param cls closure
1938 * @param peer peer which sent the message (immediate sender)
1939 * @param message the message
1940 * @param latency the latency of the connection we received the message from
1941 * @param distance the distance to the immediate peer
1943 static int handle_dv_gossip_message (void *cls,
1944 const struct GNUNET_PeerIdentity *peer,
1945 const struct GNUNET_MessageHeader *message,
1946 struct GNUNET_TIME_Relative latency,
1949 struct HelloContext *hello_context;
1950 struct GNUNET_HELLO_Message *hello_msg;
1951 struct DirectNeighbor *referrer;
1952 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
1954 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
1956 return GNUNET_SYSERR; /* invalid message */
1959 #if DEBUG_DV_GOSSIP_RECEIPT
1960 char * encPeerAbout;
1963 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
1964 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1966 "%s: Receives %s message from peer %s about peer %s id %u distance %d!\n", "dv", "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl(enc_message->neighbor_id), ntohl (enc_message->cost) + 1);
1967 GNUNET_free(encPeerAbout);
1968 GNUNET_free(encPeerFrom);
1971 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1973 if (referrer == NULL)
1976 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
1977 ntohl (enc_message->neighbor_id),
1978 referrer, ntohl (enc_message->cost) + 1);
1980 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
1981 hello_context->direct_peer = peer;
1982 memcpy(&hello_context->distant_peer, &enc_message->neighbor, sizeof(struct GNUNET_PeerIdentity));
1983 hello_context->addresses_to_add = 1;
1984 hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context);
1986 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, ntohl(enc_message->cost) + 1);
1987 GNUNET_free(hello_context);
1988 GNUNET_free(hello_msg);
1994 * Iterate over all currently known peers, add them to the
1995 * fast gossip list for this peer so we get DV routing information
1996 * out as fast as possible!
1998 * @param cls the direct neighbor we will gossip to
1999 * @param key the hashcode of the peer
2000 * @param value the distant neighbor we should add to the list
2002 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2004 static int add_all_extended_peers (void *cls,
2005 const GNUNET_HashCode * key,
2008 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2009 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2010 struct FastGossipNeighborList *gossip_entry;
2012 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2013 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2016 if (distant->hidden == GNUNET_YES)
2017 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2019 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2020 gossip_entry->about = distant;
2022 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2023 send_context->fast_gossip_list_tail,
2024 send_context->fast_gossip_list_tail,
2032 * Iterate over all current direct peers, add DISTANT newly connected
2033 * peer to the fast gossip list for that peer so we get DV routing
2034 * information out as fast as possible!
2036 * @param cls the newly connected neighbor we will gossip about
2037 * @param key the hashcode of the peer
2038 * @param value the direct neighbor we should gossip to
2040 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2042 static int add_distant_all_direct_neighbors (void *cls,
2043 const GNUNET_HashCode * key,
2046 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2047 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2048 struct NeighborSendContext *send_context = direct->send_context;
2049 struct FastGossipNeighborList *gossip_entry;
2051 if (distant == NULL)
2056 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2058 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2062 if (distant->hidden == GNUNET_YES)
2063 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2065 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2066 gossip_entry->about = distant;
2068 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2069 send_context->fast_gossip_list_tail,
2070 send_context->fast_gossip_list_tail,
2072 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2073 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2075 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2081 * Iterate over all current direct peers, add newly connected peer
2082 * to the fast gossip list for that peer so we get DV routing
2083 * information out as fast as possible!
2085 * @param cls the newly connected neighbor we will gossip about
2086 * @param key the hashcode of the peer
2087 * @param value the direct neighbor we should gossip to
2089 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2091 static int add_all_direct_neighbors (void *cls,
2092 const GNUNET_HashCode * key,
2095 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2096 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2097 struct DistantNeighbor *distant;
2098 struct NeighborSendContext *send_context = direct->send_context;
2099 struct FastGossipNeighborList *gossip_entry;
2101 distant = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &to->identity.hashPubKey);
2102 if (distant == NULL)
2107 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2109 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2113 if (distant->hidden == GNUNET_YES)
2114 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2116 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2117 gossip_entry->about = distant;
2119 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2120 send_context->fast_gossip_list_tail,
2121 send_context->fast_gossip_list_tail,
2123 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2124 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2126 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2132 process_peerinfo (void *cls,
2133 const struct GNUNET_PeerIdentity *peer,
2134 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2136 struct PeerIteratorContext *peerinfo_iterator = cls;
2137 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2139 if ((peer == NULL))/* && (neighbor->pkey == NULL))*/
2141 /* FIXME: Remove peer! */
2142 GNUNET_free(peerinfo_iterator);
2146 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2149 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2151 GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
2153 &add_pkey_to_extended,
2156 GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2158 GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &add_all_direct_neighbors, neighbor);
2159 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2165 * Method called whenever a peer connects.
2167 * @param cls closure
2168 * @param peer peer identity this notification is about
2169 * @param latency reported latency of the connection with peer
2170 * @param distance reported distance (DV) to peer
2172 void handle_core_connect (void *cls,
2173 const struct GNUNET_PeerIdentity * peer,
2174 struct GNUNET_TIME_Relative latency,
2177 struct DirectNeighbor *neighbor;
2178 struct DistantNeighbor *about;
2179 struct PeerIteratorContext *peerinfo_iterator;
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2185 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL))
2187 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2188 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2189 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2190 neighbor->send_context->toNeighbor = neighbor;
2191 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2193 GNUNET_CONTAINER_multihashmap_put (ctx.direct_neighbors,
2195 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2196 addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2197 peerinfo_iterator->neighbor = neighbor;
2198 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2201 GNUNET_TIME_UNIT_FOREVER_REL,
2205 /* Only add once we get the publicKey of this guy
2207 * neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2212 about = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &peer->hashPubKey);
2213 if ((GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2214 GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &add_distant_all_direct_neighbors, about);
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2225 * Method called whenever a given peer disconnects.
2227 * @param cls closure
2228 * @param peer peer identity this notification is about
2230 void handle_core_disconnect (void *cls,
2231 const struct GNUNET_PeerIdentity * peer)
2233 struct DirectNeighbor *neighbor;
2234 struct DistantNeighbor *referee;
2235 struct FindDestinationContext fdc;
2236 struct DisconnectContext disconnect_context;
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2239 "%s: Receives core peer disconnect message!\n", "dv");
2243 GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors, &peer->hashPubKey);
2244 if (neighbor == NULL)
2248 while (NULL != (referee = neighbor->referee_head))
2249 distant_neighbor_free (referee);
2254 GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &find_distant_peer, &fdc);
2256 if (fdc.dest != NULL)
2258 disconnect_context.direct = neighbor;
2259 disconnect_context.distant = fdc.dest;
2260 GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2263 GNUNET_assert (neighbor->referee_tail == NULL);
2264 GNUNET_CONTAINER_multihashmap_remove (ctx.direct_neighbors,
2265 &peer->hashPubKey, neighbor);
2266 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2267 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2268 GNUNET_free (neighbor);
2273 * Process dv requests.
2275 * @param cls closure
2276 * @param scheduler scheduler to use
2277 * @param server the initialized server
2278 * @param c configuration to use
2282 struct GNUNET_SCHEDULER_Handle *scheduler,
2283 struct GNUNET_SERVER_Handle *server,
2284 const struct GNUNET_CONFIGURATION_Handle *c)
2286 unsigned long long max_hosts;
2290 /* FIXME: Read from config, or calculate, or something other than this! */
2291 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2292 ctx.max_table_size = DEFAULT_DV_SIZE;
2293 ctx.fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2295 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2297 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts);
2300 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2302 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &ctx.max_table_size);
2305 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2307 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &ctx.fisheye_depth);
2310 ctx.neighbor_min_heap =
2311 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2312 ctx.neighbor_max_heap =
2313 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2315 ctx.direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2316 ctx.extended_neighbors =
2317 GNUNET_CONTAINER_multihashmap_create (ctx.max_table_size * 3);
2319 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2321 GNUNET_CORE_connect (sched,
2323 GNUNET_TIME_relative_get_forever(),
2324 NULL, /* FIXME: anything we want to pass around? */
2326 &handle_core_connect,
2327 &handle_core_disconnect,
2334 if (coreAPI == NULL)
2337 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2339 if (peerinfo_handle == NULL)
2341 GNUNET_CORE_disconnect(coreAPI);
2345 /* Scheduled the task to clean up when shutdown is called */
2346 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
2347 GNUNET_TIME_UNIT_FOREVER_REL,
2354 * The main function for the dv service.
2356 * @param argc number of arguments from the command line
2357 * @param argv command line arguments
2358 * @return 0 ok, 1 on error
2361 main (int argc, char *const *argv)
2363 return (GNUNET_OK ==
2364 GNUNET_SERVICE_run (argc,
2367 GNUNET_SERVICE_OPTION_NONE,
2368 &run, NULL)) ? 0 : 1;