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_WARNING, "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_WARNING, "`%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 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Queued client send receipt success message!\n");
782 if (client_handle != NULL)
784 if (plugin_transmit_handle == NULL)
786 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
787 sizeof(struct GNUNET_DV_SendResultMessage),
788 GNUNET_TIME_UNIT_FOREVER_REL,
789 &transmit_to_plugin, NULL);
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
797 memcpy (&cbuf[off], reply->msg, msize);
806 * Send a DV data message via DV.
808 * @param sender the original sender of the message
809 * @param specific_neighbor the specific DistantNeighbor to use, complete with referrer!
810 * @param send_context the send context
813 send_message_via (const struct GNUNET_PeerIdentity * sender,
814 const struct DistantNeighbor * specific_neighbor,
815 struct DV_SendContext *send_context)
817 p2p_dv_MESSAGE_Data *toSend;
818 unsigned int msg_size;
820 unsigned int recipient_id;
821 unsigned int sender_id;
822 struct DistantNeighbor *source;
823 struct PendingMessage *pending_message;
828 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
830 if (specific_neighbor == NULL)
832 /* target unknown to us, drop! */
833 return GNUNET_SYSERR;
835 recipient_id = specific_neighbor->referrer_id;
837 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
838 &sender->hashPubKey);
841 if (0 != (memcmp (&my_identity,
842 sender, sizeof (struct GNUNET_PeerIdentity))))
844 /* sender unknown to us, drop! */
845 return GNUNET_SYSERR;
847 sender_id = 0; /* 0 == us */
851 /* find out the number that we use when we gossip about
853 sender_id = source->our_id;
856 cost = specific_neighbor->cost;
857 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
858 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
859 pending_message->send_result = send_context->send_result;
860 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
861 toSend->header.size = htons (msg_size);
862 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
863 toSend->sender = htonl (sender_id);
864 toSend->recipient = htonl (recipient_id);
865 memcpy (&toSend[1], send_context->message, send_context->message_size);
868 memcpy(&shortname, GNUNET_i2s(&specific_neighbor->identity), 4);
870 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);
873 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
877 if (core_transmit_handle == NULL)
878 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);
880 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
887 * Send a DV data message via DV.
889 * @param recipient the ultimate recipient of this message
890 * @param sender the original sender of the message
891 * @param specific_neighbor the specific neighbor to send this message via
892 * @param message the packed message
893 * @param message_size size of the message
894 * @param importance what priority to send this message with
895 * @param timeout how long to possibly delay sending this message
898 send_message (const struct GNUNET_PeerIdentity * recipient,
899 const struct GNUNET_PeerIdentity * sender,
900 const struct DistantNeighbor * specific_neighbor,
901 const struct GNUNET_MessageHeader * message,
903 unsigned int importance, struct GNUNET_TIME_Relative timeout)
905 p2p_dv_MESSAGE_Data *toSend;
906 unsigned int msg_size;
908 unsigned int recipient_id;
909 unsigned int sender_id;
910 struct DistantNeighbor *target;
911 struct DistantNeighbor *source;
912 struct PendingMessage *pending_message;
914 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
916 target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
917 &recipient->hashPubKey);
920 /* target unknown to us, drop! */
921 return GNUNET_SYSERR;
923 recipient_id = target->referrer_id;
925 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
926 &sender->hashPubKey);
929 if (0 != (memcmp (&my_identity,
930 sender, sizeof (struct GNUNET_PeerIdentity))))
932 /* sender unknown to us, drop! */
933 return GNUNET_SYSERR;
935 sender_id = 0; /* 0 == us */
939 /* find out the number that we use when we gossip about
941 sender_id = source->our_id;
945 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
946 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
947 pending_message->send_result = NULL;
948 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
949 toSend->header.size = htons (msg_size);
950 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
951 toSend->sender = htonl (sender_id);
952 toSend->recipient = htonl (recipient_id);
953 memcpy (&toSend[1], message, message_size);
955 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
960 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));
962 if (core_transmit_handle == NULL)
963 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
970 * Core handler for dv data messages. Whatever this message
971 * contains all we really have to do is rip it out of its
972 * DV layering and give it to our pal the DV plugin to report
976 * @param peer peer which sent the message (immediate sender)
977 * @param message the message
978 * @param latency the latency of the connection we received the message from
979 * @param distance the distance to the immediate peer
981 static int handle_dv_data_message (void *cls,
982 const struct GNUNET_PeerIdentity * peer,
983 const struct GNUNET_MessageHeader * message,
984 struct GNUNET_TIME_Relative latency,
987 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
988 const struct GNUNET_MessageHeader *packed_message;
989 struct DirectNeighbor *dn;
990 struct DistantNeighbor *pos;
991 unsigned int sid; /* Sender id */
992 unsigned int tid; /* Target id */
993 struct GNUNET_PeerIdentity original_sender;
994 struct GNUNET_PeerIdentity destination;
995 struct FindDestinationContext fdc;
997 size_t packed_message_size;
1001 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "%s: Receives %s message size %d, packed message size %d!\n", "dv", "DV DATA", ntohs(incoming->header.size), packed_message_size);
1007 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "`%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));
1013 return GNUNET_SYSERR;
1016 dn = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "%s: dn NULL!\n", "dv");
1026 sid = ntohl (incoming->sender);
1027 pos = dn->referee_head;
1028 while ((NULL != pos) && (pos->referrer_id != sid))
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "%s: unknown sender (%d), size of extended_peers is %d!\n", "dv", ntohl(incoming->sender), GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors));
1036 /* unknown sender */
1039 original_sender = pos->identity;
1040 tid = ntohl (incoming->recipient);
1045 cbuf = (char *)&incoming[1];
1047 while(offset < packed_message_size)
1049 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "%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);
1054 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1055 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1056 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1057 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1059 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
1061 offset += ntohs(packed_message->size);
1068 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1071 /* FIXME: this is the *only* per-request operation we have in DV
1072 that is O(n) in relation to the number of connected peers; a
1073 hash-table lookup could easily solve this (minor performance
1077 GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap,
1078 &find_destination, &fdc);
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1085 if (fdc.dest == NULL)
1089 destination = fdc.dest->identity;
1091 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1093 /* FIXME: create stat: routing loop-discard! */
1095 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1100 /* At this point we have a message, and we need to forward it on to the
1103 /* FIXME: Can't send message on, we have to behave.
1104 * We have to tell core we have a message for the next peer, and let
1105 * transport do transport selection on how to get this message to 'em */
1106 /*ret = send_message (&destination,
1108 packed_message, DV_PRIORITY, DV_DELAY);*/
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "%s: Sends message size %d on!\n", "dv", packed_message_size);
1113 ret = send_message(&destination, &original_sender, NULL, packed_message, packed_message_size, default_dv_priority, GNUNET_TIME_relative_get_forever());
1115 if (ret != GNUNET_SYSERR)
1118 return GNUNET_SYSERR;
1123 * Scheduled task which gossips about known direct peers to other connected
1124 * peers. Will run until called with reason shutdown.
1127 neighbor_send_task (void *cls,
1128 const struct GNUNET_SCHEDULER_TaskContext *tc)
1130 struct NeighborSendContext *send_context = cls;
1131 #if DEBUG_DV_GOSSIP_SEND
1132 char * encPeerAbout;
1135 struct DistantNeighbor *about;
1136 struct DirectNeighbor *to;
1137 struct FastGossipNeighborList *about_list;
1139 p2p_dv_MESSAGE_NeighborInfo *message;
1140 struct PendingMessage *pending_message;
1142 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "%s: Called with reason shutdown, shutting down!\n",
1147 GNUNET_i2s(&my_identity));
1149 send_context->task = GNUNET_SCHEDULER_NO_TASK;
1153 if (send_context->fast_gossip_list_head != NULL)
1155 about_list = send_context->fast_gossip_list_head;
1156 about = send_context->fast_gossip_list_head->about;
1157 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1158 send_context->fast_gossip_list_tail,
1160 GNUNET_free(about_list);
1164 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1165 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1166 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1167 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1168 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1169 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1170 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1171 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1172 * asynchronously enough to avoid major problems (-;
1174 about = GNUNET_CONTAINER_heap_walk_get_next (ctx.neighbor_min_heap);
1176 to = send_context->toNeighbor;
1178 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1180 (about->hidden == GNUNET_NO) &&
1183 (0 != memcmp (&about->identity,
1184 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1185 (about->pkey != NULL))
1187 #if DEBUG_DV_GOSSIP_SEND
1188 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1189 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1191 "%s: Sending info about peer %s to directly connected peer %s\n",
1192 GNUNET_i2s(&my_identity),
1193 encPeerAbout, encPeerTo);
1194 GNUNET_free(encPeerAbout);
1195 GNUNET_free(encPeerTo);
1197 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1198 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1199 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1200 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1201 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1202 message->cost = htonl (about->cost);
1203 message->neighbor_id = htonl (about->our_id);
1205 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1206 memcpy (&message->neighbor,
1207 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1209 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1214 if (core_transmit_handle == NULL)
1215 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);
1219 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1221 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1222 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1226 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1227 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1235 * Handle START-message. This is the first message sent to us
1236 * by the client (can only be one!).
1238 * @param cls closure (always NULL)
1239 * @param client identification of the client
1240 * @param message the actual message
1243 handle_start (void *cls,
1244 struct GNUNET_SERVER_Client *client,
1245 const struct GNUNET_MessageHeader *message)
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 "Received `%s' request from client\n", "START");
1253 client_handle = client;
1255 GNUNET_SERVER_client_keep(client_handle);
1256 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1261 * Iterate over hash map entries for a distant neighbor,
1262 * if direct neighbor matches context call send message
1264 * @param cls closure, a DV_SendContext
1265 * @param key current key code
1266 * @param value value in the hash map
1267 * @return GNUNET_YES if we should continue to
1271 int send_iterator (void *cls,
1272 const GNUNET_HashCode * key,
1275 struct DV_SendContext *send_context = cls;
1276 struct DistantNeighbor *distant_neighbor = value;
1278 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1280 send_message_via(&my_identity, distant_neighbor, send_context);
1287 * Service server's handler for message send requests (which come
1288 * bubbling up to us through the DV plugin).
1290 * @param cls closure
1291 * @param client identification of the client
1292 * @param message the actual message
1294 void handle_dv_send_message (void *cls,
1295 struct GNUNET_SERVER_Client * client,
1296 const struct GNUNET_MessageHeader * message)
1298 struct GNUNET_DV_SendMessage *send_msg;
1299 struct GNUNET_DV_SendResultMessage *send_result_msg;
1300 struct PendingMessage *pending_message;
1302 size_t message_size;
1303 struct GNUNET_PeerIdentity *destination;
1304 struct GNUNET_PeerIdentity *direct;
1305 struct GNUNET_MessageHeader *message_buf;
1308 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1309 struct DV_SendContext *send_context;
1311 if (client_handle == NULL)
1313 client_handle = client;
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1317 else if (client_handle != client)
1319 client_handle = client;
1320 /* What should we do in this case, assert fail or just log the warning? */
1321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1322 "%s: Setting client handle (was a different client!)!\n", "dv");
1325 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1326 send_msg = (struct GNUNET_DV_SendMessage *)message;
1328 address_len = ntohl(send_msg->addrlen);
1329 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1330 message_size = ntohl(send_msg->msgbuf_size);
1333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1334 "%s: Receives %s message size %u!\n\n\n", "dv", "SEND", message_size);
1336 GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size);
1337 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1338 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1339 message_buf = GNUNET_malloc(message_size);
1341 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1342 offset = 0; /* Offset starts at zero */
1344 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1345 offset += sizeof(struct GNUNET_PeerIdentity);
1347 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1348 offset += sizeof(struct GNUNET_PeerIdentity);
1351 memcpy(message_buf, &temp_pos[offset], message_size);
1352 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1354 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1355 dest_hash.encoding[4] = '\0';
1356 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);
1360 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1361 dest_hash.encoding[4] = '\0';
1362 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "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));
1364 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1366 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1367 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1368 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1369 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1371 send_context->importance = ntohl(send_msg->priority);
1372 send_context->timeout = send_msg->timeout;
1373 send_context->direct_peer = direct;
1374 send_context->distant_peer = destination;
1375 send_context->message = message_buf;
1376 send_context->message_size = message_size;
1377 send_context->send_result = send_result_msg;
1379 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1380 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1382 send_result_msg->result = htons(1);
1383 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1384 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1385 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1386 GNUNET_free(send_result_msg);
1388 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1390 if (client_handle != NULL)
1392 if (plugin_transmit_handle == NULL)
1394 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1395 sizeof(struct GNUNET_DV_SendResultMessage),
1396 GNUNET_TIME_UNIT_FOREVER_REL,
1397 &transmit_to_plugin, NULL);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1404 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));
1411 GNUNET_free(message_buf);
1412 GNUNET_free(send_context);
1413 GNUNET_free(direct);
1414 GNUNET_free(destination);
1416 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1419 /** Forward declarations **/
1420 static int handle_dv_gossip_message (void *cls,
1421 const struct GNUNET_PeerIdentity *peer,
1422 const struct GNUNET_MessageHeader *message,
1423 struct GNUNET_TIME_Relative latency,
1426 static int handle_dv_disconnect_message (void *cls,
1427 const struct GNUNET_PeerIdentity *peer,
1428 const struct GNUNET_MessageHeader *message,
1429 struct GNUNET_TIME_Relative latency,
1431 /** End forward declarations **/
1435 * List of handlers for the messages understood by this
1438 * Hmm... will we need to register some handlers with core and
1439 * some handlers with our server here? Because core should be
1440 * getting the incoming DV messages (from whichever lower level
1441 * transport) and then our server should be getting messages
1442 * from the dv_plugin, right?
1444 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1445 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1446 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1447 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1451 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1452 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1453 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1458 * Free a DistantNeighbor node, including removing it
1459 * from the referer's list.
1462 distant_neighbor_free (struct DistantNeighbor *referee)
1464 struct DirectNeighbor *referrer;
1466 referrer = referee->referrer;
1467 if (referrer != NULL)
1469 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1470 referrer->referee_tail, referee);
1472 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_max_heap, referee->max_loc);
1473 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_min_heap, referee->min_loc);
1474 GNUNET_CONTAINER_multihashmap_remove_all (ctx.extended_neighbors,
1475 &referee->identity.hashPubKey);
1476 GNUNET_free (referee->pkey);
1477 GNUNET_free (referee);
1481 * Free a DirectNeighbor node, including removing it
1482 * from the referer's list.
1485 direct_neighbor_free (struct DirectNeighbor *direct)
1487 struct NeighborSendContext *send_context;
1488 struct FastGossipNeighborList *about_list;
1489 struct FastGossipNeighborList *prev_about;
1491 send_context = direct->send_context;
1493 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1494 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1496 about_list = send_context->fast_gossip_list_head;
1497 while (about_list != NULL)
1499 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1500 prev_about = about_list;
1501 about_list = about_list->next;
1502 GNUNET_free(prev_about);
1504 GNUNET_free(send_context);
1505 GNUNET_free(direct);
1509 * Multihashmap iterator for sending out disconnect messages
1512 * @param cls the peer that was disconnected
1513 * @param key key value stored under
1514 * @param value the direct neighbor to send disconnect to
1516 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1518 static int schedule_disconnect_messages (void *cls,
1519 const GNUNET_HashCode * key,
1522 struct DisconnectContext *disconnect_context = cls;
1523 struct DirectNeighbor *disconnected = disconnect_context->direct;
1524 struct DirectNeighbor *notify = value;
1525 struct PendingMessage *pending_message;
1526 p2p_dv_MESSAGE_Disconnect *disconnect_message;
1528 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1529 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
1531 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
1532 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1533 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
1534 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
1535 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
1536 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
1538 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1543 if (core_transmit_handle == NULL)
1544 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);
1550 * Multihashmap iterator for freeing extended neighbors.
1553 * @param key key value stored under
1554 * @param value the distant neighbor to be freed
1556 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1558 static int free_extended_neighbors (void *cls,
1559 const GNUNET_HashCode * key,
1562 struct DistantNeighbor *distant = value;
1563 distant_neighbor_free(distant);
1568 * Multihashmap iterator for freeing direct neighbors.
1571 * @param key key value stored under
1572 * @param value the direct neighbor to be freed
1574 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1576 static int free_direct_neighbors (void *cls,
1577 const GNUNET_HashCode * key,
1580 struct DirectNeighbor *direct = value;
1581 direct_neighbor_free(direct);
1586 * Task run during shutdown.
1592 shutdown_task (void *cls,
1593 const struct GNUNET_SCHEDULER_TaskContext *tc)
1596 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
1598 GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &free_extended_neighbors, NULL);
1599 GNUNET_CONTAINER_multihashmap_destroy(ctx.extended_neighbors);
1600 GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &free_direct_neighbors, NULL);
1601 GNUNET_CONTAINER_multihashmap_destroy(ctx.direct_neighbors);
1603 GNUNET_CONTAINER_heap_destroy(ctx.neighbor_max_heap);
1604 GNUNET_CONTAINER_heap_destroy(ctx.neighbor_min_heap);
1606 GNUNET_CORE_disconnect (coreAPI);
1607 GNUNET_PEERINFO_disconnect(peerinfo_handle);
1609 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
1614 * To be called on core init/fail.
1616 void core_init (void *cls,
1617 struct GNUNET_CORE_Handle * server,
1618 const struct GNUNET_PeerIdentity *identity,
1619 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
1624 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
1625 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
1632 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
1637 * Iterator over hash map entries.
1639 * @param cls closure
1640 * @param key current key code
1641 * @param value value in the hash map
1642 * @return GNUNET_YES if we should continue to
1646 static int add_pkey_to_extended (void *cls,
1647 const GNUNET_HashCode * key,
1650 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
1651 struct DistantNeighbor *distant_neighbor = value;
1653 if (distant_neighbor->pkey == NULL)
1655 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1656 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1663 * Iterator over hash map entries.
1665 * @param cls closure
1666 * @param key current key code
1667 * @param value value in the hash map
1668 * @return GNUNET_YES if we should continue to
1672 static int update_matching_neighbors (void *cls,
1673 const GNUNET_HashCode * key,
1676 struct NeighborUpdateInfo * update_info = cls;
1677 struct DistantNeighbor *distant_neighbor = value;
1679 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
1681 /* same referrer, cost change! */
1682 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_max_heap,
1683 update_info->neighbor->max_loc, update_info->cost);
1684 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_min_heap,
1685 update_info->neighbor->min_loc, update_info->cost);
1686 update_info->neighbor->last_activity = update_info->now;
1687 update_info->neighbor->cost = update_info->cost;
1697 * Iterator over hash map entries.
1699 * @param cls closure (NULL)
1700 * @param key current key code
1701 * @param value value in the hash map (DistantNeighbor)
1702 * @return GNUNET_YES if we should continue to
1706 int print_neighbors (void *cls,
1707 const GNUNET_HashCode * key,
1710 struct DistantNeighbor *distant_neighbor = value;
1711 char my_shortname[5];
1712 char referrer_shortname[5];
1713 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1714 my_shortname[4] = '\0';
1715 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1716 referrer_shortname[4] = '\0';
1718 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);
1725 * Handles when a peer is either added due to being newly connected
1726 * or having been gossiped about, also called when the cost for a neighbor
1727 * needs to be updated.
1729 * @param peer identity of the peer whose info is being added/updated
1730 * @param pkey public key of the peer whose info is being added/updated
1731 * @param referrer_peer_id id to use when sending to 'peer'
1732 * @param referrer if this is a gossiped peer, who did we hear it from?
1733 * @param cost the cost of communicating with this peer via 'referrer'
1736 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
1737 unsigned int referrer_peer_id,
1738 struct DirectNeighbor *referrer, unsigned int cost)
1740 struct DistantNeighbor *neighbor;
1741 struct DistantNeighbor *max;
1742 struct GNUNET_TIME_Absolute now;
1743 struct NeighborUpdateInfo *neighbor_update;
1744 unsigned int our_id;
1746 now = GNUNET_TIME_absolute_get ();
1747 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, RAND_MAX - 1) + 1;
1749 neighbor = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
1751 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
1752 neighbor_update->neighbor = neighbor;
1753 neighbor_update->cost = cost;
1754 neighbor_update->now = now;
1755 neighbor_update->referrer = referrer;
1757 /* Either we do not know this peer, or we already do but via a different immediate peer */
1758 if ((neighbor == NULL) ||
1759 (GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1761 &update_matching_neighbors,
1762 neighbor_update) != GNUNET_SYSERR))
1765 if (cost > ctx.fisheye_depth)
1768 GNUNET_free(neighbor_update);
1771 if (ctx.max_table_size <=
1772 GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors))
1774 /* remove most expensive entry */
1775 max = GNUNET_CONTAINER_heap_peek (ctx.neighbor_max_heap);
1776 if (cost > max->cost)
1778 /* new entry most expensive, don't create */
1779 GNUNET_free(neighbor_update);
1784 /* only free if this is not a direct connection;
1785 we could theoretically have more direct
1786 connections than DV entries allowed total! */
1787 distant_neighbor_free (max);
1791 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
1792 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
1793 referrer->referee_tail, neighbor);
1794 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_max_heap,
1796 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_min_heap,
1798 neighbor->referrer = referrer;
1799 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
1800 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
1802 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1803 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1806 neighbor->pkey = pkey;
1808 neighbor->last_activity = now;
1809 neighbor->cost = cost;
1810 neighbor->referrer_id = referrer_peer_id;
1811 neighbor->our_id = our_id;
1813 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
1815 GNUNET_CONTAINER_multihashmap_put (ctx.extended_neighbors, &peer->hashPubKey,
1817 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors));
1829 GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &print_neighbors, NULL);
1831 GNUNET_free(neighbor_update);
1837 generate_hello_address (void *cls, size_t max, void *buf)
1839 struct HelloContext *hello_context = cls;
1845 if (hello_context->addresses_to_add == 0)
1848 /* Hello "address" will be concatenation of distant peer and direct peer identities */
1849 size = 2 * sizeof(struct GNUNET_PeerIdentity);
1850 GNUNET_assert(max >= size);
1852 addr_buffer = GNUNET_malloc(size);
1854 /* Copy the distant peer identity to buffer */
1855 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
1856 offset += sizeof(struct GNUNET_PeerIdentity);
1857 /* Copy the direct peer identity to buffer */
1858 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
1859 ret = GNUNET_HELLO_add_address ("dv",
1860 GNUNET_TIME_relative_to_absolute
1861 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
1864 hello_context->addresses_to_add--;
1866 GNUNET_free(addr_buffer);
1872 * Core handler for dv disconnect messages. These will be used
1873 * by us to tell transport via the dv plugin that a peer can
1874 * no longer be contacted by us via a certain address. We should
1875 * then propagate these messages on, given that the distance to
1876 * the peer indicates we would have gossiped about it to others.
1878 * @param cls closure
1879 * @param peer peer which sent the message (immediate sender)
1880 * @param message the message
1881 * @param latency the latency of the connection we received the message from
1882 * @param distance the distance to the immediate peer
1884 static int handle_dv_disconnect_message (void *cls,
1885 const struct GNUNET_PeerIdentity *peer,
1886 const struct GNUNET_MessageHeader *message,
1887 struct GNUNET_TIME_Relative latency,
1890 struct DirectNeighbor *referrer;
1891 struct DistantNeighbor *distant;
1892 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
1894 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
1896 return GNUNET_SYSERR; /* invalid message */
1899 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1901 if (referrer == NULL)
1904 distant = referrer->referee_head;
1905 while (distant != NULL)
1907 if (distant->referrer_id == ntohl(enc_message->peer_id))
1909 distant_neighbor_free(distant);
1918 * Core handler for dv gossip messages. These will be used
1919 * by us to create a HELLO message for the newly peer containing
1920 * which direct peer we can connect through, and what the cost
1921 * is. This HELLO will then be scheduled for validation by the
1922 * transport service so that it can be used by all others.
1924 * @param cls closure
1925 * @param peer peer which sent the message (immediate sender)
1926 * @param message the message
1927 * @param latency the latency of the connection we received the message from
1928 * @param distance the distance to the immediate peer
1930 static int handle_dv_gossip_message (void *cls,
1931 const struct GNUNET_PeerIdentity *peer,
1932 const struct GNUNET_MessageHeader *message,
1933 struct GNUNET_TIME_Relative latency,
1936 struct HelloContext *hello_context;
1937 struct GNUNET_HELLO_Message *hello_msg;
1938 struct DirectNeighbor *referrer;
1939 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
1941 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
1943 return GNUNET_SYSERR; /* invalid message */
1946 #if DEBUG_DV_GOSSIP_RECEIPT
1947 char * encPeerAbout;
1950 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
1951 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953 "%s: Receives %s message from peer %s about peer %s distance %d!\n", "dv", "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl (enc_message->cost) + 1);
1954 GNUNET_free(encPeerAbout);
1955 GNUNET_free(encPeerFrom);
1958 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1960 if (referrer == NULL)
1963 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
1964 ntohl (enc_message->neighbor_id),
1965 referrer, ntohl (enc_message->cost) + 1);
1967 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
1968 hello_context->direct_peer = peer;
1969 memcpy(&hello_context->distant_peer, &enc_message->neighbor, sizeof(struct GNUNET_PeerIdentity));
1970 hello_context->addresses_to_add = 1;
1971 hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context);
1973 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);
1974 GNUNET_free(hello_context);
1975 GNUNET_free(hello_msg);
1981 * Iterate over all currently known peers, add them to the
1982 * fast gossip list for this peer so we get DV routing information
1983 * out as fast as possible!
1985 * @param cls the direct neighbor we will gossip to
1986 * @param key the hashcode of the peer
1987 * @param value the distant neighbor we should add to the list
1989 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
1991 static int add_all_extended_peers (void *cls,
1992 const GNUNET_HashCode * key,
1995 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
1996 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
1997 struct FastGossipNeighborList *gossip_entry;
1999 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2000 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2002 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: adding extended neighbor to fast send list\n");
2004 if (distant->hidden == GNUNET_YES)
2005 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2007 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2008 gossip_entry->about = distant;
2010 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2011 send_context->fast_gossip_list_tail,
2012 send_context->fast_gossip_list_tail,
2020 * Iterate over all current direct peers, add newly connected peer
2021 * to the fast gossip list for that peer so we get DV routing
2022 * information out as fast as possible!
2024 * @param cls the newly connected neighbor we will gossip about
2025 * @param key the hashcode of the peer
2026 * @param value the direct neighbor we should gossip to
2028 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2030 static int add_all_direct_neighbors (void *cls,
2031 const GNUNET_HashCode * key,
2034 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2035 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2036 struct DistantNeighbor *distant;
2037 struct NeighborSendContext *send_context = direct->send_context;
2038 struct FastGossipNeighborList *gossip_entry;
2040 distant = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &to->identity.hashPubKey);
2041 if (distant == NULL)
2044 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2045 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2047 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: adding new DISTANT neighbor to fast send list\n");
2049 if (distant->hidden == GNUNET_YES)
2050 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2052 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2053 gossip_entry->about = distant;
2055 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2056 send_context->fast_gossip_list_tail,
2057 send_context->fast_gossip_list_tail,
2059 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2060 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2062 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2068 process_peerinfo (void *cls,
2069 const struct GNUNET_PeerIdentity *peer,
2070 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2072 struct PeerIteratorContext *peerinfo_iterator = cls;
2073 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2075 if ((peer == NULL))/* && (neighbor->pkey == NULL))*/
2077 /* FIXME: Remove peer! */
2078 GNUNET_free(peerinfo_iterator);
2082 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2085 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2087 GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
2089 &add_pkey_to_extended,
2092 GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2094 GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &add_all_direct_neighbors, neighbor);
2095 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2101 * Method called whenever a peer connects.
2103 * @param cls closure
2104 * @param peer peer identity this notification is about
2105 * @param latency reported latency of the connection with peer
2106 * @param distance reported distance (DV) to peer
2108 void handle_core_connect (void *cls,
2109 const struct GNUNET_PeerIdentity * peer,
2110 struct GNUNET_TIME_Relative latency,
2113 struct DirectNeighbor *neighbor;
2114 struct DistantNeighbor *about;
2115 struct PeerIteratorContext *peerinfo_iterator;
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2121 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL))
2123 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2124 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2125 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2126 neighbor->send_context->toNeighbor = neighbor;
2127 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2129 GNUNET_CONTAINER_multihashmap_put (ctx.direct_neighbors,
2131 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2132 addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2133 peerinfo_iterator->neighbor = neighbor;
2134 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2137 GNUNET_TIME_UNIT_FOREVER_REL,
2141 /* Only add once we get the publicKey of this guy
2143 * neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2148 about = GNUNET_CONTAINER_multihashmap_get(ctx.extended_neighbors, &peer->hashPubKey);
2149 if ((GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2150 GNUNET_CONTAINER_multihashmap_iterate(ctx.direct_neighbors, &add_all_direct_neighbors, about);
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2154 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2161 * Method called whenever a given peer disconnects.
2163 * @param cls closure
2164 * @param peer peer identity this notification is about
2166 void handle_core_disconnect (void *cls,
2167 const struct GNUNET_PeerIdentity * peer)
2169 struct DirectNeighbor *neighbor;
2170 struct DistantNeighbor *referee;
2171 struct FindDestinationContext fdc;
2172 struct DisconnectContext disconnect_context;
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "%s: Receives core peer disconnect message!\n", "dv");
2179 GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors, &peer->hashPubKey);
2180 if (neighbor == NULL)
2184 while (NULL != (referee = neighbor->referee_head))
2185 distant_neighbor_free (referee);
2190 GNUNET_CONTAINER_multihashmap_iterate (ctx.extended_neighbors, &find_distant_peer, &fdc);
2192 if (fdc.dest != NULL)
2194 disconnect_context.direct = neighbor;
2195 disconnect_context.distant = fdc.dest;
2196 GNUNET_CONTAINER_multihashmap_iterate (ctx.direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2199 GNUNET_assert (neighbor->referee_tail == NULL);
2200 GNUNET_CONTAINER_multihashmap_remove (ctx.direct_neighbors,
2201 &peer->hashPubKey, neighbor);
2202 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2203 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2204 GNUNET_free (neighbor);
2209 * Process dv requests.
2211 * @param cls closure
2212 * @param scheduler scheduler to use
2213 * @param server the initialized server
2214 * @param c configuration to use
2218 struct GNUNET_SCHEDULER_Handle *scheduler,
2219 struct GNUNET_SERVER_Handle *server,
2220 const struct GNUNET_CONFIGURATION_Handle *c)
2222 unsigned long long max_hosts;
2226 /* FIXME: Read from config, or calculate, or something other than this! */
2227 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2228 ctx.max_table_size = DEFAULT_DV_SIZE;
2229 ctx.fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2231 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2233 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts);
2236 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2238 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &ctx.max_table_size);
2241 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2243 GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &ctx.fisheye_depth);
2246 ctx.neighbor_min_heap =
2247 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2248 ctx.neighbor_max_heap =
2249 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2251 ctx.direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2252 ctx.extended_neighbors =
2253 GNUNET_CONTAINER_multihashmap_create (ctx.max_table_size * 3);
2255 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2257 GNUNET_CORE_connect (sched,
2259 GNUNET_TIME_relative_get_forever(),
2260 NULL, /* FIXME: anything we want to pass around? */
2262 &handle_core_connect,
2263 &handle_core_disconnect,
2270 if (coreAPI == NULL)
2273 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2275 if (peerinfo_handle == NULL)
2277 GNUNET_CORE_disconnect(coreAPI);
2281 /* Scheduled the task to clean up when shutdown is called */
2282 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
2283 GNUNET_TIME_UNIT_FOREVER_REL,
2290 * The main function for the dv service.
2292 * @param argc number of arguments from the command line
2293 * @param argv command line arguments
2294 * @return 0 ok, 1 on error
2297 main (int argc, char *const *argv)
2299 return (GNUNET_OK ==
2300 GNUNET_SERVICE_run (argc,
2303 GNUNET_SERVICE_OPTION_NONE,
2304 &run, NULL)) ? 0 : 1;