aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-service-xdht_neighbours.c
diff options
context:
space:
mode:
authorSupriti Singh <supritisingh08@gmail.com>2014-07-16 15:40:07 +0000
committerSupriti Singh <supritisingh08@gmail.com>2014-07-16 15:40:07 +0000
commitdca438751efb796b9aec4fc5cbfe6d2a93af8d59 (patch)
tree25ffd6fe587e8cd2df70c4ca01aa525cf52789d2 /src/dht/gnunet-service-xdht_neighbours.c
parentb4a203c2fd2cdebc74c37215455ec2545c8bc99e (diff)
downloadgnunet-dca438751efb796b9aec4fc5cbfe6d2a93af8d59.tar.gz
gnunet-dca438751efb796b9aec4fc5cbfe6d2a93af8d59.zip
code cleanup
Diffstat (limited to 'src/dht/gnunet-service-xdht_neighbours.c')
-rw-r--r--src/dht/gnunet-service-xdht_neighbours.c1244
1 files changed, 87 insertions, 1157 deletions
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c
index ffe8642d2..a1b892ff7 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 * FIXME: 48 * TODO:
49 * 1. In X-Vine paper, there is no policy defined for replicating the data to 49 * 1. In X-Vine paper, there is no policy defined for replicating the data to
50 * recover in case of peer failure. We can do it in Chord way. In R5N, the key 50 * recover in case of peer failure. We can do it in Chord way. In R5N, the key
51 * is hashed and then data is stored according to the key value generated after 51 * is hashed and then data is stored according to the key value generated after
@@ -1040,84 +1040,6 @@ GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
1040} 1040}
1041 1041
1042 1042
1043#if 0
1044/**
1045 * Construct a trail setup message and forward it to target_friend
1046 * @param source_peer Peer which wants to setup the trail
1047 * @param ultimate_destination_finger_value Peer identity closest to this value
1048 * will be finger to @a source_peer
1049 * @param best_known_destination Best known destination (could be finger or friend)
1050 * which should get this message. In case it is
1051 * friend, then it is same as target_friend
1052 * @param target_friend Friend to which message is forwarded now.
1053 * @param trail_length Total number of peers in trail setup so far.
1054 * @param trail_peer_list Trail setup so far
1055 * @param is_predecessor Is @a source_peer looking for trail to a predecessor or not.
1056 * @param trail_id Unique identifier for the trail we are trying to setup.
1057 * @param intermediate_trail_id Trail id of intermediate trail to reach to
1058 * best_known_destination when its a finger. If not
1059 * used then set to 0.
1060 */
1061void
1062GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
1063 uint64_t ultimate_destination_finger_value,
1064 struct GNUNET_PeerIdentity best_known_destination,
1065 struct FriendInfo *target_friend,
1066 unsigned int trail_length,
1067 const struct GNUNET_PeerIdentity *trail_peer_list,
1068 unsigned int is_predecessor,
1069 struct GNUNET_HashCode trail_id,
1070 struct GNUNET_HashCode *intermediate_trail_id)
1071{
1072 struct P2PPendingMessage *pending;
1073 struct PeerTrailSetupMessage *tsm;
1074 struct GNUNET_PeerIdentity *peer_list;
1075 size_t msize;
1076
1077 msize = sizeof (struct PeerTrailSetupMessage) +
1078 (trail_length * sizeof (struct GNUNET_PeerIdentity));
1079
1080 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1081 {
1082 GNUNET_break (0);
1083 return;
1084 }
1085
1086 if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
1087 {
1088 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1089 1, GNUNET_NO);
1090 }
1091
1092 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1093 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1094 tsm = (struct PeerTrailSetupMessage *) &pending[1];
1095 pending->msg = &tsm->header;
1096 tsm->header.size = htons (msize);
1097 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP);
1098 tsm->final_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
1099 tsm->source_peer = source_peer;
1100 tsm->best_known_destination = best_known_destination;
1101 tsm->is_predecessor = htonl (is_predecessor);
1102 tsm->trail_id = trail_id;
1103
1104 if (NULL == intermediate_trail_id)
1105 memset (&tsm->intermediate_trail_id, 0, sizeof (tsm->intermediate_trail_id));
1106 else
1107 tsm->intermediate_trail_id = *intermediate_trail_id;
1108
1109 if (trail_length > 0)
1110 {
1111 peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
1112 memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
1113 }
1114
1115 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
1116 target_friend->pending_count++;
1117 process_friend_queue (target_friend);
1118}
1119#endif
1120
1121/** 1043/**
1122 * Construct a trail setup result message and forward it to target friend. 1044 * Construct a trail setup result message and forward it to target friend.
1123 * @param querying_peer Peer which sent the trail setup request and should get 1045 * @param querying_peer Peer which sent the trail setup request and should get
@@ -1847,6 +1769,7 @@ test_finger_table_print()
1847 } 1769 }
1848 } 1770 }
1849 } 1771 }
1772
1850} 1773}
1851#endif 1774#endif
1852 1775
@@ -2025,54 +1948,6 @@ compare_finger_and_current_successor (struct Closest_Peer *current_closest_peer)
2025 } 1948 }
2026} 1949}
2027 1950
2028#if 0
2029/**
2030 * Compare friend entry with current successor.
2031 * If friend identity and current_successor is same, then do nothing.
2032 * If friend is not congested and has not crossed trail threshold, then check
2033 * if friend peer identity is closer to final_destination_finger_value than
2034 * current_successor. If yes then update current_successor.
2035 * @param cls closure
2036 * @param key current public key
2037 * @param value struct Closest_Peer
2038 * @return #GNUNET_YES if we should continue to iterate,
2039 * #GNUNET_NO if not.
2040 */
2041static int
2042compare_friend_and_current_closest_peer (void *cls,
2043 const struct GNUNET_PeerIdentity *key,
2044 void *value)
2045{
2046 struct FriendInfo *friend = value;
2047 struct Closest_Peer *current_closest_peer = cls;
2048 struct GNUNET_PeerIdentity *closest_peer;
2049
2050 /* If friend is either congested or has crossed threshold, then don't consider
2051 * this friend.*/
2052 if (GNUNET_YES == is_friend_congested (friend))
2053 return GNUNET_YES;
2054
2055 /* If current_closest_peer and friend identity are same, then do nothing.*/
2056 if (0 ==
2057 GNUNET_CRYPTO_cmp_peer_identity (&friend->id,
2058 &current_closest_peer->best_known_destination))
2059 return GNUNET_YES;
2060
2061 closest_peer = select_closest_peer (&friend->id,
2062 &current_closest_peer->best_known_destination,
2063 current_closest_peer->destination_finger_value,
2064 current_closest_peer->is_predecessor);
2065
2066 /* Is friend the closest successor? */
2067 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id, closest_peer))
2068 {
2069 current_closest_peer->best_known_destination = friend->id;
2070 current_closest_peer->next_hop = friend->id;
2071 }
2072
2073 return GNUNET_YES;
2074}
2075#endif
2076 1951
2077 1952
2078/** 1953/**
@@ -2123,32 +1998,6 @@ compare_friend_and_current_closest_peer (void *cls,
2123} 1998}
2124 1999
2125 2000
2126#if 0
2127/**
2128 * Initialize current_successor to my_identity.
2129 * @param my_identity My peer identity
2130 * @return current_successor
2131 */
2132static struct Closest_Peer *
2133init_current_successor (struct GNUNET_PeerIdentity my_identity,
2134 uint64_t destination_finger_value,
2135 unsigned int is_predecessor)
2136{
2137 struct Closest_Peer *current_closest_peer;
2138
2139 current_closest_peer = GNUNET_new (struct Closest_Peer);
2140 //memset (&current_closest_peer->trail_id, 0, sizeof (current_closest_peer->trail_id));
2141 current_closest_peer->trail_id = NULL;
2142 current_closest_peer->destination_finger_value = destination_finger_value;
2143 current_closest_peer->is_predecessor = is_predecessor;
2144 current_closest_peer->next_hop = my_identity;
2145 current_closest_peer->best_known_destination = my_identity;
2146
2147 return current_closest_peer;
2148}
2149#endif
2150
2151
2152/** 2001/**
2153 * Initialize current_successor to my_identity. 2002 * Initialize current_successor to my_identity.
2154 * @param my_identity My peer identity 2003 * @param my_identity My peer identity
@@ -2207,64 +2056,6 @@ find_successor (uint64_t destination_finger_value,
2207 return current_closest_peer; 2056 return current_closest_peer;
2208} 2057}
2209 2058
2210#if 0
2211/**
2212 * Find the successor for destination_finger_value among my_identity, all my
2213 * friend and all my fingers. Don't consider friends or fingers which are either
2214 * congested or have crossed the threshold.
2215 * NOTE: In case a friend is also a finger, then it is always chosen as friend
2216 * not a finger.
2217 * @param destination_finger_value Peer closest to this value will be the next successor.
2218 * @param local_best_known_destination[out] Updated to my_identity if I am the
2219 * final destination. Updated to friend
2220 * identity in case a friend is successor,
2221 * updated to finger in case finger is the
2222 * destination.
2223 * @param new_intermediate_trail_id[out] In case a finger is the
2224 * @a local_best_known_destination,
2225 * then it is the trail to reach it. Else
2226 * default set to 0.
2227 * @param is_predecessor Are we looking for predecessor or finger?
2228 * @return Next hop to reach to local_best_known_destination. In case my_identity
2229 * or a friend is a local_best_known_destination, then
2230 * next_hop = local_best_known_destination. Else
2231 * next_hop is the first friend to reach to finger (local_best_known_destination)
2232 */
2233static struct GNUNET_PeerIdentity *
2234find_successor (uint64_t destination_finger_value,
2235 struct GNUNET_PeerIdentity *local_best_known_destination,
2236 struct GNUNET_HashCode *new_intermediate_trail_id,
2237 unsigned int is_predecessor)
2238{
2239 struct Closest_Peer *current_closest_peer;
2240 struct GNUNET_PeerIdentity *next_hop;
2241
2242 /* Initialize current_successor to my_identity. */
2243 current_closest_peer = init_current_successor (my_identity,
2244 destination_finger_value,
2245 is_predecessor);
2246 /* Compare each friend entry with current_successor and update current_successor
2247 * with friend if its closest. */
2248 GNUNET_assert
2249 (GNUNET_SYSERR !=
2250 GNUNET_CONTAINER_multipeermap_iterate (friend_peermap,
2251 &compare_friend_and_current_closest_peer,
2252 current_closest_peer));
2253
2254 /* Compare each finger entry with current_successor and update current_successor
2255 * with finger if its closest. */
2256 compare_finger_and_current_successor (current_closest_peer);
2257 *local_best_known_destination = current_closest_peer->best_known_destination;
2258 if (current_closest_peer->trail_id != NULL)
2259 {
2260 /* FIXME I was assigning values but if address is NULL, then how do we assing. */
2261 memcpy (new_intermediate_trail_id,current_closest_peer->trail_id,sizeof (struct GNUNET_HashCode));
2262 }
2263 /* FIXME: what value do we pass in case it it NULL. TRAIL _ID */
2264 return &current_closest_peer->next_hop;
2265}
2266#endif
2267
2268 2059
2269/** 2060/**
2270 * Construct a Put message and send it to target_peer. 2061 * Construct a Put message and send it to target_peer.
@@ -2381,145 +2172,6 @@ GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
2381} 2172}
2382 2173
2383 2174
2384#if 0
2385/**
2386 * Construct a Put message and send it to target_peer.
2387 * @param key Key for the content
2388 * @param block_type Type of the block
2389 * @param options Routing options
2390 * @param desired_replication_level Desired replication count
2391 * @param best_known_dest Peer to which this message should reach eventually,
2392 * as it is best known destination to me.
2393 * @param intermediate_trail_id Trail id in case
2394 * @param target_peer Peer to which this message will be forwarded.
2395 * @param hop_count Number of hops traversed so far.
2396 * @param put_path_length Total number of peers in @a put_path
2397 * @param put_path Number of peers traversed so far
2398 * @param expiration_time When does the content expire
2399 * @param data Content to store
2400 * @param data_size Size of content @a data in bytes
2401 */
2402void
2403GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
2404 enum GNUNET_BLOCK_Type block_type,
2405 enum GNUNET_DHT_RouteOption options,
2406 uint32_t desired_replication_level,
2407 struct GNUNET_PeerIdentity *best_known_dest,
2408 struct GNUNET_HashCode *intermediate_trail_id,
2409 struct GNUNET_PeerIdentity *target_peer,
2410 uint32_t hop_count,
2411 uint32_t put_path_length,
2412 struct GNUNET_PeerIdentity *put_path,
2413 struct GNUNET_TIME_Absolute expiration_time,
2414 const void *data, size_t data_size)
2415{
2416 struct PeerPutMessage *ppm;
2417 struct P2PPendingMessage *pending;
2418 struct FriendInfo *target_friend;
2419 struct GNUNET_PeerIdentity *pp;
2420 struct GNUNET_PeerIdentity local_best_known_dest;
2421 struct GNUNET_HashCode *inter_trail_id;
2422 size_t msize;
2423
2424 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
2425 sizeof (struct PeerPutMessage);
2426
2427 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2428 {
2429 put_path_length = 0;
2430 msize = data_size + sizeof (struct PeerPutMessage);
2431 }
2432
2433 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2434 {
2435 GNUNET_break (0);
2436 return;
2437 }
2438
2439 test_friend_peermap_print();
2440 test_finger_table_print();
2441
2442 inter_trail_id = GNUNET_new (struct GNUNET_HashCode);
2443
2444 if (NULL != intermediate_trail_id)
2445 {
2446 *inter_trail_id = *intermediate_trail_id;
2447 }
2448
2449 /* This is the first call made from clients file. So, we should search for the
2450 target_friend. */
2451 if (NULL == target_peer)
2452 {
2453 uint64_t key_value;
2454 struct GNUNET_PeerIdentity next_hop;
2455
2456 memcpy (&key_value, key, sizeof (uint64_t));
2457 key_value = GNUNET_ntohll (key_value);
2458
2459 /* FIXME: Check that intermediate trail id is set properly. */
2460#if 0
2461 next_hop = find_successor (key_value, &local_best_known_dest,
2462 inter_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
2463#endif
2464 struct Closest_Peer successor = find_successor (key_value,
2465 GDS_FINGER_TYPE_NON_PREDECESSOR);
2466 local_best_known_dest = successor.best_known_destination;
2467 next_hop = successor.next_hop;
2468 inter_trail_id = successor.trail_id;
2469 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&local_best_known_dest, &my_identity))
2470 {
2471 /* I am the destination but we have already done datacache_put in client file. */
2472 return;
2473 }
2474 else
2475 GNUNET_assert (NULL !=
2476 (target_friend =
2477 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
2478 //GNUNET_free (next_hop);
2479 }
2480 else
2481 {
2482 GNUNET_assert (NULL !=
2483 (target_friend =
2484 GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
2485 }
2486
2487 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2488 pending->timeout = expiration_time;
2489 ppm = (struct PeerPutMessage *) &pending[1];
2490 pending->msg = &ppm->header;
2491 ppm->header.size = htons (msize);
2492 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT);
2493 ppm->options = htonl (options);
2494 ppm->block_type = htonl (block_type);
2495 ppm->hop_count = htonl (hop_count + 1);
2496 ppm->desired_replication_level = htonl (desired_replication_level);
2497 ppm->put_path_length = htonl (put_path_length);
2498 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
2499 if (NULL == best_known_dest)
2500 ppm->best_known_destination = local_best_known_dest;
2501 else
2502 ppm->best_known_destination = *best_known_dest;
2503 ppm->key = *key;
2504 if (NULL == inter_trail_id)
2505 memset (&ppm->intermediate_trail_id, 0, sizeof (ppm->intermediate_trail_id));
2506 else
2507 ppm->intermediate_trail_id = *inter_trail_id;
2508 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
2509 if (put_path_length != 0)
2510 {
2511 memcpy (pp, put_path,
2512 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
2513 }
2514 memcpy (&pp[put_path_length], data, data_size);
2515
2516 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2517 target_friend->pending_count++;
2518 process_friend_queue (target_friend);
2519}
2520#endif
2521
2522
2523/** 2175/**
2524 * Construct a Get message and send it to target_peer. 2176 * Construct a Get message and send it to target_peer.
2525 * @param key Key for the content 2177 * @param key Key for the content
@@ -2629,136 +2281,6 @@ GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
2629} 2281}
2630 2282
2631 2283
2632#if 0
2633/**
2634 * Construct a Get message and send it to target_peer.
2635 * @param key Key for the content
2636 * @param block_type Type of the block
2637 * @param options Routing options
2638 * @param desired_replication_level Desired replication count
2639 * @param best_known_dest Peer which should get this message. Same as target peer
2640 * if best_known_dest is a friend else its a finger.
2641 * @param intermediate_trail_id Trail id to reach to @a best_known_dest
2642 * in case it is a finger else set to 0.
2643 * @param target_peer Peer to which this message will be forwarded.
2644 * @param hop_count Number of hops traversed so far.
2645 * @param data Content to store
2646 * @param data_size Size of content @a data in bytes
2647 * @param get_path_length Total number of peers in @a get_path
2648 * @param get_path Number of peers traversed so far
2649 */
2650void
2651GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
2652 enum GNUNET_BLOCK_Type block_type,
2653 enum GNUNET_DHT_RouteOption options,
2654 uint32_t desired_replication_level,
2655 const struct GNUNET_PeerIdentity *best_known_dest,
2656 struct GNUNET_HashCode *intermediate_trail_id,
2657 struct GNUNET_PeerIdentity *target_peer,
2658 uint32_t hop_count,
2659 uint32_t get_path_length,
2660 struct GNUNET_PeerIdentity *get_path)
2661{
2662 struct PeerGetMessage *pgm;
2663 struct P2PPendingMessage *pending;
2664 struct FriendInfo *target_friend;
2665 struct GNUNET_PeerIdentity local_best_known_dest;
2666 struct GNUNET_PeerIdentity *gp;
2667 struct GNUNET_HashCode *inter_trail_id;
2668 size_t msize;
2669
2670 msize = sizeof (struct PeerGetMessage) +
2671 (get_path_length * sizeof (struct GNUNET_PeerIdentity));
2672
2673 /* In this case we don't make get_path_length = 0, as we need get path to
2674 * return the message back to querying client. */
2675 if (msize > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2676 {
2677 GNUNET_break (0);
2678 return;
2679 }
2680
2681 test_friend_peermap_print();
2682 test_finger_table_print();
2683 inter_trail_id = GNUNET_new (struct GNUNET_HashCode);
2684 if (NULL != intermediate_trail_id)
2685 {
2686
2687 *inter_trail_id = *intermediate_trail_id;
2688 }
2689 /* This is the first time we got request from our own client file. */
2690 if (NULL == target_peer)
2691 {
2692 struct GNUNET_PeerIdentity next_hop;
2693 uint64_t key_value;
2694
2695 memcpy (&key_value, key, sizeof (uint64_t));
2696 key_value = GNUNET_ntohll (key_value);
2697
2698 /* Find the next destination to forward the packet. */
2699#if 0
2700 next_hop = find_successor (key_value, &local_best_known_dest,
2701 inter_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
2702#endif
2703 struct Closest_Peer successor = find_successor (key_value,
2704 GDS_FINGER_TYPE_NON_PREDECESSOR);
2705 local_best_known_dest = successor.best_known_destination;
2706 inter_trail_id = successor.trail_id;
2707 next_hop = successor.next_hop;
2708 /* I am the destination. I have the data. */
2709 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
2710 &local_best_known_dest))
2711 {
2712 GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
2713 NULL, 0, 1, &my_identity, NULL,&my_identity);
2714
2715 return;
2716 }
2717 else
2718 {
2719 GNUNET_assert (NULL !=
2720 (target_friend =
2721 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
2722 }
2723 //GNUNET_free (next_hop);
2724 }
2725 else
2726 {
2727 local_best_known_dest = *best_known_dest;
2728 GNUNET_assert (NULL !=
2729 (target_friend =
2730 GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
2731 }
2732
2733 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
2734 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
2735 pending->importance = 0; /* FIXME */
2736 pgm = (struct PeerGetMessage *) &pending[1];
2737 pending->msg = &pgm->header;
2738 pgm->header.size = htons (msize);
2739 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET);
2740 pgm->get_path_length = htonl (get_path_length);
2741 pgm->best_known_destination = local_best_known_dest;
2742 pgm->key = *key;
2743
2744 if (NULL == inter_trail_id)
2745 memset ((void *)&pgm->intermediate_trail_id, 0, sizeof (pgm->intermediate_trail_id));
2746 else
2747 pgm->intermediate_trail_id = *inter_trail_id;
2748 pgm->hop_count = htonl (hop_count + 1);
2749 gp = (struct GNUNET_PeerIdentity *) &pgm[1];
2750
2751 if (get_path_length != 0)
2752 {
2753 memcpy (gp, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
2754 }
2755
2756 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
2757 target_friend->pending_count++;
2758 process_friend_queue (target_friend);
2759}
2760#endif
2761
2762/** 2284/**
2763 * Send the get result to requesting client. 2285 * Send the get result to requesting client.
2764 * @param key Key of the requested data. 2286 * @param key Key of the requested data.
@@ -3718,7 +3240,6 @@ remove_existing_finger (struct FingerInfo *existing_finger,
3718 3240
3719 3241
3720/** 3242/**
3721 * FIXME: Check for memory leaks.
3722 * Check if there is already an entry in finger_table at finger_table_index. 3243 * Check if there is already an entry in finger_table at finger_table_index.
3723 * We get the finger_table_index from 64bit finger value we got from the network. 3244 * We get the finger_table_index from 64bit finger value we got from the network.
3724 * -- If yes, then select the closest finger. 3245 * -- If yes, then select the closest finger.
@@ -3763,8 +3284,7 @@ finger_table_add (struct GNUNET_PeerIdentity finger_identity,
3763 return; 3284 return;
3764 } 3285 }
3765 3286
3766 //test_finger_table_print(); 3287
3767 //test_friend_peermap_print();
3768 //GDS_ROUTING_test_print(); 3288 //GDS_ROUTING_test_print();
3769 /* If the new entry for any finger table index other than successor or predecessor 3289 /* If the new entry for any finger table index other than successor or predecessor
3770 * is same as successor then don't add it in finger table, 3290 * is same as successor then don't add it in finger table,
@@ -4050,185 +3570,6 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
4050 } 3570 }
4051 return GNUNET_OK; 3571 return GNUNET_OK;
4052} 3572}
4053#if 0
4054/**
4055 * Core handler for P2P put messages.
4056 * @param cls closure
4057 * @param peer sender of the request
4058 * @param message message
4059 * @return #GNUNET_OK to keep the connection open,
4060 * #GNUNET_SYSERR to close it (signal serious error)
4061 */
4062static int
4063handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
4064 const struct GNUNET_MessageHeader *message)
4065{
4066 struct PeerPutMessage *put;
4067 struct GNUNET_PeerIdentity *put_path;
4068 struct GNUNET_HashCode test_key;
4069 enum GNUNET_DHT_RouteOption options;
4070 struct GNUNET_PeerIdentity best_known_dest;
4071 struct GNUNET_HashCode intermediate_trail_id;
4072 struct GNUNET_PeerIdentity *next_hop;
4073 void *payload;
4074 size_t msize;
4075 uint32_t putlen;
4076 size_t payload_size;
4077 uint64_t key_value;
4078
4079 msize = ntohs (message->size);
4080 if (msize < sizeof (struct PeerPutMessage))
4081 {
4082 GNUNET_break_op (0);
4083 return GNUNET_YES;
4084 }
4085
4086 put = (struct PeerPutMessage *) message;
4087 putlen = ntohl (put->put_path_length);
4088
4089 if ((msize <
4090 sizeof (struct PeerPutMessage) +
4091 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
4092 (putlen >
4093 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
4094 {
4095 GNUNET_break_op (0);
4096 return GNUNET_YES;
4097 }
4098
4099 best_known_dest = put->best_known_destination;
4100 put_path = (struct GNUNET_PeerIdentity *) &put[1];
4101 payload = &put_path[putlen];
4102 options = ntohl (put->options);
4103 intermediate_trail_id = put->intermediate_trail_id;
4104
4105 payload_size = msize - (sizeof (struct PeerPutMessage) +
4106 putlen * sizeof (struct GNUNET_PeerIdentity));
4107
4108 switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
4109 payload, payload_size, &test_key))
4110 {
4111 case GNUNET_YES:
4112 if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
4113 {
4114 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
4115 GNUNET_break_op (0);
4116 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4117 "PUT with key `%s' for block with key %s\n",
4118 put_s, GNUNET_h2s_full (&test_key));
4119 GNUNET_free (put_s);
4120 return GNUNET_YES;
4121 }
4122 break;
4123 case GNUNET_NO:
4124 GNUNET_break_op (0);
4125 return GNUNET_YES;
4126 case GNUNET_SYSERR:
4127 /* cannot verify, good luck */
4128 break;
4129 }
4130
4131 if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
4132 {
4133 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
4134 ntohl (put->block_type),
4135 NULL, /* query */
4136 NULL, 0, /* bloom filer */
4137 NULL, 0, /* xquery */
4138 payload, payload_size))
4139 {
4140 case GNUNET_BLOCK_EVALUATION_OK_MORE:
4141 case GNUNET_BLOCK_EVALUATION_OK_LAST:
4142 break;
4143
4144 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
4145 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
4146 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
4147 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
4148 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
4149 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
4150 default:
4151 GNUNET_break_op (0);
4152 return GNUNET_OK;
4153 }
4154 }
4155
4156 /* extend 'put path' by sender */
4157 struct GNUNET_PeerIdentity pp[putlen + 1];
4158 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
4159 {
4160 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
4161 pp[putlen] = *peer;
4162 putlen++;
4163 }
4164 else
4165 putlen = 0;
4166
4167 memcpy (&key_value, &(put->key), sizeof (uint64_t));
4168 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
4169 {
4170 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
4171 GDS_ROUTING_SRC_TO_DEST);
4172 }
4173 else
4174 {
4175 key_value = GNUNET_ntohll (key_value);
4176 //fixme: should intermediatE_trail_id be const. so it always points to same
4177 ///memory location but diffrentvalue.
4178#if 0
4179 next_hop = find_successor (key_value, &best_known_dest,
4180 &intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
4181#endif
4182 struct Closest_Peer successor = find_successor (key_value,
4183 GDS_FINGER_TYPE_NON_PREDECESSOR);
4184 *next_hop = successor.next_hop;
4185
4186 }
4187
4188#if 0
4189 if (NULL == next_hop)
4190 {
4191 GNUNET_STATISTICS_update (GDS_stats,
4192 gettext_noop ("# Next hop to forward the packet not found "
4193 "trail setup request, packet dropped."),
4194 1, GNUNET_NO);
4195 return GNUNET_SYSERR;
4196 }
4197#endif
4198 GDS_CLIENTS_process_put (options,
4199 ntohl (put->block_type),
4200 ntohl (put->hop_count),
4201 ntohl (put->desired_replication_level),
4202 putlen, pp,
4203 GNUNET_TIME_absolute_ntoh (put->expiration_time),
4204 &put->key,
4205 payload,
4206 payload_size);
4207
4208 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &best_known_dest)) /* I am the final destination */
4209 {
4210 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
4211 &(put->key),putlen, pp, ntohl (put->block_type),
4212 payload_size, payload);
4213 GNUNET_free_non_null (next_hop);
4214 return GNUNET_YES;
4215 }
4216 else
4217 {
4218 /* FIXME: check what value is being passed in intermediate_trail_id */
4219 GDS_NEIGHBOURS_send_put (&put->key,
4220 ntohl (put->block_type),ntohl (put->options),
4221 ntohl (put->desired_replication_level),
4222 &best_known_dest, &intermediate_trail_id, next_hop,
4223 ntohl (put->hop_count), putlen, pp,
4224 GNUNET_TIME_absolute_ntoh (put->expiration_time),
4225 payload, payload_size);
4226
4227 return GNUNET_YES;
4228 }
4229 return GNUNET_SYSERR;
4230}
4231#endif
4232 3573
4233 3574
4234/** 3575/**
@@ -4340,122 +3681,6 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
4340} 3681}
4341 3682
4342 3683
4343#if 0
4344/**
4345 * Core handler for p2p get requests.
4346 *
4347 * @param cls closure
4348 * @param peer sender of the request
4349 * @param message message
4350 * @return #GNUNET_OK to keep the connection open,
4351 * #GNUNET_SYSERR to close it (signal serious error)
4352 */
4353static int
4354handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
4355 const struct GNUNET_MessageHeader *message)
4356{
4357 const struct PeerGetMessage *get;
4358 const struct GNUNET_PeerIdentity *get_path;
4359 struct GNUNET_PeerIdentity best_known_dest;
4360 struct GNUNET_HashCode intermediate_trail_id;
4361 struct GNUNET_PeerIdentity *next_hop;
4362 uint32_t get_length;
4363 uint64_t key_value;
4364 size_t msize;
4365
4366 msize = ntohs (message->size);
4367 if (msize < sizeof (struct PeerGetMessage))
4368 {
4369 GNUNET_break_op (0);
4370 return GNUNET_YES;
4371 }
4372
4373 get = (const struct PeerGetMessage *)message;
4374 get_length = ntohl (get->get_path_length);
4375 best_known_dest = get->best_known_destination;
4376 intermediate_trail_id = get->intermediate_trail_id;
4377 get_path = (const struct GNUNET_PeerIdentity *)&get[1];
4378
4379 if ((msize <
4380 sizeof (struct PeerGetMessage) +
4381 get_length * sizeof (struct GNUNET_PeerIdentity)) ||
4382 (get_length >
4383 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
4384 {
4385 GNUNET_break_op (0);
4386 return GNUNET_YES;
4387 }
4388
4389 /* Add sender to get path */
4390 struct GNUNET_PeerIdentity gp[get_length + 1];
4391 if (get_length > 0)
4392 memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
4393 gp[get_length] = *peer;
4394 get_length = get_length + 1;
4395
4396 memcpy (&key_value, &(get->key), sizeof (uint64_t));
4397 key_value = GNUNET_ntohll (key_value);
4398
4399 /* I am not the final destination. I am part of trail to reach final dest. */
4400 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
4401 {
4402 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
4403 GDS_ROUTING_SRC_TO_DEST);
4404 }
4405 else
4406 {
4407 /* Get the next hop to pass the message to. */
4408#if 0
4409 next_hop = find_successor (key_value, &best_known_dest,
4410 &intermediate_trail_id, GDS_FINGER_TYPE_NON_PREDECESSOR);
4411#endif
4412 struct Closest_Peer successor = find_successor (key_value,
4413 GDS_FINGER_TYPE_NON_PREDECESSOR);
4414 *next_hop = successor.next_hop;
4415 }
4416
4417 if (NULL == next_hop)
4418 {
4419 GNUNET_STATISTICS_update (GDS_stats,
4420 gettext_noop ("# Next hop to forward the packet not found "
4421 "trail setup request, packet dropped."),
4422 1, GNUNET_NO);
4423 return GNUNET_SYSERR;
4424 }
4425 GDS_CLIENTS_process_get (get->options, get->block_type,get->hop_count,
4426 get->desired_replication_level, get->get_path_length,
4427 gp, &get->key);
4428
4429 /* I am the final destination. */
4430 if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &best_known_dest))
4431 {
4432 struct GNUNET_PeerIdentity final_get_path[get_length+1];
4433
4434 memcpy (final_get_path, gp, get_length * sizeof (struct GNUNET_PeerIdentity));
4435 memcpy (&final_get_path[get_length], &my_identity, sizeof (struct GNUNET_PeerIdentity));
4436 get_length = get_length + 1;
4437 /* FIXME: here it may happen that we find our identity closest to key, but
4438 we don't have the data. then in that case, we should forward the packet
4439 to the next closest peer. */
4440 GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
4441 get_length, final_get_path,
4442 &final_get_path[get_length-2], &my_identity);
4443
4444 return GNUNET_YES;
4445 }
4446 else
4447 {
4448 /* FIXME: check what value is being passed in intermediate_trail_id*/
4449 GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
4450 get->desired_replication_level, &best_known_dest,
4451 &intermediate_trail_id, next_hop, 0,
4452 get_length, gp);
4453 GNUNET_free (next_hop);
4454 }
4455 return GNUNET_SYSERR;
4456}
4457#endif
4458
4459/** 3684/**
4460 * Core handler for get result 3685 * Core handler for get result
4461 * @param cls closure 3686 * @param cls closure
@@ -4588,84 +3813,6 @@ get_local_best_known_next_hop (uint64_t final_dest_finger_val,
4588} 3813}
4589 3814
4590 3815
4591#if 0
4592/**
4593 * Get the best known next destination (local_dest) among your fingers, friends
4594 * and my identity. In case I was part of trail to reach to some other destination
4595 * (current_dest), then compare current_dest and local_dest, and choose the
4596 * closest peer.
4597 * @param final_dest_finger_value Peer closest to this value will be
4598 * @a local_best_known_dest
4599 * @param local_best_known_dest[out] Updated to peer identity which is closest to
4600 * @a final_dest_finger_value.
4601 * @param new_intermediate_trail_id In case @a local_best_known_dest is a finger,
4602 * then the trail id to reach to the finger
4603 * @param is_predecessor Is source peer trying to setup trail to its predecessor
4604 * or not.
4605 * @param current_dest Peer which should get this message ultimately according
4606 * to the peer which sent me this message. It could be me
4607 * or some other peer. In case it is not me, then I am a part
4608 * of trail to reach to that peer.
4609 * @return
4610 */
4611static struct GNUNET_PeerIdentity *
4612get_local_best_known_next_hop (uint64_t final_dest_finger_value,
4613 struct GNUNET_PeerIdentity *local_best_known_dest,
4614 struct GNUNET_HashCode *new_intermediate_trail_id,
4615 struct GNUNET_HashCode intermediate_trail_id,
4616 unsigned int is_predecessor,
4617 struct GNUNET_PeerIdentity *current_dest)
4618{
4619 struct GNUNET_PeerIdentity *next_hop_to_local_best_known_dest;
4620
4621 /* Choose a local best known hop among your fingers, friends and you. */
4622#if 0
4623 next_hop_to_local_best_known_dest = find_successor (final_dest_finger_value,
4624 local_best_known_dest,
4625 new_intermediate_trail_id,
4626 is_predecessor);
4627#endif
4628 struct Closest_Peer successor = find_successor(final_dest_finger_value, is_predecessor);
4629 /* Are we just a part of a trail towards a finger (current_destination)? */
4630 if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest)))
4631 {
4632 struct GNUNET_PeerIdentity *closest_peer;
4633
4634 /* Select best successor among one found locally and current_destination
4635 * that we got from network.*/
4636 if (0 != GNUNET_CRYPTO_cmp_peer_identity (local_best_known_dest, current_dest))
4637 {
4638 closest_peer = select_closest_peer (local_best_known_dest,
4639 current_dest,
4640 final_dest_finger_value,
4641 is_predecessor);
4642
4643 /* Is current dest (end point of the trail of which I am a part) closest_peer? */
4644 if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, closest_peer))
4645 {
4646 struct GNUNET_PeerIdentity *next_hop;
4647 next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
4648 GDS_ROUTING_SRC_TO_DEST);
4649 /* FIXME: Here we found next_hop NULL from routing table, but we still
4650 * have a next_hop from find_successor should we not break and choose that
4651 * next_hop. */
4652 if (NULL == next_hop)
4653 {
4654 GNUNET_break_op (0);
4655 return NULL;
4656 }
4657 next_hop_to_local_best_known_dest = next_hop;
4658 local_best_known_dest = current_dest;
4659 *new_intermediate_trail_id = intermediate_trail_id;
4660 }
4661 }
4662 }
4663
4664 GNUNET_assert (NULL != next_hop_to_local_best_known_dest);
4665 return next_hop_to_local_best_known_dest;
4666}
4667#endif
4668
4669/* 3816/*
4670 * Core handle for PeerTrailSetupMessage. 3817 * Core handle for PeerTrailSetupMessage.
4671 * @param cls closure 3818 * @param cls closure
@@ -4784,62 +3931,6 @@ handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
4784 next_peer.trail_id); 3931 next_peer.trail_id);
4785 3932
4786 } 3933 }
4787#if 0
4788 next_hop_towards_local_best_known_dest =
4789 get_local_best_known_next_hop (final_dest_finger_val,
4790 local_best_known_dest,
4791 new_intermediate_trail_id,
4792 intermediate_trail_id,
4793 is_predecessor,
4794 &current_dest);
4795
4796 GNUNET_assert (NULL != local_best_known_dest);
4797 GNUNET_assert (NULL != next_hop_towards_local_best_known_dest);
4798
4799
4800 /* Am I the final destination? */
4801 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (local_best_known_dest,
4802 &my_identity)))
4803 {
4804 /* If I was not the source of this message for which now I am destination */
4805 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
4806 {
4807 GDS_ROUTING_add (trail_id, *peer, my_identity);
4808 }
4809
4810 GNUNET_assert (NULL !=
4811 (target_friend =
4812 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
4813 GDS_NEIGHBOURS_send_trail_setup_result (source,
4814 my_identity,
4815 target_friend, trail_length,
4816 trail_peer_list,
4817 final_dest_finger_val,
4818 is_predecessor, trail_id);
4819 }
4820 else
4821 {
4822 /* Add yourself to list of peers. */
4823 struct GNUNET_PeerIdentity peer_list[trail_length + 1];
4824
4825 memcpy (peer_list, trail_peer_list,
4826 trail_length * sizeof (struct GNUNET_PeerIdentity));
4827 peer_list[trail_length] = my_identity;
4828 GNUNET_assert (NULL !=
4829 (target_friend =
4830 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
4831 next_hop_towards_local_best_known_dest)));
4832 /* FIXME; CHECK IF INTERMEDIATE TRAIL ID is 0 thne pas null. */
4833 /* URGENT TODO. */
4834 GDS_NEIGHBOURS_send_trail_setup (source,
4835 final_dest_finger_val,
4836 *local_best_known_dest,
4837 target_friend, trail_length + 1, peer_list,
4838 is_predecessor, trail_id,
4839 new_intermediate_trail_id);
4840 }
4841 GNUNET_free (local_best_known_dest);
4842#endif
4843 return GNUNET_OK; 3934 return GNUNET_OK;
4844} 3935}
4845 3936
@@ -5067,6 +4158,8 @@ invert_trail (const struct GNUNET_PeerIdentity *trail,
5067} 4158}
5068 4159
5069 4160
4161//FIXME: URGENT.
4162
5070/** 4163/**
5071 * Return the shortest trail to reach from me to my_predecessor. 4164 * Return the shortest trail to reach from me to my_predecessor.
5072 * @param current_trail Trail from source to me. 4165 * @param current_trail Trail from source to me.
@@ -5075,7 +4168,7 @@ invert_trail (const struct GNUNET_PeerIdentity *trail,
5075 * @return Updated trail from source peer to my_predecessor. 4168 * @return Updated trail from source peer to my_predecessor.
5076 */ 4169 */
5077static struct GNUNET_PeerIdentity * 4170static struct GNUNET_PeerIdentity *
5078trail_source_to_my_predecessor (const struct GNUNET_PeerIdentity *current_trail, 4171trail_me_to_my_predecessor (const struct GNUNET_PeerIdentity *current_trail,
5079 unsigned int current_trail_length, 4172 unsigned int current_trail_length,
5080 unsigned int *trail_length) 4173 unsigned int *trail_length)
5081{ 4174{
@@ -5282,29 +4375,6 @@ compare_and_update_predecessor (struct GNUNET_PeerIdentity finger,
5282 4375
5283 4376
5284/* 4377/*
5285 * FIXME: if i have no predecessor and I get a new predecessor but the first
5286 * friend to reach to that hop is congested then?
5287 * 1. check if you are the successor or not.
5288 * 2. if not then get the next hop from routing table, and pass the message,
5289 * 3. In case you are successor, then
5290 * 3.1 check if you have a predecessor
5291 * 3.2 if no predecessor, then add the source of this message as your
5292 * predecessor. To add, first you should generate a new trail id,
5293 * invert the trail, send add trail message across new trail, add
5294 * an entry in finger table. Now, destination also have routing
5295 * table entry so add in your routing table also.
5296 * 3.3 If its closer than existing one, then do everything in step 1 and
5297 * free existing finger.
5298 * 3.3 If its same as the old one, then do nothing.
5299 * 3.4 if its not same as old one, and between source and old one, old one
5300 * is the correct predecessor, then choose the shortest path to reach
5301 * from you to your predecessor. Pass this trail to source of this message.
5302 * It is the responsibility of source peer to scan the trail to reach to
5303 * its new probable successor.
5304 * 4. send verify successor result, with trail id of trail from source to
5305 * me. And also send the trail from me to reach to my predecessor, if
5306 * my_predecessor != source.
5307 *
5308 * Core handle for p2p verify successor messages. 4378 * Core handle for p2p verify successor messages.
5309 * @param cls closure 4379 * @param cls closure
5310 * @param message message 4380 * @param message message
@@ -5397,7 +4467,7 @@ handle_dht_p2p_verify_successor(void *cls,
5397 &current_predecessor->finger_identity))) 4467 &current_predecessor->finger_identity)))
5398 { 4468 {
5399 trail_to_predecessor = 4469 trail_to_predecessor =
5400 trail_source_to_my_predecessor (trail, 4470 trail_me_to_my_predecessor (trail,
5401 trail_length, 4471 trail_length,
5402 &trail_to_predecessor_length); 4472 &trail_to_predecessor_length);
5403 } 4473 }
@@ -5436,11 +4506,17 @@ handle_dht_p2p_verify_successor(void *cls,
5436static struct GNUNET_PeerIdentity * 4506static struct GNUNET_PeerIdentity *
5437get_trail_to_new_successor (struct FingerInfo *current_successor, 4507get_trail_to_new_successor (struct FingerInfo *current_successor,
5438 struct GNUNET_PeerIdentity probable_successor, 4508 struct GNUNET_PeerIdentity probable_successor,
5439 const struct GNUNET_PeerIdentity *trail, 4509 const struct GNUNET_PeerIdentity *trail_from_curr_to_probable_successor,
5440 unsigned int trail_length, 4510 unsigned int trail_from_curr_to_probable_successor_length,
5441 unsigned int *trail_to_new_successor_length) 4511 unsigned int *trail_to_new_successor_length)
5442{ 4512{
5443 struct GNUNET_PeerIdentity *trail_to_new_successor; 4513 struct GNUNET_PeerIdentity *trail_to_new_successor;
4514 unsigned int shortest_trail_length;
4515 unsigned int flag = 0;
4516 unsigned int shortest_trail_index;
4517 struct Trail *trail;
4518 struct Trail_Element *trail_element;
4519 int i;
5444 4520
5445 /* If the probable successor is a friend, then we don't need to have a trail 4521 /* If the probable successor is a friend, then we don't need to have a trail
5446 * to reach to it.*/ 4522 * to reach to it.*/
@@ -5462,7 +4538,73 @@ get_trail_to_new_successor (struct FingerInfo *current_successor,
5462 * 5. get the shortest trail. 4538 * 5. get the shortest trail.
5463 * 6. send back the trail. 4539 * 6. send back the trail.
5464 */ 4540 */
5465 return NULL; 4541
4542
4543 /* Choose the shortest trail to reach the current_successor */
4544 for (i = 0; i < current_successor->trails_count; i++)
4545 {
4546 trail = &current_successor->trail_list[i];
4547 if (0 == flag)
4548 {
4549 shortest_trail_index = i;
4550 shortest_trail_length = trail->trail_length;
4551 flag = 1;
4552 continue;
4553 }
4554
4555 if (shortest_trail_length > trail->trail_length)
4556 {
4557 shortest_trail_index = i;
4558 shortest_trail_length = trail->trail_length;
4559 }
4560 continue;
4561 }
4562
4563 /* It means that probable successor is the friend of current successor. */
4564 if (0 == trail_from_curr_to_probable_successor_length)
4565 {
4566 *trail_to_new_successor_length = shortest_trail_length + 1;
4567 trail_to_new_successor = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
4568 (*trail_to_new_successor_length));
4569 /* Copy the selected trail and send this trail to calling function. */
4570 i = 0;
4571 trail = &current_successor->trail_list[shortest_trail_index];
4572 trail_element = trail->trail_head;
4573 while ( i < shortest_trail_length)
4574 {
4575 trail_to_new_successor[i] = trail_element->peer;
4576 i++;
4577 trail_element = trail_element->next;
4578 }
4579
4580 trail_to_new_successor[i] = current_successor->finger_identity;
4581 }
4582 else
4583 {
4584 *trail_to_new_successor_length = shortest_trail_length +
4585 trail_from_curr_to_probable_successor_length;
4586 trail_to_new_successor = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
4587 (*trail_to_new_successor_length));
4588 /* Copy the selected trail and send this trail to calling function. */
4589 i = 0;
4590 trail = &current_successor->trail_list[shortest_trail_index];
4591 trail_element = trail->trail_head;
4592 while ( i < shortest_trail_length)
4593 {
4594 trail_to_new_successor[i] = trail_element->peer;
4595 i++;
4596 trail_element = trail_element->next;
4597 }
4598
4599 int j = 0;
4600 while (j < trail_from_curr_to_probable_successor_length)
4601 {
4602 trail_to_new_successor[i] = trail_from_curr_to_probable_successor[j];
4603 i++;
4604 j++;
4605 }
4606 }
4607 return trail_to_new_successor;
5466} 4608}
5467 4609
5468 4610
@@ -5560,15 +4702,6 @@ compare_and_update_successor (struct GNUNET_PeerIdentity probable_successor,
5560 4702
5561 4703
5562/* 4704/*
5563 * 1. If you are not the querying peer then pass on the message,
5564 * 2. If you are querying peer, then
5565 * 2.1 is new successor same as old one
5566 * 2.1.1 if yes then do noting
5567 * 2.1.2 if no then you need to notify the new one about your existence,
5568 * 2.1.2,1 also you need to remove the older successor, remove entry
5569 * from finger table, send trail teardown message across all the trail
5570 * of older successor. Call notify new successor with new trail id
5571 * and new trail to reach it.
5572 * Core handle for p2p verify successor result messages. 4705 * Core handle for p2p verify successor result messages.
5573 * @param cls closure 4706 * @param cls closure
5574 * @param message message 4707 * @param message message
@@ -5643,15 +4776,6 @@ handle_dht_p2p_verify_successor_result(void *cls,
5643 4776
5644 4777
5645/* 4778/*
5646 * Add entry in your routing table if source of the message is not a friend.
5647 * Irrespective if destination peer accepts source peer as predecessor or not,
5648 * we need to add an entry. So, that in next round to verify successor, source
5649 * is able to reach to its successor.
5650 * Check if you are the new successor of this message
5651 * 1. If yes the call function compare_and_update_successor(). This function
5652 * checks if source is real predecessor or not and take action accordingly.
5653 * 2. If not then find the next hop to find the message from the trail that
5654 * you got from the message and pass on the message.
5655 * Core handle for p2p notify new successor messages. 4779 * Core handle for p2p notify new successor messages.
5656 * @param cls closure 4780 * @param cls closure
5657 * @param message message 4781 * @param message message
@@ -5731,21 +4855,8 @@ handle_dht_p2p_notify_new_successor(void *cls,
5731 4855
5732} 4856}
5733 4857
4858
5734/** 4859/**
5735 * 1. Set the congestion timeout for the friend which is congested and sent
5736 * you this message.
5737 * 2. Check if you were the source of this message
5738 * 2.1 If yes then exit. find_finger_trail_task is scheduled periodically.
5739 * So, we will eventually send a new request to setup trail to finger.
5740 * 2. Check if you can handle more trails through you. (Routing table space)
5741 * 2.1 If not then you are congested. Set your congestion time and pass this
5742 * message to peer before you in the trail setup so far.
5743 * 2.2 If yes, the call find_successor(). It will give you the next hop to
5744 * pass this message.
5745 * 2.2.1 If you are the final destination, then send back the trail setup
5746 * result.
5747 * 2.2.2 If you are not the final dest, then send trail setup message to
5748 * next_hop.
5749 * Core handler for P2P trail rejection message 4860 * Core handler for P2P trail rejection message
5750 * @param cls closure 4861 * @param cls closure
5751 * @param message message 4862 * @param message message
@@ -5889,185 +5000,8 @@ handle_dht_p2p_trail_setup_rejection (void *cls,
5889 return GNUNET_OK; 5000 return GNUNET_OK;
5890} 5001}
5891 5002
5892#if 0
5893/**
5894 * 1. Set the congestion timeout for the friend which is congested and sent
5895 * you this message.
5896 * 2. Check if you were the source of this message
5897 * 2.1 If yes then exit. find_finger_trail_task is scheduled periodically.
5898 * So, we will eventually send a new request to setup trail to finger.
5899 * 2. Check if you can handle more trails through you. (Routing table space)
5900 * 2.1 If not then you are congested. Set your congestion time and pass this
5901 * message to peer before you in the trail setup so far.
5902 * 2.2 If yes, the call find_successor(). It will give you the next hop to
5903 * pass this message.
5904 * 2.2.1 If you are the final destination, then send back the trail setup
5905 * result.
5906 * 2.2.2 If you are not the final dest, then send trail setup message to
5907 * next_hop.
5908 * Core handler for P2P trail rejection message
5909 * @param cls closure
5910 * @param message message
5911 * @param peer peer identity this notification is about
5912 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
5913 */
5914static int
5915handle_dht_p2p_trail_setup_rejection (void *cls,
5916 const struct GNUNET_PeerIdentity *peer,
5917 const struct GNUNET_MessageHeader *message)
5918{
5919 const struct PeerTrailRejectionMessage *trail_rejection;
5920 unsigned int trail_length;
5921 const struct GNUNET_PeerIdentity *trail_peer_list;
5922 struct FriendInfo *target_friend;
5923 struct GNUNET_TIME_Relative congestion_timeout;
5924 struct GNUNET_HashCode trail_id;
5925 struct GNUNET_PeerIdentity next_destination;
5926 struct GNUNET_HashCode new_intermediate_trail_id;
5927 struct GNUNET_PeerIdentity next_peer;
5928 struct GNUNET_PeerIdentity source;
5929 struct GNUNET_PeerIdentity *next_hop;
5930 uint64_t ultimate_destination_finger_value;
5931 unsigned int is_predecessor;
5932 size_t msize;
5933
5934 msize = ntohs (message->size);
5935 /* We are passing the trail setup so far. */
5936 if (msize < sizeof (struct PeerTrailRejectionMessage))
5937 {
5938 GNUNET_break_op (0);
5939 return GNUNET_YES;
5940 }
5941
5942 trail_rejection = (const struct PeerTrailRejectionMessage *) message;
5943 trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
5944 sizeof (struct GNUNET_PeerIdentity);
5945 if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
5946 sizeof (struct GNUNET_PeerIdentity) != 0)
5947 {
5948 GNUNET_break_op (0);
5949 return GNUNET_OK;
5950 }
5951
5952 trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_rejection[1];
5953 is_predecessor = ntohl (trail_rejection->is_predecessor);
5954 congestion_timeout = trail_rejection->congestion_time;
5955 source = trail_rejection->source_peer;
5956 trail_id = trail_rejection->trail_id;
5957 ultimate_destination_finger_value =
5958 GNUNET_ntohll (trail_rejection->ultimate_destination_finger_value);
5959
5960 /* First set the congestion time of the friend that sent you this message. */
5961 GNUNET_assert (NULL !=
5962 (target_friend =
5963 GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
5964 target_friend->congestion_timestamp =
5965 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
5966 congestion_timeout);
5967
5968 /* I am the source peer which wants to setup the trail. Do nothing.
5969 * send_find_finger_trail_task is scheduled periodically.*/
5970 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
5971 return GNUNET_OK;
5972
5973 /* If I am congested then pass this message to peer before me in trail. */
5974 if(GNUNET_YES == GDS_ROUTING_threshold_reached())
5975 {
5976 struct GNUNET_PeerIdentity *new_trail;
5977 unsigned int new_trail_length;
5978
5979 /* Remove yourself from the trail setup so far. */
5980 if (trail_length == 1)
5981 {
5982 new_trail = NULL;
5983 new_trail_length = 0;
5984 next_hop = &source;
5985 }
5986 else
5987 {
5988 memcpy (&next_hop , &trail_peer_list[trail_length - 2],
5989 sizeof (struct GNUNET_PeerIdentity));
5990
5991 /* Remove myself from the trail. */
5992 new_trail_length = trail_length -1;
5993 new_trail = GNUNET_malloc (new_trail_length * sizeof (struct GNUNET_PeerIdentity));
5994 memcpy (new_trail, trail_peer_list, new_trail_length * sizeof (struct GNUNET_PeerIdentity));
5995 }
5996
5997 GNUNET_assert (NULL !=
5998 (target_friend =
5999 GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)));
6000 GDS_NEIGHBOURS_send_trail_rejection (source,
6001 ultimate_destination_finger_value,
6002 my_identity, is_predecessor,
6003 new_trail,new_trail_length,trail_id,
6004 target_friend, CONGESTION_TIMEOUT);
6005
6006 GNUNET_free (new_trail);
6007 return GNUNET_OK;
6008 }
6009#if 0
6010 /* Look for next_hop to pass the trail setup message */
6011 next_hop = find_successor (ultimate_destination_finger_value,
6012 &next_destination,
6013 &new_intermediate_trail_id,
6014 is_predecessor);
6015#endif
6016 struct Closest_Peer successor = find_successor (ultimate_destination_finger_value,
6017 is_predecessor);
6018 /* Am I the final destination? */
6019 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))
6020 {
6021 /* Add an entry in routing table only
6022 * 1. If I am not the original source which sent the request for trail setup
6023 FIXME: check that you always add trail entry even if your finger is
6024 friend. */
6025 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
6026 GNUNET_assert (GNUNET_YES == GDS_ROUTING_add (trail_id, *peer, my_identity));
6027
6028 if (0 == trail_length)
6029 next_peer = source;
6030 else
6031 next_peer = trail_peer_list[trail_length-1];
6032
6033 GNUNET_assert (NULL !=
6034 (target_friend =
6035 GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer)));
6036 GDS_NEIGHBOURS_send_trail_setup_result (source,
6037 my_identity,
6038 target_friend, trail_length,
6039 trail_peer_list,
6040 is_predecessor,
6041 ultimate_destination_finger_value,
6042 trail_id);
6043 }
6044 else
6045 {
6046 struct GNUNET_PeerIdentity peer_list[trail_length + 1];
6047
6048 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
6049 peer_list[trail_length] = my_identity;
6050
6051 GNUNET_assert (NULL !=
6052 (target_friend =
6053 GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)));
6054 GDS_NEIGHBOURS_send_trail_setup (source,
6055 ultimate_destination_finger_value,
6056 next_destination,
6057 target_friend, trail_length + 1, peer_list,
6058 is_predecessor, trail_id,
6059 new_intermediate_trail_id);
6060 }
6061
6062 GNUNET_free (next_hop);
6063 return GNUNET_OK;
6064}
6065#endif
6066 5003
6067/* 5004/*
6068 * If you are the new first friend, then update prev hop to source of this message
6069 * else get the next hop and pass this message forward to ultimately reach
6070 * new first_friend.
6071 * Core handle for p2p trail tear compression messages. 5005 * Core handle for p2p trail tear compression messages.
6072 * @param cls closure 5006 * @param cls closure
6073 * @param message message 5007 * @param message message
@@ -6114,7 +5048,7 @@ handle_dht_p2p_trail_compression (void *cls, const struct GNUNET_PeerIdentity *p
6114 /* Pass the message to next hop to finally reach to new_first_friend. */ 5048 /* Pass the message to next hop to finally reach to new_first_friend. */
6115 /* FIXME THIS FAILS.*/ 5049 /* FIXME THIS FAILS.*/
6116 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST); 5050 next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
6117 if (NULL == next_hop) 5051 if (NULL == next_hop) //FIXME: Assertion failure
6118 { 5052 {
6119 GNUNET_break (0); 5053 GNUNET_break (0);
6120 return GNUNET_OK; 5054 return GNUNET_OK;
@@ -6135,8 +5069,6 @@ handle_dht_p2p_trail_compression (void *cls, const struct GNUNET_PeerIdentity *p
6135 5069
6136 5070
6137/** 5071/**
6138 * Remove entry from your own routing table and pass the message to next
6139 * peer in the trail.
6140 * Core handler for trail teardown message. 5072 * Core handler for trail teardown message.
6141 * @param cls closure 5073 * @param cls closure
6142 * @param message message 5074 * @param message message
@@ -6206,9 +5138,6 @@ handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer
6206 5138
6207 5139
6208/** 5140/**
6209 * Add an entry in your routing table. If you are destination of this message
6210 * then next_hop in routing table should be your own identity. If you are NOT
6211 * destination, then get the next hop and forward message to it.
6212 * Core handle for p2p add trail message. 5141 * Core handle for p2p add trail message.
6213 * @param cls closure 5142 * @param cls closure
6214 * @param message message 5143 * @param message message
@@ -6560,11 +5489,12 @@ core_init (void *cls,
6560 const struct GNUNET_PeerIdentity *identity) 5489 const struct GNUNET_PeerIdentity *identity)
6561{ 5490{
6562 my_identity = *identity; 5491 my_identity = *identity;
6563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 5492
6564 "my_indentity = %s\n",GNUNET_i2s(&my_identity));
6565 uint64_t my_id64; 5493 uint64_t my_id64;
6566 memcpy (&my_id64, &my_identity, sizeof (uint64_t)); 5494 memcpy (&my_id64, &my_identity, sizeof (uint64_t));
6567 my_id64 = GNUNET_ntohll (my_id64); 5495 my_id64 = GNUNET_ntohll (my_id64);
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5497 "my_indentity = %s, my_id64=%llu\n",GNUNET_i2s(&my_identity),(unsigned long long)my_id64);
6568} 5498}
6569 5499
6570 5500