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: Currently the final hop of a DV message assigns a 0 to the receiver
31 * id field. This probably can't work(!) even if we know that the peer is
32 * a direct neighbor (unless we can trust that transport will choose that
33 * address for the peer). So the DV message will likely need to have the
34 * peer identity of the recipient.
36 * Also the gossip rates need to be worked out. Probably many other things
41 #include "gnunet_client_lib.h"
42 #include "gnunet_getopt_lib.h"
43 #include "gnunet_os_lib.h"
44 #include "gnunet_protocols.h"
45 #include "gnunet_service_lib.h"
46 #include "gnunet_core_service.h"
47 #include "gnunet_signal_lib.h"
48 #include "gnunet_util_lib.h"
49 #include "gnunet_hello_lib.h"
50 #include "gnunet_peerinfo_service.h"
51 #include "gnunet_crypto_lib.h"
55 * DV Service Context stuff goes here...
59 * Handle to the core service api.
61 static struct GNUNET_CORE_Handle *coreAPI;
64 * The identity of our peer.
66 static struct GNUNET_PeerIdentity my_identity;
69 * The configuration for this service.
71 static const struct GNUNET_CONFIGURATION_Handle *cfg;
74 * The scheduler for this service.
76 static struct GNUNET_SCHEDULER_Handle *sched;
79 * How often do we check about sending out more peer information (if
80 * we are connected to no peers previously).
82 #define GNUNET_DV_DEFAULT_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500))
85 * How long do we wait at most between sending out information?
87 #define GNUNET_DV_MAX_SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
90 * How long can we have not heard from a peer and
91 * still have it in our tables?
93 #define GNUNET_DV_PEER_EXPIRATION_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1000))
96 * Priority for gossip.
98 #define GNUNET_DV_DHT_GOSSIP_PRIORITY (GNUNET_EXTREME_PRIORITY / 10)
101 * How often should we check if expiration time has elapsed for
104 #define GNUNET_DV_MAINTAIN_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
107 * How long to allow a message to be delayed?
109 #define DV_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5))
112 * Priority to use for DV data messages.
114 #define DV_PRIORITY 0
117 * The client, should be the DV plugin connected to us. Hopefully
118 * this client will never change, although if the plugin dies
119 * and returns for some reason it may happen.
121 static struct GNUNET_SERVER_Client * client_handle;
124 * Task to run when we shut down, cleaning up all our trash
126 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
129 * Task to run to gossip about peers. Will reschedule itself forever until shutdown!
131 GNUNET_SCHEDULER_TaskIdentifier gossip_task;
134 * Struct where neighbor information is stored.
136 struct DistantNeighbor *referees;
138 static struct GNUNET_TIME_Relative client_transmit_timeout;
140 static struct GNUNET_TIME_Relative default_dv_delay;
142 static size_t default_dv_priority = 0;
146 * Linked list of messages to send to clients.
148 struct PendingMessage
151 * Pointer to next item in the list
153 struct PendingMessage *next;
156 * Pointer to previous item in the list
158 struct PendingMessage *prev;
161 * Actual message to be sent; // avoid allocation
163 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
168 * Transmit handle to the plugin.
170 struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
173 * Head of DLL for client messages
175 struct PendingMessage *plugin_pending_head;
178 * Tail of DLL for client messages
180 struct PendingMessage *plugin_pending_tail;
184 * Transmit handle to core service.
186 struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
189 * Head of DLL for core messages
191 struct PendingMessage *core_pending_head;
194 * Tail of DLL for core messages
196 struct PendingMessage *core_pending_tail;
202 * Context created whenever a direct peer connects to us,
203 * used to gossip other peers to it.
205 struct NeighborSendContext
208 * The peer we will gossip to.
210 struct DirectNeighbor *toNeighbor;
213 * The timeout for this task.
215 struct GNUNET_TIME_Relative timeout;
218 * The task associated with this context.
220 GNUNET_SCHEDULER_TaskIdentifier task;
226 * Struct to hold information for updating existing neighbors
228 struct NeighborUpdateInfo
236 * The existing neighbor
238 struct DistantNeighbor *neighbor;
241 * The referrer of the possibly existing peer
243 struct DirectNeighbor *referrer;
246 * The time we heard about this peer
248 struct GNUNET_TIME_Absolute now;
252 * Struct where actual neighbor information is stored,
253 * referenced by min_heap and max_heap. Freeing dealt
254 * with when items removed from hashmap.
256 struct DirectNeighbor
259 * Identity of neighbor.
261 struct GNUNET_PeerIdentity identity;
264 * PublicKey of neighbor.
266 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
269 * Head of DLL of nodes that this direct neighbor referred to us.
271 struct DistantNeighbor *referee_head;
274 * Tail of DLL of nodes that this direct neighbor referred to us.
276 struct DistantNeighbor *referee_tail;
279 * The sending context for gossiping peers to this neighbor.
281 struct NeighborSendContext *send_context;
284 * Is this one of the direct neighbors that we are "hiding"
292 * Struct where actual neighbor information is stored,
293 * referenced by min_heap and max_heap. Freeing dealt
294 * with when items removed from hashmap.
296 struct DistantNeighbor
299 * We keep distant neighbor's of the same referrer in a DLL.
301 struct DistantNeighbor *next;
304 * We keep distant neighbor's of the same referrer in a DLL.
306 struct DistantNeighbor *prev;
311 struct GNUNET_CONTAINER_HeapNode *min_loc;
316 struct GNUNET_CONTAINER_HeapNode *max_loc;
319 * Identity of referrer (next hop towards 'neighbor').
321 struct DirectNeighbor *referrer;
324 * Identity of neighbor.
326 struct GNUNET_PeerIdentity identity;
329 * PublicKey of neighbor.
331 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
334 * Last time we received routing information from this peer
336 struct GNUNET_TIME_Absolute last_activity;
339 * Cost to neighbor, used for actual distance vector computations
344 * Random identifier *we* use for this peer, to be used as shortcut
345 * instead of sending full peer id for each message
350 * Random identifier the *referrer* uses for this peer.
352 unsigned int referrer_id;
355 * Is this one of the direct neighbors that we are "hiding"
362 struct PeerIteratorContext
365 * The actual context, to be freed later.
367 struct GNUNET_PEERINFO_IteratorContext *ic;
370 * The neighbor about which we are concerned.
372 struct DirectNeighbor *neighbor;
377 * Context used for creating hello messages when
378 * gossips are received.
383 * Identity of distant neighbor.
385 struct GNUNET_PeerIdentity distant_peer;
388 * Identity of direct neighbor, via which we send this message.
390 const struct GNUNET_PeerIdentity *direct_peer;
393 * How many addresses do we need to add (always starts at 1, then set to 0)
395 int addresses_to_add;
399 struct DV_SendContext
402 * The distant peer (should always match)
404 struct GNUNET_PeerIdentity *distant_peer;
407 * The direct peer, we need to verify the referrer of.
409 struct GNUNET_PeerIdentity *direct_peer;
412 * The message to be sent
414 struct GNUNET_MessageHeader *message;
417 * The size of the message being sent, may be larger
418 * than message->header.size because it's multiple
419 * messages packed into one!
424 * How important is this message?
426 unsigned int importance;
429 * Timeout for this message
431 struct GNUNET_TIME_Relative timeout;
437 struct GNUNET_DV_Context
440 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for all
441 * directly connected peers.
443 struct GNUNET_CONTAINER_MultiHashMap *direct_neighbors;
446 * Map of PeerIdentifiers to 'struct GNUNET_dv_neighbor*'s for
447 * peers connected via DV (extended neighborhood). Does ALSO
448 * include any peers that are in 'direct_neighbors'; for those
449 * peers, the cost will be zero and the referrer all zeros.
451 struct GNUNET_CONTAINER_MultiHashMap *extended_neighbors;
454 * We use the min heap (min refers to cost) to prefer
455 * gossipping about peers with small costs.
457 struct GNUNET_CONTAINER_Heap *neighbor_min_heap;
460 * We use the max heap (max refers to cost) for general
461 * iterations over all peers and to remove the most costly
462 * connection if we have too many.
464 struct GNUNET_CONTAINER_Heap *neighbor_max_heap;
466 unsigned long long fisheye_depth;
468 unsigned long long max_table_size;
470 unsigned int neighbor_id_loc;
476 static struct GNUNET_DV_Context ctx;
478 struct FindDestinationContext
481 struct DistantNeighbor *dest;
486 * We've been given a target ID based on the random numbers that
487 * we assigned to our DV-neighborhood. Find the entry for the
488 * respective neighbor.
491 find_destination (void *cls,
492 struct GNUNET_CONTAINER_HeapNode *node,
493 void *element, GNUNET_CONTAINER_HeapCostType cost)
495 struct FindDestinationContext *fdc = cls;
496 struct DistantNeighbor *dn = element;
498 if (fdc->tid != dn->our_id)
505 * Function called to notify a client about the socket
506 * begin ready to queue more data. "buf" will be
507 * NULL and "size" zero if the socket was closed for
508 * writing in the meantime.
511 * @param size number of bytes available in buf
512 * @param buf where the callee should write the message
513 * @return number of bytes written to buf
515 size_t transmit_to_plugin (void *cls,
516 size_t size, void *buf)
519 struct PendingMessage *reply;
525 /* client disconnected */
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
531 plugin_transmit_handle = NULL;
533 while ( (NULL != (reply = plugin_pending_head)) &&
534 (size >= off + (msize = ntohs (reply->msg->size))))
537 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (plugin) called with size %d\n", "dv service", msize);
539 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
542 memcpy (&cbuf[off], reply->msg, msize);
547 if (plugin_pending_head != NULL)
548 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
549 ntohs(plugin_pending_head->msg->size),
550 GNUNET_TIME_UNIT_FOREVER_REL,
551 &transmit_to_plugin, NULL);
557 void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
558 const struct GNUNET_MessageHeader *message,
560 struct GNUNET_PeerIdentity *distant_neighbor,
563 struct GNUNET_DV_MessageReceived *received_msg;
564 struct PendingMessage *pending_message;
566 struct GNUNET_MessageHeader * packed_message_header;
567 struct GNUNET_HELLO_Message *hello_msg;
568 struct GNUNET_PeerIdentity hello_identity;
570 char *sender_address;
571 size_t sender_address_len;
572 char *packed_msg_start;
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
579 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
581 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
582 sender_address = GNUNET_malloc(sender_address_len);
583 memcpy(sender_address, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
584 memcpy(&sender_address[sizeof(struct GNUNET_PeerIdentity)], sender, sizeof(struct GNUNET_PeerIdentity));
588 sender_address_len = sizeof(struct GNUNET_PeerIdentity);
589 sender_address = GNUNET_malloc(sender_address_len);
590 memcpy(sender_address, sender, sizeof(struct GNUNET_PeerIdentity));
593 size = sizeof(struct GNUNET_DV_MessageReceived) + sender_address_len + message_size;
594 received_msg = GNUNET_malloc(size);
595 received_msg->header.size = htons(size);
596 received_msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
597 received_msg->sender_address_len = htons(sender_address_len);
598 received_msg->distance = htonl(cost);
599 received_msg->msg_len = htons(message_size);
600 /* Set the sender in this message to be the original sender! */
601 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
602 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
603 memcpy(&received_msg[1], sender_address, sender_address_len);
604 GNUNET_free(sender_address);
605 /* Copy the actual message after the sender */
606 packed_msg_start = (char *)&received_msg[1];
607 packed_msg_start = &packed_msg_start[sender_address_len];
608 memcpy(packed_msg_start, message, message_size);
610 packed_message_header = (struct GNUNET_MessageHeader *)packed_msg_start;
611 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, ntohs(received_msg->msg_len), ntohs(received_msg->header.size));
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dv packed message len %d, type %d\n", ntohs(packed_message_header->size), ntohs(packed_message_header->type));
613 if (ntohs(packed_message_header->type) == GNUNET_MESSAGE_TYPE_HELLO)
615 hello_msg = (struct GNUNET_HELLO_Message *)packed_message_header;
616 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(hello_msg, &hello_identity));
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity));
620 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
621 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
622 memcpy(&pending_message[1], received_msg, size);
623 GNUNET_free(received_msg);
625 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
627 if (client_handle != NULL)
629 if (plugin_transmit_handle == NULL)
631 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
632 size, GNUNET_TIME_UNIT_FOREVER_REL,
633 &transmit_to_plugin, NULL);
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to queue message for plugin, must be one in progress already!!\n");
644 * Function called to notify a client about the socket
645 * begin ready to queue more data. "buf" will be
646 * NULL and "size" zero if the socket was closed for
647 * writing in the meantime.
650 * @param size number of bytes available in buf
651 * @param buf where the callee should write the message
652 * @return number of bytes written to buf
654 size_t core_transmit_notify (void *cls,
655 size_t size, void *buf)
658 struct PendingMessage *reply;
664 /* client disconnected */
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
671 core_transmit_handle = NULL;
673 while ( (NULL != (reply = core_pending_head)) &&
674 (size >= off + (msize = ntohs (reply->msg->size))))
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
679 GNUNET_CONTAINER_DLL_remove (core_pending_head,
682 memcpy (&cbuf[off], reply->msg, msize);
691 * Send a DV data message via DV.
693 * @param sender the original sender of the message
694 * @param specific_neighbor the specific DistantNeighbor to use, complete with referrer!
695 * @param send_context the send context
698 send_message_via (const struct GNUNET_PeerIdentity * sender,
699 const struct DistantNeighbor * specific_neighbor,
700 struct DV_SendContext *send_context)
702 p2p_dv_MESSAGE_Data *toSend;
703 unsigned int msg_size;
705 unsigned int recipient_id;
706 unsigned int sender_id;
707 struct DistantNeighbor *source;
708 struct PendingMessage *pending_message;
713 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
715 if (specific_neighbor == NULL)
717 /* target unknown to us, drop! */
718 return GNUNET_SYSERR;
720 recipient_id = specific_neighbor->referrer_id;
722 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
723 &sender->hashPubKey);
726 if (0 != (memcmp (&my_identity,
727 sender, sizeof (struct GNUNET_PeerIdentity))))
729 /* sender unknown to us, drop! */
730 return GNUNET_SYSERR;
732 sender_id = 0; /* 0 == us */
736 /* find out the number that we use when we gossip about
738 sender_id = source->our_id;
741 cost = specific_neighbor->cost;
742 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
743 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
744 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
745 toSend->header.size = htons (msg_size);
746 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
747 toSend->sender = htonl (sender_id);
748 toSend->recipient = htonl (recipient_id);
749 memcpy (&toSend[1], send_context->message, send_context->message_size);
752 memcpy(&shortname, GNUNET_i2s(&specific_neighbor->identity), 4);
754 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(&specific_neighbor->referrer->identity), msg_size);
757 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
761 if (core_transmit_handle == NULL)
762 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, send_context->importance, send_context->timeout, &specific_neighbor->referrer->identity, msg_size, &core_transmit_notify, NULL);
764 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
771 * Send a DV data message via DV.
773 * @param recipient the ultimate recipient of this message
774 * @param sender the original sender of the message
775 * @param specific_neighbor the specific neighbor to send this message via
776 * @param message the packed message
777 * @param message_size size of the message
778 * @param importance what priority to send this message with
779 * @param timeout how long to possibly delay sending this message
782 send_message (const struct GNUNET_PeerIdentity * recipient,
783 const struct GNUNET_PeerIdentity * sender,
784 const struct DistantNeighbor * specific_neighbor,
785 const struct GNUNET_MessageHeader * message,
787 unsigned int importance, struct GNUNET_TIME_Relative timeout)
789 p2p_dv_MESSAGE_Data *toSend;
790 unsigned int msg_size;
792 unsigned int recipient_id;
793 unsigned int sender_id;
794 struct DistantNeighbor *target;
795 struct DistantNeighbor *source;
796 struct PendingMessage *pending_message;
798 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
800 target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
801 &recipient->hashPubKey);
804 /* target unknown to us, drop! */
805 return GNUNET_SYSERR;
807 recipient_id = target->referrer_id;
809 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
810 &sender->hashPubKey);
813 if (0 != (memcmp (&my_identity,
814 sender, sizeof (struct GNUNET_PeerIdentity))))
816 /* sender unknown to us, drop! */
817 return GNUNET_SYSERR;
819 sender_id = 0; /* 0 == us */
823 /* find out the number that we use when we gossip about
825 sender_id = source->our_id;
829 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
830 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
831 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
832 toSend->header.size = htons (msg_size);
833 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
834 toSend->sender = htonl (sender_id);
835 toSend->recipient = htonl (recipient_id);
836 memcpy (&toSend[1], message, message_size);
838 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
843 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));
845 if (core_transmit_handle == NULL)
846 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
853 * Core handler for dv data messages. Whatever this message
854 * contains all we really have to do is rip it out of its
855 * DV layering and give it to our pal the DV plugin to report
859 * @param peer peer which sent the message (immediate sender)
860 * @param message the message
861 * @param latency the latency of the connection we received the message from
862 * @param distance the distance to the immediate peer
864 static int handle_dv_data_message (void *cls,
865 const struct GNUNET_PeerIdentity * peer,
866 const struct GNUNET_MessageHeader * message,
867 struct GNUNET_TIME_Relative latency,
870 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
871 const struct GNUNET_MessageHeader *packed_message;
872 struct DirectNeighbor *dn;
873 struct DistantNeighbor *pos;
874 unsigned int sid; /* Sender id */
875 unsigned int tid; /* Target id */
876 struct GNUNET_PeerIdentity original_sender;
877 struct GNUNET_PeerIdentity destination;
878 struct FindDestinationContext fdc;
880 size_t packed_message_size;
884 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "%s: Receives %s message size %d, packed message size %d!\n", "dv", "DV DATA", ntohs(incoming->header.size), packed_message_size);
890 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "`%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));
896 return GNUNET_SYSERR;
899 dn = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "%s: dn NULL!\n", "dv");
909 sid = ntohl (incoming->sender);
910 pos = dn->referee_head;
911 while ((NULL != pos) && (pos->referrer_id != sid))
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "%s: unknown sender (%d), size of extended_peers is %d!\n", "dv", ntohl(incoming->sender), GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors));
922 original_sender = pos->identity;
923 tid = ntohl (incoming->recipient);
928 cbuf = (char *)&incoming[1];
930 while(offset < packed_message_size)
932 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 "%s: Receives %s message for me, size %d type %d!\n", "dv", "DV DATA", ntohs(packed_message->size), ntohs(packed_message->type));
937 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
938 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
939 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
940 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
942 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
944 offset += ntohs(packed_message->size);
951 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
954 /* FIXME: this is the *only* per-request operation we have in DV
955 that is O(n) in relation to the number of connected peers; a
956 hash-table lookup could easily solve this (minor performance
960 GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap,
961 &find_destination, &fdc);
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
968 if (fdc.dest == NULL)
972 destination = fdc.dest->identity;
974 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
976 /* FIXME: create stat: routing loop-discard! */
978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
983 /* At this point we have a message, and we need to forward it on to the
986 /* FIXME: Can't send message on, we have to behave.
987 * We have to tell core we have a message for the next peer, and let
988 * transport do transport selection on how to get this message to 'em */
989 /*ret = send_message (&destination,
991 packed_message, DV_PRIORITY, DV_DELAY);*/
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "%s: Sends message size %d on!\n", "dv", packed_message_size);
996 ret = send_message(&destination, &original_sender, NULL, packed_message, packed_message_size, default_dv_priority, default_dv_delay);
998 if (ret != GNUNET_SYSERR)
1001 return GNUNET_SYSERR;
1006 * Thread which chooses a peer to gossip about and a peer to gossip
1007 * to, then constructs the message and sends it out. Will run until
1008 * done_module_dv is called.
1011 neighbor_send_task (void *cls,
1012 const struct GNUNET_SCHEDULER_TaskContext *tc)
1014 struct NeighborSendContext *send_context = cls;
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "%s: Entering neighbor_send_task...\n",
1018 GNUNET_i2s(&my_identity));
1019 char * encPeerAbout;
1022 struct DistantNeighbor *about;
1023 struct DirectNeighbor *to;
1025 p2p_dv_MESSAGE_NeighborInfo *message;
1026 struct PendingMessage *pending_message;
1028 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "%s: Called with reason shutdown, shutting down!\n",
1033 GNUNET_i2s(&my_identity));
1035 send_context->toNeighbor->send_context = NULL;
1036 GNUNET_free(send_context);
1041 /* FIXME: this may become a problem, because the heap walk has only one internal "walker". This means
1042 * that if two neighbor_send_tasks are operating in lockstep (which is quite possible, given default
1043 * values for all connected peers) there may be a serious bias as to which peers get gossiped about!
1044 * Probably the *best* way to fix would be to have an opaque pointer to the walk position passed as
1045 * part of the walk_get_next call. Then the heap would have to keep a list of walks, or reset the walk
1046 * whenever a modification has been detected. Yuck either way. Perhaps we could iterate over the heap
1047 * once to get a list of peers to gossip about and gossip them over time... But then if one goes away
1048 * in the mean time that becomes nasty. For now we'll just assume that the walking is done
1049 * asynchronously enough to avoid major problems (-;
1051 about = GNUNET_CONTAINER_heap_walk_get_next (ctx.neighbor_min_heap);
1052 to = send_context->toNeighbor;
1054 if ((about != NULL) && (to != about->referrer /* split horizon */ ) &&
1056 (about->hidden == GNUNET_NO) &&
1059 (0 != memcmp (&about->identity,
1060 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
1061 (about->pkey != NULL))
1064 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
1065 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "%s: Sending info about peer %s to directly connected peer %s\n",
1068 GNUNET_i2s(&my_identity),
1069 encPeerAbout, encPeerTo);
1070 GNUNET_free(encPeerAbout);
1071 GNUNET_free(encPeerTo);
1073 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
1074 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1075 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
1076 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
1077 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
1078 message->cost = htonl (about->cost);
1079 message->neighbor_id = htonl (about->our_id);
1081 memcpy (&message->pkey, about->pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1082 memcpy (&message->neighbor,
1083 &about->identity, sizeof (struct GNUNET_PeerIdentity));
1085 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
1090 if (core_transmit_handle == NULL)
1091 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, default_dv_delay, &to->identity, sizeof(p2p_dv_MESSAGE_NeighborInfo), &core_transmit_notify, NULL);
1095 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, send_context->timeout, &neighbor_send_task, send_context);
1101 * Handle START-message. This is the first message sent to us
1102 * by the client (can only be one!).
1104 * @param cls closure (always NULL)
1105 * @param client identification of the client
1106 * @param message the actual message
1109 handle_start (void *cls,
1110 struct GNUNET_SERVER_Client *client,
1111 const struct GNUNET_MessageHeader *message)
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Received `%s' request from client\n", "START");
1119 client_handle = client;
1121 GNUNET_SERVER_client_keep(client_handle);
1122 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1127 * Iterate over hash map entries for a distant neighbor,
1128 * if direct neighbor matches context call send message
1130 * @param cls closure, a DV_SendContext
1131 * @param key current key code
1132 * @param value value in the hash map
1133 * @return GNUNET_YES if we should continue to
1137 int send_iterator (void *cls,
1138 const GNUNET_HashCode * key,
1141 struct DV_SendContext *send_context = cls;
1142 struct DistantNeighbor *distant_neighbor = value;
1144 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1146 send_message_via(&my_identity, distant_neighbor, send_context);
1153 * Service server's handler for message send requests (which come
1154 * bubbling up to us through the DV plugin).
1156 * @param cls closure
1157 * @param client identification of the client
1158 * @param message the actual message
1160 void handle_dv_send_message (void *cls,
1161 struct GNUNET_SERVER_Client * client,
1162 const struct GNUNET_MessageHeader * message)
1164 struct GNUNET_DV_SendMessage *send_msg;
1166 size_t message_size;
1167 struct GNUNET_PeerIdentity *destination;
1168 struct GNUNET_PeerIdentity *direct;
1169 struct GNUNET_MessageHeader *message_buf;
1172 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1173 struct DV_SendContext *send_context;
1175 if (client_handle == NULL)
1177 client_handle = client;
1178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1179 "%s: Setting initial client handle, never received `%s' message?\n", "dv", "START");
1181 else if (client_handle != client)
1183 client_handle = client;
1184 /* What should we do in this case, assert fail or just log the warning? */
1185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1186 "%s: Setting client handle (was a different client!)!\n", "dv");
1189 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
1190 send_msg = (struct GNUNET_DV_SendMessage *)message;
1192 address_len = ntohs(send_msg->addrlen);
1193 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
1194 message_size = ntohs(send_msg->msgbuf_size);
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "%s: Receives %s message size %u!\n\n\n", "dv", "SEND", message_size);
1200 GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size);
1201 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1202 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1203 message_buf = GNUNET_malloc(message_size);
1205 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1206 offset = 0; /* Offset starts at zero */
1208 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1209 offset += sizeof(struct GNUNET_PeerIdentity);
1211 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1212 offset += sizeof(struct GNUNET_PeerIdentity);
1215 memcpy(message_buf, &temp_pos[offset], message_size);
1216 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1218 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1219 dest_hash.encoding[4] = '\0';
1220 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);
1224 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1225 dest_hash.encoding[4] = '\0';
1226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d type %d, destination `%s' via `%s'\n", message_size, ntohs(message_buf->type), (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1228 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
1230 send_context->importance = ntohs(send_msg->priority);
1231 send_context->timeout = send_msg->timeout;
1232 send_context->direct_peer = direct;
1233 send_context->distant_peer = destination;
1234 send_context->message = message_buf;
1235 send_context->message_size = message_size;
1237 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
1238 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
1240 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SEND failed to send message to destination `%s' via `%s'\n", (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
1243 GNUNET_free(message_buf);
1244 GNUNET_free(send_context);
1245 GNUNET_free(direct);
1246 GNUNET_free(destination);
1248 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1251 static int handle_dv_gossip_message (void *cls,
1252 const struct GNUNET_PeerIdentity *peer,
1253 const struct GNUNET_MessageHeader *message,
1254 struct GNUNET_TIME_Relative latency,
1258 * List of handlers for the messages understood by this
1261 * Hmm... will we need to register some handlers with core and
1262 * some handlers with our server here? Because core should be
1263 * getting the incoming DV messages (from whichever lower level
1264 * transport) and then our server should be getting messages
1265 * from the dv_plugin, right?
1267 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1268 {&handle_dv_data_message, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
1269 {&handle_dv_gossip_message, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
1273 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1274 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
1275 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
1281 * Task run during shutdown.
1287 shutdown_task (void *cls,
1288 const struct GNUNET_SCHEDULER_TaskContext *tc)
1291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
1293 GNUNET_CORE_disconnect (coreAPI);
1295 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
1300 * To be called on core init/fail.
1302 void core_init (void *cls,
1303 struct GNUNET_CORE_Handle * server,
1304 const struct GNUNET_PeerIdentity *identity,
1305 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
1310 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
1311 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 "%s: Core connection initialized, I am peer: %s\n", "dv", GNUNET_i2s(identity));
1318 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
1323 * Iterator over hash map entries.
1325 * @param cls closure
1326 * @param key current key code
1327 * @param value value in the hash map
1328 * @return GNUNET_YES if we should continue to
1332 static int add_pkey_to_extended (void *cls,
1333 const GNUNET_HashCode * key,
1336 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
1337 struct DistantNeighbor *distant_neighbor = value;
1339 if (distant_neighbor->pkey == NULL)
1341 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1342 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1349 * Iterator over hash map entries.
1351 * @param cls closure
1352 * @param key current key code
1353 * @param value value in the hash map
1354 * @return GNUNET_YES if we should continue to
1358 static int update_matching_neighbors (void *cls,
1359 const GNUNET_HashCode * key,
1362 struct NeighborUpdateInfo * update_info = cls;
1363 struct DistantNeighbor *distant_neighbor = value;
1365 if (update_info->referrer == distant_neighbor->referrer) /* Direct neighbor matches, update it's info and return GNUNET_NO */
1367 /* same referrer, cost change! */
1368 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_max_heap,
1369 update_info->neighbor->max_loc, update_info->cost);
1370 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_min_heap,
1371 update_info->neighbor->min_loc, update_info->cost);
1372 update_info->neighbor->last_activity = update_info->now;
1373 update_info->neighbor->cost = update_info->cost;
1382 * Free a DistantNeighbor node, including removing it
1383 * from the referer's list.
1386 distant_neighbor_free (struct DistantNeighbor *referee)
1388 struct DirectNeighbor *referrer;
1390 referrer = referee->referrer;
1391 if (referrer != NULL)
1393 GNUNET_CONTAINER_DLL_remove (referrer->referee_head,
1394 referrer->referee_tail, referee);
1396 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_max_heap, referee->max_loc);
1397 GNUNET_CONTAINER_heap_remove_node (ctx.neighbor_min_heap, referee->min_loc);
1398 GNUNET_CONTAINER_multihashmap_remove_all (ctx.extended_neighbors,
1399 &referee->identity.hashPubKey);
1400 GNUNET_free (referee);
1406 * Iterator over hash map entries.
1408 * @param cls closure (NULL)
1409 * @param key current key code
1410 * @param value value in the hash map (DistantNeighbor)
1411 * @return GNUNET_YES if we should continue to
1415 int print_neighbors (void *cls,
1416 const GNUNET_HashCode * key,
1419 struct DistantNeighbor *distant_neighbor = value;
1420 char my_shortname[5];
1421 char referrer_shortname[5];
1422 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1423 my_shortname[4] = '\0';
1424 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1425 referrer_shortname[4] = '\0';
1427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' %s: Peer `%s', distance %d, referrer `%s'\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname);
1434 * Handles when a peer is either added due to being newly connected
1435 * or having been gossiped about, also called when the cost for a neighbor
1436 * needs to be updated.
1438 * @param peer identity of the peer whose info is being added/updated
1439 * @param pkey public key of the peer whose info is being added/updated
1440 * @param referrer_peer_id id to use when sending to 'peer'
1441 * @param referrer if this is a gossiped peer, who did we hear it from?
1442 * @param cost the cost of communicating with this peer via 'referrer'
1445 addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
1446 unsigned int referrer_peer_id,
1447 struct DirectNeighbor *referrer, unsigned int cost)
1449 struct DistantNeighbor *neighbor;
1450 struct DistantNeighbor *max;
1451 struct GNUNET_TIME_Absolute now;
1452 struct NeighborUpdateInfo *neighbor_update;
1453 unsigned int our_id;
1455 now = GNUNET_TIME_absolute_get ();
1456 our_id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, RAND_MAX - 1) + 1;
1458 neighbor = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
1460 neighbor_update = GNUNET_malloc(sizeof(struct NeighborUpdateInfo));
1461 neighbor_update->neighbor = neighbor;
1462 neighbor_update->cost = cost;
1463 neighbor_update->now = now;
1464 neighbor_update->referrer = referrer;
1466 /* Either we do not know this peer, or we already do but via a different immediate peer */
1467 if ((neighbor == NULL) ||
1468 (GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1470 &update_matching_neighbors,
1471 neighbor_update) != GNUNET_SYSERR))
1474 if (cost > ctx.fisheye_depth)
1477 GNUNET_free(neighbor_update);
1480 if (ctx.max_table_size <=
1481 GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors))
1483 /* remove most expensive entry */
1484 max = GNUNET_CONTAINER_heap_peek (ctx.neighbor_max_heap);
1485 if (cost > max->cost)
1487 /* new entry most expensive, don't create */
1488 GNUNET_free(neighbor_update);
1493 /* only free if this is not a direct connection;
1494 we could theoretically have more direct
1495 connections than DV entries allowed total! */
1496 distant_neighbor_free (max);
1500 neighbor = GNUNET_malloc (sizeof (struct DistantNeighbor));
1501 GNUNET_CONTAINER_DLL_insert (referrer->referee_head,
1502 referrer->referee_tail, neighbor);
1503 neighbor->max_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_max_heap,
1505 neighbor->min_loc = GNUNET_CONTAINER_heap_insert (ctx.neighbor_min_heap,
1507 neighbor->referrer = referrer;
1508 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
1509 if (pkey != NULL) /* pkey will be null on direct neighbor addition */
1511 neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1512 memcpy (neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1515 neighbor->pkey = pkey;
1517 neighbor->last_activity = now;
1518 neighbor->cost = cost;
1519 neighbor->referrer_id = referrer_peer_id;
1520 neighbor->our_id = our_id;
1522 (cost == 0) ? (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4) ==
1524 GNUNET_CONTAINER_multihashmap_put (ctx.extended_neighbors, &peer->hashPubKey,
1526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors));
1538 GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &print_neighbors, NULL);
1540 GNUNET_free(neighbor_update);
1541 /* Old logic to remove entry and replace, not needed now as we only want to remove when full
1542 * or when the referring peer disconnects from us.
1544 * FIXME: add new functionality, or check if it already exists (i forget)
1547 GNUNET_DLL_remove (neighbor->referrer->referee_head,
1548 neighbor->referrer->referee_tail, neighbor);
1549 neighbor->referrer = referrer;
1550 GNUNET_DLL_insert (referrer->referee_head,
1551 referrer->referee_tail, neighbor);
1552 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_max_heap,
1553 neighbor->max_loc, cost);
1554 GNUNET_CONTAINER_heap_update_cost (ctx.neighbor_min_heap,
1555 neighbor->min_loc, cost);
1556 neighbor->referrer_id = referrer_peer_id;
1557 neighbor->last_activity = now;
1558 neighbor->cost = cost;
1564 generate_hello_address (void *cls, size_t max, void *buf)
1566 struct HelloContext *hello_context = cls;
1572 if (hello_context->addresses_to_add == 0)
1575 /* Hello "address" will be concatenation of distant peer and direct peer identities */
1576 size = 2 * sizeof(struct GNUNET_PeerIdentity);
1577 GNUNET_assert(max >= size);
1579 addr_buffer = GNUNET_malloc(size);
1581 /* Copy the distant peer identity to buffer */
1582 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
1583 offset += sizeof(struct GNUNET_PeerIdentity);
1584 /* Copy the direct peer identity to buffer */
1585 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
1586 ret = GNUNET_HELLO_add_address ("dv",
1587 GNUNET_TIME_relative_to_absolute
1588 (GNUNET_TIME_UNIT_HOURS), addr_buffer, size,
1591 hello_context->addresses_to_add--;
1593 GNUNET_free(addr_buffer);
1599 * Core handler for dv gossip messages. These will be used
1600 * by us to create a HELLO message for the newly peer containing
1601 * which direct peer we can connect through, and what the cost
1602 * is. This HELLO will then be scheduled for validation by the
1603 * transport service so that it can be used by all others.
1605 * @param cls closure
1606 * @param peer peer which sent the message (immediate sender)
1607 * @param message the message
1608 * @param latency the latency of the connection we received the message from
1609 * @param distance the distance to the immediate peer
1611 static int handle_dv_gossip_message (void *cls,
1612 const struct GNUNET_PeerIdentity *peer,
1613 const struct GNUNET_MessageHeader *message,
1614 struct GNUNET_TIME_Relative latency,
1617 struct HelloContext *hello_context;
1618 struct GNUNET_HELLO_Message *hello_msg;
1619 struct DirectNeighbor *referrer;
1620 p2p_dv_MESSAGE_NeighborInfo *enc_message = (p2p_dv_MESSAGE_NeighborInfo *)message;
1622 if (ntohs (message->size) < sizeof (p2p_dv_MESSAGE_NeighborInfo))
1624 return GNUNET_SYSERR; /* invalid message */
1628 char * encPeerAbout;
1631 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&enc_message->neighbor));
1632 encPeerFrom = GNUNET_strdup(GNUNET_i2s(peer));
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 "%s: Receives %s message from peer %s about peer %s!\n", "dv", "DV GOSSIP", encPeerFrom, encPeerAbout);
1635 GNUNET_free(encPeerAbout);
1636 GNUNET_free(encPeerFrom);
1639 referrer = GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors,
1641 if (referrer == NULL)
1644 addUpdateNeighbor (&enc_message->neighbor, &enc_message->pkey,
1645 ntohl (enc_message->neighbor_id),
1646 referrer, ntohl (enc_message->cost) + 1);
1648 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
1649 hello_context->direct_peer = peer;
1650 memcpy(&hello_context->distant_peer, &enc_message->neighbor, sizeof(struct GNUNET_PeerIdentity));
1651 hello_context->addresses_to_add = 1;
1652 hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context);
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655 "%s: Sending %s message to plugin, type is %d, size %d!\n", "dv", "HELLO", ntohs(hello_hdr->type), ntohs(hello_hdr->size));
1658 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, ntohl(enc_message->cost) + 1);
1659 GNUNET_free(hello_context);
1660 GNUNET_free(hello_msg);
1665 process_peerinfo (void *cls,
1666 const struct GNUNET_PeerIdentity *peer,
1667 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
1669 struct PeerIteratorContext *peerinfo_iterator = cls;
1670 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
1672 if ((peer == NULL))/* && (neighbor->pkey == NULL))*/
1674 /* FIXME: Remove peer! */
1675 GNUNET_free(peerinfo_iterator);
1679 if (memcmp(&neighbor->identity, peer, sizeof(struct GNUNET_PeerIdentity) != 0))
1682 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
1684 GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1686 &add_pkey_to_extended,
1688 neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
1693 * Method called whenever a peer connects.
1695 * @param cls closure
1696 * @param peer peer identity this notification is about
1697 * @param latency reported latency of the connection with peer
1698 * @param distance reported distance (DV) to peer
1700 void handle_core_connect (void *cls,
1701 const struct GNUNET_PeerIdentity * peer,
1702 struct GNUNET_TIME_Relative latency,
1705 struct DirectNeighbor *neighbor;
1706 struct PeerIteratorContext *peerinfo_iterator;
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1709 "%s: Receives core connect message for peer %s distance %d!\n", "dv", GNUNET_i2s(peer), distance);
1712 if ((distance == 0) && (GNUNET_CONTAINER_multihashmap_get(ctx.direct_neighbors, &peer->hashPubKey) == NULL))
1714 peerinfo_iterator = GNUNET_malloc(sizeof(struct PeerIteratorContext));
1715 neighbor = GNUNET_malloc (sizeof (struct DirectNeighbor));
1716 neighbor->send_context = GNUNET_malloc(sizeof(struct NeighborSendContext));
1717 neighbor->send_context->toNeighbor = neighbor;
1718 neighbor->send_context->timeout = default_dv_delay; /* FIXME: base this on total gossip tasks, or bandwidth */
1719 memcpy (&neighbor->identity, peer, sizeof (struct GNUNET_PeerIdentity));
1720 /*memcpy (&neighbor->pkey, ,sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));*/
1721 GNUNET_CONTAINER_multihashmap_put (ctx.direct_neighbors,
1723 neighbor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1724 addUpdateNeighbor (peer, NULL, 0, neighbor, 0);
1725 peerinfo_iterator->neighbor = neighbor;
1726 peerinfo_iterator->ic = GNUNET_PEERINFO_iterate (cfg,
1730 GNUNET_TIME_relative_multiply
1731 (GNUNET_TIME_UNIT_SECONDS, 15),
1732 &process_peerinfo, peerinfo_iterator);
1733 /* Only add once we get the publicKey of this guy
1735 * neighbor->send_context->task = GNUNET_SCHEDULER_add_now(sched, &neighbor_send_task, neighbor->send_context);
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1742 "%s: Distance (%d) greater than 0 or already know about peer (%s), not re-adding!\n", "dv", distance, GNUNET_i2s(peer));
1749 * Method called whenever a given peer disconnects.
1751 * @param cls closure
1752 * @param peer peer identity this notification is about
1754 void handle_core_disconnect (void *cls,
1755 const struct GNUNET_PeerIdentity * peer)
1757 struct DirectNeighbor *neighbor;
1758 struct DistantNeighbor *referee;
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "%s: Receives core peer disconnect message!\n", "dv");
1766 GNUNET_CONTAINER_multihashmap_get (ctx.direct_neighbors, &peer->hashPubKey);
1767 if (neighbor == NULL)
1771 while (NULL != (referee = neighbor->referee_head))
1772 distant_neighbor_free (referee);
1773 GNUNET_assert (neighbor->referee_tail == NULL);
1774 GNUNET_CONTAINER_multihashmap_remove (ctx.direct_neighbors,
1775 &peer->hashPubKey, neighbor);
1776 if ((neighbor->send_context != NULL) && (neighbor->send_context->task != GNUNET_SCHEDULER_NO_TASK))
1777 GNUNET_SCHEDULER_cancel(sched, neighbor->send_context->task);
1778 GNUNET_free (neighbor);
1783 * Process dv requests.
1785 * @param cls closure
1786 * @param scheduler scheduler to use
1787 * @param server the initialized server
1788 * @param c configuration to use
1792 struct GNUNET_SCHEDULER_Handle *scheduler,
1793 struct GNUNET_SERVER_Handle *server,
1794 const struct GNUNET_CONFIGURATION_Handle *c)
1796 struct GNUNET_TIME_Relative timeout;
1797 unsigned long long max_hosts;
1798 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5);
1802 /* FIXME: Read from config, or calculate, or something other than this! */
1804 ctx.max_table_size = 100;
1805 ctx.fisheye_depth = 3;
1807 ctx.neighbor_min_heap =
1808 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1809 ctx.neighbor_max_heap =
1810 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
1812 ctx.direct_neighbors = GNUNET_CONTAINER_multihashmap_create (max_hosts);
1813 ctx.extended_neighbors =
1814 GNUNET_CONTAINER_multihashmap_create (ctx.max_table_size * 3);
1816 client_transmit_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5);
1817 default_dv_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5);
1818 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1820 GNUNET_CORE_connect (sched,
1823 NULL, /* FIXME: anything we want to pass around? */
1825 &handle_core_connect,
1826 &handle_core_disconnect,
1833 if (coreAPI == NULL)
1835 /* load (server); Huh? */
1837 /* Scheduled the task to clean up when shutdown is called */
1838 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
1839 GNUNET_TIME_UNIT_FOREVER_REL,
1846 * The main function for the dv service.
1848 * @param argc number of arguments from the command line
1849 * @param argv command line arguments
1850 * @return 0 ok, 1 on error
1853 main (int argc, char *const *argv)
1855 return (GNUNET_OK ==
1856 GNUNET_SERVICE_run (argc,
1859 GNUNET_SERVICE_OPTION_NONE,
1860 &run, NULL)) ? 0 : 1;