aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-service-fs.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-31 21:23:23 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-31 21:23:23 +0000
commita78990b412db2c0ead2da8061c4f454f068991d1 (patch)
tree2e87adae62fd1ca3cd5a9f4c69248986f78bb106 /src/fs/gnunet-service-fs.c
parent406c7d2d2d126c994a1fff13470b1f96439c6f9d (diff)
downloadgnunet-a78990b412db2c0ead2da8061c4f454f068991d1.tar.gz
gnunet-a78990b412db2c0ead2da8061c4f454f068991d1.zip
converting FS to new MQ-based core API
Diffstat (limited to 'src/fs/gnunet-service-fs.c')
-rw-r--r--src/fs/gnunet-service-fs.c228
1 files changed, 85 insertions, 143 deletions
diff --git a/src/fs/gnunet-service-fs.c b/src/fs/gnunet-service-fs.c
index 348bab092..bc0da09bc 100644
--- a/src/fs/gnunet-service-fs.c
+++ b/src/fs/gnunet-service-fs.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2009-2014 GNUnet e.V. 3 Copyright (C) 2009-2014, 2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -177,7 +177,7 @@ static struct GNUNET_LOAD_Value *datastore_get_load;
177/** 177/**
178 * Identity of this peer. 178 * Identity of this peer.
179 */ 179 */
180static struct GNUNET_PeerIdentity my_id; 180struct GNUNET_PeerIdentity GSF_my_id;
181 181
182 182
183/** 183/**
@@ -277,33 +277,26 @@ update_latencies (void *cls,
277 277
278 278
279/** 279/**
280 * Handle P2P "PUT" message. 280 * Check P2P "PUT" message.
281 * 281 *
282 * @param cls closure, always NULL 282 * @param cls closure with the `struct GSF_ConnectedPeer`
283 * @param other the other peer involved (sender or receiver, NULL
284 * for loopback messages where we are both sender and receiver)
285 * @param message the actual message 283 * @param message the actual message
286 * @return #GNUNET_OK to keep the connection open, 284 * @return #GNUNET_OK to keep the connection open,
287 * #GNUNET_SYSERR to close it (signal serious error) 285 * #GNUNET_SYSERR to close it (signal serious error)
288 */ 286 */
289static int 287static int
290handle_p2p_put (void *cls, 288check_p2p_put (void *cls,
291 const struct GNUNET_PeerIdentity *other, 289 const struct PutMessage *put)
292 const struct GNUNET_MessageHeader *message)
293{ 290{
294 struct GSF_ConnectedPeer *cp; 291 enum GNUNET_BLOCK_Type type;
295 292
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 293 type = ntohl (put->type);
297 "Received P2P PUT from %s\n", 294 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
298 GNUNET_i2s (other));
299 cp = GSF_peer_get_ (other);
300 if (NULL == cp)
301 { 295 {
302 GNUNET_break (0); 296 GNUNET_break_op (0);
303 return GNUNET_OK; 297 return GNUNET_SYSERR;
304 } 298 }
305 GSF_cover_content_count++; 299 return GNUNET_OK;
306 return GSF_handle_p2p_content_ (cp, message);
307} 300}
308 301
309 302
@@ -324,7 +317,8 @@ consider_request_for_forwarding (void *cls,
324{ 317{
325 struct GSF_PendingRequest *pr = cls; 318 struct GSF_PendingRequest *pr = cls;
326 319
327 if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer)) 320 if (GNUNET_YES !=
321 GSF_pending_request_test_target_ (pr, peer))
328 { 322 {
329#if INSANE_STATISTICS 323#if INSANE_STATISTICS
330 GNUNET_STATISTICS_update (GSF_stats, 324 GNUNET_STATISTICS_update (GSF_stats,
@@ -333,7 +327,8 @@ consider_request_for_forwarding (void *cls,
333#endif 327#endif
334 return; 328 return;
335 } 329 }
336 GSF_plan_add_ (cp, pr); 330 GSF_plan_add_ (cp,
331 pr);
337} 332}
338 333
339 334
@@ -347,10 +342,10 @@ consider_request_for_forwarding (void *cls,
347 * @param pr the pending request we were processing 342 * @param pr the pending request we were processing
348 * @param result final datastore lookup result 343 * @param result final datastore lookup result
349 */ 344 */
350static void 345void
351consider_forwarding (void *cls, 346GSF_consider_forwarding (void *cls,
352 struct GSF_PendingRequest *pr, 347 struct GSF_PendingRequest *pr,
353 enum GNUNET_BLOCK_EvaluationResult result) 348 enum GNUNET_BLOCK_EvaluationResult result)
354{ 349{
355 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result) 350 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
356 return; /* we're done... */ 351 return; /* we're done... */
@@ -363,31 +358,44 @@ consider_forwarding (void *cls,
363 358
364 359
365/** 360/**
366 * Handle P2P "GET" request. 361 * Check P2P "GET" request.
367 * 362 *
368 * @param cls closure, always NULL 363 * @param cls closure
369 * @param other the other peer involved (sender or receiver, NULL 364 * @param gm the actual message
370 * for loopback messages where we are both sender and receiver)
371 * @param message the actual message
372 * @return #GNUNET_OK to keep the connection open, 365 * @return #GNUNET_OK to keep the connection open,
373 * #GNUNET_SYSERR to close it (signal serious error) 366 * #GNUNET_SYSERR to close it (signal serious error)
374 */ 367 */
375static int 368static int
376handle_p2p_get (void *cls, 369check_p2p_get (void *cls,
377 const struct GNUNET_PeerIdentity *other, 370 const struct GetMessage *gm)
378 const struct GNUNET_MessageHeader *message)
379{ 371{
380 struct GSF_PendingRequest *pr; 372 size_t msize;
381 373 unsigned int bm;
382 pr = GSF_handle_p2p_query_ (other, 374 unsigned int bits;
383 message); 375 size_t bfsize;
384 if (NULL == pr) 376
385 return GNUNET_OK; /* exists, identical to existing request, or malformed */ 377 msize = ntohs (gm->header.size);
386 GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES; 378 bm = ntohl (gm->hash_bitmap);
387 GSF_local_lookup_ (pr, 379 bits = 0;
388 &consider_forwarding, 380 while (bm > 0)
389 NULL); 381 {
390 return GNUNET_OK; 382 if (1 == (bm & 1))
383 bits++;
384 bm >>= 1;
385 }
386 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_PeerIdentity))
387 {
388 GNUNET_break_op (0);
389 return GNUNET_SYSERR;
390 }
391 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_PeerIdentity);
392 /* bfsize must be power of 2, check! */
393 if (0 != ((bfsize - 1) & bfsize))
394 {
395 GNUNET_break_op (0);
396 return GNUNET_SYSERR;
397 }
398 return GNUNET_OK;
391} 399}
392 400
393 401
@@ -416,7 +424,8 @@ start_p2p_processing (void *cls,
416 prd = GSF_pending_request_get_data_ (pr); 424 prd = GSF_pending_request_get_data_ (pr);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Finished database lookup for local request `%s' with result %d\n", 426 "Finished database lookup for local request `%s' with result %d\n",
419 GNUNET_h2s (&prd->query), result); 427 GNUNET_h2s (&prd->query),
428 result);
420 if (0 == prd->anonymity_level) 429 if (0 == prd->anonymity_level)
421 { 430 {
422 switch (prd->type) 431 switch (prd->type)
@@ -439,7 +448,7 @@ start_p2p_processing (void *cls,
439 break; 448 break;
440 } 449 }
441 } 450 }
442 consider_forwarding (NULL, pr, result); 451 GSF_consider_forwarding (NULL, pr, result);
443} 452}
444 453
445 454
@@ -538,7 +547,7 @@ shutdown_task (void *cls)
538 GSF_cadet_stop_server (); 547 GSF_cadet_stop_server ();
539 if (NULL != GSF_core) 548 if (NULL != GSF_core)
540 { 549 {
541 GNUNET_CORE_disconnect (GSF_core); 550 GNUNET_CORE_disconnecT (GSF_core);
542 GSF_core = NULL; 551 GSF_core = NULL;
543 } 552 }
544 if (NULL != GSF_ats) 553 if (NULL != GSF_ats)
@@ -575,80 +584,7 @@ shutdown_task (void *cls)
575 584
576 585
577/** 586/**
578 * Function called for each pending request whenever a new 587 * Function called after GNUNET_CORE_connecT has succeeded
579 * peer connects, giving us a chance to decide about submitting
580 * the existing request to the new peer.
581 *
582 * @param cls the `struct GSF_ConnectedPeer` of the new peer
583 * @param key query for the request
584 * @param pr handle to the pending request
585 * @return #GNUNET_YES to continue to iterate
586 */
587static int
588consider_peer_for_forwarding (void *cls,
589 const struct GNUNET_HashCode *key,
590 struct GSF_PendingRequest *pr)
591{
592 struct GSF_ConnectedPeer *cp = cls;
593 struct GNUNET_PeerIdentity pid;
594
595 if (GNUNET_YES !=
596 GSF_pending_request_test_active_ (pr))
597 return GNUNET_YES; /* request is not actually active, skip! */
598 GSF_connected_peer_get_identity_ (cp, &pid);
599 if (GNUNET_YES !=
600 GSF_pending_request_test_target_ (pr, &pid))
601 {
602 GNUNET_STATISTICS_update (GSF_stats,
603 gettext_noop ("# Loopback routes suppressed"),
604 1,
605 GNUNET_NO);
606 return GNUNET_YES;
607 }
608 GSF_plan_add_ (cp, pr);
609 return GNUNET_YES;
610}
611
612
613/**
614 * Function called after the creation of a connected peer record is complete.
615 *
616 * @param cls closure (unused)
617 * @param cp handle to the newly created connected peer record
618 */
619static void
620connected_peer_cb (void *cls,
621 struct GSF_ConnectedPeer *cp)
622{
623 if (NULL == cp)
624 return;
625 GSF_iterate_pending_requests_ (&consider_peer_for_forwarding,
626 cp);
627}
628
629
630/**
631 * Method called whenever a given peer connects.
632 *
633 * @param cls closure, not used
634 * @param peer peer identity this notification is about
635 */
636static void
637peer_connect_handler (void *cls,
638 const struct GNUNET_PeerIdentity *peer)
639{
640 if (0 ==
641 GNUNET_CRYPTO_cmp_peer_identity (&my_id,
642 peer))
643 return;
644 GSF_peer_connect_handler_ (peer,
645 &connected_peer_cb,
646 NULL);
647}
648
649
650/**
651 * Function called after GNUNET_CORE_connect has succeeded
652 * (or failed for good). Note that the private key of the 588 * (or failed for good). Note that the private key of the
653 * peer is intentionally not exposed here; if you need it, 589 * peer is intentionally not exposed here; if you need it,
654 * your process should try to read the private key file 590 * your process should try to read the private key file
@@ -661,7 +597,7 @@ static void
661peer_init_handler (void *cls, 597peer_init_handler (void *cls,
662 const struct GNUNET_PeerIdentity *my_identity) 598 const struct GNUNET_PeerIdentity *my_identity)
663{ 599{
664 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_id, 600 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&GSF_my_id,
665 my_identity)) 601 my_identity))
666 { 602 {
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -681,18 +617,23 @@ static int
681main_init (struct GNUNET_SERVER_Handle *server, 617main_init (struct GNUNET_SERVER_Handle *server,
682 const struct GNUNET_CONFIGURATION_Handle *c) 618 const struct GNUNET_CONFIGURATION_Handle *c)
683{ 619{
684 static const struct GNUNET_CORE_MessageHandler no_p2p_handlers[] = { 620 GNUNET_MQ_hd_var_size (p2p_get,
685 { NULL, 0, 0 } 621 GNUNET_MESSAGE_TYPE_FS_GET,
622 struct GetMessage);
623 GNUNET_MQ_hd_var_size (p2p_put,
624 GNUNET_MESSAGE_TYPE_FS_PUT,
625 struct PutMessage);
626 GNUNET_MQ_hd_fixed_size (p2p_migration_stop,
627 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
628 struct MigrationStopMessage);
629 struct GNUNET_MQ_MessageHandler no_p2p_handlers[] = {
630 GNUNET_MQ_handler_end ()
686 }; 631 };
687 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = { 632 struct GNUNET_MQ_MessageHandler p2p_handlers[] = {
688 { &handle_p2p_get, 633 make_p2p_get_handler (NULL),
689 GNUNET_MESSAGE_TYPE_FS_GET, 0 }, 634 make_p2p_put_handler (NULL),
690 { &handle_p2p_put, 635 make_p2p_migration_stop_handler (NULL),
691 GNUNET_MESSAGE_TYPE_FS_PUT, 0 }, 636 GNUNET_MQ_handler_end ()
692 { &GSF_handle_p2p_migration_stop_,
693 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
694 sizeof (struct MigrationStopMessage) },
695 { NULL, 0, 0 }
696 }; 637 };
697 static const struct GNUNET_SERVER_MessageHandler handlers[] = { 638 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
698 { &GNUNET_FS_handle_index_start, NULL, 639 { &GNUNET_FS_handle_index_start, NULL,
@@ -735,28 +676,29 @@ main_init (struct GNUNET_SERVER_Handle *server,
735 GNUNET_free (keyfile); 676 GNUNET_free (keyfile);
736 GNUNET_assert (NULL != pk); 677 GNUNET_assert (NULL != pk);
737 GNUNET_CRYPTO_eddsa_key_get_public (pk, 678 GNUNET_CRYPTO_eddsa_key_get_public (pk,
738 &my_id.public_key); 679 &GSF_my_id.public_key);
739 680
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "I am peer %s\n", 682 "I am peer %s\n",
742 GNUNET_i2s (&my_id)); 683 GNUNET_i2s (&GSF_my_id));
743 GSF_core 684 GSF_core
744 = GNUNET_CORE_connect (GSF_cfg, NULL, 685 = GNUNET_CORE_connecT (GSF_cfg,
686 NULL,
745 &peer_init_handler, 687 &peer_init_handler,
746 &peer_connect_handler, 688 &GSF_peer_connect_handler,
747 &GSF_peer_disconnect_handler_, 689 &GSF_peer_disconnect_handler,
748 NULL, GNUNET_NO,
749 NULL, GNUNET_NO,
750 (GNUNET_YES == anon_p2p_off) 690 (GNUNET_YES == anon_p2p_off)
751 ? no_p2p_handlers 691 ? no_p2p_handlers
752 : p2p_handlers); 692 : p2p_handlers);
753 if (NULL == GSF_core) 693 if (NULL == GSF_core)
754 { 694 {
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 695 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756 _("Failed to connect to `%s' service.\n"), "core"); 696 _("Failed to connect to `%s' service.\n"),
697 "core");
757 return GNUNET_SYSERR; 698 return GNUNET_SYSERR;
758 } 699 }
759 GNUNET_SERVER_disconnect_notify (server, &GSF_client_disconnect_handler_, 700 GNUNET_SERVER_disconnect_notify (server,
701 &GSF_client_disconnect_handler_,
760 NULL); 702 NULL);
761 GNUNET_SERVER_add_handlers (server, handlers); 703 GNUNET_SERVER_add_handlers (server, handlers);
762 cover_age_task = 704 cover_age_task =