From a9e83be91832bfa07b33c8d865cab9f00df05b78 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Wed, 9 May 2012 09:50:13 +0000 Subject: [PATCH] -new per zone api --- src/gns/gns.h | 21 ++++++- src/gns/gns_api.c | 69 ++++++++++++++++++++++- src/gns/gnunet-service-gns.c | 94 ++++++++++++++++++++++++++++++-- src/include/gnunet_gns_service.h | 40 +++++++++++++- 4 files changed, 214 insertions(+), 10 deletions(-) diff --git a/src/gns/gns.h b/src/gns/gns.h index 5950c460b..408b68606 100644 --- a/src/gns/gns.h +++ b/src/gns/gns.h @@ -52,10 +52,19 @@ struct GNUNET_GNS_ClientLookupMessage */ uint32_t id GNUNET_PACKED; + /** + * Should we look up in the default zone? + */ + uint32_t use_default_zone GNUNET_PACKED; + + /** + * If use_default_zone is empty this zone is used for lookup + */ + struct GNUNET_CRYPTO_ShortHashCode zone; + /** * the type of record to look up */ - // FIXME: bad type - should be of GNUNET_GNS_RecordType enum GNUNET_GNS_RecordType type; /* Followed by the name to look up */ @@ -102,6 +111,16 @@ struct GNUNET_GNS_ClientShortenMessage */ uint32_t id GNUNET_PACKED; + /** + * Should we look up in the default zone? + */ + uint32_t use_default_zone GNUNET_PACKED; + + /** + * If use_default_zone is empty this zone is used for lookup + */ + struct GNUNET_CRYPTO_ShortHashCode zone; + /* Followed by the name to shorten up */ }; diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c index 1a06f34e0..a92280f25 100644 --- a/src/gns/gns_api.c +++ b/src/gns/gns_api.c @@ -639,14 +639,16 @@ get_request_id (struct GNUNET_GNS_Handle *h) * * @param handle handle to the GNS service * @param name the name to look up + * @param zone the zone to start the resolution in * @param type the record type to look up * @param proc processor to call on result * @param proc_cls closure for processor * @return handle to the get */ struct GNUNET_GNS_QueueEntry * -GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, +GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle, const char * name, + struct GNUNET_CRYPTO_ShortHashCode *zone, enum GNUNET_GNS_RecordType type, GNUNET_GNS_LookupResultProcessor proc, void *proc_cls) @@ -682,6 +684,18 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP); lookup_msg->header.size = htons (msize); lookup_msg->id = htonl(qe->r_id); + + if (NULL != zone) + { + lookup_msg->use_default_zone = htonl(0); + memcpy(&lookup_msg->zone, zone, sizeof(struct GNUNET_CRYPTO_ShortHashCode)); + } + else + { + lookup_msg->use_default_zone = htonl(1); + memset(&lookup_msg->zone, 0, sizeof(struct GNUNET_CRYPTO_ShortHashCode)); + } + lookup_msg->type = htonl(type); memcpy(&lookup_msg[1], name, strlen(name)); @@ -693,19 +707,40 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, return qe; } +/** + * Perform an asynchronous Lookup operation on the GNS. + * + * @param handle handle to the GNS service + * @param name the name to look up + * @param type the record type to look up + * @param proc processor to call on result + * @param proc_cls closure for processor + * @return handle to the get + */ +struct GNUNET_GNS_QueueEntry * +GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, + const char * name, + enum GNUNET_GNS_RecordType type, + GNUNET_GNS_LookupResultProcessor proc, + void *proc_cls) +{ + return GNUNET_GNS_lookup_zone (handle, name, NULL, type, proc, proc_cls); +} /** * Perform a name shortening operation on the GNS. * * @param handle handle to the GNS service * @param name the name to look up + * @param zone the zone to start the resolution in * @param proc function to call on result * @param proc_cls closure for processor * @return handle to the operation */ struct GNUNET_GNS_QueueEntry * -GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle, +GNUNET_GNS_shorten_zone (struct GNUNET_GNS_Handle *handle, const char * name, + struct GNUNET_CRYPTO_ShortHashCode *zone, GNUNET_GNS_ShortenResultProcessor proc, void *proc_cls) { @@ -740,6 +775,18 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle, shorten_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_SHORTEN); shorten_msg->header.size = htons (msize); shorten_msg->id = htonl(qe->r_id); + + if (NULL != zone) + { + shorten_msg->use_default_zone = htonl(0); + memcpy(&shorten_msg->zone, zone, + sizeof(struct GNUNET_CRYPTO_ShortHashCode)); + } + else + { + shorten_msg->use_default_zone = htonl(1); + memset(&shorten_msg->zone, 0, sizeof(struct GNUNET_CRYPTO_ShortHashCode)); + } memcpy(&shorten_msg[1], name, strlen(name)); @@ -750,7 +797,23 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle, return qe; } - +/** + * Perform a name shortening operation on the GNS. + * + * @param handle handle to the GNS service + * @param name the name to look up + * @param proc function to call on result + * @param proc_cls closure for processor + * @return handle to the operation + */ +struct GNUNET_GNS_QueueEntry * +GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle, + const char * name, + GNUNET_GNS_ShortenResultProcessor proc, + void *proc_cls) +{ + return GNUNET_GNS_shorten_zone (handle, name, NULL, proc, proc_cls); +} /** * Perform an authority lookup for a given name. * diff --git a/src/gns/gnunet-service-gns.c b/src/gns/gnunet-service-gns.c index a27d79cc7..d964a6f68 100644 --- a/src/gns/gnunet-service-gns.c +++ b/src/gns/gnunet-service-gns.c @@ -60,6 +60,9 @@ struct ClientShortenHandle /* request type */ enum GNUNET_GNS_RecordType type; + /* optional zone private key used for lookup */ + struct GNUNET_CRYPTO_RsaPrivateKey *zone_key; + /* name to shorten */ char* name; @@ -97,6 +100,9 @@ struct ClientLookupHandle /* request type */ enum GNUNET_GNS_RecordType type; + /* optional zone private key used for lookup */ + struct GNUNET_CRYPTO_RsaPrivateKey *zone_key; + /* the name to look up */ char* name; //Needed? }; @@ -416,6 +422,43 @@ update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) NULL); } +/** + * Lookup the private key for the zone + * + * @param zone the zone we want a private key for + * @return NULL of not found else the key + */ +struct GNUNET_CRYPTO_RsaPrivateKey* +lookup_private_key(struct GNUNET_CRYPTO_ShortHashCode *zone) +{ + char* keydir; + struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename; + char* location; + struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL; + + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (GNS_cfg, + "namestore", + "ZONEFILE_DIRECTORY", &keydir)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No zonefile directory!\n"); + return NULL; + } + + GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename); + + GNUNET_asprintf(&location, "%s%s%s.zkey", keydir, + DIR_SEPARATOR_STR, zonename); + + if (GNUNET_YES == GNUNET_DISK_file_test (location)) + key = GNUNET_CRYPTO_rsa_key_create_from_file (location); + + GNUNET_free(location); + GNUNET_free(keydir); + + return key; + +} /* END DHT ZONE PROPAGATION */ @@ -456,6 +499,7 @@ send_shorten_response(void* cls, const char* name) GNUNET_free(rmsg); GNUNET_free_non_null(csh->name); + GNUNET_free_non_null(csh->zone_key); GNUNET_free(csh); } @@ -477,6 +521,8 @@ static void handle_shorten(void *cls, struct ClientShortenHandle *csh; char name[MAX_DNS_NAME_LENGTH]; char* nameptr = name; + struct GNUNET_CRYPTO_ShortHashCode zone; + struct GNUNET_CRYPTO_RsaPrivateKey *key; if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage)) { @@ -501,6 +547,7 @@ static void handle_shorten(void *cls, csh = GNUNET_malloc(sizeof(struct ClientShortenHandle)); csh->client = client; csh->unique_id = sh_msg->id; + csh->zone_key = NULL; GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr); @@ -531,12 +578,26 @@ static void handle_shorten(void *cls, GNUNET_SERVER_notification_context_add (nc, client); + if (1 == ntohl(sh_msg->use_default_zone)) + zone = zone_hash; //Default zone + else + zone = sh_msg->zone; + /* Start shortening */ if (GNUNET_YES == auto_import_pkey) - gns_resolver_shorten_name(zone_hash, name, zone_key, + { + if (1 == ntohl(sh_msg->use_default_zone)) + key = zone_key; + else + { + key = lookup_private_key(&sh_msg->zone); + csh->zone_key = key; + } + gns_resolver_shorten_name(zone, name, key, &send_shorten_response, csh); + } else - gns_resolver_shorten_name(zone_hash, name, NULL, + gns_resolver_shorten_name(zone, name, NULL, &send_shorten_response, csh); } @@ -682,6 +743,7 @@ static void handle_get_authority(void *cls, } + /** * Reply to client with the result from our lookup. * @@ -719,6 +781,10 @@ send_lookup_response(void* cls, GNUNET_free(rmsg); GNUNET_free(clh->name); + + if (NULL != clh->zone_key) + GNUNET_free(clh->zone_key); + GNUNET_free(clh); } @@ -743,6 +809,8 @@ handle_lookup(void *cls, char name[MAX_DNS_NAME_LENGTH]; struct ClientLookupHandle *clh; char* nameptr = name; + struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL; + struct GNUNET_CRYPTO_ShortHashCode zone; if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage)) { @@ -773,6 +841,7 @@ handle_lookup(void *cls, strcpy(clh->name, name); clh->unique_id = sh_msg->id; clh->type = ntohl(sh_msg->type); + clh->zone_key = NULL; if (strlen (name) > MAX_DNS_NAME_LENGTH) { GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, @@ -781,17 +850,30 @@ handle_lookup(void *cls, send_lookup_response(clh, 0, NULL); return; } + + if (1 == ntohl(sh_msg->use_default_zone)) + zone = zone_hash; //Default zone + else + zone = sh_msg->zone; if (GNUNET_YES == auto_import_pkey) { - gns_resolver_lookup_record(zone_hash, clh->type, name, - zone_key, + if (1 == ntohl(sh_msg->use_default_zone)) + key = zone_key; + else + { + key = lookup_private_key(&sh_msg->zone); + clh->zone_key = key; + } + + gns_resolver_lookup_record(zone, clh->type, name, + key, default_lookup_timeout, &send_lookup_response, clh); } else { - gns_resolver_lookup_record(zone_hash, clh->type, name, + gns_resolver_lookup_record(zone, clh->type, name, NULL, default_lookup_timeout, &send_lookup_response, clh); @@ -826,6 +908,8 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0} }; + GNS_cfg = c; + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns", "ZONEKEY", &keyfile)) { diff --git a/src/include/gnunet_gns_service.h b/src/include/gnunet_gns_service.h index 5528532cd..4e040f777 100644 --- a/src/include/gnunet_gns_service.h +++ b/src/include/gnunet_gns_service.h @@ -120,7 +120,8 @@ typedef void (*GNUNET_GNS_LookupResultProcessor) (void *cls, /** - * Perform an asynchronous lookup operation on the GNS. + * Perform an asynchronous lookup operation on the GNS + * in the default zone. * * @param handle handle to the GNS service * @param name the name to look up @@ -137,6 +138,26 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, GNUNET_GNS_LookupResultProcessor proc, void *proc_cls); +/** + * Perform an asynchronous lookup operation on the GNS + * in the zone specified by 'zone'. + * + * @param handle handle to the GNS service + * @param name the name to look up + * @param zone the zone to start the resolution in + * @param type the GNUNET_GNS_RecordType to look for + * @param proc function to call on result + * @param proc_cls closure for processor + * + * @return handle to the queued request + */ +struct GNUNET_GNS_QueueEntry * +GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle, + const char * name, + struct GNUNET_CRYPTO_ShortHashCode *zone, + enum GNUNET_GNS_RecordType type, + GNUNET_GNS_LookupResultProcessor proc, + void *proc_cls); /* *************** Standard API: shorten ******************* */ @@ -168,6 +189,23 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle, void *proc_cls); +/** + * Perform a name shortening operation on the GNS. + * + * @param handle handle to the GNS service + * @param name the name to look up + * @param zone the zone to start the resolution in + * @param proc function to call on result + * @param proc_cls closure for processor + * @return handle to the operation + */ +struct GNUNET_GNS_QueueEntry * +GNUNET_GNS_shorten_zone (struct GNUNET_GNS_Handle *handle, + const char * name, + struct GNUNET_CRYPTO_ShortHashCode *zone, + GNUNET_GNS_ShortenResultProcessor proc, + void *proc_cls); + /* *************** Standard API: get authority ******************* */ -- 2.25.1