From 4803fd2b87550bb34fa8bd88ededb8f73d5d0b4d Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Thu, 8 Mar 2012 16:13:47 +0000 Subject: [PATCH] -lookup now works in test, removed dns hijack from defaults --- src/gns/Makefile.am | 32 ++++--- src/gns/gns.conf.in | 3 +- src/gns/gns_api.c | 87 ++++++++++++++--- src/gns/gnunet-service-gns.c | 139 +++++++++++++++++++++++++++- src/gns/test_gns_simple_lookup.c | 68 +++++++++----- src/gns/test_gns_simple_lookup.conf | 2 +- src/gns/test_gns_simple_shorten.c | 9 +- src/include/gnunet_gns_service.h | 15 ++- 8 files changed, 289 insertions(+), 66 deletions(-) diff --git a/src/gns/Makefile.am b/src/gns/Makefile.am index 65081436f..912468774 100644 --- a/src/gns/Makefile.am +++ b/src/gns/Makefile.am @@ -27,7 +27,8 @@ bin_PROGRAMS = \ # gnunet-gns-lookup check_PROGRAMS = \ - test_gns_simple_shorten + test_gns_simple_shorten \ + test_gns_simple_lookup # test_gns_simple_lookup @@ -49,16 +50,18 @@ plugin_LTLIBRARIES = \ # $(top_builddir)/src/namestore/libgnunetnamestore.la \ # $(top_builddir)/src/testing/libgnunettesting.la -#test_gns_simple_lookup_SOURCES = \ -# test_gns_simple_lookup.c -#test_gns_simple_lookup_LDADD = \ -# $(top_builddir)/src/util/libgnunetutil.la \ -# $(top_builddir)/src/namestore/libgnunetnamestore.la \ -# $(top_builddir)/src/testing/libgnunettesting.la -#test_gns_simple_lookup_DEPENDENCIES = \ -# $(top_builddir)/src/util/libgnunetutil.la \ -# $(top_builddir)/src/namestore/libgnunetnamestore.la \ -# $(top_builddir)/src/testing/libgnunettesting.la +test_gns_simple_lookup_SOURCES = \ + test_gns_simple_lookup.c +test_gns_simple_lookup_LDADD = \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/namestore/libgnunetnamestore.la \ + $(top_builddir)/src/gns/libgnunetgns.la \ + $(top_builddir)/src/testing/libgnunettesting.la +test_gns_simple_lookup_DEPENDENCIES = \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/namestore/libgnunetnamestore.la \ + $(top_builddir)/src/gns/libgnunetgns.la \ + $(top_builddir)/src/testing/libgnunettesting.la #test_gns_simple_delegated_lookup_SOURCES = \ # test_gns_simple_delegated_lookup.c @@ -104,6 +107,7 @@ gnunet_gns_SOURCES = \ gnunet_gns_LDADD = \ $(top_builddir)/src/gns/libgnunetgns.la \ $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/namestore/libgnunetnamestore.la \ $(GN_LIBINTL) gnunet_gns_DEPENDENCIES = \ libgnunetgns.la @@ -145,11 +149,13 @@ gnunet_gns_fcfsd_DEPENDENCIES = \ libgnunetgns_la_SOURCES = \ gns_api.c gns.h libgnunetgns_la_LIBADD = \ - $(top_builddir)/src/util/libgnunetutil.la $(XLIB) + $(top_builddir)/src/util/libgnunetutil.la $(XLIB) \ + $(top_builddir)/src/namestore/libgnunetnamestore.la libgnunetgns_la_LDFLAGS = \ $(GN_LIB_LDFLAGS) libgnunetgns_la_DEPENDENCIES = \ - $(top_builddir)/src/util/libgnunetutil.la + $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/namestore/libgnunetnamestore.la libgnunet_plugin_block_gns_la_SOURCES = \ plugin_block_gns.c diff --git a/src/gns/gns.conf.in b/src/gns/gns.conf.in index 07a955a9c..a2286f232 100644 --- a/src/gns/gns.conf.in +++ b/src/gns/gns.conf.in @@ -6,8 +6,7 @@ CONFIG = $DEFAULTCONFIG BINARY = gnunet-service-gns UNIXPATH = /tmp/gnunet-service-gns.sock ZONEKEY = $SERVICEHOME/gns/zonekey -HIJACK_DNS = YES -OPTIONS = -L INFO +HIJACK_DNS = NO [fcfsd] HTTPPORT = 18080 diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c index 47efe5058..f9aa63335 100644 --- a/src/gns/gns_api.c +++ b/src/gns/gns_api.c @@ -35,10 +35,6 @@ #include "gns.h" #include "gnunet_gns_service.h" -#define DEBUG_GNS_API GNUNET_EXTRA_LOGGING - -#define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__) - /* TODO into gnunet_protocols */ #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24 @@ -67,7 +63,10 @@ struct GNUNET_GNS_QueueEntry struct GNUNET_GNS_Handle *gns_handle; /* processor to call on shorten result */ - GNUNET_GNS_ShortenResultProcessor proc; + GNUNET_GNS_ShortenResultProcessor shorten_proc; + + /* processor to call on lookup result */ + GNUNET_GNS_LookupResultProcessor lookup_proc; /* processor closure */ void *proc_cls; @@ -355,7 +354,7 @@ process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe, "Received shortened reply `%s' from GNS service\n", short_name); - qe->proc(qe->proc_cls, short_name); + qe->shorten_proc(qe->proc_cls, short_name); } @@ -373,6 +372,31 @@ static void process_lookup_reply (struct GNUNET_GNS_QueueEntry *qe, const struct GNUNET_GNS_ClientLookupResultMessage *msg) { + struct GNUNET_GNS_Handle *h = qe->gns_handle; + int rd_count = ntohl(msg->rd_count); + size_t len = ntohs (((struct GNUNET_MessageHeader*)msg)->size); + struct GNUNET_NAMESTORE_RecordData rd[rd_count]; + + GNUNET_CONTAINER_DLL_remove(h->lookup_head, h->lookup_tail, qe); + + if (len < sizeof (struct GNUNET_GNS_ClientLookupResultMessage)) + { + GNUNET_break (0); + force_reconnect (h); + return; + } + + len -= sizeof(struct GNUNET_GNS_ClientLookupResultMessage); + + GNUNET_NAMESTORE_records_deserialize (len, (char*)&msg[1], + rd_count, + rd); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received lookup reply from GNS service (count=%d)\n", + ntohl(msg->rd_count)); + + qe->lookup_proc(qe->proc_cls, rd_count, rd); } /** @@ -522,12 +546,51 @@ get_request_id (struct GNUNET_GNS_Handle *h) */ struct GNUNET_GNS_QueueEntry * GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, - const char * name, - enum GNUNET_GNS_RecordType type, - GNUNET_GNS_LookupIterator iter, - void *iter_cls) + const char * name, + enum GNUNET_GNS_RecordType type, + GNUNET_GNS_LookupResultProcessor proc, + void *proc_cls) { - return NULL; + /* IPC to shorten gns names, return shorten_handle */ + struct GNUNET_GNS_ClientLookupMessage *lookup_msg; + struct GNUNET_GNS_QueueEntry *qe; + size_t msize; + struct PendingMessage *pending; + + if (NULL == name) + { + return NULL; + } + + msize = sizeof (struct GNUNET_GNS_ClientLookupMessage) + strlen(name) + 1; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to shorten %s in GNS\n", name); + + qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry)); + qe->gns_handle = handle; + qe->lookup_proc = proc; + qe->proc_cls = proc_cls; + qe->r_id = get_request_id(handle); + GNUNET_CONTAINER_DLL_insert_tail(handle->lookup_head, + handle->lookup_tail, qe); + + pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize); + memset(pending, 0, (sizeof (struct PendingMessage) + msize)); + + pending->size = msize; + + lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1]; + lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP); + lookup_msg->header.size = htons (msize); + lookup_msg->id = htonl(qe->r_id); + lookup_msg->type = htonl(type); + + memcpy(&lookup_msg[1], name, strlen(name)); + + GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail, + pending); + + process_pending_messages (handle); + return qe; } @@ -562,7 +625,7 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle, qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry)); qe->gns_handle = handle; - qe->proc = proc; + qe->shorten_proc = proc; qe->proc_cls = proc_cls; qe->r_id = get_request_id(handle); GNUNET_CONTAINER_DLL_insert_tail(handle->shorten_head, diff --git a/src/gns/gnunet-service-gns.c b/src/gns/gnunet-service-gns.c index ce83af60d..19195d419 100644 --- a/src/gns/gnunet-service-gns.c +++ b/src/gns/gnunet-service-gns.c @@ -153,8 +153,8 @@ struct ClientShortenHandle /* request id */ uint64_t unique_id; - /* request key */ - GNUNET_HashCode key; + /* request type */ + enum GNUNET_GNS_RecordType type; /* name to shorten */ char* name; @@ -172,8 +172,8 @@ struct ClientLookupHandle /* request id */ uint64_t unique_id; - /* request key */ - GNUNET_HashCode key; + /* request type */ + enum GNUNET_GNS_RecordType type; /* the name to look up */ char* name; //Needed? @@ -2229,13 +2229,142 @@ static void handle_shorten(void *cls, } /** - * TODO + * Reply to client with the result from our lookup. + * + * @param cls the closure (our client lookup handle) + * @param rh the request handle of the lookup + * @param rd_count the number of records + * @param rd the record data + */ +static void +reply_to_client(void* cls, struct GNUNET_GNS_ResolverHandle *rh, + uint32_t rd_count, + const struct GNUNET_NAMESTORE_RecordData *rd) +{ + struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls; + struct GNUNET_GNS_ClientLookupResultMessage *rmsg; + size_t len; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n", + "LOOKUP_RESULT", rd_count); + + len = GNUNET_NAMESTORE_records_get_size (rd_count, rd); + rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage)); + + rmsg->id = clh->unique_id; + rmsg->rd_count = htonl(rd_count); + rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT); + rmsg->header.size = + htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage)); + + GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]); + + GNUNET_SERVER_notification_context_unicast (nc, clh->client, + (const struct GNUNET_MessageHeader *) rmsg, + GNUNET_NO); + GNUNET_SERVER_receive_done (clh->client, GNUNET_OK); + GNUNET_free(rmsg); + GNUNET_free(clh->name); + GNUNET_free(clh); + +} + +/** + * Lookup a given name + * + * @param name the name to looku[ + * @param clh the client lookup handle + */ +static void +lookup_name(char* name, struct ClientLookupHandle* clh) +{ + + struct GNUNET_GNS_ResolverHandle *rh; + struct RecordLookupHandle* rlh; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Starting resolution for %s (type=%d)!\n", + name, clh->type); + + rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle)); + rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle)); + + rh->authority = zone_hash; + + rlh->record_type = clh->type; + rlh->name = clh->name; + rlh->proc = &reply_to_client; + rlh->proc_cls = clh; + + rh->proc_cls = rlh; + + rh->name = GNUNET_malloc(strlen(name) + - strlen(gnunet_tld) + 1); + memset(rh->name, 0, + strlen(name)-strlen(gnunet_tld) + 1); + memcpy(rh->name, name, + strlen(name)-strlen(gnunet_tld)); + + rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH); + + rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain)); + rh->authority_chain_head->prev = NULL; + rh->authority_chain_head->next = NULL; + rh->authority_chain_tail = rh->authority_chain_head; + rh->authority_chain_head->zone = zone_hash; + + /* Start resolution in our zone */ + rh->proc = &process_ns_delegation_dns; //FIXME rename + resolve_delegation_from_ns(rh); +} + + +/** + * Handle lookup requests from client + * + * @param cls the closure + * @param client the client + * @param message the message */ static void handle_lookup(void *cls, struct GNUNET_SERVER_Client * client, const struct GNUNET_MessageHeader * message) { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP"); + + size_t msg_size = 0; + struct ClientLookupHandle *clh; + + if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage)) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + GNUNET_SERVER_notification_context_add (nc, client); + + struct GNUNET_GNS_ClientLookupMessage *sh_msg = + (struct GNUNET_GNS_ClientLookupMessage *) message; + + msg_size = ntohs(message->size); + + if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + clh = GNUNET_malloc(sizeof(struct ClientLookupHandle)); + clh->client = client; + clh->name = GNUNET_malloc(strlen((char*)&sh_msg[1]) + 1); + strcpy(clh->name, (char*)&sh_msg[1]); + clh->unique_id = sh_msg->id; + clh->type = ntohl(sh_msg->type); + + lookup_name((char*)&sh_msg[1], clh); } /** diff --git a/src/gns/test_gns_simple_lookup.c b/src/gns/test_gns_simple_lookup.c index d75ff2d28..0142677d7 100644 --- a/src/gns/test_gns_simple_lookup.c +++ b/src/gns/test_gns_simple_lookup.c @@ -77,6 +77,8 @@ static int ok; static struct GNUNET_NAMESTORE_Handle *namestore_handle; +static struct GNUNET_GNS_Handle *gns_handle; + const struct GNUNET_CONFIGURATION_Handle *cfg; /** @@ -95,41 +97,39 @@ shutdown_callback (void *cls, const char *emsg) GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok); } -/** - * Function scheduled to be run on the successful start of services - * tries to look up the dns record for TEST_DOMAIN - */ + static void -finish_testing (void *cls, int32_t success, const char *emsg) +on_lookup_result(void *cls, uint32_t rd_count, + const struct GNUNET_NAMESTORE_RecordData *rd) { - struct hostent *he; struct in_addr a; + int i; char* addr; - GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES); - - he = gethostbyname (TEST_DOMAIN); - - if (!he) + if (rd_count == 0) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "gethostbyname failed, rp_filtering?\n"); + "Lookup failed, rp_filtering?\n"); ok = 2; } else { ok = 1; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", he->h_name); - while (*he->h_addr_list) + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls); + for (i=0; ih_addr_list++, sizeof(a)); - addr = inet_ntoa(a); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr); - if (0 == strcmp(addr, TEST_IP)) + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type); + if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, + memcpy(&a, rd[i].data, sizeof(a)); + addr = inet_ntoa(a); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr); + if (0 == strcmp(addr, TEST_IP)) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s correctly resolved to %s!\n", TEST_DOMAIN, addr); - ok = 0; + ok = 0; + } } else { @@ -142,6 +142,32 @@ finish_testing (void *cls, int32_t success, const char *emsg) GNUNET_YES, GNUNET_NO); } + +/** + * Function scheduled to be run on the successful start of services + * tries to look up the dns record for TEST_DOMAIN + */ +static void +commence_testing (void *cls, int32_t success, const char *emsg) +{ + + + GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES); + + gns_handle = GNUNET_GNS_connect(cfg); + + if (NULL == gns_handle) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to connect to GNS!\n"); + ok = 2; + } + + GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_A, + &on_lookup_result, TEST_DOMAIN); +} + + /** * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut * down the peers without freeing memory associated with GET request. @@ -216,7 +242,7 @@ do_lookup(void *cls, const struct GNUNET_PeerIdentity *id, alice_key, TEST_RECORD_NAME, &rd, - &finish_testing, + &commence_testing, NULL); } diff --git a/src/gns/test_gns_simple_lookup.conf b/src/gns/test_gns_simple_lookup.conf index cda207a1e..da61f3d25 100644 --- a/src/gns/test_gns_simple_lookup.conf +++ b/src/gns/test_gns_simple_lookup.conf @@ -74,7 +74,7 @@ DNS_EXIT = 8.8.8.8 AUTOSTART = YES BINARY = gnunet-service-gns ZONEKEY = zonekey -HIJACK_DNS = YES +HIJACK_DNS = NO UNIXPATH = /tmp/gnunet-service-gns.sock HOME = $SERVICEHOME CONFIG = $DEFAULTCONFIG diff --git a/src/gns/test_gns_simple_shorten.c b/src/gns/test_gns_simple_shorten.c index 0f36447f6..fc0c0f1f0 100644 --- a/src/gns/test_gns_simple_shorten.c +++ b/src/gns/test_gns_simple_shorten.c @@ -93,7 +93,7 @@ shutdown_callback (void *cls, const char *emsg) * Called when gns shorten finishes */ static void -process_shorten_result(void* cls, const char* lname, const char* sname) +process_shorten_result(void* cls, const char* sname) { GNUNET_GNS_disconnect(gns_handle); @@ -108,12 +108,12 @@ process_shorten_result(void* cls, const char* lname, const char* sname) else { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "%s shortened to %s\n", lname, sname); + "%s shortened to %s\n", (char*)cls, sname); if (0 != strcmp(sname, TEST_EXPECTED_RESULT)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "shorten test failed! (wanted: %s got: %s\n", - lname, sname); + (char*)cls, sname); ok = 1; } @@ -152,7 +152,8 @@ commence_testing (void *cls, int32_t success, const char *emsg) return; } - GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result, NULL); + GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result, + TEST_DOMAIN); } diff --git a/src/include/gnunet_gns_service.h b/src/include/gnunet_gns_service.h index 60a2fb227..5ae7fbefe 100644 --- a/src/include/gnunet_gns_service.h +++ b/src/include/gnunet_gns_service.h @@ -104,7 +104,7 @@ GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle); /* *************** Standard API: lookup ******************* */ /** - * Iterator called on each result obtained for a GNS + * Iterator called on obtained result for a GNS * lookup * * @param cls closure @@ -112,8 +112,7 @@ GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle); * @param rd_count number of records * @param rd the records in reply */ -typedef void (*GNUNET_GNS_LookupIterator) (void *cls, - const char * name, +typedef void (*GNUNET_GNS_LookupResultProcessor) (void *cls, uint32_t rd_count, const struct GNUNET_NAMESTORE_RecordData *rd); @@ -125,17 +124,17 @@ typedef void (*GNUNET_GNS_LookupIterator) (void *cls, * @param handle handle to the GNS service * @param name the name to look up * @param type the GNUNET_GNS_RecordType to look for - * @param iter function to call on each result - * @param iter_cls closure for iter + * @param proc function to call on result + * @param proc_cls closure for iter * - * @return handle to stop the async lookup + * @return handle to the queued request */ struct GNUNET_GNS_QueueEntry * GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, const char * name, enum GNUNET_GNS_RecordType type, - GNUNET_GNS_LookupIterator iter, - void *iter_cls); + GNUNET_GNS_LookupResultProcessor proc, + void *proc_cls); /* *************** Standard API: shorten ******************* */ -- 2.25.1