2 This file is part of GNUnet.
3 (C) 2011 - 2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-xdht_routing.c
23 * @brief GNUnet DHT tracking of requests for routing replies
24 * @author Supriti Singh
27 #include "gnunet-service-xdht_neighbours.h"
28 #include "gnunet-service-xdht_routing.h"
29 #include "gnunet-service-xdht.h"
32 1. to understand if we really need all the four fields.
33 2. if we can merge remove_peer and remove_trail
34 3. do we need next_hop to uniquely identify a trail in remove_trail. */
37 * Maximum number of entries in routing table.
39 #define ROUTING_TABLE_THRESHOLD 64
42 * Routing table entry .
49 struct GNUNET_PeerIdentity source;
54 struct GNUNET_PeerIdentity destination;
57 * The peer to which this request should be passed to.
59 struct GNUNET_PeerIdentity next_hop;
62 * Peer just before next hop in the trail.
64 struct GNUNET_PeerIdentity prev_hop;
70 * Routing table of the peer
72 static struct GNUNET_CONTAINER_MultiPeerMap *routing_table;
76 * Get next hop from the trail with source peer, destination peer and next hop
77 * same as the argument to this function.
78 * @param source_peer Source peer of the trail.
79 * @param destination_peer Destination peer of the trail.
80 * @param prev_hop Previous hop of the trail.
81 * @return #GNUNET_YES if we found the matching trail.
82 * #GNUNET_NO if we found no matching trail.
85 get_next_hop (struct RoutingTrail *trail,
86 struct GNUNET_PeerIdentity *source_peer,
87 struct GNUNET_PeerIdentity *destination_peer,
88 const struct GNUNET_PeerIdentity *prev_hop)
90 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->source),source_peer))
92 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->prev_hop),prev_hop))
104 * FIXME: How to ensure that with only 3 fields also we have a unique trail.
105 * in case of redundant routes we can have different next hop.
106 * in that case we have to call this function on each entry of routing table
107 * and from multiple next hop we return one. Here also we are going to return one.
109 * Assumption - there can be only on one trail with all these fields. But if
110 * we consider only 3 fields then it is possible that next hop is differet.
111 * Update prev_hop field to source_peer. Trail from source peer to destination
112 * peer is compressed such that I am the first friend in the trail.
113 * @param source_peer Source of the trail.
114 * @param destination_peer Destination of the trail.
115 * @param prev_hop Peer before me in the trail.
116 * @return #GNUNET_YES trail is updated.
117 * #GNUNET_NO, trail not found.
120 GDS_ROUTING_trail_update (struct GNUNET_PeerIdentity *source_peer,
121 struct GNUNET_PeerIdentity *destination_peer,
122 const struct GNUNET_PeerIdentity *prev_hop)
124 /* 1. find the trail corresponding to these values.
125 2. update the prev hop to source peer. */
126 struct RoutingTrail *trail;
127 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
130 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
131 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
133 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
134 (const void **)&trail))
136 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->destination), destination_peer))
138 if (GNUNET_YES == get_next_hop (trail, source_peer, destination_peer, prev_hop))
140 memcpy (&(trail->prev_hop), source_peer, sizeof (struct GNUNET_PeerIdentity));
151 * Find the next hop to send packet to.
152 * @param source_peer Source of the trail.
153 * @param destination_peer Destination of the trail.
154 * @param prev_hop Previous hop in the trail.
155 * @return Next hop in the trail from source to destination.
157 struct GNUNET_PeerIdentity *
158 GDS_ROUTING_search (struct GNUNET_PeerIdentity *source_peer,
159 struct GNUNET_PeerIdentity *destination_peer,
160 const struct GNUNET_PeerIdentity *prev_hop)
162 struct RoutingTrail *trail;
163 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
166 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
167 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
169 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
170 (const void **)&trail))
172 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->destination), destination_peer))
174 if (GNUNET_YES == get_next_hop (trail, source_peer, destination_peer, prev_hop))
176 return &(trail->next_hop);
181 GNUNET_CONTAINER_multipeermap_iterator_destroy (iterator);
187 * Add a new entry to our routing table.
188 * @param source peer Source of the trail.
189 * @param destintation Destination of the trail.
190 * @param next_hop Next peer to forward the message to reach the destination.
192 * GNUNET_SYSERR If the number of routing entries crossed thershold.
195 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *source,
196 const struct GNUNET_PeerIdentity *dest,
197 const struct GNUNET_PeerIdentity *next_hop,
198 struct GNUNET_PeerIdentity *prev_hop)
200 struct RoutingTrail *new_routing_entry;
202 if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD)
203 return GNUNET_SYSERR;
205 new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
206 memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity));
207 memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
208 memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity));
209 memcpy (&(new_routing_entry->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
211 GNUNET_assert (GNUNET_OK ==
212 GNUNET_CONTAINER_multipeermap_put (routing_table,
213 dest, new_routing_entry,
214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
221 * Iterate over routing table and remove entries for which peer is a part.
223 * @param key current public key
224 * @param value value in the hash map
225 * @return #GNUNET_YES if we should continue to
230 remove_routing_entry (void *cls,
231 const struct GNUNET_PeerIdentity *key,
234 struct RoutingTrail *remove_entry = value;
235 const struct GNUNET_PeerIdentity *disconnected_peer = cls;
237 if ((0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->source), disconnected_peer)) ||
238 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->destination), disconnected_peer)) ||
239 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->next_hop), disconnected_peer)) ||
240 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->prev_hop), disconnected_peer)))
242 GNUNET_assert (GNUNET_YES ==
243 GNUNET_CONTAINER_multipeermap_remove (routing_table,
252 * FIXME: add a return value.
253 * Iterate over routing table and remove all entries for which peer is a part.
254 * @param peer Peer to be searched for in the trail to remove that trail.
257 GDS_ROUTING_remove_entry (const struct GNUNET_PeerIdentity *peer)
259 GNUNET_CONTAINER_multipeermap_iterate (routing_table, &remove_routing_entry,
265 * In response to trail teardown message, remove the trail with source peer,
266 * destination peer and next hop same as the argument to this function.
267 * Assumption - there can be only one possible trail with these 4 values.
268 * @param source_peer Source of the trail.
269 * @param destination_peer Destination of the trail.
270 * @param next_hop Next hop
271 * @param prev_hop Previous hop.
272 * @return #GNUNET_YES Matching trail deleted from routing table.
273 * #GNUNET_NO No matching trail found.
277 GDS_ROUTING_remove_trail (struct GNUNET_PeerIdentity *source_peer,
278 struct GNUNET_PeerIdentity *destination_peer,
279 const struct GNUNET_PeerIdentity *prev_hop)
281 struct RoutingTrail *trail;
282 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
285 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
286 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
288 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
289 (const void **)&trail))
291 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->destination), destination_peer))
293 GNUNET_assert (GNUNET_YES ==
294 GNUNET_CONTAINER_multipeermap_remove (routing_table,
295 &(trail->destination),
301 GNUNET_CONTAINER_multipeermap_iterator_destroy (iterator);
308 * Check if the size of routing table has crossed threshold.
309 * @return #GNUNET_YES, if threshold crossed else #GNUNET_NO.
312 GDS_ROUTING_check_threshold ()
314 return (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) ?
315 GNUNET_YES:GNUNET_NO;
320 * Initialize routing subsystem.
323 GDS_ROUTING_init (void)
325 routing_table = GNUNET_CONTAINER_multipeermap_create (ROUTING_TABLE_THRESHOLD * 4 / 3,
333 GDS_ROUTING_print (void)
335 struct RoutingTrail *trail;
336 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing table entries \n");
340 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
341 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
343 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
344 (const void **)&trail))
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->source)));
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->destination)));
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->next_hop)));
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->prev_hop)));
355 * FIXME: here you can have routing table with size 0, only when you delete
356 * the entries correctly. Possible scenarios where we delete the entries are
357 * 1. when one of my friend gets disconnected then I remove any trail (does not
358 * matter if that friend is source, destination, next hop or previous hop).
359 * 2. if I get a trail teardown message then I remove the entry.
360 * Is there any other case that I may have missed?
361 * Shutdown routing subsystem.
364 GDS_ROUTING_done (void)
366 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
367 GNUNET_CONTAINER_multipeermap_destroy (routing_table);
370 /* end of gnunet-service-xdht_routing.c */