diff options
author | Bart Polot <bart@net.in.tum.de> | 2013-10-10 14:15:33 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2013-10-10 14:15:33 +0000 |
commit | 8beabcd96c0cf1e1873c0b5ff96e537f1beb0b34 (patch) | |
tree | bad2b4db91c8ef35e7c0dba3fe7db32e8cefb783 | |
parent | f6d51b4a6e8b6cde3a89343cede98b347857084a (diff) | |
download | gnunet-8beabcd96c0cf1e1873c0b5ff96e537f1beb0b34.tar.gz gnunet-8beabcd96c0cf1e1873c0b5ff96e537f1beb0b34.zip |
- move path manipulation to API
-rw-r--r-- | src/mesh/gnunet-service-mesh_connection.c | 7 | ||||
-rw-r--r-- | src/mesh/gnunet-service-mesh_peer.c | 277 | ||||
-rw-r--r-- | src/mesh/gnunet-service-mesh_peer.h | 37 |
3 files changed, 183 insertions, 138 deletions
diff --git a/src/mesh/gnunet-service-mesh_connection.c b/src/mesh/gnunet-service-mesh_connection.c index b399b135b..9a58bf843 100644 --- a/src/mesh/gnunet-service-mesh_connection.c +++ b/src/mesh/gnunet-service-mesh_connection.c | |||
@@ -32,7 +32,6 @@ | |||
32 | #include "mesh_path.h" | 32 | #include "mesh_path.h" |
33 | #include "mesh_protocol_enc.h" | 33 | #include "mesh_protocol_enc.h" |
34 | #include "mesh_enc.h" | 34 | #include "mesh_enc.h" |
35 | |||
36 | #include "gnunet-service-mesh_connection.h" | 35 | #include "gnunet-service-mesh_connection.h" |
37 | #include "gnunet-service-mesh_peer.h" | 36 | #include "gnunet-service-mesh_peer.h" |
38 | #include "gnunet-service-mesh_tunnel.h" | 37 | #include "gnunet-service-mesh_tunnel.h" |
@@ -1078,7 +1077,7 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer, | |||
1078 | if (c->own_pos == size - 1) | 1077 | if (c->own_pos == size - 1) |
1079 | { | 1078 | { |
1080 | LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n"); | 1079 | LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n"); |
1081 | peer_add_path_to_origin (orig_peer, path, GNUNET_YES); | 1080 | GMP_add_path_to_origin (orig_peer, path, GNUNET_YES); |
1082 | 1081 | ||
1083 | if (NULL == orig_peer->tunnel) | 1082 | if (NULL == orig_peer->tunnel) |
1084 | { | 1083 | { |
@@ -1100,8 +1099,8 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer, | |||
1100 | { | 1099 | { |
1101 | /* It's for somebody else! Retransmit. */ | 1100 | /* It's for somebody else! Retransmit. */ |
1102 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n"); | 1101 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n"); |
1103 | peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO); | 1102 | GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO); |
1104 | peer_add_path_to_origin (orig_peer, path, GNUNET_NO); | 1103 | GMP_add_path_to_origin (orig_peer, path, GNUNET_NO); |
1105 | GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES); | 1104 | GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES); |
1106 | } | 1105 | } |
1107 | return GNUNET_OK; | 1106 | return GNUNET_OK; |
diff --git a/src/mesh/gnunet-service-mesh_peer.c b/src/mesh/gnunet-service-mesh_peer.c index 157b48dad..a4e17cefc 100644 --- a/src/mesh/gnunet-service-mesh_peer.c +++ b/src/mesh/gnunet-service-mesh_peer.c | |||
@@ -202,6 +202,11 @@ static struct GNUNET_CORE_Handle *core_handle; | |||
202 | */ | 202 | */ |
203 | const static struct GNUNET_PeerIdentity *my_full_id; | 203 | const static struct GNUNET_PeerIdentity *my_full_id; |
204 | 204 | ||
205 | /** | ||
206 | * Local peer own ID (short) | ||
207 | */ | ||
208 | static GNUNET_PEER_Id my_short_id; | ||
209 | |||
205 | /******************************************************************************/ | 210 | /******************************************************************************/ |
206 | /***************************** CORE CALLBACKS *********************************/ | 211 | /***************************** CORE CALLBACKS *********************************/ |
207 | /******************************************************************************/ | 212 | /******************************************************************************/ |
@@ -764,140 +769,6 @@ peer_get_best_path (const struct MeshPeer *peer) | |||
764 | 769 | ||
765 | 770 | ||
766 | /** | 771 | /** |
767 | * Add the path to the peer and update the path used to reach it in case this | ||
768 | * is the shortest. | ||
769 | * | ||
770 | * @param peer_info Destination peer to add the path to. | ||
771 | * @param path New path to add. Last peer must be the peer in arg 1. | ||
772 | * Path will be either used of freed if already known. | ||
773 | * @param trusted Do we trust that this path is real? | ||
774 | */ | ||
775 | void | ||
776 | peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path, | ||
777 | int trusted) | ||
778 | { | ||
779 | struct MeshPeerPath *aux; | ||
780 | unsigned int l; | ||
781 | unsigned int l2; | ||
782 | |||
783 | if ((NULL == peer_info) || (NULL == path)) | ||
784 | { | ||
785 | GNUNET_break (0); | ||
786 | path_destroy (path); | ||
787 | return; | ||
788 | } | ||
789 | if (path->peers[path->length - 1] != peer_info->id) | ||
790 | { | ||
791 | GNUNET_break (0); | ||
792 | path_destroy (path); | ||
793 | return; | ||
794 | } | ||
795 | if (2 >= path->length && GNUNET_NO == trusted) | ||
796 | { | ||
797 | /* Only allow CORE to tell us about direct paths */ | ||
798 | path_destroy (path); | ||
799 | return; | ||
800 | } | ||
801 | for (l = 1; l < path->length; l++) | ||
802 | { | ||
803 | if (path->peers[l] == myid) | ||
804 | { | ||
805 | LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l); | ||
806 | for (l2 = 0; l2 < path->length - l; l2++) | ||
807 | { | ||
808 | path->peers[l2] = path->peers[l + l2]; | ||
809 | } | ||
810 | path->length -= l; | ||
811 | l = 1; | ||
812 | path->peers = | ||
813 | GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id)); | ||
814 | } | ||
815 | } | ||
816 | |||
817 | LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n", | ||
818 | path->length, peer2s (peer_info)); | ||
819 | |||
820 | l = path_get_length (path); | ||
821 | if (0 == l) | ||
822 | { | ||
823 | path_destroy (path); | ||
824 | return; | ||
825 | } | ||
826 | |||
827 | GNUNET_assert (peer_info->id == path->peers[path->length - 1]); | ||
828 | for (aux = peer_info->path_head; aux != NULL; aux = aux->next) | ||
829 | { | ||
830 | l2 = path_get_length (aux); | ||
831 | if (l2 > l) | ||
832 | { | ||
833 | GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head, | ||
834 | peer_info->path_tail, aux, path); | ||
835 | return; | ||
836 | } | ||
837 | else | ||
838 | { | ||
839 | if (l2 == l && memcmp (path->peers, aux->peers, l) == 0) | ||
840 | { | ||
841 | path_destroy (path); | ||
842 | return; | ||
843 | } | ||
844 | } | ||
845 | } | ||
846 | GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail, | ||
847 | path); | ||
848 | return; | ||
849 | } | ||
850 | |||
851 | |||
852 | /** | ||
853 | * Add the path to the origin peer and update the path used to reach it in case | ||
854 | * this is the shortest. | ||
855 | * The path is given in peer_info -> destination, therefore we turn the path | ||
856 | * upside down first. | ||
857 | * | ||
858 | * @param peer_info Peer to add the path to, being the origin of the path. | ||
859 | * @param path New path to add after being inversed. | ||
860 | * Path will be either used or freed. | ||
861 | * @param trusted Do we trust that this path is real? | ||
862 | */ | ||
863 | static void | ||
864 | peer_add_path_to_origin (struct MeshPeer *peer_info, | ||
865 | struct MeshPeerPath *path, int trusted) | ||
866 | { | ||
867 | if (NULL == path) | ||
868 | return; | ||
869 | path_invert (path); | ||
870 | peer_add_path (peer_info, path, trusted); | ||
871 | } | ||
872 | |||
873 | |||
874 | /** | ||
875 | * Adds a path to the peer_infos of all the peers in the path | ||
876 | * | ||
877 | * @param p Path to process. | ||
878 | * @param confirmed Whether we know if the path works or not. | ||
879 | */ | ||
880 | static void | ||
881 | path_add_to_peers (struct MeshPeerPath *p, int confirmed) | ||
882 | { | ||
883 | unsigned int i; | ||
884 | |||
885 | /* TODO: invert and add */ | ||
886 | for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ; | ||
887 | for (i++; i < p->length; i++) | ||
888 | { | ||
889 | struct MeshPeer *aux; | ||
890 | struct MeshPeerPath *copy; | ||
891 | |||
892 | aux = peer_get_short (p->peers[i]); | ||
893 | copy = path_duplicate (p); | ||
894 | copy->length = i + 1; | ||
895 | peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed); | ||
896 | } | ||
897 | } | ||
898 | |||
899 | |||
900 | /** | ||
901 | * Function to process paths received for a new peer addition. The recorded | 772 | * Function to process paths received for a new peer addition. The recorded |
902 | * paths form the initial tunnel, which can be optimized later. | 773 | * paths form the initial tunnel, which can be optimized later. |
903 | * Called on each result obtained for the DHT search. | 774 | * Called on each result obtained for the DHT search. |
@@ -1452,6 +1323,8 @@ GMP_init (const struct GNUNET_CONFIGURATION_Handle *c, | |||
1452 | const struct GNUNET_PeerIdentity *id) | 1323 | const struct GNUNET_PeerIdentity *id) |
1453 | { | 1324 | { |
1454 | my_full_id = id; | 1325 | my_full_id = id; |
1326 | my_short_id = GNUNET_PEER_intern (id); | ||
1327 | |||
1455 | peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO); | 1328 | peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO); |
1456 | if (GNUNET_OK != | 1329 | if (GNUNET_OK != |
1457 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS", | 1330 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS", |
@@ -1509,6 +1382,7 @@ GMP_shutdown (void) | |||
1509 | GNUNET_CORE_disconnect (core_handle); | 1382 | GNUNET_CORE_disconnect (core_handle); |
1510 | core_handle = NULL; | 1383 | core_handle = NULL; |
1511 | } | 1384 | } |
1385 | GNUNET_PEER_change_rc (my_short_id, -1); | ||
1512 | } | 1386 | } |
1513 | 1387 | ||
1514 | 1388 | ||
@@ -1658,6 +1532,141 @@ GMP_add_connection (struct MeshPeer *peer, | |||
1658 | 1532 | ||
1659 | 1533 | ||
1660 | /** | 1534 | /** |
1535 | * Add the path to the peer and update the path used to reach it in case this | ||
1536 | * is the shortest. | ||
1537 | * | ||
1538 | * @param peer_info Destination peer to add the path to. | ||
1539 | * @param path New path to add. Last peer must be the peer in arg 1. | ||
1540 | * Path will be either used of freed if already known. | ||
1541 | * @param trusted Do we trust that this path is real? | ||
1542 | */ | ||
1543 | void | ||
1544 | GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path, | ||
1545 | int trusted) | ||
1546 | { | ||
1547 | struct MeshPeerPath *aux; | ||
1548 | unsigned int l; | ||
1549 | unsigned int l2; | ||
1550 | |||
1551 | if ((NULL == peer_info) || (NULL == path)) | ||
1552 | { | ||
1553 | GNUNET_break (0); | ||
1554 | path_destroy (path); | ||
1555 | return; | ||
1556 | } | ||
1557 | if (path->peers[path->length - 1] != peer_info->id) | ||
1558 | { | ||
1559 | GNUNET_break (0); | ||
1560 | path_destroy (path); | ||
1561 | return; | ||
1562 | } | ||
1563 | if (2 >= path->length && GNUNET_NO == trusted) | ||
1564 | { | ||
1565 | /* Only allow CORE to tell us about direct paths */ | ||
1566 | path_destroy (path); | ||
1567 | return; | ||
1568 | } | ||
1569 | for (l = 1; l < path->length; l++) | ||
1570 | { | ||
1571 | if (path->peers[l] == myid) | ||
1572 | { | ||
1573 | LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l); | ||
1574 | for (l2 = 0; l2 < path->length - l; l2++) | ||
1575 | { | ||
1576 | path->peers[l2] = path->peers[l + l2]; | ||
1577 | } | ||
1578 | path->length -= l; | ||
1579 | l = 1; | ||
1580 | path->peers = | ||
1581 | GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id)); | ||
1582 | } | ||
1583 | } | ||
1584 | |||
1585 | LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n", | ||
1586 | path->length, peer2s (peer_info)); | ||
1587 | |||
1588 | l = path_get_length (path); | ||
1589 | if (0 == l) | ||
1590 | { | ||
1591 | path_destroy (path); | ||
1592 | return; | ||
1593 | } | ||
1594 | |||
1595 | GNUNET_assert (peer_info->id == path->peers[path->length - 1]); | ||
1596 | for (aux = peer_info->path_head; aux != NULL; aux = aux->next) | ||
1597 | { | ||
1598 | l2 = path_get_length (aux); | ||
1599 | if (l2 > l) | ||
1600 | { | ||
1601 | GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head, | ||
1602 | peer_info->path_tail, aux, path); | ||
1603 | return; | ||
1604 | } | ||
1605 | else | ||
1606 | { | ||
1607 | if (l2 == l && memcmp (path->peers, aux->peers, l) == 0) | ||
1608 | { | ||
1609 | path_destroy (path); | ||
1610 | return; | ||
1611 | } | ||
1612 | } | ||
1613 | } | ||
1614 | GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail, | ||
1615 | path); | ||
1616 | return; | ||
1617 | } | ||
1618 | |||
1619 | |||
1620 | /** | ||
1621 | * Add the path to the origin peer and update the path used to reach it in case | ||
1622 | * this is the shortest. | ||
1623 | * The path is given in peer_info -> destination, therefore we turn the path | ||
1624 | * upside down first. | ||
1625 | * | ||
1626 | * @param peer_info Peer to add the path to, being the origin of the path. | ||
1627 | * @param path New path to add after being inversed. | ||
1628 | * Path will be either used or freed. | ||
1629 | * @param trusted Do we trust that this path is real? | ||
1630 | */ | ||
1631 | void | ||
1632 | GMP_add_path_to_origin (struct MeshPeer *peer, | ||
1633 | struct MeshPeerPath *path, | ||
1634 | int trusted) | ||
1635 | { | ||
1636 | if (NULL == path) | ||
1637 | return; | ||
1638 | path_invert (path); | ||
1639 | GMP_add_path (peer, path, trusted); | ||
1640 | } | ||
1641 | |||
1642 | |||
1643 | /** | ||
1644 | * Adds a path to the info of all the peers in the path | ||
1645 | * | ||
1646 | * @param p Path to process. | ||
1647 | * @param confirmed Whether we know if the path works or not. | ||
1648 | */ | ||
1649 | void | ||
1650 | GMP_add_path_to_all (struct MeshPeerPath *p, int confirmed) | ||
1651 | { | ||
1652 | unsigned int i; | ||
1653 | |||
1654 | /* TODO: invert and add */ | ||
1655 | for (i = 0; i < p->length && p->peers[i] != my_short_id; i++) /* skip'em */ ; | ||
1656 | for (i++; i < p->length; i++) | ||
1657 | { | ||
1658 | struct MeshPeer *aux; | ||
1659 | struct MeshPeerPath *copy; | ||
1660 | |||
1661 | aux = peer_get_short (p->peers[i]); | ||
1662 | copy = path_duplicate (p); | ||
1663 | copy->length = i + 1; | ||
1664 | GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed); | ||
1665 | } | ||
1666 | } | ||
1667 | |||
1668 | |||
1669 | /** | ||
1661 | * Remove a connection from a neighboring peer. | 1670 | * Remove a connection from a neighboring peer. |
1662 | * | 1671 | * |
1663 | * @param peer Peer to remove connection from. | 1672 | * @param peer Peer to remove connection from. |
diff --git a/src/mesh/gnunet-service-mesh_peer.h b/src/mesh/gnunet-service-mesh_peer.h index c898af351..3f31069dc 100644 --- a/src/mesh/gnunet-service-mesh_peer.h +++ b/src/mesh/gnunet-service-mesh_peer.h | |||
@@ -142,6 +142,43 @@ int | |||
142 | GMP_add_connection (struct MeshPeer *peer, struct MeshConnection *c); | 142 | GMP_add_connection (struct MeshPeer *peer, struct MeshConnection *c); |
143 | 143 | ||
144 | /** | 144 | /** |
145 | * Add the path to the peer and update the path used to reach it in case this | ||
146 | * is the shortest. | ||
147 | * | ||
148 | * @param peer_info Destination peer to add the path to. | ||
149 | * @param path New path to add. Last peer must be the peer in arg 1. | ||
150 | * Path will be either used of freed if already known. | ||
151 | * @param trusted Do we trust that this path is real? | ||
152 | */ | ||
153 | void | ||
154 | GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *p, int trusted); | ||
155 | |||
156 | /** | ||
157 | * Add the path to the origin peer and update the path used to reach it in case | ||
158 | * this is the shortest. | ||
159 | * The path is given in peer_info -> destination, therefore we turn the path | ||
160 | * upside down first. | ||
161 | * | ||
162 | * @param peer_info Peer to add the path to, being the origin of the path. | ||
163 | * @param path New path to add after being inversed. | ||
164 | * Path will be either used or freed. | ||
165 | * @param trusted Do we trust that this path is real? | ||
166 | */ | ||
167 | void | ||
168 | GMP_add_path_to_origin (struct MeshPeer *peer_info, | ||
169 | struct MeshPeerPath *path, | ||
170 | int trusted); | ||
171 | |||
172 | /** | ||
173 | * Adds a path to the info of all the peers in the path | ||
174 | * | ||
175 | * @param p Path to process. | ||
176 | * @param confirmed Whether we know if the path works or not. | ||
177 | */ | ||
178 | void | ||
179 | GMP_add_path_to_all (struct MeshPeerPath *p, int confirmed); | ||
180 | |||
181 | /** | ||
145 | * Remove a connection from a neighboring peer. | 182 | * Remove a connection from a neighboring peer. |
146 | * | 183 | * |
147 | * @param peer Peer to remove connection from. | 184 | * @param peer Peer to remove connection from. |