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 * For testing mostly, remember only the
50 * shortest path to a distant neighbor.
52 #define AT_MOST_ONE GNUNET_NO
54 #define USE_PEER_ID GNUNET_YES
57 * How often do we check about sending out more peer information (if
58 * we are connected to no peers previously).
60 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
63 * How long do we wait at most between sending out information?
65 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
68 * How long can we have not heard from a peer and
69 * still have it in our tables?
71 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
74 * Priority for gossip.
76 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
79 * How often should we check if expiration time has elapsed for
82 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
85 * How long to allow a message to be delayed?
87 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
90 * Priority to use for DV data messages.
95 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
97 #define DIRECT_NEIGHBOR_COST 1
100 * The default number of direct connections to store in DV (max)
102 #define DEFAULT_DIRECT_CONNECTIONS 50
105 * The default size of direct + extended peers in DV (max)
107 #define DEFAULT_DV_SIZE 100
110 * The default fisheye depth, from how many hops away will
113 #define DEFAULT_FISHEYE_DEPTH 4
116 * Linked list of messages to send to clients.
118 struct PendingMessage
121 * Pointer to next item in the list
123 struct PendingMessage *next;
126 * Pointer to previous item in the list
128 struct PendingMessage *prev;
131 * The PeerIdentity to send to
133 struct GNUNET_PeerIdentity recipient;
136 * The result of message sending.
138 struct GNUNET_DV_SendResultMessage *send_result;
141 * Message importance level.
143 unsigned int importance;
148 unsigned int msg_size;
151 * How long to wait before sending message.
153 struct GNUNET_TIME_Relative timeout;
156 * Actual message to be sent; // avoid allocation
158 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
162 struct FastGossipNeighborList
165 * Next element of DLL
167 struct FastGossipNeighborList *next;
170 * Prev element of DLL
172 struct FastGossipNeighborList *prev;
175 * The neighbor to gossip about
177 struct DistantNeighbor *about;
181 * Context created whenever a direct peer connects to us,
182 * used to gossip other peers to it.
184 struct NeighborSendContext
187 * The peer we will gossip to.
189 struct DirectNeighbor *toNeighbor;
192 * The task associated with this context.
194 GNUNET_SCHEDULER_TaskIdentifier task;
197 * Head of DLL of peers to gossip about
198 * as fast as possible to this peer, for initial
201 struct FastGossipNeighborList *fast_gossip_list_head;
204 * Tail of DLL of peers to gossip about
205 * as fast as possible to this peer, for initial
208 struct FastGossipNeighborList *fast_gossip_list_tail;
214 * Struct to hold information for updating existing neighbors
216 struct NeighborUpdateInfo
224 * The existing neighbor
226 struct DistantNeighbor *neighbor;
229 * The referrer of the possibly existing peer
231 struct DirectNeighbor *referrer;
234 * The time we heard about this peer
236 struct GNUNET_TIME_Absolute now;
239 * Peer id this peer uses to refer to neighbor.
241 unsigned int referrer_peer_id;
246 * Struct to store a single message received with
249 struct UnknownSenderMessage
252 * Message sender (immediate)
254 struct GNUNET_PeerIdentity sender;
257 * The actual message received
259 struct GNUNET_MessageHeader *message;
262 * Latency of connection
264 struct GNUNET_TIME_Relative latency;
267 * Distance to destination
278 * Struct where actual neighbor information is stored,
279 * referenced by min_heap and max_heap. Freeing dealt
280 * with when items removed from hashmap.
282 struct DirectNeighbor
285 * Identity of neighbor.
287 struct GNUNET_PeerIdentity identity;
290 * PublicKey of neighbor.
292 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
295 * Head of DLL of nodes that this direct neighbor referred to us.
297 struct DistantNeighbor *referee_head;
300 * Tail of DLL of nodes that this direct neighbor referred to us.
302 struct DistantNeighbor *referee_tail;
305 * The sending context for gossiping peers to this neighbor.
307 struct NeighborSendContext *send_context;
310 * Is this one of the direct neighbors that we are "hiding"
316 * Save a single message from a direct neighbor from a peer
317 * we don't know on the chance that it will be gossiped about
318 * and we can deliver the message.
320 struct UnknownSenderMessage pending_message;
325 * Struct where actual neighbor information is stored,
326 * referenced by min_heap and max_heap. Freeing dealt
327 * with when items removed from hashmap.
329 struct DistantNeighbor
332 * We keep distant neighbor's of the same referrer in a DLL.
334 struct DistantNeighbor *next;
337 * We keep distant neighbor's of the same referrer in a DLL.
339 struct DistantNeighbor *prev;
344 struct GNUNET_CONTAINER_HeapNode *min_loc;
349 struct GNUNET_CONTAINER_HeapNode *max_loc;
352 * Identity of referrer (next hop towards 'neighbor').
354 struct DirectNeighbor *referrer;
357 * Identity of neighbor.
359 struct GNUNET_PeerIdentity identity;
362 * PublicKey of neighbor.
364 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
367 * Last time we received routing information from this peer
369 struct GNUNET_TIME_Absolute last_activity;
372 * Last time we sent routing information about this peer
374 struct GNUNET_TIME_Absolute last_gossip;
377 * Cost to neighbor, used for actual distance vector computations
382 * Random identifier *we* use for this peer, to be used as shortcut
383 * instead of sending full peer id for each message
388 * Random identifier the *referrer* uses for this peer.
390 unsigned int referrer_id;
393 * Is this one of the direct neighbors that we are "hiding"
400 struct PeerIteratorContext
403 * The actual context, to be freed later.
405 struct GNUNET_PEERINFO_IteratorContext *ic;
408 * The neighbor about which we are concerned.
410 struct DirectNeighbor *neighbor;
413 * The distant neighbor entry for this direct neighbor.
415 struct DistantNeighbor *distant;
420 * Context used for creating hello messages when
421 * gossips are received.
426 * Identity of distant neighbor.
428 struct GNUNET_PeerIdentity distant_peer;
431 * Identity of direct neighbor, via which we send this message.
433 const struct GNUNET_PeerIdentity *direct_peer;
436 * How many addresses do we need to add (always starts at 1, then set to 0)
438 int addresses_to_add;
442 struct DV_SendContext
445 * The distant peer (should always match)
447 struct GNUNET_PeerIdentity *distant_peer;
450 * The direct peer, we need to verify the referrer of.
452 struct GNUNET_PeerIdentity *direct_peer;
455 * The message to be sent
457 struct GNUNET_MessageHeader *message;
460 * The pre-built send result message. Simply needs to be queued
461 * and freed once send has been called!
463 struct GNUNET_DV_SendResultMessage *send_result;
466 * The size of the message being sent, may be larger
467 * than message->header.size because it's multiple
468 * messages packed into one!
473 * How important is this message?
475 unsigned int importance;
478 * Timeout for this message
480 struct GNUNET_TIME_Relative timeout;
483 * Unique ID for DV message
488 struct FindDestinationContext
491 struct DistantNeighbor *dest;
497 struct GNUNET_PeerIdentity *dest;
498 const struct GNUNET_PeerIdentity *via;
501 struct DisconnectContext
504 * Distant neighbor to get pid from.
506 struct DistantNeighbor *distant;
509 * Direct neighbor that disconnected.
511 struct DirectNeighbor *direct;
514 struct TokenizedMessageContext
517 * Immediate sender of this message
519 const struct GNUNET_PeerIdentity *peer;
522 * Distant sender of the message
524 struct DistantNeighbor *distant;
527 * Uid for this set of messages
533 * Context for finding the least cost peer to send to.
534 * Transport selection can only go so far.
536 struct FindLeastCostContext
538 struct DistantNeighbor *target;
539 unsigned int least_cost;
543 * Handle to the core service api.
545 static struct GNUNET_CORE_Handle *coreAPI;
548 * Stream tokenizer to handle messages coming in from core.
550 static struct GNUNET_SERVER_MessageStreamTokenizer *coreMST;
553 * The identity of our peer.
555 static struct GNUNET_PeerIdentity my_identity;
558 * The configuration for this service.
560 static const struct GNUNET_CONFIGURATION_Handle *cfg;
563 * The scheduler for this service.
565 static struct GNUNET_SCHEDULER_Handle *sched;
568 * The client, the DV plugin connected to us. Hopefully
569 * this client will never change, although if the plugin dies
570 * and returns for some reason it may happen.
572 static struct GNUNET_SERVER_Client * client_handle;
575 * Task to run when we shut down, cleaning up all our trash
577 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
579 static size_t default_dv_priority = 0;
581 static char *my_short_id;
584 * Transmit handle to the plugin.
586 static struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
589 * Head of DLL for client messages
591 static struct PendingMessage *plugin_pending_head;
594 * Tail of DLL for client messages
596 static struct PendingMessage *plugin_pending_tail;
599 * Handle to the peerinfo service
601 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
604 * Transmit handle to core service.
606 static struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
609 * Head of DLL for core messages
611 static struct PendingMessage *core_pending_head;
614 * Tail of DLL for core messages
616 static struct PendingMessage *core_pending_tail;
619 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
620 * directly connected peers.
622 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
625 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
626 * peers connected via DV (extended neighborhood). Does ALSO
627 * include any peers that are in 'direct_neighbors'; for those
628 * peers, the cost will be zero and the referrer all zeros.
630 static struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
633 * We use the min heap (min refers to cost) to prefer
634 * gossipping about peers with small costs.
636 static struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
639 * We use the max heap (max refers to cost) for general
640 * iterations over all peers and to remove the most costly
641 * connection if we have too many.
643 static struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
646 * How far out to keep peers we learn about.
648 static unsigned long long fisheye_depth;
651 * How many peers to store at most.
653 static unsigned long long max_table_size;
656 * We've been given a target ID based on the random numbers that
657 * we assigned to our DV-neighborhood. Find the entry for the
658 * respective neighbor.
661 find_destination (void *cls,
662 struct GNUNET_CONTAINER_HeapNode *node,
663 void *element, GNUNET_CONTAINER_HeapCostType cost)
665 struct FindDestinationContext *fdc = cls;
666 struct DistantNeighbor *dn = element;
668 if (fdc->tid != dn->our_id)
676 * We've been given a target ID based on the random numbers that
677 * we assigned to our DV-neighborhood. Find the entry for the
678 * respective neighbor.
681 find_specific_id (void *cls,
682 const GNUNET_HashCode *key,
685 struct FindIDContext *fdc = cls;
686 struct DistantNeighbor *dn = value;
688 if (memcmp(&dn->referrer->identity, fdc->via, sizeof(struct GNUNET_PeerIdentity)) == 0)
690 fdc->tid = dn->referrer_id;
697 * Find a distant peer whose referrer_id matches what we're
698 * looking for. For looking up a peer we've gossipped about
699 * but is now disconnected. Need to do this because we don't
700 * want to remove those that may be accessible via a different
703 static int find_distant_peer (void *cls,
704 const GNUNET_HashCode * key,
707 struct FindDestinationContext *fdc = cls;
708 struct DistantNeighbor *distant = value;
710 if (fdc->tid == distant->referrer_id)
719 * Function called to notify a client about the socket
720 * begin ready to queue more data. "buf" will be
721 * NULL and "size" zero if the socket was closed for
722 * writing in the meantime.
725 * @param size number of bytes available in buf
726 * @param buf where the callee should write the message
727 * @return number of bytes written to buf
729 size_t transmit_to_plugin (void *cls,
730 size_t size, void *buf)
733 struct PendingMessage *reply;
739 /* client disconnected */
740 #if DEBUG_DV_MESSAGES
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: %s buffer was NULL (client disconnect?)\n", my_short_id, "transmit_to_plugin");
745 plugin_transmit_handle = NULL;
747 while ( (NULL != (reply = plugin_pending_head)) &&
748 (size >= off + (msize = ntohs (reply->msg->size))))
750 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
753 memcpy (&cbuf[off], reply->msg, msize);
758 if (plugin_pending_head != NULL)
759 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
760 ntohs(plugin_pending_head->msg->size),
761 GNUNET_TIME_UNIT_FOREVER_REL,
762 &transmit_to_plugin, NULL);
768 * Send a message to the dv plugin.
770 * @param sender the direct sender of the message
771 * @param message the message to send to the plugin
772 * (may be an encapsulated type)
773 * @param message_size the size of the message to be sent
774 * @param distant_neighbor the original sender of the message
775 * @param cost the cost to the original sender of the message
777 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
778 const struct GNUNET_MessageHeader *message,
780 struct GNUNET_PeerIdentity *distant_neighbor,
783 struct GNUNET_DV_MessageReceived *received_msg;
784 struct PendingMessage *pending_message;
785 char *sender_address;
786 size_t sender_address_len;
787 char *packed_msg_start;
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
794 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
796 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
797 sender_address = GNUNET_malloc(sender_address_len);
798 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
799 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
803 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
804 sender_address = GNUNET_malloc(sender_address_len);
805 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
808 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
809 received_msg = GNUNET_malloc(size);
810 received_msg->header.size = htons(size);
811 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
812 received_msg->sender_address_len = htonl(sender_address_len);
813 received_msg->distance = htonl(cost);
814 received_msg->msg_len = htonl(message_size);
815 /* Set the sender in this message to be the original sender! */
816 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
817 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
818 memcpy(&received_msg[1], sender_address, sender_address_len);
819 GNUNET_free(sender_address);
820 /* Copy the actual message after the sender */
821 packed_msg_start = (char *)&received_msg[1];
822 packed_msg_start = &packed_msg_start[sender_address_len];
823 memcpy(packed_msg_start, message, message_size);
824 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
825 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
826 memcpy(&pending_message[1], received_msg, size);
827 GNUNET_free(received_msg);
829 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
831 if (client_handle != NULL)
833 if (plugin_transmit_handle == NULL)
835 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
836 size, GNUNET_TIME_UNIT_FOREVER_REL,
837 &transmit_to_plugin, NULL);
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
846 /* Declare here so retry_core_send is aware of it */
847 size_t core_transmit_notify (void *cls,
848 size_t size, void *buf);
851 * Try to send another message from our core sending list
854 try_core_send (void *cls,
855 const struct GNUNET_SCHEDULER_TaskContext *tc)
857 struct PendingMessage *pending;
858 pending = core_pending_head;
860 if (core_transmit_handle != NULL)
861 return; /* Message send already in progress */
864 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, pending->importance, pending->timeout, &pending->recipient, pending->msg_size, &core_transmit_notify, NULL);
868 * Function called to notify a client about the socket
869 * being ready to queue more data. "buf" will be
870 * NULL and "size" zero if the socket was closed for
871 * writing in the meantime.
873 * @param cls closure (NULL)
874 * @param size number of bytes available in buf
875 * @param buf where the callee should write the message
876 * @return number of bytes written to buf
878 size_t core_transmit_notify (void *cls,
879 size_t size, void *buf)
882 struct PendingMessage *pending;
883 struct PendingMessage *client_reply;
889 /* client disconnected */
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
896 core_transmit_handle = NULL;
898 pending = core_pending_head;
899 if ( (pending != NULL) &&
900 (size >= (msize = ntohs (pending->msg->size))))
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
905 GNUNET_CONTAINER_DLL_remove (core_pending_head,
908 if (pending->send_result != NULL) /* Will only be non-null if a real client asked for this send */
910 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
911 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
912 memcpy(&client_reply[1], pending->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
913 GNUNET_free(pending->send_result);
915 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
916 if (client_handle != NULL)
918 if (plugin_transmit_handle == NULL)
920 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
921 sizeof(struct GNUNET_DV_SendResultMessage),
922 GNUNET_TIME_UNIT_FOREVER_REL,
923 &transmit_to_plugin, NULL);
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
931 memcpy (&cbuf[off], pending->msg, msize);
932 GNUNET_free (pending);
935 /*reply = core_pending_head;*/
937 GNUNET_SCHEDULER_add_now(sched, &try_core_send, NULL);
939 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);*/
946 * Send a DV data message via DV.
948 * @param sender the original sender of the message
949 * @param recipient the next hop recipient, may be our direct peer, maybe not
950 * @param send_context the send context
953 send_message_via (const struct GNUNET_PeerIdentity *sender,
954 const struct GNUNET_PeerIdentity *recipient,
955 struct DV_SendContext *send_context)
957 p2p_dv_MESSAGE_Data *toSend;
958 unsigned int msg_size;
959 unsigned int recipient_id;
960 unsigned int sender_id;
961 struct DistantNeighbor *source;
962 struct PendingMessage *pending_message;
963 struct FindIDContext find_context;
968 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
970 find_context.dest = send_context->distant_peer;
971 find_context.via = recipient;
972 find_context.tid = 0;
973 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
974 &find_specific_id, &find_context);
976 if (find_context.tid == 0)
978 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
979 /* target unknown to us, drop! */
980 return GNUNET_SYSERR;
982 recipient_id = find_context.tid;
984 if (0 == (memcmp (&my_identity,
985 sender, sizeof (struct GNUNET_PeerIdentity))))
988 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
989 &sender->hashPubKey);
991 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: send_message_via found %s, myself in extended peer list???\n", my_short_id, GNUNET_i2s(&source->identity));
995 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
996 &sender->hashPubKey);
999 /* sender unknown to us, drop! */
1000 return GNUNET_SYSERR;
1002 sender_id = source->our_id;
1005 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1006 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1007 pending_message->send_result = send_context->send_result;
1008 memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
1009 pending_message->msg_size = msg_size;
1010 pending_message->importance = send_context->importance;
1011 pending_message->timeout = send_context->timeout;
1012 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1013 toSend->header.size = htons (msg_size);
1014 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1015 toSend->sender = htonl (sender_id);
1016 toSend->recipient = htonl (recipient_id);
1017 #if DEBUG_DV_MESSAGES
1018 toSend->uid = send_context->uid; /* Still sent around in network byte order */
1020 toSend->uid = htonl(0);
1023 memcpy (&toSend[1], send_context->message, send_context->message_size);
1026 memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
1027 shortname[4] = '\0';
1028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(recipient), msg_size);
1031 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1036 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1042 * Given a FindLeastCostContext, and a set
1043 * of peers that match the target, return the cheapest.
1045 * @param cls closure, a struct FindLeastCostContext
1046 * @param key the key identifying the target peer
1047 * @param value the target peer
1049 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1052 find_least_cost_peer (void *cls,
1053 const GNUNET_HashCode *key,
1056 struct FindLeastCostContext *find_context = cls;
1057 struct DistantNeighbor *dn = value;
1059 if (dn->cost < find_context->least_cost)
1061 find_context->target = dn;
1063 if (dn->cost == DIRECT_NEIGHBOR_COST)
1069 * Send a DV data message via DV.
1071 * @param recipient the ultimate recipient of this message
1072 * @param sender the original sender of the message
1073 * @param specific_neighbor the specific neighbor to send this message via
1074 * @param message the packed message
1075 * @param message_size size of the message
1076 * @param importance what priority to send this message with
1077 * @param timeout how long to possibly delay sending this message
1080 send_message (const struct GNUNET_PeerIdentity * recipient,
1081 const struct GNUNET_PeerIdentity * sender,
1082 const struct DistantNeighbor * specific_neighbor,
1083 const struct GNUNET_MessageHeader * message,
1084 size_t message_size,
1085 unsigned int importance,
1087 struct GNUNET_TIME_Relative timeout)
1089 p2p_dv_MESSAGE_Data *toSend;
1090 unsigned int msg_size;
1092 unsigned int recipient_id;
1093 unsigned int sender_id;
1094 struct DistantNeighbor *target;
1095 struct DistantNeighbor *source;
1096 struct PendingMessage *pending_message;
1097 struct FindLeastCostContext find_least_ctx;
1098 #if DEBUG_DV_PEER_NUMBERS
1099 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1100 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1101 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1103 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1105 find_least_ctx.least_cost = -1;
1106 find_least_ctx.target = NULL;
1108 * Need to find the least cost peer, lest the transport selection keep
1109 * picking the same DV route for the same destination which results
1110 * in messages looping forever. Relatively cheap, we don't iterate
1111 * over all known peers, just those that apply.
1113 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1114 &recipient->hashPubKey, &find_least_cost_peer, &find_least_ctx);
1115 target = find_least_ctx.target;
1119 /* target unknown to us, drop! */
1120 return GNUNET_SYSERR;
1122 recipient_id = target->referrer_id;
1124 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1125 &sender->hashPubKey);
1128 if (0 != (memcmp (&my_identity,
1129 sender, sizeof (struct GNUNET_PeerIdentity))))
1131 /* sender unknown to us, drop! */
1132 return GNUNET_SYSERR;
1134 sender_id = 0; /* 0 == us */
1138 /* find out the number that we use when we gossip about
1140 sender_id = source->our_id;
1143 #if DEBUG_DV_PEER_NUMBERS
1144 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1145 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1146 encPeerFrom.encoding[4] = '\0';
1147 encPeerVia.encoding[4] = '\0';
1149 if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1154 cost = target->cost;
1155 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1156 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1157 pending_message->send_result = NULL;
1158 pending_message->importance = importance;
1159 pending_message->timeout = timeout;
1160 memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1161 pending_message->msg_size = msg_size;
1162 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1163 toSend->header.size = htons (msg_size);
1164 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1165 toSend->sender = htonl (sender_id);
1166 toSend->recipient = htonl (recipient_id);
1167 #if DEBUG_DV_MESSAGES
1168 toSend->uid = htonl(uid);
1170 toSend->uid = htonl(0);
1173 #if DEBUG_DV_PEER_NUMBERS
1174 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1175 encPeerTo.encoding[4] = '\0';
1176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Sending DATA message. Sender id %u, source %s, destination %s, via %s\n", GNUNET_i2s(&my_identity), sender_id, &encPeerFrom, &encPeerTo, &encPeerVia);
1178 memcpy (&toSend[1], message, message_size);
1179 if (source->pkey == NULL) /* Test our hypothesis about message failures! */
1181 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: Sending message, but anticipate recipient will not know sender!!!\n\n\n");
1183 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1188 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));
1191 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1192 /*if (core_transmit_handle == NULL)
1193 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
1195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: CORE ALREADY SENDING\n", "DV SEND MESSAGE", msg_size);*/
1200 struct CheckPeerContext
1205 struct DistantNeighbor *peer;
1208 * Sender id to search for
1210 unsigned int sender_id;
1214 * Iterator over hash map entries.
1216 * @param cls closure
1217 * @param key current key code
1218 * @param value value in the hash map
1219 * @return GNUNET_YES if we should continue to
1223 int checkPeerID (void *cls,
1224 const GNUNET_HashCode * key,
1227 struct CheckPeerContext *ctx = cls;
1228 struct DistantNeighbor *distant = value;
1230 if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1232 ctx->peer = distant;
1242 * Handler for messages parsed out by the tokenizer from
1243 * DV DATA received for this peer.
1246 * @param client the TokenizedMessageContext which contains message information
1247 * @param message the actual message
1249 void tokenized_message_handler (void *cls,
1251 const struct GNUNET_MessageHeader *message)
1253 struct TokenizedMessageContext *ctx = client;
1254 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1255 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1256 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1257 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1259 #if DEBUG_DV_MESSAGES
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "%s: Receives %s message for me, uid %u, size %d, type %d cost %u from %s!\n", my_short_id, "DV DATA", ctx->uid, ntohs(message->size), ntohs(message->type), ctx->distant->cost, GNUNET_i2s(&ctx->distant->identity));
1263 GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1264 send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1269 struct DelayedMessageContext
1271 struct GNUNET_PeerIdentity dest;
1272 struct GNUNET_PeerIdentity sender;
1273 struct GNUNET_MessageHeader *message;
1274 size_t message_size;
1278 void send_message_delayed (void *cls,
1279 const struct GNUNET_SCHEDULER_TaskContext *tc)
1281 struct DelayedMessageContext *msg_ctx = cls;
1282 if (msg_ctx != NULL)
1284 send_message(&msg_ctx->dest,
1288 msg_ctx->message_size,
1289 default_dv_priority,
1291 GNUNET_TIME_relative_get_forever());
1292 GNUNET_free(msg_ctx->message);
1293 GNUNET_free(msg_ctx);
1300 * Core handler for dv data messages. Whatever this message
1301 * contains all we really have to do is rip it out of its
1302 * DV layering and give it to our pal the DV plugin to report
1305 * @param cls closure
1306 * @param peer peer which sent the message (immediate sender)
1307 * @param message the message
1308 * @param latency the latency of the connection we received the message from
1309 * @param distance the distance to the immediate peer
1311 static int handle_dv_data_message (void *cls,
1312 const struct GNUNET_PeerIdentity * peer,
1313 const struct GNUNET_MessageHeader * message,
1314 struct GNUNET_TIME_Relative latency,
1317 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1318 const struct GNUNET_MessageHeader *packed_message;
1319 struct DirectNeighbor *dn;
1320 struct DistantNeighbor *pos;
1321 unsigned int sid; /* Sender id */
1322 unsigned int tid; /* Target id */
1323 struct GNUNET_PeerIdentity *original_sender;
1324 struct GNUNET_PeerIdentity *destination;
1325 struct FindDestinationContext fdc;
1326 struct TokenizedMessageContext tkm_ctx;
1328 struct DelayedMessageContext *delayed_context;
1331 struct CheckPeerContext checkPeerCtx;
1336 size_t packed_message_size;
1341 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "%s: Receives DATA message from %s size %d, packed size %d!\n", my_short_id, GNUNET_i2s(peer) , ntohs(incoming->header.size), packed_message_size);
1349 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "`%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));
1356 return GNUNET_SYSERR;
1359 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1364 sid = ntohl (incoming->sender);
1368 checkPeerCtx.sender_id = sid;
1369 checkPeerCtx.peer = NULL;
1370 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1371 pos = checkPeerCtx.peer;
1375 pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1379 pos = dn->referee_head;
1380 while ((NULL != pos) && (pos->referrer_id != sid))
1386 #if DEBUG_DV_MESSAGES
1387 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "%s: unknown sender (%u), Message uid %llu from %s!\n", GNUNET_i2s(&my_identity), ntohl(incoming->sender), ntohl(incoming->uid), direct_id);
1390 GNUNET_free(direct_id);
1391 pos = dn->referee_head;
1392 while ((NULL != pos) && (pos->referrer_id != sid))
1394 sender_id = strdup(GNUNET_i2s(&pos->identity));
1395 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1396 GNUNET_free(sender_id);
1400 if (dn->pending_message.sender_id != 0)
1402 GNUNET_free(dn->pending_message.message);
1405 dn->pending_message.message = GNUNET_malloc(ntohs (message->size));
1406 memcpy(dn->pending_message.message, message, ntohs(message->size));
1407 dn->pending_message.distance = distance;
1408 dn->pending_message.latency = latency;
1409 memcpy(&dn->pending_message.sender, peer, sizeof(struct GNUNET_PeerIdentity));
1410 dn->pending_message.sender_id = sid;
1412 #if DEBUG_MESSAGE_DROP
1413 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "%s: DROPPING MESSAGE type %d, unknown sender! Message immediately from %s!\n", GNUNET_i2s(&my_identity), ntohs(((struct GNUNET_MessageHeader *)&incoming[1])->type), direct_id);
1416 GNUNET_free(direct_id);
1418 /* unknown sender */
1421 original_sender = &pos->identity;
1422 tid = ntohl (incoming->recipient);
1426 cbuf = (char *)&incoming[1];
1428 tkm_ctx.peer = peer;
1429 tkm_ctx.distant = pos;
1430 tkm_ctx.uid = ntohl(incoming->uid);
1431 if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1434 packed_message_size,
1439 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1445 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1448 /* FIXME: this is the *only* per-request operation we have in DV
1449 that is O(n) in relation to the number of connected peers; a
1450 hash-table lookup could easily solve this (minor performance
1454 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1455 &find_destination, &fdc);
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1462 if (fdc.dest == NULL)
1464 #if DEBUG_DV_MESSAGES
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1470 destination = &fdc.dest->identity;
1472 if (0 == memcmp (destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1474 /* FIXME: create stat: routing loop-discard! */
1475 #if DEBUG_DV_PEER_NUMBERS
1476 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1479 #if DEBUG_DV_MESSAGES
1480 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 "%s: DROPPING MESSAGE uid %u type %d, routing loop! Message immediately from %s!\n", my_short_id, ntohl(incoming->uid), ntohs(packed_message->type), direct_id);
1487 /* At this point we have a message, and we need to forward it on to the
1490 #if DEBUG_DV_MESSAGES
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "%s: FORWARD %s message for %s, uid %u, size %d type %d, cost %u!\n", my_short_id, "DV DATA", GNUNET_i2s(destination), ntohl(incoming->uid), ntohs(packed_message->size), ntohs(packed_message->type), pos->cost);
1496 if (GNUNET_TIME_absolute_get_duration(pos->last_gossip).value < GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2).value)
1498 delayed_context = GNUNET_malloc(sizeof(struct DelayedMessageContext));
1499 memcpy(&delayed_context->dest, destination, sizeof(struct GNUNET_PeerIdentity));
1500 memcpy(&delayed_context->sender, original_sender, sizeof(struct GNUNET_PeerIdentity));
1501 delayed_context->message = GNUNET_malloc(packed_message_size);
1502 memcpy(delayed_context->message, packed_message, packed_message_size);
1503 delayed_context->message_size = packed_message_size;
1504 delayed_context->uid = ntohl(incoming->uid);
1505 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 2500), &send_message_delayed, delayed_context);
1511 ret = send_message(destination,
1515 packed_message_size,
1516 default_dv_priority,
1517 ntohl(incoming->uid),
1518 GNUNET_TIME_relative_get_forever());
1520 if (ret != GNUNET_SYSERR)
1524 #if DEBUG_MESSAGE_DROP
1525 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "%s: DROPPING MESSAGE type %d, forwarding failed! Message immediately from %s!\n", GNUNET_i2s(&my_identity), ntohs(((struct GNUNET_MessageHeader *)&incoming[1])->type), direct_id);
1529 return GNUNET_SYSERR;
1535 * Iterator over hash map entries.
1537 * @param cls closure (NULL)
1538 * @param key current key code
1539 * @param value value in the hash map (DistantNeighbor)
1540 * @return GNUNET_YES if we should continue to
1544 int print_neighbors (void *cls,
1545 const GNUNET_HashCode * key,
1548 struct DistantNeighbor *distant_neighbor = value;
1549 char my_shortname[5];
1550 char referrer_shortname[5];
1551 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1552 my_shortname[4] = '\0';
1553 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1554 referrer_shortname[4] = '\0';
1556 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`%s' %s: Peer `%s', distance %d, referrer `%s' pkey: %s\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname, distant_neighbor->pkey == NULL ? "no" : "yes");
1562 * Scheduled task which gossips about known direct peers to other connected
1563 * peers. Will run until called with reason shutdown.
1566 neighbor_send_task (void *cls,
1567 const struct GNUNET_SCHEDULER_TaskContext *tc)
1569 struct NeighborSendContext *send_context = cls;
1570 #if DEBUG_DV_GOSSIP_SEND
1571 char * encPeerAbout;
1574 struct DistantNeighbor *about;
1575 struct DirectNeighbor *to;
1576 struct FastGossipNeighborList *about_list;
1578 p2p_dv_MESSAGE_NeighborInfo *message;
1579 struct PendingMessage *pending_message;
1581 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "%s: Called with reason shutdown, shutting down!\n",
1586 GNUNET_i2s(&my_identity));
1591 if (send_context->fast_gossip_list_head != NULL)
1593 about_list = send_context->fast_gossip_list_head;
1594 about = about_list->about;
1595 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1596 send_context->fast_gossip_list_tail,
1598 GNUNET_free(about_list);
1602 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1603 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1604 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1605 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1606 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1607 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1608 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1609 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1610 * asynchronously enough to avoid major problems (-;
1612 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1614 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1616 to = send_context->toNeighbor;
1618 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1620 (about->hidden == GNUNET_NO) &&
1623 (0 != memcmp (&about->identity,
1624 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1625 (about->pkey != NULL))
1627 #if DEBUG_DV_GOSSIP_SEND
1628 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1629 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1632 GNUNET_i2s(&my_identity),
1633 encPeerAbout, about->our_id, encPeerTo);
1634 GNUNET_free(encPeerAbout);
1635 GNUNET_free(encPeerTo);
1637 about->last_gossip = GNUNET_TIME_absolute_get();
1638 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1639 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1640 pending_message->importance = default_dv_priority;
1641 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1642 memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1643 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1644 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1645 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1646 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1647 message->cost = htonl (about->cost);
1648 message->neighbor_id = htonl (about->our_id);
1650 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1651 memcpy (&message->neighbor,
1652 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1654 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1659 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1660 /*if (core_transmit_handle == NULL)
1661 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);*/
1665 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1667 #if DEBUG_DV_PEER_NUMBERS
1668 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1670 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1674 #if DEBUG_DV_PEER_NUMBERS
1675 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1677 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1685 * Handle START-message. This is the first message sent to us
1686 * by the client (can only be one!).
1688 * @param cls closure (always NULL)
1689 * @param client identification of the client
1690 * @param message the actual message
1693 handle_start (void *cls,
1694 struct GNUNET_SERVER_Client *client,
1695 const struct GNUNET_MessageHeader *message)
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 "Received `%s' request from client\n", "START");
1703 client_handle = client;
1705 GNUNET_SERVER_client_keep(client_handle);
1706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1711 * Iterate over hash map entries for a distant neighbor,
1712 * if direct neighbor matches context call send message
1714 * @param cls closure, a DV_SendContext
1715 * @param key current key code
1716 * @param value value in the hash map
1717 * @return GNUNET_YES if we should continue to
1721 int send_iterator (void *cls,
1722 const GNUNET_HashCode * key,
1725 struct DV_SendContext *send_context = cls;
1726 struct DistantNeighbor *distant_neighbor = value;
1728 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1730 send_message_via(&my_identity, distant_neighbor, send_context);
1738 * Service server's handler for message send requests (which come
1739 * bubbling up to us through the DV plugin).
1741 * @param cls closure
1742 * @param client identification of the client
1743 * @param message the actual message
1745 void handle_dv_send_message (void *cls,
1746 struct GNUNET_SERVER_Client * client,
1747 const struct GNUNET_MessageHeader * message)
1749 struct GNUNET_DV_SendMessage *send_msg;
1750 struct GNUNET_DV_SendResultMessage *send_result_msg;
1751 struct PendingMessage *pending_message;
1753 size_t message_size;
1754 struct GNUNET_PeerIdentity *destination;
1755 struct GNUNET_PeerIdentity *direct;
1756 struct GNUNET_MessageHeader *message_buf;
1759 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1760 struct DV_SendContext *send_context;
1761 #if DEBUG_DV_MESSAGES
1763 struct GNUNET_MessageHeader *packed_message;
1766 if (client_handle == NULL)
1768 client_handle = client;
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1772 else if (client_handle != client)
1774 client_handle = client;
1775 /* What should we do in this case, assert fail or just log the warning? */
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "%s: Setting client handle (was a different client!)!\n", "dv");
1782 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1783 send_msg = (struct GNUNET_DV_SendMessage *)message;
1785 address_len = ntohl(send_msg->addrlen);
1786 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1787 message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1788 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1789 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1790 message_buf = GNUNET_malloc(message_size);
1792 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1793 offset = 0; /* Offset starts at zero */
1795 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1796 offset += sizeof(struct GNUNET_PeerIdentity);
1798 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1799 offset += sizeof(struct GNUNET_PeerIdentity);
1802 memcpy(message_buf, &temp_pos[offset], message_size);
1803 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1805 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1806 dest_hash.encoding[4] = '\0';
1807 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);
1810 #if DEBUG_DV_MESSAGES
1811 cbuf = (char *)message_buf;
1813 while(offset < message_size)
1815 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1816 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: DV PLUGIN SEND uid %u type %d to %s\n", my_short_id, ntohl(send_msg->uid), ntohs(packed_message->type), GNUNET_i2s(destination));
1817 offset += ntohs(packed_message->size);
1819 /*GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: DV PLUGIN SEND uid %u type %d to %s\n", my_short_id, ntohl(send_msg->uid), ntohs(message_buf->type), GNUNET_i2s(destination));*/
1821 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1822 dest_hash.encoding[4] = '\0';
1823 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1825 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1826 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1827 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1828 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1830 send_context->importance = ntohl(send_msg->priority);
1831 send_context->timeout = send_msg->timeout;
1832 send_context->direct_peer = direct;
1833 send_context->distant_peer = destination;
1834 send_context->message = message_buf;
1835 send_context->message_size = message_size;
1836 send_context->send_result = send_result_msg;
1837 #if DEBUG_DV_MESSAGES
1838 send_context->uid = send_msg->uid;
1841 if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1843 send_result_msg->result = htons(1);
1844 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1845 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1846 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1847 GNUNET_free(send_result_msg);
1849 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1851 if (client_handle != NULL)
1853 if (plugin_transmit_handle == NULL)
1855 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1856 sizeof(struct GNUNET_DV_SendResultMessage),
1857 GNUNET_TIME_UNIT_FOREVER_REL,
1858 &transmit_to_plugin, NULL);
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1865 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1866 dest_hash.encoding[4] = '\0';
1867 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s DV SEND failed to send message to destination `%s' via `%s'\n", my_short_id, (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1870 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1872 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1874 send_result_msg->result = htons(1);
1875 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1876 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1877 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1878 GNUNET_free(send_result_msg);
1880 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1882 if (client_handle != NULL)
1884 if (plugin_transmit_handle == NULL)
1886 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1887 sizeof(struct GNUNET_DV_SendResultMessage),
1888 GNUNET_TIME_UNIT_FOREVER_REL,
1889 &transmit_to_plugin, NULL);
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1896 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1897 dest_hash.encoding[4] = '\0';
1898 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s DV SEND failed to send message to destination `%s' via `%s'\n", my_short_id, (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1901 GNUNET_free(message_buf);
1902 GNUNET_free(send_context);
1903 GNUNET_free(direct);
1904 GNUNET_free(destination);
1906 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1909 /** Forward declarations **/
1910 static int handle_dv_gossip_message (void *cls,
1911 const struct GNUNET_PeerIdentity *peer,
1912 const struct GNUNET_MessageHeader *message,
1913 struct GNUNET_TIME_Relative latency,
1916 static int handle_dv_disconnect_message (void *cls,
1917 const struct GNUNET_PeerIdentity *peer,
1918 const struct GNUNET_MessageHeader *message,
1919 struct GNUNET_TIME_Relative latency,
1921 /** End forward declarations **/
1925 * List of handlers for the messages understood by this
1928 * Hmm... will we need to register some handlers with core and
1929 * some handlers with our server here? Because core should be
1930 * getting the incoming DV messages (from whichever lower level
1931 * transport) and then our server should be getting messages
1932 * from the dv_plugin, right?
1934 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1935 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1936 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1937 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1941 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1942 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1943 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1948 * Free a DistantNeighbor node, including removing it
1949 * from the referer's list.
1952 distant_neighbor_free (struct DistantNeighbor *referee)
1954 struct DirectNeighbor *referrer;
1956 referrer = referee->referrer;
1957 if (referrer != NULL)
1959 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1960 referrer->referee_tail, referee);
1962 GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
1963 GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
1964 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
1965 &referee->identity.hashPubKey);
1966 GNUNET_free_non_null (referee->pkey);
1967 GNUNET_free (referee);
1971 * Free a DirectNeighbor node, including removing it
1972 * from the referer's list.
1975 direct_neighbor_free (struct DirectNeighbor *direct)
1977 struct NeighborSendContext *send_context;
1978 struct FastGossipNeighborList *about_list;
1979 struct FastGossipNeighborList *prev_about;
1981 send_context = direct->send_context;
1983 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1984 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1986 about_list = send_context->fast_gossip_list_head;
1987 while (about_list != NULL)
1989 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1990 prev_about = about_list;
1991 about_list = about_list->next;
1992 GNUNET_free(prev_about);
1994 GNUNET_free(send_context);
1995 GNUNET_free(direct);
1999 * Multihashmap iterator for sending out disconnect messages
2002 * @param cls the peer that was disconnected
2003 * @param key key value stored under
2004 * @param value the direct neighbor to send disconnect to
2006 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2008 static int schedule_disconnect_messages (void *cls,
2009 const GNUNET_HashCode * key,
2012 struct DisconnectContext *disconnect_context = cls;
2013 struct DirectNeighbor *disconnected = disconnect_context->direct;
2014 struct DirectNeighbor *notify = value;
2015 struct PendingMessage *pending_message;
2016 p2p_dv_MESSAGE_Disconnect *disconnect_message;
2018 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2019 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
2021 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
2022 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
2023 pending_message->importance = default_dv_priority;
2024 pending_message->timeout = GNUNET_TIME_relative_get_forever();
2025 memcpy(&pending_message->recipient, ¬ify->identity, sizeof(struct GNUNET_PeerIdentity));
2026 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
2027 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
2028 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
2029 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
2030 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
2032 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
2037 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
2038 /*if (core_transmit_handle == NULL)
2039 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);*/
2045 * Multihashmap iterator for freeing extended neighbors.
2048 * @param key key value stored under
2049 * @param value the distant neighbor to be freed
2051 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2053 static int free_extended_neighbors (void *cls,
2054 const GNUNET_HashCode * key,
2057 struct DistantNeighbor *distant = value;
2058 distant_neighbor_free(distant);
2063 * Multihashmap iterator for freeing direct neighbors.
2066 * @param key key value stored under
2067 * @param value the direct neighbor to be freed
2069 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2071 static int free_direct_neighbors (void *cls,
2072 const GNUNET_HashCode * key,
2075 struct DirectNeighbor *direct = value;
2076 direct_neighbor_free(direct);
2082 * Task run during shutdown.
2088 shutdown_task (void *cls,
2089 const struct GNUNET_SCHEDULER_TaskContext *tc)
2092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2093 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2095 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2096 GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2097 GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2098 GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2100 GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2101 GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2103 GNUNET_CORE_disconnect (coreAPI);
2104 GNUNET_PEERINFO_disconnect(peerinfo_handle);
2105 GNUNET_SERVER_mst_destroy(coreMST);
2106 GNUNET_free_non_null(my_short_id);
2108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2113 * To be called on core init/fail.
2115 void core_init (void *cls,
2116 struct GNUNET_CORE_Handle * server,
2117 const struct GNUNET_PeerIdentity *identity,
2118 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2123 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
2124 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2131 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2132 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2137 #if PKEY_NO_NEIGHBOR_ON_ADD
2139 * Iterator over hash map entries.
2141 * @param cls closure
2142 * @param key current key code
2143 * @param value value in the hash map
2144 * @return GNUNET_YES if we should continue to
2148 static int add_pkey_to_extended (void *cls,
2149 const GNUNET_HashCode * key,
2152 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2153 struct DistantNeighbor *distant_neighbor = value;
2155 if (distant_neighbor->pkey == NULL)
2157 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2158 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2166 * Iterator over hash map entries.
2168 * @param cls closure
2169 * @param key current key code
2170 * @param value value in the hash map
2171 * @return GNUNET_YES if we should continue to
2175 static int update_matching_neighbors (void *cls,
2176 const GNUNET_HashCode * key,
2179 struct NeighborUpdateInfo * update_info = cls;
2180 struct DistantNeighbor *distant_neighbor = value;
2182 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2184 /* same referrer, cost change! */
2185 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2186 update_info->neighbor->max_loc, update_info->cost);
2187 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2188 update_info->neighbor->min_loc, update_info->cost);
2189 update_info->neighbor->last_activity = update_info->now;
2190 update_info->neighbor->cost = update_info->cost;
2191 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2200 * Iterate over all current direct peers, add DISTANT newly connected
2201 * peer to the fast gossip list for that peer so we get DV routing
2202 * information out as fast as possible!
2204 * @param cls the newly connected neighbor we will gossip about
2205 * @param key the hashcode of the peer
2206 * @param value the direct neighbor we should gossip to
2208 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2210 static int add_distant_all_direct_neighbors (void *cls,
2211 const GNUNET_HashCode * key,
2214 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2215 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2216 struct NeighborSendContext *send_context = direct->send_context;
2217 struct FastGossipNeighborList *gossip_entry;
2223 if (distant == NULL)
2228 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2230 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2234 if (distant->hidden == GNUNET_YES)
2235 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2237 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2238 gossip_entry->about = distant;
2240 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2241 send_context->fast_gossip_list_tail,
2242 send_context->fast_gossip_list_tail,
2245 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2246 encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2249 GNUNET_i2s(&my_identity),
2250 encPeerAbout, distant->our_id, encPeerTo);
2251 GNUNET_free(encPeerAbout);
2252 GNUNET_free(encPeerTo);
2254 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2255 GNUNET_SCHEDULER_cancel(sched, send_context->task);*/
2257 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2262 * Callback for hello address creation.
2264 * @param cls closure, a struct HelloContext
2265 * @param max maximum number of bytes that can be written to buf
2266 * @param buf where to write the address information
2268 * @return number of bytes written, 0 to signal the
2269 * end of the iteration.
2272 generate_hello_address (void *cls, size_t max, void *buf)
2274 struct HelloContext *hello_context = cls;
2280 if (hello_context->addresses_to_add == 0)
2283 /* Hello "address" will be concatenation of distant peer and direct peer identities */
2284 size = 2 * sizeof(struct GNUNET_PeerIdentity);
2285 GNUNET_assert(max >= size);
2287 addr_buffer = GNUNET_malloc(size);
2289 /* Copy the distant peer identity to buffer */
2290 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
2291 offset += sizeof(struct GNUNET_PeerIdentity);
2292 /* Copy the direct peer identity to buffer */
2293 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
2294 ret = GNUNET_HELLO_add_address ("dv",
2295 GNUNET_TIME_relative_to_absolute
2296 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2299 hello_context->addresses_to_add--;
2301 GNUNET_free(addr_buffer);
2307 * Handles when a peer is either added due to being newly connected
2308 * or having been gossiped about, also called when the cost for a neighbor
2309 * needs to be updated.
2311 * @param peer identity of the peer whose info is being added/updated
2312 * @param pkey public key of the peer whose info is being added/updated
2313 * @param referrer_peer_id id to use when sending to 'peer'
2314 * @param referrer if this is a gossiped peer, who did we hear it from?
2315 * @param cost the cost of communicating with this peer via 'referrer'
2317 * @return the added neighbor, the updated neighbor or NULL (neighbor
2320 static struct DistantNeighbor *
2321 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2322 unsigned int referrer_peer_id,
2323 struct DirectNeighbor *referrer, unsigned int cost)
2325 struct DistantNeighbor *neighbor;
2326 struct DistantNeighbor *max;
2327 struct GNUNET_TIME_Absolute now;
2328 struct NeighborUpdateInfo *neighbor_update;
2329 struct HelloContext *hello_context;
2330 struct GNUNET_HELLO_Message *hello_msg;
2331 unsigned int our_id;
2335 #if DEBUG_DV_PEER_NUMBERS
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2338 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2341 now = GNUNET_TIME_absolute_get ();
2342 neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2344 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2345 neighbor_update->neighbor = neighbor;
2346 neighbor_update->cost = cost;
2347 neighbor_update->now = now;
2348 neighbor_update->referrer = referrer;
2349 neighbor_update->referrer_peer_id = referrer_peer_id;
2351 if (neighbor != NULL)
2354 memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2356 our_id = neighbor->our_id;
2362 memcpy(&our_id, peer, sizeof(unsigned int));
2364 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2368 /* Either we do not know this peer, or we already do but via a different immediate peer */
2369 if ((neighbor == NULL) ||
2370 (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2372 &update_matching_neighbors,
2373 neighbor_update) != GNUNET_SYSERR))
2377 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2379 distant_neighbor_free(neighbor);
2381 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2387 if (cost > fisheye_depth)
2390 GNUNET_free(neighbor_update);
2394 #if DEBUG_DV_PEER_NUMBERS
2395 encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2398 GNUNET_free(encAbout);
2401 if (max_table_size <=
2402 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2404 /* remove most expensive entry */
2405 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2406 GNUNET_assert(max != NULL);
2407 if (cost > max->cost)
2409 /* new entry most expensive, don't create */
2410 GNUNET_free(neighbor_update);
2415 /* only free if this is not a direct connection;
2416 we could theoretically have more direct
2417 connections than DV entries allowed total! */
2418 distant_neighbor_free (max);
2422 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2423 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2424 referrer->referee_tail, neighbor);
2425 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2427 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2429 neighbor->referrer = referrer;
2430 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2431 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2433 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2434 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2437 neighbor->pkey = pkey;
2439 neighbor->last_activity = now;
2440 neighbor->cost = cost;
2441 neighbor->referrer_id = referrer_peer_id;
2442 neighbor->our_id = our_id;
2444 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2447 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2449 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2451 if ((referrer_peer_id != 0) && (referrer->pending_message.sender_id == referrer_peer_id)) /* We have a queued message from just learned about peer! */
2453 #if DEBUG_DV_MESSAGES
2454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: learned about peer %llu from which we have a previous unknown message, processing!\n", my_short_id, referrer_peer_id);
2456 handle_dv_data_message(NULL, &referrer->pending_message.sender, referrer->pending_message.message, referrer->pending_message.latency, referrer->pending_message.distance);
2457 GNUNET_free(referrer->pending_message.message);
2458 referrer->pending_message.sender_id = 0;
2461 if (cost != DIRECT_NEIGHBOR_COST)
2463 /* Added neighbor, now send HELLO to transport */
2464 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2465 hello_context->direct_peer = &referrer->identity;
2466 memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2467 hello_context->addresses_to_add = 1;
2468 hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2469 GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2470 addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2471 addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: GIVING HELLO size %d for %s via %s to TRANSPORT\n", my_short_id, GNUNET_HELLO_size(hello_msg), addr2, addr1);
2475 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2476 GNUNET_free(hello_context);
2477 GNUNET_free(hello_msg);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2490 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2493 GNUNET_free(neighbor_update);
2499 * Core handler for dv disconnect messages. These will be used
2500 * by us to tell transport via the dv plugin that a peer can
2501 * no longer be contacted by us via a certain address. We should
2502 * then propagate these messages on, given that the distance to
2503 * the peer indicates we would have gossiped about it to others.
2505 * @param cls closure
2506 * @param peer peer which sent the message (immediate sender)
2507 * @param message the message
2508 * @param latency the latency of the connection we received the message from
2509 * @param distance the distance to the immediate peer
2511 static int handle_dv_disconnect_message (void *cls,
2512 const struct GNUNET_PeerIdentity *peer,
2513 const struct GNUNET_MessageHeader *message,
2514 struct GNUNET_TIME_Relative latency,
2517 struct DirectNeighbor *referrer;
2518 struct DistantNeighbor *distant;
2519 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2521 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2523 return GNUNET_SYSERR; /* invalid message */
2526 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2528 if (referrer == NULL)
2531 distant = referrer->referee_head;
2532 while (distant != NULL)
2534 if (distant->referrer_id == ntohl(enc_message->peer_id))
2536 distant_neighbor_free(distant);
2538 distant = referrer->referee_head;
2546 * Core handler for dv gossip messages. These will be used
2547 * by us to create a HELLO message for the newly peer containing
2548 * which direct peer we can connect through, and what the cost
2549 * is. This HELLO will then be scheduled for validation by the
2550 * transport service so that it can be used by all others.
2552 * @param cls closure
2553 * @param peer peer which sent the message (immediate sender)
2554 * @param message the message
2555 * @param latency the latency of the connection we received the message from
2556 * @param distance the distance to the immediate peer
2558 static int handle_dv_gossip_message (void *cls,
2559 const struct GNUNET_PeerIdentity *peer,
2560 const struct GNUNET_MessageHeader *message,
2561 struct GNUNET_TIME_Relative latency,
2564 struct DirectNeighbor *referrer;
2565 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2567 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2569 return GNUNET_SYSERR; /* invalid message */
2572 #if DEBUG_DV_GOSSIP_RECEIPT
2573 char * encPeerAbout;
2576 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2577 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "%s: Received %s message from peer %s about peer %s id %u distance %d!\n", GNUNET_i2s(&my_identity), "DV GOSSIP", encPeerFrom, encPeerAbout, ntohl(enc_message->neighbor_id), ntohl (enc_message->cost) + 1);
2580 GNUNET_free(encPeerAbout);
2581 GNUNET_free(encPeerFrom);
2584 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2586 if (referrer == NULL)
2589 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2590 ntohl (enc_message->neighbor_id),
2591 referrer, ntohl (enc_message->cost) + 1);
2598 * Iterate over all currently known peers, add them to the
2599 * fast gossip list for this peer so we get DV routing information
2600 * out as fast as possible!
2602 * @param cls the direct neighbor we will gossip to
2603 * @param key the hashcode of the peer
2604 * @param value the distant neighbor we should add to the list
2606 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2608 static int add_all_extended_peers (void *cls,
2609 const GNUNET_HashCode * key,
2612 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2613 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2614 struct FastGossipNeighborList *gossip_entry;
2616 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2617 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2620 if (distant->hidden == GNUNET_YES)
2621 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2623 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2624 gossip_entry->about = distant;
2626 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2627 send_context->fast_gossip_list_tail,
2628 send_context->fast_gossip_list_tail,
2636 * Iterator over hash map entries.
2638 * @param cls closure
2639 * @param key current key code
2640 * @param value value in the hash map
2641 * @return GNUNET_YES if we should continue to
2645 static int gossip_all_to_all_iterator (void *cls,
2646 const GNUNET_HashCode * key,
2649 struct DirectNeighbor *direct = value;
2651 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2653 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2654 GNUNET_SCHEDULER_cancel(sched, direct->send_context->task);
2656 direct->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, direct->send_context);
2661 * Task run during shutdown.
2667 gossip_all_to_all (void *cls,
2668 const struct GNUNET_SCHEDULER_TaskContext *tc)
2670 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2672 GNUNET_SCHEDULER_add_delayed (sched,
2673 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2680 * Iterate over all current direct peers, add newly connected peer
2681 * to the fast gossip list for that peer so we get DV routing
2682 * information out as fast as possible!
2684 * @param cls the newly connected neighbor we will gossip about
2685 * @param key the hashcode of the peer
2686 * @param value the direct neighbor we should gossip to
2688 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2690 static int add_all_direct_neighbors (void *cls,
2691 const GNUNET_HashCode * key,
2694 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2695 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2696 struct DistantNeighbor *distant;
2697 struct NeighborSendContext *send_context = direct->send_context;
2698 struct FastGossipNeighborList *gossip_entry;
2702 distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2703 if (distant == NULL)
2708 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2710 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2714 if (distant->hidden == GNUNET_YES)
2715 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2717 direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2719 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: adding peer %s to fast send list for %s\n", my_short_id, GNUNET_i2s(&distant->identity), direct_id);
2721 GNUNET_free(direct_id);
2722 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2723 gossip_entry->about = distant;
2725 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2726 send_context->fast_gossip_list_tail,
2727 send_context->fast_gossip_list_tail,
2729 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2730 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2732 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2733 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2734 //neighbor_send_task(send_context, &tc);
2739 * Type of an iterator over the hosts. Note that each
2740 * host will be called with each available protocol.
2742 * @param cls closure
2743 * @param peer id of the peer, NULL for last call
2744 * @param hello hello message for the peer (can be NULL)
2745 * @param trust amount of trust we have in the peer
2748 process_peerinfo (void *cls,
2749 const struct GNUNET_PeerIdentity *peer,
2750 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2752 struct PeerIteratorContext *peerinfo_iterator = cls;
2753 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2754 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2755 #if DEBUG_DV_PEER_NUMBERS
2762 if (distant->pkey == NULL)
2765 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2767 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2768 &peerinfo_iterator->neighbor->identity,
2770 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2776 GNUNET_free(peerinfo_iterator);
2781 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2784 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2786 if (distant->pkey == NULL)
2788 distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2789 memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2792 sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2794 #if DEBUG_DV_PEER_NUMBERS
2795 neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2798 sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2799 #if DEBUG_DV_PEER_NUMBERS
2800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2801 GNUNET_free(neighbor_pid);
2803 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2809 * Method called whenever a peer connects.
2811 * @param cls closure
2812 * @param peer peer identity this notification is about
2813 * @param latency reported latency of the connection with peer
2814 * @param distance reported distance (DV) to peer
2816 void handle_core_connect (void *cls,
2817 const struct GNUNET_PeerIdentity * peer,
2818 struct GNUNET_TIME_Relative latency,
2821 struct DirectNeighbor *neighbor;
2822 struct DistantNeighbor *about;
2823 struct PeerIteratorContext *peerinfo_iterator;
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2827 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2830 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2832 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2833 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2834 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2835 neighbor->send_context->toNeighbor = neighbor;
2836 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2838 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2840 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2841 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2842 peerinfo_iterator->distant = about;
2843 peerinfo_iterator->neighbor = neighbor;
2844 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2847 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2851 if ((about != NULL) && (about->pkey == NULL))
2854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2857 else if (about != NULL)
2859 GNUNET_free(peerinfo_iterator);
2864 about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2865 if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2866 sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2869 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2876 * Method called whenever a given peer disconnects.
2878 * @param cls closure
2879 * @param peer peer identity this notification is about
2881 void handle_core_disconnect (void *cls,
2882 const struct GNUNET_PeerIdentity * peer)
2884 struct DirectNeighbor *neighbor;
2885 struct DistantNeighbor *referee;
2886 struct FindDestinationContext fdc;
2887 struct DisconnectContext disconnect_context;
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 "%s: Receives core peer disconnect message!\n", "dv");
2895 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2896 if (neighbor == NULL)
2900 while (NULL != (referee = neighbor->referee_head))
2901 distant_neighbor_free (referee);
2906 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
2908 if (fdc.dest != NULL)
2910 disconnect_context.direct = neighbor;
2911 disconnect_context.distant = fdc.dest;
2912 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2915 GNUNET_assert (neighbor->referee_tail == NULL);
2916 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
2917 &peer->hashPubKey, neighbor))
2921 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2922 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2923 GNUNET_free (neighbor);
2928 * Process dv requests.
2930 * @param cls closure
2931 * @param scheduler scheduler to use
2932 * @param server the initialized server
2933 * @param c configuration to use
2937 struct GNUNET_SCHEDULER_Handle *scheduler,
2938 struct GNUNET_SERVER_Handle *server,
2939 const struct GNUNET_CONFIGURATION_Handle *c)
2941 unsigned long long max_hosts;
2945 /* FIXME: Read from config, or calculate, or something other than this! */
2946 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2947 max_table_size = DEFAULT_DV_SIZE;
2948 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2950 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2951 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
2953 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2954 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
2957 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2958 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
2961 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2963 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2965 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2966 extended_neighbors =
2967 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
2969 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2971 GNUNET_CORE_connect (sched,
2973 GNUNET_TIME_relative_get_forever(),
2974 NULL, /* FIXME: anything we want to pass around? */
2976 &handle_core_connect,
2977 &handle_core_disconnect,
2984 if (coreAPI == NULL)
2987 coreMST = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE,
2988 &tokenized_message_handler,
2991 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2993 if (peerinfo_handle == NULL)
2995 GNUNET_CORE_disconnect(coreAPI);
2999 /* Scheduled the task to clean up when shutdown is called */
3000 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
3001 GNUNET_TIME_UNIT_FOREVER_REL,
3005 GNUNET_SCHEDULER_add_delayed (sched,
3006 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
3014 * The main function for the dv service.
3016 * @param argc number of arguments from the command line
3017 * @param argv command line arguments
3018 * @return 0 ok, 1 on error
3021 main (int argc, char *const *argv)
3023 return (GNUNET_OK ==
3024 GNUNET_SERVICE_run (argc,
3027 GNUNET_SERVICE_OPTION_NONE,
3028 &run, NULL)) ? 0 : 1;