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