c73a07a66c2b482c7b1f28d8e6ee22d40401cb0f
[oweals/gnunet.git] / src / dht / dht.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2009 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  * @author Christian Grothoff
23  * @author Nathan Evans
24  * @file dht/dht.h
25  */
26
27 #ifndef DHT_H_
28 #define DHT_H_
29
30 #define DEBUG_DHT GNUNET_NO
31
32 #define DEBUG_DHT_ROUTING GNUNET_YES
33
34 #define DHT_BLOOM_SIZE 16
35
36 #define DHT_BLOOM_K 8
37
38 #define MAX_OUTSTANDING_FORWARDS 100
39
40 #define DHT_FORWARD_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
41
42 #define DHT_SEND_PRIORITY 4
43
44 #define STAT_ROUTES "# DHT ROUTE Requests Seen"
45 #define STAT_ROUTE_FORWARDS "# DHT ROUTE Requests Forwarded"
46 #define STAT_ROUTE_FORWARDS_CLOSEST "# DHT ROUTE Requests Forwarded to Closest Known Peer"
47 #define STAT_RESULTS "# DHT ROUTE RESULT Requests Seen"
48 #define STAT_RESULTS_TO_CLIENT "# DHT ROUTE RESULT Sent to Client"
49 #define STAT_RESULT_FORWARDS "# DHT ROUTE RESULT Requests Forwarded"
50 #define STAT_GETS "# DHT GET Requests Handled"
51 #define STAT_PUTS "# DHT PUT Requests Handled"
52 #define STAT_PUTS_INSERTED "# DHT PUT Data Inserts"
53 #define STAT_FIND_PEER "# DHT FIND_PEER Requests Handled"
54 #define STAT_FIND_PEER_START "# DHT FIND_PEER Requests Initiated"
55 #define STAT_GET_START "# DHT GET Requests Initiated"
56 #define STAT_PUT_START "# DHT PUT Requests Initiated"
57 #define STAT_FIND_PEER_REPLY "# DHT FIND_PEER Responses Received"
58 #define STAT_GET_REPLY "# DHT GET Responses Received"
59 #define STAT_FIND_PEER_ANSWER "# DHT FIND_PEER Responses Initiated"
60 #define STAT_BLOOM_FIND_PEER "# DHT FIND_PEER Responses Ignored (bloom match)"
61 #define STAT_GET_RESPONSE_START "# DHT GET Responses Initiated"
62 #define STAT_HELLOS_PROVIDED "# HELLO Messages given to transport"
63 #define STAT_DISCONNECTS "# Disconnects received"
64 #define STAT_DUPLICATE_UID "# Duplicate UID's encountered (bad if any!)"
65 #define STAT_RECENT_SEEN "# recent requests seen again (routing loops, alternate paths)"
66
67 typedef void (*GNUNET_DHT_MessageReceivedHandler) (void *cls,
68                                                    const struct GNUNET_MessageHeader
69                                                    *msg);
70 struct GNUNET_DHT_ControlMessage
71 {
72   /**
73    * Type: GNUNET_MESSAGE_TYPE_DHT_CONTROL
74    */
75   struct GNUNET_MessageHeader header;
76
77   /**
78    * Command code of the message.
79    */
80   uint16_t command;
81
82   /**
83    * Variable parameter for the command.
84    */
85   uint16_t variable;
86 };
87
88 /**
89  * Message which indicates the DHT should cancel outstanding
90  * requests and discard any state.
91  */
92 struct GNUNET_DHT_StopMessage
93 {
94   /**
95    * Type: GNUNET_MESSAGE_TYPE_DHT_STOP
96    */
97   struct GNUNET_MessageHeader header;
98
99   /**
100    * Always zero.
101    */
102   uint32_t reserved GNUNET_PACKED;
103
104   /**
105    * Unique ID identifying this request
106    */
107   uint64_t unique_id GNUNET_PACKED;
108
109   /**
110    * Key of this request
111    */
112   GNUNET_HashCode key;
113
114 };
115
116
117 /**
118  * Generic DHT message, indicates that a route request
119  * should be issued, if coming from a client.  Shared
120  * usage for api->server and P2P message passing.
121  */
122 struct GNUNET_DHT_RouteMessage
123 {
124   /**
125    * Type: GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE
126    */
127   struct GNUNET_MessageHeader header;
128
129   /**
130    * Message options
131    */
132   uint32_t options GNUNET_PACKED;
133
134   /**
135    * The key to search for
136    */
137   GNUNET_HashCode key;
138
139   /**
140    * Unique ID identifying this request, if 0 then
141    * the client will not expect a response
142    */
143   uint64_t unique_id GNUNET_PACKED;
144
145   /**
146    * Replication level for this message
147    */
148   uint32_t desired_replication_level GNUNET_PACKED;
149
150
151   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
152
153 };
154
155 /**
156  * Generic local route result message
157  */
158 struct GNUNET_DHT_RouteResultMessage
159 {
160   /**
161    * Type: GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT
162    */
163   struct GNUNET_MessageHeader header;
164
165   /**
166    * Message options
167    */
168   uint32_t options GNUNET_PACKED;
169
170   /**
171    * Unique ID identifying this request (necessary for
172    * client to compare to sent requests)
173    */
174   uint64_t unique_id GNUNET_PACKED;
175
176   /**
177    * The key that was searched for
178    */
179   GNUNET_HashCode key;
180
181   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
182 };
183
184 /**
185  * Generic P2P DHT route message
186  */
187 struct GNUNET_DHT_P2PRouteMessage
188 {
189   /**
190    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE
191    */
192   struct GNUNET_MessageHeader header;
193
194   /**
195    * Message options
196    */
197   uint32_t options GNUNET_PACKED;
198
199   /**
200    * Hop count
201    */
202   uint32_t hop_count GNUNET_PACKED;
203
204   /**
205    * Network size estimate
206    */
207   uint32_t network_size GNUNET_PACKED;
208
209   /**
210    * Replication level for this message
211    */
212   uint32_t desired_replication_level GNUNET_PACKED;
213
214   /**
215    * Unique ID identifying this request
216    */
217   uint64_t unique_id GNUNET_PACKED;
218
219   /*
220    * Bloomfilter to stop circular routes
221    */
222   char bloomfilter[DHT_BLOOM_SIZE];
223
224   /**
225    * FIXME: add DHT logging for analysis!
226    */
227 #if LOG_SQL
228   /*
229    * Unique query id for sql database interaction.
230    */
231   uint64_t queryuid;
232
233   /*
234    * Unique trial id for sql database interaction
235    */
236   uint64_t trialuid;
237
238 #endif
239
240   /**
241    * The key to search for
242    */
243   GNUNET_HashCode key;
244
245   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
246
247 };
248
249 /**
250  * Generic P2P route result
251  *
252  * FIXME: One question is how much to include for a route result message.
253  *        Assuming a peer receives such a message, but has no record of a
254  *        route message, what should it do?  It can either drop the message
255  *        or try to forward it towards the original peer...  However, for
256  *        that to work we would need to include the original peer identity
257  *        in the GET request, which adds more data to the message.
258  */
259 struct GNUNET_DHT_P2PRouteResultMessage
260 {
261   /**
262    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT
263    */
264   struct GNUNET_MessageHeader header;
265
266   /**
267    * Message options
268    */
269   uint32_t options GNUNET_PACKED;
270
271   /**
272    * Hop count
273    */
274   uint32_t hop_count GNUNET_PACKED;
275
276   /**
277    * Unique ID identifying this request (may not be set)
278    */
279   uint64_t unique_id GNUNET_PACKED;
280
281   /*
282    * Bloomfilter to stop circular routes
283    */
284   char bloomfilter[DHT_BLOOM_SIZE];
285
286   /**
287    * The key that was searched for
288    */
289   GNUNET_HashCode key;
290
291 #if FORWARD_UNKNOWN
292   /**
293    * Network size estimate
294    */
295   uint32_t network_size GNUNET_PACKED;
296 #endif
297
298   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
299 };
300
301
302 /**
303  * Message to insert data into the DHT, shared
304  * between api->server communication and P2P communication.
305  * The type must be different for the two purposes.
306  */
307 struct GNUNET_DHT_PutMessage
308 {
309   /**
310    * Type: GNUNET_MESSAGE_TYPE_DHT_PUT / GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
311    */
312   struct GNUNET_MessageHeader header;
313
314   /**
315    * The type of data to insert.
316    */
317   size_t type GNUNET_PACKED;
318
319   /**
320    * How long should this data persist?
321    */
322   struct GNUNET_TIME_AbsoluteNBO expiration;
323
324   /**
325    * The size of the data, appended to the end of this message.
326    */
327   size_t data_size GNUNET_PACKED;
328
329 };
330
331
332 /**
333  * Message to request data from the DHT, shared
334  * between P2P requests and local get requests.
335  * Main difference is that if the request comes in
336  * locally we need to remember it (for client response).
337  */
338 struct GNUNET_DHT_GetMessage
339 {
340   /**
341    * Type: GNUNET_MESSAGE_TYPE_DHT_GET / GNUNET_MESSAGE_TYPE_DHT_P2P_GET
342    */
343   struct GNUNET_MessageHeader header;
344
345   /**
346    * The type for the data for the GET request
347    */
348   uint32_t type;
349
350 };
351
352 /**
353  * Generic DHT message, indicates that a route request
354  * should be issued, if coming from a client.  Shared
355  * usage for api->server and P2P message passing.
356  */
357 struct GNUNET_DHT_FindPeerMessage
358 {
359   /**
360    * Type: GNUNET_MESSAGE_TYPE_DHT_FIND_PEER
361    */
362   struct GNUNET_MessageHeader header;
363
364   /*
365    * Bloomfilter to reduce find peer responses
366    */
367   char bloomfilter[DHT_BLOOM_SIZE];
368 };
369
370 /**
371  * Message to return data either to the client API
372  * or to respond to a request received from another
373  * peer.  Shared format, different types.
374  */
375 struct GNUNET_DHT_GetResultMessage
376 {
377   /**
378    * Type: GNUNET_MESSAGE_TYPE_DHT_GET_RESULT / GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT
379    */
380   struct GNUNET_MessageHeader header;
381
382   /**
383    * The type for the data for the GET request
384    */
385   uint32_t type;
386
387   /**
388    * The key that was searched for
389    */
390   //GNUNET_HashCode key;
391
392   /**
393    * When does this entry expire?
394    */
395   struct GNUNET_TIME_AbsoluteNBO expiration;
396
397 };
398
399
400 #endif /* DHT_H_ */