aboutsummaryrefslogtreecommitdiff
path: root/src/dht
diff options
context:
space:
mode:
authorSupriti Singh <supritisingh08@gmail.com>2014-05-13 12:06:08 +0000
committerSupriti Singh <supritisingh08@gmail.com>2014-05-13 12:06:08 +0000
commit783fc956a05c0f321fa63fbcaeab00bc1865a069 (patch)
tree383833f1ab97b8dd9416eb2e3f73bf133b3eaa30 /src/dht
parent9e524a56b9cfd4ea3fadbd62a74a76f4d99092bc (diff)
downloadgnunet-783fc956a05c0f321fa63fbcaeab00bc1865a069.tar.gz
gnunet-783fc956a05c0f321fa63fbcaeab00bc1865a069.zip
- Adding a new field in struct PeerTrailTearDownMessage, new_first_friend.
Diffstat (limited to 'src/dht')
-rw-r--r--src/dht/gnunet-service-xdht_neighbours.c383
1 files changed, 264 insertions, 119 deletions
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c
index 77eba79d3..22b4d55c3 100644
--- a/src/dht/gnunet-service-xdht_neighbours.c
+++ b/src/dht/gnunet-service-xdht_neighbours.c
@@ -478,21 +478,27 @@ struct PeerTrailTearDownMessage
478 struct GNUNET_MessageHeader header; 478 struct GNUNET_MessageHeader header;
479 479
480 /** 480 /**
481 * Source peer which wants to notify its new successor. 481 * Source peer of this trail.
482 */ 482 */
483 struct GNUNET_PeerIdentity source_peer; 483 struct GNUNET_PeerIdentity source_peer;
484 484
485 /** 485 /**
486 * New successor identity. 486 * Destination peer of this trail.
487 */ 487 */
488 struct GNUNET_PeerIdentity destination_peer; 488 struct GNUNET_PeerIdentity destination_peer;
489 489
490 /** 490 /**
491 * Number of peers in trail from source_peer to new successor. 491 * Trail from source_peer to destination_peer compressed such that
492 * new_first_friend is the first hop in the trail from source to
493 * destination.
494 */
495 struct GNUNET_PeerIdentity new_first_friend;
496 /**
497 * Number of peers in trail from source_peer to new first friend.
492 */ 498 */
493 uint32_t trail_length; 499 uint32_t trail_length;
494 500
495 /* Trail to from source_peer to destination_peer. */ 501 /* Trail to from source_peer to new first friend. */
496}; 502};
497 503
498GNUNET_NETWORK_STRUCT_END 504GNUNET_NETWORK_STRUCT_END
@@ -834,7 +840,7 @@ find_closest_predecessor(struct Sorting_List *all_known_peers, uint64_t value,
834 } 840 }
835 else if(all_known_peers[middle].peer_id == value) 841 else if(all_known_peers[middle].peer_id == value)
836 { 842 {
837 if(middle == (0)) 843 if(middle == 0)
838 { 844 {
839 return &all_known_peers[size - 1]; 845 return &all_known_peers[size - 1];
840 } 846 }
@@ -1316,16 +1322,19 @@ GDS_NEIGHBOURS_send_notify_new_successor (const struct GNUNET_PeerIdentity *sour
1316 * Send a trail tear down message 1322 * Send a trail tear down message
1317 * @param source_peer Source of the trail. 1323 * @param source_peer Source of the trail.
1318 * @param destination_peer Destination of the trail. 1324 * @param destination_peer Destination of the trail.
1319 * @param trail_list Peers in the trail from @a source_peer to @a destination_peer 1325 * @param discarded_trail Discarded trail from source to destination.
1320 * @param trail_length Total number of peers in trail_list. 1326 * @param discarded_trail_length Total number of peers in trail_list.
1321 * @pararm target_peer Next peer to forward this message to. 1327 * @pararm target_peer Next peer to forward this message to.
1328 * @param new_first_friend The new first hop in the new trail from source to destination
1329 * peer.
1322 */ 1330 */
1323void 1331void
1324GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_PeerIdentity *source_peer, 1332GDS_NEIGHBOURS_send_trail_teardown (const struct GNUNET_PeerIdentity *source_peer,
1325 const struct GNUNET_PeerIdentity *destination_peer, 1333 const struct GNUNET_PeerIdentity *destination_peer,
1326 struct GNUNET_PeerIdentity *trail_peer_list, 1334 const struct GNUNET_PeerIdentity *discarded_trail,
1327 unsigned int trail_length, 1335 unsigned int discarded_trail_length,
1328 struct FriendInfo *target_friend) 1336 struct FriendInfo *target_friend,
1337 const struct GNUNET_PeerIdentity *new_first_friend)
1329{ 1338{
1330 struct P2PPendingMessage *pending; 1339 struct P2PPendingMessage *pending;
1331 struct PeerTrailTearDownMessage *ttdm; 1340 struct PeerTrailTearDownMessage *ttdm;
@@ -1333,7 +1342,7 @@ GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_PeerIdentity *source_peer,
1333 size_t msize; 1342 size_t msize;
1334 1343
1335 msize = sizeof (struct PeerTrailTearDownMessage) + 1344 msize = sizeof (struct PeerTrailTearDownMessage) +
1336 (trail_length * sizeof(struct GNUNET_PeerIdentity)); 1345 (discarded_trail_length * sizeof(struct GNUNET_PeerIdentity));
1337 1346
1338 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 1347 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1339 { 1348 {
@@ -1356,10 +1365,11 @@ GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_PeerIdentity *source_peer,
1356 ttdm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN); 1365 ttdm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN);
1357 memcpy (&(ttdm->source_peer), source_peer, sizeof (struct GNUNET_PeerIdentity)); 1366 memcpy (&(ttdm->source_peer), source_peer, sizeof (struct GNUNET_PeerIdentity));
1358 memcpy (&(ttdm->destination_peer), destination_peer, sizeof (struct GNUNET_PeerIdentity)); 1367 memcpy (&(ttdm->destination_peer), destination_peer, sizeof (struct GNUNET_PeerIdentity));
1359 ttdm->trail_length = htonl (trail_length); 1368 memcpy (&(ttdm->new_first_friend),new_first_friend, sizeof (struct GNUNET_PeerIdentity));
1369 ttdm->trail_length = htonl (discarded_trail_length);
1360 1370
1361 peer_list = (struct GNUNET_PeerIdentity *) &ttdm[1]; 1371 peer_list = (struct GNUNET_PeerIdentity *) &ttdm[1];
1362 memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity)); 1372 memcpy (peer_list, discarded_trail, discarded_trail_length * sizeof (struct GNUNET_PeerIdentity));
1363 1373
1364 /* Send the message to chosen friend. */ 1374 /* Send the message to chosen friend. */
1365 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending); 1375 GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
@@ -1580,15 +1590,10 @@ send_find_finger_trail_message (void *cls,
1580} 1590}
1581 1591
1582 1592
1583
1584
1585/* In this function, we want to return the compressed trail and the trail length.
1586 We can send back a new trail and update the trail length value as we get as
1587 parameter to our function. There are many cases where we don't need to call
1588 this function. Move that logic to calling function. */
1589/** 1593/**
1590 * Scan the trail to check if any of my own friend is part of trail. If yes 1594 * Scan the trail to check if any of my own friend is part of trail. If yes
1591 * then shortcut the trail, update trail length and send back the new trail. 1595 * then shortcut the trail, send a trail teardown for the discarded trail,
1596 * update trail list and trail_length.
1592 * @param trail[Out] Current trail to reach to @a finger, will be updated 1597 * @param trail[Out] Current trail to reach to @a finger, will be updated
1593 * in case we compress the trail. 1598 * in case we compress the trail.
1594 * @param trail_length[Out] Number of peers in @a finger_trail, will be updated 1599 * @param trail_length[Out] Number of peers in @a finger_trail, will be updated
@@ -1601,11 +1606,16 @@ scan_and_compress_trail (struct GNUNET_PeerIdentity *trail,
1601 const struct GNUNET_PeerIdentity *finger) 1606 const struct GNUNET_PeerIdentity *finger)
1602{ 1607{
1603 int i; 1608 int i;
1604 1609 struct FriendInfo *target_friend;
1605 /* If finger is my friend, then set trail_length = 0;*/ 1610
1611 /* If finger is my friend, then send a trail teardown message and then set
1612 * trail_length = 0; */
1606 if (GNUNET_CONTAINER_multipeermap_get (friend_peermap, finger)) 1613 if (GNUNET_CONTAINER_multipeermap_get (friend_peermap, finger))
1607 { 1614 {
1608 /* supu' delete entry from the thrail. */ 1615 int discarded_trail_length = *trail_length;
1616 target_friend = GNUNET_CONTAINER_multipeermap_get(friend_peermap, &trail[0]);
1617 GDS_NEIGHBOURS_send_trail_teardown (&my_identity, finger, trail,
1618 discarded_trail_length, target_friend, finger);
1609 trail_length = 0; 1619 trail_length = 0;
1610 trail = NULL; 1620 trail = NULL;
1611 return; 1621 return;
@@ -1626,29 +1636,20 @@ scan_and_compress_trail (struct GNUNET_PeerIdentity *trail,
1626 * Now, we should remove the entry from A's routing table, B's routing table 1636 * Now, we should remove the entry from A's routing table, B's routing table
1627 * and update the entry in C's routing table. Rest everything will be same. 1637 * and update the entry in C's routing table. Rest everything will be same.
1628 * C's routing table should have source peer as the prev.hop. 1638 * C's routing table should have source peer as the prev.hop.
1629 * In case we found a friend not at i = 0, then we can discard all the 1639 */
1630 peers before it in the trail and short cut the path. We need to send
1631 trail teardown message also but not to all the peers in the trail. only
1632 the peer just behind me and also update the routing table of the friend,
1633 to prev hop as the source peer ie my_identity. */
1634 struct GNUNET_PeerIdentity *discarded_trail; 1640 struct GNUNET_PeerIdentity *discarded_trail;
1635 struct FriendInfo *target_friend; 1641 struct FriendInfo *target_friend;
1636 int discarded_trail_length; 1642 int discarded_trail_length;
1637 int j = 0; 1643 int j = 0;
1638 /* Here I am adding the friend (C) found to the discarded trail also, as we 1644
1639 need to update its routing table also. */ 1645 discarded_trail_length = i - 1;
1640 discarded_trail_length = i;
1641 discarded_trail = GNUNET_malloc (discarded_trail_length * sizeof (struct GNUNET_PeerIdentity)); 1646 discarded_trail = GNUNET_malloc (discarded_trail_length * sizeof (struct GNUNET_PeerIdentity));
1642 memcpy (discarded_trail, trail, discarded_trail_length * sizeof (struct GNUNET_PeerIdentity)); 1647 memcpy (discarded_trail, trail, discarded_trail_length * sizeof (struct GNUNET_PeerIdentity));
1643 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail[0]); 1648 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail[0]);
1644 /* send_update_routing_table(friend). so that it removes prev hop 1649
1645 and update it to source for given finger. */
1646 /* FIXME: Modify trail_teardown function to handle such cases. In case
1647 the last element of the trail update the routing table, in case it
1648 is trail compression. But teardown is called from various places so
1649 need to differentiate these two cases. URGENT*/
1650 GDS_NEIGHBOURS_send_trail_teardown (&my_identity, finger, discarded_trail, 1650 GDS_NEIGHBOURS_send_trail_teardown (&my_identity, finger, discarded_trail,
1651 discarded_trail_length, target_friend); 1651 discarded_trail_length, target_friend,
1652 &trail[i]);
1652 1653
1653 /* Copy the trail from index i to index trail_length -1 and change 1654 /* Copy the trail from index i to index trail_length -1 and change
1654 trail length and return */ 1655 trail length and return */
@@ -1699,48 +1700,47 @@ free_finger (struct FingerInfo *finger)
1699 1700
1700 1701
1701/** 1702/**
1702 * * 1.* If you remove an entry from finger table, and if the finger is not your friend 1703 * FIXME: First check if both the trails are present if yes then send it
1703 * and the trail length > 1 for the finger that you removed, then you should send 1704 * for both of them. Currently sending it only for one trail.
1704 * a trail_teardown message along the trail. so that the peers which have an 1705 * Send a trail teardown message for the trail of removed finger from the finger
1705 * entry in their routing table for this trail can remove it from their routing 1706 * peermap.
1706 * table. 1707 * @param existing_finger Finger to removed from the finger peermap.
1707 * Better name
1708 * TODO: First check if both the trails are present if yes then send it
1709 * for both of them.
1710 * @param existing_finger
1711 */ 1708 */
1712static 1709static
1713void send_trail_teardown (struct FingerInfo *existing_finger) 1710void send_trail_teardown (struct FingerInfo *removed_finger)
1714{ 1711{
1715 struct GNUNET_PeerIdentity *peer_list; 1712 struct GNUNET_PeerIdentity *peer_list;
1716 struct FriendInfo *friend; 1713 struct FriendInfo *friend;
1717 struct TrailPeerList *finger_trail; 1714 struct TrailPeerList *finger_trail;
1718 int existing_finger_trail_length = existing_finger->first_trail_length; 1715 int removed_finger_trail_length = removed_finger->first_trail_length;
1719 int i = 0; 1716 int i = 0;
1720 1717
1721 1718
1722 if (existing_finger->first_trail_length == 0) 1719 if (removed_finger->first_trail_length == 0)
1723 return; 1720 return;
1724 finger_trail = existing_finger->first_trail_head; 1721 finger_trail = removed_finger->first_trail_head;
1725 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &(finger_trail->peer)); 1722 friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &(finger_trail->peer));
1726 peer_list = GNUNET_malloc ( existing_finger_trail_length * sizeof (struct GNUNET_PeerIdentity)); 1723 peer_list = GNUNET_malloc ( removed_finger_trail_length * sizeof (struct GNUNET_PeerIdentity));
1727 while (i < existing_finger->first_trail_length) 1724 while (i < removed_finger->first_trail_length)
1728 { 1725 {
1729 memcpy (&peer_list[i], &(finger_trail->peer), sizeof (struct GNUNET_PeerIdentity)); 1726 memcpy (&peer_list[i], &(finger_trail->peer), sizeof (struct GNUNET_PeerIdentity));
1730 finger_trail = finger_trail->next; 1727 finger_trail = finger_trail->next;
1731 i++; 1728 i++;
1732 } 1729 }
1733 1730
1734 GDS_NEIGHBOURS_send_trail_teardown (&my_identity, &(existing_finger->finger_identity), 1731 GDS_NEIGHBOURS_send_trail_teardown (&my_identity, &(removed_finger->finger_identity),
1735 peer_list, existing_finger_trail_length, friend); 1732 peer_list, removed_finger_trail_length, friend,
1733 &(removed_finger->finger_identity));
1736} 1734}
1737 1735
1738 1736
1739/** 1737/**
1740 * Add a new trail to reach an existing finger in finger peermap. 1738 * FIXME: How do we understand which is the correct trail head?
1741 * @param existing_finger 1739 * Add a new trail to reach an existing finger in finger peermap and increment
1742 * @param trail 1740 * the count of number of trails to reach to this finger.
1743 * @param trail_length 1741 * @param existing_finger Finger
1742 * @param trail New trail to be added
1743 * @param trail_length Total number of peers in the trail.
1744 */ 1744 */
1745static 1745static
1746void add_new_trail (struct FingerInfo *existing_finger, 1746void add_new_trail (struct FingerInfo *existing_finger,
@@ -1749,8 +1749,10 @@ void add_new_trail (struct FingerInfo *existing_finger,
1749{ 1749{
1750 int i; 1750 int i;
1751 i = 0; 1751 i = 0;
1752 1752 /* FIXME: Here you need to understand which trail is there and which not.
1753 if (existing_finger->second_trail_head != NULL) 1753 In case first_trail_head != NULL, then that trail is present
1754 so you should add the second one. Need to verify this logic. */
1755 if (existing_finger->first_trail_head != NULL)
1754 { 1756 {
1755 while (i < trail_length) 1757 while (i < trail_length)
1756 { 1758 {
@@ -1774,7 +1776,7 @@ void add_new_trail (struct FingerInfo *existing_finger,
1774 element->prev = NULL; 1776 element->prev = NULL;
1775 1777
1776 memcpy (&(element->peer), &trail[i], sizeof(struct GNUNET_PeerIdentity)); 1778 memcpy (&(element->peer), &trail[i], sizeof(struct GNUNET_PeerIdentity));
1777 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->second_trail_head, existing_finger->second_trail_tail, element); 1779 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->first_trail_head, existing_finger->first_trail_tail, element);
1778 i++; 1780 i++;
1779 } 1781 }
1780 } 1782 }
@@ -1783,10 +1785,13 @@ void add_new_trail (struct FingerInfo *existing_finger,
1783 1785
1784 1786
1785/** 1787/**
1786 * FIXME: In this case first you should check which of the trail is longest and
1787 * the just discard it. Right now you are not checking it.
1788 * In case there are already maximum number of possible trail to reach to a finger, 1788 * In case there are already maximum number of possible trail to reach to a finger,
1789 * then check if the new trail can replace an existing one. If yes then replace. 1789 * then check if the new trail's length is lesser than any of the existing trails.
1790 * If yes then replace that old trail by new trail.
1791 * Note: Here we are taking length as a parameter to choose the best possible trail,
1792 * but there could be other parameters also like - 1. duration of existence of a
1793 * trail - older the better. 2. if the new trail is completely disjoint than the
1794 * other trails, then may be choosing it is better.
1790 * @param existing_finger 1795 * @param existing_finger
1791 * @param trail 1796 * @param trail
1792 * @param trail_length 1797 * @param trail_length
@@ -1795,11 +1800,41 @@ void add_new_trail (struct FingerInfo *existing_finger,
1795 */ 1800 */
1796static 1801static
1797void select_and_replace_trail (struct FingerInfo *existing_finger, 1802void select_and_replace_trail (struct FingerInfo *existing_finger,
1798 struct GNUNET_PeerIdentity *trail, 1803 struct GNUNET_PeerIdentity *new_trail,
1799 unsigned int trail_length) 1804 unsigned int new_trail_length)
1800{ 1805{
1801 if (trail_length < existing_finger->first_trail_length) 1806 if (existing_finger->first_trail_length == existing_finger->second_trail_length)
1802 { 1807 {
1808 if (new_trail_length < existing_finger->first_trail_length)
1809 {
1810 /* Randomly choose one of the trail. FIXME:currently I am just replacing the
1811 first trail.*/
1812 struct TrailPeerList *peer;
1813 int i = 0;
1814
1815 while (NULL != (peer = existing_finger->first_trail_head))
1816 {
1817 GNUNET_CONTAINER_DLL_remove (existing_finger->first_trail_head, existing_finger->first_trail_tail, peer);
1818 GNUNET_free (peer);
1819 }
1820
1821 while (i < new_trail_length)
1822 {
1823 struct TrailPeerList *element;
1824 element = GNUNET_malloc (sizeof (struct TrailPeerList));
1825 element->next = NULL;
1826 element->prev = NULL;
1827
1828 memcpy (&(element->peer), &new_trail[i], sizeof(struct GNUNET_PeerIdentity));
1829 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->second_trail_head, existing_finger->second_trail_tail, element);
1830 i++;
1831 }
1832 }
1833 }
1834 else if ((new_trail_length < existing_finger->second_trail_length) &&
1835 (existing_finger->second_trail_length < existing_finger->first_trail_length))
1836 {
1837 /* Replace the first trail by the new trail. */
1803 struct TrailPeerList *peer; 1838 struct TrailPeerList *peer;
1804 int i = 0; 1839 int i = 0;
1805 1840
@@ -1809,20 +1844,22 @@ void select_and_replace_trail (struct FingerInfo *existing_finger,
1809 GNUNET_free (peer); 1844 GNUNET_free (peer);
1810 } 1845 }
1811 1846
1812 while (i < trail_length) 1847 while (i < new_trail_length)
1813 { 1848 {
1814 struct TrailPeerList *element; 1849 struct TrailPeerList *element;
1815 element = GNUNET_malloc (sizeof (struct TrailPeerList)); 1850 element = GNUNET_malloc (sizeof (struct TrailPeerList));
1816 element->next = NULL; 1851 element->next = NULL;
1817 element->prev = NULL; 1852 element->prev = NULL;
1818 1853
1819 memcpy (&(element->peer), &trail[i], sizeof(struct GNUNET_PeerIdentity)); 1854 memcpy (&(element->peer), &new_trail[i], sizeof(struct GNUNET_PeerIdentity));
1820 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->second_trail_head, existing_finger->second_trail_tail, element); 1855 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->second_trail_head, existing_finger->second_trail_tail, element);
1821 i++; 1856 i++;
1822 } 1857 }
1823 } 1858 }
1824 else if (trail_length < existing_finger->second_trail_length) 1859 else if ( (new_trail_length < existing_finger->first_trail_length) &&
1860 (existing_finger->first_trail_length < existing_finger->second_trail_length))
1825 { 1861 {
1862 /* Replace the second trail by the new trail. */
1826 struct TrailPeerList *peer; 1863 struct TrailPeerList *peer;
1827 int i = 0; 1864 int i = 0;
1828 1865
@@ -1832,14 +1869,14 @@ void select_and_replace_trail (struct FingerInfo *existing_finger,
1832 GNUNET_free (peer); 1869 GNUNET_free (peer);
1833 } 1870 }
1834 1871
1835 while (i < trail_length) 1872 while (i < new_trail_length)
1836 { 1873 {
1837 struct TrailPeerList *element; 1874 struct TrailPeerList *element;
1838 element = GNUNET_malloc (sizeof (struct TrailPeerList)); 1875 element = GNUNET_malloc (sizeof (struct TrailPeerList));
1839 element->next = NULL; 1876 element->next = NULL;
1840 element->prev = NULL; 1877 element->prev = NULL;
1841 1878
1842 memcpy (&(element->peer), &trail[i], sizeof(struct GNUNET_PeerIdentity)); 1879 memcpy (&(element->peer), &new_trail[i], sizeof(struct GNUNET_PeerIdentity));
1843 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->second_trail_head, existing_finger->second_trail_tail, element); 1880 GNUNET_CONTAINER_DLL_insert_tail(existing_finger->second_trail_head, existing_finger->second_trail_tail, element);
1844 i++; 1881 i++;
1845 } 1882 }
@@ -1848,8 +1885,11 @@ void select_and_replace_trail (struct FingerInfo *existing_finger,
1848 1885
1849 1886
1850/** 1887/**
1851 * FIXME: If we remove a finger which is our friend, then do we need to handle it 1888 * FIXME: If we remove a finger which is our friend, then how should we handle it.
1852 * differentlty in regard to trail count. 1889 * Ideally only in case if the trail_length > 0,we increment the trail count
1890 * of the first friend in the trail to reach to the finger. in case finger is
1891 * our friend then trail length = 0, and hence, we have never incremented the
1892 * trail count associated with that friend.
1853 * Decrement the trail count for the first friend to reach to the finger. 1893 * Decrement the trail count for the first friend to reach to the finger.
1854 * @param finger 1894 * @param finger
1855 */ 1895 */
@@ -1862,7 +1902,7 @@ decrement_friend_trail_count (struct FingerInfo *finger)
1862 if(finger->first_trail_head != NULL) 1902 if(finger->first_trail_head != NULL)
1863 { 1903 {
1864 first_trail_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 1904 first_trail_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
1865 &(finger->first_trail_head->peer)); 1905 &(finger->first_trail_head->peer));
1866 first_trail_friend->trails_count--; 1906 first_trail_friend->trails_count--;
1867 } 1907 }
1868 1908
@@ -1873,28 +1913,28 @@ decrement_friend_trail_count (struct FingerInfo *finger)
1873 second_trail_friend->trails_count--; 1913 second_trail_friend->trails_count--;
1874 } 1914 }
1875 1915
1916#if 0
1917 /* We will not need this variable any more, all_friends_trail_threshold,
1918 FIXME: REMOVE IT. */
1876 if (GNUNET_YES == all_friends_trail_threshold) 1919 if (GNUNET_YES == all_friends_trail_threshold)
1877 { 1920 {
1878 all_friends_trail_threshold = GNUNET_NO; 1921 all_friends_trail_threshold = GNUNET_NO;
1879 /* FIXME; Here you should reschedule the send_find_finger_task here. or 1922 /* FIXME; Here you should reschedule the send_find_finger_task here. or
1880 make a call.*/ 1923 make a call.*/
1881 } 1924 }
1925#endif
1882} 1926}
1883 1927
1884 1928
1885/** 1929/**
1886 * FIXME: consider the case where my_id = 2, and we are in circle from 0 to 7. 1930 * Select the closest finger. Used for both predecessor and other fingers..
1887 * my current_predecessor is 6, and now the new finger 1. Here we are checking 1931 * But internally calls different functions for predecessor and other fingers.
1888 * if existing_finger < new_entry then new_entry is predecessor. This holds 1932 * @param existing_finger Finger in finger peermap.
1889 * true in case where lets say existing_finger = 5, new_entry= 6. But in the case 1933 * @param new_finger New finger identity
1890 * above, 6 > 1 but still 1 is correct predecessor. We have not handled it here. 1934 * @param finger_map_index Index in finger peermap where @a existing_finger is stored.
1891 * We can put all the three values in an array and then the peer just before me 1935 * @return #GNUNET_YES if the new finger is closest.
1892 * will be mine predecessor. 1936 * #GNUNET_NO if the old finger is closest.
1893 * FIXME: Currently I am using struct Sorting_list to compare the values, 1937 * #GNUNET_SYSERR in case our own identity is closest (should never happen).
1894 * will create a new ds if needed.
1895 * @param existing_finger
1896 * @param new_finger
1897 * @return
1898 */ 1938 */
1899static 1939static
1900int select_finger (struct FingerInfo *existing_finger, 1940int select_finger (struct FingerInfo *existing_finger,
@@ -1909,21 +1949,23 @@ int select_finger (struct FingerInfo *existing_finger,
1909 for (k = 0; k < 3; k++) 1949 for (k = 0; k < 3; k++)
1910 peers[k].data = 0; 1950 peers[k].data = 0;
1911 1951
1952 /* Add your entry to peers. */
1912 memcpy (&peers[0], &my_identity, sizeof (uint64_t)); 1953 memcpy (&peers[0], &my_identity, sizeof (uint64_t));
1913 peers[0].type = MY_ID; 1954 peers[0].type = MY_ID;
1914 peers[0].data = NULL; 1955 peers[0].data = NULL;
1915 1956
1957 /* Add existing_finger identity to the peers. */
1916 memcpy (&peers[1], &(existing_finger->finger_identity), sizeof (uint64_t)); 1958 memcpy (&peers[1], &(existing_finger->finger_identity), sizeof (uint64_t));
1917 peers[1].type = FINGER; 1959 peers[1].type = FINGER;
1918 peers[1].data = existing_finger; 1960 peers[1].data = existing_finger;
1919 1961
1962 /* Add new_finger identity to the peers. s*/
1920 memcpy (&peers[2], &new_finger, sizeof (uint64_t)); 1963 memcpy (&peers[2], &new_finger, sizeof (uint64_t));
1921 peers[2].type = VALUE; 1964 peers[2].type = VALUE;
1922 peers[2].data = NULL; 1965 peers[2].data = NULL;
1923 1966
1924 memcpy (&value, &my_identity, sizeof (uint64_t)); 1967 memcpy (&value, &my_identity, sizeof (uint64_t));
1925 1968
1926
1927 qsort (&peers, 3, sizeof (struct Sorting_List), &compare_peer_id); 1969 qsort (&peers, 3, sizeof (struct Sorting_List), &compare_peer_id);
1928 1970
1929 if (PREDECESSOR_FINGER_ID == finger_map_index) 1971 if (PREDECESSOR_FINGER_ID == finger_map_index)
@@ -1947,9 +1989,15 @@ int select_finger (struct FingerInfo *existing_finger,
1947 1989
1948 1990
1949/** 1991/**
1950 * Choose the closest finger between existing_finger and new_finger. In case new_finger 1992 * FIXME: Do we need to reinsert the existing finger into finger peermap
1951 * is closest and finger_map_index != PREDCESSOR_FINGER_ID, 1993 * in case we add a new trail?
1952 * then send a tear down message along the trail to reach existing_finger. 1994 * Choose the closest finger between existing finger and new finger.
1995 * If the new finger is closest and finger_map_index != PREDECESSOR_FINGER_ID,
1996 * then send a trail_teardown message along existing_finger's trail.
1997 * In case both the id's are same, and there is a place to keep more trails, then
1998 * store both of them. In case there is no space to store any more trail, then
1999 * choose the best trail (best - depends on length in current_implementation) and
2000 * discard the others.
1953 * @param existing_finger Existing entry in finger peer map 2001 * @param existing_finger Existing entry in finger peer map
1954 * @param new_finger New finger 2002 * @param new_finger New finger
1955 * @param trail Trail to reach to the new finger from me. 2003 * @param trail Trail to reach to the new finger from me.
@@ -1973,8 +2021,8 @@ int select_closest_finger (struct FingerInfo *existing_finger,
1973 /* Both the new entry and existing entry are same. */ 2021 /* Both the new entry and existing entry are same. */
1974 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity), &my_identity)) 2022 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity), &my_identity))
1975 { 2023 {
1976 /* If both are same then exit. You already have that entry in your finger table, 2024 /* If existing_finger is my_identity then trail_length = 0, trail = NULL. In
1977 then you don't need to add it again. */ 2025 this case you don't need to check the trails. Exit. */
1978 return GNUNET_NO; 2026 return GNUNET_NO;
1979 } 2027 }
1980 if (trail_length > 1) 2028 if (trail_length > 1)
@@ -1994,23 +2042,21 @@ int select_closest_finger (struct FingerInfo *existing_finger,
1994 } 2042 }
1995 else if (GNUNET_YES == select_finger (existing_finger, new_finger, finger_map_index)) 2043 else if (GNUNET_YES == select_finger (existing_finger, new_finger, finger_map_index))
1996 { 2044 {
1997 /* Here in case finger_map_index was Predecessor_finger then also you don't 2045 /* new_finger is the correct finger. */
1998 need to send trail teardown and in case its successor then you found it in
1999 trail_setup and then you don't need to send trail teardown. FIXME: check if
2000 its true for every call made to finger_table_add. Also, if we have an entry
2001 which is not my identity should I replace it with my identity or not? */
2002 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, new_finger)) 2046 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, new_finger))
2003 { 2047 {
2004 return GNUNET_NO; /* FIXME: In case I have a peer id which is not my id then 2048 /* FIXME: Here in case the new finger is my_identity and old entry is not,
2005 * should I keep it as finger */ 2049 should we keep the old entry even if the old entry is not the closest? */
2006 2050 return GNUNET_NO;
2007 } 2051 }
2008 /* new_finger is the correct finger. */ 2052
2053 /* Clear all things associated with existing_finger (only if its not a
2054 predecessor) */
2009 if (PREDECESSOR_FINGER_ID != finger_map_index) 2055 if (PREDECESSOR_FINGER_ID != finger_map_index)
2010 send_trail_teardown (existing_finger); 2056 send_trail_teardown (existing_finger);
2011
2012 decrement_friend_trail_count (existing_finger); 2057 decrement_friend_trail_count (existing_finger);
2013 free_finger (existing_finger); 2058 free_finger (existing_finger);
2059
2014 if (trail_length > 1) 2060 if (trail_length > 1)
2015 scan_and_compress_trail (trail, &trail_length, new_finger); 2061 scan_and_compress_trail (trail, &trail_length, new_finger);
2016 return GNUNET_YES; 2062 return GNUNET_YES;
@@ -2086,7 +2132,8 @@ compare_and_update_predecessor (const struct GNUNET_PeerIdentity *peer,
2086 first_friend_trail->trails_count++; 2132 first_friend_trail->trails_count++;
2087 } 2133 }
2088 new_finger_entry->first_friend_trails_count = first_friend_trail->trails_count; 2134 new_finger_entry->first_friend_trails_count = first_friend_trail->trails_count;
2089 2135
2136 /* Invert the trail and then add. */
2090 if (trail_length != 0) 2137 if (trail_length != 0)
2091 { 2138 {
2092 i = trail_length - 1; 2139 i = trail_length - 1;
@@ -2230,14 +2277,12 @@ add_new_entry (const struct GNUNET_PeerIdentity *finger_identity,
2230} 2277}
2231 2278
2232 2279
2233/** 2280/**1. Should we check if there is already an entry for same finger id in the finger
2234 * 1. removed predecessor_finger_id check as in select_closest_finger we check it 2281 * peermap as we are checking for index. I think its too much of code and the use
2235 * and handle it accordingly. 2282 * of having unique identifier in finger map is only in case of find_successor
2283 * but anyways we will find the correct successor.
2236 * 2. you don't handle the second trail here as in new entry you will have only 2284 * 2. you don't handle the second trail here as in new entry you will have only
2237 * one trail to reach to the finger. 2285 * one trail to reach to the finger.
2238 * 3. check how do you handle the return value of this function.
2239 * FIXME: Functions calling finger_table_add will not check if finger identity
2240 * and my identity are same, it should be done in this function.
2241 * Add an entry in the finger table. If there is already an existing entry in 2286 * Add an entry in the finger table. If there is already an existing entry in
2242 * the finger peermap for given finger map index, then choose the closest one. 2287 * the finger peermap for given finger map index, then choose the closest one.
2243 * In case both the new entry and old entry are same, store both of them. (Redundant 2288 * In case both the new entry and old entry are same, store both of them. (Redundant
@@ -2260,6 +2305,16 @@ int finger_table_add (const struct GNUNET_PeerIdentity *finger_identity,
2260 int i; 2305 int i;
2261 int new_entry_added = GNUNET_NO; 2306 int new_entry_added = GNUNET_NO;
2262 2307
2308 if (PREDECESSOR_FINGER_ID == finger_map_index)
2309 {
2310 /* FIXME: Here GNUNET_NO, means that we did not update predecessor . But it
2311 we also need to handle the case that insertion failed in peer map after we decided to add
2312 the entry. */
2313 if( GNUNET_YES == compare_and_update_predecessor (finger_identity, finger_trail, finger_trail_length))
2314 new_entry_added = GNUNET_YES;
2315 goto update_current_search_finger_index;
2316 }
2317
2263 /* Check if there is already an entry for the finger map index in the finger peer map. */ 2318 /* Check if there is already an entry for the finger map index in the finger peer map. */
2264 finger_iter = GNUNET_CONTAINER_multipeermap_iterator_create (finger_peermap); 2319 finger_iter = GNUNET_CONTAINER_multipeermap_iterator_create (finger_peermap);
2265 for (i= 0; i < GNUNET_CONTAINER_multipeermap_size (finger_peermap); i++) 2320 for (i= 0; i < GNUNET_CONTAINER_multipeermap_size (finger_peermap); i++)
@@ -2270,7 +2325,8 @@ int finger_table_add (const struct GNUNET_PeerIdentity *finger_identity,
2270 if (existing_finger->finger_map_index == finger_map_index) 2325 if (existing_finger->finger_map_index == finger_map_index)
2271 { 2326 {
2272 if ( GNUNET_NO == select_closest_finger (existing_finger, finger_identity, 2327 if ( GNUNET_NO == select_closest_finger (existing_finger, finger_identity,
2273 finger_trail, finger_trail_length,finger_map_index)) 2328 finger_trail, finger_trail_length,
2329 finger_map_index))
2274 goto update_current_search_finger_index; 2330 goto update_current_search_finger_index;
2275 else 2331 else
2276 break; 2332 break;
@@ -2278,7 +2334,8 @@ int finger_table_add (const struct GNUNET_PeerIdentity *finger_identity,
2278 } 2334 }
2279 } 2335 }
2280 GNUNET_CONTAINER_multipeermap_iterator_destroy (finger_iter); 2336 GNUNET_CONTAINER_multipeermap_iterator_destroy (finger_iter);
2281 2337 /* SUPU: in this case you get GNUNET_NO, only when insertion fails in the peer map.
2338 so its an error as we already have decided to add the entry into finger peer map. */
2282 if(GNUNET_OK == add_new_entry (finger_identity,finger_trail,finger_trail_length, finger_map_index)) 2339 if(GNUNET_OK == add_new_entry (finger_identity,finger_trail,finger_trail_length, finger_map_index))
2283 new_entry_added = GNUNET_YES; 2340 new_entry_added = GNUNET_YES;
2284 else 2341 else
@@ -2293,7 +2350,6 @@ int finger_table_add (const struct GNUNET_PeerIdentity *finger_identity,
2293 } 2350 }
2294 else if (GNUNET_YES == compare_new_entry_and_successor (finger_identity,finger_map_index)) 2351 else if (GNUNET_YES == compare_new_entry_and_successor (finger_identity,finger_map_index))
2295 { 2352 {
2296 /* If the new entry is same as our successor, then reset the current_search_finger_index to 0*/
2297 current_search_finger_index = 0; 2353 current_search_finger_index = 0;
2298 } 2354 }
2299 else 2355 else
@@ -3519,7 +3575,11 @@ handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity *pee
3519} 3575}
3520 3576
3521 3577
3522/** 3578/**FIXME: in case we have a new successor do we need to update the entries in
3579 * routing table to change the destination of the message from old successor
3580 * to new successor or when the old successor sends the message the I am not
3581 * your successor then it sends a trail teardown message across the old trail.
3582 * Need to decide on a strategy.
3523 * Core handle for p2p verify successor result messages. 3583 * Core handle for p2p verify successor result messages.
3524 * @param cls closure 3584 * @param cls closure
3525 * @param message message 3585 * @param message message
@@ -3694,6 +3754,7 @@ handle_dht_p2p_notify_new_successor(void *cls, const struct GNUNET_PeerIdentity
3694 memcpy (&next_hop, &trail_peer_list[my_index+1], sizeof (struct GNUNET_PeerIdentity)); 3754 memcpy (&next_hop, &trail_peer_list[my_index+1], sizeof (struct GNUNET_PeerIdentity));
3695 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop); 3755 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
3696 } 3756 }
3757 /* FIXME: Should we update the entries in routing table? */
3697 GDS_NEIGHBOURS_send_notify_new_successor (&(nsm->source_peer), 3758 GDS_NEIGHBOURS_send_notify_new_successor (&(nsm->source_peer),
3698 &(nsm->destination_peer), 3759 &(nsm->destination_peer),
3699 target_friend, trail_peer_list, 3760 target_friend, trail_peer_list,
@@ -3880,6 +3941,89 @@ int handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity *
3880} 3941}
3881 3942
3882 3943
3944/* Core handle for p2p trail tear down messages.
3945 * @param cls closure
3946 * @param message message
3947 * @param peer peer identity this notification is about
3948 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
3949 */
3950static
3951int handle_dht_p2p_trail_teardown(void *cls, const struct GNUNET_PeerIdentity *peer,
3952 const struct GNUNET_MessageHeader *message)
3953{
3954 struct PeerTrailTearDownMessage *trail_teardown;
3955 struct GNUNET_PeerIdentity *discarded_trail;
3956 struct GNUNET_PeerIdentity next_hop;
3957 struct FriendInfo *target_friend;
3958 uint32_t discarded_trail_length;
3959 size_t msize;
3960 int my_index;
3961
3962 msize = ntohs (message->size);
3963 if (msize < sizeof (struct PeerTrailTearDownMessage))
3964 {
3965 GNUNET_break_op (0);
3966 return GNUNET_OK;
3967 }
3968
3969 trail_teardown = (struct PeerTrailTearDownMessage *) message;
3970 discarded_trail_length = ntohl (trail_teardown->trail_length);
3971
3972 if ((msize < sizeof (struct PeerTrailTearDownMessage) +
3973 discarded_trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
3974 (discarded_trail_length >
3975 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
3976 {
3977 GNUNET_break_op (0);
3978 return GNUNET_OK;
3979 }
3980
3981 discarded_trail = (struct GNUNET_PeerIdentity *) &trail_teardown[1];
3982
3983 if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_teardown->new_first_friend),
3984 &my_identity)))
3985 {
3986 if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_teardown->destination_peer),
3987 &my_identity)))
3988 {
3989 return GNUNET_OK;
3990 }
3991 else
3992 {
3993 /* FIXME:
3994 * I am the new first hop in the trail to reach from source to destination.
3995 Update the trail in routing table with prev_hop == source peer. */
3996 }
3997 }
3998 else
3999 {
4000 my_index = search_my_index (discarded_trail, discarded_trail_length);
4001 if(GNUNET_SYSERR == my_index)
4002 return GNUNET_SYSERR;
4003
4004 if (GNUNET_NO == GDS_ROUTING_remove_trail (&(trail_teardown->source_peer),
4005 &(trail_teardown->destination_peer),peer))
4006 {
4007 /* Here we get GNUNET_NO, only if there is no matching entry found in routing
4008 table. */
4009 GNUNET_break (0);
4010 return GNUNET_YES;
4011 }
4012
4013 memcpy (&next_hop, &discarded_trail[my_index + 1], sizeof (struct GNUNET_PeerIdentity));
4014 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
4015
4016 GDS_NEIGHBOURS_send_trail_teardown (&(trail_teardown->source_peer),
4017 &(trail_teardown->destination_peer),
4018 discarded_trail, discarded_trail_length,
4019 target_friend, &(trail_teardown->new_first_friend));
4020 return GNUNET_YES;
4021 }
4022 return GNUNET_SYSERR;
4023}
4024
4025
4026#if 0
3883/** 4027/**
3884 * FIXME: we don't send trail teardown to finger for which the trail was setup. 4028 * FIXME: we don't send trail teardown to finger for which the trail was setup.
3885 * Trail teardown only aim is to remove entries from the routing table. Destination 4029 * Trail teardown only aim is to remove entries from the routing table. Destination
@@ -3952,12 +4096,13 @@ int handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *
3952 4096
3953 memcpy (&next_hop, &trail_peer_list[my_index + 1], sizeof (struct GNUNET_PeerIdentity)); 4097 memcpy (&next_hop, &trail_peer_list[my_index + 1], sizeof (struct GNUNET_PeerIdentity));
3954 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop); 4098 target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
3955 4099 /* FIXME:add a new field new_first_friend. */
3956 GDS_NEIGHBOURS_send_trail_teardown (&(trail_teardown->source_peer), 4100 GDS_NEIGHBOURS_send_trail_teardown (&(trail_teardown->source_peer),
3957 &(trail_teardown->destination_peer), 4101 &(trail_teardown->destination_peer),
3958 trail_peer_list, trail_length, target_friend); 4102 trail_peer_list, trail_length, target_friend);
3959 return GNUNET_YES; 4103 return GNUNET_YES;
3960} 4104}
4105#endif
3961 4106
3962/** 4107/**
3963 * Iterate over finger_peermap, and remove entries with peer as the first element 4108 * Iterate over finger_peermap, and remove entries with peer as the first element