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