diff options
Diffstat (limited to 'src/fs/gnunet-service-fs_new.c')
-rw-r--r-- | src/fs/gnunet-service-fs_new.c | 555 |
1 files changed, 555 insertions, 0 deletions
diff --git a/src/fs/gnunet-service-fs_new.c b/src/fs/gnunet-service-fs_new.c new file mode 100644 index 000000000..f0a1513c3 --- /dev/null +++ b/src/fs/gnunet-service-fs_new.c | |||
@@ -0,0 +1,555 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 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 fs/gnunet-service-fs.c | ||
23 | * @brief gnunet anonymity protocol implementation | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * To use: | ||
27 | * - GSF_plan_get_ (!) | ||
28 | * - GSF_plan_size_ (?) | ||
29 | * - GSF_plan_notify_request_done (!) | ||
30 | * - | ||
31 | * | ||
32 | * | ||
33 | */ | ||
34 | #include "platform.h" | ||
35 | #include <float.h> | ||
36 | #include "gnunet_constants.h" | ||
37 | #include "gnunet_core_service.h" | ||
38 | #include "gnunet_dht_service.h" | ||
39 | #include "gnunet_datastore_service.h" | ||
40 | #include "gnunet_load_lib.h" | ||
41 | #include "gnunet_peer_lib.h" | ||
42 | #include "gnunet_protocols.h" | ||
43 | #include "gnunet_signatures.h" | ||
44 | #include "gnunet_statistics_service.h" | ||
45 | #include "gnunet_transport_service.h" | ||
46 | #include "gnunet_util_lib.h" | ||
47 | #include "gnunet-service-fs_cp.h" | ||
48 | #include "gnunet-service-fs_indexing.h" | ||
49 | #include "gnunet-service-fs_lc.h" | ||
50 | #include "gnunet-service-fs_pe.h" | ||
51 | #include "gnunet-service-fs_pr.h" | ||
52 | #include "gnunet-service-fs_push.h" | ||
53 | #include "gnunet-service-fs_put.h" | ||
54 | #include "fs.h" | ||
55 | |||
56 | #define DEBUG_FS GNUNET_YES | ||
57 | |||
58 | /** | ||
59 | * Size for the hash map for DHT requests from the FS | ||
60 | * service. Should be about the number of concurrent | ||
61 | * DHT requests we plan to make. | ||
62 | */ | ||
63 | #define FS_DHT_HT_SIZE 1024 | ||
64 | |||
65 | |||
66 | /** | ||
67 | * How quickly do we age cover traffic? At the given | ||
68 | * time interval, remaining cover traffic counters are | ||
69 | * decremented by 1/16th. | ||
70 | */ | ||
71 | #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
72 | |||
73 | |||
74 | /* ****************************** globals ****************************** */ | ||
75 | |||
76 | /** | ||
77 | * Our connection to the datastore. | ||
78 | */ | ||
79 | struct GNUNET_DATASTORE_Handle *GSF_dsh; | ||
80 | |||
81 | /** | ||
82 | * Our configuration. | ||
83 | */ | ||
84 | const struct GNUNET_CONFIGURATION_Handle *GSF_cfg; | ||
85 | |||
86 | /** | ||
87 | * Handle for reporting statistics. | ||
88 | */ | ||
89 | struct GNUNET_STATISTICS_Handle *GSF_stats; | ||
90 | |||
91 | /** | ||
92 | * Pointer to handle to the core service (points to NULL until we've | ||
93 | * connected to it). | ||
94 | */ | ||
95 | struct GNUNET_CORE_Handle *GSF_core; | ||
96 | |||
97 | /** | ||
98 | * Handle for DHT operations. | ||
99 | */ | ||
100 | struct GNUNET_DHT_Handle *GSF_dht; | ||
101 | |||
102 | /** | ||
103 | * How long do requests typically stay in the routing table? | ||
104 | */ | ||
105 | struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime; | ||
106 | |||
107 | /** | ||
108 | * Typical priorities we're seeing from other peers right now. Since | ||
109 | * most priorities will be zero, this value is the weighted average of | ||
110 | * non-zero priorities seen "recently". In order to ensure that new | ||
111 | * values do not dramatically change the ratio, values are first | ||
112 | * "capped" to a reasonable range (+N of the current value) and then | ||
113 | * averaged into the existing value by a ratio of 1:N. Hence | ||
114 | * receiving the largest possible priority can still only raise our | ||
115 | * "current_priorities" by at most 1. | ||
116 | */ | ||
117 | double GSF_current_priorities; | ||
118 | |||
119 | /** | ||
120 | * How many query messages have we received 'recently' that | ||
121 | * have not yet been claimed as cover traffic? | ||
122 | */ | ||
123 | unsigned int GSF_cover_query_count; | ||
124 | |||
125 | /** | ||
126 | * How many content messages have we received 'recently' that | ||
127 | * have not yet been claimed as cover traffic? | ||
128 | */ | ||
129 | unsigned int GSF_cover_content_count; | ||
130 | |||
131 | /** | ||
132 | * Our block context. | ||
133 | */ | ||
134 | struct GNUNET_BLOCK_Context *GSF_block_ctx; | ||
135 | |||
136 | |||
137 | /* ***************************** locals ******************************* */ | ||
138 | |||
139 | /** | ||
140 | * Configuration for block library. | ||
141 | */ | ||
142 | static struct GNUNET_CONFIGURATION_Handle *block_cfg; | ||
143 | |||
144 | /** | ||
145 | * ID of our task that we use to age the cover counters. | ||
146 | */ | ||
147 | static GNUNET_SCHEDULER_TaskIdentifier cover_age_task; | ||
148 | |||
149 | /** | ||
150 | * Pointer to handle to the core service (points to NULL until we've | ||
151 | * connected to it). | ||
152 | */ | ||
153 | static struct GNUNET_CORE_Handle *core; | ||
154 | |||
155 | /** | ||
156 | * Identity of this peer. | ||
157 | */ | ||
158 | static struct GNUNET_PeerIdentity my_id; | ||
159 | |||
160 | /** | ||
161 | * Task that periodically ages our cover traffic statistics. | ||
162 | * | ||
163 | * @param cls unused closure | ||
164 | * @param tc task context | ||
165 | */ | ||
166 | static void | ||
167 | age_cover_counters (void *cls, | ||
168 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
169 | { | ||
170 | GSF_cover_content_count = (GSF_cover_content_count * 15) / 16; | ||
171 | GSF_cover_query_count = (GSF_cover_query_count * 15) / 16; | ||
172 | cover_age_task = GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, | ||
173 | &age_cover_counters, | ||
174 | NULL); | ||
175 | } | ||
176 | |||
177 | |||
178 | /** | ||
179 | * Handle P2P "PUT" message. | ||
180 | * | ||
181 | * @param cls closure, always NULL | ||
182 | * @param other the other peer involved (sender or receiver, NULL | ||
183 | * for loopback messages where we are both sender and receiver) | ||
184 | * @param message the actual message | ||
185 | * @param atsi performance information | ||
186 | * @return GNUNET_OK to keep the connection open, | ||
187 | * GNUNET_SYSERR to close it (signal serious error) | ||
188 | */ | ||
189 | static int | ||
190 | handle_p2p_put (void *cls, | ||
191 | const struct GNUNET_PeerIdentity *other, | ||
192 | const struct GNUNET_MessageHeader *message, | ||
193 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | ||
194 | { | ||
195 | struct GSF_ConnectedPeer *cp; | ||
196 | |||
197 | cp = GSF_peer_get_ (other); | ||
198 | if (NULL == cp) | ||
199 | { | ||
200 | GNUNET_break (0); | ||
201 | return GNUNET_OK; | ||
202 | } | ||
203 | return GSF_handle_p2p_content_ (cp, message); | ||
204 | } | ||
205 | |||
206 | |||
207 | /** | ||
208 | * Decide with what weight we should forward the given | ||
209 | * request to the given peer. | ||
210 | * | ||
211 | * @param cp target peer | ||
212 | * @param pr request | ||
213 | */ | ||
214 | static void | ||
215 | plan (struct GSF_ConnectedPeer *cp, | ||
216 | struct GSF_PendingRequest *pr) | ||
217 | { | ||
218 | GNUNET_CONTAINER_HeapCostType weight; | ||
219 | |||
220 | weight = 0; | ||
221 | /* FIXME: calculate weight properly... */ | ||
222 | GSF_plan_add_ (cp, pr, weight); | ||
223 | } | ||
224 | |||
225 | |||
226 | /** | ||
227 | * Handle P2P "GET" request. | ||
228 | * | ||
229 | * @param cls closure, always NULL | ||
230 | * @param other the other peer involved (sender or receiver, NULL | ||
231 | * for loopback messages where we are both sender and receiver) | ||
232 | * @param message the actual message | ||
233 | * @param atsi performance information | ||
234 | * @return GNUNET_OK to keep the connection open, | ||
235 | * GNUNET_SYSERR to close it (signal serious error) | ||
236 | */ | ||
237 | static int | ||
238 | handle_p2p_get (void *cls, | ||
239 | const struct GNUNET_PeerIdentity *other, | ||
240 | const struct GNUNET_MessageHeader *message, | ||
241 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | ||
242 | { | ||
243 | struct GSF_PendingRequest *pr; | ||
244 | |||
245 | pr = GSF_handle_p2p_query_ (other, message); | ||
246 | if (NULL == pr) | ||
247 | return GNUNET_SYSERR; | ||
248 | /* FIXME: local lookup! */ | ||
249 | /* FIXME: after local lookup, trigger forwarding/routing! */ | ||
250 | return GNUNET_OK; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * We have a new request, consider forwarding it to the given | ||
256 | * peer. | ||
257 | * | ||
258 | * @param cls the 'struct GSF_PendingRequest' | ||
259 | * @param peer identity of the peer | ||
260 | * @param cp handle to the connected peer record | ||
261 | * @param perf peer performance data | ||
262 | */ | ||
263 | static void | ||
264 | consider_request_for_forwarding (void *cls, | ||
265 | const struct GNUNET_PeerIdentity *peer, | ||
266 | struct GSF_ConnectedPeer *cp, | ||
267 | const struct GSF_PeerPerformanceData *ppd) | ||
268 | { | ||
269 | struct GSF_PendingRequest *pr = cls; | ||
270 | |||
271 | plan (cp, pr); | ||
272 | } | ||
273 | |||
274 | |||
275 | /** | ||
276 | * Handle START_SEARCH-message (search request from client). | ||
277 | * | ||
278 | * @param cls closure | ||
279 | * @param client identification of the client | ||
280 | * @param message the actual message | ||
281 | */ | ||
282 | static void | ||
283 | handle_start_search (void *cls, | ||
284 | struct GNUNET_SERVER_Client *client, | ||
285 | const struct GNUNET_MessageHeader *message) | ||
286 | { | ||
287 | struct GSF_PendingRequest *pr; | ||
288 | |||
289 | pr = GSF_local_client_start_search_handler_ (client, message); | ||
290 | if (NULL == pr) | ||
291 | { | ||
292 | /* 'GNUNET_SERVER_receive_done was already called! */ | ||
293 | return; | ||
294 | } | ||
295 | /* FIXME: local lookup, then (after DB done!) receive_done: */ | ||
296 | GNUNET_SERVER_receive_done (client, | ||
297 | GNUNET_OK); | ||
298 | #if 0 | ||
299 | /* FIXME: also do DHT lookup */ | ||
300 | struct GNUNET_DHT_GetHandle *gh; | ||
301 | /* store 'gh' with 'pr', cancel it on pr destruction, etc. */ | ||
302 | gh = GNUNET_DHT_get_start (GSF_dht, | ||
303 | timeout, | ||
304 | type, | ||
305 | key, | ||
306 | des_repl_level, | ||
307 | options, | ||
308 | bf, | ||
309 | bf_mutator, | ||
310 | xquery, | ||
311 | xquery_size, | ||
312 | &GSF_handle_dht_reply_, | ||
313 | pr); | ||
314 | #endif | ||
315 | GSF_iterate_connected_peers_ (&consider_request_for_forwarding, | ||
316 | pr); | ||
317 | } | ||
318 | |||
319 | |||
320 | /** | ||
321 | * Task run during shutdown. | ||
322 | * | ||
323 | * @param cls unused | ||
324 | * @param tc unused | ||
325 | */ | ||
326 | static void | ||
327 | shutdown_task (void *cls, | ||
328 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
329 | { | ||
330 | if (NULL != core) | ||
331 | { | ||
332 | GNUNET_CORE_disconnect (core); | ||
333 | core = NULL; | ||
334 | } | ||
335 | GSF_put_done_ (); | ||
336 | GSF_push_done_ (); | ||
337 | GSF_pending_request_done_ (); | ||
338 | GSF_plan_done (); | ||
339 | GSF_connected_peer_done_ (); | ||
340 | GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO); | ||
341 | GSF_dsh = NULL; | ||
342 | GNUNET_DHT_disconnect (GSF_dht); | ||
343 | GSF_dht = NULL; | ||
344 | GNUNET_BLOCK_context_destroy (GSF_block_ctx); | ||
345 | GSF_block_ctx = NULL; | ||
346 | GNUNET_CONFIGURATION_destroy (block_cfg); | ||
347 | block_cfg = NULL; | ||
348 | GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO); | ||
349 | if (GNUNET_SCHEDULER_NO_TASK != cover_age_task) | ||
350 | { | ||
351 | GNUNET_SCHEDULER_cancel (cover_age_task); | ||
352 | cover_age_task = GNUNET_SCHEDULER_NO_TASK; | ||
353 | } | ||
354 | } | ||
355 | |||
356 | |||
357 | /** | ||
358 | * Function called for each pending request whenever a new | ||
359 | * peer connects, giving us a chance to decide about submitting | ||
360 | * the existing request to the new peer. | ||
361 | * | ||
362 | * @param cls the 'struct GSF_ConnectedPeer' of the new peer | ||
363 | * @param key query for the request | ||
364 | * @param pr handle to the pending request | ||
365 | * @return GNUNET_YES to continue to iterate | ||
366 | */ | ||
367 | static int | ||
368 | consider_peer_for_forwarding (void *cls, | ||
369 | const GNUNET_HashCode *key, | ||
370 | struct GSF_PendingRequest *pr) | ||
371 | { | ||
372 | struct GSF_ConnectedPeer *cp = cls; | ||
373 | |||
374 | plan (cp, pr); | ||
375 | return GNUNET_YES; | ||
376 | } | ||
377 | |||
378 | |||
379 | /** | ||
380 | * Method called whenever a given peer connects. | ||
381 | * | ||
382 | * @param cls closure, not used | ||
383 | * @param peer peer identity this notification is about | ||
384 | * @param atsi performance information | ||
385 | */ | ||
386 | static void | ||
387 | peer_connect_handler (void *cls, | ||
388 | const struct GNUNET_PeerIdentity *peer, | ||
389 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | ||
390 | { | ||
391 | struct GSF_ConnectedPeer *cp; | ||
392 | |||
393 | if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity))) | ||
394 | return; | ||
395 | cp = GSF_peer_connect_handler_ (peer, atsi); | ||
396 | if (NULL == cp) | ||
397 | return; | ||
398 | GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, | ||
399 | cp); | ||
400 | } | ||
401 | |||
402 | |||
403 | /** | ||
404 | * Function called after GNUNET_CORE_connect has succeeded | ||
405 | * (or failed for good). Note that the private key of the | ||
406 | * peer is intentionally not exposed here; if you need it, | ||
407 | * your process should try to read the private key file | ||
408 | * directly (which should work if you are authorized...). | ||
409 | * | ||
410 | * @param cls closure | ||
411 | * @param server handle to the server, NULL if we failed | ||
412 | * @param my_identity ID of this peer, NULL if we failed | ||
413 | * @param publicKey public key of this peer, NULL if we failed | ||
414 | */ | ||
415 | static void | ||
416 | peer_init_handler (void *cls, | ||
417 | struct GNUNET_CORE_Handle * server, | ||
418 | const struct GNUNET_PeerIdentity * | ||
419 | my_identity, | ||
420 | const struct | ||
421 | GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * | ||
422 | publicKey) | ||
423 | { | ||
424 | my_id = *my_identity; | ||
425 | } | ||
426 | |||
427 | |||
428 | /** | ||
429 | * Process fs requests. | ||
430 | * | ||
431 | * @param server the initialized server | ||
432 | * @param c configuration to use | ||
433 | */ | ||
434 | static int | ||
435 | main_init (struct GNUNET_SERVER_Handle *server, | ||
436 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
437 | { | ||
438 | static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = | ||
439 | { | ||
440 | { &handle_p2p_get, | ||
441 | GNUNET_MESSAGE_TYPE_FS_GET, 0 }, | ||
442 | { &handle_p2p_put, | ||
443 | GNUNET_MESSAGE_TYPE_FS_PUT, 0 }, | ||
444 | { &GSF_handle_p2p_migration_stop_, | ||
445 | GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP, | ||
446 | sizeof (struct MigrationStopMessage) }, | ||
447 | { NULL, 0, 0 } | ||
448 | }; | ||
449 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
450 | {&GNUNET_FS_handle_index_start, NULL, | ||
451 | GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0}, | ||
452 | {&GNUNET_FS_handle_index_list_get, NULL, | ||
453 | GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) }, | ||
454 | {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX, | ||
455 | sizeof (struct UnindexMessage) }, | ||
456 | {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH, | ||
457 | 0 }, | ||
458 | {NULL, NULL, 0, 0} | ||
459 | }; | ||
460 | |||
461 | core = GNUNET_CORE_connect (GSF_cfg, | ||
462 | 1, /* larger? */ | ||
463 | NULL, | ||
464 | &peer_init_handler, | ||
465 | &peer_connect_handler, | ||
466 | &GSF_peer_disconnect_handler_, | ||
467 | &GSF_peer_status_handler_, | ||
468 | NULL, GNUNET_NO, | ||
469 | NULL, GNUNET_NO, | ||
470 | p2p_handlers); | ||
471 | if (NULL == core) | ||
472 | { | ||
473 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
474 | _("Failed to connect to `%s' service.\n"), | ||
475 | "core"); | ||
476 | return GNUNET_SYSERR; | ||
477 | } | ||
478 | GNUNET_SERVER_disconnect_notify (server, | ||
479 | &GSF_client_disconnect_handler_, | ||
480 | NULL); | ||
481 | GNUNET_SERVER_add_handlers (server, handlers); | ||
482 | cover_age_task = GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, | ||
483 | &age_cover_counters, | ||
484 | NULL); | ||
485 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
486 | &shutdown_task, | ||
487 | NULL); | ||
488 | return GNUNET_OK; | ||
489 | } | ||
490 | |||
491 | |||
492 | /** | ||
493 | * Process fs requests. | ||
494 | * | ||
495 | * @param cls closure | ||
496 | * @param server the initialized server | ||
497 | * @param cfg configuration to use | ||
498 | */ | ||
499 | static void | ||
500 | run (void *cls, | ||
501 | struct GNUNET_SERVER_Handle *server, | ||
502 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
503 | { | ||
504 | GSF_cfg = cfg; | ||
505 | GSF_dsh = GNUNET_DATASTORE_connect (cfg); | ||
506 | if (GSF_dsh == NULL) | ||
507 | { | ||
508 | GNUNET_SCHEDULER_shutdown (); | ||
509 | return; | ||
510 | } | ||
511 | GSF_stats = GNUNET_STATISTICS_create ("fs", cfg); | ||
512 | block_cfg = GNUNET_CONFIGURATION_create (); | ||
513 | GNUNET_CONFIGURATION_set_value_string (block_cfg, | ||
514 | "block", | ||
515 | "PLUGINS", | ||
516 | "fs"); | ||
517 | GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg); | ||
518 | GNUNET_assert (NULL != GSF_block_ctx); | ||
519 | GSF_dht = GNUNET_DHT_connect (cfg, | ||
520 | FS_DHT_HT_SIZE); | ||
521 | GSF_plan_init (); | ||
522 | GSF_pending_request_init_ (); | ||
523 | GSF_connected_peer_init_ (); | ||
524 | GSF_push_init_ (); | ||
525 | GSF_put_init_ (); | ||
526 | if ( (GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) || | ||
527 | |||
528 | (GNUNET_OK != main_init (server, cfg)) ) | ||
529 | { | ||
530 | GNUNET_SCHEDULER_shutdown (); | ||
531 | shutdown_task (NULL, NULL); | ||
532 | return; | ||
533 | } | ||
534 | } | ||
535 | |||
536 | |||
537 | /** | ||
538 | * The main function for the fs service. | ||
539 | * | ||
540 | * @param argc number of arguments from the command line | ||
541 | * @param argv command line arguments | ||
542 | * @return 0 ok, 1 on error | ||
543 | */ | ||
544 | int | ||
545 | main (int argc, char *const *argv) | ||
546 | { | ||
547 | return (GNUNET_OK == | ||
548 | GNUNET_SERVICE_run (argc, | ||
549 | argv, | ||
550 | "fs", | ||
551 | GNUNET_SERVICE_OPTION_NONE, | ||
552 | &run, NULL)) ? 0 : 1; | ||
553 | } | ||
554 | |||
555 | /* end of gnunet-service-fs.c */ | ||