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