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"
33 * Number of requests we track at most (for routing replies).
35 #define DHT_MAX_RECENT (1024 * 16)
38 * Maximum number of entries in routing table.
40 #define ROUTING_TABLE_THRESHOLD 64
43 * Routing table entry .
50 struct GNUNET_PeerIdentity source;
55 struct GNUNET_PeerIdentity destination;
58 * The peer to which this request should be passed to.
60 struct GNUNET_PeerIdentity next_hop;
63 * Peer just before next hop in the trail.
65 struct GNUNET_PeerIdentity prev_hop;
71 * Routing table of the peer
73 static struct GNUNET_CONTAINER_MultiPeerMap *routing_table;
76 * Iterate over routing table and remove entries for which peer is a part.
78 * @param key current public key
79 * @param value value in the hash map
80 * @return #GNUNET_YES if we should continue to
85 remove_routing_entry (void *cls,
86 const struct GNUNET_PeerIdentity *key,
89 struct RoutingTrail *remove_entry = value;
90 const struct GNUNET_PeerIdentity *disconnected_peer = cls;
92 if ((0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->source), disconnected_peer)) ||
93 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->destination), disconnected_peer)) ||
94 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->next_hop), disconnected_peer)) ||
95 (0 == GNUNET_CRYPTO_cmp_peer_identity (&(remove_entry->prev_hop), disconnected_peer)))
97 GNUNET_assert (GNUNET_YES ==
98 GNUNET_CONTAINER_multipeermap_remove (routing_table,
107 * Iterate over multiple entries for same destination value and get
108 * the correct next hop.
109 * @param cls struct RoutingTrail
110 * @param key Destination identity
111 * @param value struct RoutingTrail
112 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the next hop
115 get_next_hop (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
117 /* Here you should match if source, prev hop matches if yes then send
118 GNUNET_NO as you don't need to check more entries. */
119 struct RoutingTrail *request = cls;
120 struct RoutingTrail *existing_entry = (struct RoutingTrail *)value;
122 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->source), &(existing_entry->source)))
124 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->prev_hop), &(existing_entry->prev_hop)))
126 memcpy (&(request->next_hop), &(existing_entry->next_hop), sizeof (struct GNUNET_PeerIdentity));
135 * Add a new entry to our routing table.
136 * @param source peer Source of the trail.
137 * @param destintation Destination of the trail.
138 * @param next_hop Next peer to forward the message to reach the destination.
140 * GNUNET_SYSERR If the number of routing entries crossed thershold.
143 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *source,
144 const struct GNUNET_PeerIdentity *dest,
145 const struct GNUNET_PeerIdentity *next_hop,
146 struct GNUNET_PeerIdentity *prev_hop)
148 struct RoutingTrail *new_routing_entry;
150 if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD)
151 return GNUNET_SYSERR;
153 new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
154 memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity));
155 memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
156 memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity));
157 memcpy (&(new_routing_entry->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
159 GNUNET_assert (GNUNET_OK ==
160 GNUNET_CONTAINER_multipeermap_put (routing_table,
161 dest, new_routing_entry,
162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
169 * Iterate over routing table and remove entries for which peer is a part.
174 GDS_ROUTING_remove_entry (const struct GNUNET_PeerIdentity *peer)
176 GNUNET_CONTAINER_multipeermap_iterate (routing_table, &remove_routing_entry,
182 * Find the next hop to send packet to.
183 * @param source_peer Source of the trail.
184 * @param destination_peer Destination of the trail.
185 * @param prev_hop Previous hop in the trail.
186 * @return Next hop in the trail from source to destination.
188 struct GNUNET_PeerIdentity *
189 GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer,
190 struct GNUNET_PeerIdentity *destination_peer,
191 const struct GNUNET_PeerIdentity *prev_hop)
193 struct RoutingTrail *trail;
194 trail = GNUNET_malloc (sizeof (struct RoutingTrail));
195 memcpy (&(trail->destination), destination_peer, sizeof (struct GNUNET_PeerIdentity));
196 memcpy (&(trail->source), source_peer, sizeof (struct GNUNET_PeerIdentity));
197 memcpy (&(trail->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
199 GNUNET_CONTAINER_multipeermap_get_multiple (routing_table, destination_peer,
200 get_next_hop, trail);
202 return &(trail->next_hop);
209 * Check if the size of routing table has crossed threshold.
213 GDS_ROUTING_check_threshold ()
216 ret = (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) ? 0:1;
222 * Initialize routing subsystem.
225 GDS_ROUTING_init (void)
227 routing_table = GNUNET_CONTAINER_multipeermap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
232 * Shutdown routing subsystem.
235 GDS_ROUTING_done (void)
237 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
238 GNUNET_CONTAINER_multipeermap_destroy (routing_table);
241 /* end of gnunet-service-xdht_routing.c */