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 3, 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"
46 #include "gnunet_statistics_service.h"
50 * For testing mostly, remember only the
51 * shortest path to a distant neighbor.
53 #define AT_MOST_ONE GNUNET_NO
55 #define USE_PEER_ID GNUNET_YES
58 * How many outstanding messages (unknown sender) will we allow per peer?
60 #define MAX_OUTSTANDING_MESSAGES 5
63 * How often do we check about sending out more peer information (if
64 * we are connected to no peers previously).
66 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
69 * How long do we wait at most between sending out information?
71 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
74 * How long can we have not heard from a peer and
75 * still have it in our tables?
77 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
80 * Priority for gossip.
82 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
85 * How often should we check if expiration time has elapsed for
88 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
91 * How long to allow a message to be delayed?
93 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
96 * Priority to use for DV data messages.
101 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
103 #define DIRECT_NEIGHBOR_COST 1
106 * The default number of direct connections to store in DV (max)
108 #define DEFAULT_DIRECT_CONNECTIONS 50
111 * The default size of direct + extended peers in DV (max)
113 #define DEFAULT_DV_SIZE 100
116 * The default fisheye depth, from how many hops away will
119 #define DEFAULT_FISHEYE_DEPTH 4
122 * Linked list of messages to send to clients.
124 struct PendingMessage
127 * Pointer to next item in the list
129 struct PendingMessage *next;
132 * Pointer to previous item in the list
134 struct PendingMessage *prev;
137 * The PeerIdentity to send to
139 struct GNUNET_PeerIdentity recipient;
142 * The result of message sending.
144 struct GNUNET_DV_SendResultMessage *send_result;
147 * Message importance level.
149 unsigned int importance;
154 unsigned int msg_size;
157 * How long to wait before sending message.
159 struct GNUNET_TIME_Relative timeout;
162 * Actual message to be sent; // avoid allocation
164 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
168 struct FastGossipNeighborList
171 * Next element of DLL
173 struct FastGossipNeighborList *next;
176 * Prev element of DLL
178 struct FastGossipNeighborList *prev;
181 * The neighbor to gossip about
183 struct DistantNeighbor *about;
187 * Context created whenever a direct peer connects to us,
188 * used to gossip other peers to it.
190 struct NeighborSendContext
193 * The peer we will gossip to.
195 struct DirectNeighbor *toNeighbor;
198 * The task associated with this context.
200 GNUNET_SCHEDULER_TaskIdentifier task;
203 * Head of DLL of peers to gossip about
204 * as fast as possible to this peer, for initial
207 struct FastGossipNeighborList *fast_gossip_list_head;
210 * Tail of DLL of peers to gossip about
211 * as fast as possible to this peer, for initial
214 struct FastGossipNeighborList *fast_gossip_list_tail;
220 * Struct to hold information for updating existing neighbors
222 struct NeighborUpdateInfo
230 * The existing neighbor
232 struct DistantNeighbor *neighbor;
235 * The referrer of the possibly existing peer
237 struct DirectNeighbor *referrer;
240 * The time we heard about this peer
242 struct GNUNET_TIME_Absolute now;
245 * Peer id this peer uses to refer to neighbor.
247 unsigned int referrer_peer_id;
252 * Struct to store a single message received with
255 struct UnknownSenderMessage
258 * Message sender (immediate)
260 struct GNUNET_PeerIdentity sender;
263 * The actual message received
265 struct GNUNET_MessageHeader *message;
268 * Latency of connection
270 struct GNUNET_TIME_Relative latency;
273 * Distance to destination
284 * Struct where actual neighbor information is stored,
285 * referenced by min_heap and max_heap. Freeing dealt
286 * with when items removed from hashmap.
288 struct DirectNeighbor
291 * Identity of neighbor.
293 struct GNUNET_PeerIdentity identity;
296 * PublicKey of neighbor.
298 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
301 * Head of DLL of nodes that this direct neighbor referred to us.
303 struct DistantNeighbor *referee_head;
306 * Tail of DLL of nodes that this direct neighbor referred to us.
308 struct DistantNeighbor *referee_tail;
311 * The sending context for gossiping peers to this neighbor.
313 struct NeighborSendContext *send_context;
316 * Is this one of the direct neighbors that we are "hiding"
322 * Save messages immediately from this direct neighbor from a
323 * distan peer we don't know on the chance that it will be
324 * gossiped about and we can deliver the message.
326 struct UnknownSenderMessage pending_messages[MAX_OUTSTANDING_MESSAGES];
331 * Struct where actual neighbor information is stored,
332 * referenced by min_heap and max_heap. Freeing dealt
333 * with when items removed from hashmap.
335 struct DistantNeighbor
338 * We keep distant neighbor's of the same referrer in a DLL.
340 struct DistantNeighbor *next;
343 * We keep distant neighbor's of the same referrer in a DLL.
345 struct DistantNeighbor *prev;
350 struct GNUNET_CONTAINER_HeapNode *min_loc;
355 struct GNUNET_CONTAINER_HeapNode *max_loc;
358 * Identity of referrer (next hop towards 'neighbor').
360 struct DirectNeighbor *referrer;
363 * Identity of neighbor.
365 struct GNUNET_PeerIdentity identity;
368 * PublicKey of neighbor.
370 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
373 * Last time we received routing information from this peer
375 struct GNUNET_TIME_Absolute last_activity;
378 * Last time we sent routing information about this peer
380 struct GNUNET_TIME_Absolute last_gossip;
383 * Cost to neighbor, used for actual distance vector computations
388 * Random identifier *we* use for this peer, to be used as shortcut
389 * instead of sending full peer id for each message
394 * Random identifier the *referrer* uses for this peer.
396 unsigned int referrer_id;
399 * Is this one of the direct neighbors that we are "hiding"
406 struct PeerIteratorContext
409 * The actual context, to be freed later.
411 struct GNUNET_PEERINFO_IteratorContext *ic;
414 * The neighbor about which we are concerned.
416 struct DirectNeighbor *neighbor;
419 * The distant neighbor entry for this direct neighbor.
421 struct DistantNeighbor *distant;
426 * Context used for creating hello messages when
427 * gossips are received.
432 * Identity of distant neighbor.
434 struct GNUNET_PeerIdentity distant_peer;
437 * Identity of direct neighbor, via which we send this message.
439 const struct GNUNET_PeerIdentity *direct_peer;
442 * How many addresses do we need to add (always starts at 1, then set to 0)
444 int addresses_to_add;
448 struct DV_SendContext
451 * The distant peer (should always match)
453 struct GNUNET_PeerIdentity *distant_peer;
456 * The direct peer, we need to verify the referrer of.
458 struct GNUNET_PeerIdentity *direct_peer;
461 * The message to be sent
463 struct GNUNET_MessageHeader *message;
466 * The pre-built send result message. Simply needs to be queued
467 * and freed once send has been called!
469 struct GNUNET_DV_SendResultMessage *send_result;
472 * The size of the message being sent, may be larger
473 * than message->header.size because it's multiple
474 * messages packed into one!
479 * How important is this message?
481 unsigned int importance;
484 * Timeout for this message
486 struct GNUNET_TIME_Relative timeout;
489 * Unique ID for DV message
494 struct FindDestinationContext
497 struct DistantNeighbor *dest;
503 struct GNUNET_PeerIdentity *dest;
504 const struct GNUNET_PeerIdentity *via;
507 struct DisconnectContext
510 * Distant neighbor to get pid from.
512 struct DistantNeighbor *distant;
515 * Direct neighbor that disconnected.
517 struct DirectNeighbor *direct;
520 struct TokenizedMessageContext
523 * Immediate sender of this message
525 const struct GNUNET_PeerIdentity *peer;
528 * Distant sender of the message
530 struct DistantNeighbor *distant;
533 * Uid for this set of messages
539 * Context for finding the least cost peer to send to.
540 * Transport selection can only go so far.
542 struct FindLeastCostContext
544 struct DistantNeighbor *target;
545 unsigned int least_cost;
549 * Handle to the core service api.
551 static struct GNUNET_CORE_Handle *coreAPI;
554 * Stream tokenizer to handle messages coming in from core.
556 static struct GNUNET_SERVER_MessageStreamTokenizer *coreMST;
559 * The identity of our peer.
561 static struct GNUNET_PeerIdentity my_identity;
564 * The configuration for this service.
566 static const struct GNUNET_CONFIGURATION_Handle *cfg;
569 * The scheduler for this service.
571 static struct GNUNET_SCHEDULER_Handle *sched;
574 * The client, the DV plugin connected to us. Hopefully
575 * this client will never change, although if the plugin dies
576 * and returns for some reason it may happen.
578 static struct GNUNET_SERVER_Client * client_handle;
581 * Task to run when we shut down, cleaning up all our trash
583 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
585 static size_t default_dv_priority = 0;
587 static char *my_short_id;
590 * Transmit handle to the plugin.
592 static struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
595 * Head of DLL for client messages
597 static struct PendingMessage *plugin_pending_head;
600 * Tail of DLL for client messages
602 static struct PendingMessage *plugin_pending_tail;
605 * Handle to the peerinfo service
607 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
610 * Transmit handle to core service.
612 static struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
615 * Head of DLL for core messages
617 static struct PendingMessage *core_pending_head;
620 * Tail of DLL for core messages
622 static struct PendingMessage *core_pending_tail;
625 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
626 * directly connected peers.
628 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
631 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
632 * peers connected via DV (extended neighborhood). Does ALSO
633 * include any peers that are in 'direct_neighbors'; for those
634 * peers, the cost will be zero and the referrer all zeros.
636 static struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
639 * We use the min heap (min refers to cost) to prefer
640 * gossipping about peers with small costs.
642 static struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
645 * We use the max heap (max refers to cost) for general
646 * iterations over all peers and to remove the most costly
647 * connection if we have too many.
649 static struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
652 * Handle for the statistics service.
654 struct GNUNET_STATISTICS_Handle *stats;
657 * How far out to keep peers we learn about.
659 static unsigned long long fisheye_depth;
662 * How many peers to store at most.
664 static unsigned long long max_table_size;
667 * We've been given a target ID based on the random numbers that
668 * we assigned to our DV-neighborhood. Find the entry for the
669 * respective neighbor.
672 find_destination (void *cls,
673 struct GNUNET_CONTAINER_HeapNode *node,
674 void *element, GNUNET_CONTAINER_HeapCostType cost)
676 struct FindDestinationContext *fdc = cls;
677 struct DistantNeighbor *dn = element;
679 if (fdc->tid != dn->our_id)
687 * We've been given a target ID based on the random numbers that
688 * we assigned to our DV-neighborhood. Find the entry for the
689 * respective neighbor.
692 find_specific_id (void *cls,
693 const GNUNET_HashCode *key,
696 struct FindIDContext *fdc = cls;
697 struct DistantNeighbor *dn = value;
699 if (memcmp(&dn->referrer->identity, fdc->via, sizeof(struct GNUNET_PeerIdentity)) == 0)
701 fdc->tid = dn->referrer_id;
708 * Find a distant peer whose referrer_id matches what we're
709 * looking for. For looking up a peer we've gossipped about
710 * but is now disconnected. Need to do this because we don't
711 * want to remove those that may be accessible via a different
714 static int find_distant_peer (void *cls,
715 const GNUNET_HashCode * key,
718 struct FindDestinationContext *fdc = cls;
719 struct DistantNeighbor *distant = value;
721 if (fdc->tid == distant->referrer_id)
730 * Function called to notify a client about the socket
731 * begin ready to queue more data. "buf" will be
732 * NULL and "size" zero if the socket was closed for
733 * writing in the meantime.
736 * @param size number of bytes available in buf
737 * @param buf where the callee should write the message
738 * @return number of bytes written to buf
740 size_t transmit_to_plugin (void *cls,
741 size_t size, void *buf)
744 struct PendingMessage *reply;
750 /* client disconnected */
751 #if DEBUG_DV_MESSAGES
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: %s buffer was NULL (client disconnect?)\n", my_short_id, "transmit_to_plugin");
756 plugin_transmit_handle = NULL;
758 while ( (NULL != (reply = plugin_pending_head)) &&
759 (size >= off + (msize = ntohs (reply->msg->size))))
761 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
764 memcpy (&cbuf[off], reply->msg, msize);
769 if (plugin_pending_head != NULL)
770 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
771 ntohs(plugin_pending_head->msg->size),
772 GNUNET_TIME_UNIT_FOREVER_REL,
773 &transmit_to_plugin, NULL);
779 * Send a message to the dv plugin.
781 * @param sender the direct sender of the message
782 * @param message the message to send to the plugin
783 * (may be an encapsulated type)
784 * @param message_size the size of the message to be sent
785 * @param distant_neighbor the original sender of the message
786 * @param cost the cost to the original sender of the message
788 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
789 const struct GNUNET_MessageHeader *message,
791 struct GNUNET_PeerIdentity *distant_neighbor,
794 struct GNUNET_DV_MessageReceived *received_msg;
795 struct PendingMessage *pending_message;
796 char *sender_address;
797 size_t sender_address_len;
798 char *packed_msg_start;
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
805 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
807 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
808 sender_address = GNUNET_malloc(sender_address_len);
809 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
810 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
814 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
815 sender_address = GNUNET_malloc(sender_address_len);
816 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
819 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
820 received_msg = GNUNET_malloc(size);
821 received_msg->header.size = htons(size);
822 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
823 received_msg->distance = htonl(cost);
824 received_msg->msg_len = htonl(message_size);
825 /* Set the sender in this message to be the original sender! */
826 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
827 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
828 memcpy(&received_msg[1], sender_address, sender_address_len);
829 GNUNET_free(sender_address);
830 /* Copy the actual message after the sender */
831 packed_msg_start = (char *)&received_msg[1];
832 packed_msg_start = &packed_msg_start[sender_address_len];
833 memcpy(packed_msg_start, message, message_size);
834 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
835 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
836 memcpy(&pending_message[1], received_msg, size);
837 GNUNET_free(received_msg);
839 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
841 if (client_handle != NULL)
843 if (plugin_transmit_handle == NULL)
845 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
846 size, GNUNET_TIME_UNIT_FOREVER_REL,
847 &transmit_to_plugin, NULL);
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
856 /* Declare here so retry_core_send is aware of it */
857 size_t core_transmit_notify (void *cls,
858 size_t size, void *buf);
861 * Try to send another message from our core sending list
864 try_core_send (void *cls,
865 const struct GNUNET_SCHEDULER_TaskContext *tc)
867 struct PendingMessage *pending;
868 pending = core_pending_head;
870 if (core_transmit_handle != NULL)
871 return; /* Message send already in progress */
874 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, pending->importance, pending->timeout, &pending->recipient, pending->msg_size, &core_transmit_notify, NULL);
878 * Function called to notify a client about the socket
879 * being ready to queue more data. "buf" will be
880 * NULL and "size" zero if the socket was closed for
881 * writing in the meantime.
883 * @param cls closure (NULL)
884 * @param size number of bytes available in buf
885 * @param buf where the callee should write the message
886 * @return number of bytes written to buf
888 size_t core_transmit_notify (void *cls,
889 size_t size, void *buf)
892 struct PendingMessage *pending;
893 struct PendingMessage *client_reply;
899 /* client disconnected */
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
906 core_transmit_handle = NULL;
908 pending = core_pending_head;
909 if ( (pending != NULL) &&
910 (size >= (msize = ntohs (pending->msg->size))))
913 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
915 GNUNET_CONTAINER_DLL_remove (core_pending_head,
918 if (pending->send_result != NULL) /* Will only be non-null if a real client asked for this send */
920 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
921 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
922 memcpy(&client_reply[1], pending->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
923 GNUNET_free(pending->send_result);
925 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
926 if (client_handle != NULL)
928 if (plugin_transmit_handle == NULL)
930 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
931 sizeof(struct GNUNET_DV_SendResultMessage),
932 GNUNET_TIME_UNIT_FOREVER_REL,
933 &transmit_to_plugin, NULL);
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
941 memcpy (&cbuf[off], pending->msg, msize);
942 GNUNET_free (pending);
945 /*reply = core_pending_head;*/
947 GNUNET_SCHEDULER_add_now(sched, &try_core_send, NULL);
949 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);*/
956 * Send a DV data message via DV.
958 * @param sender the original sender of the message
959 * @param recipient the next hop recipient, may be our direct peer, maybe not
960 * @param send_context the send context
963 send_message_via (const struct GNUNET_PeerIdentity *sender,
964 const struct GNUNET_PeerIdentity *recipient,
965 struct DV_SendContext *send_context)
967 p2p_dv_MESSAGE_Data *toSend;
968 unsigned int msg_size;
969 unsigned int recipient_id;
970 unsigned int sender_id;
971 struct DistantNeighbor *source;
972 struct PendingMessage *pending_message;
973 struct FindIDContext find_context;
978 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
980 find_context.dest = send_context->distant_peer;
981 find_context.via = recipient;
982 find_context.tid = 0;
983 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
984 &find_specific_id, &find_context);
986 if (find_context.tid == 0)
988 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
989 /* target unknown to us, drop! */
990 return GNUNET_SYSERR;
992 recipient_id = find_context.tid;
994 if (0 == (memcmp (&my_identity,
995 sender, sizeof (struct GNUNET_PeerIdentity))))
998 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
999 &sender->hashPubKey);
1001 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));
1005 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1006 &sender->hashPubKey);
1009 /* sender unknown to us, drop! */
1010 return GNUNET_SYSERR;
1012 sender_id = source->our_id;
1015 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1016 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1017 pending_message->send_result = send_context->send_result;
1018 memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
1019 pending_message->msg_size = msg_size;
1020 pending_message->importance = send_context->importance;
1021 pending_message->timeout = send_context->timeout;
1022 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1023 toSend->header.size = htons (msg_size);
1024 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1025 toSend->sender = htonl (sender_id);
1026 toSend->recipient = htonl (recipient_id);
1027 #if DEBUG_DV_MESSAGES
1028 toSend->uid = send_context->uid; /* Still sent around in network byte order */
1030 toSend->uid = htonl(0);
1033 memcpy (&toSend[1], send_context->message, send_context->message_size);
1036 memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
1037 shortname[4] = '\0';
1038 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);
1041 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1046 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1052 * Given a FindLeastCostContext, and a set
1053 * of peers that match the target, return the cheapest.
1055 * @param cls closure, a struct FindLeastCostContext
1056 * @param key the key identifying the target peer
1057 * @param value the target peer
1059 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1062 find_least_cost_peer (void *cls,
1063 const GNUNET_HashCode *key,
1066 struct FindLeastCostContext *find_context = cls;
1067 struct DistantNeighbor *dn = value;
1069 if (dn->cost < find_context->least_cost)
1071 find_context->target = dn;
1073 if (dn->cost == DIRECT_NEIGHBOR_COST)
1079 * Send a DV data message via DV.
1081 * @param recipient the ultimate recipient of this message
1082 * @param sender the original sender of the message
1083 * @param specific_neighbor the specific neighbor to send this message via
1084 * @param message the packed message
1085 * @param message_size size of the message
1086 * @param importance what priority to send this message with
1087 * @param uid the unique identifier of this message (or 0 for none)
1088 * @param timeout how long to possibly delay sending this message
1091 send_message (const struct GNUNET_PeerIdentity * recipient,
1092 const struct GNUNET_PeerIdentity * sender,
1093 const struct DistantNeighbor * specific_neighbor,
1094 const struct GNUNET_MessageHeader * message,
1095 size_t message_size,
1096 unsigned int importance,
1098 struct GNUNET_TIME_Relative timeout)
1100 p2p_dv_MESSAGE_Data *toSend;
1101 unsigned int msg_size;
1103 unsigned int recipient_id;
1104 unsigned int sender_id;
1105 struct DistantNeighbor *target;
1106 struct DistantNeighbor *source;
1107 struct PendingMessage *pending_message;
1108 struct FindLeastCostContext find_least_ctx;
1109 #if DEBUG_DV_PEER_NUMBERS
1110 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1111 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1112 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1114 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1116 find_least_ctx.least_cost = -1;
1117 find_least_ctx.target = NULL;
1119 * Need to find the least cost peer, lest the transport selection keep
1120 * picking the same DV route for the same destination which results
1121 * in messages looping forever. Relatively cheap, we don't iterate
1122 * over all known peers, just those that apply.
1124 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1125 &recipient->hashPubKey, &find_least_cost_peer, &find_least_ctx);
1126 target = find_least_ctx.target;
1130 /* target unknown to us, drop! */
1131 return GNUNET_SYSERR;
1133 recipient_id = target->referrer_id;
1135 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1136 &sender->hashPubKey);
1139 if (0 != (memcmp (&my_identity,
1140 sender, sizeof (struct GNUNET_PeerIdentity))))
1142 /* sender unknown to us, drop! */
1143 return GNUNET_SYSERR;
1145 sender_id = 0; /* 0 == us */
1149 /* find out the number that we use when we gossip about
1151 sender_id = source->our_id;
1154 #if DEBUG_DV_PEER_NUMBERS
1155 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1156 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1157 encPeerFrom.encoding[4] = '\0';
1158 encPeerVia.encoding[4] = '\0';
1160 if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1165 cost = target->cost;
1166 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1167 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1168 pending_message->send_result = NULL;
1169 pending_message->importance = importance;
1170 pending_message->timeout = timeout;
1171 memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1172 pending_message->msg_size = msg_size;
1173 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1174 toSend->header.size = htons (msg_size);
1175 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1176 toSend->sender = htonl (sender_id);
1177 toSend->recipient = htonl (recipient_id);
1178 #if DEBUG_DV_MESSAGES
1179 toSend->uid = htonl(uid);
1181 toSend->uid = htonl(0);
1184 #if DEBUG_DV_PEER_NUMBERS
1185 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1186 encPeerTo.encoding[4] = '\0';
1187 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);
1189 memcpy (&toSend[1], message, message_size);
1190 if ((source != NULL) && (source->pkey == NULL)) /* Test our hypothesis about message failures! */
1192 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: Sending message, but anticipate recipient will not know sender!!!\n\n\n", my_short_id);
1194 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1199 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));
1202 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1207 struct CheckPeerContext
1212 struct DistantNeighbor *peer;
1215 * Sender id to search for
1217 unsigned int sender_id;
1221 * Iterator over hash map entries.
1223 * @param cls closure
1224 * @param key current key code
1225 * @param value value in the hash map
1226 * @return GNUNET_YES if we should continue to
1230 int checkPeerID (void *cls,
1231 const GNUNET_HashCode * key,
1234 struct CheckPeerContext *ctx = cls;
1235 struct DistantNeighbor *distant = value;
1237 if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1239 ctx->peer = distant;
1249 * Handler for messages parsed out by the tokenizer from
1250 * DV DATA received for this peer.
1253 * @param client the TokenizedMessageContext which contains message information
1254 * @param message the actual message
1256 void tokenized_message_handler (void *cls,
1258 const struct GNUNET_MessageHeader *message)
1260 struct TokenizedMessageContext *ctx = client;
1261 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1262 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1263 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1264 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1266 #if DEBUG_DV_MESSAGES
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "%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));
1270 GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1271 send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1276 struct DelayedMessageContext
1278 struct GNUNET_PeerIdentity dest;
1279 struct GNUNET_PeerIdentity sender;
1280 struct GNUNET_MessageHeader *message;
1281 size_t message_size;
1285 void send_message_delayed (void *cls,
1286 const struct GNUNET_SCHEDULER_TaskContext *tc)
1288 struct DelayedMessageContext *msg_ctx = cls;
1289 if (msg_ctx != NULL)
1291 send_message(&msg_ctx->dest,
1295 msg_ctx->message_size,
1296 default_dv_priority,
1298 GNUNET_TIME_relative_get_forever());
1299 GNUNET_free(msg_ctx->message);
1300 GNUNET_free(msg_ctx);
1307 * Core handler for dv data messages. Whatever this message
1308 * contains all we really have to do is rip it out of its
1309 * DV layering and give it to our pal the DV plugin to report
1312 * @param cls closure
1313 * @param peer peer which sent the message (immediate sender)
1314 * @param message the message
1315 * @param latency the latency of the connection we received the message from
1316 * @param distance the distance to the immediate peer
1318 static int handle_dv_data_message (void *cls,
1319 const struct GNUNET_PeerIdentity * peer,
1320 const struct GNUNET_MessageHeader * message,
1321 struct GNUNET_TIME_Relative latency,
1324 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1325 const struct GNUNET_MessageHeader *packed_message;
1326 struct DirectNeighbor *dn;
1327 struct DistantNeighbor *pos;
1328 unsigned int sid; /* Sender id */
1329 unsigned int tid; /* Target id */
1330 struct GNUNET_PeerIdentity *original_sender;
1331 struct GNUNET_PeerIdentity *destination;
1332 struct FindDestinationContext fdc;
1333 struct TokenizedMessageContext tkm_ctx;
1337 struct DelayedMessageContext *delayed_context;
1340 struct CheckPeerContext checkPeerCtx;
1342 #if DEBUG_DV_MESSAGES
1346 size_t packed_message_size;
1349 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "%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);
1355 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "`%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));
1362 return GNUNET_SYSERR;
1365 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1370 sid = ntohl (incoming->sender);
1374 checkPeerCtx.sender_id = sid;
1375 checkPeerCtx.peer = NULL;
1376 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1377 pos = checkPeerCtx.peer;
1381 pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1385 pos = dn->referee_head;
1386 while ((NULL != pos) && (pos->referrer_id != sid))
1392 #if DEBUG_DV_MESSAGES
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "%s: unknown sender (%u), Message uid %u from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1395 pos = dn->referee_head;
1396 while ((NULL != pos) && (pos->referrer_id != sid))
1398 sender_id = strdup(GNUNET_i2s(&pos->identity));
1399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1400 GNUNET_free(sender_id);
1404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1405 "%s: unknown sender (%u), Message uid %u from %s!\n", my_short_id, ntohl(incoming->sender), ntohl(incoming->uid), GNUNET_i2s(&dn->identity));
1408 for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
1410 if (dn->pending_messages[i].sender_id == 0)
1417 if (found_pos == -1)
1419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1420 "%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));
1424 dn->pending_messages[found_pos].message = GNUNET_malloc(ntohs (message->size));
1425 memcpy(dn->pending_messages[found_pos].message, message, ntohs(message->size));
1426 dn->pending_messages[found_pos].distance = distance;
1427 dn->pending_messages[found_pos].latency = latency;
1428 memcpy(&dn->pending_messages[found_pos].sender, peer, sizeof(struct GNUNET_PeerIdentity));
1429 dn->pending_messages[found_pos].sender_id = sid;
1431 /* unknown sender */
1434 original_sender = &pos->identity;
1435 tid = ntohl (incoming->recipient);
1439 cbuf = (char *)&incoming[1];
1441 tkm_ctx.peer = peer;
1442 tkm_ctx.distant = pos;
1443 tkm_ctx.uid = ntohl(incoming->uid);
1444 if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1447 packed_message_size,
1452 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1458 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1461 /* FIXME: this is the *only* per-request operation we have in DV
1462 that is O(n) in relation to the number of connected peers; a
1463 hash-table lookup could easily solve this (minor performance
1467 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1468 &find_destination, &fdc);
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1475 if (fdc.dest == NULL)
1477 #if DEBUG_DV_MESSAGES
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1483 destination = &fdc.dest->identity;
1485 if (0 == memcmp (destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1487 /* FIXME: create stat: routing loop-discard! */
1488 #if DEBUG_DV_PEER_NUMBERS
1489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1492 #if DEBUG_DV_MESSAGES
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "%s: DROPPING MESSAGE uid %u type %d, routing loop! Message immediately from %s!\n", my_short_id, ntohl(incoming->uid), ntohs(packed_message->type), GNUNET_i2s(&dn->identity));
1499 /* At this point we have a message, and we need to forward it on to the
1502 #if DEBUG_DV_MESSAGES
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "%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);
1508 if (GNUNET_TIME_absolute_get_duration(pos->last_gossip).value < GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2).value)
1510 delayed_context = GNUNET_malloc(sizeof(struct DelayedMessageContext));
1511 memcpy(&delayed_context->dest, destination, sizeof(struct GNUNET_PeerIdentity));
1512 memcpy(&delayed_context->sender, original_sender, sizeof(struct GNUNET_PeerIdentity));
1513 delayed_context->message = GNUNET_malloc(packed_message_size);
1514 memcpy(delayed_context->message, packed_message, packed_message_size);
1515 delayed_context->message_size = packed_message_size;
1516 delayed_context->uid = ntohl(incoming->uid);
1517 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 2500), &send_message_delayed, delayed_context);
1523 ret = send_message(destination,
1527 packed_message_size,
1528 default_dv_priority,
1529 ntohl(incoming->uid),
1530 GNUNET_TIME_relative_get_forever());
1532 if (ret != GNUNET_SYSERR)
1536 #if DEBUG_MESSAGE_DROP
1537 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "%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);
1541 return GNUNET_SYSERR;
1547 * Iterator over hash map entries.
1549 * @param cls closure (NULL)
1550 * @param key current key code
1551 * @param value value in the hash map (DistantNeighbor)
1552 * @return GNUNET_YES if we should continue to
1556 int print_neighbors (void *cls,
1557 const GNUNET_HashCode * key,
1560 struct DistantNeighbor *distant_neighbor = value;
1561 char my_shortname[5];
1562 char referrer_shortname[5];
1563 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1564 my_shortname[4] = '\0';
1565 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1566 referrer_shortname[4] = '\0';
1568 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");
1574 * Scheduled task which gossips about known direct peers to other connected
1575 * peers. Will run until called with reason shutdown.
1578 neighbor_send_task (void *cls,
1579 const struct GNUNET_SCHEDULER_TaskContext *tc)
1581 struct NeighborSendContext *send_context = cls;
1582 #if DEBUG_DV_GOSSIP_SEND
1583 char * encPeerAbout;
1586 struct DistantNeighbor *about;
1587 struct DirectNeighbor *to;
1588 struct FastGossipNeighborList *about_list;
1590 p2p_dv_MESSAGE_NeighborInfo *message;
1591 struct PendingMessage *pending_message;
1593 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 "%s: Called with reason shutdown, shutting down!\n",
1598 GNUNET_i2s(&my_identity));
1603 if (send_context->fast_gossip_list_head != NULL)
1605 about_list = send_context->fast_gossip_list_head;
1606 about = about_list->about;
1607 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1608 send_context->fast_gossip_list_tail,
1610 GNUNET_free(about_list);
1614 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1615 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1616 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1617 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1618 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1619 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1620 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1621 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1622 * asynchronously enough to avoid major problems (-;
1624 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1626 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1628 to = send_context->toNeighbor;
1630 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1632 (about->hidden == GNUNET_NO) &&
1635 (0 != memcmp (&about->identity,
1636 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1637 (about->pkey != NULL))
1639 #if DEBUG_DV_GOSSIP_SEND
1640 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1641 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1644 GNUNET_i2s(&my_identity),
1645 encPeerAbout, about->our_id, encPeerTo);
1646 GNUNET_free(encPeerAbout);
1647 GNUNET_free(encPeerTo);
1649 about->last_gossip = GNUNET_TIME_absolute_get();
1650 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1651 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1652 pending_message->importance = default_dv_priority;
1653 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1654 memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1655 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1656 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1657 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1658 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1659 message->cost = htonl (about->cost);
1660 message->neighbor_id = htonl (about->our_id);
1662 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1663 memcpy (&message->neighbor,
1664 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1666 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1671 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
1672 /*if (core_transmit_handle == NULL)
1673 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);*/
1677 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1679 #if DEBUG_DV_PEER_NUMBERS
1680 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1682 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1686 #if DEBUG_DV_PEER_NUMBERS
1687 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1689 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1697 * Handle START-message. This is the first message sent to us
1698 * by the client (can only be one!).
1700 * @param cls closure (always NULL)
1701 * @param client identification of the client
1702 * @param message the actual message
1705 handle_start (void *cls,
1706 struct GNUNET_SERVER_Client *client,
1707 const struct GNUNET_MessageHeader *message)
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Received `%s' request from client\n", "START");
1715 client_handle = client;
1717 GNUNET_SERVER_client_keep(client_handle);
1718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1723 * Iterate over hash map entries for a distant neighbor,
1724 * if direct neighbor matches context call send message
1726 * @param cls closure, a DV_SendContext
1727 * @param key current key code
1728 * @param value value in the hash map
1729 * @return GNUNET_YES if we should continue to
1733 int send_iterator (void *cls,
1734 const GNUNET_HashCode * key,
1737 struct DV_SendContext *send_context = cls;
1738 struct DistantNeighbor *distant_neighbor = value;
1740 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1742 send_message_via(&my_identity, distant_neighbor, send_context);
1750 * Service server's handler for message send requests (which come
1751 * bubbling up to us through the DV plugin).
1753 * @param cls closure
1754 * @param client identification of the client
1755 * @param message the actual message
1757 void handle_dv_send_message (void *cls,
1758 struct GNUNET_SERVER_Client * client,
1759 const struct GNUNET_MessageHeader * message)
1761 struct GNUNET_DV_SendMessage *send_msg;
1762 struct GNUNET_DV_SendResultMessage *send_result_msg;
1763 struct PendingMessage *pending_message;
1765 size_t message_size;
1766 struct GNUNET_PeerIdentity *destination;
1767 struct GNUNET_PeerIdentity *direct;
1768 struct GNUNET_MessageHeader *message_buf;
1771 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1772 struct DV_SendContext *send_context;
1773 #if DEBUG_DV_MESSAGES
1775 struct GNUNET_MessageHeader *packed_message;
1778 if (client_handle == NULL)
1780 client_handle = client;
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1784 else if (client_handle != client)
1786 client_handle = client;
1787 /* What should we do in this case, assert fail or just log the warning? */
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "%s: Setting client handle (was a different client!)!\n", "dv");
1794 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1795 send_msg = (struct GNUNET_DV_SendMessage *)message;
1797 address_len = ntohl(send_msg->addrlen);
1798 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1799 message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1800 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1801 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1802 message_buf = GNUNET_malloc(message_size);
1804 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1805 offset = 0; /* Offset starts at zero */
1807 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1808 offset += sizeof(struct GNUNET_PeerIdentity);
1810 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1811 offset += sizeof(struct GNUNET_PeerIdentity);
1814 memcpy(message_buf, &temp_pos[offset], message_size);
1815 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1817 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1818 dest_hash.encoding[4] = '\0';
1819 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);
1822 #if DEBUG_DV_MESSAGES
1823 cbuf = (char *)message_buf;
1825 while(offset < message_size)
1827 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1828 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));
1829 offset += ntohs(packed_message->size);
1831 /*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));*/
1833 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1834 dest_hash.encoding[4] = '\0';
1835 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1837 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1838 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1839 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1840 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1842 send_context->importance = ntohl(send_msg->priority);
1843 send_context->timeout = send_msg->timeout;
1844 send_context->direct_peer = direct;
1845 send_context->distant_peer = destination;
1846 send_context->message = message_buf;
1847 send_context->message_size = message_size;
1848 send_context->send_result = send_result_msg;
1849 #if DEBUG_DV_MESSAGES
1850 send_context->uid = send_msg->uid;
1853 if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1855 send_result_msg->result = htons(1);
1856 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1857 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1858 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1859 GNUNET_free(send_result_msg);
1861 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1863 if (client_handle != NULL)
1865 if (plugin_transmit_handle == NULL)
1867 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1868 sizeof(struct GNUNET_DV_SendResultMessage),
1869 GNUNET_TIME_UNIT_FOREVER_REL,
1870 &transmit_to_plugin, NULL);
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1877 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1878 dest_hash.encoding[4] = '\0';
1879 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));
1882 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1884 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1886 send_result_msg->result = htons(1);
1887 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1888 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1889 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1890 GNUNET_free(send_result_msg);
1892 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1894 if (client_handle != NULL)
1896 if (plugin_transmit_handle == NULL)
1898 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1899 sizeof(struct GNUNET_DV_SendResultMessage),
1900 GNUNET_TIME_UNIT_FOREVER_REL,
1901 &transmit_to_plugin, NULL);
1905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1908 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1909 dest_hash.encoding[4] = '\0';
1910 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));
1913 GNUNET_free(message_buf);
1914 GNUNET_free(send_context);
1915 GNUNET_free(direct);
1916 GNUNET_free(destination);
1918 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1921 /** Forward declarations **/
1922 static int handle_dv_gossip_message (void *cls,
1923 const struct GNUNET_PeerIdentity *peer,
1924 const struct GNUNET_MessageHeader *message,
1925 struct GNUNET_TIME_Relative latency,
1928 static int handle_dv_disconnect_message (void *cls,
1929 const struct GNUNET_PeerIdentity *peer,
1930 const struct GNUNET_MessageHeader *message,
1931 struct GNUNET_TIME_Relative latency,
1933 /** End forward declarations **/
1937 * List of handlers for the messages understood by this
1940 * Hmm... will we need to register some handlers with core and
1941 * some handlers with our server here? Because core should be
1942 * getting the incoming DV messages (from whichever lower level
1943 * transport) and then our server should be getting messages
1944 * from the dv_plugin, right?
1946 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1947 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1948 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1949 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1953 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1954 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1955 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1960 * Free a DistantNeighbor node, including removing it
1961 * from the referer's list.
1964 distant_neighbor_free (struct DistantNeighbor *referee)
1966 struct DirectNeighbor *referrer;
1968 referrer = referee->referrer;
1969 if (referrer != NULL)
1971 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1972 referrer->referee_tail, referee);
1974 GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
1975 GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
1976 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
1977 &referee->identity.hashPubKey);
1978 GNUNET_free_non_null (referee->pkey);
1979 GNUNET_free (referee);
1983 * Free a DirectNeighbor node, including removing it
1984 * from the referer's list.
1987 direct_neighbor_free (struct DirectNeighbor *direct)
1989 struct NeighborSendContext *send_context;
1990 struct FastGossipNeighborList *about_list;
1991 struct FastGossipNeighborList *prev_about;
1993 send_context = direct->send_context;
1995 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1996 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1998 about_list = send_context->fast_gossip_list_head;
1999 while (about_list != NULL)
2001 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
2002 prev_about = about_list;
2003 about_list = about_list->next;
2004 GNUNET_free(prev_about);
2006 GNUNET_free(send_context);
2007 GNUNET_free(direct);
2011 * Multihashmap iterator for sending out disconnect messages
2014 * @param cls the peer that was disconnected
2015 * @param key key value stored under
2016 * @param value the direct neighbor to send disconnect to
2018 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2020 static int schedule_disconnect_messages (void *cls,
2021 const GNUNET_HashCode * key,
2024 struct DisconnectContext *disconnect_context = cls;
2025 struct DirectNeighbor *disconnected = disconnect_context->direct;
2026 struct DirectNeighbor *notify = value;
2027 struct PendingMessage *pending_message;
2028 p2p_dv_MESSAGE_Disconnect *disconnect_message;
2030 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2031 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
2033 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
2034 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
2035 pending_message->importance = default_dv_priority;
2036 pending_message->timeout = GNUNET_TIME_relative_get_forever();
2037 memcpy(&pending_message->recipient, ¬ify->identity, sizeof(struct GNUNET_PeerIdentity));
2038 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
2039 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
2040 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
2041 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
2042 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
2044 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
2049 GNUNET_SCHEDULER_add_now(sched, try_core_send, NULL);
2050 /*if (core_transmit_handle == NULL)
2051 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);*/
2057 * Multihashmap iterator for freeing extended neighbors.
2060 * @param key key value stored under
2061 * @param value the distant neighbor to be freed
2063 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2065 static int free_extended_neighbors (void *cls,
2066 const GNUNET_HashCode * key,
2069 struct DistantNeighbor *distant = value;
2070 distant_neighbor_free(distant);
2075 * Multihashmap iterator for freeing direct neighbors.
2078 * @param key key value stored under
2079 * @param value the direct neighbor to be freed
2081 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
2083 static int free_direct_neighbors (void *cls,
2084 const GNUNET_HashCode * key,
2087 struct DirectNeighbor *direct = value;
2088 direct_neighbor_free(direct);
2094 * Task run during shutdown.
2100 shutdown_task (void *cls,
2101 const struct GNUNET_SCHEDULER_TaskContext *tc)
2104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2105 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2107 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2108 GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2109 GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2110 GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2112 GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2113 GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2115 GNUNET_CORE_disconnect (coreAPI);
2116 GNUNET_PEERINFO_disconnect(peerinfo_handle);
2117 GNUNET_SERVER_mst_destroy(coreMST);
2118 GNUNET_free_non_null(my_short_id);
2120 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2125 * To be called on core init/fail.
2127 void core_init (void *cls,
2128 struct GNUNET_CORE_Handle * server,
2129 const struct GNUNET_PeerIdentity *identity,
2130 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2135 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
2136 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2141 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2143 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2144 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2149 #if PKEY_NO_NEIGHBOR_ON_ADD
2151 * Iterator over hash map entries.
2153 * @param cls closure
2154 * @param key current key code
2155 * @param value value in the hash map
2156 * @return GNUNET_YES if we should continue to
2160 static int add_pkey_to_extended (void *cls,
2161 const GNUNET_HashCode * key,
2164 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2165 struct DistantNeighbor *distant_neighbor = value;
2167 if (distant_neighbor->pkey == NULL)
2169 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2170 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2178 * Iterator over hash map entries.
2180 * @param cls closure
2181 * @param key current key code
2182 * @param value value in the hash map
2183 * @return GNUNET_YES if we should continue to
2187 static int update_matching_neighbors (void *cls,
2188 const GNUNET_HashCode * key,
2191 struct NeighborUpdateInfo * update_info = cls;
2192 struct DistantNeighbor *distant_neighbor = value;
2194 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2196 /* same referrer, cost change! */
2197 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2198 update_info->neighbor->max_loc, update_info->cost);
2199 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2200 update_info->neighbor->min_loc, update_info->cost);
2201 update_info->neighbor->last_activity = update_info->now;
2202 update_info->neighbor->cost = update_info->cost;
2203 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2212 * Iterate over all current direct peers, add DISTANT newly connected
2213 * peer to the fast gossip list for that peer so we get DV routing
2214 * information out as fast as possible!
2216 * @param cls the newly connected neighbor we will gossip about
2217 * @param key the hashcode of the peer
2218 * @param value the direct neighbor we should gossip to
2220 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2222 static int add_distant_all_direct_neighbors (void *cls,
2223 const GNUNET_HashCode * key,
2226 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2227 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2228 struct NeighborSendContext *send_context = direct->send_context;
2229 struct FastGossipNeighborList *gossip_entry;
2235 if (distant == NULL)
2240 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2242 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2246 if (distant->hidden == GNUNET_YES)
2247 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2249 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2250 gossip_entry->about = distant;
2252 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2253 send_context->fast_gossip_list_tail,
2254 send_context->fast_gossip_list_tail,
2257 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2258 encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2261 GNUNET_i2s(&my_identity),
2262 encPeerAbout, distant->our_id, encPeerTo);
2263 GNUNET_free(encPeerAbout);
2264 GNUNET_free(encPeerTo);
2266 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2267 GNUNET_SCHEDULER_cancel(sched, send_context->task);*/
2269 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2274 * Callback for hello address creation.
2276 * @param cls closure, a struct HelloContext
2277 * @param max maximum number of bytes that can be written to buf
2278 * @param buf where to write the address information
2280 * @return number of bytes written, 0 to signal the
2281 * end of the iteration.
2284 generate_hello_address (void *cls, size_t max, void *buf)
2286 struct HelloContext *hello_context = cls;
2292 if (hello_context->addresses_to_add == 0)
2295 /* Hello "address" will be concatenation of distant peer and direct peer identities */
2296 size = 2 * sizeof(struct GNUNET_PeerIdentity);
2297 GNUNET_assert(max >= size);
2299 addr_buffer = GNUNET_malloc(size);
2301 /* Copy the distant peer identity to buffer */
2302 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
2303 offset += sizeof(struct GNUNET_PeerIdentity);
2304 /* Copy the direct peer identity to buffer */
2305 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
2306 ret = GNUNET_HELLO_add_address ("dv",
2307 GNUNET_TIME_relative_to_absolute
2308 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2311 hello_context->addresses_to_add--;
2313 GNUNET_free(addr_buffer);
2319 * Handles when a peer is either added due to being newly connected
2320 * or having been gossiped about, also called when the cost for a neighbor
2321 * needs to be updated.
2323 * @param peer identity of the peer whose info is being added/updated
2324 * @param pkey public key of the peer whose info is being added/updated
2325 * @param referrer_peer_id id to use when sending to 'peer'
2326 * @param referrer if this is a gossiped peer, who did we hear it from?
2327 * @param cost the cost of communicating with this peer via 'referrer'
2329 * @return the added neighbor, the updated neighbor or NULL (neighbor
2332 static struct DistantNeighbor *
2333 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2334 unsigned int referrer_peer_id,
2335 struct DirectNeighbor *referrer, unsigned int cost)
2337 struct DistantNeighbor *neighbor;
2338 struct DistantNeighbor *max;
2339 struct GNUNET_TIME_Absolute now;
2340 struct NeighborUpdateInfo *neighbor_update;
2341 struct HelloContext *hello_context;
2342 struct GNUNET_HELLO_Message *hello_msg;
2343 unsigned int our_id;
2348 #if DEBUG_DV_PEER_NUMBERS
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2354 now = GNUNET_TIME_absolute_get ();
2355 neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2357 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2358 neighbor_update->neighbor = neighbor;
2359 neighbor_update->cost = cost;
2360 neighbor_update->now = now;
2361 neighbor_update->referrer = referrer;
2362 neighbor_update->referrer_peer_id = referrer_peer_id;
2364 if (neighbor != NULL)
2367 memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2369 our_id = neighbor->our_id;
2375 memcpy(&our_id, peer, sizeof(unsigned int));
2377 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2381 /* Either we do not know this peer, or we already do but via a different immediate peer */
2382 if ((neighbor == NULL) ||
2383 (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2385 &update_matching_neighbors,
2386 neighbor_update) != GNUNET_SYSERR))
2390 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2392 distant_neighbor_free(neighbor);
2394 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2400 if (cost > fisheye_depth)
2403 GNUNET_free(neighbor_update);
2407 #if DEBUG_DV_PEER_NUMBERS
2408 encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2411 GNUNET_free(encAbout);
2414 if (max_table_size <=
2415 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2417 /* remove most expensive entry */
2418 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2419 GNUNET_assert(max != NULL);
2420 if (cost > max->cost)
2422 /* new entry most expensive, don't create */
2423 GNUNET_free(neighbor_update);
2428 /* only free if this is not a direct connection;
2429 we could theoretically have more direct
2430 connections than DV entries allowed total! */
2431 distant_neighbor_free (max);
2435 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2436 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2437 referrer->referee_tail, neighbor);
2438 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2440 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2442 neighbor->referrer = referrer;
2443 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2444 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2446 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2447 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2450 neighbor->pkey = pkey;
2452 neighbor->last_activity = now;
2453 neighbor->cost = cost;
2454 neighbor->referrer_id = referrer_peer_id;
2455 neighbor->our_id = our_id;
2457 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2460 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2463 if (referrer_peer_id != 0)
2465 for (i = 0; i< MAX_OUTSTANDING_MESSAGES; i++)
2467 if (referrer->pending_messages[i].sender_id == referrer_peer_id) /* We have a queued message from just learned about peer! */
2469 #if DEBUG_DV_MESSAGES
2470 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);
2472 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);
2473 GNUNET_free(referrer->pending_messages[i].message);
2474 referrer->pending_messages[i].sender_id = 0;
2478 if ((cost != DIRECT_NEIGHBOR_COST) && (neighbor->pkey != NULL))
2480 /* Added neighbor, now send HELLO to transport */
2481 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2482 hello_context->direct_peer = &referrer->identity;
2483 memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2484 hello_context->addresses_to_add = 1;
2485 hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2486 GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2487 addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2488 addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2490 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);
2494 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2495 GNUNET_free(hello_context);
2496 GNUNET_free(hello_msg);
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2512 GNUNET_free(neighbor_update);
2518 * Core handler for dv disconnect messages. These will be used
2519 * by us to tell transport via the dv plugin that a peer can
2520 * no longer be contacted by us via a certain address. We should
2521 * then propagate these messages on, given that the distance to
2522 * the peer indicates we would have gossiped about it to others.
2524 * @param cls closure
2525 * @param peer peer which sent the message (immediate sender)
2526 * @param message the message
2527 * @param latency the latency of the connection we received the message from
2528 * @param distance the distance to the immediate peer
2530 static int handle_dv_disconnect_message (void *cls,
2531 const struct GNUNET_PeerIdentity *peer,
2532 const struct GNUNET_MessageHeader *message,
2533 struct GNUNET_TIME_Relative latency,
2536 struct DirectNeighbor *referrer;
2537 struct DistantNeighbor *distant;
2538 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2540 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2542 return GNUNET_SYSERR; /* invalid message */
2545 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2547 if (referrer == NULL)
2550 distant = referrer->referee_head;
2551 while (distant != NULL)
2553 if (distant->referrer_id == ntohl(enc_message->peer_id))
2555 distant_neighbor_free(distant);
2556 distant = referrer->referee_head;
2559 distant = distant->next;
2567 * Core handler for dv gossip messages. These will be used
2568 * by us to create a HELLO message for the newly peer containing
2569 * which direct peer we can connect through, and what the cost
2570 * is. This HELLO will then be scheduled for validation by the
2571 * transport service so that it can be used by all others.
2573 * @param cls closure
2574 * @param peer peer which sent the message (immediate sender)
2575 * @param message the message
2576 * @param latency the latency of the connection we received the message from
2577 * @param distance the distance to the immediate peer
2579 static int handle_dv_gossip_message (void *cls,
2580 const struct GNUNET_PeerIdentity *peer,
2581 const struct GNUNET_MessageHeader *message,
2582 struct GNUNET_TIME_Relative latency,
2585 struct DirectNeighbor *referrer;
2586 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2588 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2590 return GNUNET_SYSERR; /* invalid message */
2593 #if DEBUG_DV_GOSSIP_RECEIPT
2594 char * encPeerAbout;
2597 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2598 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2600 "%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);
2601 GNUNET_free(encPeerAbout);
2602 GNUNET_free(encPeerFrom);
2605 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2607 if (referrer == NULL)
2610 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2611 ntohl (enc_message->neighbor_id),
2612 referrer, ntohl (enc_message->cost) + 1);
2619 * Iterate over all currently known peers, add them to the
2620 * fast gossip list for this peer so we get DV routing information
2621 * out as fast as possible!
2623 * @param cls the direct neighbor we will gossip to
2624 * @param key the hashcode of the peer
2625 * @param value the distant neighbor we should add to the list
2627 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2629 static int add_all_extended_peers (void *cls,
2630 const GNUNET_HashCode * key,
2633 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2634 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2635 struct FastGossipNeighborList *gossip_entry;
2637 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2638 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2641 if (distant->hidden == GNUNET_YES)
2642 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2644 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2645 gossip_entry->about = distant;
2647 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2648 send_context->fast_gossip_list_tail,
2649 send_context->fast_gossip_list_tail,
2657 * Iterator over hash map entries.
2659 * @param cls closure
2660 * @param key current key code
2661 * @param value value in the hash map
2662 * @return GNUNET_YES if we should continue to
2666 static int gossip_all_to_all_iterator (void *cls,
2667 const GNUNET_HashCode * key,
2670 struct DirectNeighbor *direct = value;
2672 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2674 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2675 GNUNET_SCHEDULER_cancel(sched, direct->send_context->task);
2677 direct->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, direct->send_context);
2682 * Task run during shutdown.
2688 gossip_all_to_all (void *cls,
2689 const struct GNUNET_SCHEDULER_TaskContext *tc)
2691 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2693 GNUNET_SCHEDULER_add_delayed (sched,
2694 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2701 * Iterate over all current direct peers, add newly connected peer
2702 * to the fast gossip list for that peer so we get DV routing
2703 * information out as fast as possible!
2705 * @param cls the newly connected neighbor we will gossip about
2706 * @param key the hashcode of the peer
2707 * @param value the direct neighbor we should gossip to
2709 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2711 static int add_all_direct_neighbors (void *cls,
2712 const GNUNET_HashCode * key,
2715 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2716 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2717 struct DistantNeighbor *distant;
2718 struct NeighborSendContext *send_context = direct->send_context;
2719 struct FastGossipNeighborList *gossip_entry;
2723 distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2724 if (distant == NULL)
2729 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2731 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2735 if (distant->hidden == GNUNET_YES)
2736 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2738 direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2740 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);
2742 GNUNET_free(direct_id);
2743 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2744 gossip_entry->about = distant;
2746 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2747 send_context->fast_gossip_list_tail,
2748 send_context->fast_gossip_list_tail,
2750 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2751 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2753 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2754 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2755 //neighbor_send_task(send_context, &tc);
2760 * Type of an iterator over the hosts. Note that each
2761 * host will be called with each available protocol.
2763 * @param cls closure
2764 * @param peer id of the peer, NULL for last call
2765 * @param hello hello message for the peer (can be NULL)
2768 process_peerinfo (void *cls,
2769 const struct GNUNET_PeerIdentity *peer,
2770 const struct GNUNET_HELLO_Message *hello)
2772 struct PeerIteratorContext *peerinfo_iterator = cls;
2773 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2774 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2775 #if DEBUG_DV_PEER_NUMBERS
2782 if (distant->pkey == NULL)
2785 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2787 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2788 &peerinfo_iterator->neighbor->identity,
2789 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2795 GNUNET_free(peerinfo_iterator);
2800 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2803 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2805 if (distant->pkey == NULL)
2807 distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2808 memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2811 sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2814 GNUNET_STATISTICS_update (stats, "# distant peers gossiped to direct neighbors", sent, GNUNET_NO);
2816 #if DEBUG_DV_PEER_NUMBERS
2817 neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2818 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2820 sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2823 GNUNET_STATISTICS_update (stats, "# direct peers gossiped to direct neighbors", sent, GNUNET_NO);
2825 #if DEBUG_DV_PEER_NUMBERS
2826 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2827 GNUNET_free(neighbor_pid);
2829 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2835 * Method called whenever a peer connects.
2837 * @param cls closure
2838 * @param peer peer identity this notification is about
2839 * @param latency reported latency of the connection with peer
2840 * @param distance reported distance (DV) to peer
2842 void handle_core_connect (void *cls,
2843 const struct GNUNET_PeerIdentity * peer,
2844 struct GNUNET_TIME_Relative latency,
2847 struct DirectNeighbor *neighbor;
2848 struct DistantNeighbor *about;
2849 struct PeerIteratorContext *peerinfo_iterator;
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2853 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2856 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2858 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2859 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2860 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2861 neighbor->send_context->toNeighbor = neighbor;
2862 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2864 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2866 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2867 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2868 peerinfo_iterator->distant = about;
2869 peerinfo_iterator->neighbor = neighbor;
2870 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2872 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2876 if ((about != NULL) && (about->pkey == NULL))
2879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2882 else if (about != NULL)
2884 GNUNET_free(peerinfo_iterator);
2889 about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2890 if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2892 sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2895 GNUNET_STATISTICS_update (stats, "# direct peers gossiped to new direct neighbors", sent, GNUNET_NO);
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2907 * Method called whenever a given peer disconnects.
2909 * @param cls closure
2910 * @param peer peer identity this notification is about
2912 void handle_core_disconnect (void *cls,
2913 const struct GNUNET_PeerIdentity * peer)
2915 struct DirectNeighbor *neighbor;
2916 struct DistantNeighbor *referee;
2917 struct FindDestinationContext fdc;
2918 struct DisconnectContext disconnect_context;
2921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2922 "%s: Receives core peer disconnect message!\n", "dv");
2926 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2927 if (neighbor == NULL)
2931 while (NULL != (referee = neighbor->referee_head))
2932 distant_neighbor_free (referee);
2937 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
2939 if (fdc.dest != NULL)
2941 disconnect_context.direct = neighbor;
2942 disconnect_context.distant = fdc.dest;
2943 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2946 GNUNET_assert (neighbor->referee_tail == NULL);
2947 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
2948 &peer->hashPubKey, neighbor))
2952 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2953 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2954 GNUNET_free (neighbor);
2959 * Process dv requests.
2961 * @param cls closure
2962 * @param scheduler scheduler to use
2963 * @param server the initialized server
2964 * @param c configuration to use
2968 struct GNUNET_SCHEDULER_Handle *scheduler,
2969 struct GNUNET_SERVER_Handle *server,
2970 const struct GNUNET_CONFIGURATION_Handle *c)
2972 unsigned long long max_hosts;
2976 /* FIXME: Read from config, or calculate, or something other than this! */
2977 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2978 max_table_size = DEFAULT_DV_SIZE;
2979 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2981 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2982 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
2984 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2985 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
2988 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2989 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
2992 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2994 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2996 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2997 extended_neighbors =
2998 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
3000 GNUNET_SERVER_add_handlers (server, plugin_handlers);
3002 GNUNET_CORE_connect (sched,
3004 GNUNET_TIME_relative_get_forever(),
3005 NULL, /* FIXME: anything we want to pass around? */
3007 &handle_core_connect,
3008 &handle_core_disconnect,
3016 if (coreAPI == NULL)
3019 coreMST = GNUNET_SERVER_mst_create (&tokenized_message_handler,
3022 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
3024 if (peerinfo_handle == NULL)
3026 GNUNET_CORE_disconnect(coreAPI);
3030 /* Scheduled the task to clean up when shutdown is called */
3031 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
3032 GNUNET_TIME_UNIT_FOREVER_REL,
3039 * The main function for the dv service.
3041 * @param argc number of arguments from the command line
3042 * @param argv command line arguments
3043 * @return 0 ok, 1 on error
3046 main (int argc, char *const *argv)
3048 return (GNUNET_OK ==
3049 GNUNET_SERVICE_run (argc,
3052 GNUNET_SERVICE_OPTION_NONE,
3053 &run, NULL)) ? 0 : 1;