aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSupriti Singh <supritisingh08@gmail.com>2014-06-05 15:24:26 +0000
committerSupriti Singh <supritisingh08@gmail.com>2014-06-05 15:24:26 +0000
commit2a56323e1dba888957d093396438ab625d8cc1b4 (patch)
treec606bce94a78864888717841db172f35340c4d58 /src
parentb5cffcc2a077c2f2bba2d4c13569eaecb98b9490 (diff)
downloadgnunet-2a56323e1dba888957d093396438ab625d8cc1b4.tar.gz
gnunet-2a56323e1dba888957d093396438ab625d8cc1b4.zip
Not passing finger_map_index in trail setup message.
Not passing trail length in any message.
Diffstat (limited to 'src')
-rw-r--r--src/dht/gnunet-service-xdht_clients.c10
-rw-r--r--src/dht/gnunet-service-xdht_neighbours.c2318
-rw-r--r--src/dht/gnunet-service-xdht_neighbours.h37
3 files changed, 1625 insertions, 740 deletions
diff --git a/src/dht/gnunet-service-xdht_clients.c b/src/dht/gnunet-service-xdht_clients.c
index 056377e0e..0621f8ea8 100644
--- a/src/dht/gnunet-service-xdht_clients.c
+++ b/src/dht/gnunet-service-xdht_clients.c
@@ -844,10 +844,8 @@ transmit_request (struct ClientQueryRecord *cqr)
844 cqr->replication, 844 cqr->replication,
845 cqr->seen_replies_count); 845 cqr->seen_replies_count);
846 846
847 struct GNUNET_PeerIdentity my_identity;
848 my_identity = GDS_NEIGHBOURS_get_my_id ();
849 GDS_NEIGHBOURS_send_get (&cqr->key, cqr->type, cqr->msg_options, 847 GDS_NEIGHBOURS_send_get (&cqr->key, cqr->type, cqr->msg_options,
850 cqr->replication, my_identity, my_identity, NULL, 848 cqr->replication, NULL, NULL , NULL,
851 0, 0, NULL); 849 0, 0, NULL);
852 850
853 /* exponential back-off for retries. 851 /* exponential back-off for retries.
@@ -947,12 +945,12 @@ handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
947 struct GNUNET_PeerIdentity my_identity = GDS_NEIGHBOURS_get_my_id(); 945 struct GNUNET_PeerIdentity my_identity = GDS_NEIGHBOURS_get_my_id();
948 GDS_NEIGHBOURS_send_put (&put_msg->key, 946 GDS_NEIGHBOURS_send_put (&put_msg->key,
949 ntohl (put_msg->type), ntohl (put_msg->options), 947 ntohl (put_msg->type), ntohl (put_msg->options),
950 ntohl (put_msg->desired_replication_level), 948 ntohl (put_msg->desired_replication_level), NULL,
951 my_identity, my_identity, NULL, 0, 0, NULL, 949 NULL, NULL, 0, 0, NULL,
952 GNUNET_TIME_absolute_ntoh (put_msg->expiration), 950 GNUNET_TIME_absolute_ntoh (put_msg->expiration),
953 &put_msg[1], 951 &put_msg[1],
954 size - sizeof (struct GNUNET_DHT_ClientPutMessage)); 952 size - sizeof (struct GNUNET_DHT_ClientPutMessage));
955 953
956 954
957 GDS_CLIENTS_process_put (ntohl (put_msg->options), 955 GDS_CLIENTS_process_put (ntohl (put_msg->options),
958 ntohl (put_msg->type), 956 ntohl (put_msg->type),
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c
index c2927f100..ef19beed8 100644
--- a/src/dht/gnunet-service-xdht_neighbours.c
+++ b/src/dht/gnunet-service-xdht_neighbours.c
@@ -45,7 +45,7 @@
45#include "dht.h" 45#include "dht.h"
46 46
47/** 47/**
48 * Maximum possible fingers of a peer. 48 * Maximum possible fingers (including predecessor) of a peer
49 */ 49 */
50#define MAX_FINGERS 65 50#define MAX_FINGERS 65
51 51
@@ -65,92 +65,261 @@
65#define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2) 65#define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
66 66
67/** 67/**
68 * How long will I remain congested? 68 * Duration for which I may remain congested.
69 * Note: Its a static value. In future, a peer may do some analysis and calculate
70 * congestion_timeout based on 'some' parameters.
69 */ 71 */
70#define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2) 72#define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
71 73
72/** 74/**
75 * Maximum number of trails allowed to go through a friend.
76 */
77#define TRAILS_THROUGH_FRIEND_THRESHOLD 64
78
79/**
73 * Maximum number of trails stored per finger. 80 * Maximum number of trails stored per finger.
74 */ 81 */
75#define MAXIMUM_TRAILS_PER_FINGER 2 82#define MAXIMUM_TRAILS_PER_FINGER 2
76 83
77/** 84/**
78 * Used to distinguish put/get request use of find_successor() from others 85 * Finger map index for predecessor entry in finger peermap.
79 */ 86 */
80#define PUT_GET_REQUEST 65 87#define PREDECESSOR_FINGER_ID 64
81 88
82/** 89/**
83 * Maximum number of trails that can go through a friend. 90 * Wrap around in peer identity circle.
91 * FIXME: not used anywhere, should be used in
92 * find_successor() while comparing two peers.
84 */ 93 */
85#define TRAIL_THROUGH_FRIEND_THRESHOLD 64 94#define PEER_IDENTITES_WRAP_AROUND pow(2, 64) - 1
86 95
87/** 96/**
88 * Finger map index for predecessor entry in finger peermap. 97 * To check if a finger is predecessor or not.
89 */ 98 */
90#define PREDECESSOR_FINGER_ID 64 99enum GDS_NEIGHBOURS_finger_type
100{
101 GDS_FINGER_TYPE_PREDECESSOR = 0,
102 GDS_FINGER_TYPE_NON_PREDECESSOR = 1
103};
104
105GNUNET_NETWORK_STRUCT_BEGIN
91 106
92/** 107/**
93 * Maximum number of trails allowed to go through a friend. 108 * P2P PUT message
94 */ 109 */
95#define TRAILS_THROUGH_FRIEND_THRESHOLD 64 110struct PeerPutMessage
111{
112 /**
113 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
114 */
115 struct GNUNET_MessageHeader header;
116
117 /**
118 * Processing options
119 */
120 uint32_t options GNUNET_PACKED;
121
122 /**
123 * Content type.
124 */
125 uint32_t block_type GNUNET_PACKED;
126
127 /**
128 * Hop count
129 */
130 uint32_t hop_count GNUNET_PACKED;
131
132 /**
133 * Replication level for this message
134 * In the current implementation, this value is not used.
135 */
136 uint32_t desired_replication_level GNUNET_PACKED;
137
138 /**
139 * Length of the PUT path that follows (if tracked).
140 */
141 uint32_t put_path_length GNUNET_PACKED;
142
143 /**
144 * Best known destination (could be my friend or finger) which should
145 * get this message next.
146 */
147 struct GNUNET_PeerIdentity best_known_destination;
148
149 /**
150 * In case best_known_destination is a finger, then trail to reach
151 * to that finger. Else its default value is 0.
152 */
153 struct GNUNET_HashCode intermediate_trail_id;
154
155 /**
156 * When does the content expire?
157 */
158 struct GNUNET_TIME_AbsoluteNBO expiration_time;
159
160 /**
161 * The key to store the value under.
162 */
163 struct GNUNET_HashCode key GNUNET_PACKED;
164
165 /* put path (if tracked) */
166
167 /* Payload */
168
169};
96 170
97/** 171/**
98 * Wrap around in peer identity circle. 172 * P2P GET message
99 */ 173 */
100#define PEER_IDENTITES_WRAP_AROUND pow(2, 256) - 1 174struct PeerGetMessage
175{
176 /**
177 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
178 */
179 struct GNUNET_MessageHeader header;
180
181 /**
182 * Processing options
183 */
184 uint32_t options GNUNET_PACKED;
101 185
102GNUNET_NETWORK_STRUCT_BEGIN 186 /**
187 * Desired content type.
188 */
189 uint32_t block_type GNUNET_PACKED;
190
191 /**
192 * Hop count
193 */
194 uint32_t hop_count GNUNET_PACKED;
195
196 /**
197 * Desired replication level for this request.
198 * In the current implementation, this value is not used.
199 */
200 uint32_t desired_replication_level GNUNET_PACKED;
201
202 /**
203 * Total number of peers in get path.
204 */
205 unsigned int get_path_length;
206
207 /**
208 * Best known destination (could be my friend or finger) which should
209 * get this message next.
210 */
211 struct GNUNET_PeerIdentity best_known_destination;
212
213 /**
214 * In case best_known_destination is a finger, then trail to reach
215 * to that finger. Else its default value is 0.
216 */
217 struct GNUNET_HashCode intermediate_trail_id;
218
219 /**
220 * The key we are looking for.
221 */
222 struct GNUNET_HashCode key;
223
224 /* Get path. */
225
226};
103 227
104/** 228/**
105 * P2P Trail setup message 229 * P2P Result message
106 */ 230 */
107struct PeerTrailSetupMessage 231struct PeerGetResultMessage
108{ 232{
109 /** 233 /**
110 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP 234 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT
111 */ 235 */
112 struct GNUNET_MessageHeader header; 236 struct GNUNET_MessageHeader header;
113 237
114 /* Bitmask of options, 1 = IS_PREDECESSOR */
115 // int32_t options;
116
117 /** 238 /**
118 * Peer closest to this value will be our finger. 239 * The type for the data.
240 */
241 uint32_t type GNUNET_PACKED;
242
243 /**
244 * Number of peers recorded in the outgoing path from source to the
245 * stored location of this message.
119 */ 246 */
120 uint64_t ultimate_destination_finger; 247 uint32_t put_path_length GNUNET_PACKED;
248
249 /**
250 * Length of the GET path that follows (if tracked).
251 */
252 uint32_t get_path_length GNUNET_PACKED;
253
254 /**
255 * Peer which queried for get and should get the result.
256 */
257 struct GNUNET_PeerIdentity querying_peer;
258
259 /**
260 * When does the content expire?
261 */
262 struct GNUNET_TIME_Absolute expiration_time;
121 263
122 /** 264 /**
123 * Source peer which wants to setup the trail to one of its finger. 265 * The key of the corresponding GET request.
124 */ 266 */
125 struct GNUNET_PeerIdentity source_peer; 267 struct GNUNET_HashCode key;
268
269 /* put path (if tracked) */
126 270
271 /* get path (if tracked) */
272
273 /* Payload */
274
275};
276
277/**
278 * P2P Trail setup message
279 */
280struct PeerTrailSetupMessage
281{
282 /**
283 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP
284 */
285 struct GNUNET_MessageHeader header;
286
287 /**
288 * Is source_peer trying to setup the trail to any finger or predecessor.
289 */
290 uint32_t is_predecessor;
291
127 /** 292 /**
128 * Peer to which this packet is forwarded. 293 * Peer closest to this value will be our finger.
129 */ 294 */
130 struct GNUNET_PeerIdentity next_destination; // rename "best_known_dest" 295 uint64_t ultimate_destination_finger_value;
131 296
132 /** 297 /**
133 * Index into finger peer map, in Network Byte Order. 298 * Source peer which wants to setup the trail to one of its finger.
134 */ 299 */
135 uint32_t finger_map_index; // remove this, include ultimate_destination_finger in response, calculate index from it 300 struct GNUNET_PeerIdentity source_peer;
136 301
137 /** 302 /**
138 * Number of entries in trail_list, in Network Byte Order. 303 * Best known destination (could be my friend or finger) which should
304 * get this message next.
139 */ 305 */
140 uint32_t trail_length GNUNET_PACKED; // remove this, calculte length from message size 306 struct GNUNET_PeerIdentity best_known_destination;
141 307
142 /** 308 /**
143 * Trail id of any intermediate trail we may encounter while doing trail setup. 309 * Trail id of any intermediate trail we may encounter while doing trail setup.
144 */ 310 */
145 struct GNUNET_HashCode intermediate_trail_id; 311 struct GNUNET_HashCode intermediate_trail_id;
146 312
147 /** 313 /**
148 * Trail id for trail which we are trying to setup. 314 * Trail id for trail which we are trying to setup.
149 */ 315 */
150 struct GNUNET_HashCode new_trail_id; // rename to "trail_id" 316 struct GNUNET_HashCode trail_id;
151 317
152 /* Trail formed in the process. */ 318 /* List of peers which are part of trail setup so far.
153 /* GNUNET_PeerIdentity trail_list[] */ 319 * Trail does NOT include source_peer and peer which will be closest to
320 * ultimate_destination_finger_value.
321 * struct GNUNET_PeerIdentity trail_list[]
322 */
154}; 323};
155 324
156/** 325/**
@@ -170,27 +339,30 @@ struct PeerTrailSetupResultMessage
170 struct GNUNET_PeerIdentity finger_identity; 339 struct GNUNET_PeerIdentity finger_identity;
171 340
172 /** 341 /**
173 * Peer which was looking for the trail to finger. 342 * Peer which started trail_setup to find trail to finger_identity
174 */ 343 */
175 struct GNUNET_PeerIdentity destination_peer; // querying_peer 344 struct GNUNET_PeerIdentity querying_peer;
176 345
177 /** 346 /**
178 * Index into finger peer map in NBO. 347 * Is the trail setup to querying_peer's predecessor or finger?
179 */ 348 */
180 uint32_t finger_map_index; // flag/option with IS_PREDECESSOR 349 uint32_t is_predecessor;
181 350
182 /** 351 /**
183 * Number of entries in trail list in NBO. 352 * Value to which finger_identity is the closest peer.
184 */ 353 */
185 uint32_t trail_length GNUNET_PACKED; // remove, calculate 354 uint64_t ulitmate_destination_finger_value;
186 355
187 /** 356 /**
188 * Identifier of the trail. 357 * Identifier of the trail from querying peer to finger_identity, NOT
358 * including both endpoints.
189 */ 359 */
190 struct GNUNET_HashCode trail_id; 360 struct GNUNET_HashCode trail_id;
191 361
192 /* Trail from "destination_peer" to finger_identity, NOT including both */ 362 /* List of peers which are part of the trail from querying peer to
193 /* struct GNUNET_PeerIdentity trail[] */ 363 * finger_identity, NOT including both endpoints.
364 * struct GNUNET_PeerIdentity trail[]
365 */
194}; 366};
195 367
196/** 368/**
@@ -204,12 +376,12 @@ struct PeerVerifySuccessorMessage
204 struct GNUNET_MessageHeader header; 376 struct GNUNET_MessageHeader header;
205 377
206 /** 378 /**
207 * Source peer which wants to verify its successor. 379 * Peer which wants to verify its successor.
208 */ 380 */
209 struct GNUNET_PeerIdentity source_peer; 381 struct GNUNET_PeerIdentity source_peer;
210 382
211 /** 383 /**
212 * My current successor. 384 * Source Peer's current successor.
213 */ 385 */
214 struct GNUNET_PeerIdentity successor; 386 struct GNUNET_PeerIdentity successor;
215 387
@@ -218,15 +390,17 @@ struct PeerVerifySuccessorMessage
218 */ 390 */
219 struct GNUNET_HashCode trail_id; 391 struct GNUNET_HashCode trail_id;
220 392
221 /** 393 /* List of the peers which are part of trail to reach from source_peer
222 * Total number of peers to reach from source to successor. 394 * to successor, NOT including them
395 * struct GNUNET_PeerIdentity trail[]
223 */ 396 */
224 unsigned int trail_length;
225
226 /* Trail. */
227}; 397};
228 398
229/** 399/**
400 * FIXME: In case you append the trail it may contain the same peer twice.
401 * So, when you call search_my_index it can return error. Solution is while
402 * appending the entry first check for duplicate entries or may be don't
403 * send the current_predecessor at all.
230 * P2P Verify Successor Result Message 404 * P2P Verify Successor Result Message
231 */ 405 */
232struct PeerVerifySuccessorResultMessage 406struct PeerVerifySuccessorResultMessage
@@ -237,9 +411,9 @@ struct PeerVerifySuccessorResultMessage
237 struct GNUNET_MessageHeader header; 411 struct GNUNET_MessageHeader header;
238 412
239 /** 413 /**
240 * Destination peer which sent the request to verify its successor. 414 * Peer which sent the request to verify its successor.
241 */ 415 */
242 struct GNUNET_PeerIdentity destination_peer; 416 struct GNUNET_PeerIdentity querying_peer;
243 417
244 /** 418 /**
245 * Successor to which PeerVerifySuccessorMessage was sent. 419 * Successor to which PeerVerifySuccessorMessage was sent.
@@ -247,28 +421,25 @@ struct PeerVerifySuccessorResultMessage
247 struct GNUNET_PeerIdentity source_successor; 421 struct GNUNET_PeerIdentity source_successor;
248 422
249 /** 423 /**
250 * source_successor's predecessor 424 * Current Predecessor of source_successor. It can be same as querying peer
425 * or different.
251 */ 426 */
252 struct GNUNET_PeerIdentity my_predecessor; 427 struct GNUNET_PeerIdentity current_predecessor;
253 428
254 /** 429 /**
255 * Trail identifier of trail from my_predecessor to source_successor. 430 * Trail identifier of trail from querying_peer to source_successor.
256 */ 431 */
257 struct GNUNET_HashCode trail_id; 432 struct GNUNET_HashCode trail_id;
258 433
259 /** 434 /**
260 * Direction in which we are looking at the trail. 435 * Direction in which we are looking at the trail.
261 */ 436 */
262 enum GDS_ROUTING_trail_direction trail_direction; 437 uint32_t trail_direction;
263 438
264 /** 439 /* In case current_predecessor != querying_peer, then trail to reach from
265 * Total number of peers in trail from source_successor to my_predecessor 440 * querying_peer to current_predecessor, NOT including end points.
266 * if my_predecessor is not same as destination_peer. 441 * struct GNUNET_PeerIdentity trail[]
267 */ 442 */
268 uint32_t trail_length;
269
270 /* Trail from source_successor to my_predecessor where
271 * my_predecessor != destination_peer*/
272}; 443};
273 444
274/** 445/**
@@ -282,29 +453,31 @@ struct PeerNotifyNewSuccessorMessage
282 struct GNUNET_MessageHeader header; 453 struct GNUNET_MessageHeader header;
283 454
284 /** 455 /**
285 * Source peer which wants to notify its new successor. 456 * Peer which wants to notify its new successor.
286 */ 457 */
287 struct GNUNET_PeerIdentity source_peer; 458 struct GNUNET_PeerIdentity source_peer;
288 459
289 /** 460 /**
290 * New successor identity. 461 * New successor of source_peer.
291 */ 462 */
292 struct GNUNET_PeerIdentity destination_peer; 463 struct GNUNET_PeerIdentity new_successor;
293 464
294 /** 465 /**
295 * Total number of peers in trail from source_peer to destination_peer 466 * Unique identifier of the trail from source_peer to new_successor,
296 */ 467 * NOT including the endpoints.
297 unsigned int trail_length;
298
299 /**
300 * Unique identifier of the trail.
301 */ 468 */
302 struct GNUNET_HashCode trail_id; 469 struct GNUNET_HashCode trail_id;
303 470
304 /* Trail. */ 471 /* List of peers in trail from source_peer to new_successor,
472 * NOT including the endpoints.
473 * struct GNUNET_PeerIdentity trail[]
474 */
305}; 475};
306 476
307/** 477/**
478 * FIXME: Check if you can merge trail compression and trail teardown
479 * by modifying routing table structure a little bit. PRIORITY: at the end
480 * less code.
308 * P2P Trail Compression Message. 481 * P2P Trail Compression Message.
309 */ 482 */
310struct PeerTrailCompressionMessage 483struct PeerTrailCompressionMessage
@@ -366,7 +539,7 @@ struct PeerTrailTearDownMessage
366 /** 539 /**
367 * Direction of trail. 540 * Direction of trail.
368 */ 541 */
369 enum GDS_ROUTING_trail_direction trail_direction; 542 uint32_t trail_direction;
370}; 543};
371 544
372 545
@@ -381,42 +554,39 @@ struct PeerTrailRejectionMessage
381 struct GNUNET_MessageHeader header; 554 struct GNUNET_MessageHeader header;
382 555
383 /** 556 /**
384 * Source peer which wants to set up the trail. 557 * Peer which wants to set up the trail.
385 */ 558 */
386 struct GNUNET_PeerIdentity source_peer; 559 struct GNUNET_PeerIdentity source_peer;
387 560
388 /** 561 /**
389 * Peer which sent trail rejection message. 562 * Peer which sent trail rejection message as it it congested.
390 */ 563 */
391 struct GNUNET_PeerIdentity congested_peer; 564 struct GNUNET_PeerIdentity congested_peer;
392 565
393 /** 566 /**
394 * Peer identity which will be successor to this value will be finger of 567 * Peer identity closest to this value will be finger of
395 * source_peer. 568 * source_peer.
396 */ 569 */
397 uint64_t ultimate_destination_finger_identity_value; 570 uint64_t ultimate_destination_finger_value;
398
399 /**
400 * Index in finger peer map of source peer.
401 */
402 uint32_t finger_map_index;
403 571
404 /** 572 /**
405 * Total number of peers in the trail. 573 * Is source_peer trying to setup the trail to its predecessor or finger.
406 */ 574 */
407 uint32_t trail_length; 575 uint32_t is_predecessor;
408 576
409 /** 577 /**
410 * Identifier for the trail source peer is trying to setup. 578 * Identifier for the trail that source peer is trying to setup.
411 */ 579 */
412 struct GNUNET_HashCode trail_id; 580 struct GNUNET_HashCode trail_id;
581
413 /** 582 /**
414 * Relative time for which congested_peer will remain congested. 583 * Relative time for which congested_peer will remain congested.
415 */ 584 */
416 struct GNUNET_TIME_Relative congestion_time; 585 struct GNUNET_TIME_Relative congestion_time;
417 586
418 /* Trail_list from source_peer to peer which sent the message for trail setup 587 /* Trail_list from source_peer to peer which sent the message for trail setup
419 * to congested peer.*/ 588 * to congested peer. This trail does NOT include source_peer.
589 struct GNUNET_PeerIdnetity trail[]*/
420}; 590};
421 591
422/** 592/**
@@ -430,26 +600,24 @@ struct PeerAddTrailMessage
430 struct GNUNET_MessageHeader header; 600 struct GNUNET_MessageHeader header;
431 601
432 /** 602 /**
433 * Source peer of the routing trail. 603 * Source of the routing trail.
434 */ 604 */
435 struct GNUNET_PeerIdentity source_peer; 605 struct GNUNET_PeerIdentity source_peer;
436 606
437 /** 607 /**
438 * Destination peer of the routing trail. 608 * Destination of the routing trail.
439 */ 609 */
440 struct GNUNET_PeerIdentity destination_peer; 610 struct GNUNET_PeerIdentity destination_peer;
441 611
442 /** 612 /**
443 * Total number of peers from source peer to destination peer. 613 * Unique identifier of the trail from source_peer to destination_peer,
444 */ 614 * NOT including the endpoints.
445 unsigned int trail_length;
446
447 /**
448 * Unique identifier of the trail.
449 */ 615 */
450 struct GNUNET_HashCode trail_id; 616 struct GNUNET_HashCode trail_id;
451 617
452 /* Trail from source peer to destination peer. */ 618 /* Trail from source peer to destination peer, NOT including them.
619 * struct GNUNET_PeerIdentity trail[]
620 */
453}; 621};
454 622
455GNUNET_NETWORK_STRUCT_END 623GNUNET_NETWORK_STRUCT_END
@@ -500,7 +668,8 @@ struct FriendInfo
500 struct GNUNET_PeerIdentity id; 668 struct GNUNET_PeerIdentity id;
501 669
502 /** 670 /**
503 * Number of trails for which this friend is the first hop. 671 * Number of trails for which this friend is the first hop or if the friend
672 * is finger.
504 */ 673 */
505 unsigned int trails_count; 674 unsigned int trails_count;
506 675
@@ -532,19 +701,19 @@ struct FriendInfo
532}; 701};
533 702
534/** 703/**
535 * An individual trail to reach to a finger. 704 * An individual element of the trail to reach to a finger.
536 */ 705 */
537struct Trail // "node" "element" "relay" 706struct Trail_Element
538{ 707{
539 /** 708 /**
540 * Pointer to next item in the list 709 * Pointer to next item in the list
541 */ 710 */
542 struct Trail *next; 711 struct Trail_Element *next;
543 712
544 /** 713 /**
545 * Pointer to prev item in the list 714 * Pointer to prev item in the list
546 */ 715 */
547 struct Trail *prev; 716 struct Trail_Element *prev;
548 717
549 /** 718 /**
550 * An element in this trail. 719 * An element in this trail.
@@ -553,19 +722,22 @@ struct Trail // "node" "element" "relay"
553}; 722};
554 723
555/** 724/**
556 * List of all trails to reach a particular finger. 725 * FIXME: removed first_friend_trails_count, need to write a function
726 * to calculate each time we need it. Else, keep a pointer to first
727 * friend of in the trail.
728 * Information about an individual trail.
557 */ 729 */
558struct TrailList // "trail": list of "elements" 730struct Trail
559{ 731{
560 /** 732 /**
561 * Head of trail. 733 * Head of trail.
562 */ 734 */
563 struct Trail *trail_head; 735 struct Trail_Element *trail_head;
564 736
565 /** 737 /**
566 * Tail of trail. 738 * Tail of trail.
567 */ 739 */
568 struct Trail *trail_tail; 740 struct Trail_Element *trail_tail;
569 741
570 /** 742 /**
571 * Unique identifier of this trail. 743 * Unique identifier of this trail.
@@ -576,12 +748,6 @@ struct TrailList // "trail": list of "elements"
576 * Length of trail pointed 748 * Length of trail pointed
577 */ 749 */
578 unsigned int trail_length; 750 unsigned int trail_length;
579
580 /**
581 * Number of trails that the first friend of this trail is a part of.
582 */
583 unsigned int first_friend_trail_count; // remove this, duplicate variable!
584 // include a pointer to Friend or function that calculates it
585}; 751};
586 752
587/** 753/**
@@ -601,16 +767,18 @@ struct FingerInfo
601 767
602 /** 768 /**
603 * Number of trails setup so far for this finger. 769 * Number of trails setup so far for this finger.
770 * Should not cross MAXIMUM_TRAILS_PER_FINGER.
604 */ 771 */
605 uint32_t trails_count; 772 uint32_t trails_count;
606 773
607 /** 774 /**
608 * Array of trails. 775 * Array of trails to reach to this finger.
609 */ 776 */
610 struct TrailList trail_list[MAXIMUM_TRAILS_PER_FINGER]; // OK! 777 struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER];
611}; 778};
612 779
613/** 780/**
781 * FIXME: Need to check if we need all the fields or not.
614 * Data structure to keep track of closest peer seen so far in find_successor() 782 * Data structure to keep track of closest peer seen so far in find_successor()
615 */ 783 */
616struct Closest_Peer 784struct Closest_Peer
@@ -626,7 +794,7 @@ struct Closest_Peer
626 struct GNUNET_HashCode trail_id; 794 struct GNUNET_HashCode trail_id;
627 795
628 /** 796 /**
629 * First hop, NULL in case of friend and my_identity 797 * FIXME: see the usage of this field and write comment.
630 */ 798 */
631 struct GNUNET_PeerIdentity next_hop; 799 struct GNUNET_PeerIdentity next_hop;
632 800
@@ -634,13 +802,15 @@ struct Closest_Peer
634 * Next destination. In case of friend and my_identity , it is same as next_hop 802 * Next destination. In case of friend and my_identity , it is same as next_hop
635 * In case of finger it is finger identity. 803 * In case of finger it is finger identity.
636 */ 804 */
637 struct GNUNET_PeerIdentity next_destination; 805 struct GNUNET_PeerIdentity best_known_destination;
638}; 806};
639 807
640/** 808/**
809 * FIXME: now I have removed the first_friend_trail_count,
810 * Need to update the code to find the count.
641 * Data structure to store the trail chosen to reach to finger. 811 * Data structure to store the trail chosen to reach to finger.
642 */ 812 */
643struct Correct_Trail 813struct Selected_Finger_Trail
644{ 814{
645 /** 815 /**
646 * First friend in the trail to reach finger. 816 * First friend in the trail to reach finger.
@@ -680,13 +850,19 @@ static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
680static struct GNUNET_CONTAINER_MultiHashMap32 *finger_hashmap; 850static struct GNUNET_CONTAINER_MultiHashMap32 *finger_hashmap;
681 851
682/** 852/**
853 * FIXME: Check if you can replace finger_hashmap by an array of fingers.
854 * Array of all the fingers.
855 *
856static struct FingerInfo finger_table [MAX_FINGERS];*/
857
858/**
683 * Handle to CORE. 859 * Handle to CORE.
684 */ 860 */
685static struct GNUNET_CORE_Handle *core_api; 861static struct GNUNET_CORE_Handle *core_api;
686 862
687/** 863/**
688 * The current finger index that we have want to find trail to. We start the 864 * The current finger index that we have want to find trail to. We start the
689 * search with value = 0, i.e. successor peer and then go to PREDCESSOR_FINGER_ID 865 * search with value = 0, i.e. successor and then go to PREDCESSOR_FINGER_ID
690 * and decrement it. For any index 63 <= index < 0, if finger is same as successor, 866 * and decrement it. For any index 63 <= index < 0, if finger is same as successor,
691 * we reset this index to 0. 867 * we reset this index to 0.
692 */ 868 */
@@ -766,7 +942,6 @@ core_transmit_notify (void *cls, size_t size, void *buf)
766 942
767 943
768/** 944/**
769 * FIXME: assertion fails at the end of this function. also in core_api.c at 1299.
770 * Transmit all messages in the friend's message queue. 945 * Transmit all messages in the friend's message queue.
771 * 946 *
772 * @param peer message queue to process 947 * @param peer message queue to process
@@ -799,29 +974,29 @@ process_friend_queue (struct FriendInfo *peer)
799 974
800/** 975/**
801 * Construct a trail setup message and forward it to target_friend 976 * Construct a trail setup message and forward it to target_friend
802 * @param source_peer Source peer which wants to setup the trail 977 * @param source_peer Peer which wants to setup the trail
803 * @param ultimate_destination_finger Peer identity closest to this value will 978 * @param ultimate_destination_finger_value Peer identity closest to this value
804 * be finger to @a source_peer 979 * will be finger to @a source_peer
805 * @param next_destination Peer which should get the packet. I can be same as 980 * @param best_known_destination Best known destination (could be finger or friend)
806 * target_friend or different. 981 * which should get this message.
807 * @param target_friend Friend to which message is forwarded now. 982 * @param target_friend Friend to which message is forwarded now.
808 * @param trail_length Total number of peers in trail setup so far. 983 * @param trail_length Total number of peers in trail setup so far.
809 * @param trail_peer_list Trail setup so far 984 * @param trail_peer_list Trail setup so far
810 * @param finger_map_index Index in finger map for which we are looking for finger. 985 * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
811 * @param trail_id Unique identifier for the trail we are trying to setup. 986 * @param trail_id Unique identifier for the trail we are trying to setup.
812 * @param intermediate_trail_id Trail id of any intermediate trail we may have to 987 * @param intermediate_trail_id Trail id of intermediate trail to reach to
813 * traverse during trail setup. If not used then set to 988 * best_known_destination when its a finger. If not
814 * 0. 989 * used then set to 0.
815 */ 990 */
816void 991void
817GDS_NEIGHBOURS_send_trail_setup (const struct GNUNET_PeerIdentity source_peer, 992GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
818 uint64_t ultimate_destination_finger, 993 uint64_t ultimate_destination_finger_value,
819 struct GNUNET_PeerIdentity next_destination, 994 struct GNUNET_PeerIdentity best_known_destination,
820 struct FriendInfo *target_friend, 995 struct FriendInfo *target_friend,
821 unsigned int trail_length, 996 unsigned int trail_length,
822 const struct GNUNET_PeerIdentity *trail_peer_list, 997 const struct GNUNET_PeerIdentity *trail_peer_list,
823 unsigned int finger_map_index, 998 unsigned int is_predecessor,
824 struct GNUNET_HashCode new_trail_id, 999 struct GNUNET_HashCode trail_id,
825 struct GNUNET_HashCode *intermediate_trail_id) 1000 struct GNUNET_HashCode *intermediate_trail_id)
826{ 1001{
827 struct P2PPendingMessage *pending; 1002 struct P2PPendingMessage *pending;
@@ -850,16 +1025,17 @@ GDS_NEIGHBOURS_send_trail_setup (const struct GNUNET_PeerIdentity source_peer,
850 pending->msg = &tsm->header; 1025 pending->msg = &tsm->header;
851 tsm->header.size = htons (msize); 1026 tsm->header.size = htons (msize);
852 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP); 1027 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP);
853 tsm->ultimate_destination_finger = GNUNET_htonll (ultimate_destination_finger); 1028 tsm->ultimate_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
854 tsm->source_peer = source_peer; 1029 tsm->source_peer = source_peer;
855 tsm->next_destination = next_destination; 1030 tsm->best_known_destination = best_known_destination;
856 tsm->trail_length = htonl (trail_length); 1031 tsm->is_predecessor = htonl (is_predecessor);
857 tsm->finger_map_index = htonl (finger_map_index); 1032 tsm->trail_id = trail_id;
858 tsm->new_trail_id = new_trail_id; 1033
859 if (NULL == intermediate_trail_id) 1034 if (NULL == intermediate_trail_id)
860 memset (&tsm->intermediate_trail_id, 0, sizeof (tsm->intermediate_trail_id)); 1035 memset (&tsm->intermediate_trail_id, 0, sizeof (tsm->intermediate_trail_id));
861 else 1036 else
862 tsm->intermediate_trail_id = *intermediate_trail_id; 1037 tsm->intermediate_trail_id = *intermediate_trail_id;
1038
863 if (trail_length > 0) 1039 if (trail_length > 0)
864 { 1040 {
865 peer_list = (struct GNUNET_PeerIdentity *) &tsm[1]; 1041 peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
@@ -874,21 +1050,26 @@ GDS_NEIGHBOURS_send_trail_setup (const struct GNUNET_PeerIdentity source_peer,
874 1050
875/** 1051/**
876 * Construct a trail setup result message and forward it to target friend. 1052 * Construct a trail setup result message and forward it to target friend.
877 * @param destination_peer Peer which will get the trail to one of its finger. 1053 * @param querying_peer Peer which sent the trail setup request and should get
878 * @param source_finger Peer to which the trail has been setup to. 1054 * the result back.
1055 * @param Finger Peer to which the trail has been setup to.
879 * @param target_friend Friend to which this message should be forwarded. 1056 * @param target_friend Friend to which this message should be forwarded.
880 * @param trail_length Numbers of peers in the trail. 1057 * @param trail_length Numbers of peers in the trail.
881 * @param trail_peer_list Peers which are part of the trail from source to destination. 1058 * @param trail_peer_list Peers which are part of the trail from q
882 * @param finger_map_index Index in finger peer map 1059 * querying_peer to Finger, NOT including them.
1060 * @param is_predecessor Is @a Finger predecessor to @a querying_peer
1061 * @param ultimate_destination_finger_value Value to which @a finger is the closest
1062 * peer.
883 * @param trail_id Unique identifier of the trail. 1063 * @param trail_id Unique identifier of the trail.
884 */ 1064 */
885void 1065void
886GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity destination_peer, 1066GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity querying_peer,
887 struct GNUNET_PeerIdentity source_finger, 1067 struct GNUNET_PeerIdentity finger,
888 struct FriendInfo *target_friend, 1068 struct FriendInfo *target_friend,
889 unsigned int trail_length, 1069 unsigned int trail_length,
890 const struct GNUNET_PeerIdentity *trail_peer_list, 1070 const struct GNUNET_PeerIdentity *trail_peer_list,
891 unsigned int finger_map_index, 1071 unsigned int is_predecessor,
1072 uint64_t ultimate_destination_finger_value,
892 struct GNUNET_HashCode trail_id) 1073 struct GNUNET_HashCode trail_id)
893{ 1074{
894 struct P2PPendingMessage *pending; 1075 struct P2PPendingMessage *pending;
@@ -918,12 +1099,12 @@ GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity destination_p
918 pending->msg = &tsrm->header; 1099 pending->msg = &tsrm->header;
919 tsrm->header.size = htons (msize); 1100 tsrm->header.size = htons (msize);
920 tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT); 1101 tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT);
921 tsrm->destination_peer = destination_peer; 1102 tsrm->querying_peer = querying_peer;
922 tsrm->finger_identity = source_finger; 1103 tsrm->finger_identity = finger;
923 tsrm->trail_length = htonl (trail_length); 1104 tsrm->is_predecessor = htonl (is_predecessor);
924 tsrm->finger_map_index = htonl (finger_map_index);
925 tsrm->trail_id = trail_id; 1105 tsrm->trail_id = trail_id;
926 1106 tsrm->ulitmate_destination_finger_value =
1107 GNUNET_htonll (ultimate_destination_finger_value);
927 peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1]; 1108 peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
928 if (trail_length > 0) 1109 if (trail_length > 0)
929 { 1110 {
@@ -938,24 +1119,25 @@ GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity destination_p
938 1119
939/** 1120/**
940 * Send trail rejection message to next_hop 1121 * Send trail rejection message to next_hop
941 * @param source_peer Source peer which is trying to setup the trail. 1122 * @param source_peer Peer which is trying to setup the trail.
942 * @param finger_identity Peer closest to this value will be @a source_peer's finger 1123 * @param ultimate_destination_finger_value Peer closest to this value will be
1124 * @a source_peer's finger
943 * @param congested_peer Peer which sent this message as it is congested. 1125 * @param congested_peer Peer which sent this message as it is congested.
944 * @param next_hop Peer to which we are forwarding this message. 1126 * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
945 * @param finger_map_index Index in finger peermap for which we are searching for finger.
946 * @param trail_peer_list Trails seen so far in trail setup before getting rejected 1127 * @param trail_peer_list Trails seen so far in trail setup before getting rejected
947 * by congested_peer 1128 * by congested_peer. This does not include @a source_peer
948 * @param trail_length Total number of peers in trail_peer_list 1129 * @param trail_length Total number of peers in trail_peer_list, not including
1130 * @a source_peer
949 * @param trail_id Unique identifier of this trail. 1131 * @param trail_id Unique identifier of this trail.
950 * @param congestion_timeout Duration given by congested peer as an estimate of 1132 * @param congestion_timeout Duration given by congested peer as an estimate of
951 * how long it may remain congested. 1133 * how long it may remain congested.
952 */ 1134 */
953void 1135void
954GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer, 1136GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
955 uint64_t finger_identity, 1137 uint64_t ultimate_destination_finger_value,
956 struct GNUNET_PeerIdentity congested_peer, 1138 struct GNUNET_PeerIdentity congested_peer,
957 unsigned int finger_map_index, 1139 unsigned int is_predecessor,
958 struct GNUNET_PeerIdentity *trail_peer_list, 1140 const struct GNUNET_PeerIdentity *trail_peer_list,
959 unsigned int trail_length, 1141 unsigned int trail_length,
960 struct GNUNET_HashCode trail_id, 1142 struct GNUNET_HashCode trail_id,
961 struct FriendInfo *target_friend, 1143 struct FriendInfo *target_friend,
@@ -991,14 +1173,16 @@ GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
991 trm->source_peer = source_peer; 1173 trm->source_peer = source_peer;
992 trm->congested_peer = congested_peer; 1174 trm->congested_peer = congested_peer;
993 trm->congestion_time = congestion_timeout; 1175 trm->congestion_time = congestion_timeout;
994 trm->finger_map_index = htonl (finger_map_index); 1176 trm->is_predecessor = htonl (is_predecessor);
995 trm->trail_id = trail_id; 1177 trm->trail_id = trail_id;
1178 trm->ultimate_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
996 1179
997 peer_list = (struct GNUNET_PeerIdentity *) &trm[1]; 1180 peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
998 if (trail_length > 0) 1181 if (trail_length > 0)
999 { 1182 {
1000 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity)); 1183 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
1001 } 1184 }
1185
1002 /* Send the message to chosen friend. */ 1186 /* Send the message to chosen friend. */
1003 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending); 1187 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1004 target_friend->pending_count++; 1188 target_friend->pending_count++;
@@ -1010,10 +1194,12 @@ GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
1010 * Construct a verify successor message and forward it to target_friend. 1194 * Construct a verify successor message and forward it to target_friend.
1011 * @param source_peer Peer which wants to verify its successor. 1195 * @param source_peer Peer which wants to verify its successor.
1012 * @param successor Peer which is @a source_peer's current successor. 1196 * @param successor Peer which is @a source_peer's current successor.
1013 * @param trail_id Identifier of trail to reach successor. 1197 * @param trail_id Unique Identifier of trail from @a source_peer to @a successor,
1014 * @param trail Trail to reach from source_peer to successor 1198 * NOT including them.
1199 * @param trail List of peers which are part of trail to reach from @a source_peer
1200 * to @a successor, NOT including them.
1015 * @param trail_length Total number of peers in @a trail. 1201 * @param trail_length Total number of peers in @a trail.
1016 * @param target_friend Message send to this friend. 1202 * @param target_friend Next friend to get this message.
1017 */ 1203 */
1018void 1204void
1019GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer, 1205GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
@@ -1051,8 +1237,7 @@ GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_
1051 vsm->source_peer = source_peer; 1237 vsm->source_peer = source_peer;
1052 vsm->successor = successor; 1238 vsm->successor = successor;
1053 vsm->trail_id = trail_id; 1239 vsm->trail_id = trail_id;
1054 vsm->trail_length = htonl (trail_length); 1240
1055
1056 if (trail_length > 0) 1241 if (trail_length > 0)
1057 { 1242 {
1058 peer_list = (struct GNUNET_PeerIdentity *) &vsm[1]; 1243 peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
@@ -1067,6 +1252,7 @@ GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_
1067 1252
1068 1253
1069/** 1254/**
1255 * FIXME: check if we can merge send_trail_teardown and trail_compression
1070 * Construct a trail teardown message and send it to target_friend 1256 * Construct a trail teardown message and send it to target_friend
1071 * @param source_peer Source of the trail. 1257 * @param source_peer Source of the trail.
1072 * @param destination_peer Destination of the trail. 1258 * @param destination_peer Destination of the trail.
@@ -1119,19 +1305,24 @@ GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_PeerIdentity source_peer,
1119 1305
1120 1306
1121/** 1307/**
1122 * Construct a verify successor message and send it to target_friend 1308 * Construct a verify successor result message and send it to target_friend
1123 * @param destination_peer 1309 * @param querying_peer Peer which sent the verify successor message.
1124 * @param source_successor 1310 * @param source_successor Current_successor of @a querying_peer.
1125 * @param succ_predecessor 1311 * @param current_predecessor Current predecessor of @a successor. Could be same
1126 * @param trail_id 1312 * or different from @a querying_peer.
1127 * @param trail 1313 * @param trail_id Unique identifier of the trail from @a querying_peer to
1128 * @param trail_length 1314 * @a successor, NOT including them.
1129 * @param target_friend 1315 * @param trail List of peers which are part of trail from @a querying_peer to
1316 * @a successor, NOT including them.
1317 * @param trail_length Total number of peers in @a trail
1318 * @param trail_direction Direction in which we are sending the message. In this
1319 * case we are sending result from @a successor to @a querying_peer.
1320 * @param target_friend Next friend to get this message.
1130 */ 1321 */
1131void 1322void
1132GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity destination_peer, 1323GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity querying_peer,
1133 struct GNUNET_PeerIdentity source_successor, 1324 struct GNUNET_PeerIdentity source_successor,
1134 struct GNUNET_PeerIdentity succ_predecessor, 1325 struct GNUNET_PeerIdentity current_predecessor,
1135 struct GNUNET_HashCode trail_id, 1326 struct GNUNET_HashCode trail_id,
1136 const struct GNUNET_PeerIdentity *trail, 1327 const struct GNUNET_PeerIdentity *trail,
1137 unsigned int trail_length, 1328 unsigned int trail_length,
@@ -1165,11 +1356,12 @@ GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity destinat
1165 pending->msg = &vsmr->header; 1356 pending->msg = &vsmr->header;
1166 vsmr->header.size = htons (msize); 1357 vsmr->header.size = htons (msize);
1167 vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT); 1358 vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT);
1168 vsmr->destination_peer = destination_peer; 1359 vsmr->querying_peer = querying_peer;
1169 vsmr->my_predecessor = succ_predecessor;
1170 vsmr->source_successor = source_successor; 1360 vsmr->source_successor = source_successor;
1361 vsmr->current_predecessor = current_predecessor;
1171 vsmr->trail_direction = htonl (trail_direction); 1362 vsmr->trail_direction = htonl (trail_direction);
1172 1363 vsmr->trail_id = trail_id;
1364
1173 if (trail_length > 0) 1365 if (trail_length > 0)
1174 { 1366 {
1175 peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1]; 1367 peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
@@ -1185,18 +1377,22 @@ GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity destinat
1185 1377
1186/** 1378/**
1187 * Construct a notify new successor message and send it to target_friend 1379 * Construct a notify new successor message and send it to target_friend
1188 * @param source_peer 1380 * @param source_peer Peer which wants to notify to its new successor that it
1189 * @param new_successor 1381 * could be its predecessor.
1190 * @param new_successor_trail 1382 * @param successor New successor of @a source_peer
1191 * @param new_successor_trail_length 1383 * @param successor_trail List of peers in Trail to reach from
1192 * @param new_succesor_trail_id 1384 * @a source_peer to @a new_successor, NOT including
1385 * the endpoints.
1386 * @param successor_trail_length Total number of peers in @a new_successor_trail.
1387 * @param successor_trail_id Unique identifier of @a new_successor_trail.
1388 * @param target_friend Next friend to get this message.
1193 */ 1389 */
1194void 1390void
1195GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer, 1391GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer,
1196 struct GNUNET_PeerIdentity new_successor, 1392 struct GNUNET_PeerIdentity successor,
1197 struct GNUNET_PeerIdentity *new_successor_trail, 1393 struct GNUNET_PeerIdentity *successor_trail,
1198 unsigned int new_successor_trail_length, 1394 unsigned int successor_trail_length,
1199 struct GNUNET_HashCode new_succesor_trail_id, 1395 struct GNUNET_HashCode succesor_trail_id,
1200 struct FriendInfo *target_friend) 1396 struct FriendInfo *target_friend)
1201{ 1397{
1202 struct PeerNotifyNewSuccessorMessage *nsm; 1398 struct PeerNotifyNewSuccessorMessage *nsm;
@@ -1205,7 +1401,7 @@ GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer
1205 size_t msize; 1401 size_t msize;
1206 1402
1207 msize = sizeof (struct PeerNotifyNewSuccessorMessage) + 1403 msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
1208 (new_successor_trail_length * sizeof(struct GNUNET_PeerIdentity)); 1404 (successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
1209 1405
1210 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 1406 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1211 { 1407 {
@@ -1226,15 +1422,15 @@ GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer
1226 pending->msg = &nsm->header; 1422 pending->msg = &nsm->header;
1227 nsm->header.size = htons (msize); 1423 nsm->header.size = htons (msize);
1228 nsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR); 1424 nsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR);
1425 nsm->new_successor = successor;
1229 nsm->source_peer = source_peer; 1426 nsm->source_peer = source_peer;
1230 nsm->destination_peer = new_successor; 1427 nsm->trail_id = succesor_trail_id;
1231 nsm->trail_length = htonl (new_successor_trail_length); 1428
1232 nsm->trail_id = new_succesor_trail_id; 1429 if (successor_trail_length > 0)
1233 if (new_successor_trail_length > 0)
1234 { 1430 {
1235 peer_list = (struct GNUNET_PeerIdentity *) &nsm[1]; 1431 peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
1236 memcpy (peer_list, new_successor_trail, 1432 memcpy (peer_list, successor_trail,
1237 new_successor_trail_length * sizeof (struct GNUNET_PeerIdentity)); 1433 successor_trail_length * sizeof (struct GNUNET_PeerIdentity));
1238 } 1434 }
1239 1435
1240 /* Send the message to chosen friend. */ 1436 /* Send the message to chosen friend. */
@@ -1248,10 +1444,12 @@ GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer
1248 * Construct an add_trail message and send it to target_friend 1444 * Construct an add_trail message and send it to target_friend
1249 * @param source_peer Source of the trail. 1445 * @param source_peer Source of the trail.
1250 * @param destination_peer Destination of the trail. 1446 * @param destination_peer Destination of the trail.
1251 * @param trail_id Unique identifer of the trail 1447 * @param trail_id Unique identifier of the trail from
1252 * @param trail Trail from @a source_peer to @a destination_peer 1448 * @a source_peer to @a destination_peer, NOT including the endpoints.
1449 * @param trail List of peers in Trail from @a source_peer to @a destination_peer,
1450 * NOT including the endpoints.
1253 * @param trail_length Total number of peers in @a trail. 1451 * @param trail_length Total number of peers in @a trail.
1254 * @param target_friend Next peer to get this message. 1452 * @param target_friend Next friend to get this message.
1255 */ 1453 */
1256void 1454void
1257GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer, 1455GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
@@ -1290,7 +1488,6 @@ GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
1290 adm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL); 1488 adm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL);
1291 adm->source_peer = source_peer; 1489 adm->source_peer = source_peer;
1292 adm->destination_peer = destination_peer; 1490 adm->destination_peer = destination_peer;
1293 adm->trail_length = htonl (trail_length);
1294 adm->trail_id = trail_id; 1491 adm->trail_id = trail_id;
1295 1492
1296 if (trail_length > 0) 1493 if (trail_length > 0)
@@ -1308,7 +1505,7 @@ GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
1308 1505
1309 1506
1310/** 1507/**
1311 * Merge into "teardown", add source and "last/new_first" in message struct. 1508 * FIXME: Merge into "teardown", add source and "last/new_first" in message struct.
1312 * Construct a trail compression message and send it to target_friend. 1509 * Construct a trail compression message and send it to target_friend.
1313 * @param source_peer Source of the trail. 1510 * @param source_peer Source of the trail.
1314 * @param destination_finger Destination of trail. 1511 * @param destination_finger Destination of trail.
@@ -1362,7 +1559,7 @@ GDS_NEIGHBOURS_send_trail_compression (struct GNUNET_PeerIdentity source_peer,
1362 1559
1363 1560
1364/** 1561/**
1365 * Seach my location in trail. 1562 * Search my location in trail.
1366 * @param trail List of peers 1563 * @param trail List of peers
1367 * @return my_index if found 1564 * @return my_index if found
1368 * -1 if no entry found. 1565 * -1 if no entry found.
@@ -1382,64 +1579,71 @@ search_my_index (const struct GNUNET_PeerIdentity *trail,
1382 return -1; 1579 return -1;
1383} 1580}
1384 1581
1582/**
1583 * Check if the friend is congested or have reached maximum number of trails
1584 * it can be part of of.
1585 * @param friend Friend to be chechked.
1586 * @return #GNUNET_YES if friend is not congested or have not crossed threshold.
1587 * #GNUNET_NO if friend is either congested or have crossed threshold
1588 */
1589static int
1590is_friend_congested (struct FriendInfo *friend)
1591{
1592 if ((friend->trails_count == TRAILS_THROUGH_FRIEND_THRESHOLD)||
1593 ((0 != GNUNET_TIME_absolute_get_remaining
1594 (friend->congestion_timestamp).rel_value_us)))
1595 return GNUNET_YES;
1596 else
1597 return GNUNET_NO;
1598}
1599
1385 1600
1386/** 1601/**
1387 * Iterate over the list of all the trails to reach Finger. In case the first 1602 * FIXME: here we should also check if iterator is null or not. It can be NULL
1388 * friend to reach the finger has crossed the trail threshold or is congested, 1603 * only if we insert randomly at locations. But as we are using trails_count
1604 * as the parameter, it should not happen.
1605 * Iterate over the list of all the trails of a finger. In case the first
1606 * friend to reach the finger has reached trail threshold or is congested,
1389 * then don't select it. In case there multiple available good trails to reach 1607 * then don't select it. In case there multiple available good trails to reach
1390 * to Finger, choose the one with shortest trail length. 1608 * to Finger, choose the one with shortest trail length.
1609 * Note: We use length as parameter. But we can use any other suitable parameter
1610 * also.
1391 * @param finger Finger 1611 * @param finger Finger
1392 * @return struct Correct_Trail which contains the first friend , trail id 1612 * @return struct Selected_Finger_Trail which contains the first friend , trail id
1393 * and trail length. NULL in case none of the trails are free. 1613 * and trail length. NULL in case none of the trails are free.
1394 */ 1614 */
1395static struct Correct_Trail * 1615static struct Selected_Finger_Trail *
1396select_trail_to_finger (struct FingerInfo *finger) 1616select_finger_trail (struct FingerInfo *finger)
1397{ 1617{
1398 struct FriendInfo *friend; 1618 struct FriendInfo *friend;
1399 struct TrailList *iterator; 1619 struct Trail *iterator;
1400 struct Correct_Trail *finger_trail; 1620 struct Selected_Finger_Trail *finger_trail;
1401 int i; 1621 unsigned int i;
1402
1403 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1404 &my_identity))
1405 return NULL;
1406
1407 finger_trail = GNUNET_new (struct Correct_Trail);
1408 1622
1623 finger_trail = GNUNET_new (struct Selected_Finger_Trail);
1624
1409 for (i = 0; i < finger->trails_count; i++) 1625 for (i = 0; i < finger->trails_count; i++)
1410 { 1626 {
1411 iterator = &finger->trail_list[i]; 1627 iterator = &finger->trail_list[i];
1412 if (iterator->trail_length > 0) 1628 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1413 { 1629 &iterator->trail_head->peer);
1414 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 1630 if (GNUNET_YES == is_friend_congested (friend))
1415 &iterator->trail_head->peer); 1631 continue;
1416 } 1632
1417 else 1633 /* Check if the trail length of this trail is least seen so far. If yes then
1418 { 1634 set finger_trail to this trail.*/
1419 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 1635 if (finger_trail->trail_length > iterator->trail_length)
1420 &finger->finger_identity);
1421 }
1422
1423 if ((friend->trails_count < TRAILS_THROUGH_FRIEND_THRESHOLD)||
1424 ((0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us)))
1425 { 1636 {
1426 if (iterator->trail_length == 0) 1637 finger_trail->friend = *friend;
1427 { 1638 finger_trail->trail_id = iterator->trail_id;
1428 finger_trail->friend = *friend; 1639 finger_trail->trail_length = iterator->trail_length;
1429 //finger_trail->trail_id = 0;
1430 finger_trail->trail_length = 0;
1431 return finger_trail;
1432 }
1433
1434 if (finger_trail->trail_length > iterator->trail_length)
1435 {
1436 finger_trail->friend = *friend;
1437 finger_trail->trail_id = iterator->trail_id;
1438 finger_trail->trail_length = iterator->trail_length;
1439 }
1440 } 1640 }
1441 } 1641 }
1442 1642
1643 /* No trail found. */
1644 if (i == finger->trails_count)
1645 finger_trail = NULL;
1646
1443 return finger_trail; 1647 return finger_trail;
1444} 1648}
1445 1649
@@ -1546,91 +1750,104 @@ select_closest_peer (struct GNUNET_PeerIdentity *peer1,
1546 * @param finger_map_index Index in finger peermap for which we are 1750 * @param finger_map_index Index in finger peermap for which we are
1547 * looking for a finger, to discern between 1751 * looking for a finger, to discern between
1548 * IS_PREDECESSOR or not. 1752 * IS_PREDECESSOR or not.
1549 * @return 1753 * @return Closest successor for value.
1550 */ 1754 */
1551static struct GNUNET_PeerIdentity * 1755static struct GNUNET_PeerIdentity *
1552find_successor (uint64_t destination_finger_value, 1756find_successor (uint64_t destination_finger_value,
1553 struct GNUNET_PeerIdentity *next_destination, 1757 struct GNUNET_PeerIdentity *best_known_destination,
1554 struct GNUNET_HashCode *new_intermediate_trail_id, 1758 struct GNUNET_HashCode *new_intermediate_trail_id,
1555 unsigned int finger_map_index) 1759 unsigned int finger_map_index)
1556{ 1760{
1557 struct Closest_Peer *successor; 1761 struct Closest_Peer *current_successor;
1558 struct GNUNET_PeerIdentity *next_hop; 1762 struct GNUNET_PeerIdentity *next_hop;
1559 struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter; 1763 struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter;
1560 struct GNUNET_CONTAINER_MultiHashMap32Iterator *finger_iter; 1764 struct GNUNET_CONTAINER_MultiHashMap32Iterator *finger_iter;
1561 struct GNUNET_PeerIdentity *closest_peer; 1765 struct Selected_Finger_Trail *finger_trail;
1562 struct Correct_Trail *finger_trail;
1563 struct FriendInfo *friend; 1766 struct FriendInfo *friend;
1564 struct FingerInfo *finger; 1767 struct FingerInfo *finger;
1565 int i; 1768 int i;
1566 1769
1567 successor = GNUNET_new (struct Closest_Peer); 1770 /* Initialize current_successor to my_identity. */
1568 memcpy (&successor->value, &my_identity, sizeof (uint64_t)); 1771 current_successor = GNUNET_new (struct Closest_Peer);
1569 //successor->trail_id = 0; /* FIXME:Default value for trail id */ 1772 memcpy (&current_successor->value, &my_identity, sizeof (uint64_t));
1570 successor->next_hop = my_identity; 1773 memset (&current_successor->trail_id, 0, sizeof (current_successor->trail_id));
1571 successor->next_destination = my_identity; 1774 current_successor->next_hop = my_identity;
1572 1775 current_successor->best_known_destination = my_identity;
1776
1777 /* Iterate over friend_peermap and compare each friend with current_successor.*/
1573 friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap); 1778 friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
1574 for (i= 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++) 1779 for (i= 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
1575 { 1780 {
1576 GNUNET_assert (GNUNET_YES == 1781 GNUNET_assert (GNUNET_YES ==
1577 GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter, NULL, 1782 GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter, NULL,
1578 (const void **)&friend)); 1783 (const void **)&friend));
1579 if ((friend->trails_count > TRAILS_THROUGH_FRIEND_THRESHOLD)|| 1784
1580 (0 != GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us)) 1785 if (GNUNET_YES == is_friend_congested (friend))
1581 continue; 1786 continue;
1582 1787
1583 closest_peer = select_closest_peer (&my_identity, &friend->id, 1788 /* FIXME: select closest peer w.r.t. value. [friend_id, current_successor->id)
1584 destination_finger_value, 1789 and [current_successor->id, friend_id). Check in which range value lies.
1585 finger_map_index); 1790 Also, check for wrap around. Set the value of current_successor accordingly.*/
1586 if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, &friend->id))
1587 {
1588 memcpy (&successor->value, &friend->id, sizeof (uint64_t));
1589 //successor->trail_id = 0;
1590 successor->next_hop = friend->id;
1591 successor->next_destination = friend->id;
1592 }
1593 } 1791 }
1594 1792
1793 /* Iterate over finger_hashmap and compare each finger with current_successor.*/
1595 finger_iter = GNUNET_CONTAINER_multihashmap32_iterator_create (finger_hashmap); 1794 finger_iter = GNUNET_CONTAINER_multihashmap32_iterator_create (finger_hashmap);
1596 for (i = 0; i < GNUNET_CONTAINER_multihashmap32_size (finger_hashmap); i++) 1795 for (i = 0; i < GNUNET_CONTAINER_multihashmap32_size (finger_hashmap); i++)
1597 { 1796 {
1598 GNUNET_assert (GNUNET_YES == 1797 GNUNET_assert (GNUNET_YES ==
1599 GNUNET_CONTAINER_multihashmap32_iterator_next (finger_iter, NULL, 1798 GNUNET_CONTAINER_multihashmap32_iterator_next (finger_iter, NULL,
1600 (void *)&finger)); 1799 (void *)&finger));
1601 finger_trail = select_trail_to_finger (finger); 1800
1602 if (NULL == finger_trail) 1801 /* If I am my own finger, then ignore this finger. */
1603 continue; 1802 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
1604 1803 &my_identity))
1605 closest_peer = select_closest_peer (&my_identity, 1804 continue;
1606 &finger->finger_identity, 1805
1607 destination_finger_value, 1806 /* If finger is friend. */
1608 finger_map_index); 1807 if(GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger->finger_identity))
1609 if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, 1808 {
1610 &finger->finger_identity)) 1809 /* If not congested then compare it with current_successor. */
1611 { 1810 if (GNUNET_NO == is_friend_congested (friend))
1612 memcpy (&successor->value, &finger->finger_identity, sizeof (uint64_t)); 1811 {
1613 successor->trail_id = finger_trail->trail_id; 1812 //compare it with current successor.
1614 successor->next_hop = finger_trail->friend.id; 1813 }
1615 successor->next_destination = finger->finger_identity;
1616 } 1814 }
1815 /* Choose one of the trail to reach to finger. */
1816 finger_trail = select_finger_trail (finger);
1817
1818 /* In case no trail found, ignore this finger. */
1819 if (NULL == finger_trail)
1820 continue;
1821
1822 /* FIXME: select closest peer w.r.t. value. [finger->friend_id, current_successor->id)
1823 and [current_successor->id, finger->friend_id). Check in which range value lies.
1824 Also, check for wrap around. But this will give you the immediate predecessor
1825 For example. if we have 0,1,6 and I am 0 and one of my finger is 6. Then
1826 for 1, we will have ranges like [0,6) and [6,0) 1 lies in range from [0,6)
1827 but successor is 6 not 0 as 6 is > than 1. If you are the closest one,
1828 then set the values
1829 in current_successor. Want to write a generic code so that it is used in
1830 * finger_table_add also while choosing the closest one among new and existing
1831 * one. */
1617 } 1832 }
1618 1833
1619 next_destination = &successor->next_destination; 1834 best_known_destination = &current_successor->best_known_destination;
1620 new_intermediate_trail_id = &successor->trail_id; 1835 new_intermediate_trail_id = &current_successor->trail_id;
1621 next_hop = &successor->next_hop; 1836 next_hop = &current_successor->next_hop;
1622 1837
1623 return next_hop; 1838 return next_hop;
1624} 1839}
1625 1840
1841
1626/** 1842/**
1627 * Construct a Put message and send it to target_peer. 1843 * Construct a Put message and send it to target_peer.
1628 * @param key Key for the content 1844 * @param key Key for the content
1629 * @param block_type Type of the block 1845 * @param block_type Type of the block
1630 * @param options Routing options 1846 * @param options Routing options
1631 * @param desired_replication_level Desired replication count 1847 * @param desired_replication_level Desired replication count
1632 * @param current_destination Next current destination which will get this message. 1848 * @param best_known_dest Peer to which this message should reach eventually,
1633 * @param current_source Source for @a current_destination 1849 * as it is best known destination to me.
1850 * @param intermediate_trail_id Trail id in case
1634 * @param target_peer Peer to which this message will be forwarded. 1851 * @param target_peer Peer to which this message will be forwarded.
1635 * @param hop_count Number of hops traversed so far. 1852 * @param hop_count Number of hops traversed so far.
1636 * @param put_path_length Total number of peers in @a put_path 1853 * @param put_path_length Total number of peers in @a put_path
@@ -1642,28 +1859,97 @@ find_successor (uint64_t destination_finger_value,
1642void 1859void
1643GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key, 1860GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
1644 enum GNUNET_BLOCK_Type block_type, 1861 enum GNUNET_BLOCK_Type block_type,
1645 enum GNUNET_DHT_RouteOption options, 1862 enum GNUNET_DHT_RouteOption options,
1646 uint32_t desired_replication_level, 1863 uint32_t desired_replication_level,
1647 struct GNUNET_PeerIdentity current_destination, 1864 struct GNUNET_PeerIdentity *best_known_dest,
1648 struct GNUNET_PeerIdentity current_source, 1865 struct GNUNET_HashCode *intermediate_trail_id,
1649 struct GNUNET_PeerIdentity *target_peer, 1866 struct GNUNET_PeerIdentity *target_peer,
1650 uint32_t hop_count, 1867 uint32_t hop_count,
1651 uint32_t put_path_length, 1868 uint32_t put_path_length,
1652 struct GNUNET_PeerIdentity *put_path, 1869 struct GNUNET_PeerIdentity *put_path,
1653 struct GNUNET_TIME_Absolute expiration_time, 1870 struct GNUNET_TIME_Absolute expiration_time,
1654 const void *data, size_t data_size) 1871 const void *data, size_t data_size)
1655{ 1872{
1873 struct PeerPutMessage *ppm;
1874 struct P2PPendingMessage *pending;
1875 struct FriendInfo *target_friend;
1876 struct GNUNET_PeerIdentity *pp;
1877 size_t msize;
1656 1878
1879 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
1880 sizeof (struct PeerPutMessage);
1881
1882 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1883 {
1884 put_path_length = 0;
1885 msize = data_size + sizeof (struct PeerPutMessage);
1886 }
1887
1888 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1889 {
1890 GNUNET_break (0);
1891 return;
1892 }
1893
1894 /* This is the first call made from clients file. So, we should search for the
1895 target_friend. */
1896 if (NULL == target_peer)
1897 {
1898 uint64_t key_value;
1899 struct GNUNET_PeerIdentity *next_hop;
1900
1901 memcpy (&key_value, key, sizeof (uint64_t));
1902 next_hop = find_successor (key_value, best_known_dest,
1903 intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
1904 if (0 == GNUNET_CRYPTO_cmp_peer_identity(next_hop, &my_identity))
1905 {
1906 /* I am the destination but we have already done datacache_put in client file. */
1907 return;
1908 }
1909 else
1910 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
1911 }
1912
1913 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1914 pending->timeout = expiration_time;
1915 ppm = (struct PeerPutMessage *) &pending[1];
1916 pending->msg = &ppm->header;
1917 ppm->header.size = htons (msize);
1918 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1919 ppm->options = htonl (options);
1920 ppm->block_type = htonl (block_type);
1921 ppm->hop_count = htonl (hop_count + 1);
1922 ppm->desired_replication_level = htonl (desired_replication_level);
1923 ppm->put_path_length = htonl (put_path_length);
1924 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1925 ppm->best_known_destination = *best_known_dest;
1926 ppm->key = *key;
1927 if (NULL == intermediate_trail_id)
1928 memset (&ppm->intermediate_trail_id, 0, sizeof (ppm->intermediate_trail_id));
1929 else
1930 ppm->intermediate_trail_id = *intermediate_trail_id;
1931 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
1932 if (put_path_length != 0)
1933 {
1934 memcpy (pp, put_path,
1935 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1936 }
1937 memcpy (&pp[put_path_length], data, data_size);
1938 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1939 target_friend->pending_count++;
1940 process_friend_queue (target_friend);
1657} 1941}
1658 1942
1943
1944
1659/** 1945/**
1660 * Construct a Get message and send it to target_peer. 1946 * Construct a Get message and send it to target_peer.
1661 * @param key Key for the content 1947 * @param key Key for the content
1662 * @param block_type Type of the block 1948 * @param block_type Type of the block
1663 * @param options Routing options 1949 * @param options Routing options
1664 * @param desired_replication_level Desired replication count 1950 * @param desired_replication_level Desired replication count
1665 * @param current_destination Next current destination which will get this message. 1951 * @param best_known_dest
1666 * @param current_source Source for @a current_destination 1952 * @param intermediate_trail_id
1667 * @param target_peer Peer to which this message will be forwarded. 1953 * @param target_peer Peer to which this message will be forwarded.
1668 * @param hop_count Number of hops traversed so far. 1954 * @param hop_count Number of hops traversed so far.
1669 * @param data Content to store 1955 * @param data Content to store
@@ -1676,14 +1962,71 @@ GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
1676 enum GNUNET_BLOCK_Type block_type, 1962 enum GNUNET_BLOCK_Type block_type,
1677 enum GNUNET_DHT_RouteOption options, 1963 enum GNUNET_DHT_RouteOption options,
1678 uint32_t desired_replication_level, 1964 uint32_t desired_replication_level,
1679 struct GNUNET_PeerIdentity current_destination, 1965 struct GNUNET_PeerIdentity *best_known_dest,
1680 struct GNUNET_PeerIdentity current_source, 1966 struct GNUNET_HashCode *intermediate_trail_id,
1681 struct GNUNET_PeerIdentity *target_peer, 1967 struct GNUNET_PeerIdentity *target_peer,
1682 uint32_t hop_count, 1968 uint32_t hop_count,
1683 uint32_t get_path_length, 1969 uint32_t get_path_length,
1684 struct GNUNET_PeerIdentity *get_path) 1970 struct GNUNET_PeerIdentity *get_path)
1685{ 1971{
1972 struct PeerGetMessage *pgm;
1973 struct P2PPendingMessage *pending;
1974 struct FriendInfo *target_friend;
1975 struct GNUNET_PeerIdentity *gp;
1976 size_t msize;
1686 1977
1978 msize = sizeof (struct PeerGetMessage) +
1979 (get_path_length * sizeof (struct GNUNET_PeerIdentity));
1980
1981 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1982 {
1983 GNUNET_break (0);
1984 return;
1985 }
1986
1987 if (NULL == target_peer)
1988 {
1989 struct GNUNET_PeerIdentity *next_hop;
1990 uint64_t key_value;
1991
1992 memcpy (&key_value, key, sizeof (uint64_t));
1993 // FIXME: endianess of key_value!?
1994 /* FIXME: Here you should use enum GDS_NEIGHBOURS_FINGER_TYPE in place of 0. */
1995 next_hop = find_successor (key_value, best_known_dest,
1996 intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
1997
1998 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity,next_hop))
1999 {
2000 GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
2001 NULL, 0, 1, &my_identity, NULL,&my_identity);
2002 return;
2003 }
2004 else
2005 {
2006 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
2007 }
2008 }
2009
2010 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2011 pending->importance = 0; /* FIXME */
2012 pgm = (struct PeerGetMessage *) &pending[1];
2013 pending->msg = &pgm->header;
2014 pgm->header.size = htons (msize);
2015 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
2016 pgm->get_path_length = htonl (get_path_length);
2017 pgm->key = *key;
2018 pgm->best_known_destination = *best_known_dest;
2019 pgm->intermediate_trail_id = *intermediate_trail_id;
2020 pgm->hop_count = htonl (hop_count + 1);
2021
2022 if (get_path != 0)
2023 {
2024 gp = (struct GNUNET_PeerIdentity *) &pgm[1];
2025 memcpy (gp, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
2026 }
2027 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2028 target_friend->pending_count++;
2029 process_friend_queue (target_friend);
1687} 2030}
1688 2031
1689 2032
@@ -1713,7 +2056,72 @@ GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
1713 struct GNUNET_TIME_Absolute expiration, 2056 struct GNUNET_TIME_Absolute expiration,
1714 const void *data, size_t data_size) 2057 const void *data, size_t data_size)
1715{ 2058{
1716 2059 struct PeerGetResultMessage *get_result;
2060 struct GNUNET_PeerIdentity *get_result_path;
2061 struct GNUNET_PeerIdentity *pp;
2062 struct P2PPendingMessage *pending;
2063 struct FriendInfo *target_friend;
2064 int current_path_index;
2065 size_t msize;
2066
2067 msize = get_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
2068 sizeof (struct PeerPutMessage);
2069
2070 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2071 {
2072 GNUNET_break (0);
2073 return;
2074 }
2075
2076 if(get_path_length > 0)
2077 {
2078 current_path_index = search_my_index(get_path, get_path_length);
2079 if (GNUNET_SYSERR == current_path_index)
2080 {
2081 GNUNET_break (0);
2082 return;
2083 }
2084 }
2085 if (0 == current_path_index)
2086 {
2087 GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
2088 get_path, put_path_length,
2089 put_path, type, data_size, data);
2090 return;
2091 }
2092
2093 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2094 pending->importance = 0;
2095 get_result = (struct PeerGetResultMessage *)&pending[1];
2096 pending->msg = &get_result->header;
2097 get_result->header.size = htons (msize);
2098 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT);
2099 get_result->key = *key;
2100 /* FIXME: check if you are passing the correct querying_peer as described in
2101 the get_result documentation. */
2102 memcpy (&(get_result->querying_peer), source_peer, sizeof (struct GNUNET_PeerIdentity));
2103 get_result->expiration_time = expiration;
2104
2105 if (get_path_length != 0)
2106 {
2107 get_result_path = (struct GNUNET_PeerIdentity *)&get_result[1];
2108 memcpy (get_result_path, get_path,
2109 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
2110 }
2111 memcpy (&get_result_path[get_path_length], data, data_size);
2112
2113 /* FIXME: Is this correct? */
2114 if (put_path_length != 0)
2115 {
2116 pp = (struct GNUNET_PeerIdentity *)&get_result_path[1];
2117 memcpy (pp, put_path,sizeof (struct GNUNET_PeerIdentity) * put_path_length);
2118 }
2119
2120 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2121 &get_result_path[current_path_index - 1]);
2122 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2123 target_friend->pending_count++;
2124 process_friend_queue (target_friend);
1717} 2125}
1718 2126
1719 2127
@@ -1759,7 +2167,7 @@ select_random_friend ()
1759 (const void **)&friend)); 2167 (const void **)&friend));
1760 2168
1761 2169
1762 if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) && 2170 if ((TRAILS_THROUGH_FRIEND_THRESHOLD == friend->trails_count) &&
1763 (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us)) 2171 (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us))
1764 { 2172 {
1765 break; 2173 break;
@@ -1779,7 +2187,7 @@ select_random_friend ()
1779 * @return finger_identity 2187 * @return finger_identity
1780 */ 2188 */
1781static uint64_t 2189static uint64_t
1782compute_finger_identity() 2190compute_finger_identity_value ()
1783{ 2191{
1784 uint64_t my_id64; 2192 uint64_t my_id64;
1785 2193
@@ -1794,7 +2202,7 @@ compute_finger_identity()
1794 * @return peer identity of immediate predecessor. 2202 * @return peer identity of immediate predecessor.
1795 */ 2203 */
1796static uint64_t 2204static uint64_t
1797compute_predecessor_identity() 2205compute_predecessor_identity_value ()
1798{ 2206{
1799 uint64_t my_id64; 2207 uint64_t my_id64;
1800 2208
@@ -1818,8 +2226,8 @@ send_find_finger_trail_message (void *cls,
1818 struct FriendInfo *target_friend; 2226 struct FriendInfo *target_friend;
1819 struct GNUNET_TIME_Relative next_send_time; 2227 struct GNUNET_TIME_Relative next_send_time;
1820 struct GNUNET_HashCode trail_id; 2228 struct GNUNET_HashCode trail_id;
1821 unsigned int finger_map_index; 2229 unsigned int is_predecessor;
1822 uint64_t finger_identity; 2230 uint64_t finger_id_value;
1823 2231
1824 next_send_time.rel_value_us = 2232 next_send_time.rel_value_us =
1825 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us + 2233 DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
@@ -1837,20 +2245,20 @@ send_find_finger_trail_message (void *cls,
1837 2245
1838 if (PREDECESSOR_FINGER_ID == current_search_finger_index) 2246 if (PREDECESSOR_FINGER_ID == current_search_finger_index)
1839 { 2247 {
1840 finger_identity = compute_predecessor_identity(); 2248 finger_id_value = compute_predecessor_identity_value();
2249 is_predecessor = 0;
1841 } 2250 }
1842 else 2251 else
1843 { 2252 {
1844 finger_identity = compute_finger_identity(); 2253 finger_id_value = compute_finger_identity_value();
2254 is_predecessor = 1;
1845 } 2255 }
1846 2256
1847 finger_map_index = current_search_finger_index;
1848
1849 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG, 2257 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
1850 &trail_id, sizeof (trail_id)); 2258 &trail_id, sizeof (trail_id));
1851 GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_identity, 2259 GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_id_value,
1852 target_friend->id, target_friend, 0, NULL, 2260 target_friend->id, target_friend, 0, NULL,
1853 finger_map_index, trail_id, NULL); 2261 is_predecessor, trail_id, NULL);
1854} 2262}
1855 2263
1856 2264
@@ -1873,14 +2281,14 @@ send_find_finger_trail_message (void *cls,
1873 */ 2281 */
1874static void 2282static void
1875select_and_replace_trail (struct FingerInfo *existing_finger, 2283select_and_replace_trail (struct FingerInfo *existing_finger,
1876 struct GNUNET_PeerIdentity *new_trail, 2284 const struct GNUNET_PeerIdentity *new_trail,
1877 unsigned int new_trail_length, 2285 unsigned int new_trail_length,
1878 struct GNUNET_HashCode new_trail_id) 2286 struct GNUNET_HashCode new_trail_id)
1879{ 2287{
1880 struct TrailList *trail_list_iterator; 2288 struct Trail *trail_list_iterator;
1881 unsigned int largest_trail_length; 2289 unsigned int largest_trail_length;
1882 unsigned int largest_trail_index; 2290 unsigned int largest_trail_index;
1883 struct Trail *trail_element; 2291 struct Trail_Element *trail_element;
1884 unsigned int i; 2292 unsigned int i;
1885 2293
1886 largest_trail_length = new_trail_length; 2294 largest_trail_length = new_trail_length;
@@ -1905,7 +2313,7 @@ select_and_replace_trail (struct FingerInfo *existing_finger,
1905 } 2313 }
1906 2314
1907 /* Send trail teardown message across the replaced trail. */ 2315 /* Send trail teardown message across the replaced trail. */
1908 struct TrailList *replace_trail = &existing_finger->trail_list[largest_trail_index]; 2316 struct Trail *replace_trail = &existing_finger->trail_list[largest_trail_index];
1909 struct FriendInfo *target_friend = 2317 struct FriendInfo *target_friend =
1910 GNUNET_CONTAINER_multipeermap_get (friend_peermap, 2318 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1911 &replace_trail->trail_head->peer); 2319 &replace_trail->trail_head->peer);
@@ -1926,7 +2334,7 @@ select_and_replace_trail (struct FingerInfo *existing_finger,
1926 i = 0; 2334 i = 0;
1927 while (i < new_trail_length) 2335 while (i < new_trail_length)
1928 { 2336 {
1929 struct Trail *element = GNUNET_new (struct Trail); 2337 struct Trail_Element *element = GNUNET_new (struct Trail_Element);
1930 element->peer = new_trail[i]; 2338 element->peer = new_trail[i];
1931 2339
1932 GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head, 2340 GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
@@ -1947,11 +2355,11 @@ select_and_replace_trail (struct FingerInfo *existing_finger,
1947 */ 2355 */
1948static int 2356static int
1949is_new_trail_unique (struct FingerInfo *existing_finger, 2357is_new_trail_unique (struct FingerInfo *existing_finger,
1950 struct GNUNET_PeerIdentity *new_trail, 2358 const struct GNUNET_PeerIdentity *new_trail,
1951 unsigned int trail_length) 2359 unsigned int trail_length)
1952{ 2360{
1953 struct TrailList *trail_list_iterator; 2361 struct Trail *trail_list_iterator;
1954 struct Trail *trail_element; 2362 struct Trail_Element *trail_element;
1955 int i; 2363 int i;
1956 int j; 2364 int j;
1957 int trail_unique = GNUNET_NO; 2365 int trail_unique = GNUNET_NO;
@@ -1985,11 +2393,11 @@ is_new_trail_unique (struct FingerInfo *existing_finger,
1985 */ 2393 */
1986static void 2394static void
1987add_new_trail (struct FingerInfo *existing_finger, 2395add_new_trail (struct FingerInfo *existing_finger,
1988 struct GNUNET_PeerIdentity *new_trail, 2396 const struct GNUNET_PeerIdentity *new_trail,
1989 unsigned int new_trail_length, 2397 unsigned int new_trail_length,
1990 struct GNUNET_HashCode new_trail_id) 2398 struct GNUNET_HashCode new_trail_id)
1991{ 2399{
1992 struct TrailList *trail_list_iterator; 2400 struct Trail *trail_list_iterator;
1993 struct FriendInfo *first_friend; 2401 struct FriendInfo *first_friend;
1994 int i; 2402 int i;
1995 2403
@@ -2012,13 +2420,14 @@ add_new_trail (struct FingerInfo *existing_finger,
2012 first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 2420 first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2013 &(existing_finger->finger_identity)); 2421 &(existing_finger->finger_identity));
2014 first_friend->trails_count++; 2422 first_friend->trails_count++;
2015 trail_list_iterator->first_friend_trail_count = first_friend->trails_count; 2423 /* FIXME; we removed this field but read fixme. */
2424 //trail_list_iterator->first_friend_trail_count = first_friend->trails_count;
2016 trail_list_iterator->trail_length = new_trail_length; 2425 trail_list_iterator->trail_length = new_trail_length;
2017 2426
2018 for (i = 0; i < new_trail_length; i++) 2427 for (i = 0; i < new_trail_length; i++)
2019 { 2428 {
2020 struct Trail *element; 2429 struct Trail_Element *element;
2021 element = GNUNET_new (struct Trail); 2430 element = GNUNET_new (struct Trail_Element);
2022 2431
2023 element->peer = new_trail[i]; 2432 element->peer = new_trail[i];
2024 GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head, 2433 GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head,
@@ -2036,7 +2445,7 @@ add_new_trail (struct FingerInfo *existing_finger,
2036static void 2445static void
2037send_trail_teardown (struct FingerInfo *finger) 2446send_trail_teardown (struct FingerInfo *finger)
2038{ 2447{
2039 struct TrailList *trail_list_iterator; 2448 struct Trail *trail_list_iterator;
2040 struct FriendInfo *target_friend; 2449 struct FriendInfo *target_friend;
2041 int i; 2450 int i;
2042 2451
@@ -2071,7 +2480,7 @@ send_trail_teardown (struct FingerInfo *finger)
2071static void 2480static void
2072decrement_friend_trail_count (struct FingerInfo *finger) 2481decrement_friend_trail_count (struct FingerInfo *finger)
2073{ 2482{
2074 struct TrailList *trail_list_iterator; 2483 struct Trail *trail_list_iterator;
2075 struct FriendInfo *target_friend; 2484 struct FriendInfo *target_friend;
2076 int i = 0; 2485 int i = 0;
2077 2486
@@ -2092,8 +2501,9 @@ decrement_friend_trail_count (struct FingerInfo *finger)
2092 &finger->finger_identity); 2501 &finger->finger_identity);
2093 2502
2094 // check target_friend for NULL 2503 // check target_friend for NULL
2095 target_friend->trails_count--; 2504 /* FIXME: we have removed first_friend_trail_count field. */
2096 trail_list_iterator->first_friend_trail_count--; 2505 target_friend->trails_count--;
2506 //trail_list_iterator->first_friend_trail_count--;
2097 } 2507 }
2098 return; 2508 return;
2099} 2509}
@@ -2106,8 +2516,8 @@ decrement_friend_trail_count (struct FingerInfo *finger)
2106static void 2516static void
2107free_finger (struct FingerInfo *finger) 2517free_finger (struct FingerInfo *finger)
2108{ 2518{
2109 struct TrailList *trail_list_iterator; 2519 struct Trail *trail_list_iterator;
2110 struct Trail *trail_element; 2520 struct Trail_Element *trail_element;
2111 unsigned int i; 2521 unsigned int i;
2112 2522
2113 for (i = 0; i < finger->trails_count; i++) 2523 for (i = 0; i < finger->trails_count; i++)
@@ -2126,85 +2536,6 @@ free_finger (struct FingerInfo *finger)
2126 2536
2127 2537
2128/** 2538/**
2129 * FIXME: merge into finger_table_add
2130 * FIXME: leave as simple a check
2131 * Check if new finger is closer than existing_finger. If both new finger and
2132 * existing finger are same then we may add a new trail (if there is space)
2133 * or choose the best trail among existing trails and new trails.
2134 * @param existing_finger Finger present in finger_peermap at @a finger_map_index
2135 * or NULL if none
2136 * @param new_finger_identity Peer identity of new finger.
2137 * FIXME: all the following params *should* not be necessary
2138 * @param new_finger_trail Trail to reach from source to new_finger.
2139 * @param new_finger_trail_length Total number of peers in @a new_finger_trail.
2140 * @param trail_id Unique identifier of trail.
2141 * @param finger_map_index Index in finger map.
2142 * @return #GNUNET_YES if the new finger is closest.
2143 * #GNUNET_NO either new_finger and existing_finger are same, or
2144 * existing_finger is closest.
2145 */
2146static int
2147is_new_finger_closest (struct FingerInfo *existing_finger,
2148 struct GNUNET_PeerIdentity new_finger_identity,
2149 struct GNUNET_PeerIdentity *new_finger_trail,
2150 unsigned int new_finger_trail_length,
2151 struct GNUNET_HashCode new_finger_trail_id,
2152 unsigned int finger_map_index)
2153{
2154 struct GNUNET_PeerIdentity *closest_peer;
2155 uint64_t finger_identity_value;
2156 uint64_t my_id64;
2157
2158 if (NULL == existing_finger)
2159 return GNUNET_YES;
2160
2161 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
2162 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
2163 &new_finger_identity))
2164 {
2165 if (PREDECESSOR_FINGER_ID == finger_map_index)
2166 finger_identity_value = my_id64 - 1;
2167 else
2168 finger_identity_value = my_id64 + (unsigned long) pow (2, finger_map_index);
2169 closest_peer = select_closest_peer (&existing_finger->finger_identity,
2170 &new_finger_identity,
2171 finger_identity_value, finger_map_index);
2172
2173 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity, closest_peer))
2174 {
2175 GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2176 &new_finger_identity));
2177
2178 send_trail_teardown (existing_finger);
2179 decrement_friend_trail_count (existing_finger);
2180 free_finger (existing_finger);
2181 return GNUNET_YES;
2182 }
2183 }
2184 else
2185 {
2186 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
2187 &my_identity))
2188 {
2189 return GNUNET_NO;
2190 }
2191 if (NULL ==
2192 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2193 &(existing_finger->finger_identity)))
2194 {
2195 if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
2196 add_new_trail (existing_finger, new_finger_trail,
2197 new_finger_trail_length, new_finger_trail_id);
2198 else
2199 select_and_replace_trail (existing_finger, new_finger_trail,
2200 new_finger_trail_length, new_finger_trail_id);
2201 }
2202 }
2203 return GNUNET_NO;
2204}
2205
2206
2207/**
2208 * Add a new entry in finger hashmap at finger_map_index 2539 * Add a new entry in finger hashmap at finger_map_index
2209 * @param finger_identity Peer Identity of new finger 2540 * @param finger_identity Peer Identity of new finger
2210 * @param finger_trail Trail to reach from me to finger (excluding both end points). 2541 * @param finger_trail Trail to reach from me to finger (excluding both end points).
@@ -2217,14 +2548,14 @@ is_new_finger_closest (struct FingerInfo *existing_finger,
2217 */ 2548 */
2218static int 2549static int
2219add_new_entry (struct GNUNET_PeerIdentity finger_identity, 2550add_new_entry (struct GNUNET_PeerIdentity finger_identity,
2220 struct GNUNET_PeerIdentity *finger_trail, 2551 const struct GNUNET_PeerIdentity *finger_trail,
2221 unsigned int finger_trail_length, 2552 unsigned int finger_trail_length,
2222 struct GNUNET_HashCode trail_id, 2553 struct GNUNET_HashCode trail_id,
2223 unsigned int finger_map_index) 2554 unsigned int finger_map_index)
2224{ 2555{
2225 struct FingerInfo *new_entry; 2556 struct FingerInfo *new_entry;
2226 struct FriendInfo *first_trail_hop; 2557 struct FriendInfo *first_trail_hop;
2227 struct TrailList *first_trail; 2558 struct Trail *first_trail;
2228 int i = 0; 2559 int i = 0;
2229 2560
2230 new_entry = GNUNET_new (struct FingerInfo); 2561 new_entry = GNUNET_new (struct FingerInfo);
@@ -2247,11 +2578,12 @@ add_new_entry (struct GNUNET_PeerIdentity finger_identity,
2247 2578
2248 first_trail_hop->trails_count++; 2579 first_trail_hop->trails_count++;
2249 first_trail = &new_entry->trail_list[0]; 2580 first_trail = &new_entry->trail_list[0];
2250 first_trail->first_friend_trail_count = first_trail_hop->trails_count; 2581 /* FIXME: We have removed this field. */
2582 //first_trail->first_friend_trail_count = first_trail_hop->trails_count;
2251 2583
2252 while (i < finger_trail_length) 2584 while (i < finger_trail_length)
2253 { 2585 {
2254 struct Trail *element = GNUNET_new (struct Trail); 2586 struct Trail_Element *element = GNUNET_new (struct Trail_Element);
2255 2587
2256 element->next = NULL; 2588 element->next = NULL;
2257 element->prev = NULL; 2589 element->prev = NULL;
@@ -2282,18 +2614,21 @@ add_new_entry (struct GNUNET_PeerIdentity finger_identity,
2282 * @return updated trail length in case we shortcut the trail, else original 2614 * @return updated trail length in case we shortcut the trail, else original
2283 * trail length. 2615 * trail length.
2284 */ 2616 */
2285static int 2617static struct GNUNET_PeerIdentity *
2286scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity, 2618scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
2287 struct GNUNET_PeerIdentity *trail, 2619 const struct GNUNET_PeerIdentity *trail,
2288 unsigned int trail_length, 2620 unsigned int trail_length,
2289 struct GNUNET_HashCode trail_id) 2621 struct GNUNET_HashCode trail_id,
2622 int *new_trail_length)
2290{ 2623{
2291 struct FriendInfo *target_friend; 2624 struct FriendInfo *target_friend;
2625 struct GNUNET_PeerIdentity *new_trail;
2292 int i; 2626 int i;
2293 2627
2294 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity)) 2628 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
2295 { 2629 {
2296 return 0; 2630 *new_trail_length = 0;
2631 return NULL;
2297 } 2632 }
2298 2633
2299 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger_identity)) 2634 if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger_identity))
@@ -2303,11 +2638,11 @@ scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
2303 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 2638 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2304 &trail[0]); 2639 &trail[0]);
2305 GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity, 2640 GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity,
2306 trail_id, finger_identity, 2641 trail_id, finger_identity,
2307 target_friend); 2642 target_friend);
2308 trail = NULL; 2643 *new_trail_length = 0;
2309 } 2644 }
2310 return 0; 2645 return NULL;
2311 } 2646 }
2312 2647
2313 for (i = trail_length - 1; i > 0; i--) 2648 for (i = trail_length - 1; i > 0; i--)
@@ -2323,26 +2658,30 @@ scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
2323 trail_id, trail[i], 2658 trail_id, trail[i],
2324 target_friend); 2659 target_friend);
2325 2660
2326 // FIXME WARNING WARNING WARNING rewrite!!! consider directly creating a 2661
2327 // struct TrailList (current) // struct Trial (after renaming).
2328
2329 /* Copy the trail from index i to index trail_length -1 and change 2662 /* Copy the trail from index i to index trail_length -1 and change
2330 trail length and return */ 2663 trail length and return */
2664 new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * i);
2331 while (i < trail_length) 2665 while (i < trail_length)
2332 { 2666 {
2333 memcpy (&trail[j], &trail[i], sizeof(struct GNUNET_PeerIdentity)); 2667 memcpy (&new_trail[j], &trail[i], sizeof(struct GNUNET_PeerIdentity));
2334 j++; 2668 j++;
2335 i++; 2669 i++;
2336 } 2670 }
2337 trail_length = j+1; 2671 *new_trail_length = j+1;
2338 break; 2672 break;
2673 return new_trail;
2339 } 2674 }
2340 } 2675 }
2341 return trail_length; 2676 *new_trail_length = trail_length;
2677 memcpy (new_trail, new_trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
2678 return new_trail;
2342} 2679}
2343 2680
2344 2681
2345/** 2682/**
2683 * FIXME: Ensure that we add trail in succession in the trail list.
2684 * There are no free spots within the trail list.
2346 * Send verify successor message to your successor on all trails to reach 2685 * Send verify successor message to your successor on all trails to reach
2347 * the successor. 2686 * the successor.
2348 * @param successor My current successor 2687 * @param successor My current successor
@@ -2350,7 +2689,7 @@ scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
2350static void 2689static void
2351send_verify_successor_message (struct FingerInfo *successor) 2690send_verify_successor_message (struct FingerInfo *successor)
2352{ 2691{
2353 struct TrailList *trail_list_iterator; 2692 struct Trail *trail_list_iterator;
2354 struct GNUNET_HashCode trail_id; 2693 struct GNUNET_HashCode trail_id;
2355 struct GNUNET_PeerIdentity next_hop; 2694 struct GNUNET_PeerIdentity next_hop;
2356 struct FriendInfo *target_friend; 2695 struct FriendInfo *target_friend;
@@ -2359,17 +2698,14 @@ send_verify_successor_message (struct FingerInfo *successor)
2359 int i; 2698 int i;
2360 int j; 2699 int j;
2361 2700
2362 for (i = 0; i < MAXIMUM_TRAILS_PER_FINGER; i++) 2701 for (i = 0; i < successor->trails_count; i++)
2363 { 2702 {
2364 trail_list_iterator = &successor->trail_list[i]; 2703 trail_list_iterator = &successor->trail_list[i];
2365 2704 GNUNET_assert (NULL != trail_list_iterator->trail_head);
2366// FIXME check if this entry in the trail list is valid!
2367// if (NULL == trail_list_iterator->SOMETHING)
2368// continue;
2369 2705
2370 if (trail_list_iterator->trail_length > 0) 2706 if (trail_list_iterator->trail_length > 0)
2371 { 2707 {
2372 struct Trail *element; 2708 struct Trail_Element *element;
2373 2709
2374 trail_length = trail_list_iterator->trail_length; 2710 trail_length = trail_list_iterator->trail_length;
2375 trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) 2711 trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)
@@ -2386,8 +2722,9 @@ send_verify_successor_message (struct FingerInfo *successor)
2386 next_hop = successor->finger_identity; 2722 next_hop = successor->finger_identity;
2387 } 2723 }
2388 trail_id = trail_list_iterator->trail_id; 2724 trail_id = trail_list_iterator->trail_id;
2389 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop); 2725 GNUNET_assert (NULL != (target_friend =
2390 // check friend for NULL 2726 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
2727
2391 GDS_NEIGHBOURS_send_verify_successor_message (my_identity, 2728 GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
2392 successor->finger_identity, 2729 successor->finger_identity,
2393 trail_id, trail, trail_length, 2730 trail_id, trail, trail_length,
@@ -2398,6 +2735,73 @@ send_verify_successor_message (struct FingerInfo *successor)
2398 2735
2399 2736
2400/** 2737/**
2738 * FIXME: Is it safe to assume that current_search_finger_index == finger_map_index?
2739 * Update the current search finger index.
2740 */
2741static void
2742update_current_search_finger_index (struct GNUNET_PeerIdentity new_finger_identity)
2743{
2744 struct FingerInfo *successor;
2745
2746 successor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 0);
2747
2748 if (0 == current_search_finger_index)
2749 {
2750 current_search_finger_index = PREDECESSOR_FINGER_ID;
2751
2752 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_finger_identity))
2753 {
2754 send_verify_successor_message (successor);
2755 }
2756 }
2757 else if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity,
2758 &(successor->finger_identity)))
2759 {
2760 current_search_finger_index = 0;
2761 }
2762 else
2763 current_search_finger_index = current_search_finger_index - 1;
2764}
2765
2766/**
2767 * FIXME: Is it sage to assume that finger_map_index == current_search_finger_index
2768 * Calculate finger_map_index from initial value that we send in trail setup
2769 * message.
2770 * @param ultimate_destination_finger_value Value that we calculated from our
2771 * identity and finger_map_index.
2772 * @param is_predecessor Is the entry for predecessor or not.
2773 * @return finger_map_index which is a value between 0 <= finger_map_index <= 64
2774 * -1, if no valid finger_map_index is found.
2775 */
2776static int
2777get_finger_map_index (uint64_t ultimate_destination_finger_value,
2778 unsigned int is_predecessor)
2779{
2780 uint64_t my_id64;
2781 unsigned int finger_map_index;
2782
2783 finger_map_index = -1;
2784 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
2785
2786 if (is_predecessor)
2787 {
2788 if(1 == (my_id64 - ultimate_destination_finger_value))
2789 finger_map_index = PREDECESSOR_FINGER_ID;
2790 }
2791 else
2792 {
2793 finger_map_index = log (ultimate_destination_finger_value - my_id64);
2794 }
2795
2796 if ((finger_map_index > PREDECESSOR_FINGER_ID) ||
2797 (finger_map_index == current_search_finger_index))
2798 finger_map_index = -1;
2799
2800 return finger_map_index;
2801}
2802
2803
2804/**
2401 * Check if there is already an entry in finger peermap for given finger map index. 2805 * Check if there is already an entry in finger peermap for given finger map index.
2402 * If yes, then select the closest finger. If new and existing finger are same, 2806 * If yes, then select the closest finger. If new and existing finger are same,
2403 * the check if you can store more trails. If yes then add trail, else keep the best 2807 * the check if you can store more trails. If yes then add trail, else keep the best
@@ -2406,76 +2810,102 @@ send_verify_successor_message (struct FingerInfo *successor)
2406 * @param new_finger_identity Peer Identity of new finger 2810 * @param new_finger_identity Peer Identity of new finger
2407 * @param new_finger_trail Trail to reach the new finger 2811 * @param new_finger_trail Trail to reach the new finger
2408 * @param new_finger_length Total number of peers in @a new_finger_trail. 2812 * @param new_finger_length Total number of peers in @a new_finger_trail.
2409 * @param finger_map_index Index in finger peermap. 2813 * @param is_predecessor Is this entry for predecessor in finger_peermap.
2410 * @param new_finger_trail_id Unique identifier of @new_finger_trail. 2814 * @param new_finger_trail_id Unique identifier of @new_finger_trail.
2411 * @return #GNUNET_YES if the new entry is added 2815 * @return #GNUNET_YES if the new entry is added
2412 * #GNUNET_NO if new entry is not added, either it was discarded or 2816 * #GNUNET_NO if new entry is not added, either it was discarded or
2413 * it was same as existing finger at finger map index. 2817 * it was same as existing finger at finger map index.
2414 */ 2818 */
2415static int 2819static int
2416finger_table_add (struct GNUNET_PeerIdentity new_finger_identity, // "finger_id" 2820finger_table_add (struct GNUNET_PeerIdentity finger_identity,
2417 struct GNUNET_PeerIdentity *new_finger_trail, // "trail" 2821 const struct GNUNET_PeerIdentity *finger_trail,
2418 unsigned int new_finger_trail_length, 2822 unsigned int finger_trail_length,
2419 unsigned int finger_map_index, 2823 unsigned int is_predecessor,
2420 struct GNUNET_HashCode new_finger_trail_id) 2824 uint64_t finger_value,
2825 struct GNUNET_HashCode finger_trail_id)
2421{ 2826{
2422 struct FingerInfo *existing_finger; 2827 struct FingerInfo *existing_finger;
2423 struct FingerInfo *successor; 2828 struct GNUNET_PeerIdentity *closest_peer;
2424 unsigned int new_entry_added = GNUNET_NO; 2829 int updated_finger_trail_length;
2425 int new_finger_updated_trail_length; // new_finger_trail_length 2830 struct GNUNET_PeerIdentity *updated_trail;
2426 2831 unsigned int finger_map_index;
2427 new_finger_updated_trail_length = 2832 unsigned int new_entry_added;
2428 scan_and_compress_trail (new_finger_identity, new_finger_trail, 2833
2429 new_finger_trail_length, new_finger_trail_id); 2834 new_entry_added = GNUNET_NO;
2430 2835
2836 finger_map_index = get_finger_map_index (finger_value,
2837 is_predecessor);
2838
2839 if (-1 == finger_map_index)
2840 {
2841 GNUNET_break_op (0);
2842 return GNUNET_SYSERR;
2843 }
2844
2845 updated_trail =
2846 scan_and_compress_trail (finger_identity, finger_trail,
2847 finger_trail_length, finger_trail_id,
2848 &updated_finger_trail_length);
2849
2431 existing_finger = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 2850 existing_finger = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
2432 finger_map_index); 2851 finger_map_index);
2433 2852
2434 if (GNUNET_YES == is_new_finger_closest (existing_finger, 2853 /* No entry present in finger hashmap for given finger map index. */
2435 new_finger_identity, 2854 if (NULL == existing_finger)
2436 new_finger_trail, 2855 {
2437 new_finger_updated_trail_length, 2856 add_new_entry (finger_identity, updated_trail, updated_finger_trail_length,
2438 new_finger_trail_id, finger_map_index)) 2857 finger_trail_id, finger_map_index);
2439 { 2858 update_current_search_finger_index (finger_identity);
2440 // send_destroy_existing_finger ... 2859 return GNUNET_YES;
2441 // free_exisiting_finger ... 2860 }
2442 GNUNET_assert (GNUNET_YES == add_new_entry (new_finger_identity, 2861
2443 new_finger_trail, 2862 /* If existing entry and finger identity are not same. */
2444 new_finger_updated_trail_length, 2863 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
2445 new_finger_trail_id, 2864 &finger_identity))
2446 finger_map_index)); 2865 {
2447 new_entry_added = GNUNET_YES; 2866 closest_peer = select_closest_peer (&existing_finger->finger_identity,
2448 } 2867 &finger_identity,
2449// else if if_new_finger_equal () { 2868 finger_value, finger_map_index);
2450// 2869
2451// } 2870 /* If the new finger is the closest peer. */
2452// else // existing finger is closest 2871 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, closest_peer))
2453// {
2454//
2455// }
2456
2457 // FIXME move block to "update_succesor"
2458 {
2459 successor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 0);
2460 // WARNING FIXME check that current_search_finger_index does not go out of bounds
2461 if (0 == finger_map_index)
2462 { 2872 {
2463 current_search_finger_index = PREDECESSOR_FINGER_ID; 2873 GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2874 &finger_identity));
2464 2875
2465 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_finger_identity)) 2876 send_trail_teardown (existing_finger);
2466 { 2877 decrement_friend_trail_count (existing_finger);
2467 send_verify_successor_message (successor); 2878 free_finger (existing_finger);
2468 } 2879 add_new_entry (finger_identity, updated_trail, updated_finger_trail_length,
2880 finger_trail_id, finger_map_index);
2881 new_entry_added = GNUNET_YES;
2469 } 2882 }
2470 else if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity, 2883 }
2471 &(successor->finger_identity))) 2884 else
2885 {
2886 /* If both new and existing entry are same as my_identity, then do nothing. */
2887 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
2888 &my_identity))
2472 { 2889 {
2473 current_search_finger_index = 0; 2890 return GNUNET_NO;
2474 } 2891 }
2475 else 2892
2476 current_search_finger_index = current_search_finger_index - 1; 2893 /* If the existing finger is not a friend. */
2894 if (NULL ==
2895 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
2896 &(existing_finger->finger_identity)))
2897 {
2898 if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
2899 add_new_trail (existing_finger, updated_trail,
2900 finger_trail_length, finger_trail_id);
2901 else
2902 select_and_replace_trail (existing_finger, updated_trail,
2903 finger_trail_length, finger_trail_id);
2904 }
2905 new_entry_added = GNUNET_NO;
2477 } 2906 }
2478 2907
2908 update_current_search_finger_index (finger_identity);
2479 return new_entry_added; 2909 return new_entry_added;
2480} 2910}
2481 2911
@@ -2492,7 +2922,159 @@ static int
2492handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer, 2922handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
2493 const struct GNUNET_MessageHeader *message) 2923 const struct GNUNET_MessageHeader *message)
2494{ 2924{
2495 return GNUNET_OK; 2925 struct PeerPutMessage *put;
2926 struct GNUNET_PeerIdentity *put_path;
2927 struct GNUNET_HashCode test_key;
2928 enum GNUNET_DHT_RouteOption options;
2929 struct GNUNET_PeerIdentity best_known_dest;
2930 struct GNUNET_HashCode intermediate_trail_id;
2931 struct GNUNET_PeerIdentity *next_hop;
2932 void *payload;
2933 size_t msize;
2934 uint32_t putlen;
2935 size_t payload_size;
2936 uint64_t key_value;
2937
2938 msize = ntohs (message->size);
2939 if (msize < sizeof (struct PeerPutMessage))
2940 {
2941 GNUNET_break_op (0);
2942 return GNUNET_YES;
2943 }
2944
2945 put = (struct PeerPutMessage *) message;
2946 putlen = ntohl (put->put_path_length);
2947
2948 if ((msize <
2949 sizeof (struct PeerPutMessage) +
2950 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
2951 (putlen >
2952 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
2953 {
2954 GNUNET_break_op (0);
2955 return GNUNET_YES;
2956 }
2957
2958 best_known_dest = put->best_known_destination;
2959 put_path = (struct GNUNET_PeerIdentity *) &put[1];
2960 payload = &put_path[putlen];
2961 options = ntohl (put->options);
2962 intermediate_trail_id = put->intermediate_trail_id;
2963
2964 payload_size = msize - (sizeof (struct PeerPutMessage) +
2965 putlen * sizeof (struct GNUNET_PeerIdentity));
2966
2967 switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
2968 payload, payload_size, &test_key))
2969 {
2970 case GNUNET_YES:
2971 if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
2972 {
2973 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
2974 GNUNET_break_op (0);
2975 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2976 "PUT with key `%s' for block with key %s\n",
2977 put_s, GNUNET_h2s_full (&test_key));
2978 GNUNET_free (put_s);
2979 return GNUNET_YES;
2980 }
2981 break;
2982 case GNUNET_NO:
2983 GNUNET_break_op (0);
2984 return GNUNET_YES;
2985 case GNUNET_SYSERR:
2986 /* cannot verify, good luck */
2987 break;
2988 }
2989
2990 if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
2991 {
2992 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
2993 ntohl (put->block_type),
2994 NULL, /* query */
2995 NULL, 0, /* bloom filer */
2996 NULL, 0, /* xquery */
2997 payload, payload_size))
2998 {
2999 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3000 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3001 break;
3002
3003 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3004 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3005 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
3006 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3007 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3008 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3009 default:
3010 GNUNET_break_op (0);
3011 return GNUNET_OK;
3012 }
3013 }
3014
3015 /* extend 'put path' by sender */
3016 struct GNUNET_PeerIdentity pp[putlen + 1];
3017 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
3018 {
3019 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
3020 pp[putlen] = *peer;
3021 putlen++;
3022 }
3023 else
3024 putlen = 0;
3025
3026 memcpy (&key_value, &(put->key), sizeof (uint64_t));
3027 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3028 {
3029 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3030 GDS_ROUTING_SRC_TO_DEST);
3031 }
3032 else
3033 {
3034 /*FIXME: Here you should use enum GDS_NEIGHBOURS_FINGER_TYPE in place of 0. */
3035 next_hop = find_successor (key_value, &best_known_dest,
3036 &intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
3037 }
3038
3039 if (NULL == next_hop)
3040 {
3041 GNUNET_STATISTICS_update (GDS_stats,
3042 gettext_noop ("# Next hop to forward the packet not found "
3043 "trail setup request, packet dropped."),
3044 1, GNUNET_NO);
3045 return GNUNET_SYSERR;
3046 }
3047
3048 GDS_CLIENTS_process_put (options,
3049 ntohl (put->block_type),
3050 ntohl (put->hop_count),
3051 ntohl (put->desired_replication_level),
3052 putlen, pp,
3053 GNUNET_TIME_absolute_ntoh (put->expiration_time),
3054 &put->key,
3055 payload,
3056 payload_size);
3057
3058 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, next_hop)) /* I am the final destination */
3059 {
3060 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
3061 &(put->key),putlen, pp, ntohl (put->block_type),
3062 payload_size, payload);
3063 return GNUNET_YES;
3064 }
3065 else
3066 {
3067 GDS_NEIGHBOURS_send_put (&put->key,
3068 ntohl (put->block_type),ntohl (put->options),
3069 ntohl (put->desired_replication_level),
3070 &best_known_dest, &intermediate_trail_id, next_hop,
3071 ntohl (put->hop_count), putlen, pp,
3072 GNUNET_TIME_absolute_ntoh (put->expiration_time),
3073 payload, payload_size);
3074
3075 return GNUNET_YES;
3076 }
3077 return GNUNET_SYSERR;
2496} 3078}
2497 3079
2498 3080
@@ -2509,7 +3091,89 @@ static int
2509handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer, 3091handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
2510 const struct GNUNET_MessageHeader *message) 3092 const struct GNUNET_MessageHeader *message)
2511{ 3093{
2512 return GNUNET_OK; 3094 struct PeerGetMessage *get;
3095 struct GNUNET_PeerIdentity *get_path;
3096 struct GNUNET_PeerIdentity best_known_dest;
3097 struct GNUNET_HashCode intermediate_trail_id;
3098 struct GNUNET_PeerIdentity *next_hop;
3099 uint32_t get_length;
3100 uint64_t key_value;
3101 size_t msize;
3102
3103 msize = ntohs (message->size);
3104 if (msize < sizeof (struct PeerGetMessage))
3105 {
3106 GNUNET_break_op (0);
3107 return GNUNET_YES;
3108 }
3109
3110 get = (struct PeerGetMessage *)message;
3111 get_length = ntohl (get->get_path_length);
3112 best_known_dest = get->best_known_destination;
3113 intermediate_trail_id = get->intermediate_trail_id;
3114 if (get_length > 0)
3115 get_path = (struct GNUNET_PeerIdentity *)&get[1];
3116
3117 if ((msize <
3118 sizeof (struct PeerGetMessage) +
3119 get_length * sizeof (struct GNUNET_PeerIdentity)) ||
3120 (get_length >
3121 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3122 {
3123 GNUNET_break_op (0);
3124 return GNUNET_YES;
3125 }
3126
3127 /* Add sender to get path */
3128 struct GNUNET_PeerIdentity gp[get_length + 1];
3129 memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
3130 gp[get_length + 1] = *peer;
3131 get_length = get_length + 1;
3132
3133 memcpy (&key_value, &(get->key), sizeof (uint64_t));
3134 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
3135 {
3136 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3137 GDS_ROUTING_SRC_TO_DEST);
3138 }
3139 else
3140 {
3141 /*FIXME: Here you should use enum GDS_NEIGHBOURS_FINGER_TYPE in place of 0. */
3142 next_hop = find_successor (key_value, &best_known_dest,
3143 &intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
3144 }
3145
3146 if (NULL == next_hop)
3147 {
3148 GNUNET_STATISTICS_update (GDS_stats,
3149 gettext_noop ("# Next hop to forward the packet not found "
3150 "trail setup request, packet dropped."),
3151 1, GNUNET_NO);
3152 return GNUNET_SYSERR;
3153 }
3154 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, next_hop))
3155 {
3156 /* I am the destination.*/
3157 struct GNUNET_PeerIdentity final_get_path[get_length+1];
3158 struct GNUNET_PeerIdentity next_hop;
3159
3160 memcpy (final_get_path, gp, get_length * sizeof (struct GNUNET_PeerIdentity));
3161 memcpy (&final_get_path[get_length+1], &my_identity, sizeof (struct GNUNET_PeerIdentity));
3162 get_length = get_length + 1;
3163 memcpy (&next_hop, &final_get_path[get_length-2], sizeof (struct GNUNET_PeerIdentity));
3164 GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
3165 get_length, final_get_path,&next_hop, &my_identity);
3166
3167 return GNUNET_YES;
3168 }
3169 else
3170 {
3171 GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
3172 get->desired_replication_level, &best_known_dest,
3173 &intermediate_trail_id, next_hop, 0,
3174 get_length, gp);
3175 }
3176 return GNUNET_SYSERR;
2513} 3177}
2514 3178
2515 3179
@@ -2525,7 +3189,74 @@ static int
2525handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer, 3189handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
2526 const struct GNUNET_MessageHeader *message) 3190 const struct GNUNET_MessageHeader *message)
2527{ 3191{
2528 return GNUNET_OK; 3192 struct PeerGetResultMessage *get_result;
3193 struct GNUNET_PeerIdentity *get_path;
3194 struct GNUNET_PeerIdentity *put_path;
3195 void *payload;
3196 size_t payload_size;
3197 size_t msize;
3198 unsigned int getlen;
3199 unsigned int putlen;
3200 int current_path_index;
3201
3202 msize = ntohs (message->size);
3203 if (msize < sizeof (struct PeerGetResultMessage))
3204 {
3205 GNUNET_break_op (0);
3206 return GNUNET_YES;
3207 }
3208
3209 get_result = (struct PeerGetResultMessage *)message;
3210 getlen = ntohl (get_result->get_path_length);
3211 putlen = ntohl (get_result->put_path_length);
3212
3213 if ((msize <
3214 sizeof (struct PeerGetResultMessage) +
3215 getlen * sizeof (struct GNUNET_PeerIdentity) +
3216 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
3217 (getlen >
3218 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity) ||
3219 (putlen >
3220 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))))
3221 {
3222 GNUNET_break_op (0);
3223 return GNUNET_YES;
3224 }
3225
3226 if (getlen > 0)
3227 get_path = (struct GNUNET_PeerIdentity *) &get_result[1];
3228 payload = &get_path[getlen];
3229 payload_size = msize - (sizeof (struct PeerGetResultMessage) +
3230 getlen * sizeof (struct GNUNET_PeerIdentity));
3231
3232 if (putlen > 0)
3233 put_path = &get_path[1];
3234 else
3235 put_path = NULL;
3236
3237 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
3238 {
3239 GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
3240 getlen, get_path, putlen,
3241 put_path, get_result->type, payload_size, payload);
3242 return GNUNET_YES;
3243 }
3244 else
3245 {
3246 current_path_index = search_my_index (get_path, getlen);
3247 if (GNUNET_SYSERR == current_path_index )
3248 {
3249 GNUNET_break (0);
3250 return GNUNET_SYSERR;
3251 }
3252 GDS_NEIGHBOURS_send_get_result (&(get_result->key), get_result->type,
3253 &get_path[current_path_index - 1],
3254 &(get_result->querying_peer), putlen, put_path,
3255 getlen, get_path, get_result->expiration_time,
3256 payload, payload_size);
3257 return GNUNET_YES;
3258 }
3259 return GNUNET_SYSERR;
2529} 3260}
2530 3261
2531 3262
@@ -2539,19 +3270,19 @@ static int
2539handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer, 3270handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
2540 const struct GNUNET_MessageHeader *message) 3271 const struct GNUNET_MessageHeader *message)
2541{ 3272{
2542 struct PeerTrailSetupMessage *trail_setup; 3273 const struct PeerTrailSetupMessage *trail_setup;
2543 struct GNUNET_PeerIdentity *trail_peer_list; 3274 const struct GNUNET_PeerIdentity *trail_peer_list;
2544 struct GNUNET_PeerIdentity next_destination; // "local_best_known_destination" 3275 struct GNUNET_PeerIdentity local_best_known_destination;
2545 struct GNUNET_PeerIdentity *current_destination; 3276 struct GNUNET_PeerIdentity current_destination;
2546 struct GNUNET_PeerIdentity *next_hop; 3277 struct GNUNET_PeerIdentity *next_hop;
2547 struct GNUNET_PeerIdentity next_peer; 3278 struct GNUNET_PeerIdentity next_peer;
2548 struct FriendInfo *target_friend; 3279 struct FriendInfo *target_friend;
2549 struct GNUNET_PeerIdentity source; 3280 struct GNUNET_PeerIdentity source;
2550 uint64_t ultimate_destination_finger_value; 3281 uint64_t ultimate_destination_finger_value;
2551 struct GNUNET_HashCode new_intermediate_trail_id; 3282 struct GNUNET_HashCode new_intermediate_trail_id;
2552 //struct GNUNET_HashCode *intermediate_trail_id; 3283 struct GNUNET_HashCode intermediate_trail_id;
2553 struct GNUNET_HashCode new_trail_id; 3284 struct GNUNET_HashCode trail_id;
2554 unsigned int finger_map_index; 3285 unsigned int is_predecessor;
2555 uint32_t trail_length; 3286 uint32_t trail_length;
2556 size_t msize; 3287 size_t msize;
2557 3288
@@ -2563,67 +3294,71 @@ handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
2563 return GNUNET_YES; 3294 return GNUNET_YES;
2564 } 3295 }
2565 3296
2566 trail_setup = (struct PeerTrailSetupMessage *) message; 3297 trail_setup = (const struct PeerTrailSetupMessage *) message;
2567 trail_length = ntohl (trail_setup->trail_length); // (msize - sizeof (msg)) / sizeof(PI) 3298 trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
2568 // if ((msize - sizeof (msg)) % sizeof(PI) != 0) 3299 sizeof (struct GNUNET_PeerIdentity);
2569 if ((msize != sizeof (struct PeerTrailSetupMessage) + 3300 if ((msize - sizeof (struct PeerTrailSetupMessage)) %
2570 trail_length * sizeof (struct GNUNET_PeerIdentity)) || 3301 sizeof (struct GNUNET_PeerIdentity) != 0)
2571 (trail_length >
2572 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
2573 { 3302 {
2574 GNUNET_break_op (0); 3303 GNUNET_break_op (0);
2575 return GNUNET_OK; 3304 return GNUNET_OK;
2576 } 3305 }
2577 3306
2578 trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_setup[1]; 3307 trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_setup[1];
2579 current_destination = &trail_setup->next_destination; 3308 current_destination = trail_setup->best_known_destination;
2580 new_trail_id = trail_setup->new_trail_id; 3309 trail_id = trail_setup->trail_id;
2581 ultimate_destination_finger_value = GNUNET_ntohll (trail_setup->ultimate_destination_finger); 3310 ultimate_destination_finger_value =
3311 GNUNET_ntohll (trail_setup->ultimate_destination_finger_value);
2582 source = trail_setup->source_peer; 3312 source = trail_setup->source_peer;
2583 finger_map_index = ntohl (trail_setup->finger_map_index); 3313 is_predecessor = ntohl (trail_setup->is_predecessor);
2584 //intermediate_trail_id = &trail_setup->intermediate_trail_id; 3314 intermediate_trail_id = trail_setup->intermediate_trail_id;
2585 3315
2586 if (GNUNET_YES == GDS_ROUTING_threshold_reached()) 3316 if (GNUNET_YES == GDS_ROUTING_threshold_reached())
2587 { 3317 {
2588 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer); 3318 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
2589 GDS_NEIGHBOURS_send_trail_rejection (source, ultimate_destination_finger_value, 3319 GDS_NEIGHBOURS_send_trail_rejection (source, ultimate_destination_finger_value,
2590 my_identity, finger_map_index, 3320 my_identity, is_predecessor,
2591 trail_peer_list, trail_length, 3321 trail_peer_list, trail_length,
2592 new_trail_id, target_friend, 3322 trail_id, target_friend,
2593 CONGESTION_TIMEOUT); 3323 CONGESTION_TIMEOUT);
2594 return GNUNET_OK; 3324 return GNUNET_OK;
2595 } 3325 }
2596 3326
2597 next_hop = find_successor (ultimate_destination_finger_value, &next_destination, 3327
2598 &new_intermediate_trail_id, finger_map_index); 3328 next_hop = find_successor (ultimate_destination_finger_value,
2599 3329 &local_best_known_destination,
2600 /* Are we just a part of a trail towards a finger? */ 3330 &new_intermediate_trail_id, is_predecessor);
2601 if (0 != (GNUNET_CRYPTO_cmp_peer_identity(&my_identity, current_destination)))
2602 {
2603// struct GNUNET_PeerIdentity *closest_peer;
2604// struct GNUNET_PeerIdentity *peer1 =
2605// GDS_ROUTING_get_next_hop (intermediate_trail_id,
2606// GDS_ROUTING_SRC_TO_DEST);
2607 /* Is next_destination better than the original best_known_dest? */
2608 // BIG FIXME START
2609// if (0 != GNUNET_CRYPTO_cmp_peer_identity (next_destination,
2610// current_destination))
2611// {
2612// closest_peer = select_closest_peer (peer1, next_hop,
2613// ultimate_destination_finger_value,
2614// finger_map_index);
2615// }
2616// if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, closest_peer) ||
2617// (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, next_hop)))
2618// {
2619// next_hop = peer1;
2620// next_destination = *current_destination;
2621// new_intermediate_trail_id = intermediate_trail_id;
2622// }
2623 // BIG FIXME END
2624 }
2625 3331
3332 /* Are we just a part of a trail towards a finger (current_destination)? */
3333 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &current_destination)))
3334 {
3335 struct GNUNET_PeerIdentity *closest_peer;
3336
3337 /* Select best successor among one found locally and current_destination.*/
3338 closest_peer = select_closest_peer (&local_best_known_destination,
3339 &current_destination,
3340 ultimate_destination_finger_value,
3341 is_predecessor);
3342 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&current_destination,
3343 closest_peer))
3344 {
3345 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
3346 GDS_ROUTING_SRC_TO_DEST);
3347 if (NULL == next_hop) /* FIXME: Here we found next_hop NULL from routing table,
3348 * but we still have a next_hop from find_successor.
3349 * Should we not break and choose that next_hop. */
3350 {
3351 GNUNET_break_op (0);
3352 return GNUNET_SYSERR;
3353 }
3354
3355 local_best_known_destination = current_destination;
3356 new_intermediate_trail_id = intermediate_trail_id;
3357 }
3358 }
3359
2626 GNUNET_assert (NULL != next_hop); 3360 GNUNET_assert (NULL != next_hop);
3361
2627 /* Am I the final destination? */ 3362 /* Am I the final destination? */
2628 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))) 3363 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))
2629 { 3364 {
@@ -2637,7 +3372,8 @@ handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
2637 my_identity, 3372 my_identity,
2638 target_friend, trail_length, 3373 target_friend, trail_length,
2639 trail_peer_list, 3374 trail_peer_list,
2640 finger_map_index, new_trail_id); 3375 ultimate_destination_finger_value,
3376 is_predecessor, trail_id);
2641 } 3377 }
2642 else 3378 else
2643 { 3379 {
@@ -2648,33 +3384,103 @@ handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
2648 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop); 3384 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
2649 GDS_NEIGHBOURS_send_trail_setup (source, 3385 GDS_NEIGHBOURS_send_trail_setup (source,
2650 ultimate_destination_finger_value, 3386 ultimate_destination_finger_value,
2651 next_destination, 3387 local_best_known_destination,
2652 target_friend, trail_length + 1, peer_list, 3388 target_friend, trail_length + 1, peer_list,
2653 finger_map_index, new_trail_id, 3389 is_predecessor, trail_id,
2654 &new_intermediate_trail_id); 3390 &new_intermediate_trail_id);
2655 } 3391 }
2656 return GNUNET_OK; 3392 return GNUNET_OK;
2657} 3393}
2658 3394
2659 3395
3396/* FIXME: here we are calculating my_index and comparing also in this function.
3397 And we are doing it again here in this function. Re factor the code. */
2660/** 3398/**
2661 * Core handle for p2p trail construction result messages. 3399 * Check if sender_peer and peer from which we should receive the message are
3400 * same or different.
3401 * @param trail_peer_list List of peers in trail
3402 * @param trail_length Total number of peers in @a trail_peer_list
3403 * @param sender_peer Peer from which we got the message.
3404 * @param finger_identity Finger to which trail is setup. It is not part of trail.
3405 * @return #GNUNET_YES if sender_peer and peer from which we should receive the
3406 * message are different.
3407 * #GNUNET_NO if sender_peer and peer from which we should receive the
3408 * message are different.
3409 */
3410static int
3411is_sender_peer_correct (const struct GNUNET_PeerIdentity *trail_peer_list,
3412 unsigned int trail_length,
3413 const struct GNUNET_PeerIdentity *sender_peer,
3414 struct GNUNET_PeerIdentity finger_identity,
3415 struct GNUNET_PeerIdentity source_peer)
3416{
3417 int my_index;
3418
3419 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
3420 &my_identity)))
3421 {
3422 if (trail_length > 0)
3423 {
3424 // source, then trail_length > 0, trail_peer_list[0] != peer
3425 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[0],
3426 sender_peer))
3427 return GNUNET_NO;
3428 }
3429 else
3430 {
3431 // source, trail_length == 0, finger != peer
3432 if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
3433 &finger_identity))
3434 return GNUNET_NO;
3435 }
3436 }
3437 else
3438 {
3439 my_index = search_my_index (trail_peer_list, trail_length);
3440 if (-1 == my_index)
3441 return GNUNET_NO;
3442
3443 // my_index == trail_length -1, finger != peer
3444 if ((trail_length - 1) == my_index)
3445 {
3446 if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
3447 &finger_identity))
3448 return GNUNET_NO;
3449 }
3450 else
3451 {
3452 // FIXME: if trail_peer_list[my_index + 1] != peer
3453 if (0 != GNUNET_CRYPTO_cmp_peer_identity (sender_peer,
3454 &trail_peer_list[my_index + 1]))
3455 return GNUNET_NO;
3456 }
3457 }
3458 return GNUNET_YES;
3459}
3460
3461
3462/**
3463 * Core handle for p2p trail setup result messages.
2662 * @param closure 3464 * @param closure
2663 * @param message message 3465 * @param message message
2664 * @param peer peer identity this notification is about 3466 * @param peer sender of this message.
2665 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 3467 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2666 */ 3468 */
2667static int 3469static int
2668handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer, 3470handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
2669 const struct GNUNET_MessageHeader *message) 3471 const struct GNUNET_MessageHeader *message)
2670{ 3472{
2671 struct PeerTrailSetupResultMessage *trail_result; 3473 const struct PeerTrailSetupResultMessage *trail_result;
2672 struct GNUNET_PeerIdentity *trail_peer_list; 3474 const struct GNUNET_PeerIdentity *trail_peer_list;
2673 struct GNUNET_PeerIdentity destination_peer; 3475 struct GNUNET_PeerIdentity next_hop;
3476 struct FriendInfo *target_friend;
3477 struct GNUNET_PeerIdentity querying_peer;
2674 struct GNUNET_PeerIdentity finger_identity; 3478 struct GNUNET_PeerIdentity finger_identity;
2675 uint32_t trail_length; 3479 uint32_t trail_length;
2676 uint32_t finger_map_index; 3480 uint64_t ulitmate_destination_finger_value;
3481 uint32_t is_predecessor;
2677 struct GNUNET_HashCode trail_id; 3482 struct GNUNET_HashCode trail_id;
3483 int my_index;
2678 size_t msize; 3484 size_t msize;
2679 3485
2680 msize = ntohs (message->size); 3486 msize = ntohs (message->size);
@@ -2684,66 +3490,71 @@ handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *p
2684 return GNUNET_YES; 3490 return GNUNET_YES;
2685 } 3491 }
2686 3492
2687 trail_result = (struct PeerTrailSetupResultMessage *) message; 3493 trail_result = (const struct PeerTrailSetupResultMessage *) message;
2688 3494 trail_length = (msize - sizeof (struct PeerTrailSetupResultMessage))/
2689 // calculate trail_length with message size, check for % 0 3495 sizeof (struct GNUNET_PeerIdentity);
2690 trail_length = ntohl (trail_result->trail_length); 3496 if ((msize - sizeof (struct PeerTrailSetupResultMessage)) %
2691 if ((msize != 3497 sizeof (struct GNUNET_PeerIdentity) != 0)
2692 sizeof (struct PeerTrailSetupResultMessage) +
2693 trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
2694 (trail_length >
2695 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
2696 { 3498 {
2697 GNUNET_break_op (0); 3499 GNUNET_break_op (0);
2698 return GNUNET_YES; 3500 return GNUNET_OK;
2699 } 3501 }
2700 3502
2701 finger_map_index = htonl (trail_result->finger_map_index); 3503 is_predecessor = htonl (trail_result->is_predecessor);
2702 destination_peer = trail_result->destination_peer; 3504 querying_peer = trail_result->querying_peer;
2703 finger_identity = trail_result->finger_identity; 3505 finger_identity = trail_result->finger_identity;
2704 trail_id = trail_result->trail_id; 3506 trail_id = trail_result->trail_id;
2705 trail_peer_list = (struct GNUNET_PeerIdentity *) &trail_result[1]; 3507 trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
2706 3508 ulitmate_destination_finger_value =
2707 // FIXME: check that trail_peer_list[my_index + 1] == peer or 3509 GNUNET_ntohll (trail_result->ulitmate_destination_finger_value);
2708 // my_index == trail_length - 1 AND finger_identity == peer 3510 /* FIXME: here we are calculating my_index and comparing also in this function.
2709 3511 And we are doing it again here in this function. Re factor the code. */
3512 /* Ensure that sender peer is the peer from which we were expecting the message. */
3513 if (GNUNET_NO == is_sender_peer_correct (trail_peer_list,
3514 trail_length,
3515 peer, finger_identity, querying_peer))
3516 {
3517 GNUNET_break_op (0);
3518 return GNUNET_SYSERR;
3519 }
3520
2710 /* Am I the one who initiated the query? */ 3521 /* Am I the one who initiated the query? */
2711 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&destination_peer, 3522 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer,
2712 &my_identity))) 3523 &my_identity)))
2713 { 3524 {
2714 finger_table_add (finger_identity, trail_peer_list, 3525 finger_table_add (finger_identity, trail_peer_list,
2715 trail_length, 3526 trail_length, ulitmate_destination_finger_value,
2716 finger_map_index, trail_id); 3527 is_predecessor, trail_id);
2717 return GNUNET_YES; 3528 return GNUNET_YES;
2718 } 3529 }
2719 3530
2720 struct GNUNET_PeerIdentity next_hop;
2721 struct FriendInfo *target_friend;
2722 int my_index;
2723
2724 my_index = search_my_index(trail_peer_list, trail_length); 3531 my_index = search_my_index(trail_peer_list, trail_length);
2725 if (-1 == my_index) 3532 if (-1 == my_index)
2726 { 3533 {
2727 GNUNET_break_op(0); 3534 GNUNET_break_op(0);
2728 return GNUNET_SYSERR; 3535 return GNUNET_SYSERR;
2729 } 3536 }
2730 3537
2731
2732 if (my_index == 0) 3538 if (my_index == 0)
2733 next_hop = trail_result->destination_peer; 3539 next_hop = trail_result->querying_peer;
2734 else 3540 else
2735 next_hop = trail_peer_list[my_index - 1]; 3541 next_hop = trail_peer_list[my_index - 1];
2736 3542
2737 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->destination_peer), 3543 /* If the querying_peer is its own finger, then don't add an entry in routing
2738 &(trail_result->finger_identity)))) 3544 * table as querying peer will discard the trail.
3545 */
3546 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->querying_peer),
3547 &(trail_result->finger_identity))))
2739 { 3548 {
2740 GDS_ROUTING_add (trail_id, next_hop, *peer); 3549 GDS_ROUTING_add (trail_id, next_hop, *peer);
2741 } 3550 }
2742 3551
2743 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop); 3552 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
2744 GDS_NEIGHBOURS_send_trail_setup_result (destination_peer, finger_identity, 3553 GDS_NEIGHBOURS_send_trail_setup_result (querying_peer, finger_identity,
2745 target_friend, trail_length, trail_peer_list, 3554 target_friend, trail_length, trail_peer_list,
2746 finger_map_index, trail_id); 3555 is_predecessor,
3556 ulitmate_destination_finger_value,
3557 trail_id);
2747 return GNUNET_OK; 3558 return GNUNET_OK;
2748} 3559}
2749 3560
@@ -2755,7 +3566,7 @@ handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *p
2755 * @return Updated trail 3566 * @return Updated trail
2756 */ 3567 */
2757static struct GNUNET_PeerIdentity * 3568static struct GNUNET_PeerIdentity *
2758invert_trail (struct GNUNET_PeerIdentity *trail, 3569invert_trail (const struct GNUNET_PeerIdentity *trail,
2759 unsigned int trail_length) 3570 unsigned int trail_length)
2760{ 3571{
2761 int i; 3572 int i;
@@ -2777,37 +3588,10 @@ invert_trail (struct GNUNET_PeerIdentity *trail,
2777 3588
2778 3589
2779/** 3590/**
2780 * Check if the new finger can be our predecessor. If yes then update predecessor 3591 * FIXME:
2781 * 3592 * my_current_predecessor != source_peer. get the trail to reach to
2782 * @param new_finger 3593 * my_current_predecessor and append it to the trail from source to me.
2783 * @param new_finger_trail 3594 * It can contain duplicate elements. Need to get the correct one.
2784 * @param new_finger_trail_length
2785 * @return
2786 */
2787static int
2788is_new_entry_correct_predecessor (struct FingerInfo *my_predecessor,
2789 struct GNUNET_PeerIdentity new_finger,
2790 struct GNUNET_PeerIdentity *new_finger_trail,
2791 unsigned int new_finger_trail_length)
2792{
2793 struct GNUNET_PeerIdentity *updated_trail;
2794 struct GNUNET_HashCode new_trail_id;
2795
2796 updated_trail = invert_trail (new_finger_trail, new_finger_trail_length);
2797 if (GNUNET_YES == is_new_finger_closest (my_predecessor, new_finger,
2798 new_finger_trail,
2799 new_finger_trail_length,
2800 new_trail_id, PREDECESSOR_FINGER_ID))
2801 {
2802 add_new_entry (new_finger, updated_trail, new_finger_trail_length,
2803 new_trail_id, PREDECESSOR_FINGER_ID);
2804 /* FIXME: check where you send add trail message */
2805 return GNUNET_YES;
2806 }
2807 return GNUNET_NO;
2808}
2809
2810/**
2811 * In case the source peer of verify successor message is not my successor, 3595 * In case the source peer of verify successor message is not my successor,
2812 * then construct a trail from source peer to my current predecessor. 3596 * then construct a trail from source peer to my current predecessor.
2813 * @param my_predecessor my current predecessor. 3597 * @param my_predecessor my current predecessor.
@@ -2817,18 +3601,21 @@ is_new_entry_correct_predecessor (struct FingerInfo *my_predecessor,
2817 * @return Updated trail from source peer to my_predecessor. 3601 * @return Updated trail from source peer to my_predecessor.
2818 */ 3602 */
2819static struct GNUNET_PeerIdentity * 3603static struct GNUNET_PeerIdentity *
2820trail_source_to_my_predecessor (struct FingerInfo *my_predecessor, 3604trail_source_to_my_predecessor (const struct GNUNET_PeerIdentity *current_trail,
2821 struct GNUNET_PeerIdentity *current_trail,
2822 unsigned int current_trail_length, 3605 unsigned int current_trail_length,
2823 unsigned int *new_trail_length) 3606 unsigned int *new_trail_length)
2824{ 3607{
2825 struct GNUNET_PeerIdentity *new_trail; 3608 struct GNUNET_PeerIdentity *new_trail;
2826 struct TrailList *trail_list_iterator; 3609 struct Trail *trail_list_iterator;
2827 struct Trail *trail_iterator; 3610 struct Trail_Element *trail_iterator;
3611 struct FingerInfo *my_predecessor;
2828 unsigned int i; 3612 unsigned int i;
2829 unsigned int j; 3613 unsigned int j;
2830 unsigned int shortest_trail_length = 0; 3614 unsigned int shortest_trail_length = 0;
2831 unsigned int trail_index = 0; 3615 unsigned int trail_index = 0;
3616
3617 my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
3618 PREDECESSOR_FINGER_ID);
2832 3619
2833 for (i = 0; i < my_predecessor->trails_count; i++) 3620 for (i = 0; i < my_predecessor->trails_count; i++)
2834 { 3621 {
@@ -2864,6 +3651,96 @@ trail_source_to_my_predecessor (struct FingerInfo *my_predecessor,
2864 3651
2865 3652
2866/** 3653/**
3654 * FIMXE: IF you update your predecessor with new finger identity or added
3655 * a new trail, then you should send add_trail message or else the peers
3656 * which are part of the trail to reach to your new predecessor will not
3657 * know its existence.
3658 * Check if we already have an entry for predecessor in our finger hashmap.
3659 * If no, then add new predecessor.
3660 * If yes, then compare new and existing entry. If both are same, and there is
3661 * place to store more trail, then add entry. If both are different then choose
3662 * the closest one.
3663 * @param finger Peer which claims to be our predecessor.
3664 * @param trail Trail to reach from @a predecessor to me, NOT including both endpoints
3665 * @param trail_length Total number of peers in @a trail.
3666 * @return #GNUNET_YES if we added the new entry.
3667 * #GNUNET_NO, if new entry is not the closest predecessor or both new
3668 * and existing entry are same.
3669 */
3670static struct GNUNET_PeerIdentity
3671compare_and_update_predecessor (struct GNUNET_PeerIdentity finger_identity,
3672 const struct GNUNET_PeerIdentity *trail,
3673 unsigned int trail_length)
3674{
3675 struct FingerInfo *current_predecessor;
3676 struct GNUNET_PeerIdentity *closest_peer;
3677 struct GNUNET_PeerIdentity *inverted_trail;
3678 struct GNUNET_HashCode trail_id;
3679 uint64_t finger_value;
3680
3681 current_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
3682 PREDECESSOR_FINGER_ID);
3683 inverted_trail = invert_trail (trail, trail_length);
3684
3685 /* No predecessor in finger table. Add the new predecessor. */
3686 if (NULL == current_predecessor)
3687 {
3688 add_new_entry (finger_identity, inverted_trail, trail_length,
3689 trail_id, PREDECESSOR_FINGER_ID);
3690 return finger_identity;
3691 }
3692
3693 /* If the new predecessor and current predecessor are not same, then choose
3694 the closest one. */
3695 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
3696 &current_predecessor->finger_identity))
3697 {
3698 finger_value = compute_predecessor_identity_value();
3699 closest_peer = select_closest_peer (&current_predecessor->finger_identity,
3700 &finger_identity,
3701 finger_value, PREDECESSOR_FINGER_ID);
3702
3703 /* If the finger is the closest predecessor, then add it in finger table
3704 * and free resources of current_predecessor. */
3705 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, closest_peer))
3706 {
3707 GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
3708 &finger_identity));
3709
3710 send_trail_teardown (current_predecessor);
3711 decrement_friend_trail_count (current_predecessor);
3712 free_finger (current_predecessor);
3713 add_new_entry (finger_identity, inverted_trail, trail_length,
3714 trail_id, PREDECESSOR_FINGER_ID);
3715 return finger_identity;
3716 }
3717 }
3718
3719 /* If both finger and current_predecessor are same as my_identity, then do nothing. */
3720 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, &my_identity))
3721 return current_predecessor->finger_identity;
3722
3723 /* If the current_predecessor is not a friend. */
3724 if (NULL ==
3725 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3726 &current_predecessor->finger_identity))
3727 {
3728 if (current_predecessor->trails_count < MAXIMUM_TRAILS_PER_FINGER)
3729 add_new_trail (current_predecessor, inverted_trail,
3730 trail_length, trail_id);
3731 //FIXME: here you should send add a new trail as you don't have this trail
3732 // in the routing table.
3733 else
3734 select_and_replace_trail (current_predecessor, inverted_trail,
3735 trail_length, trail_id);
3736 //FIXME: here you should send add a new trail as you don't have this trail
3737 // in the routing table.
3738 }
3739 return current_predecessor->finger_identity;
3740}
3741
3742
3743/**
2867 * Core handle for p2p verify successor messages. 3744 * Core handle for p2p verify successor messages.
2868 * @param cls closure 3745 * @param cls closure
2869 * @param message message 3746 * @param message message
@@ -2880,7 +3757,7 @@ handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *pee
2880 struct GNUNET_PeerIdentity *next_hop; 3757 struct GNUNET_PeerIdentity *next_hop;
2881 struct FriendInfo *target_friend; 3758 struct FriendInfo *target_friend;
2882 struct GNUNET_HashCode trail_id; 3759 struct GNUNET_HashCode trail_id;
2883 struct FingerInfo *my_predecessor; 3760 struct GNUNET_PeerIdentity current_predecessor;
2884 struct GNUNET_PeerIdentity *trail; 3761 struct GNUNET_PeerIdentity *trail;
2885 struct GNUNET_PeerIdentity *new_trail; 3762 struct GNUNET_PeerIdentity *new_trail;
2886 unsigned int trail_length; 3763 unsigned int trail_length;
@@ -2895,19 +3772,22 @@ handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *pee
2895 } 3772 }
2896 3773
2897 vsm = (struct PeerVerifySuccessorMessage *) message; 3774 vsm = (struct PeerVerifySuccessorMessage *) message;
2898 trail_length = ntohl (vsm->trail_length); 3775 trail_length = (msize - sizeof (struct PeerVerifySuccessorMessage))/
2899 if ((msize != sizeof (struct PeerVerifySuccessorMessage) + 3776 sizeof (struct GNUNET_PeerIdentity);
2900 trail_length * sizeof (struct GNUNET_PeerIdentity)) || 3777 if ((msize - sizeof (struct PeerVerifySuccessorMessage)) %
2901 (trail_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) 3778 sizeof (struct GNUNET_PeerIdentity) != 0)
2902 { 3779 {
2903 GNUNET_break_op (0); 3780 GNUNET_break_op (0);
2904 return GNUNET_YES; 3781 return GNUNET_OK;
2905 } 3782 }
3783
2906 trail = (struct GNUNET_PeerIdentity *)&vsm[1]; 3784 trail = (struct GNUNET_PeerIdentity *)&vsm[1];
2907 source_peer = vsm->source_peer; 3785 source_peer = vsm->source_peer;
2908 successor = vsm->successor; 3786 successor = vsm->successor;
2909 trail_id = vsm->trail_id; 3787 trail_id = vsm->trail_id;
2910 3788
3789 /* I am not the successor of source_peer. Pass the message to next_hop on
3790 * the trail. */
2911 if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity))) 3791 if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
2912 { 3792 {
2913 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST); 3793 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
@@ -2922,39 +3802,26 @@ handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *pee
2922 target_friend); 3802 target_friend);
2923 return GNUNET_OK; 3803 return GNUNET_OK;
2924 } 3804 }
2925 3805
2926 my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
2927 PREDECESSOR_FINGER_ID);
2928 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer); 3806 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
2929 if (GNUNET_NO == is_new_entry_correct_predecessor (my_predecessor, source_peer, 3807 current_predecessor = compare_and_update_predecessor (source_peer,
2930 trail, trail_length)) 3808 trail, trail_length);
3809 /* source_peer is my current_predecessor. */
3810 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&current_predecessor, &source_peer)))
2931 { 3811 {
2932 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer, 3812 /* Here we don't need to pass the trail as it is of new use. We can pass
2933 &my_predecessor->finger_identity)) 3813 NULL and trail length = 0. */
2934 { 3814 new_trail = NULL;
2935 new_trail = trail_source_to_my_predecessor (my_predecessor, trail, 3815 new_trail_length = 0;
2936 trail_length, &new_trail_length);
2937 }
2938 } 3816 }
2939 else 3817 else
2940 { 3818 {
2941 my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 3819 new_trail = trail_source_to_my_predecessor (trail, trail_length,
2942 PREDECESSOR_FINGER_ID); 3820 &new_trail_length);
2943 GDS_NEIGHBOURS_send_add_trail (my_predecessor->finger_identity,
2944 source_peer, trail_id,
2945 trail, trail_length,
2946 target_friend);
2947 new_trail_length = trail_length;
2948 new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
2949 new_trail_length);
2950 memcpy (new_trail, trail, sizeof (struct GNUNET_PeerIdentity) *
2951 trail_length);
2952 } 3821 }
2953
2954 GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity, 3822 GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
2955 my_predecessor->finger_identity, 3823 current_predecessor, trail_id,
2956 trail_id, new_trail, 3824 new_trail, new_trail_length,
2957 new_trail_length,
2958 GDS_ROUTING_DEST_TO_SRC, 3825 GDS_ROUTING_DEST_TO_SRC,
2959 target_friend); 3826 target_friend);
2960 return GNUNET_OK; 3827 return GNUNET_OK;
@@ -2962,6 +3829,9 @@ handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *pee
2962 3829
2963 3830
2964/** 3831/**
3832 * Here you may or may not be the predecessor. If you are then also while adding
3833 * you have to check and if not then notify new successor. But again the trail
3834 * may be a problem here.
2965 * FIXME: I will keep the logic to remove the old trail to reach from me to 3835 * FIXME: I will keep the logic to remove the old trail to reach from me to
2966 * my old successor here and move adding a new trail from me to new successor to notify 3836 * my old successor here and move adding a new trail from me to new successor to notify
2967 * new successor. And in case if the new successor also take it as predecessor 3837 * new successor. And in case if the new successor also take it as predecessor
@@ -2981,9 +3851,9 @@ handle_dht_p2p_verify_successor_result(void *cls, const struct GNUNET_PeerIdenti
2981 struct GNUNET_HashCode trail_id; 3851 struct GNUNET_HashCode trail_id;
2982 unsigned int new_trail_length; 3852 unsigned int new_trail_length;
2983 struct GNUNET_PeerIdentity *new_trail; 3853 struct GNUNET_PeerIdentity *new_trail;
2984 struct GNUNET_PeerIdentity destination_peer; 3854 struct GNUNET_PeerIdentity querying_peer;
2985 struct GNUNET_PeerIdentity my_new_successor; 3855 struct GNUNET_PeerIdentity current_predecessor;
2986 struct GNUNET_PeerIdentity old_successor; 3856 //struct GNUNET_PeerIdentity old_successor;
2987 struct GNUNET_PeerIdentity *next_hop; 3857 struct GNUNET_PeerIdentity *next_hop;
2988 struct FriendInfo *target_friend; 3858 struct FriendInfo *target_friend;
2989 size_t msize; 3859 size_t msize;
@@ -2995,28 +3865,29 @@ handle_dht_p2p_verify_successor_result(void *cls, const struct GNUNET_PeerIdenti
2995 return GNUNET_YES; 3865 return GNUNET_YES;
2996 } 3866 }
2997 vsrm = (struct PeerVerifySuccessorResultMessage *) message; 3867 vsrm = (struct PeerVerifySuccessorResultMessage *) message;
2998 new_trail_length = ntohl (vsrm->trail_length); 3868 new_trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
2999 trail_direction = ntohl (vsrm->trail_direction); 3869 sizeof (struct GNUNET_PeerIdentity);
3000 trail_id = vsrm->trail_id; 3870 if ((msize - sizeof (struct PeerTrailSetupMessage)) %
3001 3871 sizeof (struct GNUNET_PeerIdentity) != 0)
3002 if ((msize !=
3003 sizeof (struct PeerVerifySuccessorResultMessage) +
3004 new_trail_length *
3005 sizeof (struct GNUNET_PeerIdentity)) ||
3006 (new_trail_length >
3007 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3008 { 3872 {
3009 GNUNET_break_op (0); 3873 GNUNET_break_op (0);
3010 return GNUNET_YES; 3874 return GNUNET_OK;
3011 } 3875 }
3876 trail_direction = ntohl (vsrm->trail_direction);
3877 trail_id = vsrm->trail_id;
3012 3878
3013 new_trail = (struct GNUNET_PeerIdentity *) &vsrm[1]; 3879 new_trail = (struct GNUNET_PeerIdentity *) &vsrm[1];
3014 destination_peer = vsrm->destination_peer; 3880 querying_peer = vsrm->querying_peer;
3015 my_new_successor = vsrm->my_predecessor; 3881 current_predecessor = vsrm->current_predecessor;
3016 old_successor = vsrm->source_successor; 3882 //old_successor = vsrm->source_successor;
3017 3883
3018 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&destination_peer, &my_identity))) 3884 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
3019 { 3885 {
3886#if 0
3887 //Here I am the final quering peer and its verify successor result.
3888 // now I know it is for successor and now I don't have any finger_map_index
3889 // as parameter in finger_table_add.
3890 // BIG FIX HOW TO ADAPT IT FOR change in finger_table_add
3020 struct GNUNET_HashCode new_finger_trail_id; 3891 struct GNUNET_HashCode new_finger_trail_id;
3021 /* FIXME: generate a new trail id. */ 3892 /* FIXME: generate a new trail id. */
3022 if (GNUNET_YES == finger_table_add (my_new_successor, 3893 if (GNUNET_YES == finger_table_add (my_new_successor,
@@ -3037,15 +3908,16 @@ handle_dht_p2p_verify_successor_result(void *cls, const struct GNUNET_PeerIdenti
3037 new_trail_length, 3908 new_trail_length,
3038 new_finger_trail_id, target_friend); 3909 new_finger_trail_id, target_friend);
3039 } 3910 }
3911#endif
3040 return GNUNET_OK; 3912 return GNUNET_OK;
3041 } 3913 }
3042 3914
3043 GNUNET_assert (NULL != (next_hop = 3915 GNUNET_assert (NULL != (next_hop =
3044 GDS_ROUTING_get_next_hop (trail_id, trail_direction))); 3916 GDS_ROUTING_get_next_hop (trail_id, trail_direction)));
3045 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop); 3917 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
3046 GDS_NEIGHBOURS_send_verify_successor_result (destination_peer, 3918 GDS_NEIGHBOURS_send_verify_successor_result (querying_peer,
3047 vsrm->source_successor, 3919 vsrm->source_successor,
3048 my_new_successor, trail_id, 3920 current_predecessor, trail_id,
3049 new_trail, 3921 new_trail,
3050 new_trail_length, 3922 new_trail_length,
3051 trail_direction, target_friend); 3923 trail_direction, target_friend);
@@ -3054,6 +3926,8 @@ handle_dht_p2p_verify_successor_result(void *cls, const struct GNUNET_PeerIdenti
3054 3926
3055 3927
3056/* 3928/*
3929 * FIXME: here before adding the peer you again have to call to check
3930 * if its correct predecessor or not.
3057 * Core handle for p2p notify new successor messages. 3931 * Core handle for p2p notify new successor messages.
3058 * @param cls closure 3932 * @param cls closure
3059 * @param message message 3933 * @param message message
@@ -3067,8 +3941,9 @@ handle_dht_p2p_notify_new_successor(void *cls, const struct GNUNET_PeerIdentity
3067 struct PeerNotifyNewSuccessorMessage *nsm; 3941 struct PeerNotifyNewSuccessorMessage *nsm;
3068 struct GNUNET_PeerIdentity *trail; 3942 struct GNUNET_PeerIdentity *trail;
3069 struct GNUNET_PeerIdentity source; 3943 struct GNUNET_PeerIdentity source;
3070 struct GNUNET_PeerIdentity destination; 3944 struct GNUNET_PeerIdentity new_successor;
3071 struct FriendInfo *target_friend; 3945 struct FriendInfo *target_friend;
3946 struct GNUNET_PeerIdentity closest_successor;
3072 struct GNUNET_HashCode trail_id; 3947 struct GNUNET_HashCode trail_id;
3073 unsigned int my_index; 3948 unsigned int my_index;
3074 struct GNUNET_PeerIdentity next_hop; 3949 struct GNUNET_PeerIdentity next_hop;
@@ -3083,7 +3958,14 @@ handle_dht_p2p_notify_new_successor(void *cls, const struct GNUNET_PeerIdentity
3083 } 3958 }
3084 3959
3085 nsm = (struct PeerNotifyNewSuccessorMessage *) message; 3960 nsm = (struct PeerNotifyNewSuccessorMessage *) message;
3086 trail_length = ntohl (nsm->trail_length); 3961 trail_length = (msize - sizeof (struct PeerNotifyNewSuccessorMessage))/
3962 sizeof (struct GNUNET_PeerIdentity);
3963 if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
3964 sizeof (struct GNUNET_PeerIdentity) != 0)
3965 {
3966 GNUNET_break_op (0);
3967 return GNUNET_OK;
3968 }
3087 3969
3088 if ((msize < sizeof (struct PeerNotifyNewSuccessorMessage) + 3970 if ((msize < sizeof (struct PeerNotifyNewSuccessorMessage) +
3089 trail_length * sizeof (struct GNUNET_PeerIdentity)) || 3971 trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
@@ -3096,45 +3978,42 @@ handle_dht_p2p_notify_new_successor(void *cls, const struct GNUNET_PeerIdentity
3096 3978
3097 trail = (struct GNUNET_PeerIdentity *) &nsm[1]; 3979 trail = (struct GNUNET_PeerIdentity *) &nsm[1];
3098 source = nsm->source_peer; 3980 source = nsm->source_peer;
3099 destination = nsm->destination_peer; 3981 new_successor = nsm->new_successor;
3100 trail_id = nsm->trail_id; 3982 trail_id = nsm->trail_id;
3101 3983
3102 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination)) 3984 /* I am the new_successor to source_peer. */
3985 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_successor))
3103 { 3986 {
3104 struct GNUNET_HashCode new_trail_id; 3987 closest_successor = compare_and_update_predecessor (source, trail, trail_length);
3105 struct FingerInfo *my_predecessor; 3988 /* If the source peer is my new predecessor, then add trail in the
3106 3989 routing table of all the peers which are part of the trail to reach from
3107 my_predecessor = 3990 me to source, NOT including endpoints.
3108 GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 3991 FIXME: In case we have same predecessor but we added just a new trail, then
3109 PREDECESSOR_FINGER_ID); 3992 we are not sending add a new trail message. Need to update this. */
3110 /* FIXME: get new_trail_id*/ 3993 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&closest_successor, &source))
3111
3112 if (GNUNET_YES == is_new_entry_correct_predecessor (my_predecessor,
3113 source, trail,
3114 trail_length))
3115 { 3994 {
3116 3995 struct GNUNET_HashCode new_trail_id;
3117 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 3996 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
3118 peer); 3997 peer);
3119 GDS_NEIGHBOURS_send_add_trail (my_identity, source, new_trail_id, 3998 GDS_NEIGHBOURS_send_add_trail (my_identity, source, new_trail_id,
3120 trail, trail_length, target_friend); 3999 trail, trail_length, target_friend);
3121 return GNUNET_OK;
3122 } 4000 }
4001 return GNUNET_OK;
3123 } 4002 }
3124 4003
3125 my_index = search_my_index (trail, trail_length); 4004 my_index = search_my_index (trail, trail_length);
3126 if (GNUNET_SYSERR == my_index) 4005 if (-1 == my_index)
3127 { 4006 {
3128 GNUNET_break_op (0); 4007 GNUNET_break_op (0);
3129 return GNUNET_SYSERR; 4008 return GNUNET_SYSERR;
3130 } 4009 }
3131 if (trail_length == my_index) 4010 if (trail_length == my_index)
3132 next_hop = destination; 4011 next_hop = new_successor;
3133 else 4012 else
3134 next_hop = trail[my_index + 1]; 4013 next_hop = trail[my_index + 1];
3135 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop)); 4014 GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
3136 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop); 4015 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
3137 GDS_NEIGHBOURS_send_notify_new_successor (source, destination, trail, 4016 GDS_NEIGHBOURS_send_notify_new_successor (source, new_successor, trail,
3138 trail_length, 4017 trail_length,
3139 trail_id, target_friend); 4018 trail_id, target_friend);
3140 return GNUNET_OK; 4019 return GNUNET_OK;
@@ -3165,7 +4044,7 @@ handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer
3165 struct GNUNET_PeerIdentity source; 4044 struct GNUNET_PeerIdentity source;
3166 struct GNUNET_PeerIdentity *next_hop; 4045 struct GNUNET_PeerIdentity *next_hop;
3167 uint64_t ultimate_destination_finger_value; 4046 uint64_t ultimate_destination_finger_value;
3168 unsigned int finger_map_index; 4047 unsigned int is_predecessor;
3169 size_t msize; 4048 size_t msize;
3170 4049
3171 msize = ntohs (message->size); 4050 msize = ntohs (message->size);
@@ -3176,24 +4055,22 @@ handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer
3176 } 4055 }
3177 4056
3178 trail_rejection = (struct PeerTrailRejectionMessage *) message; 4057 trail_rejection = (struct PeerTrailRejectionMessage *) message;
3179 trail_length = ntohl (trail_rejection->trail_length); 4058 trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
3180 4059 sizeof (struct GNUNET_PeerIdentity);
3181 if ((msize != sizeof (struct PeerTrailRejectionMessage) + 4060 if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
3182 trail_length * sizeof (struct GNUNET_PeerIdentity)) || 4061 sizeof (struct GNUNET_PeerIdentity) != 0)
3183 (trail_length >
3184 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3185 { 4062 {
3186 GNUNET_break_op (0); 4063 GNUNET_break_op (0);
3187 return GNUNET_YES; 4064 return GNUNET_OK;
3188 } 4065 }
3189 4066
3190 trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_rejection[1]; 4067 trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_rejection[1];
3191 finger_map_index = ntohl (trail_rejection->finger_map_index); 4068 is_predecessor = ntohl (trail_rejection->is_predecessor);
3192 congestion_timeout = trail_rejection->congestion_time; 4069 congestion_timeout = trail_rejection->congestion_time;
3193 source = trail_rejection->source_peer; 4070 source = trail_rejection->source_peer;
3194 trail_id = trail_rejection->trail_id; 4071 trail_id = trail_rejection->trail_id;
3195 ultimate_destination_finger_value = 4072 ultimate_destination_finger_value =
3196 trail_rejection->ultimate_destination_finger_identity_value; 4073 trail_rejection->ultimate_destination_finger_value;
3197 4074
3198 /* First set the congestion time of the friend that sent you this message. */ 4075 /* First set the congestion time of the friend that sent you this message. */
3199 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer); 4076 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
@@ -3229,7 +4106,7 @@ handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer
3229 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop); 4106 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
3230 GDS_NEIGHBOURS_send_trail_rejection (source, 4107 GDS_NEIGHBOURS_send_trail_rejection (source,
3231 ultimate_destination_finger_value, 4108 ultimate_destination_finger_value,
3232 my_identity, finger_map_index, 4109 my_identity, is_predecessor,
3233 new_trail,new_trail_length,trail_id, 4110 new_trail,new_trail_length,trail_id,
3234 target_friend, CONGESTION_TIMEOUT); 4111 target_friend, CONGESTION_TIMEOUT);
3235 return GNUNET_YES; 4112 return GNUNET_YES;
@@ -3239,7 +4116,7 @@ handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer
3239 next_hop = find_successor (ultimate_destination_finger_value, 4116 next_hop = find_successor (ultimate_destination_finger_value,
3240 &next_destination, 4117 &next_destination,
3241 &new_intermediate_trail_id, 4118 &new_intermediate_trail_id,
3242 finger_map_index); 4119 is_predecessor);
3243 4120
3244 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This means I am the final destination */ 4121 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This means I am the final destination */
3245 { 4122 {
@@ -3253,7 +4130,9 @@ handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer
3253 my_identity, 4130 my_identity,
3254 target_friend, trail_length, 4131 target_friend, trail_length,
3255 trail_peer_list, 4132 trail_peer_list,
3256 finger_map_index, trail_id); 4133 is_predecessor,
4134 ultimate_destination_finger_value,
4135 trail_id);
3257 } 4136 }
3258 else 4137 else
3259 { 4138 {
@@ -3267,7 +4146,7 @@ handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *peer
3267 ultimate_destination_finger_value, 4146 ultimate_destination_finger_value,
3268 next_destination, 4147 next_destination,
3269 target_friend, trail_length + 1, peer_list, 4148 target_friend, trail_length + 1, peer_list,
3270 finger_map_index, trail_id, 4149 is_predecessor, trail_id,
3271 &new_intermediate_trail_id); 4150 &new_intermediate_trail_id);
3272 } 4151 }
3273 return GNUNET_OK; 4152 return GNUNET_OK;
@@ -3415,7 +4294,14 @@ handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
3415 } 4294 }
3416 4295
3417 add_trail = (struct PeerAddTrailMessage *) message; 4296 add_trail = (struct PeerAddTrailMessage *) message;
3418 trail_length = ntohl (add_trail->trail_length); 4297 trail_length = (msize - sizeof (struct PeerAddTrailMessage))/
4298 sizeof (struct GNUNET_PeerIdentity);
4299 if ((msize - sizeof (struct PeerAddTrailMessage)) %
4300 sizeof (struct GNUNET_PeerIdentity) != 0)
4301 {
4302 GNUNET_break_op (0);
4303 return GNUNET_OK;
4304 }
3419 4305
3420 if ((msize < sizeof (struct PeerAddTrailMessage) + 4306 if ((msize < sizeof (struct PeerAddTrailMessage) +
3421 trail_length * sizeof (struct GNUNET_PeerIdentity)) || 4307 trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
@@ -3480,7 +4366,7 @@ remove_matching_finger (void *cls,
3480{ 4366{
3481 struct FingerInfo *remove_finger = value; 4367 struct FingerInfo *remove_finger = value;
3482 const struct GNUNET_PeerIdentity *disconnected_peer = cls; 4368 const struct GNUNET_PeerIdentity *disconnected_peer = cls;
3483 struct TrailList *trail_list; 4369 struct Trail *trail_list;
3484 int i; 4370 int i;
3485 4371
3486 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity, 4372 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity,
diff --git a/src/dht/gnunet-service-xdht_neighbours.h b/src/dht/gnunet-service-xdht_neighbours.h
index a1a6ea564..75f1f2a67 100644
--- a/src/dht/gnunet-service-xdht_neighbours.h
+++ b/src/dht/gnunet-service-xdht_neighbours.h
@@ -32,18 +32,19 @@
32#include "gnunet_dht_service.h" 32#include "gnunet_dht_service.h"
33 33
34 34
35/** 35/**
36 * Construct a Put message and send it to target_peer. 36 * Construct a Put message and send it to target_peer.
37 * @param key Key for the content 37 * @param key Key for the content
38 * @param block_type Type of the block 38 * @param block_type Type of the block
39 * @param options Routing options 39 * @param options Routing options
40 * @param desired_replication_level Desired replication count 40 * @param desired_replication_level Desired replication count
41 * @param current_destination Next current destination which will get this message. 41 * @param best_known_dest Peer to which this message should reach eventually,
42 * @param current_source Source for @a current_destination 42 * as it is best known destination to me.
43 * @param intermediate_trail_id Trail id in case
43 * @param target_peer Peer to which this message will be forwarded. 44 * @param target_peer Peer to which this message will be forwarded.
44 * @param hop_count Number of hops traversed so far. 45 * @param hop_count Number of hops traversed so far.
45 * @param put_path_length Total number of peers in @a put_path 46 * @param put_path_length Total number of peers in @a put_path
46 * @param put_path Number of peers traversed so far 47 * @param put_path Number of peers traversed so far
47 * @param expiration_time When does the content expire 48 * @param expiration_time When does the content expire
48 * @param data Content to store 49 * @param data Content to store
49 * @param data_size Size of content @a data in bytes 50 * @param data_size Size of content @a data in bytes
@@ -51,11 +52,11 @@
51void 52void
52GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key, 53GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
53 enum GNUNET_BLOCK_Type block_type, 54 enum GNUNET_BLOCK_Type block_type,
54 enum GNUNET_DHT_RouteOption options, 55 enum GNUNET_DHT_RouteOption options,
55 uint32_t desired_replication_level, 56 uint32_t desired_replication_level,
56 struct GNUNET_PeerIdentity current_destination, 57 struct GNUNET_PeerIdentity *best_known_dest,
57 struct GNUNET_PeerIdentity current_source, 58 struct GNUNET_HashCode *intermediate_trail_id,
58 struct GNUNET_PeerIdentity *target_peer, 59 struct GNUNET_PeerIdentity *target_peer,
59 uint32_t hop_count, 60 uint32_t hop_count,
60 uint32_t put_path_length, 61 uint32_t put_path_length,
61 struct GNUNET_PeerIdentity *put_path, 62 struct GNUNET_PeerIdentity *put_path,
@@ -63,14 +64,14 @@ GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
63 const void *data, size_t data_size); 64 const void *data, size_t data_size);
64 65
65 66
66/** 67/**
67 * Construct a Get message and send it to target_peer. 68 * Construct a Get message and send it to target_peer.
68 * @param key Key for the content 69 * @param key Key for the content
69 * @param block_type Type of the block 70 * @param block_type Type of the block
70 * @param options Routing options 71 * @param options Routing options
71 * @param desired_replication_level Desired replication count 72 * @param desired_replication_level Desired replication count
72 * @param current_destination Next current destination which will get this message. 73 * @param best_known_dest
73 * @param current_source Source for @a current_destination 74 * @param intermediate_trail_id
74 * @param target_peer Peer to which this message will be forwarded. 75 * @param target_peer Peer to which this message will be forwarded.
75 * @param hop_count Number of hops traversed so far. 76 * @param hop_count Number of hops traversed so far.
76 * @param data Content to store 77 * @param data Content to store
@@ -83,8 +84,8 @@ GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
83 enum GNUNET_BLOCK_Type block_type, 84 enum GNUNET_BLOCK_Type block_type,
84 enum GNUNET_DHT_RouteOption options, 85 enum GNUNET_DHT_RouteOption options,
85 uint32_t desired_replication_level, 86 uint32_t desired_replication_level,
86 struct GNUNET_PeerIdentity current_destination, 87 struct GNUNET_PeerIdentity *best_known_dest,
87 struct GNUNET_PeerIdentity current_source, 88 struct GNUNET_HashCode *intermediate_trail_id,
88 struct GNUNET_PeerIdentity *target_peer, 89 struct GNUNET_PeerIdentity *target_peer,
89 uint32_t hop_count, 90 uint32_t hop_count,
90 uint32_t get_path_length, 91 uint32_t get_path_length,