From 602dd5a348d139cdc65095c21f3f05cf92013a54 Mon Sep 17 00:00:00 2001 From: "Nathan S. Evans" Date: Mon, 5 Apr 2010 19:40:07 +0000 Subject: [PATCH] basic find peer functionality, added to test_dht_api testcase --- src/dht/Makefile.am | 2 + src/dht/dht_api.c | 6 ++- src/dht/gnunet-service-dht.c | 78 +++++++++++++++++++++++++++++++++++- src/dht/test_dht_api.c | 24 ++++++++++- 4 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/dht/Makefile.am b/src/dht/Makefile.am index ecf5a2e92..f941b0dfb 100644 --- a/src/dht/Makefile.am +++ b/src/dht/Makefile.am @@ -31,6 +31,8 @@ gnunet_service_dht_SOURCES = \ gnunet_service_dht_LDADD = \ $(top_builddir)/src/statistics/libgnunetstatistics.la \ $(top_builddir)/src/core/libgnunetcore.la \ + $(top_builddir)/src/transport/libgnunettransport.la \ + $(top_builddir)/src/hello/libgnunethello.la \ $(top_builddir)/src/datacache/libgnunetdatacache.la \ $(top_builddir)/src/util/libgnunetutil.la diff --git a/src/dht/dht_api.c b/src/dht/dht_api.c index 2fa5fa79f..12a83fc72 100644 --- a/src/dht/dht_api.c +++ b/src/dht/dht_api.c @@ -662,6 +662,10 @@ find_peer_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply) size_t data_size; struct GNUNET_MessageHeader *result_data; +#if DEBUG_DHT_API + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Find peer iterator called.\n"); +#endif if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) return; @@ -670,7 +674,7 @@ find_peer_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply) result = (struct GNUNET_DHT_FindPeerResultMessage *) reply; data_size = ntohs (result->data_size); GNUNET_assert (ntohs (reply->size) == - sizeof (struct GNUNET_DHT_GetResultMessage) + data_size); + sizeof (struct GNUNET_DHT_FindPeerResultMessage) + data_size); if (data_size > 0) result_data = (struct GNUNET_MessageHeader *) &result[1]; /* Set data pointer to end of message */ 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 @@ #include "gnunet_signal_lib.h" #include "gnunet_util_lib.h" #include "gnunet_datacache_lib.h" +#include "gnunet_transport_service.h" +#include "gnunet_hello_lib.h" #include "dht.h" /** @@ -62,11 +64,21 @@ static struct GNUNET_TIME_Relative client_transmit_timeout; */ static struct GNUNET_CORE_Handle *coreAPI; +/** + * Handle to the transport service, for getting our hello + */ +static struct GNUNET_TRANSPORT_Handle *transport_handle; + /** * The identity of our peer. */ static struct GNUNET_PeerIdentity my_identity; +/** + * Our HELLO + */ +static struct GNUNET_MessageHeader *my_hello; + /** * Task to run when we shut down, cleaning up all our trash */ @@ -395,7 +407,6 @@ send_reply_to_client (struct ClientList *client, pending_message = GNUNET_malloc (sizeof (struct PendingMessage)); pending_message->msg = &reply->header; - pending_message->next = NULL; /* We insert at the end of the list */ add_pending_message (client, pending_message); } @@ -506,6 +517,9 @@ static void handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, struct DHT_MessageContext *message_context) { + struct GNUNET_DHT_FindPeerResultMessage *find_peer_result; + size_t hello_size; + size_t tsize; #if DEBUG_DHT GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%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, GNUNET_assert (ntohs (find_msg->header.size) >= sizeof (struct GNUNET_DHT_FindPeerMessage)); + if (my_hello == NULL) + { +#if DEBUG_DHT + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "`%s': Our HELLO is null, can't return.\n", + "DHT"); +#endif + + return; + } + + /* Simplistic find_peer functionality, always return our hello */ + hello_size = ntohs(my_hello->size); + tsize = hello_size + sizeof (struct GNUNET_DHT_FindPeerResultMessage); + find_peer_result = GNUNET_malloc (tsize); + find_peer_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT); + find_peer_result->header.size = htons (tsize); + find_peer_result->data_size = htons (hello_size); + memcpy(&find_peer_result->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity)); + memcpy (&find_peer_result[1], &my_hello, hello_size); + + send_reply_to_client(message_context->client, &find_peer_result->header, message_context->unique_id); + /* FIXME: Implement find peer functionality here */ } @@ -784,6 +821,29 @@ handle_dht_p2p_find_peer (void *cls, return GNUNET_YES; } + +/** + * Receive the HELLO from transport service, + * free current and replace if necessary. + * + * @param cls NULL + * @param message HELLO message of peer + */ +static void +process_hello (void *cls, const struct GNUNET_MessageHeader *message) +{ +#if DEBUG_DHT + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received our `%s' from transport service\n", + "HELLO"); +#endif + + GNUNET_assert (message != NULL); + GNUNET_free_non_null(my_hello); + my_hello = GNUNET_malloc(ntohs(message->size)); + memcpy(my_hello, message, ntohs(message->size)); +} + /** * Task run during shutdown. * @@ -793,9 +853,16 @@ handle_dht_p2p_find_peer (void *cls, static void shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { + if (transport_handle != NULL) + { + GNUNET_free_non_null(my_hello); + GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL); + GNUNET_TRANSPORT_disconnect(transport_handle); + } GNUNET_CORE_disconnect (coreAPI); } + /** * To be called on core init/fail. * @@ -828,6 +895,7 @@ core_init (void *cls, coreAPI = server; } + /** * Process dht requests. * @@ -865,6 +933,14 @@ run (void *cls, GNUNET_NO, /* For header only outbound notification */ core_handlers); /* Register these handlers */ + transport_handle = GNUNET_TRANSPORT_connect(sched, cfg, NULL, NULL, NULL, NULL); + + if (transport_handle != NULL) + GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL); + else + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to connect to transport service!\n"); + + if (coreAPI == NULL) return; diff --git a/src/dht/test_dht_api.c b/src/dht/test_dht_api.c index f9d9e008c..f09a00879 100644 --- a/src/dht/test_dht_api.c +++ b/src/dht/test_dht_api.c @@ -147,6 +147,28 @@ test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } + +/** + * Iterator called on each result obtained from a find peer + * operation + * + * @param cls closure (NULL) + * @param peer the peer we learned about + * @param reply response + */ +void test_find_peer_processor (void *cls, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *reply) +{ + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer)); + + GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1, + GNUNET_SCHEDULER_REASON_PREREQ_DONE); +} + + /** * Signature of the main function of a task. * @@ -165,7 +187,7 @@ test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) peer->find_peer_handle = GNUNET_DHT_find_peer_start (peer->dht_handle, TIMEOUT, 0, NULL, &hash, - NULL, NULL, &test_find_peer_stop, &p1); + &test_find_peer_processor, NULL, NULL, NULL); if (peer->find_peer_handle == NULL) GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1); -- 2.25.1