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 * It can happen that a particular peer removed the entry because one of the peers
152 * (source, destination, next , prev) was friend which got disconnected. So,
153 * no matching trail is found. In this case we return NULL. Its on calling function
154 * to handle the return value.
155 * Find the next hop to send packet to.
156 * @param source_peer Source of the trail.
157 * @param destination_peer Destination of the trail.
158 * @param prev_hop Previous hop in the trail.
159 * @return Next hop in the trail from source to destination.
160 * NULL in case no matching trail found in routing table.
162 struct GNUNET_PeerIdentity *
163 GDS_ROUTING_search (struct GNUNET_PeerIdentity *source_peer,
164 struct GNUNET_PeerIdentity *destination_peer,
165 const struct GNUNET_PeerIdentity *prev_hop)
167 struct RoutingTrail *trail;
168 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
171 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
172 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
174 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
175 (const void **)&trail))
177 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->destination), destination_peer))
179 if (GNUNET_YES == get_next_hop (trail, source_peer, destination_peer, prev_hop))
181 return &(trail->next_hop);
186 GNUNET_CONTAINER_multipeermap_iterator_destroy (iterator);
192 * FIXME: first search in routing table and if same entry found then don't add
194 * Add a new entry to our routing table.
195 * @param source peer Source of the trail.
196 * @param destintation Destination of the trail.
197 * @param next_hop Next peer to forward the message to reach the destination.
199 * GNUNET_SYSERR If the number of routing entries crossed thershold.
202 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *source,
203 const struct GNUNET_PeerIdentity *dest,
204 const struct GNUNET_PeerIdentity *next_hop,
205 const struct GNUNET_PeerIdentity *prev_hop)
207 struct RoutingTrail *new_routing_entry;
209 if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD)
210 return GNUNET_SYSERR;
212 new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
213 memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity));
214 memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
215 memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity));
216 memcpy (&(new_routing_entry->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
218 GNUNET_assert (GNUNET_OK ==
219 GNUNET_CONTAINER_multipeermap_put (routing_table,
220 dest, new_routing_entry,
221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
228 * Iterate over routing table and remove entries for which peer is a part.
230 * @param key current public key
231 * @param value value in the hash map
232 * @return #GNUNET_YES if we should continue to
237 remove_routing_entry (void *cls,
238 const struct GNUNET_PeerIdentity *key,
241 struct RoutingTrail *remove_entry = value;
242 const struct GNUNET_PeerIdentity *disconnected_peer = cls;
244 if ((0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->source), disconnected_peer)) ||
245 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->destination), disconnected_peer)) ||
246 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->next_hop), disconnected_peer)) ||
247 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->prev_hop), disconnected_peer)))
249 GNUNET_assert (GNUNET_YES ==
250 GNUNET_CONTAINER_multipeermap_remove (routing_table,
259 * FIXME: add a return value.
260 * Iterate over routing table and remove all entries for which peer is a part.
261 * @param peer Peer to be searched for in the trail to remove that trail.
264 GDS_ROUTING_remove_entry (const struct GNUNET_PeerIdentity *peer)
266 GNUNET_CONTAINER_multipeermap_iterate (routing_table, &remove_routing_entry,
272 * In response to trail teardown message, remove the trail with source peer,
273 * destination peer and next hop same as the argument to this function.
274 * Assumption - there can be only one possible trail with these 4 values.
275 * @param source_peer Source of the trail.
276 * @param destination_peer Destination of the trail.
277 * @param next_hop Next hop
278 * @param prev_hop Previous hop.
279 * @return #GNUNET_YES Matching trail deleted from routing table.
280 * #GNUNET_NO No matching trail found.
284 GDS_ROUTING_remove_trail (struct GNUNET_PeerIdentity *source_peer,
285 struct GNUNET_PeerIdentity *destination_peer,
286 const struct GNUNET_PeerIdentity *prev_hop)
288 struct RoutingTrail *trail;
289 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
292 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
293 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
295 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
296 (const void **)&trail))
298 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(trail->destination), destination_peer))
300 GNUNET_assert (GNUNET_YES ==
301 GNUNET_CONTAINER_multipeermap_remove (routing_table,
302 &(trail->destination),
308 GNUNET_CONTAINER_multipeermap_iterator_destroy (iterator);
315 * Check if the size of routing table has crossed threshold.
316 * @return #GNUNET_YES, if threshold crossed else #GNUNET_NO.
319 GDS_ROUTING_check_threshold ()
321 return (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) ?
322 GNUNET_YES:GNUNET_NO;
327 * Initialize routing subsystem.
330 GDS_ROUTING_init (void)
332 routing_table = GNUNET_CONTAINER_multipeermap_create (ROUTING_TABLE_THRESHOLD * 4 / 3,
340 GDS_ROUTING_print (void)
342 struct RoutingTrail *trail;
343 struct GNUNET_CONTAINER_MultiPeerMapIterator *iterator;
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing table entries \n");
347 iterator = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table);
348 for (i = 0; i< GNUNET_CONTAINER_multipeermap_size(routing_table); i++)
350 if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (iterator, NULL,
351 (const void **)&trail))
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->source)));
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->destination)));
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->next_hop)));
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Routing trail source \n", GNUNET_i2s (&(trail->prev_hop)));
362 * FIXME: here you can have routing table with size 0, only when you delete
363 * the entries correctly. Possible scenarios where we delete the entries are
364 * 1. when one of my friend gets disconnected then I remove any trail (does not
365 * matter if that friend is source, destination, next hop or previous hop).
366 * 2. if I get a trail teardown message then I remove the entry.
367 * Is there any other case that I may have missed?
368 * Shutdown routing subsystem.
371 GDS_ROUTING_done (void)
373 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
374 GNUNET_CONTAINER_multipeermap_destroy (routing_table);
377 /* end of gnunet-service-xdht_routing.c */