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 */
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
701 plugin_transmit_handle = NULL;
703 while ( (NULL != (reply = plugin_pending_head)) &&
704 (size >= off + (msize = ntohs (reply->msg->size))))
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (plugin) called with size %d\n", "dv service", 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;
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);
789 packed_message_header = (struct GNUNET_MessageHeader *)packed_msg_start;
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv service created received message. sender_address_len %lu, packed message len %d, total len %d\n", sender_address_len, ntohl(received_msg->msg_len), ntohs(received_msg->header.size));
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv packed message len %d, type %d\n", ntohs(packed_message_header->size), ntohs(packed_message_header->type));
792 if (ntohs(packed_message_header->type) == GNUNET_MESSAGE_TYPE_HELLO)
794 hello_msg = (struct GNUNET_HELLO_Message *)packed_message_header;
795 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello_msg, &hello_identity));
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity));
799 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
800 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
801 memcpy(&pending_message[1], received_msg, size);
802 GNUNET_free(received_msg);
804 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
806 if (client_handle != NULL)
808 if (plugin_transmit_handle == NULL)
810 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
811 size, GNUNET_TIME_UNIT_FOREVER_REL,
812 &transmit_to_plugin, NULL);
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
825 * Function called to notify a client about the socket
826 * being ready to queue more data. "buf" will be
827 * NULL and "size" zero if the socket was closed for
828 * writing in the meantime.
831 * @param size number of bytes available in buf
832 * @param buf where the callee should write the message
833 * @return number of bytes written to buf
835 size_t core_transmit_notify (void *cls,
836 size_t size, void *buf)
839 struct PendingMessage *reply;
840 struct PendingMessage *client_reply;
846 /* client disconnected */
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
853 core_transmit_handle = NULL;
855 reply = core_pending_head;
856 if ( (reply != NULL) &&
857 (size >= (msize = ntohs (reply->msg->size))))
860 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
862 GNUNET_CONTAINER_DLL_remove (core_pending_head,
865 if (reply->send_result != NULL) /* Will only be non-null if a real client asked for this send */
867 client_reply = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
868 client_reply->msg = (struct GNUNET_MessageHeader *)&client_reply[1];
869 memcpy(&client_reply[1], reply->send_result, sizeof(struct GNUNET_DV_SendResultMessage));
870 GNUNET_free(reply->send_result);
872 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, client_reply);
873 if (client_handle != NULL)
875 if (plugin_transmit_handle == NULL)
877 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
878 sizeof(struct GNUNET_DV_SendResultMessage),
879 GNUNET_TIME_UNIT_FOREVER_REL,
880 &transmit_to_plugin, NULL);
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
888 memcpy (&cbuf[off], reply->msg, msize);
892 reply = core_pending_head;
894 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, reply->importance, reply->timeout, &reply->recipient, reply->msg_size, &core_transmit_notify, NULL);
901 * Send a DV data message via DV.
903 * @param sender the original sender of the message
904 * @param recipient the next hop recipient, may be our direct peer, maybe not
905 * @param send_context the send context
908 send_message_via (const struct GNUNET_PeerIdentity *sender,
909 const struct GNUNET_PeerIdentity *recipient,
910 struct DV_SendContext *send_context)
912 p2p_dv_MESSAGE_Data *toSend;
913 unsigned int msg_size;
914 unsigned int recipient_id;
915 unsigned int sender_id;
916 struct DistantNeighbor *source;
917 struct PendingMessage *pending_message;
918 struct FindIDContext find_context;
923 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
925 find_context.dest = send_context->distant_peer;
926 find_context.via = recipient;
927 find_context.tid = 0;
928 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors, &send_context->distant_peer->hashPubKey,
929 &find_specific_id, &find_context);
931 if (find_context.tid == 0)
933 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: find_specific_id failed to find peer!\n", my_short_id);
934 /* target unknown to us, drop! */
935 return GNUNET_SYSERR;
937 recipient_id = find_context.tid;
939 if (0 == (memcmp (&my_identity,
940 sender, sizeof (struct GNUNET_PeerIdentity))))
943 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
944 &sender->hashPubKey);
946 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));
950 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
951 &sender->hashPubKey);
954 /* sender unknown to us, drop! */
955 return GNUNET_SYSERR;
957 sender_id = source->our_id;
960 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
961 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
962 pending_message->send_result = send_context->send_result;
963 memcpy(&pending_message->recipient, recipient, sizeof(struct GNUNET_PeerIdentity));
964 pending_message->msg_size = msg_size;
965 pending_message->importance = send_context->importance;
966 pending_message->timeout = send_context->timeout;
967 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
968 toSend->header.size = htons (msg_size);
969 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
970 toSend->sender = htonl (sender_id);
971 toSend->recipient = htonl (recipient_id);
972 #if DEBUG_DV_MESSAGES
973 toSend->uid = send_context->uid; /* Still sent around in network byte order */
975 toSend->uid = htonl(0);
978 memcpy (&toSend[1], send_context->message, send_context->message_size);
981 memcpy(&shortname, GNUNET_i2s(send_context->distant_peer), 4);
983 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);
986 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
991 if (core_transmit_handle == NULL)
992 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, send_context->importance, send_context->timeout, recipient, msg_size, &core_transmit_notify, NULL);
996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
1003 * Given a FindLeastCostContext, and a set
1004 * of peers that match the target, return the cheapest.
1006 * @param cls closure, a struct FindLeastCostContext
1007 * @param key the key identifying the target peer
1008 * @param value the target peer
1010 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1013 find_least_cost_peer (void *cls,
1014 const GNUNET_HashCode *key,
1017 struct FindLeastCostContext *find_context = cls;
1018 struct DistantNeighbor *dn = value;
1020 if (dn->cost < find_context->least_cost)
1022 find_context->target = dn;
1024 if (dn->cost == DIRECT_NEIGHBOR_COST)
1030 * Send a DV data message via DV.
1032 * @param recipient the ultimate recipient of this message
1033 * @param sender the original sender of the message
1034 * @param specific_neighbor the specific neighbor to send this message via
1035 * @param message the packed message
1036 * @param message_size size of the message
1037 * @param importance what priority to send this message with
1038 * @param timeout how long to possibly delay sending this message
1041 send_message (const struct GNUNET_PeerIdentity * recipient,
1042 const struct GNUNET_PeerIdentity * sender,
1043 const struct DistantNeighbor * specific_neighbor,
1044 const struct GNUNET_MessageHeader * message,
1045 size_t message_size,
1046 unsigned int importance,
1048 struct GNUNET_TIME_Relative timeout)
1050 p2p_dv_MESSAGE_Data *toSend;
1051 unsigned int msg_size;
1053 unsigned int recipient_id;
1054 unsigned int sender_id;
1055 struct DistantNeighbor *target;
1056 struct DistantNeighbor *source;
1057 struct PendingMessage *pending_message;
1058 struct FindLeastCostContext find_least_ctx;
1059 #if DEBUG_DV_PEER_NUMBERS
1060 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerFrom;
1061 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerTo;
1062 struct GNUNET_CRYPTO_HashAsciiEncoded encPeerVia;
1064 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
1066 find_least_ctx.least_cost = -1;
1067 find_least_ctx.target = NULL;
1069 * Need to find the least cost peer, lest the transport selection keep
1070 * picking the same DV route for the same destination which results
1071 * in messages looping forever. Relatively cheap, we don't iterate
1072 * over all known peers, just those that apply.
1074 GNUNET_CONTAINER_multihashmap_get_multiple (extended_neighbors,
1075 &recipient->hashPubKey, &find_least_cost_peer, &find_least_ctx);
1076 target = find_least_ctx.target;
1080 /* target unknown to us, drop! */
1081 return GNUNET_SYSERR;
1083 recipient_id = target->referrer_id;
1085 source = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1086 &sender->hashPubKey);
1089 if (0 != (memcmp (&my_identity,
1090 sender, sizeof (struct GNUNET_PeerIdentity))))
1092 /* sender unknown to us, drop! */
1093 return GNUNET_SYSERR;
1095 sender_id = 0; /* 0 == us */
1099 /* find out the number that we use when we gossip about
1101 sender_id = source->our_id;
1104 #if DEBUG_DV_PEER_NUMBERS
1105 GNUNET_CRYPTO_hash_to_enc (&source->identity.hashPubKey, &encPeerFrom);
1106 GNUNET_CRYPTO_hash_to_enc (&target->referrer->identity.hashPubKey, &encPeerVia);
1107 encPeerFrom.encoding[4] = '\0';
1108 encPeerVia.encoding[4] = '\0';
1110 if ((sender_id != 0) && (0 == memcmp(&source->identity, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity))))
1115 cost = target->cost;
1116 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
1117 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1118 pending_message->send_result = NULL;
1119 pending_message->importance = importance;
1120 pending_message->timeout = timeout;
1121 memcpy(&pending_message->recipient, &target->referrer->identity, sizeof(struct GNUNET_PeerIdentity));
1122 pending_message->msg_size = msg_size;
1123 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
1124 toSend->header.size = htons (msg_size);
1125 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
1126 toSend->sender = htonl (sender_id);
1127 toSend->recipient = htonl (recipient_id);
1128 #if DEBUG_DV_MESSAGES
1129 toSend->uid = htonl(uid);
1131 toSend->uid = htonl(0);
1134 #if DEBUG_DV_PEER_NUMBERS
1135 GNUNET_CRYPTO_hash_to_enc (&target->identity.hashPubKey, &encPeerTo);
1136 encPeerTo.encoding[4] = '\0';
1137 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);
1139 memcpy (&toSend[1], message, message_size);
1140 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1145 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));
1147 if (core_transmit_handle == NULL)
1148 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
1150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: CORE ALREADY SENDING\n", "DV SEND MESSAGE", msg_size);
1155 struct CheckPeerContext
1160 struct DistantNeighbor *peer;
1163 * Sender id to search for
1165 unsigned int sender_id;
1169 * Iterator over hash map entries.
1171 * @param cls closure
1172 * @param key current key code
1173 * @param value value in the hash map
1174 * @return GNUNET_YES if we should continue to
1178 int checkPeerID (void *cls,
1179 const GNUNET_HashCode * key,
1182 struct CheckPeerContext *ctx = cls;
1183 struct DistantNeighbor *distant = value;
1185 if (memcmp(key, &ctx->sender_id, sizeof(unsigned int)) == 0)
1187 ctx->peer = distant;
1197 * Handler for messages parsed out by the tokenizer from
1198 * DV DATA received for this peer.
1201 * @param client the TokenizedMessageContext which contains message information
1202 * @param message the actual message
1204 void tokenized_message_handler (void *cls,
1206 const struct GNUNET_MessageHeader *message)
1208 struct TokenizedMessageContext *ctx = client;
1209 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1210 GNUNET_break_op (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1211 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1212 (ntohs (message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1214 #if DEBUG_DV_MESSAGES
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "%s: Receives %s message for me, uid %u, total size %d cost %u from %s!\n", my_short_id, "DV DATA", ctx->uid, ntohs(message->size), ctx->distant->cost, GNUNET_i2s(&ctx->distant->identity));
1218 GNUNET_assert(memcmp(ctx->peer, &ctx->distant->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1219 send_to_plugin(ctx->peer, message, ntohs(message->size), &ctx->distant->identity, ctx->distant->cost);
1224 * Core handler for dv data messages. Whatever this message
1225 * contains all we really have to do is rip it out of its
1226 * DV layering and give it to our pal the DV plugin to report
1229 * @param cls closure
1230 * @param peer peer which sent the message (immediate sender)
1231 * @param message the message
1232 * @param latency the latency of the connection we received the message from
1233 * @param distance the distance to the immediate peer
1235 static int handle_dv_data_message (void *cls,
1236 const struct GNUNET_PeerIdentity * peer,
1237 const struct GNUNET_MessageHeader * message,
1238 struct GNUNET_TIME_Relative latency,
1241 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
1242 const struct GNUNET_MessageHeader *packed_message;
1243 struct DirectNeighbor *dn;
1244 struct DistantNeighbor *pos;
1245 unsigned int sid; /* Sender id */
1246 unsigned int tid; /* Target id */
1247 struct GNUNET_PeerIdentity original_sender;
1248 struct GNUNET_PeerIdentity destination;
1249 struct FindDestinationContext fdc;
1250 struct TokenizedMessageContext tkm_ctx;
1252 struct CheckPeerContext checkPeerCtx;
1257 size_t packed_message_size;
1262 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "%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);
1270 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1275 "`%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));
1277 return GNUNET_SYSERR;
1280 dn = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "%s: dn NULL!\n", "dv");
1290 sid = ntohl (incoming->sender);
1294 checkPeerCtx.sender_id = sid;
1295 checkPeerCtx.peer = NULL;
1296 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &checkPeerID, &checkPeerCtx);
1297 pos = checkPeerCtx.peer;
1301 pos = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
1305 pos = dn->referee_head;
1306 while ((NULL != pos) && (pos->referrer_id != sid))
1312 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "%s: unknown sender (%u), Message from %s!\n", GNUNET_i2s(&my_identity), ntohl(incoming->sender), direct_id);
1317 GNUNET_free(direct_id);
1318 pos = dn->referee_head;
1319 while ((NULL != pos) && (pos->referrer_id != sid))
1321 sender_id = strdup(GNUNET_i2s(&pos->identity));
1322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I know sender %u %s\n", pos->referrer_id, sender_id);
1323 GNUNET_free(sender_id);
1327 #if DEBUG_MESSAGE_DROP
1328 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "%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);
1331 GNUNET_free(direct_id);
1333 /* unknown sender */
1336 original_sender = pos->identity;
1337 tid = ntohl (incoming->recipient);
1341 cbuf = (char *)&incoming[1];
1343 tkm_ctx.peer = peer;
1344 tkm_ctx.distant = pos;
1345 tkm_ctx.uid = ntohl(incoming->uid);
1346 if (GNUNET_OK != GNUNET_SERVER_mst_receive (coreMST,
1349 packed_message_size,
1354 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: %s Received corrupt data, discarding!", my_short_id, "DV SERVICE");
1358 while(offset < packed_message_size)
1360 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1362 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1363 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
1364 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
1365 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
1367 #if DEBUG_DV_MESSAGES
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 "%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));
1371 GNUNET_assert(memcmp(peer, &pos->identity, sizeof(struct GNUNET_PeerIdentity)) != 0);
1372 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
1374 offset += ntohs(packed_message->size);
1381 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
1384 /* FIXME: this is the *only* per-request operation we have in DV
1385 that is O(n) in relation to the number of connected peers; a
1386 hash-table lookup could easily solve this (minor performance
1390 GNUNET_CONTAINER_heap_iterate (neighbor_max_heap,
1391 &find_destination, &fdc);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
1398 if (fdc.dest == NULL)
1400 #if DEBUG_DV_MESSAGES
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "%s: Receives %s message uid %u for someone we don't know (id %u)!\n", my_short_id, "DV DATA", ntohl(incoming->uid), tid);
1406 destination = fdc.dest->identity;
1408 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
1410 /* FIXME: create stat: routing loop-discard! */
1411 #if DEBUG_DV_PEER_NUMBERS
1412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
1415 #if DEBUG_DV_MESSAGES
1416 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "%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);
1423 /* At this point we have a message, and we need to forward it on to the
1426 /* FIXME: Can't send message on, we have to behave.
1427 * We have to tell core we have a message for the next peer, and let
1428 * transport do transport selection on how to get this message to 'em */
1429 /*ret = send_message (&destination,
1431 packed_message, DV_PRIORITY, DV_DELAY);*/
1433 #if DEBUG_DV_MESSAGES
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "%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);
1438 ret = send_message(&destination,
1442 packed_message_size,
1443 default_dv_priority,
1444 ntohl(incoming->uid),
1445 GNUNET_TIME_relative_get_forever());
1447 if (ret != GNUNET_SYSERR)
1451 #if DEBUG_MESSAGE_DROP
1452 direct_id = GNUNET_strdup(GNUNET_i2s(&dn->identity));
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1454 "%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);
1456 return GNUNET_SYSERR;
1462 * Iterator over hash map entries.
1464 * @param cls closure (NULL)
1465 * @param key current key code
1466 * @param value value in the hash map (DistantNeighbor)
1467 * @return GNUNET_YES if we should continue to
1471 int print_neighbors (void *cls,
1472 const GNUNET_HashCode * key,
1475 struct DistantNeighbor *distant_neighbor = value;
1476 char my_shortname[5];
1477 char referrer_shortname[5];
1478 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1479 my_shortname[4] = '\0';
1480 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1481 referrer_shortname[4] = '\0';
1483 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");
1489 * Scheduled task which gossips about known direct peers to other connected
1490 * peers. Will run until called with reason shutdown.
1493 neighbor_send_task (void *cls,
1494 const struct GNUNET_SCHEDULER_TaskContext *tc)
1496 struct NeighborSendContext *send_context = cls;
1497 #if DEBUG_DV_GOSSIP_SEND
1498 char * encPeerAbout;
1501 struct DistantNeighbor *about;
1502 struct DirectNeighbor *to;
1503 struct FastGossipNeighborList *about_list;
1505 p2p_dv_MESSAGE_NeighborInfo *message;
1506 struct PendingMessage *pending_message;
1508 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "%s: Called with reason shutdown, shutting down!\n",
1513 GNUNET_i2s(&my_identity));
1518 if (send_context->fast_gossip_list_head != NULL)
1520 about_list = send_context->fast_gossip_list_head;
1521 about = about_list->about;
1522 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head,
1523 send_context->fast_gossip_list_tail,
1525 GNUNET_free(about_list);
1529 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1530 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1531 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1532 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1533 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1534 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1535 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1536 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1537 * asynchronously enough to avoid major problems (-;
1539 * NOTE: probably fixed once we decided send rate based on allowed bandwidth.
1541 about = GNUNET_CONTAINER_heap_walk_get_next (neighbor_min_heap);
1543 to = send_context->toNeighbor;
1545 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1547 (about->hidden == GNUNET_NO) &&
1550 (0 != memcmp (&about->identity,
1551 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1552 (about->pkey != NULL))
1554 #if DEBUG_DV_GOSSIP_SEND
1555 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1556 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "%s: Sending info about peer %s id %u to directly connected peer %s\n",
1559 GNUNET_i2s(&my_identity),
1560 encPeerAbout, about->our_id, encPeerTo);
1561 GNUNET_free(encPeerAbout);
1562 GNUNET_free(encPeerTo);
1564 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1565 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1566 pending_message->importance = default_dv_priority;
1567 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1568 memcpy(&pending_message->recipient, &to->identity, sizeof(struct GNUNET_PeerIdentity));
1569 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo);
1570 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1571 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1572 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1573 message->cost = htonl (about->cost);
1574 message->neighbor_id = htonl (about->our_id);
1576 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1577 memcpy (&message->neighbor,
1578 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1580 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1585 if (core_transmit_handle == NULL)
1586 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);
1590 if (send_context->fast_gossip_list_head != NULL) /* If there are other peers in the fast list, schedule right away */
1592 #if DEBUG_DV_PEER_NUMBERS
1593 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: still in fast send mode\n");
1595 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
1599 #if DEBUG_DV_PEER_NUMBERS
1600 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SERVICE: entering slow send mode\n");
1602 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_DV_DEFAULT_SEND_INTERVAL, &neighbor_send_task, send_context);
1610 * Handle START-message. This is the first message sent to us
1611 * by the client (can only be one!).
1613 * @param cls closure (always NULL)
1614 * @param client identification of the client
1615 * @param message the actual message
1618 handle_start (void *cls,
1619 struct GNUNET_SERVER_Client *client,
1620 const struct GNUNET_MessageHeader *message)
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "Received `%s' request from client\n", "START");
1628 client_handle = client;
1630 GNUNET_SERVER_client_keep(client_handle);
1631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1636 * Iterate over hash map entries for a distant neighbor,
1637 * if direct neighbor matches context call send message
1639 * @param cls closure, a DV_SendContext
1640 * @param key current key code
1641 * @param value value in the hash map
1642 * @return GNUNET_YES if we should continue to
1646 int send_iterator (void *cls,
1647 const GNUNET_HashCode * key,
1650 struct DV_SendContext *send_context = cls;
1651 struct DistantNeighbor *distant_neighbor = value;
1653 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1655 send_message_via(&my_identity, distant_neighbor, send_context);
1663 * Service server's handler for message send requests (which come
1664 * bubbling up to us through the DV plugin).
1666 * @param cls closure
1667 * @param client identification of the client
1668 * @param message the actual message
1670 void handle_dv_send_message (void *cls,
1671 struct GNUNET_SERVER_Client * client,
1672 const struct GNUNET_MessageHeader * message)
1674 struct GNUNET_DV_SendMessage *send_msg;
1675 struct GNUNET_DV_SendResultMessage *send_result_msg;
1676 struct PendingMessage *pending_message;
1678 size_t message_size;
1679 struct GNUNET_PeerIdentity *destination;
1680 struct GNUNET_PeerIdentity *direct;
1681 struct GNUNET_MessageHeader *message_buf;
1684 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1685 struct DV_SendContext *send_context;
1686 #if DEBUG_DV_MESSAGES
1688 struct GNUNET_MessageHeader *packed_message;
1691 if (client_handle == NULL)
1693 client_handle = client;
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1697 else if (client_handle != client)
1699 client_handle = client;
1700 /* What should we do in this case, assert fail or just log the warning? */
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "%s: Setting client handle (was a different client!)!\n", "dv");
1707 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1708 send_msg = (struct GNUNET_DV_SendMessage *)message;
1710 address_len = ntohl(send_msg->addrlen);
1711 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1712 message_size = ntohs(message->size) - sizeof(struct GNUNET_DV_SendMessage) - address_len;
1713 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1714 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1715 message_buf = GNUNET_malloc(message_size);
1717 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1718 offset = 0; /* Offset starts at zero */
1720 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1721 offset += sizeof(struct GNUNET_PeerIdentity);
1723 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1724 offset += sizeof(struct GNUNET_PeerIdentity);
1727 memcpy(message_buf, &temp_pos[offset], message_size);
1728 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1730 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1731 dest_hash.encoding[4] = '\0';
1732 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);
1735 #if DEBUG_DV_MESSAGES
1736 cbuf = (char *)message_buf;
1738 while(offset < message_size)
1740 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
1741 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));
1742 offset += ntohs(packed_message->size);
1744 /*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));*/
1746 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1747 dest_hash.encoding[4] = '\0';
1748 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1750 send_result_msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendResultMessage));
1751 send_result_msg->header.size = htons(sizeof(struct GNUNET_DV_SendResultMessage));
1752 send_result_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT);
1753 send_result_msg->uid = send_msg->uid; /* No need to ntohl->htonl this */
1755 send_context->importance = ntohl(send_msg->priority);
1756 send_context->timeout = send_msg->timeout;
1757 send_context->direct_peer = direct;
1758 send_context->distant_peer = destination;
1759 send_context->message = message_buf;
1760 send_context->message_size = message_size;
1761 send_context->send_result = send_result_msg;
1762 #if DEBUG_DV_MESSAGES
1763 send_context->uid = send_msg->uid;
1766 if (send_message_via(&my_identity, direct, send_context) != GNUNET_YES)
1768 send_result_msg->result = htons(1);
1769 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1770 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1771 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1772 GNUNET_free(send_result_msg);
1774 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1776 if (client_handle != NULL)
1778 if (plugin_transmit_handle == NULL)
1780 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1781 sizeof(struct GNUNET_DV_SendResultMessage),
1782 GNUNET_TIME_UNIT_FOREVER_REL,
1783 &transmit_to_plugin, NULL);
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1790 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1791 dest_hash.encoding[4] = '\0';
1792 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));
1795 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1797 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1799 send_result_msg->result = htons(1);
1800 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(struct GNUNET_DV_SendResultMessage));
1801 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1802 memcpy(&pending_message[1], send_result_msg, sizeof(struct GNUNET_DV_SendResultMessage));
1803 GNUNET_free(send_result_msg);
1805 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
1807 if (client_handle != NULL)
1809 if (plugin_transmit_handle == NULL)
1811 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
1812 sizeof(struct GNUNET_DV_SendResultMessage),
1813 GNUNET_TIME_UNIT_FOREVER_REL,
1814 &transmit_to_plugin, NULL);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to queue message for plugin, must be one in progress already!!\n");
1821 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1822 dest_hash.encoding[4] = '\0';
1823 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));
1826 GNUNET_free(message_buf);
1827 GNUNET_free(send_context);
1828 GNUNET_free(direct);
1829 GNUNET_free(destination);
1831 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1834 /** Forward declarations **/
1835 static int handle_dv_gossip_message (void *cls,
1836 const struct GNUNET_PeerIdentity *peer,
1837 const struct GNUNET_MessageHeader *message,
1838 struct GNUNET_TIME_Relative latency,
1841 static int handle_dv_disconnect_message (void *cls,
1842 const struct GNUNET_PeerIdentity *peer,
1843 const struct GNUNET_MessageHeader *message,
1844 struct GNUNET_TIME_Relative latency,
1846 /** End forward declarations **/
1850 * List of handlers for the messages understood by this
1853 * Hmm... will we need to register some handlers with core and
1854 * some handlers with our server here? Because core should be
1855 * getting the incoming DV messages (from whichever lower level
1856 * transport) and then our server should be getting messages
1857 * from the dv_plugin, right?
1859 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1860 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1861 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1862 {&handle_dv_disconnect_message, GNUNET_MESSAGE_TYPE_DV_DISCONNECT, 0},
1866 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1867 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1868 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1873 * Free a DistantNeighbor node, including removing it
1874 * from the referer's list.
1877 distant_neighbor_free (struct DistantNeighbor *referee)
1879 struct DirectNeighbor *referrer;
1881 referrer = referee->referrer;
1882 if (referrer != NULL)
1884 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1885 referrer->referee_tail, referee);
1887 GNUNET_CONTAINER_heap_remove_node (neighbor_max_heap, referee->max_loc);
1888 GNUNET_CONTAINER_heap_remove_node (neighbor_min_heap, referee->min_loc);
1889 GNUNET_CONTAINER_multihashmap_remove_all (extended_neighbors,
1890 &referee->identity.hashPubKey);
1891 GNUNET_free_non_null (referee->pkey);
1892 GNUNET_free (referee);
1896 * Free a DirectNeighbor node, including removing it
1897 * from the referer's list.
1900 direct_neighbor_free (struct DirectNeighbor *direct)
1902 struct NeighborSendContext *send_context;
1903 struct FastGossipNeighborList *about_list;
1904 struct FastGossipNeighborList *prev_about;
1906 send_context = direct->send_context;
1908 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
1909 GNUNET_SCHEDULER_cancel(sched, send_context->task);
1911 about_list = send_context->fast_gossip_list_head;
1912 while (about_list != NULL)
1914 GNUNET_CONTAINER_DLL_remove(send_context->fast_gossip_list_head, send_context->fast_gossip_list_tail, about_list);
1915 prev_about = about_list;
1916 about_list = about_list->next;
1917 GNUNET_free(prev_about);
1919 GNUNET_free(send_context);
1920 GNUNET_free(direct);
1924 * Multihashmap iterator for sending out disconnect messages
1927 * @param cls the peer that was disconnected
1928 * @param key key value stored under
1929 * @param value the direct neighbor to send disconnect to
1931 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1933 static int schedule_disconnect_messages (void *cls,
1934 const GNUNET_HashCode * key,
1937 struct DisconnectContext *disconnect_context = cls;
1938 struct DirectNeighbor *disconnected = disconnect_context->direct;
1939 struct DirectNeighbor *notify = value;
1940 struct PendingMessage *pending_message;
1941 p2p_dv_MESSAGE_Disconnect *disconnect_message;
1943 if (memcmp(¬ify->identity, &disconnected->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
1944 return GNUNET_YES; /* Don't send disconnect message to peer that disconnected! */
1946 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_Disconnect));
1947 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1948 pending_message->importance = default_dv_priority;
1949 pending_message->timeout = GNUNET_TIME_relative_get_forever();
1950 memcpy(&pending_message->recipient, ¬ify->identity, sizeof(struct GNUNET_PeerIdentity));
1951 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_Disconnect);
1952 disconnect_message = (p2p_dv_MESSAGE_Disconnect *)pending_message->msg;
1953 disconnect_message->header.size = htons (sizeof (p2p_dv_MESSAGE_Disconnect));
1954 disconnect_message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
1955 disconnect_message->peer_id = htonl(disconnect_context->distant->our_id);
1957 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1962 if (core_transmit_handle == NULL)
1963 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);
1969 * Multihashmap iterator for freeing extended neighbors.
1972 * @param key key value stored under
1973 * @param value the distant neighbor to be freed
1975 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1977 static int free_extended_neighbors (void *cls,
1978 const GNUNET_HashCode * key,
1981 struct DistantNeighbor *distant = value;
1982 distant_neighbor_free(distant);
1987 * Multihashmap iterator for freeing direct neighbors.
1990 * @param key key value stored under
1991 * @param value the direct neighbor to be freed
1993 * @return GNUNET_YES to continue iteration, GNUNET_NO to stop
1995 static int free_direct_neighbors (void *cls,
1996 const GNUNET_HashCode * key,
1999 struct DirectNeighbor *direct = value;
2000 direct_neighbor_free(direct);
2006 * Task run during shutdown.
2012 shutdown_task (void *cls,
2013 const struct GNUNET_SCHEDULER_TaskContext *tc)
2016 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
2017 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &print_neighbors, NULL);
2019 GNUNET_CONTAINER_multihashmap_iterate(extended_neighbors, &free_extended_neighbors, NULL);
2020 GNUNET_CONTAINER_multihashmap_destroy(extended_neighbors);
2021 GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &free_direct_neighbors, NULL);
2022 GNUNET_CONTAINER_multihashmap_destroy(direct_neighbors);
2024 GNUNET_CONTAINER_heap_destroy(neighbor_max_heap);
2025 GNUNET_CONTAINER_heap_destroy(neighbor_min_heap);
2027 GNUNET_CORE_disconnect (coreAPI);
2028 GNUNET_PEERINFO_disconnect(peerinfo_handle);
2029 GNUNET_SERVER_mst_destroy(coreMST);
2030 GNUNET_free_non_null(my_short_id);
2032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
2037 * To be called on core init/fail.
2039 void core_init (void *cls,
2040 struct GNUNET_CORE_Handle * server,
2041 const struct GNUNET_PeerIdentity *identity,
2042 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
2047 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
2048 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
2055 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
2056 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
2061 #if PKEY_NO_NEIGHBOR_ON_ADD
2063 * Iterator over hash map entries.
2065 * @param cls closure
2066 * @param key current key code
2067 * @param value value in the hash map
2068 * @return GNUNET_YES if we should continue to
2072 static int add_pkey_to_extended (void *cls,
2073 const GNUNET_HashCode * key,
2076 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
2077 struct DistantNeighbor *distant_neighbor = value;
2079 if (distant_neighbor->pkey == NULL)
2081 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2082 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2090 * Iterator over hash map entries.
2092 * @param cls closure
2093 * @param key current key code
2094 * @param value value in the hash map
2095 * @return GNUNET_YES if we should continue to
2099 static int update_matching_neighbors (void *cls,
2100 const GNUNET_HashCode * key,
2103 struct NeighborUpdateInfo * update_info = cls;
2104 struct DistantNeighbor *distant_neighbor = value;
2106 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
2108 /* same referrer, cost change! */
2109 GNUNET_CONTAINER_heap_update_cost (neighbor_max_heap,
2110 update_info->neighbor->max_loc, update_info->cost);
2111 GNUNET_CONTAINER_heap_update_cost (neighbor_min_heap,
2112 update_info->neighbor->min_loc, update_info->cost);
2113 update_info->neighbor->last_activity = update_info->now;
2114 update_info->neighbor->cost = update_info->cost;
2115 update_info->neighbor->referrer_id = update_info->referrer_peer_id;
2124 * Iterate over all current direct peers, add DISTANT newly connected
2125 * peer to the fast gossip list for that peer so we get DV routing
2126 * information out as fast as possible!
2128 * @param cls the newly connected neighbor we will gossip about
2129 * @param key the hashcode of the peer
2130 * @param value the direct neighbor we should gossip to
2132 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2134 static int add_distant_all_direct_neighbors (void *cls,
2135 const GNUNET_HashCode * key,
2138 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2139 struct DistantNeighbor *distant = (struct DistantNeighbor *)cls;
2140 struct NeighborSendContext *send_context = direct->send_context;
2141 struct FastGossipNeighborList *gossip_entry;
2147 if (distant == NULL)
2152 if (memcmp(&direct->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2154 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2158 if (distant->hidden == GNUNET_YES)
2159 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2161 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2162 gossip_entry->about = distant;
2164 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2165 send_context->fast_gossip_list_tail,
2166 send_context->fast_gossip_list_tail,
2169 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&distant->identity));
2170 encPeerTo = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Fast send info about peer %s id %u for directly connected peer %s\n",
2173 GNUNET_i2s(&my_identity),
2174 encPeerAbout, distant->our_id, encPeerTo);
2175 GNUNET_free(encPeerAbout);
2176 GNUNET_free(encPeerTo);
2178 /*if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2179 GNUNET_SCHEDULER_cancel(sched, send_context->task);*/
2181 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2186 * Callback for hello address creation.
2188 * @param cls closure, a struct HelloContext
2189 * @param max maximum number of bytes that can be written to buf
2190 * @param buf where to write the address information
2192 * @return number of bytes written, 0 to signal the
2193 * end of the iteration.
2196 generate_hello_address (void *cls, size_t max, void *buf)
2198 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 addr1 = GNUNET_strdup(GNUNET_i2s(hello_context->direct_peer));
2221 addr2 = GNUNET_strdup(GNUNET_i2s(&hello_context->distant_peer));
2222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: GIVING HELLO for %s via %s to TRANSPORT\n", my_short_id, addr2, addr1);
2225 ret = GNUNET_HELLO_add_address ("dv",
2226 GNUNET_TIME_relative_to_absolute
2227 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
2230 hello_context->addresses_to_add--;
2232 GNUNET_free(addr_buffer);
2238 * Handles when a peer is either added due to being newly connected
2239 * or having been gossiped about, also called when the cost for a neighbor
2240 * needs to be updated.
2242 * @param peer identity of the peer whose info is being added/updated
2243 * @param pkey public key of the peer whose info is being added/updated
2244 * @param referrer_peer_id id to use when sending to 'peer'
2245 * @param referrer if this is a gossiped peer, who did we hear it from?
2246 * @param cost the cost of communicating with this peer via 'referrer'
2248 * @return the added neighbor, the updated neighbor or NULL (neighbor
2251 static struct DistantNeighbor *
2252 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
2253 unsigned int referrer_peer_id,
2254 struct DirectNeighbor *referrer, unsigned int cost)
2256 struct DistantNeighbor *neighbor;
2257 struct DistantNeighbor *max;
2258 struct GNUNET_TIME_Absolute now;
2259 struct NeighborUpdateInfo *neighbor_update;
2260 struct HelloContext *hello_context;
2261 struct GNUNET_HELLO_Message *hello_msg;
2262 unsigned int our_id;
2264 #if DEBUG_DV_PEER_NUMBERS
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2267 "%s Received sender id (%u)!\n", "DV SERVICE", referrer_peer_id);
2270 now = GNUNET_TIME_absolute_get ();
2271 neighbor = GNUNET_CONTAINER_multihashmap_get (extended_neighbors,
2273 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
2274 neighbor_update->neighbor = neighbor;
2275 neighbor_update->cost = cost;
2276 neighbor_update->now = now;
2277 neighbor_update->referrer = referrer;
2278 neighbor_update->referrer_peer_id = referrer_peer_id;
2280 if (neighbor != NULL)
2283 memcpy(&our_id, &neighbor->identity, sizeof(unsigned int));
2285 our_id = neighbor->our_id;
2291 memcpy(&our_id, peer, sizeof(unsigned int));
2293 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, RAND_MAX - 1) + 1;
2297 /* Either we do not know this peer, or we already do but via a different immediate peer */
2298 if ((neighbor == NULL) ||
2299 (GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2301 &update_matching_neighbors,
2302 neighbor_update) != GNUNET_SYSERR))
2306 if ((neighbor != NULL) && (cost < neighbor->cost)) /* New cost is less than old, remove old */
2308 distant_neighbor_free(neighbor);
2310 else if (neighbor != NULL) /* Only allow one DV connection to each peer */
2316 if (cost > fisheye_depth)
2319 GNUNET_free(neighbor_update);
2323 #if DEBUG_DV_PEER_NUMBERS
2324 encAbout = GNUNET_strdup(GNUNET_i2s(peer));
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "%s: %s Chose NEW id (%u) for peer %s!\n", GNUNET_i2s(&my_identity), "DV SERVICE", our_id, encAbout);
2327 GNUNET_free(encAbout);
2330 if (max_table_size <=
2331 GNUNET_CONTAINER_multihashmap_size (extended_neighbors))
2333 /* remove most expensive entry */
2334 max = GNUNET_CONTAINER_heap_peek (neighbor_max_heap);
2335 GNUNET_assert(max != NULL);
2336 if (cost > max->cost)
2338 /* new entry most expensive, don't create */
2339 GNUNET_free(neighbor_update);
2344 /* only free if this is not a direct connection;
2345 we could theoretically have more direct
2346 connections than DV entries allowed total! */
2347 distant_neighbor_free (max);
2351 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
2352 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
2353 referrer->referee_tail, neighbor);
2354 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (neighbor_max_heap,
2356 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (neighbor_min_heap,
2358 neighbor->referrer = referrer;
2359 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2360 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
2362 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2363 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2366 neighbor->pkey = pkey;
2368 neighbor->last_activity = now;
2369 neighbor->cost = cost;
2370 neighbor->referrer_id = referrer_peer_id;
2371 neighbor->our_id = our_id;
2373 (cost == DIRECT_NEIGHBOR_COST) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
2376 GNUNET_CONTAINER_multihashmap_put (extended_neighbors, &peer->hashPubKey,
2378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2380 if (cost != DIRECT_NEIGHBOR_COST)
2382 /* Added neighbor, now send HELLO to transport */
2383 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
2384 hello_context->direct_peer = &referrer->identity;
2385 memcpy(&hello_context->distant_peer, peer, sizeof(struct GNUNET_PeerIdentity));
2386 hello_context->addresses_to_add = 1;
2387 hello_msg = GNUNET_HELLO_create(pkey, &generate_hello_address, hello_context);
2388 GNUNET_assert(memcmp(hello_context->direct_peer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)) != 0);
2389 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, cost);
2390 GNUNET_free(hello_context);
2391 GNUNET_free(hello_msg);
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2399 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(extended_neighbors));
2407 GNUNET_free(neighbor_update);
2413 * Core handler for dv disconnect messages. These will be used
2414 * by us to tell transport via the dv plugin that a peer can
2415 * no longer be contacted by us via a certain address. We should
2416 * then propagate these messages on, given that the distance to
2417 * the peer indicates we would have gossiped about it to others.
2419 * @param cls closure
2420 * @param peer peer which sent the message (immediate sender)
2421 * @param message the message
2422 * @param latency the latency of the connection we received the message from
2423 * @param distance the distance to the immediate peer
2425 static int handle_dv_disconnect_message (void *cls,
2426 const struct GNUNET_PeerIdentity *peer,
2427 const struct GNUNET_MessageHeader *message,
2428 struct GNUNET_TIME_Relative latency,
2431 struct DirectNeighbor *referrer;
2432 struct DistantNeighbor *distant;
2433 p2p_dv_MESSAGE_Disconnect *enc_message = (p2p_dv_MESSAGE_Disconnect *)message;
2435 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_Disconnect))
2437 return GNUNET_SYSERR; /* invalid message */
2440 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2442 if (referrer == NULL)
2445 distant = referrer->referee_head;
2446 while (distant != NULL)
2448 if (distant->referrer_id == ntohl(enc_message->peer_id))
2450 distant_neighbor_free(distant);
2452 distant = referrer->referee_head;
2460 * Core handler for dv gossip messages. These will be used
2461 * by us to create a HELLO message for the newly peer containing
2462 * which direct peer we can connect through, and what the cost
2463 * is. This HELLO will then be scheduled for validation by the
2464 * transport service so that it can be used by all others.
2466 * @param cls closure
2467 * @param peer peer which sent the message (immediate sender)
2468 * @param message the message
2469 * @param latency the latency of the connection we received the message from
2470 * @param distance the distance to the immediate peer
2472 static int handle_dv_gossip_message (void *cls,
2473 const struct GNUNET_PeerIdentity *peer,
2474 const struct GNUNET_MessageHeader *message,
2475 struct GNUNET_TIME_Relative latency,
2478 struct DirectNeighbor *referrer;
2479 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
2481 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
2483 return GNUNET_SYSERR; /* invalid message */
2486 #if DEBUG_DV_GOSSIP_RECEIPT
2487 char * encPeerAbout;
2490 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
2491 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2493 "%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);
2494 GNUNET_free(encPeerAbout);
2495 GNUNET_free(encPeerFrom);
2498 referrer = GNUNET_CONTAINER_multihashmap_get (direct_neighbors,
2500 if (referrer == NULL)
2503 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
2504 ntohl (enc_message->neighbor_id),
2505 referrer, ntohl (enc_message->cost) + 1);
2512 * Iterate over all currently known peers, add them to the
2513 * fast gossip list for this peer so we get DV routing information
2514 * out as fast as possible!
2516 * @param cls the direct neighbor we will gossip to
2517 * @param key the hashcode of the peer
2518 * @param value the distant neighbor we should add to the list
2520 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2522 static int add_all_extended_peers (void *cls,
2523 const GNUNET_HashCode * key,
2526 struct NeighborSendContext *send_context = (struct NeighborSendContext *)cls;
2527 struct DistantNeighbor *distant = (struct DistantNeighbor *)value;
2528 struct FastGossipNeighborList *gossip_entry;
2530 if (memcmp(&send_context->toNeighbor->identity, &distant->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2531 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2534 if (distant->hidden == GNUNET_YES)
2535 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2537 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2538 gossip_entry->about = distant;
2540 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2541 send_context->fast_gossip_list_tail,
2542 send_context->fast_gossip_list_tail,
2550 * Iterator over hash map entries.
2552 * @param cls closure
2553 * @param key current key code
2554 * @param value value in the hash map
2555 * @return GNUNET_YES if we should continue to
2559 static int gossip_all_to_all_iterator (void *cls,
2560 const GNUNET_HashCode * key,
2563 struct DirectNeighbor *direct = value;
2565 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, direct->send_context);
2567 if (direct->send_context->task != GNUNET_SCHEDULER_NO_TASK)
2568 GNUNET_SCHEDULER_cancel(sched, direct->send_context->task);
2570 direct->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, direct->send_context);
2575 * Task run during shutdown.
2581 gossip_all_to_all (void *cls,
2582 const struct GNUNET_SCHEDULER_TaskContext *tc)
2584 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &gossip_all_to_all_iterator, NULL);
2586 GNUNET_SCHEDULER_add_delayed (sched,
2587 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2594 * Iterate over all current direct peers, add newly connected peer
2595 * to the fast gossip list for that peer so we get DV routing
2596 * information out as fast as possible!
2598 * @param cls the newly connected neighbor we will gossip about
2599 * @param key the hashcode of the peer
2600 * @param value the direct neighbor we should gossip to
2602 * @return GNUNET_YES to continue iteration, GNUNET_NO otherwise
2604 static int add_all_direct_neighbors (void *cls,
2605 const GNUNET_HashCode * key,
2608 struct DirectNeighbor *direct = (struct DirectNeighbor *)value;
2609 struct DirectNeighbor *to = (struct DirectNeighbor *)cls;
2610 struct DistantNeighbor *distant;
2611 struct NeighborSendContext *send_context = direct->send_context;
2612 struct FastGossipNeighborList *gossip_entry;
2616 distant = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &to->identity.hashPubKey);
2617 if (distant == NULL)
2622 if (memcmp(&direct->identity, &to->identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
2624 return GNUNET_YES; /* Don't gossip to a peer about itself! */
2628 if (distant->hidden == GNUNET_YES)
2629 return GNUNET_YES; /* This peer should not be gossipped about (hidden) */
2631 direct_id = GNUNET_strdup(GNUNET_i2s(&direct->identity));
2633 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);
2635 GNUNET_free(direct_id);
2636 gossip_entry = GNUNET_malloc(sizeof(struct FastGossipNeighborList));
2637 gossip_entry->about = distant;
2639 GNUNET_CONTAINER_DLL_insert_after(send_context->fast_gossip_list_head,
2640 send_context->fast_gossip_list_tail,
2641 send_context->fast_gossip_list_tail,
2643 if (send_context->task != GNUNET_SCHEDULER_NO_TASK)
2644 GNUNET_SCHEDULER_cancel(sched, send_context->task);
2646 send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, send_context);
2647 //tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
2648 //neighbor_send_task(send_context, &tc);
2653 * Type of an iterator over the hosts. Note that each
2654 * host will be called with each available protocol.
2656 * @param cls closure
2657 * @param peer id of the peer, NULL for last call
2658 * @param hello hello message for the peer (can be NULL)
2659 * @param trust amount of trust we have in the peer
2662 process_peerinfo (void *cls,
2663 const struct GNUNET_PeerIdentity *peer,
2664 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
2666 struct PeerIteratorContext *peerinfo_iterator = cls;
2667 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
2668 struct DistantNeighbor *distant = peerinfo_iterator->distant;
2669 #if DEBUG_DV_PEER_NUMBERS
2674 if (peer == NULL) /* && (neighbor->pkey == NULL))*/
2676 if (distant->pkey == NULL) /* FIXME: Reschedule? */
2679 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to get peerinfo information for this peer, retrying!\n");
2681 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate(peerinfo_handle,
2682 &peerinfo_iterator->neighbor->identity,
2684 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2690 GNUNET_free(peerinfo_iterator);
2695 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
2698 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
2700 if (distant->pkey == NULL)
2702 distant->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2703 memcpy(distant->pkey, &neighbor->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2706 /* Why do it this way, now we have the distant neighbor! */
2707 /*GNUNET_CONTAINER_multihashmap_get_multiple(extended_neighbors,
2709 &add_pkey_to_extended,
2712 sent = GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &add_all_extended_peers, neighbor->send_context);
2714 #if DEBUG_DV_PEER_NUMBERS
2715 neighbor_pid = GNUNET_strdup(GNUNET_i2s(&neighbor->identity));
2716 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped %d extended peers to %s\n", GNUNET_i2s(&my_identity), sent, neighbor_pid);
2718 sent = GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &add_all_direct_neighbors, neighbor);
2719 #if DEBUG_DV_PEER_NUMBERS
2720 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Gossipped about %s to %d direct peers\n", GNUNET_i2s(&my_identity), neighbor_pid, sent);
2721 GNUNET_free(neighbor_pid);
2723 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
2729 * Method called whenever a peer connects.
2731 * @param cls closure
2732 * @param peer peer identity this notification is about
2733 * @param latency reported latency of the connection with peer
2734 * @param distance reported distance (DV) to peer
2736 void handle_core_connect (void *cls,
2737 const struct GNUNET_PeerIdentity * peer,
2738 struct GNUNET_TIME_Relative latency,
2741 struct DirectNeighbor *neighbor;
2742 struct DistantNeighbor *about;
2743 struct PeerIteratorContext *peerinfo_iterator;
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2747 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
2750 if ((distance == DIRECT_NEIGHBOR_COST) && (GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL))
2752 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
2753 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
2754 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
2755 neighbor->send_context->toNeighbor = neighbor;
2756 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
2758 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (direct_neighbors,
2760 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2761 about = addUpdateNeighbor (peer, NULL, 0, neighbor, DIRECT_NEIGHBOR_COST);
2762 peerinfo_iterator->distant = about;
2763 peerinfo_iterator->neighbor = neighbor;
2764 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (peerinfo_handle,
2767 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
2771 if ((about != NULL) && (about->pkey == NULL))
2774 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Newly added peer %s has NULL pkey!\n", GNUNET_i2s(peer));
2777 else if (about != NULL)
2779 GNUNET_free(peerinfo_iterator);
2784 about = GNUNET_CONTAINER_multihashmap_get(extended_neighbors, &peer->hashPubKey);
2785 if ((GNUNET_CONTAINER_multihashmap_get(direct_neighbors, &peer->hashPubKey) == NULL) && (about != NULL))
2786 sent = GNUNET_CONTAINER_multihashmap_iterate(direct_neighbors, &add_distant_all_direct_neighbors, about);
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 "%s: Distance (%d) greater than %d or already know about peer (%s), not re-adding!\n", "dv", distance, DIRECT_NEIGHBOR_COST, GNUNET_i2s(peer));
2796 * Method called whenever a given peer disconnects.
2798 * @param cls closure
2799 * @param peer peer identity this notification is about
2801 void handle_core_disconnect (void *cls,
2802 const struct GNUNET_PeerIdentity * peer)
2804 struct DirectNeighbor *neighbor;
2805 struct DistantNeighbor *referee;
2806 struct FindDestinationContext fdc;
2807 struct DisconnectContext disconnect_context;
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2811 "%s: Receives core peer disconnect message!\n", "dv");
2815 GNUNET_CONTAINER_multihashmap_get (direct_neighbors, &peer->hashPubKey);
2816 if (neighbor == NULL)
2820 while (NULL != (referee = neighbor->referee_head))
2821 distant_neighbor_free (referee);
2826 GNUNET_CONTAINER_multihashmap_iterate (extended_neighbors, &find_distant_peer, &fdc);
2828 if (fdc.dest != NULL)
2830 disconnect_context.direct = neighbor;
2831 disconnect_context.distant = fdc.dest;
2832 GNUNET_CONTAINER_multihashmap_iterate (direct_neighbors, &schedule_disconnect_messages, &disconnect_context);
2835 GNUNET_assert (neighbor->referee_tail == NULL);
2836 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (direct_neighbors,
2837 &peer->hashPubKey, neighbor))
2841 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
2842 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
2843 GNUNET_free (neighbor);
2848 * Process dv requests.
2850 * @param cls closure
2851 * @param scheduler scheduler to use
2852 * @param server the initialized server
2853 * @param c configuration to use
2857 struct GNUNET_SCHEDULER_Handle *scheduler,
2858 struct GNUNET_SERVER_Handle *server,
2859 const struct GNUNET_CONFIGURATION_Handle *c)
2861 unsigned long long max_hosts;
2865 /* FIXME: Read from config, or calculate, or something other than this! */
2866 max_hosts = DEFAULT_DIRECT_CONNECTIONS;
2867 max_table_size = DEFAULT_DV_SIZE;
2868 fisheye_depth = DEFAULT_FISHEYE_DEPTH;
2870 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_direct_connections"))
2871 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_direct_connections", &max_hosts));
2873 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "max_total_connections"))
2874 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "max_total_connections", &max_table_size));
2877 if (GNUNET_CONFIGURATION_have_value(cfg, "dv", "fisheye_depth"))
2878 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "dv", "fisheye_depth", &fisheye_depth));
2881 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2883 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
2885 direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
2886 extended_neighbors =
2887 GNUNET_CONTAINER_multihashmap_create (max_table_size * 3);
2889 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2891 GNUNET_CORE_connect (sched,
2893 GNUNET_TIME_relative_get_forever(),
2894 NULL, /* FIXME: anything we want to pass around? */
2896 &handle_core_connect,
2897 &handle_core_disconnect,
2904 if (coreAPI == NULL)
2907 coreMST = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE,
2908 &tokenized_message_handler,
2911 peerinfo_handle = GNUNET_PEERINFO_connect(sched, cfg);
2913 if (peerinfo_handle == NULL)
2915 GNUNET_CORE_disconnect(coreAPI);
2919 /* Scheduled the task to clean up when shutdown is called */
2920 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
2921 GNUNET_TIME_UNIT_FOREVER_REL,
2925 GNUNET_SCHEDULER_add_delayed (sched,
2926 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
2934 * The main function for the dv service.
2936 * @param argc number of arguments from the command line
2937 * @param argv command line arguments
2938 * @return 0 ok, 1 on error
2941 main (int argc, char *const *argv)
2943 return (GNUNET_OK ==
2944 GNUNET_SERVICE_run (argc,
2947 GNUNET_SERVICE_OPTION_NONE,
2948 &run, NULL)) ? 0 : 1;