Adding correct include file "gnunet_dht_service.h" in neighbours.h
[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 /**
77  * Add a new entry to our routing table.
78  * @param source peer Source of the trail.
79  * @param destintation Destination of the trail.
80  * @param next_hop Next peer to forward the message to reach the destination.
81  * @return GNUNET_YES
82  *         GNUNET_SYSERR If the number of routing entries crossed thershold.
83  */
84 int
85 GDS_ROUTING_add (struct GNUNET_PeerIdentity *source,
86                  struct GNUNET_PeerIdentity *dest,
87                  const struct GNUNET_PeerIdentity *next_hop,
88                  struct GNUNET_PeerIdentity *prev_hop)
89 {
90   struct RoutingTrail *new_routing_entry;
91     
92   if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD)
93     return GNUNET_SYSERR;
94   //FPRINTF (stderr,_("\nSUPU ROUTING ADD %s, %s, %d"),__FILE__, __func__,__LINE__);
95   new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
96   memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity));
97   memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
98   memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity));
99   memcpy (&(new_routing_entry->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
100   
101   GNUNET_assert (GNUNET_OK ==
102     GNUNET_CONTAINER_multipeermap_put (routing_table,
103                                        dest, new_routing_entry,
104                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
105   
106   /* SUPU TEST CODE */
107   /* Here I want to see if routing table is correct or not. */
108   int test_index;
109   struct GNUNET_CONTAINER_MultiPeerMapIterator *test_iter;
110   struct GNUNET_PeerIdentity *print_peer;
111   print_peer = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
112   struct RoutingTrail *test_trail;
113   test_iter = GNUNET_CONTAINER_multipeermap_iterator_create (routing_table); 
114   for (test_index = 0; test_index < GNUNET_CONTAINER_multipeermap_size (routing_table); test_index++)
115   {
116     FPRINTF (stderr,_("\nSUPU %s, %s, %d, entry[%d]"),__FILE__, __func__,__LINE__,test_index);
117     if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (test_iter, NULL,
118                                                                  (const void **)&test_trail)) 
119     {
120       memcpy (print_peer, &(test_trail->source),sizeof (struct GNUNET_PeerIdentity));
121       FPRINTF (stderr,_("\nSUPU %s, %s, %d, test_trail->source =%s"),__FILE__, __func__,__LINE__,GNUNET_i2s (print_peer));
122       memcpy (print_peer, &(test_trail->destination),sizeof (struct GNUNET_PeerIdentity));
123       FPRINTF (stderr,_("\nSUPU %s, %s, %d, test_trail->destination =%s"),__FILE__, __func__,__LINE__,GNUNET_i2s(print_peer));
124       memcpy (print_peer, &(test_trail->prev_hop),sizeof (struct GNUNET_PeerIdentity));
125       FPRINTF (stderr,_("\nSUPU %s, %s, %d, test_trail->prev_hop =%s"),__FILE__, __func__,__LINE__,GNUNET_i2s(print_peer));
126       memcpy (print_peer, &(test_trail->next_hop),sizeof (struct GNUNET_PeerIdentity));
127       FPRINTF (stderr,_("\nSUPU %s, %s, %d, test_trail->next_hop =%s"),__FILE__, __func__,__LINE__,GNUNET_i2s(print_peer));
128       
129     }
130   }
131   /* SUPU TEST CODE ENDS*/
132   return GNUNET_YES;
133 }
134
135
136 /**
137  * Iterate over multiple entries for same destinational value and get
138  * the correct next hop.
139  * @param cls struct RoutingTrail
140  * @param key Destination identity
141  * @param value struct RoutingTrail
142  * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the next hop
143  */
144 int
145 get_next_hop (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
146 {
147   /* Here you should match if source, prev hop matches if yes then send 
148    GNUNET_NO as you don't need to check more entries. */
149   struct RoutingTrail *request = cls;
150   struct RoutingTrail *existing_entry = (struct RoutingTrail *)value;
151   
152   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->source), &(existing_entry->source)))
153   {
154     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->prev_hop), &(existing_entry->prev_hop)))
155     {
156       memcpy (&(request->next_hop), &(existing_entry->next_hop), sizeof (struct GNUNET_PeerIdentity));
157       return GNUNET_YES;
158     }
159   }
160   return GNUNET_NO;
161 }
162
163
164 /**
165  * Find the next hop to send packet to.
166  * @param source_peer Source of the trail.
167  * @param destination_peer Destination of the trail.
168  * @param prev_hop Previous hop in the trail. 
169  * @return Next hop in the trail from source to destination. 
170  */
171 struct GNUNET_PeerIdentity *
172 GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer,
173                    struct GNUNET_PeerIdentity *destination_peer,
174                    const struct GNUNET_PeerIdentity *prev_hop)
175 {
176   struct RoutingTrail *trail;
177   trail = GNUNET_malloc (sizeof (struct RoutingTrail));
178   memcpy (&(trail->destination), destination_peer, sizeof (struct GNUNET_PeerIdentity));
179   memcpy (&(trail->source), source_peer, sizeof (struct GNUNET_PeerIdentity));
180   memcpy (&(trail->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity));
181   //trail->next_hop = NULL;
182   //FPRINTF (stderr,_("\nSUPU ROUTING SEARCH %s, %s, %d"),__FILE__, __func__,__LINE__);
183   GNUNET_CONTAINER_multipeermap_get_multiple (routing_table, destination_peer,
184                                               get_next_hop, trail);
185   if(trail != NULL)
186     return &(trail->next_hop);
187   else
188     return NULL;
189 }
190
191
192 /**FIXME: Old implementation just to remove error
193  * Handle a reply (route to origin).  Only forwards the reply back to
194  * other peers waiting for it.  Does not do local caching or
195  * forwarding to local clients.  Essentially calls
196  * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
197  * request recently.
198  *
199  * @param type type of the block
200  * @param expiration_time when does the content expire
201  * @param key key for the content
202  * @param put_path_length number of entries in put_path
203  * @param put_path peers the original PUT traversed (if tracked)
204  * @param get_path_length number of entries in get_path
205  * @param get_path peers this reply has traversed so far (if tracked)
206  * @param data payload of the reply
207  * @param data_size number of bytes in data
208  */
209 void
210 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
211                      struct GNUNET_TIME_Absolute expiration_time,
212                      const struct GNUNET_HashCode * key, unsigned int put_path_length,
213                      const struct GNUNET_PeerIdentity *put_path,
214                      unsigned int get_path_length,
215                      const struct GNUNET_PeerIdentity *get_path,
216                      const void *data, size_t data_size)
217 {
218   return;
219 }
220
221 /**
222  * Check if the size of routing table has crossed threshold. 
223  * @return 
224  */
225 int
226 GDS_ROUTING_size ()
227 {
228   int ret;
229   ret = (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) ? 0:1;
230   return ret;    
231 }
232
233
234 /**
235  * Initialize routing subsystem.
236  */
237 void
238 GDS_ROUTING_init ()
239
240   routing_table = GNUNET_CONTAINER_multipeermap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
241 }
242
243
244 /**
245  * Shutdown routing subsystem.
246  */
247 void
248 GDS_ROUTING_done ()
249 {
250   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
251   GNUNET_CONTAINER_multipeermap_destroy (routing_table);
252 }
253
254 /* end of gnunet-service-xdht_routing.c */