2 This file is part of GNUnet.
3 (C) 2009-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-xdht_neighbours.c
23 * @brief GNUnet DHT service's finger and friend table management code
24 * @author Supriti Singh
28 #include "gnunet_util_lib.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_ats_service.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_datacache_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "gnunet_dht_service.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet-service-xdht.h"
40 #include "gnunet-service-xdht_clients.h"
41 #include "gnunet-service-xdht_datacache.h"
42 #include "gnunet-service-xdht_neighbours.h"
43 #include "gnunet-service-xdht_routing.h"
49 * 1. In X-Vine paper, there is no policy defined for replicating the data to
50 * recover in case of peer failure. We can do it in Chord way. In R5N, the key
51 * is hashed and then data is stored according to the key value generated after
58 * We should have a message type like notify successor result. only when
59 * this message is being recvied by the new successor. we should schedule
60 * another round of verify successor.
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 * Maximum possible fingers (including predecessor) of a peer
68 #define MAX_FINGERS 65
71 * Maximum allowed number of pending messages per friend peer.
73 #define MAXIMUM_PENDING_PER_FRIEND 64
76 * How long to wait before sending another find finger trail request
78 #define DHT_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
81 * How long to wait before sending another verify successor message.
83 #define DHT_SEND_VERIFY_SUCCESSOR_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 60)
86 * How long at most to wait for transmission of a request to a friend ?
88 #define PENDING_MESSAGE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
91 * Duration for which I may remain congested.
92 * Note: Its a static value. In future, a peer may do some analysis and calculate
93 * congestion_timeout based on 'some' parameters.
95 #define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
98 * Maximum number of trails allowed to go through a friend.
100 #define TRAILS_THROUGH_FRIEND_THRESHOLD 64
103 * Maximum number of trails stored per finger.
105 #define MAXIMUM_TRAILS_PER_FINGER 1
108 * Finger map index for predecessor entry in finger table.
110 #define PREDECESSOR_FINGER_ID 64
113 * Wrap around in peer identity circle.
115 #define PEER_IDENTITES_WRAP_AROUND pow(2, 64) - 1
118 * FIXME: Its use only at 3 places check if you can remove it.
119 * To check if a finger is predecessor or not.
121 enum GDS_NEIGHBOURS_finger_type
123 GDS_FINGER_TYPE_PREDECESSOR = 1,
124 GDS_FINGER_TYPE_NON_PREDECESSOR = 0
127 GNUNET_NETWORK_STRUCT_BEGIN
132 struct PeerPutMessage
135 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT
137 struct GNUNET_MessageHeader header;
142 uint32_t options GNUNET_PACKED;
147 uint32_t block_type GNUNET_PACKED;
152 uint32_t hop_count GNUNET_PACKED;
155 * Replication level for this message
156 * In the current implementation, this value is not used.
158 uint32_t desired_replication_level GNUNET_PACKED;
161 * Length of the PUT path that follows (if tracked).
163 uint32_t put_path_length GNUNET_PACKED;
166 * Best known destination (could be my friend or finger) which should
167 * get this message next.
169 struct GNUNET_PeerIdentity best_known_destination;
172 * In case best_known_destination is a finger, then trail to reach
173 * to that finger. Else its default value is 0.
175 struct GNUNET_HashCode intermediate_trail_id;
178 * When does the content expire?
180 struct GNUNET_TIME_AbsoluteNBO expiration_time;
183 * The key to store the value under.
185 struct GNUNET_HashCode key GNUNET_PACKED;
187 /* put path (if tracked) */
196 struct PeerGetMessage
199 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET
201 struct GNUNET_MessageHeader header;
206 uint32_t options GNUNET_PACKED;
209 * Desired content type.
211 uint32_t block_type GNUNET_PACKED;
216 uint32_t hop_count GNUNET_PACKED;
219 * Desired replication level for this request.
220 * In the current implementation, this value is not used.
222 uint32_t desired_replication_level GNUNET_PACKED;
225 * Total number of peers in get path.
227 unsigned int get_path_length;
230 * Best known destination (could be my friend or finger) which should
231 * get this message next.
233 struct GNUNET_PeerIdentity best_known_destination;
236 * In case best_known_destination is a finger, then trail to reach
237 * to that finger. Else its default value is 0.
239 struct GNUNET_HashCode intermediate_trail_id;
242 * The key we are looking for.
244 struct GNUNET_HashCode key;
247 /* struct GNUNET_PeerIdentity[]*/
253 struct PeerGetResultMessage
256 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT
258 struct GNUNET_MessageHeader header;
261 * The type for the data.
263 uint32_t type GNUNET_PACKED;
266 * Number of peers recorded in the outgoing path from source to the
267 * stored location of this message.
269 uint32_t put_path_length GNUNET_PACKED;
272 * Length of the GET path that follows (if tracked).
274 uint32_t get_path_length GNUNET_PACKED;
277 * Peer which queried for get and should get the result.
279 struct GNUNET_PeerIdentity querying_peer;
282 * When does the content expire?
284 struct GNUNET_TIME_Absolute expiration_time;
287 * The key of the corresponding GET request.
289 struct GNUNET_HashCode key;
291 /* put path (if tracked) */
293 /* get path (if tracked) */
300 * P2P Trail setup message
302 struct PeerTrailSetupMessage
305 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP
307 struct GNUNET_MessageHeader header;
310 * Is source_peer trying to setup the trail to a predecessor or any finger.
312 uint32_t is_predecessor;
315 * Peer closest to this value will be our finger.
317 uint64_t final_destination_finger_value;
320 * Source peer which wants to setup the trail to one of its finger.
322 struct GNUNET_PeerIdentity source_peer;
325 * Best known destination (could be my friend or finger) which should
326 * get this message next.
328 * FIXME: this could be removed if we include trail_source / trail_dest
329 * in the routing table. This way we save 32 bytes of bandwidth by using
330 * extra 8 bytes of memory (2 * sizeof (GNUNET_PEER_ID))
332 struct GNUNET_PeerIdentity best_known_destination;
335 * In case best_known_destination is a finger, then trail id of trail to
336 * reach to this finger.
338 struct GNUNET_HashCode intermediate_trail_id;
341 * Trail id for trail which we are trying to setup.
343 struct GNUNET_HashCode trail_id;
345 /* List of peers which are part of trail setup so far.
346 * Trail does NOT include source_peer and peer which will be closest to
347 * ultimate_destination_finger_value.
348 * struct GNUNET_PeerIdentity trail[]
353 * P2P Trail Setup Result message
355 struct PeerTrailSetupResultMessage
359 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT
361 struct GNUNET_MessageHeader header;
364 * Finger to which we have found the path.
366 struct GNUNET_PeerIdentity finger_identity;
369 * Peer which started trail_setup to find trail to finger_identity
371 struct GNUNET_PeerIdentity querying_peer;
374 * Is the trail setup to querying_peer's predecessor or finger?
376 uint32_t is_predecessor;
379 * Value to which finger_identity is the closest peer.
381 uint64_t ulitmate_destination_finger_value;
384 * Identifier of the trail from querying peer to finger_identity, NOT
385 * including both endpoints.
387 struct GNUNET_HashCode trail_id;
389 /* List of peers which are part of the trail from querying peer to
390 * finger_identity, NOT including both endpoints.
391 * struct GNUNET_PeerIdentity trail[]
396 * P2P Verify Successor Message.
398 struct PeerVerifySuccessorMessage
401 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR
403 struct GNUNET_MessageHeader header;
406 * Peer which wants to verify its successor.
408 struct GNUNET_PeerIdentity source_peer;
411 * Source Peer's current successor.
413 struct GNUNET_PeerIdentity successor;
416 * Identifier of trail to reach from source_peer to successor.
418 struct GNUNET_HashCode trail_id;
420 /* List of the peers which are part of trail to reach from source_peer
421 * to successor, NOT including them
422 * struct GNUNET_PeerIdentity trail[]
427 * P2P Verify Successor Result Message
429 struct PeerVerifySuccessorResultMessage
432 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT
434 struct GNUNET_MessageHeader header;
437 * Peer which sent the request to verify its successor.
439 struct GNUNET_PeerIdentity querying_peer;
442 * Successor to which PeerVerifySuccessorMessage was sent.
444 struct GNUNET_PeerIdentity current_successor;
447 * Current Predecessor of source_successor. It can be same as querying peer
448 * or different. In case it is different then it can be querying_peer's
449 * probable successor.
451 struct GNUNET_PeerIdentity probable_successor;
454 * Trail identifier of trail from querying_peer to current_successor.
456 struct GNUNET_HashCode trail_id;
459 * Direction in which we are looking at the trail.
461 uint32_t trail_direction;
463 /* In case probable_successor != querying_peer, then trail to reach from
464 * querying_peer to probable_successor, NOT including end points.
465 * struct GNUNET_PeerIdentity trail[]
470 * P2P Notify New Successor Message.
472 struct PeerNotifyNewSuccessorMessage
475 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR
477 struct GNUNET_MessageHeader header;
480 * Peer which wants to notify its new successor.
482 struct GNUNET_PeerIdentity source_peer;
485 * New successor of source_peer.
487 struct GNUNET_PeerIdentity new_successor;
490 * Unique identifier of the trail from source_peer to new_successor,
491 * NOT including the endpoints.
493 struct GNUNET_HashCode trail_id;
495 /* List of peers in trail from source_peer to new_successor,
496 * NOT including the endpoints.
497 * struct GNUNET_PeerIdentity trail[]
503 * P2P Trail Tear Down message.
505 struct PeerTrailTearDownMessage
508 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN
510 struct GNUNET_MessageHeader header;
513 * Unique identifier of the trail.
515 struct GNUNET_HashCode trail_id;
518 * Direction of trail.
520 uint32_t trail_direction;
525 * P2P Trail Rejection Message.
527 struct PeerTrailRejectionMessage
530 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION
532 struct GNUNET_MessageHeader header;
535 * Peer which wants to set up the trail.
537 struct GNUNET_PeerIdentity source_peer;
540 * Peer which sent trail rejection message as it it congested.
542 struct GNUNET_PeerIdentity congested_peer;
545 * Peer identity closest to this value will be finger of
548 uint64_t ultimate_destination_finger_value;
551 * Is source_peer trying to setup the trail to its predecessor or finger.
553 uint32_t is_predecessor;
556 * Identifier for the trail that source peer is trying to setup.
558 struct GNUNET_HashCode trail_id;
561 * Relative time for which congested_peer will remain congested.
563 struct GNUNET_TIME_Relative congestion_time;
565 /* Trail_list from source_peer to peer which sent the message for trail setup
566 * to congested peer. This trail does NOT include source_peer.
567 struct GNUNET_PeerIdnetity trail[]*/
571 * P2P Add Trail Message.
573 struct PeerAddTrailMessage
576 * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL
578 struct GNUNET_MessageHeader header;
581 * Source of the routing trail.
583 struct GNUNET_PeerIdentity source_peer;
586 * Destination of the routing trail.
588 struct GNUNET_PeerIdentity destination_peer;
591 * Unique identifier of the trail from source_peer to destination_peer,
592 * NOT including the endpoints.
594 struct GNUNET_HashCode trail_id;
596 /* Trail from source peer to destination peer, NOT including them.
597 * struct GNUNET_PeerIdentity trail[]
601 GNUNET_NETWORK_STRUCT_END
604 * Linked list of messages to send to a particular other peer.
606 struct P2PPendingMessage
609 * Pointer to next item in the list
611 struct P2PPendingMessage *next;
614 * Pointer to previous item in the list
616 struct P2PPendingMessage *prev;
619 * Message importance level. FIXME: used? useful?
621 unsigned int importance;
624 * When does this message time out?
626 struct GNUNET_TIME_Absolute timeout;
629 * Actual message to be sent, allocated at the end of the struct:
630 * // msg = (cast) &pm[1];
631 * // memcpy (&pm[1], data, len);
633 const struct GNUNET_MessageHeader *msg;
638 * Entry in friend_peermap.
645 struct GNUNET_PeerIdentity id;
648 * Number of trails for which this friend is the first hop or if the friend
651 unsigned int trails_count;
654 * Count of outstanding messages for this friend.
656 unsigned int pending_count;
659 * In case not 0, then amount of time for which this friend is congested.
661 struct GNUNET_TIME_Absolute congestion_timestamp;
664 // TODO : Change name of head and tail to pending_messages_list_head and so.
666 * Head of pending messages to be sent to this friend.
668 struct P2PPendingMessage *head;
671 * Tail of pending messages to be sent to this friend.
673 struct P2PPendingMessage *tail;
676 * Core handle for sending messages to this friend.
678 struct GNUNET_CORE_TransmitHandle *th;
683 * An individual element of the trail to reach to a finger.
688 * Pointer to next item in the list
690 struct Trail_Element *next;
693 * Pointer to prev item in the list
695 struct Trail_Element *prev;
698 * An element in this trail.
700 struct GNUNET_PeerIdentity peer;
704 * Information about an individual trail.
711 struct Trail_Element *trail_head;
716 struct Trail_Element *trail_tail;
719 * Unique identifier of this trail.
721 struct GNUNET_HashCode trail_id;
724 * Length of trail pointed
726 unsigned int trail_length;
729 * Is there a valid trail entry.
731 unsigned int is_present;
735 * An entry in finger_table
742 struct GNUNET_PeerIdentity finger_identity;
745 * Is any finger stored at this finger index.
747 unsigned int is_present;
750 * Index in finger peer map
752 uint32_t finger_table_index;
755 * Number of trails setup so far for this finger.
756 * Should not cross MAXIMUM_TRAILS_PER_FINGER.
758 uint32_t trails_count;
761 * Array of trails to reach to this finger.
763 struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER];
768 * Stores information about the peer which is closest to destination_finger_value.
769 * 'closest' can be either successor or predecessor depending on is_predecessor
775 * Destination finger value.
777 uint64_t destination_finger_value;
780 * Is finger_value a predecessor or any other finger.
782 unsigned int is_predecessor;
785 * Trail id to reach to peer.
786 * In case peer is my identity or friend, it is set to 0.
788 struct GNUNET_HashCode trail_id;
791 * Next destination. In case of friend and my_identity , it is same as next_hop
792 * In case of finger it is finger identity.
794 struct GNUNET_PeerIdentity best_known_destination;
797 * In case best_known_destination is a finger, then first friend in the trail
798 * to reach to it. In other case, same as best_known_destination.
800 struct GNUNET_PeerIdentity next_hop;
803 * In case finger is the next hop, it contains a valid finger table index
804 * at which the finger is stored. Else, It contains 65, which is out of range
805 * of finger table index.
807 unsigned int finger_table_index;
812 * Task that sends FIND FINGER TRAIL requests. This task is started when we have
813 * get our first friend.
815 static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
818 * Task that sends verify successor message. This task is started when we get
819 * our successor for the first time.
821 static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_task;
824 * Identity of this peer.
826 static struct GNUNET_PeerIdentity my_identity;
829 * Peer map of all the friends of a peer
831 static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
834 * Array of all the fingers.
836 static struct FingerInfo finger_table [MAX_FINGERS];
841 static struct GNUNET_CORE_Handle *core_api;
844 * Handle for the statistics service.
846 //extern struct GNUNET_STATISTICS_Handle *GDS_stats;
849 * The current finger index that we have want to find trail to. We start the
850 * search with value = 0, i.e. successor and then go to PREDCESSOR_FINGER_ID
851 * and decrement it. For any index 63 <= index < 0, if finger is same as successor,
852 * we reset this index to 0.
854 static unsigned int current_search_finger_index;
857 * Should we store our topology predecessor and successor IDs into statistics?
859 unsigned int track_topology;
862 * Should I be a malicious peer and drop the PUT/GET packets?
863 * if 0 then NOT malicious.
865 unsigned int act_malicious;
868 * Time duration to schedule find finger trail task.
870 static struct GNUNET_TIME_Relative find_finger_trail_task_next_send_time;
873 * Time duration to schedule verify successor task.
875 static struct GNUNET_TIME_Relative verify_successor_next_send_time;
878 * Called when core is ready to send a message we asked for
879 * out to the destination.
881 * @param cls the 'struct FriendInfo' of the target friend
882 * @param size number of bytes available in buf
883 * @param buf where the callee should write the message
884 * @return number of bytes written to buf
887 core_transmit_notify (void *cls, size_t size, void *buf)
889 struct FriendInfo *peer = cls;
891 struct P2PPendingMessage *pending;
896 while ((NULL != (pending = peer->head)) &&
897 (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us))
899 peer->pending_count--;
900 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
901 GNUNET_free (pending);
905 /* no messages pending */
911 GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
912 GNUNET_CORE_PRIO_BEST_EFFORT,
913 GNUNET_TIME_absolute_get_remaining
914 (pending->timeout), &peer->id,
915 ntohs (pending->msg->size),
916 &core_transmit_notify, peer);
917 GNUNET_break (NULL != peer->th);
921 while ((NULL != (pending = peer->head)) &&
922 (size - off >= (msize = ntohs (pending->msg->size))))
924 GNUNET_STATISTICS_update (GDS_stats,
926 ("# Bytes transmitted to other peers"), msize,
928 memcpy (&cbuf[off], pending->msg, msize);
930 peer->pending_count--;
931 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
932 GNUNET_free (pending);
934 if (peer->head != NULL)
937 GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
938 GNUNET_CORE_PRIO_BEST_EFFORT,
939 GNUNET_TIME_absolute_get_remaining
940 (pending->timeout), &peer->id, msize,
941 &core_transmit_notify, peer);
942 GNUNET_break (NULL != peer->th);
949 * Transmit all messages in the friend's message queue.
951 * @param peer message queue to process
954 process_friend_queue (struct FriendInfo *peer)
956 struct P2PPendingMessage *pending;
958 if (NULL == (pending = peer->head))
962 if (NULL != peer->th)
968 GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
970 GNUNET_TIME_absolute_get_remaining
971 (pending->timeout), &peer->id,
972 ntohs (pending->msg->size),
973 &core_transmit_notify, peer);
974 GNUNET_break (NULL != peer->th);
980 * Set the ENABLE_MALICIOUS value to malicious.
984 GDS_NEIGHBOURS_act_malicious (unsigned int malicious)
986 act_malicious = malicious;
991 * Construct a trail setup message and forward it to target_friend
992 * @param source_peer Peer which wants to setup the trail
993 * @param ultimate_destination_finger_value Peer identity closest to this value
994 * will be finger to @a source_peer
995 * @param best_known_destination Best known destination (could be finger or friend)
996 * which should get this message. In case it is
997 * friend, then it is same as target_friend
998 * @param target_friend Friend to which message is forwarded now.
999 * @param trail_length Total number of peers in trail setup so far.
1000 * @param trail_peer_list Trail setup so far
1001 * @param is_predecessor Is @a source_peer looking for trail to a predecessor or not.
1002 * @param trail_id Unique identifier for the trail we are trying to setup.
1003 * @param intermediate_trail_id Trail id of intermediate trail to reach to
1004 * best_known_destination when its a finger. If not
1005 * used then set to 0.
1008 GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
1009 uint64_t ultimate_destination_finger_value,
1010 struct GNUNET_PeerIdentity best_known_destination,
1011 struct FriendInfo *target_friend,
1012 unsigned int trail_length,
1013 const struct GNUNET_PeerIdentity *trail_peer_list,
1014 unsigned int is_predecessor,
1015 struct GNUNET_HashCode trail_id,
1016 struct GNUNET_HashCode intermediate_trail_id)
1018 struct P2PPendingMessage *pending;
1019 struct PeerTrailSetupMessage *tsm;
1020 struct GNUNET_PeerIdentity *peer_list;
1023 msize = sizeof (struct PeerTrailSetupMessage) +
1024 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1026 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1032 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1034 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1037 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1038 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1039 tsm = (struct PeerTrailSetupMessage *) &pending[1];
1040 pending->msg = &(tsm->header);
1041 tsm->header.size = htons (msize);
1042 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP);
1043 tsm->final_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
1044 tsm->source_peer = source_peer;
1045 tsm->best_known_destination = best_known_destination;
1046 tsm->is_predecessor = htonl (is_predecessor);
1047 tsm->trail_id = trail_id;
1048 tsm->intermediate_trail_id = intermediate_trail_id;
1050 if (trail_length > 0)
1052 peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
1053 memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
1056 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1057 target_friend->pending_count++;
1058 process_friend_queue (target_friend);
1063 * Construct a trail setup result message and forward it to target friend.
1064 * @param querying_peer Peer which sent the trail setup request and should get
1066 * @param Finger Peer to which the trail has been setup to.
1067 * @param target_friend Friend to which this message should be forwarded.
1068 * @param trail_length Numbers of peers in the trail.
1069 * @param trail_peer_list Peers which are part of the trail from
1070 * querying_peer to Finger, NOT including them.
1071 * @param is_predecessor Is @a Finger predecessor to @a querying_peer ?
1072 * @param ultimate_destination_finger_value Value to which @a finger is the closest
1074 * @param trail_id Unique identifier of the trail.
1077 GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity querying_peer,
1078 struct GNUNET_PeerIdentity finger,
1079 struct FriendInfo *target_friend,
1080 unsigned int trail_length,
1081 const struct GNUNET_PeerIdentity *trail_peer_list,
1082 unsigned int is_predecessor,
1083 uint64_t ultimate_destination_finger_value,
1084 struct GNUNET_HashCode trail_id)
1086 struct P2PPendingMessage *pending;
1087 struct PeerTrailSetupResultMessage *tsrm;
1088 struct GNUNET_PeerIdentity *peer_list;
1091 msize = sizeof (struct PeerTrailSetupResultMessage) +
1092 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1094 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1100 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1102 GNUNET_STATISTICS_update (GDS_stats,
1103 gettext_noop ("# P2P messages dropped due to full queue"),
1107 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1108 pending->importance = 0;
1109 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1110 tsrm = (struct PeerTrailSetupResultMessage *) &pending[1];
1111 pending->msg = &tsrm->header;
1112 tsrm->header.size = htons (msize);
1113 tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT);
1114 tsrm->querying_peer = querying_peer;
1115 tsrm->finger_identity = finger;
1116 tsrm->is_predecessor = htonl (is_predecessor);
1117 tsrm->trail_id = trail_id;
1118 tsrm->ulitmate_destination_finger_value =
1119 GNUNET_htonll (ultimate_destination_finger_value);
1120 peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
1121 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1123 /* Send the message to chosen friend. */
1124 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1125 target_friend->pending_count++;
1126 process_friend_queue (target_friend);
1131 * Send trail rejection message to target friend
1132 * @param source_peer Peer which is trying to setup the trail.
1133 * @param ultimate_destination_finger_value Peer closest to this value will be
1134 * @a source_peer's finger
1135 * @param congested_peer Peer which sent this message as it is congested.
1136 * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
1137 * @param trail_peer_list Trails seen so far in trail setup before getting rejected
1138 * by congested_peer. This does NOT include @a source_peer
1139 * and congested_peer.
1140 * @param trail_length Total number of peers in trail_peer_list, NOT including
1141 * @a source_peer and @a congested_peer
1142 * @param trail_id Unique identifier of this trail.
1143 * @param congestion_timeout Duration given by congested peer as an estimate of
1144 * how long it may remain congested.
1147 GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
1148 uint64_t ultimate_destination_finger_value,
1149 struct GNUNET_PeerIdentity congested_peer,
1150 unsigned int is_predecessor,
1151 const struct GNUNET_PeerIdentity *trail_peer_list,
1152 unsigned int trail_length,
1153 struct GNUNET_HashCode trail_id,
1154 struct FriendInfo *target_friend,
1155 const struct GNUNET_TIME_Relative congestion_timeout)
1157 struct PeerTrailRejectionMessage *trm;
1158 struct P2PPendingMessage *pending;
1159 struct GNUNET_PeerIdentity *peer_list;
1162 msize = sizeof (struct PeerTrailRejectionMessage) +
1163 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1165 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1171 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1173 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1177 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1178 pending->importance = 0;
1179 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1180 trm = (struct PeerTrailRejectionMessage *)&pending[1];
1181 pending->msg = &trm->header;
1182 trm->header.size = htons (msize);
1183 trm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION);
1184 trm->source_peer = source_peer;
1185 trm->congested_peer = congested_peer;
1186 trm->congestion_time = congestion_timeout;
1187 trm->is_predecessor = htonl (is_predecessor);
1188 trm->trail_id = trail_id;
1189 trm->ultimate_destination_finger_value =
1190 GNUNET_htonll (ultimate_destination_finger_value);
1192 peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
1193 if (trail_length > 0)
1195 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1198 /* Send the message to chosen friend. */
1199 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1200 target_friend->pending_count++;
1201 process_friend_queue (target_friend);
1206 * Construct a verify successor message and forward it to target_friend.
1207 * @param source_peer Peer which wants to verify its successor.
1208 * @param successor Peer which is @a source_peer's current successor.
1209 * @param trail_id Unique Identifier of trail from @a source_peer to @a successor,
1210 * NOT including them.
1211 * @param trail List of peers which are part of trail to reach from @a source_peer
1212 * to @a successor, NOT including them.
1213 * @param trail_length Total number of peers in @a trail.
1214 * @param target_friend Next friend to get this message.
1217 GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
1218 struct GNUNET_PeerIdentity successor,
1219 struct GNUNET_HashCode trail_id,
1220 struct GNUNET_PeerIdentity *trail,
1221 unsigned int trail_length,
1222 struct FriendInfo *target_friend)
1224 struct PeerVerifySuccessorMessage *vsm;
1225 struct P2PPendingMessage *pending;
1226 struct GNUNET_PeerIdentity *peer_list;
1229 msize = sizeof (struct PeerVerifySuccessorMessage) +
1230 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1232 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1238 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1240 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1244 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1245 pending->importance = 0; /* FIXME */
1246 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1247 vsm = (struct PeerVerifySuccessorMessage *) &pending[1];
1248 pending->msg = &vsm->header;
1249 vsm->header.size = htons (msize);
1250 vsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR);
1251 vsm->source_peer = source_peer;
1252 vsm->successor = successor;
1253 vsm->trail_id = trail_id;
1254 peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
1255 memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
1257 /* Send the message to chosen friend. */
1258 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1259 target_friend->pending_count++;
1260 process_friend_queue (target_friend);
1265 * FIXME: In every function we pass target friend except for this one.
1266 * so, either change everything or this one. also, should se just store
1267 * the pointer to friend in routing table rather than gnunet_peeridentity.
1268 * if yes then we should keep friend info in.h andmake lot of changes.
1269 * Construct a trail teardown message and forward it to target friend.
1270 * @param trail_id Unique identifier of the trail.
1271 * @param trail_direction Direction of trail.
1272 * @param target_friend Friend to get this message.
1275 GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_HashCode trail_id,
1276 unsigned int trail_direction,
1277 struct GNUNET_PeerIdentity peer)
1279 struct PeerTrailTearDownMessage *ttdm;
1280 struct P2PPendingMessage *pending;
1281 struct FriendInfo *target_friend;
1284 msize = sizeof (struct PeerTrailTearDownMessage);
1286 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1292 /*FIXME:In what case friend can be null. ?*/
1293 if (NULL == (target_friend =
1294 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &peer)));
1297 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1299 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1303 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1304 pending->importance = 0; /* FIXME */
1305 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1306 ttdm = (struct PeerTrailTearDownMessage *) &pending[1];
1307 pending->msg = &ttdm->header;
1308 ttdm->header.size = htons (msize);
1309 ttdm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN);
1310 ttdm->trail_id = trail_id;
1311 ttdm->trail_direction = htonl (trail_direction);
1313 /* Send the message to chosen friend. */
1314 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1315 target_friend->pending_count++;
1316 process_friend_queue (target_friend);
1321 * Construct a verify successor result message and send it to target_friend
1322 * @param querying_peer Peer which sent the verify successor message.
1323 * @param source_successor Current_successor of @a querying_peer.
1324 * @param current_predecessor Current predecessor of @a successor. Could be same
1325 * or different from @a querying_peer.
1326 * @param trail_id Unique identifier of the trail from @a querying_peer to
1327 * @a successor, NOT including them.
1328 * @param trail List of peers which are part of trail from @a querying_peer to
1329 * @a successor, NOT including them.
1330 * @param trail_length Total number of peers in @a trail
1331 * @param trail_direction Direction in which we are sending the message. In this
1332 * case we are sending result from @a successor to @a querying_peer.
1333 * @param target_friend Next friend to get this message.
1336 GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity querying_peer,
1337 struct GNUNET_PeerIdentity current_successor,
1338 struct GNUNET_PeerIdentity probable_successor,
1339 struct GNUNET_HashCode trail_id,
1340 const struct GNUNET_PeerIdentity *trail,
1341 unsigned int trail_length,
1342 enum GDS_ROUTING_trail_direction trail_direction,
1343 struct FriendInfo *target_friend)
1345 struct PeerVerifySuccessorResultMessage *vsmr;
1346 struct P2PPendingMessage *pending;
1347 struct GNUNET_PeerIdentity *peer_list;
1350 msize = sizeof (struct PeerVerifySuccessorResultMessage) +
1351 (trail_length * sizeof(struct GNUNET_PeerIdentity));
1353 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1359 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1361 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1365 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1366 pending->importance = 0; /* FIXME */
1367 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1368 vsmr = (struct PeerVerifySuccessorResultMessage *) &pending[1];
1369 pending->msg = &vsmr->header;
1370 vsmr->header.size = htons (msize);
1371 vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT);
1372 vsmr->querying_peer = querying_peer;
1373 vsmr->current_successor = current_successor;
1374 vsmr->probable_successor = probable_successor;
1375 vsmr->trail_direction = htonl (trail_direction);
1376 vsmr->trail_id = trail_id;
1377 peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
1378 memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
1380 /* Send the message to chosen friend. */
1381 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1382 target_friend->pending_count++;
1383 process_friend_queue (target_friend);
1388 * Construct a notify new successor message and send it to target_friend
1389 * @param source_peer Peer which wants to notify to its new successor that it
1390 * could be its predecessor.
1391 * @param successor New successor of @a source_peer
1392 * @param successor_trail List of peers in Trail to reach from
1393 * @a source_peer to @a new_successor, NOT including
1395 * @param successor_trail_length Total number of peers in @a new_successor_trail.
1396 * @param successor_trail_id Unique identifier of @a new_successor_trail.
1397 * @param target_friend Next friend to get this message.
1400 GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer,
1401 struct GNUNET_PeerIdentity successor,
1402 const struct GNUNET_PeerIdentity *successor_trail,
1403 unsigned int successor_trail_length,
1404 struct GNUNET_HashCode succesor_trail_id,
1405 struct FriendInfo *target_friend)
1407 struct PeerNotifyNewSuccessorMessage *nsm;
1408 struct P2PPendingMessage *pending;
1409 struct GNUNET_PeerIdentity *peer_list;
1412 msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
1413 (successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
1415 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1421 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1423 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1427 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1428 pending->importance = 0; /* FIXME */
1429 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1430 nsm = (struct PeerNotifyNewSuccessorMessage *) &pending[1];
1431 pending->msg = &nsm->header;
1432 nsm->header.size = htons (msize);
1433 nsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR);
1434 nsm->new_successor = successor;
1435 nsm->source_peer = source_peer;
1436 nsm->trail_id = succesor_trail_id;
1437 peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
1438 memcpy (peer_list, successor_trail,
1439 successor_trail_length * sizeof (struct GNUNET_PeerIdentity));
1441 /* Send the message to chosen friend. */
1442 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1443 target_friend->pending_count++;
1444 process_friend_queue (target_friend);
1449 * Construct an add_trail message and send it to target_friend
1450 * @param source_peer Source of the trail.
1451 * @param destination_peer Destination of the trail.
1452 * @param trail_id Unique identifier of the trail from
1453 * @a source_peer to @a destination_peer, NOT including the endpoints.
1454 * @param trail List of peers in Trail from @a source_peer to @a destination_peer,
1455 * NOT including the endpoints.
1456 * @param trail_length Total number of peers in @a trail.
1457 * @param target_friend Next friend to get this message.
1460 GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
1461 struct GNUNET_PeerIdentity destination_peer,
1462 struct GNUNET_HashCode trail_id,
1463 const struct GNUNET_PeerIdentity *trail,
1464 unsigned int trail_length,
1465 struct FriendInfo *target_friend)
1467 struct PeerAddTrailMessage *adm;
1468 struct GNUNET_PeerIdentity *peer_list;
1469 struct P2PPendingMessage *pending;
1472 msize = sizeof (struct PeerAddTrailMessage) +
1473 (trail_length * sizeof(struct GNUNET_PeerIdentity));
1475 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1481 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1483 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1487 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1488 pending->importance = 0; /* FIXME */
1489 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
1490 adm = (struct PeerAddTrailMessage *) &pending[1];
1491 pending->msg = &adm->header;
1492 adm->header.size = htons (msize);
1493 adm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL);
1494 adm->source_peer = source_peer;
1495 adm->destination_peer = destination_peer;
1496 adm->trail_id = trail_id;
1497 peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
1498 memcpy (peer_list, trail, sizeof (struct GNUNET_PeerIdentity) * trail_length);
1500 /* Send the message to chosen friend. */
1501 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1502 target_friend->pending_count++;
1503 process_friend_queue (target_friend);
1509 * Search my location in trail. In case I am present more than once in the
1510 * trail (can happen during trail setup), then return my lowest index.
1511 * @param trail List of peers
1512 * @return my_index if found
1513 * trail_length + 1 if an entry is present twice, It is an error.
1514 * -1 if no entry found.
1517 search_my_index (const struct GNUNET_PeerIdentity *trail,
1521 int index_seen = trail_length + 1;
1524 for (i = 0; i < trail_length; i++)
1526 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
1529 if(index_seen == (trail_length + 1))
1533 DEBUG("Entry is present twice in trail. Its not allowed\n");
1547 * Check if the friend is congested or have reached maximum number of trails
1548 * it can be part of of.
1549 * @param friend Friend to be checked.
1550 * @return #GNUNET_NO if friend is not congested or have not crossed threshold.
1551 * #GNUNET_YES if friend is either congested or have crossed threshold
1554 is_friend_congested (struct FriendInfo *friend)
1556 if (( friend->trails_count < TRAILS_THROUGH_FRIEND_THRESHOLD) &&
1557 ((0 == GNUNET_TIME_absolute_get_remaining
1558 (friend->congestion_timestamp).rel_value_us)))
1566 * Select closest finger to value.
1567 * @param peer1 First peer
1568 * @param peer2 Second peer
1569 * @param value Value to be compare
1570 * @return Closest peer
1572 static struct GNUNET_PeerIdentity
1573 select_closest_finger (const struct GNUNET_PeerIdentity *peer1,
1574 const struct GNUNET_PeerIdentity *peer2,
1577 uint64_t peer1_value;
1578 uint64_t peer2_value;
1580 memcpy (&peer1_value, peer1, sizeof (uint64_t));
1581 memcpy (&peer2_value, peer2, sizeof (uint64_t));
1582 peer1_value = GNUNET_ntohll (peer1_value);
1583 peer2_value = GNUNET_ntohll (peer2_value);
1585 // TODO: Can use a simpler (to understand) idea here!
1586 if (peer1_value == value)
1591 if (peer2_value == value)
1596 if (peer2_value < peer1_value)
1598 if ((peer2_value < value) && (value < peer1_value))
1602 else if (((peer1_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1603 ((0 < value) && (value < peer2_value)))
1609 //if (peer1_value < peer2_value)
1611 if ((peer1_value < value) && (value < peer2_value))
1615 //else if (((peer2_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1616 // ((0 < value) && (value < peer1_value)))
1625 * Select closest predecessor to value.
1626 * @param peer1 First peer
1627 * @param peer2 Second peer
1628 * @param value Value to be compare
1629 * @return Peer which precedes value in the network.
1631 static struct GNUNET_PeerIdentity
1632 select_closest_predecessor (const struct GNUNET_PeerIdentity *peer1,
1633 const struct GNUNET_PeerIdentity *peer2,
1636 uint64_t peer1_value;
1637 uint64_t peer2_value;
1639 memcpy (&peer1_value, peer1, sizeof (uint64_t));
1640 memcpy (&peer2_value, peer2, sizeof (uint64_t));
1641 peer1_value = GNUNET_ntohll (peer1_value);
1642 peer2_value = GNUNET_ntohll (peer2_value);
1644 if (peer1_value == value)
1647 if (peer2_value == value)
1650 if (peer1_value < peer2_value)
1652 if ((peer1_value < value) && (value < peer2_value))
1656 else if (((peer2_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1657 ((PEER_IDENTITES_WRAP_AROUND > value) && (value < peer1_value)))
1663 // if (peer2_value < peer1_value)
1665 if ((peer2_value < value) && (value < peer1_value))
1669 //else if (((peer1_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1670 // ((PEER_IDENTITES_WRAP_AROUND > value) && (value < peer2_value)))
1683 test_print_trail (struct GNUNET_PeerIdentity *trail,
1684 unsigned int trail_length)
1686 struct GNUNET_PeerIdentity print_peer;
1689 FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail_length = %d"),
1690 __FILE__, __func__,__LINE__,trail_length);
1691 for (i =0 ; i< trail_length; i++)
1693 print_peer = trail[i];
1694 FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d]=%s"),
1695 __FILE__, __func__,__LINE__,i,GNUNET_i2s(&print_peer));
1702 * This is a test function to print all the entries of friend table.
1705 test_friend_peermap_print ()
1707 struct FriendInfo *friend;
1708 struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter;
1709 struct GNUNET_PeerIdentity print_peer;
1710 struct GNUNET_PeerIdentity key_ret;
1713 print_peer = my_identity;
1714 FPRINTF (stderr,_("\nSUPU************ FRIEND_PEERMAP of %s"),GNUNET_i2s(&print_peer));
1715 friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
1717 for (i = 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
1719 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter,
1721 (const void **)&friend))
1723 memcpy (&print_peer, &key_ret, sizeof (struct GNUNET_PeerIdentity));
1724 FPRINTF (stderr,_("\nSUPU %s, %s, %d, friend = %s, friend->trails_count = %d"),
1725 __FILE__, __func__,__LINE__, GNUNET_i2s(&print_peer), friend->trails_count);
1733 * This is a test function, to print all the entries of finger table.
1736 test_finger_table_print()
1738 struct FingerInfo *finger;
1739 struct GNUNET_PeerIdentity print_peer;
1740 //struct Trail *trail;
1744 print_peer = my_identity;
1745 FPRINTF (stderr,_("\nSUPU************ FINGER_TABLE of %s"),GNUNET_i2s(&print_peer));
1746 for (i = 0; i < MAX_FINGERS; i++)
1748 finger = &finger_table[i];
1750 if (GNUNET_NO == finger->is_present)
1753 print_peer = finger->finger_identity;
1754 FPRINTF (stderr,_("\nSUPU %s, %s, %d, finger_table[%d] = %s, trails_count = %d"),
1755 __FILE__, __func__,__LINE__,i,GNUNET_i2s (&print_peer), finger->trails_count);
1758 for (j = 0; j < finger->trails_count; j++)
1760 trail = &finger->trail_list[j];
1761 FPRINTF (stderr,_("\nSUPU %s, %s, %d, trail_id[%d]=%s"),__FILE__, __func__,__LINE__,j, GNUNET_h2s(&trail->trail_id));
1762 struct Trail_Element *element;
1763 element = trail->trail_head;
1764 for (k = 0; k < trail->trail_length; k++)
1766 print_peer = element->peer;
1767 FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d] = %s "),__FILE__, __func__,__LINE__,k, GNUNET_i2s(&print_peer));
1768 element = element->next;
1777 * Select the closest peer among two peers (which should not be same)
1778 * with respect to value and finger_table_index
1779 * NOTE: peer1 != peer2
1780 * @param peer1 First peer
1781 * @param peer2 Second peer
1782 * @param value Value relative to which we find the closest
1783 * @param is_predecessor Is value a predecessor or any other finger.
1784 * @return Closest peer among two peers.
1786 static struct GNUNET_PeerIdentity
1787 select_closest_peer (const struct GNUNET_PeerIdentity *peer1,
1788 const struct GNUNET_PeerIdentity *peer2,
1790 unsigned int is_predecessor)
1792 /* This check is here to ensure that calling function never sends
1793 same peer value in peer1 and peer2. Remove it later. */
1794 GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (peer1, peer2));
1795 if (1 == is_predecessor)
1796 return select_closest_predecessor (peer1, peer2, value);
1798 // TODO: Change name to something like select_closest_successor!!
1799 return select_closest_finger (peer1, peer2, value);
1804 * Iterate over the list of all the trails of a finger. In case the first
1805 * friend to reach the finger has reached trail threshold or is congested,
1806 * then don't select it. In case there multiple available good trails to reach
1807 * to Finger, choose the one with shortest trail length.
1808 * Note: We use length as parameter. But we can use any other suitable parameter
1810 * @param finger Finger
1813 static struct Trail *
1814 select_finger_trail (struct FingerInfo *finger)
1816 struct FriendInfo *friend;
1817 struct Trail *current_finger_trail;
1818 struct Trail *best_trail = NULL;
1821 GNUNET_assert (finger->trails_count > 0);
1823 for (i = 0; i < finger->trails_count; i++)
1825 current_finger_trail = &finger->trail_list[i];
1827 /* No trail stored at this index. */
1828 if (GNUNET_NO == current_finger_trail->is_present)
1831 GNUNET_assert (NULL !=
1833 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1834 ¤t_finger_trail->trail_head->peer)));
1836 /* First friend to reach trail is not free. */
1837 if (GNUNET_YES == is_friend_congested (friend))
1840 if (NULL == best_trail ||
1841 best_trail->trail_length > current_finger_trail->trail_length)
1843 best_trail = current_finger_trail;
1852 * Compare FINGER entry with current successor. If finger's first friend of all
1853 * its trail is not congested and has not crossed trail threshold, then check
1854 * if finger peer identity is closer to final_destination_finger_value than
1855 * current_successor. If yes then update current_successor.
1856 * @param current_successor[in/out]
1860 compare_finger_and_current_closest_peer (struct Closest_Peer *current_closest_peer)
1862 struct FingerInfo *finger;
1863 struct GNUNET_PeerIdentity closest_peer;
1864 struct Trail *finger_trail;
1867 /* Iterate over finger table. */
1868 for (i = 0; i < MAX_FINGERS; i++)
1870 finger = &finger_table[i];
1872 if (GNUNET_NO == finger->is_present)
1875 /* FIXME write correct comment here */
1876 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1877 ¤t_closest_peer->best_known_destination))
1880 /* If I am my own finger, then ignore this finger. */
1881 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1884 /* FIXME: I think a peer should not select itself as its own identity ever.
1885 But it does select. Find out why??*/
1891 /* If finger is a friend, then do nothing. As we have already checked
1892 * for each friend in compare_friend_and_current_successor(). */
1893 if (NULL != (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1894 &finger->finger_identity)))
1899 closest_peer = select_closest_peer (&finger->finger_identity,
1900 ¤t_closest_peer->best_known_destination,
1901 current_closest_peer->destination_finger_value,
1902 current_closest_peer->is_predecessor);
1904 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&finger->finger_identity, &closest_peer))
1906 /* Choose one of the trail to reach to finger. */
1907 finger_trail = select_finger_trail (finger);
1909 /* In case no trail found, ignore this finger. */
1910 if (NULL == finger_trail)
1913 current_closest_peer->best_known_destination = closest_peer;
1914 current_closest_peer->next_hop = finger_trail->trail_head->peer;
1915 current_closest_peer->trail_id = finger_trail->trail_id;
1916 current_closest_peer->finger_table_index = i;
1924 * Compare friend entry with current successor.
1925 * If friend identity and current_successor is same, then do nothing.
1926 * If friend is not congested and has not crossed trail threshold, then check
1927 * if friend peer identity is closer to final_destination_finger_value than
1928 * current_successor. If yes then update current_successor.
1929 * @param cls closure
1930 * @param key current public key
1931 * @param value struct Closest_Peer
1932 * @return #GNUNET_YES if we should continue to iterate,
1933 * #GNUNET_NO if not.
1936 compare_friend_and_current_closest_peer (void *cls,
1937 const struct GNUNET_PeerIdentity *key,
1940 struct FriendInfo *friend = value;
1941 struct Closest_Peer *current_closest_peer = cls;
1942 struct GNUNET_PeerIdentity closest_peer;
1944 /* Friend is either congested or has crossed threshold. */
1945 if (GNUNET_YES == is_friend_congested (friend))
1948 /* If current_closest_peer and friend identity are same, then do nothing.*/
1949 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id,
1950 ¤t_closest_peer->best_known_destination))
1956 closest_peer = select_closest_peer (&friend->id,
1957 ¤t_closest_peer->best_known_destination,
1958 current_closest_peer->destination_finger_value,
1959 current_closest_peer->is_predecessor);
1961 /* Is friend the closest successor? */
1962 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&friend->id, &closest_peer))
1964 current_closest_peer->best_known_destination = friend->id;
1965 current_closest_peer->next_hop = friend->id;
1973 * Initialize current_successor to my_identity.
1974 * @param my_identity My peer identity
1975 * @return Updated closest_peer
1977 static struct Closest_Peer
1978 init_current_successor (struct GNUNET_PeerIdentity my_identity,
1979 uint64_t destination_finger_value,
1980 unsigned int is_predecessor)
1982 struct Closest_Peer current_closest_peer;
1984 memset (¤t_closest_peer.trail_id, 0, sizeof(struct GNUNET_HashCode));
1985 current_closest_peer.destination_finger_value = destination_finger_value;
1986 current_closest_peer.is_predecessor = is_predecessor;
1987 current_closest_peer.next_hop = my_identity;
1988 current_closest_peer.best_known_destination = my_identity;
1989 current_closest_peer.finger_table_index = 65; //65 is a for non valid finger table index.
1990 return current_closest_peer;
1995 * FIXME: at the moment, there is not 100% get and put in case of non-malicious
1996 * peer. It could be because of the logic we wrote here. Verify if its correct.
1997 * If not then return immediate_successor.
1999 * Find the successor for destination_finger_value among my_identity, my
2000 * friends and my fingers. Don't consider friends or fingers which are either
2001 * congested or have crossed the threshold.
2002 * NOTE: In case a friend is also a finger, then it is always chosen as friend
2004 * @param destination_finger_value Peer closest to this value will be the next successor.
2005 * @param is_predecessor Are we looking for predecessor or finger?
2006 * @return Successor It is never NULL, in case none of friend or finger is closest,
2007 * then we return my_identity.
2009 static struct Closest_Peer
2010 find_successor (uint64_t destination_finger_value,
2011 unsigned int is_predecessor)
2013 struct Closest_Peer current_closest_peer;
2015 /* Initialize current_successor to my_identity. */
2016 current_closest_peer = init_current_successor (my_identity,
2017 destination_finger_value,
2020 /* Compare each friend entry with current_successor and update current_successor
2021 * with friend if its closest. */
2024 GNUNET_CONTAINER_multipeermap_iterate (friend_peermap,
2025 &compare_friend_and_current_closest_peer,
2026 ¤t_closest_peer));
2028 /* Compare each finger entry with current_successor and update current_successor
2029 * with finger if its closest. */
2030 compare_finger_and_current_closest_peer (¤t_closest_peer);
2031 return current_closest_peer;
2035 * FIXME; Send put message across all the trail to reach to next hop to handle
2037 * Construct a Put message and send it to target_peer.
2038 * @param key Key for the content
2039 * @param block_type Type of the block
2040 * @param options Routing options
2041 * @param desired_replication_level Desired replication count
2042 * @param best_known_dest Peer to which this message should reach eventually,
2043 * as it is best known destination to me.
2044 * @param intermediate_trail_id Trail id in case
2045 * @param target_peer Peer to which this message will be forwarded.
2046 * @param hop_count Number of hops traversed so far.
2047 * @param put_path_length Total number of peers in @a put_path
2048 * @param put_path Number of peers traversed so far
2049 * @param expiration_time When does the content expire
2050 * @param data Content to store
2051 * @param data_size Size of content @a data in bytes
2054 GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
2055 enum GNUNET_BLOCK_Type block_type,
2056 enum GNUNET_DHT_RouteOption options,
2057 uint32_t desired_replication_level,
2058 struct GNUNET_PeerIdentity best_known_dest,
2059 struct GNUNET_HashCode intermediate_trail_id,
2060 struct GNUNET_PeerIdentity *target_peer,
2062 uint32_t put_path_length,
2063 struct GNUNET_PeerIdentity *put_path,
2064 struct GNUNET_TIME_Absolute expiration_time,
2065 const void *data, size_t data_size)
2067 struct PeerPutMessage *ppm;
2068 struct P2PPendingMessage *pending;
2069 struct FriendInfo *target_friend;
2070 struct GNUNET_PeerIdentity *pp;
2073 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
2074 sizeof (struct PeerPutMessage);
2075 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2077 put_path_length = 0;
2078 msize = data_size + sizeof (struct PeerPutMessage);
2081 /* Should it be GNUNET_SERVER_MAX_MESSAGE_SIZE? */
2082 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2084 DEBUG("msize = %lu\n",msize);
2089 GNUNET_assert (NULL !=
2091 GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
2092 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2093 pending->timeout = expiration_time;
2094 ppm = (struct PeerPutMessage *) &pending[1];
2095 pending->msg = &ppm->header;
2096 ppm->header.size = htons (msize);
2097 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT);
2098 ppm->options = htonl (options);
2099 ppm->block_type = htonl (block_type);
2100 ppm->hop_count = htonl (hop_count + 1);
2101 ppm->desired_replication_level = htonl (desired_replication_level);
2102 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
2103 ppm->best_known_destination = best_known_dest;
2104 ppm->intermediate_trail_id = intermediate_trail_id;
2106 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
2107 ppm->put_path_length = htonl (put_path_length);
2108 if(put_path_length > 0)
2110 memcpy (pp, put_path,
2111 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
2113 memcpy (&pp[put_path_length], data, data_size);
2114 GNUNET_assert (NULL != target_friend);
2115 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2116 target_friend->pending_count++;
2117 process_friend_queue (target_friend);
2122 * Handle the put request from the client.
2123 * @param key Key for the content
2124 * @param block_type Type of the block
2125 * @param options Routing options
2126 * @param desired_replication_level Desired replication count
2127 * @param expiration_time When does the content expire
2128 * @param data Content to store
2129 * @param data_size Size of content @a data in bytes
2132 GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
2133 enum GNUNET_BLOCK_Type block_type,
2134 enum GNUNET_DHT_RouteOption options,
2135 uint32_t desired_replication_level,
2136 struct GNUNET_TIME_Absolute expiration_time,
2137 const void *data, size_t data_size)
2139 struct GNUNET_PeerIdentity best_known_dest;
2140 struct GNUNET_HashCode intermediate_trail_id;
2141 struct GNUNET_PeerIdentity next_hop;
2143 struct Closest_Peer successor;
2145 memcpy (&key_value, key, sizeof (uint64_t));
2146 key_value = GNUNET_ntohll (key_value);
2147 successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
2148 best_known_dest = successor.best_known_destination;
2149 next_hop = successor.next_hop;
2150 intermediate_trail_id = successor.trail_id;
2152 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity))
2154 /* I am the destination. */
2155 DEBUG("PUT destination is me = %s,key =%s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(key));
2156 GDS_DATACACHE_handle_put (expiration_time, key, 0, NULL,
2157 block_type,data_size,data);
2158 GDS_CLIENTS_process_put (options, block_type, 0,
2159 ntohl (desired_replication_level),
2160 1, &my_identity, expiration_time, //FIXME: GNUNETnthoh something on expiration time.
2161 key, data, data_size);
2165 /* In case we are sending the request to a finger, then send across all of its
2168 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
2169 &successor.next_hop))
2171 struct FingerInfo *next_hop_finger;
2174 next_hop_finger = &finger_table[successor.finger_table_index];
2175 for (i = 0; i < next_hop_finger->trails_count; i++)
2177 if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
2179 GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
2181 next_hop_finger->trail_list[i].trail_id,
2182 &next_hop, hop_count, put_path_length, put_path,
2190 GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
2191 best_known_dest, intermediate_trail_id, &next_hop,
2192 0, 1, &my_identity, expiration_time,
2197 * FIXME; Send get message across all the trail to reach to next hop to handle
2199 * Construct a Get message and send it to target_peer.
2200 * @param key Key for the content
2201 * @param block_type Type of the block
2202 * @param options Routing options
2203 * @param desired_replication_level Desired replication count
2204 * @param best_known_dest Peer which should get this message. Same as target peer
2205 * if best_known_dest is a friend else its a finger.
2206 * @param intermediate_trail_id Trail id to reach to @a best_known_dest
2207 * in case it is a finger else set to 0.
2208 * @param target_peer Peer to which this message will be forwarded.
2209 * @param hop_count Number of hops traversed so far.
2210 * @param data Content to store
2211 * @param data_size Size of content @a data in bytes
2212 * @param get_path_length Total number of peers in @a get_path
2213 * @param get_path Number of peers traversed so far
2216 GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
2217 enum GNUNET_BLOCK_Type block_type,
2218 enum GNUNET_DHT_RouteOption options,
2219 uint32_t desired_replication_level,
2220 struct GNUNET_PeerIdentity best_known_dest,
2221 struct GNUNET_HashCode intermediate_trail_id,
2222 struct GNUNET_PeerIdentity *target_peer,
2224 uint32_t get_path_length,
2225 struct GNUNET_PeerIdentity *get_path)
2227 struct PeerGetMessage *pgm;
2228 struct P2PPendingMessage *pending;
2229 struct FriendInfo *target_friend;
2230 struct GNUNET_PeerIdentity *gp;
2233 msize = sizeof (struct PeerGetMessage) +
2234 (get_path_length * sizeof (struct GNUNET_PeerIdentity));
2236 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2241 GNUNET_assert (NULL !=
2243 GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
2245 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2246 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
2247 pending->importance = 0; /* FIXME */
2248 pgm = (struct PeerGetMessage *) &pending[1];
2249 pending->msg = &pgm->header;
2250 pgm->header.size = htons (msize);
2251 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET);
2252 pgm->get_path_length = htonl (get_path_length);
2253 pgm->best_known_destination = best_known_dest;
2255 pgm->intermediate_trail_id = intermediate_trail_id;
2256 pgm->hop_count = htonl (hop_count + 1);
2257 pgm->get_path_length = htonl (get_path_length);
2258 gp = (struct GNUNET_PeerIdentity *) &pgm[1];
2259 memcpy (gp, get_path,
2260 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
2261 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2262 target_friend->pending_count++;
2263 process_friend_queue (target_friend);
2268 * Handle the get request from the client file. If I am destination do
2269 * datacache put and return. Else find the target friend and forward message
2271 * @param key Key for the content
2272 * @param block_type Type of the block
2273 * @param options Routing options
2274 * @param desired_replication_level Desired replication count
2277 GDS_NEIGHBOURS_handle_get(const struct GNUNET_HashCode *key,
2278 enum GNUNET_BLOCK_Type block_type,
2279 enum GNUNET_DHT_RouteOption options,
2280 uint32_t desired_replication_level)
2282 struct Closest_Peer successor;
2283 struct GNUNET_PeerIdentity best_known_dest;
2284 struct GNUNET_HashCode intermediate_trail_id;
2287 memcpy (&key_value, key, sizeof (uint64_t));
2288 key_value = GNUNET_ntohll (key_value);
2290 successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
2292 best_known_dest = successor.best_known_destination;
2293 intermediate_trail_id = successor.trail_id;
2295 /* I am the destination. I have the data. */
2296 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2299 DEBUG("GET destination is me = %s,KEY = %s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(key));
2300 GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
2301 NULL, 0, 1, &my_identity, NULL,&my_identity);
2305 /* fixme; for multiple trails, we need to send back finger index and send trail
2306 across all the fingers. but in current implementation we don't have this case.
2307 compare finger and current_successor returns, */
2308 GDS_NEIGHBOURS_send_get (key, block_type, options, desired_replication_level,
2309 best_known_dest,intermediate_trail_id, &successor.next_hop,
2310 0, 1, &my_identity);
2315 * Send the get result to requesting client.
2316 * @param key Key of the requested data.
2317 * @param type Block type
2318 * @param target_peer Next peer to forward the message to.
2319 * @param source_peer Peer which has the data for the key.
2320 * @param put_path_length Number of peers in @a put_path
2321 * @param put_path Path taken to put the data at its stored location.
2322 * @param get_path_length Number of peers in @a get_path
2323 * @param get_path Path taken to reach to the location of the key.
2324 * @param expiration When will this result expire?
2325 * @param data Payload to store
2326 * @param data_size Size of the @a data
2329 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
2330 enum GNUNET_BLOCK_Type type,
2331 const struct GNUNET_PeerIdentity *target_peer,
2332 const struct GNUNET_PeerIdentity *source_peer,
2333 unsigned int put_path_length,
2334 const struct GNUNET_PeerIdentity *put_path,
2335 unsigned int get_path_length,
2336 const struct GNUNET_PeerIdentity *get_path,
2337 struct GNUNET_TIME_Absolute expiration,
2338 const void *data, size_t data_size)
2340 struct PeerGetResultMessage *get_result;
2341 struct GNUNET_PeerIdentity *paths;
2342 struct P2PPendingMessage *pending;
2343 struct FriendInfo *target_friend;
2344 int current_path_index;
2347 msize = (put_path_length + get_path_length )* sizeof (struct GNUNET_PeerIdentity) +
2349 sizeof (struct PeerGetResultMessage);
2351 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2353 put_path_length = 0;
2354 msize = msize - put_path_length;
2358 if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2363 DEBUG("GET RESULT FOR DATA_SIZE = %lu\n",msize);
2364 current_path_index = 0;
2365 if(get_path_length > 0)
2367 current_path_index = search_my_index(get_path, get_path_length);
2368 if (-1 == current_path_index)
2373 if ((get_path_length + 1) == current_path_index)
2375 DEBUG ("Peer found twice in get path. Not allowed \n");
2380 if (0 == current_path_index)
2382 GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
2383 get_path, put_path_length,
2384 put_path, type, data_size, data);
2388 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2389 pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
2390 pending->importance = 0;
2391 get_result = (struct PeerGetResultMessage *)&pending[1];
2392 pending->msg = &get_result->header;
2393 get_result->header.size = htons (msize);
2394 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT);
2395 get_result->key = *key;
2396 get_result->querying_peer = *source_peer;
2397 get_result->expiration_time = expiration;
2398 get_result->get_path_length = htonl (get_path_length);
2399 get_result->put_path_length = htonl (put_path_length);
2400 paths = (struct GNUNET_PeerIdentity *)&get_result[1];
2401 memcpy (paths, put_path,
2402 put_path_length * sizeof (struct GNUNET_PeerIdentity));
2403 memcpy (&paths[put_path_length], get_path,
2404 get_path_length * sizeof (struct GNUNET_PeerIdentity));
2405 memcpy (&paths[put_path_length + get_path_length], data, data_size);
2407 GNUNET_assert (NULL !=
2409 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2410 &get_path[current_path_index - 1])));
2411 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2412 target_friend->pending_count++;
2413 process_friend_queue (target_friend);
2418 * Randomly choose one of your friends (which is not congested and have not crossed
2419 * trail threshold) from the friend_peermap
2420 * @return Friend Randomly chosen friend.
2421 * NULL in case friend peermap is empty, or all the friends are either
2422 * congested or have crossed trail threshold.
2424 static struct FriendInfo *
2425 select_random_friend ()
2427 unsigned int current_size;
2430 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
2431 struct GNUNET_PeerIdentity key_ret;
2432 struct FriendInfo *friend;
2434 current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
2437 if (0 == current_size)
2440 index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
2441 iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
2443 /* Iterate till you don't reach to index. */
2444 for (j = 0; j < index ; j++)
2445 GNUNET_assert (GNUNET_YES ==
2446 GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL, NULL));
2450 /* Reset the index in friend peermap to 0 as we reached to the end. */
2451 if (j == current_size)
2454 GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
2455 iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
2459 /* Get the friend stored at the index, j*/
2460 GNUNET_assert (GNUNET_YES ==
2461 GNUNET_CONTAINER_multipeermap_iterator_next (iter,
2463 (const void **)&friend));
2465 /* This friend is not congested and has not crossed trail threshold. */
2466 if ((friend->trails_count < TRAILS_THROUGH_FRIEND_THRESHOLD) &&
2467 (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us))
2473 } while (j != index);
2475 GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
2481 * Compute 64 bit value of finger_identity corresponding to a finger index using
2483 * For all fingers, n.finger[i] = n + pow (2,i),
2484 * For predecessor, n.finger[PREDECESSOR_FINGER_ID] = n - 1, where
2485 * n = my_identity, i = finger_index, n.finger[i] = 64 bit finger value
2486 * @param finger_index Index corresponding to which we calculate 64 bit value.
2487 * @return 64 bit value.
2490 compute_finger_identity_value (unsigned int finger_index)
2494 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
2495 my_id64 = GNUNET_ntohll (my_id64);
2497 /* Are we looking for immediate predecessor? */
2498 if (PREDECESSOR_FINGER_ID == finger_index)
2499 return (my_id64 - 1);
2502 uint64_t add = (uint64_t)1 << finger_index;
2503 return (my_id64 + add);
2509 * Choose a random friend. Calculate the next finger identity to search,from
2510 * current_search_finger_index. Start looking for the trail to reach to
2511 * finger identity through this random friend.
2513 * @param cls closure for this task
2514 * @param tc the context under which the task is running
2517 send_find_finger_trail_message (void *cls,
2518 const struct GNUNET_SCHEDULER_TaskContext *tc)
2520 struct FriendInfo *target_friend;
2521 struct GNUNET_HashCode trail_id;
2522 struct GNUNET_HashCode intermediate_trail_id;
2523 unsigned int is_predecessor;
2524 uint64_t finger_id_value;
2526 /* Schedule another send_find_finger_trail_message task. */
2527 find_finger_trail_task_next_send_time.rel_value_us =
2528 find_finger_trail_task_next_send_time.rel_value_us +
2529 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2530 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
2531 find_finger_trail_task =
2532 GNUNET_SCHEDULER_add_delayed (find_finger_trail_task_next_send_time,
2533 &send_find_finger_trail_message,
2536 /* No space in my routing table. (Source and destination peers also store entries
2537 * in their routing table). */
2538 if (GNUNET_YES == GDS_ROUTING_threshold_reached())
2541 target_friend = select_random_friend ();
2542 if (NULL == target_friend)
2547 finger_id_value = compute_finger_identity_value (current_search_finger_index);
2549 if (PREDECESSOR_FINGER_ID == current_search_finger_index)
2554 /* Generate a unique trail id for trail we are trying to setup. */
2555 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
2556 &trail_id, sizeof (trail_id));
2557 memset(&intermediate_trail_id, 0, sizeof (struct GNUNET_HashCode));
2558 GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_id_value,
2559 target_friend->id, target_friend, 0, NULL,
2560 is_predecessor, trail_id,
2561 intermediate_trail_id);
2566 * In case there are already maximum number of possible trails to reach to a
2567 * finger, then check if the new trail's length is lesser than any of the
2569 * If yes then replace that old trail by new trail.
2571 * Note: Here we are taking length as a parameter to choose the best possible
2572 * trail, but there could be other parameters also like:
2573 * 1. duration of existence of a trail - older the better.
2574 * 2. if the new trail is completely disjoint than the
2575 * other trails, then may be choosing it is better.
2577 * @param finger Finger
2578 * @param new_finger_trail List of peers to reach from me to @a finger, NOT
2579 * including the endpoints.
2580 * @param new_finger_trail_length Total number of peers in @a new_finger_trail
2581 * @param new_finger_trail_id Unique identifier of @a new_finger_trail.
2584 select_and_replace_trail (struct FingerInfo *finger,
2585 const struct GNUNET_PeerIdentity *new_trail,
2586 unsigned int new_trail_length,
2587 struct GNUNET_HashCode new_trail_id)
2589 struct Trail *trail;
2590 unsigned int largest_trail_length;
2591 unsigned int largest_trail_index;
2592 struct Trail_Element *trail_element;
2595 largest_trail_length = new_trail_length;
2596 largest_trail_index = MAXIMUM_TRAILS_PER_FINGER + 1;
2598 GNUNET_assert (MAXIMUM_TRAILS_PER_FINGER == finger->trails_count);
2600 for (i = 0; i < finger->trails_count; i++)
2602 trail = &finger->trail_list[i];
2603 if (trail->trail_length > largest_trail_length)
2605 largest_trail_length = trail->trail_length;
2606 largest_trail_index = i;
2610 /* New trail is not better than existing ones. Send trail teardown. */
2611 if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
2613 struct GNUNET_PeerIdentity next_hop;
2615 memcpy (&next_hop, &new_trail[0], sizeof(struct GNUNET_PeerIdentity));
2616 GDS_ROUTING_remove_trail (new_trail_id);
2617 GDS_NEIGHBOURS_send_trail_teardown (new_trail_id,
2618 GDS_ROUTING_SRC_TO_DEST,
2623 /* Send trail teardown message across the replaced trail. */
2624 struct Trail *replace_trail = &finger->trail_list[largest_trail_index];
2626 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (replace_trail->trail_id));
2627 GDS_NEIGHBOURS_send_trail_teardown (replace_trail->trail_id,
2628 GDS_ROUTING_SRC_TO_DEST,
2629 replace_trail->trail_head->peer);
2630 /* Free the trail. */
2631 while (NULL != (trail_element = replace_trail->trail_head))
2633 GNUNET_CONTAINER_DLL_remove (replace_trail->trail_head,
2634 replace_trail->trail_tail, trail_element);
2635 GNUNET_free_non_null (trail_element);
2638 /* Add new trial at that location. */
2639 replace_trail->is_present = GNUNET_YES;
2640 replace_trail->trail_length = new_trail_length;
2641 replace_trail->trail_id = new_trail_id;
2643 for (i = 0; i < new_trail_length; i++)
2645 struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2646 element->peer = new_trail[i];
2648 GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
2649 replace_trail->trail_tail,
2652 /* FIXME: URGENT Are we adding the trail back to the list. */
2657 * Check if the new trail to reach to finger is unique or do we already have
2658 * such a trail present for finger.
2659 * @param existing_finger Finger identity
2660 * @param new_trail New trail to reach @a existing_finger
2661 * @param trail_length Total number of peers in new_trail.
2662 * @return #GNUNET_YES if the new trail is unique
2663 * #GNUNET_NO if same trail is already present.
2666 is_new_trail_unique (struct FingerInfo *existing_finger,
2667 const struct GNUNET_PeerIdentity *new_trail,
2668 unsigned int trail_length)
2670 struct Trail *trail;
2671 struct Trail_Element *trail_element;
2675 GNUNET_assert (existing_finger->trails_count > 0);
2677 /* Iterate over list of trails. */
2678 for (i = 0; i < existing_finger->trails_count; i++)
2680 trail = &(existing_finger->trail_list[i]);
2681 GNUNET_assert (GNUNET_YES == trail->is_present);
2683 /* New trail and existing trail length are not same. */
2684 if (trail->trail_length != trail_length)
2689 trail_element = trail->trail_head;
2690 GNUNET_assert (trail_element != NULL);
2691 for (j = 0; j < trail->trail_length; j++)
2693 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&new_trail[j],
2694 &trail_element->peer))
2698 trail_element = trail_element->next;
2706 * FIXME; In case of multiple trails, we may have a case where a trail from in
2707 * between has been removed, then we should try to find a free slot , not simply
2708 * add a trail at then end of the list.
2709 * Add a new trail to existing finger. This function is called only when finger
2710 * is not my own identity or a friend.
2711 * @param existing_finger Finger
2712 * @param new_finger_trail New trail from me to finger, NOT including endpoints
2713 * @param new_finger_trail_length Total number of peers in @a new_finger_trail
2714 * @param new_finger_trail_id Unique identifier of the trail.
2717 add_new_trail (struct FingerInfo *existing_finger,
2718 const struct GNUNET_PeerIdentity *new_trail,
2719 unsigned int new_trail_length,
2720 struct GNUNET_HashCode new_trail_id)
2722 struct Trail *trail;
2723 struct FriendInfo *first_friend;
2727 if (GNUNET_NO == is_new_trail_unique (existing_finger, new_trail,
2733 index = existing_finger->trails_count;
2734 trail = &existing_finger->trail_list[index];
2735 GNUNET_assert (GNUNET_NO == trail->is_present);
2736 trail->trail_id = new_trail_id;
2737 trail->trail_length = new_trail_length;
2738 existing_finger->trails_count++;
2739 trail->is_present = GNUNET_YES;
2741 GNUNET_assert (NULL == (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2742 &existing_finger->finger_identity)));
2743 /* If finger is a friend then we never call this function. */
2744 GNUNET_assert (new_trail_length > 0);
2746 first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2748 first_friend->trails_count++;
2750 for (i = 0; i < new_trail_length; i++)
2752 struct Trail_Element *element;
2754 element = GNUNET_new (struct Trail_Element);
2755 element->peer = new_trail[i];
2756 GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
2760 /* Do we need to add trail head and trail tail in the trail list itearator.*/
2761 existing_finger->trail_list[index].trail_head = trail->trail_head;
2762 existing_finger->trail_list[index].trail_tail = trail->trail_tail;
2763 existing_finger->trail_list[index].trail_length = new_trail_length;
2764 existing_finger->trail_list[index].trail_id = new_trail_id;
2765 existing_finger->trail_list[index].is_present = GNUNET_YES;
2770 * FIXME Check if this function is called for opposite direction if yes then
2771 * take it as parameter.
2772 * Get the next hop to send trail teardown message from routing table and
2773 * then delete the entry from routing table. Send trail teardown message for a
2774 * specific trail of a finger.
2775 * @param finger Finger whose trail is to be removed.
2776 * @param trail List of peers in trail from me to a finger, NOT including
2780 send_trail_teardown (struct FingerInfo *finger,
2781 struct Trail *trail)
2783 struct FriendInfo *friend;
2784 struct GNUNET_PeerIdentity *next_hop;
2786 next_hop = GDS_ROUTING_get_next_hop (trail->trail_id,
2787 GDS_ROUTING_SRC_TO_DEST);
2789 if (NULL == next_hop)
2791 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
2792 GNUNET_i2s(&my_identity), GNUNET_h2s(&trail->trail_id), __LINE__);
2797 GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
2800 /*FIXME: here was an assertion that trail->is_present = GNUNET_YES. But it
2801 used to fail. We have removed assertion with a condition, just for now.
2802 Find out the reason why assertion failed. */
2803 if (trail->is_present == GNUNET_NO)
2805 DEBUG(" trail id %s of peer %s is not present to send a trail teardown message., line",
2806 GNUNET_i2s(&my_identity), GNUNET_h2s(&trail->trail_id), __LINE__);
2810 /* Finger is not a friend. */
2811 if (trail->trail_length > 0)
2813 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2814 &trail->trail_head->peer);
2818 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2819 &finger->finger_identity);
2824 DEBUG ("\n LINE NO: = %d, Friend not found for trail id %s of peer %s trail length = %d",
2825 __LINE__,GNUNET_h2s(&trail->trail_id), GNUNET_i2s(&my_identity),trail->trail_length);
2828 if (0 != GNUNET_CRYPTO_cmp_peer_identity (next_hop, &friend->id)
2829 && (0 == trail->trail_length))
2831 DEBUG ("\n LINE NO: = %d, Friend not found for trail id %s of peer %s trail length = %d",
2832 __LINE__,GNUNET_h2s(&trail->trail_id), GNUNET_i2s(&my_identity),trail->trail_length);
2835 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail->trail_id));
2836 friend->trails_count--;
2837 GDS_NEIGHBOURS_send_trail_teardown (trail->trail_id,
2838 GDS_ROUTING_SRC_TO_DEST,
2844 * Send trail teardown message across all the trails to reach to finger.
2845 * @param finger Finger whose all the trail should be freed.
2848 send_all_finger_trails_teardown (struct FingerInfo *finger)
2851 for (i = 0; i < finger->trails_count; i++)
2853 struct Trail *trail;
2855 trail = &finger->trail_list[i];
2856 GNUNET_assert (trail->is_present == GNUNET_YES);
2857 send_trail_teardown (finger, trail);
2858 trail->is_present = GNUNET_NO;
2864 * Free a specific trail
2865 * @param trail List of peers to be freed.
2868 free_trail (struct Trail *trail)
2870 struct Trail_Element *trail_element;
2872 while (NULL != (trail_element = trail->trail_head))
2874 GNUNET_CONTAINER_DLL_remove (trail->trail_head,
2877 GNUNET_free_non_null (trail_element);
2879 trail->trail_head = NULL;
2880 trail->trail_tail = NULL;
2885 * Free finger and its trail.
2886 * @param finger Finger to be freed.
2887 * @param finger_table_index Index at which finger is stored.
2890 free_finger (struct FingerInfo *finger, unsigned int finger_table_index)
2892 struct Trail *trail;
2894 for (i = 0; i < finger->trails_count; i++)
2896 trail = &finger->trail_list[i];
2897 if (GNUNET_NO == trail->is_present)
2900 if (trail->trail_length > 0)
2902 trail->is_present = GNUNET_NO;
2905 finger->is_present = GNUNET_NO;
2906 memset ((void *)&finger_table[finger_table_index], 0, sizeof (finger_table[finger_table_index]));
2911 * Add a new entry in finger table at finger_table_index.
2912 * In case I am my own finger, then we don't have a trail. In case of a friend,
2913 * we have a trail with unique id and '0' trail length.
2914 * In case a finger is a friend, then increment the trails count of the friend.
2915 * @param finger_identity Peer Identity of new finger
2916 * @param finger_trail Trail to reach from me to finger (excluding both end points).
2917 * @param finger_trail_length Total number of peers in @a finger_trail.
2918 * @param trail_id Unique identifier of the trail.
2919 * @param finger_table_index Index in finger table.
2922 add_new_finger (struct GNUNET_PeerIdentity finger_identity,
2923 const struct GNUNET_PeerIdentity *finger_trail,
2924 unsigned int finger_trail_length,
2925 struct GNUNET_HashCode trail_id,
2926 unsigned int finger_table_index)
2928 struct FingerInfo *new_entry;
2929 struct FriendInfo *first_trail_hop;
2930 struct Trail *trail;
2933 new_entry = GNUNET_new (struct FingerInfo);
2934 new_entry->finger_identity = finger_identity;
2935 new_entry->finger_table_index = finger_table_index;
2936 new_entry->is_present = GNUNET_YES;
2938 /* If the new entry is my own identity. */
2939 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2942 new_entry->trails_count = 0;
2943 finger_table[finger_table_index] = *new_entry;
2944 GNUNET_free (new_entry);
2948 /* If finger is a friend, then we don't actually have a trail.
2949 * Just a trail id */
2950 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2953 new_entry->trail_list[0].trail_id = trail_id;
2954 new_entry->trails_count = 1;
2955 new_entry->trail_list[0].is_present = GNUNET_YES;
2956 new_entry->trail_list[0].trail_length = 0;
2957 new_entry->trail_list[0].trail_head = NULL;
2958 new_entry->trail_list[0].trail_tail = NULL;
2959 finger_table[finger_table_index] = *new_entry;
2960 GNUNET_assert (NULL !=
2962 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2963 &finger_identity)));
2965 first_trail_hop->trails_count++;
2966 GNUNET_free (new_entry);
2970 GNUNET_assert (NULL !=
2972 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2973 &finger_trail[0])));
2974 new_entry->trails_count = 1;
2975 first_trail_hop->trails_count++;
2976 GNUNET_assert (finger_trail_length != 0);
2977 /* Copy the finger trail into trail. */
2978 trail = GNUNET_new (struct Trail);
2979 for(i = 0; i < finger_trail_length; i++)
2981 struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2983 element->next = NULL;
2984 element->prev = NULL;
2985 element->peer = finger_trail[i];
2986 GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
2992 /* Add trail to trail list. */
2993 new_entry->trail_list[0].trail_head = trail->trail_head;
2994 new_entry->trail_list[0].trail_tail = trail->trail_tail;
2995 new_entry->trail_list[0].trail_length = finger_trail_length;
2996 new_entry->trail_list[0].trail_id = trail_id;
2997 new_entry->trail_list[0].is_present = GNUNET_YES;
2998 finger_table[finger_table_index] = *new_entry;
2999 GNUNET_free (new_entry);
3005 * Periodic task to verify current successor. There can be multiple trails to reach
3006 * to successor, choose the shortest one and send verify successor message
3007 * across that trail.
3008 * @param cls closure for this task
3009 * @param tc the context under which the task is running
3012 send_verify_successor_message (void *cls,
3013 const struct GNUNET_SCHEDULER_TaskContext *tc)
3015 struct FriendInfo *target_friend;
3016 struct GNUNET_HashCode trail_id;
3018 struct Trail *trail;
3019 struct Trail_Element *element;
3020 unsigned int trail_length;
3022 struct FingerInfo *successor;
3024 /* Schedule another send_find_finger_trail_message task.
3025 verify_successor_next_send_time.rel_value_us =
3026 verify_successor_next_send_time.rel_value_us +
3027 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3028 DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us);*/
3029 send_verify_successor_task =
3030 GNUNET_SCHEDULER_add_delayed (verify_successor_next_send_time,
3031 &send_verify_successor_message,
3034 successor = &finger_table[0];
3035 for (i = 0; i < successor->trails_count; i++)
3037 trail = &successor->trail_list[i];
3038 if(GNUNET_YES == trail->is_present)
3042 /* No valid trail found to reach to successor. */
3043 if (i == successor->trails_count)
3046 GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
3047 &successor->finger_identity));
3049 /* Trail stored at this index. */
3050 GNUNET_assert (GNUNET_YES == trail->is_present);
3052 trail_id = trail->trail_id;
3053 if (NULL == GDS_ROUTING_get_next_hop(trail_id,GDS_ROUTING_SRC_TO_DEST))
3055 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
3056 GNUNET_i2s(&my_identity), GNUNET_h2s(&trail->trail_id), __LINE__);
3060 trail_length = trail->trail_length;
3062 if (trail_length > 0)
3064 /* Copy the trail into peer list. */
3065 struct GNUNET_PeerIdentity peer_list[trail_length];
3067 element = trail->trail_head;
3068 for(j = 0; j < trail_length; j++)
3070 peer_list[j] = element->peer;
3071 element = element->next;
3074 GNUNET_assert (NULL != (target_friend =
3075 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3077 GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
3078 successor->finger_identity,
3079 trail_id, peer_list, trail_length,
3085 GNUNET_assert (NULL != (target_friend =
3086 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3087 &successor->finger_identity)));
3088 GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
3089 successor->finger_identity,
3098 * FIXME: should this be a periodic task, incrementing the search finger index?
3099 * Update the current search finger index.
3101 * FIXME document parameters!
3104 update_current_search_finger_index (struct GNUNET_PeerIdentity finger_identity,
3105 unsigned int finger_table_index)
3107 struct FingerInfo *successor;
3109 /* FIXME correct this: only move current index periodically */
3110 if (finger_table_index != current_search_finger_index)
3113 successor = &finger_table[0];
3114 GNUNET_assert (GNUNET_YES == successor->is_present);
3116 /* We were looking for immediate successor. */
3117 if (0 == current_search_finger_index)
3119 /* Start looking for immediate predecessor. */
3120 current_search_finger_index = PREDECESSOR_FINGER_ID;
3122 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
3124 if (GNUNET_SCHEDULER_NO_TASK == send_verify_successor_task)
3126 send_verify_successor_task =
3127 GNUNET_SCHEDULER_add_now (&send_verify_successor_message, NULL);
3133 current_search_finger_index = current_search_finger_index - 1;
3139 * Get the least significant bit set in val.
3142 * @return Position of first bit set, 65 in case of error.
3145 find_set_bit (uint64_t val)
3165 return 65; /* Some other bit was set to 1 as well. */
3172 * Calculate finger_table_index from initial 64 bit finger identity value that
3173 * we send in trail setup message.
3174 * @param ultimate_destination_finger_value Value that we calculated from our
3175 * identity and finger_table_index.
3176 * @param is_predecessor Is the entry for predecessor or not?
3177 * @return finger_table_index Value between 0 <= finger_table_index <= 64
3178 * finger_table_index > PREDECESSOR_FINGER_ID, if error occurs.
3181 get_finger_table_index (uint64_t ultimate_destination_finger_value,
3182 unsigned int is_predecessor)
3186 unsigned int finger_table_index;
3188 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
3189 my_id64 = GNUNET_ntohll (my_id64);
3191 /* Is this a predecessor finger? */
3192 if (1 == is_predecessor)
3194 diff = my_id64 - ultimate_destination_finger_value;
3196 finger_table_index = PREDECESSOR_FINGER_ID;
3198 finger_table_index = PREDECESSOR_FINGER_ID + 1; //error value
3203 diff = ultimate_destination_finger_value - my_id64;
3204 finger_table_index = find_set_bit (diff);
3206 return finger_table_index;
3211 * Remove finger and its associated data structures from finger table.
3212 * @param existing_finger Finger to be removed which is in finger table.
3213 * @param finger_table_index Index in finger table where @a existing_finger
3217 remove_existing_finger (struct FingerInfo *existing_finger,
3218 unsigned int finger_table_index)
3220 GNUNET_assert (GNUNET_YES == existing_finger->is_present);
3222 /* If I am my own finger, then we have no trails. */
3223 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&existing_finger->finger_identity,
3226 existing_finger->is_present = GNUNET_NO;
3227 memset ((void *)&finger_table[finger_table_index], 0,
3228 sizeof (finger_table[finger_table_index]));
3232 /* For all other fingers, send trail teardown across all the trails to reach
3233 finger, and free the finger. */
3234 send_all_finger_trails_teardown (existing_finger);
3235 free_finger (existing_finger, finger_table_index);
3241 * Check if there is already an entry in finger_table at finger_table_index.
3242 * We get the finger_table_index from 64bit finger value we got from the network.
3243 * -- If yes, then select the closest finger.
3244 * -- If new and existing finger are same, then check if you can store more
3246 * -- If yes then add trail, else keep the best trails to reach to the
3248 * -- If the new finger is closest, remove the existing entry, send trail
3249 * teardown message across all the trails to reach the existing entry.
3250 * Add the new finger.
3251 * -- If new and existing finger are different, and existing finger is closest
3253 * -- Update current_search_finger_index.
3254 * @param finger_identity Peer Identity of new finger
3255 * @param finger_trail Trail to reach the new finger
3256 * @param finger_trail_length Total number of peers in @a new_finger_trail.
3257 * @param is_predecessor Is this entry for predecessor in finger_table?
3258 * @param finger_value 64 bit value of finger identity that we got from network.
3259 * @param finger_trail_id Unique identifier of @finger_trail.
3262 finger_table_add (struct GNUNET_PeerIdentity finger_identity,
3263 const struct GNUNET_PeerIdentity *finger_trail,
3264 unsigned int finger_trail_length,
3265 unsigned int is_predecessor,
3266 uint64_t finger_value,
3267 struct GNUNET_HashCode finger_trail_id)
3269 struct FingerInfo *existing_finger;
3270 struct GNUNET_PeerIdentity closest_peer;
3271 struct FingerInfo *successor;
3272 unsigned int finger_table_index;
3274 /* Get the finger_table_index corresponding to finger_value we got from network.*/
3275 finger_table_index = get_finger_table_index (finger_value, is_predecessor);
3277 /* Invalid finger_table_index. */
3278 if ((finger_table_index > PREDECESSOR_FINGER_ID))
3280 GNUNET_break_op (0);
3284 /* New entry same as successor. */
3285 if ((0 != finger_table_index) &&
3286 (PREDECESSOR_FINGER_ID != finger_table_index))
3288 successor = &finger_table[0];
3289 if (GNUNET_NO == successor->is_present)
3294 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
3295 &successor->finger_identity))
3297 find_finger_trail_task_next_send_time =
3298 GNUNET_TIME_STD_BACKOFF(find_finger_trail_task_next_send_time);
3299 current_search_finger_index = 0;
3303 struct FingerInfo prev_finger;
3304 prev_finger = finger_table[finger_table_index - 1];
3305 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
3306 &prev_finger.finger_identity))
3308 current_search_finger_index--;
3313 existing_finger = &finger_table[finger_table_index];
3315 /* No entry present in finger_table for given finger map index. */
3316 if (GNUNET_NO == existing_finger->is_present)
3318 add_new_finger (finger_identity, finger_trail,
3319 finger_trail_length,
3320 finger_trail_id, finger_table_index);
3321 update_current_search_finger_index (finger_identity,
3322 finger_table_index);
3327 /* If existing entry and finger identity are not same. */
3328 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
3331 closest_peer = select_closest_peer (&existing_finger->finger_identity,
3336 /* If the new finger is the closest peer. */
3337 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, &closest_peer))
3339 remove_existing_finger (existing_finger, finger_table_index);
3340 add_new_finger (finger_identity, finger_trail, finger_trail_length,
3341 finger_trail_id, finger_table_index);
3345 /* Existing finger is the closest one. We need to send trail teardown
3346 across the trail setup in routing table of all the peers. */
3347 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, &my_identity))
3349 if (finger_trail_length > 0)
3350 GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id,
3351 GDS_ROUTING_SRC_TO_DEST,
3354 GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id,
3355 GDS_ROUTING_SRC_TO_DEST,
3362 /* If both new and existing entry are same as my_identity, then do nothing. */
3363 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
3368 /* If the existing finger is not a friend. */
3370 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3371 &existing_finger->finger_identity))
3373 /* If there is space to store more trails. */
3374 if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
3375 add_new_trail (existing_finger, finger_trail,
3376 finger_trail_length, finger_trail_id);
3378 select_and_replace_trail (existing_finger, finger_trail,
3379 finger_trail_length, finger_trail_id);
3383 update_current_search_finger_index (finger_identity, finger_table_index);
3389 * FIXME: Check for loop in the request. If you already are part of put path,
3390 * then you need to reset the put path length.
3391 * Core handler for P2P put messages.
3392 * @param cls closure
3393 * @param peer sender of the request
3394 * @param message message
3395 * @return #GNUNET_OK to keep the connection open,
3396 * #GNUNET_SYSERR to close it (signal serious error)
3399 handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
3400 const struct GNUNET_MessageHeader *message)
3402 struct PeerPutMessage *put;
3403 struct GNUNET_PeerIdentity *put_path;
3404 struct GNUNET_PeerIdentity best_known_dest;
3405 struct GNUNET_HashCode intermediate_trail_id;
3406 struct GNUNET_PeerIdentity *next_hop;
3407 enum GNUNET_DHT_RouteOption options;
3408 struct GNUNET_HashCode test_key;
3412 size_t payload_size;
3415 #if ENABLE_MALICIOUS
3416 if(1 == act_malicious)
3418 DEBUG("I am malicious,dropping put request. \n");
3423 msize = ntohs (message->size);
3424 if (msize < sizeof (struct PeerPutMessage))
3426 GNUNET_break_op (0);
3430 put = (struct PeerPutMessage *) message;
3431 putlen = ntohl (put->put_path_length);
3433 sizeof (struct PeerPutMessage) +
3434 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3436 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3438 GNUNET_break_op (0);
3441 DEBUG("GET FOR DATA_SIZE = %lu\n",msize);
3442 GNUNET_STATISTICS_update (GDS_stats,
3444 ("# Bytes received from other peers"), (int64_t) msize,
3447 best_known_dest = put->best_known_destination;
3448 put_path = (struct GNUNET_PeerIdentity *) &put[1];
3449 payload = &put_path[putlen];
3450 options = ntohl (put->options);
3451 intermediate_trail_id = put->intermediate_trail_id;
3452 payload_size = msize - (sizeof (struct PeerPutMessage) +
3453 putlen * sizeof (struct GNUNET_PeerIdentity));
3455 switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
3456 payload, payload_size, &test_key))
3459 if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
3461 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
3462 GNUNET_break_op (0);
3463 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3464 "PUT with key `%s' for block with key %s\n",
3465 put_s, GNUNET_h2s_full (&test_key));
3466 GNUNET_free (put_s);
3471 GNUNET_break_op (0);
3474 /* cannot verify, good luck */
3478 if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
3480 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
3481 ntohl (put->block_type),
3483 NULL, 0, /* bloom filer */
3484 NULL, 0, /* xquery */
3485 payload, payload_size))
3487 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3488 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3491 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3492 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3493 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
3494 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3495 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3496 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3498 GNUNET_break_op (0);
3503 /* Check if you are already a part of put path. */
3505 for (i = 0; i < putlen; i++)
3507 if(0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &put_path[i]))
3514 /* Add yourself to the list. */
3515 struct GNUNET_PeerIdentity pp[putlen + 1];
3516 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
3518 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
3519 pp[putlen] = my_identity;
3525 memcpy (&key_value, &(put->key), sizeof (uint64_t));
3526 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3528 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3529 GDS_ROUTING_SRC_TO_DEST);
3530 if (NULL == next_hop)
3532 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
3533 GNUNET_i2s(&my_identity), GNUNET_h2s(&intermediate_trail_id), __LINE__);
3534 GNUNET_STATISTICS_update (GDS_stats,
3535 gettext_noop ("# Next hop to forward the packet not found "
3536 "trail setup request, packet dropped."),
3539 GNUNET_break_op (0);
3540 //FIXME: Adding put here,only to ensure that process does not hang. but
3541 // should not be here. fix the logic.
3542 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
3543 &(put->key),putlen, pp, ntohl (put->block_type),
3544 payload_size, payload);
3549 GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
3555 struct Closest_Peer successor;
3556 key_value = GNUNET_ntohll (key_value);
3557 successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
3558 next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
3559 *next_hop = successor.next_hop;
3560 intermediate_trail_id = successor.trail_id;
3561 best_known_dest = successor.best_known_destination;
3566 GDS_CLIENTS_process_put (options,
3567 ntohl (put->block_type),
3568 ntohl (put->hop_count),
3569 ntohl (put->desired_replication_level),
3571 GNUNET_TIME_absolute_ntoh (put->expiration_time),
3576 /* I am the final destination */
3577 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &best_known_dest))
3579 DEBUG("PUT destination is me = %s,KEY = %s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(&(put->key)));
3580 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
3581 &(put->key),putlen, pp, ntohl (put->block_type),
3582 payload_size, payload);
3586 GDS_NEIGHBOURS_send_put (&put->key,
3587 ntohl (put->block_type),ntohl (put->options),
3588 ntohl (put->desired_replication_level),
3589 best_known_dest, intermediate_trail_id, next_hop,
3590 ntohl (put->hop_count), putlen, pp,
3591 GNUNET_TIME_absolute_ntoh (put->expiration_time),
3592 payload, payload_size);
3599 * FIXME: Check for loop in the request. If you already are part of get path,
3600 * then you need to reset the get path length.
3601 * Core handler for p2p get requests.
3603 * @param cls closure
3604 * @param peer sender of the request
3605 * @param message message
3606 * @return #GNUNET_OK to keep the connection open,
3607 * #GNUNET_SYSERR to close it (signal serious error)
3610 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
3611 const struct GNUNET_MessageHeader *message)
3613 const struct PeerGetMessage *get;
3614 const struct GNUNET_PeerIdentity *get_path;
3615 struct GNUNET_PeerIdentity best_known_dest;
3616 struct GNUNET_HashCode intermediate_trail_id;
3617 struct GNUNET_PeerIdentity *next_hop;
3618 uint32_t get_length;
3622 #if ENABLE_MALICIOUS
3623 if(1 == act_malicious)
3625 DEBUG("I am malicious,dropping get request. \n");
3630 msize = ntohs (message->size);
3631 if (msize < sizeof (struct PeerGetMessage))
3633 GNUNET_break_op (0);
3637 get = (const struct PeerGetMessage *)message;
3638 get_length = ntohl (get->get_path_length);
3639 best_known_dest = get->best_known_destination;
3640 intermediate_trail_id = get->intermediate_trail_id;
3641 get_path = (const struct GNUNET_PeerIdentity *)&get[1];
3644 sizeof (struct PeerGetMessage) +
3645 get_length * sizeof (struct GNUNET_PeerIdentity)) ||
3647 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3649 GNUNET_break_op (0);
3652 DEBUG("PUT FOR DATA_SIZE = %lu\n",msize);
3653 GNUNET_STATISTICS_update (GDS_stats,
3655 ("# Bytes received from other peers"), msize,
3658 memcpy (&key_value, &(get->key), sizeof (uint64_t));
3659 key_value = GNUNET_ntohll (key_value);
3661 /* Check if you are already a part of get path. */
3663 for (i = 0; i < get_length; i++)
3665 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &get_path[i]))
3672 /* Add yourself in the get path. */
3673 struct GNUNET_PeerIdentity gp[get_length + 1];
3674 memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
3675 gp[get_length] = my_identity;
3676 get_length = get_length + 1;
3677 GDS_CLIENTS_process_get (get->options, get->block_type,get->hop_count,
3678 get->desired_replication_level, get->get_path_length,
3681 /* I am not the final destination. I am part of trail to reach final dest. */
3682 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3684 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3685 GDS_ROUTING_SRC_TO_DEST);
3686 if (NULL == next_hop)
3688 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
3689 GNUNET_i2s(&my_identity), GNUNET_h2s(&intermediate_trail_id), __LINE__);
3690 GNUNET_STATISTICS_update (GDS_stats,
3691 gettext_noop ("# Next hop to forward the packet not found "
3692 "GET request, packet dropped."),
3695 /* We are not able to proceed further*/
3696 GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
3697 get_length, gp, &gp[get_length - 2],
3704 struct Closest_Peer successor;
3706 successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
3707 next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
3708 *next_hop = successor.next_hop;
3709 best_known_dest = successor.best_known_destination;
3710 intermediate_trail_id = successor.trail_id;
3713 /* I am the final destination. */
3714 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &best_known_dest))
3716 DEBUG("GET destination is me = %s,KEY = %s,get_length = %d\n",
3717 GNUNET_i2s(&my_identity),GNUNET_h2s(&(get->key)),get_length);
3718 if (1 == get_length)
3720 GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0,
3721 NULL, 0, 1, &my_identity, NULL,&my_identity);
3725 GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
3726 get_length, gp, &gp[get_length - 2],
3732 GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
3733 get->desired_replication_level, best_known_dest,
3734 intermediate_trail_id, next_hop, 0,
3742 * Core handler for get result
3743 * @param cls closure
3744 * @param peer sender of the request
3745 * @param message message
3746 * @return #GNUNET_OK to keep the connection open,
3747 * #GNUNET_SYSERR to close it (signal serious error)
3750 handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
3751 const struct GNUNET_MessageHeader *message)
3753 const struct PeerGetResultMessage *get_result;
3754 const struct GNUNET_PeerIdentity *get_path;
3755 const struct GNUNET_PeerIdentity *put_path;
3756 const void *payload;
3757 size_t payload_size;
3759 unsigned int getlen;
3760 unsigned int putlen;
3761 int current_path_index;
3763 msize = ntohs (message->size);
3764 if (msize < sizeof (struct PeerGetResultMessage))
3766 GNUNET_break_op (0);
3770 get_result = (const struct PeerGetResultMessage *)message;
3771 getlen = ntohl (get_result->get_path_length);
3772 putlen = ntohl (get_result->put_path_length);
3775 sizeof (struct PeerGetResultMessage) +
3776 getlen * sizeof (struct GNUNET_PeerIdentity) +
3777 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3779 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity) ||
3781 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))))
3783 GNUNET_break_op (0);
3786 DEBUG("GET_RESULT FOR DATA_SIZE = %lu\n",msize);
3787 GNUNET_STATISTICS_update (GDS_stats,
3789 ("# Bytes received from other peers"), msize,
3792 put_path = (const struct GNUNET_PeerIdentity *) &get_result[1];
3793 get_path = &put_path[putlen];
3794 payload = (const void *) &get_path[getlen];
3795 payload_size = msize - (sizeof (struct PeerGetResultMessage) +
3796 (getlen + putlen) * sizeof (struct GNUNET_PeerIdentity));
3798 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
3800 GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
3801 getlen, get_path, putlen,
3802 put_path, get_result->type, payload_size, payload);
3807 current_path_index = search_my_index (get_path, getlen);
3808 if (-1 == current_path_index )
3810 DEBUG ("No entry found in get path.\n");
3812 return GNUNET_SYSERR;
3814 if((getlen + 1) == current_path_index)
3816 DEBUG("Present twice in get path. Not allowed. \n");
3818 return GNUNET_SYSERR;
3820 GDS_NEIGHBOURS_send_get_result (&(get_result->key), get_result->type,
3821 &get_path[current_path_index - 1],
3822 &(get_result->querying_peer), putlen, put_path,
3823 getlen, get_path, get_result->expiration_time,
3824 payload, payload_size);
3827 return GNUNET_SYSERR;
3832 * Find the next hop to pass trail setup message. First find the local best known
3833 * hop from your own identity, friends and finger. If you were part of trail,
3834 * then get the next hop from routing table. Compare next_hop from routing table
3835 * and local best known hop, and return the closest one to final_dest_finger_val
3836 * @param final_dest_finger_val 64 bit value of finger identity
3837 * @param intermediate_trail_id If you are part of trail to reach to some other
3838 * finger, then it is the trail id to reach to
3839 * that finger, else set to 0.
3840 * @param is_predecessor Are we looking for closest successor or predecessor.
3841 * @param current_dest In case you are part of trail, then finger to which
3842 * we should forward the message. Else my own identity
3843 * @return Closest Peer for @a final_dest_finger_val
3845 static struct Closest_Peer
3846 get_local_best_known_next_hop (uint64_t final_dest_finger_val,
3847 struct GNUNET_HashCode intermediate_trail_id,
3848 unsigned int is_predecessor,
3849 struct GNUNET_PeerIdentity prev_hop,
3850 struct GNUNET_PeerIdentity source,
3851 struct GNUNET_PeerIdentity *current_dest)
3853 struct Closest_Peer peer;
3855 /* Find a local best known peer. */
3856 peer = find_successor (final_dest_finger_val, is_predecessor);//FIXME: chnage to better name
3858 /* Am I just a part of a trail towards a finger (current_destination)? */
3859 /* Select best successor among one found locally and current_destination
3860 * that we got from network.*/
3861 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest) &&
3862 0 != GNUNET_CRYPTO_cmp_peer_identity (&peer.best_known_destination,
3865 struct GNUNET_PeerIdentity closest_peer;
3867 closest_peer = select_closest_peer (&peer.best_known_destination,
3869 final_dest_finger_val,
3872 /* Is current dest (end point of the trail of which I am a part) closest_peer? */
3873 if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, &closest_peer))
3875 struct GNUNET_PeerIdentity *next_hop;
3877 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3878 GDS_ROUTING_SRC_TO_DEST);
3879 /* It may happen that trail teardown message got delayed and hence,
3880 the previous hop sent the message over intermediate trail id.In that
3881 case next_hop could be NULL. */
3882 if(NULL != next_hop)
3884 peer.next_hop = *next_hop;
3885 peer.best_known_destination = *current_dest;
3886 peer.trail_id = intermediate_trail_id;
3895 * Core handle for PeerTrailSetupMessage.
3896 * @param cls closure
3897 * @param message message
3898 * @param peer peer identity this notification is about
3899 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
3902 handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
3903 const struct GNUNET_MessageHeader *message)
3905 const struct PeerTrailSetupMessage *trail_setup;
3906 const struct GNUNET_PeerIdentity *trail_peer_list;
3907 struct GNUNET_PeerIdentity current_dest;
3908 struct FriendInfo *target_friend;
3909 struct GNUNET_PeerIdentity source;
3910 uint64_t final_dest_finger_val;
3911 struct GNUNET_HashCode intermediate_trail_id;
3912 struct GNUNET_HashCode trail_id;
3913 unsigned int is_predecessor;
3914 uint32_t trail_length;
3918 msize = ntohs (message->size);
3919 if (msize < sizeof (struct PeerTrailSetupMessage))
3921 GNUNET_break_op (0);
3922 return GNUNET_SYSERR;
3925 trail_setup = (const struct PeerTrailSetupMessage *) message;
3926 if ((msize - sizeof (struct PeerTrailSetupMessage)) %
3927 sizeof (struct GNUNET_PeerIdentity) != 0)
3929 GNUNET_break_op (0);
3932 trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
3933 sizeof (struct GNUNET_PeerIdentity);
3935 GNUNET_STATISTICS_update (GDS_stats,
3937 ("# Bytes received from other peers"), msize,
3940 trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_setup[1];
3941 current_dest = trail_setup->best_known_destination;
3942 trail_id = trail_setup->trail_id;
3943 final_dest_finger_val =
3944 GNUNET_ntohll (trail_setup->final_destination_finger_value);
3945 source = trail_setup->source_peer;
3946 is_predecessor = ntohl (trail_setup->is_predecessor);
3947 intermediate_trail_id = trail_setup->intermediate_trail_id;
3949 /* Did the friend insert its ID in the trail list? */
3950 if (trail_length > 0 &&
3951 0 != memcmp (&trail_peer_list[trail_length-1], peer, sizeof (struct GNUNET_PeerIdentity)))
3953 GNUNET_break_op (0);
3954 return GNUNET_SYSERR;
3957 /* If I was the source and got the message back, then set trail length to 0.*/
3958 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
3963 /* Check if you are friend of source. */
3964 if (trail_length >= 1)
3966 if(NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &source))
3968 /* If I am a friend, then I can be the first contact, so make
3969 trail length 0. We are going to add ourself later in the code.*/
3974 /* Check if you are present in the trail seen so far? */
3975 for (i = 0; i < trail_length ; i++)
3977 if(0 == GNUNET_CRYPTO_cmp_peer_identity(&trail_peer_list[i],&my_identity))
3979 trail_length = i; /* Check that you add yourself again */
3984 /* Is my routing table full? */
3985 if (GNUNET_YES == GDS_ROUTING_threshold_reached())
3987 GNUNET_assert (NULL !=
3989 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
3990 GDS_NEIGHBOURS_send_trail_rejection (source, final_dest_finger_val,
3991 my_identity, is_predecessor,
3992 trail_peer_list, trail_length,
3993 trail_id, target_friend,
3994 CONGESTION_TIMEOUT);
3998 /* Get the next hop to forward the trail setup request. */
3999 struct Closest_Peer next_peer =
4000 get_local_best_known_next_hop (final_dest_finger_val,
4001 intermediate_trail_id,
4007 /* Am I the final destination? */
4008 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&next_peer.best_known_destination,
4011 if(0 == GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
4013 finger_table_add (my_identity, NULL, 0, is_predecessor,
4014 final_dest_finger_val, trail_id);
4018 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4024 if (trail_length > 0)
4026 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4027 &trail_peer_list[trail_length-1]);
4030 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &source);
4032 if (NULL == target_friend)
4034 GNUNET_break_op (0);
4035 return GNUNET_SYSERR;
4037 GDS_ROUTING_add (trail_id, target_friend->id, my_identity);
4038 GDS_NEIGHBOURS_send_trail_setup_result (source,
4040 target_friend, trail_length,
4043 final_dest_finger_val,trail_id);
4045 else /* I'm not the final destination. */
4047 GNUNET_assert (NULL !=
4049 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4050 &next_peer.next_hop)));
4052 if (0 != GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
4054 /* Add yourself to list of peers. */
4055 struct GNUNET_PeerIdentity peer_list[trail_length + 1];
4057 memcpy (peer_list, trail_peer_list,
4058 trail_length * sizeof (struct GNUNET_PeerIdentity));
4059 peer_list[trail_length] = my_identity;
4060 GDS_NEIGHBOURS_send_trail_setup (source,
4061 final_dest_finger_val,
4062 next_peer.best_known_destination,
4063 target_friend, trail_length + 1, peer_list,
4064 is_predecessor, trail_id,
4065 next_peer.trail_id);
4068 GDS_NEIGHBOURS_send_trail_setup (source,
4069 final_dest_finger_val,
4070 next_peer.best_known_destination,
4071 target_friend, 0, NULL,
4072 is_predecessor, trail_id,
4073 next_peer.trail_id);
4080 * Core handle for p2p trail setup result messages.
4082 * @param message message
4083 * @param peer sender of this message.
4084 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4087 handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
4088 const struct GNUNET_MessageHeader *message)
4090 const struct PeerTrailSetupResultMessage *trail_result;
4091 const struct GNUNET_PeerIdentity *trail_peer_list;
4092 struct GNUNET_PeerIdentity next_hop;
4093 struct FriendInfo *target_friend;
4094 struct GNUNET_PeerIdentity querying_peer;
4095 struct GNUNET_PeerIdentity finger_identity;
4096 uint32_t trail_length;
4097 uint64_t ulitmate_destination_finger_value;
4098 uint32_t is_predecessor;
4099 struct GNUNET_HashCode trail_id;
4103 msize = ntohs (message->size);
4104 if (msize < sizeof (struct PeerTrailSetupResultMessage))
4106 GNUNET_break_op (0);
4110 trail_result = (const struct PeerTrailSetupResultMessage *) message;
4111 if ((msize - sizeof (struct PeerTrailSetupResultMessage)) %
4112 sizeof (struct GNUNET_PeerIdentity) != 0)
4114 GNUNET_break_op (0);
4117 trail_length = (msize - sizeof (struct PeerTrailSetupResultMessage))/
4118 sizeof (struct GNUNET_PeerIdentity);
4120 GNUNET_STATISTICS_update (GDS_stats,
4122 ("# Bytes received from other peers"), msize,
4125 is_predecessor = ntohl (trail_result->is_predecessor);
4126 querying_peer = trail_result->querying_peer;
4127 finger_identity = trail_result->finger_identity;
4128 trail_id = trail_result->trail_id;
4129 trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
4130 ulitmate_destination_finger_value =
4131 GNUNET_ntohll (trail_result->ulitmate_destination_finger_value);
4133 /* Am I the one who initiated the query? */
4134 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
4136 /* Check that you got the message from the correct peer. */
4137 if (trail_length > 0)
4139 GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[0],
4144 GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
4147 GDS_ROUTING_add (trail_id, my_identity, *peer);
4148 finger_table_add (finger_identity, trail_peer_list, trail_length,
4149 is_predecessor, ulitmate_destination_finger_value, trail_id);
4153 /* Get my location in the trail. */
4154 my_index = search_my_index (trail_peer_list, trail_length);
4157 DEBUG ("Not found in trail\n");
4159 return GNUNET_SYSERR;
4162 if ((trail_length + 1) == my_index)
4164 DEBUG ("Found twice in trail.\n");
4166 return GNUNET_SYSERR;
4169 //TODO; Refactor code here and above to check if sender peer is correct
4172 if(trail_length > 1)
4173 GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[1],
4176 GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
4178 next_hop = trail_result->querying_peer;
4182 if(my_index == trail_length - 1)
4185 GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
4190 GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[my_index + 1],
4192 next_hop = trail_peer_list[my_index - 1];
4195 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
4196 if (NULL == target_friend)
4198 GNUNET_break_op (0);
4199 return GNUNET_SYSERR;
4201 GDS_ROUTING_add (trail_id, next_hop, *peer);
4202 GDS_NEIGHBOURS_send_trail_setup_result (querying_peer, finger_identity,
4203 target_friend, trail_length, trail_peer_list,
4205 ulitmate_destination_finger_value,
4213 * @param trail Trail to be inverted
4214 * @param trail_length Total number of peers in the trail.
4215 * @return Updated trail
4217 static struct GNUNET_PeerIdentity *
4218 invert_trail (const struct GNUNET_PeerIdentity *trail,
4219 unsigned int trail_length)
4223 struct GNUNET_PeerIdentity *inverted_trail;
4225 inverted_trail = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
4228 j = trail_length - 1;
4229 while (i < trail_length)
4231 inverted_trail[i] = trail[j];
4236 GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4237 &inverted_trail[0]));
4238 return inverted_trail;
4243 * Return the shortest trail among all the trails to reach to finger from me.
4244 * @param finger Finger
4245 * @param shortest_trail_length[out] Trail length of shortest trail from me
4247 * @return Shortest trail.
4249 static struct GNUNET_PeerIdentity *
4250 get_shortest_trail (struct FingerInfo *finger,
4251 unsigned int *trail_length)
4253 struct Trail *trail;
4254 unsigned int flag = 0;
4255 unsigned int shortest_trail_index = 0;
4256 int shortest_trail_length = -1;
4257 struct Trail_Element *trail_element;
4258 struct GNUNET_PeerIdentity *trail_list;
4261 trail = GNUNET_new (struct Trail);
4263 /* Get the shortest trail to reach to current successor. */
4264 for (i = 0; i < finger->trails_count; i++)
4266 trail = &finger->trail_list[i];
4270 shortest_trail_index = i;
4271 shortest_trail_length = trail->trail_length;
4276 if (shortest_trail_length > trail->trail_length)
4278 shortest_trail_index = i;
4279 shortest_trail_length = trail->trail_length;
4284 /* Copy the shortest trail and return. */
4285 trail = &finger->trail_list[shortest_trail_index];
4286 trail_element = trail->trail_head;
4288 trail_list = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
4289 shortest_trail_length);
4291 for(i = 0; i < shortest_trail_length; i++,trail_element = trail_element->next)
4293 trail_list[i] = trail_element->peer;
4296 GNUNET_assert(shortest_trail_length != -1);
4298 *trail_length = shortest_trail_length;
4304 * Check if trail_1 and trail_2 have any common element. If yes then join
4305 * them at common element. trail_1 always preceeds trail_2 in joined trail.
4306 * @param trail_1 Trail from source to me, NOT including endpoints.
4307 * @param trail_1_len Total number of peers @a trail_1
4308 * @param trail_2 Trail from me to current predecessor, NOT including endpoints.
4309 * @param trail_2_len Total number of peers @a trail_2
4310 * @param joined_trail_len Total number of peers in combined trail of trail_1
4312 * @return Joined trail.
4314 static struct GNUNET_PeerIdentity *
4315 check_for_duplicate_entries (const struct GNUNET_PeerIdentity *trail_1,
4316 unsigned int trail_1_len,
4317 struct GNUNET_PeerIdentity *trail_2,
4318 unsigned int trail_2_len,
4319 unsigned int *joined_trail_len)
4321 struct GNUNET_PeerIdentity *joined_trail;
4326 for (i = 0; i < trail_1_len; i++)
4328 for (j = 0; j < trail_2_len; j++)
4330 if(0 != GNUNET_CRYPTO_cmp_peer_identity (&trail_1[i],&trail_2[j]))
4333 *joined_trail_len = i + (trail_2_len - j);
4334 joined_trail = GNUNET_malloc (*joined_trail_len *
4335 sizeof(struct GNUNET_PeerIdentity));
4338 /* Copy all the elements from 0 to i into joined_trail. */
4339 for(k = 0; k < ( i+1); k++)
4341 joined_trail[k] = trail_1[k];
4344 /* Increment j as entry stored is same as entry stored at i*/
4347 /* Copy all the elements from j to trail_2_len-1 to joined trail.*/
4348 while(k <= (*joined_trail_len - 1))
4350 joined_trail[k] = trail_2[j];
4355 return joined_trail;
4359 /* Here you should join the trails. */
4360 *joined_trail_len = trail_1_len + trail_2_len + 1;
4361 joined_trail = GNUNET_malloc (*joined_trail_len *
4362 sizeof(struct GNUNET_PeerIdentity));
4365 for(i = 0; i < trail_1_len;i++)
4367 joined_trail[i] = trail_1[i];
4370 joined_trail[i] = my_identity;
4373 for (j = 0; i < *joined_trail_len; i++,j++)
4375 joined_trail[i] = trail_2[j];
4378 return joined_trail;
4383 * Return the trail from source to my current predecessor. Check if source
4384 * is already part of the this trail, if yes then return the shorten trail.
4385 * @param current_trail Trail from source to me, NOT including the endpoints.
4386 * @param current_trail_length Number of peers in @a current_trail.
4387 * @param trail_src_to_curr_pred_length[out] Number of peers in trail from
4388 * source to my predecessor, NOT including
4390 * @return Trail from source to my predecessor.
4392 static struct GNUNET_PeerIdentity *
4393 get_trail_src_to_curr_pred (struct GNUNET_PeerIdentity source_peer,
4394 const struct GNUNET_PeerIdentity *trail_src_to_me,
4395 unsigned int trail_src_to_me_len,
4396 unsigned int *trail_src_to_curr_pred_length)
4398 struct GNUNET_PeerIdentity *trail_me_to_curr_pred;
4399 struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
4400 unsigned int trail_me_to_curr_pred_length;
4401 struct FingerInfo *current_predecessor;
4405 current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
4407 /* Check if trail_src_to_me contains current_predecessor. */
4408 for (i = 0; i < trail_src_to_me_len; i++)
4410 if(0 != GNUNET_CRYPTO_cmp_peer_identity(&trail_src_to_me[i],
4411 ¤t_predecessor->finger_identity))
4415 *trail_src_to_curr_pred_length = i;
4420 trail_src_to_curr_pred = GNUNET_malloc (*trail_src_to_curr_pred_length *
4421 sizeof(struct GNUNET_PeerIdentity));
4422 for(j = 0; j < i;j++)
4423 trail_src_to_curr_pred[j] = trail_src_to_me[j];
4424 return trail_src_to_curr_pred;
4428 trail_me_to_curr_pred = get_shortest_trail (current_predecessor,
4429 &trail_me_to_curr_pred_length);
4431 /* Check if trail contains the source_peer. */
4432 for(i = trail_me_to_curr_pred_length - 1; i >= 0; i--)
4434 if(0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
4435 &trail_me_to_curr_pred[i]))
4438 /* Source is NOT part of trail. */
4441 /* Source is the last element in the trail to reach to my pred.
4442 Source is direct friend of the pred. */
4443 if (trail_me_to_curr_pred_length == i)
4445 *trail_src_to_curr_pred_length = 0;
4449 *trail_src_to_curr_pred_length = trail_me_to_curr_pred_length - i;
4450 trail_src_to_curr_pred = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)*
4451 *trail_src_to_curr_pred_length);
4453 for(j = 0; j < *trail_src_to_curr_pred_length; i++,j++)
4455 trail_src_to_curr_pred[j] = trail_me_to_curr_pred[i];
4457 GNUNET_free_non_null(trail_me_to_curr_pred);
4458 return trail_src_to_curr_pred;
4462 trail_src_to_curr_pred = check_for_duplicate_entries (trail_src_to_me,
4463 trail_src_to_me_len,
4464 trail_me_to_curr_pred,
4465 trail_me_to_curr_pred_length,
4467 *trail_src_to_curr_pred_length = len;
4468 GNUNET_free_non_null(trail_me_to_curr_pred);
4469 return trail_src_to_curr_pred;
4474 * Add finger as your predecessor. To add, first generate a new trail id, invert
4475 * the trail to get the trail from me to finger, add an entry in your routing
4476 * table, send add trail message to peers which are part of trail from me to
4477 * finger and add finger in finger table.
4480 * @param trail_length
4483 update_predecessor (struct GNUNET_PeerIdentity finger,
4484 struct GNUNET_PeerIdentity *trail,
4485 unsigned int trail_length)
4487 struct GNUNET_HashCode trail_to_new_predecessor_id;
4488 struct GNUNET_PeerIdentity *trail_to_new_predecessor;
4489 struct FriendInfo *target_friend;
4491 /* Generate trail id for trail from me to new predecessor = finger. */
4492 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
4493 &trail_to_new_predecessor_id,
4494 sizeof (trail_to_new_predecessor_id));
4496 /* Finger is a friend. */
4497 if (trail_length == 0)
4499 trail_to_new_predecessor = NULL;
4500 GDS_ROUTING_add (trail_to_new_predecessor_id, my_identity, finger);
4501 GNUNET_assert (NULL != (target_friend =
4502 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4507 /* Invert the trail to get the trail from me to finger, NOT including the
4509 GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4510 &trail[trail_length-1]));
4512 trail_to_new_predecessor = invert_trail (trail, trail_length);
4514 /* Add an entry in your routing table. */
4515 GDS_ROUTING_add (trail_to_new_predecessor_id,
4517 trail_to_new_predecessor[0]);
4519 GNUNET_assert (NULL != (target_friend =
4520 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4521 &trail_to_new_predecessor[0])));
4522 GNUNET_assert (NULL != (
4523 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4524 &trail[trail_length - 1])));
4527 /* Add entry in routing table of all peers that are part of trail from me
4528 to finger, including finger. */
4529 GDS_NEIGHBOURS_send_add_trail (my_identity,
4531 trail_to_new_predecessor_id,
4532 trail_to_new_predecessor,
4536 add_new_finger (finger, trail_to_new_predecessor, trail_length,
4537 trail_to_new_predecessor_id, PREDECESSOR_FINGER_ID);
4538 GNUNET_free_non_null(trail_to_new_predecessor);
4543 * Check if you already have a predecessor. If not then add finger as your
4544 * predecessor. If you have predecessor, then compare two peer identites.
4545 * If finger is correct predecessor, then remove the old entry, add finger in
4546 * finger table and send add_trail message to add the trail in the routing
4547 * table of all peers which are part of trail to reach from me to finger.
4548 * @param finger New peer which may be our predecessor.
4549 * @param trail List of peers to reach from @finger to me.
4550 * @param trail_length Total number of peer in @a trail.
4553 compare_and_update_predecessor (struct GNUNET_PeerIdentity finger,
4554 struct GNUNET_PeerIdentity *trail,
4555 unsigned int trail_length)
4557 struct FingerInfo *current_predecessor;
4558 struct GNUNET_PeerIdentity closest_peer;
4559 uint64_t predecessor_value;
4560 unsigned int is_predecessor = 1;
4562 current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
4563 GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger, &my_identity));
4565 /* No predecessor. Add finger as your predecessor. */
4566 if (GNUNET_NO == current_predecessor->is_present)
4568 update_predecessor (finger, trail, trail_length);
4572 if (0 == GNUNET_CRYPTO_cmp_peer_identity (¤t_predecessor->finger_identity,
4578 predecessor_value = compute_finger_identity_value (PREDECESSOR_FINGER_ID);
4579 closest_peer = select_closest_peer (&finger,
4580 ¤t_predecessor->finger_identity,
4581 predecessor_value, is_predecessor);
4583 /* Finger is the closest predecessor. Remove the existing one and add the new
4585 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&closest_peer, &finger))
4587 remove_existing_finger (current_predecessor, PREDECESSOR_FINGER_ID);
4588 update_predecessor (finger, trail, trail_length);
4596 * Core handle for p2p verify successor messages.
4597 * @param cls closure
4598 * @param message message
4599 * @param peer peer identity this notification is about
4600 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4603 handle_dht_p2p_verify_successor(void *cls,
4604 const struct GNUNET_PeerIdentity *peer,
4605 const struct GNUNET_MessageHeader *message)
4607 const struct PeerVerifySuccessorMessage *vsm;
4608 struct GNUNET_HashCode trail_id;
4609 struct GNUNET_PeerIdentity successor;
4610 struct GNUNET_PeerIdentity source_peer;
4611 struct GNUNET_PeerIdentity *trail;
4612 struct GNUNET_PeerIdentity *next_hop;
4613 struct FingerInfo current_predecessor;
4614 struct FriendInfo *target_friend;
4615 unsigned int trail_src_to_curr_pred_len = 0;
4616 struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
4617 unsigned int trail_length;
4620 msize = ntohs (message->size);
4622 if (msize < sizeof (struct PeerVerifySuccessorMessage))
4624 GNUNET_break_op (0);
4628 vsm = (const struct PeerVerifySuccessorMessage *) message;
4629 trail_length = (msize - sizeof (struct PeerVerifySuccessorMessage))/
4630 sizeof (struct GNUNET_PeerIdentity);
4631 if ((msize - sizeof (struct PeerVerifySuccessorMessage)) %
4632 sizeof (struct GNUNET_PeerIdentity) != 0)
4634 GNUNET_break_op (0);
4638 GNUNET_STATISTICS_update (GDS_stats,
4640 ("# Bytes received from other peers"), msize,
4643 trail_id = vsm->trail_id;
4644 source_peer = vsm->source_peer;
4645 successor = vsm->successor;
4646 trail = (struct GNUNET_PeerIdentity *)&vsm[1];
4648 /* I am NOT the successor of source_peer. Pass the message to next_hop on
4650 if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
4652 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
4653 if (NULL == next_hop)
4655 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
4656 GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
4657 GNUNET_break_op (0);
4661 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4663 if(NULL == target_friend)
4668 GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
4669 trail_id, trail, trail_length,
4674 /* I am the destination of this message. */
4676 /* Check if the source_peer could be our predecessor and if yes then update
4678 compare_and_update_predecessor (source_peer, trail, trail_length);
4679 current_predecessor = finger_table[PREDECESSOR_FINGER_ID];
4681 /* Is source of this message NOT my predecessor. */
4682 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (¤t_predecessor.finger_identity,
4685 trail_src_to_curr_pred =
4686 get_trail_src_to_curr_pred (source_peer,
4689 &trail_src_to_curr_pred_len);
4693 trail_src_to_curr_pred_len = trail_length;
4696 trail_src_to_curr_pred =
4697 GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)
4698 *trail_src_to_curr_pred_len);
4699 for(i = 0; i < trail_src_to_curr_pred_len; i++)
4701 trail_src_to_curr_pred[i] = trail[i];
4705 GNUNET_assert (NULL !=
4707 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
4708 GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
4709 current_predecessor.finger_identity,
4710 trail_id, trail_src_to_curr_pred,
4711 trail_src_to_curr_pred_len,
4712 GDS_ROUTING_DEST_TO_SRC,
4714 GNUNET_free_non_null(trail_src_to_curr_pred);
4720 * If the trail from me to my probable successor contains a friend not
4721 * at index 0, then we can shorten the trail.
4722 * @param probable_successor Peer which is our probable successor
4723 * @param trail_me_to_probable_successor Peers in path from me to my probable
4724 * successor, NOT including the endpoints.
4725 * @param trail_me_to_probable_successor_len Total number of peers in
4726 * @a trail_me_to_probable_succesor.
4727 * @return Updated trail, if any friend found.
4728 * Else the trail_me_to_probable_successor.
4730 struct GNUNET_PeerIdentity *
4731 check_trail_me_to_probable_succ (struct GNUNET_PeerIdentity probable_successor,
4732 const struct GNUNET_PeerIdentity *trail_me_to_probable_successor,
4733 unsigned int trail_me_to_probable_successor_len,
4734 unsigned int *trail_to_new_successor_length)
4738 struct GNUNET_PeerIdentity *trail_to_new_successor;
4740 /* Probable successor is a friend */
4741 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4742 &probable_successor))
4744 trail_to_new_successor = NULL;
4745 *trail_to_new_successor_length = 0;
4746 return trail_to_new_successor;
4749 /* Is there any friend of yours in this trail. */
4750 if(trail_me_to_probable_successor_len > 1)
4752 for (i = trail_me_to_probable_successor_len - 1; i > 0; i--)
4754 if (NULL == GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4755 &trail_me_to_probable_successor[i]))
4758 *trail_to_new_successor_length = (trail_me_to_probable_successor_len - i);
4759 trail_to_new_successor = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
4760 *trail_to_new_successor_length);
4763 for(j = 0;j < *trail_to_new_successor_length;i++,j++)
4765 trail_to_new_successor[j] = trail_me_to_probable_successor[i];
4768 return trail_to_new_successor;
4772 *trail_to_new_successor_length = trail_me_to_probable_successor_len;
4773 return (struct GNUNET_PeerIdentity*)trail_me_to_probable_successor;
4778 * Check if the peer which sent us verify successor result message is still ours
4779 * successor or not. If not, then compare existing successor and probable successor.
4780 * In case probable successor is the correct successor, remove the existing
4781 * successor. Add probable successor as new successor. Send notify new successor
4782 * message to new successor.
4783 * @param curr_succ Peer to which we sent the verify successor message. It may
4784 * or may not be our real current successor, as we may have few iterations of
4785 * find finger trail task.
4786 * @param probable_successor Peer which should be our successor accroding to @a
4788 * @param trail List of peers to reach from me to @a probable successor, NOT including
4790 * @param trail_length Total number of peers in @a trail.
4793 compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
4794 struct GNUNET_PeerIdentity probable_successor,
4795 const struct GNUNET_PeerIdentity *trail,
4796 unsigned int trail_length)
4798 struct FingerInfo *current_successor;
4799 struct GNUNET_PeerIdentity closest_peer;
4800 struct GNUNET_HashCode trail_id;
4801 struct GNUNET_PeerIdentity *trail_me_to_probable_succ;
4802 struct FriendInfo *target_friend;
4803 unsigned int trail_me_to_probable_succ_len;
4804 unsigned int is_predecessor = 0;
4805 uint64_t successor_value;
4807 current_successor = &finger_table[0];
4808 successor_value = compute_finger_identity_value(0);
4810 /* If probable successor is same as current_successor, do nothing. */
4811 if(0 == GNUNET_CRYPTO_cmp_peer_identity (&probable_successor,
4812 ¤t_successor->finger_identity))
4815 closest_peer = select_closest_peer (&probable_successor,
4816 ¤t_successor->finger_identity,
4817 successor_value, is_predecessor);
4819 /* If the current_successor in the finger table is closest, then do nothing. */
4820 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&closest_peer ,
4821 ¤t_successor->finger_identity))
4823 if ((NULL != GDS_stats))
4829 my_id_str = GNUNET_strdup (GNUNET_i2s_full (&my_identity));
4830 memcpy(&succ, ¤t_successor->finger_identity, sizeof(uint64_t));
4831 GNUNET_asprintf (&key, "XDHT:%s:", my_id_str);
4832 GNUNET_free (my_id_str);
4833 GNUNET_STATISTICS_set (GDS_stats, key, succ, 0);
4839 /* Probable successor is the closest peer.*/
4840 if(trail_length > 0)
4842 GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4847 GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4848 &probable_successor));
4851 trail_me_to_probable_succ_len = 0;
4852 trail_me_to_probable_succ =
4853 check_trail_me_to_probable_succ (probable_successor,
4854 trail, trail_length,
4855 &trail_me_to_probable_succ_len);
4857 /* Remove the existing successor. */
4858 remove_existing_finger (current_successor, 0);
4859 /* Generate a new trail id to reach to your new successor. */
4860 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
4861 &trail_id, sizeof (trail_id));
4863 if (trail_me_to_probable_succ_len > 0)
4865 GDS_ROUTING_add (trail_id, my_identity, trail_me_to_probable_succ[0]);
4866 GNUNET_assert (NULL !=
4868 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4869 &trail_me_to_probable_succ[0])));
4873 GDS_ROUTING_add (trail_id, my_identity, probable_successor);
4874 GNUNET_assert (NULL !=
4876 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4877 &probable_successor)));
4880 add_new_finger (probable_successor, trail_me_to_probable_succ,
4881 trail_me_to_probable_succ_len, trail_id, 0);
4883 GDS_NEIGHBOURS_send_notify_new_successor (my_identity, probable_successor,
4884 trail_me_to_probable_succ,
4885 trail_me_to_probable_succ_len,
4893 * FIXME: Check for duplicate elements everywhere when you are making
4895 * Core handle for p2p verify successor result messages.
4896 * @param cls closure
4897 * @param message message
4898 * @param peer peer identity this notification is about
4899 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4902 handle_dht_p2p_verify_successor_result(void *cls,
4903 const struct GNUNET_PeerIdentity *peer,
4904 const struct GNUNET_MessageHeader *message)
4906 const struct PeerVerifySuccessorResultMessage *vsrm;
4907 enum GDS_ROUTING_trail_direction trail_direction;
4908 struct GNUNET_PeerIdentity querying_peer;
4909 struct GNUNET_HashCode trail_id;
4910 struct GNUNET_PeerIdentity *next_hop;
4911 struct FriendInfo *target_friend;
4912 struct GNUNET_PeerIdentity probable_successor;
4913 struct GNUNET_PeerIdentity current_successor;
4914 const struct GNUNET_PeerIdentity *trail;
4915 unsigned int trail_length;
4918 msize = ntohs (message->size);
4919 /* We send a trail to reach from old successor to new successor, if
4920 * old_successor != new_successor.*/
4921 if (msize < sizeof (struct PeerVerifySuccessorResultMessage))
4923 GNUNET_break_op (0);
4927 vsrm = (const struct PeerVerifySuccessorResultMessage *) message;
4928 trail_length = (msize - sizeof (struct PeerVerifySuccessorResultMessage))/
4929 sizeof (struct GNUNET_PeerIdentity);
4931 if ((msize - sizeof (struct PeerVerifySuccessorResultMessage)) %
4932 sizeof (struct GNUNET_PeerIdentity) != 0)
4934 GNUNET_break_op (0);
4938 GNUNET_STATISTICS_update (GDS_stats,
4940 ("# Bytes received from other peers"), msize,
4943 trail = (const struct GNUNET_PeerIdentity *) &vsrm[1];
4944 querying_peer = vsrm->querying_peer;
4945 trail_direction = ntohl (vsrm->trail_direction);
4946 trail_id = vsrm->trail_id;
4947 probable_successor = vsrm->probable_successor;
4948 current_successor = vsrm->current_successor;
4950 verify_successor_next_send_time =
4951 GNUNET_TIME_STD_BACKOFF(verify_successor_next_send_time);
4952 /* I am the querying_peer. */
4953 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
4955 compare_and_update_successor (current_successor,
4956 probable_successor, trail, trail_length);
4960 /*If you are not the querying peer then pass on the message */
4961 if(NULL == (next_hop =
4962 GDS_ROUTING_get_next_hop (trail_id, trail_direction)))
4964 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
4965 GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
4969 if (NULL == (target_friend =
4970 GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)))
4975 GDS_NEIGHBOURS_send_verify_successor_result (querying_peer,
4976 vsrm->current_successor,
4977 probable_successor, trail_id,
4980 trail_direction, target_friend);
4986 * Core handle for p2p notify new successor messages.
4987 * @param cls closure
4988 * @param message message
4989 * @param peer peer identity this notification is about
4990 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4993 handle_dht_p2p_notify_new_successor(void *cls,
4994 const struct GNUNET_PeerIdentity *peer,
4995 const struct GNUNET_MessageHeader *message)
4997 const struct PeerNotifyNewSuccessorMessage *nsm;
4998 struct GNUNET_PeerIdentity *trail;
4999 struct GNUNET_PeerIdentity source;
5000 struct GNUNET_PeerIdentity new_successor;
5001 struct GNUNET_HashCode trail_id;
5002 struct GNUNET_PeerIdentity next_hop;
5003 struct FriendInfo *target_friend;
5006 uint32_t trail_length;
5008 msize = ntohs (message->size);
5010 /* We have the trail to reach from source to new successor. */
5011 if (msize < sizeof (struct PeerNotifyNewSuccessorMessage))
5013 GNUNET_break_op (0);
5017 nsm = (const struct PeerNotifyNewSuccessorMessage *) message;
5018 trail_length = (msize - sizeof (struct PeerNotifyNewSuccessorMessage))/
5019 sizeof (struct GNUNET_PeerIdentity);
5020 if ((msize - sizeof (struct PeerNotifyNewSuccessorMessage)) %
5021 sizeof (struct GNUNET_PeerIdentity) != 0)
5023 GNUNET_break_op (0);
5027 GNUNET_STATISTICS_update (GDS_stats,
5029 ("# Bytes received from other peers"), msize,
5032 trail = (struct GNUNET_PeerIdentity *) &nsm[1];
5033 source = nsm->source_peer;
5034 new_successor = nsm->new_successor;
5035 trail_id = nsm->trail_id;
5037 /* I am the new_successor to source_peer. */
5038 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_successor))
5040 if(trail_length > 0)
5041 GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity(&trail[trail_length - 1],
5044 GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity(&source, peer));
5046 compare_and_update_predecessor (source, trail, trail_length);
5050 GNUNET_assert(trail_length > 0);
5051 /* I am part of trail to reach to successor. */
5052 my_index = search_my_index (trail, trail_length);
5055 DEBUG ("No entry found in trail\n");
5056 GNUNET_break_op (0);
5057 return GNUNET_SYSERR;
5059 if((trail_length + 1) == my_index)
5061 DEBUG ("Found twice in trail.\n");
5062 GNUNET_break_op (0);
5063 return GNUNET_SYSERR;
5065 if ((trail_length-1) == my_index)
5066 next_hop = new_successor;
5068 next_hop = trail[my_index + 1];
5069 /* Add an entry in routing table for trail from source to its new successor. */
5070 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
5071 GNUNET_assert (NULL !=
5073 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
5074 GDS_NEIGHBOURS_send_notify_new_successor (source, new_successor, trail,
5076 trail_id, target_friend);
5083 * Core handler for P2P trail rejection message
5084 * @param cls closure
5085 * @param message message
5086 * @param peer peer identity this notification is about
5087 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5090 handle_dht_p2p_trail_setup_rejection (void *cls,
5091 const struct GNUNET_PeerIdentity *peer,
5092 const struct GNUNET_MessageHeader *message)
5094 const struct PeerTrailRejectionMessage *trail_rejection;
5095 unsigned int trail_length;
5096 const struct GNUNET_PeerIdentity *trail_peer_list;
5097 struct FriendInfo *target_friend;
5098 struct GNUNET_TIME_Relative congestion_timeout;
5099 struct GNUNET_HashCode trail_id;
5100 struct GNUNET_PeerIdentity next_peer;
5101 struct GNUNET_PeerIdentity source;
5102 uint64_t ultimate_destination_finger_value;
5103 unsigned int is_predecessor;
5106 msize = ntohs (message->size);
5107 /* We are passing the trail setup so far. */
5108 if (msize < sizeof (struct PeerTrailRejectionMessage))
5110 GNUNET_break_op (0);
5114 trail_rejection = (const struct PeerTrailRejectionMessage *) message;
5115 if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
5116 sizeof (struct GNUNET_PeerIdentity) != 0)
5118 GNUNET_break_op (0);
5121 trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
5122 sizeof (struct GNUNET_PeerIdentity);
5124 GNUNET_STATISTICS_update (GDS_stats,
5126 ("# Bytes received from other peers"), msize,
5129 trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_rejection[1];
5130 is_predecessor = ntohl (trail_rejection->is_predecessor);
5131 congestion_timeout = trail_rejection->congestion_time;
5132 source = trail_rejection->source_peer;
5133 trail_id = trail_rejection->trail_id;
5134 ultimate_destination_finger_value =
5135 GNUNET_ntohll (trail_rejection->ultimate_destination_finger_value);
5137 /* First set the congestion time of the friend that sent you this message. */
5138 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
5139 if (NULL == target_friend)
5141 DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
5145 target_friend->congestion_timestamp =
5146 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
5147 congestion_timeout);
5149 /* I am the source peer which wants to setup the trail. Do nothing.
5150 * send_find_finger_trail_task is scheduled periodically.*/
5151 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
5154 /* If I am congested then pass this message to peer before me in trail. */
5155 if(GNUNET_YES == GDS_ROUTING_threshold_reached())
5157 if (0 == trail_length)
5160 next_peer = trail_peer_list[trail_length-1];
5163 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
5164 if (NULL == target_friend)
5166 DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
5170 GDS_NEIGHBOURS_send_trail_rejection (source,
5171 ultimate_destination_finger_value,
5172 my_identity, is_predecessor,
5173 trail_peer_list,trail_length,trail_id,
5174 target_friend, CONGESTION_TIMEOUT);
5178 struct Closest_Peer successor;
5179 successor = find_successor (ultimate_destination_finger_value, is_predecessor);
5181 /* Am I the final destination? */
5182 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
5185 if (0 == trail_length)
5188 next_peer = trail_peer_list[trail_length-1];
5191 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
5192 if (NULL == target_friend)
5194 DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
5198 GDS_NEIGHBOURS_send_trail_setup_result (source,
5200 target_friend, trail_length,
5203 ultimate_destination_finger_value,
5208 struct GNUNET_PeerIdentity peer_list[trail_length + 1];
5210 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
5211 peer_list[trail_length] = my_identity;
5214 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5215 &successor.next_hop);
5216 if (NULL == target_friend)
5218 DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
5222 GDS_NEIGHBOURS_send_trail_setup (source,
5223 ultimate_destination_finger_value,
5224 successor.best_known_destination,
5225 target_friend, trail_length + 1, peer_list,
5226 is_predecessor, trail_id,
5227 successor.trail_id);
5234 * Core handler for trail teardown message.
5235 * @param cls closure
5236 * @param message message
5237 * @param peer sender of this messsage.
5238 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5241 handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer,
5242 const struct GNUNET_MessageHeader *message)
5244 const struct PeerTrailTearDownMessage *trail_teardown;
5245 enum GDS_ROUTING_trail_direction trail_direction;
5246 struct GNUNET_HashCode trail_id;
5247 struct GNUNET_PeerIdentity *next_hop;
5250 msize = ntohs (message->size);
5252 /* Here we pass only the trail id. */
5253 if (msize != sizeof (struct PeerTrailTearDownMessage))
5255 GNUNET_break_op (0);
5259 GNUNET_STATISTICS_update (GDS_stats,
5261 ("# Bytes received from other peers"), msize,
5264 trail_teardown = (const struct PeerTrailTearDownMessage *) message;
5265 trail_direction = ntohl (trail_teardown->trail_direction);
5266 trail_id = trail_teardown->trail_id;
5268 /* Check if peer is the real peer from which we should get this message.*/
5269 /* Get the prev_hop for this trail by getting the next hop in opposite direction. */
5271 GNUNET_assert (NULL != (prev_hop =
5272 GDS_ROUTING_get_next_hop (trail_id, !trail_direction)));
5273 if (0 != GNUNET_CRYPTO_cmp_peer_identity (prev_hop, peer))
5276 return GNUNET_SYSERR;
5280 next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
5281 if (NULL == next_hop)
5283 DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
5284 GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
5286 return GNUNET_SYSERR;
5289 /* I am the next hop, which means I am the final destination. */
5290 if (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))
5292 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
5297 /* If not final destination, then send a trail teardown message to next hop.*/
5298 GNUNET_assert (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop));
5299 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
5300 GDS_NEIGHBOURS_send_trail_teardown (trail_id, trail_direction, *next_hop);
5308 * Core handle for p2p add trail message.
5309 * @param cls closure
5310 * @param message message
5311 * @param peer peer identity this notification is about
5312 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5315 handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
5316 const struct GNUNET_MessageHeader *message)
5318 const struct PeerAddTrailMessage *add_trail;
5319 const struct GNUNET_PeerIdentity *trail;
5320 struct GNUNET_HashCode trail_id;
5321 struct GNUNET_PeerIdentity destination_peer;
5322 struct GNUNET_PeerIdentity source_peer;
5323 struct GNUNET_PeerIdentity next_hop;
5324 unsigned int trail_length;
5325 unsigned int my_index;
5328 msize = ntohs (message->size);
5329 /* In this message we pass the whole trail from source to destination as we
5330 * are adding that trail.*/
5331 //FIXME: failed when run with 1000 pears. check why.
5332 if (msize < sizeof (struct PeerAddTrailMessage))
5334 GNUNET_break_op (0);
5338 add_trail = (const struct PeerAddTrailMessage *) message;
5339 trail_length = (msize - sizeof (struct PeerAddTrailMessage))/
5340 sizeof (struct GNUNET_PeerIdentity);
5341 if ((msize - sizeof (struct PeerAddTrailMessage)) %
5342 sizeof (struct GNUNET_PeerIdentity) != 0)
5344 GNUNET_break_op (0);
5348 GNUNET_STATISTICS_update (GDS_stats,
5350 ("# Bytes received from other peers"), msize,
5353 trail = (const struct GNUNET_PeerIdentity *)&add_trail[1];
5354 destination_peer = add_trail->destination_peer;
5355 source_peer = add_trail->source_peer;
5356 trail_id = add_trail->trail_id;
5358 //FIXME: add a check that sender peer is not malicious. Make it a generic
5359 // function so that it can be used in all other functions where we need the
5360 // same functionality.
5362 /* I am not the destination of the trail. */
5363 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination_peer))
5365 struct FriendInfo *target_friend;
5367 /* Get my location in the trail. */
5368 my_index = search_my_index (trail, trail_length);
5371 GNUNET_break_op (0);
5372 return GNUNET_SYSERR;
5374 if((trail_length + 1) == my_index)
5376 DEBUG ("Found twice in trail.\n");
5377 GNUNET_break_op (0);
5378 return GNUNET_SYSERR;
5380 if ((trail_length - 1) == my_index)
5382 next_hop = destination_peer;
5386 next_hop = trail[my_index + 1];
5388 /* Add in your routing table. */
5389 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, next_hop, *peer));
5390 GNUNET_assert (NULL !=
5392 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
5393 GDS_NEIGHBOURS_send_add_trail (source_peer, destination_peer, trail_id,
5394 trail, trail_length, target_friend);
5397 /* I am the destination. Add an entry in routing table. */
5398 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, my_identity));
5404 * Free the finger trail in which the first friend to reach to a finger is
5405 * disconnected_friend. Also remove entry from routing table for that particular
5407 * @param disconnected_friend PeerIdentity of friend which got disconnected
5408 * @param remove_finger Finger whose trail we need to check if it has
5409 * disconnected_friend as the first hop.
5410 * @return Total number of trails in which disconnected_friend was the first
5414 remove_matching_trails (const struct GNUNET_PeerIdentity *disconnected_friend,
5415 struct FingerInfo *finger)
5417 unsigned int matching_trails_count = 0;
5420 /* Iterate over all the trails of finger. */
5421 for (i = 0; i < finger->trails_count; i++)
5423 struct Trail *current_trail;
5424 current_trail = &finger->trail_list[i];
5425 //FIXME: This assertion fails if we have gap in trail list from o to trails count.
5426 GNUNET_assert (GNUNET_YES == current_trail->is_present);
5427 /* First friend to reach to finger is disconnected_peer. */
5428 if (0 == GNUNET_CRYPTO_cmp_peer_identity (¤t_trail->trail_head->peer,
5429 disconnected_friend))
5431 struct GNUNET_PeerIdentity *next_hop;
5432 struct FriendInfo *remove_friend;
5434 GNUNET_assert (NULL !=
5436 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5437 disconnected_friend)));
5438 next_hop = GDS_ROUTING_get_next_hop (current_trail->trail_id,
5439 GDS_ROUTING_SRC_TO_DEST);
5441 /* Here it may happen that as all the peers got disconnected, the entry in
5442 routing table for that particular trail has been removed, because the
5443 previously disconnected peer was either a next hop or prev hop of that
5445 if (NULL != next_hop)
5447 GNUNET_assert (0 == (GNUNET_CRYPTO_cmp_peer_identity (disconnected_friend,
5449 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (current_trail->trail_id));
5451 matching_trails_count++;
5452 free_trail (current_trail);
5453 current_trail->is_present = GNUNET_NO;
5456 return matching_trails_count;
5461 * Iterate over finger_table entries.
5462 * 0. Ignore finger which is my_identity or if no valid entry present at
5463 * that finger index.
5464 * 1. If disconnected_friend is a finger, then remove the routing entry from
5465 your own table. Free the trail.
5466 * 2. Check if disconnected_friend is the first friend in the trail to reach to a finger.
5467 * 2.1 Remove all the trails and entry from routing table in which disconnected
5468 * friend is the first friend in the trail. If disconnected_friend is the
5469 * first friend in all the trails to reach finger, then remove the finger.
5470 * @param disconnected_friend Peer identity of friend which got disconnected.
5473 remove_matching_fingers (const struct GNUNET_PeerIdentity *disconnected_peer)
5475 struct FingerInfo *current_finger;
5476 struct FriendInfo *remove_friend;
5477 int removed_trails_count;
5480 /* Iterate over finger table entries. */
5481 for (i = 0; i < MAX_FINGERS; i++)
5483 current_finger = &finger_table[i];
5485 /* No finger stored at this trail index. */
5486 if ((GNUNET_NO == current_finger->is_present) ||
5487 (0 == GNUNET_CRYPTO_cmp_peer_identity (¤t_finger->finger_identity,
5491 /* Is disconnected_peer a finger? */
5492 if (0 == GNUNET_CRYPTO_cmp_peer_identity (disconnected_peer,
5493 ¤t_finger->finger_identity))
5495 struct GNUNET_PeerIdentity *next_hop;
5496 struct GNUNET_HashCode trail_id;
5498 GNUNET_assert (0 == current_finger->trail_list[0].trail_length);
5499 GNUNET_assert (GNUNET_YES == (current_finger->trail_list[0].is_present));
5500 trail_id = current_finger->trail_list[0].trail_id;
5504 GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST)))
5506 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
5507 GNUNET_assert (NULL !=
5509 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5510 disconnected_peer)));
5512 current_finger->trail_list[0].is_present = GNUNET_NO;
5513 current_finger->is_present = GNUNET_NO;
5514 memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
5518 /* If finger is a friend but not disconnected_friend, then continue. */
5519 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5520 ¤t_finger->finger_identity))
5523 /* Iterate over the list of trails to reach remove_finger. Check if
5524 * disconnected_friend is the first friend in any of the trail. */
5525 removed_trails_count = remove_matching_trails (disconnected_peer,
5527 current_finger->trails_count =
5528 current_finger->trails_count - removed_trails_count;
5529 if (0 == current_finger->trails_count)
5531 current_finger->is_present = GNUNET_NO;
5532 memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
5539 * Method called whenever a peer disconnects.
5541 * @param cls closure
5542 * @param peer peer identity this notification is about
5545 handle_core_disconnect (void *cls,
5546 const struct GNUNET_PeerIdentity *peer)
5548 struct FriendInfo *remove_friend;
5549 struct P2PPendingMessage *pos;
5550 unsigned int discarded;
5552 /* If disconnected to own identity, then return. */
5553 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
5556 if(NULL == (remove_friend =
5557 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)))
5559 DEBUG("\n friend already disconnected.");
5563 remove_matching_fingers (peer);
5564 GNUNET_assert (GNUNET_SYSERR != GDS_ROUTING_remove_trail_by_peer (peer));
5565 GNUNET_assert (GNUNET_YES ==
5566 GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
5570 /* Remove all the messages queued in pending list of this peer is discarded.*/
5571 if (remove_friend->th != NULL)
5573 GNUNET_CORE_notify_transmit_ready_cancel(remove_friend->th);
5574 remove_friend->th = NULL;
5578 while (NULL != (pos = remove_friend->head))
5580 GNUNET_CONTAINER_DLL_remove (remove_friend->head, remove_friend->tail, pos);
5585 GNUNET_STATISTICS_update (GDS_stats,
5587 ("# Queued messages discarded (peer disconnected)"),
5588 discarded, GNUNET_NO);
5589 //GNUNET_free (remove_friend);
5591 if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
5594 if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
5596 GNUNET_SCHEDULER_cancel (find_finger_trail_task);
5597 find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
5605 * Method called whenever a peer connects.
5607 * @param cls closure
5608 * @param peer_identity peer identity this notification is about
5611 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
5613 struct FriendInfo *friend;
5615 /* Check for connect to self message */
5616 if (0 == memcmp (&my_identity, peer_identity, sizeof (struct GNUNET_PeerIdentity)))
5619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to %s\n", GNUNET_i2s (peer_identity));
5621 /* If peer already exists in our friend_peermap, then exit. */
5622 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
5629 friend = GNUNET_new (struct FriendInfo);
5630 friend->id = *peer_identity;
5632 GNUNET_assert (GNUNET_OK ==
5633 GNUNET_CONTAINER_multipeermap_put (friend_peermap,
5634 peer_identity, friend,
5635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
5637 /* got a first connection, good time to start with FIND FINGER TRAIL requests...*/
5638 if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
5640 find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
5646 * To be called on core init/fail.
5648 * @param cls service closure
5649 * @param identity the public identity of this peer
5652 core_init (void *cls,
5653 const struct GNUNET_PeerIdentity *identity)
5655 my_identity = *identity;
5658 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
5659 my_id64 = GNUNET_ntohll (my_id64);
5660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5661 "my_indentity = %s, my_id64=%llu\n",GNUNET_i2s(&my_identity),(unsigned long long)my_id64);
5667 * Initialize finger table entries.
5670 finger_table_init ()
5672 memset (&finger_table, 0, sizeof (finger_table));
5677 * Initialize neighbours subsystem.
5678 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5681 GDS_NEIGHBOURS_init (void)
5683 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5684 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT, 0},
5685 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_XDHT_P2P_GET, 0},
5686 {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT, 0},
5687 {&handle_dht_p2p_trail_setup, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP, 0},
5688 {&handle_dht_p2p_trail_setup_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT, 0},
5689 {&handle_dht_p2p_verify_successor, GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR, 0},
5690 {&handle_dht_p2p_verify_successor_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT, 0},
5691 {&handle_dht_p2p_notify_new_successor, GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR, 0},
5692 {&handle_dht_p2p_trail_setup_rejection, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION, 0},
5693 {&handle_dht_p2p_trail_teardown, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN,
5694 sizeof (struct PeerTrailTearDownMessage)},
5695 {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL, 0},
5699 #if ENABLE_MALICIOUS
5704 GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
5705 &handle_core_disconnect, NULL, GNUNET_NO, NULL,
5706 GNUNET_NO, core_handlers);
5708 if (NULL == core_api)
5709 return GNUNET_SYSERR;
5711 //TODO: check size of this peer map?
5712 friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
5713 finger_table_init ();
5715 find_finger_trail_task_next_send_time.rel_value_us =
5716 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
5717 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
5718 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
5720 verify_successor_next_send_time.rel_value_us =
5721 DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us +
5722 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
5723 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
5729 * Free the memory held up by trails of a finger.
5732 delete_finger_table_entries()
5737 for(i = 0; i < MAX_FINGERS; i++)
5739 if(GNUNET_NO == finger_table[i].is_present)
5742 for(j = 0; j < finger_table[i].trails_count; j++)
5744 free_trail(&finger_table[i].trail_list[i]);
5750 * Shutdown neighbours subsystem.
5753 GDS_NEIGHBOURS_done (void)
5755 if (NULL == core_api)
5758 GNUNET_CORE_disconnect (core_api);
5761 delete_finger_table_entries();
5763 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
5764 GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
5765 friend_peermap = NULL;
5767 if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
5770 GNUNET_SCHEDULER_cancel (find_finger_trail_task);
5771 find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
5774 if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_task)
5776 GNUNET_SCHEDULER_cancel (send_verify_successor_task);
5777 send_verify_successor_task = GNUNET_SCHEDULER_NO_TASK;
5785 * @return my identity
5787 struct GNUNET_PeerIdentity
5788 GDS_NEIGHBOURS_get_my_id (void)
5793 /* end of gnunet-service-xdht_neighbours.c */