2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dv/gnunet-service-dv.c
23 * @brief the distance vector service, primarily handles gossip of nearby
24 * peers and sending/receiving DV messages from core and decapsulating
27 * @author Christian Grothoff
28 * @author Nathan Evans
30 * TODO: The gossip rates need to be worked out. Probably many other things
35 #include "gnunet_client_lib.h"
36 #include "gnunet_getopt_lib.h"
37 #include "gnunet_os_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_service_lib.h"
40 #include "gnunet_core_service.h"
41 #include "gnunet_signal_lib.h"
42 #include "gnunet_util_lib.h"
43 #include "gnunet_hello_lib.h"
44 #include "gnunet_peerinfo_service.h"
45 #include "gnunet_crypto_lib.h"
49 * For testing mostly, remember only the
50 * shortest path to a distant neighbor.
52 #define AT_MOST_ONE GNUNET_NO
54 #define USE_PEER_ID GNUNET_YES
57 * How often do we check about sending out more peer information (if
58 * we are connected to no peers previously).
60 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
63 * How long do we wait at most between sending out information?
65 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500000)
68 * How long can we have not heard from a peer and
69 * still have it in our tables?
71 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
74 * Priority for gossip.
76 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
79 * How often should we check if expiration time has elapsed for
82 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
85 * How long to allow a message to be delayed?
87 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
90 * Priority to use for DV data messages.
95 * The cost to a direct neighbor. We used to use 0, but 1 makes more sense.
97 #define DIRECT_NEIGHBOR_COST 1
100 * The default number of direct connections to store in DV (max)
102 #define DEFAULT_DIRECT_CONNECTIONS 50
105 * The default size of direct + extended peers in DV (max)
107 #define DEFAULT_DV_SIZE 100
110 * The default fisheye depth, from how many hops away will
113 #define DEFAULT_FISHEYE_DEPTH 4
116 * Linked list of messages to send to clients.
118 struct PendingMessage
121 * Pointer to next item in the list
123 struct PendingMessage *next;
126 * Pointer to previous item in the list
128 struct PendingMessage *prev;
131 * The PeerIdentity to send to
133 struct GNUNET_PeerIdentity recipient;
136 * The result of message sending.
138 struct GNUNET_DV_SendResultMessage *send_result;
141 * Message importance level.
143 unsigned int importance;
148 unsigned int msg_size;
151 * How long to wait before sending message.
153 struct GNUNET_TIME_Relative timeout;
156 * Actual message to be sent; // avoid allocation
158 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
162 struct FastGossipNeighborList
165 * Next element of DLL
167 struct FastGossipNeighborList *next;
170 * Prev element of DLL
172 struct FastGossipNeighborList *prev;
175 * The neighbor to gossip about
177 struct DistantNeighbor *about;
181 * Context created whenever a direct peer connects to us,
182 * used to gossip other peers to it.
184 struct NeighborSendContext
187 * The peer we will gossip to.
189 struct DirectNeighbor *toNeighbor;
192 * The task associated with this context.
194 GNUNET_SCHEDULER_TaskIdentifier task;
197 * Head of DLL of peers to gossip about
198 * as fast as possible to this peer, for initial
201 struct FastGossipNeighborList *fast_gossip_list_head;
204 * Tail of DLL of peers to gossip about
205 * as fast as possible to this peer, for initial
208 struct FastGossipNeighborList *fast_gossip_list_tail;
214 * Struct to hold information for updating existing neighbors
216 struct NeighborUpdateInfo
224 * The existing neighbor
226 struct DistantNeighbor *neighbor;
229 * The referrer of the possibly existing peer
231 struct DirectNeighbor *referrer;
234 * The time we heard about this peer
236 struct GNUNET_TIME_Absolute now;
239 * Peer id this peer uses to refer to neighbor.
241 unsigned int referrer_peer_id;
246 * Struct where actual neighbor information is stored,
247 * referenced by min_heap and max_heap. Freeing dealt
248 * with when items removed from hashmap.
250 struct DirectNeighbor
253 * Identity of neighbor.
255 struct GNUNET_PeerIdentity identity;
258 * PublicKey of neighbor.
260 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
263 * Head of DLL of nodes that this direct neighbor referred to us.
265 struct DistantNeighbor *referee_head;
268 * Tail of DLL of nodes that this direct neighbor referred to us.
270 struct DistantNeighbor *referee_tail;
273 * The sending context for gossiping peers to this neighbor.
275 struct NeighborSendContext *send_context;
278 * Is this one of the direct neighbors that we are "hiding"
286 * Struct where actual neighbor information is stored,
287 * referenced by min_heap and max_heap. Freeing dealt
288 * with when items removed from hashmap.
290 struct DistantNeighbor
293 * We keep distant neighbor's of the same referrer in a DLL.
295 struct DistantNeighbor *next;
298 * We keep distant neighbor's of the same referrer in a DLL.
300 struct DistantNeighbor *prev;
305 struct GNUNET_CONTAINER_HeapNode *min_loc;
310 struct GNUNET_CONTAINER_HeapNode *max_loc;
313 * Identity of referrer (next hop towards 'neighbor').
315 struct DirectNeighbor *referrer;
318 * Identity of neighbor.
320 struct GNUNET_PeerIdentity identity;
323 * PublicKey of neighbor.
325 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
328 * Last time we received routing information from this peer
330 struct GNUNET_TIME_Absolute last_activity;
333 * Cost to neighbor, used for actual distance vector computations
338 * Random identifier *we* use for this peer, to be used as shortcut
339 * instead of sending full peer id for each message
344 * Random identifier the *referrer* uses for this peer.
346 unsigned int referrer_id;
349 * Is this one of the direct neighbors that we are "hiding"
356 struct PeerIteratorContext
359 * The actual context, to be freed later.
361 struct GNUNET_PEERINFO_IteratorContext *ic;
364 * The neighbor about which we are concerned.
366 struct DirectNeighbor *neighbor;
369 * The distant neighbor entry for this direct neighbor.
371 struct DistantNeighbor *distant;
376 * Context used for creating hello messages when
377 * gossips are received.
382 * Identity of distant neighbor.
384 struct GNUNET_PeerIdentity distant_peer;
387 * Identity of direct neighbor, via which we send this message.
389 const struct GNUNET_PeerIdentity *direct_peer;
392 * How many addresses do we need to add (always starts at 1, then set to 0)
394 int addresses_to_add;
398 struct DV_SendContext
401 * The distant peer (should always match)
403 struct GNUNET_PeerIdentity *distant_peer;
406 * The direct peer, we need to verify the referrer of.
408 struct GNUNET_PeerIdentity *direct_peer;
411 * The message to be sent
413 struct GNUNET_MessageHeader *message;
416 * The pre-built send result message. Simply needs to be queued
417 * and freed once send has been called!
419 struct GNUNET_DV_SendResultMessage *send_result;
422 * The size of the message being sent, may be larger
423 * than message->header.size because it's multiple
424 * messages packed into one!
429 * How important is this message?
431 unsigned int importance;
434 * Timeout for this message
436 struct GNUNET_TIME_Relative timeout;
439 * Unique ID for DV message
444 struct FindDestinationContext
447 struct DistantNeighbor *dest;
453 struct GNUNET_PeerIdentity *dest;
454 const struct GNUNET_PeerIdentity *via;
457 struct DisconnectContext
460 * Distant neighbor to get pid from.
462 struct DistantNeighbor *distant;
465 * Direct neighbor that disconnected.
467 struct DirectNeighbor *direct;
470 struct TokenizedMessageContext
473 * Immediate sender of this message
475 const struct GNUNET_PeerIdentity *peer;
478 * Distant sender of the message
480 struct DistantNeighbor *distant;
483 * Uid for this set of messages
489 * Context for finding the least cost peer to send to.
490 * Transport selection can only go so far.
492 struct FindLeastCostContext
494 struct DistantNeighbor *target;
495 unsigned int least_cost;
499 * Handle to the core service api.
501 static struct GNUNET_CORE_Handle *coreAPI;
504 * Stream tokenizer to handle messages coming in from core.
506 static struct GNUNET_SERVER_MessageStreamTokenizer *coreMST;
509 * The identity of our peer.
511 static struct GNUNET_PeerIdentity my_identity;
514 * The configuration for this service.
516 static const struct GNUNET_CONFIGURATION_Handle *cfg;
519 * The scheduler for this service.
521 static struct GNUNET_SCHEDULER_Handle *sched;
524 * The client, the DV plugin connected to us. Hopefully
525 * this client will never change, although if the plugin dies
526 * and returns for some reason it may happen.
528 static struct GNUNET_SERVER_Client * client_handle;
531 * Task to run when we shut down, cleaning up all our trash
533 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
535 static size_t default_dv_priority = 0;
537 static char *my_short_id;
540 * Transmit handle to the plugin.
542 static struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
545 * Head of DLL for client messages
547 static struct PendingMessage *plugin_pending_head;
550 * Tail of DLL for client messages
552 static struct PendingMessage *plugin_pending_tail;
555 * Handle to the peerinfo service
557 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
560 * Transmit handle to core service.
562 static struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
565 * Head of DLL for core messages
567 static struct PendingMessage *core_pending_head;
570 * Tail of DLL for core messages
572 static struct PendingMessage *core_pending_tail;
575 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
576 * directly connected peers.
578 static struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
581 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
582 * peers connected via DV (extended neighborhood). Does ALSO
583 * include any peers that are in 'direct_neighbors'; for those
584 * peers, the cost will be zero and the referrer all zeros.
586 static struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
589 * We use the min heap (min refers to cost) to prefer
590 * gossipping about peers with small costs.
592 static struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
595 * We use the max heap (max refers to cost) for general
596 * iterations over all peers and to remove the most costly
597 * connection if we have too many.
599 static struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
602 * How far out to keep peers we learn about.
604 static unsigned long long fisheye_depth;
607 * How many peers to store at most.
609 static unsigned long long max_table_size;
612 * We've been given a target ID based on the random numbers that
613 * we assigned to our DV-neighborhood. Find the entry for the
614 * respective neighbor.
617 find_destination (void *cls,
618 struct GNUNET_CONTAINER_HeapNode *node,
619 void *element, GNUNET_CONTAINER_HeapCostType cost)
621 struct FindDestinationContext *fdc = cls;
622 struct DistantNeighbor *dn = element;
624 if (fdc->tid != dn->our_id)
632 * We've been given a target ID based on the random numbers that
633 * we assigned to our DV-neighborhood. Find the entry for the
634 * respective neighbor.
637 find_specific_id (void *cls,
638 const GNUNET_HashCode *key,
641 struct FindIDContext *fdc = cls;
642 struct DistantNeighbor *dn = value;
644 if (memcmp(&dn->referrer->identity, fdc->via, sizeof(struct GNUNET_PeerIdentity)) == 0)
646 fdc->tid = dn->referrer_id;
653 * Find a distant peer whose referrer_id matches what we're
654 * looking for. For looking up a peer we've gossipped about
655 * but is now disconnected. Need to do this because we don't
656 * want to remove those that may be accessible via a different
659 static int find_distant_peer (void *cls,
660 const GNUNET_HashCode * key,
663 struct FindDestinationContext *fdc = cls;
664 struct DistantNeighbor *distant = value;
666 if (fdc->tid == distant->referrer_id)
675 * Function called to notify a client about the socket
676 * begin ready to queue more data. "buf" will be
677 * NULL and "size" zero if the socket was closed for
678 * writing in the meantime.
681 * @param size number of bytes available in buf
682 * @param buf where the callee should write the message
683 * @return number of bytes written to buf
685 size_t transmit_to_plugin (void *cls,
686 size_t size, void *buf)
689 struct PendingMessage *reply;
695 /* client disconnected */
696 #if DEBUG_DV_MESSAGES
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: %s buffer was NULL (client disconnect?)\n", my_short_id, "transmit_to_plugin");
701 plugin_transmit_handle = NULL;
703 while ( (NULL != (reply = plugin_pending_head)) &&
704 (size >= off + (msize = ntohs (reply->msg->size))))
706 #if DEBUG_DV_MESSAGES
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: transmit_notify (plugin) called with size %d (message sent)\n", my_short_id, msize);
709 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
712 memcpy (&cbuf[off], reply->msg, msize);
717 if (plugin_pending_head != NULL)
718 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
719 ntohs(plugin_pending_head->msg->size),
720 GNUNET_TIME_UNIT_FOREVER_REL,
721 &transmit_to_plugin, NULL);
727 * Send a message to the dv plugin.
729 * @param sender the direct sender of the message
730 * @param message the message to send to the plugin
731 * (may be an encapsulated type)
732 * @param message_size the size of the message to be sent
733 * @param distant_neighbor the original sender of the message
734 * @param cost the cost to the original sender of the message
736 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
737 const struct GNUNET_MessageHeader *message,
739 struct GNUNET_PeerIdentity *distant_neighbor,
742 struct GNUNET_DV_MessageReceived *received_msg;
743 struct PendingMessage *pending_message;
744 #if DEBUG_DV_MESSAGES
745 struct GNUNET_MessageHeader * packed_message_header;
746 struct GNUNET_HELLO_Message *hello_msg;
747 struct GNUNET_PeerIdentity hello_identity;
749 char *sender_address;
750 size_t sender_address_len;
751 char *packed_msg_start;
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
758 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
760 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
761 sender_address = GNUNET_malloc(sender_address_len);
762 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
763 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
767 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
768 sender_address = GNUNET_malloc(sender_address_len);
769 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
772 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
773 received_msg = GNUNET_malloc(size);
774 received_msg->header.size = htons(size);
775 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
776 received_msg->sender_address_len = htonl(sender_address_len);
777 received_msg->distance = htonl(cost);
778 received_msg->msg_len = htonl(message_size);
779 /* Set the sender in this message to be the original sender! */
780 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
781 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
782 memcpy(&received_msg[1], sender_address, sender_address_len);
783 GNUNET_free(sender_address);
784 /* Copy the actual message after the sender */
785 packed_msg_start = (char *)&received_msg[1];
786 packed_msg_start = &packed_msg_start[sender_address_len];
787 memcpy(packed_msg_start, message, message_size);
788 #if DEBUG_DV_MESSAGES
789 packed_message_header = (struct GNUNET_MessageHeader *)packed_msg_start;
790 if (ntohs(packed_message_header->type) == GNUNET_MESSAGE_TYPE_HELLO)
792 hello_msg = (struct GNUNET_HELLO_Message *)packed_msg_start;
793 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello_msg, &hello_identity));
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: send_to_plugin: Packed HELLO message is about peer %s\n", my_short_id, GNUNET_i2s(&hello_identity));
797 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
798 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
799 memcpy(&pending_message[1], received_msg, size);
800 GNUNET_free(received_msg);
802 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
804 if (client_handle != NULL)
806 if (plugin_transmit_handle == NULL)
808 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
809 size, GNUNET_TIME_UNIT_FOREVER_REL,
810 &transmit_to_plugin, NULL);
812 #if DEBUG_DV_MESSAGES
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, client_handle not yet set (how?)!\n");
827 * Function called to notify a client about the socket
828 * being ready to queue more data. "buf" will be
829 * NULL and "size" zero if the socket was closed for
830 * writing in the meantime.
833 * @param size number of bytes available in buf
834 * @param buf where the callee should write the message
835 * @return number of bytes written to buf
837 size_t core_transmit_notify (void *cls,
838 size_t size, void *buf)
841 struct PendingMessage *reply;
842 struct PendingMessage *client_reply;
848 /* client disconnected */
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
855 core_transmit_handle = NULL;
857 reply = core_pending_head;
858 if ( (reply != NULL) &&
859 (size >= (msize = ntohs (reply->msg->size))))
862 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
864 GNUNET_CONTAINER_DLL_remove (core_pending_head,
867 if (reply->send_result != NULL) /* Will only be non-null if a real client asked for this send */
869 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
870 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
871 memcpy(&client_reply[1], reply->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
872 GNUNET_free(reply->send_result);
874 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
875 if (client_handle != NULL)
877 if (plugin_transmit_handle == NULL)
879 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
880 sizeof(struct GNUNET_DV_SendResultMessage),
881 GNUNET_TIME_UNIT_FOREVER_REL,
882 &transmit_to_plugin, NULL);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
890 memcpy (&cbuf[off], reply->msg, msize);
894 reply = core_pending_head;
896 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);
903 * Send a DV data message via DV.
905 * @param sender the original sender of the message
906 * @param recipient the next hop recipient, may be our direct peer, maybe not
907 * @param send_context the send context
910 send_message_via (const struct GNUNET_PeerIdentity *sender,
911 const struct GNUNET_PeerIdentity *recipient,
912 struct DV_SendContext *send_context)
914 p2p_dv_MESSAGE_Data *toSend;
915 unsigned int msg_size;
916 unsigned int recipient_id;
917 unsigned int sender_id;
918 struct DistantNeighbor *source;
919 struct PendingMessage *pending_message;
920 struct FindIDContext find_context;
925 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
927 find_context.dest = send_context->distant_peer;
928 find_context.via = recipient;
929 find_context.tid = 0;
930 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
931 &find_specific_id, &find_context);
933 if (find_context.tid == 0)
935 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
936 /* target unknown to us, drop! */
937 return GNUNET_SYSERR;
939 recipient_id = find_context.tid;
941 if (0 == (memcmp (&my_identity,
942 sender, sizeof (struct GNUNET_PeerIdentity))))
945 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
946 &sender->hashPubKey);
948 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));
952 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
953 &sender->hashPubKey);
956 /* sender unknown to us, drop! */
957 return GNUNET_SYSERR;
959 sender_id = source->our_id;
962 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
963 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
964 pending_message->send_result = send_context->send_result;
965 memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
966 pending_message->msg_size = msg_size;
967 pending_message->importance = send_context->importance;
968 pending_message->timeout = send_context->timeout;
969 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
970 toSend->header.size = htons (msg_size);
971 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
972 toSend->sender = htonl (sender_id);
973 toSend->recipient = htonl (recipient_id);
974 #if DEBUG_DV_MESSAGES
975 toSend->uid = send_context->uid; /* Still sent around in network byte order */
977 toSend->uid = htonl(0);
980 memcpy (&toSend[1], send_context->message, send_context->message_size);
983 memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
985 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);
988 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
993 if (core_transmit_handle == NULL)
994 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, send_context->importance, send_context->timeout, recipient, msg_size, &core_transmit_notify, NULL);
998 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
1005 * Given a FindLeastCostContext, and a set
1006 * of peers that match the target, return the cheapest.
1008 * @param cls closure, a struct FindLeastCostContext
1009 * @param key the key identifying the target peer
1010 * @param value the target peer
1012 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1015 find_least_cost_peer (void *cls,
1016 const GNUNET_HashCode *key,
1019 struct FindLeastCostContext *find_context = cls;
1020 struct DistantNeighbor *dn = value;
1022 if (dn->cost < find_context->least_cost)
1024 find_context->target = dn;
1026 if (dn->cost == DIRECT_NEIGHBOR_COST)
1032 * Send a DV data message via DV.
1034 * @param recipient the ultimate recipient of this message
1035 * @param sender the original sender of the message
1036 * @param specific_neighbor the specific neighbor to send this message via
1037 * @param message the packed message
1038 * @param message_size size of the message
1039 * @param importance what priority to send this message with
1040 * @param timeout how long to possibly delay sending this message
1043 send_message (const struct GNUNET_PeerIdentity * recipient,
1044 const struct GNUNET_PeerIdentity * sender,
1045 const struct DistantNeighbor * specific_neighbor,
1046 const struct GNUNET_MessageHeader * message,
1047 size_t message_size,
1048 unsigned int importance,
1050 struct GNUNET_TIME_Relative timeout)
1052 p2p_dv_MESSAGE_Data *toSend;
1053 unsigned int msg_size;
1055 unsigned int recipient_id;
1056 unsigned int sender_id;
1057 struct DistantNeighbor *target;
1058 struct DistantNeighbor *source;
1059 struct PendingMessage *pending_message;
1060 struct FindLeastCostContext find_least_ctx;
1061 #if DEBUG_DV_PEER_NUMBERS
1062 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1063 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1064 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1066 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1068 find_least_ctx.least_cost = -1;
1069 find_least_ctx.target = NULL;
1071 * Need to find the least cost peer, lest the transport selection keep
1072 * picking the same DV route for the same destination which results
1073 * in messages looping forever. Relatively cheap, we don't iterate
1074 * over all known peers, just those that apply.
1076 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1077 &recipient->hashPubKey, &find_least_cost_peer, &find_least_ctx);
1078 target = find_least_ctx.target;
1082 /* target unknown to us, drop! */
1083 return GNUNET_SYSERR;
1085 recipient_id = target->referrer_id;
1087 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1088 &sender->hashPubKey);
1091 if (0 != (memcmp (&my_identity,
1092 sender, sizeof (struct GNUNET_PeerIdentity))))
1094 /* sender unknown to us, drop! */
1095 return GNUNET_SYSERR;
1097 sender_id = 0; /* 0 == us */
1101 /* find out the number that we use when we gossip about
1103 sender_id = source->our_id;
1106 #if DEBUG_DV_PEER_NUMBERS
1107 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1108 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1109 encPeerFrom.encoding[4] = '\0';
1110 encPeerVia.encoding[4] = '\0';
1112 if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1117 cost = target->cost;
1118 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1119 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1120 pending_message->send_result = NULL;
1121 pending_message->importance = importance;
1122 pending_message->timeout = timeout;
1123 memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1124 pending_message->msg_size = msg_size;
1125 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1126 toSend->header.size = htons (msg_size);
1127 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1128 toSend->sender = htonl (sender_id);
1129 toSend->recipient = htonl (recipient_id);
1130 #if DEBUG_DV_MESSAGES
1131 toSend->uid = htonl(uid);
1133 toSend->uid = htonl(0);
1136 #if DEBUG_DV_PEER_NUMBERS
1137 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1138 encPeerTo.encoding[4] = '\0';
1139 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);
1141 memcpy (&toSend[1], message, message_size);
1142 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1147 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));
1149 if (core_transmit_handle == NULL)
1150 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
1152 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: CORE ALREADY SENDING\n", "DV SEND MESSAGE", msg_size);
1157 struct CheckPeerContext
1162 struct DistantNeighbor *peer;
1165 * Sender id to search for
1167 unsigned int sender_id;
1171 * Iterator over hash map entries.
1173 * @param cls closure
1174 * @param key current key code
1175 * @param value value in the hash map
1176 * @return GNUNET_YES if we should continue to
1180 int checkPeerID (void *cls,
1181 const GNUNET_HashCode * key,
1184 struct CheckPeerContext *ctx = cls;
1185 struct DistantNeighbor *distant = value;
1187 if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1189 ctx->peer = distant;
1199 * Handler for messages parsed out by the tokenizer from
1200 * DV DATA received for this peer.
1203 * @param client the TokenizedMessageContext which contains message information
1204 * @param message the actual message
1206 void tokenized_message_handler (void *cls,
1208 const struct GNUNET_MessageHeader *message)
1210 struct TokenizedMessageContext *ctx = client;
1211 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1212 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1213 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1214 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1216 #if DEBUG_DV_MESSAGES
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "%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));
1220 GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1221 send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1226 * Core handler for dv data messages. Whatever this message
1227 * contains all we really have to do is rip it out of its
1228 * DV layering and give it to our pal the DV plugin to report
1231 * @param cls closure
1232 * @param peer peer which sent the message (immediate sender)
1233 * @param message the message
1234 * @param latency the latency of the connection we received the message from
1235 * @param distance the distance to the immediate peer
1237 static int handle_dv_data_message (void *cls,
1238 const struct GNUNET_PeerIdentity * peer,
1239 const struct GNUNET_MessageHeader * message,
1240 struct GNUNET_TIME_Relative latency,
1243 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1244 const struct GNUNET_MessageHeader *packed_message;
1245 struct DirectNeighbor *dn;
1246 struct DistantNeighbor *pos;
1247 unsigned int sid; /* Sender id */
1248 unsigned int tid; /* Target id */
1249 struct GNUNET_PeerIdentity original_sender;
1250 struct GNUNET_PeerIdentity destination;
1251 struct FindDestinationContext fdc;
1252 struct TokenizedMessageContext tkm_ctx;
1254 struct CheckPeerContext checkPeerCtx;
1259 size_t packed_message_size;
1264 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "%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);
1272 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "`%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));
1279 return GNUNET_SYSERR;
1282 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "%s: dn NULL!\n", "dv");
1292 sid = ntohl (incoming->sender);
1296 checkPeerCtx.sender_id = sid;
1297 checkPeerCtx.peer = NULL;
1298 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1299 pos = checkPeerCtx.peer;
1303 pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1307 pos = dn->referee_head;
1308 while ((NULL != pos) && (pos->referrer_id != sid))
1314 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1315 #if DEBUG_DV_MESSAGES
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "%s: unknown sender (%u), Message uid %llu from %s!\n", GNUNET_i2s(&my_identity), ntohl(incoming->sender), ntohl(incoming->uid), direct_id);
1319 GNUNET_free(direct_id);
1320 pos = dn->referee_head;
1321 while ((NULL != pos) && (pos->referrer_id != sid))
1323 sender_id = strdup(GNUNET_i2s(&pos->identity));
1324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1325 GNUNET_free(sender_id);
1329 #if DEBUG_MESSAGE_DROP
1330 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "%s: DROPPING MESSAGE type %d, unknown sender! Message immediately from %s!\n", GNUNET_i2s(&my_identity), ntohs(((struct GNUNET_MessageHeader *)&incoming[1])->type), direct_id);
1333 GNUNET_free(direct_id);
1335 /* unknown sender */
1338 original_sender = pos->identity;
1339 tid = ntohl (incoming->recipient);
1343 cbuf = (char *)&incoming[1];
1345 tkm_ctx.peer = peer;
1346 tkm_ctx.distant = pos;
1347 tkm_ctx.uid = ntohl(incoming->uid);
1348 if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1351 packed_message_size,
1356 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1360 while(offset < packed_message_size)
1362 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1364 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1365 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1366 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1367 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1369 #if DEBUG_DV_MESSAGES
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "%s: Receives %s message(s) for me, uid %u, total size %d cost %u from %s!\n", my_short_id, "DV DATA", ntohl(incoming->uid), ntohs(packed_message->size), pos->cost, GNUNET_i2s(&pos->identity));
1373 GNUNET_assert(memcmp(peer, &pos->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1374 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
1376 offset += ntohs(packed_message->size);
1383 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1386 /* FIXME: this is the *only* per-request operation we have in DV
1387 that is O(n) in relation to the number of connected peers; a
1388 hash-table lookup could easily solve this (minor performance
1392 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1393 &find_destination, &fdc);
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1400 if (fdc.dest == NULL)
1402 #if DEBUG_DV_MESSAGES
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1404 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1408 destination = fdc.dest->identity;
1410 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1412 /* FIXME: create stat: routing loop-discard! */
1413 #if DEBUG_DV_PEER_NUMBERS
1414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1417 #if DEBUG_DV_MESSAGES
1418 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "%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);
1425 /* At this point we have a message, and we need to forward it on to the
1428 /* FIXME: Can't send message on, we have to behave.
1429 * We have to tell core we have a message for the next peer, and let
1430 * transport do transport selection on how to get this message to 'em */
1431 /*ret = send_message (&destination,
1433 packed_message, DV_PRIORITY, DV_DELAY);*/
1435 #if DEBUG_DV_MESSAGES
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1437 "%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);
1440 ret = send_message(&destination,
1444 packed_message_size,
1445 default_dv_priority,
1446 ntohl(incoming->uid),
1447 GNUNET_TIME_relative_get_forever());
1449 if (ret != GNUNET_SYSERR)
1453 #if DEBUG_MESSAGE_DROP
1454 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "%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);
1458 return GNUNET_SYSERR;
1464 * Iterator over hash map entries.
1466 * @param cls closure (NULL)
1467 * @param key current key code
1468 * @param value value in the hash map (DistantNeighbor)
1469 * @return GNUNET_YES if we should continue to
1473 int print_neighbors (void *cls,
1474 const GNUNET_HashCode * key,
1477 struct DistantNeighbor *distant_neighbor = value;
1478 char my_shortname[5];
1479 char referrer_shortname[5];
1480 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1481 my_shortname[4] = '\0';
1482 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1483 referrer_shortname[4] = '\0';
1485 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");
1491 * Scheduled task which gossips about known direct peers to other connected
1492 * peers. Will run until called with reason shutdown.
1495 neighbor_send_task (void *cls,
1496 const struct GNUNET_SCHEDULER_TaskContext *tc)
1498 struct NeighborSendContext *send_context = cls;
1499 #if DEBUG_DV_GOSSIP_SEND
1500 char * encPeerAbout;
1503 struct DistantNeighbor *about;
1504 struct DirectNeighbor *to;
1505 struct FastGossipNeighborList *about_list;
1507 p2p_dv_MESSAGE_NeighborInfo *message;
1508 struct PendingMessage *pending_message;
1510 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "%s: Called with reason shutdown, shutting down!\n",
1515 GNUNET_i2s(&my_identity));
1520 if (send_context->fast_gossip_list_head != NULL)
1522 about_list = send_context->fast_gossip_list_head;
1523 about = about_list->about;
1524 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1525 send_context->fast_gossip_list_tail,
1527 GNUNET_free(about_list);
1531 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1532 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1533 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1534 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1535 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1536 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1537 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1538 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1539 * asynchronously enough to avoid major problems (-;
1541 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1543 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1545 to = send_context->toNeighbor;
1547 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1549 (about->hidden == GNUNET_NO) &&
1552 (0 != memcmp (&about->identity,
1553 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1554 (about->pkey != NULL))
1556 #if DEBUG_DV_GOSSIP_SEND
1557 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1558 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1561 GNUNET_i2s(&my_identity),
1562 encPeerAbout, about->our_id, encPeerTo);
1563 GNUNET_free(encPeerAbout);
1564 GNUNET_free(encPeerTo);
1566 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1567 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1568 pending_message->importance = default_dv_priority;
1569 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1570 memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1571 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1572 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1573 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1574 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1575 message->cost = htonl (about->cost);
1576 message->neighbor_id = htonl (about->our_id);
1578 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1579 memcpy (&message->neighbor,
1580 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1582 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1587 if (core_transmit_handle == NULL)
1588 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);
1592 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1594 #if DEBUG_DV_PEER_NUMBERS
1595 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1597 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1601 #if DEBUG_DV_PEER_NUMBERS
1602 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1604 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1612 * Handle START-message. This is the first message sent to us
1613 * by the client (can only be one!).
1615 * @param cls closure (always NULL)
1616 * @param client identification of the client
1617 * @param message the actual message
1620 handle_start (void *cls,
1621 struct GNUNET_SERVER_Client *client,
1622 const struct GNUNET_MessageHeader *message)
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 "Received `%s' request from client\n", "START");
1630 client_handle = client;
1632 GNUNET_SERVER_client_keep(client_handle);
1633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1638 * Iterate over hash map entries for a distant neighbor,
1639 * if direct neighbor matches context call send message
1641 * @param cls closure, a DV_SendContext
1642 * @param key current key code
1643 * @param value value in the hash map
1644 * @return GNUNET_YES if we should continue to
1648 int send_iterator (void *cls,
1649 const GNUNET_HashCode * key,
1652 struct DV_SendContext *send_context = cls;
1653 struct DistantNeighbor *distant_neighbor = value;
1655 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1657 send_message_via(&my_identity, distant_neighbor, send_context);
1665 * Service server's handler for message send requests (which come
1666 * bubbling up to us through the DV plugin).
1668 * @param cls closure
1669 * @param client identification of the client
1670 * @param message the actual message
1672 void handle_dv_send_message (void *cls,
1673 struct GNUNET_SERVER_Client * client,
1674 const struct GNUNET_MessageHeader * message)
1676 struct GNUNET_DV_SendMessage *send_msg;
1677 struct GNUNET_DV_SendResultMessage *send_result_msg;
1678 struct PendingMessage *pending_message;
1680 size_t message_size;
1681 struct GNUNET_PeerIdentity *destination;
1682 struct GNUNET_PeerIdentity *direct;
1683 struct GNUNET_MessageHeader *message_buf;
1686 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1687 struct DV_SendContext *send_context;
1688 #if DEBUG_DV_MESSAGES
1690 struct GNUNET_MessageHeader *packed_message;
1693 if (client_handle == NULL)
1695 client_handle = client;
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1699 else if (client_handle != client)
1701 client_handle = client;
1702 /* What should we do in this case, assert fail or just log the warning? */
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "%s: Setting client handle (was a different client!)!\n", "dv");
1709 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1710 send_msg = (struct GNUNET_DV_SendMessage *)message;
1712 address_len = ntohl(send_msg->addrlen);
1713 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1714 message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1715 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1716 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1717 message_buf = GNUNET_malloc(message_size);
1719 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1720 offset = 0; /* Offset starts at zero */
1722 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1723 offset += sizeof(struct GNUNET_PeerIdentity);
1725 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1726 offset += sizeof(struct GNUNET_PeerIdentity);
1729 memcpy(message_buf, &temp_pos[offset], message_size);
1730 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1732 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1733 dest_hash.encoding[4] = '\0';
1734 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);
1737 #if DEBUG_DV_MESSAGES
1738 cbuf = (char *)message_buf;
1740 while(offset < message_size)
1742 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1743 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));
1744 offset += ntohs(packed_message->size);
1746 /*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));*/
1748 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1749 dest_hash.encoding[4] = '\0';
1750 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1752 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1753 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1754 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1755 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1757 send_context->importance = ntohl(send_msg->priority);
1758 send_context->timeout = send_msg->timeout;
1759 send_context->direct_peer = direct;
1760 send_context->distant_peer = destination;
1761 send_context->message = message_buf;
1762 send_context->message_size = message_size;
1763 send_context->send_result = send_result_msg;
1764 #if DEBUG_DV_MESSAGES
1765 send_context->uid = send_msg->uid;
1768 if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1770 send_result_msg->result = htons(1);
1771 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1772 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1773 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1774 GNUNET_free(send_result_msg);
1776 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1778 if (client_handle != NULL)
1780 if (plugin_transmit_handle == NULL)
1782 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1783 sizeof(struct GNUNET_DV_SendResultMessage),
1784 GNUNET_TIME_UNIT_FOREVER_REL,
1785 &transmit_to_plugin, NULL);
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1792 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1793 dest_hash.encoding[4] = '\0';
1794 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));
1797 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1799 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1801 send_result_msg->result = htons(1);
1802 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1803 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1804 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1805 GNUNET_free(send_result_msg);
1807 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1809 if (client_handle != NULL)
1811 if (plugin_transmit_handle == NULL)
1813 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1814 sizeof(struct GNUNET_DV_SendResultMessage),
1815 GNUNET_TIME_UNIT_FOREVER_REL,
1816 &transmit_to_plugin, NULL);
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1823 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1824 dest_hash.encoding[4] = '\0';
1825 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));
1828 GNUNET_free(message_buf);
1829 GNUNET_free(send_context);
1830 GNUNET_free(direct);
1831 GNUNET_free(destination);
1833 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1836 /** Forward declarations **/
1837 static int handle_dv_gossip_message (void *cls,
1838 const struct GNUNET_PeerIdentity *peer,
1839 const struct GNUNET_MessageHeader *message,
1840 struct GNUNET_TIME_Relative latency,
1843 static int handle_dv_disconnect_message (void *cls,
1844 const struct GNUNET_PeerIdentity *peer,
1845 const struct GNUNET_MessageHeader *message,
1846 struct GNUNET_TIME_Relative latency,
1848 /** End forward declarations **/
1852 * List of handlers for the messages understood by this
1855 * Hmm... will we need to register some handlers with core and
1856 * some handlers with our server here? Because core should be
1857 * getting the incoming DV messages (from whichever lower level
1858 * transport) and then our server should be getting messages
1859 * from the dv_plugin, right?
1861 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1862 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1863 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1864 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1868 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1869 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1870 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1875 * Free a DistantNeighbor node, including removing it
1876 * from the referer's list.
1879 distant_neighbor_free (struct DistantNeighbor *referee)
1881 struct DirectNeighbor *referrer;
1883 referrer = referee->referrer;
1884 if (referrer != NULL)
1886 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1887 referrer->referee_tail, referee);
1889 GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
1890 GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
1891 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
1892 &referee->identity.hashPubKey);
1893 GNUNET_free_non_null (referee->pkey);
1894 GNUNET_free (referee);
1898 * Free a DirectNeighbor node, including removing it
1899 * from the referer's list.
1902 direct_neighbor_free (struct DirectNeighbor *direct)
1904 struct NeighborSendContext *send_context;
1905 struct FastGossipNeighborList *about_list;
1906 struct FastGossipNeighborList *prev_about;
1908 send_context = direct->send_context;
1910 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1911 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1913 about_list = send_context->fast_gossip_list_head;
1914 while (about_list != NULL)
1916 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1917 prev_about = about_list;
1918 about_list = about_list->next;
1919 GNUNET_free(prev_about);
1921 GNUNET_free(send_context);
1922 GNUNET_free(direct);
1926 * Multihashmap iterator for sending out disconnect messages
1929 * @param cls the peer that was disconnected
1930 * @param key key value stored under
1931 * @param value the direct neighbor to send disconnect to
1933 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1935 static int schedule_disconnect_messages (void *cls,
1936 const GNUNET_HashCode * key,
1939 struct DisconnectContext *disconnect_context = cls;
1940 struct DirectNeighbor *disconnected = disconnect_context->direct;
1941 struct DirectNeighbor *notify = value;
1942 struct PendingMessage *pending_message;
1943 p2p_dv_MESSAGE_Disconnect *disconnect_message;
1945 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1946 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
1948 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
1949 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1950 pending_message->importance = default_dv_priority;
1951 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1952 memcpy(&pending_message->recipient, ¬ify->identity, sizeof(struct GNUNET_PeerIdentity));
1953 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
1954 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
1955 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
1956 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
1957 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
1959 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1964 if (core_transmit_handle == NULL)
1965 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);
1971 * Multihashmap iterator for freeing extended neighbors.
1974 * @param key key value stored under
1975 * @param value the distant neighbor to be freed
1977 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1979 static int free_extended_neighbors (void *cls,
1980 const GNUNET_HashCode * key,
1983 struct DistantNeighbor *distant = value;
1984 distant_neighbor_free(distant);
1989 * Multihashmap iterator for freeing direct neighbors.
1992 * @param key key value stored under
1993 * @param value the direct neighbor to be freed
1995 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1997 static int free_direct_neighbors (void *cls,
1998 const GNUNET_HashCode * key,
2001 struct DirectNeighbor *direct = value;
2002 direct_neighbor_free(direct);
2008 * Task run during shutdown.
2014 shutdown_task (void *cls,
2015 const struct GNUNET_SCHEDULER_TaskContext *tc)
2018 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2019 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2021 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2022 GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2023 GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2024 GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2026 GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2027 GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2029 GNUNET_CORE_disconnect (coreAPI);
2030 GNUNET_PEERINFO_disconnect(peerinfo_handle);
2031 GNUNET_SERVER_mst_destroy(coreMST);
2032 GNUNET_free_non_null(my_short_id);
2034 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2039 * To be called on core init/fail.
2041 void core_init (void *cls,
2042 struct GNUNET_CORE_Handle * server,
2043 const struct GNUNET_PeerIdentity *identity,
2044 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2049 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
2050 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2057 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2058 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2063 #if PKEY_NO_NEIGHBOR_ON_ADD
2065 * Iterator over hash map entries.
2067 * @param cls closure
2068 * @param key current key code
2069 * @param value value in the hash map
2070 * @return GNUNET_YES if we should continue to
2074 static int add_pkey_to_extended (void *cls,
2075 const GNUNET_HashCode * key,
2078 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2079 struct DistantNeighbor *distant_neighbor = value;
2081 if (distant_neighbor->pkey == NULL)
2083 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2084 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2092 * Iterator over hash map entries.
2094 * @param cls closure
2095 * @param key current key code
2096 * @param value value in the hash map
2097 * @return GNUNET_YES if we should continue to
2101 static int update_matching_neighbors (void *cls,
2102 const GNUNET_HashCode * key,
2105 struct NeighborUpdateInfo * update_info = cls;
2106 struct DistantNeighbor *distant_neighbor = value;
2108 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2110 /* same referrer, cost change! */
2111 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2112 update_info->neighbor->max_loc, update_info->cost);
2113 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2114 update_info->neighbor->min_loc, update_info->cost);
2115 update_info->neighbor->last_activity = update_info->now;
2116 update_info->neighbor->cost = update_info->cost;
2117 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2126 * Iterate over all current direct peers, add DISTANT newly connected
2127 * peer to the fast gossip list for that peer so we get DV routing
2128 * information out as fast as possible!
2130 * @param cls the newly connected neighbor we will gossip about
2131 * @param key the hashcode of the peer
2132 * @param value the direct neighbor we should gossip to
2134 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2136 static int add_distant_all_direct_neighbors (void *cls,
2137 const GNUNET_HashCode * key,
2140 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2141 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2142 struct NeighborSendContext *send_context = direct->send_context;
2143 struct FastGossipNeighborList *gossip_entry;
2149 if (distant == NULL)
2154 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2156 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2160 if (distant->hidden == GNUNET_YES)
2161 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2163 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2164 gossip_entry->about = distant;
2166 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2167 send_context->fast_gossip_list_tail,
2168 send_context->fast_gossip_list_tail,
2171 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2172 encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2175 GNUNET_i2s(&my_identity),
2176 encPeerAbout, distant->our_id, encPeerTo);
2177 GNUNET_free(encPeerAbout);
2178 GNUNET_free(encPeerTo);
2180 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2181 GNUNET_SCHEDULER_cancel(sched, send_context->task);*/
2183 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2188 * Callback for hello address creation.
2190 * @param cls closure, a struct HelloContext
2191 * @param max maximum number of bytes that can be written to buf
2192 * @param buf where to write the address information
2194 * @return number of bytes written, 0 to signal the
2195 * end of the iteration.
2198 generate_hello_address (void *cls, size_t max, void *buf)
2200 struct HelloContext *hello_context = cls;
2206 if (hello_context->addresses_to_add == 0)
2209 /* Hello "address" will be concatenation of distant peer and direct peer identities */
2210 size = 2 * sizeof(struct GNUNET_PeerIdentity);
2211 GNUNET_assert(max >= size);
2213 addr_buffer = GNUNET_malloc(size);
2215 /* Copy the distant peer identity to buffer */
2216 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
2217 offset += sizeof(struct GNUNET_PeerIdentity);
2218 /* Copy the direct peer identity to buffer */
2219 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
2220 ret = GNUNET_HELLO_add_address ("dv",
2221 GNUNET_TIME_relative_to_absolute
2222 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2225 hello_context->addresses_to_add--;
2227 GNUNET_free(addr_buffer);
2233 * Handles when a peer is either added due to being newly connected
2234 * or having been gossiped about, also called when the cost for a neighbor
2235 * needs to be updated.
2237 * @param peer identity of the peer whose info is being added/updated
2238 * @param pkey public key of the peer whose info is being added/updated
2239 * @param referrer_peer_id id to use when sending to 'peer'
2240 * @param referrer if this is a gossiped peer, who did we hear it from?
2241 * @param cost the cost of communicating with this peer via 'referrer'
2243 * @return the added neighbor, the updated neighbor or NULL (neighbor
2246 static struct DistantNeighbor *
2247 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2248 unsigned int referrer_peer_id,
2249 struct DirectNeighbor *referrer, unsigned int cost)
2251 struct DistantNeighbor *neighbor;
2252 struct DistantNeighbor *max;
2253 struct GNUNET_TIME_Absolute now;
2254 struct NeighborUpdateInfo *neighbor_update;
2255 struct HelloContext *hello_context;
2256 struct GNUNET_HELLO_Message *hello_msg;
2257 unsigned int our_id;
2261 #if DEBUG_DV_PEER_NUMBERS
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2267 now = GNUNET_TIME_absolute_get ();
2268 neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2270 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2271 neighbor_update->neighbor = neighbor;
2272 neighbor_update->cost = cost;
2273 neighbor_update->now = now;
2274 neighbor_update->referrer = referrer;
2275 neighbor_update->referrer_peer_id = referrer_peer_id;
2277 if (neighbor != NULL)
2280 memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2282 our_id = neighbor->our_id;
2288 memcpy(&our_id, peer, sizeof(unsigned int));
2290 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2294 /* Either we do not know this peer, or we already do but via a different immediate peer */
2295 if ((neighbor == NULL) ||
2296 (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2298 &update_matching_neighbors,
2299 neighbor_update) != GNUNET_SYSERR))
2303 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2305 distant_neighbor_free(neighbor);
2307 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2313 if (cost > fisheye_depth)
2316 GNUNET_free(neighbor_update);
2320 #if DEBUG_DV_PEER_NUMBERS
2321 encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2323 "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2324 GNUNET_free(encAbout);
2327 if (max_table_size <=
2328 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2330 /* remove most expensive entry */
2331 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2332 GNUNET_assert(max != NULL);
2333 if (cost > max->cost)
2335 /* new entry most expensive, don't create */
2336 GNUNET_free(neighbor_update);
2341 /* only free if this is not a direct connection;
2342 we could theoretically have more direct
2343 connections than DV entries allowed total! */
2344 distant_neighbor_free (max);
2348 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2349 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2350 referrer->referee_tail, neighbor);
2351 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2353 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2355 neighbor->referrer = referrer;
2356 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2357 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2359 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2360 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2363 neighbor->pkey = pkey;
2365 neighbor->last_activity = now;
2366 neighbor->cost = cost;
2367 neighbor->referrer_id = referrer_peer_id;
2368 neighbor->our_id = our_id;
2370 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2373 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2377 if (cost != DIRECT_NEIGHBOR_COST)
2379 /* Added neighbor, now send HELLO to transport */
2380 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2381 hello_context->direct_peer = &referrer->identity;
2382 memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2383 hello_context->addresses_to_add = 1;
2384 hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2385 GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2386 addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2387 addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2388 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);
2391 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2392 GNUNET_free(hello_context);
2393 GNUNET_free(hello_msg);
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2409 GNUNET_free(neighbor_update);
2415 * Core handler for dv disconnect messages. These will be used
2416 * by us to tell transport via the dv plugin that a peer can
2417 * no longer be contacted by us via a certain address. We should
2418 * then propagate these messages on, given that the distance to
2419 * the peer indicates we would have gossiped about it to others.
2421 * @param cls closure
2422 * @param peer peer which sent the message (immediate sender)
2423 * @param message the message
2424 * @param latency the latency of the connection we received the message from
2425 * @param distance the distance to the immediate peer
2427 static int handle_dv_disconnect_message (void *cls,
2428 const struct GNUNET_PeerIdentity *peer,
2429 const struct GNUNET_MessageHeader *message,
2430 struct GNUNET_TIME_Relative latency,
2433 struct DirectNeighbor *referrer;
2434 struct DistantNeighbor *distant;
2435 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2437 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2439 return GNUNET_SYSERR; /* invalid message */
2442 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2444 if (referrer == NULL)
2447 distant = referrer->referee_head;
2448 while (distant != NULL)
2450 if (distant->referrer_id == ntohl(enc_message->peer_id))
2452 distant_neighbor_free(distant);
2454 distant = referrer->referee_head;
2462 * Core handler for dv gossip messages. These will be used
2463 * by us to create a HELLO message for the newly peer containing
2464 * which direct peer we can connect through, and what the cost
2465 * is. This HELLO will then be scheduled for validation by the
2466 * transport service so that it can be used by all others.
2468 * @param cls closure
2469 * @param peer peer which sent the message (immediate sender)
2470 * @param message the message
2471 * @param latency the latency of the connection we received the message from
2472 * @param distance the distance to the immediate peer
2474 static int handle_dv_gossip_message (void *cls,
2475 const struct GNUNET_PeerIdentity *peer,
2476 const struct GNUNET_MessageHeader *message,
2477 struct GNUNET_TIME_Relative latency,
2480 struct DirectNeighbor *referrer;
2481 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2483 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2485 return GNUNET_SYSERR; /* invalid message */
2488 #if DEBUG_DV_GOSSIP_RECEIPT
2489 char * encPeerAbout;
2492 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2493 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "%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);
2496 GNUNET_free(encPeerAbout);
2497 GNUNET_free(encPeerFrom);
2500 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2502 if (referrer == NULL)
2505 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2506 ntohl (enc_message->neighbor_id),
2507 referrer, ntohl (enc_message->cost) + 1);
2514 * Iterate over all currently known peers, add them to the
2515 * fast gossip list for this peer so we get DV routing information
2516 * out as fast as possible!
2518 * @param cls the direct neighbor we will gossip to
2519 * @param key the hashcode of the peer
2520 * @param value the distant neighbor we should add to the list
2522 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2524 static int add_all_extended_peers (void *cls,
2525 const GNUNET_HashCode * key,
2528 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2529 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2530 struct FastGossipNeighborList *gossip_entry;
2532 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2533 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2536 if (distant->hidden == GNUNET_YES)
2537 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2539 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2540 gossip_entry->about = distant;
2542 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2543 send_context->fast_gossip_list_tail,
2544 send_context->fast_gossip_list_tail,
2552 * Iterator over hash map entries.
2554 * @param cls closure
2555 * @param key current key code
2556 * @param value value in the hash map
2557 * @return GNUNET_YES if we should continue to
2561 static int gossip_all_to_all_iterator (void *cls,
2562 const GNUNET_HashCode * key,
2565 struct DirectNeighbor *direct = value;
2567 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2569 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2570 GNUNET_SCHEDULER_cancel(sched, direct->send_context->task);
2572 direct->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, direct->send_context);
2577 * Task run during shutdown.
2583 gossip_all_to_all (void *cls,
2584 const struct GNUNET_SCHEDULER_TaskContext *tc)
2586 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2588 GNUNET_SCHEDULER_add_delayed (sched,
2589 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2596 * Iterate over all current direct peers, add newly connected peer
2597 * to the fast gossip list for that peer so we get DV routing
2598 * information out as fast as possible!
2600 * @param cls the newly connected neighbor we will gossip about
2601 * @param key the hashcode of the peer
2602 * @param value the direct neighbor we should gossip to
2604 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2606 static int add_all_direct_neighbors (void *cls,
2607 const GNUNET_HashCode * key,
2610 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2611 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2612 struct DistantNeighbor *distant;
2613 struct NeighborSendContext *send_context = direct->send_context;
2614 struct FastGossipNeighborList *gossip_entry;
2618 distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2619 if (distant == NULL)
2624 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2626 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2630 if (distant->hidden == GNUNET_YES)
2631 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2633 direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2635 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);
2637 GNUNET_free(direct_id);
2638 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2639 gossip_entry->about = distant;
2641 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2642 send_context->fast_gossip_list_tail,
2643 send_context->fast_gossip_list_tail,
2645 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2646 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2648 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2649 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2650 //neighbor_send_task(send_context, &tc);
2655 * Type of an iterator over the hosts. Note that each
2656 * host will be called with each available protocol.
2658 * @param cls closure
2659 * @param peer id of the peer, NULL for last call
2660 * @param hello hello message for the peer (can be NULL)
2661 * @param trust amount of trust we have in the peer
2664 process_peerinfo (void *cls,
2665 const struct GNUNET_PeerIdentity *peer,
2666 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2668 struct PeerIteratorContext *peerinfo_iterator = cls;
2669 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2670 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2671 #if DEBUG_DV_PEER_NUMBERS
2676 if (peer == NULL) /* && (neighbor->pkey == NULL))*/
2678 if (distant->pkey == NULL) /* FIXME: Reschedule? */
2681 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2683 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2684 &peerinfo_iterator->neighbor->identity,
2686 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2692 GNUNET_free(peerinfo_iterator);
2697 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2700 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2702 if (distant->pkey == NULL)
2704 distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2705 memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2708 /* Why do it this way, now we have the distant neighbor! */
2709 /*GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2711 &add_pkey_to_extended,
2714 sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2716 #if DEBUG_DV_PEER_NUMBERS
2717 neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2720 sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2721 #if DEBUG_DV_PEER_NUMBERS
2722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2723 GNUNET_free(neighbor_pid);
2725 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2731 * Method called whenever a peer connects.
2733 * @param cls closure
2734 * @param peer peer identity this notification is about
2735 * @param latency reported latency of the connection with peer
2736 * @param distance reported distance (DV) to peer
2738 void handle_core_connect (void *cls,
2739 const struct GNUNET_PeerIdentity * peer,
2740 struct GNUNET_TIME_Relative latency,
2743 struct DirectNeighbor *neighbor;
2744 struct DistantNeighbor *about;
2745 struct PeerIteratorContext *peerinfo_iterator;
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2749 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2752 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2754 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2755 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2756 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2757 neighbor->send_context->toNeighbor = neighbor;
2758 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2760 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2762 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2763 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2764 peerinfo_iterator->distant = about;
2765 peerinfo_iterator->neighbor = neighbor;
2766 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2769 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2773 if ((about != NULL) && (about->pkey == NULL))
2776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2779 else if (about != NULL)
2781 GNUNET_free(peerinfo_iterator);
2786 about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2787 if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2788 sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2798 * Method called whenever a given peer disconnects.
2800 * @param cls closure
2801 * @param peer peer identity this notification is about
2803 void handle_core_disconnect (void *cls,
2804 const struct GNUNET_PeerIdentity * peer)
2806 struct DirectNeighbor *neighbor;
2807 struct DistantNeighbor *referee;
2808 struct FindDestinationContext fdc;
2809 struct DisconnectContext disconnect_context;
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 "%s: Receives core peer disconnect message!\n", "dv");
2817 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2818 if (neighbor == NULL)
2822 while (NULL != (referee = neighbor->referee_head))
2823 distant_neighbor_free (referee);
2828 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
2830 if (fdc.dest != NULL)
2832 disconnect_context.direct = neighbor;
2833 disconnect_context.distant = fdc.dest;
2834 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2837 GNUNET_assert (neighbor->referee_tail == NULL);
2838 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
2839 &peer->hashPubKey, neighbor))
2843 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2844 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2845 GNUNET_free (neighbor);
2850 * Process dv requests.
2852 * @param cls closure
2853 * @param scheduler scheduler to use
2854 * @param server the initialized server
2855 * @param c configuration to use
2859 struct GNUNET_SCHEDULER_Handle *scheduler,
2860 struct GNUNET_SERVER_Handle *server,
2861 const struct GNUNET_CONFIGURATION_Handle *c)
2863 unsigned long long max_hosts;
2867 /* FIXME: Read from config, or calculate, or something other than this! */
2868 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2869 max_table_size = DEFAULT_DV_SIZE;
2870 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2872 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2873 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
2875 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2876 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
2879 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2880 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
2883 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2885 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2887 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2888 extended_neighbors =
2889 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
2891 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2893 GNUNET_CORE_connect (sched,
2895 GNUNET_TIME_relative_get_forever(),
2896 NULL, /* FIXME: anything we want to pass around? */
2898 &handle_core_connect,
2899 &handle_core_disconnect,
2906 if (coreAPI == NULL)
2909 coreMST = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE,
2910 &tokenized_message_handler,
2913 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2915 if (peerinfo_handle == NULL)
2917 GNUNET_CORE_disconnect(coreAPI);
2921 /* Scheduled the task to clean up when shutdown is called */
2922 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
2923 GNUNET_TIME_UNIT_FOREVER_REL,
2927 GNUNET_SCHEDULER_add_delayed (sched,
2928 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2936 * The main function for the dv service.
2938 * @param argc number of arguments from the command line
2939 * @param argv command line arguments
2940 * @return 0 ok, 1 on error
2943 main (int argc, char *const *argv)
2945 return (GNUNET_OK ==
2946 GNUNET_SERVICE_run (argc,
2949 GNUNET_SERVICE_OPTION_NONE,
2950 &run, NULL)) ? 0 : 1;