diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 19:40:07 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 19:40:07 +0000 |
commit | 602dd5a348d139cdc65095c21f3f05cf92013a54 (patch) | |
tree | 207c1000657d9bf4ac261349466aa7ae5e272231 /src/dht/gnunet-service-dht.c | |
parent | da0733704943528795d9798c89ecfda1078ca191 (diff) | |
download | gnunet-602dd5a348d139cdc65095c21f3f05cf92013a54.tar.gz gnunet-602dd5a348d139cdc65095c21f3f05cf92013a54.zip |
basic find peer functionality, added to test_dht_api testcase
Diffstat (limited to 'src/dht/gnunet-service-dht.c')
-rw-r--r-- | src/dht/gnunet-service-dht.c | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/src/dht/gnunet-service-dht.c b/src/dht/gnunet-service-dht.c index 7e733615c..906ff63d5 100644 --- a/src/dht/gnunet-service-dht.c +++ b/src/dht/gnunet-service-dht.c | |||
@@ -35,6 +35,8 @@ | |||
35 | #include "gnunet_signal_lib.h" | 35 | #include "gnunet_signal_lib.h" |
36 | #include "gnunet_util_lib.h" | 36 | #include "gnunet_util_lib.h" |
37 | #include "gnunet_datacache_lib.h" | 37 | #include "gnunet_datacache_lib.h" |
38 | #include "gnunet_transport_service.h" | ||
39 | #include "gnunet_hello_lib.h" | ||
38 | #include "dht.h" | 40 | #include "dht.h" |
39 | 41 | ||
40 | /** | 42 | /** |
@@ -63,11 +65,21 @@ static struct GNUNET_TIME_Relative client_transmit_timeout; | |||
63 | static struct GNUNET_CORE_Handle *coreAPI; | 65 | static struct GNUNET_CORE_Handle *coreAPI; |
64 | 66 | ||
65 | /** | 67 | /** |
68 | * Handle to the transport service, for getting our hello | ||
69 | */ | ||
70 | static struct GNUNET_TRANSPORT_Handle *transport_handle; | ||
71 | |||
72 | /** | ||
66 | * The identity of our peer. | 73 | * The identity of our peer. |
67 | */ | 74 | */ |
68 | static struct GNUNET_PeerIdentity my_identity; | 75 | static struct GNUNET_PeerIdentity my_identity; |
69 | 76 | ||
70 | /** | 77 | /** |
78 | * Our HELLO | ||
79 | */ | ||
80 | static struct GNUNET_MessageHeader *my_hello; | ||
81 | |||
82 | /** | ||
71 | * Task to run when we shut down, cleaning up all our trash | 83 | * Task to run when we shut down, cleaning up all our trash |
72 | */ | 84 | */ |
73 | static GNUNET_SCHEDULER_TaskIdentifier cleanup_task; | 85 | static GNUNET_SCHEDULER_TaskIdentifier cleanup_task; |
@@ -395,7 +407,6 @@ send_reply_to_client (struct ClientList *client, | |||
395 | 407 | ||
396 | pending_message = GNUNET_malloc (sizeof (struct PendingMessage)); | 408 | pending_message = GNUNET_malloc (sizeof (struct PendingMessage)); |
397 | pending_message->msg = &reply->header; | 409 | pending_message->msg = &reply->header; |
398 | pending_message->next = NULL; /* We insert at the end of the list */ | ||
399 | 410 | ||
400 | add_pending_message (client, pending_message); | 411 | add_pending_message (client, pending_message); |
401 | } | 412 | } |
@@ -506,6 +517,9 @@ static void | |||
506 | handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, | 517 | handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, |
507 | struct DHT_MessageContext *message_context) | 518 | struct DHT_MessageContext *message_context) |
508 | { | 519 | { |
520 | struct GNUNET_DHT_FindPeerResultMessage *find_peer_result; | ||
521 | size_t hello_size; | ||
522 | size_t tsize; | ||
509 | #if DEBUG_DHT | 523 | #if DEBUG_DHT |
510 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 524 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
511 | "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n", | 525 | "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n", |
@@ -517,6 +531,29 @@ handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, | |||
517 | GNUNET_assert (ntohs (find_msg->header.size) >= | 531 | GNUNET_assert (ntohs (find_msg->header.size) >= |
518 | sizeof (struct GNUNET_DHT_FindPeerMessage)); | 532 | sizeof (struct GNUNET_DHT_FindPeerMessage)); |
519 | 533 | ||
534 | if (my_hello == NULL) | ||
535 | { | ||
536 | #if DEBUG_DHT | ||
537 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
538 | "`%s': Our HELLO is null, can't return.\n", | ||
539 | "DHT"); | ||
540 | #endif | ||
541 | |||
542 | return; | ||
543 | } | ||
544 | |||
545 | /* Simplistic find_peer functionality, always return our hello */ | ||
546 | hello_size = ntohs(my_hello->size); | ||
547 | tsize = hello_size + sizeof (struct GNUNET_DHT_FindPeerResultMessage); | ||
548 | find_peer_result = GNUNET_malloc (tsize); | ||
549 | find_peer_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT); | ||
550 | find_peer_result->header.size = htons (tsize); | ||
551 | find_peer_result->data_size = htons (hello_size); | ||
552 | memcpy(&find_peer_result->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity)); | ||
553 | memcpy (&find_peer_result[1], &my_hello, hello_size); | ||
554 | |||
555 | send_reply_to_client(message_context->client, &find_peer_result->header, message_context->unique_id); | ||
556 | |||
520 | /* FIXME: Implement find peer functionality here */ | 557 | /* FIXME: Implement find peer functionality here */ |
521 | } | 558 | } |
522 | 559 | ||
@@ -784,6 +821,29 @@ handle_dht_p2p_find_peer (void *cls, | |||
784 | return GNUNET_YES; | 821 | return GNUNET_YES; |
785 | } | 822 | } |
786 | 823 | ||
824 | |||
825 | /** | ||
826 | * Receive the HELLO from transport service, | ||
827 | * free current and replace if necessary. | ||
828 | * | ||
829 | * @param cls NULL | ||
830 | * @param message HELLO message of peer | ||
831 | */ | ||
832 | static void | ||
833 | process_hello (void *cls, const struct GNUNET_MessageHeader *message) | ||
834 | { | ||
835 | #if DEBUG_DHT | ||
836 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
837 | "Received our `%s' from transport service\n", | ||
838 | "HELLO"); | ||
839 | #endif | ||
840 | |||
841 | GNUNET_assert (message != NULL); | ||
842 | GNUNET_free_non_null(my_hello); | ||
843 | my_hello = GNUNET_malloc(ntohs(message->size)); | ||
844 | memcpy(my_hello, message, ntohs(message->size)); | ||
845 | } | ||
846 | |||
787 | /** | 847 | /** |
788 | * Task run during shutdown. | 848 | * Task run during shutdown. |
789 | * | 849 | * |
@@ -793,9 +853,16 @@ handle_dht_p2p_find_peer (void *cls, | |||
793 | static void | 853 | static void |
794 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 854 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
795 | { | 855 | { |
856 | if (transport_handle != NULL) | ||
857 | { | ||
858 | GNUNET_free_non_null(my_hello); | ||
859 | GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL); | ||
860 | GNUNET_TRANSPORT_disconnect(transport_handle); | ||
861 | } | ||
796 | GNUNET_CORE_disconnect (coreAPI); | 862 | GNUNET_CORE_disconnect (coreAPI); |
797 | } | 863 | } |
798 | 864 | ||
865 | |||
799 | /** | 866 | /** |
800 | * To be called on core init/fail. | 867 | * To be called on core init/fail. |
801 | * | 868 | * |
@@ -828,6 +895,7 @@ core_init (void *cls, | |||
828 | coreAPI = server; | 895 | coreAPI = server; |
829 | } | 896 | } |
830 | 897 | ||
898 | |||
831 | /** | 899 | /** |
832 | * Process dht requests. | 900 | * Process dht requests. |
833 | * | 901 | * |
@@ -865,6 +933,14 @@ run (void *cls, | |||
865 | GNUNET_NO, /* For header only outbound notification */ | 933 | GNUNET_NO, /* For header only outbound notification */ |
866 | core_handlers); /* Register these handlers */ | 934 | core_handlers); /* Register these handlers */ |
867 | 935 | ||
936 | transport_handle = GNUNET_TRANSPORT_connect(sched, cfg, NULL, NULL, NULL, NULL); | ||
937 | |||
938 | if (transport_handle != NULL) | ||
939 | GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL); | ||
940 | else | ||
941 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to connect to transport service!\n"); | ||
942 | |||
943 | |||
868 | if (coreAPI == NULL) | 944 | if (coreAPI == NULL) |
869 | return; | 945 | return; |
870 | 946 | ||