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"
33 * FIXME: Check if its better to store pointer to friend rather than storing
34 * peer identity next_hop or prev_hop.
35 * keep entries in destnation and source peer also. so when we send the trail
36 * teardown message then we don't know the source but if source gets the message
37 * then it shold remove that trail id from its finger table. But how does
38 * source know what is the desination finger ? It will whenevr contact a trail
39 * will do a lookup in routing table and if no trail id present the remove
40 * that trail of the finger and if only one trail then remove the finger.
41 * because of this use case of trail teardown I think trail compression
42 * and trail teardown should not be merged.
43 * 2. store a pointer to friendInfo in place o peer identity.
46 * Maximum number of entries in routing table.
48 #define ROUTING_TABLE_THRESHOLD 64
51 * Routing table entry .
56 * Global Unique identifier of the trail.
58 struct GNUNET_HashCode trail_id;
61 * The peer to which this request should be passed to.
63 struct GNUNET_PeerIdentity next_hop;
66 * Peer just before next hop in the trail.
68 struct GNUNET_PeerIdentity prev_hop;
72 * Routing table of the peer
74 static struct GNUNET_CONTAINER_MultiHashMap *routing_table;
77 * Update the prev. hop of the trail. Call made by trail compression where
78 * if you are the first friend now in the trail then you need to update
81 * @return #GNUNET_OK success
82 * #GNUNET_SYSERR in case no matching entry found in routing table.
85 GDS_ROUTING_update_trail_prev_hop (const struct GNUNET_HashCode trail_id,
86 struct GNUNET_PeerIdentity prev_hop)
88 struct RoutingTrail *trail;
90 trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
95 trail->prev_hop = prev_hop;
100 * Update the next hop of the trail. Call made by trail compression where
101 * if you are source of the trail and now you have a new first friend, then
102 * you should update the trail.
104 * @return #GNUNET_OK success
105 * #GNUNET_SYSERR in case no matching entry found in routing table.
108 GDS_ROUTING_update_trail_next_hop (const struct GNUNET_HashCode trail_id,
109 struct GNUNET_PeerIdentity next_hop)
111 struct RoutingTrail *trail;
113 trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
116 return GNUNET_SYSERR;
118 trail->next_hop = next_hop;
123 * Get the next hop for trail corresponding to trail_id
124 * @param trail_id Trail id to be searched.
125 * @return Next_hop if found
126 * NULL If next hop not found.
128 struct GNUNET_PeerIdentity *
129 GDS_ROUTING_get_next_hop (const struct GNUNET_HashCode trail_id,
130 enum GDS_ROUTING_trail_direction trail_direction)
132 struct RoutingTrail *trail;
134 trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
139 switch (trail_direction)
141 case GDS_ROUTING_SRC_TO_DEST:
142 return &(trail->next_hop);
143 case GDS_ROUTING_DEST_TO_SRC:
144 return &(trail->prev_hop);
151 * Remove trail with trail_id
152 * @param trail_id Trail id to be removed
153 * @return #GNUNET_YES success
154 * #GNUNET_NO if entry not found.
157 GDS_ROUTING_remove_trail (const struct GNUNET_HashCode remove_trail_id)
159 struct RoutingTrail *remove_entry;
161 remove_entry = GNUNET_CONTAINER_multihashmap_get (routing_table, &remove_trail_id);
163 if (NULL == remove_entry)
166 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (routing_table,
170 GNUNET_free (remove_entry);
178 * Iterate over routing table and remove entries with value as part of any trail.
181 * @param key current public key
182 * @param value value in the hash map
183 * @return #GNUNET_YES if we should continue to iterate,
186 static int remove_matching_trails (void *cls,
187 const struct GNUNET_HashCode *key,
190 struct RoutingTrail *remove_trail = value;
191 struct GNUNET_PeerIdentity *disconnected_peer = cls;
192 struct GNUNET_HashCode trail_id = *key;
193 struct GNUNET_PeerIdentity my_identity;
195 /* If disconnected_peer is next_hop, then send a trail teardown message through
196 * prev_hop in direction from destination to source. */
197 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_trail->next_hop,
200 my_identity = GDS_NEIGHBOURS_get_my_id ();
201 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
202 &remove_trail->prev_hop))
204 GDS_NEIGHBOURS_send_trail_teardown (trail_id,
205 GDS_ROUTING_DEST_TO_SRC,
206 remove_trail->prev_hop);
210 /* If disconnected_peer is prev_hop, then send a trail teardown through
211 * next_hop in direction from Source to Destination. */
212 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_trail->prev_hop,
215 my_identity = GDS_NEIGHBOURS_get_my_id ();
216 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
217 &remove_trail->next_hop))
219 GDS_NEIGHBOURS_send_trail_teardown (trail_id,
220 GDS_ROUTING_SRC_TO_DEST,
221 remove_trail->next_hop);
225 GNUNET_assert (GNUNET_YES ==
226 GNUNET_CONTAINER_multihashmap_remove (routing_table,
229 GNUNET_free (remove_trail);
236 * Remove after using.
239 GDS_ROUTING_test_print (void)
241 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
242 struct RoutingTrail *trail;
243 struct GNUNET_PeerIdentity print_peer;
244 struct GNUNET_HashCode key_ret;
247 FPRINTF (stderr,_("\nSUPU ***PRINTING ROUTING TABLE *****"));
248 iter =GNUNET_CONTAINER_multihashmap_iterator_create (routing_table);
249 for (i = 0; i < GNUNET_CONTAINER_multihashmap_size(routing_table); i++)
251 if(GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter,
253 (const void **)&trail))
255 FPRINTF (stderr,_("\nSUPU %s, %s, %d, trail->trail_id = %s"),
256 __FILE__, __func__,__LINE__, GNUNET_h2s(&trail->trail_id));
257 memcpy (&print_peer, &trail->next_hop, sizeof (struct GNUNET_PeerIdentity));
258 FPRINTF (stderr,_("\nSUPU %s, %s, %d, trail->next_hop = %s"),
259 __FILE__, __func__,__LINE__, GNUNET_i2s(&print_peer));
260 memcpy (&print_peer, &trail->prev_hop, sizeof (struct GNUNET_PeerIdentity));
261 FPRINTF (stderr,_("\nSUPU %s, %s, %d, trail->prev_hop = %s"),
262 __FILE__, __func__,__LINE__, GNUNET_i2s(&print_peer));
268 * Remove every trail where peer is either next_hop or prev_hop. Also send a
269 * trail teardown message in direction of hop which is not disconnected.
270 * @param peer Peer identity. Trail containing this peer should be removed.
273 GDS_ROUTING_remove_trail_by_peer (const struct GNUNET_PeerIdentity *peer)
275 /* No entries in my routing table. */
276 if (0 == GNUNET_CONTAINER_multihashmap_size(routing_table))
279 GNUNET_CONTAINER_multihashmap_iterate (routing_table, &remove_matching_trails,
285 * Add a new entry in routing table
286 * @param new_trail_id
289 * @return #GNUNET_OK success
290 * #GNUNET_SYSERR in case new_trail_id already exists in the network
291 * but with different prev_hop/next_hop
294 GDS_ROUTING_add (struct GNUNET_HashCode new_trail_id,
295 struct GNUNET_PeerIdentity prev_hop,
296 struct GNUNET_PeerIdentity next_hop)
298 struct RoutingTrail *new_entry;
300 new_entry = GNUNET_new (struct RoutingTrail);
301 new_entry->trail_id = new_trail_id;
302 new_entry->next_hop = next_hop;
303 new_entry->prev_hop = prev_hop;
305 return GNUNET_CONTAINER_multihashmap_put (routing_table,
306 &new_trail_id, new_entry,
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
312 * Check if the size of routing table has crossed ROUTING_TABLE_THRESHOLD.
313 * It means that I don't have any more space in my routing table and I can not
314 * be part of any more trails till there is free space in my routing table.
315 * @return #GNUNET_YES, if threshold crossed else #GNUNET_NO.
318 GDS_ROUTING_threshold_reached (void)
320 return (GNUNET_CONTAINER_multihashmap_size(routing_table) >
321 ROUTING_TABLE_THRESHOLD) ? GNUNET_YES:GNUNET_NO;
326 * Initialize routing subsystem.
329 GDS_ROUTING_init (void)
331 routing_table = GNUNET_CONTAINER_multihashmap_create (ROUTING_TABLE_THRESHOLD * 4 / 3,
337 * Shutdown routing subsystem.
340 GDS_ROUTING_done (void)
342 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (routing_table));
343 GNUNET_CONTAINER_multihashmap_destroy (routing_table);
346 /* end of gnunet-service-xdht_routing.c */