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 many outstanding messages (unknown sender) will we allow per peer?
59 #define MAX_OUTSTANDING_MESSAGES 5
62 * How often do we check about sending out more peer information (if
63 * we are connected to no peers previously).
65 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
68 * How long do we wait at most between sending out information?
70 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
73 * How long can we have not heard from a peer and
74 * still have it in our tables?
76 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
79 * Priority for gossip.
81 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
84 * How often should we check if expiration time has elapsed for
87 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
90 * How long to allow a message to be delayed?
92 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
95 * Priority to use for DV data messages.
100 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
102 #define DIRECT_NEIGHBOR_COST 1
105 * The default number of direct connections to store in DV (max)
107 #define DEFAULT_DIRECT_CONNECTIONS 50
110 * The default size of direct + extended peers in DV (max)
112 #define DEFAULT_DV_SIZE 100
115 * The default fisheye depth, from how many hops away will
118 #define DEFAULT_FISHEYE_DEPTH 4
121 * Linked list of messages to send to clients.
123 struct PendingMessage
126 * Pointer to next item in the list
128 struct PendingMessage *next;
131 * Pointer to previous item in the list
133 struct PendingMessage *prev;
136 * The PeerIdentity to send to
138 struct GNUNET_PeerIdentity recipient;
141 * The result of message sending.
143 struct GNUNET_DV_SendResultMessage *send_result;
146 * Message importance level.
148 unsigned int importance;
153 unsigned int msg_size;
156 * How long to wait before sending message.
158 struct GNUNET_TIME_Relative timeout;
161 * Actual message to be sent; // avoid allocation
163 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
167 struct FastGossipNeighborList
170 * Next element of DLL
172 struct FastGossipNeighborList *next;
175 * Prev element of DLL
177 struct FastGossipNeighborList *prev;
180 * The neighbor to gossip about
182 struct DistantNeighbor *about;
186 * Context created whenever a direct peer connects to us,
187 * used to gossip other peers to it.
189 struct NeighborSendContext
192 * The peer we will gossip to.
194 struct DirectNeighbor *toNeighbor;
197 * The task associated with this context.
199 GNUNET_SCHEDULER_TaskIdentifier task;
202 * Head of DLL of peers to gossip about
203 * as fast as possible to this peer, for initial
206 struct FastGossipNeighborList *fast_gossip_list_head;
209 * Tail of DLL of peers to gossip about
210 * as fast as possible to this peer, for initial
213 struct FastGossipNeighborList *fast_gossip_list_tail;
219 * Struct to hold information for updating existing neighbors
221 struct NeighborUpdateInfo
229 * The existing neighbor
231 struct DistantNeighbor *neighbor;
234 * The referrer of the possibly existing peer
236 struct DirectNeighbor *referrer;
239 * The time we heard about this peer
241 struct GNUNET_TIME_Absolute now;
244 * Peer id this peer uses to refer to neighbor.
246 unsigned int referrer_peer_id;
251 * Struct to store a single message received with
254 struct UnknownSenderMessage
257 * Message sender (immediate)
259 struct GNUNET_PeerIdentity sender;
262 * The actual message received
264 struct GNUNET_MessageHeader *message;
267 * Latency of connection
269 struct GNUNET_TIME_Relative latency;
272 * Distance to destination
283 * Struct where actual neighbor information is stored,
284 * referenced by min_heap and max_heap. Freeing dealt
285 * with when items removed from hashmap.
287 struct DirectNeighbor
290 * Identity of neighbor.
292 struct GNUNET_PeerIdentity identity;
295 * PublicKey of neighbor.
297 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
300 * Head of DLL of nodes that this direct neighbor referred to us.
302 struct DistantNeighbor *referee_head;
305 * Tail of DLL of nodes that this direct neighbor referred to us.
307 struct DistantNeighbor *referee_tail;
310 * The sending context for gossiping peers to this neighbor.
312 struct NeighborSendContext *send_context;
315 * Is this one of the direct neighbors that we are "hiding"
321 * Save messages immediately from this direct neighbor from a
322 * distan peer we don't know on the chance that it will be
323 * gossiped about and we can deliver the message.
325 struct UnknownSenderMessage pending_messages[MAX_OUTSTANDING_MESSAGES];
330 * Struct where actual neighbor information is stored,
331 * referenced by min_heap and max_heap. Freeing dealt
332 * with when items removed from hashmap.
334 struct DistantNeighbor
337 * We keep distant neighbor's of the same referrer in a DLL.
339 struct DistantNeighbor *next;
342 * We keep distant neighbor's of the same referrer in a DLL.
344 struct DistantNeighbor *prev;
349 struct GNUNET_CONTAINER_HeapNode *min_loc;
354 struct GNUNET_CONTAINER_HeapNode *max_loc;
357 * Identity of referrer (next hop towards 'neighbor').
359 struct DirectNeighbor *referrer;
362 * Identity of neighbor.
364 struct GNUNET_PeerIdentity identity;
367 * PublicKey of neighbor.
369 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
372 * Last time we received routing information from this peer
374 struct GNUNET_TIME_Absolute last_activity;
377 * Last time we sent routing information about this peer
379 struct GNUNET_TIME_Absolute last_gossip;
382 * Cost to neighbor, used for actual distance vector computations
387 * Random identifier *we* use for this peer, to be used as shortcut
388 * instead of sending full peer id for each message
393 * Random identifier the *referrer* uses for this peer.
395 unsigned int referrer_id;
398 * Is this one of the direct neighbors that we are "hiding"
405 struct PeerIteratorContext
408 * The actual context, to be freed later.
410 struct GNUNET_PEERINFO_IteratorContext *ic;
413 * The neighbor about which we are concerned.
415 struct DirectNeighbor *neighbor;
418 * The distant neighbor entry for this direct neighbor.
420 struct DistantNeighbor *distant;
425 * Context used for creating hello messages when
426 * gossips are received.
431 * Identity of distant neighbor.
433 struct GNUNET_PeerIdentity distant_peer;
436 * Identity of direct neighbor, via which we send this message.
438 const struct GNUNET_PeerIdentity *direct_peer;
441 * How many addresses do we need to add (always starts at 1, then set to 0)
443 int addresses_to_add;
447 struct DV_SendContext
450 * The distant peer (should always match)
452 struct GNUNET_PeerIdentity *distant_peer;
455 * The direct peer, we need to verify the referrer of.
457 struct GNUNET_PeerIdentity *direct_peer;
460 * The message to be sent
462 struct GNUNET_MessageHeader *message;
465 * The pre-built send result message. Simply needs to be queued
466 * and freed once send has been called!
468 struct GNUNET_DV_SendResultMessage *send_result;
471 * The size of the message being sent, may be larger
472 * than message->header.size because it's multiple
473 * messages packed into one!
478 * How important is this message?
480 unsigned int importance;
483 * Timeout for this message
485 struct GNUNET_TIME_Relative timeout;
488 * Unique ID for DV message
493 struct FindDestinationContext
496 struct DistantNeighbor *dest;
502 struct GNUNET_PeerIdentity *dest;
503 const struct GNUNET_PeerIdentity *via;
506 struct DisconnectContext
509 * Distant neighbor to get pid from.
511 struct DistantNeighbor *distant;
514 * Direct neighbor that disconnected.
516 struct DirectNeighbor *direct;
519 struct TokenizedMessageContext
522 * Immediate sender of this message
524 const struct GNUNET_PeerIdentity *peer;
527 * Distant sender of the message
529 struct DistantNeighbor *distant;
532 * Uid for this set of messages
538 * Context for finding the least cost peer to send to.
539 * Transport selection can only go so far.
541 struct FindLeastCostContext
543 struct DistantNeighbor *target;
544 unsigned int least_cost;
548 * Handle to the core service api.
550 static struct GNUNET_CORE_Handle *coreAPI;
553 * Stream tokenizer to handle messages coming in from core.
555 static struct GNUNET_SERVER_MessageStreamTokenizer *coreMST;
558 * The identity of our peer.
560 static struct GNUNET_PeerIdentity my_identity;
563 * The configuration for this service.
565 static const struct GNUNET_CONFIGURATION_Handle *cfg;
568 * The scheduler for this service.
570 static struct GNUNET_SCHEDULER_Handle *sched;
573 * The client, the DV plugin connected to us. Hopefully
574 * this client will never change, although if the plugin dies
575 * and returns for some reason it may happen.
577 static struct GNUNET_SERVER_Client * client_handle;
580 * Task to run when we shut down, cleaning up all our trash
582 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
584 static size_t default_dv_priority = 0;
586 static char *my_short_id;
589 * Transmit handle to the plugin.
591 static struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
594 * Head of DLL for client messages
596 static struct PendingMessage *plugin_pending_head;
599 * Tail of DLL for client messages
601 static struct PendingMessage *plugin_pending_tail;
604 * Handle to the peerinfo service
606 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
609 * Transmit handle to core service.
611 static struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
614 * Head of DLL for core messages
616 static struct PendingMessage *core_pending_head;
619 * Tail of DLL for core messages
621 static struct PendingMessage *core_pending_tail;
624 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
625 * directly connected peers.
627 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
630 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
631 * peers connected via DV (extended neighborhood). Does ALSO
632 * include any peers that are in 'direct_neighbors'; for those
633 * peers, the cost will be zero and the referrer all zeros.
635 static struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
638 * We use the min heap (min refers to cost) to prefer
639 * gossipping about peers with small costs.
641 static struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
644 * We use the max heap (max refers to cost) for general
645 * iterations over all peers and to remove the most costly
646 * connection if we have too many.
648 static struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
651 * How far out to keep peers we learn about.
653 static unsigned long long fisheye_depth;
656 * How many peers to store at most.
658 static unsigned long long max_table_size;
661 * We've been given a target ID based on the random numbers that
662 * we assigned to our DV-neighborhood. Find the entry for the
663 * respective neighbor.
666 find_destination (void *cls,
667 struct GNUNET_CONTAINER_HeapNode *node,
668 void *element, GNUNET_CONTAINER_HeapCostType cost)
670 struct FindDestinationContext *fdc = cls;
671 struct DistantNeighbor *dn = element;
673 if (fdc->tid != dn->our_id)
681 * We've been given a target ID based on the random numbers that
682 * we assigned to our DV-neighborhood. Find the entry for the
683 * respective neighbor.
686 find_specific_id (void *cls,
687 const GNUNET_HashCode *key,
690 struct FindIDContext *fdc = cls;
691 struct DistantNeighbor *dn = value;
693 if (memcmp(&dn->referrer->identity, fdc->via, sizeof(struct GNUNET_PeerIdentity)) == 0)
695 fdc->tid = dn->referrer_id;
702 * Find a distant peer whose referrer_id matches what we're
703 * looking for. For looking up a peer we've gossipped about
704 * but is now disconnected. Need to do this because we don't
705 * want to remove those that may be accessible via a different
708 static int find_distant_peer (void *cls,
709 const GNUNET_HashCode * key,
712 struct FindDestinationContext *fdc = cls;
713 struct DistantNeighbor *distant = value;
715 if (fdc->tid == distant->referrer_id)
724 * Function called to notify a client about the socket
725 * begin ready to queue more data. "buf" will be
726 * NULL and "size" zero if the socket was closed for
727 * writing in the meantime.
730 * @param size number of bytes available in buf
731 * @param buf where the callee should write the message
732 * @return number of bytes written to buf
734 size_t transmit_to_plugin (void *cls,
735 size_t size, void *buf)
738 struct PendingMessage *reply;
744 /* client disconnected */
745 #if DEBUG_DV_MESSAGES
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: %s buffer was NULL (client disconnect?)\n", my_short_id, "transmit_to_plugin");
750 plugin_transmit_handle = NULL;
752 while ( (NULL != (reply = plugin_pending_head)) &&
753 (size >= off + (msize = ntohs (reply->msg->size))))
755 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
758 memcpy (&cbuf[off], reply->msg, msize);
763 if (plugin_pending_head != NULL)
764 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
765 ntohs(plugin_pending_head->msg->size),
766 GNUNET_TIME_UNIT_FOREVER_REL,
767 &transmit_to_plugin, NULL);
773 * Send a message to the dv plugin.
775 * @param sender the direct sender of the message
776 * @param message the message to send to the plugin
777 * (may be an encapsulated type)
778 * @param message_size the size of the message to be sent
779 * @param distant_neighbor the original sender of the message
780 * @param cost the cost to the original sender of the message
782 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
783 const struct GNUNET_MessageHeader *message,
785 struct GNUNET_PeerIdentity *distant_neighbor,
788 struct GNUNET_DV_MessageReceived *received_msg;
789 struct PendingMessage *pending_message;
790 char *sender_address;
791 size_t sender_address_len;
792 char *packed_msg_start;
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
799 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
801 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
802 sender_address = GNUNET_malloc(sender_address_len);
803 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
804 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
808 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
809 sender_address = GNUNET_malloc(sender_address_len);
810 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
813 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
814 received_msg = GNUNET_malloc(size);
815 received_msg->header.size = htons(size);
816 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
817 received_msg->sender_address_len = htonl(sender_address_len);
818 received_msg->distance = htonl(cost);
819 received_msg->msg_len = htonl(message_size);
820 /* Set the sender in this message to be the original sender! */
821 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
822 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
823 memcpy(&received_msg[1], sender_address, sender_address_len);
824 GNUNET_free(sender_address);
825 /* Copy the actual message after the sender */
826 packed_msg_start = (char *)&received_msg[1];
827 packed_msg_start = &packed_msg_start[sender_address_len];
828 memcpy(packed_msg_start, message, message_size);
829 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
830 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
831 memcpy(&pending_message[1], received_msg, size);
832 GNUNET_free(received_msg);
834 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
836 if (client_handle != NULL)
838 if (plugin_transmit_handle == NULL)
840 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
841 size, GNUNET_TIME_UNIT_FOREVER_REL,
842 &transmit_to_plugin, NULL);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
851 /* Declare here so retry_core_send is aware of it */
852 size_t core_transmit_notify (void *cls,
853 size_t size, void *buf);
856 * Try to send another message from our core sending list
859 try_core_send (void *cls,
860 const struct GNUNET_SCHEDULER_TaskContext *tc)
862 struct PendingMessage *pending;
863 pending = core_pending_head;
865 if (core_transmit_handle != NULL)
866 return; /* Message send already in progress */
869 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, pending->importance, pending->timeout, &pending->recipient, pending->msg_size, &core_transmit_notify, NULL);
873 * Function called to notify a client about the socket
874 * being ready to queue more data. "buf" will be
875 * NULL and "size" zero if the socket was closed for
876 * writing in the meantime.
878 * @param cls closure (NULL)
879 * @param size number of bytes available in buf
880 * @param buf where the callee should write the message
881 * @return number of bytes written to buf
883 size_t core_transmit_notify (void *cls,
884 size_t size, void *buf)
887 struct PendingMessage *pending;
888 struct PendingMessage *client_reply;
894 /* client disconnected */
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
901 core_transmit_handle = NULL;
903 pending = core_pending_head;
904 if ( (pending != NULL) &&
905 (size >= (msize = ntohs (pending->msg->size))))
908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
910 GNUNET_CONTAINER_DLL_remove (core_pending_head,
913 if (pending->send_result != NULL) /* Will only be non-null if a real client asked for this send */
915 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
916 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
917 memcpy(&client_reply[1], pending->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
918 GNUNET_free(pending->send_result);
920 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
921 if (client_handle != NULL)
923 if (plugin_transmit_handle == NULL)
925 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
926 sizeof(struct GNUNET_DV_SendResultMessage),
927 GNUNET_TIME_UNIT_FOREVER_REL,
928 &transmit_to_plugin, NULL);
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
936 memcpy (&cbuf[off], pending->msg, msize);
937 GNUNET_free (pending);
940 /*reply = core_pending_head;*/
942 GNUNET_SCHEDULER_add_now(sched, &try_core_send, NULL);
944 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);*/
951 * Send a DV data message via DV.
953 * @param sender the original sender of the message
954 * @param recipient the next hop recipient, may be our direct peer, maybe not
955 * @param send_context the send context
958 send_message_via (const struct GNUNET_PeerIdentity *sender,
959 const struct GNUNET_PeerIdentity *recipient,
960 struct DV_SendContext *send_context)
962 p2p_dv_MESSAGE_Data *toSend;
963 unsigned int msg_size;
964 unsigned int recipient_id;
965 unsigned int sender_id;
966 struct DistantNeighbor *source;
967 struct PendingMessage *pending_message;
968 struct FindIDContext find_context;
973 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
975 find_context.dest = send_context->distant_peer;
976 find_context.via = recipient;
977 find_context.tid = 0;
978 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
979 &find_specific_id, &find_context);
981 if (find_context.tid == 0)
983 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
984 /* target unknown to us, drop! */
985 return GNUNET_SYSERR;
987 recipient_id = find_context.tid;
989 if (0 == (memcmp (&my_identity,
990 sender, sizeof (struct GNUNET_PeerIdentity))))
993 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
994 &sender->hashPubKey);
996 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));
1000 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1001 &sender->hashPubKey);
1004 /* sender unknown to us, drop! */
1005 return GNUNET_SYSERR;
1007 sender_id = source->our_id;
1010 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1011 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1012 pending_message->send_result = send_context->send_result;
1013 memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
1014 pending_message->msg_size = msg_size;
1015 pending_message->importance = send_context->importance;
1016 pending_message->timeout = send_context->timeout;
1017 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1018 toSend->header.size = htons (msg_size);
1019 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1020 toSend->sender = htonl (sender_id);
1021 toSend->recipient = htonl (recipient_id);
1022 #if DEBUG_DV_MESSAGES
1023 toSend->uid = send_context->uid; /* Still sent around in network byte order */
1025 toSend->uid = htonl(0);
1028 memcpy (&toSend[1], send_context->message, send_context->message_size);
1031 memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
1032 shortname[4] = '\0';
1033 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);
1036 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1041 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1047 * Given a FindLeastCostContext, and a set
1048 * of peers that match the target, return the cheapest.
1050 * @param cls closure, a struct FindLeastCostContext
1051 * @param key the key identifying the target peer
1052 * @param value the target peer
1054 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1057 find_least_cost_peer (void *cls,
1058 const GNUNET_HashCode *key,
1061 struct FindLeastCostContext *find_context = cls;
1062 struct DistantNeighbor *dn = value;
1064 if (dn->cost < find_context->least_cost)
1066 find_context->target = dn;
1068 if (dn->cost == DIRECT_NEIGHBOR_COST)
1074 * Send a DV data message via DV.
1076 * @param recipient the ultimate recipient of this message
1077 * @param sender the original sender of the message
1078 * @param specific_neighbor the specific neighbor to send this message via
1079 * @param message the packed message
1080 * @param message_size size of the message
1081 * @param importance what priority to send this message with
1082 * @param timeout how long to possibly delay sending this message
1085 send_message (const struct GNUNET_PeerIdentity * recipient,
1086 const struct GNUNET_PeerIdentity * sender,
1087 const struct DistantNeighbor * specific_neighbor,
1088 const struct GNUNET_MessageHeader * message,
1089 size_t message_size,
1090 unsigned int importance,
1092 struct GNUNET_TIME_Relative timeout)
1094 p2p_dv_MESSAGE_Data *toSend;
1095 unsigned int msg_size;
1097 unsigned int recipient_id;
1098 unsigned int sender_id;
1099 struct DistantNeighbor *target;
1100 struct DistantNeighbor *source;
1101 struct PendingMessage *pending_message;
1102 struct FindLeastCostContext find_least_ctx;
1103 #if DEBUG_DV_PEER_NUMBERS
1104 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1105 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1106 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1108 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1110 find_least_ctx.least_cost = -1;
1111 find_least_ctx.target = NULL;
1113 * Need to find the least cost peer, lest the transport selection keep
1114 * picking the same DV route for the same destination which results
1115 * in messages looping forever. Relatively cheap, we don't iterate
1116 * over all known peers, just those that apply.
1118 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1119 &recipient->hashPubKey, &find_least_cost_peer, &find_least_ctx);
1120 target = find_least_ctx.target;
1124 /* target unknown to us, drop! */
1125 return GNUNET_SYSERR;
1127 recipient_id = target->referrer_id;
1129 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1130 &sender->hashPubKey);
1133 if (0 != (memcmp (&my_identity,
1134 sender, sizeof (struct GNUNET_PeerIdentity))))
1136 /* sender unknown to us, drop! */
1137 return GNUNET_SYSERR;
1139 sender_id = 0; /* 0 == us */
1143 /* find out the number that we use when we gossip about
1145 sender_id = source->our_id;
1148 #if DEBUG_DV_PEER_NUMBERS
1149 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1150 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1151 encPeerFrom.encoding[4] = '\0';
1152 encPeerVia.encoding[4] = '\0';
1154 if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1159 cost = target->cost;
1160 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1161 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1162 pending_message->send_result = NULL;
1163 pending_message->importance = importance;
1164 pending_message->timeout = timeout;
1165 memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1166 pending_message->msg_size = msg_size;
1167 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1168 toSend->header.size = htons (msg_size);
1169 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1170 toSend->sender = htonl (sender_id);
1171 toSend->recipient = htonl (recipient_id);
1172 #if DEBUG_DV_MESSAGES
1173 toSend->uid = htonl(uid);
1175 toSend->uid = htonl(0);
1178 #if DEBUG_DV_PEER_NUMBERS
1179 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1180 encPeerTo.encoding[4] = '\0';
1181 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);
1183 memcpy (&toSend[1], message, message_size);
1184 if (source->pkey == NULL) /* Test our hypothesis about message failures! */
1186 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: Sending message, but anticipate recipient will not know sender!!!\n\n\n");
1188 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1193 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));
1196 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1197 /*if (core_transmit_handle == NULL)
1198 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
1200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: CORE ALREADY SENDING\n", "DV SEND MESSAGE", msg_size);*/
1205 struct CheckPeerContext
1210 struct DistantNeighbor *peer;
1213 * Sender id to search for
1215 unsigned int sender_id;
1219 * Iterator over hash map entries.
1221 * @param cls closure
1222 * @param key current key code
1223 * @param value value in the hash map
1224 * @return GNUNET_YES if we should continue to
1228 int checkPeerID (void *cls,
1229 const GNUNET_HashCode * key,
1232 struct CheckPeerContext *ctx = cls;
1233 struct DistantNeighbor *distant = value;
1235 if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1237 ctx->peer = distant;
1247 * Handler for messages parsed out by the tokenizer from
1248 * DV DATA received for this peer.
1251 * @param client the TokenizedMessageContext which contains message information
1252 * @param message the actual message
1254 void tokenized_message_handler (void *cls,
1256 const struct GNUNET_MessageHeader *message)
1258 struct TokenizedMessageContext *ctx = client;
1259 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1260 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1261 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1262 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1264 #if DEBUG_DV_MESSAGES
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "%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));
1268 GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1269 send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1274 struct DelayedMessageContext
1276 struct GNUNET_PeerIdentity dest;
1277 struct GNUNET_PeerIdentity sender;
1278 struct GNUNET_MessageHeader *message;
1279 size_t message_size;
1283 void send_message_delayed (void *cls,
1284 const struct GNUNET_SCHEDULER_TaskContext *tc)
1286 struct DelayedMessageContext *msg_ctx = cls;
1287 if (msg_ctx != NULL)
1289 send_message(&msg_ctx->dest,
1293 msg_ctx->message_size,
1294 default_dv_priority,
1296 GNUNET_TIME_relative_get_forever());
1297 GNUNET_free(msg_ctx->message);
1298 GNUNET_free(msg_ctx);
1305 * Core handler for dv data messages. Whatever this message
1306 * contains all we really have to do is rip it out of its
1307 * DV layering and give it to our pal the DV plugin to report
1310 * @param cls closure
1311 * @param peer peer which sent the message (immediate sender)
1312 * @param message the message
1313 * @param latency the latency of the connection we received the message from
1314 * @param distance the distance to the immediate peer
1316 static int handle_dv_data_message (void *cls,
1317 const struct GNUNET_PeerIdentity * peer,
1318 const struct GNUNET_MessageHeader * message,
1319 struct GNUNET_TIME_Relative latency,
1322 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1323 const struct GNUNET_MessageHeader *packed_message;
1324 struct DirectNeighbor *dn;
1325 struct DistantNeighbor *pos;
1326 unsigned int sid; /* Sender id */
1327 unsigned int tid; /* Target id */
1328 struct GNUNET_PeerIdentity *original_sender;
1329 struct GNUNET_PeerIdentity *destination;
1330 struct FindDestinationContext fdc;
1331 struct TokenizedMessageContext tkm_ctx;
1335 struct DelayedMessageContext *delayed_context;
1338 struct CheckPeerContext checkPeerCtx;
1340 #if DEBUG_DV_MESSAGES
1344 size_t packed_message_size;
1347 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "%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);
1353 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 "`%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));
1360 return GNUNET_SYSERR;
1363 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1368 sid = ntohl (incoming->sender);
1372 checkPeerCtx.sender_id = sid;
1373 checkPeerCtx.peer = NULL;
1374 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1375 pos = checkPeerCtx.peer;
1379 pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1383 pos = dn->referee_head;
1384 while ((NULL != pos) && (pos->referrer_id != sid))
1390 #if DEBUG_DV_MESSAGES
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "%s: unknown sender (%u), Message uid %llu from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1393 pos = dn->referee_head;
1394 while ((NULL != pos) && (pos->referrer_id != sid))
1396 sender_id = strdup(GNUNET_i2s(&pos->identity));
1397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1398 GNUNET_free(sender_id);
1402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1403 "%s: unknown sender (%u), Message uid %llu from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1406 for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
1408 if (dn->pending_messages[i].sender_id == 0)
1415 if (found_pos == -1)
1417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1418 "%s: Too many unknown senders (%u), ignoring message! Message uid %llu from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1422 dn->pending_messages[found_pos].message = GNUNET_malloc(ntohs (message->size));
1423 memcpy(dn->pending_messages[found_pos].message, message, ntohs(message->size));
1424 dn->pending_messages[found_pos].distance = distance;
1425 dn->pending_messages[found_pos].latency = latency;
1426 memcpy(&dn->pending_messages[found_pos].sender, peer, sizeof(struct GNUNET_PeerIdentity));
1427 dn->pending_messages[found_pos].sender_id = sid;
1429 /* unknown sender */
1432 original_sender = &pos->identity;
1433 tid = ntohl (incoming->recipient);
1437 cbuf = (char *)&incoming[1];
1439 tkm_ctx.peer = peer;
1440 tkm_ctx.distant = pos;
1441 tkm_ctx.uid = ntohl(incoming->uid);
1442 if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1445 packed_message_size,
1450 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1456 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1459 /* FIXME: this is the *only* per-request operation we have in DV
1460 that is O(n) in relation to the number of connected peers; a
1461 hash-table lookup could easily solve this (minor performance
1465 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1466 &find_destination, &fdc);
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1473 if (fdc.dest == NULL)
1475 #if DEBUG_DV_MESSAGES
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1477 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1481 destination = &fdc.dest->identity;
1483 if (0 == memcmp (destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1485 /* FIXME: create stat: routing loop-discard! */
1486 #if DEBUG_DV_PEER_NUMBERS
1487 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1490 #if DEBUG_DV_MESSAGES
1491 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "%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);
1498 /* At this point we have a message, and we need to forward it on to the
1501 #if DEBUG_DV_MESSAGES
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "%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);
1507 if (GNUNET_TIME_absolute_get_duration(pos->last_gossip).value < GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2).value)
1509 delayed_context = GNUNET_malloc(sizeof(struct DelayedMessageContext));
1510 memcpy(&delayed_context->dest, destination, sizeof(struct GNUNET_PeerIdentity));
1511 memcpy(&delayed_context->sender, original_sender, sizeof(struct GNUNET_PeerIdentity));
1512 delayed_context->message = GNUNET_malloc(packed_message_size);
1513 memcpy(delayed_context->message, packed_message, packed_message_size);
1514 delayed_context->message_size = packed_message_size;
1515 delayed_context->uid = ntohl(incoming->uid);
1516 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 2500), &send_message_delayed, delayed_context);
1522 ret = send_message(destination,
1526 packed_message_size,
1527 default_dv_priority,
1528 ntohl(incoming->uid),
1529 GNUNET_TIME_relative_get_forever());
1531 if (ret != GNUNET_SYSERR)
1535 #if DEBUG_MESSAGE_DROP
1536 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538 "%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);
1540 return GNUNET_SYSERR;
1546 * Iterator over hash map entries.
1548 * @param cls closure (NULL)
1549 * @param key current key code
1550 * @param value value in the hash map (DistantNeighbor)
1551 * @return GNUNET_YES if we should continue to
1555 int print_neighbors (void *cls,
1556 const GNUNET_HashCode * key,
1559 struct DistantNeighbor *distant_neighbor = value;
1560 char my_shortname[5];
1561 char referrer_shortname[5];
1562 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1563 my_shortname[4] = '\0';
1564 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1565 referrer_shortname[4] = '\0';
1567 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");
1573 * Scheduled task which gossips about known direct peers to other connected
1574 * peers. Will run until called with reason shutdown.
1577 neighbor_send_task (void *cls,
1578 const struct GNUNET_SCHEDULER_TaskContext *tc)
1580 struct NeighborSendContext *send_context = cls;
1581 #if DEBUG_DV_GOSSIP_SEND
1582 char * encPeerAbout;
1585 struct DistantNeighbor *about;
1586 struct DirectNeighbor *to;
1587 struct FastGossipNeighborList *about_list;
1589 p2p_dv_MESSAGE_NeighborInfo *message;
1590 struct PendingMessage *pending_message;
1592 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "%s: Called with reason shutdown, shutting down!\n",
1597 GNUNET_i2s(&my_identity));
1602 if (send_context->fast_gossip_list_head != NULL)
1604 about_list = send_context->fast_gossip_list_head;
1605 about = about_list->about;
1606 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1607 send_context->fast_gossip_list_tail,
1609 GNUNET_free(about_list);
1613 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1614 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1615 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1616 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1617 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1618 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1619 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1620 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1621 * asynchronously enough to avoid major problems (-;
1623 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1625 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1627 to = send_context->toNeighbor;
1629 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1631 (about->hidden == GNUNET_NO) &&
1634 (0 != memcmp (&about->identity,
1635 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1636 (about->pkey != NULL))
1638 #if DEBUG_DV_GOSSIP_SEND
1639 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1640 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1643 GNUNET_i2s(&my_identity),
1644 encPeerAbout, about->our_id, encPeerTo);
1645 GNUNET_free(encPeerAbout);
1646 GNUNET_free(encPeerTo);
1648 about->last_gossip = GNUNET_TIME_absolute_get();
1649 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1650 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1651 pending_message->importance = default_dv_priority;
1652 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1653 memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1654 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1655 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1656 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1657 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1658 message->cost = htonl (about->cost);
1659 message->neighbor_id = htonl (about->our_id);
1661 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1662 memcpy (&message->neighbor,
1663 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1665 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1670 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1671 /*if (core_transmit_handle == NULL)
1672 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);*/
1676 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1678 #if DEBUG_DV_PEER_NUMBERS
1679 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1681 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1685 #if DEBUG_DV_PEER_NUMBERS
1686 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1688 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1696 * Handle START-message. This is the first message sent to us
1697 * by the client (can only be one!).
1699 * @param cls closure (always NULL)
1700 * @param client identification of the client
1701 * @param message the actual message
1704 handle_start (void *cls,
1705 struct GNUNET_SERVER_Client *client,
1706 const struct GNUNET_MessageHeader *message)
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Received `%s' request from client\n", "START");
1714 client_handle = client;
1716 GNUNET_SERVER_client_keep(client_handle);
1717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1722 * Iterate over hash map entries for a distant neighbor,
1723 * if direct neighbor matches context call send message
1725 * @param cls closure, a DV_SendContext
1726 * @param key current key code
1727 * @param value value in the hash map
1728 * @return GNUNET_YES if we should continue to
1732 int send_iterator (void *cls,
1733 const GNUNET_HashCode * key,
1736 struct DV_SendContext *send_context = cls;
1737 struct DistantNeighbor *distant_neighbor = value;
1739 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1741 send_message_via(&my_identity, distant_neighbor, send_context);
1749 * Service server's handler for message send requests (which come
1750 * bubbling up to us through the DV plugin).
1752 * @param cls closure
1753 * @param client identification of the client
1754 * @param message the actual message
1756 void handle_dv_send_message (void *cls,
1757 struct GNUNET_SERVER_Client * client,
1758 const struct GNUNET_MessageHeader * message)
1760 struct GNUNET_DV_SendMessage *send_msg;
1761 struct GNUNET_DV_SendResultMessage *send_result_msg;
1762 struct PendingMessage *pending_message;
1764 size_t message_size;
1765 struct GNUNET_PeerIdentity *destination;
1766 struct GNUNET_PeerIdentity *direct;
1767 struct GNUNET_MessageHeader *message_buf;
1770 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1771 struct DV_SendContext *send_context;
1772 #if DEBUG_DV_MESSAGES
1774 struct GNUNET_MessageHeader *packed_message;
1777 if (client_handle == NULL)
1779 client_handle = client;
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1783 else if (client_handle != client)
1785 client_handle = client;
1786 /* What should we do in this case, assert fail or just log the warning? */
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "%s: Setting client handle (was a different client!)!\n", "dv");
1793 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1794 send_msg = (struct GNUNET_DV_SendMessage *)message;
1796 address_len = ntohl(send_msg->addrlen);
1797 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1798 message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1799 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1800 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1801 message_buf = GNUNET_malloc(message_size);
1803 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1804 offset = 0; /* Offset starts at zero */
1806 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1807 offset += sizeof(struct GNUNET_PeerIdentity);
1809 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1810 offset += sizeof(struct GNUNET_PeerIdentity);
1813 memcpy(message_buf, &temp_pos[offset], message_size);
1814 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1816 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1817 dest_hash.encoding[4] = '\0';
1818 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);
1821 #if DEBUG_DV_MESSAGES
1822 cbuf = (char *)message_buf;
1824 while(offset < message_size)
1826 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1827 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));
1828 offset += ntohs(packed_message->size);
1830 /*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));*/
1832 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1833 dest_hash.encoding[4] = '\0';
1834 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1836 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1837 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1838 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1839 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1841 send_context->importance = ntohl(send_msg->priority);
1842 send_context->timeout = send_msg->timeout;
1843 send_context->direct_peer = direct;
1844 send_context->distant_peer = destination;
1845 send_context->message = message_buf;
1846 send_context->message_size = message_size;
1847 send_context->send_result = send_result_msg;
1848 #if DEBUG_DV_MESSAGES
1849 send_context->uid = send_msg->uid;
1852 if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1854 send_result_msg->result = htons(1);
1855 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1856 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1857 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1858 GNUNET_free(send_result_msg);
1860 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1862 if (client_handle != NULL)
1864 if (plugin_transmit_handle == NULL)
1866 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1867 sizeof(struct GNUNET_DV_SendResultMessage),
1868 GNUNET_TIME_UNIT_FOREVER_REL,
1869 &transmit_to_plugin, NULL);
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1876 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1877 dest_hash.encoding[4] = '\0';
1878 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));
1881 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1883 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1885 send_result_msg->result = htons(1);
1886 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1887 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1888 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1889 GNUNET_free(send_result_msg);
1891 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1893 if (client_handle != NULL)
1895 if (plugin_transmit_handle == NULL)
1897 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1898 sizeof(struct GNUNET_DV_SendResultMessage),
1899 GNUNET_TIME_UNIT_FOREVER_REL,
1900 &transmit_to_plugin, NULL);
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1907 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1908 dest_hash.encoding[4] = '\0';
1909 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));
1912 GNUNET_free(message_buf);
1913 GNUNET_free(send_context);
1914 GNUNET_free(direct);
1915 GNUNET_free(destination);
1917 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1920 /** Forward declarations **/
1921 static int handle_dv_gossip_message (void *cls,
1922 const struct GNUNET_PeerIdentity *peer,
1923 const struct GNUNET_MessageHeader *message,
1924 struct GNUNET_TIME_Relative latency,
1927 static int handle_dv_disconnect_message (void *cls,
1928 const struct GNUNET_PeerIdentity *peer,
1929 const struct GNUNET_MessageHeader *message,
1930 struct GNUNET_TIME_Relative latency,
1932 /** End forward declarations **/
1936 * List of handlers for the messages understood by this
1939 * Hmm... will we need to register some handlers with core and
1940 * some handlers with our server here? Because core should be
1941 * getting the incoming DV messages (from whichever lower level
1942 * transport) and then our server should be getting messages
1943 * from the dv_plugin, right?
1945 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1946 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1947 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1948 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1952 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1953 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1954 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1959 * Free a DistantNeighbor node, including removing it
1960 * from the referer's list.
1963 distant_neighbor_free (struct DistantNeighbor *referee)
1965 struct DirectNeighbor *referrer;
1967 referrer = referee->referrer;
1968 if (referrer != NULL)
1970 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1971 referrer->referee_tail, referee);
1973 GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
1974 GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
1975 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
1976 &referee->identity.hashPubKey);
1977 GNUNET_free_non_null (referee->pkey);
1978 GNUNET_free (referee);
1982 * Free a DirectNeighbor node, including removing it
1983 * from the referer's list.
1986 direct_neighbor_free (struct DirectNeighbor *direct)
1988 struct NeighborSendContext *send_context;
1989 struct FastGossipNeighborList *about_list;
1990 struct FastGossipNeighborList *prev_about;
1992 send_context = direct->send_context;
1994 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1995 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1997 about_list = send_context->fast_gossip_list_head;
1998 while (about_list != NULL)
2000 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
2001 prev_about = about_list;
2002 about_list = about_list->next;
2003 GNUNET_free(prev_about);
2005 GNUNET_free(send_context);
2006 GNUNET_free(direct);
2010 * Multihashmap iterator for sending out disconnect messages
2013 * @param cls the peer that was disconnected
2014 * @param key key value stored under
2015 * @param value the direct neighbor to send disconnect to
2017 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2019 static int schedule_disconnect_messages (void *cls,
2020 const GNUNET_HashCode * key,
2023 struct DisconnectContext *disconnect_context = cls;
2024 struct DirectNeighbor *disconnected = disconnect_context->direct;
2025 struct DirectNeighbor *notify = value;
2026 struct PendingMessage *pending_message;
2027 p2p_dv_MESSAGE_Disconnect *disconnect_message;
2029 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2030 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
2032 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
2033 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
2034 pending_message->importance = default_dv_priority;
2035 pending_message->timeout = GNUNET_TIME_relative_get_forever();
2036 memcpy(&pending_message->recipient, ¬ify->identity, sizeof(struct GNUNET_PeerIdentity));
2037 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
2038 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
2039 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
2040 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
2041 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
2043 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
2048 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
2049 /*if (core_transmit_handle == NULL)
2050 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);*/
2056 * Multihashmap iterator for freeing extended neighbors.
2059 * @param key key value stored under
2060 * @param value the distant neighbor to be freed
2062 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2064 static int free_extended_neighbors (void *cls,
2065 const GNUNET_HashCode * key,
2068 struct DistantNeighbor *distant = value;
2069 distant_neighbor_free(distant);
2074 * Multihashmap iterator for freeing direct neighbors.
2077 * @param key key value stored under
2078 * @param value the direct neighbor to be freed
2080 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2082 static int free_direct_neighbors (void *cls,
2083 const GNUNET_HashCode * key,
2086 struct DirectNeighbor *direct = value;
2087 direct_neighbor_free(direct);
2093 * Task run during shutdown.
2099 shutdown_task (void *cls,
2100 const struct GNUNET_SCHEDULER_TaskContext *tc)
2103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2104 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2106 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2107 GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2108 GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2109 GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2111 GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2112 GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2114 GNUNET_CORE_disconnect (coreAPI);
2115 GNUNET_PEERINFO_disconnect(peerinfo_handle);
2116 GNUNET_SERVER_mst_destroy(coreMST);
2117 GNUNET_free_non_null(my_short_id);
2119 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2124 * To be called on core init/fail.
2126 void core_init (void *cls,
2127 struct GNUNET_CORE_Handle * server,
2128 const struct GNUNET_PeerIdentity *identity,
2129 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2134 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
2135 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2142 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2143 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2148 #if PKEY_NO_NEIGHBOR_ON_ADD
2150 * Iterator over hash map entries.
2152 * @param cls closure
2153 * @param key current key code
2154 * @param value value in the hash map
2155 * @return GNUNET_YES if we should continue to
2159 static int add_pkey_to_extended (void *cls,
2160 const GNUNET_HashCode * key,
2163 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2164 struct DistantNeighbor *distant_neighbor = value;
2166 if (distant_neighbor->pkey == NULL)
2168 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2169 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2177 * Iterator over hash map entries.
2179 * @param cls closure
2180 * @param key current key code
2181 * @param value value in the hash map
2182 * @return GNUNET_YES if we should continue to
2186 static int update_matching_neighbors (void *cls,
2187 const GNUNET_HashCode * key,
2190 struct NeighborUpdateInfo * update_info = cls;
2191 struct DistantNeighbor *distant_neighbor = value;
2193 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2195 /* same referrer, cost change! */
2196 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2197 update_info->neighbor->max_loc, update_info->cost);
2198 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2199 update_info->neighbor->min_loc, update_info->cost);
2200 update_info->neighbor->last_activity = update_info->now;
2201 update_info->neighbor->cost = update_info->cost;
2202 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2211 * Iterate over all current direct peers, add DISTANT newly connected
2212 * peer to the fast gossip list for that peer so we get DV routing
2213 * information out as fast as possible!
2215 * @param cls the newly connected neighbor we will gossip about
2216 * @param key the hashcode of the peer
2217 * @param value the direct neighbor we should gossip to
2219 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2221 static int add_distant_all_direct_neighbors (void *cls,
2222 const GNUNET_HashCode * key,
2225 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2226 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2227 struct NeighborSendContext *send_context = direct->send_context;
2228 struct FastGossipNeighborList *gossip_entry;
2234 if (distant == NULL)
2239 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2241 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2245 if (distant->hidden == GNUNET_YES)
2246 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2248 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2249 gossip_entry->about = distant;
2251 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2252 send_context->fast_gossip_list_tail,
2253 send_context->fast_gossip_list_tail,
2256 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2257 encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2260 GNUNET_i2s(&my_identity),
2261 encPeerAbout, distant->our_id, encPeerTo);
2262 GNUNET_free(encPeerAbout);
2263 GNUNET_free(encPeerTo);
2265 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2266 GNUNET_SCHEDULER_cancel(sched, send_context->task);*/
2268 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2273 * Callback for hello address creation.
2275 * @param cls closure, a struct HelloContext
2276 * @param max maximum number of bytes that can be written to buf
2277 * @param buf where to write the address information
2279 * @return number of bytes written, 0 to signal the
2280 * end of the iteration.
2283 generate_hello_address (void *cls, size_t max, void *buf)
2285 struct HelloContext *hello_context = cls;
2291 if (hello_context->addresses_to_add == 0)
2294 /* Hello "address" will be concatenation of distant peer and direct peer identities */
2295 size = 2 * sizeof(struct GNUNET_PeerIdentity);
2296 GNUNET_assert(max >= size);
2298 addr_buffer = GNUNET_malloc(size);
2300 /* Copy the distant peer identity to buffer */
2301 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
2302 offset += sizeof(struct GNUNET_PeerIdentity);
2303 /* Copy the direct peer identity to buffer */
2304 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
2305 ret = GNUNET_HELLO_add_address ("dv",
2306 GNUNET_TIME_relative_to_absolute
2307 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2310 hello_context->addresses_to_add--;
2312 GNUNET_free(addr_buffer);
2318 * Handles when a peer is either added due to being newly connected
2319 * or having been gossiped about, also called when the cost for a neighbor
2320 * needs to be updated.
2322 * @param peer identity of the peer whose info is being added/updated
2323 * @param pkey public key of the peer whose info is being added/updated
2324 * @param referrer_peer_id id to use when sending to 'peer'
2325 * @param referrer if this is a gossiped peer, who did we hear it from?
2326 * @param cost the cost of communicating with this peer via 'referrer'
2328 * @return the added neighbor, the updated neighbor or NULL (neighbor
2331 static struct DistantNeighbor *
2332 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2333 unsigned int referrer_peer_id,
2334 struct DirectNeighbor *referrer, unsigned int cost)
2336 struct DistantNeighbor *neighbor;
2337 struct DistantNeighbor *max;
2338 struct GNUNET_TIME_Absolute now;
2339 struct NeighborUpdateInfo *neighbor_update;
2340 struct HelloContext *hello_context;
2341 struct GNUNET_HELLO_Message *hello_msg;
2342 unsigned int our_id;
2347 #if DEBUG_DV_PEER_NUMBERS
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2353 now = GNUNET_TIME_absolute_get ();
2354 neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2356 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2357 neighbor_update->neighbor = neighbor;
2358 neighbor_update->cost = cost;
2359 neighbor_update->now = now;
2360 neighbor_update->referrer = referrer;
2361 neighbor_update->referrer_peer_id = referrer_peer_id;
2363 if (neighbor != NULL)
2366 memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2368 our_id = neighbor->our_id;
2374 memcpy(&our_id, peer, sizeof(unsigned int));
2376 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2380 /* Either we do not know this peer, or we already do but via a different immediate peer */
2381 if ((neighbor == NULL) ||
2382 (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2384 &update_matching_neighbors,
2385 neighbor_update) != GNUNET_SYSERR))
2389 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2391 distant_neighbor_free(neighbor);
2393 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2399 if (cost > fisheye_depth)
2402 GNUNET_free(neighbor_update);
2406 #if DEBUG_DV_PEER_NUMBERS
2407 encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2410 GNUNET_free(encAbout);
2413 if (max_table_size <=
2414 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2416 /* remove most expensive entry */
2417 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2418 GNUNET_assert(max != NULL);
2419 if (cost > max->cost)
2421 /* new entry most expensive, don't create */
2422 GNUNET_free(neighbor_update);
2427 /* only free if this is not a direct connection;
2428 we could theoretically have more direct
2429 connections than DV entries allowed total! */
2430 distant_neighbor_free (max);
2434 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2435 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2436 referrer->referee_tail, neighbor);
2437 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2439 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2441 neighbor->referrer = referrer;
2442 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2443 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2445 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2446 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2449 neighbor->pkey = pkey;
2451 neighbor->last_activity = now;
2452 neighbor->cost = cost;
2453 neighbor->referrer_id = referrer_peer_id;
2454 neighbor->our_id = our_id;
2456 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2459 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2461 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2462 if (referrer_peer_id != 0)
2464 for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
2466 if (referrer->pending_messages[i].sender_id == referrer_peer_id) /* We have a queued message from just learned about peer! */
2468 #if DEBUG_DV_MESSAGES
2469 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);
2471 handle_dv_data_message(NULL, &referrer->pending_messages[i].sender, referrer->pending_messages[i].message, referrer->pending_messages[i].latency, referrer->pending_messages[i].distance);
2472 GNUNET_free(referrer->pending_messages[i].message);
2473 referrer->pending_messages[i].sender_id = 0;
2477 if (cost != DIRECT_NEIGHBOR_COST)
2479 /* Added neighbor, now send HELLO to transport */
2480 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2481 hello_context->direct_peer = &referrer->identity;
2482 memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2483 hello_context->addresses_to_add = 1;
2484 hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2485 GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2486 addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2487 addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2489 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);
2493 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2494 GNUNET_free(hello_context);
2495 GNUNET_free(hello_msg);
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2511 GNUNET_free(neighbor_update);
2517 * Core handler for dv disconnect messages. These will be used
2518 * by us to tell transport via the dv plugin that a peer can
2519 * no longer be contacted by us via a certain address. We should
2520 * then propagate these messages on, given that the distance to
2521 * the peer indicates we would have gossiped about it to others.
2523 * @param cls closure
2524 * @param peer peer which sent the message (immediate sender)
2525 * @param message the message
2526 * @param latency the latency of the connection we received the message from
2527 * @param distance the distance to the immediate peer
2529 static int handle_dv_disconnect_message (void *cls,
2530 const struct GNUNET_PeerIdentity *peer,
2531 const struct GNUNET_MessageHeader *message,
2532 struct GNUNET_TIME_Relative latency,
2535 struct DirectNeighbor *referrer;
2536 struct DistantNeighbor *distant;
2537 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2539 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2541 return GNUNET_SYSERR; /* invalid message */
2544 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2546 if (referrer == NULL)
2549 distant = referrer->referee_head;
2550 while (distant != NULL)
2552 if (distant->referrer_id == ntohl(enc_message->peer_id))
2554 distant_neighbor_free(distant);
2556 distant = referrer->referee_head;
2564 * Core handler for dv gossip messages. These will be used
2565 * by us to create a HELLO message for the newly peer containing
2566 * which direct peer we can connect through, and what the cost
2567 * is. This HELLO will then be scheduled for validation by the
2568 * transport service so that it can be used by all others.
2570 * @param cls closure
2571 * @param peer peer which sent the message (immediate sender)
2572 * @param message the message
2573 * @param latency the latency of the connection we received the message from
2574 * @param distance the distance to the immediate peer
2576 static int handle_dv_gossip_message (void *cls,
2577 const struct GNUNET_PeerIdentity *peer,
2578 const struct GNUNET_MessageHeader *message,
2579 struct GNUNET_TIME_Relative latency,
2582 struct DirectNeighbor *referrer;
2583 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2585 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2587 return GNUNET_SYSERR; /* invalid message */
2590 #if DEBUG_DV_GOSSIP_RECEIPT
2591 char * encPeerAbout;
2594 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2595 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597 "%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);
2598 GNUNET_free(encPeerAbout);
2599 GNUNET_free(encPeerFrom);
2602 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2604 if (referrer == NULL)
2607 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2608 ntohl (enc_message->neighbor_id),
2609 referrer, ntohl (enc_message->cost) + 1);
2616 * Iterate over all currently known peers, add them to the
2617 * fast gossip list for this peer so we get DV routing information
2618 * out as fast as possible!
2620 * @param cls the direct neighbor we will gossip to
2621 * @param key the hashcode of the peer
2622 * @param value the distant neighbor we should add to the list
2624 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2626 static int add_all_extended_peers (void *cls,
2627 const GNUNET_HashCode * key,
2630 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2631 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2632 struct FastGossipNeighborList *gossip_entry;
2634 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2635 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2638 if (distant->hidden == GNUNET_YES)
2639 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2641 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2642 gossip_entry->about = distant;
2644 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2645 send_context->fast_gossip_list_tail,
2646 send_context->fast_gossip_list_tail,
2654 * Iterator over hash map entries.
2656 * @param cls closure
2657 * @param key current key code
2658 * @param value value in the hash map
2659 * @return GNUNET_YES if we should continue to
2663 static int gossip_all_to_all_iterator (void *cls,
2664 const GNUNET_HashCode * key,
2667 struct DirectNeighbor *direct = value;
2669 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2671 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2672 GNUNET_SCHEDULER_cancel(sched, direct->send_context->task);
2674 direct->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, direct->send_context);
2679 * Task run during shutdown.
2685 gossip_all_to_all (void *cls,
2686 const struct GNUNET_SCHEDULER_TaskContext *tc)
2688 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2690 GNUNET_SCHEDULER_add_delayed (sched,
2691 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2698 * Iterate over all current direct peers, add newly connected peer
2699 * to the fast gossip list for that peer so we get DV routing
2700 * information out as fast as possible!
2702 * @param cls the newly connected neighbor we will gossip about
2703 * @param key the hashcode of the peer
2704 * @param value the direct neighbor we should gossip to
2706 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2708 static int add_all_direct_neighbors (void *cls,
2709 const GNUNET_HashCode * key,
2712 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2713 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2714 struct DistantNeighbor *distant;
2715 struct NeighborSendContext *send_context = direct->send_context;
2716 struct FastGossipNeighborList *gossip_entry;
2720 distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2721 if (distant == NULL)
2726 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2728 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2732 if (distant->hidden == GNUNET_YES)
2733 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2735 direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2737 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);
2739 GNUNET_free(direct_id);
2740 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2741 gossip_entry->about = distant;
2743 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2744 send_context->fast_gossip_list_tail,
2745 send_context->fast_gossip_list_tail,
2747 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2748 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2750 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2751 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2752 //neighbor_send_task(send_context, &tc);
2757 * Type of an iterator over the hosts. Note that each
2758 * host will be called with each available protocol.
2760 * @param cls closure
2761 * @param peer id of the peer, NULL for last call
2762 * @param hello hello message for the peer (can be NULL)
2763 * @param trust amount of trust we have in the peer
2766 process_peerinfo (void *cls,
2767 const struct GNUNET_PeerIdentity *peer,
2768 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2770 struct PeerIteratorContext *peerinfo_iterator = cls;
2771 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2772 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2773 #if DEBUG_DV_PEER_NUMBERS
2780 if (distant->pkey == NULL)
2783 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2785 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2786 &peerinfo_iterator->neighbor->identity,
2788 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2794 GNUNET_free(peerinfo_iterator);
2799 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2802 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2804 if (distant->pkey == NULL)
2806 distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2807 memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2810 sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2812 #if DEBUG_DV_PEER_NUMBERS
2813 neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2816 sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2817 #if DEBUG_DV_PEER_NUMBERS
2818 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2819 GNUNET_free(neighbor_pid);
2821 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2827 * Method called whenever a peer connects.
2829 * @param cls closure
2830 * @param peer peer identity this notification is about
2831 * @param latency reported latency of the connection with peer
2832 * @param distance reported distance (DV) to peer
2834 void handle_core_connect (void *cls,
2835 const struct GNUNET_PeerIdentity * peer,
2836 struct GNUNET_TIME_Relative latency,
2839 struct DirectNeighbor *neighbor;
2840 struct DistantNeighbor *about;
2841 struct PeerIteratorContext *peerinfo_iterator;
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2848 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2850 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2851 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2852 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2853 neighbor->send_context->toNeighbor = neighbor;
2854 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2856 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2858 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2859 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2860 peerinfo_iterator->distant = about;
2861 peerinfo_iterator->neighbor = neighbor;
2862 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2865 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2869 if ((about != NULL) && (about->pkey == NULL))
2872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2875 else if (about != NULL)
2877 GNUNET_free(peerinfo_iterator);
2882 about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2883 if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2884 sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2887 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2894 * Method called whenever a given peer disconnects.
2896 * @param cls closure
2897 * @param peer peer identity this notification is about
2899 void handle_core_disconnect (void *cls,
2900 const struct GNUNET_PeerIdentity * peer)
2902 struct DirectNeighbor *neighbor;
2903 struct DistantNeighbor *referee;
2904 struct FindDestinationContext fdc;
2905 struct DisconnectContext disconnect_context;
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2909 "%s: Receives core peer disconnect message!\n", "dv");
2913 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2914 if (neighbor == NULL)
2918 while (NULL != (referee = neighbor->referee_head))
2919 distant_neighbor_free (referee);
2924 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
2926 if (fdc.dest != NULL)
2928 disconnect_context.direct = neighbor;
2929 disconnect_context.distant = fdc.dest;
2930 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2933 GNUNET_assert (neighbor->referee_tail == NULL);
2934 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
2935 &peer->hashPubKey, neighbor))
2939 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2940 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2941 GNUNET_free (neighbor);
2946 * Process dv requests.
2948 * @param cls closure
2949 * @param scheduler scheduler to use
2950 * @param server the initialized server
2951 * @param c configuration to use
2955 struct GNUNET_SCHEDULER_Handle *scheduler,
2956 struct GNUNET_SERVER_Handle *server,
2957 const struct GNUNET_CONFIGURATION_Handle *c)
2959 unsigned long long max_hosts;
2963 /* FIXME: Read from config, or calculate, or something other than this! */
2964 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2965 max_table_size = DEFAULT_DV_SIZE;
2966 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2968 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2969 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
2971 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2972 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
2975 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2976 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
2979 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2981 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2983 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2984 extended_neighbors =
2985 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
2987 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2989 GNUNET_CORE_connect (sched,
2991 GNUNET_TIME_relative_get_forever(),
2992 NULL, /* FIXME: anything we want to pass around? */
2994 &handle_core_connect,
2995 &handle_core_disconnect,
3002 if (coreAPI == NULL)
3005 coreMST = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE,
3006 &tokenized_message_handler,
3009 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
3011 if (peerinfo_handle == NULL)
3013 GNUNET_CORE_disconnect(coreAPI);
3017 /* Scheduled the task to clean up when shutdown is called */
3018 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
3019 GNUNET_TIME_UNIT_FOREVER_REL,
3023 GNUNET_SCHEDULER_add_delayed (sched,
3024 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
3032 * The main function for the dv service.
3034 * @param argc number of arguments from the command line
3035 * @param argv command line arguments
3036 * @return 0 ok, 1 on error
3039 main (int argc, char *const *argv)
3041 return (GNUNET_OK ==
3042 GNUNET_SERVICE_run (argc,
3045 GNUNET_SERVICE_OPTION_NONE,
3046 &run, NULL)) ? 0 : 1;