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"
49 * DV Service Context stuff goes here...
53 * Handle to the core service api.
55 static struct GNUNET_CORE_Handle *coreAPI;
58 * The identity of our peer.
60 static struct GNUNET_PeerIdentity my_identity;
63 * The configuration for this service.
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * The scheduler for this service.
70 static struct GNUNET_SCHEDULER_Handle *sched;
73 * How often do we check about sending out more peer information (if
74 * we are connected to no peers previously).
76 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50000)
79 * How long do we wait at most between sending out information?
81 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50000)
84 * How long can we have not heard from a peer and
85 * still have it in our tables?
87 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
90 * Priority for gossip.
92 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
95 * How often should we check if expiration time has elapsed for
98 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
101 * How long to allow a message to be delayed?
103 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
106 * Priority to use for DV data messages.
108 #define DV_PRIORITY 0
111 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
113 #define DIRECT_NEIGHBOR_COST 1
116 * The default number of direct connections to store in DV (max)
118 #define DEFAULT_DIRECT_CONNECTIONS 50
121 * The default size of direct + extended peers in DV (max)
123 #define DEFAULT_DV_SIZE 100
126 * The default fisheye depth, from how many hops away will
129 #define DEFAULT_FISHEYE_DEPTH 4
132 * The client, the DV plugin connected to us. Hopefully
133 * this client will never change, although if the plugin dies
134 * and returns for some reason it may happen.
136 static struct GNUNET_SERVER_Client * client_handle;
139 * Task to run when we shut down, cleaning up all our trash
141 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
144 * Task to run to gossip about peers. Will reschedule itself forever until shutdown!
146 GNUNET_SCHEDULER_TaskIdentifier gossip_task;
149 * Struct where neighbor information is stored.
151 struct DistantNeighbor *referees;
153 static size_t default_dv_priority = 0;
157 * Linked list of messages to send to clients.
159 struct PendingMessage
162 * Pointer to next item in the list
164 struct PendingMessage *next;
167 * Pointer to previous item in the list
169 struct PendingMessage *prev;
171 struct GNUNET_DV_SendResultMessage *send_result;
173 * Actual message to be sent; // avoid allocation
175 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
180 * Transmit handle to the plugin.
182 struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
185 * Head of DLL for client messages
187 struct PendingMessage *plugin_pending_head;
190 * Tail of DLL for client messages
192 struct PendingMessage *plugin_pending_tail;
195 * Handle to the peerinfo service
197 struct GNUNET_PEERINFO_Handle *peerinfo_handle;
200 * Transmit handle to core service.
202 struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
205 * Head of DLL for core messages
207 struct PendingMessage *core_pending_head;
210 * Tail of DLL for core messages
212 struct PendingMessage *core_pending_tail;
215 struct FastGossipNeighborList
218 * Next element of DLL
220 struct FastGossipNeighborList *next;
223 * Prev element of DLL
225 struct FastGossipNeighborList *prev;
228 * The neighbor to gossip about
230 struct DistantNeighbor *about;
234 * Context created whenever a direct peer connects to us,
235 * used to gossip other peers to it.
237 struct NeighborSendContext
240 * The peer we will gossip to.
242 struct DirectNeighbor *toNeighbor;
245 * The task associated with this context.
247 GNUNET_SCHEDULER_TaskIdentifier task;
250 * Head of DLL of peers to gossip about
251 * as fast as possible to this peer, for initial
254 struct FastGossipNeighborList *fast_gossip_list_head;
257 * Tail of DLL of peers to gossip about
258 * as fast as possible to this peer, for initial
261 struct FastGossipNeighborList *fast_gossip_list_tail;
267 * Struct to hold information for updating existing neighbors
269 struct NeighborUpdateInfo
277 * The existing neighbor
279 struct DistantNeighbor *neighbor;
282 * The referrer of the possibly existing peer
284 struct DirectNeighbor *referrer;
287 * The time we heard about this peer
289 struct GNUNET_TIME_Absolute now;
293 * Struct where actual neighbor information is stored,
294 * referenced by min_heap and max_heap. Freeing dealt
295 * with when items removed from hashmap.
297 struct DirectNeighbor
300 * Identity of neighbor.
302 struct GNUNET_PeerIdentity identity;
305 * PublicKey of neighbor.
307 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
310 * Head of DLL of nodes that this direct neighbor referred to us.
312 struct DistantNeighbor *referee_head;
315 * Tail of DLL of nodes that this direct neighbor referred to us.
317 struct DistantNeighbor *referee_tail;
320 * The sending context for gossiping peers to this neighbor.
322 struct NeighborSendContext *send_context;
325 * Is this one of the direct neighbors that we are "hiding"
333 * Struct where actual neighbor information is stored,
334 * referenced by min_heap and max_heap. Freeing dealt
335 * with when items removed from hashmap.
337 struct DistantNeighbor
340 * We keep distant neighbor's of the same referrer in a DLL.
342 struct DistantNeighbor *next;
345 * We keep distant neighbor's of the same referrer in a DLL.
347 struct DistantNeighbor *prev;
352 struct GNUNET_CONTAINER_HeapNode *min_loc;
357 struct GNUNET_CONTAINER_HeapNode *max_loc;
360 * Identity of referrer (next hop towards 'neighbor').
362 struct DirectNeighbor *referrer;
365 * Identity of neighbor.
367 struct GNUNET_PeerIdentity identity;
370 * PublicKey of neighbor.
372 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
375 * Last time we received routing information from this peer
377 struct GNUNET_TIME_Absolute last_activity;
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;
418 * Context used for creating hello messages when
419 * gossips are received.
424 * Identity of distant neighbor.
426 struct GNUNET_PeerIdentity distant_peer;
429 * Identity of direct neighbor, via which we send this message.
431 const struct GNUNET_PeerIdentity *direct_peer;
434 * How many addresses do we need to add (always starts at 1, then set to 0)
436 int addresses_to_add;
440 struct DV_SendContext
443 * The distant peer (should always match)
445 struct GNUNET_PeerIdentity *distant_peer;
448 * The direct peer, we need to verify the referrer of.
450 struct GNUNET_PeerIdentity *direct_peer;
453 * The message to be sent
455 struct GNUNET_MessageHeader *message;
458 * The pre-built send result message. Simply needs to be queued
459 * and freed once send has been called!
461 struct GNUNET_DV_SendResultMessage *send_result;
464 * The size of the message being sent, may be larger
465 * than message->header.size because it's multiple
466 * messages packed into one!
471 * How important is this message?
473 unsigned int importance;
476 * Timeout for this message
478 struct GNUNET_TIME_Relative timeout;
484 struct GNUNET_DV_Context
487 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
488 * directly connected peers.
490 struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
493 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
494 * peers connected via DV (extended neighborhood). Does ALSO
495 * include any peers that are in 'direct_neighbors'; for those
496 * peers, the cost will be zero and the referrer all zeros.
498 struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
501 * We use the min heap (min refers to cost) to prefer
502 * gossipping about peers with small costs.
504 struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
507 * We use the max heap (max refers to cost) for general
508 * iterations over all peers and to remove the most costly
509 * connection if we have too many.
511 struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
513 unsigned long long fisheye_depth;
515 unsigned long long max_table_size;
517 unsigned int neighbor_id_loc;
523 static struct GNUNET_DV_Context ctx;
525 struct FindDestinationContext
528 struct DistantNeighbor *dest;
531 struct DisconnectContext
534 * Distant neighbor to get pid from.
536 struct DistantNeighbor *distant;
539 * Direct neighbor that disconnected.
541 struct DirectNeighbor *direct;
545 * We've been given a target ID based on the random numbers that
546 * we assigned to our DV-neighborhood. Find the entry for the
547 * respective neighbor.
550 find_destination (void *cls,
551 struct GNUNET_CONTAINER_HeapNode *node,
552 void *element, GNUNET_CONTAINER_HeapCostType cost)
554 struct FindDestinationContext *fdc = cls;
555 struct DistantNeighbor *dn = element;
557 if (fdc->tid != dn->our_id)
564 * Find a distant peer whose referrer_id matches what we're
565 * looking for. For looking up a peer we've gossipped about
566 * but is now disconnected. Need to do this because we don't
567 * want to remove those that may be accessible via a different
570 static int find_distant_peer (void *cls,
571 const GNUNET_HashCode * key,
574 struct FindDestinationContext *fdc = cls;
575 struct DistantNeighbor *distant = value;
577 if (fdc->tid == distant->referrer_id)
586 * Function called to notify a client about the socket
587 * begin ready to queue more data. "buf" will be
588 * NULL and "size" zero if the socket was closed for
589 * writing in the meantime.
592 * @param size number of bytes available in buf
593 * @param buf where the callee should write the message
594 * @return number of bytes written to buf
596 size_t transmit_to_plugin (void *cls,
597 size_t size, void *buf)
600 struct PendingMessage *reply;
606 /* client disconnected */
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
612 plugin_transmit_handle = NULL;
614 while ( (NULL != (reply = plugin_pending_head)) &&
615 (size >= off + (msize = ntohs (reply->msg->size))))
618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (plugin) called with size %d\n", "dv service", msize);
620 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
623 memcpy (&cbuf[off], reply->msg, msize);
628 if (plugin_pending_head != NULL)
629 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
630 ntohs(plugin_pending_head->msg->size),
631 GNUNET_TIME_UNIT_FOREVER_REL,
632 &transmit_to_plugin, NULL);
638 * Send a message to the dv plugin.
640 * @param sender the direct sender of the message
641 * @param message the message to send to the plugin
642 * (may be an encapsulated type)
643 * @param message_size the size of the message to be sent
644 * @param distant_neighbor the original sender of the message
645 * @param cost the cost to the original sender of the message
647 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
648 const struct GNUNET_MessageHeader *message,
650 struct GNUNET_PeerIdentity *distant_neighbor,
653 struct GNUNET_DV_MessageReceived *received_msg;
654 struct PendingMessage *pending_message;
656 struct GNUNET_MessageHeader * packed_message_header;
657 struct GNUNET_HELLO_Message *hello_msg;
658 struct GNUNET_PeerIdentity hello_identity;
660 char *sender_address;
661 size_t sender_address_len;
662 char *packed_msg_start;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
669 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
671 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
672 sender_address = GNUNET_malloc(sender_address_len);
673 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
674 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
678 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
679 sender_address = GNUNET_malloc(sender_address_len);
680 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
683 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
684 received_msg = GNUNET_malloc(size);
685 received_msg->header.size = htons(size);
686 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
687 received_msg->sender_address_len = htonl(sender_address_len);
688 received_msg->distance = htonl(cost);
689 received_msg->msg_len = htonl(message_size);
690 /* Set the sender in this message to be the original sender! */
691 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
692 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
693 memcpy(&received_msg[1], sender_address, sender_address_len);
694 GNUNET_free(sender_address);
695 /* Copy the actual message after the sender */
696 packed_msg_start = (char *)&received_msg[1];
697 packed_msg_start = &packed_msg_start[sender_address_len];
698 memcpy(packed_msg_start, message, message_size);
700 packed_message_header = (struct GNUNET_MessageHeader *)packed_msg_start;
701 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));
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv packed message len %d, type %d\n", ntohs(packed_message_header->size), ntohs(packed_message_header->type));
703 if (ntohs(packed_message_header->type) == GNUNET_MESSAGE_TYPE_HELLO)
705 hello_msg = (struct GNUNET_HELLO_Message *)packed_message_header;
706 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello_msg, &hello_identity));
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity));
710 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
711 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
712 memcpy(&pending_message[1], received_msg, size);
713 GNUNET_free(received_msg);
715 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
717 if (client_handle != NULL)
719 if (plugin_transmit_handle == NULL)
721 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
722 size, GNUNET_TIME_UNIT_FOREVER_REL,
723 &transmit_to_plugin, NULL);
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
734 * Function called to notify a client about the socket
735 * being ready to queue more data. "buf" will be
736 * NULL and "size" zero if the socket was closed for
737 * writing in the meantime.
740 * @param size number of bytes available in buf
741 * @param buf where the callee should write the message
742 * @return number of bytes written to buf
744 size_t core_transmit_notify (void *cls,
745 size_t size, void *buf)
748 struct PendingMessage *reply;
749 struct PendingMessage *client_reply;
755 /* client disconnected */
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
762 core_transmit_handle = NULL;
764 while ( (NULL != (reply = core_pending_head)) &&
765 (size >= off + (msize = ntohs (reply->msg->size))))
768 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
770 GNUNET_CONTAINER_DLL_remove (core_pending_head,
773 if (reply->send_result != NULL) /* Will only be non-null if a real client asked for this send */
775 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
776 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
777 memcpy(&client_reply[1], reply->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
778 GNUNET_free(reply->send_result);
780 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
781 if (client_handle != NULL)
783 if (plugin_transmit_handle == NULL)
785 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
786 sizeof(struct GNUNET_DV_SendResultMessage),
787 GNUNET_TIME_UNIT_FOREVER_REL,
788 &transmit_to_plugin, NULL);
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
796 memcpy (&cbuf[off], reply->msg, msize);
805 * Send a DV data message via DV.
807 * @param sender the original sender of the message
808 * @param specific_neighbor the specific DistantNeighbor to use, complete with referrer!
809 * @param send_context the send context
812 send_message_via (const struct GNUNET_PeerIdentity * sender,
813 const struct DistantNeighbor * specific_neighbor,
814 struct DV_SendContext *send_context)
816 p2p_dv_MESSAGE_Data *toSend;
817 unsigned int msg_size;
819 unsigned int recipient_id;
820 unsigned int sender_id;
821 struct DistantNeighbor *source;
822 struct PendingMessage *pending_message;
827 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
829 if (specific_neighbor == NULL)
831 /* target unknown to us, drop! */
832 return GNUNET_SYSERR;
834 recipient_id = specific_neighbor->referrer_id;
836 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
837 &sender->hashPubKey);
840 if (0 != (memcmp (&my_identity,
841 sender, sizeof (struct GNUNET_PeerIdentity))))
843 /* sender unknown to us, drop! */
844 return GNUNET_SYSERR;
846 sender_id = 0; /* 0 == us */
850 /* find out the number that we use when we gossip about
852 sender_id = source->our_id;
855 cost = specific_neighbor->cost;
856 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
857 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
858 pending_message->send_result = send_context->send_result;
859 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
860 toSend->header.size = htons (msg_size);
861 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
862 toSend->sender = htonl (sender_id);
863 toSend->recipient = htonl (recipient_id);
864 memcpy (&toSend[1], send_context->message, send_context->message_size);
867 memcpy(&shortname, GNUNET_i2s(&specific_neighbor->identity), 4);
869 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);
872 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
876 if (core_transmit_handle == NULL)
877 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);
879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
886 * Send a DV data message via DV.
888 * @param recipient the ultimate recipient of this message
889 * @param sender the original sender of the message
890 * @param specific_neighbor the specific neighbor to send this message via
891 * @param message the packed message
892 * @param message_size size of the message
893 * @param importance what priority to send this message with
894 * @param timeout how long to possibly delay sending this message
897 send_message (const struct GNUNET_PeerIdentity * recipient,
898 const struct GNUNET_PeerIdentity * sender,
899 const struct DistantNeighbor * specific_neighbor,
900 const struct GNUNET_MessageHeader * message,
902 unsigned int importance, struct GNUNET_TIME_Relative timeout)
904 p2p_dv_MESSAGE_Data *toSend;
905 unsigned int msg_size;
907 unsigned int recipient_id;
908 unsigned int sender_id;
909 struct DistantNeighbor *target;
910 struct DistantNeighbor *source;
911 struct PendingMessage *pending_message;
913 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
915 target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
916 &recipient->hashPubKey);
919 /* target unknown to us, drop! */
920 return GNUNET_SYSERR;
922 recipient_id = target->referrer_id;
924 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
925 &sender->hashPubKey);
928 if (0 != (memcmp (&my_identity,
929 sender, sizeof (struct GNUNET_PeerIdentity))))
931 /* sender unknown to us, drop! */
932 return GNUNET_SYSERR;
934 sender_id = 0; /* 0 == us */
938 /* find out the number that we use when we gossip about
940 sender_id = source->our_id;
944 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
945 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
946 pending_message->send_result = NULL;
947 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
948 toSend->header.size = htons (msg_size);
949 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
950 toSend->sender = htonl (sender_id);
951 toSend->recipient = htonl (recipient_id);
952 memcpy (&toSend[1], message, message_size);
954 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
959 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));
961 if (core_transmit_handle == NULL)
962 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
969 * Core handler for dv data messages. Whatever this message
970 * contains all we really have to do is rip it out of its
971 * DV layering and give it to our pal the DV plugin to report
975 * @param peer peer which sent the message (immediate sender)
976 * @param message the message
977 * @param latency the latency of the connection we received the message from
978 * @param distance the distance to the immediate peer
980 static int handle_dv_data_message (void *cls,
981 const struct GNUNET_PeerIdentity * peer,
982 const struct GNUNET_MessageHeader * message,
983 struct GNUNET_TIME_Relative latency,
986 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
987 const struct GNUNET_MessageHeader *packed_message;
988 struct DirectNeighbor *dn;
989 struct DistantNeighbor *pos;
990 unsigned int sid; /* Sender id */
991 unsigned int tid; /* Target id */
992 struct GNUNET_PeerIdentity original_sender;
993 struct GNUNET_PeerIdentity destination;
994 struct FindDestinationContext fdc;
996 size_t packed_message_size;
1000 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "%s: Receives %s message size %d, packed message size %d!\n", "dv", "DV DATA", ntohs(incoming->header.size), packed_message_size);
1006 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "`%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));
1012 return GNUNET_SYSERR;
1015 dn = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "%s: dn NULL!\n", "dv");
1025 sid = ntohl (incoming->sender);
1026 pos = dn->referee_head;
1027 while ((NULL != pos) && (pos->referrer_id != sid))
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "%s peer %s: unknown sender (%d)!\n", "DV SERVICE", GNUNET_i2s(&my_identity), ntohl(incoming->sender), GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors));
1035 /* unknown sender */
1038 original_sender = pos->identity;
1039 tid = ntohl (incoming->recipient);
1044 cbuf = (char *)&incoming[1];
1046 while(offset < packed_message_size)
1048 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "%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);
1053 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1054 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1055 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1056 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1058 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
1060 offset += ntohs(packed_message->size);
1067 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1070 /* FIXME: this is the *only* per-request operation we have in DV
1071 that is O(n) in relation to the number of connected peers; a
1072 hash-table lookup could easily solve this (minor performance
1076 GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap,
1077 &find_destination, &fdc);
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1084 if (fdc.dest == NULL)
1088 destination = fdc.dest->identity;
1090 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1092 /* FIXME: create stat: routing loop-discard! */
1094 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1099 /* At this point we have a message, and we need to forward it on to the
1102 /* FIXME: Can't send message on, we have to behave.
1103 * We have to tell core we have a message for the next peer, and let
1104 * transport do transport selection on how to get this message to 'em */
1105 /*ret = send_message (&destination,
1107 packed_message, DV_PRIORITY, DV_DELAY);*/
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "%s: Sends message size %d on!\n", "dv", packed_message_size);
1112 ret = send_message(&destination, &original_sender, NULL, packed_message, packed_message_size, default_dv_priority, GNUNET_TIME_relative_get_forever());
1114 if (ret != GNUNET_SYSERR)
1117 return GNUNET_SYSERR;
1122 * Scheduled task which gossips about known direct peers to other connected
1123 * peers. Will run until called with reason shutdown.
1126 neighbor_send_task (void *cls,
1127 const struct GNUNET_SCHEDULER_TaskContext *tc)
1129 struct NeighborSendContext *send_context = cls;
1130 #if DEBUG_DV_GOSSIP_SEND
1131 char * encPeerAbout;
1134 struct DistantNeighbor *about;
1135 struct DirectNeighbor *to;
1136 struct FastGossipNeighborList *about_list;
1138 p2p_dv_MESSAGE_NeighborInfo *message;
1139 struct PendingMessage *pending_message;
1141 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "%s: Called with reason shutdown, shutting down!\n",
1146 GNUNET_i2s(&my_identity));
1148 send_context->task = GNUNET_SCHEDULER_NO_TASK;
1152 if (send_context->fast_gossip_list_head != NULL)
1154 about_list = send_context->fast_gossip_list_head;
1155 about = send_context->fast_gossip_list_head->about;
1156 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1157 send_context->fast_gossip_list_tail,
1159 GNUNET_free(about_list);
1163 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1164 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1165 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1166 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1167 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1168 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1169 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1170 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1171 * asynchronously enough to avoid major problems (-;
1173 about = GNUNET_CONTAINER_heap_walk_get_next (ctx.neighbor_min_heap);
1175 to = send_context->toNeighbor;
1177 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1179 (about->hidden == GNUNET_NO) &&
1182 (0 != memcmp (&about->identity,
1183 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1184 (about->pkey != NULL))
1186 #if DEBUG_DV_GOSSIP_SEND
1187 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1188 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "%s: Sending info about peer %s to directly connected peer %s\n",
1191 GNUNET_i2s(&my_identity),
1192 encPeerAbout, encPeerTo);
1193 GNUNET_free(encPeerAbout);
1194 GNUNET_free(encPeerTo);
1196 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1197 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1198 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1199 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1200 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1201 message->cost = htonl (about->cost);
1202 message->neighbor_id = htonl (about->our_id);
1204 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1205 memcpy (&message->neighbor,
1206 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1208 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1213 if (core_transmit_handle == NULL)
1214 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);
1218 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SERVICE: still in fast send mode\n");
1221 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SERVICE: entering slow send mode\n");
1226 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1234 * Handle START-message. This is the first message sent to us
1235 * by the client (can only be one!).
1237 * @param cls closure (always NULL)
1238 * @param client identification of the client
1239 * @param message the actual message
1242 handle_start (void *cls,
1243 struct GNUNET_SERVER_Client *client,
1244 const struct GNUNET_MessageHeader *message)
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Received `%s' request from client\n", "START");
1252 client_handle = client;
1254 GNUNET_SERVER_client_keep(client_handle);
1255 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1260 * Iterate over hash map entries for a distant neighbor,
1261 * if direct neighbor matches context call send message
1263 * @param cls closure, a DV_SendContext
1264 * @param key current key code
1265 * @param value value in the hash map
1266 * @return GNUNET_YES if we should continue to
1270 int send_iterator (void *cls,
1271 const GNUNET_HashCode * key,
1274 struct DV_SendContext *send_context = cls;
1275 struct DistantNeighbor *distant_neighbor = value;
1277 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1279 send_message_via(&my_identity, distant_neighbor, send_context);
1286 * Service server's handler for message send requests (which come
1287 * bubbling up to us through the DV plugin).
1289 * @param cls closure
1290 * @param client identification of the client
1291 * @param message the actual message
1293 void handle_dv_send_message (void *cls,
1294 struct GNUNET_SERVER_Client * client,
1295 const struct GNUNET_MessageHeader * message)
1297 struct GNUNET_DV_SendMessage *send_msg;
1298 struct GNUNET_DV_SendResultMessage *send_result_msg;
1299 struct PendingMessage *pending_message;
1301 size_t message_size;
1302 struct GNUNET_PeerIdentity *destination;
1303 struct GNUNET_PeerIdentity *direct;
1304 struct GNUNET_MessageHeader *message_buf;
1307 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1308 struct DV_SendContext *send_context;
1310 if (client_handle == NULL)
1312 client_handle = client;
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1316 else if (client_handle != client)
1318 client_handle = client;
1319 /* What should we do in this case, assert fail or just log the warning? */
1320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1321 "%s: Setting client handle (was a different client!)!\n", "dv");
1324 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1325 send_msg = (struct GNUNET_DV_SendMessage *)message;
1327 address_len = ntohl(send_msg->addrlen);
1328 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1329 message_size = ntohl(send_msg->msgbuf_size);
1331 GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size);
1332 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1333 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1334 message_buf = GNUNET_malloc(message_size);
1336 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1337 offset = 0; /* Offset starts at zero */
1339 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1340 offset += sizeof(struct GNUNET_PeerIdentity);
1342 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1343 offset += sizeof(struct GNUNET_PeerIdentity);
1346 memcpy(message_buf, &temp_pos[offset], message_size);
1347 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1349 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1350 dest_hash.encoding[4] = '\0';
1351 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);
1355 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1356 dest_hash.encoding[4] = '\0';
1357 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));
1359 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1361 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1362 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1363 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1364 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1366 send_context->importance = ntohl(send_msg->priority);
1367 send_context->timeout = send_msg->timeout;
1368 send_context->direct_peer = direct;
1369 send_context->distant_peer = destination;
1370 send_context->message = message_buf;
1371 send_context->message_size = message_size;
1372 send_context->send_result = send_result_msg;
1374 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1375 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1377 send_result_msg->result = htons(1);
1378 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1379 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1380 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1381 GNUNET_free(send_result_msg);
1383 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1385 if (client_handle != NULL)
1387 if (plugin_transmit_handle == NULL)
1389 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1390 sizeof(struct GNUNET_DV_SendResultMessage),
1391 GNUNET_TIME_UNIT_FOREVER_REL,
1392 &transmit_to_plugin, NULL);
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1399 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));
1406 GNUNET_free(message_buf);
1407 GNUNET_free(send_context);
1408 GNUNET_free(direct);
1409 GNUNET_free(destination);
1411 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1414 /** Forward declarations **/
1415 static int handle_dv_gossip_message (void *cls,
1416 const struct GNUNET_PeerIdentity *peer,
1417 const struct GNUNET_MessageHeader *message,
1418 struct GNUNET_TIME_Relative latency,
1421 static int handle_dv_disconnect_message (void *cls,
1422 const struct GNUNET_PeerIdentity *peer,
1423 const struct GNUNET_MessageHeader *message,
1424 struct GNUNET_TIME_Relative latency,
1426 /** End forward declarations **/
1430 * List of handlers for the messages understood by this
1433 * Hmm... will we need to register some handlers with core and
1434 * some handlers with our server here? Because core should be
1435 * getting the incoming DV messages (from whichever lower level
1436 * transport) and then our server should be getting messages
1437 * from the dv_plugin, right?
1439 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1440 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1441 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1442 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1446 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1447 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1448 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1453 * Free a DistantNeighbor node, including removing it
1454 * from the referer's list.
1457 distant_neighbor_free (struct DistantNeighbor *referee)
1459 struct DirectNeighbor *referrer;
1461 referrer = referee->referrer;
1462 if (referrer != NULL)
1464 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1465 referrer->referee_tail, referee);
1467 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_max_heap, referee->max_loc);
1468 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_min_heap, referee->min_loc);
1469 GNUNET_CONTAINER_multihashmap_remove_all (ctx.extended_neighbors,
1470 &referee->identity.hashPubKey);
1471 GNUNET_free (referee->pkey);
1472 GNUNET_free (referee);
1476 * Free a DirectNeighbor node, including removing it
1477 * from the referer's list.
1480 direct_neighbor_free (struct DirectNeighbor *direct)
1482 struct NeighborSendContext *send_context;
1483 struct FastGossipNeighborList *about_list;
1484 struct FastGossipNeighborList *prev_about;
1486 send_context = direct->send_context;
1488 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1489 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1491 about_list = send_context->fast_gossip_list_head;
1492 while (about_list != NULL)
1494 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1495 prev_about = about_list;
1496 about_list = about_list->next;
1497 GNUNET_free(prev_about);
1499 GNUNET_free(send_context);
1500 GNUNET_free(direct);
1504 * Multihashmap iterator for sending out disconnect messages
1507 * @param cls the peer that was disconnected
1508 * @param key key value stored under
1509 * @param value the direct neighbor to send disconnect to
1511 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1513 static int schedule_disconnect_messages (void *cls,
1514 const GNUNET_HashCode * key,
1517 struct DisconnectContext *disconnect_context = cls;
1518 struct DirectNeighbor *disconnected = disconnect_context->direct;
1519 struct DirectNeighbor *notify = value;
1520 struct PendingMessage *pending_message;
1521 p2p_dv_MESSAGE_Disconnect *disconnect_message;
1523 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1524 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
1526 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
1527 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1528 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
1529 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
1530 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
1531 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
1533 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1538 if (core_transmit_handle == NULL)
1539 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);
1545 * Multihashmap iterator for freeing extended neighbors.
1548 * @param key key value stored under
1549 * @param value the distant neighbor to be freed
1551 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1553 static int free_extended_neighbors (void *cls,
1554 const GNUNET_HashCode * key,
1557 struct DistantNeighbor *distant = value;
1558 distant_neighbor_free(distant);
1563 * Multihashmap iterator for freeing direct neighbors.
1566 * @param key key value stored under
1567 * @param value the direct neighbor to be freed
1569 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1571 static int free_direct_neighbors (void *cls,
1572 const GNUNET_HashCode * key,
1575 struct DirectNeighbor *direct = value;
1576 direct_neighbor_free(direct);
1581 * Task run during shutdown.
1587 shutdown_task (void *cls,
1588 const struct GNUNET_SCHEDULER_TaskContext *tc)
1591 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
1593 GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &free_extended_neighbors, NULL);
1594 GNUNET_CONTAINER_multihashmap_destroy(ctx.extended_neighbors);
1595 GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &free_direct_neighbors, NULL);
1596 GNUNET_CONTAINER_multihashmap_destroy(ctx.direct_neighbors);
1598 GNUNET_CONTAINER_heap_destroy(ctx.neighbor_max_heap);
1599 GNUNET_CONTAINER_heap_destroy(ctx.neighbor_min_heap);
1601 GNUNET_CORE_disconnect (coreAPI);
1602 GNUNET_PEERINFO_disconnect(peerinfo_handle);
1604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
1609 * To be called on core init/fail.
1611 void core_init (void *cls,
1612 struct GNUNET_CORE_Handle * server,
1613 const struct GNUNET_PeerIdentity *identity,
1614 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
1619 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
1620 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
1627 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
1632 * Iterator over hash map entries.
1634 * @param cls closure
1635 * @param key current key code
1636 * @param value value in the hash map
1637 * @return GNUNET_YES if we should continue to
1641 static int add_pkey_to_extended (void *cls,
1642 const GNUNET_HashCode * key,
1645 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
1646 struct DistantNeighbor *distant_neighbor = value;
1648 if (distant_neighbor->pkey == NULL)
1650 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1651 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1658 * Iterator over hash map entries.
1660 * @param cls closure
1661 * @param key current key code
1662 * @param value value in the hash map
1663 * @return GNUNET_YES if we should continue to
1667 static int update_matching_neighbors (void *cls,
1668 const GNUNET_HashCode * key,
1671 struct NeighborUpdateInfo * update_info = cls;
1672 struct DistantNeighbor *distant_neighbor = value;
1674 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
1676 /* same referrer, cost change! */
1677 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_max_heap,
1678 update_info->neighbor->max_loc, update_info->cost);
1679 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_min_heap,
1680 update_info->neighbor->min_loc, update_info->cost);
1681 update_info->neighbor->last_activity = update_info->now;
1682 update_info->neighbor->cost = update_info->cost;
1692 * Iterator over hash map entries.
1694 * @param cls closure (NULL)
1695 * @param key current key code
1696 * @param value value in the hash map (DistantNeighbor)
1697 * @return GNUNET_YES if we should continue to
1701 int print_neighbors (void *cls,
1702 const GNUNET_HashCode * key,
1705 struct DistantNeighbor *distant_neighbor = value;
1706 char my_shortname[5];
1707 char referrer_shortname[5];
1708 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1709 my_shortname[4] = '\0';
1710 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1711 referrer_shortname[4] = '\0';
1713 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);
1720 * Handles when a peer is either added due to being newly connected
1721 * or having been gossiped about, also called when the cost for a neighbor
1722 * needs to be updated.
1724 * @param peer identity of the peer whose info is being added/updated
1725 * @param pkey public key of the peer whose info is being added/updated
1726 * @param referrer_peer_id id to use when sending to 'peer'
1727 * @param referrer if this is a gossiped peer, who did we hear it from?
1728 * @param cost the cost of communicating with this peer via 'referrer'
1731 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
1732 unsigned int referrer_peer_id,
1733 struct DirectNeighbor *referrer, unsigned int cost)
1735 struct DistantNeighbor *neighbor;
1736 struct DistantNeighbor *max;
1737 struct GNUNET_TIME_Absolute now;
1738 struct NeighborUpdateInfo *neighbor_update;
1739 unsigned int our_id;
1741 now = GNUNET_TIME_absolute_get ();
1742 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, RAND_MAX - 1) + 1;
1744 neighbor = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
1746 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
1747 neighbor_update->neighbor = neighbor;
1748 neighbor_update->cost = cost;
1749 neighbor_update->now = now;
1750 neighbor_update->referrer = referrer;
1752 /* Either we do not know this peer, or we already do but via a different immediate peer */
1753 if ((neighbor == NULL) ||
1754 (GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1756 &update_matching_neighbors,
1757 neighbor_update) != GNUNET_SYSERR))
1760 if (cost > ctx.fisheye_depth)
1763 GNUNET_free(neighbor_update);
1766 if (ctx.max_table_size <=
1767 GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors))
1769 /* remove most expensive entry */
1770 max = GNUNET_CONTAINER_heap_peek (ctx.neighbor_max_heap);
1771 if (cost > max->cost)
1773 /* new entry most expensive, don't create */
1774 GNUNET_free(neighbor_update);
1779 /* only free if this is not a direct connection;
1780 we could theoretically have more direct
1781 connections than DV entries allowed total! */
1782 distant_neighbor_free (max);
1786 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
1787 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
1788 referrer->referee_tail, neighbor);
1789 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_max_heap,
1791 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_min_heap,
1793 neighbor->referrer = referrer;
1794 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
1795 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
1797 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1798 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1801 neighbor->pkey = pkey;
1803 neighbor->last_activity = now;
1804 neighbor->cost = cost;
1805 neighbor->referrer_id = referrer_peer_id;
1806 neighbor->our_id = our_id;
1808 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
1810 GNUNET_CONTAINER_multihashmap_put (ctx.extended_neighbors, &peer->hashPubKey,
1812 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors));
1825 GNUNET_free(neighbor_update);
1831 generate_hello_address (void *cls, size_t max, void *buf)
1833 struct HelloContext *hello_context = cls;
1839 if (hello_context->addresses_to_add == 0)
1842 /* Hello "address" will be concatenation of distant peer and direct peer identities */
1843 size = 2 * sizeof(struct GNUNET_PeerIdentity);
1844 GNUNET_assert(max >= size);
1846 addr_buffer = GNUNET_malloc(size);
1848 /* Copy the distant peer identity to buffer */
1849 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
1850 offset += sizeof(struct GNUNET_PeerIdentity);
1851 /* Copy the direct peer identity to buffer */
1852 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
1853 ret = GNUNET_HELLO_add_address ("dv",
1854 GNUNET_TIME_relative_to_absolute
1855 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
1858 hello_context->addresses_to_add--;
1860 GNUNET_free(addr_buffer);
1866 * Core handler for dv disconnect messages. These will be used
1867 * by us to tell transport via the dv plugin that a peer can
1868 * no longer be contacted by us via a certain address. We should
1869 * then propagate these messages on, given that the distance to
1870 * the peer indicates we would have gossiped about it to others.
1872 * @param cls closure
1873 * @param peer peer which sent the message (immediate sender)
1874 * @param message the message
1875 * @param latency the latency of the connection we received the message from
1876 * @param distance the distance to the immediate peer
1878 static int handle_dv_disconnect_message (void *cls,
1879 const struct GNUNET_PeerIdentity *peer,
1880 const struct GNUNET_MessageHeader *message,
1881 struct GNUNET_TIME_Relative latency,
1884 struct DirectNeighbor *referrer;
1885 struct DistantNeighbor *distant;
1886 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
1888 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
1890 return GNUNET_SYSERR; /* invalid message */
1893 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1895 if (referrer == NULL)
1898 distant = referrer->referee_head;
1899 while (distant != NULL)
1901 if (distant->referrer_id == ntohl(enc_message->peer_id))
1903 distant_neighbor_free(distant);
1912 * Core handler for dv gossip messages. These will be used
1913 * by us to create a HELLO message for the newly peer containing
1914 * which direct peer we can connect through, and what the cost
1915 * is. This HELLO will then be scheduled for validation by the
1916 * transport service so that it can be used by all others.
1918 * @param cls closure
1919 * @param peer peer which sent the message (immediate sender)
1920 * @param message the message
1921 * @param latency the latency of the connection we received the message from
1922 * @param distance the distance to the immediate peer
1924 static int handle_dv_gossip_message (void *cls,
1925 const struct GNUNET_PeerIdentity *peer,
1926 const struct GNUNET_MessageHeader *message,
1927 struct GNUNET_TIME_Relative latency,
1930 struct HelloContext *hello_context;
1931 struct GNUNET_HELLO_Message *hello_msg;
1932 struct DirectNeighbor *referrer;
1933 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
1935 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
1937 return GNUNET_SYSERR; /* invalid message */
1940 #if DEBUG_DV_GOSSIP_RECEIPT
1941 char * encPeerAbout;
1944 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
1945 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "%s: Receives %s message from peer %s about peer %s distance %d!\n", "dv", "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl (enc_message->cost) + 1);
1948 GNUNET_free(encPeerAbout);
1949 GNUNET_free(encPeerFrom);
1952 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1954 if (referrer == NULL)
1957 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
1958 ntohl (enc_message->neighbor_id),
1959 referrer, ntohl (enc_message->cost) + 1);
1961 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
1962 hello_context->direct_peer = peer;
1963 memcpy(&hello_context->distant_peer, &enc_message->neighbor, sizeof(struct GNUNET_PeerIdentity));
1964 hello_context->addresses_to_add = 1;
1965 hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context);
1967 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);
1968 GNUNET_free(hello_context);
1969 GNUNET_free(hello_msg);
1975 * Iterate over all currently known peers, add them to the
1976 * fast gossip list for this peer so we get DV routing information
1977 * out as fast as possible!
1979 * @param cls the direct neighbor we will gossip to
1980 * @param key the hashcode of the peer
1981 * @param value the distant neighbor we should add to the list
1983 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
1985 static int add_all_extended_peers (void *cls,
1986 const GNUNET_HashCode * key,
1989 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
1990 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
1991 struct FastGossipNeighborList *gossip_entry;
1993 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1994 return GNUNET_YES; /* Don't gossip to a peer about itself! */
1997 if (distant->hidden == GNUNET_YES)
1998 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2000 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2001 gossip_entry->about = distant;
2003 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2004 send_context->fast_gossip_list_tail,
2005 send_context->fast_gossip_list_tail,
2013 * Iterate over all current direct peers, add DISTANT newly connected
2014 * peer to the fast gossip list for that peer so we get DV routing
2015 * information out as fast as possible!
2017 * @param cls the newly connected neighbor we will gossip about
2018 * @param key the hashcode of the peer
2019 * @param value the direct neighbor we should gossip to
2021 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2023 static int add_distant_all_direct_neighbors (void *cls,
2024 const GNUNET_HashCode * key,
2027 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2028 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2029 struct NeighborSendContext *send_context = direct->send_context;
2030 struct FastGossipNeighborList *gossip_entry;
2032 if (distant == NULL)
2037 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2039 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2043 if (distant->hidden == GNUNET_YES)
2044 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2046 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2047 gossip_entry->about = distant;
2049 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2050 send_context->fast_gossip_list_tail,
2051 send_context->fast_gossip_list_tail,
2053 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2054 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2056 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2062 * Iterate over all current direct peers, add newly connected peer
2063 * to the fast gossip list for that peer so we get DV routing
2064 * information out as fast as possible!
2066 * @param cls the newly connected neighbor we will gossip about
2067 * @param key the hashcode of the peer
2068 * @param value the direct neighbor we should gossip to
2070 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2072 static int add_all_direct_neighbors (void *cls,
2073 const GNUNET_HashCode * key,
2076 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2077 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2078 struct DistantNeighbor *distant;
2079 struct NeighborSendContext *send_context = direct->send_context;
2080 struct FastGossipNeighborList *gossip_entry;
2082 distant = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &to->identity.hashPubKey);
2083 if (distant == NULL)
2088 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2090 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2094 if (distant->hidden == GNUNET_YES)
2095 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2097 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2098 gossip_entry->about = distant;
2100 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2101 send_context->fast_gossip_list_tail,
2102 send_context->fast_gossip_list_tail,
2104 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2105 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2107 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2113 process_peerinfo (void *cls,
2114 const struct GNUNET_PeerIdentity *peer,
2115 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2117 struct PeerIteratorContext *peerinfo_iterator = cls;
2118 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2120 if ((peer == NULL))/* && (neighbor->pkey == NULL))*/
2122 /* FIXME: Remove peer! */
2123 GNUNET_free(peerinfo_iterator);
2127 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2130 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2132 GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
2134 &add_pkey_to_extended,
2137 GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2139 GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &add_all_direct_neighbors, neighbor);
2140 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2146 * Method called whenever a peer connects.
2148 * @param cls closure
2149 * @param peer peer identity this notification is about
2150 * @param latency reported latency of the connection with peer
2151 * @param distance reported distance (DV) to peer
2153 void handle_core_connect (void *cls,
2154 const struct GNUNET_PeerIdentity * peer,
2155 struct GNUNET_TIME_Relative latency,
2158 struct DirectNeighbor *neighbor;
2159 struct DistantNeighbor *about;
2160 struct PeerIteratorContext *peerinfo_iterator;
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2166 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL))
2168 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2169 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2170 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2171 neighbor->send_context->toNeighbor = neighbor;
2172 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2174 GNUNET_CONTAINER_multihashmap_put (ctx.direct_neighbors,
2176 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2177 addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2178 peerinfo_iterator->neighbor = neighbor;
2179 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2182 GNUNET_TIME_UNIT_FOREVER_REL,
2186 /* Only add once we get the publicKey of this guy
2188 * neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2193 about = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &peer->hashPubKey);
2194 if ((GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2195 GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &add_distant_all_direct_neighbors, about);
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2206 * Method called whenever a given peer disconnects.
2208 * @param cls closure
2209 * @param peer peer identity this notification is about
2211 void handle_core_disconnect (void *cls,
2212 const struct GNUNET_PeerIdentity * peer)
2214 struct DirectNeighbor *neighbor;
2215 struct DistantNeighbor *referee;
2216 struct FindDestinationContext fdc;
2217 struct DisconnectContext disconnect_context;
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2220 "%s: Receives core peer disconnect message!\n", "dv");
2224 GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors, &peer->hashPubKey);
2225 if (neighbor == NULL)
2229 while (NULL != (referee = neighbor->referee_head))
2230 distant_neighbor_free (referee);
2235 GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &find_distant_peer, &fdc);
2237 if (fdc.dest != NULL)
2239 disconnect_context.direct = neighbor;
2240 disconnect_context.distant = fdc.dest;
2241 GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2244 GNUNET_assert (neighbor->referee_tail == NULL);
2245 GNUNET_CONTAINER_multihashmap_remove (ctx.direct_neighbors,
2246 &peer->hashPubKey, neighbor);
2247 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2248 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2249 GNUNET_free (neighbor);
2254 * Process dv requests.
2256 * @param cls closure
2257 * @param scheduler scheduler to use
2258 * @param server the initialized server
2259 * @param c configuration to use
2263 struct GNUNET_SCHEDULER_Handle *scheduler,
2264 struct GNUNET_SERVER_Handle *server,
2265 const struct GNUNET_CONFIGURATION_Handle *c)
2267 unsigned long long max_hosts;
2271 /* FIXME: Read from config, or calculate, or something other than this! */
2272 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2273 ctx.max_table_size = DEFAULT_DV_SIZE;
2274 ctx.fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2276 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2278 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts);
2281 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2283 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &ctx.max_table_size);
2286 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2288 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &ctx.fisheye_depth);
2291 ctx.neighbor_min_heap =
2292 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2293 ctx.neighbor_max_heap =
2294 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2296 ctx.direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2297 ctx.extended_neighbors =
2298 GNUNET_CONTAINER_multihashmap_create (ctx.max_table_size * 3);
2300 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2302 GNUNET_CORE_connect (sched,
2304 GNUNET_TIME_relative_get_forever(),
2305 NULL, /* FIXME: anything we want to pass around? */
2307 &handle_core_connect,
2308 &handle_core_disconnect,
2315 if (coreAPI == NULL)
2318 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2320 if (peerinfo_handle == NULL)
2322 GNUNET_CORE_disconnect(coreAPI);
2326 /* Scheduled the task to clean up when shutdown is called */
2327 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
2328 GNUNET_TIME_UNIT_FOREVER_REL,
2335 * The main function for the dv service.
2337 * @param argc number of arguments from the command line
2338 * @param argv command line arguments
2339 * @return 0 ok, 1 on error
2342 main (int argc, char *const *argv)
2344 return (GNUNET_OK ==
2345 GNUNET_SERVICE_run (argc,
2348 GNUNET_SERVICE_OPTION_NONE,
2349 &run, NULL)) ? 0 : 1;