2 This file is part of GNUnet.
3 (C) 2011 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 * Number of requests we track at most (for routing replies).
39 #define DHT_MAX_RECENT (1024 * 16)
42 * Maximum number of entries in routing table.
44 #define ROUTING_TABLE_THRESHOLD 64
47 * Routing table entry .
54 struct GNUNET_PeerIdentity source;
59 struct GNUNET_PeerIdentity destination;
62 * The peer to which this request should be passed to.
64 struct GNUNET_PeerIdentity next_hop;
67 * Peer just before next hop in the trail.
69 struct GNUNET_PeerIdentity prev_hop;
75 * Routing table of the peer
77 static struct GNUNET_CONTAINER_MultiPeerMap *routing_table;
80 * Iterate over routing table and remove entries for which peer is a part.
82 * @param key current public key
83 * @param value value in the hash map
84 * @return #GNUNET_YES if we should continue to
89 remove_routing_entry (void *cls,
90 const struct GNUNET_PeerIdentity *key,
93 struct RoutingTrail *remove_entry = value;
94 const struct GNUNET_PeerIdentity *disconnected_peer = cls;
96 if ((0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->source), disconnected_peer)) ||
97 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->destination), disconnected_peer)) ||
98 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->next_hop), disconnected_peer)) ||
99 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->prev_hop), disconnected_peer)))
101 GNUNET_assert (GNUNET_YES ==
102 GNUNET_CONTAINER_multipeermap_remove (routing_table,
111 * Iterate over multiple entries for same destination value and get
112 * the correct next hop.
113 * @param cls struct RoutingTrail
114 * @param key Destination identity
115 * @param value struct RoutingTrail
116 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the next hop
119 get_next_hop (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
121 /* Here you should match if source, prev hop matches if yes then send
122 GNUNET_NO as you don't need to check more entries. */
123 struct RoutingTrail *request = cls;
124 struct RoutingTrail *existing_entry = (struct RoutingTrail *)value;
126 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->source), &(existing_entry->source)))
128 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->prev_hop), &(existing_entry->prev_hop)))
130 memcpy (&(request->next_hop), &(existing_entry->next_hop), sizeof (struct GNUNET_PeerIdentity));
139 * Add a new entry to our routing table.
140 * @param source peer Source of the trail.
141 * @param destintation Destination of the trail.
142 * @param next_hop Next peer to forward the message to reach the destination.
144 * GNUNET_SYSERR If the number of routing entries crossed thershold.
147 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *source,
148 const struct GNUNET_PeerIdentity *dest,
149 const struct GNUNET_PeerIdentity *next_hop,
150 struct GNUNET_PeerIdentity *prev_hop)
152 struct RoutingTrail *new_routing_entry;
154 if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD)
155 return GNUNET_SYSERR;
157 new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
158 memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity));
159 memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
160 memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity));
161 memcpy (&(new_routing_entry->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
163 GNUNET_assert (GNUNET_OK ==
164 GNUNET_CONTAINER_multipeermap_put (routing_table,
165 dest, new_routing_entry,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
173 * Iterate over routing table and remove entries for which peer is a part.
178 GDS_ROUTING_remove_entry (const struct GNUNET_PeerIdentity *peer)
180 GNUNET_CONTAINER_multipeermap_iterate (routing_table, &remove_routing_entry,
185 /** FIXME:TODO URGNET Need to understand if we need next hop to uniquely identify an entry
186 * in routing table or not.
187 * Remove the trail as result of trail tear down message.
188 * @param source_peer Source of the trail.
189 * @param destination_peer Destination of the trail.
190 * @param next_hop Next hop
191 * @param prev_hop Previous hop.
194 GDS_ROUTING_remove_trail (struct GNUNET_PeerIdentity *source_peer,
195 struct GNUNET_PeerIdentity *destination_peer,
196 const struct GNUNET_PeerIdentity *prev_hop)
202 * Find the next hop to send packet to.
203 * @param source_peer Source of the trail.
204 * @param destination_peer Destination of the trail.
205 * @param prev_hop Previous hop in the trail.
206 * @return Next hop in the trail from source to destination.
208 struct GNUNET_PeerIdentity *
209 GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer,
210 struct GNUNET_PeerIdentity *destination_peer,
211 const struct GNUNET_PeerIdentity *prev_hop)
213 struct RoutingTrail *trail;
214 trail = GNUNET_malloc (sizeof (struct RoutingTrail));
215 memcpy (&(trail->destination), destination_peer, sizeof (struct GNUNET_PeerIdentity));
216 memcpy (&(trail->source), source_peer, sizeof (struct GNUNET_PeerIdentity));
217 memcpy (&(trail->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
219 GNUNET_CONTAINER_multipeermap_get_multiple (routing_table, destination_peer,
220 get_next_hop, trail);
222 return &(trail->next_hop);
229 * Check if the size of routing table has crossed threshold.
233 GDS_ROUTING_check_threshold ()
236 ret = (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) ? 1:0;
242 * Initialize routing subsystem.
245 GDS_ROUTING_init (void)
247 routing_table = GNUNET_CONTAINER_multipeermap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
252 * Shutdown routing subsystem.
255 GDS_ROUTING_done (void)
257 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
258 GNUNET_CONTAINER_multipeermap_destroy (routing_table);
261 /* end of gnunet-service-xdht_routing.c */