- verify successor result
[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 /* FIXME
32  * 1. We need field to understand which routing table is for which peer.
33  * 2. Better function names and variable names.
34  * 3. Use destination peer id as key for routing table. 
35  * 4. What does GDS stands for?
36  * 
37  */
38
39
40 /**
41  * Number of requests we track at most (for routing replies).
42  */
43 #define DHT_MAX_RECENT (1024 * 16)
44
45
46 /**
47  * FIXME: Do we need a field prev_hop
48  * Routing table entry .
49  */
50 struct RoutingTrail
51 {
52   /**
53    * Source peer .
54    */
55   struct GNUNET_PeerIdentity *source;
56
57   /**
58    * Destination peer.
59    */
60   struct GNUNET_PeerIdentity *destination;
61
62   /**
63    * The peer to which this request should be passed to.
64    */
65   struct GNUNET_PeerIdentity *next_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  * FIXME: Change the name of variable. 
78  * Ensure that everywhere in this file you are using destination as the key.
79  * Do we need prev field in routing table?
80  * Add a new entry to our routing table.
81  * @param source peer
82  * @param destintation
83  * @param next_hop
84  */
85 void
86 GDS_ROUTING_add (struct GNUNET_PeerIdentity *source,
87                  struct GNUNET_PeerIdentity *dest,
88                  struct GNUNET_PeerIdentity *next_hop)
89 {
90   struct RoutingTrail *new_routing_entry;
91     
92   /* If dest is already present in the routing table, then exit.*/
93   if (GNUNET_YES ==
94     GNUNET_CONTAINER_multipeermap_contains (routing_table,
95                                               dest))
96   {
97     GNUNET_break (0);
98     return;
99   }
100   
101   new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
102   new_routing_entry->source = source;
103   new_routing_entry->next_hop = next_hop;
104   new_routing_entry->destination = dest;
105   
106   GNUNET_assert (GNUNET_OK ==
107     GNUNET_CONTAINER_multipeermap_put (routing_table,
108                                        dest, new_routing_entry,
109                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
110 }
111
112
113 /**
114  * Find the next hop to send packet to .
115  * @return next hop peer id
116  */
117 struct GNUNET_PeerIdentity *
118 GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer,
119                    struct GNUNET_PeerIdentity *destination_peer)
120 {
121   struct RoutingTrail *trail;
122   trail = (struct RoutingTrail *)(GNUNET_CONTAINER_multipeermap_get(routing_table,destination_peer));
123     
124   if(trail == NULL)
125       return NULL;
126     
127   return trail->next_hop;
128 }
129
130
131 /**FIXME: Old implementation just to remove error
132  * Handle a reply (route to origin).  Only forwards the reply back to
133  * other peers waiting for it.  Does not do local caching or
134  * forwarding to local clients.  Essentially calls
135  * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
136  * request recently.
137  *
138  * @param type type of the block
139  * @param expiration_time when does the content expire
140  * @param key key for the content
141  * @param put_path_length number of entries in put_path
142  * @param put_path peers the original PUT traversed (if tracked)
143  * @param get_path_length number of entries in get_path
144  * @param get_path peers this reply has traversed so far (if tracked)
145  * @param data payload of the reply
146  * @param data_size number of bytes in data
147  */
148 void
149 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
150                      struct GNUNET_TIME_Absolute expiration_time,
151                      const struct GNUNET_HashCode * key, unsigned int put_path_length,
152                      const struct GNUNET_PeerIdentity *put_path,
153                      unsigned int get_path_length,
154                      const struct GNUNET_PeerIdentity *get_path,
155                      const void *data, size_t data_size)
156 {
157
158 }
159
160
161 /**
162  * Initialize routing subsystem.
163  */
164 void
165 GDS_ROUTING_init ()
166
167   routing_table = GNUNET_CONTAINER_multipeermap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
168 }
169
170
171 /**
172  * Shutdown routing subsystem.
173  */
174 void
175 GDS_ROUTING_done ()
176 {
177   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
178   GNUNET_CONTAINER_multipeermap_destroy (routing_table);
179 }
180
181 /* end of gnunet-service-xdht_routing.c */