WiP
[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 /**
33  * Needs to be GNUNET_YES for logging to dhtlog to work!
34  */
35 #define DEBUG_DHT_ROUTING GNUNET_YES
36
37 /**
38  * FIXME: document.
39  */
40 #define DHT_BLOOM_SIZE 128
41
42 /**
43  * FIXME: document.
44  */
45 #define DHT_BLOOM_K 6
46
47 /**
48  * How many requests to remember for forwarding responses.
49  */
50 #define MAX_OUTSTANDING_FORWARDS 100
51
52 /**
53  * How long to remember requests so we can forward responses.
54  */
55 #define DHT_FORWARD_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
56
57 /**
58  * Priority for routing results from other peers through
59  * the DHT.
60  */
61 #define DHT_SEND_PRIORITY 4
62
63
64 #define STAT_ROUTES "# DHT ROUTE Requests Seen"
65 #define STAT_ROUTE_FORWARDS "# DHT ROUTE Requests Forwarded"
66 #define STAT_ROUTE_FORWARDS_CLOSEST "# DHT ROUTE Requests Forwarded to Closest Known Peer"
67 #define STAT_RESULTS "# DHT ROUTE RESULT Requests Seen"
68 #define STAT_RESULTS_TO_CLIENT "# DHT ROUTE RESULT Sent to Client"
69 #define STAT_RESULT_FORWARDS "# DHT ROUTE RESULT Requests Forwarded"
70 #define STAT_GETS "# DHT GET Requests Handled"
71 #define STAT_PUTS "# DHT PUT Requests Handled"
72 #define STAT_PUTS_INSERTED "# DHT PUT Data Inserts"
73 #define STAT_FIND_PEER "# DHT FIND_PEER Requests Handled"
74 #define STAT_FIND_PEER_START "# DHT FIND_PEER Requests Initiated"
75 #define STAT_GET_START "# DHT GET Requests Initiated"
76 #define STAT_PUT_START "# DHT PUT Requests Initiated"
77 #define STAT_FIND_PEER_REPLY "# DHT FIND_PEER Responses Received"
78 #define STAT_GET_REPLY "# DHT GET Responses Received"
79 #define STAT_FIND_PEER_ANSWER "# DHT FIND_PEER Responses Initiated"
80 #define STAT_BLOOM_FIND_PEER "# DHT FIND_PEER Responses Ignored (bloom match)"
81 #define STAT_GET_RESPONSE_START "# DHT GET Responses Initiated"
82 #define STAT_HELLOS_PROVIDED "# HELLO Messages given to transport"
83 #define STAT_DISCONNECTS "# Disconnects received"
84 #define STAT_DUPLICATE_UID "# Duplicate UID's encountered (bad if any!)"
85 #define STAT_RECENT_SEEN "# recent requests seen again (routing loops, alternate paths)"
86
87
88 /**
89  * FIXME: document.
90  */
91 typedef void (*GNUNET_DHT_MessageReceivedHandler) (void *cls,
92                                                    const struct GNUNET_MessageHeader
93                                                    *msg);
94
95
96 /**
97  * FIXME: document.
98  */
99 struct GNUNET_DHT_ControlMessage
100 {
101   /**
102    * Type: GNUNET_MESSAGE_TYPE_DHT_CONTROL
103    */
104   struct GNUNET_MessageHeader header;
105
106   /**
107    * Command code of the message.
108    */
109   uint16_t command;
110
111   /**
112    * Variable parameter for the command.
113    */
114   uint16_t variable;
115 };
116
117
118 /**
119  * Message which indicates the DHT should cancel outstanding
120  * requests and discard any state.
121  */
122 struct GNUNET_DHT_StopMessage
123 {
124   /**
125    * Type: GNUNET_MESSAGE_TYPE_DHT_STOP
126    */
127   struct GNUNET_MessageHeader header;
128
129   /**
130    * Always zero.
131    */
132   uint32_t reserved GNUNET_PACKED;
133
134   /**
135    * Unique ID identifying this request
136    */
137   uint64_t unique_id GNUNET_PACKED;
138
139   /**
140    * Key of this request
141    */
142   GNUNET_HashCode key;
143
144 };
145
146
147 /**
148  * Generic DHT message, indicates that a route request
149  * should be issued, if coming from a client.  Shared
150  * usage for api->server and P2P message passing.
151  */
152 struct GNUNET_DHT_RouteMessage
153 {
154   /**
155    * Type: GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE
156    */
157   struct GNUNET_MessageHeader header;
158
159   /**
160    * Message options, actually an 'enum GNUNET_DHT_RouteOption' value.
161    */
162   uint32_t options GNUNET_PACKED;
163
164   /**
165    * Replication level for this message
166    */
167   uint32_t desired_replication_level GNUNET_PACKED;
168
169   /**
170    * For alignment, always zero.
171    */
172   uint32_t reserved GNUNET_PACKED;
173
174   /**
175    * The key to search for
176    */
177   GNUNET_HashCode key;
178
179   /**
180    * Unique ID identifying this request, if 0 then
181    * the client will not expect a response
182    */
183   uint64_t unique_id GNUNET_PACKED;
184
185
186   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
187
188 };
189
190
191 /**
192  * Generic local route result message
193  */
194 struct GNUNET_DHT_RouteResultMessage
195 {
196   /**
197    * Type: GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT
198    */
199   struct GNUNET_MessageHeader header;
200
201   /**
202    * Number of peers recorded in the outgoing
203    * path from source to the final destination
204    * of this message.
205    */
206   uint32_t outgoing_path_length GNUNET_PACKED;
207
208   /**
209    * Unique ID identifying this request (necessary for
210    * client to compare to sent requests)
211    */
212   uint64_t unique_id GNUNET_PACKED;
213
214   /**
215    * The key that was searched for
216    */
217   GNUNET_HashCode key;
218
219   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
220
221   /* OUTGOING path */
222 };
223
224
225 /**
226  * Generic P2P DHT route message
227  */
228 struct GNUNET_DHT_P2PRouteMessage
229 {
230   /**
231    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE
232    */
233   struct GNUNET_MessageHeader header;
234
235   /**
236    * Always zero.
237    */
238   uint32_t reserved GNUNET_PACKED;
239
240   /**
241    * Message options
242    */
243   uint32_t options GNUNET_PACKED;
244
245   /**
246    * Hop count
247    */
248   uint32_t hop_count GNUNET_PACKED;
249
250   /**
251    * Replication level for this message
252    */
253   uint32_t desired_replication_level GNUNET_PACKED;
254
255   /**
256    * Network size estimate
257    */
258   uint32_t network_size GNUNET_PACKED;
259
260   /**
261    * Generic route path length for a message in the
262    * DHT that arrived at a peer and generated
263    * a reply. Copied to the end of this message.
264    */
265   uint32_t outgoing_path_length GNUNET_PACKED;
266
267   /**
268    * Unique ID identifying this request
269    */
270   uint64_t unique_id GNUNET_PACKED;
271
272   /**
273    * Bloomfilter to stop circular routes
274    */
275   char bloomfilter[DHT_BLOOM_SIZE];
276
277   /**
278    * The key to search for
279    */
280   GNUNET_HashCode key;
281
282   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
283
284   /* OUTGOING PATH */
285
286 };
287
288 /**
289  * Generic P2P route result
290  */
291 struct GNUNET_DHT_P2PRouteResultMessage
292 {
293   /**
294    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT
295    */
296   struct GNUNET_MessageHeader header;
297
298   /**
299    * Number of peers recorded in the path
300    * (inverse of the path the outgoing message took).
301    * These peer identities follow this message.
302    */
303   uint32_t outgoing_path_length GNUNET_PACKED;
304
305   /**
306    * Message options
307    */
308   uint32_t options GNUNET_PACKED;
309
310   /**
311    * Hop count
312    */
313   uint32_t hop_count GNUNET_PACKED;
314
315   /**
316    * Unique ID identifying this request (may not be set)
317    */
318   uint64_t unique_id GNUNET_PACKED;
319
320   /**
321    * Bloomfilter to stop circular routes
322    */
323   char bloomfilter[DHT_BLOOM_SIZE];
324
325   /**
326    * The key that was searched for
327    */
328   GNUNET_HashCode key;
329
330 #if FORWARD_UNKNOWN
331   /**
332    * Network size estimate
333    */
334   uint32_t network_size GNUNET_PACKED;
335 #endif
336
337   /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
338
339   /* OUTGOING PATH */
340 };
341
342
343 /**
344  * Message to insert data into the DHT, shared
345  * between api->server communication and P2P communication.
346  * The type must be different for the two purposes.
347  */
348 struct GNUNET_DHT_PutMessage
349 {
350   /**
351    * Type: GNUNET_MESSAGE_TYPE_DHT_PUT / GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
352    */
353   struct GNUNET_MessageHeader header;
354
355   /**
356    * The type of data to insert.
357    */
358   uint32_t type GNUNET_PACKED;
359
360   /**
361    * How long should this data persist?
362    */
363   struct GNUNET_TIME_AbsoluteNBO expiration;
364
365 };
366
367
368 /**
369  * Message to request data from the DHT, shared
370  * between P2P requests and local get requests.
371  * Main difference is that if the request comes in
372  * locally we need to remember it (for client response).
373  */
374 struct GNUNET_DHT_GetMessage
375 {
376   /**
377    * Type: GNUNET_MESSAGE_TYPE_DHT_GET / GNUNET_MESSAGE_TYPE_DHT_P2P_GET
378    */
379   struct GNUNET_MessageHeader header;
380
381   /**
382    * The type for the data for the GET request; actually an 'enum
383    * GNUNET_BLOCK_Type'.
384    */
385   uint32_t type;
386
387   /**
388    * Mutator used for the bloom filter (0 if no bf is used).
389    */
390   uint32_t bf_mutator;
391
392   /**
393    * Size of the eXtended query (xquery).
394    */
395   uint16_t xquery_size;
396
397   /**
398    * Size of the bloom filter.
399    */
400   uint16_t bf_size;
401
402   /* Followed by the xquery which has 'xquery_size' bytes */
403
404   /* Followed by the bloom filter (after xquery) with 'bf_size' bytes */
405 };
406
407
408 /**
409  * Generic DHT message, indicates that a route request
410  * should be issued, if coming from a client.  Shared
411  * usage for api->server and P2P message passing.
412  */
413 struct GNUNET_DHT_FindPeerMessage
414 {
415   /**
416    * Type: GNUNET_MESSAGE_TYPE_DHT_FIND_PEER
417    */
418   struct GNUNET_MessageHeader header;
419
420   /**
421    * Bloomfilter to reduce find peer responses
422    */
423   char bloomfilter[DHT_BLOOM_SIZE];
424 };
425
426
427 /**
428  * Message to return data either to the client API
429  * or to respond to a request received from another
430  * peer.  Shared format, different types.
431  */
432 struct GNUNET_DHT_GetResultMessage
433 {
434   /**
435    * Type: GNUNET_MESSAGE_TYPE_DHT_GET_RESULT / GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT
436    */
437   struct GNUNET_MessageHeader header;
438
439   /**
440    * The type for the data for the GET request
441    */
442   uint16_t type;
443
444   /**
445    * The number of peer identities appended to the end of this
446    * message.
447    */
448   uint16_t put_path_length;
449
450   /**
451    * When does this entry expire?
452    */
453   struct GNUNET_TIME_AbsoluteNBO expiration;
454
455 };
456
457 /**
458  * Entry for inserting data into datacache from the DHT.
459  * Needed here so block library can verify entries that
460  * are shoveled into the DHT.
461  */
462 struct DHTPutEntry
463 {
464   /**
465    * Size of data.
466    */
467   uint16_t data_size;
468
469   /**
470    * Length of recorded path.
471    */
472   uint16_t path_length;
473
474   /* PUT DATA */
475
476   /* PATH ENTRIES */
477 };
478
479
480 #endif /* DHT_H_ */