aboutsummaryrefslogtreecommitdiff
path: root/src/cadet
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2014-10-15 00:55:38 +0000
committerBart Polot <bart@net.in.tum.de>2014-10-15 00:55:38 +0000
commit3dfe0b93bba61ca11519adca8a901fd07563612c (patch)
tree9ed9a368c72832c646cb39b08de59a5cfcd66e89 /src/cadet
parentf00081f2abb7dd098f6c4433ca6117ce90c1adc7 (diff)
downloadgnunet-3dfe0b93bba61ca11519adca8a901fd07563612c.tar.gz
gnunet-3dfe0b93bba61ca11519adca8a901fd07563612c.zip
Use optimizaiton of paths on store from DHT as well as on receipt from CONN_CREATE
Diffstat (limited to 'src/cadet')
-rw-r--r--src/cadet/cadet_path.c66
-rw-r--r--src/cadet/cadet_path.h16
-rw-r--r--src/cadet/gnunet-service-cadet_connection.c65
-rw-r--r--src/cadet/gnunet-service-cadet_dht.c78
4 files changed, 98 insertions, 127 deletions
diff --git a/src/cadet/cadet_path.c b/src/cadet/cadet_path.c
index f378e124e..7e950a5ce 100644
--- a/src/cadet/cadet_path.c
+++ b/src/cadet/cadet_path.c
@@ -28,6 +28,9 @@
28#include "cadet_path.h" 28#include "cadet_path.h"
29#include "gnunet-service-cadet_peer.h" 29#include "gnunet-service-cadet_peer.h"
30 30
31#define LOG(level, ...) GNUNET_log_from (level,"cadet-pth",__VA_ARGS__)
32
33
31/** 34/**
32 * @brief Destroy a path after some time has past. 35 * @brief Destroy a path after some time has past.
33 * 36 *
@@ -147,6 +150,69 @@ path_invalidate (struct CadetPeerPath *p)
147 150
148 151
149/** 152/**
153 * Builds a path from a PeerIdentity array.
154 *
155 * @param peers PeerIdentity array.
156 * @param size Size of the @c peers array.
157 * @param myid ID of local peer, to find @c own_pos.
158 * @param own_pos Output parameter: own position in the path.
159 *
160 * @return Fixed and shortened path.
161 */
162struct CadetPeerPath *
163path_build_from_peer_ids (struct GNUNET_PeerIdentity *peers,
164 unsigned int size,
165 GNUNET_PEER_Id myid,
166 unsigned int *own_pos)
167{
168 struct CadetPeerPath *path;
169 GNUNET_PEER_Id shortid;
170 unsigned int i;
171 unsigned int j;
172 unsigned int offset;
173
174 /* Create path */
175 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
176 path = path_new (size);
177 *own_pos = 0;
178 offset = 0;
179 for (i = 0; i < size; i++)
180 {
181 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
182 i, GNUNET_i2s (&peers[i]));
183 shortid = GNUNET_PEER_intern (&peers[i]);
184
185 /* Check for loops / duplicates */
186 for (j = 0; j < i - offset; j++)
187 {
188 if (path->peers[j] == shortid)
189 {
190 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
191 offset = i - j;
192 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
193 GNUNET_PEER_change_rc (shortid, -1);
194 }
195 }
196 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
197 path->peers[i - offset] = shortid;
198 if (path->peers[i - offset] == myid)
199 *own_pos = i - offset;
200 }
201 path->length -= offset;
202
203 if (path->peers[*own_pos] != myid)
204 {
205 /* create path: self not found in path through self */
206 GNUNET_break_op (0);
207 path_destroy (path);
208 return NULL;
209 }
210
211 return path;
212}
213
214
215/**
150 * Test if a path is valid (or at least not known to be invalid). 216 * Test if a path is valid (or at least not known to be invalid).
151 * 217 *
152 * @param path Path to test. 218 * @param path Path to test.
diff --git a/src/cadet/cadet_path.h b/src/cadet/cadet_path.h
index 9991b380d..6281ddc05 100644
--- a/src/cadet/cadet_path.h
+++ b/src/cadet/cadet_path.h
@@ -159,6 +159,22 @@ int
159path_destroy (struct CadetPeerPath *p); 159path_destroy (struct CadetPeerPath *p);
160 160
161/** 161/**
162 * Builds a path from a PeerIdentity array.
163 *
164 * @param peers PeerIdentity array.
165 * @param size Size of the @c peers array.
166 * @param myid ID of local peer, to find @c own_pos.
167 * @param own_pos Output parameter: own position in the path.
168 *
169 * @return Fixed and shortened path.
170 */
171struct CadetPeerPath *
172path_build_from_peer_ids (struct GNUNET_PeerIdentity *peers,
173 unsigned int size,
174 GNUNET_PEER_Id myid,
175 unsigned int *own_pos);
176
177/**
162 * Path -> allocated one line string. Caller must free. 178 * Path -> allocated one line string. Caller must free.
163 * 179 *
164 * @param p Path. 180 * @param p Path.
diff --git a/src/cadet/gnunet-service-cadet_connection.c b/src/cadet/gnunet-service-cadet_connection.c
index a7a3b9121..147bf1e8e 100644
--- a/src/cadet/gnunet-service-cadet_connection.c
+++ b/src/cadet/gnunet-service-cadet_connection.c
@@ -1517,67 +1517,6 @@ add_to_peer (struct CadetConnection *c, struct CadetPeer *peer)
1517 1517
1518 1518
1519/** 1519/**
1520 * Builds a path from a PeerIdentity array.
1521 *
1522 * @param peers PeerIdentity array.
1523 * @param size Size of the @c peers array.
1524 * @param own_pos Output parameter: own position in the path.
1525 *
1526 * @return Fixed and shortened path.
1527 */
1528static struct CadetPeerPath *
1529build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1530 unsigned int size,
1531 unsigned int *own_pos)
1532{
1533 struct CadetPeerPath *path;
1534 GNUNET_PEER_Id shortid;
1535 unsigned int i;
1536 unsigned int j;
1537 unsigned int offset;
1538
1539 /* Create path */
1540 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1541 path = path_new (size);
1542 *own_pos = 0;
1543 offset = 0;
1544 for (i = 0; i < size; i++)
1545 {
1546 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1547 i, GNUNET_i2s (&peers[i]));
1548 shortid = GNUNET_PEER_intern (&peers[i]);
1549
1550 /* Check for loops / duplicates */
1551 for (j = 0; j < i - offset; j++)
1552 {
1553 if (path->peers[j] == shortid)
1554 {
1555 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1556 offset = i - j;
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
1558 GNUNET_PEER_change_rc (shortid, -1);
1559 }
1560 }
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1562 path->peers[i - offset] = shortid;
1563 if (path->peers[i - offset] == myid)
1564 *own_pos = i - offset;
1565 }
1566 path->length -= offset;
1567
1568 if (path->peers[*own_pos] != myid)
1569 {
1570 /* create path: self not found in path through self */
1571 GNUNET_break_op (0);
1572 path_destroy (path);
1573 return NULL;
1574 }
1575
1576 return path;
1577}
1578
1579
1580/**
1581 * Log receipt of message on stderr (INFO level). 1520 * Log receipt of message on stderr (INFO level).
1582 * 1521 *
1583 * @param message Message received. 1522 * @param message Message received.
@@ -1656,8 +1595,8 @@ GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1656 c = connection_get (cid); 1595 c = connection_get (cid);
1657 if (NULL == c) 1596 if (NULL == c)
1658 { 1597 {
1659 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1], 1598 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1660 size, &own_pos); 1599 size, myid, &own_pos);
1661 if (NULL == path) 1600 if (NULL == path)
1662 return GNUNET_OK; 1601 return GNUNET_OK;
1663 1602
diff --git a/src/cadet/gnunet-service-cadet_dht.c b/src/cadet/gnunet-service-cadet_dht.c
index 7d31603df..f18e868e3 100644
--- a/src/cadet/gnunet-service-cadet_dht.c
+++ b/src/cadet/gnunet-service-cadet_dht.c
@@ -119,77 +119,27 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
119 const struct GNUNET_PeerIdentity *put_path, 119 const struct GNUNET_PeerIdentity *put_path,
120 unsigned int put_path_length) 120 unsigned int put_path_length)
121{ 121{
122 size_t size = get_path_length + put_path_length;
123 struct GNUNET_PeerIdentity peers[size];
124 const struct GNUNET_PeerIdentity *peer;
122 struct CadetPeerPath *p; 125 struct CadetPeerPath *p;
123 GNUNET_PEER_Id id; 126 unsigned int own_pos;
124 int i; 127 int i;
125 128
126 p = path_new (1); 129 LOG (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", get_path_length);
127 p->peers[0] = myid; 130 for (i = 0 ; i < get_path_length; i++)
128 GNUNET_PEER_change_rc (myid, 1);
129 i = get_path_length;
130 LOG (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
131 for (i--; i >= 0; i--)
132 { 131 {
133 id = GNUNET_PEER_intern (&get_path[i]); 132 peer = &get_path[get_path_length - i - 1];
134 if (p->length > 0 && id == p->peers[p->length - 1]) 133 LOG (GNUNET_ERROR_TYPE_DEBUG, " From GET: %s\n", GNUNET_i2s (peer));
135 { 134 peers[i] = *peer;
136 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
137 GNUNET_PEER_change_rc (id, -1);
138 }
139 else
140 {
141 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
142 GNUNET_i2s (&get_path[i]));
143 p->length++;
144 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
145 p->peers[p->length - 1] = id;
146 }
147 } 135 }
148 i = put_path_length; 136 for (i = 0 ; i < put_path_length; i++)
149 LOG (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
150 for (i--; i >= 0; i--)
151 { 137 {
152 id = GNUNET_PEER_intern (&put_path[i]); 138 peer = &put_path[put_path_length - i - 1];
153 if (id == myid) 139 LOG (GNUNET_ERROR_TYPE_DEBUG, " From PUT: %s\n", GNUNET_i2s (peer));
154 { 140 peers[i + get_path_length] = *peer;
155 /* PUT path went through us, so discard the path up until now and start
156 * from here to get a much shorter (and loop-free) path.
157 */
158 path_destroy (p);
159 p = path_new (0);
160 }
161 if (p->length > 0 && id == p->peers[p->length - 1])
162 {
163 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
164 GNUNET_PEER_change_rc (id, -1);
165 }
166 else
167 {
168 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
169 GNUNET_i2s (&put_path[i]));
170 p->length++;
171 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
172 p->peers[p->length - 1] = id;
173 }
174 } 141 }
175#if CADET_DEBUG 142 p = path_build_from_peer_ids (peers, size, myid, &own_pos);
176 if (get_path_length > 0)
177 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
178 GNUNET_i2s (&get_path[0]));
179 if (put_path_length > 0)
180 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
181 GNUNET_i2s (&put_path[0]));
182 LOG (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
183 p->length);
184 for (i = 0; i < p->length; i++)
185 {
186 struct GNUNET_PeerIdentity peer_id;
187
188 GNUNET_PEER_resolve (p->peers[i], &peer_id);
189 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
190 GNUNET_i2s (&peer_id));
191 }
192#endif
193 return p; 143 return p;
194} 144}
195 145