/*
This file is part of GNUnet.
- (C) 2009-2013 Christian Grothoff (and other contributing authors)
+ (C) 2009-2014 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
#include "gnunet_hello_lib.h"
#include "gnunet_constants.h"
#include "gnunet_protocols.h"
-#include "gnunet_nse_service.h"
#include "gnunet_ats_service.h"
#include "gnunet_core_service.h"
#include "gnunet_datacache_lib.h"
#include "gnunet_transport_service.h"
-#include "gnunet_hello_lib.h"
#include "gnunet_dht_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet-service-xdht.h"
#include "gnunet-service-xdht_clients.h"
#include "gnunet-service-xdht_datacache.h"
-#include "gnunet-service-xdht_hello.h"
#include "gnunet-service-xdht_neighbours.h"
-#include "gnunet-service-xdht_nse.h"
#include "gnunet-service-xdht_routing.h"
#include <fenv.h>
#include "dht.h"
/**
* Maximum possible fingers of a peer.
*/
-#define MAX_FINGERS 64
+#define MAX_FINGERS 65
/**
* Maximum allowed number of pending messages per friend peer.
#define MAXIMUM_PENDING_PER_FRIEND 64
/**
- * How long at least to wait before sending another find finger trail request.
+ * How long to wait before sending another find finger trail request
*/
-#define DHT_MINIMUM_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
+#define DHT_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
/**
- * How long at most to wait before sending another find finger trail request.
+ * How long at most to wait for transmission of a request to another peer?
*/
-#define DHT_MAXIMUM_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10)
+#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
/**
- * How long at most to wait for transmission of a GET request to another peer?
+ * How long will I remain congested?
*/
-#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+#define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+
+/**
+ * Maximum number of trails stored per finger.
+ */
+#define MAXIMUM_TRAILS_PER_FINGER 2
+
+/**
+ * Used to distinguish put/get request use of find_successor() from others
+ */
+#define PUT_GET_REQUEST 65
+
+/**
+ * Maximum number of trails that can go through a friend.
+ */
+#define TRAIL_THROUGH_FRIEND_THRESHOLD 64
+
+/**
+ * Finger map index for predecessor entry in finger peermap.
+ */
+#define PREDECESSOR_FINGER_ID 64
+
+/**
+ * Maximum number of trails allowed to go through a friend.
+ */
+#define TRAILS_THROUGH_FRIEND_THRESHOLD 64
GNUNET_NETWORK_STRUCT_BEGIN
-
+
/**
- * P2P PUT message
+ * P2P Trail setup message
*/
-struct PeerPutMessage
+struct PeerTrailSetupMessage
{
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP
*/
struct GNUNET_MessageHeader header;
-
- /**
- * Processing options
- */
- uint32_t options GNUNET_PACKED;
-
+
/**
- * Content type.
+ * Peer closest to this value will be our finger.
*/
- uint32_t type GNUNET_PACKED;
+ uint64_t ultimate_destination_finger;
/**
- * Hop count
+ * Source peer which wants to setup the trail to one of its finger.
*/
- uint32_t hop_count GNUNET_PACKED;
-
+ struct GNUNET_PeerIdentity source_peer;
+
/**
- * Replication level for this message
+ * Peer to which this packet is forwarded.
*/
- uint32_t desired_replication_level GNUNET_PACKED;
-
+ struct GNUNET_PeerIdentity next_destination;
+
/**
- * Length of the PUT path that follows (if tracked).
+ * Index into finger peer map, in Network Byte Order.
*/
- uint32_t put_path_length GNUNET_PACKED;
-
+ uint32_t finger_map_index;
+
/**
- * When does the content expire?
+ * Number of entries in trail list, in Network Byte Order.
*/
- struct GNUNET_TIME_AbsoluteNBO expiration_time;
-
+ uint32_t trail_length GNUNET_PACKED;
+
/**
- * Bloomfilter (for peer identities) to stop circular routes
+ * Trail id of any intermediate trail we may encounter while doing trail setup.
*/
- char bloomfilter[DHT_BLOOM_SIZE];
-
+ struct GNUNET_HashCode intermediate_trail_id;
+
/**
- * The key we are storing under.
+ * Trail id for trail which we are trying to setup.
*/
- struct GNUNET_HashCode key;
-
- /* put path (if tracked) */
-
- /* Payload */
-
+ struct GNUNET_HashCode new_trail_id;
+
+ /* Trail formed in the process. */
};
-
/**
- * P2P Result message
+ * P2P Trail Setup Result message
*/
-struct PeerResultMessage
+struct PeerTrailSetupResultMessage
{
+
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT
*/
struct GNUNET_MessageHeader header;
-
+
/**
- * Content type.
+ * Finger to which we have found the path.
*/
- uint32_t type GNUNET_PACKED;
+ struct GNUNET_PeerIdentity finger_identity;
/**
- * Length of the PUT path that follows (if tracked).
+ * Peer which was looking for the trail to finger.
*/
- uint32_t put_path_length GNUNET_PACKED;
-
+ struct GNUNET_PeerIdentity destination_peer;
+
/**
- * Length of the GET path that follows (if tracked).
+ * Index into finger peer map in NBO.
*/
- uint32_t get_path_length GNUNET_PACKED;
-
+ uint32_t finger_map_index;
+
/**
- * When does the content expire?
+ * Number of entries in trail list in NBO.
*/
- struct GNUNET_TIME_AbsoluteNBO expiration_time;
-
+ uint32_t trail_length GNUNET_PACKED;
+
/**
- * The key of the corresponding GET request.
+ * Identifier of the trail.
*/
- struct GNUNET_HashCode key;
-
- /* put path (if tracked) */
-
- /* get path (if tracked) */
-
- /* Payload */
-
+ struct GNUNET_HashCode trail_id;
+
+ /* Trail from destination_peer to finger_identity */
};
-
/**
- * P2P GET message
+ * P2P Verify Successor Message.
*/
-struct PeerGetMessage
+struct PeerVerifySuccessorMessage
{
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR
*/
struct GNUNET_MessageHeader header;
-
- /**
- * Processing options
- */
- uint32_t options GNUNET_PACKED;
-
- /**
- * Desired content type.
- */
- uint32_t type GNUNET_PACKED;
-
- /**
- * Hop count
- */
- uint32_t hop_count GNUNET_PACKED;
-
- /**
- * Desired replication level for this request.
- */
- uint32_t desired_replication_level GNUNET_PACKED;
-
+
/**
- * Size of the extended query.
+ * Source peer which wants to verify its successor.
*/
- uint32_t xquery_size;
-
+ struct GNUNET_PeerIdentity source_peer;
+
/**
- * Bloomfilter mutator.
+ * My current successor.
*/
- uint32_t bf_mutator;
-
+ struct GNUNET_PeerIdentity successor;
+
/**
- * Bloomfilter (for peer identities) to stop circular routes
+ * Identifier of trail to reach from source_peer to successor.
*/
- char bloomfilter[DHT_BLOOM_SIZE];
-
+ struct GNUNET_HashCode trail_id;
+
/**
- * The key we are looking for.
+ * Total number of peers to reach from source to successor.
*/
- struct GNUNET_HashCode key;
-
-};
-
-
-/**
- * A destination can be either a friend, finger or me.
- * Used in trail setup to understand if the message is sent to an intermediate
- * finger or a friend.
- */
-enum current_destination_type
-{
- FRIEND ,
- FINGER ,
- MY_ID
+ unsigned int trail_length;
+
+ /* Trail. */
};
-
/**
- * P2P Trail setup message
+ * P2P Verify Successor Result Message
*/
-struct PeerTrailSetupMessage
+struct PeerVerifySuccessorResultMessage
{
-
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT
*/
struct GNUNET_MessageHeader header;
-
- /**
- * Source peer which wants to setup the trail to one of its finger.
- */
- struct GNUNET_PeerIdentity source_peer;
-
- /**
- * Finger id to which we want to set up the trail to.
- */
- uint64_t destination_finger;
/**
- * If set to 1, then we are looking for trail to our immediate successor.
+ * Destination peer which sent the request to verify its successor.
*/
- unsigned int successor_flag;
+ struct GNUNET_PeerIdentity destination_peer;
/**
- * If set to 1, then we are looking for trail to our immediate predecessor.
+ * Successor to which PeerVerifySuccessorMessage was sent.
*/
- unsigned int predecessor_flag;
+ struct GNUNET_PeerIdentity source_successor;
/**
- * Peer which gets this message can be either an intermediate finger or friend.
+ * source_successor's predecessor
*/
- enum current_destination_type current_destination_type;
+ struct GNUNET_PeerIdentity my_predecessor;
/**
- * Peer to which this packet is forwarded.
+ * Trail identifier of trail from my_predecessor to source_successor.
*/
- struct GNUNET_PeerIdentity current_destination;
+ struct GNUNET_HashCode trail_id;
/**
- * Index into finger peer map.
+ * Direction in which we are looking at the trail.
*/
- unsigned int finger_map_index;
+ enum GDS_ROUTING_trail_direction trail_direction;
/**
- * Number of entries in trail list.
+ * Total number of peers in trail from source_successor to my_predecessor
+ * if my_predecessor is not same as destination_peer.
*/
- uint32_t trail_length GNUNET_PACKED;
+ uint32_t trail_length;
+ /* Trail from source_successor to my_predecessor where
+ * my_predecessor != destination_peer*/
};
-
/**
- * P2P Trail setup Result message
+ * P2P Notify New Successor Message.
*/
-struct PeerTrailSetupResultMessage
+struct PeerNotifyNewSuccessorMessage
{
-
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_RESULT_SETUP
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR
*/
struct GNUNET_MessageHeader header;
/**
- * Finger to which we have found the path.
- */
- struct GNUNET_PeerIdentity finger;
-
- /**
- * Peer which was looking for the trail to finger.
- */
- struct GNUNET_PeerIdentity destination_peer;
-
- /**
- * Peer to which this packet is forwarded next.
- */
- struct GNUNET_PeerIdentity current_destination;
-
- /**
- * Index at which peer list should be accessed.
- */
- unsigned int current_index;
-
- /**
- * If set to 1, then this trail is the trail to our successor.
+ * Source peer which wants to notify its new successor.
*/
- unsigned int successor_flag;
+ struct GNUNET_PeerIdentity source_peer;
/**
- * If set to 1, then this trail is the trail to our predecessor.
+ * New successor identity.
*/
- unsigned int predecessor_flag;
+ struct GNUNET_PeerIdentity destination_peer;
/**
- * Index into finger peer map
+ * Total number of peers in trail from source_peer to destination_peer
*/
- unsigned int finger_map_index;
+ unsigned int trail_length;
/**
- * Number of entries in trail list.
+ * Unique identifier of the trail.
*/
- uint32_t trail_length GNUNET_PACKED;
+ struct GNUNET_HashCode trail_id;
+ /* Trail. */
};
-
/**
- * P2P verify successor message.
+ * P2P Trail Compression Message.
*/
-struct PeerVerifySuccessorMessage
+struct PeerTrailCompressionMessage
{
-
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION
*/
struct GNUNET_MessageHeader header;
/**
- * Source peer which wants to verify its successor.
+ * Source peer of this trail.
*/
struct GNUNET_PeerIdentity source_peer;
/**
- * My current successor.
+ * Destination of this trail.
*/
- struct GNUNET_PeerIdentity successor;
+ struct GNUNET_PeerIdentity destination_peer;
/**
- * Total number of peers in trail to current successor.
+ * Trail from source_peer to destination_peer compressed such that
+ * new_first_friend is the first hop in the trail from source to
+ * destination.
*/
- unsigned int trail_length;
+ struct GNUNET_PeerIdentity new_first_friend;
/**
- * Index in trail which points to next destination to send this message.
+ * Unique identifier of trail.
*/
- unsigned int current_trail_index;
-
+ struct GNUNET_HashCode trail_id;
};
/**
- * P2P verify successor result message.
+ * P2P Trail Tear Down message.
*/
-struct PeerVerifySuccessorResultMessage
+struct PeerTrailTearDownMessage
{
-
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN
+ */
+ struct GNUNET_MessageHeader header;
+};
+
+
+/**
+ * P2P Trail Rejection Message.
+ */
+struct PeerTrailRejectionMessage
+{
+ /**
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION
*/
struct GNUNET_MessageHeader header;
/**
- * Destination peer which sent the request to verify its successor.
+ * Source peer which wants to set up the trail.
*/
- struct GNUNET_PeerIdentity destination_peer;
+ struct GNUNET_PeerIdentity source_peer;
/**
- * Successor to which PeerVerifySuccessorMessage was sent.
+ * Peer which sent trail rejection message.
*/
- struct GNUNET_PeerIdentity source_successor;
+ struct GNUNET_PeerIdentity congested_peer;
/**
- * source_successor's predecessor
+ * Peer identity which will be successor to this value will be finger of
+ * source_peer.
*/
- struct GNUNET_PeerIdentity my_predecessor;
+ uint64_t ultimate_destination_finger_identity_value;
/**
- * Total number of peers in trail.
- * If source_successor is not destination peer, then trail is from destination_peer
- * to my_predecessor.
- * If source_successor is destination peer, then trail is from destination_peer
- * to source_successor.
+ * Index in finger peer map of source peer.
*/
- unsigned int trail_length;
+ uint32_t finger_map_index;
/**
- * Index in trail which points to next destination to send this message.
+ * Total number of peers in the trail.
*/
- unsigned int current_index;
+ uint32_t trail_length;
+
+ /**
+ * Identifier for the trail source peer is trying to setup.
+ */
+ struct GNUNET_HashCode trail_id;
+ /**
+ * Relative time for which congested_peer will remain congested.
+ */
+ struct GNUNET_TIME_Relative congestion_time;
+ /* Trail_list from source_peer to peer which sent the message for trail setup
+ * to congested peer.*/
};
/**
- * P2P notify new successor message.
+ * P2P Add Trail Message.
*/
-struct PeerNotifyNewSuccessorMessage
+struct PeerAddTrailMessage
{
/**
- * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL
*/
struct GNUNET_MessageHeader header;
/**
- * Source peer which wants to notify its new successor.
+ * Source peer of the routing trail.
*/
struct GNUNET_PeerIdentity source_peer;
/**
- * New successor identity.
+ * Destination peer of the routing trail.
*/
struct GNUNET_PeerIdentity destination_peer;
/**
- * Number of peers in trail from source_peer to new successor.
+ * Total number of peers from source peer to destination peer.
*/
unsigned int trail_length;
/**
- * Index in trail which points to next destination to send this message.
+ * Unique identifier of the trail.
*/
- unsigned int current_index;
+ struct GNUNET_HashCode trail_id;
+ /* Trail from source peer to destination peer. */
};
-
-GNUNET_NETWORK_STRUCT_END
-
-
+GNUNET_NETWORK_STRUCT_END
+
/**
* Linked list of messages to send to a particular other peer.
*/
struct P2PPendingMessage *prev;
/**
- * When does this message time out?
- */
- struct GNUNET_TIME_Absolute timeout;
-
- /**
* Message importance level. FIXME: used? useful?
*/
unsigned int importance;
-
+
+ /**
+ * When does this message time out?
+ */
+ struct GNUNET_TIME_Absolute timeout;
+
/**
* Actual message to be sent, allocated at the end of the struct:
* // msg = (cast) &pm[1];
};
-
- /**
- * Linked List of peers which are part of trail to reach a particular Finger.
- */
-struct TrailPeerList
-{
- /**
- * Pointer to next item in the list
- */
- struct TrailPeerList *next;
-
- /**
- * Pointer to previous item in the list
- */
- struct TrailPeerList *prev;
-
- /**
- * An element in this trail list
- */
- struct GNUNET_PeerIdentity peer;
-
-};
-
-
+
/**
* Entry in friend_peermap.
*/
*/
struct GNUNET_PeerIdentity id;
+ /**
+ * Number of trails for which this friend is the first hop.
+ */
+ unsigned int trails_count;
+
/**
* Count of outstanding messages for this friend.
*/
unsigned int pending_count;
+ /**
+ * In case not 0, then amount of time for which this friend is congested.
+ */
+ struct GNUNET_TIME_Absolute congestion_duration;
+
/**
* Head of pending messages to be sent to this friend.
*/
- struct P2PPendingMessage *head;
+ struct P2PPendingMessage *head;
- /**
- * Tail of pending messages to be sent to this friend.
- */
- struct P2PPendingMessage *tail;
+ /**
+ * Tail of pending messages to be sent to this friend.
+ */
+ struct P2PPendingMessage *tail;
- /**
- * Core handle for sending messages to this friend.
- */
- struct GNUNET_CORE_TransmitHandle *th;
+ /**
+ * Core handle for sending messages to this friend.
+ */
+ struct GNUNET_CORE_TransmitHandle *th;
};
-
/**
- * Entry in finger_peermap.
+ * An individual trail to reach to a finger.
*/
-struct FingerInfo
+struct Trail
{
/**
- * Finger identity.
- */
- struct GNUNET_PeerIdentity finger_identity;
+ * Pointer to next item in the list
+ */
+ struct Trail *next;
/**
- * If 1, then this finger entry is my first finger(successor).
- */
- unsigned int successor;
+ * Pointer to prev item in the list
+ */
+ struct Trail *prev;
/**
- * If 1, then this finger entry is my first predecessor.
+ * An element in this trail.
*/
- unsigned int predecessor;
-
+ struct GNUNET_PeerIdentity peer;
+};
+
+/**
+ * List of all trails to reach a particular finger.
+ */
+struct TrailList
+{
/**
- * Index in finger peer map
+ * Head of trail.
*/
- unsigned int finger_map_index;
+ struct Trail *trail_head;
/**
- * Total number of entries in trail from me to finger.
+ * Tail of trail.
*/
- unsigned int trail_length;
+ struct Trail *trail_tail;
/**
- * Head of trail to reach this finger.
+ * Unique identifier of this trail.
*/
- struct TrailPeerList *head;
+ struct GNUNET_HashCode trail_id;
/**
- * Tail of trail to reach this finger.
+ * Length of trail pointed
*/
- struct TrailPeerList *tail;
+ unsigned int trail_length;
+ /**
+ * Number of trails that the first friend of this trail is a part of.
+ */
+ unsigned int first_friend_trail_count;
};
-
/**
- * Task that sends FIND FINGER TRAIL requests.
+ * An entry in finger_hashmap.
*/
-static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
-
-/**
- *
- * Task that periodically checks for who is my successor.
+struct FingerInfo
+{
+ /**
+ * Finger identity.
+ */
+ struct GNUNET_PeerIdentity finger_identity;
+
+ /**
+ * Index in finger peer map
+ */
+ unsigned int finger_map_index;
+
+ /**
+ * Number of trails setup so far for this finger.
+ */
+ unsigned int trails_count;
+
+ /**
+ * Array of trails.
+ */
+ struct TrailList trail_list[MAXIMUM_TRAILS_PER_FINGER];
+};
+
+/**
+ * Data structure to keep track of closest peer seen so far in find_successor()
+ */
+struct Closest_Peer
+{
+ /**
+ * 64 bit value of the peer
+ */
+ uint64_t value;
+
+ /**
+ * Trail id to reach to peer.
+ */
+ struct GNUNET_HashCode trail_id;
+
+ /**
+ * First hop, NULL in case of friend and my_identity
+ */
+ struct GNUNET_PeerIdentity next_hop;
+
+ /**
+ * Next destination. In case of friend and my_identity , it is same as next_hop
+ * In case of finger it is finger identity.
+ */
+ struct GNUNET_PeerIdentity next_destination;
+};
+
+/**
+ * Data structure to store the trail chosen to reach to finger.
+ */
+struct Correct_Trail
+{
+ /**
+ * First friend in the trail to reach finger.
+ */
+ struct FriendInfo friend;
+
+ /**
+ * Identifier of this trail.
+ */
+ struct GNUNET_HashCode trail_id;
+
+ /**
+ * Total number of peers in this trail.
+ */
+ unsigned int trail_length;
+};
+
+/**
+ * Task that sends FIND FINGER TRAIL requests. This task is started when we have
+ * get our first friend.
*/
-static GNUNET_SCHEDULER_TaskIdentifier verify_successor;
+static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
/**
* Identity of this peer.
static struct GNUNET_PeerIdentity my_identity;
/**
- * Hash map of all the friends of a peer
+ * Peer map of all the friends of a peer
*/
static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
/**
* Hash map of all the fingers of a peer
*/
-static struct GNUNET_CONTAINER_MultiPeerMap *finger_peermap;
-
-/**
- * Handle to ATS.
- */
-static struct GNUNET_ATS_PerformanceHandle *atsAPI;
+static struct GNUNET_CONTAINER_MultiHashMap32 *finger_hashmap;
/**
* Handle to CORE.
static struct GNUNET_CORE_Handle *core_api;
/**
- * FIXME: Is it safe to assume its initialized to 0 by default.
- * The current finger index that we have found trail to.
+ * The current finger index that we have want to find trail to. We start the
+ * search with value = 0, i.e. successor peer and then go to PREDCESSOR_FINGER_ID
+ * and decrement it. For any index 63 <= index < 0, if finger is same as successor,
+ * we reset this index to 0.
*/
-static unsigned int current_finger_index;
+static unsigned int current_search_finger_index;
/**
* Called when core is ready to send a message we asked for
* out to the destination.
*
- * @param cls the 'struct PeerInfo' of the target peer
+ * @param cls the 'struct FriendInfo' of the target friend
* @param size number of bytes available in buf
* @param buf where the callee should write the message
* @return number of bytes written to buf
&core_transmit_notify, peer);
GNUNET_break (NULL != peer->th);
}
+
return off;
}
/**
+ * FIXME: assertion fails at the end of this function. also in core_api.c at 1299.
* Transmit all messages in the friend's message queue.
*
* @param peer message queue to process
("# Bytes of bandwidth requested from core"),
ntohs (pending->msg->size), GNUNET_NO);
- /* FIXME: Are we correctly initializing importance and pending. */
peer->th =
GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
pending->importance,
/**
- * Setup the trail message and forward it to a friend.
- * @param source_peer Peer which wants to set up the trail to one of its finger.
- * @param destination_finger Peer to which we want to set up the trail to.
- * @param target_friend Current friend to which this message should be forwarded.
- * @param trail_length Numbers of peers in the trail.
- * @param trail_peer_list peers this request has traversed so far
- * @param successor_flag If 1 then we are looking for trail to our successor.
- * @param predecessor_flag If 1, then we are looking for trail to our predecessor.
- * @param current_finger_index Finger index in finger peer map
+ * Construct a trail setup message and forward it to target_friend
+ * @param source_peer Source peer which wants to setup the trail
+ * @param ultimate_destination_finger Peer identity closest to this value will
+ * be finger to @a source_peer
+ * @param next_destination Peer which should get the packet. I can be same as
+ * target_friend or different.
+ * @param target_friend Friend to which message is forwarded now.
+ * @param trail_length Total number of peers in trail setup so far.
+ * @param trail_peer_list Trail setup so far
+ * @param finger_map_index Index in finger map for which we are looking for finger.
+ * @param trail_id Unique identifier for the trail we are trying to setup.
+ * @param intermediate_trail_id Trail id of any intermediate trail we may have to
+ * traverse during trail setup. If not used then set to
+ * 0.
*/
void
-GDS_NEIGHBOURS_handle_trail_setup (struct GNUNET_PeerIdentity *source_peer,
- uint64_t *destination_finger,
- struct FriendInfo *target_friend,
- unsigned int trail_length,
- struct GNUNET_PeerIdentity *trail_peer_list,
- unsigned int successor_flag,
- unsigned int predecessor_flag,
- unsigned int current_finger_index)
+GDS_NEIGHBOURS_send_trail_setup (const struct GNUNET_PeerIdentity source_peer,
+ uint64_t ultimate_destination_finger,
+ struct GNUNET_PeerIdentity next_destination,
+ struct FriendInfo *target_friend,
+ unsigned int trail_length,
+ const struct GNUNET_PeerIdentity *trail_peer_list,
+ unsigned int finger_map_index,
+ struct GNUNET_HashCode new_trail_id,
+ struct GNUNET_HashCode intermediate_trail_id)
{
struct P2PPendingMessage *pending;
struct PeerTrailSetupMessage *tsm;
}
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
- pending->importance = 0; /* FIXME */
pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
tsm = (struct PeerTrailSetupMessage *) &pending[1];
pending->msg = &tsm->header;
tsm->header.size = htons (msize);
tsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP);
- memcpy (&(tsm->destination_finger), destination_finger, sizeof (uint64_t)); /* FIXME: Wrong value of finger identity goes to the target peer in
- * handle_dht_p2p_trail_setup */
- memcpy (&(tsm->source_peer), source_peer, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(tsm->current_destination), &(target_friend->id),
- sizeof (struct GNUNET_PeerIdentity));
- tsm->current_destination_type = htonl (FRIEND);
+ tsm->ultimate_destination_finger = GNUNET_htonll (ultimate_destination_finger);
+ tsm->source_peer = source_peer;
+ tsm->next_destination = next_destination;
tsm->trail_length = htonl (trail_length);
- tsm->finger_map_index = htonl (current_finger_index);
- if(1 == successor_flag)
+ tsm->finger_map_index = htonl (finger_map_index);
+ tsm->new_trail_id = new_trail_id;
+ tsm->intermediate_trail_id = intermediate_trail_id;
+ if (trail_length > 0)
{
- tsm->successor_flag = htonl(1);
- tsm->predecessor_flag = htonl (0);
+ peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
+ memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
}
- else if (1 == predecessor_flag)
- {
- tsm->predecessor_flag = htonl(1);
- tsm->successor_flag = htonl(0);
- }
- else
- {
- tsm->successor_flag = htonl(0);
- tsm->predecessor_flag = htonl(0);
- }
- peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
- memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
+
GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
target_friend->pending_count++;
process_friend_queue (target_friend);
-
}
/**
- * Handle a tail setup result message.
+ * Construct a trail setup result message and forward it to target friend.
* @param destination_peer Peer which will get the trail to one of its finger.
* @param source_finger Peer to which the trail has been setup to.
* @param target_friend Friend to which this message should be forwarded.
* @param trail_length Numbers of peers in the trail.
* @param trail_peer_list Peers which are part of the trail from source to destination.
- * @param current_trail_index Index in trail_peer_list.
- * @param successor_flag If 1, then this is the trail to our successor.
- * @param predecessor_flag If 1, then this is the trail to our predecessor.
- * @param finger_map_index Finger index in finger peer map
+ * @param finger_map_index Index in finger peer map
+ * @param trail_id Unique identifier of the trail.
*/
void
-GDS_NEIGHBOURS_handle_trail_setup_result (struct GNUNET_PeerIdentity *destination_peer,
- struct GNUNET_PeerIdentity *source_finger,
- struct FriendInfo *target_friend,
- unsigned int trail_length,
- struct GNUNET_PeerIdentity *trail_peer_list,
- unsigned int current_trail_index,
- unsigned int successor_flag,
- unsigned int predecessor_flag,
- unsigned int finger_map_index)
+GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity destination_peer,
+ struct GNUNET_PeerIdentity source_finger,
+ struct FriendInfo *target_friend,
+ unsigned int trail_length,
+ const struct GNUNET_PeerIdentity *trail_peer_list,
+ unsigned int finger_map_index,
+ struct GNUNET_HashCode trail_id)
{
struct P2PPendingMessage *pending;
struct PeerTrailSetupResultMessage *tsrm;
GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1, GNUNET_NO);
}
-
+
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
pending->importance = 0;
pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
pending->msg = &tsrm->header;
tsrm->header.size = htons (msize);
tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT);
- memcpy (&(tsrm->current_destination), &(target_friend->id), sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(tsrm->destination_peer), destination_peer, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(tsrm->finger), source_finger, sizeof (struct GNUNET_PeerIdentity));
+ tsrm->destination_peer = destination_peer;
+ tsrm->finger_identity = source_finger;
tsrm->trail_length = htonl (trail_length);
- tsrm->current_index = htonl (current_trail_index);
- tsrm->successor_flag = htonl (successor_flag);
- tsrm->predecessor_flag = htonl (predecessor_flag);
tsrm->finger_map_index = htonl (finger_map_index);
- peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
- memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ tsrm->trail_id = trail_id;
+ peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
+ if (trail_length > 0)
+ {
+ memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ }
/* Send the message to chosen friend. */
GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
target_friend->pending_count++;
/**
- * Construct a PeerVerifySuccessor message and send it to friend.
- * @param source_peer Peer which wants to verify its successor
- * @param successor Peer which is our current successor
- * @param target_friend Friend to which this message should be forwarded.
- * @param trail_peer_list Peer which are part of trail from source to destination
- * @param trail_length Number of peers in the trail list.
- * @param current_trail_index Index in the trial list at which receiving peer should
- * get the next element.
- */
-void GDS_NEIGHBOURS_handle_verify_successor(struct GNUNET_PeerIdentity *source_peer,
- struct GNUNET_PeerIdentity *successor,
- struct FriendInfo *target_friend,
- struct GNUNET_PeerIdentity *trail_peer_list,
- unsigned int trail_length,
- unsigned int current_trail_index)
+ * Send trail rejection message to next_hop
+ * @param source_peer Source peer which is trying to setup the trail.
+ * @param finger_identity Peer closest to this value will be @a source_peer's finger
+ * @param congested_peer Peer which sent this message as it is congested.
+ * @param next_hop Peer to which we are forwarding this message.
+ * @param finger_map_index Index in finger peermap for which we are searching for finger.
+ * @param trail_peer_list Trails seen so far in trail setup before getting rejected
+ * by congested_peer
+ * @param trail_length Total number of peers in trail_peer_list
+ * @param trail_id Unique identifier of this trail.
+ * @param congestion_timeout Duration given by congested peer as an estimate of
+ * how long it may remain congested.
+ */
+void
+GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
+ uint64_t finger_identity,
+ struct GNUNET_PeerIdentity congested_peer,
+ unsigned int finger_map_index,
+ struct GNUNET_PeerIdentity *trail_peer_list,
+ unsigned int trail_length,
+ struct GNUNET_HashCode trail_id,
+ struct FriendInfo *target_friend,
+ const struct GNUNET_TIME_Relative congestion_timeout)
{
- struct PeerVerifySuccessorMessage *vsm;
+ struct PeerTrailRejectionMessage *trm;
struct P2PPendingMessage *pending;
struct GNUNET_PeerIdentity *peer_list;
size_t msize;
- msize = sizeof (struct PeerVerifySuccessorMessage) +
+ msize = sizeof (struct PeerTrailRejectionMessage) +
(trail_length * sizeof (struct GNUNET_PeerIdentity));
if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
GNUNET_break (0);
return;
}
+
+ if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+ {
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
+ 1, GNUNET_NO);
+ }
+
+ pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+ pending->importance = 0;
+ pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
+ trm = (struct PeerTrailRejectionMessage *)&pending[1];
+ pending->msg = &trm->header;
+ trm->header.size = htons (msize);
+ trm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION);
+ trm->source_peer = source_peer;
+ trm->congested_peer = congested_peer;
+ trm->congestion_time = congestion_timeout;
+ trm->finger_map_index = htonl (finger_map_index);
+ trm->trail_id = trail_id;
+
+ peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
+ if (trail_length > 0)
+ {
+ memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ }
+ /* Send the message to chosen friend. */
+ GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
+ target_friend->pending_count++;
+ process_friend_queue (target_friend);
+}
+
+
+/**
+ * Construct a verify successor message and forward it to target_friend.
+ * @param source_peer Peer which wants to verify its successor.
+ * @param successor Peer which is @a source_peer's current successor.
+ * @param trail_id Identifier of trail to reach successor.
+ * @param trail Trail to reach from source_peer to successor
+ * @param trail_length Total number of peers in @a trail.
+ * @param target_friend Message send to this friend.
+ */
+void
+GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
+ struct GNUNET_PeerIdentity successor,
+ const struct GNUNET_HashCode trail_id,
+ struct GNUNET_PeerIdentity *trail,
+ unsigned int trail_length,
+ struct FriendInfo *target_friend)
+{
+ struct PeerVerifySuccessorMessage *vsm;
+ struct P2PPendingMessage *pending;
+ struct GNUNET_PeerIdentity *peer_list;
+ size_t msize;
+
+ msize = sizeof (struct PeerVerifySuccessorMessage);
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ return;
+ }
if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
{
pending->msg = &vsm->header;
vsm->header.size = htons (msize);
vsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR);
- memcpy (&(vsm->successor), successor, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(vsm->source_peer), source_peer, sizeof (struct GNUNET_PeerIdentity));
+ vsm->source_peer = source_peer;
+ vsm->successor = successor;
+ vsm->trail_id = trail_id;
vsm->trail_length = htonl (trail_length);
- vsm->current_trail_index = htonl (current_trail_index);
- peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
- memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+
+ if (trail_length > 0)
+ {
+ peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
+ memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ }
/* Send the message to chosen friend. */
GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
target_friend->pending_count++;
process_friend_queue (target_friend);
+}
+
+
+/**
+ * Construct a trail teardown message and send it to target_friend
+ * @param source_peer
+ * @param destination_peer
+ * @param trail_id
+ * @param trail_direction
+ * @param target_friend
+ */
+void
+GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_PeerIdentity source_peer,
+ struct GNUNET_PeerIdentity destination_peer,
+ struct GNUNET_HashCode trail_id,
+ enum GDS_ROUTING_trail_direction trail_direction,
+ struct FriendInfo *target_friend)
+{
}
/**
- * Construct a PeerVerifySuccessorResult message and send it to friend.
- * @param destination_peer Peer which sent verify successor message
- * @param source_successor Peer to which verify successor message was sent.
- * @param my_predecessor source_successor predecessor.
- * @param target_friend Friend to which this message should be forwarded.
- * @param trail_peer_list Peer which are part of trail from source to destination
- * @param trail_length Number of peers in the trail list.
- * @param current_trail_index Index in the trial list at which receiving peer should
- * get the next element.
- */
-void GDS_NEIGHBOURS_handle_verify_successor_result (struct GNUNET_PeerIdentity *destination_peer,
- struct GNUNET_PeerIdentity *source_successor,
- struct GNUNET_PeerIdentity *my_predecessor,
- struct FriendInfo *target_friend,
- struct GNUNET_PeerIdentity *trail_peer_list,
- unsigned int trail_length,
- unsigned int current_trail_index)
+ * Construct a verify successor message and send it to target_friend
+ * @param destination_peer
+ * @param source_successor
+ * @param succ_predecessor
+ * @param trail_id
+ * @param trail
+ * @param trail_length
+ * @param target_friend
+ */
+void
+GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity destination_peer,
+ struct GNUNET_PeerIdentity source_successor,
+ struct GNUNET_PeerIdentity succ_predecessor,
+ struct GNUNET_HashCode trail_id,
+ const struct GNUNET_PeerIdentity *trail,
+ unsigned int trail_length,
+ enum GDS_ROUTING_trail_direction trail_direction,
+ struct FriendInfo *target_friend)
{
struct PeerVerifySuccessorResultMessage *vsmr;
struct P2PPendingMessage *pending;
GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1, GNUNET_NO);
}
-
+
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
pending->importance = 0; /* FIXME */
pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
pending->msg = &vsmr->header;
vsmr->header.size = htons (msize);
vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT);
- memcpy (&(vsmr->destination_peer), destination_peer, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(vsmr->source_successor), source_successor, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(vsmr->my_predecessor), my_predecessor, sizeof (struct GNUNET_PeerIdentity));
- vsmr->trail_length = htonl (trail_length);
- vsmr->current_index = htonl (current_trail_index);
+ vsmr->destination_peer = destination_peer;
+ vsmr->my_predecessor = succ_predecessor;
+ vsmr->source_successor = source_successor;
+ vsmr->trail_direction = htonl (trail_direction);
- peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
- memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ if (trail_length > 0)
+ {
+ peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
+ memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ }
/* Send the message to chosen friend. */
GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
/**
- * Construct a PeerNotifyNewSuccessor message and send it to friend.
- * @param source_peer Peer which is sending notify message to its new successor.
- * @param destination_peer Peer which is the new destination.
- * @param target_friend Next friend to pass this message to.
- * @param peer_list List of peers in the trail to reach to destination_peer.
- * @param current_trail_index Index of peer_list for next target friend position.
- * @param trail_length Total number of peers in peer list
+ * Construct a notify new successor message and send it to target_friend
+ * @param source_peer
+ * @param new_successor
+ * @param new_successor_trail
+ * @param new_successor_trail_length
+ * @param new_succesor_trail_id
*/
void
-GDS_NEIGHBOURS_notify_new_successor (struct GNUNET_PeerIdentity *source_peer,
- struct GNUNET_PeerIdentity *destination_peer,
- struct FriendInfo *target_friend,
- struct GNUNET_PeerIdentity *trail_peer_list,
- unsigned int trail_length,
- unsigned int current_trail_index)
+GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer,
+ struct GNUNET_PeerIdentity new_successor,
+ struct GNUNET_PeerIdentity *new_successor_trail,
+ unsigned int new_successor_trail_length,
+ struct GNUNET_HashCode new_succesor_trail_id,
+ struct FriendInfo *target_friend)
{
struct PeerNotifyNewSuccessorMessage *nsm;
struct P2PPendingMessage *pending;
size_t msize;
msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
- (trail_length * sizeof(struct GNUNET_PeerIdentity));
+ (new_successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
{
GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1, GNUNET_NO);
}
-
+
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
pending->importance = 0; /* FIXME */
pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
pending->msg = &nsm->header;
nsm->header.size = htons (msize);
nsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR);
- memcpy (&(nsm->source_peer), source_peer, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&(nsm->destination_peer), destination_peer, sizeof (struct GNUNET_PeerIdentity));
- nsm->trail_length = htonl (trail_length);
- nsm->current_index = htonl (current_trail_index);
-
- peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
- memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ nsm->source_peer = source_peer;
+ nsm->destination_peer = new_successor;
+ nsm->trail_length = htonl (new_successor_trail_length);
+ nsm->trail_id = new_succesor_trail_id;
+ if (new_successor_trail_length > 0)
+ {
+ peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
+ memcpy (peer_list, new_successor_trail,
+ new_successor_trail_length * sizeof (struct GNUNET_PeerIdentity));
+ }
/* Send the message to chosen friend. */
GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
}
-/**FIXME: Old implementation just to remove error
- * TODO: Modify this function to handle our get request.
- * Perform a GET operation. Forwards the given request to other
- * peers. Does not lookup the key locally. May do nothing if this is
- * the only peer in the network (or if we are the closest peer in the
- * network).
- *
- * @param type type of the block
- * @param options routing options
- * @param desired_replication_level desired replication count
- * @param hop_count how many hops did this request traverse so far?
- * @param key key for the content
- * @param xquery extended query
- * @param xquery_size number of bytes in @a xquery
- * @param reply_bf bloomfilter to filter duplicates
- * @param reply_bf_mutator mutator for @a reply_bf
- * @param peer_bf filter for peers not to select (again)
+/**
+ * Construct an add_trail message and send it to target_friend
+ * @param source_peer Source of the trail.
+ * @param destination_peer Destination of the trail.
+ * @param trail_id Unique identifer of the trail
+ * @param trail Trail from @a source_peer to @a destination_peer
+ * @param trail_length Total number of peers in @a trail.
+ * @param target_friend Next peer to get this message.
*/
void
-GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
- enum GNUNET_DHT_RouteOption options,
- uint32_t desired_replication_level,
- uint32_t hop_count, const struct GNUNET_HashCode * key,
- const void *xquery, size_t xquery_size,
- const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
- uint32_t reply_bf_mutator,
- struct GNUNET_CONTAINER_BloomFilter *peer_bf)
+GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
+ struct GNUNET_PeerIdentity destination_peer,
+ struct GNUNET_HashCode trail_id,
+ struct GNUNET_PeerIdentity *trail,
+ unsigned int trail_length,
+ struct FriendInfo *target_friend)
{
-
- /*
- 1. take the key, get the 64 bit value of the key.
- 2. call find_successor to get the successor of the key.
- 3. successor can be either a friend or finger.
- 4. update the field in get message to reflect if its a friend or finger table
- 5. add the put message to pending message and send it.
- */
+ struct PeerAddTrailMessage *adm;
+ struct GNUNET_PeerIdentity *peer_list;
+ struct P2PPendingMessage *pending;
+ size_t msize;
+
+ msize = sizeof (struct PeerAddTrailMessage) +
+ (trail_length * sizeof(struct GNUNET_PeerIdentity));
+
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+ {
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
+ 1, GNUNET_NO);
+ }
+
+ pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+ pending->importance = 0; /* FIXME */
+ pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
+ adm = (struct PeerAddTrailMessage *) &pending[1];
+ pending->msg = &adm->header;
+ adm->header.size = htons (msize);
+ adm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL);
+ adm->source_peer = source_peer;
+ adm->destination_peer = destination_peer;
+ adm->trail_length = htonl (trail_length);
+ adm->trail_id = trail_id;
+
+ if (trail_length > 0)
+ {
+ peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
+ memcpy (peer_list, trail, sizeof (struct GNUNET_PeerIdentity) * trail_length);
+ }
+
+ /* Send the message to chosen friend. */
+ GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
+ target_friend->pending_count++;
+ process_friend_queue (target_friend);
}
-/**FIXME: Old implementation just to remove error.
- * TODO: Modify this function to handle our put request.
- * Perform a PUT operation. Forwards the given request to other
- * peers. Does not store the data locally. Does not give the
- * data to local clients. May do nothing if this is the only
- * peer in the network (or if we are the closest peer in the
- * network).
- *
- * @param type type of the block
- * @param options routing options
- * @param desired_replication_level desired replication count
- * @param expiration_time when does the content expire
- * @param hop_count how many hops has this message traversed so far
- * @param bf Bloom filter of peers this PUT has already traversed
- * @param key key for the content
- * @param put_path_length number of entries in @a put_path
- * @param put_path peers this request has traversed so far (if tracked)
- * @param data payload to store
- * @param data_size number of bytes in @a data
+
+/**
+ * Construct a trail compression message and send it to target_friend.
+ * @param source_peer Source of the trail.
+ * @param destination_finger Destination of trail.
+ * @param trail_id Unique identifier of trail.
+ * @param first_friend First hop in compressed trail to reach from source to finger
+ * @param target_friend Next friend to get this message.
*/
void
-GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
- enum GNUNET_DHT_RouteOption options,
- uint32_t desired_replication_level,
- struct GNUNET_TIME_Absolute expiration_time,
- uint32_t hop_count,
- struct GNUNET_CONTAINER_BloomFilter *bf,
- const struct GNUNET_HashCode *key,
- unsigned int put_path_length,
- struct GNUNET_PeerIdentity *put_path,
- const void *data, size_t data_size)
-{
-
- /*
- 1. take the key, get the 64 bit value of the key.
- 2. call find_successor to get the successor of the key.
- 3. successor can be either a friend or finger.
- 4. update the field in put message to reflect if its a friend or finger table
- 5. add the put message to pending message and send it.
- */
- /* SUPU: Call is made to this function from client. It does not seem to be
- waiting for a confirmation So, once we got the request, we use the key and
- try to find the closest successor, but in this case when we reach to the
- closest successor in handle_dht_p2p_put, then just do datacache_put. As the calling
- function does not need any confirmation, we don't need the result back. */
+GDS_NEIGHBOURS_send_trail_compression (struct GNUNET_PeerIdentity source_peer,
+ struct GNUNET_PeerIdentity destination_peer,
+ struct GNUNET_HashCode trail_id,
+ struct GNUNET_PeerIdentity first_friend,
+ struct FriendInfo *target_friend)
+{
+ struct P2PPendingMessage *pending;
+ struct PeerTrailCompressionMessage *tcm;
+ size_t msize;
+
+ msize = sizeof (struct PeerTrailCompressionMessage);
+
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P messages dropped due to full queue"),
+ 1, GNUNET_NO);
+ }
+
+ pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+ pending->importance = 0; /* FIXME */
+ pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
+ tcm = (struct PeerTrailCompressionMessage *) &pending[1];
+ pending->msg = &tcm->header;
+ tcm->header.size = htons (msize);
+ tcm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION);
+ tcm->source_peer = source_peer;
+ tcm->new_first_friend = first_friend;
+ tcm->trail_id = trail_id;
+ tcm->destination_peer = destination_peer;
+
+ GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
+ target_friend->pending_count++;
+ process_friend_queue (target_friend);
+
}
-/**
- * Randomly choose one of your friends from the friends_peer map
- * @return Friend
+/**
+ * Seach my location in trail.
+ * @param trail List of peers
+ * @return my_index if found
+ * #GNUNET_SYSERR if no entry found.
*/
-static struct FriendInfo *
-select_random_friend()
-{
- unsigned int current_size;
- unsigned int *index;
- unsigned int j = 0;
- struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
- struct GNUNET_PeerIdentity key_ret;
- struct FriendInfo *friend;
+static int
+search_my_index (const struct GNUNET_PeerIdentity *trail,
+ int trail_length)
+{
+ int i;
- current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
+ for (i = 0; i < trail_length; i++)
+ {
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
+ return i;
+ }
+
+ return GNUNET_SYSERR;
+}
+
+
+/**
+ * Iterate over the list of all the trails to reach Finger. In case the first
+ * friend to reach the finger has crossed the trail threshold or is congested,
+ * then don't select it. In case there multiple available good trails to reach
+ * to Finger, choose the one with shortest trail length.
+ * @param finger Finger
+ * @return struct Correct_Trail which contains the first friend , trail id
+ * and trail length. NULL in case none of the trails are free.
+ */
+static struct Correct_Trail *
+select_trail_to_finger (struct FingerInfo *finger)
+{
+ struct FriendInfo *friend;
+ struct TrailList *iterator;
+ struct Correct_Trail *finger_trail;
+ int i;
- /* Element stored at this index in friend_peermap should be selected friend. */
- index = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
+ &my_identity))
+ return NULL;
- /* Create an iterator for friend_peermap. */
- iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
+ finger_trail = GNUNET_new (struct Correct_Trail);
- /* Set the position of iterator to index. */
- while(j < (*index))
+ for (i = 0; i < finger->trails_count; i++)
{
- if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iter,NULL,NULL))
+ iterator = &finger->trail_list[i];
+ if (iterator->trail_length > 0)
{
- j++;
+ friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &iterator->trail_head->peer);
+ }
+ else
+ {
+ friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &finger->finger_identity);
+ }
+
+ if ((friend->trails_count < TRAIL_THROUGH_FRIEND_THRESHOLD)||
+ ((0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_duration).rel_value_us)))
+ {
+ if (iterator->trail_length == 0)
+ {
+ finger_trail->friend = *friend;
+ //finger_trail->trail_id = 0;
+ finger_trail->trail_length = 0;
+ return finger_trail;
+ }
+
+ if (finger_trail->trail_length > iterator->trail_length)
+ {
+ finger_trail->friend = *friend;
+ finger_trail->trail_id = iterator->trail_id;
+ finger_trail->trail_length = iterator->trail_length;
+ }
}
- else
- return NULL;
- }
-
- if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iter,&key_ret,(const void **)&friend))
- {
- return friend;
}
-
- return NULL;
+
+ return finger_trail;
}
/**
- * Compute finger_identity to which we want to setup the trail
- * @return finger_identity
+ * Select closest finger to value.
+ * @param peer1 First peer
+ * @param peer2 Second peer
+ * @param value Value to be compare
+ * @return Closest peer
*/
-static uint64_t *
-compute_finger_identity()
+static struct GNUNET_PeerIdentity *
+select_closest_finger (struct GNUNET_PeerIdentity *peer1,
+ struct GNUNET_PeerIdentity *peer2,
+ uint64_t value)
{
- uint64_t *my_id64 ;
- uint64_t *finger_identity64;
-
- my_id64 = GNUNET_malloc (sizeof (uint64_t));
- finger_identity64 = GNUNET_malloc (sizeof (uint64_t));
+ uint64_t peer1_value;
+ uint64_t peer2_value;
+
+ memcpy (&peer1_value, peer1, sizeof (uint64_t));
+ memcpy (&peer2_value, peer2, sizeof (uint64_t));
+
+ if ((peer1_value <= value) && (value <= peer2_value))
+ return peer2;
+ else if ((peer2_value <= value) && (value <= peer1_value))
+ return peer1;
+ else if ((peer1_value <= peer2_value) && (peer2_value <= value))
+ return peer1;
+ else if ((peer2_value <= peer1_value) && (peer1_value <= value))
+ return peer2;
+ else if ((value <= peer1_value) && (peer1_value <= peer2_value))
+ return peer1;
+ else /*if ((value <= peer2_value) && (peer2_value <= peer1_value))*/
+ return peer2;
+}
- memcpy (my_id64, &(my_identity.public_key.q_y), sizeof (uint64_t));
- *finger_identity64 = fmod ((*my_id64 + pow (2,current_finger_index)),( (pow (2,MAX_FINGERS))));
-
- return finger_identity64;
+
+/**
+ * Select closest predecessor to value.
+ * @param peer1 First peer
+ * @param peer2 Second peer
+ * @param value Value to be compare
+ * @return Closest peer
+ */
+static struct GNUNET_PeerIdentity *
+select_closest_predecessor (struct GNUNET_PeerIdentity *peer1,
+ struct GNUNET_PeerIdentity *peer2,
+ uint64_t value)
+{
+ uint64_t peer1_value;
+ uint64_t peer2_value;
+
+ memcpy (&peer1_value, peer1, sizeof (uint64_t));
+ memcpy (&peer2_value, peer2, sizeof (uint64_t));
+
+ if ((peer1_value <= value) && (value <= peer2_value))
+ return peer1;
+ else if ((peer2_value <= value) && (value <= peer1_value))
+ return peer2;
+ else if ((peer1_value <= peer2_value) && (peer2_value <= value))
+ return peer2;
+ else if ((peer2_value <= peer1_value) && (peer1_value <= value))
+ return peer1;
+ else if ((value <= peer1_value) && (peer1_value <= peer2_value))
+ return peer2;
+ else /*if ((value <= peer2_value) && (peer2_value <= peer1_value))*/
+ return peer1;
}
/**
- * Compute immediate predecessor identity in the network.
- * @return peer identity of immediate predecessor.
+ * Select the closest peer among two peers (which should not be same)
+ * with respect to value and finger_map_index
+ * @param peer1 First peer
+ * @param peer2 Second peer
+ * @param value Value relative to which we find the closest
+ * @param finger_map_index Index in finger map. If equal to PREDECESSOR_FINGER_ID,
+ * then we use different logic than other
+ * finger_map_index
+ * @return Closest peer among two peers.
*/
-static uint64_t *
-compute_predecessor_identity()
+static struct GNUNET_PeerIdentity *
+select_closest_peer (struct GNUNET_PeerIdentity *peer1,
+ struct GNUNET_PeerIdentity *peer2,
+ uint64_t value,
+ unsigned int finger_map_index)
{
- uint64_t *my_id ;
- uint64_t *predecessor;
+ struct GNUNET_PeerIdentity *closest_peer;
- my_id = GNUNET_malloc (sizeof (uint64_t));
- predecessor = GNUNET_malloc (sizeof (uint64_t));
+ if (PREDECESSOR_FINGER_ID == finger_map_index)
+ closest_peer = select_closest_predecessor (peer1, peer2, value);
+ else
+ closest_peer = select_closest_finger (peer1, peer2, value);
- memcpy (my_id, &(my_identity.public_key.q_y), sizeof (uint64_t));
- *predecessor = fmod ((*my_id -1), (pow (2,MAX_FINGERS)));
-
- return predecessor;
+ return closest_peer;
}
/**
- * SUPU: You should pass the trail index from where next peer should read. read
- * position should be set and after you read you should update the read position
- * for next peer in the trail list.
- * Periodically ping your successor to ask its current predecessor
- *
- * @param cls closure for this task
- * @param tc the context under which the task is running
+ * Find the successor for destination_finger_value among my_identity, all my
+ * friend and all my fingers. Don't consider friends/ fingers with first friend in
+ * the trail which are congested or have crossed the threshold.
+ * @param destination_finger_value Peer closest to this value will be the next successor.
+ * @param next_destination [out] Updated to friend identity in case a friend is
+ * successor, updated to first friend to reach to finger
+ * in case finger is the destination.
+ * @param new_intermediate_trail_id [out] In case we finger is the @a next_destination,
+ * then we updated the field with trail id to reach
+ * to that finger.
+ * @param finger_map_index Index in finger peermap for which we are looking for a finger.
+ * @return
*/
-static void
-send_verify_successor_message (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc )
+static struct GNUNET_PeerIdentity *
+find_successor (uint64_t destination_finger_value,
+ struct GNUNET_PeerIdentity *next_destination,
+ struct GNUNET_HashCode *new_intermediate_trail_id,
+ unsigned int finger_map_index)
{
- struct GNUNET_TIME_Relative next_send_time;
- struct GNUNET_CONTAINER_MultiPeerMapIterator *finger_iter;
- struct GNUNET_PeerIdentity key_ret;
- struct FriendInfo *target_friend;
+ struct Closest_Peer *successor;
struct GNUNET_PeerIdentity *next_hop;
- struct GNUNET_PeerIdentity *peer_list;
- unsigned int finger_trail_current_index;
+ struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter;
+ struct GNUNET_CONTAINER_MultiHashMap32Iterator *finger_iter;
+ struct GNUNET_PeerIdentity *closest_peer;
+ struct Correct_Trail *finger_trail;
+ struct FriendInfo *friend;
struct FingerInfo *finger;
- unsigned int finger_index;
- unsigned int i;
+ int i;
+
+ successor = GNUNET_new (struct Closest_Peer);
+ memcpy (&successor->value, &my_identity, sizeof (uint64_t));
+ //successor->trail_id = 0;
+ successor->next_hop = my_identity;
+ successor->next_destination = my_identity;
- finger_iter = GNUNET_CONTAINER_multipeermap_iterator_create (finger_peermap);
- for (finger_index = 0; finger_index < GNUNET_CONTAINER_multipeermap_size (finger_peermap); finger_index++)
+ friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
+ for (i= 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
{
- if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (finger_iter, &key_ret,
- (const void **)&finger))
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter, NULL,
+ (const void **)&friend));
+ if ((friend->trails_count > TRAIL_THROUGH_FRIEND_THRESHOLD)||
+ (0 != GNUNET_TIME_absolute_get_remaining (friend->congestion_duration).rel_value_us))
+ continue;
+
+ closest_peer = select_closest_peer (&my_identity, &friend->id,
+ destination_finger_value,
+ finger_map_index);
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, &friend->id))
{
- if (1 == finger->successor)
- break;
- }
+ memcpy (&successor->value, &friend->id, sizeof (uint64_t));
+ //successor->trail_id = 0;
+ successor->next_hop = friend->id;
+ successor->next_destination = friend->id;
+ }
}
- GNUNET_CONTAINER_multipeermap_iterator_destroy (finger_iter);
-
- peer_list = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * finger->trail_length);
- /* Iterate over your linked list of trail and copy it into peer_list. */
- struct TrailPeerList *iterate;
- iterate = finger->head;
- i = 0;
- while ( i < (finger->trail_length))
+ finger_iter = GNUNET_CONTAINER_multihashmap32_iterator_create (finger_hashmap);
+ for (i = 0; i < GNUNET_CONTAINER_multihashmap32_size (finger_hashmap); i++)
{
- memcpy (&peer_list[i], &(iterate->peer), sizeof (struct GNUNET_PeerIdentity));
- iterate = iterate->next;
- i++;
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap32_iterator_next (finger_iter, NULL,
+ (void *)&finger));
+ finger_trail = select_trail_to_finger (finger);
+ if (NULL == finger_trail)
+ continue;
+
+ closest_peer = select_closest_peer (&my_identity,
+ &finger->finger_identity,
+ destination_finger_value,
+ finger_map_index);
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer,
+ &finger->finger_identity))
+ {
+ memcpy (&successor->value, &finger->finger_identity, sizeof (uint64_t));
+ successor->trail_id = finger_trail->trail_id;
+ successor->next_hop = finger_trail->friend.id;
+ successor->next_destination = finger->finger_identity;
+ }
}
-
- /* element stored at location 0 is my own identity. element stored at location 1
- is the next hop. */
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- memcpy (next_hop, &peer_list[1], sizeof (struct GNUNET_PeerIdentity));
+
+ next_destination = &successor->next_destination;
+ new_intermediate_trail_id = &successor->trail_id;
+ next_hop = &successor->next_hop;
+
+ return next_hop;
+}
+/**
+ * Construct a Put message and send it to target_peer.
+ * @param key Key for the content
+ * @param block_type Type of the block
+ * @param options Routing options
+ * @param desired_replication_level Desired replication count
+ * @param current_destination Next current destination which will get this message.
+ * @param current_source Source for @a current_destination
+ * @param target_peer Peer to which this message will be forwarded.
+ * @param hop_count Number of hops traversed so far.
+ * @param put_path_length Total number of peers in @a put_path
+ * @param put_path Number of peers traversed so far
+ * @param expiration_time When does the content expire
+ * @param data Content to store
+ * @param data_size Size of content @a data in bytes
+ */
+void
+GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
+ enum GNUNET_BLOCK_Type block_type,
+ enum GNUNET_DHT_RouteOption options,
+ uint32_t desired_replication_level,
+ struct GNUNET_PeerIdentity current_destination,
+ struct GNUNET_PeerIdentity current_source,
+ struct GNUNET_PeerIdentity *target_peer,
+ uint32_t hop_count,
+ uint32_t put_path_length,
+ struct GNUNET_PeerIdentity *put_path,
+ struct GNUNET_TIME_Absolute expiration_time,
+ const void *data, size_t data_size)
+{
- /* Find the friend corresponding to this next hop. */
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
- finger_trail_current_index = 2;
- GDS_NEIGHBOURS_handle_verify_successor (&my_identity,
- &(finger->finger_identity),
- target_friend,
- peer_list,
- finger->trail_length,
- finger_trail_current_index);
+}
+
+/**
+ * Construct a Get message and send it to target_peer.
+ * @param key Key for the content
+ * @param block_type Type of the block
+ * @param options Routing options
+ * @param desired_replication_level Desired replication count
+ * @param current_destination Next current destination which will get this message.
+ * @param current_source Source for @a current_destination
+ * @param target_peer Peer to which this message will be forwarded.
+ * @param hop_count Number of hops traversed so far.
+ * @param data Content to store
+ * @param data_size Size of content @a data in bytes
+ * @param get_path_length Total number of peers in @a get_path
+ * @param get_path Number of peers traversed so far
+ */
+void
+GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
+ enum GNUNET_BLOCK_Type block_type,
+ enum GNUNET_DHT_RouteOption options,
+ uint32_t desired_replication_level,
+ struct GNUNET_PeerIdentity current_destination,
+ struct GNUNET_PeerIdentity current_source,
+ struct GNUNET_PeerIdentity *target_peer,
+ uint32_t hop_count,
+ uint32_t get_path_length,
+ struct GNUNET_PeerIdentity *get_path)
+{
+}
+
+
+/**
+ * Send the get result to requesting client.
+ * @param key Key of the requested data.
+ * @param type Block type
+ * @param target_peer Next peer to forward the message to.
+ * @param source_peer Peer which has the data for the key.
+ * @param put_path_length Number of peers in @a put_path
+ * @param put_path Path taken to put the data at its stored location.
+ * @param get_path_length Number of peers in @a get_path
+ * @param get_path Path taken to reach to the location of the key.
+ * @param expiration When will this result expire?
+ * @param data Payload to store
+ * @param data_size Size of the @a data
+ */
+void
+GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
+ enum GNUNET_BLOCK_Type type,
+ struct GNUNET_PeerIdentity *target_peer,
+ struct GNUNET_PeerIdentity *source_peer,
+ unsigned int put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int get_path_length,
+ struct GNUNET_PeerIdentity *get_path,
+ struct GNUNET_TIME_Absolute expiration,
+ const void *data, size_t data_size)
+{
- /* FIXME: Use a random value so that this message is send not at the same
- interval as send_find_finger_trail_message. */
- next_send_time.rel_value_us =
- DHT_MINIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
- GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
- DHT_MAXIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us /
- (current_finger_index + 1));
+}
+
+
+/**
+ * Randomly choose one of your friends (which is not congested and have not crossed
+ * trail threshold) from the friends_peer map
+ * @return Friend Randomly chosen friend.
+ * NULL in case friend peermap is empty, or all the friends are either
+ * congested or have crossed trail threshold.
+ */
+static struct FriendInfo *
+select_random_friend ()
+{
+ unsigned int current_size;
+ uint32_t index;
+ unsigned int j = 0;
+ struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
+ struct GNUNET_PeerIdentity key_ret;
+ struct FriendInfo *friend;
+
+ current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
+ if (0 == current_size)
+ return NULL;
+
+ index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
+ iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
- verify_successor =
- GNUNET_SCHEDULER_add_delayed (next_send_time, &send_verify_successor_message,
- NULL);
+ for (j = 0; j < index ; j++)
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL, NULL));
+ do
+ {
+ if (j == current_size)
+ {
+ j = 0;
+ GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
+ iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
+
+ }
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_iterator_next (iter,
+ &key_ret,
+ (const void **)&friend));
+
+
+ if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) &&
+ (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_duration).rel_value_us))
+ {
+ break;
+ }
+ friend = NULL;
+ j++;
+ } while (j != index);
+
+ GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
+ return friend;
}
/**
- * Task to send a find finger trail message. We attempt to find trail
- * to our fingers, successor and predecessor in the network.
+ * Compute finger_identity to which we want to setup the trail
+ * @return finger_identity
+ */
+static uint64_t
+compute_finger_identity()
+{
+ uint64_t my_id64;
+
+ memcpy (&my_id64, &my_identity, sizeof (uint64_t));
+ my_id64 = GNUNET_ntohll (my_id64);
+ return (my_id64 + (unsigned long) pow (2, current_search_finger_index));
+}
+
+
+/**
+ * Compute immediate predecessor identity in the network.
+ * @return peer identity of immediate predecessor.
+ */
+static uint64_t
+compute_predecessor_identity()
+{
+ uint64_t my_id64;
+
+ memcpy (&my_id64, &my_identity, sizeof (uint64_t));
+ my_id64 = GNUNET_ntohll (my_id64);
+ return (my_id64 -1);
+}
+
+
+/*
+ * Choose a random friend and start looking for the trail to reach to
+ * finger identity through this random friend.
*
* @param cls closure for this task
* @param tc the context under which the task is running
{
struct FriendInfo *target_friend;
struct GNUNET_TIME_Relative next_send_time;
- struct GNUNET_PeerIdentity *peer_list;
- unsigned int successor_flag;
- unsigned int predecessor_flag;
- uint64_t *finger_identity;
- unsigned int finger_index;
+ struct GNUNET_HashCode trail_id;
+ uint64_t trail_id_value;
+ struct GNUNET_HashCode intermediate_trail_id;
+ unsigned int finger_map_index;
+ uint64_t finger_identity;
+
+ next_send_time.rel_value_us =
+ DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
+ GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+ DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
+ find_finger_trail_task =
+ GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_finger_trail_message,
+ NULL);
- /* Initialize flag values */
- predecessor_flag = 0;
- successor_flag = 0;
+ target_friend = select_random_friend ();
+ if (NULL == target_friend)
+ {
+ return;
+ }
- if (1 == current_finger_index)
+ if (PREDECESSOR_FINGER_ID == current_search_finger_index)
{
- /* We have started the process to find the successor. We should search
- for our predecessor. */
finger_identity = compute_predecessor_identity();
- predecessor_flag = 1;
- goto select_friend;
}
else
{
finger_identity = compute_finger_identity();
}
+
+ finger_map_index = current_search_finger_index;
- if(0 == current_finger_index)
+ trail_id_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX);
+ memcpy (&trail_id, &trail_id_value, sizeof (struct GNUNET_HashCode));
+ GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_identity,
+ target_friend->id, target_friend, 0, NULL,
+ finger_map_index, trail_id, intermediate_trail_id);
+}
+
+
+/**
+ * In case there are already maximum number of possible trail to reach to a finger,
+ * then check if the new trail's length is lesser than any of the existing trails.
+ * If yes then replace that old trail by new trail.
+ * Note: Here we are taking length as a parameter to choose the best possible trail,
+ * but there could be other parameters also like - 1. duration of existence of a
+ * trail - older the better. 2. if the new trail is completely disjoint than the
+ * other trails, then may be choosing it is better.
+ * @param existing_finger
+ * @param new_finger_trail
+ * @param new_finger_trail_length
+ * @param new_finger_trail_id
+ */
+static void
+select_and_replace_trail (struct FingerInfo *existing_finger,
+ struct GNUNET_PeerIdentity *new_trail,
+ unsigned int new_trail_length,
+ struct GNUNET_HashCode new_trail_id)
+{
+ struct TrailList *trail_list_iterator;
+ unsigned int largest_trail_length;
+ unsigned int largest_trail_index;
+ struct Trail *trail_element;
+ unsigned int i;
+
+ largest_trail_length = new_trail_length;
+ largest_trail_index = MAXIMUM_TRAILS_PER_FINGER + 1;
+
+ GNUNET_assert (MAXIMUM_TRAILS_PER_FINGER == existing_finger->trails_count);
+
+ for (i = 0; i < existing_finger->trails_count; i++)
{
- /* We are searching for our successor in the network. */
- successor_flag = 1;
+ trail_list_iterator = &existing_finger->trail_list[i];
+ if (trail_list_iterator->trail_length > largest_trail_length)
+ {
+ largest_trail_length = trail_list_iterator->trail_length;
+ largest_trail_index = i;
+ }
}
- select_friend:
- finger_index = current_finger_index;
- current_finger_index = ( current_finger_index + 1) % MAX_FINGERS;
+ if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
+ return;
- target_friend = select_random_friend();
-
- /* We found a friend.*/
- if(NULL != target_friend)
- {
- /* Add yourself and selected friend in the trail list. */
- unsigned int trail_length = 2;
- peer_list = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * trail_length);
- memcpy (&peer_list[0], &(my_identity), sizeof (struct GNUNET_PeerIdentity));
- memcpy (&peer_list[1], &(target_friend->id), sizeof (struct GNUNET_PeerIdentity));
+ /* Send trail teardown message across the replaced trail. */
+ struct TrailList *replace_trail = &existing_finger->trail_list[largest_trail_index];
+ struct FriendInfo *target_friend =
+ GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &replace_trail->trail_head->peer);
+
+ GDS_NEIGHBOURS_send_trail_teardown (my_identity,
+ existing_finger->finger_identity,
+ replace_trail->trail_id,
+ GDS_ROUTING_SRC_TO_DEST, target_friend);
+ /* Free the trail .*/
+ while (NULL != (trail_element = replace_trail->trail_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (replace_trail->trail_head,
+ replace_trail->trail_tail, trail_element);
+ GNUNET_free (trail_element);
+ }
+
+ /* Add new trial at that location. */
+ i = 0;
+ while ( i < new_trail_length)
+ {
+ struct Trail *element = GNUNET_new (struct Trail);
+ element->next = NULL;
+ element->prev = NULL;
+ element->peer = new_trail[0];
- GDS_NEIGHBOURS_handle_trail_setup (&my_identity, finger_identity,
- target_friend, trail_length, peer_list,
- successor_flag, predecessor_flag,
- finger_index);
+ GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
+ replace_trail->trail_tail,
+ element);
}
+}
+
+
+/**
+ * Check if the new trail to reach to finger is unique or do we already have
+ * such a trail present for finger.
+ * @param existing_finger Finger identity
+ * @param new_trail New trail to reach @a existing_finger
+ * @param trail_length Total number of peers in new_trail.
+ * @return #GNUNET_YES if the new trail is unique
+ * #GNUNET_NO if same trail is already present.
+ */
+static int
+is_new_trail_unique (struct FingerInfo *existing_finger,
+ struct GNUNET_PeerIdentity *new_trail,
+ unsigned int trail_length)
+{
+ struct TrailList *trail_list_iterator;
+ struct Trail *trail_element;
+ int i;
+ int j;
+ int trail_unique = GNUNET_NO;
- /* FIXME: Should we be using current_finger_index to generate random interval.*/
- next_send_time.rel_value_us =
- DHT_MINIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
- GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
- DHT_MAXIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us /
- (current_finger_index + 1));
-
- find_finger_trail_task =
- GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_finger_trail_message,
- NULL);
+ for (i = 0; i < existing_finger->trails_count; i++)
+ {
+ trail_list_iterator = &existing_finger->trail_list[i];
+ trail_element = trail_list_iterator->trail_head;
+ for (j = 0; j < trail_list_iterator->trail_length; j++)
+ {
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (&new_trail[j],
+ &trail_element->peer))
+ {
+ trail_unique = GNUNET_YES;
+ break;
+ }
+ }
+ }
+ return trail_unique;
}
/**
- * Method called whenever a peer connects.
- *
- * @param cls closure
- * @param peer_identity peer identity this notification is about
+ * Add a new trail to existing finger.
+ * @param existing_finger
+ * @param new_finger_trail
+ * @param new_finger_trail_length
+ * @param new_finger_trail_id
*/
static void
-handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
+add_new_trail (struct FingerInfo *existing_finger,
+ struct GNUNET_PeerIdentity *new_trail,
+ unsigned int new_trail_length,
+ struct GNUNET_HashCode new_trail_id)
{
- struct FriendInfo *friend;
-
- /* Check for connect to self message */
- if (0 == memcmp (&my_identity, peer_identity, sizeof (struct GNUNET_PeerIdentity)))
+ struct TrailList *trail_list_iterator;
+ struct FriendInfo *first_friend;
+ int i = 0;
+ int j;
+
+ if (GNUNET_NO == is_new_trail_unique (existing_finger,
+ new_trail,
+ new_trail_length))
return;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to %s\n", GNUNET_i2s (peer_identity));
+ do
+ {
+ trail_list_iterator = &existing_finger->trail_list[i];
+ i++;
+ } while (trail_list_iterator->trail_head != NULL);
- /* If peer already exists in our friend_peermap, then exit. */
- if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap, peer_identity))
+ if (new_trail_length > 0)
+ first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &new_trail[0]);
+ else
+ first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &(existing_finger->finger_identity));
+ first_friend->trails_count++;
+ trail_list_iterator->first_friend_trail_count = first_friend->trails_count;
+ trail_list_iterator->trail_length = new_trail_length;
+
+ for (j = 0; j < new_trail_length; j++)
{
- GNUNET_break (0);
- return;
+ struct Trail *element;
+ element = GNUNET_new (struct Trail);
+
+ element->next = NULL;
+ element->prev = NULL;
+ element->peer = new_trail[j];
+ GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head,
+ trail_list_iterator->trail_tail,
+ element);
}
+ existing_finger->trails_count++;
+}
- GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1,
- GNUNET_NO);
+/**
+ * Send trail teardown message on all trails associated with finger.
+ * @param finger_to_be_removed
+ */
+static void
+send_trail_teardown (struct FingerInfo *finger)
+{
+ struct TrailList *trail_list_iterator;
+ struct GNUNET_HashCode trail_id;
+ struct FriendInfo *target_friend;
+ int i;
- friend = GNUNET_new (struct FriendInfo);
- friend->id = *peer_identity;
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity, &my_identity)
+ || (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &finger->finger_identity)))
+ return;
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CONTAINER_multipeermap_put (friend_peermap,
- peer_identity, friend,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ for (i = 0; i < finger->trails_count; i++)
+ {
+ trail_list_iterator = &finger->trail_list[i];
+ if (trail_list_iterator->trail_length > 0)
+ {
+ trail_id = trail_list_iterator->trail_id;
+ target_friend =
+ GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &trail_list_iterator->trail_head->peer);
+ GDS_NEIGHBOURS_send_trail_teardown (my_identity, finger->finger_identity,
+ trail_id, GDS_ROUTING_SRC_TO_DEST,
+ target_friend);
+ }
+ }
+}
+
+/**
+ * Decrement the trail count of the first friend to reach the finger
+ * In case finger is the friend, then decrement its trail count.
+ * @param finger
+ */
+static void
+decrement_friend_trail_count (struct FingerInfo *finger)
+{
+ struct TrailList *trail_list_iterator;
+ struct FriendInfo *target_friend;
+ int i = 0;
- /* got a first connection, good time to start with FIND FINGER TRAIL requests... */
- if (1 == GNUNET_CONTAINER_multipeermap_size (friend_peermap))
- find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
+ &my_identity))
+ return;
+
+ for (i = 0; i < finger->trails_count; i++)
+ {
+ trail_list_iterator = &finger->trail_list[i];
+ if (trail_list_iterator->trail_length > 0)
+ target_friend =
+ GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &trail_list_iterator->trail_head->peer);
+ else
+ target_friend =
+ GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &finger->finger_identity);
+
+ target_friend->trails_count--;
+ trail_list_iterator->first_friend_trail_count--;
+ }
+ return;
}
/**
- * Method called whenever a peer disconnects.
- *
- * @param cls closure
- * @param peer peer identity this notification is about
+ * Free finger and its trail.
+ * @param finger Finger to be freed.
*/
static void
-handle_core_disconnect (void *cls,
- const struct GNUNET_PeerIdentity *peer)
+free_finger (struct FingerInfo *finger)
{
- struct FriendInfo *remove_friend;
+ struct TrailList *trail_list_iterator;
+ struct Trail *trail_element;
+ unsigned int i;
- /* Check for self message. */
- if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
- return;
+ for (i = 0; i < finger->trails_count; i++)
+ {
+ trail_list_iterator = &finger->trail_list[i];
+ while (NULL != (trail_element = trail_list_iterator->trail_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (trail_list_iterator->trail_head,
+ trail_list_iterator->trail_tail, trail_element);
+ GNUNET_free (trail_element);
+ }
+ }
+ GNUNET_free (finger);
+}
+
+
+/**
+ * Check if new finger is closer than existing_finger. If both new finger and
+ * existing finger are same then we may add a new trail (if there is space)
+ * or choose the best trail among existing trails and new trails.
+ * @param existing_finger Finger present in finger_peermap at @a finger_map_index
+ * @param new_finger_identity Peer identity of new finger.
+ * @param new_finger_trail Trail to reach from source to new_finger.
+ * @param new_finger_trail_length Total number of peers in @a new_finger_trail.
+ * @param trail_id Unique identifier of trail.
+ * @param finger_map_index Index in finger map.
+ * @return #GNUNET_YES if the new finger is closest.
+ * #GNUNET_NO either new_finger and existing_finger are same, or
+ * existing_finger is closest.
+ */
+static int
+is_new_finger_closest (struct FingerInfo *existing_finger,
+ struct GNUNET_PeerIdentity new_finger_identity,
+ struct GNUNET_PeerIdentity *new_finger_trail,
+ unsigned int new_finger_trail_length,
+ struct GNUNET_HashCode new_finger_trail_id,
+ unsigned int finger_map_index)
+{
+ struct GNUNET_PeerIdentity *closest_peer;
+ uint64_t my_id64;
- /* Search for peer to remove in your friend_peermap. */
- remove_friend =
- GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
+ if (NULL == existing_finger)
+ return GNUNET_YES;
- if (NULL == remove_friend)
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
+ &new_finger_identity))
{
- GNUNET_break (0);
- return;
+ memcpy (&my_id64, &my_identity, sizeof (uint64_t));
+ closest_peer = select_closest_peer (&existing_finger->finger_identity,
+ &new_finger_identity,
+ my_id64, finger_map_index);
+
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity, closest_peer))
+ {
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
+ &new_finger_identity)) /* FIXME: not sure what to do here? */
+ return GNUNET_NO;
+
+ send_trail_teardown (existing_finger);
+ decrement_friend_trail_count (existing_finger);
+ free_finger (existing_finger);
+ return GNUNET_YES;
+ }
}
+ else
+ {
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
+ &my_identity))
+ {
+ if (NULL ==
+ GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &(existing_finger->finger_identity)))
+ {
+ if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
+ add_new_trail (existing_finger, new_finger_trail,
+ new_finger_trail_length, new_finger_trail_id);
+ else
+ select_and_replace_trail (existing_finger, new_finger_trail,
+ new_finger_trail_length, new_finger_trail_id);
+ }
+ }
+ }
+ return GNUNET_NO;
+}
+
+
+/**
+ * Add a new entry in finger hashmap at finger_map_index
+ * @param finger_identity Peer Identity of new finger
+ * @param finger_trail Trail to reach from me to finger (excluding both end points).
+ * @param finger_trail_length Total number of peers in @a finger_trail.
+ * @param trail_id Unique identifier of the trail.
+ * @param finger_map_index Index in finger hashmap.
+ * @return #GNUNET_OK if new entry is added
+ * #GNUNET_NO -- FIXME: need to check what value does hahsmap put
+ * returns on failure.
+ */
+static int
+add_new_entry (struct GNUNET_PeerIdentity finger_identity,
+ struct GNUNET_PeerIdentity *finger_trail,
+ unsigned int finger_trail_length,
+ struct GNUNET_HashCode trail_id,
+ unsigned int finger_map_index)
+{
+ struct FingerInfo *new_entry;
+ struct FriendInfo *first_trail_hop;
+ struct TrailList *first_trail;
+ int i = 0;
- /* Remove the friend from friend_peermap. */
- GNUNET_assert (GNUNET_YES ==
- GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
- peer,
- remove_friend));
+ new_entry = GNUNET_new (struct FingerInfo);
+ new_entry->finger_identity = finger_identity;
+ new_entry->finger_map_index = finger_map_index;
+ new_entry->trails_count = 1;
- /* If the peer is removed then all the trail which goes through this
- peer also becomes invalid. */
- /* FIXME: Iterate over finger peermap, get the trail index and find all the
- finger whose trail's first peer was this peer. and remove them from finger
- peermap. Assumption that in send_find_finger_trail we will eventually reach
- to this finger and we will setup up the new trail.
- So, we need a threshold on number of trail thats can go through a node
- so that if that nodes go away then also our system is up and runnning.
- Where can we specify that threshold.*/
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
+ {
+ if (finger_trail_length > 0)
+ {
+ first_trail_hop = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &finger_trail[0]);
+ }
+ else
+ {
+ first_trail_hop = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &finger_identity);
+ }
+
+ first_trail_hop->trails_count++;
+ first_trail = &new_entry->trail_list[0];
+ first_trail->first_friend_trail_count = first_trail_hop->trails_count;
+
+ while (i < finger_trail_length)
+ {
+ struct Trail *element = GNUNET_new (struct Trail);
+
+ element->next = NULL;
+ element->prev = NULL;
+ element->peer = finger_trail[i];
+ GNUNET_CONTAINER_DLL_insert_tail (first_trail->trail_head,
+ first_trail->trail_tail,
+ element);
+ i++;
+ }
+ }
+
+ return GNUNET_CONTAINER_multihashmap32_put (finger_hashmap,
+ new_entry->finger_map_index,
+ new_entry,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
}
/**
- * To be called on core init/fail.
- *
- * @param cls service closure
- * @param identity the public identity of this peer
+ * Scan the trail to check if there is any other friend in the trail other than
+ * first hop. If yes the shortcut the trail, send trail compression message to
+ * peers which are no longer part of trail and send back the updated trail
+ * and trail_length to calling function.
+ * @param finger_identity Finger whose trail we will scan.
+ * @param finger_trail [in, out] Trail to reach from source to finger,
+ * @param finger_trail_length Total number of peers in original finger_trail.
+ * @param finger_trail_id Unique identifier of the finger trail.
+ * @return updated trail length in case we shortcut the trail, else original
+ * trail length.
+ */
+static int
+scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
+ struct GNUNET_PeerIdentity *trail,
+ unsigned int trail_length,
+ struct GNUNET_HashCode trail_id)
+{
+ struct FriendInfo *target_friend;
+ int i;
+
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
+ {
+ trail = NULL;
+ return 0;
+ }
+
+ if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger_identity))
+ {
+ if (trail_length > 0)
+ {
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &trail[0]);
+ GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity,
+ trail_id, finger_identity,
+ target_friend);
+ trail = NULL;
+ }
+ return 0;
+ }
+
+ for ( i = trail_length - 1; i > 0; i--)
+ {
+ if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail[i]))
+ {
+ struct FriendInfo *target_friend;
+ int j = 0;
+
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &trail[0]);
+ GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity,
+ trail_id, trail[i],
+ target_friend);
+
+ /* Copy the trail from index i to index trail_length -1 and change
+ trail length and return */
+ while (i < trail_length)
+ {
+ memcpy (&trail[j], &trail[i], sizeof(struct GNUNET_PeerIdentity));
+ j++;
+ i++;
+ }
+ trail_length = j+1;
+ break;
+ }
+ }
+ return trail_length;
+}
+
+
+/**
+ * Send verify successor message to your successor on all trails to reach successor.
+ * @param successor My current successor
*/
static void
-core_init (void *cls,
- const struct GNUNET_PeerIdentity *identity)
+send_verify_successor_message (struct FingerInfo *successor)
{
- my_identity = *identity;
+ struct TrailList *trail_list_iterator;
+ struct GNUNET_HashCode trail_id;
+ struct GNUNET_PeerIdentity next_hop;
+ struct FriendInfo *target_friend;
+ struct GNUNET_PeerIdentity *trail;
+ unsigned int trail_length;
+ int i;
+ int j;
+
+ for (i = 0; i < successor->trails_count; i++)
+ {
+ trail_list_iterator = &successor->trail_list[i];
+
+ if (trail_list_iterator->trail_length > 0)
+ {
+ trail_length = trail_list_iterator->trail_length;
+ j = 0;
+ trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
+ trail_length);
+ struct Trail *element;
+ element = trail_list_iterator->trail_head;
+ while ( j < trail_length)
+ {
+ trail[j] = element->peer;
+ element = element->next;
+ j++;
+ }
+ next_hop = trail_list_iterator->trail_head->peer;
+ }
+ else
+ {
+ trail = NULL;
+ trail_length = 0;
+ next_hop = successor->finger_identity;
+ }
+ trail_id = trail_list_iterator->trail_id;
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
+ GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
+ successor->finger_identity,
+ trail_id, trail, trail_length,
+ target_friend);
+ }
+}
+
+
+/**
+ * Check if there is already an entry in finger peermap for given finger map index.
+ * If yes, then select the closest finger. If new and existing finger are same,
+ * the check if you can store more trails. If yes then add trail, else keep the best
+ * trails to reach to the finger. If the new finger is closest, add it.
+ * Then, update current_search_finger_index.
+ * @param new_finger_identity Peer Identity of new finger
+ * @param new_finger_trail Trail to reach the new finger
+ * @param new_finger_length Total number of peers in @a new_finger_trail.
+ * @param finger_map_index Index in finger peermap.
+ * @param new_finger_trail_id Unique identifier of @new_finger_trail.
+ * @return #GNUNET_YES if the new entry is added
+ * #GNUNET_NO if new entry is not added, either it was discarded or
+ * it was same as existing finger at finger map index.
+ */
+static int
+finger_table_add (struct GNUNET_PeerIdentity new_finger_identity,
+ struct GNUNET_PeerIdentity *new_finger_trail,
+ unsigned int new_finger_trail_length,
+ unsigned int finger_map_index,
+ struct GNUNET_HashCode new_finger_trail_id)
+{
+ struct FingerInfo *existing_finger;
+ struct FingerInfo *successor;
+ unsigned int new_entry_added = GNUNET_NO;
+
+ int new_finger_updated_trail_length =
+ scan_and_compress_trail (new_finger_identity, new_finger_trail,
+ new_finger_trail_length, new_finger_trail_id);
+
+ existing_finger = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
+ finger_map_index);
+
+ if (GNUNET_YES == is_new_finger_closest (existing_finger,
+ new_finger_identity,
+ new_finger_trail,
+ new_finger_updated_trail_length,
+ new_finger_trail_id, finger_map_index))
+ {
+ GNUNET_assert (GNUNET_YES == add_new_entry (new_finger_identity,
+ new_finger_trail,
+ new_finger_updated_trail_length,
+ new_finger_trail_id,
+ finger_map_index));
+ new_entry_added = GNUNET_YES;
+ }
+
+ successor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
+ finger_map_index);
+ if (0 == finger_map_index)
+ {
+ current_search_finger_index = PREDECESSOR_FINGER_ID;
+
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,&new_finger_identity))
+ {
+ send_verify_successor_message (successor);
+ }
+ }
+ else if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity,
+ &(successor->finger_identity)))
+ {
+ current_search_finger_index = 0;
+ }
+ else
+ current_search_finger_index = current_search_finger_index - 1;
+
+ return new_entry_added;
+}
+
+
+/**
+ * Core handler for P2P put messages.
+ * @param cls closure
+ * @param peer sender of the request
+ * @param message message
+ * @return #GNUNET_OK to keep the connection open,
+ * #GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ return GNUNET_OK;
}
/**
- * Core handler for p2p put requests.
+ * Core handler for p2p get requests.
*
* @param cls closure
* @param peer sender of the request
* @param message message
- * @param peer peer identity this notification is about
* @return #GNUNET_OK to keep the connection open,
* #GNUNET_SYSERR to close it (signal serious error)
*/
static int
-handle_dht_p2p_put (void *cls,
- const struct GNUNET_PeerIdentity *peer,
+handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_MessageHeader *message)
{
- /**
- 1. Check if destination is friend or finger.
- 2. If finger then get the next hop from routing table and
- * call GDS_NEGIHBOURS_handle_get.
- 3. If friend then call find_successor to get the next hop and again
- * call GDS_NEIGHBOURS_handle_get to send to chosen hop.
- 4. If you are the destination then do datacache_store.
- */
- return 0;
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for get result
+ * @param cls closure
+ * @param peer sender of the request
+ * @param message message
+ * @return #GNUNET_OK to keep the connection open,
+ * #GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ return GNUNET_OK;
}
-/**
- * Core handler for p2p get requests.
- *
- * @param cls closure
- * @param peer sender of the request
- * @param message message
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- /**
- 1. Check if destination is friend or finger.
- 2. If finger then get the next hop from routing table and
- * call GDS_NEGIHBOURS_handle_get.
- 3. If friend then call find_successor to get the next hop and again
- * call GDS_NEIGHBOURS_handle_get to send to chosen hop.
- 4. If you are the destination then send the data back to source peer
- * Assuming we have trail setup we can
- * either store the whole trail or again do the search process..
- */
- return 0;
+/* Core handle for PeerTrailSetupMessage.
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+static int
+handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct PeerTrailSetupMessage *trail_setup;
+ struct GNUNET_PeerIdentity *trail_peer_list;
+ struct GNUNET_PeerIdentity next_destination;
+ struct GNUNET_PeerIdentity *current_destination;
+ struct GNUNET_PeerIdentity *next_hop;
+ struct GNUNET_PeerIdentity next_peer;
+ struct FriendInfo *target_friend;
+ struct GNUNET_PeerIdentity source;
+ uint64_t ultimate_destination_finger_value;
+ struct GNUNET_HashCode new_intermediate_trail_id;
+ struct GNUNET_HashCode intermediate_trail_id;
+ struct GNUNET_HashCode new_trail_id;
+ unsigned int finger_map_index;
+ uint32_t trail_length;
+ size_t msize;
+
+ msize = ntohs (message->size);
+ if (msize != sizeof (struct PeerTrailSetupMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+
+ trail_setup = (struct PeerTrailSetupMessage *) message;
+ trail_length = ntohl (trail_setup->trail_length);
+ if ((msize != sizeof (struct PeerTrailSetupMessage) +
+ trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
+ (trail_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+
+ trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_setup[1];
+ current_destination = &trail_setup->next_destination;
+ intermediate_trail_id = trail_setup->intermediate_trail_id;
+ new_trail_id = trail_setup->new_trail_id;
+ ultimate_destination_finger_value = GNUNET_ntohll (trail_setup->ultimate_destination_finger);
+ source = trail_setup->source_peer;
+ finger_map_index = ntohl (trail_setup->finger_map_index);
+
+ if (GNUNET_YES == GDS_ROUTING_threshold_reached())
+ {
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
+ GDS_NEIGHBOURS_send_trail_rejection (source, ultimate_destination_finger_value,
+ my_identity, finger_map_index,
+ trail_peer_list, trail_length,
+ new_trail_id, target_friend,
+ CONGESTION_TIMEOUT);
+ return GNUNET_OK;
+ }
+
+ next_hop = find_successor (ultimate_destination_finger_value, &next_destination,
+ &new_intermediate_trail_id, finger_map_index);
+
+ if (0 != (GNUNET_CRYPTO_cmp_peer_identity(&my_identity, current_destination)))
+ {
+ struct GNUNET_PeerIdentity *closest_peer;
+ struct GNUNET_PeerIdentity *peer1 =
+ GDS_ROUTING_get_next_hop (intermediate_trail_id, GDS_ROUTING_SRC_TO_DEST);
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (peer1, next_hop))
+ {
+ closest_peer = select_closest_peer (peer1, next_hop,
+ ultimate_destination_finger_value,
+ finger_map_index);
+ }
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, closest_peer) ||
+ (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, next_hop)))
+ {
+ next_hop = peer1;
+ next_destination = *current_destination;
+ new_intermediate_trail_id = intermediate_trail_id;
+ }
+ }
+
+ GNUNET_assert (NULL != next_hop);
+ if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This means I am the final destination */
+ {
+ if (0 == trail_length)
+ memcpy (&next_peer, &source, sizeof (struct GNUNET_PeerIdentity));
+ else
+ memcpy (&next_peer, &trail_peer_list[trail_length-1], sizeof (struct GNUNET_PeerIdentity));
+
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
+ GDS_NEIGHBOURS_send_trail_setup_result (source,
+ my_identity,
+ target_friend, trail_length,
+ trail_peer_list,
+ finger_map_index, new_trail_id);
+ }
+ else
+ {
+ struct GNUNET_PeerIdentity peer_list[trail_length + 1];
+
+ memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ peer_list[trail_length] = my_identity;
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+ GDS_NEIGHBOURS_send_trail_setup (source,
+ ultimate_destination_finger_value,
+ next_destination,
+ target_friend, trail_length + 1, peer_list,
+ finger_map_index, new_trail_id,
+ new_intermediate_trail_id);
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handle for p2p trail construction result messages.
+ * @param closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+static int
+handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct PeerTrailSetupResultMessage *trail_result;
+ struct GNUNET_PeerIdentity *trail_peer_list;
+ struct GNUNET_PeerIdentity destination_peer;
+ struct GNUNET_PeerIdentity finger_identity;
+ uint32_t trail_length;
+ uint32_t finger_map_index;
+ struct GNUNET_HashCode trail_id;
+ size_t msize;
+
+ msize = ntohs (message->size);
+ if (msize != sizeof (struct PeerTrailSetupResultMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+
+ trail_result = (struct PeerTrailSetupResultMessage *) message;
+ trail_length = ntohl (trail_result->trail_length);
+
+ if ((msize !=
+ sizeof (struct PeerTrailSetupResultMessage) +
+ trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
+ (trail_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+
+ finger_map_index = htonl (trail_result->finger_map_index);
+ destination_peer = trail_result->destination_peer;
+ finger_identity = trail_result->finger_identity;
+ trail_id = trail_result->trail_id;
+ trail_peer_list = (struct GNUNET_PeerIdentity *) &trail_result[1];
+
+ if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&destination_peer,
+ &my_identity)))
+ {
+ finger_table_add (finger_identity, trail_peer_list,
+ trail_length,
+ finger_map_index, trail_id);
+ return GNUNET_YES;
+ }
+
+ struct GNUNET_PeerIdentity next_hop;
+ struct FriendInfo *target_friend;
+ int my_index;
+
+ my_index = search_my_index(trail_peer_list, trail_length);
+ if (my_index == GNUNET_SYSERR)
+ {
+ GNUNET_break_op(0);
+ return GNUNET_SYSERR;
+ }
+
+ if (my_index == 0)
+ next_hop = trail_result->destination_peer;
+ else
+ next_hop = trail_peer_list[my_index - 1];
+
+ if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->destination_peer),
+ &(trail_result->finger_identity))))
+ {
+ GDS_ROUTING_add (trail_id, &next_hop, peer);
+ }
+
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
+ GDS_NEIGHBOURS_send_trail_setup_result (destination_peer, finger_identity,
+ target_friend, trail_length, trail_peer_list,
+ finger_map_index, trail_id);
+ return GNUNET_OK;
}
/**
- * Compare two peer identities.
- * @param p1 Peer identity
- * @param p2 Peer identity
- * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
+ * Invert the trail.
+ * @param trail Trail to be inverted
+ * @param trail_length Total number of peers in the trail.
+ * @return Updated trail
*/
-#if 0
-static int
-compare_peer_id (const void *p1, const void *p2)
+static struct GNUNET_PeerIdentity *
+invert_trail (struct GNUNET_PeerIdentity *trail,
+ unsigned int trail_length)
{
- return memcmp (p1, p2, sizeof (uint64_t));;
+ int i;
+ int j;
+ struct GNUNET_PeerIdentity *inverted_trail;
+
+ inverted_trail = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
+ trail_length);
+ i = 0;
+ j = trail_length - 1;
+ while (i < trail_length)
+ {
+ inverted_trail[i] = trail[j];
+ i++;
+ j--;
+ }
+ return inverted_trail;
}
-#endif
+
/**
- * Returns the previous element of value in all_known_peers.
- * @param all_known_peers list of all the peers
- * @param value value we have to search in the all_known_peers.
+ * Check if the new finger can be our predecessor. If yes then update predecessor
+ *
+ * @param new_finger
+ * @param new_finger_trail
+ * @param new_finger_trail_length
* @return
*/
-#if 0
-static struct GNUNET_PeerIdentity *
-binary_search(struct GNUNET_PeerIdentity *all_known_peers, uint64_t *value,
- unsigned int size)
+static int
+is_new_entry_correct_predecessor (struct FingerInfo *my_predecessor,
+ struct GNUNET_PeerIdentity new_finger,
+ struct GNUNET_PeerIdentity *new_finger_trail,
+ unsigned int new_finger_trail_length)
{
- int first;
- int last;
- int middle;
- struct GNUNET_PeerIdentity *successor;
- successor = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
-
- first = 0;
- last = size - 1;
- middle = (first + last)/2;
-
- while(first <= last)
+ struct GNUNET_PeerIdentity *updated_trail;
+ struct GNUNET_HashCode new_trail_id;
+
+ updated_trail = invert_trail (new_finger_trail, new_finger_trail_length);
+ if (GNUNET_YES == is_new_finger_closest (my_predecessor, new_finger,
+ new_finger_trail,
+ new_finger_trail_length,
+ new_trail_id, PREDECESSOR_FINGER_ID))
{
- if(compare_peer_id(&all_known_peers[middle], &value) > 0)
- {
- first = middle + 1;
- }
- else if(0 == compare_peer_id(&all_known_peers[middle], &value))
- {
- if(middle == 0)
- {
- memcpy (successor, &(all_known_peers[size - 1]), sizeof (struct GNUNET_PeerIdentity));
- }
- else
- {
- memcpy (successor, &(all_known_peers[middle-1]), sizeof (struct GNUNET_PeerIdentity));
- }
- }
- else
- {
- last = middle - 1;
- }
-
- middle = (first + last)/2;
+ add_new_entry (new_finger, updated_trail, new_finger_trail_length,
+ new_trail_id, PREDECESSOR_FINGER_ID);
+ /* FIXME: check where you send add trail message */
+ return GNUNET_YES;
}
-
- return successor;
+ return GNUNET_NO;
}
-#endif
/**
- * Find closest successor for the value.
- * @param value Value for which we are looking for successor
- * @param current_destination NULL if my_identity is successor else finger/friend
- * identity
- * @param type Next destination type
- * @return Peer identity of next destination i.e. successor of value.
+ * In case the source peer of verify successor message is not my successor,
+ * then construct a trail from source peer to my current predecessor.
+ * @param my_predecessor my current predecessor.
+ * @param current_trail Trail from source to me.
+ * @param current_trail_length Total number of peers in @a current_trail
+ * @param new_trail_length [out] Total number of peers in updated trail.
+ * @return Updated trail from source peer to my_predecessor.
*/
static struct GNUNET_PeerIdentity *
-find_successor(uint64_t *value, struct GNUNET_PeerIdentity *current_destination,
- enum current_destination_type *type)
+trail_source_to_my_predecessor (struct FingerInfo *my_predecessor,
+ struct GNUNET_PeerIdentity *current_trail,
+ unsigned int current_trail_length,
+ unsigned int *new_trail_length)
{
-#if 0
- struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter;
- struct GNUNET_CONTAINER_MultiPeerMapIterator *finger_iter;
- struct GNUNET_PeerIdentity key_ret;
- struct FriendInfo *friend;
- struct FingerInfo *finger;
- unsigned int finger_index;
- unsigned int friend_index;
- struct GNUNET_PeerIdentity *all_known_peers;
- struct GNUNET_PeerIdentity *successor;
- unsigned int size;
+ struct GNUNET_PeerIdentity *new_trail;
+ struct TrailList *trail_list_iterator;
+ struct Trail *trail_iterator;
+ unsigned int i;
unsigned int j;
+ unsigned int shortest_trail_length = 0;
+ unsigned int trail_index = 0;
- /* 2 is added in size for my_identity and value which will part of all_known_peers. */
- size = GNUNET_CONTAINER_multipeermap_size (friend_peermap)+
- GNUNET_CONTAINER_multipeermap_size (finger_peermap)+
- 2;
-
- all_known_peers = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * size);
-
- /* Copy your identity at 0th index in all_known_peers. */
- j = 0;
- memcpy (&all_known_peers[j], &(my_identity), sizeof (struct GNUNET_PeerIdentity));
-
- /* Copy the value that you are searching at index 1 in all_known_peers. */
- j++;
- memcpy (&all_known_peers[j], value, sizeof(uint64_t));
-
- /* Iterate over friend peer map and copy all the elements into array. */
- friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
- for (friend_index = 0; friend_index < GNUNET_CONTAINER_multipeermap_size (friend_peermap); friend_index++)
- {
- if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next(friend_iter,&key_ret,(const void **)&friend))
- {
- memcpy (&all_known_peers[j], &(friend->id), sizeof (struct GNUNET_PeerIdentity));
- j++;
- }
- }
-
- /* Iterate over finger map and copy all the entries into all_known_peers array. */
- finger_iter = GNUNET_CONTAINER_multipeermap_iterator_create (finger_peermap);
- for (finger_index = 0; finger_index < GNUNET_CONTAINER_multipeermap_size (finger_peermap); finger_index++)
+ for (i = 0; i < my_predecessor->trails_count; i++)
{
- /* FIXME: I don't think we are actually iterating.
- Read about how to iterate over the multi peer map. */
- if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next(finger_iter,&key_ret,(const void **)&finger))
- {
- memcpy (&all_known_peers[j], &(finger->finger_identity), sizeof (struct GNUNET_PeerIdentity));
- j++;
- }
+ trail_list_iterator = &my_predecessor->trail_list[i];
+ if (trail_list_iterator->trail_length > shortest_trail_length)
+ continue;
+ shortest_trail_length = trail_list_iterator->trail_length;
+ trail_index = i;
}
- GNUNET_CONTAINER_multipeermap_iterator_destroy (finger_iter);
- GNUNET_CONTAINER_multipeermap_iterator_destroy (friend_iter);
-
- /* FIMXE : Should we not sort it for 64 bits. */
- qsort (all_known_peers, size, sizeof (uint64_t), &compare_peer_id);
+ *new_trail_length = current_trail_length + shortest_trail_length + 1;
+ new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
+ *new_trail_length);
+ memcpy (new_trail, current_trail,
+ current_trail_length * sizeof (struct GNUNET_PeerIdentity));
+ new_trail[current_trail_length + 1] = my_identity;
- /* search value in all_known_peers array. */
- successor = binary_search (all_known_peers, value, size);
-
- /* compare successor with my_identity, finger and friend */
- if(0 == GNUNET_CRYPTO_cmp_peer_identity(&(my_identity), successor))
+ i = 0;
+ j = current_trail_length + 1;
+ trail_list_iterator = &my_predecessor->trail_list[trail_index];
+ trail_iterator = trail_list_iterator->trail_head;
+ while ( i < shortest_trail_length)
{
- FPRINTF (stderr,_("\nSUPU %s, %s, %d"), __FILE__, __func__,__LINE__);
- *type = MY_ID;
- return NULL;
+ new_trail[j] = trail_iterator->peer;
+ j++;
+ i++;
+ trail_iterator = trail_iterator->next;
}
- else if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
- successor))
- {
- FPRINTF (stderr,_("\nSUPU %s, %s, %d"), __FILE__, __func__,__LINE__);
- *type = FRIEND;
- memcpy (current_destination, successor, sizeof (struct GNUNET_PeerIdentity));
- return successor;
- }
- else if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (finger_peermap,
- successor))
- {
- FPRINTF (stderr,_("\nSUPU %s, %s, %d"), __FILE__, __func__,__LINE__);
- *type = FINGER;
- memcpy (current_destination, successor, sizeof (struct GNUNET_PeerIdentity));
- /* get the corresponding finger for succcesor and read the first element from
- the trail list and return that element. */
- struct FingerInfo *successor_finger;
- struct GNUNET_PeerIdentity *next_hop;
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- successor_finger = GNUNET_CONTAINER_multipeermap_get (finger_peermap, successor);
- //memcpy (next_hop, &(successor_finger->trail_peer_list[0]), sizeof (struct GNUNET_PeerIdentity));
- return next_hop;
- }
- FPRINTF (stderr,_("\nSUPU %s, %s, %d"), __FILE__, __func__,__LINE__);
- return NULL;
-#endif
- *type = MY_ID;
- return &my_identity;
+
+ *new_trail_length = j;
+ return new_trail;
}
/**
- * SUPU: The first element in the trail setup message is your identity.
- * in this function you should increment the trail length.
- * Handle a PeerTrailSetupMessage.
+ * Core handle for p2p verify successor messages.
* @param cls closure
* @param message message
* @param peer peer identity this notification is about
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
static int
-handle_dht_p2p_trail_setup(void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
{
- struct PeerTrailSetupMessage *trail_setup;
- struct GNUNET_PeerIdentity *next_hop;
+ struct PeerVerifySuccessorMessage *vsm;
+ struct GNUNET_PeerIdentity successor;
+ struct GNUNET_PeerIdentity source_peer;
+ struct GNUNET_PeerIdentity *next_hop;
struct FriendInfo *target_friend;
+ struct GNUNET_HashCode trail_id;
+ struct FingerInfo *my_predecessor;
+ struct GNUNET_PeerIdentity *trail;
+ struct GNUNET_PeerIdentity *new_trail;
+ unsigned int trail_length;
+ unsigned int new_trail_length;
size_t msize;
- uint32_t trail_length;
- enum current_destination_type peer_type;
- struct GNUNET_PeerIdentity *trail_peer_list;
- uint32_t current_trail_index;
- unsigned int finger_map_index;
- struct GNUNET_PeerIdentity *next_peer;
- unsigned int successor_flag;
- unsigned int predecessor_flag;
-
- /* parse and validate message. */
+
msize = ntohs (message->size);
- if (msize < sizeof (struct PeerTrailSetupMessage))
+ if (msize != sizeof (struct PeerVerifySuccessorMessage))
{
GNUNET_break_op (0);
return GNUNET_YES;
}
-
- trail_setup = (struct PeerTrailSetupMessage *) message;
- trail_length = ntohl (trail_setup->trail_length);
- peer_type = ntohl (trail_setup->current_destination_type);
- finger_map_index = ntohl (trail_setup->finger_map_index);
- successor_flag = ntohl (trail_setup->successor_flag);
- predecessor_flag = ntohl (trail_setup->predecessor_flag);
-
- trail_peer_list = (struct GNUNET_PeerIdentity *) &trail_setup[1];
-
- if ((msize <
- sizeof (struct PeerTrailSetupMessage) +
- trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
- (trail_length >
- GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ vsm = (struct PeerVerifySuccessorMessage *) message;
+ trail_length = ntohl (vsm->trail_length);
+ if ((msize != sizeof (struct PeerVerifySuccessorMessage) +
+ trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
+ (trail_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
{
GNUNET_break_op (0);
- return GNUNET_YES;
+ return GNUNET_YES;
}
+ trail = (struct GNUNET_PeerIdentity *)&vsm[1];
+ source_peer = vsm->source_peer;
+ successor = vsm->successor;
+ trail_id = vsm->trail_id;
- GNUNET_STATISTICS_update (GDS_stats,
- gettext_noop ("# TRAIL SETUP requests received"), 1,
- GNUNET_NO);
- GNUNET_STATISTICS_update (GDS_stats,
- gettext_noop ("# TRAIL SETUP bytes received"), msize,
- GNUNET_NO);
-
- if (peer_type == FRIEND)
+ if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
{
- if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_setup->current_destination),
- &my_identity)))
+ next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
+ if (NULL == next_hop)
{
- next_hop = find_successor (&(trail_setup->destination_finger),
- &(trail_setup->current_destination),
- &(peer_type));
- }
- else
- return GNUNET_SYSERR;
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+ GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
+ trail_id, trail, trail_length,
+ target_friend);
+ return GNUNET_OK;
}
- else if (peer_type == FINGER)
+
+ my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
+ PREDECESSOR_FINGER_ID);
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
+ if (GNUNET_NO == is_new_entry_correct_predecessor (my_predecessor, source_peer,
+ trail, trail_length))
{
- if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_setup->current_destination),
- &my_identity)))
- {
- next_hop = GDS_ROUTING_search (&(trail_setup->source_peer),
- &(trail_setup->current_destination));
-
- #if 0
- /* This is an optimization. Uncomment when basic code is running first. */
- /* I am part of trail.*/
- struct GNUNET_PeerIdentity *next_peer_routing_table;
- next_peer_routing_table = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- next_peer_routing_table = GDS_ROUTING_search (&(trail_setup->source_peer),
- &(trail_setup->current_destination));
-
- struct GNUNET_PeerIdentity *next_peer_find_successor;
- next_peer_find_successor = find_successor (&(trail_setup->destination_finger),
- &(trail_setup->current_destination),
- &(peer_type));
-
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- next_hop = find_closest_destination (next_peer_routing_table,
- next_peer_find_successor,
- &(trail_setup->destination_finger) );
- #endif
- }
- else
+ if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
+ &my_predecessor->finger_identity))
{
- /* I am the current_destination finger */
- next_hop = find_successor (&(trail_setup->destination_finger),
- &(trail_setup->current_destination), &(peer_type));
+ new_trail = trail_source_to_my_predecessor (my_predecessor, trail,
+ trail_length, &new_trail_length);
}
}
-
- /* If you are the next hop, then you are the final destination */
- if (peer_type == MY_ID)
- {
- /*SUPU:
- 1. You were the destination of this message which means you were already added
- in the peer list by previous calling function.
- 2. current_trail_index should point to the trail element at which the peer
- which receives this message should look for the next peer to forward the packet
- to. */
- current_trail_index = trail_length - 2;
- next_peer = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- memcpy (next_peer, &trail_peer_list[current_trail_index], sizeof (struct GNUNET_PeerIdentity));
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_peer);
- GNUNET_free (next_peer);
-
- if(current_trail_index != 0)
- current_trail_index = current_trail_index - 1;
-
- GDS_NEIGHBOURS_handle_trail_setup_result (&(trail_setup->source_peer),
- &(my_identity),
- target_friend, trail_length,
- trail_peer_list, current_trail_index,
- successor_flag,
- predecessor_flag,
- finger_map_index);
-
- return GNUNET_YES;
- }
-
- /* Add next hop to list of peers. */
- struct GNUNET_PeerIdentity *peer_list;
- peer_list = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * (trail_length + 1));
- memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
- memcpy (&peer_list[trail_length], next_hop, sizeof (struct GNUNET_PeerIdentity));
- trail_length++;
-
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
-
- if(peer_type == FINGER)
+ else
{
- GDS_ROUTING_add (&(trail_setup->source_peer),
- &(trail_setup->current_destination),
- next_hop);
+ my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
+ PREDECESSOR_FINGER_ID);
+ GDS_NEIGHBOURS_send_add_trail (my_predecessor->finger_identity,
+ source_peer, trail_id,
+ trail, trail_length,
+ target_friend);
+ new_trail_length = trail_length;
+ new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
+ new_trail_length);
+ memcpy (new_trail, trail, sizeof (struct GNUNET_PeerIdentity) *
+ trail_length);
}
- GDS_NEIGHBOURS_handle_trail_setup (&(trail_setup->source_peer),
- &(trail_setup->destination_finger),
- target_friend,
- trail_setup->trail_length,
- peer_list,trail_setup->successor_flag,
- trail_setup->predecessor_flag,
- finger_map_index);
-
-return GNUNET_YES;
+ GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
+ my_predecessor->finger_identity,
+ trail_id, new_trail,
+ new_trail_length,
+ GDS_ROUTING_DEST_TO_SRC,
+ target_friend);
+ return GNUNET_OK;
}
/**
- * FIXME: For redundant routing, we may start looking for different
- * paths to reach to same finger. So, in send_find_finger, we are starting
- * the search for trail to a finger, even if we already have found trail to
- * reach to it. There are several reasons for doing so
- * 1. We may reach to a closer successor than we have at the moment. So, we
- * should keep looking for the successor.
- * 2. We may reach to the same successor but through a shorter path.
- * 3. As I don't know how keys are distributed and how put/get will react
- * because of this, I have to think further before implementing it.
- * Add an entry in finger table.
- * @param finger Finger to be added to finger table
- * @param peer_list peers this request has traversed so far
- * @param trail_length Numbers of peers in the trail.
+ * Core handle for p2p verify successor result messages.
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
-static
-void finger_table_add (struct GNUNET_PeerIdentity *finger,
- struct GNUNET_PeerIdentity *peer_list,
- unsigned int trail_length,
- unsigned int successor_flag,
- unsigned int predecessor_flag,
- unsigned int finger_map_index)
+static int
+handle_dht_p2p_verify_successor_result(void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
{
- struct FingerInfo *new_finger_entry;
- unsigned int i = 0;
- /** SUPU: when we add an entry then we should look if
- * we already have an entry for that index. If yes, then
- * 1) if both the finger identity are same, and same first friend, then choose
- * the one with shorter trail length.
- * 2) if the finger identity is different, then keep the one which is closest.*/
-
- new_finger_entry = GNUNET_malloc (sizeof (struct FingerInfo));
- memcpy (&(new_finger_entry->finger_identity), finger, sizeof (struct GNUNET_PeerIdentity));
-
-
- /* Insert elements of peer_list into TrailPeerList. */
- i = 0;
- while (i < trail_length)
+ struct PeerVerifySuccessorResultMessage *vsrm;
+ enum GDS_ROUTING_trail_direction trail_direction;
+ struct GNUNET_HashCode trail_id;
+ unsigned int new_trail_length;
+ struct GNUNET_PeerIdentity *new_trail;
+ struct GNUNET_PeerIdentity destination_peer;
+ struct GNUNET_PeerIdentity my_new_successor;
+ struct GNUNET_PeerIdentity *next_hop;
+ struct FriendInfo *target_friend;
+ size_t msize;
+
+ msize = ntohs (message->size);
+ if (msize != sizeof (struct PeerVerifySuccessorResultMessage))
{
- struct TrailPeerList *element;
- element = GNUNET_malloc (sizeof (struct TrailPeerList));
- element->next = NULL;
- element->prev = NULL;
-
- memcpy (&(element->peer), &peer_list[i], sizeof(struct GNUNET_PeerIdentity));
- GNUNET_CONTAINER_DLL_insert_tail(new_finger_entry->head, new_finger_entry->tail, element);
- i++;
+ GNUNET_break_op (0);
+ return GNUNET_YES;
}
+ vsrm = (struct PeerVerifySuccessorResultMessage *) message;
+ new_trail_length = ntohl (vsrm->trail_length);
+ trail_direction = ntohl (vsrm->trail_direction);
+ trail_id = vsrm->trail_id;
+ if ((msize !=
+ sizeof (struct PeerVerifySuccessorResultMessage) +
+ new_trail_length *
+ sizeof (struct GNUNET_PeerIdentity)) ||
+ (new_trail_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
- new_finger_entry->successor = successor_flag;
- new_finger_entry->predecessor = predecessor_flag;
- new_finger_entry->finger_map_index = finger_map_index;
- new_finger_entry->trail_length = trail_length;
-
-
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CONTAINER_multipeermap_put (finger_peermap,
- &(new_finger_entry->finger_identity),
- new_finger_entry,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ new_trail = (struct GNUNET_PeerIdentity *) &vsrm[1];
+ destination_peer = vsrm->destination_peer;
+ my_new_successor = vsrm->my_predecessor;
- /*FIXME: Is it really a good time to call verify successor message. */
- if (1 == GNUNET_CONTAINER_multipeermap_size (finger_peermap))
+ if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&destination_peer, &my_identity)))
{
- verify_successor = GNUNET_SCHEDULER_add_now (&send_verify_successor_message, NULL);
+ struct GNUNET_HashCode new_finger_trail_id;
+ if (GNUNET_YES == finger_table_add (my_new_successor,
+ new_trail,
+ new_trail_length,
+ PREDECESSOR_FINGER_ID, new_finger_trail_id))
+ {
+ if (new_trail_length > 0)
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ &new_trail[0]);
+ else
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+ peer);
+ GDS_NEIGHBOURS_send_notify_new_successor (my_identity, my_new_successor,
+ new_trail,
+ new_trail_length,
+ new_finger_trail_id, target_friend);
+ }
+ return GNUNET_OK;
}
+
+ GNUNET_assert (NULL != (next_hop =
+ GDS_ROUTING_get_next_hop (trail_id, trail_direction)));
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+ GDS_NEIGHBOURS_send_verify_successor_result (destination_peer,
+ vsrm->source_successor,
+ my_new_successor, trail_id,
+ new_trail,
+ new_trail_length,
+ trail_direction, target_friend);
+ return GNUNET_OK;
}
-
+
/**
- * Core handle for p2p trail construction result messages.
+ * Here we need to pass the whole trail to reach to new successor as we
+ don't have that stored in our routing table. while passing through each
+ peer we will have to add an entry. also when you are the destination and
+ if you have added it back as pred, then you also need to add the trail in
+ your own finger table and send add trail message to add this trail. you
+ shoudl generate a new trail id. although they are same trails but you have
+ to ahve different trail id.
+ * Core handle for p2p notify new successor messages.
* @param cls closure
* @param message message
* @param peer peer identity this notification is about
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
static int
-handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+handle_dht_p2p_notify_new_successor(void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
{
- struct PeerTrailSetupResultMessage *trail_result;
- size_t msize;
- unsigned int trail_length;
- struct GNUNET_PeerIdentity *trail_peer_list;
- unsigned int current_trail_index;
- struct GNUNET_PeerIdentity *next_peer;
+ struct PeerNotifyNewSuccessorMessage *nsm;
+ struct GNUNET_PeerIdentity *trail;
+ struct GNUNET_PeerIdentity source;
+ struct GNUNET_PeerIdentity destination;
struct FriendInfo *target_friend;
- unsigned int finger_map_index;
- unsigned int successor_flag;
- unsigned int predecessor_flag;
+ //struct GNUNET_HashCode trail_id;
+ size_t msize;
+ uint32_t trail_length;
msize = ntohs (message->size);
- if (msize < sizeof (struct PeerTrailSetupMessage))
+ if (msize < sizeof (struct PeerNotifyNewSuccessorMessage))
{
GNUNET_break_op (0);
return GNUNET_YES;
}
+ nsm = (struct PeerNotifyNewSuccessorMessage *) message;
+ trail_length = ntohl (nsm->trail_length);
- trail_result = (struct PeerTrailSetupResultMessage *) message;
- trail_length = ntohl (trail_result->trail_length);
-
- if ((msize <
- sizeof (struct PeerTrailSetupResultMessage) +
- trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
- (trail_length >
- GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ if ((msize < sizeof (struct PeerNotifyNewSuccessorMessage) +
+ trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
+ (trail_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
{
GNUNET_break_op (0);
return GNUNET_YES;
}
- current_trail_index = ntohl (trail_result->current_index);
- successor_flag = ntohl (trail_result->successor_flag);
- predecessor_flag = ntohl (trail_result->predecessor_flag);
- finger_map_index = ntohl (trail_result->finger_map_index);
-
- trail_peer_list = (struct GNUNET_PeerIdentity *) &trail_result[1];
-
- if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->current_destination),
- &my_identity)))
+ trail = (struct GNUNET_PeerIdentity *) &nsm[1];
+ source = nsm->source_peer;
+ destination = nsm->destination_peer;
+ //trail_id = nsm->trail_id;
+#if 0
+ if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination))
{
- if ( 0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->destination_peer),
- &my_identity)))
- {
- #if 0
- /* SUPU: Here I have removed myself from the trail before storing it in
- th finger table - to save space, but in case of verify successor result
- the result trail does not contain me, and I will never get the message back.
- So, keeping myself in the trail list. Think of better solution.*/
- struct GNUNET_PeerIdentity *finger_trail;
- finger_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * (trail_length - 1));
-
- /* Copy the whole trail_peer_list except the first element into trail */
- unsigned int i;
- i = trail_length - 1;
- while (i > 0)
- {
- memcpy (&finger_trail[i], &trail_peer_list[i], sizeof (struct GNUNET_PeerIdentity));
- i--;
- }
- trail_length = trail_length -1 ; SUPU: As you removed yourself from the trail.*/
- #endif
-
- finger_table_add (&(trail_result->finger), trail_peer_list, trail_length,
- successor_flag, predecessor_flag,
- finger_map_index);
-
- return GNUNET_YES;
- }
- else
+ struct GNUNET_HashCode new_trail_id;
+ struct FingerInfo *my_predecessor;
+ if (GNUNET_YES == is_new_entry_correct_predecessor (my_predecessor,
+ source, trail,
+ trail_length))
{
- next_peer = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- memcpy (next_peer, &(trail_peer_list[current_trail_index]),
- sizeof (struct GNUNET_PeerIdentity));
- /* SUPU: here current trail index will always be greater than 0.
- so no need for this check here. trail index = 0, contains the final
- destination, and if we are in this loop we have not yet reached the
- final destination. */
- current_trail_index = current_trail_index - 1;
-
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_peer);
- GNUNET_free (next_peer);
-
- GDS_NEIGHBOURS_handle_trail_setup_result (&(trail_result->destination_peer),
- &(trail_result->finger),
- target_friend, trail_length,
- trail_peer_list,current_trail_index,
- trail_result->successor_flag,
- trail_result->predecessor_flag,
- finger_map_index);
- return GNUNET_YES;
+ GDS_NEIGHBOURS_send_add_trail (my_identity, source, new_trail_id,
+ trail, trail_length, target_friend);
}
}
- else
- return GNUNET_SYSERR;
+
+ //GDS_ROUTING_add (new_trail_id, peer, next_hop);
+#endif
+ return GNUNET_OK;
}
+
/**
- * SUPU: In this function you don't do anything with trail length
- * You increment the current trail index so that you find the correct
- * peer to send the packet forward.
- * Core handle for p2p verify successor messages.
+ * FIXME: Here you should keep the trail id with you.
+ * Core handler for P2P trail rejection message
* @param cls closure
* @param message message
* @param peer peer identity this notification is about
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
-static int
-handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+static int
+handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
{
- struct PeerVerifySuccessorMessage *vsm;
- size_t msize;
+ struct PeerTrailRejectionMessage *trail_rejection;
unsigned int trail_length;
struct GNUNET_PeerIdentity *trail_peer_list;
- unsigned int current_trail_index;
struct FriendInfo *target_friend;
+ struct GNUNET_TIME_Relative congestion_timeout;
+ struct GNUNET_HashCode trail_id;
+ struct GNUNET_PeerIdentity next_destination;
+ struct GNUNET_HashCode new_intermediate_trail_id;
+ struct GNUNET_PeerIdentity next_peer;
+ struct GNUNET_PeerIdentity source;
struct GNUNET_PeerIdentity *next_hop;
-
+ uint64_t ultimate_destination_finger_value;
+ unsigned int finger_map_index;
+ size_t msize;
+
msize = ntohs (message->size);
- if (msize < sizeof (struct PeerVerifySuccessorMessage))
+ if (msize != sizeof (struct PeerTrailRejectionMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+
+ trail_rejection = (struct PeerTrailRejectionMessage *) message;
+ trail_length = ntohl (trail_rejection->trail_length);
+
+ if ((msize != sizeof (struct PeerTrailRejectionMessage) +
+ trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
+ (trail_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
{
GNUNET_break_op (0);
return GNUNET_YES;
}
- vsm = (struct PeerVerifySuccessorMessage *) message;
- trail_length = ntohl (vsm->trail_length);
-
- if ((msize <
- sizeof (struct PeerVerifySuccessorMessage) +
- trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
- (trail_length >
- GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
- {
- GNUNET_break_op (0);
- return GNUNET_YES;
- }
+ trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_rejection[1];
+ finger_map_index = ntohl (trail_rejection->finger_map_index);
+ congestion_timeout = trail_rejection->congestion_time;
+ source = trail_rejection->source_peer;
+ trail_id = trail_rejection->trail_id;
+ ultimate_destination_finger_value =
+ trail_rejection->ultimate_destination_finger_identity_value;
- current_trail_index = ntohl (vsm->current_trail_index);
-
- trail_peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
+ /* First set the congestion time of the friend that sent you this message. */
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
+ target_friend->congestion_duration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
+ congestion_timeout);
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
+ if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
+ {
+ return GNUNET_OK;
+ }
- if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&(vsm->successor),
- &my_identity)))
+ /* If I am congested then pass this message to peer before me in trail. */
+ if(GNUNET_YES == GDS_ROUTING_threshold_reached())
{
- /* I am the successor, check who is my predecessor. If my predecessor is not
- same as source peer then update the trail and send back to calling function.
- */
- struct GNUNET_CONTAINER_MultiPeerMapIterator *finger_iter;
- struct GNUNET_PeerIdentity key_ret;
- unsigned int finger_index;
- struct FingerInfo *my_predecessor;
- struct GNUNET_PeerIdentity *destination_peer;
-
- /* Iterate over finger peer map and extract your predecessor. */
- finger_iter = GNUNET_CONTAINER_multipeermap_iterator_create (finger_peermap);
- for (finger_index = 0; finger_index < GNUNET_CONTAINER_multipeermap_size (finger_peermap); finger_index++)
+ struct GNUNET_PeerIdentity *new_trail;
+ unsigned int new_trail_length;
+
+ if (trail_length == 1)
{
- if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next
- (finger_iter,&key_ret,(const void **)&my_predecessor))
- {
- if(1 == my_predecessor->predecessor)
- break;
- }
+ new_trail = NULL;
+ new_trail_length = 0;
+ next_hop = &source;
}
-
- GNUNET_CONTAINER_multipeermap_iterator_destroy (finger_iter);
- destination_peer = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- memcpy (destination_peer, &(vsm->source_peer), sizeof (struct GNUNET_PeerIdentity));
- current_trail_index = trail_length - 2; /*SUPU: I am the last element in the trail.*/
- memcpy (next_hop, &trail_peer_list[current_trail_index], sizeof (struct GNUNET_PeerIdentity));
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
- GNUNET_free (next_hop);
-
- if (current_trail_index != 0)
- current_trail_index = current_trail_index - 1;
-
- /* FIXME: Here we should check if our predecessor is source peer or not.
- If not then, we can send an updated trail that goes through us. Instead of
- looking for a new trail to reach to the new successor, source peer
- can just use this trail. It may not be an optimal route. */
- if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(vsm->source_peer),
- &(my_predecessor->finger_identity))))
+ else
{
- /*If we have a new predecessor, then create a new trail to reach from
- vsm source peer to this new successor of source peer. */
- struct GNUNET_PeerIdentity *new_successor_trail;
- unsigned int my_predecessor_trail_length;
- unsigned int new_trail_length;
- unsigned int i;
-
- /* SUPU: The trail that we store corresponding to each finger contains
- * me as the first element. So, we are included twice when we join the
- * two trails. */
- my_predecessor_trail_length = (my_predecessor->trail_length) - 1; /*SUPU: Removing myself from the trail */
- new_trail_length = trail_length + my_predecessor_trail_length;
-
- new_successor_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)
- * new_trail_length);
- memcpy (new_successor_trail, trail_peer_list,
- trail_length * sizeof (struct GNUNET_PeerIdentity));
-
- struct TrailPeerList *iterator;
- iterator = my_predecessor->head->next; /* FIXME: Check if you are removing yourself */
- i = trail_length;
- while (i < new_trail_length)
- {
- memcpy (&new_successor_trail[i], &(iterator->peer), sizeof (struct GNUNET_PeerIdentity));
- iterator = iterator->next;
- i++;
- }
-
-
- GDS_NEIGHBOURS_handle_verify_successor_result (destination_peer,
- &(my_identity),
- &(my_predecessor->finger_identity),
- target_friend,
- new_successor_trail,
- new_trail_length,
- current_trail_index);
+ next_hop = &trail_peer_list[trail_length - 2];
+ /* Remove myself from the trail. */
+ new_trail_length = trail_length -1;
+ new_trail = GNUNET_malloc (new_trail_length * sizeof (struct GNUNET_PeerIdentity));
+ memcpy (new_trail, trail_peer_list, new_trail_length * sizeof (struct GNUNET_PeerIdentity));
}
- GDS_NEIGHBOURS_handle_verify_successor_result (destination_peer,
- &(my_identity),
- &(my_predecessor->finger_identity),
- target_friend,
- trail_peer_list,
- trail_length,
- current_trail_index);
-
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+ GDS_NEIGHBOURS_send_trail_rejection (source,
+ ultimate_destination_finger_value,
+ my_identity, finger_map_index,
+ new_trail,new_trail_length,trail_id,
+ target_friend, CONGESTION_TIMEOUT);
+ return GNUNET_YES;
+ }
+
+ /* Look for next_hop to pass the trail setup message */
+ next_hop = find_successor (ultimate_destination_finger_value,
+ &next_destination,
+ &new_intermediate_trail_id,
+ finger_map_index);
+
+ if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This means I am the final destination */
+ {
+ if (0 == trail_length)
+ next_peer = source;
+ else
+ next_peer = trail_peer_list[trail_length-1];
+
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
+ GDS_NEIGHBOURS_send_trail_setup_result (source,
+ my_identity,
+ target_friend, trail_length,
+ trail_peer_list,
+ finger_map_index, trail_id);
}
else
{
- memcpy (next_hop, &trail_peer_list[current_trail_index], sizeof (struct GNUNET_PeerIdentity));
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
- GNUNET_free (next_hop);
+ struct GNUNET_PeerIdentity peer_list[trail_length + 1];
- current_trail_index = current_trail_index + 1;
+ memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+ peer_list[trail_length] = my_identity;
- GDS_NEIGHBOURS_handle_verify_successor(&(vsm->source_peer),
- &(vsm->successor),
- target_friend,
- trail_peer_list,
- trail_length,
- current_trail_index);
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+ GDS_NEIGHBOURS_send_trail_setup (source,
+ ultimate_destination_finger_value,
+ next_destination,
+ target_friend, trail_length + 1, peer_list,
+ finger_map_index, trail_id,
+ new_intermediate_trail_id);
}
- return GNUNET_YES;
+ return GNUNET_OK;
}
-/**
- * Update successor field in finger table with new successor.
- * @param successor New successor which is the predecessor my old successor.
- * @param peer_list Trail list to reach to new successor = trail to reach old
- * successor + trail to reach to new successor from that old successor.
- * @param trail_length Number of peers to reach to the new successor.
+/*
+ * Core handle for p2p trail tear down messages.
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
-static void
-update_successor (struct GNUNET_PeerIdentity *successor_identity,
- struct GNUNET_PeerIdentity *peer_list,
- unsigned int trail_length)
+static int
+handle_dht_p2p_trail_compression (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
{
- struct FingerInfo *new_finger_entry;
- unsigned int i;
+ struct PeerTrailCompressionMessage *trail_compression;
+ struct GNUNET_PeerIdentity *next_hop;
+ struct GNUNET_HashCode trail_id;
+ struct FriendInfo *target_friend;
+ size_t msize;
- new_finger_entry = GNUNET_malloc (sizeof (struct FingerInfo));
- new_finger_entry->predecessor = 0;
- new_finger_entry->successor = 1;
- new_finger_entry->trail_length = trail_length;
- new_finger_entry->finger_map_index = 0;
- memcpy (&(new_finger_entry->finger_identity), successor_identity, sizeof (struct GNUNET_PeerIdentity));
+ msize = ntohs (message->size);
+ if (msize != sizeof (struct PeerTrailCompressionMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
- i = 0;
- while (i < trail_length)
+ trail_compression = (struct PeerTrailCompressionMessage *) message;
+ trail_id = trail_compression->trail_id;
+
+ if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_compression->new_first_friend),
+ &my_identity)))
{
- struct TrailPeerList *element;
- element = GNUNET_malloc (sizeof (struct TrailPeerList));
- element->next = NULL;
- element->prev = NULL;
-
- memcpy (&(element->peer), &peer_list[i], sizeof(struct GNUNET_PeerIdentity));
- GNUNET_CONTAINER_DLL_insert_tail(new_finger_entry->head, new_finger_entry->tail, element);
- i++;
+ if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_compression->destination_peer),
+ &my_identity)))
+ {
+ GDS_ROUTING_update_trail_prev_hop (trail_id,
+ trail_compression->source_peer);
+ }
+ return GNUNET_OK;
}
+
+ next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
+ if (NULL == next_hop)
+ {
+ GNUNET_break (0); /*FIXME: How to handle this case. */
+ return GNUNET_OK;
+ }
+ GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
+ target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+ GDS_NEIGHBOURS_send_trail_compression (trail_compression->source_peer,
+ trail_compression->destination_peer,
+ trail_id,
+ trail_compression->new_first_friend,
+ target_friend);
+ return GNUNET_OK;
}
/**
- * FIXME: Also copy the trail list in reverse direction that is the path to
- * reach to your predecessor.
- * Replace your predecessor with new predecessor.
- * @param predecessor My new predecessor
- * @param peer_list Trail list to reach to my new predecessor
- * @param trail_length Number of peers in the trail.
+ * Core handler for trail teardown message.
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
-static void
-update_predecessor (struct GNUNET_PeerIdentity *predecessor,
- struct GNUNET_PeerIdentity *peer_list,
- unsigned int trail_length)
+static int
+handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
{
- struct GNUNET_PeerIdentity *trail_peer_list;
- struct FingerInfo *new_finger_entry;
- unsigned int i;
- unsigned int j;
-
- i = trail_length - 1;
- j = 0;
- trail_peer_list = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
- trail_length);
- while (i > 0)
- {
- memcpy( &trail_peer_list[j], &peer_list[i], sizeof (struct GNUNET_PeerIdentity));
- i--;
- j++;
- }
- memcpy (&trail_peer_list[j], &peer_list[i], sizeof(struct GNUNET_PeerIdentity));
-
- new_finger_entry = GNUNET_malloc (sizeof (struct FingerInfo));
- memcpy (&(new_finger_entry->finger_identity), predecessor, sizeof (struct GNUNET_PeerIdentity));
- new_finger_entry->finger_map_index = 1;
- new_finger_entry->predecessor = 1;
- new_finger_entry->successor = 0;
-
- i = 0;
- while (i < trail_length)
- {
- struct TrailPeerList *element;
- element = GNUNET_malloc (sizeof (struct TrailPeerList));
- element->next = NULL;
- element->prev = NULL;
-
- memcpy (&(element->peer), &trail_peer_list[i], sizeof(struct GNUNET_PeerIdentity));
- GNUNET_CONTAINER_DLL_insert_tail(new_finger_entry->head, new_finger_entry->tail, element);
- i++;
- }
+ return GNUNET_OK;
}
/**
- * Core handle for p2p notify new successor messages.
+ * TRAIL ID and each peer should add an entry in the routing table.
+ * Core handle for p2p add trail message.
* @param cls closure
* @param message message
* @param peer peer identity this notification is about
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+static int
+handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ return GNUNET_OK;
+}
+
+
+/**
+ *FIXME; call send_trail_teardown_message on all the trails of the finger that
+ * you remove. Also you don't need to decerement friend trail count as that
+ * friend is removed. But you can not send trail teardown message as the friend
+ * is disconnected. then you don't have any next_hop. and in case there are
+ * multiple trails. and friend is the first trail then you remove only the trail.
+ * Iterate over finger_hashmap, and remove entries if finger is the disconnected
+ * peer or if disconnected peer is the first friend in the trail to reach the
+ * finger.
+ * @param cls closure
+ * @param key current public key
+ * @param value value in the hash map
+ * @return #GNUNET_YES if we should continue to
+ * iterate,
+ * #GNUNET_NO if not.
*/
static int
-handle_dht_p2p_notify_new_successor(void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+remove_matching_finger (void *cls,
+ uint32_t key,
+ void *value)
{
- struct PeerNotifyNewSuccessorMessage *nsm;
- size_t msize;
- unsigned int trail_length;
- struct GNUNET_PeerIdentity *trail_peer_list;
- unsigned int current_trail_index;
-
- msize = ntohs (message->size);
- if (msize < sizeof (struct PeerNotifyNewSuccessorMessage))
- {
- GNUNET_break_op (0);
- return GNUNET_YES;
- }
+ struct FingerInfo *remove_finger = value;
+ const struct GNUNET_PeerIdentity *disconnected_peer = cls;
+ struct TrailList *trail_list;
+ int i;
- /* Again in the function you have the whole trail to reach to the destination. */
- nsm = (struct PeerNotifyNewSuccessorMessage *) message;
- trail_length = ntohl (nsm->trail_length);
-
- if ((msize <
- sizeof (struct PeerNotifyNewSuccessorMessage) +
- trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
- (trail_length >
- GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity,
+ disconnected_peer))
{
- GNUNET_break_op (0);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap32_remove (finger_hashmap,
+ key,
+ remove_finger));
+ free_finger (remove_finger);
return GNUNET_YES;
}
- current_trail_index = ntohl (nsm->current_index);
- trail_peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
-
- if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&(nsm->destination_peer),
- &my_identity)))
- {
- update_predecessor (&(nsm->source_peer),
- trail_peer_list,
- trail_length);
- return GNUNET_YES;
- }
- else
+ for (i = 0; i< remove_finger->trails_count; i++)
{
- struct FriendInfo *target_friend;
- target_friend = GNUNET_malloc (sizeof (struct FriendInfo));
- struct GNUNET_PeerIdentity *next_hop;
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- memcpy (next_hop, &trail_peer_list[current_trail_index], sizeof (struct GNUNET_PeerIdentity));
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
- GNUNET_free (next_hop);
- current_trail_index = current_trail_index + 1;
-
- GDS_NEIGHBOURS_notify_new_successor (&(nsm->source_peer),
- &(nsm->destination_peer),
- target_friend, trail_peer_list, trail_length,
- current_trail_index);
+ trail_list = &remove_finger->trail_list[i];
+ if (0 == GNUNET_CRYPTO_cmp_peer_identity (&trail_list->trail_head->peer,
+ disconnected_peer))
+ {
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap32_remove (finger_hashmap,
+ key,
+ remove_finger));
+ free_finger (remove_finger);
+ }
}
+
return GNUNET_YES;
}
/**
- * Core handle for p2p verify successor result messages.
+ * Method called whenever a peer disconnects.
+ *
* @param cls closure
- * @param message message
* @param peer peer identity this notification is about
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
-static int
-handle_dht_p2p_verify_successor_result(void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+static void
+handle_core_disconnect (void *cls,
+ const struct GNUNET_PeerIdentity *peer)
{
- struct PeerVerifySuccessorResultMessage *vsrm;
- size_t msize;
- struct FriendInfo *target_friend;
- unsigned int current_trail_index;
- struct GNUNET_PeerIdentity *trail_peer_list;
- struct GNUNET_PeerIdentity *next_hop;
- unsigned int trail_length;
+ struct FriendInfo *remove_friend;
- msize = ntohs (message->size);
- if (msize < sizeof (struct PeerVerifySuccessorResultMessage))
- {
- GNUNET_break_op (0);
- return GNUNET_YES;
- }
-
- /* Again in the function you have the whole trail to reach to the destination. */
- vsrm = (struct PeerVerifySuccessorResultMessage *) message;
- current_trail_index = ntohl (vsrm->current_index);
- trail_length = ntohl (vsrm->trail_length);
+ if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
+ return;
- trail_peer_list = (struct GNUNET_PeerIdentity *) &vsrm[1];
+ remove_friend =
+ GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
- if ((msize <
- sizeof (struct PeerVerifySuccessorResultMessage) +
- trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
- (trail_length >
- GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ if (NULL == remove_friend)
{
- GNUNET_break_op (0);
- return GNUNET_YES;
+ GNUNET_break (0);
+ return;
}
- if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&(vsrm->destination_peer),
- &(my_identity))))
+ GNUNET_assert (GNUNET_SYSERR !=
+ GNUNET_CONTAINER_multihashmap32_iterate (finger_hashmap,
+ &remove_matching_finger,
+ (void *)peer));
+ GDS_ROUTING_remove_trail_by_peer (peer);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
+ peer,
+ remove_friend));
+
+ if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
+ return;
+
+ if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
{
- if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&(vsrm->my_predecessor),
- &(my_identity))))
- {
- update_successor (&(vsrm->my_predecessor), trail_peer_list, trail_length);
-
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
- /* FIXME: Assuming that I am also in trail list and I am the first peer. */
- memcpy (next_hop, &trail_peer_list[1], sizeof (struct GNUNET_PeerIdentity));
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
- GNUNET_free (next_hop);
-
- GDS_NEIGHBOURS_notify_new_successor (&my_identity, &(vsrm->my_predecessor),
- target_friend, trail_peer_list,
- trail_length, current_trail_index);
- }
+ GNUNET_SCHEDULER_cancel (find_finger_trail_task);
+ find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
}
else
+ GNUNET_break (0);
+}
+
+
+/**
+ * Method called whenever a peer connects.
+ *
+ * @param cls closure
+ * @param peer_identity peer identity this notification is about
+ */
+static void
+handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
+{
+ struct FriendInfo *friend;
+
+ /* Check for connect to self message */
+ if (0 == memcmp (&my_identity, peer_identity, sizeof (struct GNUNET_PeerIdentity)))
+ return;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to %s\n", GNUNET_i2s (peer_identity));
+
+ /* If peer already exists in our friend_peermap, then exit. */
+ if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap, peer_identity))
{
- /* Read the peer trail list and find out the next destination to forward this
- packet to. */
- next_hop = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
-
- /* FIXME: Assuming that I am also in trail list and I am the first peer. */
- memcpy (next_hop, &trail_peer_list[current_trail_index], sizeof (struct GNUNET_PeerIdentity));
- target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
- GNUNET_free (next_hop);
- current_trail_index = current_trail_index - 1;
-
- GDS_NEIGHBOURS_handle_verify_successor_result (&(vsrm->destination_peer),
- &(vsrm->source_successor),
- &(vsrm->my_predecessor),
- target_friend,
- trail_peer_list,
- trail_length,
- current_trail_index);
+ GNUNET_break (0);
+ return;
}
- return GNUNET_YES;
+
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1,
+ GNUNET_NO);
+
+ friend = GNUNET_new (struct FriendInfo);
+ friend->id = *peer_identity;
+
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multipeermap_put (friend_peermap,
+ peer_identity, friend,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+
+ /* got a first connection, good time to start with FIND FINGER TRAIL requests... */
+ if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
+ find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
+}
+
+
+/**
+ * To be called on core init/fail.
+ *
+ * @param cls service closure
+ * @param identity the public identity of this peer
+ */
+static void
+core_init (void *cls,
+ const struct GNUNET_PeerIdentity *identity)
+{
+ my_identity = *identity;
}
/**
* Initialize neighbours subsystem.
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
int
-GDS_NEIGHBOURS_init()
+GDS_NEIGHBOURS_init (void)
{
static struct GNUNET_CORE_MessageHandler core_handlers[] = {
- {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
{&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
+ {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
+ {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT, 0},
{&handle_dht_p2p_trail_setup, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP, 0},
{&handle_dht_p2p_trail_setup_result, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT, 0},
{&handle_dht_p2p_verify_successor, GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR, 0},
{&handle_dht_p2p_verify_successor_result, GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT, 0},
{&handle_dht_p2p_notify_new_successor, GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR, 0},
+ {&handle_dht_p2p_trail_rejection, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION, 0},
+ {&handle_dht_p2p_trail_compression, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION, 0},
+ {&handle_dht_p2p_trail_teardown, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN, 0},
+ {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL, 0},
{NULL, 0, 0}
};
-
-
- /*TODO: What is ATS? Why do we need it? */
- atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL);
+
core_api =
GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
&handle_core_disconnect, NULL, GNUNET_NO, NULL,
GNUNET_NO, core_handlers);
if (NULL == core_api)
return GNUNET_SYSERR;
-
+
friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
- finger_peermap = GNUNET_CONTAINER_multipeermap_create (MAX_FINGERS, GNUNET_NO);
-
+ finger_hashmap = GNUNET_CONTAINER_multihashmap32_create (MAX_FINGERS * 4/3);
+
return GNUNET_OK;
}
-
/**
* Shutdown neighbours subsystem.
*/
void
-GDS_NEIGHBOURS_done ()
+GDS_NEIGHBOURS_done (void)
{
if (NULL == core_api)
return;
GNUNET_CORE_disconnect (core_api);
core_api = NULL;
- GNUNET_ATS_performance_done (atsAPI);
- atsAPI = NULL;
-
- /* FIXME: In case of friends, every time we are disconnected from a friend
- we remove it from friend table. So, this assertion works for friend.
- But in case of finger_peermap, we never remove any entry from our
- finger peermap. So, either when we remove the friend from friend peermap,then
- I remove all the finger for which that friend was the first trail and leave
- it on send_find_finger_trail to eventually find path to that finger. In that
- case may be assertion for finger peermap will also succed. Or else if
- peermap are not empty check it and empty it and then destroy because
- multipeermpa_destroy does not free individual entries. */
+
GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
friend_peermap = NULL;
-
- GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (finger_peermap));
- GNUNET_CONTAINER_multipeermap_destroy (finger_peermap);
- finger_peermap = NULL;
+
+ GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (finger_hashmap));
+ GNUNET_CONTAINER_multihashmap32_destroy (finger_hashmap);
+ finger_hashmap = NULL;
if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
{
+ GNUNET_break (0);
GNUNET_SCHEDULER_cancel (find_finger_trail_task);
find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
}
-
- if (GNUNET_SCHEDULER_NO_TASK != verify_successor)
- {
- GNUNET_SCHEDULER_cancel (verify_successor);
- verify_successor = GNUNET_SCHEDULER_NO_TASK;
- }
-
}
/**
- * Get the ID of the local node.
+ * Get my identity
*
- * @return identity of the local node
+ * @return my identity
*/
-struct GNUNET_PeerIdentity *
-GDS_NEIGHBOURS_get_id ()
+struct GNUNET_PeerIdentity
+GDS_NEIGHBOURS_get_my_id (void)
{
- return &my_identity;
-}
-
-
-/* end of gnunet-service-xdht_neighbours.c */
\ No newline at end of file
+ return my_identity;
+}
\ No newline at end of file