Removing routing table entries corresponding to disconnected peer
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_routing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file dht/gnunet-service-xdht_routing.c
23  * @brief GNUnet DHT tracking of requests for routing replies
24  * @author Supriti Singh
25  */
26 #include "platform.h"
27 #include "gnunet-service-xdht_neighbours.h"
28 #include "gnunet-service-xdht_routing.h"
29 #include "gnunet-service-xdht.h"
30
31
32 /**
33  * Number of requests we track at most (for routing replies).
34  */
35 #define DHT_MAX_RECENT (1024 * 16)
36
37 /**
38  * Maximum number of entries in routing table. 
39  */
40 #define ROUTING_TABLE_THRESHOLD 64
41
42 /**
43  * Routing table entry .
44  */
45 struct RoutingTrail
46 {
47   /**
48    * Source peer .
49    */
50   struct GNUNET_PeerIdentity source;
51
52   /**
53    * Destination peer.
54    */
55   struct GNUNET_PeerIdentity destination;
56
57   /**
58    * The peer to which this request should be passed to.
59    */
60   struct GNUNET_PeerIdentity next_hop;
61   
62   /**
63    * Peer just before next hop in the trail. 
64    */
65   struct GNUNET_PeerIdentity prev_hop;
66   
67 };
68
69
70 /**
71  * Routing table of the peer
72  */
73 static struct GNUNET_CONTAINER_MultiPeerMap *routing_table;
74
75 /**
76  * Iterate over routing table and remove entries for which peer is a part. 
77  * @param cls closure
78  * @param key current public key
79  * @param value value in the hash map
80  * @return #GNUNET_YES if we should continue to
81  *         iterate,
82  *         #GNUNET_NO if not.
83  */
84 static int
85 remove_routing_entry (void *cls,
86                       const struct GNUNET_PeerIdentity *key,
87                       void *value)
88 {
89   struct RoutingTrail *remove_entry = value;
90   const struct GNUNET_PeerIdentity *disconnected_peer = cls;
91   
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)))
96   {
97     GNUNET_assert (GNUNET_YES ==
98                    GNUNET_CONTAINER_multipeermap_remove (routing_table,
99                                                          key, 
100                                                          remove_entry));
101   }
102   return GNUNET_YES;
103 }
104
105
106 /**
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
113  */
114 int
115 get_next_hop (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
116 {
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;
121   
122   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->source), &(existing_entry->source)))
123   {
124     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->prev_hop), &(existing_entry->prev_hop)))
125     {
126       memcpy (&(request->next_hop), &(existing_entry->next_hop), sizeof (struct GNUNET_PeerIdentity));
127       return GNUNET_YES;
128     }
129   }
130   return GNUNET_NO;
131 }
132
133
134 /**
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.
139  * @return GNUNET_YES
140  *         GNUNET_SYSERR If the number of routing entries crossed thershold.
141  */
142 int
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)
147 {
148   struct RoutingTrail *new_routing_entry;
149     
150   if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD)
151     return GNUNET_SYSERR;
152  
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));
158   
159   GNUNET_assert (GNUNET_OK ==
160     GNUNET_CONTAINER_multipeermap_put (routing_table,
161                                        dest, new_routing_entry,
162                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
163
164   return GNUNET_YES;
165 }
166
167
168 /**
169  * Iterate over routing table and remove entries for which peer is a part. 
170  * @param peer
171  * @return 
172  */
173 void
174 GDS_ROUTING_remove_entry (const struct GNUNET_PeerIdentity *peer)
175 {
176   GNUNET_CONTAINER_multipeermap_iterate (routing_table, &remove_routing_entry,
177                                          (void *)peer);
178 }
179
180
181 /**
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. 
187  */
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)
192 {
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));
198
199   GNUNET_CONTAINER_multipeermap_get_multiple (routing_table, destination_peer,
200                                               get_next_hop, trail);
201   if(trail != NULL)
202     return &(trail->next_hop);
203   else
204     return NULL;
205 }
206
207
208 /**
209  * Check if the size of routing table has crossed threshold. 
210  * @return 
211  */
212 int
213 GDS_ROUTING_check_threshold ()
214 {
215   int ret;
216   ret = (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) ? 0:1;
217   return ret;    
218 }
219
220
221 /**
222  * Initialize routing subsystem.
223  */
224 void
225 GDS_ROUTING_init (void)
226
227   routing_table = GNUNET_CONTAINER_multipeermap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
228 }
229
230
231 /**
232  * Shutdown routing subsystem.
233  */
234 void
235 GDS_ROUTING_done (void)
236 {
237   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
238   GNUNET_CONTAINER_multipeermap_destroy (routing_table);
239 }
240
241 /* end of gnunet-service-xdht_routing.c */