xvine:fixes
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_neighbours.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2014 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file dht/gnunet-service-xdht_neighbours.c
23  * @brief GNUnet DHT service's finger and friend table management code
24  * @author Supriti Singh
25  */
26
27 #include "platform.h"
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"
44 #include <fenv.h>
45 #include "dht.h"
46
47 /**
48  * TODO:
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
52  * hashing.
53  */
54
55 #define DEBUG(...)                                           \
56   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
57
58 /**
59  * Maximum possible fingers (including predecessor) of a peer
60  */
61 #define MAX_FINGERS 65
62
63 /**
64  * Maximum allowed number of pending messages per friend peer.
65  */
66 #define MAXIMUM_PENDING_PER_FRIEND 64
67
68 /**
69  * How long to wait before sending another find finger trail request
70  */
71 #define DHT_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
72
73 /**
74  * How long to wait before sending another verify successor message.
75  */
76 #define DHT_SEND_VERIFY_SUCCESSOR_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
77
78 /**
79  * How long at most to wait for transmission of a request to a friend ?
80  */
81 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
82
83 /**
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.
87  */
88 #define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
89
90 /**
91  * Maximum number of trails allowed to go through a friend.
92  */
93 #define TRAILS_THROUGH_FRIEND_THRESHOLD 64
94
95 /**
96  * Maximum number of trails stored per finger.
97  */
98 #define MAXIMUM_TRAILS_PER_FINGER 1
99
100 /**
101  * Finger map index for predecessor entry in finger table.
102  */
103 #define PREDECESSOR_FINGER_ID 64
104
105 /**
106  * Wrap around in peer identity circle.
107  */
108 #define PEER_IDENTITES_WRAP_AROUND pow(2, 64) - 1
109
110 /**
111  * FIXME: Its use only at 3 places check if you can remove it.
112  * To check if a finger is predecessor or not.
113  */
114 enum GDS_NEIGHBOURS_finger_type
115 {
116   GDS_FINGER_TYPE_PREDECESSOR = 1,
117   GDS_FINGER_TYPE_NON_PREDECESSOR = 0
118 };
119
120 GNUNET_NETWORK_STRUCT_BEGIN
121
122 /**
123  * P2P PUT message
124  */
125 struct PeerPutMessage
126 {
127   /**
128    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT
129    */
130   struct GNUNET_MessageHeader header;
131
132   /**
133    * Processing options
134    */
135   uint32_t options GNUNET_PACKED;
136
137   /**
138    * Content type.
139    */
140   uint32_t block_type GNUNET_PACKED;
141
142   /**
143    * Hop count
144    */
145   uint32_t hop_count GNUNET_PACKED;
146
147   /**
148    * Replication level for this message
149    * In the current implementation, this value is not used.
150    */
151   uint32_t desired_replication_level GNUNET_PACKED;
152
153   /**
154    * Length of the PUT path that follows (if tracked).
155    */
156   uint32_t put_path_length GNUNET_PACKED;
157
158   /**
159    * Best known destination (could be my friend or finger) which should
160    * get this message next.
161    */
162   struct GNUNET_PeerIdentity best_known_destination;
163
164   /**
165    * In case best_known_destination is a finger, then trail to reach
166    * to that finger. Else its default value is 0.
167    */
168   struct GNUNET_HashCode intermediate_trail_id;
169
170   /**
171    * When does the content expire?
172    */
173   struct GNUNET_TIME_AbsoluteNBO expiration_time;
174
175   /**
176    * The key to store the value under.
177    */
178   struct GNUNET_HashCode key GNUNET_PACKED;
179
180   /* put path (if tracked) */
181
182   /* Payload */
183
184 };
185
186 /**
187  * P2P GET message
188  */
189 struct PeerGetMessage
190 {
191   /**
192    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET
193    */
194   struct GNUNET_MessageHeader header;
195
196   /**
197    * Processing options
198    */
199   uint32_t options GNUNET_PACKED;
200
201   /**
202    * Desired content type.
203    */
204   uint32_t block_type GNUNET_PACKED;
205
206   /**
207    * Hop count
208    */
209   uint32_t hop_count GNUNET_PACKED;
210
211   /**
212    * Desired replication level for this request.
213    * In the current implementation, this value is not used.
214    */
215   uint32_t desired_replication_level GNUNET_PACKED;
216
217   /**
218    * Total number of peers in get path.
219    */
220   unsigned int get_path_length;
221
222   /**
223    * Best known destination (could be my friend or finger) which should
224    * get this message next.
225    */
226   struct GNUNET_PeerIdentity best_known_destination;
227
228   /**
229    * In case best_known_destination is a finger, then trail to reach
230    * to that finger. Else its default value is 0.
231    */
232   struct GNUNET_HashCode intermediate_trail_id;
233
234   /**
235    * The key we are looking for.
236    */
237   struct GNUNET_HashCode key;
238
239   /* Get path. */
240   /* struct GNUNET_PeerIdentity[]*/
241 };
242
243 /**
244  * P2P Result message
245  */
246 struct PeerGetResultMessage
247 {
248   /**
249    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT
250    */
251   struct GNUNET_MessageHeader header;
252
253   /**
254    * The type for the data.
255    */
256   uint32_t type GNUNET_PACKED;
257
258   /**
259    * Number of peers recorded in the outgoing path from source to the
260    * stored location of this message.
261    */
262   uint32_t put_path_length GNUNET_PACKED;
263
264   /**
265    * Length of the GET path that follows (if tracked).
266    */
267   uint32_t get_path_length GNUNET_PACKED;
268
269   /**
270    * Peer which queried for get and should get the result.
271    */
272   struct GNUNET_PeerIdentity querying_peer;
273
274   /**
275    * When does the content expire?
276    */
277   struct GNUNET_TIME_Absolute expiration_time;
278
279   /**
280    * The key of the corresponding GET request.
281    */
282   struct GNUNET_HashCode key;
283
284   /* put path (if tracked) */
285
286   /* get path (if tracked) */
287
288   /* Payload */
289
290 };
291
292 /**
293  * P2P Trail setup message
294  */
295 struct PeerTrailSetupMessage
296 {
297   /**
298    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP
299    */
300   struct GNUNET_MessageHeader header;
301
302   /**
303    * Is source_peer trying to setup the trail to a predecessor or any finger.
304    */
305   uint32_t is_predecessor;
306
307   /**
308    * Peer closest to this value will be our finger.
309    */
310   uint64_t final_destination_finger_value;
311
312   /**
313    * Source peer which wants to setup the trail to one of its finger.
314    */
315   struct GNUNET_PeerIdentity source_peer;
316
317   /**
318    * Best known destination (could be my friend or finger) which should
319    * get this message next.
320    *
321    * FIXME: this could be removed if we include trail_source / trail_dest
322    * in the routing table. This way we save 32 bytes of bandwidth by using
323    * extra 8 bytes of memory (2 * sizeof (GNUNET_PEER_ID))
324    */
325   struct GNUNET_PeerIdentity best_known_destination;
326
327   /**
328    * In case best_known_destination is a finger, then trail id of trail to
329    * reach to this finger.
330    */
331   struct GNUNET_HashCode intermediate_trail_id;
332
333   /**
334    * Trail id for trail which we are trying to setup.
335    */
336   struct GNUNET_HashCode trail_id;
337
338   /* List of peers which are part of trail setup so far.
339    * Trail does NOT include source_peer and peer which will be closest to
340    * ultimate_destination_finger_value.
341    * struct GNUNET_PeerIdentity trail[]
342    */
343 };
344
345 /**
346   * P2P Trail Setup Result message
347  */
348 struct PeerTrailSetupResultMessage
349 {
350
351   /**
352    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT
353    */
354   struct GNUNET_MessageHeader header;
355
356   /**
357    * Finger to which we have found the path.
358    */
359   struct GNUNET_PeerIdentity finger_identity;
360
361   /**
362    * Peer which started trail_setup to find trail to finger_identity
363    */
364   struct GNUNET_PeerIdentity querying_peer;
365
366   /**
367    * Is the trail setup to querying_peer's predecessor or finger?
368    */
369   uint32_t is_predecessor;
370
371   /**
372    * Value to which finger_identity is the closest peer.
373    */
374   uint64_t ulitmate_destination_finger_value;
375
376   /**
377    * Identifier of the trail from querying peer to finger_identity, NOT
378    * including both endpoints.
379    */
380   struct GNUNET_HashCode trail_id;
381
382   /* List of peers which are part of the trail from querying peer to
383    * finger_identity, NOT including both endpoints.
384    * struct GNUNET_PeerIdentity trail[]
385    */
386 };
387
388 /**
389  * P2P Verify Successor Message.
390  */
391 struct PeerVerifySuccessorMessage
392 {
393   /**
394    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR
395    */
396   struct GNUNET_MessageHeader header;
397
398   /**
399    * Peer which wants to verify its successor.
400    */
401   struct GNUNET_PeerIdentity source_peer;
402
403   /**
404    * Source Peer's current successor.
405    */
406   struct GNUNET_PeerIdentity successor;
407
408   /**
409    * Identifier of trail to reach from source_peer to successor.
410    */
411   struct GNUNET_HashCode trail_id;
412
413   /* List of the peers which are part of trail to reach  from source_peer
414    * to successor, NOT including them
415    * struct GNUNET_PeerIdentity trail[]
416    */
417 };
418
419 /**
420  * P2P Verify Successor Result Message
421  */
422 struct PeerVerifySuccessorResultMessage
423 {
424   /**
425    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT
426    */
427   struct GNUNET_MessageHeader header;
428
429   /**
430    * Peer which sent the request to verify its successor.
431    */
432   struct GNUNET_PeerIdentity querying_peer;
433
434   /**
435    * Successor to which PeerVerifySuccessorMessage was sent.
436    */
437   struct GNUNET_PeerIdentity current_successor;
438
439   /**
440    * Current Predecessor of source_successor. It can be same as querying peer
441    * or different. In case it is different then it can be querying_peer's
442    * probable successor.
443    */
444   struct GNUNET_PeerIdentity probable_successor;
445
446   /**
447    * Trail identifier of trail from querying_peer to current_successor.
448    */
449   struct GNUNET_HashCode trail_id;
450
451   /**
452    * Direction in which we are looking at the trail.
453    */
454   uint32_t trail_direction;
455
456   /* In case probable_successor != querying_peer, then trail to reach from
457    * querying_peer to probable_successor, NOT including end points.
458    * struct GNUNET_PeerIdentity trail[]
459    */
460 };
461
462 /**
463  * P2P Notify New Successor Message.
464  */
465 struct PeerNotifyNewSuccessorMessage
466 {
467   /**
468    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR
469    */
470   struct GNUNET_MessageHeader header;
471
472   /**
473    * Peer which wants to notify its new successor.
474    */
475   struct GNUNET_PeerIdentity source_peer;
476
477   /**
478    * New successor of source_peer.
479    */
480   struct GNUNET_PeerIdentity new_successor;
481
482   /**
483    * Unique identifier of the trail from source_peer to new_successor,
484    * NOT including the endpoints.
485    */
486   struct GNUNET_HashCode trail_id;
487
488   /* List of peers in trail from source_peer to new_successor,
489    * NOT including the endpoints.
490    * struct GNUNET_PeerIdentity trail[]
491    */
492 };
493
494 /**
495  * P2P Trail Compression Message.
496  */
497 struct PeerTrailCompressionMessage
498 {
499   /**
500    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_COMPRESSION
501    */
502   struct GNUNET_MessageHeader header;
503
504   /**
505    * Source peer of this trail.
506    */
507   struct GNUNET_PeerIdentity source_peer;
508
509   /**
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
512    * destination.
513    */
514   struct GNUNET_PeerIdentity new_first_friend;
515
516   /**
517    * Unique identifier of trail.
518    */
519   struct GNUNET_HashCode trail_id;
520 };
521
522
523 /**
524  * P2P Trail Tear Down message.
525  */
526 struct PeerTrailTearDownMessage
527 {
528   /**
529    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN
530    */
531   struct GNUNET_MessageHeader header;
532
533   /**
534    * Unique identifier of the trail.
535    */
536   struct GNUNET_HashCode trail_id;
537
538   /**
539    * Direction of trail.
540    */
541   uint32_t trail_direction;
542 };
543
544
545 /**
546  * P2P Trail Rejection Message.
547  */
548 struct PeerTrailRejectionMessage
549 {
550   /**
551    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION
552    */
553   struct GNUNET_MessageHeader header;
554
555   /**
556    * Peer which wants to set up the trail.
557    */
558   struct GNUNET_PeerIdentity source_peer;
559
560   /**
561    * Peer which sent trail rejection message as it it congested.
562    */
563   struct GNUNET_PeerIdentity congested_peer;
564
565   /**
566    * Peer identity closest to this value will be finger of
567    * source_peer.
568    */
569   uint64_t ultimate_destination_finger_value;
570
571   /**
572    * Is source_peer trying to setup the trail to its predecessor or finger.
573    */
574   uint32_t is_predecessor;
575
576   /**
577    * Identifier for the trail that source peer is trying to setup.
578    */
579   struct GNUNET_HashCode trail_id;
580
581   /**
582    * Relative time for which congested_peer will remain congested.
583    */
584   struct GNUNET_TIME_Relative congestion_time;
585
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[]*/
589 };
590
591 /**
592  * P2P Add Trail Message.
593  */
594 struct PeerAddTrailMessage
595 {
596   /**
597    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL
598    */
599   struct GNUNET_MessageHeader header;
600
601   /**
602    * Source of the routing trail.
603    */
604   struct GNUNET_PeerIdentity source_peer;
605
606   /**
607    * Destination of the routing trail.
608    */
609   struct GNUNET_PeerIdentity destination_peer;
610
611   /**
612    * Unique identifier of the trail from source_peer to destination_peer,
613    * NOT including the endpoints.
614    */
615   struct GNUNET_HashCode trail_id;
616
617   /* Trail from source peer to destination peer, NOT including them.
618    * struct GNUNET_PeerIdentity trail[]
619    */
620 };
621
622 GNUNET_NETWORK_STRUCT_END
623
624 /**
625  * Linked list of messages to send to a particular other peer.
626  */
627 struct P2PPendingMessage
628 {
629   /**
630    * Pointer to next item in the list
631    */
632   struct P2PPendingMessage *next;
633
634   /**
635    * Pointer to previous item in the list
636    */
637   struct P2PPendingMessage *prev;
638
639   /**
640    * Message importance level.  FIXME: used? useful?
641    */
642   unsigned int importance;
643
644   /**
645    * When does this message time out?
646    */
647   struct GNUNET_TIME_Absolute timeout;
648
649   /**
650    * Actual message to be sent, allocated at the end of the struct:
651    * // msg = (cast) &pm[1];
652    * // memcpy (&pm[1], data, len);
653    */
654   const struct GNUNET_MessageHeader *msg;
655
656 };
657
658 /**
659  *  Entry in friend_peermap.
660  */
661 struct FriendInfo
662 {
663   /**
664    * Friend Identity
665    */
666   struct GNUNET_PeerIdentity id;
667
668   /**
669    * Number of trails for which this friend is the first hop or if the friend
670    * is finger.
671    */
672   unsigned int trails_count;
673
674   /**
675    * Count of outstanding messages for this friend.
676    */
677   unsigned int pending_count;
678
679   /**
680    * In case not 0, then amount of time for which this friend is congested.
681    */
682   struct GNUNET_TIME_Absolute congestion_timestamp;
683
684   /**
685    * Head of pending messages to be sent to this friend.
686    */
687   struct P2PPendingMessage *head;
688
689   /**
690    * Tail of pending messages to be sent to this friend.
691    */
692   struct P2PPendingMessage *tail;
693
694   /**
695    * Core handle for sending messages to this friend.
696    */
697   struct GNUNET_CORE_TransmitHandle *th;
698
699 };
700
701 /**
702  * An individual element of the trail to reach to a finger.
703  */
704 struct Trail_Element
705 {
706   /**
707     * Pointer to next item in the list
708     */
709   struct Trail_Element *next;
710
711   /**
712     * Pointer to prev item in the list
713     */
714   struct Trail_Element *prev;
715
716   /**
717    * An element in this trail.
718    */
719   struct GNUNET_PeerIdentity peer;
720 };
721
722 /**
723  * Information about an individual trail.
724  */
725 struct Trail
726 {
727   /**
728    * Head of trail.
729    */
730   struct Trail_Element *trail_head;
731
732   /**
733    * Tail of trail.
734    */
735   struct Trail_Element *trail_tail;
736
737   /**
738    * Unique identifier of this trail.
739    */
740   struct GNUNET_HashCode trail_id;
741
742   /**
743    * Length of trail pointed
744    */
745   unsigned int trail_length;
746
747   /**
748    * Is there a valid trail entry.
749    */
750   unsigned int is_present;
751 };
752
753 /**
754  * An entry in finger_table
755  */
756 struct FingerInfo
757 {
758   /**
759    * Finger identity.
760    */
761   struct GNUNET_PeerIdentity finger_identity;
762
763   /**
764    * Is any finger stored at this finger index.
765    */
766   unsigned int is_present;
767
768   /**
769    * Index in finger peer map
770    */
771   uint32_t finger_table_index;
772
773   /**
774    * Number of trails setup so far for this finger.
775    * Should not cross MAXIMUM_TRAILS_PER_FINGER.
776    */
777   uint32_t trails_count;
778
779   /**
780    * Array of trails to reach to this finger.
781    */
782   struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER];
783 };
784
785
786 /**
787  * Stores information about the peer which is closest to destination_finger_value.
788  * 'closest' can be either successor or predecessor depending on is_predecessor
789  * flag.
790  */
791 struct Closest_Peer
792 {
793   /**
794    * Destination finger value.
795    */
796   uint64_t destination_finger_value;
797
798   /**
799    * Is finger_value a predecessor or any other finger.
800    */
801   unsigned int is_predecessor;
802
803   /**
804    * Trail id to reach to peer.
805    * In case peer is my identity or friend, it is set to 0.
806    */
807   struct GNUNET_HashCode trail_id;
808
809   /**
810    * Next destination. In case of friend and my_identity , it is same as next_hop
811    * In case of finger it is finger identity.
812    */
813   struct GNUNET_PeerIdentity best_known_destination;
814
815   /**
816    * In case best_known_destination is a finger, then first friend in the trail
817    * to reach to it. In other case, same as best_known_destination.
818    */
819   struct GNUNET_PeerIdentity next_hop;
820 };
821
822
823 /**
824  * Data structure to store the trail chosen to reach to finger.
825  */
826 struct Selected_Finger_Trail
827 {
828   /**
829    * First friend in the trail to reach finger.
830    */
831   struct FriendInfo friend;
832
833   /**
834    * Identifier of this trail.
835    */
836   struct GNUNET_HashCode trail_id;
837
838   /**
839    * Total number of peers in this trail.
840    */
841   unsigned int trail_length;
842 };
843
844 /**
845  * Task that sends FIND FINGER TRAIL requests. This task is started when we have
846  * get our first friend.
847  */
848 static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
849
850 /**
851  * Task that sends verify successor message. This task is started when we get
852  * our successor for the first time.
853  */
854 static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_task;
855
856 /**
857  * Identity of this peer.
858  */
859 static struct GNUNET_PeerIdentity my_identity;
860
861 /**
862  * Peer map of all the friends of a peer
863  */
864 static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
865
866 /**
867  * Array of all the fingers.
868  */
869 static struct FingerInfo finger_table [MAX_FINGERS];
870
871 /**
872  * Handle to CORE.
873  */
874 static struct GNUNET_CORE_Handle *core_api;
875
876 /**
877  * Handle for the statistics service.
878  */
879 extern struct GNUNET_STATISTICS_Handle *GDS_stats;
880
881 /**
882  * The current finger index that we have want to find trail to. We start the
883  * search with value = 0, i.e. successor  and then go to PREDCESSOR_FINGER_ID
884  * and decrement it. For any index 63 <= index < 0, if finger is same as successor,
885  * we reset this index to 0.
886  */
887 static unsigned int current_search_finger_index;
888
889 /**
890  * Should we store our topology predecessor and successor IDs into statistics?
891  */
892 unsigned int track_topology;
893
894 /**
895  * Called when core is ready to send a message we asked for
896  * out to the destination.
897  *
898  * @param cls the 'struct FriendInfo' of the target friend
899  * @param size number of bytes available in buf
900  * @param buf where the callee should write the message
901  * @return number of bytes written to buf
902  */
903 static size_t
904 core_transmit_notify (void *cls, size_t size, void *buf)
905 {
906   struct FriendInfo *peer = cls;
907   char *cbuf = buf;
908   struct P2PPendingMessage *pending;
909   size_t off;
910   size_t msize;
911
912   peer->th = NULL;
913   while ((NULL != (pending = peer->head)) &&
914          (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us))
915   {
916     peer->pending_count--;
917     GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
918     GNUNET_free (pending);
919   }
920   if (NULL == pending)
921   {
922     /* no messages pending */
923     return 0;
924   }
925   if (NULL == buf)
926   {
927     peer->th =
928         GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
929                                            GNUNET_CORE_PRIO_BEST_EFFORT,
930                                            GNUNET_TIME_absolute_get_remaining
931                                            (pending->timeout), &peer->id,
932                                            ntohs (pending->msg->size),
933                                            &core_transmit_notify, peer);
934     GNUNET_break (NULL != peer->th);
935     return 0;
936   }
937   off = 0;
938   while ((NULL != (pending = peer->head)) &&
939          (size - off >= (msize = ntohs (pending->msg->size))))
940   {
941     GNUNET_STATISTICS_update (GDS_stats,
942                               gettext_noop
943                               ("# Bytes transmitted to other peers"), msize,
944                               GNUNET_NO);
945     memcpy (&cbuf[off], pending->msg, msize);
946     off += msize;
947     peer->pending_count--;
948     GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
949     GNUNET_free (pending);
950   }
951   if (peer->head != NULL)
952   {
953     peer->th =
954         GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
955                                            GNUNET_CORE_PRIO_BEST_EFFORT,
956                                            GNUNET_TIME_absolute_get_remaining
957                                            (pending->timeout), &peer->id, msize,
958                                            &core_transmit_notify, peer);
959     GNUNET_break (NULL != peer->th);
960   }
961   return off;
962 }
963
964
965 /**
966  * Transmit all messages in the friend's message queue.
967  *
968  * @param peer message queue to process
969  */
970 static void
971 process_friend_queue (struct FriendInfo *peer)
972 {
973   struct P2PPendingMessage *pending;
974
975   if (NULL == (pending = peer->head))
976   {
977     return;
978   }
979   if (NULL != peer->th)
980   {
981     return;
982   }
983  
984   peer->th =
985       GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
986                                          pending->importance,
987                                          GNUNET_TIME_absolute_get_remaining
988                                          (pending->timeout), &peer->id,
989                                          ntohs (pending->msg->size),
990                                          &core_transmit_notify, peer);
991   GNUNET_break (NULL != peer->th);
992 }
993
994
995 /**
996  * Construct a trail setup message and forward it to target_friend
997  * @param source_peer Peer which wants to setup the trail
998  * @param ultimate_destination_finger_value Peer identity closest to this value
999  *                                          will be finger to @a source_peer
1000  * @param best_known_destination Best known destination (could be finger or friend)
1001  *                               which should get this message. In case it is
1002  *                               friend, then it is same as target_friend
1003  * @param target_friend Friend to which message is forwarded now.
1004  * @param trail_length Total number of peers in trail setup so far.
1005  * @param trail_peer_list Trail setup so far
1006  * @param is_predecessor Is @a source_peer looking for trail to a predecessor or not.
1007  * @param trail_id Unique identifier for the trail we are trying to setup.
1008  * @param intermediate_trail_id Trail id of intermediate trail to reach to
1009  *                              best_known_destination when its a finger. If not
1010  *                              used then set to 0.
1011  */
1012 void
1013 GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
1014                                  uint64_t ultimate_destination_finger_value,
1015                                  struct GNUNET_PeerIdentity best_known_destination,
1016                                  struct FriendInfo *target_friend,
1017                                  unsigned int trail_length,
1018                                  const struct GNUNET_PeerIdentity *trail_peer_list,
1019                                  unsigned int is_predecessor,
1020                                  struct GNUNET_HashCode trail_id,
1021                                  struct GNUNET_HashCode intermediate_trail_id)
1022 {
1023   struct P2PPendingMessage *pending;
1024   struct PeerTrailSetupMessage *tsm;
1025   struct GNUNET_PeerIdentity *peer_list;
1026   size_t msize;
1027
1028   msize = sizeof (struct PeerTrailSetupMessage) +
1029           (trail_length * sizeof (struct GNUNET_PeerIdentity));
1030
1031   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1032   {
1033     GNUNET_break (0);
1034     return;
1035   }
1036
1037   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1038   {
1039     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1040                                 1, GNUNET_NO);
1041   }
1042
1043   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1044   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1045   tsm = (struct PeerTrailSetupMessage *) &pending[1];
1046   pending->msg = &tsm->header;
1047   tsm->header.size = htons (msize);
1048   tsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP);
1049   tsm->final_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
1050   tsm->source_peer = source_peer;
1051   tsm->best_known_destination = best_known_destination;
1052   tsm->is_predecessor = htonl (is_predecessor);
1053   tsm->trail_id = trail_id;
1054   tsm->intermediate_trail_id = intermediate_trail_id;
1055
1056   if (trail_length > 0)
1057   {
1058     peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
1059     memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
1060   }
1061
1062   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1063   target_friend->pending_count++;
1064   process_friend_queue (target_friend);
1065 }
1066
1067
1068 /**
1069  * Construct a trail setup result message and forward it to target friend.
1070  * @param querying_peer Peer which sent the trail setup request and should get
1071  *                      the result back.
1072  * @param Finger Peer to which the trail has been setup to.
1073  * @param target_friend Friend to which this message should be forwarded.
1074  * @param trail_length Numbers of peers in the trail.
1075  * @param trail_peer_list Peers which are part of the trail from
1076  *                        querying_peer to Finger, NOT including them.
1077  * @param is_predecessor Is @a Finger predecessor to @a querying_peer ?
1078  * @param ultimate_destination_finger_value Value to which @a finger is the closest
1079  *                                          peer.
1080  * @param trail_id Unique identifier of the trail.
1081  */
1082 void
1083 GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity querying_peer,
1084                                         struct GNUNET_PeerIdentity finger,
1085                                         struct FriendInfo *target_friend,
1086                                         unsigned int trail_length,
1087                                         const struct GNUNET_PeerIdentity *trail_peer_list,
1088                                         unsigned int is_predecessor,
1089                                         uint64_t ultimate_destination_finger_value,
1090                                         struct GNUNET_HashCode trail_id)
1091 {
1092   struct P2PPendingMessage *pending;
1093   struct PeerTrailSetupResultMessage *tsrm;
1094   struct GNUNET_PeerIdentity *peer_list;
1095   size_t msize;
1096
1097   msize = sizeof (struct PeerTrailSetupResultMessage) +
1098           (trail_length * sizeof (struct GNUNET_PeerIdentity));
1099
1100   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1101   {
1102     GNUNET_break (0);
1103     return;
1104   }
1105
1106   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1107   {
1108     GNUNET_STATISTICS_update (GDS_stats,
1109                               gettext_noop ("# P2P messages dropped due to full queue"),
1110                               1, GNUNET_NO);
1111   }
1112
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_XDHT_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
1128   memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1129
1130   /* Send the message to chosen friend. */
1131   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1132   target_friend->pending_count++;
1133   process_friend_queue (target_friend);
1134 }
1135
1136
1137 /**
1138  * Send trail rejection message to target friend
1139  * @param source_peer Peer which is trying to setup the trail.
1140  * @param ultimate_destination_finger_value Peer closest to this value will be
1141  *                                          @a source_peer's finger
1142  * @param congested_peer Peer which sent this message as it is congested.
1143  * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
1144  * @param trail_peer_list Trails seen so far in trail setup before getting rejected
1145  *                        by congested_peer. This does NOT include @a source_peer
1146  *                        and congested_peer.
1147  * @param trail_length Total number of peers in trail_peer_list, NOT including
1148  *                     @a source_peer and @a congested_peer
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.
1152  */
1153 void
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)
1163 {
1164   struct PeerTrailRejectionMessage *trm;
1165   struct P2PPendingMessage *pending;
1166   struct GNUNET_PeerIdentity *peer_list;
1167   size_t msize;
1168
1169   msize = sizeof (struct PeerTrailRejectionMessage) +
1170           (trail_length * sizeof (struct GNUNET_PeerIdentity));
1171
1172   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1173   {
1174     GNUNET_break (0);
1175     return;
1176   }
1177
1178   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1179   {
1180     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1181                                 1, GNUNET_NO);
1182   }
1183
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_XDHT_P2P_TRAIL_SETUP_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 =
1197           GNUNET_htonll (ultimate_destination_finger_value);
1198
1199   peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
1200   if (trail_length > 0)
1201   {
1202     memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1203   }
1204
1205   /* Send the message to chosen friend. */
1206   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1207   target_friend->pending_count++;
1208   process_friend_queue (target_friend);
1209 }
1210
1211
1212 /**
1213  * Construct a verify successor message and forward it to target_friend.
1214  * @param source_peer Peer which wants to verify its successor.
1215  * @param successor Peer which is @a source_peer's current successor.
1216  * @param trail_id Unique Identifier of trail from @a source_peer to @a successor,
1217  *                 NOT including them.
1218  * @param trail List of peers which are part of trail to reach from @a source_peer
1219  *              to @a successor, NOT including them.
1220  * @param trail_length Total number of peers in @a trail.
1221  * @param target_friend Next friend to get this message.
1222  */
1223 void
1224 GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
1225                                               struct GNUNET_PeerIdentity successor,
1226                                               struct GNUNET_HashCode trail_id,
1227                                               struct GNUNET_PeerIdentity *trail,
1228                                               unsigned int trail_length,
1229                                               struct FriendInfo *target_friend)
1230 {
1231   struct PeerVerifySuccessorMessage *vsm;
1232   struct P2PPendingMessage *pending;
1233   struct GNUNET_PeerIdentity *peer_list;
1234   size_t msize;
1235
1236   msize = sizeof (struct PeerVerifySuccessorMessage) +
1237          (trail_length * sizeof (struct GNUNET_PeerIdentity));;
1238   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1239   {
1240     GNUNET_break (0);
1241     return;
1242   }
1243
1244   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1245   {
1246     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1247                                 1, GNUNET_NO);
1248   }
1249
1250   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1251   pending->importance = 0;    /* FIXME */
1252   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1253   vsm = (struct PeerVerifySuccessorMessage *) &pending[1];
1254   pending->msg = &vsm->header;
1255   vsm->header.size = htons (msize);
1256   vsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR);
1257   vsm->source_peer = source_peer;
1258   vsm->successor = successor;
1259   vsm->trail_id = trail_id;
1260   peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
1261   memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
1262
1263   /* Send the message to chosen friend. */
1264   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1265   target_friend->pending_count++;
1266   process_friend_queue (target_friend);
1267 }
1268
1269
1270 /**
1271  * FIXME: In every function we pass target friend except for this one.
1272  * so, either change everything or this one. also, should se just store
1273  * the pointer to friend in routing table rather than gnunet_peeridentity.
1274  * if yes then we should keep friend info in.h  andmake lot of changes.
1275  * Construct a trail teardown message and forward it to target friend.
1276  * @param trail_id Unique identifier of the trail.
1277  * @param trail_direction Direction of trail.
1278  * @param target_friend Friend to get this message.
1279  */
1280 void
1281 GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_HashCode trail_id,
1282                                     unsigned int trail_direction,
1283                                     struct GNUNET_PeerIdentity peer)
1284 {
1285   struct PeerTrailTearDownMessage *ttdm;
1286   struct P2PPendingMessage *pending;
1287   struct FriendInfo *target_friend;
1288   size_t msize;
1289
1290   msize = sizeof (struct PeerTrailTearDownMessage);
1291
1292   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1293   {
1294     GNUNET_break (0);
1295     return;
1296   }
1297
1298   /*FIXME:In what case friend can be null. ?*/
1299   if (NULL == (target_friend =
1300                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, &peer)));
1301   return;
1302
1303   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1304   {
1305     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1306                                 1, GNUNET_NO);
1307   }
1308
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_XDHT_P2P_TRAIL_TEARDOWN);
1316   ttdm->trail_id = trail_id;
1317   ttdm->trail_direction = htonl (trail_direction);
1318
1319   /* Send the message to chosen friend. */
1320   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1321   target_friend->pending_count++;
1322   process_friend_queue (target_friend);
1323 }
1324
1325
1326 /**
1327  * Construct a verify successor result message and send it to target_friend
1328  * @param querying_peer Peer which sent the verify successor message.
1329  * @param source_successor Current_successor of @a querying_peer.
1330  * @param current_predecessor Current predecessor of @a successor. Could be same
1331  *                            or different from @a querying_peer.
1332  * @param trail_id Unique identifier of the trail from @a querying_peer to
1333  *                 @a successor, NOT including them.
1334  * @param trail List of peers which are part of trail from @a querying_peer to
1335  *                 @a successor, NOT including them.
1336  * @param trail_length Total number of peers in @a trail
1337  * @param trail_direction Direction in which we are sending the message. In this
1338  *                        case we are sending result from @a successor to @a querying_peer.
1339  * @param target_friend Next friend to get this message.
1340  */
1341 void
1342 GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity querying_peer,
1343                                              struct GNUNET_PeerIdentity current_successor,
1344                                              struct GNUNET_PeerIdentity probable_successor,
1345                                              struct GNUNET_HashCode trail_id,
1346                                              const struct GNUNET_PeerIdentity *trail,
1347                                              unsigned int trail_length,
1348                                              enum GDS_ROUTING_trail_direction trail_direction,
1349                                              struct FriendInfo *target_friend)
1350 {
1351   struct PeerVerifySuccessorResultMessage *vsmr;
1352   struct P2PPendingMessage *pending;
1353   struct GNUNET_PeerIdentity *peer_list;
1354   size_t msize;
1355
1356   msize = sizeof (struct PeerVerifySuccessorResultMessage) +
1357           (trail_length * sizeof(struct GNUNET_PeerIdentity));
1358
1359   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1360   {
1361     GNUNET_break (0);
1362     return;
1363   }
1364
1365   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1366   {
1367     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1368                                 1, GNUNET_NO);
1369   }
1370
1371   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1372   pending->importance = 0;    /* FIXME */
1373   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1374   vsmr = (struct PeerVerifySuccessorResultMessage *) &pending[1];
1375   pending->msg = &vsmr->header;
1376   vsmr->header.size = htons (msize);
1377   vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT);
1378   vsmr->querying_peer = querying_peer;
1379   vsmr->current_successor = current_successor;
1380   vsmr->probable_successor = probable_successor;
1381   vsmr->trail_direction = htonl (trail_direction);
1382   vsmr->trail_id = trail_id; 
1383   peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
1384   memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
1385   
1386    /* Send the message to chosen friend. */
1387   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1388   target_friend->pending_count++;
1389   process_friend_queue (target_friend);
1390 }
1391
1392
1393 /**
1394  * Construct a notify new successor message and send it to target_friend
1395  * @param source_peer Peer which wants to notify to its new successor that it
1396  *                    could be its predecessor.
1397  * @param successor New successor of @a source_peer
1398  * @param successor_trail List of peers in Trail to reach from
1399  *                            @a source_peer to @a new_successor, NOT including
1400  *                            the endpoints.
1401  * @param successor_trail_length Total number of peers in @a new_successor_trail.
1402  * @param successor_trail_id Unique identifier of @a new_successor_trail.
1403  * @param target_friend Next friend to get this message.
1404  */
1405 void
1406 GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer,
1407                                           struct GNUNET_PeerIdentity successor,
1408                                           const struct GNUNET_PeerIdentity *successor_trail,
1409                                           unsigned int successor_trail_length,
1410                                           struct GNUNET_HashCode succesor_trail_id,
1411                                           struct FriendInfo *target_friend)
1412 {
1413   struct PeerNotifyNewSuccessorMessage *nsm;
1414   struct P2PPendingMessage *pending;
1415   struct GNUNET_PeerIdentity *peer_list;
1416   size_t msize;
1417
1418   msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
1419           (successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
1420
1421   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1422   {
1423     GNUNET_break (0);
1424     return;
1425   }
1426
1427   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1428   {
1429     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1430                                 1, GNUNET_NO);
1431   }
1432   
1433   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1434   pending->importance = 0;    /* FIXME */
1435   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1436   nsm = (struct PeerNotifyNewSuccessorMessage *) &pending[1];
1437   pending->msg = &nsm->header;
1438   nsm->header.size = htons (msize);
1439   nsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR);
1440   nsm->new_successor = successor;
1441   nsm->source_peer = source_peer;
1442   nsm->trail_id = succesor_trail_id;
1443
1444   peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
1445   memcpy (peer_list, successor_trail,
1446           successor_trail_length * sizeof (struct GNUNET_PeerIdentity));
1447  
1448    /* Send the message to chosen friend. */
1449   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1450   target_friend->pending_count++;
1451   process_friend_queue (target_friend);
1452 }
1453
1454
1455 /**
1456  * Construct an add_trail message and send it to target_friend
1457  * @param source_peer Source of the trail.
1458  * @param destination_peer Destination of the trail.
1459  * @param trail_id Unique identifier of the trail from
1460  *                 @a source_peer to @a destination_peer, NOT including the endpoints.
1461  * @param trail List of peers in Trail from @a source_peer to @a destination_peer,
1462  *              NOT including the endpoints.
1463  * @param trail_length Total number of peers in @a trail.
1464  * @param target_friend Next friend to get this message.
1465  */
1466 void
1467 GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
1468                                struct GNUNET_PeerIdentity destination_peer,
1469                                struct GNUNET_HashCode trail_id,
1470                                const struct GNUNET_PeerIdentity *trail,
1471                                unsigned int trail_length,
1472                                struct FriendInfo *target_friend)
1473 {
1474   struct PeerAddTrailMessage *adm;
1475   struct GNUNET_PeerIdentity *peer_list;
1476   struct P2PPendingMessage *pending;
1477   size_t msize;
1478
1479   msize = sizeof (struct PeerAddTrailMessage) +
1480           (trail_length * sizeof(struct GNUNET_PeerIdentity));
1481
1482   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1483   {
1484     GNUNET_break (0);
1485     return;
1486   }
1487
1488   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1489   {
1490     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1491                                 1, GNUNET_NO);
1492   }
1493
1494   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1495   pending->importance = 0;    /* FIXME */
1496   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1497   adm = (struct PeerAddTrailMessage *) &pending[1];
1498   pending->msg = &adm->header;
1499   adm->header.size = htons (msize);
1500   adm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL);
1501   adm->source_peer = source_peer;
1502   adm->destination_peer = destination_peer;
1503   adm->trail_id = trail_id;
1504   peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
1505   memcpy (peer_list, trail, sizeof (struct GNUNET_PeerIdentity) * trail_length);
1506   
1507   /* Send the message to chosen friend. */
1508   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1509   target_friend->pending_count++;
1510   process_friend_queue (target_friend);
1511
1512 }
1513
1514
1515 /**
1516  * Construct a trail compression message and send it to target_friend.
1517  * @param source_peer Source of the trail.
1518  * @param trail_id Unique identifier of trail.
1519  * @param first_friend First hop in compressed trail to reach from source to finger
1520  * @param target_friend Next friend to get this message.
1521  */
1522 void
1523 GDS_NEIGHBOURS_send_trail_compression (struct GNUNET_PeerIdentity source_peer,
1524                                        struct GNUNET_HashCode trail_id,
1525                                        struct GNUNET_PeerIdentity first_friend,
1526                                        struct FriendInfo *target_friend)
1527 {
1528   struct P2PPendingMessage *pending;
1529   struct PeerTrailCompressionMessage *tcm;
1530   size_t msize;
1531
1532   msize = sizeof (struct PeerTrailCompressionMessage);
1533
1534   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1535   {
1536     GNUNET_break (0);
1537     return;
1538   }
1539
1540   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1541   {
1542     GNUNET_STATISTICS_update (GDS_stats,
1543                               gettext_noop ("# P2P messages dropped due to full queue"),
1544                                                       1, GNUNET_NO);
1545   }
1546
1547   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1548   pending->importance = 0;    /* FIXME */
1549   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1550   tcm = (struct PeerTrailCompressionMessage *) &pending[1];
1551   pending->msg = &tcm->header;
1552   tcm->header.size = htons (msize);
1553   tcm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_COMPRESSION);
1554   tcm->source_peer = source_peer;
1555   tcm->new_first_friend = first_friend;
1556   tcm->trail_id = trail_id;
1557
1558   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1559   target_friend->pending_count++;
1560   process_friend_queue (target_friend);
1561
1562 }
1563
1564
1565 /**
1566  * Search my location in trail. In case I am present more than once in the
1567  * trail (can happen during trail setup), then return my lowest index.
1568  * @param trail List of peers
1569  * @return my_index if found
1570  *         -1 if no entry found.
1571  */
1572 static int
1573 search_my_index (const struct GNUNET_PeerIdentity *trail,
1574                  int trail_length)
1575 {
1576   int i;
1577
1578   for (i = 0; i < trail_length; i++)
1579   {
1580     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
1581       return i;
1582   }
1583
1584   return -1;
1585 }
1586
1587
1588 /**
1589  * Check if the friend is congested or have reached maximum number of trails
1590  * it can be part of of.
1591  * @param friend Friend to be checked.
1592  * @return #GNUNET_NO if friend is not congested or have not crossed threshold.
1593  *         #GNUNET_YES if friend is either congested or have crossed threshold
1594  */
1595 static int
1596 is_friend_congested (struct FriendInfo *friend)
1597 {
1598   if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) &&
1599       ((0 == GNUNET_TIME_absolute_get_remaining
1600              (friend->congestion_timestamp).rel_value_us)))
1601     return GNUNET_NO;
1602   else
1603     return GNUNET_YES;
1604 }
1605
1606
1607 /**
1608  * Select closest finger to value.
1609  * @param peer1 First peer
1610  * @param peer2 Second peer
1611  * @param value Value to be compare
1612  * @return Closest peer
1613  */
1614 const static struct GNUNET_PeerIdentity *
1615 select_closest_finger (const struct GNUNET_PeerIdentity *peer1,
1616                        const struct GNUNET_PeerIdentity *peer2,
1617                        uint64_t value)
1618 {
1619   uint64_t peer1_value;
1620   uint64_t peer2_value;
1621
1622   memcpy (&peer1_value, peer1, sizeof (uint64_t));
1623   memcpy (&peer2_value, peer2, sizeof (uint64_t));
1624   peer1_value = GNUNET_ntohll (peer1_value);
1625   peer2_value = GNUNET_ntohll (peer2_value);
1626
1627   if (peer1_value == value)
1628   {
1629     return peer1;
1630   }
1631
1632   if (peer2_value == value)
1633   {
1634     return peer2;
1635   }
1636
1637   if (peer2_value < peer1_value)
1638   {
1639     if ((peer2_value < value) && (value < peer1_value))
1640     {
1641       return peer1;
1642     }
1643     else if (((peer1_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1644              ((0 < value) && (value < peer2_value)))
1645     {
1646       return peer2;
1647     }
1648   }
1649
1650   if (peer1_value < peer2_value)
1651   {
1652     if ((peer1_value < value) && (value < peer2_value))
1653     {
1654       return peer2;
1655     }
1656     else if (((peer2_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1657              ((0 < value) && (value < peer1_value)))
1658     {
1659       return peer1;
1660     }
1661   }
1662   return NULL;
1663 }
1664
1665
1666 /**
1667  * Select closest predecessor to value.
1668  * @param peer1 First peer
1669  * @param peer2 Second peer
1670  * @param value Value to be compare
1671  * @return Peer which precedes value in the network.
1672  */
1673 const static struct GNUNET_PeerIdentity *
1674 select_closest_predecessor (const struct GNUNET_PeerIdentity *peer1,
1675                             const struct GNUNET_PeerIdentity *peer2,
1676                             uint64_t value)
1677 {
1678   uint64_t peer1_value;
1679   uint64_t peer2_value;
1680
1681   memcpy (&peer1_value, peer1, sizeof (uint64_t));
1682   memcpy (&peer2_value, peer2, sizeof (uint64_t));
1683   peer1_value = GNUNET_ntohll (peer1_value);
1684   peer2_value = GNUNET_ntohll (peer2_value);
1685
1686   if (peer1_value == value)
1687     return peer1;
1688
1689   if (peer2_value == value)
1690     return peer2;
1691
1692   if (peer1_value < peer2_value)
1693   {
1694     if ((peer1_value < value) && (value < peer2_value))
1695     {
1696       return peer1;
1697     }
1698     else if (((peer2_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1699              ((PEER_IDENTITES_WRAP_AROUND > value) && (value < peer1_value)))
1700     {
1701       return peer2;
1702     }
1703   }
1704
1705   if (peer2_value < peer1_value)
1706   {
1707     if ((peer2_value < value) && (value < peer1_value))
1708     {
1709       return peer2;
1710     }
1711     else if (((peer1_value < value) && (value < PEER_IDENTITES_WRAP_AROUND)) ||
1712              ((PEER_IDENTITES_WRAP_AROUND > value) && (value < peer2_value)))
1713     {
1714       return peer1;
1715     }
1716   }
1717   return NULL;
1718 }
1719
1720 #if 0
1721 /**
1722  * 
1723  *
1724  */
1725 void
1726 test_print_trail (struct GNUNET_PeerIdentity *trail,
1727                   unsigned int trail_length)
1728 {
1729   struct GNUNET_PeerIdentity print_peer;
1730   int i;
1731   
1732   FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail_length = %d"),
1733   __FILE__, __func__,__LINE__,trail_length);
1734   for (i =0 ; i< trail_length; i++)
1735   {
1736     print_peer = trail[i];
1737     FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d]=%s"),
1738             __FILE__, __func__,__LINE__,i,GNUNET_i2s(&print_peer));
1739   }
1740 }
1741 #endif
1742
1743 #if 0
1744 /**
1745  * This is a test function to print all the entries of friend table.
1746  */
1747 static void
1748 test_friend_peermap_print ()
1749 {
1750   struct FriendInfo *friend;
1751   struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter;
1752   struct GNUNET_PeerIdentity print_peer;
1753   struct GNUNET_PeerIdentity key_ret;
1754   int i;
1755
1756   print_peer = my_identity;
1757   FPRINTF (stderr,_("\nSUPU************  FRIEND_PEERMAP of %s"),GNUNET_i2s(&print_peer));
1758   friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
1759
1760   for (i = 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
1761   {
1762     if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter,
1763                                                                   &key_ret,
1764                                                                   (const void **)&friend))
1765     {
1766       memcpy (&print_peer, &key_ret, sizeof (struct GNUNET_PeerIdentity));
1767       FPRINTF (stderr,_("\nSUPU %s, %s, %d, friend = %s, friend->trails_count = %d"),
1768               __FILE__, __func__,__LINE__, GNUNET_i2s(&print_peer), friend->trails_count);
1769     }
1770   }
1771 }
1772
1773
1774
1775 /**
1776  * This is a test function, to print all the entries of finger table.
1777  */
1778 static void
1779 test_finger_table_print()
1780 {
1781   struct FingerInfo *finger;
1782   struct GNUNET_PeerIdentity print_peer;
1783   //struct Trail *trail;
1784   int i;
1785   //int j;
1786   //int k;
1787   print_peer = my_identity;
1788   FPRINTF (stderr,_("\nSUPU************  FINGER_TABLE of %s"),GNUNET_i2s(&print_peer));
1789   for (i = 0; i < MAX_FINGERS; i++)
1790   {
1791     finger = &finger_table[i];
1792
1793     if (GNUNET_NO == finger->is_present)
1794       continue;
1795
1796     print_peer = finger->finger_identity;
1797     FPRINTF (stderr,_("\nSUPU %s, %s, %d, finger_table[%d] = %s, trails_count = %d"),
1798             __FILE__, __func__,__LINE__,i,GNUNET_i2s (&print_peer), finger->trails_count);
1799
1800 #if 0
1801     for (j = 0; j < finger->trails_count; j++)
1802     {
1803       trail = &finger->trail_list[j];
1804       FPRINTF (stderr,_("\nSUPU %s, %s, %d, trail_id[%d]=%s"),__FILE__, __func__,__LINE__,j, GNUNET_h2s(&trail->trail_id));
1805       struct Trail_Element *element;
1806       element = trail->trail_head;
1807       for (k = 0; k < trail->trail_length; k++)
1808       {
1809         print_peer = element->peer;
1810         FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d] = %s "),__FILE__, __func__,__LINE__,k, GNUNET_i2s(&print_peer));
1811         element = element->next;
1812       }
1813     }
1814     #endif
1815   }
1816 }
1817 #endif
1818
1819 /**
1820  * Select the closest peer among two peers (which should not be same)
1821  * with respect to value and finger_table_index
1822  * NOTE: peer1 != peer2
1823  * @param peer1 First peer
1824  * @param peer2 Second peer
1825  * @param value Value relative to which we find the closest
1826  * @param is_predecessor Is value a predecessor or any other finger.
1827  * @return Closest peer among two peers.
1828  */
1829 const static struct GNUNET_PeerIdentity *
1830 select_closest_peer (const struct GNUNET_PeerIdentity *peer1,
1831                      const struct GNUNET_PeerIdentity *peer2,
1832                      uint64_t value,
1833                      unsigned int is_predecessor)
1834 {
1835   if (1 == is_predecessor)
1836     return select_closest_predecessor (peer1, peer2, value);
1837
1838   return select_closest_finger (peer1, peer2, value);
1839 }
1840
1841
1842 /**
1843  * Iterate over the list of all the trails of a finger. In case the first
1844  * friend to reach the finger has reached trail threshold or is congested,
1845  * then don't select it. In case there multiple available good trails to reach
1846  * to Finger, choose the one with shortest trail length.
1847  * Note: We use length as parameter. But we can use any other suitable parameter
1848  * also.
1849  * @param finger Finger
1850  * @return struct Selected_Finger_Trail which contains the first friend , trail id
1851  * and trail length. NULL in case none of the trails are free.
1852  */
1853 static struct Selected_Finger_Trail *
1854 select_finger_trail (struct FingerInfo *finger)
1855 {
1856   struct FriendInfo *friend;
1857   struct Trail *iterator;
1858   struct Selected_Finger_Trail *finger_trail;
1859   unsigned int i;
1860   unsigned int flag = 0;
1861   unsigned int j = 0;
1862
1863   finger_trail = GNUNET_new (struct Selected_Finger_Trail);
1864   GNUNET_assert (finger->trails_count > 0);
1865
1866   for (i = 0; i < finger->trails_count; i++)
1867   {
1868     iterator = &finger->trail_list[i];
1869
1870     /* No trail stored at this index. */
1871     if (GNUNET_NO == iterator->is_present)
1872       continue;
1873
1874     GNUNET_assert (NULL !=
1875                   (friend =
1876                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1877                                                       &iterator->trail_head->peer)));
1878
1879     /* First friend to reach trail is not free. */
1880     if (GNUNET_YES == is_friend_congested (friend))
1881     {
1882       j++;
1883       continue;
1884     }
1885
1886     if (!flag)
1887     {
1888       flag = 1;
1889       finger_trail->trail_length = iterator->trail_length;
1890       finger_trail->friend = *friend;
1891       finger_trail->trail_id = iterator->trail_id;
1892     }
1893     else if (finger_trail->trail_length > iterator->trail_length)
1894     {
1895       finger_trail->friend = *friend;
1896       finger_trail->trail_id = iterator->trail_id;
1897       finger_trail->trail_length = iterator->trail_length;
1898     }
1899   }
1900
1901   /* All the first friend in all the trails to reach to finger are either
1902    congested or have crossed trail threshold. */
1903   if (j == finger->trails_count)
1904     return NULL;
1905
1906   return finger_trail;
1907 }
1908
1909
1910 /**
1911  * Compare FINGER entry with current successor. If finger's first friend of all
1912  * its trail is not congested and  has not crossed trail threshold, then check
1913  * if finger peer identity is closer to final_destination_finger_value than
1914  * current_successor. If yes then update current_successor.
1915  * @param current_successor[in/out]
1916  * @return
1917  */
1918 static void
1919 compare_finger_and_current_successor (struct Closest_Peer *current_closest_peer)
1920 {
1921   struct FingerInfo *finger;
1922   const struct GNUNET_PeerIdentity *closest_peer;
1923   struct Selected_Finger_Trail *finger_trail;
1924   int i;
1925
1926   /* Iterate over finger table. */
1927   for (i = 0; i < MAX_FINGERS; i++)
1928   {
1929     finger = &finger_table[i];
1930
1931     if (GNUNET_NO == finger->is_present)
1932       continue;
1933
1934     /* FIXME write correct comment here */
1935     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1936                                               &current_closest_peer->best_known_destination))
1937       continue;
1938
1939     /* If I am my own finger, then ignore this finger. */
1940     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1941                                               &my_identity))
1942     {
1943       /* FIXME: I think a peer should not select itself as its own identity ever.
1944        But it does select. Find out why??*/
1945       //GNUNET_break (0);
1946       //continue;
1947       return;
1948     }
1949
1950     /* If finger is a friend, then do nothing. As we have already checked
1951      * for each friend in compare_friend_and_current_successor(). */
1952     if (NULL != (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1953                                                     &finger->finger_identity)))
1954     {
1955       continue;
1956     }
1957
1958     closest_peer = select_closest_peer (&finger->finger_identity,
1959                                         &current_closest_peer->best_known_destination,
1960                                         current_closest_peer->destination_finger_value,
1961                                         current_closest_peer->is_predecessor);
1962
1963     if (&finger->finger_identity == closest_peer)
1964     {
1965       /* Choose one of the trail to reach to finger. */
1966       finger_trail = select_finger_trail (finger);
1967
1968       /* In case no trail found, ignore this finger. */
1969       if (NULL == finger_trail)
1970         continue;
1971
1972       current_closest_peer->best_known_destination = finger->finger_identity;
1973       current_closest_peer->next_hop = finger_trail->friend.id;
1974       current_closest_peer->trail_id = finger_trail->trail_id;
1975       GNUNET_free(finger_trail);
1976     }
1977     continue;
1978   }
1979 }
1980
1981
1982 /**
1983  * Compare friend entry with current successor.
1984  * If friend identity and current_successor is same, then do nothing.
1985  * If friend is not congested and has not crossed trail threshold, then check
1986  * if friend peer identity is closer to final_destination_finger_value than
1987  * current_successor. If yes then update current_successor.
1988  * @param cls closure
1989  * @param key current public key
1990  * @param value struct Closest_Peer
1991  * @return #GNUNET_YES if we should continue to iterate,
1992  *         #GNUNET_NO if not.
1993  */
1994 static int
1995 compare_friend_and_current_closest_peer (void *cls,
1996                                          const struct GNUNET_PeerIdentity *key,
1997                                          void *value)
1998 {
1999   struct FriendInfo *friend = value;
2000   struct Closest_Peer *current_closest_peer = cls;
2001   const struct GNUNET_PeerIdentity *closest_peer;
2002
2003   /* Friend is either congested or has crossed threshold. */
2004   if (GNUNET_YES == is_friend_congested (friend))
2005     return GNUNET_YES;
2006
2007   /* If current_closest_peer and friend identity are same, then do nothing.*/
2008   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id,
2009                                             &current_closest_peer->best_known_destination))
2010   {
2011     GNUNET_break (0);
2012     return GNUNET_YES;
2013   }
2014
2015   closest_peer = select_closest_peer (&friend->id,
2016                                       &current_closest_peer->best_known_destination,
2017                                       current_closest_peer->destination_finger_value,
2018                                       current_closest_peer->is_predecessor);
2019
2020   /* Is friend the closest successor? */
2021   if (&friend->id == closest_peer)
2022   {
2023     current_closest_peer->best_known_destination = friend->id;
2024     current_closest_peer->next_hop = friend->id;
2025   }
2026
2027   return GNUNET_YES;
2028 }
2029
2030
2031 /**
2032  * Initialize current_successor to my_identity.
2033  * @param my_identity My peer identity
2034  * @return Updated closest_peer
2035  */
2036 static struct Closest_Peer
2037 init_current_successor (struct GNUNET_PeerIdentity my_identity,
2038                         uint64_t destination_finger_value,
2039                         unsigned int is_predecessor)
2040 {
2041   struct Closest_Peer current_closest_peer;
2042
2043   memset (&current_closest_peer.trail_id, 0, sizeof(struct GNUNET_HashCode));
2044   current_closest_peer.destination_finger_value = destination_finger_value;
2045   current_closest_peer.is_predecessor = is_predecessor;
2046   current_closest_peer.next_hop = my_identity;
2047   current_closest_peer.best_known_destination = my_identity;
2048
2049   return current_closest_peer;
2050 }
2051
2052
2053 /**
2054  * FIXME: at the moment, there is not 100% get and put in case of non-malicious
2055  * peer. It could be because of the logic we wrote here. Verify if its correct.
2056  * If not then return immediate_successor.
2057  *
2058  * Find the successor for destination_finger_value among my_identity, my
2059  * friends and my fingers. Don't consider friends or fingers which are either
2060  * congested or have crossed the threshold.
2061  * NOTE: In case a friend is also a finger, then it is always chosen as friend
2062  * not a finger.
2063  * @param destination_finger_value Peer closest to this value will be the next successor.
2064  * @param is_predecessor Are we looking for predecessor or finger?
2065  * @return Successor It is never NULL, in case none of friend or finger is closest,
2066  *                   then we return my_identity.
2067  */
2068 static struct Closest_Peer
2069 find_successor (uint64_t destination_finger_value,
2070                 unsigned int is_predecessor)
2071 {
2072   struct Closest_Peer current_closest_peer;
2073
2074    /* Initialize current_successor to my_identity. */
2075   current_closest_peer = init_current_successor (my_identity,
2076                                                  destination_finger_value,
2077                                                  is_predecessor);
2078
2079   /* Compare each friend entry with current_successor and update current_successor
2080    * with friend if its closest. */
2081   GNUNET_assert
2082           (GNUNET_SYSERR !=
2083            GNUNET_CONTAINER_multipeermap_iterate (friend_peermap,
2084                                                   &compare_friend_and_current_closest_peer,
2085                                                   &current_closest_peer));
2086
2087   /* Compare each finger entry with current_successor and update current_successor
2088    * with finger if its closest. */
2089   compare_finger_and_current_successor (&current_closest_peer);
2090
2091   return current_closest_peer;
2092 }
2093
2094
2095 /**
2096  * Construct a Put message and send it to target_peer.
2097  * @param key Key for the content
2098  * @param block_type Type of the block
2099  * @param options Routing options
2100  * @param desired_replication_level Desired replication count
2101  * @param best_known_dest Peer to which this message should reach eventually,
2102  *                        as it is best known destination to me.
2103  * @param intermediate_trail_id Trail id in case
2104  * @param target_peer Peer to which this message will be forwarded.
2105  * @param hop_count Number of hops traversed so far.
2106  * @param put_path_length Total number of peers in @a put_path
2107  * @param put_path Number of peers traversed so far
2108  * @param expiration_time When does the content expire
2109  * @param data Content to store
2110  * @param data_size Size of content @a data in bytes
2111  */
2112 void
2113 GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
2114                          enum GNUNET_BLOCK_Type block_type,
2115                                            enum GNUNET_DHT_RouteOption options,
2116                                            uint32_t desired_replication_level,
2117                                            struct GNUNET_PeerIdentity best_known_dest,
2118                                            struct GNUNET_HashCode intermediate_trail_id,
2119                                            struct GNUNET_PeerIdentity *target_peer,
2120                          uint32_t hop_count,
2121                          uint32_t put_path_length,
2122                          struct GNUNET_PeerIdentity *put_path,
2123                          struct GNUNET_TIME_Absolute expiration_time,
2124                          const void *data, size_t data_size)
2125 {
2126   struct PeerPutMessage *ppm;
2127   struct P2PPendingMessage *pending;
2128   struct FriendInfo *target_friend;
2129   struct GNUNET_PeerIdentity *pp;
2130   struct GNUNET_PeerIdentity next_hop;
2131
2132   size_t msize;
2133
2134   msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
2135           sizeof (struct PeerPutMessage);
2136
2137   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2138   {
2139     put_path_length = 0;
2140     msize = data_size + sizeof (struct PeerPutMessage);
2141   }
2142
2143   /* Should it be GNUNET_SERVER_MAX_MESSAGE_SIZE? */
2144   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2145   {
2146     DEBUG("msize = %lu\n",msize);
2147     GNUNET_break (0);
2148     return;
2149   }
2150
2151   /* This is the first call made from clients file. So, we should search for the
2152      target_friend. */
2153   if (NULL == target_peer)
2154   {
2155     uint64_t key_value;
2156     struct Closest_Peer successor;
2157
2158     memcpy (&key_value, key, sizeof (uint64_t));
2159     key_value = GNUNET_ntohll (key_value);
2160
2161     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
2162     best_known_dest = successor.best_known_destination;
2163     next_hop = successor.next_hop;
2164     intermediate_trail_id = successor.trail_id;
2165
2166     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity))
2167     {
2168       /* I am the destination. */
2169       DEBUG("PUT destination is me = %s,key =%s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(key));
2170       GDS_DATACACHE_handle_put (expiration_time, key, 0, NULL,
2171                                 block_type,data_size,data);
2172       return;
2173     }
2174     else
2175       GNUNET_assert (NULL !=
2176                     (target_friend =
2177                      GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
2178   }
2179   else
2180   {
2181     GNUNET_assert (NULL !=
2182                    (target_friend =
2183                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
2184   }
2185
2186   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2187   pending->timeout = expiration_time;
2188   ppm = (struct PeerPutMessage *) &pending[1];
2189   pending->msg = &ppm->header;
2190   ppm->header.size = htons (msize);
2191   ppm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT);
2192   ppm->options = htonl (options);
2193   ppm->block_type = htonl (block_type);
2194   ppm->hop_count = htonl (hop_count + 1);
2195   ppm->desired_replication_level = htonl (desired_replication_level);
2196   ppm->put_path_length = htonl (put_path_length);
2197   ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
2198   ppm->best_known_destination = best_known_dest;
2199   ppm->key = *key;
2200
2201   pp = (struct GNUNET_PeerIdentity *) &ppm[1];
2202   if (put_path_length != 0)
2203   {
2204     memcpy (pp, put_path,
2205             sizeof (struct GNUNET_PeerIdentity) * put_path_length);
2206   }
2207   memcpy (&pp[put_path_length], data, data_size);
2208   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2209   target_friend->pending_count++;
2210   process_friend_queue (target_friend);
2211 }
2212
2213
2214 /**
2215  * Construct a Get message and send it to target_peer.
2216  * @param key Key for the content
2217  * @param block_type Type of the block
2218  * @param options Routing options
2219  * @param desired_replication_level Desired replication count
2220  * @param best_known_dest Peer which should get this message. Same as target peer
2221  *                        if best_known_dest is a friend else its a finger.
2222  * @param intermediate_trail_id  Trail id to reach to @a best_known_dest
2223  *                              in case it is a finger else set to 0.
2224  * @param target_peer Peer to which this message will be forwarded.
2225  * @param hop_count Number of hops traversed so far.
2226  * @param data Content to store
2227  * @param data_size Size of content @a data in bytes
2228  * @param get_path_length Total number of peers in @a get_path
2229  * @param get_path Number of peers traversed so far
2230  */
2231 void
2232 GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
2233                          enum GNUNET_BLOCK_Type block_type,
2234                          enum GNUNET_DHT_RouteOption options,
2235                          uint32_t desired_replication_level,
2236                          struct GNUNET_PeerIdentity best_known_dest,
2237                          struct GNUNET_HashCode intermediate_trail_id,
2238                          struct GNUNET_PeerIdentity *target_peer,
2239                          uint32_t hop_count,
2240                          uint32_t get_path_length,
2241                          struct GNUNET_PeerIdentity *get_path)
2242 {
2243   struct PeerGetMessage *pgm;
2244   struct P2PPendingMessage *pending;
2245   struct FriendInfo *target_friend;
2246   struct GNUNET_PeerIdentity *gp;
2247   size_t msize;
2248
2249   msize = sizeof (struct PeerGetMessage) +
2250           (get_path_length * sizeof (struct GNUNET_PeerIdentity));
2251   
2252   //GNUNET_SERVER_MAX_MESSAGE_SIZE
2253   /* FIXME:TODO:URGENTHere you can try to optimize it a bit. In case the get path contains you
2254    or your friend then shorten the path. */
2255   /* In this case we don't make get_path_length = 0, as we need get path to
2256    * return the message back to querying client. */
2257   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2258   {
2259     GNUNET_break (0);
2260     return;
2261   }
2262   
2263   DEBUG("GET FOR DATA_SIZE = %lu\n",msize);
2264   
2265   /* This is the first time we got request from our own client file. */
2266   if (NULL == target_peer)
2267   {
2268     uint64_t key_value;
2269     struct Closest_Peer successor;
2270
2271     memcpy (&key_value, key, sizeof (uint64_t));
2272     key_value = GNUNET_ntohll (key_value);
2273     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
2274
2275     best_known_dest = successor.best_known_destination;
2276     intermediate_trail_id = successor.trail_id;
2277
2278     /* I am the destination. I have the data. */
2279     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2280                                               &best_known_dest))
2281     {
2282       DEBUG("GET destination is me = %s,KEY = %s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(key));
2283       GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
2284                                 NULL, 0, 1, &my_identity, NULL,&my_identity);
2285
2286       return;
2287     }
2288     else
2289     {
2290       GNUNET_assert (NULL !=
2291                     (target_friend =
2292                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2293                                                         &successor.next_hop)));
2294     }
2295
2296   }
2297   else
2298   {
2299     GNUNET_assert (NULL !=
2300                   (target_friend =
2301                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer))); //FIXME: assertion fails.
2302   }
2303
2304   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2305   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
2306   pending->importance = 0;    /* FIXME */
2307   pgm = (struct PeerGetMessage *) &pending[1];
2308   pending->msg = &pgm->header;
2309   pgm->header.size = htons (msize);
2310   pgm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET);
2311   pgm->get_path_length = htonl (get_path_length);
2312   pgm->best_known_destination = best_known_dest;
2313   pgm->key = *key;
2314   pgm->intermediate_trail_id = intermediate_trail_id;
2315   pgm->hop_count = htonl (hop_count + 1);
2316   gp = (struct GNUNET_PeerIdentity *) &pgm[1];
2317
2318   if (get_path_length != 0)
2319   {
2320     memcpy (gp, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
2321   }
2322
2323   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2324   target_friend->pending_count++;
2325   process_friend_queue (target_friend);
2326 }
2327
2328
2329 /**
2330  * Send the get result to requesting client.
2331  * @param key Key of the requested data.
2332  * @param type Block type
2333  * @param target_peer Next peer to forward the message to.
2334  * @param source_peer Peer which has the data for the key.
2335  * @param put_path_length Number of peers in @a put_path
2336  * @param put_path Path taken to put the data at its stored location.
2337  * @param get_path_length Number of peers in @a get_path
2338  * @param get_path Path taken to reach to the location of the key.
2339  * @param expiration When will this result expire?
2340  * @param data Payload to store
2341  * @param data_size Size of the @a data
2342  */
2343 void
2344 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
2345                                 enum GNUNET_BLOCK_Type type,
2346                                 const struct GNUNET_PeerIdentity *target_peer,
2347                                 const struct GNUNET_PeerIdentity *source_peer,
2348                                 unsigned int put_path_length,
2349                                 const struct GNUNET_PeerIdentity *put_path,
2350                                 unsigned int get_path_length,
2351                                 const struct GNUNET_PeerIdentity *get_path,
2352                                 struct GNUNET_TIME_Absolute expiration,
2353                                 const void *data, size_t data_size)
2354 {
2355   struct PeerGetResultMessage *get_result;
2356   struct GNUNET_PeerIdentity *paths;
2357   struct P2PPendingMessage *pending;
2358   struct FriendInfo *target_friend;
2359   int current_path_index;
2360   size_t msize;
2361
2362   msize = (put_path_length + get_path_length )* sizeof (struct GNUNET_PeerIdentity) +
2363           data_size +
2364           sizeof (struct PeerGetResultMessage);
2365
2366   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2367   {
2368     put_path_length = 0;
2369     msize = msize - put_path_length;
2370     return;
2371   }
2372
2373   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
2374   {
2375     GNUNET_break(0);
2376     return;
2377   }
2378   DEBUG("GET RESULT  FOR DATA_SIZE = %lu\n",msize);
2379   current_path_index = 0;
2380   if(get_path_length > 0)
2381   {
2382     current_path_index = search_my_index(get_path, get_path_length);
2383     if (-1 == current_path_index)
2384     {
2385       GNUNET_break (0);
2386       return;
2387     }
2388   }
2389   if (0 == current_path_index)
2390   {
2391     GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
2392                               get_path, put_path_length,
2393                               put_path, type, data_size, data);
2394     return;
2395   }
2396
2397   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2398   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
2399   pending->importance = 0;
2400   get_result = (struct PeerGetResultMessage *)&pending[1];
2401   pending->msg = &get_result->header;
2402   get_result->header.size = htons (msize);
2403   get_result->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT);
2404   get_result->key = *key;
2405   get_result->querying_peer = *source_peer;
2406   get_result->expiration_time = expiration;
2407   get_result->get_path_length = htonl (get_path_length);
2408   get_result->put_path_length = htonl (put_path_length);
2409   paths = (struct GNUNET_PeerIdentity *)&get_result[1];
2410   memcpy (paths, put_path,
2411           put_path_length * sizeof (struct GNUNET_PeerIdentity));
2412   memcpy (&paths[put_path_length], get_path,
2413           get_path_length * sizeof (struct GNUNET_PeerIdentity));
2414   memcpy (&paths[put_path_length + get_path_length], data, data_size);
2415
2416   GNUNET_assert (NULL !=
2417                 (target_friend =
2418                  GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2419                                                     &get_path[current_path_index - 1])));
2420   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2421   target_friend->pending_count++;
2422   process_friend_queue (target_friend);
2423 }
2424
2425
2426 /**
2427  * Randomly choose one of your friends (which is not congested and have not crossed
2428  * trail threshold) from the friend_peermap
2429  * @return Friend Randomly chosen friend.
2430  *         NULL in case friend peermap is empty, or all the friends are either
2431  *              congested or have crossed trail threshold.
2432  */
2433 static struct FriendInfo *
2434 select_random_friend ()
2435 {
2436   unsigned int current_size;
2437   uint32_t index;
2438   unsigned int j = 0;
2439   struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
2440   struct GNUNET_PeerIdentity key_ret;
2441   struct FriendInfo *friend;
2442
2443   current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
2444
2445   /* No friends.*/
2446   if (0 == current_size)
2447     return NULL;
2448
2449   index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
2450   iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
2451
2452   /* Iterate till you don't reach to index. */
2453   for (j = 0; j < index ; j++)
2454     GNUNET_assert (GNUNET_YES ==
2455                    GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL, NULL));
2456   do
2457   {
2458     /* Reset the index in friend peermap to 0 as we reached to the end. */
2459     if (j == current_size)
2460     {
2461       j = 0;
2462       GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
2463       iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
2464
2465     }
2466
2467     /* Get the friend stored at the index, j*/
2468     GNUNET_assert (GNUNET_YES ==
2469                    GNUNET_CONTAINER_multipeermap_iterator_next (iter,
2470                                                                 &key_ret,
2471                                                                 (const void **)&friend));
2472
2473     /* This friend is not congested and has not crossed trail threshold. */
2474     if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) &&
2475         (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us))
2476     {
2477       break;
2478     }
2479     friend = NULL;
2480     j++;
2481   } while (j != index);
2482
2483   GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
2484   return friend;
2485 }
2486
2487
2488 /**
2489  * Compute 64 bit value of finger_identity corresponding to a finger index using
2490  * chord formula.
2491  * For all fingers, n.finger[i] = n + pow (2,i),
2492  * For predecessor, n.finger[PREDECESSOR_FINGER_ID] = n - 1, where
2493  * n = my_identity, i = finger_index, n.finger[i] = 64 bit finger value
2494  * @param finger_index Index corresponding to which we calculate 64 bit value.
2495  * @return 64 bit value.
2496  */
2497 static uint64_t
2498 compute_finger_identity_value (unsigned int finger_index)
2499 {
2500   uint64_t my_id64;
2501
2502   memcpy (&my_id64, &my_identity, sizeof (uint64_t));
2503   my_id64 = GNUNET_ntohll (my_id64);
2504
2505   /* Are we looking for immediate predecessor? */
2506   if (PREDECESSOR_FINGER_ID == finger_index)
2507     return (my_id64 - 1);
2508   else
2509   {
2510     uint64_t add = (uint64_t)1 << finger_index;
2511     return (my_id64 + add);
2512   }
2513 }
2514
2515 static struct GNUNET_TIME_Relative next_send_time;
2516
2517 /*
2518  * Choose a random friend. Calculate the next finger identity to search,from
2519  * current_search_finger_index. Start looking for the trail to reach to
2520  * finger identity through this random friend.
2521  *
2522  * @param cls closure for this task
2523  * @param tc the context under which the task is running
2524  */
2525 static void
2526 send_find_finger_trail_message (void *cls,
2527                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
2528 {
2529   struct FriendInfo *target_friend;
2530   //struct GNUNET_TIME_Relative next_send_time;
2531   struct GNUNET_HashCode trail_id;
2532   struct GNUNET_HashCode intermediate_trail_id;
2533   unsigned int is_predecessor;
2534   uint64_t finger_id_value;
2535   
2536   /* Schedule another send_find_finger_trail_message task. */
2537   find_finger_trail_task =
2538       GNUNET_SCHEDULER_add_delayed (next_send_time,
2539                                     &send_find_finger_trail_message,
2540                                     NULL);
2541
2542    /* No space in my routing table. (Source and destination peers also store entries
2543    * in their routing table).  */
2544   if (GNUNET_YES == GDS_ROUTING_threshold_reached())
2545     return;
2546
2547
2548   target_friend = select_random_friend ();
2549   if (NULL == target_friend)
2550   {
2551     return;
2552   }
2553
2554   finger_id_value = compute_finger_identity_value (current_search_finger_index);
2555
2556   if (PREDECESSOR_FINGER_ID == current_search_finger_index)
2557     is_predecessor = 1;
2558   else
2559     is_predecessor = 0;
2560
2561   /* Generate a unique trail id for trail we are trying to setup. */
2562   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
2563                               &trail_id, sizeof (trail_id));
2564   memset(&intermediate_trail_id, 0, sizeof (struct GNUNET_HashCode));
2565
2566   GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_id_value,
2567                                    target_friend->id, target_friend, 0, NULL,
2568                                    is_predecessor, trail_id,
2569                                    intermediate_trail_id);
2570 }
2571
2572
2573 /**
2574  * In case there are already maximum number of possible trails to reach to a
2575  * finger, then check if the new trail's length is lesser than any of the
2576  * existing trails.
2577  * If yes then replace that old trail by new trail.
2578  *
2579  * Note: Here we are taking length as a parameter to choose the best possible
2580  * trail, but there could be other parameters also like:
2581  * 1. duration of existence of a trail - older the better.
2582  * 2. if the new trail is completely disjoint than the
2583  *    other trails, then may be choosing it is better.
2584  *
2585  * @param existing_finger
2586  * @param new_finger_trail
2587  * @param new_finger_trail_length
2588  * @param new_finger_trail_id
2589  */
2590 static void
2591 select_and_replace_trail (struct FingerInfo *existing_finger,
2592                           const struct GNUNET_PeerIdentity *new_trail,
2593                           unsigned int new_trail_length,
2594                           struct GNUNET_HashCode new_trail_id)
2595 {
2596   struct Trail *trail_list_iterator;
2597   unsigned int largest_trail_length;
2598   unsigned int largest_trail_index;
2599   struct Trail_Element *trail_element;
2600   unsigned int i;
2601
2602   largest_trail_length = new_trail_length;
2603   largest_trail_index = MAXIMUM_TRAILS_PER_FINGER + 1;
2604
2605   GNUNET_assert (MAXIMUM_TRAILS_PER_FINGER == existing_finger->trails_count);
2606
2607   for (i = 0; i < existing_finger->trails_count; i++)
2608   {
2609     trail_list_iterator = &existing_finger->trail_list[i];
2610     if (trail_list_iterator->trail_length > largest_trail_length)
2611     {
2612       largest_trail_length = trail_list_iterator->trail_length;
2613       largest_trail_index = i;
2614     }
2615   }
2616
2617   /* New trail is not better than existing ones. Send trail teardown. */
2618   if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
2619   {
2620     struct GNUNET_PeerIdentity next_hop;
2621
2622     memcpy (&next_hop, &new_trail[0], sizeof(struct GNUNET_PeerIdentity));
2623     GDS_ROUTING_remove_trail (new_trail_id);
2624     GDS_NEIGHBOURS_send_trail_teardown (new_trail_id,
2625                                         GDS_ROUTING_SRC_TO_DEST,
2626                                         next_hop);
2627     return;
2628   }
2629
2630   /* Send trail teardown message across the replaced trail. */
2631   struct Trail *replace_trail = &existing_finger->trail_list[largest_trail_index];
2632   existing_finger->trail_list[largest_trail_index].is_present = GNUNET_NO;
2633   GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (replace_trail->trail_id));
2634   GDS_NEIGHBOURS_send_trail_teardown (replace_trail->trail_id,
2635                                       GDS_ROUTING_SRC_TO_DEST,
2636                                       replace_trail->trail_head->peer);
2637   /* Free the trail. */
2638   while (NULL != (trail_element = replace_trail->trail_head))
2639   {
2640     GNUNET_CONTAINER_DLL_remove (replace_trail->trail_head,
2641                                  replace_trail->trail_tail, trail_element);
2642     GNUNET_free_non_null (trail_element);
2643   }
2644
2645   /* Add new trial at that location. */
2646   replace_trail->is_present = GNUNET_YES;
2647   replace_trail->trail_length = new_trail_length;
2648   replace_trail->trail_id = new_trail_id;
2649   //FIXME: Do we need to add pointers for head and tail.
2650   i = 0;
2651   while (i < new_trail_length)
2652   {
2653     struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2654     element->peer = new_trail[i];
2655
2656     GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
2657                                       replace_trail->trail_tail,
2658                                       element);
2659   }
2660 }
2661
2662
2663 /**
2664  * Check if the new trail to reach to finger is unique or do we already have
2665  * such a trail present for finger.
2666  * @param existing_finger Finger identity
2667  * @param new_trail New trail to reach @a existing_finger
2668  * @param trail_length Total number of peers in new_trail.
2669  * @return #GNUNET_YES if the new trail is unique
2670  *         #GNUNET_NO if same trail is already present.
2671  */
2672 static int
2673 is_new_trail_unique (struct FingerInfo *existing_finger,
2674                      const struct GNUNET_PeerIdentity *new_trail,
2675                      unsigned int trail_length)
2676 {
2677   struct Trail *trail_list_iterator;
2678   struct Trail_Element *trail_element;
2679   int i;
2680   int j;
2681   int trail_unique = GNUNET_NO;
2682
2683   GNUNET_assert (existing_finger->trails_count > 0);
2684
2685   /* Iterate over list of trails. */
2686   for (i = 0; i < existing_finger->trails_count; i++)
2687   {
2688     trail_list_iterator = &existing_finger->trail_list[i];
2689     GNUNET_assert (GNUNET_YES == trail_list_iterator->is_present);
2690
2691     /* New trail and existing trail length are not same. */
2692     if (trail_list_iterator->trail_length != trail_length)
2693     {
2694       trail_unique = GNUNET_YES;
2695       continue;
2696     }
2697
2698     trail_element = trail_list_iterator->trail_head;
2699     for (j = 0; j < trail_list_iterator->trail_length; j++)
2700     {
2701       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&new_trail[j],
2702                                                 &trail_element->peer))
2703       {
2704         trail_unique = GNUNET_YES;
2705         continue;
2706       }
2707       trail_element = trail_element->next;
2708     }
2709
2710     trail_unique = GNUNET_NO;
2711   }
2712
2713   return trail_unique;
2714 }
2715
2716
2717 /**
2718  * Add a new trail to existing finger. This function is called only when finger
2719  * is not my own identity or a friend.
2720  * @param existing_finger Finger
2721  * @param new_finger_trail New trail from me to finger, NOT including endpoints
2722  * @param new_finger_trail_length Total number of peers in @a new_finger_trail
2723  * @param new_finger_trail_id Unique identifier of the trail.
2724  */
2725 static void
2726 add_new_trail (struct FingerInfo *existing_finger,
2727                const struct GNUNET_PeerIdentity *new_trail,
2728                unsigned int new_trail_length,
2729                struct GNUNET_HashCode new_trail_id)
2730 {
2731   struct Trail *trail_list_iterator;
2732   struct FriendInfo *first_friend;
2733   int i;
2734
2735   if (GNUNET_NO == is_new_trail_unique (existing_finger, new_trail,
2736                                         new_trail_length))
2737   {
2738     return;
2739   }
2740
2741   trail_list_iterator = &existing_finger->trail_list[existing_finger->trails_count];
2742   GNUNET_assert (GNUNET_NO == trail_list_iterator->is_present);
2743   trail_list_iterator->trail_id = new_trail_id;
2744   trail_list_iterator->trail_length = new_trail_length;
2745   existing_finger->trails_count++;
2746   trail_list_iterator->is_present = GNUNET_YES;
2747
2748   GNUNET_assert (NULL == (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2749                                                              &existing_finger->finger_identity)));
2750   /* If finger is a friend then we never call this function. */
2751   GNUNET_assert (new_trail_length > 0);
2752
2753   first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2754                                                     &new_trail[0]);
2755   first_friend->trails_count++;
2756
2757   for (i = 0; i < new_trail_length; i++)
2758   {
2759     struct Trail_Element *element;
2760
2761     element = GNUNET_new (struct Trail_Element);
2762     element->peer = new_trail[i];
2763     GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head,
2764                                       trail_list_iterator->trail_tail,
2765                                       element);
2766   }
2767   /* Do we need to add trail head and trail tail in the trail list itearator.*/
2768
2769 }
2770
2771
2772 /**
2773  * FIXME Check if this function is called for opposite direction if yes then
2774  * take it as parameter.
2775  * Get the next hop to send trail teardown message from routing table and
2776  * then delete the entry from routing table. Send trail teardown message for a
2777  * specific trail of a finger.
2778  * @param finger Finger whose trail is to be removed.
2779  * @param trail List of peers in trail from me to a finger, NOT including
2780  *              endpoints.
2781  */
2782 static void
2783 send_trail_teardown (struct FingerInfo *finger,
2784                      struct Trail *trail)
2785 {
2786   struct FriendInfo *friend;
2787   struct GNUNET_PeerIdentity *next_hop;
2788
2789   next_hop = GDS_ROUTING_get_next_hop (trail->trail_id,
2790                                        GDS_ROUTING_SRC_TO_DEST);
2791   
2792   if (NULL == next_hop)
2793   {
2794     GNUNET_break(0);
2795     return;
2796   }
2797   GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
2798                                                        &my_identity));
2799
2800   GNUNET_assert (trail->is_present == GNUNET_YES);
2801
2802   /* Finger is not a friend. */
2803   if (trail->trail_length > 0)
2804   {
2805     GNUNET_assert (NULL != (friend =
2806                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2807                                                       &trail->trail_head->peer)));
2808   }
2809   else
2810   {
2811     GNUNET_assert (NULL != (friend =
2812                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2813                                                       &finger->finger_identity)));
2814   }
2815
2816   GNUNET_assert (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &friend->id)); //Fixme: assertion fails.
2817   GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail->trail_id));
2818   friend->trails_count--;
2819   GDS_NEIGHBOURS_send_trail_teardown (trail->trail_id,
2820                                       GDS_ROUTING_SRC_TO_DEST,
2821                                       friend->id);
2822 }
2823
2824
2825 /**
2826  * Send trail teardown message across all the trails to reach to finger.
2827  * @param finger Finger whose all the trail should be freed.
2828  */
2829 static void
2830 send_all_finger_trails_teardown (struct FingerInfo *finger)
2831 {
2832   unsigned int i;
2833
2834   for (i = 0; i < finger->trails_count; i++)
2835   {
2836     struct Trail *trail;
2837
2838     trail = &finger->trail_list[i];
2839     GNUNET_assert (trail->is_present == GNUNET_YES);
2840     send_trail_teardown (finger, trail);
2841     trail->is_present = GNUNET_NO;
2842    }
2843 }
2844
2845
2846 /**
2847  * Free a specific trail
2848  * @param trail List of peers to be freed.
2849  */
2850 static void
2851 free_trail (struct Trail *trail)
2852 {
2853   struct Trail_Element *trail_element;
2854
2855   while (NULL != (trail_element = trail->trail_head))
2856   {
2857     GNUNET_CONTAINER_DLL_remove (trail->trail_head,
2858                                  trail->trail_tail,
2859                                  trail_element);
2860     GNUNET_free_non_null (trail_element);
2861   }
2862   trail->trail_head = NULL;
2863   trail->trail_tail = NULL;
2864 }
2865
2866
2867 /**
2868  * Free finger and its trail.
2869  * @param finger Finger to be freed.
2870  */
2871 static void
2872 free_finger (struct FingerInfo *finger, unsigned int finger_table_index)
2873 {
2874   struct Trail *trail;
2875   unsigned int i;
2876
2877   /* Free all the trails to reach to finger */
2878   for (i = 0; i < finger->trails_count; i++)
2879   {
2880     trail = &finger->trail_list[i];
2881     //FIXME: Check if there are any missing entry in this list because of
2882     // how we insert. If not then no need of this check.
2883     if (GNUNET_NO == trail->is_present)
2884       continue;
2885
2886     if (trail->trail_length > 0)
2887     {
2888       free_trail (trail);
2889     }
2890     trail->is_present = GNUNET_NO;
2891   }
2892
2893   finger->is_present = GNUNET_NO;
2894   memset ((void *)&finger_table[finger_table_index], 0, sizeof (finger_table[finger_table_index]));
2895 }
2896
2897
2898 /**
2899  * Add a new entry in finger table at finger_table_index.
2900  * In case I am my own finger, then we don't have a trail. In case of a friend,
2901  * we have a trail with unique id and '0' trail length.
2902  * In case a finger is a friend, then increment the trails count of the friend.
2903  * @param finger_identity Peer Identity of new finger
2904  * @param finger_trail Trail to reach from me to finger (excluding both end points).
2905  * @param finger_trail_length Total number of peers in @a finger_trail.
2906  * @param trail_id Unique identifier of the trail.
2907  * @param finger_table_index Index in finger table.
2908  */
2909 static void
2910 add_new_finger (struct GNUNET_PeerIdentity finger_identity,
2911                 const struct GNUNET_PeerIdentity *finger_trail,
2912                 unsigned int finger_trail_length,
2913                 struct GNUNET_HashCode trail_id,
2914                 unsigned int finger_table_index)
2915 {
2916   struct FingerInfo *new_entry;
2917   struct FriendInfo *first_trail_hop;
2918   struct Trail *trail;
2919   int i = 0;
2920
2921   new_entry = GNUNET_new (struct FingerInfo);
2922   new_entry->finger_identity = finger_identity;
2923   new_entry->finger_table_index = finger_table_index;
2924   new_entry->is_present = GNUNET_YES;
2925
2926   /* If the new entry is my own identity. */
2927   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2928                                             &finger_identity))
2929   {
2930     new_entry->trails_count = 0;
2931     finger_table[finger_table_index] = *new_entry;
2932     GNUNET_free (new_entry);
2933     return;
2934   }
2935
2936   /* If finger is a friend, then we don't actually have a trail.
2937    *  Just a trail id */
2938   if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2939                                                  &finger_identity))
2940   {
2941     new_entry->trail_list[0].trail_id = trail_id;
2942     new_entry->trails_count = 1;
2943     new_entry->trail_list[0].is_present = GNUNET_YES;
2944     new_entry->trail_list[0].trail_length = 0;
2945     new_entry->trail_list[0].trail_head = NULL;
2946     new_entry->trail_list[0].trail_tail = NULL;
2947     finger_table[finger_table_index] = *new_entry;
2948     GNUNET_assert (NULL !=
2949                   (first_trail_hop =
2950                        GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2951                                                           &finger_identity)));
2952
2953     first_trail_hop->trails_count++;
2954     GNUNET_free (new_entry);
2955     return;
2956   }
2957
2958   GNUNET_assert (NULL !=
2959                 (first_trail_hop =
2960                        GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2961                                                           &finger_trail[0])));
2962   new_entry->trails_count = 1;
2963   first_trail_hop->trails_count++;
2964
2965   /* Copy the finger trail into trail. */
2966   trail = GNUNET_new (struct Trail);
2967   while (i < finger_trail_length)
2968   {
2969     struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2970
2971     element->next = NULL;
2972     element->prev = NULL;
2973     element->peer = finger_trail[i];
2974     GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
2975                                       trail->trail_tail,
2976                                       element);
2977     i++;
2978   }
2979
2980   /* Add trail to trail list. */
2981   new_entry->trail_list[0].trail_head = trail->trail_head;
2982   new_entry->trail_list[0].trail_tail = trail->trail_tail;
2983   new_entry->trail_list[0].trail_length = finger_trail_length;
2984   new_entry->trail_list[0].trail_id = trail_id;
2985   new_entry->trail_list[0].is_present = GNUNET_YES;
2986   finger_table[finger_table_index] = *new_entry;
2987   GNUNET_free (new_entry);
2988   return;
2989 }
2990
2991
2992 /**
2993  * Scan the trail to check if there is any other friend in the trail other than
2994  * first hop. If yes then shortcut the trail, send trail compression message to
2995  * peers which are no longer part of trail and send back the updated trail
2996  * and trail_length to calling function.
2997  * @param finger_identity Finger whose trail we will scan.
2998  * @param finger_trail [in, out] Trail to reach from source to finger,
2999  * @param finger_trail_length  Total number of peers in original finger_trail.
3000  * @param finger_trail_id Unique identifier of the finger trail.
3001  * @return updated trail length in case we shortcut the trail, else original
3002  *         trail length.
3003  */
3004 static struct GNUNET_PeerIdentity *
3005 scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
3006                          const struct GNUNET_PeerIdentity *trail,
3007                          unsigned int trail_length,
3008                          struct GNUNET_HashCode trail_id,
3009                          int *new_trail_length)
3010 {
3011   struct FriendInfo *target_friend;
3012   struct GNUNET_PeerIdentity *new_trail;
3013   unsigned int i;
3014
3015   /* I am my own finger. */
3016   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
3017   {
3018     *new_trail_length = 0;
3019     return NULL;
3020   }
3021
3022   if (0 == trail_length)
3023   {
3024     *new_trail_length = 0;
3025     return NULL;
3026   }
3027
3028   /* If finger identity is a friend. */
3029   if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger_identity))
3030   {
3031     *new_trail_length = 0;
3032
3033     /* If there is trail to reach this finger/friend */
3034     if (trail_length > 0)
3035     {
3036       /* Finger is your first friend. */
3037       GDS_ROUTING_update_trail_next_hop (trail_id, finger_identity);
3038       GNUNET_assert (NULL !=
3039                     (target_friend =
3040                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3041                                                         &trail[0])));
3042
3043
3044       GDS_NEIGHBOURS_send_trail_compression (my_identity,
3045                                              trail_id, finger_identity,
3046                                              target_friend);
3047     }
3048     return NULL;
3049   }
3050
3051   /*  For other cases, when its neither a friend nor my own identity.*/
3052   for (i = trail_length - 1; i > 0; i--)
3053   {
3054     /* If the element at this index in trail is a friend. */
3055     if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail[i]))
3056     {
3057       struct FriendInfo *target_friend;
3058       int j = 0;
3059
3060       GNUNET_assert (NULL !=
3061                     (target_friend =
3062                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3063                                                         &trail[0])));
3064       GDS_ROUTING_update_trail_next_hop (trail_id, trail[i]);
3065       GDS_NEIGHBOURS_send_trail_compression (my_identity,
3066                                              trail_id, trail[i],
3067                                              target_friend);
3068
3069
3070       /* Copy the trail from index i to index (trail_length -1) into a new trail
3071        *  and update new trail length */
3072       new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * (trail_length - i));
3073       while (i < trail_length)
3074       {
3075         memcpy (&new_trail[j], &trail[i], sizeof(struct GNUNET_PeerIdentity));
3076         j++;
3077         i++;
3078       }
3079       *new_trail_length = j+1;
3080       return new_trail;
3081     }
3082   }
3083
3084   /* If we did not compress the trail, return the original trail back.*/
3085   *new_trail_length = trail_length;
3086   new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * trail_length);  
3087   memcpy (new_trail, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
3088   return new_trail;
3089 }
3090
3091
3092 /**
3093  * Periodic task to verify current successor. There can be multiple trails to reach
3094  * to successor, choose the shortest one and send verify successor message
3095  * across that trail.
3096  * @param cls closure for this task
3097  * @param tc the context under which the task is running
3098  */
3099 static void
3100 send_verify_successor_message (void *cls,
3101                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
3102 {
3103   struct FriendInfo *target_friend;
3104   struct GNUNET_HashCode trail_id;
3105   int i;
3106   struct GNUNET_TIME_Relative next_send_time;
3107   struct Trail *trail;
3108   struct Trail_Element *element;
3109   unsigned int trail_length;
3110   unsigned int j = 0;
3111   struct FingerInfo *successor;
3112
3113   /* Schedule another send_find_finger_trail_message task. */
3114   next_send_time.rel_value_us =
3115       DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
3116       GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3117                                 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
3118   send_verify_successor_task =
3119       GNUNET_SCHEDULER_add_delayed (next_send_time, &send_verify_successor_message,
3120                                     NULL);
3121
3122   successor = &finger_table[0];
3123   for (i = 0; i < successor->trails_count; i++)
3124   {
3125     trail = &successor->trail_list[i];
3126     if(GNUNET_YES == trail->is_present)
3127       break;
3128   }
3129   
3130   if (i == successor->trails_count)
3131     return;
3132   
3133   GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
3134                                                       &successor->finger_identity));
3135
3136   /* Trail stored at this index. */
3137   GNUNET_assert (GNUNET_YES == trail->is_present);
3138   
3139   trail_id = trail->trail_id;
3140   trail_length = trail->trail_length;
3141   
3142   if (trail_length > 0)
3143   {
3144      /* Copy the trail into peer list. */
3145     struct GNUNET_PeerIdentity peer_list[trail_length];
3146
3147     element = trail->trail_head;
3148     while (j < trail_length)
3149     {
3150       peer_list[j] = element->peer;
3151       element = element->next;
3152       j++;
3153     }
3154
3155     GNUNET_assert (NULL != (target_friend =
3156                             GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3157                                                                &peer_list[0])));
3158     GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
3159                                                   successor->finger_identity,
3160                                                   trail_id, peer_list, trail_length,
3161                                                   target_friend);
3162     return;
3163   }
3164   else
3165   {
3166     GNUNET_assert (NULL != (target_friend =
3167                             GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3168                                                                &successor->finger_identity)));
3169     GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
3170                                                   successor->finger_identity,
3171                                                   trail_id, NULL, 0,
3172                                                   target_friend);
3173     return;
3174   }
3175 }
3176
3177
3178 /**
3179  * Update the current search finger index.
3180  *
3181  * FIXME document parameters!
3182  */
3183 static void
3184 update_current_search_finger_index (struct GNUNET_PeerIdentity finger_identity,
3185                                     unsigned int finger_table_index)
3186 {
3187   struct FingerInfo *successor;
3188
3189   /* FIXME correct this: only move current index periodically */
3190   if (finger_table_index != current_search_finger_index)
3191     return;
3192
3193   successor = &finger_table[0];
3194   GNUNET_assert (GNUNET_YES == successor->is_present);
3195
3196   /* We were looking for immediate successor.  */
3197   if (0 == current_search_finger_index)
3198   {
3199     /* Start looking for immediate predecessor. */
3200     current_search_finger_index = PREDECESSOR_FINGER_ID;
3201
3202     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
3203     {
3204       if (GNUNET_SCHEDULER_NO_TASK == send_verify_successor_task)
3205         send_verify_successor_task = GNUNET_SCHEDULER_add_now (&send_verify_successor_message, NULL);
3206     }
3207
3208     return;
3209   }
3210
3211   current_search_finger_index = current_search_finger_index - 1;
3212   return;
3213 }
3214
3215
3216 /**
3217  * Get the least significant bit set in val.
3218  *
3219  * @param val Value
3220  * @return Position of first bit set, 65 in case of error.
3221  */
3222 static unsigned int
3223 find_set_bit (uint64_t val)
3224 {
3225   uint64_t i;
3226   unsigned int pos;
3227
3228   i = 1;
3229   pos = 0;
3230
3231   while (!(i & val))
3232   {
3233     i = i << 1;
3234     pos++;
3235     if (pos > 63)
3236     {
3237       GNUNET_break (0);
3238       return 65;
3239     }
3240   }
3241
3242   if (val/i != 1)
3243     return 65; /* Some other bit was set to 1 as well. */
3244
3245   return pos;
3246 }
3247
3248
3249 /**
3250  * Calculate finger_table_index from initial 64 bit finger identity value that
3251  * we send in trail setup message.
3252  * @param ultimate_destination_finger_value Value that we calculated from our
3253  *                                          identity and finger_table_index.
3254  * @param is_predecessor Is the entry for predecessor or not?
3255  * @return finger_table_index Value between 0 <= finger_table_index <= 64
3256  *         finger_table_index > PREDECESSOR_FINGER_ID, if error occurs.
3257  */
3258 static unsigned int
3259 get_finger_table_index (uint64_t ultimate_destination_finger_value,
3260                         unsigned int is_predecessor)
3261 {
3262   uint64_t my_id64;
3263   uint64_t diff;
3264   unsigned int finger_table_index;
3265
3266   memcpy (&my_id64, &my_identity, sizeof (uint64_t));
3267   my_id64 = GNUNET_ntohll (my_id64);
3268
3269   /* Is this a predecessor finger? */
3270   if (1 == is_predecessor)
3271   {
3272     diff =  my_id64 - ultimate_destination_finger_value;
3273     if (1 == diff)
3274       finger_table_index = PREDECESSOR_FINGER_ID;
3275     else
3276       finger_table_index = PREDECESSOR_FINGER_ID + 1; //error value
3277
3278   }
3279   else
3280   {
3281     diff = ultimate_destination_finger_value - my_id64;
3282     finger_table_index = find_set_bit (diff);
3283   }
3284   return finger_table_index;
3285 }
3286
3287
3288 /**
3289  * Remove finger and its associated data structures from finger table.
3290  * @param finger Finger to be removed.
3291  */
3292 static void
3293 remove_existing_finger (struct FingerInfo *existing_finger,
3294                         unsigned int finger_table_index)
3295 {
3296   struct FingerInfo *finger;
3297
3298   finger = &finger_table[finger_table_index];
3299   GNUNET_assert (GNUNET_YES == finger->is_present);
3300
3301   /* If I am my own finger, then we have no trails. */
3302   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
3303                                             &my_identity))
3304   {
3305     finger->is_present = GNUNET_NO;
3306     memset ((void *)&finger_table[finger_table_index], 0,
3307             sizeof (finger_table[finger_table_index]));
3308     return;
3309   }
3310
3311   /* For all other fingers, send trail teardown across all the trails to reach
3312    finger, and free the finger. */
3313   send_all_finger_trails_teardown (finger);
3314   free_finger (finger, finger_table_index);
3315   return;
3316 }
3317
3318
3319 /**
3320  * Check if there is already an entry in finger_table at finger_table_index.
3321  * We get the finger_table_index from 64bit finger value we got from the network.
3322  * -- If yes, then select the closest finger.
3323  *   -- If new and existing finger are same, then check if you can store more
3324  *      trails.
3325  *      -- If yes then add trail, else keep the best trails to reach to the
3326  *         finger.
3327  *   -- If the new finger is closest, remove the existing entry, send trail
3328  *      teardown message across all the trails to reach the existing entry.
3329  *      Add the new finger.
3330  *  -- If new and existing finger are different, and existing finger is closest
3331  *     then do nothing.
3332  * -- Update current_search_finger_index.
3333  * @param finger_identity Peer Identity of new finger
3334  * @param finger_trail Trail to reach the new finger
3335  * @param finger_trail_length Total number of peers in @a new_finger_trail.
3336  * @param is_predecessor Is this entry for predecessor in finger_table?
3337  * @param finger_value 64 bit value of finger identity that we got from network.
3338  * @param finger_trail_id Unique identifier of @finger_trail.
3339  */
3340 static void
3341 finger_table_add (struct GNUNET_PeerIdentity finger_identity,
3342                   const struct GNUNET_PeerIdentity *finger_trail,
3343                   unsigned int finger_trail_length,
3344                   unsigned int is_predecessor,
3345                   uint64_t finger_value,
3346                   struct GNUNET_HashCode finger_trail_id)
3347 {
3348   struct FingerInfo *existing_finger;
3349   const struct GNUNET_PeerIdentity *closest_peer;
3350   struct FingerInfo *successor;
3351   int updated_finger_trail_length;
3352   unsigned int finger_table_index;
3353
3354   /* Get the finger_table_index corresponding to finger_value we got from network.*/
3355   finger_table_index = get_finger_table_index (finger_value, is_predecessor);
3356
3357   /* Invalid finger_table_index. */
3358   if ((finger_table_index > PREDECESSOR_FINGER_ID))
3359   {
3360     GNUNET_break_op (0);
3361     return;
3362   }
3363
3364   /* New entry same as successor. */
3365   if ((0 != finger_table_index) &&
3366       (PREDECESSOR_FINGER_ID != finger_table_index))
3367   {
3368     successor = &finger_table[0];
3369     if (GNUNET_NO == successor->is_present)
3370     {
3371       GNUNET_break_op (0);
3372       return;
3373     }
3374     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
3375                                               &successor->finger_identity))
3376     {
3377       current_search_finger_index = 0;
3378       /* We slow down the find_finger_trail_task as we have completed the circle. */
3379       next_send_time = GNUNET_TIME_STD_BACKOFF(next_send_time);
3380      
3381       return;
3382     }
3383     
3384     struct FingerInfo prev_finger;
3385     prev_finger = finger_table[finger_table_index - 1];
3386     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
3387                                               &prev_finger.finger_identity))
3388     {
3389        current_search_finger_index--;
3390        return;
3391     }
3392   }
3393
3394   existing_finger = &finger_table[finger_table_index];
3395
3396   /* No entry present in finger_table for given finger map index. */
3397   if (GNUNET_NO == existing_finger->is_present)
3398   {
3399     struct GNUNET_PeerIdentity *updated_trail;
3400
3401     /* Shorten the trail if possible. */
3402     updated_finger_trail_length = finger_trail_length;
3403     updated_trail = scan_and_compress_trail (finger_identity, finger_trail,
3404                                              finger_trail_length,
3405                                              finger_trail_id,
3406                                              &updated_finger_trail_length);
3407
3408     add_new_finger (finger_identity, updated_trail,
3409                     updated_finger_trail_length,
3410                     finger_trail_id, finger_table_index);
3411     GNUNET_free_non_null(updated_trail);
3412     update_current_search_finger_index (finger_identity,
3413                                         finger_table_index);
3414     return;
3415   }
3416
3417
3418   /* If existing entry and finger identity are not same. */
3419   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
3420                                             &finger_identity))
3421   {
3422     closest_peer = select_closest_peer (&existing_finger->finger_identity,
3423                                         &finger_identity,
3424                                         finger_value,
3425                                         is_predecessor);
3426
3427     /* If the new finger is the closest peer. */
3428     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, closest_peer))
3429     {
3430       struct GNUNET_PeerIdentity *updated_trail;
3431       /* Shorten the trail if possible. */
3432       updated_finger_trail_length = finger_trail_length;
3433       updated_trail =
3434         scan_and_compress_trail (finger_identity, finger_trail,
3435                                  finger_trail_length, finger_trail_id,
3436                                  &updated_finger_trail_length);
3437       remove_existing_finger (existing_finger, finger_table_index);
3438       add_new_finger (finger_identity, updated_trail, updated_finger_trail_length,
3439                       finger_trail_id, finger_table_index);
3440       GNUNET_free_non_null((struct GNUNET_PeerIdentity *)updated_trail);
3441     }
3442     else
3443     {
3444       /* Existing finger is the closest one. We need to send trail teardown
3445          across the trail setup in routing table of all the peers. */
3446       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, &my_identity))
3447       {
3448         if (finger_trail_length > 0)
3449           GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id,
3450                                               GDS_ROUTING_SRC_TO_DEST,
3451                                               finger_trail[0]);
3452         else
3453           GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id,
3454                                               GDS_ROUTING_SRC_TO_DEST,
3455                                               finger_identity);
3456       }
3457     }
3458   }
3459   else
3460   {
3461     /* If both new and existing entry are same as my_identity, then do nothing. */
3462     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
3463                                               &my_identity))
3464     {
3465       return;
3466     }
3467     /* If the existing finger is not a friend. */
3468     if (NULL ==
3469         GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3470                                            &existing_finger->finger_identity))
3471     {
3472       struct GNUNET_PeerIdentity *updated_trail;
3473
3474       /* Shorten the trail if possible. */
3475       updated_finger_trail_length = finger_trail_length;
3476       updated_trail =
3477          scan_and_compress_trail (finger_identity, finger_trail,
3478                                   finger_trail_length, finger_trail_id,
3479                                   &updated_finger_trail_length);
3480       /* If there is space to store more trails. */
3481       if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
3482         add_new_trail (existing_finger, updated_trail,
3483                        updated_finger_trail_length, finger_trail_id);
3484       else
3485         select_and_replace_trail (existing_finger, updated_trail,
3486                                   updated_finger_trail_length, finger_trail_id);
3487
3488     }
3489   }
3490   update_current_search_finger_index (finger_identity, finger_table_index);
3491   return;
3492 }
3493
3494 /**
3495  * Core handler for P2P put messages.
3496  * @param cls closure
3497  * @param peer sender of the request
3498  * @param message message
3499  * @return #GNUNET_OK to keep the connection open,
3500  *         #GNUNET_SYSERR to close it (signal serious error)
3501  */
3502 static int
3503 handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
3504                     const struct GNUNET_MessageHeader *message)
3505 {
3506   struct PeerPutMessage *put;
3507   struct GNUNET_PeerIdentity *put_path;
3508   struct GNUNET_PeerIdentity best_known_dest;
3509   struct GNUNET_HashCode intermediate_trail_id;
3510   struct GNUNET_PeerIdentity *next_hop;
3511   enum GNUNET_DHT_RouteOption options;
3512   struct GNUNET_HashCode test_key;
3513   void *payload;
3514   size_t msize;
3515   uint32_t putlen;
3516   size_t payload_size;
3517   uint64_t key_value;
3518
3519   msize = ntohs (message->size);
3520   if (msize < sizeof (struct PeerPutMessage))
3521   {
3522     GNUNET_break_op (0);
3523     return GNUNET_OK;
3524   }
3525
3526   put = (struct PeerPutMessage *) message;
3527   putlen = ntohl (put->put_path_length);
3528
3529
3530   if ((msize <
3531        sizeof (struct PeerPutMessage) +
3532        putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3533       (putlen >
3534        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3535   {
3536     GNUNET_break_op (0);
3537     return GNUNET_OK;
3538   }
3539   DEBUG("GET FOR DATA_SIZE = %lu\n",msize);
3540   GNUNET_STATISTICS_update (GDS_stats,
3541                             gettext_noop
3542                             ("# Bytes received from other peers"), (int64_t) msize,
3543                             GNUNET_NO);
3544   
3545   best_known_dest = put->best_known_destination;
3546   put_path = (struct GNUNET_PeerIdentity *) &put[1];
3547   payload = &put_path[putlen];
3548   options = ntohl (put->options);
3549   intermediate_trail_id = put->intermediate_trail_id;
3550
3551   payload_size = msize - (sizeof (struct PeerPutMessage) +
3552                           putlen * sizeof (struct GNUNET_PeerIdentity));
3553
3554   switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
3555                                 payload, payload_size, &test_key))
3556   {
3557     case GNUNET_YES:
3558       if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
3559       {
3560         char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
3561         GNUNET_break_op (0);
3562         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3563                     "PUT with key `%s' for block with key %s\n",
3564                      put_s, GNUNET_h2s_full (&test_key));
3565         GNUNET_free (put_s);
3566         return GNUNET_OK;
3567       }
3568     break;
3569     case GNUNET_NO:
3570       GNUNET_break_op (0);
3571       return GNUNET_OK;
3572     case GNUNET_SYSERR:
3573       /* cannot verify, good luck */
3574       break;
3575   }
3576
3577    if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
3578   {
3579     switch (GNUNET_BLOCK_evaluate (GDS_block_context,
3580                                    ntohl (put->block_type),
3581                                    NULL,    /* query */
3582                                    NULL, 0, /* bloom filer */
3583                                    NULL, 0, /* xquery */
3584                                    payload, payload_size))
3585     {
3586     case GNUNET_BLOCK_EVALUATION_OK_MORE:
3587     case GNUNET_BLOCK_EVALUATION_OK_LAST:
3588       break;
3589
3590     case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3591     case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3592     case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
3593     case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3594     case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3595     case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3596     default:
3597       GNUNET_break_op (0);
3598       return GNUNET_OK;
3599     }
3600   }
3601
3602   /* extend 'put path' by sender */
3603   struct GNUNET_PeerIdentity pp[putlen + 1];
3604   if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
3605   {
3606     memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
3607     pp[putlen] = *peer;
3608     putlen++;
3609   }
3610   else
3611     putlen = 0;
3612
3613   memcpy (&key_value, &(put->key), sizeof (uint64_t));
3614   if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3615   {
3616     next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3617                                          GDS_ROUTING_SRC_TO_DEST);
3618     if (NULL == next_hop)
3619     {
3620       GNUNET_STATISTICS_update (GDS_stats,
3621                                 gettext_noop ("# Next hop to forward the packet not found "
3622                                 "trail setup request, packet dropped."),
3623                                 1, GNUNET_NO);
3624       return GNUNET_SYSERR;
3625     }
3626   }
3627   else
3628   {
3629     struct Closest_Peer successor;
3630     key_value = GNUNET_ntohll (key_value);
3631     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
3632     next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
3633     *next_hop = successor.next_hop;
3634     intermediate_trail_id = successor.trail_id;
3635     best_known_dest = successor.best_known_destination;
3636   }
3637
3638   GDS_CLIENTS_process_put (options,
3639                            ntohl (put->block_type),
3640                            ntohl (put->hop_count),
3641                            ntohl (put->desired_replication_level),
3642                            putlen, pp,
3643                            GNUNET_TIME_absolute_ntoh (put->expiration_time),
3644                            &put->key,
3645                            payload,
3646                            payload_size);
3647
3648   /* I am the final destination */
3649   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &best_known_dest))
3650   {
3651     DEBUG("PUT destination is me = %s,KEY = %s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(&(put->key)));
3652     GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
3653                               &(put->key),putlen, pp, ntohl (put->block_type),
3654                               payload_size, payload);
3655   }
3656   else
3657   {
3658     GDS_NEIGHBOURS_send_put (&put->key,
3659                              ntohl (put->block_type),ntohl (put->options),
3660                              ntohl (put->desired_replication_level),
3661                              best_known_dest, intermediate_trail_id, next_hop,
3662                              ntohl (put->hop_count), putlen, pp,
3663                              GNUNET_TIME_absolute_ntoh (put->expiration_time),
3664                              payload, payload_size);
3665   }
3666   return GNUNET_OK;
3667 }
3668
3669
3670 /**
3671  * Core handler for p2p get requests.
3672  *
3673  * @param cls closure
3674  * @param peer sender of the request
3675  * @param message message
3676  * @return #GNUNET_OK to keep the connection open,
3677  *         #GNUNET_SYSERR to close it (signal serious error)
3678  */
3679 static int
3680 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
3681                     const struct GNUNET_MessageHeader *message)
3682 {
3683   const struct PeerGetMessage *get;
3684   const struct GNUNET_PeerIdentity *get_path;
3685   struct GNUNET_PeerIdentity best_known_dest;
3686   struct GNUNET_HashCode intermediate_trail_id;
3687   struct GNUNET_PeerIdentity *next_hop;
3688   uint32_t get_length;
3689   uint64_t key_value;
3690   size_t msize;
3691
3692   msize = ntohs (message->size);
3693   if (msize < sizeof (struct PeerGetMessage))
3694   {
3695     GNUNET_break_op (0);
3696     return GNUNET_YES;
3697   }
3698
3699   get = (const struct PeerGetMessage *)message;
3700   get_length = ntohl (get->get_path_length);
3701   best_known_dest = get->best_known_destination;
3702   intermediate_trail_id = get->intermediate_trail_id;
3703   get_path = (const struct GNUNET_PeerIdentity *)&get[1];
3704
3705   if ((msize <
3706        sizeof (struct PeerGetMessage) +
3707        get_length * sizeof (struct GNUNET_PeerIdentity)) ||
3708        (get_length >
3709         GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3710   {
3711     GNUNET_break_op (0);
3712     return GNUNET_YES;
3713   }
3714   DEBUG("PUT FOR DATA_SIZE = %lu\n",msize);
3715   GNUNET_STATISTICS_update (GDS_stats,
3716                             gettext_noop
3717                             ("# Bytes received from other peers"), msize,
3718                             GNUNET_NO);
3719   
3720   /* Add sender to get path */
3721   struct GNUNET_PeerIdentity gp[get_length + 1];
3722   if (get_length > 0)
3723     memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
3724   gp[get_length] = *peer;
3725   get_length = get_length + 1;
3726
3727   memcpy (&key_value, &(get->key), sizeof (uint64_t));
3728   key_value = GNUNET_ntohll (key_value);
3729
3730   /* I am not the final destination. I am part of trail to reach final dest. */
3731   if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3732   {
3733     next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3734                                          GDS_ROUTING_SRC_TO_DEST);
3735     if (NULL == next_hop)
3736     {
3737       GNUNET_STATISTICS_update (GDS_stats,
3738                                 gettext_noop ("# Next hop to forward the packet not found "
3739                                 "GET request, packet dropped."),
3740                                 1, GNUNET_NO);
3741       return GNUNET_SYSERR;
3742     }
3743   }
3744   else
3745   {
3746     struct Closest_Peer successor;
3747
3748     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
3749     next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
3750     *next_hop = successor.next_hop;
3751     best_known_dest = successor.best_known_destination;
3752     intermediate_trail_id = successor.trail_id;
3753   }
3754
3755   GDS_CLIENTS_process_get (get->options, get->block_type,get->hop_count,
3756                            get->desired_replication_level, get->get_path_length,
3757                            gp, &get->key);
3758   /* I am the final destination. */
3759   if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &best_known_dest))
3760   {
3761     DEBUG("GET destination is me = %s,KEY = %s\n",GNUNET_i2s(&my_identity),GNUNET_h2s(&(get->key)));
3762     struct GNUNET_PeerIdentity final_get_path[get_length+1];
3763
3764     memcpy (final_get_path, gp, get_length * sizeof (struct GNUNET_PeerIdentity));
3765     memcpy (&final_get_path[get_length], &my_identity, sizeof (struct GNUNET_PeerIdentity));
3766     get_length = get_length + 1;
3767
3768     GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
3769                               get_length, final_get_path,
3770                               &final_get_path[get_length-2], &my_identity);
3771   }
3772   else
3773   {
3774     GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
3775                              get->desired_replication_level, best_known_dest,
3776                              intermediate_trail_id, next_hop, 0,
3777                              get_length, gp);
3778   }
3779   return GNUNET_YES;
3780 }
3781
3782
3783 /**
3784  * Core handler for get result
3785  * @param cls closure
3786  * @param peer sender of the request
3787  * @param message message
3788  * @return #GNUNET_OK to keep the connection open,
3789  *         #GNUNET_SYSERR to close it (signal serious error)
3790  */
3791 static int
3792 handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
3793                            const struct GNUNET_MessageHeader *message)
3794 {
3795   const struct PeerGetResultMessage *get_result;
3796   const struct GNUNET_PeerIdentity *get_path;
3797   const struct GNUNET_PeerIdentity *put_path;
3798   const void *payload;
3799   size_t payload_size;
3800   size_t msize;
3801   unsigned int getlen;
3802   unsigned int putlen;
3803   int current_path_index;
3804
3805   msize = ntohs (message->size);
3806   if (msize < sizeof (struct PeerGetResultMessage))
3807   {
3808     GNUNET_break_op (0);
3809     return GNUNET_YES;
3810   }
3811
3812   get_result = (const struct PeerGetResultMessage *)message;
3813   getlen = ntohl (get_result->get_path_length);
3814   putlen = ntohl (get_result->put_path_length);
3815
3816   if ((msize <
3817        sizeof (struct PeerGetResultMessage) +
3818        getlen * sizeof (struct GNUNET_PeerIdentity) +
3819        putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3820       (getlen >
3821        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity) ||
3822       (putlen >
3823          GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))))
3824   {
3825     GNUNET_break_op (0);
3826     return GNUNET_YES;
3827   }
3828   DEBUG("GET_RESULT  FOR DATA_SIZE = %lu\n",msize);
3829   GNUNET_STATISTICS_update (GDS_stats,
3830                             gettext_noop
3831                             ("# Bytes received from other peers"), msize,
3832                             GNUNET_NO);
3833   
3834   put_path = (const struct GNUNET_PeerIdentity *) &get_result[1];
3835   get_path = &put_path[putlen];
3836   payload = (const void *) &get_path[getlen];
3837   payload_size = msize - (sizeof (struct PeerGetResultMessage) +
3838                          (getlen + putlen) * sizeof (struct GNUNET_PeerIdentity));
3839
3840   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
3841   {
3842     GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
3843                               getlen, get_path, putlen,
3844                               put_path, get_result->type, payload_size, payload);
3845     return GNUNET_YES;
3846   }
3847   else
3848   {
3849     current_path_index = search_my_index (get_path, getlen);
3850     if (-1 == current_path_index )
3851     {
3852       GNUNET_break (0);
3853       return GNUNET_SYSERR;
3854     }
3855     GDS_NEIGHBOURS_send_get_result (&(get_result->key), get_result->type,
3856                                     &get_path[current_path_index - 1],
3857                                     &(get_result->querying_peer), putlen, put_path,
3858                                     getlen, get_path, get_result->expiration_time,
3859                                     payload, payload_size);
3860     return GNUNET_YES;
3861   }
3862   return GNUNET_SYSERR;
3863 }
3864
3865
3866 /**
3867  * Find the next hop to pass trail setup message. First find the local best known
3868  * hop from your own identity, friends and finger. If you were part of trail,
3869  * then get the next hop from routing table. Compare next_hop from routing table
3870  * and local best known hop, and return the closest one to final_dest_finger_val
3871  * @param final_dest_finger_val 64 bit value of finger identity
3872  * @param intermediate_trail_id If you are part of trail to reach to some other
3873  *                              finger, then it is the trail id to reach to
3874  *                              that finger, else set to 0.
3875  * @param is_predecessor Are we looking for closest successor or predecessor.
3876  * @param current_dest In case you are part of trail, then finger to which
3877  *                     we should forward the message. Else my own identity
3878  * @return Closest Peer for @a final_dest_finger_val
3879  */
3880 static struct Closest_Peer
3881 get_local_best_known_next_hop (uint64_t final_dest_finger_val,
3882                                struct GNUNET_HashCode intermediate_trail_id,
3883                                unsigned int is_predecessor,
3884                                 struct GNUNET_PeerIdentity prev_hop,
3885                                struct GNUNET_PeerIdentity source,
3886                                struct GNUNET_PeerIdentity *current_dest)
3887 {
3888   struct Closest_Peer peer;
3889
3890   /* Find a local best known peer. */
3891   peer = find_successor (final_dest_finger_val, is_predecessor);//FIXME: chnage to better name
3892
3893   /* Am I just a part of a trail towards a finger (current_destination)? */
3894   /* Select best successor among one found locally and current_destination
3895    * that we got from network.*/
3896   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest) &&
3897       0 != GNUNET_CRYPTO_cmp_peer_identity (&peer.best_known_destination,
3898                                             current_dest))
3899   {
3900     const struct GNUNET_PeerIdentity *closest_peer;
3901
3902     closest_peer = select_closest_peer (&peer.best_known_destination,
3903                                         current_dest,
3904                                         final_dest_finger_val,
3905                                         is_predecessor);
3906
3907     /* Is current dest (end point of the trail of which I am a part) closest_peer? */
3908     if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, closest_peer))
3909     {
3910       struct GNUNET_PeerIdentity *next_hop;
3911       
3912       next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3913                                            GDS_ROUTING_SRC_TO_DEST);
3914       /* It may happen that trail teardown message got delayed and hence,
3915          the previous hop sent the message over intermediate trail id.In that
3916          case next_hop could be NULL. */
3917       if(NULL != next_hop)
3918       {
3919          peer.next_hop = *next_hop;
3920          peer.best_known_destination =  *current_dest;
3921          peer.trail_id = intermediate_trail_id;
3922       }
3923     }
3924   }
3925   return peer;
3926 }
3927
3928 /*
3929  * Core handle for PeerTrailSetupMessage.
3930  * @param cls closure
3931  * @param message message
3932  * @param peer peer identity this notification is about
3933  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
3934  */
3935 static int
3936 handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
3937                             const struct GNUNET_MessageHeader *message)
3938 {
3939   const struct PeerTrailSetupMessage *trail_setup;
3940   const struct GNUNET_PeerIdentity *trail_peer_list;
3941   struct GNUNET_PeerIdentity current_dest;
3942   struct FriendInfo *target_friend;
3943   struct GNUNET_PeerIdentity source;
3944   uint64_t final_dest_finger_val;
3945   struct GNUNET_HashCode intermediate_trail_id;
3946   struct GNUNET_HashCode trail_id;
3947   unsigned int is_predecessor;
3948   uint32_t trail_length;
3949   unsigned int i;
3950   size_t msize;
3951
3952   msize = ntohs (message->size);
3953   if (msize < sizeof (struct PeerTrailSetupMessage))
3954   {
3955     GNUNET_break_op (0);
3956     return GNUNET_SYSERR;
3957   }
3958
3959   trail_setup = (const struct PeerTrailSetupMessage *) message;
3960   trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
3961                   sizeof (struct GNUNET_PeerIdentity);
3962   if ((msize - sizeof (struct PeerTrailSetupMessage)) %
3963       sizeof (struct GNUNET_PeerIdentity) != 0)
3964   {
3965     GNUNET_break_op (0);
3966     return GNUNET_OK;
3967   }
3968
3969   GNUNET_STATISTICS_update (GDS_stats,
3970                             gettext_noop
3971                             ("# Bytes received from other peers"), msize,
3972                             GNUNET_NO);
3973   
3974   trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_setup[1];
3975   current_dest = trail_setup->best_known_destination;
3976   trail_id = trail_setup->trail_id;
3977   final_dest_finger_val =
3978           GNUNET_ntohll (trail_setup->final_destination_finger_value);
3979   source = trail_setup->source_peer;
3980   is_predecessor = ntohl (trail_setup->is_predecessor);
3981   intermediate_trail_id = trail_setup->intermediate_trail_id;
3982
3983   /* Did the friend insert its ID in the trail list? */
3984   if (trail_length > 0 &&
3985       0 != memcmp (&trail_peer_list[trail_length-1], peer, sizeof (*peer)))
3986   {
3987     GNUNET_break_op (0);
3988     return GNUNET_SYSERR;
3989   }
3990   
3991    /* If I was the source and got the message back, then set trail length to 0.*/
3992   if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
3993   {
3994     /* IF (!) the peers know the destinations of the trails in their routing
3995      * table, then:
3996      *
3997      * This shoud only happen after 1 hop, since the first message is sent
3998      * to random friend, and we can happen to be on the best trail to the dest.
3999      * If the first friend selects someone else, the request should never come
4000      * back to us.
4001      *
4002      * (TODO)
4003      */
4004     // GNUNET_break_op (1 == trail_length);
4005     trail_length = 0;
4006   }
4007
4008   /* Check if you are present in the trail seen so far? */
4009   for (i = 0; i < trail_length ; i++)
4010   {
4011     if(0 == GNUNET_CRYPTO_cmp_peer_identity(&trail_peer_list[i],&my_identity))
4012     {
4013       trail_length = i;
4014       break;
4015     }
4016   }
4017
4018   
4019   /* Is my routing table full?  */
4020   if (GNUNET_YES == GDS_ROUTING_threshold_reached())
4021   {
4022     GNUNET_assert (NULL !=
4023                   (target_friend =
4024                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
4025     GDS_NEIGHBOURS_send_trail_rejection (source, final_dest_finger_val,
4026                                          my_identity, is_predecessor,
4027                                          trail_peer_list, trail_length,
4028                                          trail_id, target_friend,
4029                                          CONGESTION_TIMEOUT);
4030     return GNUNET_OK;
4031   }
4032
4033   /* Get the next hop to forward the trail setup request. */
4034   struct Closest_Peer next_peer =
4035           get_local_best_known_next_hop (final_dest_finger_val,
4036                                          intermediate_trail_id,
4037                                          is_predecessor,
4038                                          *peer,
4039                                          source,
4040                                          &current_dest);
4041
4042   /* Am I the final destination? */
4043   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&next_peer.best_known_destination,
4044                                              &my_identity)))
4045   {
4046     /* If I was not the source of this message for which now I am destination */
4047     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
4048     {
4049       GDS_ROUTING_add (trail_id, *peer, my_identity);
4050     }
4051
4052     if(0 == GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
4053     {
4054       finger_table_add (my_identity, NULL, 0, is_predecessor,
4055                         final_dest_finger_val, trail_id);
4056       return GNUNET_OK;
4057     }
4058
4059     if (trail_length > 0)
4060       target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail_peer_list[trail_length-1]);
4061     else
4062       target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &source);
4063     if (NULL == target_friend)
4064     {
4065       GNUNET_break_op (0);
4066       return GNUNET_SYSERR;
4067     }
4068
4069     GDS_NEIGHBOURS_send_trail_setup_result (source,
4070                                             my_identity,
4071                                             target_friend, trail_length,
4072                                             trail_peer_list,
4073                                             is_predecessor,
4074                                             final_dest_finger_val,trail_id);
4075   }
4076   else /* I'm not the final destination. */
4077   {
4078     GNUNET_assert (NULL !=
4079                     (target_friend =
4080                       GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4081                                                           &next_peer.next_hop)));
4082
4083     if (0 != GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
4084     {
4085       /* Add yourself to list of peers. */
4086       struct GNUNET_PeerIdentity peer_list[trail_length + 1];
4087
4088       memcpy (peer_list, trail_peer_list,
4089               trail_length * sizeof (struct GNUNET_PeerIdentity));
4090       peer_list[trail_length] = my_identity;
4091
4092       GDS_NEIGHBOURS_send_trail_setup (source,
4093                                        final_dest_finger_val,
4094                                        next_peer.best_known_destination,
4095                                        target_friend, trail_length + 1, peer_list,
4096                                        is_predecessor, trail_id,
4097                                        next_peer.trail_id);
4098     }
4099     else
4100         GDS_NEIGHBOURS_send_trail_setup (source,
4101                                          final_dest_finger_val,
4102                                          next_peer.best_known_destination,
4103                                          target_friend, 0, NULL,
4104                                          is_predecessor, trail_id,
4105                                          next_peer.trail_id);
4106   }
4107   return GNUNET_OK;
4108 }
4109
4110 #if 0
4111 /* FIXME: here we are calculating my_index and comparing also in this function.
4112    And we are doing it again here in this function. Re factor the code. */
4113 /**
4114  * FIXME: Should we call this function everywhere in all the handle functions
4115  * where we have a trail to verify from or a trail id. something like
4116  * if prev hop is not same then drop the message.
4117  * Check if sender_peer and peer from which we should receive the message are
4118  * same or different.
4119  * @param trail_peer_list List of peers in trail
4120  * @param trail_length Total number of peers in @a trail_peer_list
4121  * @param sender_peer Peer from which we got the message.
4122  * @param finger_identity Finger to which trail is setup. It is not part of trail.
4123  * @return #GNUNET_YES if sender_peer and peer from which we should receive the
4124  *                    message are different.
4125  *         #GNUNET_NO if sender_peer and peer from which we should receive the
4126  *                    message are different.
4127  */
4128 static int
4129 is_sender_peer_correct (const struct GNUNET_PeerIdentity *trail_peer_list,
4130                         unsigned int trail_length,
4131                         const struct GNUNET_PeerIdentity *sender_peer,
4132                         struct GNUNET_PeerIdentity finger_identity,
4133                         struct GNUNET_PeerIdentity source_peer)
4134 {
4135   int my_index;
4136
4137   /* I am the source peer. */
4138   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
4139                                              &my_identity)))
4140   {
4141     /* Is the first element of the trail is sender_peer.*/
4142     if (trail_length > 0)
4143     {
4144       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[0],
4145                                                 sender_peer))
4146         return GNUNET_NO;
4147     }
4148     else
4149     {
4150       /* Is finger the sender peer? */
4151       if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
4152                                                 &finger_identity))
4153         return GNUNET_NO;
4154     }
4155   }
4156   else
4157   {
4158     /* Get my current location in the trail. */
4159     my_index = search_my_index (trail_peer_list, trail_length);
4160     if (-1 == my_index)
4161       return GNUNET_NO;
4162
4163     /* I am the last element in the trail. */
4164     if ((trail_length - 1) == my_index)
4165     {
4166       /* Is finger the sender_peer? */
4167       if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
4168                                                 &finger_identity))
4169         return GNUNET_NO;
4170     }
4171     else
4172     {
4173       /* Is peer after me in trail the sender peer? */
4174       if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
4175                                                 &trail_peer_list[my_index + 1]))
4176         return GNUNET_NO;
4177     }
4178   }
4179   return GNUNET_YES;
4180 }
4181 #endif
4182
4183
4184 /**
4185  * FIXME: we should also add a case where we search if we are present in the trail
4186  * twice.
4187  * Core handle for p2p trail setup result messages.
4188  * @param closure
4189  * @param message message
4190  * @param peer sender of this message.
4191  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4192  */
4193 static int
4194 handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
4195                                   const struct GNUNET_MessageHeader *message)
4196 {
4197   const struct PeerTrailSetupResultMessage *trail_result;
4198   const struct GNUNET_PeerIdentity *trail_peer_list;
4199   struct GNUNET_PeerIdentity next_hop;
4200   struct FriendInfo *target_friend;
4201   struct GNUNET_PeerIdentity querying_peer;
4202   struct GNUNET_PeerIdentity finger_identity;
4203   uint32_t trail_length;
4204   uint64_t ulitmate_destination_finger_value;
4205   uint32_t is_predecessor;
4206   struct GNUNET_HashCode trail_id;
4207   int my_index;
4208   size_t msize;
4209
4210   msize = ntohs (message->size);
4211   if (msize < sizeof (struct PeerTrailSetupResultMessage))
4212   {
4213     GNUNET_break_op (0);
4214     return GNUNET_YES;
4215   }
4216
4217   trail_result = (const struct PeerTrailSetupResultMessage *) message;
4218   trail_length = (msize - sizeof (struct PeerTrailSetupResultMessage))/
4219                   sizeof (struct GNUNET_PeerIdentity);
4220   if ((msize - sizeof (struct PeerTrailSetupResultMessage)) %
4221       sizeof (struct GNUNET_PeerIdentity) != 0)
4222   {
4223     GNUNET_break_op (0);
4224     return GNUNET_OK;
4225   }
4226
4227   GNUNET_STATISTICS_update (GDS_stats,
4228                             gettext_noop
4229                             ("# Bytes received from other peers"), msize,
4230                             GNUNET_NO);
4231   
4232   is_predecessor = ntohl (trail_result->is_predecessor);
4233   querying_peer = trail_result->querying_peer;
4234   finger_identity = trail_result->finger_identity;
4235   trail_id = trail_result->trail_id;
4236   trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
4237   ulitmate_destination_finger_value =
4238           GNUNET_ntohll (trail_result->ulitmate_destination_finger_value);
4239
4240   /* FIXME: here we are calculating my_index and comparing also in this function.
4241    And we are doing it again here in this function. Re factor the code. */
4242   /* Ensure that sender peer is the peer from which we were expecting the message. */
4243 #if 0
4244   if (GNUNET_NO == is_sender_peer_correct (trail_peer_list,
4245                                            trail_length,
4246                                            peer, finger_identity, querying_peer))
4247   {
4248     GNUNET_break_op (0);
4249     return GNUNET_SYSERR;
4250   }
4251 #endif
4252
4253   /*TODO:URGENT Check if I am already present in the trail. If yes then its an error,
4254    as in trail setup we ensure that it should never happen. */
4255   /* Am I the one who initiated the query? */
4256   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
4257   {
4258     /* If I am my own finger identity, error. */
4259     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
4260     {
4261       GNUNET_break_op (0);
4262       return GNUNET_SYSERR;
4263     }
4264     GDS_ROUTING_add (trail_id, my_identity, *peer);
4265     finger_table_add (finger_identity, trail_peer_list, trail_length,
4266                       is_predecessor, ulitmate_destination_finger_value, trail_id);
4267     return GNUNET_YES;
4268   }
4269
4270   /* Get my location in the trail. */
4271   my_index = search_my_index (trail_peer_list, trail_length);
4272   if (-1 == my_index)
4273   {
4274     GNUNET_break_op(0);
4275     return GNUNET_SYSERR;
4276   }
4277
4278   if (my_index == 0)
4279     next_hop = trail_result->querying_peer;
4280   else
4281     next_hop = trail_peer_list[my_index - 1];
4282
4283   target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
4284   if (NULL == target_friend)
4285   {
4286     GNUNET_break_op (0);
4287     return GNUNET_SYSERR;
4288   }
4289
4290   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->querying_peer),
4291                                              &(trail_result->finger_identity))))
4292   {
4293     GNUNET_break_op (0);
4294     return GNUNET_SYSERR;
4295   }
4296
4297   GDS_ROUTING_add (trail_id, next_hop, *peer);
4298
4299   GDS_NEIGHBOURS_send_trail_setup_result (querying_peer, finger_identity,
4300                                           target_friend, trail_length, trail_peer_list,
4301                                           is_predecessor,
4302                                           ulitmate_destination_finger_value,
4303                                           trail_id);
4304   return GNUNET_OK;
4305 }
4306
4307
4308 /**
4309  * Invert the trail.
4310  * @param trail Trail to be inverted
4311  * @param trail_length Total number of peers in the trail.
4312  * @return Updated trail
4313  */
4314 static struct GNUNET_PeerIdentity *
4315 invert_trail (const struct GNUNET_PeerIdentity *trail,
4316               unsigned int trail_length)
4317 {
4318   int i;
4319   int j;
4320   struct GNUNET_PeerIdentity *inverted_trail;
4321
4322   inverted_trail = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
4323                                   trail_length);
4324   i = 0;
4325   j = trail_length - 1;
4326   while (i < trail_length)
4327   {
4328     inverted_trail[i] = trail[j];
4329     i++;
4330     j--;
4331   }
4332
4333   GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4334                                                           &inverted_trail[0]));
4335   return inverted_trail;
4336 }
4337
4338
4339 /**
4340  * Return the shortest trail among all the trails to reach to finger from me.
4341  * @param finger Finger
4342  * @param shortest_trail_length[out] Trail length of shortest trail from me
4343  *                                   to @a finger
4344  * @return Shortest trail.
4345  */
4346 static struct GNUNET_PeerIdentity *
4347 get_shortest_trail (struct FingerInfo *finger,
4348                     unsigned int *trail_length)
4349 {
4350   struct Trail *trail;
4351   unsigned int flag = 0;
4352   unsigned int shortest_trail_index = 0;
4353   int shortest_trail_length = -1;
4354   struct Trail_Element *trail_element;
4355   struct GNUNET_PeerIdentity *trail_list;
4356   unsigned int i;
4357
4358   trail = GNUNET_new (struct Trail);
4359
4360   /* Get the shortest trail to reach to current successor. */
4361   for (i = 0; i < finger->trails_count; i++)
4362   {
4363     trail = &finger->trail_list[i];
4364
4365     if (0 == flag)
4366     {
4367       shortest_trail_index = i;
4368       shortest_trail_length = trail->trail_length;
4369       flag = 1;
4370       continue;
4371     }
4372
4373     if (shortest_trail_length > trail->trail_length)
4374     {
4375       shortest_trail_index = i;
4376       shortest_trail_length = trail->trail_length;
4377     }
4378     continue;
4379   }
4380
4381   /* Copy the shortest trail and return. */
4382   trail = &finger->trail_list[shortest_trail_index];
4383   trail_element = trail->trail_head;
4384
4385   trail_list = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
4386                               shortest_trail_length);
4387
4388   for(i = 0; i < shortest_trail_length; i++,trail_element = trail_element->next)
4389   {
4390     trail_list[i] = trail_element->peer;
4391   }
4392
4393   GNUNET_assert(shortest_trail_length != -1);
4394
4395   *trail_length = shortest_trail_length;
4396   return trail_list;
4397 }
4398
4399
4400 /**
4401  * Check if trail_1 and trail_2 have any common element. If yes then join 
4402  * them at common element. trail_1 always preceeds trail_2 in joined trail. 
4403  * @param trail_1 Trail from source to me, NOT including endpoints.
4404  * @param trail_1_len Total number of peers @a trail_1
4405  * @param trail_2 Trail from me to current predecessor, NOT including endpoints.
4406  * @param trail_2_len Total number of peers @a trail_2
4407  * @param joined_trail_len Total number of peers in combined trail of trail_1
4408  *                          trail_2.
4409  * @return Joined trail.
4410  */
4411 static struct GNUNET_PeerIdentity *
4412 check_for_duplicate_entries (const struct GNUNET_PeerIdentity *trail_1,
4413                              unsigned int trail_1_len,
4414                              struct GNUNET_PeerIdentity *trail_2,
4415                              unsigned int trail_2_len,
4416                              unsigned int *joined_trail_len)
4417 {
4418   struct GNUNET_PeerIdentity *joined_trail;
4419   unsigned int i;
4420   unsigned int j;
4421   unsigned int k;
4422   
4423   for (i = 0; i < trail_1_len; i++)
4424   {
4425     for (j = 0; j < trail_2_len; j++)
4426     {
4427       if(0 != GNUNET_CRYPTO_cmp_peer_identity (&trail_1[i],&trail_2[j]))
4428         continue;
4429       
4430       *joined_trail_len = i + (trail_2_len - j);
4431       joined_trail = GNUNET_malloc (*joined_trail_len * 
4432                                     sizeof(struct GNUNET_PeerIdentity));
4433       
4434       
4435       /* Copy all the elements from 0 to i into joined_trail. */
4436       for(k = 0; k < (i+1); k++)
4437       {
4438         joined_trail[k] = trail_1[k];
4439       }
4440       
4441       /* Increment j as entry stored is same as entry stored at i*/
4442       j = j+1;
4443       
4444       /* Copy all the elements from j to trail_2_len-1 to joined trail.*/
4445       while((k < *joined_trail_len) && (j < trail_2_len));
4446       {
4447         joined_trail[k] = trail_2[j];
4448         j++;
4449         k++;
4450       }
4451       
4452       return joined_trail;
4453     }
4454   }
4455  
4456   /* Here you should join the  trails. */
4457   *joined_trail_len = trail_1_len + trail_2_len + 1;
4458   joined_trail = GNUNET_malloc (*joined_trail_len * 
4459                                 sizeof(struct GNUNET_PeerIdentity));
4460   
4461   
4462   for(i = 0; i < trail_1_len;i++)
4463   {
4464     joined_trail[i] = trail_1[i];
4465   }
4466   
4467   joined_trail[i] = my_identity;
4468   i++;
4469   
4470   for (j = 0; i < *joined_trail_len; i++,j++)
4471   {
4472     joined_trail[i] = trail_2[j];
4473   }
4474   
4475   return joined_trail;
4476 }
4477
4478
4479 /**
4480  * Return the trail from source to my current predecessor. Check if source
4481  * is already part of the this trail, if yes then return the shorten trail.
4482  * @param current_trail Trail from source to me, NOT including the endpoints.
4483  * @param current_trail_length Number of peers in @a current_trail.
4484  * @param trail_src_to_curr_pred_length[out] Number of peers in trail from
4485  *                                           source to my predecessor, NOT including
4486  *                                           the endpoints.
4487  * @return Trail from source to my predecessor.
4488  */
4489 static struct GNUNET_PeerIdentity *
4490 get_trail_src_to_curr_pred (struct GNUNET_PeerIdentity source_peer,
4491                             const struct GNUNET_PeerIdentity *trail_src_to_me,
4492                             unsigned int trail_src_to_me_len,
4493                             unsigned int *trail_src_to_curr_pred_length)
4494 {
4495   struct GNUNET_PeerIdentity *trail_me_to_curr_pred;
4496   struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
4497   unsigned int trail_me_to_curr_pred_length;
4498   struct FingerInfo *current_predecessor;
4499   int i;
4500   unsigned int j;
4501
4502   current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
4503   
4504   /* Check if trail_src_to_me contains current_predecessor. */
4505   for (i = 0; i < trail_src_to_me_len; i++)
4506   {
4507     if(0 != GNUNET_CRYPTO_cmp_peer_identity(&trail_src_to_me[i],
4508                                             &current_predecessor->finger_identity))
4509         continue;
4510       
4511       
4512     *trail_src_to_curr_pred_length = i;
4513     
4514     if(0 == i)
4515       return NULL;
4516     
4517      trail_src_to_curr_pred = GNUNET_malloc (*trail_src_to_curr_pred_length *
4518                                               sizeof(struct GNUNET_PeerIdentity));
4519      for(j = 0; j < i;j++)
4520        trail_src_to_curr_pred[j] = trail_src_to_me[j];
4521      return trail_src_to_curr_pred;
4522   }
4523
4524  
4525   trail_me_to_curr_pred = get_shortest_trail (current_predecessor,
4526                                               &trail_me_to_curr_pred_length);
4527   
4528   /* Check if trail contains the source_peer. */
4529   for(i = trail_me_to_curr_pred_length - 1; i >= 0; i--)
4530   {
4531     if(0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
4532                                              &trail_me_to_curr_pred[i]))
4533       continue; 
4534     
4535      /* Source is NOT part of trail. */
4536      i = i+1;
4537
4538      /* Source is the last element in the trail to reach to my pred.
4539          Source is direct friend of the pred. */
4540      if (trail_me_to_curr_pred_length == i)
4541      {
4542         *trail_src_to_curr_pred_length = 0;
4543         return NULL;
4544      }
4545      
4546      *trail_src_to_curr_pred_length = trail_me_to_curr_pred_length - i;
4547      trail_src_to_curr_pred = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)*
4548                                               *trail_src_to_curr_pred_length);
4549      
4550      for(j = 0; j < *trail_src_to_curr_pred_length; i++,j++)
4551      {
4552        trail_src_to_curr_pred[j] = trail_me_to_curr_pred[i];
4553      }
4554      GNUNET_free_non_null(trail_me_to_curr_pred);
4555      return trail_src_to_curr_pred;
4556   }
4557   
4558   unsigned int len;
4559   trail_src_to_curr_pred = check_for_duplicate_entries (trail_src_to_me, 
4560                                                         trail_src_to_me_len,
4561                                                         trail_me_to_curr_pred,
4562                                                         trail_me_to_curr_pred_length,
4563                                                         &len); 
4564   *trail_src_to_curr_pred_length = len;
4565   GNUNET_free_non_null(trail_me_to_curr_pred);
4566   return trail_src_to_curr_pred;
4567 }
4568
4569
4570 /**
4571  * Add finger as your predecessor. To add, first generate a new trail id, invert
4572  * the trail to get the trail from me to finger, add an entry in your routing
4573  * table, send add trail message to peers which are part of trail from me to
4574  * finger and add finger in finger table.
4575  * @param finger
4576  * @param trail
4577  * @param trail_length
4578  */
4579 static void
4580 update_predecessor (struct GNUNET_PeerIdentity finger,
4581                     struct GNUNET_PeerIdentity *trail,
4582                     unsigned int trail_length)
4583 {
4584   struct GNUNET_HashCode trail_to_new_predecessor_id;
4585   struct GNUNET_PeerIdentity *trail_to_new_predecessor;
4586   struct FriendInfo *target_friend;
4587
4588   /* Generate trail id for trail from me to new predecessor = finger. */
4589   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
4590                               &trail_to_new_predecessor_id,
4591                               sizeof (trail_to_new_predecessor_id));
4592
4593   /* Finger is a friend. */
4594   if (trail_length == 0)
4595   {
4596     trail_to_new_predecessor = NULL;
4597     GDS_ROUTING_add (trail_to_new_predecessor_id, my_identity, finger);
4598     GNUNET_assert (NULL != (target_friend =
4599                             GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4600                                                                &finger)));
4601   }
4602   else
4603   {
4604     /* Invert the trail to get the trail from me to finger, NOT including the
4605        endpoints.*/
4606     GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4607                                                             &trail[trail_length-1]));
4608   
4609     trail_to_new_predecessor = invert_trail (trail, trail_length);
4610     
4611     /* Add an entry in your routing table. */
4612     GDS_ROUTING_add (trail_to_new_predecessor_id,
4613                      my_identity,
4614                      trail_to_new_predecessor[0]);
4615
4616     GNUNET_assert (NULL != (target_friend =
4617                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4618                                                       &trail_to_new_predecessor[0])));
4619     GNUNET_assert (NULL != (
4620                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4621                                                       &trail[trail_length - 1])));
4622   }
4623
4624   /* Add entry in routing table of all peers that are part of trail from me
4625      to finger, including finger. */
4626   GDS_NEIGHBOURS_send_add_trail (my_identity,
4627                                  finger,
4628                                  trail_to_new_predecessor_id,
4629                                  trail_to_new_predecessor,
4630                                  trail_length,
4631                                  target_friend);
4632
4633   add_new_finger (finger, trail_to_new_predecessor, trail_length,
4634                   trail_to_new_predecessor_id, PREDECESSOR_FINGER_ID);
4635   GNUNET_free_non_null(trail_to_new_predecessor);
4636 }
4637
4638
4639 /*
4640  * Check if you already have a predecessor. If not then add finger as your
4641  * predecessor. If you have predecessor, then compare two peer identites.
4642  * If finger is correct predecessor, then remove the old entry, add finger in
4643  * finger table and send add_trail message to add the trail in the routing
4644  * table of all peers which are part of trail to reach from me to finger.
4645  * @param finger New peer which may be our predecessor.
4646  * @param trail List of peers to reach from @finger to me.
4647  * @param trail_length Total number of peer in @a trail.
4648  */
4649 static void
4650 compare_and_update_predecessor (struct GNUNET_PeerIdentity finger,
4651                                 struct GNUNET_PeerIdentity *trail,
4652                                 unsigned int trail_length)
4653 {
4654   struct FingerInfo *current_predecessor;
4655   const struct GNUNET_PeerIdentity *closest_peer;
4656   uint64_t predecessor_value;
4657   unsigned int is_predecessor = 1;
4658
4659   current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
4660
4661   GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger, &my_identity));
4662
4663   /* No predecessor. Add finger as your predecessor. */
4664   if (GNUNET_NO == current_predecessor->is_present)
4665   {
4666     update_predecessor (finger, trail, trail_length);
4667     return;
4668   }
4669   
4670   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&current_predecessor->finger_identity,
4671                                             &finger))
4672   {
4673     return;
4674   }
4675
4676   predecessor_value = compute_finger_identity_value (PREDECESSOR_FINGER_ID);
4677   closest_peer = select_closest_peer (&finger,
4678                                       &current_predecessor->finger_identity,
4679                                       predecessor_value, is_predecessor);
4680
4681   /* Finger is the closest predecessor. Remove the existing one and add the new
4682      one. */
4683   if (closest_peer == &finger)
4684   {
4685     remove_existing_finger (current_predecessor, PREDECESSOR_FINGER_ID);
4686     update_predecessor (finger, trail, trail_length);
4687     return;
4688   }
4689   return;
4690 }
4691
4692
4693 /*
4694  * Core handle for p2p verify successor messages.
4695  * @param cls closure
4696  * @param message message
4697  * @param peer peer identity this notification is about
4698  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4699  */
4700 static int
4701 handle_dht_p2p_verify_successor(void *cls,
4702                                 const struct GNUNET_PeerIdentity *peer,
4703                                 const struct GNUNET_MessageHeader *message)
4704 {
4705   const struct PeerVerifySuccessorMessage *vsm;
4706   struct GNUNET_HashCode trail_id;
4707   struct GNUNET_PeerIdentity successor;
4708   struct GNUNET_PeerIdentity source_peer;
4709   struct GNUNET_PeerIdentity *trail;
4710   struct GNUNET_PeerIdentity *next_hop;
4711   struct FingerInfo current_predecessor;
4712   struct FriendInfo *target_friend;
4713   unsigned int trail_src_to_curr_pred_len = 0;
4714   struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
4715   unsigned int trail_length;
4716   size_t msize;
4717   
4718   msize = ntohs (message->size);
4719
4720   if (msize < sizeof (struct PeerVerifySuccessorMessage))
4721   {
4722     GNUNET_break_op (0);
4723     return GNUNET_YES;
4724   }
4725
4726   vsm = (const struct PeerVerifySuccessorMessage *) message;
4727   trail_length = (msize - sizeof (struct PeerVerifySuccessorMessage))/
4728                   sizeof (struct GNUNET_PeerIdentity);
4729   if ((msize - sizeof (struct PeerVerifySuccessorMessage)) %
4730       sizeof (struct GNUNET_PeerIdentity) != 0)
4731   {
4732     GNUNET_break_op (0);
4733     return GNUNET_OK;
4734   }
4735   
4736   GNUNET_STATISTICS_update (GDS_stats,
4737                             gettext_noop
4738                             ("# Bytes received from other peers"), msize,
4739                             GNUNET_NO);
4740   
4741   trail_id = vsm->trail_id;
4742   source_peer = vsm->source_peer;
4743   successor = vsm->successor;
4744   trail = (struct GNUNET_PeerIdentity *)&vsm[1];
4745  
4746
4747   /* I am NOT the successor of source_peer. Pass the message to next_hop on
4748    * the trail. */
4749   if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
4750   {
4751     next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);    
4752     if (NULL == next_hop)
4753     {
4754 //      GNUNET_break_op (0);
4755 //      return GNUNET_SYSERR;
4756       //FIXME: Here it may happen that trail has not yet been added 
4757       //in notify successor.
4758       return GNUNET_OK;
4759     }
4760
4761     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
4762     
4763     if(NULL == target_friend)
4764     {
4765       GNUNET_break_op(0);
4766       return GNUNET_OK;
4767     }
4768     GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
4769                                                   trail_id, trail, trail_length,
4770                                                   target_friend);
4771     return GNUNET_OK;
4772   }
4773
4774   /* I am the destination of this message. */
4775
4776   /* Check if the source_peer could be our predecessor and if yes then update
4777    * it.  */
4778   compare_and_update_predecessor (source_peer, trail, trail_length);
4779   current_predecessor = finger_table[PREDECESSOR_FINGER_ID];
4780   
4781   /* Is source of this message NOT my predecessor. */
4782   if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&current_predecessor.finger_identity,
4783                                              &source_peer)))
4784   {
4785     trail_src_to_curr_pred = 
4786               get_trail_src_to_curr_pred (source_peer,
4787                                           trail,
4788                                           trail_length,
4789                                           &trail_src_to_curr_pred_len);
4790   }
4791   else
4792   {
4793     trail_src_to_curr_pred_len = trail_length;
4794     unsigned int i;
4795
4796     trail_src_to_curr_pred = 
4797             GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)
4798                            *trail_src_to_curr_pred_len);
4799     for(i = 0; i < trail_src_to_curr_pred_len; i++)
4800     {
4801       trail_src_to_curr_pred[i] = trail[i];
4802     }
4803   }
4804  
4805   GNUNET_assert (NULL !=
4806                 (target_friend =
4807                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
4808   GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
4809                                                current_predecessor.finger_identity,
4810                                                trail_id, trail_src_to_curr_pred,
4811                                                trail_src_to_curr_pred_len,
4812                                                GDS_ROUTING_DEST_TO_SRC,
4813                                                target_friend);
4814   GNUNET_free_non_null(trail_src_to_curr_pred);
4815   return GNUNET_OK;
4816 }
4817
4818
4819 /**
4820  * If the trail from me to my probable successor contains a friend not
4821  * at index 0, then we can shorten the trail.
4822  * @param probable_successor Peer which is our probable successor
4823  * @param trail_me_to_probable_successor Peers in path from me to my probable
4824  *                                       successor, NOT including the endpoints.
4825  * @param trail_me_to_probable_successor_len Total number of peers in
4826  *                                           @a trail_me_to_probable_succesor.
4827  * @return Updated trail, if any friend found.
4828  *         Else the trail_me_to_probable_successor.
4829  */
4830 struct GNUNET_PeerIdentity *
4831 check_trail_me_to_probable_succ (struct GNUNET_PeerIdentity probable_successor,
4832                                  const struct GNUNET_PeerIdentity *trail_me_to_probable_successor,
4833                                  unsigned int trail_me_to_probable_successor_len,
4834                                  unsigned int *trail_to_new_successor_length)
4835 {
4836   unsigned int i;
4837   unsigned int j;
4838   struct GNUNET_PeerIdentity *trail_to_new_successor;
4839
4840   /* Probable successor is  a friend */
4841   if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4842                                                  &probable_successor))
4843   {
4844     trail_to_new_successor = NULL;
4845     *trail_to_new_successor_length = 0;
4846     return trail_to_new_successor;
4847   }
4848   
4849   /* Is there any friend of yours in this trail. */
4850   if(trail_me_to_probable_successor_len > 1)
4851   {
4852     for (i = trail_me_to_probable_successor_len - 1; i > 0; i--)
4853     {
4854       if (NULL == GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4855                                                      &trail_me_to_probable_successor[i]))
4856         continue;
4857
4858       *trail_to_new_successor_length = (trail_me_to_probable_successor_len - i);
4859       trail_to_new_successor = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
4860                                                 *trail_to_new_successor_length);
4861
4862      
4863       for(j = 0;j < *trail_to_new_successor_length;i++,j++)
4864       {
4865         trail_to_new_successor[j] = trail_me_to_probable_successor[i];
4866       }
4867       
4868       return trail_to_new_successor;
4869     }
4870   }
4871
4872   *trail_to_new_successor_length = trail_me_to_probable_successor_len;
4873   return (struct GNUNET_PeerIdentity*)trail_me_to_probable_successor;
4874 }
4875
4876
4877 /**
4878  * Check if the peer which sent us verify successor result message is still ours
4879  * successor or not. If not, then compare existing successor and probable successor.
4880  * In case probable successor is the correct successor, remove the existing
4881  * successor. Add probable successor as new successor. Send notify new successor
4882  * message to new successor.
4883  * @param curr_succ
4884  * @param probable_successor
4885  * @param trail
4886  * @param trail_length
4887  */
4888 static void
4889 compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
4890                               struct GNUNET_PeerIdentity probable_successor,
4891                               const struct GNUNET_PeerIdentity *trail,
4892                               unsigned int trail_length)
4893 {
4894   struct FingerInfo *current_successor;
4895   const struct GNUNET_PeerIdentity *closest_peer;
4896   struct GNUNET_HashCode trail_id;
4897   struct GNUNET_PeerIdentity *trail_me_to_probable_succ;
4898   struct FriendInfo *target_friend;
4899   unsigned int trail_me_to_probable_succ_len;
4900   unsigned int is_predecessor = GNUNET_NO;
4901   uint64_t successor_value;
4902
4903   current_successor = &finger_table[0];
4904   successor_value = compute_finger_identity_value(0);
4905
4906   closest_peer = select_closest_peer (&probable_successor,
4907                                       &current_successor->finger_identity,
4908                                       successor_value, is_predecessor);
4909
4910   /* If the current_successor in the finger table is closest, then do nothing. */
4911   if (closest_peer == &current_successor->finger_identity)
4912   {
4913     /* Code for testing ONLY: Store the successor for path tracking */
4914     track_topology = 1;
4915     if (track_topology &&  (NULL != GDS_stats))
4916     {
4917       char *my_id_str;
4918       uint64_t succ;
4919       char *key;
4920     
4921       my_id_str = GNUNET_strdup (GNUNET_i2s_full (&my_identity));
4922       memcpy(&succ, &current_successor->finger_identity, sizeof(uint64_t));
4923       GNUNET_asprintf (&key, "XDHT:%s:", my_id_str);
4924       GNUNET_free (my_id_str);
4925       GNUNET_STATISTICS_set (GDS_stats, key, succ, 0);
4926       GNUNET_free (key);
4927     }
4928     return;
4929   }
4930   /* Probable successor is the closest peer.*/
4931   if(trail_length > 0)
4932   {
4933     GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4934                                                             &trail[0]));
4935   }
4936   else
4937   {
4938     GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
4939                                                             &probable_successor));
4940   }
4941   
4942   trail_me_to_probable_succ_len = 0;
4943   trail_me_to_probable_succ =
4944           check_trail_me_to_probable_succ (probable_successor,
4945                                            trail, trail_length,
4946                                            &trail_me_to_probable_succ_len);
4947
4948   /* Remove the existing successor. */
4949   remove_existing_finger (current_successor, 0);
4950
4951   /* TODO URGENT: Check if any peer is present more than once, if yes then shorten
4952    the trail. before sending it across the network. */
4953    /* Generate a new trail id to reach to your new successor. */
4954   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
4955                               &trail_id, sizeof (trail_id));
4956
4957   if (trail_me_to_probable_succ_len > 0)
4958   {
4959     GDS_ROUTING_add (trail_id, my_identity, trail_me_to_probable_succ[0]);
4960     GNUNET_assert (NULL !=
4961                   (target_friend =
4962                       GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4963                                                         &trail_me_to_probable_succ[0])));
4964   }
4965   else
4966   {
4967     GDS_ROUTING_add (trail_id, my_identity, probable_successor);
4968     GNUNET_assert (NULL !=
4969                   (target_friend =
4970                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4971                                                       &probable_successor)));
4972   }
4973
4974   add_new_finger (probable_successor, trail_me_to_probable_succ,
4975                   trail_me_to_probable_succ_len, trail_id, 0);
4976  
4977   GDS_NEIGHBOURS_send_notify_new_successor (my_identity, probable_successor,
4978                                             trail_me_to_probable_succ,
4979                                             trail_me_to_probable_succ_len,
4980                                             trail_id,
4981                                             target_friend);
4982   return;
4983 }
4984
4985
4986 /*
4987  * FIXME: Check for duplicate elements everywhere when you are making
4988  * trails. 
4989  * Core handle for p2p verify successor result messages.
4990  * @param cls closure
4991  * @param message message
4992  * @param peer peer identity this notification is about
4993  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
4994  */
4995 static int
4996 handle_dht_p2p_verify_successor_result(void *cls,
4997                                        const struct GNUNET_PeerIdentity *peer,
4998                                        const struct GNUNET_MessageHeader *message)
4999 {
5000   const struct PeerVerifySuccessorResultMessage *vsrm;
5001   enum GDS_ROUTING_trail_direction trail_direction;
5002   struct GNUNET_PeerIdentity querying_peer;
5003   struct GNUNET_HashCode trail_id;
5004   struct GNUNET_PeerIdentity *next_hop;
5005   struct FriendInfo *target_friend;
5006   struct GNUNET_PeerIdentity probable_successor;
5007   struct GNUNET_PeerIdentity current_successor;
5008   const struct GNUNET_PeerIdentity *trail;
5009   unsigned int trail_length;
5010   size_t msize;
5011
5012   msize = ntohs (message->size);
5013   /* We send a trail to reach from old successor to new successor, if
5014    * old_successor != new_successor.*/
5015   if (msize < sizeof (struct PeerVerifySuccessorResultMessage))
5016   {
5017     GNUNET_break_op (0);
5018     return GNUNET_YES;
5019   }
5020
5021   vsrm = (const struct PeerVerifySuccessorResultMessage *) message;
5022   trail_length = (msize - sizeof (struct PeerVerifySuccessorResultMessage))/
5023                       sizeof (struct GNUNET_PeerIdentity);
5024
5025   if ((msize - sizeof (struct PeerVerifySuccessorResultMessage)) %
5026       sizeof (struct GNUNET_PeerIdentity) != 0)
5027   {
5028     GNUNET_break_op (0);
5029     return GNUNET_OK;
5030   }
5031
5032   GNUNET_STATISTICS_update (GDS_stats,
5033                             gettext_noop
5034                             ("# Bytes received from other peers"), msize,
5035                             GNUNET_NO);
5036   
5037   trail = (const struct GNUNET_PeerIdentity *) &vsrm[1];
5038   querying_peer = vsrm->querying_peer;
5039   trail_direction = ntohl (vsrm->trail_direction);
5040   trail_id = vsrm->trail_id;
5041   probable_successor = vsrm->probable_successor;
5042   current_successor = vsrm->current_successor;
5043  
5044
5045   /* I am the querying_peer. */
5046   if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
5047   {
5048     compare_and_update_successor (current_successor,
5049                                   probable_successor, trail, trail_length);
5050     return GNUNET_OK;
5051   }
5052   
5053   /*If you are not the querying peer then pass on the message */
5054   GNUNET_assert (NULL != (next_hop =
5055                          GDS_ROUTING_get_next_hop (trail_id, trail_direction)));
5056   GNUNET_assert (NULL !=
5057                 (target_friend =
5058                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)));
5059   GDS_NEIGHBOURS_send_verify_successor_result (querying_peer,
5060                                                vsrm->current_successor,
5061                                                probable_successor, trail_id,
5062                                                trail,
5063                                                trail_length,
5064                                                trail_direction, target_friend);
5065   return GNUNET_OK;
5066 }
5067
5068
5069 /*
5070  * Core handle for p2p notify new successor messages.
5071  * @param cls closure
5072  * @param message message
5073  * @param peer peer identity this notification is about
5074  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5075  */
5076 static int
5077 handle_dht_p2p_notify_new_successor(void *cls,
5078                                     const struct GNUNET_PeerIdentity *peer,
5079                                     const struct GNUNET_MessageHeader *message)
5080 {
5081   const struct PeerNotifyNewSuccessorMessage *nsm;
5082   struct GNUNET_PeerIdentity *trail;
5083   struct GNUNET_PeerIdentity source;
5084   struct GNUNET_PeerIdentity new_successor;
5085   struct GNUNET_HashCode trail_id;
5086   struct GNUNET_PeerIdentity next_hop;
5087   struct FriendInfo *target_friend;
5088   int my_index;
5089   size_t msize;
5090   uint32_t trail_length;
5091
5092   msize = ntohs (message->size);
5093
5094   /* We have the trail to reach from source to new successor. */
5095   if (msize < sizeof (struct PeerNotifyNewSuccessorMessage))
5096   {
5097     GNUNET_break_op (0);
5098     return GNUNET_YES;
5099   }
5100
5101   nsm = (const struct PeerNotifyNewSuccessorMessage *) message;
5102   trail_length = (msize - sizeof (struct PeerNotifyNewSuccessorMessage))/
5103                   sizeof (struct GNUNET_PeerIdentity);
5104   if ((msize - sizeof (struct PeerNotifyNewSuccessorMessage)) %
5105       sizeof (struct GNUNET_PeerIdentity) != 0)
5106   {
5107     GNUNET_break_op (0);
5108     return GNUNET_OK;
5109   }
5110
5111   GNUNET_STATISTICS_update (GDS_stats,
5112                             gettext_noop
5113                             ("# Bytes received from other peers"), msize,
5114                             GNUNET_NO);
5115   
5116   trail = (struct GNUNET_PeerIdentity *) &nsm[1];
5117   source  = nsm->source_peer;
5118   new_successor = nsm->new_successor;
5119   trail_id = nsm->trail_id;
5120   
5121
5122   //FIXME: add a check to make sure peer is correct.
5123
5124   /* I am the new_successor to source_peer. */
5125   if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_successor))
5126   {
5127     if(trail_length > 0)
5128       GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity(&trail[trail_length - 1],
5129                                                           peer));
5130     else 
5131       GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity(&source, peer));
5132   
5133     compare_and_update_predecessor (source, trail, trail_length);
5134     return GNUNET_OK;
5135   }
5136
5137   GNUNET_assert(trail_length > 0);
5138   /* I am part of trail to reach to successor. */
5139   my_index = search_my_index (trail, trail_length);
5140   if (-1 == my_index)
5141   {
5142     GNUNET_break_op (0);
5143     return GNUNET_SYSERR;
5144   }
5145
5146   if ((trail_length-1) == my_index)
5147     next_hop = new_successor;
5148   else
5149     next_hop = trail[my_index + 1];
5150
5151
5152   /* Add an entry in routing table for trail from source to its new successor. */
5153   GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
5154   GNUNET_assert (NULL !=
5155                 (target_friend =
5156                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
5157   GDS_NEIGHBOURS_send_notify_new_successor (source, new_successor, trail,
5158                                             trail_length,
5159                                             trail_id, target_friend);
5160   return GNUNET_OK;
5161
5162 }
5163
5164
5165 /**
5166  * Core handler for P2P trail rejection message
5167  * @param cls closure
5168  * @param message message
5169  * @param peer peer identity this notification is about
5170  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5171  */
5172 static int
5173 handle_dht_p2p_trail_setup_rejection (void *cls,
5174                                       const struct GNUNET_PeerIdentity *peer,
5175                                       const struct GNUNET_MessageHeader *message)
5176 {
5177   const struct PeerTrailRejectionMessage *trail_rejection;
5178   unsigned int trail_length;
5179   const struct GNUNET_PeerIdentity *trail_peer_list;
5180   struct FriendInfo *target_friend;
5181   struct GNUNET_TIME_Relative congestion_timeout;
5182   struct GNUNET_HashCode trail_id;
5183   struct GNUNET_PeerIdentity next_peer;
5184   struct GNUNET_PeerIdentity source;
5185   struct GNUNET_PeerIdentity *next_hop;
5186   uint64_t ultimate_destination_finger_value;
5187   unsigned int is_predecessor;
5188   size_t msize;
5189
5190   msize = ntohs (message->size);
5191   /* We are passing the trail setup so far. */
5192   if (msize < sizeof (struct PeerTrailRejectionMessage))
5193   {
5194     GNUNET_break_op (0);
5195     return GNUNET_YES;
5196   }
5197
5198   trail_rejection = (const struct PeerTrailRejectionMessage *) message;
5199   trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
5200                   sizeof (struct GNUNET_PeerIdentity);
5201   if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
5202       sizeof (struct GNUNET_PeerIdentity) != 0)
5203   {
5204     GNUNET_break_op (0);
5205     return GNUNET_OK;
5206   }
5207   
5208   GNUNET_STATISTICS_update (GDS_stats,
5209                             gettext_noop
5210                             ("# Bytes received from other peers"), msize,
5211                             GNUNET_NO);
5212   
5213   trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_rejection[1];
5214   is_predecessor = ntohl (trail_rejection->is_predecessor);
5215   congestion_timeout = trail_rejection->congestion_time;
5216   source = trail_rejection->source_peer;
5217   trail_id = trail_rejection->trail_id;
5218   ultimate_destination_finger_value =
5219           GNUNET_ntohll (trail_rejection->ultimate_destination_finger_value);
5220
5221   /* First set the congestion time of the friend that sent you this message. */
5222   GNUNET_assert (NULL !=
5223                  (target_friend =
5224                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
5225   target_friend->congestion_timestamp =
5226           GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
5227                                     congestion_timeout);
5228
5229   /* I am the source peer which wants to setup the trail. Do nothing.
5230    * send_find_finger_trail_task is scheduled periodically.*/
5231   if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
5232     return GNUNET_OK;
5233
5234   /* If I am congested then pass this message to peer before me in trail. */
5235   if(GNUNET_YES == GDS_ROUTING_threshold_reached())
5236   {
5237     struct GNUNET_PeerIdentity *new_trail;
5238     unsigned int new_trail_length;
5239
5240     /* Remove yourself from the trail setup so far. */
5241     if (trail_length == 1)
5242     {
5243       new_trail = NULL;
5244       new_trail_length = 0;
5245       next_hop = &source;
5246     }
5247     else
5248     {
5249       memcpy (&next_hop , &trail_peer_list[trail_length - 2],
5250               sizeof (struct GNUNET_PeerIdentity));
5251
5252       /* Remove myself from the trail. */
5253       new_trail_length = trail_length -1;
5254       new_trail = GNUNET_malloc (new_trail_length * sizeof (struct GNUNET_PeerIdentity));
5255       memcpy (new_trail, trail_peer_list, new_trail_length * sizeof (struct GNUNET_PeerIdentity));
5256     }
5257
5258     GNUNET_assert (NULL !=
5259                   (target_friend =
5260                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)));
5261     GDS_NEIGHBOURS_send_trail_rejection (source,
5262                                          ultimate_destination_finger_value,
5263                                          my_identity, is_predecessor,
5264                                          new_trail,new_trail_length,trail_id,
5265                                          target_friend, CONGESTION_TIMEOUT);
5266     GNUNET_free (new_trail);
5267     return GNUNET_OK;
5268   }
5269
5270   struct Closest_Peer successor;
5271   successor = find_successor (ultimate_destination_finger_value, is_predecessor);
5272
5273   /* Am I the final destination? */
5274   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
5275                                              &my_identity)))
5276   {
5277     if (0 == trail_length)
5278       next_peer = source;
5279     else
5280       next_peer = trail_peer_list[trail_length-1];
5281
5282     GNUNET_assert (NULL !=
5283                   (target_friend =
5284                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer)));
5285
5286     GDS_NEIGHBOURS_send_trail_setup_result (source,
5287                                             my_identity,
5288                                             target_friend, trail_length,
5289                                             trail_peer_list,
5290                                             is_predecessor,
5291                                             ultimate_destination_finger_value,
5292                                             trail_id);
5293   }
5294   else
5295   {
5296     struct GNUNET_PeerIdentity peer_list[trail_length + 1];
5297
5298     memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
5299     peer_list[trail_length] = my_identity;
5300
5301     GNUNET_assert (NULL !=
5302                   (target_friend =
5303                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)));
5304
5305     GDS_NEIGHBOURS_send_trail_setup (source,
5306                                      ultimate_destination_finger_value,
5307                                      successor.best_known_destination,
5308                                      target_friend, trail_length + 1, peer_list,
5309                                      is_predecessor, trail_id,
5310                                      successor.trail_id);
5311   }
5312   return GNUNET_OK;
5313 }
5314
5315
5316 /*
5317  * Core handle for p2p trail tear compression messages.
5318  * @param cls closure
5319  * @param message message
5320  * @param peer peer identity this notification is about
5321  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5322  */
5323 static int
5324 handle_dht_p2p_trail_compression (void *cls, const struct GNUNET_PeerIdentity *peer,
5325                                   const struct GNUNET_MessageHeader *message)
5326 {
5327   const struct PeerTrailCompressionMessage *trail_compression;
5328   struct GNUNET_PeerIdentity *next_hop;
5329   struct FriendInfo *target_friend;
5330   struct GNUNET_HashCode trail_id;
5331   size_t msize;
5332
5333   msize = ntohs (message->size);
5334
5335   if (msize != sizeof (struct PeerTrailCompressionMessage))
5336   {
5337     GNUNET_break_op (0);
5338     return GNUNET_OK;
5339   }
5340
5341   GNUNET_STATISTICS_update (GDS_stats,
5342                             gettext_noop
5343                             ("# Bytes received from other peers"), msize,
5344                             GNUNET_NO);
5345   
5346   trail_compression = (const struct PeerTrailCompressionMessage *) message;
5347   trail_id = trail_compression->trail_id;
5348
5349   /* Am I the new first friend to reach to finger of this trail. */
5350   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&trail_compression->new_first_friend,
5351                                              &my_identity)))
5352   {
5353     GNUNET_assert (NULL !=
5354                   (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5355                                                       &trail_compression->source_peer)));
5356
5357     /* Update your prev hop to source of this message. */
5358     GNUNET_assert (GNUNET_SYSERR !=
5359                   (GDS_ROUTING_update_trail_prev_hop (trail_id,
5360                                                       trail_compression->source_peer)));
5361     return GNUNET_OK;
5362   }
5363
5364   /* Pass the message to next hop to finally reach to new_first_friend. */
5365   next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
5366
5367   if (NULL == next_hop)
5368   {
5369     GNUNET_break (0);
5370     return GNUNET_OK;
5371   }
5372
5373   GNUNET_assert (NULL !=
5374                 (target_friend =
5375                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)));
5376
5377   GDS_ROUTING_remove_trail (trail_id);
5378
5379   GDS_NEIGHBOURS_send_trail_compression (trail_compression->source_peer,
5380                                          trail_id,
5381                                          trail_compression->new_first_friend,
5382                                          target_friend);
5383   return GNUNET_OK;
5384 }
5385
5386
5387 /**
5388  * Core handler for trail teardown message.
5389  * @param cls closure
5390  * @param message message
5391  * @param peer sender of this messsage.
5392  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5393  */
5394 static int
5395 handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer,
5396                                const struct GNUNET_MessageHeader *message)
5397 {
5398   const struct PeerTrailTearDownMessage *trail_teardown;
5399   enum GDS_ROUTING_trail_direction trail_direction;
5400   struct GNUNET_HashCode trail_id;
5401   struct GNUNET_PeerIdentity *next_hop;
5402   size_t msize;
5403
5404   msize = ntohs (message->size);
5405
5406   /* Here we pass only the trail id. */
5407   if (msize != sizeof (struct PeerTrailTearDownMessage))
5408   {
5409     GNUNET_break_op (0);
5410     return GNUNET_OK;
5411   }
5412
5413   GNUNET_STATISTICS_update (GDS_stats,
5414                             gettext_noop
5415                             ("# Bytes received from other peers"), msize,
5416                             GNUNET_NO);
5417   
5418   trail_teardown = (const struct PeerTrailTearDownMessage *) message;
5419   trail_direction = ntohl (trail_teardown->trail_direction);
5420   trail_id = trail_teardown->trail_id;
5421
5422   /* Check if peer is the real peer from which we should get this message.*/
5423   /* Get the prev_hop for this trail by getting the next hop in opposite direction. */
5424 #if 0
5425   GNUNET_assert (NULL != (prev_hop =
5426                  GDS_ROUTING_get_next_hop (trail_id, !trail_direction)));
5427   if (0 != GNUNET_CRYPTO_cmp_peer_identity (prev_hop, peer))
5428   {
5429     GNUNET_break (0);
5430     return GNUNET_SYSERR;
5431   }
5432 #endif
5433
5434   next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
5435
5436   if (NULL == next_hop)
5437   {
5438     GNUNET_break (0);
5439     return GNUNET_SYSERR;
5440   }
5441
5442   /* I am the next hop, which means I am the final destination. */
5443   if (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))
5444   {
5445     GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
5446     return GNUNET_OK;
5447   }
5448   else
5449   {
5450     /* If not final destination, then send a trail teardown message to next hop.*/
5451     GNUNET_assert (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop));
5452     GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
5453     GDS_NEIGHBOURS_send_trail_teardown (trail_id, trail_direction, *next_hop);
5454   }
5455
5456   return GNUNET_OK;
5457 }
5458
5459
5460 /**
5461  * Core handle for p2p add trail message.
5462  * @param cls closure
5463  * @param message message
5464  * @param peer peer identity this notification is about
5465  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5466  */
5467 static int
5468 handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
5469                           const struct GNUNET_MessageHeader *message)
5470 {
5471   const struct PeerAddTrailMessage *add_trail;
5472   const struct GNUNET_PeerIdentity *trail;
5473   struct GNUNET_HashCode trail_id;
5474   struct GNUNET_PeerIdentity destination_peer;
5475   struct GNUNET_PeerIdentity source_peer;
5476   struct GNUNET_PeerIdentity next_hop;
5477   unsigned int trail_length;
5478   unsigned int my_index;
5479   size_t msize;
5480
5481   msize = ntohs (message->size);
5482   /* In this message we pass the whole trail from source to destination as we
5483    * are adding that trail.*/
5484   //FIXME: failed when run with 1000 pears. check why.
5485   if (msize < sizeof (struct PeerAddTrailMessage))
5486   {
5487     GNUNET_break_op (0);
5488     return GNUNET_OK;
5489   }
5490
5491   add_trail = (const struct PeerAddTrailMessage *) message;
5492   trail_length = (msize - sizeof (struct PeerAddTrailMessage))/
5493                   sizeof (struct GNUNET_PeerIdentity);
5494   if ((msize - sizeof (struct PeerAddTrailMessage)) %
5495       sizeof (struct GNUNET_PeerIdentity) != 0)
5496   {
5497     GNUNET_break_op (0);
5498     return GNUNET_OK;
5499   }
5500
5501   GNUNET_STATISTICS_update (GDS_stats,
5502                             gettext_noop
5503                             ("# Bytes received from other peers"), msize,
5504                             GNUNET_NO);
5505   
5506   trail = (const struct GNUNET_PeerIdentity *)&add_trail[1];
5507   destination_peer = add_trail->destination_peer;
5508   source_peer = add_trail->source_peer;
5509   trail_id = add_trail->trail_id;
5510
5511   //FIXME: add a check that sender peer is not malicious. Make it a generic
5512   // function so that it can be used in all other functions where we need the
5513   // same functionality.
5514
5515   /* I am not the destination of the trail. */
5516   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination_peer))
5517   {
5518     struct FriendInfo *target_friend;
5519
5520     /* Get my location in the trail. */
5521     my_index = search_my_index (trail, trail_length);
5522     if (-1 == my_index)
5523     {
5524       GNUNET_break_op (0);
5525       return GNUNET_SYSERR;
5526     }
5527
5528     if ((trail_length - 1) == my_index)
5529     {
5530       next_hop = destination_peer;
5531     }
5532     else
5533     {
5534       next_hop = trail[my_index + 1];
5535     }
5536
5537     /* Add in your routing table. */
5538     GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, next_hop, *peer));
5539     GNUNET_assert (NULL !=
5540                   (target_friend =
5541                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
5542     GDS_NEIGHBOURS_send_add_trail (source_peer, destination_peer, trail_id,
5543                                    trail, trail_length, target_friend);
5544     return GNUNET_OK;
5545   }
5546   /* I am the destination. Add an entry in routing table. */
5547   GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, my_identity));
5548   return GNUNET_OK;
5549 }
5550
5551
5552 /**
5553  * Free the finger trail in which the first friend to reach to a finger is
5554  * disconnected_friend. Also remove entry from routing table for that particular
5555  * trail id.
5556  * @param disconnected_friend PeerIdentity of friend which got disconnected
5557  * @param remove_finger Finger whose trail we need to check if it has
5558  *                      disconnected_friend as the first hop.
5559  * @return Total number of trails in which disconnected_friend was the first
5560  *         hop.
5561  */
5562 static int
5563 remove_matching_trails (const struct GNUNET_PeerIdentity *disconnected_friend,
5564                         struct FingerInfo *remove_finger)
5565 {
5566   unsigned int matching_trails_count;
5567   int i;
5568
5569   /* Number of trails with disconnected_friend as the first hop in the trail
5570    * to reach from me to remove_finger, NOT including endpoints. */
5571   matching_trails_count = 0;
5572
5573   /* Iterate over all the trails of finger. */
5574   for (i = 0; i < MAXIMUM_TRAILS_PER_FINGER; i++)
5575   {
5576     struct Trail *trail;
5577     trail = &remove_finger->trail_list[i];
5578
5579     /* This assertion is ensure that there are no gaps in the trail list.
5580      REMOVE IT AFTERWARDS. */
5581     GNUNET_assert (GNUNET_YES == trail->is_present);
5582
5583     /* First friend to reach to finger is disconnected_peer. */
5584     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&trail->trail_head->peer,
5585                                               disconnected_friend))
5586     {
5587       struct GNUNET_PeerIdentity *next_hop;
5588       struct FriendInfo *remove_friend;
5589
5590       GNUNET_assert (NULL !=
5591                     (remove_friend =
5592                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5593                                                         disconnected_friend)));
5594       /* FIXME: removing no but check it. */
5595       //remove_friend->trails_count--;
5596       next_hop = GDS_ROUTING_get_next_hop (trail->trail_id,
5597                                            GDS_ROUTING_SRC_TO_DEST);
5598
5599       /* Here it may happen that as all the peers got disconnected, the entry in
5600        routing table for that particular trail has been removed, because the
5601        previously disconnected peer was either a next hop or prev hop of that
5602        peer. */
5603       if (NULL == next_hop)
5604         continue;
5605
5606       GNUNET_assert (0 == (GNUNET_CRYPTO_cmp_peer_identity (disconnected_friend,
5607                                                             next_hop)));
5608       matching_trails_count++;
5609       GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail->trail_id));
5610
5611       free_trail (trail);
5612       trail->is_present = GNUNET_NO;
5613     }
5614   }
5615   return matching_trails_count;
5616 }
5617
5618
5619 /**
5620  * Iterate over finger_table entries.
5621  * 0. Ignore finger which is my_identity or if no valid entry present at
5622  *    that finger index.
5623  * 1. If disconnected_friend is a finger, then remove the routing entry from
5624       your own table. Free the trail.
5625  * 2. Check if disconnected_friend is the first friend in the trail to reach to a finger.
5626  *   2.1 Remove all the trails and entry from routing table in which disconnected
5627  *       friend is the first friend in the trail. If disconnected_friend is the
5628  *       first friend in all the trails to reach finger, then remove the finger.
5629  * @param disconnected_friend Peer identity of friend which got disconnected.
5630  */
5631 static void
5632 remove_matching_fingers (const struct GNUNET_PeerIdentity *disconnected_peer)
5633 {
5634   struct FingerInfo *remove_finger;
5635   struct FriendInfo *remove_friend;
5636   int removed_trails_count;
5637   int i;
5638
5639   /* Iterate over finger table entries. */
5640   for (i = 0; i < MAX_FINGERS; i++)
5641   {
5642     remove_finger = &finger_table[i];
5643
5644     /* No finger stored at this trail index. */
5645     if (GNUNET_NO == remove_finger->is_present)
5646       continue;
5647
5648     /* I am my own finger, then ignore this finger. */
5649     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity,
5650                                               &my_identity))
5651       continue;
5652
5653     /* Is disconnected_peer a finger? */
5654     if (0 == GNUNET_CRYPTO_cmp_peer_identity (disconnected_peer,
5655                                               &remove_finger->finger_identity))
5656     {
5657       struct GNUNET_PeerIdentity *next_hop;
5658       struct GNUNET_HashCode trail_id;
5659
5660
5661       GNUNET_assert (GNUNET_YES == (remove_finger->trail_list[0].is_present));
5662       trail_id = remove_finger->trail_list[0].trail_id;
5663
5664       if(NULL !=
5665               (next_hop =
5666                GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST)))
5667       {
5668         GNUNET_assert (0 ==
5669                       (GNUNET_CRYPTO_cmp_peer_identity (next_hop,
5670                                                         &remove_finger->finger_identity)));
5671         GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
5672         GNUNET_assert (NULL !=
5673                        (remove_friend =
5674                         GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5675                                                            disconnected_peer)));
5676       }
5677
5678       remove_finger->trail_list[0].is_present = GNUNET_NO;
5679       //GNUNET_assert (0 != remove_friend->trails_count);
5680       //remove_friend->trails_count--; //FIXME; CHECK WHY IT FAILS AND THEN UNCOMMENT.
5681       remove_finger->is_present = GNUNET_NO;
5682       memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
5683       continue;
5684     }
5685
5686     /* If finger is a friend but not disconnected_friend, then continue. */
5687     if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
5688                                                    &remove_finger->finger_identity))
5689       continue;
5690
5691     /* Iterate over the list of trails to reach remove_finger. Check if
5692      * disconnected_friend is the first friend in any of the trail. */
5693     removed_trails_count = remove_matching_trails (disconnected_peer,
5694                                                    remove_finger);
5695     remove_finger->trails_count =
5696             remove_finger->trails_count - removed_trails_count;
5697     /* All the finger trails had disconnected_friend as the first friend,
5698      * so free the finger. */
5699     if (remove_finger->trails_count == 0)
5700     {
5701       remove_finger->is_present = GNUNET_NO;
5702       memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
5703     }
5704   }
5705 }
5706
5707
5708 /**
5709  * Method called whenever a peer disconnects.
5710  *
5711  * @param cls closure
5712  * @param peer peer identity this notification is about
5713  */
5714 static void
5715 handle_core_disconnect (void *cls,
5716                                           const struct GNUNET_PeerIdentity *peer)
5717 {
5718   struct FriendInfo *remove_friend;
5719
5720   /* If disconnected to own identity, then return. */
5721   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
5722     return;
5723
5724   GNUNET_assert (NULL != (remove_friend =
5725                           GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
5726
5727   /* Remove fingers with peer as first friend or if peer is a finger. */
5728   remove_matching_fingers (peer);
5729
5730   /* Remove any trail from routing table of which peer is a part of. This function
5731    * internally sends a trail teardown message in the direction of which
5732    * disconnected peer is not part of. */
5733   GNUNET_assert (GNUNET_SYSERR != GDS_ROUTING_remove_trail_by_peer (peer));
5734
5735   //GNUNET_assert (0 == remove_friend->trails_count); //FIXME; why should this fai.
5736
5737   /* Remove peer from friend_peermap. */
5738   GNUNET_assert (GNUNET_YES ==
5739                  GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
5740                                                        peer,
5741                                                        remove_friend));
5742
5743   if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
5744     return;
5745
5746   if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
5747   {
5748       GNUNET_SCHEDULER_cancel (find_finger_trail_task);
5749       find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
5750   }
5751   else
5752     GNUNET_break (0);
5753
5754 }
5755
5756
5757 /**
5758  * Method called whenever a peer connects.
5759  *
5760  * @param cls closure
5761  * @param peer_identity peer identity this notification is about
5762  */
5763 static void
5764 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
5765 {
5766   struct FriendInfo *friend;
5767
5768   /* Check for connect to self message */
5769   if (0 == memcmp (&my_identity, peer_identity, sizeof (struct GNUNET_PeerIdentity)))
5770     return;
5771
5772   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to %s\n", GNUNET_i2s (peer_identity));
5773
5774   /* If peer already exists in our friend_peermap, then exit. */
5775   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
5776                                                             peer_identity))
5777   {
5778     GNUNET_break (0);
5779     return;
5780   }
5781
5782   friend = GNUNET_new (struct FriendInfo);
5783   friend->id = *peer_identity;
5784
5785   GNUNET_assert (GNUNET_OK ==
5786                  GNUNET_CONTAINER_multipeermap_put (friend_peermap,
5787                                                     peer_identity, friend,
5788                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
5789
5790
5791   /* got a first connection, good time to start with FIND FINGER TRAIL requests...*/
5792   if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
5793   {
5794     next_send_time.rel_value_us =
5795       DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
5796       GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
5797                                 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
5798     find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
5799   }
5800 }
5801
5802
5803 /**
5804  * To be called on core init/fail.
5805  *
5806  * @param cls service closure
5807  * @param identity the public identity of this peer
5808  */
5809 static void
5810 core_init (void *cls,
5811            const struct GNUNET_PeerIdentity *identity)
5812 {
5813   my_identity = *identity;
5814
5815   uint64_t my_id64;
5816   memcpy (&my_id64, &my_identity, sizeof (uint64_t));
5817   my_id64 = GNUNET_ntohll (my_id64);
5818   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5819               "my_indentity = %s, my_id64=%llu\n",GNUNET_i2s(&my_identity),(unsigned long long)my_id64);
5820
5821 }
5822
5823
5824 /**
5825  * Initialize finger table entries.
5826  */
5827 static void
5828 finger_table_init ()
5829 {
5830   memset (&finger_table, 0, sizeof (finger_table));
5831 }
5832
5833
5834 /**
5835  * Initialize neighbours subsystem.
5836  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5837  */
5838 int
5839 GDS_NEIGHBOURS_init (void)
5840 {
5841   static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5842     {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT, 0},
5843     {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_XDHT_P2P_GET, 0},
5844     {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT, 0},
5845     {&handle_dht_p2p_trail_setup, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP, 0},
5846     {&handle_dht_p2p_trail_setup_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT, 0},
5847     {&handle_dht_p2p_verify_successor, GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR, 0},
5848     {&handle_dht_p2p_verify_successor_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT, 0},
5849     {&handle_dht_p2p_notify_new_successor, GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR, 0},
5850     {&handle_dht_p2p_trail_setup_rejection, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION, 0},
5851     {&handle_dht_p2p_trail_compression, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_COMPRESSION,
5852                                         sizeof (struct PeerTrailCompressionMessage)},
5853     {&handle_dht_p2p_trail_teardown, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN,
5854                                      sizeof (struct PeerTrailTearDownMessage)},
5855     {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL, 0},
5856     {NULL, 0, 0}
5857   };
5858
5859   core_api =
5860     GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
5861                          &handle_core_disconnect, NULL, GNUNET_NO, NULL,
5862                          GNUNET_NO, core_handlers);
5863   if (NULL == core_api)
5864     return GNUNET_SYSERR;
5865
5866   friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
5867   finger_table_init ();
5868
5869   return GNUNET_OK;
5870 }
5871
5872
5873 /**
5874  * Shutdown neighbours subsystem.
5875  */
5876 void
5877 GDS_NEIGHBOURS_done (void)
5878 {
5879   if (NULL == core_api)
5880     return;
5881
5882   GNUNET_CORE_disconnect (core_api);
5883   core_api = NULL;
5884
5885   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
5886   GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
5887   friend_peermap = NULL;
5888
5889   if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
5890   {
5891     GNUNET_break (0);
5892     GNUNET_SCHEDULER_cancel (find_finger_trail_task);
5893     find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
5894   }
5895
5896   if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_task)
5897   {
5898     GNUNET_SCHEDULER_cancel (send_verify_successor_task);
5899     send_verify_successor_task = GNUNET_SCHEDULER_NO_TASK;
5900   }
5901 }
5902
5903
5904 /**
5905  * Get my identity
5906  *
5907  * @return my identity
5908  */
5909 struct GNUNET_PeerIdentity
5910 GDS_NEIGHBOURS_get_my_id (void)
5911 {
5912   return my_identity;
5913 }
5914
5915 /* end of gnunet-service-xdht_neighbours.c */