Using trail id
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_routing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 - 2014 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  * Maximum number of entries in routing table. 
33  */
34 #define ROUTING_TABLE_THRESHOLD 64
35
36
37 /**
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 .
42  */
43 struct RoutingTrail
44 {
45   /**
46    * Global Unique identifier of the trail.
47    */
48   struct GNUNET_HashCode trail_id;
49   
50   /**
51    * The peer to which this request should be passed to.
52    */
53   struct GNUNET_PeerIdentity next_hop;
54   
55   /**
56    * Peer just before next hop in the trail. 
57    */
58   struct GNUNET_PeerIdentity prev_hop;
59 };
60
61 /**
62  * Routing table of the peer
63  */
64 static struct GNUNET_CONTAINER_MultiHashMap *routing_table;
65
66 /**
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
69  * your prev. hop.
70  * @param trail_id
71  * @return #GNUNET_OK success
72  *         #GNUNET_SYSERR in case no matching entry found in routing table. 
73  */
74 int
75 GDS_ROUTING_update_trail_prev_hop (const struct GNUNET_HashCode trail_id,
76                                    struct GNUNET_PeerIdentity prev_hop)
77 {
78   struct RoutingTrail *trail;
79   
80   trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
81   
82   if (NULL == trail)
83     return GNUNET_SYSERR;
84   
85   trail->prev_hop = prev_hop;
86   return GNUNET_OK;
87 }
88
89
90 /**
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. 
95  */
96 struct GNUNET_PeerIdentity *
97 GDS_ROUTING_get_next_hop (const struct GNUNET_HashCode trail_id,
98                           enum GDS_ROUTING_trail_direction trail_direction)
99 {
100   struct RoutingTrail *trail;
101  
102   trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
103   
104   if (NULL == trail)
105     return NULL;
106   
107   switch (trail_direction)
108   {
109     case GDS_ROUTING_SRC_TO_DEST:
110       return &(trail->next_hop);
111     case GDS_ROUTING_DEST_TO_SRC:
112       return &(trail->prev_hop);
113   }
114   return NULL;
115 }
116
117
118 /**
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.
123  */
124 int
125 GDS_ROUTING_remove_trail (const struct GNUNET_HashCode remove_trail_id)
126 {
127   struct RoutingTrail *remove_entry;
128   
129   remove_entry = GNUNET_CONTAINER_multihashmap_get (routing_table, &remove_trail_id);
130   
131   if (NULL == remove_entry)
132     return GNUNET_NO;
133   
134   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (routing_table,
135                                                           &remove_trail_id, 
136                                                           remove_entry))
137   {
138     GNUNET_free (remove_entry);
139     return GNUNET_YES;
140   }  
141   return GNUNET_NO;
142 }
143
144
145 /**
146  * Iterate over routing table and remove entries with value as part of any trail.
147  * @param cls closure
148  * @param key current public key
149  * @param value value in the hash map
150  * @return #GNUNET_YES if we should continue to iterate,
151  *         #GNUNET_NO if not.
152  */
153 static int remove_matching_trails (void *cls,
154                                    const struct GNUNET_HashCode *key,
155                                    void *value)
156 {
157   struct RoutingTrail *remove_trail = cls;
158   struct GNUNET_PeerIdentity *peer = value;
159   
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)))
162   {
163     GNUNET_assert (GNUNET_YES ==
164                    GNUNET_CONTAINER_multihashmap_remove (routing_table,
165                                                          &remove_trail->trail_id,
166                                                          remove_trail));
167     GNUNET_free (remove_trail);
168   }  
169   return GNUNET_YES;  
170 }
171
172
173 /**
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.
181  */
182 void
183 GDS_ROUTING_remove_trail_by_peer (const struct GNUNET_PeerIdentity *peer)
184 {
185   GNUNET_CONTAINER_multihashmap_iterate (routing_table, &remove_matching_trails,
186                                            (void *)peer);
187 }
188
189
190 /**
191  * Add a new entry in routing table
192  * @param new_trail_id
193  * @param prev_hop
194  * @param next_hop
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
198  */
199 int
200 GDS_ROUTING_add (struct GNUNET_HashCode new_trail_id, 
201                  struct GNUNET_PeerIdentity *prev_hop,
202                  const struct GNUNET_PeerIdentity *next_hop)
203 {
204   struct RoutingTrail *new_entry;
205   
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);
213 }
214
215
216 /**
217  * Check if the size of routing table has crossed threshold. 
218  * @return #GNUNET_YES, if threshold crossed else #GNUNET_NO.
219  */
220 int
221 GDS_ROUTING_threshold_reached (void)
222 {
223   return (GNUNET_CONTAINER_multihashmap_size(routing_table) > 
224           ROUTING_TABLE_THRESHOLD) ? GNUNET_YES:GNUNET_NO;    
225 }
226
227
228 /**
229  * Initialize routing subsystem.
230  */
231 void
232 GDS_ROUTING_init (void)
233
234   routing_table = GNUNET_CONTAINER_multihashmap_create (ROUTING_TABLE_THRESHOLD * 4 / 3,
235                                                         GNUNET_NO);
236 }
237
238
239 /**
240  * Shutdown routing subsystem.
241  */
242 void
243 GDS_ROUTING_done (void)
244 {
245   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (routing_table));
246   GNUNET_CONTAINER_multihashmap_destroy (routing_table);
247 }
248
249 /* end of gnunet-service-xdht_routing.c */