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 * Maximum number of entries in routing table.
34 #define ROUTING_TABLE_THRESHOLD 64
38 * FIXME: do we need to store destination and source.
39 * because in trail teardown we will reach destination but it will not find any
40 * entry in routing table. so we should store destination and source.
41 * Routing table entry .
46 * Global Unique identifier of the trail.
48 struct GNUNET_HashCode trail_id;
51 * The peer to which this request should be passed to.
53 struct GNUNET_PeerIdentity next_hop; // change to struct FriendInfo *
56 * Peer just before next hop in the trail.
58 struct GNUNET_PeerIdentity prev_hop; // change to struct FriendInfo *
62 * Routing table of the peer
64 static struct GNUNET_CONTAINER_MultiHashMap *routing_table;
67 * Update the prev. hop of the trail. Call made by trail teardown where
68 * if you are the first friend now in the trail then you need to update
71 * @return #GNUNET_OK success
72 * #GNUNET_SYSERR in case no matching entry found in routing table.
75 GDS_ROUTING_update_trail_prev_hop (const struct GNUNET_HashCode trail_id,
76 struct GNUNET_PeerIdentity prev_hop)
78 struct RoutingTrail *trail;
80 trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
85 trail->prev_hop = prev_hop;
91 * Get the next hop for trail corresponding to trail_id
92 * @param trail_id Trail id to be searched.
93 * @return Next_hop if found
94 * NULL If next hop not found.
96 struct GNUNET_PeerIdentity *
97 GDS_ROUTING_get_next_hop (const struct GNUNET_HashCode trail_id,
98 enum GDS_ROUTING_trail_direction trail_direction)
100 struct RoutingTrail *trail;
102 trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
107 switch (trail_direction)
109 case GDS_ROUTING_SRC_TO_DEST:
110 return &(trail->next_hop);
111 case GDS_ROUTING_DEST_TO_SRC:
112 return &(trail->prev_hop);
119 * Remove trail with trail_id
120 * @param trail_id Trail id to be removed
121 * @return #GNUNET_YES success
122 * #GNUNET_NO if entry not found.
125 GDS_ROUTING_remove_trail (const struct GNUNET_HashCode remove_trail_id)
127 struct RoutingTrail *remove_entry;
129 remove_entry = GNUNET_CONTAINER_multihashmap_get (routing_table, &remove_trail_id);
131 if (NULL == remove_entry)
134 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (routing_table,
138 GNUNET_free (remove_entry);
146 * Iterate over routing table and remove entries with value as part of any trail.
148 * @param key current public key
149 * @param value value in the hash map
150 * @return #GNUNET_YES if we should continue to iterate,
153 static int remove_matching_trails (void *cls,
154 const struct GNUNET_HashCode *key,
157 struct RoutingTrail *remove_trail = cls;
158 struct GNUNET_PeerIdentity *peer = value;
160 if ((0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_trail->next_hop, peer)) ||
161 (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_trail->prev_hop, peer)))
163 GNUNET_assert (GNUNET_YES ==
164 GNUNET_CONTAINER_multihashmap_remove (routing_table,
165 &remove_trail->trail_id,
167 GNUNET_free (remove_trail);
174 * * FIXME: when a friend gets disconnected, then we remove the entry from routing
175 * table where this friend is either a next_hop or prev_hop. But we don't communicate
176 * that the trail is broken to any one who is part of trail. Should we communicate or
177 * not. And if not then the cases where trail setup fails because next_hop = NULL
178 * or something like that. VERY URGENT.
179 * Remove every trail where peer is either next_hop or prev_hop
180 * @param peer Peer to be searched.
183 GDS_ROUTING_remove_trail_by_peer (const struct GNUNET_PeerIdentity *peer)
185 GNUNET_CONTAINER_multihashmap_iterate (routing_table, &remove_matching_trails,
191 * Add a new entry in routing table
192 * @param new_trail_id
195 * @return #GNUNET_OK success
196 * #GNUNET_SYSERR in case new_trail_id already exists in the network
197 * but with different prev_hop/next_hop
200 GDS_ROUTING_add (struct GNUNET_HashCode new_trail_id,
201 const struct GNUNET_PeerIdentity prev_hop,
202 const struct GNUNET_PeerIdentity next_hop)
204 struct RoutingTrail *new_entry;
206 new_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
207 new_entry->trail_id = new_trail_id;
208 new_entry->next_hop = next_hop;
209 new_entry->prev_hop = prev_hop;
210 return GNUNET_CONTAINER_multihashmap_put (routing_table,
211 &new_trail_id, new_entry,
212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
217 * Check if the size of routing table has crossed threshold.
218 * @return #GNUNET_YES, if threshold crossed else #GNUNET_NO.
221 GDS_ROUTING_threshold_reached (void)
223 return (GNUNET_CONTAINER_multihashmap_size(routing_table) >
224 ROUTING_TABLE_THRESHOLD) ? GNUNET_YES:GNUNET_NO;
229 * Initialize routing subsystem.
232 GDS_ROUTING_init (void)
234 routing_table = GNUNET_CONTAINER_multihashmap_create (ROUTING_TABLE_THRESHOLD * 4 / 3,
240 * Shutdown routing subsystem.
243 GDS_ROUTING_done (void)
245 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (routing_table));
246 GNUNET_CONTAINER_multihashmap_destroy (routing_table);
249 /* end of gnunet-service-xdht_routing.c */