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
53 * 2. Now souce and destination of a trail also stores the trail entries for
54 * which they are end point. Make these changes in case of gds_routing_add()
55 * 3. Should we append xvine in message which are of xvine dht?
56 * 4. make sure you are adding trail for end point of trail everywhere.
57 * 5. Should we increment the trail count of a friend which is a finger.
58 * 6. You have two variables - current_search_finger_index and finger map index
59 * , now you need to understand should you update current_search_finger_index
60 * based on finger map index. Make these two variables clear in their functionality.
64 * Maximum possible fingers (including predecessor) of a peer
66 #define MAX_FINGERS 65
69 * Maximum allowed number of pending messages per friend peer.
71 #define MAXIMUM_PENDING_PER_FRIEND 64
74 * How long to wait before sending another find finger trail request
76 #define DHT_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
79 * How long at most to wait for transmission of a request to a friend ?
81 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
84 * Duration for which I may remain congested.
85 * Note: Its a static value. In future, a peer may do some analysis and calculate
86 * congestion_timeout based on 'some' parameters.
88 #define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
91 * Maximum number of trails allowed to go through a friend.
93 #define TRAILS_THROUGH_FRIEND_THRESHOLD 64
96 * Maximum number of trails stored per finger.
98 #define MAXIMUM_TRAILS_PER_FINGER 2
101 * Finger map index for predecessor entry in finger table.
103 #define PREDECESSOR_FINGER_ID 64
106 * Wrap around in peer identity circle.
107 * FIXME: not used anywhere, should be used in
108 * find_successor() while comparing two peers.
110 #define PEER_IDENTITES_WRAP_AROUND pow(2, 64) - 1
113 * To check if a finger is predecessor or not.
115 enum GDS_NEIGHBOURS_finger_type
117 GDS_FINGER_TYPE_PREDECESSOR = 0,
118 GDS_FINGER_TYPE_NON_PREDECESSOR = 1
121 GNUNET_NETWORK_STRUCT_BEGIN
126 struct PeerPutMessage
129 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
131 struct GNUNET_MessageHeader header;
136 uint32_t options GNUNET_PACKED;
141 uint32_t block_type GNUNET_PACKED;
146 uint32_t hop_count GNUNET_PACKED;
149 * Replication level for this message
150 * In the current implementation, this value is not used.
152 uint32_t desired_replication_level GNUNET_PACKED;
155 * Length of the PUT path that follows (if tracked).
157 uint32_t put_path_length GNUNET_PACKED;
160 * Best known destination (could be my friend or finger) which should
161 * get this message next.
163 struct GNUNET_PeerIdentity best_known_destination;
166 * In case best_known_destination is a finger, then trail to reach
167 * to that finger. Else its default value is 0.
169 struct GNUNET_HashCode intermediate_trail_id;
172 * When does the content expire?
174 struct GNUNET_TIME_AbsoluteNBO expiration_time;
177 * The key to store the value under.
179 struct GNUNET_HashCode key GNUNET_PACKED;
181 /* put path (if tracked) */
190 struct PeerGetMessage
193 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
195 struct GNUNET_MessageHeader header;
200 uint32_t options GNUNET_PACKED;
203 * Desired content type.
205 uint32_t block_type GNUNET_PACKED;
210 uint32_t hop_count GNUNET_PACKED;
213 * Desired replication level for this request.
214 * In the current implementation, this value is not used.
216 uint32_t desired_replication_level GNUNET_PACKED;
219 * Total number of peers in get path.
221 unsigned int get_path_length;
224 * Best known destination (could be my friend or finger) which should
225 * get this message next.
227 struct GNUNET_PeerIdentity best_known_destination;
230 * In case best_known_destination is a finger, then trail to reach
231 * to that finger. Else its default value is 0.
233 struct GNUNET_HashCode intermediate_trail_id;
236 * The key we are looking for.
238 struct GNUNET_HashCode key;
247 struct PeerGetResultMessage
250 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT
252 struct GNUNET_MessageHeader header;
255 * The type for the data.
257 uint32_t type GNUNET_PACKED;
260 * Number of peers recorded in the outgoing path from source to the
261 * stored location of this message.
263 uint32_t put_path_length GNUNET_PACKED;
266 * Length of the GET path that follows (if tracked).
268 uint32_t get_path_length GNUNET_PACKED;
271 * Peer which queried for get and should get the result.
273 struct GNUNET_PeerIdentity querying_peer;
276 * When does the content expire?
278 struct GNUNET_TIME_Absolute expiration_time;
281 * The key of the corresponding GET request.
283 struct GNUNET_HashCode key;
285 /* put path (if tracked) */
287 /* get path (if tracked) */
294 * P2P Trail setup message
296 struct PeerTrailSetupMessage
299 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP
301 struct GNUNET_MessageHeader header;
304 * Is source_peer trying to setup the trail to a predecessor or any finger.
306 uint32_t is_predecessor;
309 * Peer closest to this value will be our finger.
311 uint64_t final_destination_finger_value;
314 * Source peer which wants to setup the trail to one of its finger.
316 struct GNUNET_PeerIdentity source_peer;
319 * Best known destination (could be my friend or finger) which should
320 * get this message next.
322 struct GNUNET_PeerIdentity best_known_destination;
325 * In case best_known_destination is a finger, then trail id of trail to
326 * reach to this finger.
328 struct GNUNET_HashCode intermediate_trail_id;
331 * Trail id for trail which we are trying to setup.
333 struct GNUNET_HashCode trail_id;
335 /* List of peers which are part of trail setup so far.
336 * Trail does NOT include source_peer and peer which will be closest to
337 * ultimate_destination_finger_value.
338 * struct GNUNET_PeerIdentity trail[]
343 * P2P Trail Setup Result message
345 struct PeerTrailSetupResultMessage
349 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT
351 struct GNUNET_MessageHeader header;
354 * Finger to which we have found the path.
356 struct GNUNET_PeerIdentity finger_identity;
359 * Peer which started trail_setup to find trail to finger_identity
361 struct GNUNET_PeerIdentity querying_peer;
364 * Is the trail setup to querying_peer's predecessor or finger?
366 uint32_t is_predecessor;
369 * Value to which finger_identity is the closest peer.
371 uint64_t ulitmate_destination_finger_value;
374 * Identifier of the trail from querying peer to finger_identity, NOT
375 * including both endpoints.
377 struct GNUNET_HashCode trail_id;
379 /* List of peers which are part of the trail from querying peer to
380 * finger_identity, NOT including both endpoints.
381 * struct GNUNET_PeerIdentity trail[]
386 * P2P Verify Successor Message.
388 struct PeerVerifySuccessorMessage
391 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR
393 struct GNUNET_MessageHeader header;
396 * Peer which wants to verify its successor.
398 struct GNUNET_PeerIdentity source_peer;
401 * Source Peer's current successor.
403 struct GNUNET_PeerIdentity successor;
406 * Identifier of trail to reach from source_peer to successor.
408 struct GNUNET_HashCode trail_id;
410 /* List of the peers which are part of trail to reach from source_peer
411 * to successor, NOT including them
412 * struct GNUNET_PeerIdentity trail[]
417 * FIXME: In case you append the trail it may contain the same peer twice.
418 * So, when you call search_my_index it can return error. Solution is while
419 * appending the entry first check for duplicate entries or may be don't
420 * send the current_predecessor at all.
421 * P2P Verify Successor Result Message
423 struct PeerVerifySuccessorResultMessage
426 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT
428 struct GNUNET_MessageHeader header;
431 * Peer which sent the request to verify its successor.
433 struct GNUNET_PeerIdentity querying_peer;
436 * Successor to which PeerVerifySuccessorMessage was sent.
438 struct GNUNET_PeerIdentity source_successor;
441 * Current Predecessor of source_successor. It can be same as querying peer
444 struct GNUNET_PeerIdentity current_predecessor;
447 * Trail identifier of trail from querying_peer to source_successor.
449 struct GNUNET_HashCode trail_id;
452 * Direction in which we are looking at the trail.
454 uint32_t trail_direction;
456 /* In case current_predecessor != querying_peer, then trail to reach from
457 * querying_peer to current_predecessor, NOT including end points.
458 * struct GNUNET_PeerIdentity trail[]
463 * P2P Notify New Successor Message.
465 struct PeerNotifyNewSuccessorMessage
468 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR
470 struct GNUNET_MessageHeader header;
473 * Peer which wants to notify its new successor.
475 struct GNUNET_PeerIdentity source_peer;
478 * New successor of source_peer.
480 struct GNUNET_PeerIdentity new_successor;
483 * Unique identifier of the trail from source_peer to new_successor,
484 * NOT including the endpoints.
486 struct GNUNET_HashCode trail_id;
488 /* List of peers in trail from source_peer to new_successor,
489 * NOT including the endpoints.
490 * struct GNUNET_PeerIdentity trail[]
495 * P2P Trail Compression Message.
497 struct PeerTrailCompressionMessage
500 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION
502 struct GNUNET_MessageHeader header;
505 * Source peer of this trail.
507 struct GNUNET_PeerIdentity source_peer;
510 * Trail from source_peer to destination_peer compressed such that
511 * new_first_friend is the first hop in the trail from source to
514 struct GNUNET_PeerIdentity new_first_friend;
517 * Unique identifier of trail.
519 struct GNUNET_HashCode trail_id;
524 * P2P Trail Tear Down message.
526 struct PeerTrailTearDownMessage
529 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN
531 struct GNUNET_MessageHeader header;
534 * Unique identifier of the trail.
536 struct GNUNET_HashCode TRAIL_ID;
539 * Direction of trail.
541 uint32_t trail_direction;
546 * P2P Trail Rejection Message.
548 struct PeerTrailRejectionMessage
551 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION
553 struct GNUNET_MessageHeader header;
556 * Peer which wants to set up the trail.
558 struct GNUNET_PeerIdentity source_peer;
561 * Peer which sent trail rejection message as it it congested.
563 struct GNUNET_PeerIdentity congested_peer;
566 * Peer identity closest to this value will be finger of
569 uint64_t ultimate_destination_finger_value;
572 * Is source_peer trying to setup the trail to its predecessor or finger.
574 uint32_t is_predecessor;
577 * Identifier for the trail that source peer is trying to setup.
579 struct GNUNET_HashCode trail_id;
582 * Relative time for which congested_peer will remain congested.
584 struct GNUNET_TIME_Relative congestion_time;
586 /* Trail_list from source_peer to peer which sent the message for trail setup
587 * to congested peer. This trail does NOT include source_peer.
588 struct GNUNET_PeerIdnetity trail[]*/
592 * P2P Add Trail Message.
594 struct PeerAddTrailMessage
597 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL
599 struct GNUNET_MessageHeader header;
602 * Source of the routing trail.
604 struct GNUNET_PeerIdentity source_peer;
607 * Destination of the routing trail.
609 struct GNUNET_PeerIdentity destination_peer;
612 * Unique identifier of the trail from source_peer to destination_peer,
613 * NOT including the endpoints.
615 struct GNUNET_HashCode trail_id;
617 /* Trail from source peer to destination peer, NOT including them.
618 * struct GNUNET_PeerIdentity trail[]
622 GNUNET_NETWORK_STRUCT_END
625 * Linked list of messages to send to a particular other peer.
627 struct P2PPendingMessage
630 * Pointer to next item in the list
632 struct P2PPendingMessage *next;
635 * Pointer to previous item in the list
637 struct P2PPendingMessage *prev;
640 * Message importance level. FIXME: used? useful?
642 unsigned int importance;
645 * When does this message time out?
647 struct GNUNET_TIME_Absolute timeout;
650 * Actual message to be sent, allocated at the end of the struct:
651 * // msg = (cast) &pm[1];
652 * // memcpy (&pm[1], data, len);
654 const struct GNUNET_MessageHeader *msg;
659 * Entry in friend_peermap.
666 struct GNUNET_PeerIdentity id;
669 * Number of trails for which this friend is the first hop or if the friend
672 unsigned int trails_count;
675 * Count of outstanding messages for this friend.
677 unsigned int pending_count;
680 * In case not 0, then amount of time for which this friend is congested.
682 struct GNUNET_TIME_Absolute congestion_timestamp;
685 * Head of pending messages to be sent to this friend.
687 struct P2PPendingMessage *head;
690 * Tail of pending messages to be sent to this friend.
692 struct P2PPendingMessage *tail;
695 * Core handle for sending messages to this friend.
697 struct GNUNET_CORE_TransmitHandle *th;
702 * An individual element of the trail to reach to a finger.
707 * Pointer to next item in the list
709 struct Trail_Element *next;
712 * Pointer to prev item in the list
714 struct Trail_Element *prev;
717 * An element in this trail.
719 struct GNUNET_PeerIdentity peer;
723 * FIXME: removed first_friend_trails_count, need to write a function
724 * to calculate each time we need it. Else, keep a pointer to first
725 * friend of in the trail.
726 * Information about an individual trail.
733 struct Trail_Element *trail_head;
738 struct Trail_Element *trail_tail;
741 * Unique identifier of this trail.
743 struct GNUNET_HashCode trail_id;
746 * Length of trail pointed
748 unsigned int trail_length;
752 * An entry in finger_table
759 struct GNUNET_PeerIdentity finger_identity;
763 * Is there any valid entry for this finger.
765 unsigned int is_present;
768 * Index in finger peer map
770 uint32_t finger_table_index;
773 * Number of trails setup so far for this finger.
774 * Should not cross MAXIMUM_TRAILS_PER_FINGER.
776 uint32_t trails_count;
779 * Array of trails to reach to this finger.
781 struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER];
786 * Data structure to keep track of closest peer seen so far in find_successor()
791 * Destination finger vaule.
793 uint64_t destination_finger_value;
796 * Is finger value predecessor or any other finge
798 unsigned int is_predecessor;
801 * Trail id to reach to peer.
803 struct GNUNET_HashCode trail_id;
806 * FIXME: see the usage of this field and write comment.
808 struct GNUNET_PeerIdentity next_hop;
811 * Next destination. In case of friend and my_identity , it is same as next_hop
812 * In case of finger it is finger identity.
814 struct GNUNET_PeerIdentity best_known_destination;
818 * FIXME: now I have removed the first_friend_trail_count,
819 * Need to update the code to find the count.
820 * Data structure to store the trail chosen to reach to finger.
822 struct Selected_Finger_Trail
825 * First friend in the trail to reach finger.
827 struct FriendInfo friend;
830 * Identifier of this trail.
832 struct GNUNET_HashCode trail_id;
835 * Total number of peers in this trail.
837 unsigned int trail_length;
841 * Task that sends FIND FINGER TRAIL requests. This task is started when we have
842 * get our first friend.
844 static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
847 * Identity of this peer.
849 static struct GNUNET_PeerIdentity my_identity;
852 * Peer map of all the friends of a peer
854 static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
857 * Array of all the fingers.
859 static struct FingerInfo finger_table [MAX_FINGERS];
864 static struct GNUNET_CORE_Handle *core_api;
867 * The current finger index that we have want to find trail to. We start the
868 * search with value = 0, i.e. successor and then go to PREDCESSOR_FINGER_ID
869 * and decrement it. For any index 63 <= index < 0, if finger is same as successor,
870 * we reset this index to 0.
872 static unsigned int current_search_finger_index;
876 * Called when core is ready to send a message we asked for
877 * out to the destination.
879 * @param cls the 'struct FriendInfo' of the target friend
880 * @param size number of bytes available in buf
881 * @param buf where the callee should write the message
882 * @return number of bytes written to buf
885 core_transmit_notify (void *cls, size_t size, void *buf)
887 struct FriendInfo *peer = cls;
889 struct P2PPendingMessage *pending;
894 while ((NULL != (pending = peer->head)) &&
895 (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us))
897 peer->pending_count--;
898 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
899 GNUNET_free (pending);
903 /* no messages pending */
909 GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
910 GNUNET_CORE_PRIO_BEST_EFFORT,
911 GNUNET_TIME_absolute_get_remaining
912 (pending->timeout), &peer->id,
913 ntohs (pending->msg->size),
914 &core_transmit_notify, peer);
915 GNUNET_break (NULL != peer->th);
919 while ((NULL != (pending = peer->head)) &&
920 (size - off >= (msize = ntohs (pending->msg->size))))
922 GNUNET_STATISTICS_update (GDS_stats,
924 ("# Bytes transmitted to other peers"), msize,
926 memcpy (&cbuf[off], pending->msg, msize);
928 peer->pending_count--;
929 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
930 GNUNET_free (pending);
932 if (peer->head != NULL)
935 GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
936 GNUNET_CORE_PRIO_BEST_EFFORT,
937 GNUNET_TIME_absolute_get_remaining
938 (pending->timeout), &peer->id, msize,
939 &core_transmit_notify, peer);
940 GNUNET_break (NULL != peer->th);
948 * Transmit all messages in the friend's message queue.
950 * @param peer message queue to process
953 process_friend_queue (struct FriendInfo *peer)
955 struct P2PPendingMessage *pending;
957 if (NULL == (pending = peer->head))
959 if (NULL != peer->th)
962 GNUNET_STATISTICS_update (GDS_stats,
964 ("# Bytes of bandwidth requested from core"),
965 ntohs (pending->msg->size), GNUNET_NO);
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);
979 * Return friend corresponding to peer.
984 GDS_NEIGHBOURS_get_friend (struct GNUNET_PeerIdentity peer)
986 struct FriendInfo *friend;
987 GNUNET_assert (NULL != (friend =
988 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &peer)));
994 * Construct a trail setup message and forward it to target_friend
995 * @param source_peer Peer which wants to setup the trail
996 * @param ultimate_destination_finger_value Peer identity closest to this value
997 * will be finger to @a source_peer
998 * @param best_known_destination Best known destination (could be finger or friend)
999 * which should get this message.
1000 * @param target_friend Friend to which message is forwarded now.
1001 * @param trail_length Total number of peers in trail setup so far.
1002 * @param trail_peer_list Trail setup so far
1003 * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
1004 * @param trail_id Unique identifier for the trail we are trying to setup.
1005 * @param intermediate_trail_id Trail id of intermediate trail to reach to
1006 * best_known_destination when its a finger. If not
1007 * used then set to 0.
1010 GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
1011 uint64_t ultimate_destination_finger_value,
1012 struct GNUNET_PeerIdentity best_known_destination,
1013 struct FriendInfo *target_friend,
1014 unsigned int trail_length,
1015 const struct GNUNET_PeerIdentity *trail_peer_list,
1016 unsigned int is_predecessor,
1017 struct GNUNET_HashCode trail_id,
1018 struct GNUNET_HashCode *intermediate_trail_id)
1020 struct P2PPendingMessage *pending;
1021 struct PeerTrailSetupMessage *tsm;
1022 struct GNUNET_PeerIdentity *peer_list;
1025 msize = sizeof (struct PeerTrailSetupMessage) +
1026 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1028 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1034 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1036 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1040 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1041 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1042 tsm = (struct PeerTrailSetupMessage *) &pending[1];
1043 pending->msg = &tsm->header;
1044 tsm->header.size = htons (msize);
1045 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP);
1046 tsm->final_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
1047 tsm->source_peer = source_peer;
1048 tsm->best_known_destination = best_known_destination;
1049 tsm->is_predecessor = htonl (is_predecessor);
1050 tsm->trail_id = trail_id;
1052 if (NULL == intermediate_trail_id)
1053 memset (&tsm->intermediate_trail_id, 0, sizeof (tsm->intermediate_trail_id));
1055 tsm->intermediate_trail_id = *intermediate_trail_id;
1057 if (trail_length > 0)
1059 peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
1060 memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
1063 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1064 target_friend->pending_count++;
1065 process_friend_queue (target_friend);
1070 * Construct a trail setup result message and forward it to target friend.
1071 * @param querying_peer Peer which sent the trail setup request and should get
1073 * @param Finger Peer to which the trail has been setup to.
1074 * @param target_friend Friend to which this message should be forwarded.
1075 * @param trail_length Numbers of peers in the trail.
1076 * @param trail_peer_list Peers which are part of the trail from q
1077 * querying_peer to Finger, NOT including them.
1078 * @param is_predecessor Is @a Finger predecessor to @a querying_peer
1079 * @param ultimate_destination_finger_value Value to which @a finger is the closest
1081 * @param trail_id Unique identifier of the trail.
1084 GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity querying_peer,
1085 struct GNUNET_PeerIdentity finger,
1086 struct FriendInfo *target_friend,
1087 unsigned int trail_length,
1088 const struct GNUNET_PeerIdentity *trail_peer_list,
1089 unsigned int is_predecessor,
1090 uint64_t ultimate_destination_finger_value,
1091 struct GNUNET_HashCode trail_id)
1093 struct P2PPendingMessage *pending;
1094 struct PeerTrailSetupResultMessage *tsrm;
1095 struct GNUNET_PeerIdentity *peer_list;
1098 msize = sizeof (struct PeerTrailSetupResultMessage) +
1099 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1101 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1107 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1109 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1113 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1114 pending->importance = 0;
1115 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1116 tsrm = (struct PeerTrailSetupResultMessage *) &pending[1];
1117 pending->msg = &tsrm->header;
1118 tsrm->header.size = htons (msize);
1119 tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT);
1120 tsrm->querying_peer = querying_peer;
1121 tsrm->finger_identity = finger;
1122 tsrm->is_predecessor = htonl (is_predecessor);
1123 tsrm->trail_id = trail_id;
1124 tsrm->ulitmate_destination_finger_value =
1125 GNUNET_htonll (ultimate_destination_finger_value);
1126 peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
1127 if (trail_length > 0)
1129 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1131 /* Send the message to chosen friend. */
1132 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1133 target_friend->pending_count++;
1134 process_friend_queue (target_friend);
1139 * Send trail rejection message to next_hop
1140 * @param source_peer Peer which is trying to setup the trail.
1141 * @param ultimate_destination_finger_value Peer closest to this value will be
1142 * @a source_peer's finger
1143 * @param congested_peer Peer which sent this message as it is congested.
1144 * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
1145 * @param trail_peer_list Trails seen so far in trail setup before getting rejected
1146 * by congested_peer. This does not include @a source_peer
1147 * @param trail_length Total number of peers in trail_peer_list, not including
1149 * @param trail_id Unique identifier of this trail.
1150 * @param congestion_timeout Duration given by congested peer as an estimate of
1151 * how long it may remain congested.
1154 GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
1155 uint64_t ultimate_destination_finger_value,
1156 struct GNUNET_PeerIdentity congested_peer,
1157 unsigned int is_predecessor,
1158 const struct GNUNET_PeerIdentity *trail_peer_list,
1159 unsigned int trail_length,
1160 struct GNUNET_HashCode trail_id,
1161 struct FriendInfo *target_friend,
1162 const struct GNUNET_TIME_Relative congestion_timeout)
1164 struct PeerTrailRejectionMessage *trm;
1165 struct P2PPendingMessage *pending;
1166 struct GNUNET_PeerIdentity *peer_list;
1169 msize = sizeof (struct PeerTrailRejectionMessage) +
1170 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1172 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1178 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1180 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1184 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1185 pending->importance = 0;
1186 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1187 trm = (struct PeerTrailRejectionMessage *)&pending[1];
1188 pending->msg = &trm->header;
1189 trm->header.size = htons (msize);
1190 trm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION);
1191 trm->source_peer = source_peer;
1192 trm->congested_peer = congested_peer;
1193 trm->congestion_time = congestion_timeout;
1194 trm->is_predecessor = htonl (is_predecessor);
1195 trm->trail_id = trail_id;
1196 trm->ultimate_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
1198 peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
1199 if (trail_length > 0)
1201 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1204 /* Send the message to chosen friend. */
1205 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1206 target_friend->pending_count++;
1207 process_friend_queue (target_friend);
1212 * Construct a verify successor message and forward it to target_friend.
1213 * @param source_peer Peer which wants to verify its successor.
1214 * @param successor Peer which is @a source_peer's current successor.
1215 * @param trail_id Unique Identifier of trail from @a source_peer to @a successor,
1216 * NOT including them.
1217 * @param trail List of peers which are part of trail to reach from @a source_peer
1218 * to @a successor, NOT including them.
1219 * @param trail_length Total number of peers in @a trail.
1220 * @param target_friend Next friend to get this message.
1223 GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
1224 struct GNUNET_PeerIdentity successor,
1225 const struct GNUNET_HashCode trail_id,
1226 struct GNUNET_PeerIdentity *trail,
1227 unsigned int trail_length,
1228 struct FriendInfo *target_friend)
1230 struct PeerVerifySuccessorMessage *vsm;
1231 struct P2PPendingMessage *pending;
1232 struct GNUNET_PeerIdentity *peer_list;
1235 msize = sizeof (struct PeerVerifySuccessorMessage);
1236 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1242 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1244 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1248 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1249 pending->importance = 0; /* FIXME */
1250 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1251 vsm = (struct PeerVerifySuccessorMessage *) &pending[1];
1252 pending->msg = &vsm->header;
1253 vsm->header.size = htons (msize);
1254 vsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR);
1255 vsm->source_peer = source_peer;
1256 vsm->successor = successor;
1257 vsm->trail_id = trail_id;
1259 if (trail_length > 0)
1261 peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
1262 memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
1265 /* Send the message to chosen friend. */
1266 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1267 target_friend->pending_count++;
1268 process_friend_queue (target_friend);
1273 * FIXME: In every function we pass target friend except for this one.
1274 * so, either change everything or this one. also, should se just store
1275 * the pointer to friend in routing table rather than gnunet_peeridentity.
1276 * if yes then we should keep friend info in.h andmake lot of changes.
1277 * Construct a trail teardown message and forward it to target friend.
1278 * @param trail_id Unique identifier of the trail.
1279 * @param trail_direction Direction of trail.
1280 * @param target_friend Friend to get this message.
1283 GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_HashCode trail_id,
1284 unsigned int trail_direction,
1285 struct GNUNET_PeerIdentity *peer)
1287 struct PeerTrailTearDownMessage *ttdm;
1288 struct P2PPendingMessage *pending;
1289 struct FriendInfo *target_friend;
1292 msize = sizeof (struct PeerTrailTearDownMessage);
1294 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1300 GNUNET_assert (NULL != (target_friend =
1301 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
1303 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1305 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1309 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1310 pending->importance = 0; /* FIXME */
1311 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1312 ttdm = (struct PeerTrailTearDownMessage *) &pending[1];
1313 pending->msg = &ttdm->header;
1314 ttdm->header.size = htons (msize);
1315 ttdm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN);
1316 ttdm->TRAIL_ID = trail_id;
1317 ttdm->trail_direction = htonl (trail_direction);
1319 /* Send the message to chosen friend. */
1321 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1322 target_friend->pending_count++;
1323 process_friend_queue (target_friend);
1328 * Construct a verify successor result message and send it to target_friend
1329 * @param querying_peer Peer which sent the verify successor message.
1330 * @param source_successor Current_successor of @a querying_peer.
1331 * @param current_predecessor Current predecessor of @a successor. Could be same
1332 * or different from @a querying_peer.
1333 * @param trail_id Unique identifier of the trail from @a querying_peer to
1334 * @a successor, NOT including them.
1335 * @param trail List of peers which are part of trail from @a querying_peer to
1336 * @a successor, NOT including them.
1337 * @param trail_length Total number of peers in @a trail
1338 * @param trail_direction Direction in which we are sending the message. In this
1339 * case we are sending result from @a successor to @a querying_peer.
1340 * @param target_friend Next friend to get this message.
1343 GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity querying_peer,
1344 struct GNUNET_PeerIdentity source_successor,
1345 struct GNUNET_PeerIdentity current_predecessor,
1346 struct GNUNET_HashCode trail_id,
1347 const struct GNUNET_PeerIdentity *trail,
1348 unsigned int trail_length,
1349 enum GDS_ROUTING_trail_direction trail_direction,
1350 struct FriendInfo *target_friend)
1352 struct PeerVerifySuccessorResultMessage *vsmr;
1353 struct P2PPendingMessage *pending;
1354 struct GNUNET_PeerIdentity *peer_list;
1357 msize = sizeof (struct PeerVerifySuccessorResultMessage) +
1358 (trail_length * sizeof(struct GNUNET_PeerIdentity));
1360 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1366 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1368 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1372 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1373 pending->importance = 0; /* FIXME */
1374 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1375 vsmr = (struct PeerVerifySuccessorResultMessage *) &pending[1];
1376 pending->msg = &vsmr->header;
1377 vsmr->header.size = htons (msize);
1378 vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT);
1379 vsmr->querying_peer = querying_peer;
1380 vsmr->source_successor = source_successor;
1381 vsmr->current_predecessor = current_predecessor;
1382 vsmr->trail_direction = htonl (trail_direction);
1383 vsmr->trail_id = trail_id;
1385 if (trail_length > 0)
1387 peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
1388 memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
1391 /* Send the message to chosen friend. */
1392 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1393 target_friend->pending_count++;
1394 process_friend_queue (target_friend);
1399 * Construct a notify new successor message and send it to target_friend
1400 * @param source_peer Peer which wants to notify to its new successor that it
1401 * could be its predecessor.
1402 * @param successor New successor of @a source_peer
1403 * @param successor_trail List of peers in Trail to reach from
1404 * @a source_peer to @a new_successor, NOT including
1406 * @param successor_trail_length Total number of peers in @a new_successor_trail.
1407 * @param successor_trail_id Unique identifier of @a new_successor_trail.
1408 * @param target_friend Next friend to get this message.
1411 GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer,
1412 struct GNUNET_PeerIdentity successor,
1413 struct GNUNET_PeerIdentity *successor_trail,
1414 unsigned int successor_trail_length,
1415 struct GNUNET_HashCode succesor_trail_id,
1416 struct FriendInfo *target_friend)
1418 struct PeerNotifyNewSuccessorMessage *nsm;
1419 struct P2PPendingMessage *pending;
1420 struct GNUNET_PeerIdentity *peer_list;
1423 msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
1424 (successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
1426 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1432 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1434 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1438 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1439 pending->importance = 0; /* FIXME */
1440 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1441 nsm = (struct PeerNotifyNewSuccessorMessage *) &pending[1];
1442 pending->msg = &nsm->header;
1443 nsm->header.size = htons (msize);
1444 nsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR);
1445 nsm->new_successor = successor;
1446 nsm->source_peer = source_peer;
1447 nsm->trail_id = succesor_trail_id;
1449 if (successor_trail_length > 0)
1451 peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
1452 memcpy (peer_list, successor_trail,
1453 successor_trail_length * sizeof (struct GNUNET_PeerIdentity));
1456 /* Send the message to chosen friend. */
1457 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1458 target_friend->pending_count++;
1459 process_friend_queue (target_friend);
1464 * Construct an add_trail message and send it to target_friend
1465 * @param source_peer Source of the trail.
1466 * @param destination_peer Destination of the trail.
1467 * @param trail_id Unique identifier of the trail from
1468 * @a source_peer to @a destination_peer, NOT including the endpoints.
1469 * @param trail List of peers in Trail from @a source_peer to @a destination_peer,
1470 * NOT including the endpoints.
1471 * @param trail_length Total number of peers in @a trail.
1472 * @param target_friend Next friend to get this message.
1475 GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
1476 struct GNUNET_PeerIdentity destination_peer,
1477 struct GNUNET_HashCode trail_id,
1478 struct GNUNET_PeerIdentity *trail,
1479 unsigned int trail_length,
1480 struct FriendInfo *target_friend)
1482 struct PeerAddTrailMessage *adm;
1483 struct GNUNET_PeerIdentity *peer_list;
1484 struct P2PPendingMessage *pending;
1487 msize = sizeof (struct PeerAddTrailMessage) +
1488 (trail_length * sizeof(struct GNUNET_PeerIdentity));
1490 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1496 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1498 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1502 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1503 pending->importance = 0; /* FIXME */
1504 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1505 adm = (struct PeerAddTrailMessage *) &pending[1];
1506 pending->msg = &adm->header;
1507 adm->header.size = htons (msize);
1508 adm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL);
1509 adm->source_peer = source_peer;
1510 adm->destination_peer = destination_peer;
1511 adm->trail_id = trail_id;
1513 if (trail_length > 0)
1515 peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
1516 memcpy (peer_list, trail, sizeof (struct GNUNET_PeerIdentity) * trail_length);
1519 /* Send the message to chosen friend. */
1520 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1521 target_friend->pending_count++;
1522 process_friend_queue (target_friend);
1528 * Construct a trail compression message and send it to target_friend.
1529 * @param source_peer Source of the trail.
1530 * @param trail_id Unique identifier of trail.
1531 * @param first_friend First hop in compressed trail to reach from source to finger
1532 * @param target_friend Next friend to get this message.
1535 GDS_NEIGHBOURS_send_trail_compression (struct GNUNET_PeerIdentity source_peer,
1536 struct GNUNET_HashCode trail_id,
1537 struct GNUNET_PeerIdentity first_friend,
1538 struct FriendInfo *target_friend)
1540 struct P2PPendingMessage *pending;
1541 struct PeerTrailCompressionMessage *tcm;
1544 msize = sizeof (struct PeerTrailCompressionMessage);
1546 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1552 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1554 GNUNET_STATISTICS_update (GDS_stats,
1555 gettext_noop ("# P2P messages dropped due to full queue"),
1559 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1560 pending->importance = 0; /* FIXME */
1561 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1562 tcm = (struct PeerTrailCompressionMessage *) &pending[1];
1563 pending->msg = &tcm->header;
1564 tcm->header.size = htons (msize);
1565 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION);
1566 tcm->source_peer = source_peer;
1567 tcm->new_first_friend = first_friend;
1568 tcm->trail_id = trail_id;
1570 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1571 target_friend->pending_count++;
1572 process_friend_queue (target_friend);
1578 * Search my location in trail.
1579 * @param trail List of peers
1580 * @return my_index if found
1581 * -1 if no entry found.
1584 search_my_index (const struct GNUNET_PeerIdentity *trail,
1589 for (i = 0; i < trail_length; i++)
1591 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
1599 * Check if the friend is congested or have reached maximum number of trails
1600 * it can be part of of.
1601 * @param friend Friend to be chechked.
1602 * @return #GNUNET_YES if friend is not congested or have not crossed threshold.
1603 * #GNUNET_NO if friend is either congested or have crossed threshold
1606 is_friend_congested (struct FriendInfo *friend)
1608 if ((friend->trails_count == TRAILS_THROUGH_FRIEND_THRESHOLD)||
1609 ((0 != GNUNET_TIME_absolute_get_remaining
1610 (friend->congestion_timestamp).rel_value_us)))
1618 * FIXME: here we should also check if iterator is null or not. It can be NULL
1619 * only if we insert randomly at locations. But as we are using trails_count
1620 * as the parameter, it should not happen.
1621 * Iterate over the list of all the trails of a finger. In case the first
1622 * friend to reach the finger has reached trail threshold or is congested,
1623 * then don't select it. In case there multiple available good trails to reach
1624 * to Finger, choose the one with shortest trail length.
1625 * Note: We use length as parameter. But we can use any other suitable parameter
1627 * @param finger Finger
1628 * @return struct Selected_Finger_Trail which contains the first friend , trail id
1629 * and trail length. NULL in case none of the trails are free.
1631 static struct Selected_Finger_Trail *
1632 select_finger_trail (struct FingerInfo *finger)
1634 struct FriendInfo *friend;
1635 struct Trail *iterator;
1636 struct Selected_Finger_Trail *finger_trail;
1639 finger_trail = GNUNET_new (struct Selected_Finger_Trail);
1641 for (i = 0; i < finger->trails_count; i++)
1643 iterator = &finger->trail_list[i];
1644 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1645 &iterator->trail_head->peer);
1646 if (GNUNET_YES == is_friend_congested (friend))
1649 /* Check if the trail length of this trail is least seen so far. If yes then
1650 set finger_trail to this trail.*/
1651 if (finger_trail->trail_length > iterator->trail_length)
1653 finger_trail->friend = *friend;
1654 finger_trail->trail_id = iterator->trail_id;
1655 finger_trail->trail_length = iterator->trail_length;
1659 /* No trail found. */
1660 if (i == finger->trails_count)
1661 finger_trail = NULL;
1663 return finger_trail;
1668 * FIXME; not handling the wrap around logic correctly.
1669 * Select closest finger to value.
1670 * @param peer1 First peer
1671 * @param peer2 Second peer
1672 * @param value Value to be compare
1673 * @return Closest peer
1675 static struct GNUNET_PeerIdentity *
1676 select_closest_finger (struct GNUNET_PeerIdentity *peer1,
1677 struct GNUNET_PeerIdentity *peer2,
1680 uint64_t peer1_value;
1681 uint64_t peer2_value;
1683 memcpy (&peer1_value, peer1, sizeof (uint64_t));
1684 memcpy (&peer2_value, peer2, sizeof (uint64_t));
1686 if (peer1_value == value)
1691 if (peer2_value == value)
1696 if (peer1_value < peer2_value)
1698 if ((peer1_value < value) && (value < peer2_value))
1702 else if (((peer2_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1703 ((PEER_IDENTITES_WRAP_AROUND < value) && (value < peer1_value)))
1709 if (peer2_value < peer1_value)
1711 if ((peer2_value < value) && (value < peer1_value))
1715 else if (((peer1_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1716 ((PEER_IDENTITES_WRAP_AROUND < value) && (value < peer2_value)))
1726 * FIMXE: COMPLETE THE LOGIC.
1733 * 0 <= key < 5, so my_id 0 is the predecessor.
1734 * peer1 != peer2 ever.
1735 * Select closest predecessor to value.
1736 * @param peer1 First peer
1737 * @param peer2 Second peer
1738 * @param value Value to be compare
1739 * @return Closest peer
1741 static struct GNUNET_PeerIdentity *
1742 select_closest_predecessor (struct GNUNET_PeerIdentity *peer1,
1743 struct GNUNET_PeerIdentity *peer2,
1746 uint64_t peer1_value;
1747 uint64_t peer2_value;
1749 memcpy (&peer1_value, peer1, sizeof (uint64_t));
1750 memcpy (&peer2_value, peer2, sizeof (uint64_t));
1752 if (peer1_value == value)
1755 if (peer2_value == value)
1758 if (peer1_value < peer2_value)
1760 if ((peer1_value < value) && (value < peer2_value))
1762 else if (((peer2_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1763 ((PEER_IDENTITES_WRAP_AROUND < value) && (value < peer1_value)))
1767 if (peer2_value < peer1_value)
1769 if ((peer2_value < value) && (value < peer1_value))
1771 else if (((peer1_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1772 ((PEER_IDENTITES_WRAP_AROUND < value) && (value < peer2_value)))
1779 /* FIXME: select closest peer w.r.t. value. [finger->friend_id, current_successor->id)
1780 and [current_successor->id, finger->friend_id). Check in which range value lies.
1781 Also, check for wrap around. But this will give you the immediate predecessor
1782 For example. if we have 0,1,6 and I am 0 and one of my finger is 6. Then
1783 for 1, we will have ranges like [0,6) and [6,0) 1 lies in range from [0,6)
1784 but successor is 6 not 0 as 6 is > than 1. If you are the closest one,
1786 in current_successor. Want to write a generic code so that it is used in
1787 * finger_table_add also while choosing the closest one among new and existing
1796 * 0 <= key < 5, so key should go to 5.
1800 * Select the closest peer among two peers (which should not be same)
1801 * with respect to value and finger_table_index
1802 * @param peer1 First peer
1803 * @param peer2 Second peer
1804 * @param value Value relative to which we find the closest
1805 * @param finger_table_index Index in finger map. If equal to PREDECESSOR_FINGER_ID,
1806 * then we use different logic than other
1807 * finger_table_index
1808 * @return Closest peer among two peers.
1810 static struct GNUNET_PeerIdentity *
1811 select_closest_peer (struct GNUNET_PeerIdentity *peer1,
1812 struct GNUNET_PeerIdentity *peer2,
1814 unsigned int finger_table_index)
1816 struct GNUNET_PeerIdentity *closest_peer;
1817 /* FIXME: select closest peer w.r.t. value. [friend_id, current_successor->id)
1818 and [current_successor->id, friend_id). Check in which range value lies.
1819 Also, check for wrap around. Set the value of current_successor accordingly.*/
1821 if (PREDECESSOR_FINGER_ID == finger_table_index)
1822 closest_peer = select_closest_predecessor (peer1, peer2, value);
1824 closest_peer = select_closest_finger (peer1, peer2, value);
1826 return closest_peer;
1831 * FIXME: better names and more refactoring.
1832 * Compare FINGER entry with current successor. If finger's first friend of all
1833 * its trail is not congested and has not crossed trail threshold, then check
1834 * if finger peer identity is closer to final_destination_finger_value than
1835 * current_successor. If yes then update current_successor.
1836 * @param current_successor[in/out]
1839 static struct Closest_Peer *
1840 compare_finger_and_current_successor (struct Closest_Peer *current_closest_peer)
1842 struct FingerInfo *finger;
1843 struct FriendInfo *friend;
1844 struct Selected_Finger_Trail *finger_trail;
1845 struct GNUNET_PeerIdentity *closest_peer;
1848 for (i = 0; i < MAX_FINGERS; i++)
1850 finger = &finger_table[i];
1852 if (GNUNET_NO == finger->is_present)
1855 /* If I am my own finger, then ignore this finger. */
1856 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1860 /* If finger is friend. */
1861 if (NULL != (friend = GNUNET_CONTAINER_multipeermap_get
1862 (friend_peermap, &finger->finger_identity)))
1864 if (GNUNET_YES == is_friend_congested (friend))
1867 /* If not congested then compare it with current_successor. */
1868 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1869 ¤t_closest_peer->best_known_destination))
1872 closest_peer = select_closest_peer (&finger->finger_identity,
1873 ¤t_closest_peer->best_known_destination,
1874 current_closest_peer->destination_finger_value,
1875 current_closest_peer->is_predecessor);
1876 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1879 current_closest_peer->best_known_destination = finger->finger_identity;
1880 current_closest_peer->next_hop = finger->finger_identity;
1885 /* Choose one of the trail to reach to finger. */
1886 finger_trail = select_finger_trail (finger);
1888 /* In case no trail found, ignore this finger. */
1889 if (NULL == finger_trail)
1892 closest_peer = select_closest_peer (&finger->finger_identity,
1893 ¤t_closest_peer->best_known_destination,
1894 current_closest_peer->destination_finger_value,
1895 current_closest_peer->is_predecessor);
1896 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1899 current_closest_peer->best_known_destination = finger->finger_identity;
1900 current_closest_peer->next_hop = finger_trail->friend.id;
1901 current_closest_peer->trail_id = finger_trail->trail_id;
1905 return current_closest_peer;
1910 * Compare friend entry with current successor. If friend is not congested and
1911 * has not crossed trail threshold, then check if friend peer identity is
1912 * closer to final_destination_finger_value than current_successor. If yes
1913 * then update current_successor.
1914 * @param cls closure
1915 * @param key current public key
1916 * @param value struct Closest_Peer
1917 * @return #GNUNET_YES if we should continue to iterate,
1918 * #GNUNET_NO if not.
1921 compare_friend_and_current_closest_peer (void *cls,
1922 const struct GNUNET_PeerIdentity *key,
1925 struct FriendInfo *friend = value;
1926 struct Closest_Peer *current_closest_peer = cls;
1927 struct GNUNET_PeerIdentity *closest_peer;
1929 if (GNUNET_YES == is_friend_congested (friend))
1932 closest_peer = select_closest_peer (&friend->id,
1933 ¤t_closest_peer->best_known_destination,
1934 current_closest_peer->destination_finger_value,
1935 current_closest_peer->is_predecessor);
1937 /* If friend is the closest successor. */
1938 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id, closest_peer))
1940 current_closest_peer->best_known_destination = friend->id;
1941 current_closest_peer->next_hop = friend->id;
1948 * Initialize current_successor to my_identity.
1949 * @param my_identity My peer identity
1950 * @return current_successor
1952 static struct Closest_Peer *
1953 init_current_successor (struct GNUNET_PeerIdentity my_identity,
1954 uint64_t destination_finger_value,
1955 unsigned int is_predecessor)
1957 struct Closest_Peer *current_closest_peer;
1959 current_closest_peer = GNUNET_new (struct Closest_Peer);
1960 memset (¤t_closest_peer->trail_id, 0, sizeof (current_closest_peer->trail_id));
1961 current_closest_peer->destination_finger_value = destination_finger_value;
1962 current_closest_peer->is_predecessor = is_predecessor;
1963 current_closest_peer->next_hop = my_identity;
1964 current_closest_peer->best_known_destination = my_identity;
1966 return current_closest_peer;
1971 * FIXME: first check if the finger == closest_peer then don't do anything.
1972 * Find the successor for destination_finger_value among my_identity, all my
1973 * friend and all my fingers. Don't consider friends or fingers
1974 * which are congested or have crossed the threshold.
1975 * @param destination_finger_value Peer closest to this value will be the next successor.
1976 * @param local_best_known_destination [out] Updated to my_identity if I am the
1977 * final destination. Updated to friend
1978 * identity in case a friend is successor,
1979 * updated to first friend to reach to finger
1980 * in case finger is the destination.
1981 * @param new_intermediate_trail_id [out] In case a finger is the
1982 * @a local_best_known_destination,
1983 * then it is the trail to reach it. Else
1985 * @param is_predecessor Are we looking for predecessor or finger?
1986 * @return Next hop to reach to local_best_known_destination. In case my_identity
1987 * or a friend is a local_best_known_destination, then
1988 * next_hop = local_best_known_destination. Else
1989 * next_hop is the first friend to reach to finger (local_best_known_destination)
1991 static struct GNUNET_PeerIdentity *
1992 find_successor (uint64_t destination_finger_value,
1993 struct GNUNET_PeerIdentity *local_best_known_destination,
1994 struct GNUNET_HashCode *new_intermediate_trail_id,
1995 unsigned int is_predecessor)
1997 struct Closest_Peer *current_closest_peer;
1998 struct GNUNET_PeerIdentity *next_hop;
2000 /* Initialize current_successor to my_identity. */
2001 current_closest_peer = init_current_successor (my_identity,
2002 destination_finger_value,
2005 /* Compare each friend entry with current_successor and update current_successor
2006 * with friend if its closest. */
2007 GNUNET_assert (GNUNET_SYSERR !=
2008 GNUNET_CONTAINER_multipeermap_iterate (friend_peermap,
2009 &compare_friend_and_current_closest_peer,
2010 current_closest_peer));
2012 /* Compare each finger entry with current_successor and update current_successor
2013 * with finger if its closest. */
2014 compare_finger_and_current_successor (current_closest_peer);
2016 *local_best_known_destination = current_closest_peer->best_known_destination;
2017 new_intermediate_trail_id = ¤t_closest_peer->trail_id;
2018 next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
2019 *next_hop = current_closest_peer->next_hop;
2026 * Construct a Put message and send it to target_peer.
2027 * @param key Key for the content
2028 * @param block_type Type of the block
2029 * @param options Routing options
2030 * @param desired_replication_level Desired replication count
2031 * @param best_known_dest Peer to which this message should reach eventually,
2032 * as it is best known destination to me.
2033 * @param intermediate_trail_id Trail id in case
2034 * @param target_peer Peer to which this message will be forwarded.
2035 * @param hop_count Number of hops traversed so far.
2036 * @param put_path_length Total number of peers in @a put_path
2037 * @param put_path Number of peers traversed so far
2038 * @param expiration_time When does the content expire
2039 * @param data Content to store
2040 * @param data_size Size of content @a data in bytes
2043 GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
2044 enum GNUNET_BLOCK_Type block_type,
2045 enum GNUNET_DHT_RouteOption options,
2046 uint32_t desired_replication_level,
2047 struct GNUNET_PeerIdentity *best_known_dest,
2048 struct GNUNET_HashCode *intermediate_trail_id,
2049 struct GNUNET_PeerIdentity *target_peer,
2051 uint32_t put_path_length,
2052 struct GNUNET_PeerIdentity *put_path,
2053 struct GNUNET_TIME_Absolute expiration_time,
2054 const void *data, size_t data_size)
2056 struct PeerPutMessage *ppm;
2057 struct P2PPendingMessage *pending;
2058 struct FriendInfo *target_friend;
2059 struct GNUNET_PeerIdentity *pp;
2062 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
2063 sizeof (struct PeerPutMessage);
2065 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2067 put_path_length = 0;
2068 msize = data_size + sizeof (struct PeerPutMessage);
2071 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2077 /* This is the first call made from clients file. So, we should search for the
2079 if (NULL == target_peer)
2082 struct GNUNET_PeerIdentity *next_hop;
2084 memcpy (&key_value, key, sizeof (uint64_t));
2085 next_hop = find_successor (key_value, best_known_dest,
2086 intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
2087 if (0 == GNUNET_CRYPTO_cmp_peer_identity(next_hop, &my_identity))
2089 /* I am the destination but we have already done datacache_put in client file. */
2093 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
2096 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2097 pending->timeout = expiration_time;
2098 ppm = (struct PeerPutMessage *) &pending[1];
2099 pending->msg = &ppm->header;
2100 ppm->header.size = htons (msize);
2101 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
2102 ppm->options = htonl (options);
2103 ppm->block_type = htonl (block_type);
2104 ppm->hop_count = htonl (hop_count + 1);
2105 ppm->desired_replication_level = htonl (desired_replication_level);
2106 ppm->put_path_length = htonl (put_path_length);
2107 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
2108 ppm->best_known_destination = *best_known_dest;
2110 if (NULL == intermediate_trail_id)
2111 memset (&ppm->intermediate_trail_id, 0, sizeof (ppm->intermediate_trail_id));
2113 ppm->intermediate_trail_id = *intermediate_trail_id;
2114 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
2115 if (put_path_length != 0)
2117 memcpy (pp, put_path,
2118 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
2120 memcpy (&pp[put_path_length], data, data_size);
2121 if (NULL == target_friend)
2122 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer);
2123 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2124 target_friend->pending_count++;
2125 process_friend_queue (target_friend);
2131 * Construct a Get message and send it to target_peer.
2132 * @param key Key for the content
2133 * @param block_type Type of the block
2134 * @param options Routing options
2135 * @param desired_replication_level Desired replication count
2136 * @param best_known_dest
2137 * @param intermediate_trail_id
2138 * @param target_peer Peer to which this message will be forwarded.
2139 * @param hop_count Number of hops traversed so far.
2140 * @param data Content to store
2141 * @param data_size Size of content @a data in bytes
2142 * @param get_path_length Total number of peers in @a get_path
2143 * @param get_path Number of peers traversed so far
2146 GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
2147 enum GNUNET_BLOCK_Type block_type,
2148 enum GNUNET_DHT_RouteOption options,
2149 uint32_t desired_replication_level,
2150 struct GNUNET_PeerIdentity *best_known_dest,
2151 struct GNUNET_HashCode *intermediate_trail_id,
2152 struct GNUNET_PeerIdentity *target_peer,
2154 uint32_t get_path_length,
2155 struct GNUNET_PeerIdentity *get_path)
2157 struct PeerGetMessage *pgm;
2158 struct P2PPendingMessage *pending;
2159 struct FriendInfo *target_friend;
2160 struct GNUNET_PeerIdentity *gp;
2163 msize = sizeof (struct PeerGetMessage) +
2164 (get_path_length * sizeof (struct GNUNET_PeerIdentity));
2166 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2172 if (NULL == target_peer)
2174 struct GNUNET_PeerIdentity *next_hop;
2177 memcpy (&key_value, key, sizeof (uint64_t));
2178 // FIXME: endianess of key_value!?
2179 /* FIXME: Here you should use enum GDS_NEIGHBOURS_FINGER_TYPE in place of 0. */
2180 next_hop = find_successor (key_value, best_known_dest,
2181 intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
2183 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity,next_hop))
2185 GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
2186 NULL, 0, 1, &my_identity, NULL,&my_identity);
2191 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
2195 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2196 pending->importance = 0; /* FIXME */
2197 pgm = (struct PeerGetMessage *) &pending[1];
2198 pending->msg = &pgm->header;
2199 pgm->header.size = htons (msize);
2200 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
2201 pgm->get_path_length = htonl (get_path_length);
2203 pgm->best_known_destination = *best_known_dest;
2204 pgm->intermediate_trail_id = *intermediate_trail_id;
2205 pgm->hop_count = htonl (hop_count + 1);
2209 gp = (struct GNUNET_PeerIdentity *) &pgm[1];
2210 memcpy (gp, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
2212 if (NULL == target_friend)
2213 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer);
2214 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2215 target_friend->pending_count++;
2216 process_friend_queue (target_friend);
2221 * Send the get result to requesting client.
2222 * @param key Key of the requested data.
2223 * @param type Block type
2224 * @param target_peer Next peer to forward the message to.
2225 * @param source_peer Peer which has the data for the key.
2226 * @param put_path_length Number of peers in @a put_path
2227 * @param put_path Path taken to put the data at its stored location.
2228 * @param get_path_length Number of peers in @a get_path
2229 * @param get_path Path taken to reach to the location of the key.
2230 * @param expiration When will this result expire?
2231 * @param data Payload to store
2232 * @param data_size Size of the @a data
2235 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
2236 enum GNUNET_BLOCK_Type type,
2237 struct GNUNET_PeerIdentity *target_peer,
2238 struct GNUNET_PeerIdentity *source_peer,
2239 unsigned int put_path_length,
2240 const struct GNUNET_PeerIdentity *put_path,
2241 unsigned int get_path_length,
2242 struct GNUNET_PeerIdentity *get_path,
2243 struct GNUNET_TIME_Absolute expiration,
2244 const void *data, size_t data_size)
2246 struct PeerGetResultMessage *get_result;
2247 struct GNUNET_PeerIdentity *get_result_path;
2248 struct GNUNET_PeerIdentity *pp;
2249 struct P2PPendingMessage *pending;
2250 struct FriendInfo *target_friend;
2251 int current_path_index;
2254 msize = get_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
2255 sizeof (struct PeerPutMessage);
2257 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2263 if(get_path_length > 0)
2265 current_path_index = search_my_index(get_path, get_path_length);
2266 if (GNUNET_SYSERR == current_path_index)
2272 if (0 == current_path_index)
2274 GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
2275 get_path, put_path_length,
2276 put_path, type, data_size, data);
2280 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2281 pending->importance = 0;
2282 get_result = (struct PeerGetResultMessage *)&pending[1];
2283 pending->msg = &get_result->header;
2284 get_result->header.size = htons (msize);
2285 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT);
2286 get_result->key = *key;
2287 /* FIXME: check if you are passing the correct querying_peer as described in
2288 the get_result documentation. */
2289 memcpy (&(get_result->querying_peer), source_peer, sizeof (struct GNUNET_PeerIdentity));
2290 get_result->expiration_time = expiration;
2293 get_result_path = (struct GNUNET_PeerIdentity *)&get_result[1];
2294 memcpy (get_result_path, get_path,
2295 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
2296 memcpy (&get_result_path[get_path_length], data, data_size);
2298 /* FIXME: Is this correct? */
2299 if (put_path_length != 0)
2301 pp = (struct GNUNET_PeerIdentity *)&get_result_path[1];
2302 memcpy (pp, put_path,sizeof (struct GNUNET_PeerIdentity) * put_path_length);
2305 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2306 &get_result_path[current_path_index - 1]);
2307 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2308 target_friend->pending_count++;
2309 process_friend_queue (target_friend);
2314 * Randomly choose one of your friends (which is not congested and have not crossed
2315 * trail threshold) from the friends_peer map
2316 * @return Friend Randomly chosen friend.
2317 * NULL in case friend peermap is empty, or all the friends are either
2318 * congested or have crossed trail threshold.
2320 static struct FriendInfo *
2321 select_random_friend ()
2323 unsigned int current_size;
2326 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
2327 struct GNUNET_PeerIdentity key_ret;
2328 struct FriendInfo *friend;
2330 current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
2331 if (0 == current_size)
2334 index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
2335 iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
2337 for (j = 0; j < index ; j++)
2338 GNUNET_assert (GNUNET_YES ==
2339 GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL, NULL));
2342 if (j == current_size)
2345 GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
2346 iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
2349 GNUNET_assert (GNUNET_YES ==
2350 GNUNET_CONTAINER_multipeermap_iterator_next (iter,
2352 (const void **)&friend));
2355 if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) &&
2356 (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us))
2362 } while (j != index);
2364 GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
2370 * Compute 64 bit value of finger_identity to which we want to setup
2371 * the trail using chord formula.
2372 * @return finger_identity
2375 compute_finger_identity_value (unsigned int finger_index)
2379 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
2380 my_id64 = GNUNET_ntohll (my_id64);
2382 /* Are we looking for immediate predecessor? */
2383 if (PREDECESSOR_FINGER_ID == finger_index)
2384 return (my_id64 -1);
2386 return (my_id64 + (unsigned long) pow (2, finger_index));
2391 * Choose a random friend and start looking for the trail to reach to
2392 * finger identity through this random friend.
2394 * @param cls closure for this task
2395 * @param tc the context under which the task is running
2398 send_find_finger_trail_message (void *cls,
2399 const struct GNUNET_SCHEDULER_TaskContext *tc)
2401 struct FriendInfo *target_friend;
2402 struct GNUNET_TIME_Relative next_send_time;
2403 struct GNUNET_HashCode trail_id;
2404 unsigned int is_predecessor;
2405 uint64_t finger_id_value;
2407 next_send_time.rel_value_us =
2408 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
2409 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2410 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
2411 find_finger_trail_task =
2412 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_finger_trail_message,
2415 target_friend = select_random_friend ();
2416 if (NULL == target_friend)
2421 finger_id_value = compute_finger_identity_value (current_search_finger_index);
2422 if (PREDECESSOR_FINGER_ID == current_search_finger_index)
2427 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
2428 &trail_id, sizeof (trail_id));
2429 GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_id_value,
2430 target_friend->id, target_friend, 0, NULL,
2431 is_predecessor, trail_id, NULL);
2436 * In case there are already maximum number of possible trails to reach to a
2437 * finger, then check if the new trail's length is lesser than any of the
2439 * If yes then replace that old trail by new trail.
2441 * Note: Here we are taking length as a parameter to choose the best possible
2442 * trail, but there could be other parameters also like:
2443 * 1. duration of existence of a trail - older the better.
2444 * 2. if the new trail is completely disjoint than the
2445 * other trails, then may be choosing it is better.
2447 * @param existing_finger
2448 * @param new_finger_trail
2449 * @param new_finger_trail_length
2450 * @param new_finger_trail_id
2453 select_and_replace_trail (struct FingerInfo *existing_finger,
2454 const struct GNUNET_PeerIdentity *new_trail,
2455 unsigned int new_trail_length,
2456 struct GNUNET_HashCode new_trail_id)
2458 struct Trail *trail_list_iterator;
2459 unsigned int largest_trail_length;
2460 unsigned int largest_trail_index;
2461 struct Trail_Element *trail_element;
2464 largest_trail_length = new_trail_length;
2465 largest_trail_index = MAXIMUM_TRAILS_PER_FINGER + 1;
2467 GNUNET_assert (MAXIMUM_TRAILS_PER_FINGER == existing_finger->trails_count);
2469 for (i = 0; i < existing_finger->trails_count; i++)
2471 trail_list_iterator = &existing_finger->trail_list[i];
2472 if (trail_list_iterator->trail_length > largest_trail_length)
2474 largest_trail_length = trail_list_iterator->trail_length;
2475 largest_trail_index = i;
2479 if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
2481 // tear down new trail: it's not better than the existing ones
2485 /* Send trail teardown message across the replaced trail. */
2486 struct Trail *replace_trail = &existing_finger->trail_list[largest_trail_index];
2488 GDS_NEIGHBOURS_send_trail_teardown (replace_trail->trail_id,
2489 GDS_ROUTING_SRC_TO_DEST,
2490 &replace_trail->trail_head->peer);
2491 /* Free the trail. */
2492 while (NULL != (trail_element = replace_trail->trail_head))
2494 GNUNET_CONTAINER_DLL_remove (replace_trail->trail_head,
2495 replace_trail->trail_tail, trail_element);
2496 GNUNET_free (trail_element);
2499 /* Add new trial at that location. */
2501 while (i < new_trail_length)
2503 struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2504 element->peer = new_trail[i];
2506 GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
2507 replace_trail->trail_tail,
2514 * Check if the new trail to reach to finger is unique or do we already have
2515 * such a trail present for finger.
2516 * @param existing_finger Finger identity
2517 * @param new_trail New trail to reach @a existing_finger
2518 * @param trail_length Total number of peers in new_trail.
2519 * @return #GNUNET_YES if the new trail is unique
2520 * #GNUNET_NO if same trail is already present.
2523 is_new_trail_unique (struct FingerInfo *existing_finger,
2524 const struct GNUNET_PeerIdentity *new_trail,
2525 unsigned int trail_length)
2527 struct Trail *trail_list_iterator;
2528 struct Trail_Element *trail_element;
2531 int trail_unique = GNUNET_NO;
2533 for (i = 0; i < existing_finger->trails_count; i++)
2535 trail_list_iterator = &existing_finger->trail_list[i];
2536 if (trail_list_iterator->trail_length != trail_length)
2538 trail_element = trail_list_iterator->trail_head;
2539 for (j = 0; j < trail_list_iterator->trail_length; j++)
2541 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&new_trail[j],
2542 &trail_element->peer))
2544 trail_unique = GNUNET_YES;
2549 return trail_unique;
2554 * Add a new trail to existing finger.
2555 * @param existing_finger
2556 * @param new_finger_trail
2557 * @param new_finger_trail_length
2558 * @param new_finger_trail_id
2561 add_new_trail (struct FingerInfo *existing_finger,
2562 const struct GNUNET_PeerIdentity *new_trail,
2563 unsigned int new_trail_length,
2564 struct GNUNET_HashCode new_trail_id)
2566 struct Trail *trail_list_iterator;
2567 struct FriendInfo *first_friend;
2570 if (GNUNET_NO == is_new_trail_unique (existing_finger, new_trail,
2576 // FIXME checking trail_head is NOT a valid way to verify an open slot
2577 for (i = 0; existing_finger->trail_list[i].trail_head != NULL; i++)
2578 GNUNET_assert (i < MAXIMUM_TRAILS_PER_FINGER);
2580 trail_list_iterator = &existing_finger->trail_list[i];
2582 if (new_trail_length > 0)
2583 first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2586 first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2587 &(existing_finger->finger_identity));
2588 first_friend->trails_count++;
2589 /* FIXME; we removed this field but read fixme. */
2590 //trail_list_iterator->first_friend_trail_count = first_friend->trails_count;
2591 trail_list_iterator->trail_length = new_trail_length;
2593 for (i = 0; i < new_trail_length; i++)
2595 struct Trail_Element *element;
2596 element = GNUNET_new (struct Trail_Element);
2598 element->peer = new_trail[i];
2599 GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head,
2600 trail_list_iterator->trail_tail,
2603 existing_finger->trails_count++;
2608 * Send trail teardown message for a specific trail of a finger.
2609 * @param finger Finger whose trail is to be removed.
2610 * @param trail List of peers in trail from me to a finger, NOT including
2614 send_trail_teardown (struct FingerInfo *finger,
2615 struct Trail *trail)
2617 /* FIXME: Now source also stores a trail entry in its routing table. before
2618 sending the trail teardown, you should get next_hop from routing table.
2619 If it is NULL, it means that path is broken, then remove the trail.
2620 return a value to calling function so that if all trails are removed,
2621 then remove finger. */
2622 /* We should decerement the friend trail count here. */
2623 struct FriendInfo *friend;
2625 GNUNET_assert (NULL != (friend =
2626 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2627 &trail->trail_head->peer)));
2629 friend->trails_count--;
2630 GDS_NEIGHBOURS_send_trail_teardown (trail->trail_id,
2631 GDS_ROUTING_SRC_TO_DEST,
2632 &trail->trail_head->peer);
2637 * Send trail teardown message across all the trails to reach to finger.
2638 * @param finger Finger whose all the trail should be freed.
2641 send_all_finger_trails_teardown (struct FingerInfo *finger)
2643 struct Trail *trail;
2646 for (i = 0; i < finger->trails_count; i++)
2648 trail = &finger->trail_list[i];
2649 if (trail->trail_length > 0)
2651 /* decerement the friend trails count. */
2652 send_trail_teardown (finger, trail);
2659 * Free a specific trail
2660 * @param trail List of peers to be freed.
2663 free_trail (struct Trail *trail)
2665 struct Trail_Element *trail_element;
2667 while (NULL != (trail_element = trail->trail_head))
2669 GNUNET_CONTAINER_DLL_remove (trail->trail_head,
2672 GNUNET_free (trail_element);
2678 * Free finger and its trail.
2679 * @param finger Finger to be freed.
2682 free_finger (struct FingerInfo *finger)
2684 struct Trail *trail;
2688 for (i = 0; i < finger->trails_count; i++)
2690 trail = &finger->trail_list[i];
2693 GNUNET_free (finger);
2698 * Add a new entry in finger table at finger_table_index.
2699 * In case finger identity is me or a friend, then don't add a trail.
2700 * In case a finger is a friend, then increment the trails count of the friend.
2701 * @param finger_identity Peer Identity of new finger
2702 * @param finger_trail Trail to reach from me to finger (excluding both end points).
2703 * @param finger_trail_length Total number of peers in @a finger_trail.
2704 * @param trail_id Unique identifier of the trail.
2705 * @param finger_table_index Index in finger table.
2708 add_new_finger (struct GNUNET_PeerIdentity finger_identity,
2709 const struct GNUNET_PeerIdentity *finger_trail,
2710 unsigned int finger_trail_length,
2711 struct GNUNET_HashCode trail_id,
2712 unsigned int finger_table_index)
2714 struct FingerInfo *new_entry;
2715 struct FriendInfo *first_trail_hop;
2716 struct Trail *trail;
2719 new_entry = GNUNET_new (struct FingerInfo);
2720 new_entry->finger_identity = finger_identity;
2721 new_entry->finger_table_index = finger_table_index;
2722 new_entry->is_present = GNUNET_YES;
2724 /* Finger is not my identity. */
2725 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
2727 if (finger_trail_length == 0)
2729 first_trail_hop = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2731 first_trail_hop->trails_count++;
2732 finger_table[finger_table_index] = *new_entry;
2736 first_trail_hop = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2738 new_entry->trails_count = 1;
2739 first_trail_hop->trails_count++;
2741 /* Copy the finger trail into trail. */
2742 trail = GNUNET_new (struct Trail);
2743 while (i < finger_trail_length)
2745 struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2747 element->next = NULL;
2748 element->prev = NULL;
2749 element->peer = finger_trail[i];
2750 GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
2755 /* Add trail to trail list. */
2756 new_entry->trail_list[0].trail_head = trail->trail_head;
2757 new_entry->trail_list[0].trail_tail = trail->trail_tail;
2758 new_entry->trail_list[0].trail_length = finger_trail_length;
2759 new_entry->trail_list[0].trail_id = trail_id;
2762 finger_table[finger_table_index] = *new_entry;
2768 * Scan the trail to check if there is any other friend in the trail other than
2769 * first hop. If yes then shortcut the trail, send trail compression message to
2770 * peers which are no longer part of trail and send back the updated trail
2771 * and trail_length to calling function.
2772 * @param finger_identity Finger whose trail we will scan.
2773 * @param finger_trail [in, out] Trail to reach from source to finger,
2774 * @param finger_trail_length Total number of peers in original finger_trail.
2775 * @param finger_trail_id Unique identifier of the finger trail.
2776 * @return updated trail length in case we shortcut the trail, else original
2779 static struct GNUNET_PeerIdentity *
2780 scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
2781 const struct GNUNET_PeerIdentity *trail,
2782 unsigned int trail_length,
2783 struct GNUNET_HashCode trail_id,
2784 int *new_trail_length)
2786 struct FriendInfo *target_friend;
2787 struct GNUNET_PeerIdentity *new_trail;
2790 /* If I am my own finger identity, then we set trail_length = 0.
2791 Note: Here we don't send trail compression message, as no peer in its
2792 trail added an entry in its routing table.*/
2793 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
2795 *new_trail_length = 0;
2799 /* If finger identity is a friend. */
2800 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger_identity))
2802 /* If there is trail to reach this finger/friend */
2803 if (trail_length > 0)
2805 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2807 GDS_NEIGHBOURS_send_trail_compression (my_identity,
2808 trail_id, finger_identity,
2810 *new_trail_length = 0;
2815 /* For other cases, when its neither a friend nor my own identity.*/
2816 for (i = trail_length - 1; i > 0; i--)
2818 /* If the element at this index in trail is a friend. */
2819 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail[i]))
2821 struct FriendInfo *target_friend;
2824 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2826 GDS_NEIGHBOURS_send_trail_compression (my_identity,
2831 /* Copy the trail from index i to index (trail_length -1) into a new trail
2832 * and update new trail length */
2833 new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * i);
2834 while (i < trail_length)
2836 memcpy (&new_trail[j], &trail[i], sizeof(struct GNUNET_PeerIdentity));
2840 *new_trail_length = j+1;
2845 /* If we found no other friend except the first hop, return the original
2847 new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * trail_length);
2848 *new_trail_length = trail_length;
2849 memcpy (new_trail, new_trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
2855 * FIXME: Ensure that we add trail in succession in the trail list.
2856 * There are no free spots within the trail list.
2857 * Send verify successor message to your successor on all trails to reach
2859 * @param successor My current successor
2862 send_verify_successor_message (struct FingerInfo *successor)
2864 struct Trail *trail_list_iterator;
2865 struct GNUNET_HashCode trail_id;
2866 struct GNUNET_PeerIdentity next_hop;
2867 struct FriendInfo *target_friend;
2868 struct GNUNET_PeerIdentity *trail;
2869 unsigned int trail_length;
2873 for (i = 0; i < successor->trails_count; i++)
2875 trail_list_iterator = &successor->trail_list[i];
2876 GNUNET_assert (NULL != trail_list_iterator->trail_head);
2878 if (trail_list_iterator->trail_length > 0)
2880 struct Trail_Element *element;
2882 trail_length = trail_list_iterator->trail_length;
2883 trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)
2885 element = trail_list_iterator->trail_head;
2886 for (j = 0; j < trail_length; j++, element = element->next)
2887 trail[j] = element->peer;
2888 next_hop = trail_list_iterator->trail_head->peer;
2894 next_hop = successor->finger_identity;
2896 trail_id = trail_list_iterator->trail_id;
2897 GNUNET_assert (NULL != (target_friend =
2898 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
2900 GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
2901 successor->finger_identity,
2902 trail_id, trail, trail_length,
2904 GNUNET_free (trail);
2910 * FIXME" clear abstraction of current search finger index and finger map index.
2911 * it never goes to 63. I don't know why
2912 * Update the current search finger index.
2915 update_current_search_finger_index (struct GNUNET_PeerIdentity finger_identity)
2917 struct FingerInfo *successor;
2919 successor = &finger_table[0];
2920 if (GNUNET_NO == successor->is_present)
2923 /* We were looking for immediate successor. */
2924 if (0 == current_search_finger_index)
2926 /* Start looking for immediate predecessor. */
2927 current_search_finger_index = PREDECESSOR_FINGER_ID;
2929 /* If I am not my own successor, then send a verify successor message. */
2930 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
2932 send_verify_successor_message (successor);
2937 current_search_finger_index = current_search_finger_index - 1;
2943 * Calculate finger_table_index from initial 64 bit finger identity value that
2944 * we send in trail setup message.
2945 * @param ultimate_destination_finger_value Value that we calculated from our
2946 * identity and finger_table_index.
2947 * @param is_predecessor Is the entry for predecessor or not?
2948 * @return finger_table_index Value between 0 <= finger_table_index <= 64
2949 * -1, if no valid finger_table_index is found.
2952 get_finger_table_index (uint64_t ultimate_destination_finger_value,
2953 unsigned int is_predecessor)
2957 unsigned int finger_table_index;
2959 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
2960 my_id64 = GNUNET_ntohll (my_id64);
2962 /* Is this a predecessor finger? */
2963 if (1 == is_predecessor)
2965 diff = my_id64 - ultimate_destination_finger_value;
2967 finger_table_index = PREDECESSOR_FINGER_ID;
2969 finger_table_index = PREDECESSOR_FINGER_ID + 1; //error value
2974 diff = ultimate_destination_finger_value - my_id64;
2975 finger_table_index = (log10 (diff))/(log10 (2));
2978 return finger_table_index;
2983 * Remove finger and its associated data structures from finger table.
2984 * @param finger Finger to be removed.
2987 remove_existing_finger (struct FingerInfo *finger)
2989 struct FriendInfo *friend;
2991 GNUNET_assert (GNUNET_YES == finger->is_present);
2992 /* If I am my own finger, then we have no trails. */
2993 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
2996 GNUNET_free (finger);
3000 /* If finger is a friend, then decrement the trail count and free the finger. */
3001 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3002 &finger->finger_identity);
3005 friend->trails_count--;
3006 GNUNET_free (finger);
3010 /* For all other fingers, send trail teardown across all the trails to reach
3011 finger, and free the finger. */
3012 send_all_finger_trails_teardown (finger);
3013 free_finger (finger);
3019 * -- Check if there is already an entry in finger_table at finger_table_index.
3020 * We get the finger_table_index from 64bit finger value we got from the network.
3021 * -- If yes, then select the closest finger.
3022 * -- If new and existing finger are same, then check if you can store more
3024 * -- If yes then add trail, else keep the best trails to reach to the
3026 * -- If the new finger is closest, remove the existing entry, send trail
3027 * teardown message across all the trails to reach the existing entry.
3029 * -- If new and existing finger are different, and existing finger is same
3031 * Update current_search_finger_index.
3032 * @param new_finger_identity Peer Identity of new finger
3033 * @param new_finger_trail Trail to reach the new finger
3034 * @param new_finger_length Total number of peers in @a new_finger_trail.
3035 * @param is_predecessor Is this entry for predecessor in finger_peermap.
3036 * @param finger_value 64 bit value of finger identity that we got from network.
3037 * @param finger_trail_id Unique identifier of @finger_trail.
3040 finger_table_add (struct GNUNET_PeerIdentity finger_identity,
3041 const struct GNUNET_PeerIdentity *finger_trail,
3042 unsigned int finger_trail_length,
3043 unsigned int is_predecessor,
3044 uint64_t finger_value,
3045 struct GNUNET_HashCode finger_trail_id)
3047 struct FingerInfo *existing_finger;
3048 struct GNUNET_PeerIdentity *closest_peer;
3049 struct GNUNET_PeerIdentity *updated_trail;
3050 struct FingerInfo *successor;
3051 int updated_finger_trail_length;
3052 unsigned int finger_table_index;
3054 /* Get the finger_table_index corresponding to finger_value we got from network.*/
3055 finger_table_index = get_finger_table_index (finger_value, is_predecessor);
3057 /* Invalid finger_table_index. */
3058 if ((finger_table_index > PREDECESSOR_FINGER_ID) || (finger_table_index < 0))
3060 GNUNET_break_op (0);
3064 updated_finger_trail_length = finger_trail_length;
3066 scan_and_compress_trail (finger_identity, finger_trail,
3067 finger_trail_length, finger_trail_id,
3068 &updated_finger_trail_length);
3070 /* If the new entry is same as successor then don't add it in finger table,
3071 reset the current search finger index and exit. */
3072 if ((0 != finger_table_index) && (PREDECESSOR_FINGER_ID != finger_table_index))
3074 successor = &finger_table[0];
3075 GNUNET_assert (GNUNET_YES == successor->is_present);
3076 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
3077 &successor->finger_identity))
3079 current_search_finger_index = 0;
3084 existing_finger = &finger_table[finger_table_index];
3085 /* No entry present in finger_table for given finger map index. */
3086 if (GNUNET_NO == existing_finger->is_present)
3088 add_new_finger (finger_identity, updated_trail, updated_finger_trail_length,
3089 finger_trail_id, finger_table_index);
3090 update_current_search_finger_index (finger_identity);
3094 /* If existing entry and finger identity are not same. */
3095 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
3098 closest_peer = select_closest_peer (&existing_finger->finger_identity,
3100 finger_value, finger_table_index);
3102 /* If the new finger is the closest peer. */
3103 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, closest_peer))
3105 remove_existing_finger (existing_finger);
3106 add_new_finger (finger_identity, updated_trail, updated_finger_trail_length,
3107 finger_trail_id, finger_table_index);
3112 /* If both new and existing entry are same as my_identity, then do nothing. */
3113 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
3117 /* If the existing finger is not a friend. */
3119 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3120 &existing_finger->finger_identity))
3122 /* If there is space to store more trails. */
3123 if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
3124 add_new_trail (existing_finger, updated_trail,
3125 finger_trail_length, finger_trail_id);
3127 select_and_replace_trail (existing_finger, updated_trail,
3128 finger_trail_length, finger_trail_id);
3131 update_current_search_finger_index (finger_identity);
3137 * Core handler for P2P put messages.
3138 * @param cls closure
3139 * @param peer sender of the request
3140 * @param message message
3141 * @return #GNUNET_OK to keep the connection open,
3142 * #GNUNET_SYSERR to close it (signal serious error)
3145 handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
3146 const struct GNUNET_MessageHeader *message)
3148 struct PeerPutMessage *put;
3149 struct GNUNET_PeerIdentity *put_path;
3150 struct GNUNET_HashCode test_key;
3151 enum GNUNET_DHT_RouteOption options;
3152 struct GNUNET_PeerIdentity best_known_dest;
3153 struct GNUNET_HashCode intermediate_trail_id;
3154 struct GNUNET_PeerIdentity *next_hop;
3158 size_t payload_size;
3161 msize = ntohs (message->size);
3162 if (msize < sizeof (struct PeerPutMessage))
3164 GNUNET_break_op (0);
3168 put = (struct PeerPutMessage *) message;
3169 putlen = ntohl (put->put_path_length);
3172 sizeof (struct PeerPutMessage) +
3173 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3175 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3177 GNUNET_break_op (0);
3181 best_known_dest = put->best_known_destination;
3182 put_path = (struct GNUNET_PeerIdentity *) &put[1];
3183 payload = &put_path[putlen];
3184 options = ntohl (put->options);
3185 intermediate_trail_id = put->intermediate_trail_id;
3187 payload_size = msize - (sizeof (struct PeerPutMessage) +
3188 putlen * sizeof (struct GNUNET_PeerIdentity));
3190 switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
3191 payload, payload_size, &test_key))
3194 if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
3196 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
3197 GNUNET_break_op (0);
3198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3199 "PUT with key `%s' for block with key %s\n",
3200 put_s, GNUNET_h2s_full (&test_key));
3201 GNUNET_free (put_s);
3206 GNUNET_break_op (0);
3209 /* cannot verify, good luck */
3213 if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
3215 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
3216 ntohl (put->block_type),
3218 NULL, 0, /* bloom filer */
3219 NULL, 0, /* xquery */
3220 payload, payload_size))
3222 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3223 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3226 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3227 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3228 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
3229 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3230 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3231 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3233 GNUNET_break_op (0);
3238 /* extend 'put path' by sender */
3239 struct GNUNET_PeerIdentity pp[putlen + 1];
3240 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
3242 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
3249 memcpy (&key_value, &(put->key), sizeof (uint64_t));
3250 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3252 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3253 GDS_ROUTING_SRC_TO_DEST);
3257 /*FIXME: Here you should use enum GDS_NEIGHBOURS_FINGER_TYPE in place of 0. */
3258 next_hop = find_successor (key_value, &best_known_dest,
3259 &intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
3262 if (NULL == next_hop)
3264 GNUNET_STATISTICS_update (GDS_stats,
3265 gettext_noop ("# Next hop to forward the packet not found "
3266 "trail setup request, packet dropped."),
3268 return GNUNET_SYSERR;
3271 GDS_CLIENTS_process_put (options,
3272 ntohl (put->block_type),
3273 ntohl (put->hop_count),
3274 ntohl (put->desired_replication_level),
3276 GNUNET_TIME_absolute_ntoh (put->expiration_time),
3281 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, next_hop)) /* I am the final destination */
3283 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
3284 &(put->key),putlen, pp, ntohl (put->block_type),
3285 payload_size, payload);
3290 GDS_NEIGHBOURS_send_put (&put->key,
3291 ntohl (put->block_type),ntohl (put->options),
3292 ntohl (put->desired_replication_level),
3293 &best_known_dest, &intermediate_trail_id, next_hop,
3294 ntohl (put->hop_count), putlen, pp,
3295 GNUNET_TIME_absolute_ntoh (put->expiration_time),
3296 payload, payload_size);
3300 return GNUNET_SYSERR;
3305 * Core handler for p2p get requests.
3307 * @param cls closure
3308 * @param peer sender of the request
3309 * @param message message
3310 * @return #GNUNET_OK to keep the connection open,
3311 * #GNUNET_SYSERR to close it (signal serious error)
3314 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
3315 const struct GNUNET_MessageHeader *message)
3317 struct PeerGetMessage *get;
3318 struct GNUNET_PeerIdentity *get_path;
3319 struct GNUNET_PeerIdentity best_known_dest;
3320 struct GNUNET_HashCode intermediate_trail_id;
3321 struct GNUNET_PeerIdentity *next_hop;
3322 uint32_t get_length;
3326 msize = ntohs (message->size);
3327 if (msize < sizeof (struct PeerGetMessage))
3329 GNUNET_break_op (0);
3333 get = (struct PeerGetMessage *)message;
3334 get_length = ntohl (get->get_path_length);
3335 best_known_dest = get->best_known_destination;
3336 intermediate_trail_id = get->intermediate_trail_id;
3337 get_path = (struct GNUNET_PeerIdentity *)&get[1];
3340 sizeof (struct PeerGetMessage) +
3341 get_length * sizeof (struct GNUNET_PeerIdentity)) ||
3343 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3345 GNUNET_break_op (0);
3349 /* Add sender to get path */
3350 struct GNUNET_PeerIdentity gp[get_length + 1];
3351 memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
3352 gp[get_length + 1] = *peer;
3353 get_length = get_length + 1;
3355 memcpy (&key_value, &(get->key), sizeof (uint64_t));
3356 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3358 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3359 GDS_ROUTING_SRC_TO_DEST);
3363 /*FIXME: Here you should use enum GDS_NEIGHBOURS_FINGER_TYPE in place of 0. */
3364 next_hop = find_successor (key_value, &best_known_dest,
3365 &intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
3368 if (NULL == next_hop)
3370 GNUNET_STATISTICS_update (GDS_stats,
3371 gettext_noop ("# Next hop to forward the packet not found "
3372 "trail setup request, packet dropped."),
3374 return GNUNET_SYSERR;
3376 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, next_hop))
3378 /* I am the destination.*/
3379 struct GNUNET_PeerIdentity final_get_path[get_length+1];
3380 struct GNUNET_PeerIdentity next_hop;
3382 memcpy (final_get_path, gp, get_length * sizeof (struct GNUNET_PeerIdentity));
3383 memcpy (&final_get_path[get_length+1], &my_identity, sizeof (struct GNUNET_PeerIdentity));
3384 get_length = get_length + 1;
3385 memcpy (&next_hop, &final_get_path[get_length-2], sizeof (struct GNUNET_PeerIdentity));
3386 GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
3387 get_length, final_get_path,&next_hop, &my_identity);
3393 GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
3394 get->desired_replication_level, &best_known_dest,
3395 &intermediate_trail_id, next_hop, 0,
3398 return GNUNET_SYSERR;
3403 * Core handler for get result
3404 * @param cls closure
3405 * @param peer sender of the request
3406 * @param message message
3407 * @return #GNUNET_OK to keep the connection open,
3408 * #GNUNET_SYSERR to close it (signal serious error)
3411 handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
3412 const struct GNUNET_MessageHeader *message)
3414 struct PeerGetResultMessage *get_result;
3415 struct GNUNET_PeerIdentity *get_path;
3416 struct GNUNET_PeerIdentity *put_path;
3418 size_t payload_size;
3420 unsigned int getlen;
3421 unsigned int putlen;
3422 int current_path_index;
3424 msize = ntohs (message->size);
3425 if (msize < sizeof (struct PeerGetResultMessage))
3427 GNUNET_break_op (0);
3431 get_result = (struct PeerGetResultMessage *)message;
3432 getlen = ntohl (get_result->get_path_length);
3433 putlen = ntohl (get_result->put_path_length);
3436 sizeof (struct PeerGetResultMessage) +
3437 getlen * sizeof (struct GNUNET_PeerIdentity) +
3438 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3440 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity) ||
3442 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))))
3444 GNUNET_break_op (0);
3449 get_path = (struct GNUNET_PeerIdentity *) &get_result[1];
3450 payload = &get_path[getlen];
3451 payload_size = msize - (sizeof (struct PeerGetResultMessage) +
3452 getlen * sizeof (struct GNUNET_PeerIdentity));
3455 put_path = &get_path[1];
3459 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
3461 GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
3462 getlen, get_path, putlen,
3463 put_path, get_result->type, payload_size, payload);
3468 current_path_index = search_my_index (get_path, getlen);
3469 if (GNUNET_SYSERR == current_path_index )
3472 return GNUNET_SYSERR;
3474 GDS_NEIGHBOURS_send_get_result (&(get_result->key), get_result->type,
3475 &get_path[current_path_index - 1],
3476 &(get_result->querying_peer), putlen, put_path,
3477 getlen, get_path, get_result->expiration_time,
3478 payload, payload_size);
3481 return GNUNET_SYSERR;
3486 * Get the best known next destination (local_dest) among your fingers, friends
3487 * and my identity. If @a current_dest is some other peer and not me, then
3488 * compare curent_dest and local_dest.
3489 * @param final_dest_finger_value Peer closest to this value will be
3490 * @a local_best_known_dest
3491 * @param local_best_known_dest[out] Updated to peer identity which is closest to
3492 * @a final_dest_finger_value.
3493 * @param new_intermediate_trail_id In case @a local_best_known_dest is a finger,
3494 * then the trail id to reach to the finger
3495 * @param is_predecessor Is source peer trying to setup trail to its predecessor
3497 * @param current_dest Peer which should get this message ultimately according
3498 * to the peer which sent me this message. It could be me
3499 * or some other peer. In case it is not me, then I am a part
3500 * of trail to reach to that peer.
3503 static struct GNUNET_PeerIdentity *
3504 get_local_best_known_next_hop (uint64_t final_dest_finger_value,
3505 struct GNUNET_PeerIdentity *local_best_known_dest,
3506 struct GNUNET_HashCode *new_intermediate_trail_id,
3507 struct GNUNET_HashCode intermediate_trail_id,
3508 unsigned int is_predecessor,
3509 struct GNUNET_PeerIdentity *current_dest)
3511 struct GNUNET_PeerIdentity *next_hop_to_local_best_known_dest;
3513 /* Choose a local best known hop among your fingers, friends and you. */
3514 next_hop_to_local_best_known_dest = find_successor (final_dest_finger_value,
3515 local_best_known_dest,
3516 new_intermediate_trail_id,
3519 /* Are we just a part of a trail towards a finger (current_destination)? */
3520 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest)))
3522 struct GNUNET_PeerIdentity *closest_peer;
3524 /* Select best successor among one found locally and current_destination
3525 * that we got from network.*/
3526 closest_peer = select_closest_peer (local_best_known_dest,
3528 final_dest_finger_value,
3531 /* Is current dest (end point of the trail of which I am a part) closest_peer? */
3532 if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, closest_peer))
3534 next_hop_to_local_best_known_dest =
3535 GDS_ROUTING_get_next_hop (intermediate_trail_id,
3536 GDS_ROUTING_SRC_TO_DEST);
3537 /* FIXME: Here we found next_hop NULL from routing table, but we still
3538 * have a next_hop from find_successor should we not break and choose that
3540 if (NULL == next_hop_to_local_best_known_dest)
3542 GNUNET_break_op (0);
3546 local_best_known_dest = current_dest;
3547 *new_intermediate_trail_id = intermediate_trail_id;
3551 GNUNET_assert (NULL != next_hop_to_local_best_known_dest);
3552 return next_hop_to_local_best_known_dest;
3556 /* Core handle for PeerTrailSetupMessage.
3557 * @param cls closure
3558 * @param message message
3559 * @param peer peer identity this notification is about
3560 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
3563 handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
3564 const struct GNUNET_MessageHeader *message)
3566 const struct PeerTrailSetupMessage *trail_setup;
3567 const struct GNUNET_PeerIdentity *trail_peer_list;
3568 struct GNUNET_PeerIdentity *local_best_known_dest;
3569 struct GNUNET_PeerIdentity current_dest;
3570 struct GNUNET_PeerIdentity *next_hop_towards_local_best_known_dest;
3571 struct GNUNET_PeerIdentity next_peer;
3572 struct FriendInfo *target_friend;
3573 struct GNUNET_PeerIdentity source;
3574 uint64_t final_dest_finger_val;
3575 struct GNUNET_HashCode new_intermediate_trail_id;
3576 struct GNUNET_HashCode intermediate_trail_id;
3577 struct GNUNET_HashCode trail_id;
3578 unsigned int is_predecessor;
3579 uint32_t trail_length;
3582 msize = ntohs (message->size);
3583 if (msize < sizeof (struct PeerTrailSetupMessage))
3585 GNUNET_break_op (0);
3589 trail_setup = (const struct PeerTrailSetupMessage *) message;
3590 trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
3591 sizeof (struct GNUNET_PeerIdentity);
3592 if ((msize - sizeof (struct PeerTrailSetupMessage)) %
3593 sizeof (struct GNUNET_PeerIdentity) != 0)
3595 GNUNET_break_op (0);
3599 trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_setup[1];
3600 current_dest = trail_setup->best_known_destination;
3601 trail_id = trail_setup->trail_id;
3602 final_dest_finger_val =
3603 GNUNET_ntohll (trail_setup->final_destination_finger_value);
3604 source = trail_setup->source_peer;
3605 is_predecessor = ntohl (trail_setup->is_predecessor);
3606 intermediate_trail_id = trail_setup->intermediate_trail_id;
3608 /* Is my routing table full? */
3609 if (GNUNET_YES == GDS_ROUTING_threshold_reached())
3611 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
3612 GDS_NEIGHBOURS_send_trail_rejection (source, final_dest_finger_val,
3613 my_identity, is_predecessor,
3614 trail_peer_list, trail_length,
3615 trail_id, target_friend,
3616 CONGESTION_TIMEOUT);
3620 local_best_known_dest = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
3621 /* Get the next hop to forward the trail setup request. */
3622 next_hop_towards_local_best_known_dest =
3623 get_local_best_known_next_hop (final_dest_finger_val,
3624 local_best_known_dest,
3625 &new_intermediate_trail_id,
3626 intermediate_trail_id,
3630 /* Am I the final destination? */
3631 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (local_best_known_dest,
3634 /* If I was not the source of this message for which now I am destination.*/
3635 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
3637 GDS_ROUTING_add (trail_id, *peer, my_identity);
3639 if (0 == trail_length)
3640 memcpy (&next_peer, &source, sizeof (struct GNUNET_PeerIdentity));
3642 memcpy (&next_peer, &trail_peer_list[trail_length-1], sizeof (struct GNUNET_PeerIdentity));
3644 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
3645 GDS_NEIGHBOURS_send_trail_setup_result (source,
3647 target_friend, trail_length,
3649 final_dest_finger_val,
3650 is_predecessor, trail_id);
3654 /* Add yourself to list of peers. */
3655 struct GNUNET_PeerIdentity peer_list[trail_length + 1];
3657 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
3658 peer_list[trail_length] = my_identity;
3660 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3661 next_hop_towards_local_best_known_dest);
3662 GDS_NEIGHBOURS_send_trail_setup (source,
3663 final_dest_finger_val,
3664 *local_best_known_dest,
3665 target_friend, trail_length + 1, peer_list,
3666 is_predecessor, trail_id,
3667 &new_intermediate_trail_id);
3673 /* FIXME: here we are calculating my_index and comparing also in this function.
3674 And we are doing it again here in this function. Re factor the code. */
3676 * Check if sender_peer and peer from which we should receive the message are
3677 * same or different.
3678 * @param trail_peer_list List of peers in trail
3679 * @param trail_length Total number of peers in @a trail_peer_list
3680 * @param sender_peer Peer from which we got the message.
3681 * @param finger_identity Finger to which trail is setup. It is not part of trail.
3682 * @return #GNUNET_YES if sender_peer and peer from which we should receive the
3683 * message are different.
3684 * #GNUNET_NO if sender_peer and peer from which we should receive the
3685 * message are different.
3688 is_sender_peer_correct (const struct GNUNET_PeerIdentity *trail_peer_list,
3689 unsigned int trail_length,
3690 const struct GNUNET_PeerIdentity *sender_peer,
3691 struct GNUNET_PeerIdentity finger_identity,
3692 struct GNUNET_PeerIdentity source_peer)
3696 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
3699 if (trail_length > 0)
3701 // source, then trail_length > 0, trail_peer_list[0] != peer
3702 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[0],
3708 // source, trail_length == 0, finger != peer
3709 if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
3716 my_index = search_my_index (trail_peer_list, trail_length);
3720 // my_index == trail_length -1, finger != peer
3721 if ((trail_length - 1) == my_index)
3723 if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
3729 // FIXME: if trail_peer_list[my_index + 1] != peer
3730 if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
3731 &trail_peer_list[my_index + 1]))
3740 * FIXME: we have a new field is next_hop desintation or prev_hop source.
3741 * think how to add it. I am not adding any entry in destination or source
3742 * peer routing table as in case of handle core disconnect when we remove
3743 * an entry from routing table then we send a trail teardown message and
3744 * I am not aware about source or dest. So. we can't send dest as end point.
3745 * Core handle for p2p trail setup result messages.
3747 * @param message message
3748 * @param peer sender of this message.
3749 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
3752 handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
3753 const struct GNUNET_MessageHeader *message)
3755 const struct PeerTrailSetupResultMessage *trail_result;
3756 const struct GNUNET_PeerIdentity *trail_peer_list;
3757 struct GNUNET_PeerIdentity next_hop;
3758 struct FriendInfo *target_friend;
3759 struct GNUNET_PeerIdentity querying_peer;
3760 struct GNUNET_PeerIdentity finger_identity;
3761 uint32_t trail_length;
3762 uint64_t ulitmate_destination_finger_value;
3763 uint32_t is_predecessor;
3764 struct GNUNET_HashCode trail_id;
3768 msize = ntohs (message->size);
3769 if (msize < sizeof (struct PeerTrailSetupResultMessage))
3771 GNUNET_break_op (0);
3775 trail_result = (const struct PeerTrailSetupResultMessage *) message;
3776 trail_length = (msize - sizeof (struct PeerTrailSetupResultMessage))/
3777 sizeof (struct GNUNET_PeerIdentity);
3778 if ((msize - sizeof (struct PeerTrailSetupResultMessage)) %
3779 sizeof (struct GNUNET_PeerIdentity) != 0)
3781 GNUNET_break_op (0);
3785 is_predecessor = htonl (trail_result->is_predecessor);
3786 querying_peer = trail_result->querying_peer;
3787 finger_identity = trail_result->finger_identity;
3788 trail_id = trail_result->trail_id;
3789 trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
3790 ulitmate_destination_finger_value =
3791 GNUNET_ntohll (trail_result->ulitmate_destination_finger_value);
3793 /* FIXME: here we are calculating my_index and comparing also in this function.
3794 And we are doing it again here in this function. Re factor the code. */
3795 /* Ensure that sender peer is the peer from which we were expecting the message. */
3796 if (GNUNET_NO == is_sender_peer_correct (trail_peer_list,
3798 peer, finger_identity, querying_peer))
3800 GNUNET_break_op (0);
3801 return GNUNET_SYSERR;
3804 /* Am I the one who initiated the query? */
3805 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer,
3808 /* If I am not my own finger identity.*/
3809 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
3811 GDS_ROUTING_add (trail_id, my_identity, *peer);
3813 finger_table_add (finger_identity, trail_peer_list,
3814 trail_length, ulitmate_destination_finger_value,
3815 is_predecessor, trail_id);
3819 my_index = search_my_index(trail_peer_list, trail_length);
3823 return GNUNET_SYSERR;
3827 next_hop = trail_result->querying_peer;
3829 next_hop = trail_peer_list[my_index - 1];
3831 /* If the querying_peer is its own finger, then don't add an entry in routing
3832 * table as querying peer will discard the trail.
3834 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->querying_peer),
3835 &(trail_result->finger_identity))))
3837 GDS_ROUTING_add (trail_id, next_hop, *peer);
3840 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
3841 GDS_NEIGHBOURS_send_trail_setup_result (querying_peer, finger_identity,
3842 target_friend, trail_length, trail_peer_list,
3844 ulitmate_destination_finger_value,
3852 * @param trail Trail to be inverted
3853 * @param trail_length Total number of peers in the trail.
3854 * @return Updated trail
3856 static struct GNUNET_PeerIdentity *
3857 invert_trail (const struct GNUNET_PeerIdentity *trail,
3858 unsigned int trail_length)
3862 struct GNUNET_PeerIdentity *inverted_trail;
3864 inverted_trail = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
3867 j = trail_length - 1;
3868 while (i < trail_length)
3870 inverted_trail[i] = trail[j];
3874 return inverted_trail;
3880 * my_current_predecessor != source_peer. get the trail to reach to
3881 * my_current_predecessor and append it to the trail from source to me.
3882 * It can contain duplicate elements. Need to get the correct one.
3883 * In case the source peer of verify successor message is not my successor,
3884 * then construct a trail from source peer to my current predecessor.
3885 * @param my_predecessor my current predecessor.
3886 * @param current_trail Trail from source to me.
3887 * @param current_trail_length Total number of peers in @a current_trail
3888 * @param new_trail_length [out] Total number of peers in updated trail.
3889 * @return Updated trail from source peer to my_predecessor.
3891 static struct GNUNET_PeerIdentity *
3892 trail_source_to_my_predecessor (const struct GNUNET_PeerIdentity *current_trail,
3893 unsigned int current_trail_length,
3894 unsigned int *new_trail_length)
3896 struct GNUNET_PeerIdentity *new_trail;
3897 struct Trail *trail_list_iterator;
3898 struct Trail_Element *trail_iterator;
3899 struct FingerInfo *my_predecessor;
3902 unsigned int shortest_trail_length = 0;
3903 unsigned int trail_index = 0;
3905 my_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
3907 for (i = 0; i < my_predecessor->trails_count; i++)
3909 trail_list_iterator = &my_predecessor->trail_list[i];
3910 if (trail_list_iterator->trail_length > shortest_trail_length)
3912 shortest_trail_length = trail_list_iterator->trail_length;
3916 *new_trail_length = current_trail_length + shortest_trail_length + 1;
3917 new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
3919 memcpy (new_trail, current_trail,
3920 current_trail_length * sizeof (struct GNUNET_PeerIdentity));
3921 new_trail[current_trail_length + 1] = my_identity;
3924 j = current_trail_length + 1;
3925 trail_list_iterator = &my_predecessor->trail_list[trail_index];
3926 trail_iterator = trail_list_iterator->trail_head;
3927 while ( i < shortest_trail_length)
3929 new_trail[j] = trail_iterator->peer;
3932 trail_iterator = trail_iterator->next;
3935 *new_trail_length = j;
3941 * Add finger as your predecessor. To add, first generate a new trail id, invert
3942 * the trail to get the trail from me to finger, add an entry in your routing
3943 * table, send add trail message to peers which are part of trail from me to
3944 * finger and add finger in finger table.
3947 * @param trail_length
3950 update_predecessor (struct GNUNET_PeerIdentity *finger,
3951 struct GNUNET_PeerIdentity *trail,
3952 unsigned int trail_length)
3954 struct GNUNET_HashCode *trail_to_new_predecessor_id;
3955 struct GNUNET_PeerIdentity *trail_to_new_predecessor;
3956 struct FriendInfo *target_friend;
3958 /* Generate trail id for trail from me to new predecessor = finger. */
3959 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
3960 &trail_to_new_predecessor_id,
3961 sizeof (trail_to_new_predecessor_id));
3963 /* Invert the trail from finger to me to get the trail from me to finger. */
3964 trail_to_new_predecessor = invert_trail (trail, trail_length);
3965 if (trail_length > 0)
3967 /* Add an entry in your routing table. */
3968 GDS_ROUTING_add (*trail_to_new_predecessor_id,
3969 trail_to_new_predecessor[trail_length - 1],
3973 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3974 &trail_to_new_predecessor[trail_length - 1]);
3976 /* Add entry in routing table of all peers that are part of trail from me
3978 GDS_NEIGHBOURS_send_add_trail (my_identity,
3980 *trail_to_new_predecessor_id,
3981 trail_to_new_predecessor,
3985 add_new_finger (*finger, trail_to_new_predecessor, trail_length,
3986 *trail_to_new_predecessor_id, PREDECESSOR_FINGER_ID);
3990 /* 3. In case you are successor, then
3991 * 3.1 check if you have a predecessor
3992 * 3.2 if no predecessor, then add the source of this message as your
3993 * predecessor. To add, first you should generate a new trail id,
3994 * invert the trail, send add trail message across new trail, add
3995 * an entry in finger table. Now, destination also have routing
3996 * table entry so add in your routing table also.
3997 * 3.3 If its closer than existing one, then do everything in step 1 and
3998 * free existing finger.
3999 * 3.3 If its same as the old one, then do nothing.
4000 * 3.4 if its not same as old one, and between source and old one, old one
4001 * is the correct predecessor, then construct a trail from source
4002 * to your old successor. scan the trail to remove duplicate entries.
4003 * 4. send verify successor result, with trail id of trail from source to
4004 * me. And also send the new trail from source to reach to its probable
4007 * 1. this function is called from both verify and notify.
4008 * 2. so write in a way that it is used in both.
4011 * Check if you have a predecessor.
4012 * 1. if no predecessor, then add finger as your predecessor. To add, first
4013 * generate a new trail id, invert the trail to get the trail from me to finger,
4014 * add an entry in your routing table, send add trail message to peers which
4015 * are part of trail from me to finger and add finger in finger table.
4016 * 2. If there is a predecessor, then compare existing one and finger.
4017 * 2.1 If finger is correct predecessor, then remove current_predecessor. And
4018 * do everything in step 1 to add finger into finger table.
4019 * 2.2 If current_predecessor is correct predecessor, the construct a trail from
4020 * finger to current_predecessor.
4023 * @param trail_length
4027 compare_and_update_predecessor (struct GNUNET_PeerIdentity *finger,
4028 struct GNUNET_PeerIdentity *trail,
4029 unsigned int trail_length)
4031 struct FingerInfo *current_predecessor;
4032 struct GNUNET_PeerIdentity *closest_peer;
4033 uint64_t predecessor_value;
4035 current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
4037 /* No predecessor. Add finger as your predecessor. */
4038 if (NULL == current_predecessor) /*FIXME: is it correct value to check if there is no predecessor. */
4040 update_predecessor (finger, trail, trail_length);
4044 predecessor_value = compute_finger_identity_value (PREDECESSOR_FINGER_ID);
4045 closest_peer = select_closest_peer (finger,
4046 ¤t_predecessor->finger_identity,
4047 predecessor_value, PREDECESSOR_FINGER_ID);
4049 /* Finger is the closest predecessor. Remove the existing one and add the new
4051 if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, finger))
4053 remove_existing_finger (current_predecessor);
4054 update_predecessor (finger, trail, trail_length);
4060 /* Core handle for p2p verify successor messages.
4061 * @param cls closure
4062 * @param message message
4063 * @param peer peer identity this notification is about
4064 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4067 handle_dht_p2p_verify_successor(void *cls,
4068 const struct GNUNET_PeerIdentity *peer,
4069 const struct GNUNET_MessageHeader *message)
4072 * 1. check if you are the successor or not.
4073 * 2. if not then get the next hop from routing table, and pass the message,
4074 * 3. In case you are successor, then
4075 * 3.1 check if you have a predecessor
4076 * 3.2 if no predecessor, then add the source of this message as your
4077 * predecessor. To add, first you should generate a new trail id,
4078 * invert the trail, send add trail message across new trail, add
4079 * an entry in finger table. Now, destination also have routing
4080 * table entry so add in your routing table also.
4081 * 3.3 If its closer than existing one, then do everything in step 1 and
4082 * free existing finger.
4083 * 3.3 If its same as the old one, then do nothing.
4084 * 3.4 if its not same as old one, and between source and old one, old one
4085 * is the correct predecessor, then construct a trail from source
4086 * to your old successor. scan the trail to remove duplicate entries.
4087 * 4. send verify successor result, with trail id of trail from source to
4088 * me. And also send the new trail from source to reach to its probable
4091 const struct PeerVerifySuccessorMessage *vsm;
4092 struct GNUNET_HashCode trail_id;
4093 struct GNUNET_PeerIdentity successor;
4094 struct GNUNET_PeerIdentity source_peer;
4095 struct GNUNET_PeerIdentity *trail;
4096 struct GNUNET_PeerIdentity *next_hop;
4097 struct GNUNET_PeerIdentity *new_trail;
4098 struct FingerInfo *current_predecessor;
4099 struct FriendInfo *target_friend;
4100 unsigned int new_trail_length;
4102 unsigned int trail_length;
4104 msize = ntohs (message->size);
4105 if (msize != sizeof (struct PeerVerifySuccessorMessage))
4107 GNUNET_break_op (0);
4111 vsm = (const struct PeerVerifySuccessorMessage *) message;
4112 trail_length = (msize - sizeof (struct PeerVerifySuccessorMessage))/
4113 sizeof (struct GNUNET_PeerIdentity);
4114 if ((msize - sizeof (struct PeerVerifySuccessorMessage)) %
4115 sizeof (struct GNUNET_PeerIdentity) != 0)
4117 GNUNET_break_op (0);
4121 trail = (struct GNUNET_PeerIdentity *)&vsm[1];
4122 source_peer = vsm->source_peer;
4123 successor = vsm->successor;
4124 trail_id = vsm->trail_id;
4126 /* I am not the successor of source_peer. Pass the message to next_hop on
4128 if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
4130 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
4131 if (NULL == next_hop)
4134 return GNUNET_SYSERR;
4136 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4137 GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
4138 trail_id, trail, trail_length,
4143 /* I am the successor of this message. */
4145 compare_and_update_predecessor (&source_peer, trail, trail_length);
4147 current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
4148 /* Is source of this message my predecessor. */
4149 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (¤t_predecessor->finger_identity,
4153 new_trail_length = 0;
4157 /* Get the path from source to my predecessor. This predecessor can be
4158 source's successor. */
4160 new_trail = trail_source_to_my_predecessor (trail, trail_length,
4164 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
4165 GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
4166 current_predecessor->finger_identity,
4167 trail_id, new_trail,
4169 GDS_ROUTING_DEST_TO_SRC,
4175 /* Core handle for p2p verify successor result messages.
4176 * @param cls closure
4177 * @param message message
4178 * @param peer peer identity this notification is about
4179 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4182 handle_dht_p2p_verify_successor_result(void *cls,
4183 const struct GNUNET_PeerIdentity *peer,
4184 const struct GNUNET_MessageHeader *message)
4187 * 1. If you are not the querying peer then pass on the message,
4188 * 2, If you are querying peer, then
4189 * 2,1 is new successor same as old one
4190 * 2,1,1 if yes then do noting
4191 * 2,1,2 if no then you need to notify the new one about your existence,
4192 * 2.1.2,1 also you need to remove the older predecessor, remove entry
4193 * from finger table, send trail teardown message,
4194 * call notify new successor with new trail id and new trail to reach it.
4196 const struct PeerVerifySuccessorResultMessage *vsrm;
4197 enum GDS_ROUTING_trail_direction trail_direction;
4198 struct GNUNET_PeerIdentity querying_peer;
4199 struct GNUNET_HashCode trail_id;
4200 struct GNUNET_PeerIdentity *next_hop;
4201 struct FriendInfo *target_friend;
4202 struct GNUNET_PeerIdentity current_predecessor;
4203 struct GNUNET_PeerIdentity *trail;
4204 unsigned int trail_length;
4207 msize = ntohs (message->size);
4208 if (msize != sizeof (struct PeerVerifySuccessorResultMessage))
4210 GNUNET_break_op (0);
4214 vsrm = (struct PeerVerifySuccessorResultMessage *) message;
4215 trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
4216 sizeof (struct GNUNET_PeerIdentity);
4217 if ((msize - sizeof (struct PeerTrailSetupMessage)) %
4218 sizeof (struct GNUNET_PeerIdentity) != 0)
4220 GNUNET_break_op (0);
4224 trail = (struct GNUNET_PeerIdentity *) &vsrm[1];
4225 querying_peer = vsrm->querying_peer;
4226 trail_direction = ntohl (vsrm->trail_direction);
4227 trail_id = vsrm->trail_id;
4228 current_predecessor = vsrm->current_predecessor;
4230 /* I am the querying_peer. */
4231 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
4233 //Fixme: you check if successor is same of different. if differentthen
4234 // send notify new successor. in that function we will add in trail. scan
4235 // and compress the trail too.
4238 /*If you are not the querying peer then pass on the message */
4239 GNUNET_assert (NULL != (next_hop =
4240 GDS_ROUTING_get_next_hop (trail_id, trail_direction)));
4241 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4242 GDS_NEIGHBOURS_send_verify_successor_result (querying_peer,
4243 vsrm->source_successor,
4244 current_predecessor, trail_id,
4247 trail_direction, target_friend);
4253 * Core handle for p2p notify new successor messages.
4254 * @param cls closure
4255 * @param message message
4256 * @param peer peer identity this notification is about
4257 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4260 handle_dht_p2p_notify_new_successor(void *cls,
4261 const struct GNUNET_PeerIdentity *peer,
4262 const struct GNUNET_MessageHeader *message)
4264 const struct PeerNotifyNewSuccessorMessage *nsm;
4265 struct GNUNET_PeerIdentity *trail;
4266 struct GNUNET_PeerIdentity source;
4267 struct GNUNET_PeerIdentity new_successor;
4268 struct GNUNET_HashCode trail_id;
4269 struct GNUNET_PeerIdentity next_hop;
4270 struct FriendInfo *target_friend;
4273 uint32_t trail_length;
4275 msize = ntohs (message->size);
4276 if (msize != sizeof (struct PeerNotifyNewSuccessorMessage))
4278 GNUNET_break_op (0);
4282 nsm = (struct PeerNotifyNewSuccessorMessage *) message;
4283 trail_length = (msize - sizeof (struct PeerNotifyNewSuccessorMessage))/
4284 sizeof (struct GNUNET_PeerIdentity);
4285 if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
4286 sizeof (struct GNUNET_PeerIdentity) != 0)
4288 GNUNET_break_op (0);
4292 trail = (struct GNUNET_PeerIdentity *) &nsm[1];
4293 source = nsm->source_peer;
4294 new_successor = nsm->new_successor;
4295 trail_id = nsm->trail_id;
4299 /* I am the new_successor to source_peer. */
4300 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_successor))
4302 /* Add an entry in routing table. */
4303 GDS_ROUTING_add (trail_id, *peer, my_identity);
4304 compare_and_update_predecessor (&source, trail, trail_length);
4308 /* I am part of trail to reach to successor. */
4309 my_index = search_my_index (trail, trail_length);
4312 GNUNET_break_op (0);
4313 return GNUNET_SYSERR;
4315 if (trail_length == my_index)
4316 next_hop = new_successor;
4318 next_hop = trail[my_index + 1];
4320 /* Add an entry in routing table for trail from source to its new successor. */
4321 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
4322 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
4323 GDS_NEIGHBOURS_send_notify_new_successor (source, new_successor, trail,
4325 trail_id, target_friend);
4332 * FIXME: Here you should keep the trail id with you.
4333 * Core handler for P2P trail rejection message
4334 * @param cls closure
4335 * @param message message
4336 * @param peer peer identity this notification is about
4337 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4340 handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer,
4341 const struct GNUNET_MessageHeader *message)
4343 struct PeerTrailRejectionMessage *trail_rejection;
4344 unsigned int trail_length;
4345 struct GNUNET_PeerIdentity *trail_peer_list;
4346 struct FriendInfo *target_friend;
4347 struct GNUNET_TIME_Relative congestion_timeout;
4348 struct GNUNET_HashCode trail_id;
4349 struct GNUNET_PeerIdentity next_destination;
4350 struct GNUNET_HashCode new_intermediate_trail_id;
4351 struct GNUNET_PeerIdentity next_peer;
4352 struct GNUNET_PeerIdentity source;
4353 struct GNUNET_PeerIdentity *next_hop;
4354 uint64_t ultimate_destination_finger_value;
4355 unsigned int is_predecessor;
4358 msize = ntohs (message->size);
4359 if (msize != sizeof (struct PeerTrailRejectionMessage))
4361 GNUNET_break_op (0);
4365 trail_rejection = (struct PeerTrailRejectionMessage *) message;
4366 trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
4367 sizeof (struct GNUNET_PeerIdentity);
4368 if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
4369 sizeof (struct GNUNET_PeerIdentity) != 0)
4371 GNUNET_break_op (0);
4375 trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_rejection[1];
4376 is_predecessor = ntohl (trail_rejection->is_predecessor);
4377 congestion_timeout = trail_rejection->congestion_time;
4378 source = trail_rejection->source_peer;
4379 trail_id = trail_rejection->trail_id;
4380 ultimate_destination_finger_value =
4381 trail_rejection->ultimate_destination_finger_value;
4383 /* First set the congestion time of the friend that sent you this message. */
4384 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
4385 target_friend->congestion_timestamp = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
4386 congestion_timeout);
4388 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
4393 /* If I am congested then pass this message to peer before me in trail. */
4394 if(GNUNET_YES == GDS_ROUTING_threshold_reached())
4396 struct GNUNET_PeerIdentity *new_trail;
4397 unsigned int new_trail_length;
4399 if (trail_length == 1)
4402 new_trail_length = 0;
4407 next_hop = &trail_peer_list[trail_length - 2];
4408 /* Remove myself from the trail. */
4409 new_trail_length = trail_length -1;
4410 new_trail = GNUNET_malloc (new_trail_length * sizeof (struct GNUNET_PeerIdentity));
4411 memcpy (new_trail, trail_peer_list, new_trail_length * sizeof (struct GNUNET_PeerIdentity));
4414 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4415 GDS_NEIGHBOURS_send_trail_rejection (source,
4416 ultimate_destination_finger_value,
4417 my_identity, is_predecessor,
4418 new_trail,new_trail_length,trail_id,
4419 target_friend, CONGESTION_TIMEOUT);
4423 /* Look for next_hop to pass the trail setup message */
4424 next_hop = find_successor (ultimate_destination_finger_value,
4426 &new_intermediate_trail_id,
4429 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This means I am the final destination */
4431 if (0 == trail_length)
4434 next_peer = trail_peer_list[trail_length-1];
4436 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
4437 GDS_NEIGHBOURS_send_trail_setup_result (source,
4439 target_friend, trail_length,
4442 ultimate_destination_finger_value,
4447 struct GNUNET_PeerIdentity peer_list[trail_length + 1];
4449 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
4450 peer_list[trail_length] = my_identity;
4452 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4453 GDS_NEIGHBOURS_send_trail_setup (source,
4454 ultimate_destination_finger_value,
4456 target_friend, trail_length + 1, peer_list,
4457 is_predecessor, trail_id,
4458 &new_intermediate_trail_id);
4465 * Core handle for p2p trail tear down messages.
4466 * @param cls closure
4467 * @param message message
4468 * @param peer peer identity this notification is about
4469 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4472 handle_dht_p2p_trail_compression (void *cls, const struct GNUNET_PeerIdentity *peer,
4473 const struct GNUNET_MessageHeader *message)
4475 const struct PeerTrailCompressionMessage *trail_compression;
4476 struct GNUNET_PeerIdentity *next_hop;
4477 struct FriendInfo *target_friend;
4478 struct GNUNET_HashCode trail_id;
4481 msize = ntohs (message->size);
4482 if (msize != sizeof (struct PeerTrailCompressionMessage))
4484 GNUNET_break_op (0);
4488 trail_compression = (const struct PeerTrailCompressionMessage *) message;
4489 trail_id = trail_compression->trail_id;
4491 /* Am I the new first friend to reach to finger of this trail. */
4492 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_compression->new_first_friend),
4495 GDS_ROUTING_update_trail_prev_hop (trail_id,
4496 trail_compression->source_peer);
4500 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
4501 if (NULL == next_hop)
4507 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
4508 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4509 GDS_NEIGHBOURS_send_trail_compression (trail_compression->source_peer,
4511 trail_compression->new_first_friend,
4518 * Core handler for trail teardown message.
4519 * @param cls closure
4520 * @param message message
4521 * @param peer sender of this messsage.
4522 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4525 handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer,
4526 const struct GNUNET_MessageHeader *message)
4528 const struct PeerTrailTearDownMessage *trail_teardown;
4529 enum GDS_ROUTING_trail_direction trail_direction;
4530 struct GNUNET_HashCode trail_id;
4531 struct GNUNET_PeerIdentity *next_hop;
4534 msize = ntohs (message->size);
4535 if (msize != sizeof (struct PeerTrailTearDownMessage))
4537 GNUNET_break_op (0);
4541 trail_teardown = (const struct PeerTrailTearDownMessage *) message;
4542 trail_direction = ntohl (trail_teardown->trail_direction);
4543 trail_id = trail_teardown->TRAIL_ID;
4546 next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
4547 if (NULL == next_hop)
4550 return GNUNET_SYSERR;
4553 GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
4555 if (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))
4558 GDS_NEIGHBOURS_send_trail_teardown (trail_id, trail_direction, next_hop);
4564 * Fixme: this function is called only in case in notify new successor, the new
4565 * successor wants to add the source of the peer as its predecessor. Identify
4566 * if there is any other use case where it is required and if yes then adapt the
4568 * Core handle for p2p add trail message.
4569 * @param cls closure
4570 * @param message message
4571 * @param peer peer identity this notification is about
4572 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4575 handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
4576 const struct GNUNET_MessageHeader *message)
4578 struct PeerAddTrailMessage *add_trail;
4579 struct GNUNET_PeerIdentity *trail;
4580 struct GNUNET_HashCode trail_id;
4581 struct GNUNET_PeerIdentity destination_peer;
4582 struct GNUNET_PeerIdentity source_peer;
4583 struct GNUNET_PeerIdentity next_hop;
4584 unsigned int trail_length;
4585 unsigned int my_index;
4588 msize = ntohs (message->size);
4589 if (msize != sizeof (struct PeerAddTrailMessage))
4591 GNUNET_break_op (0);
4595 add_trail = (struct PeerAddTrailMessage *) message;
4596 trail_length = (msize - sizeof (struct PeerAddTrailMessage))/
4597 sizeof (struct GNUNET_PeerIdentity);
4598 if ((msize - sizeof (struct PeerAddTrailMessage)) %
4599 sizeof (struct GNUNET_PeerIdentity) != 0)
4601 GNUNET_break_op (0);
4605 if ((msize < sizeof (struct PeerAddTrailMessage) +
4606 trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
4608 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
4610 GNUNET_break_op (0);
4614 trail = (struct GNUNET_PeerIdentity *)&add_trail[1];
4615 destination_peer = add_trail->destination_peer;
4616 source_peer = add_trail->source_peer;
4617 trail_id = add_trail->trail_id;
4619 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
4622 struct FriendInfo *target_friend;
4624 my_index = search_my_index (trail, trail_length);
4625 if (GNUNET_SYSERR == my_index)
4627 GNUNET_break_op (0);
4628 return GNUNET_SYSERR;
4632 next_hop = source_peer;
4634 next_hop = trail[trail_length - 1];
4636 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, next_hop, *peer));
4637 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
4638 GDS_NEIGHBOURS_send_add_trail (source_peer, destination_peer, trail_id,
4639 trail, trail_length, target_friend);
4646 * Send trail teardown and free the trail of the finger for which the first
4647 * friend to reach to a finger is disconnected_peer
4648 * @param disconnected_peer
4649 * @param remove_finger
4652 remove_matching_trails (const struct GNUNET_PeerIdentity *disconnected_peer,
4653 struct FingerInfo *remove_finger)
4656 unsigned int matching_trails_count;
4657 struct Trail *trail;
4659 matching_trails_count = 0;
4661 for (i = 0; i < remove_finger->trails_count; i++)
4663 trail = &remove_finger->trail_list[i];
4665 /* First friend to reach to finger is disconnected_peer. */
4666 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&trail->trail_head->peer,
4669 matching_trails_count++;
4670 send_trail_teardown (remove_finger, trail);
4674 return matching_trails_count;
4679 * FIXME: check that you are not sending trail teardown for trail length = 0
4680 * Iterate over finger_table entries. Check if disconnected_peer is a finger. If
4681 * yes then free that entry.
4682 * Check if disconnected peer is the first friend in the trail to reach to a finger.
4683 * If disconnected peer is the first friend in not all of the trails to reach
4684 * a finger then send only trail teardown message for those trails and don't
4685 * free the finger entry.
4686 * If disconnected peer is the first friend in all of the trails to reach a finger,
4687 * then send trail teardown message and free finger.
4688 * @param disconnected_peer Peer which got disconnected.
4691 remove_matching_fingers (const struct GNUNET_PeerIdentity *disconnected_peer)
4693 struct FingerInfo *remove_finger;
4695 int removed_trails_count;
4697 for (i = 0; i < MAX_FINGERS; i++)
4700 remove_finger = &finger_table[i];
4702 /* No finger stored at this trail index. */
4703 if (GNUNET_NO == remove_finger->is_present)
4706 /* I am my own finger, then ignore this finger. */
4707 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity,
4711 if (NULL != (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4712 &remove_finger->finger_identity)))
4715 /* Is disconnected peer my finger? */
4716 if (0 == GNUNET_CRYPTO_cmp_peer_identity (disconnected_peer,
4717 &remove_finger->finger_identity))
4719 finger_table[i].is_present = GNUNET_NO;
4720 memset ((void *)&finger_table[i], 0, sizeof (struct FingerInfo));
4721 /* No trail to reach this finger, don't send trail_teardown message. */
4722 GNUNET_free (remove_finger);
4726 /* Iterate over the list of remove_finger's trails. Check if first friend
4727 in any of the trail is disconnected_peer. */
4728 removed_trails_count = remove_matching_trails (disconnected_peer, remove_finger);
4730 /* All the finger trails has disconnected peer as the first friend,
4731 so free the finger. */
4732 if (removed_trails_count == remove_finger->trails_count)
4734 GNUNET_free (remove_finger);
4741 * Method called whenever a peer disconnects.
4743 * @param cls closure
4744 * @param peer peer identity this notification is about
4747 handle_core_disconnect (void *cls,
4748 const struct GNUNET_PeerIdentity *peer)
4750 struct FriendInfo *remove_friend;
4752 /* If disconnected to own identity, then return. */
4753 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4756 GNUNET_assert (NULL != (remove_friend =
4757 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
4759 /* Remove fingers with peer as first friend or if peer is a finger. */
4760 remove_matching_fingers (peer);
4762 /* Remove any trail from routing table of which peer is a part of. */
4763 GDS_ROUTING_remove_trail_by_peer (peer);
4765 GNUNET_assert (GNUNET_YES ==
4766 GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
4769 if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
4772 if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
4774 GNUNET_SCHEDULER_cancel (find_finger_trail_task);
4775 find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
4784 * Method called whenever a peer connects.
4786 * @param cls closure
4787 * @param peer_identity peer identity this notification is about
4790 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
4792 struct FriendInfo *friend;
4794 /* Check for connect to self message */
4795 if (0 == memcmp (&my_identity, peer_identity, sizeof (struct GNUNET_PeerIdentity)))
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to %s\n", GNUNET_i2s (peer_identity));
4800 /* If peer already exists in our friend_peermap, then exit. */
4801 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap, peer_identity))
4807 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1,
4810 friend = GNUNET_new (struct FriendInfo);
4811 friend->id = *peer_identity;
4813 GNUNET_assert (GNUNET_OK ==
4814 GNUNET_CONTAINER_multipeermap_put (friend_peermap,
4815 peer_identity, friend,
4816 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4819 /* got a first connection, good time to start with FIND FINGER TRAIL requests...*/
4820 if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
4821 find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
4826 * To be called on core init/fail.
4828 * @param cls service closure
4829 * @param identity the public identity of this peer
4832 core_init (void *cls,
4833 const struct GNUNET_PeerIdentity *identity)
4835 my_identity = *identity;
4840 * Initialize finger table.
4843 finger_table_init ()
4847 for(i = 0; i < MAX_FINGERS; i++)
4849 finger_table[i].is_present = GNUNET_NO;
4850 memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
4856 * Initialize neighbours subsystem.
4857 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4860 GDS_NEIGHBOURS_init (void)
4862 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4863 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
4864 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
4865 {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT, 0},
4866 {&handle_dht_p2p_trail_setup, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP, 0},
4867 {&handle_dht_p2p_trail_setup_result, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT, 0},
4868 {&handle_dht_p2p_verify_successor, GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR, 0},
4869 {&handle_dht_p2p_verify_successor_result, GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT, 0},
4870 {&handle_dht_p2p_notify_new_successor, GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR, 0},
4871 {&handle_dht_p2p_trail_rejection, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION, 0},
4872 {&handle_dht_p2p_trail_compression, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION,
4873 sizeof (struct PeerTrailCompressionMessage)},
4874 {&handle_dht_p2p_trail_teardown, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN,
4875 sizeof (struct PeerTrailTearDownMessage)},
4876 {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL, 0},
4881 GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
4882 &handle_core_disconnect, NULL, GNUNET_NO, NULL,
4883 GNUNET_NO, core_handlers);
4884 if (NULL == core_api)
4885 return GNUNET_SYSERR;
4887 friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
4888 finger_table_init ();
4895 * Shutdown neighbours subsystem.
4898 GDS_NEIGHBOURS_done (void)
4900 if (NULL == core_api)
4903 GNUNET_CORE_disconnect (core_api);
4906 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
4907 GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
4908 friend_peermap = NULL;
4911 if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
4914 GNUNET_SCHEDULER_cancel (find_finger_trail_task);
4915 find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
4924 * @return my identity
4926 struct GNUNET_PeerIdentity
4927 GDS_NEIGHBOURS_get_my_id (void)