2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @file gns/gnunet-service-gns.c
24 * @brief GNUnet GNS service
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_gns_service.h"
35 #include "block_gns.h"
37 #include "gnunet-service-gns_resolver.h"
38 #include "gnunet-service-gns_interceptor.h"
40 /* FIXME move to proper header in include */
41 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
42 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
43 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
44 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
45 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
46 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
50 * Handle to a shorten operation from api
52 struct ClientShortenHandle
54 /* the requesting client that */
55 struct GNUNET_SERVER_Client *client;
61 enum GNUNET_GNS_RecordType type;
63 /* optional zone private key used for shorten */
64 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
67 char name[MAX_DNS_NAME_LENGTH];
70 struct GNUNET_CRYPTO_ShortHashCode root_zone;
73 struct GNUNET_CRYPTO_ShortHashCode private_zone;
76 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
81 * Handle to a get auhtority operation from api
83 struct ClientGetAuthHandle
85 /* the requesting client that */
86 struct GNUNET_SERVER_Client *client;
91 /* name to lookup authority */
98 * Handle to a lookup operation from api
100 struct ClientLookupHandle
102 /* the requesting client that */
103 struct GNUNET_SERVER_Client *client;
105 /* The zone we look up in */
106 struct GNUNET_CRYPTO_ShortHashCode zone;
108 /* Do we only want to lookup from local cache? */
115 enum GNUNET_GNS_RecordType type;
117 /* optional zone private key used for shorten */
118 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
120 /* the name to look up */
121 char* name; //Needed?
125 * Our handle to the DHT
127 static struct GNUNET_DHT_Handle *dht_handle;
130 * Our zone's private key
132 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
135 * Our handle to the namestore service
136 * FIXME maybe need a second handle for iteration
138 struct GNUNET_NAMESTORE_Handle *namestore_handle;
141 * Handle to iterate over our authoritative zone in namestore
143 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
146 * The configuration the GNS service is running with
148 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
151 * Our notification context.
153 static struct GNUNET_SERVER_NotificationContext *nc;
158 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
161 * Useful for zone update for DHT put
163 static int num_public_records;
166 * update interval in seconds
168 static unsigned long long max_record_put_interval;
170 static unsigned long long dht_max_update_interval;
172 /* dht update interval FIXME define? */
173 static struct GNUNET_TIME_Relative record_put_interval;
175 /* zone update task */
176 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
178 /* automatic pkey import for name shortening */
179 static int auto_import_pkey;
182 static struct GNUNET_TIME_Relative default_lookup_timeout;
184 /* name of the private zone */
185 static char *private_zone_id;
187 /* name of the public zone */
188 static char *shorten_zone_id;
191 static int v6_enabled;
194 static int v4_enabled;
200 on_resolver_cleanup(void)
202 if (NULL != namestore_iter)
203 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
204 GNUNET_NAMESTORE_disconnect(namestore_handle);
205 GNUNET_DHT_disconnect(dht_handle);
209 * Task run during shutdown.
215 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
222 GNUNET_SERVER_notification_context_destroy (nc);
224 gns_interceptor_stop();
225 gns_resolver_cleanup(&on_resolver_cleanup);
226 /* Kill zone task for it may make the scheduler hang */
227 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
228 GNUNET_SCHEDULER_cancel(zone_update_taskid);
233 * Method called periodicattluy that triggers
234 * iteration over root zone
237 * @param tc task context
240 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
243 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
247 * Continuation for DHT put
250 * @param success GNUNET_OK if the PUT was transmitted,
251 * GNUNET_NO on timeout,
252 * GNUNET_SYSERR on disconnect from service
253 * after the PUT message was transmitted
254 * (so we don't know if it was received or not)
257 record_dht_put(void *cls, int success)
259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
264 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
267 * Function used to put all records successively into the DHT.
269 * @param cls the closure (NULL)
270 * @param key the public key of the authority (ours)
271 * @param expiration lifetime of the namestore entry
272 * @param name the name of the records
273 * @param rd_count the number of records in data
274 * @param rd the record data
275 * @param signature the signature for the record data
278 put_gns_record(void *cls,
279 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
280 struct GNUNET_TIME_Absolute expiration,
282 unsigned int rd_count,
283 const struct GNUNET_NAMESTORE_RecordData *rd,
284 const struct GNUNET_CRYPTO_RsaSignature *signature)
287 struct GNSNameRecordBlock *nrb;
288 struct GNUNET_CRYPTO_ShortHashCode name_hash;
289 struct GNUNET_CRYPTO_ShortHashCode zhash;
290 struct GNUNET_HashCode xor_hash;
291 struct GNUNET_HashCode name_hash_double;
292 struct GNUNET_HashCode zone_hash_double;
293 uint32_t rd_payload_length;
294 char* nrb_data = NULL;
300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
301 "Zone iteration finished. Rescheduling put in %ds\n",
302 dht_max_update_interval);
303 namestore_iter = NULL;
304 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
305 GNUNET_TIME_relative_multiply(
306 GNUNET_TIME_UNIT_SECONDS,
307 dht_max_update_interval
309 &update_zone_dht_start,
314 namelen = strlen(name) + 1;
316 if (signature == NULL)
318 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
319 "No signature for %s record data provided! Skipping...\n",
321 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
328 "Putting records for %s into the DHT\n", name);
330 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
332 nrb = GNUNET_malloc(rd_payload_length + namelen
333 + sizeof(struct GNSNameRecordBlock));
335 nrb->signature = *signature;
337 nrb->public_key = *key;
339 nrb->rd_count = htonl(rd_count);
341 memcpy(&nrb[1], name, namelen);
343 nrb_data = (char*)&nrb[1];
346 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
348 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
353 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
354 "Record serialization failed! Skipping...\n");
356 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
363 * calculate DHT key: H(name) xor H(pubkey)
365 GNUNET_CRYPTO_short_hash(key,
366 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
368 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
369 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
370 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
371 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
373 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
374 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
377 "putting records for %s under key: %s with size %d\n",
378 name, GNUNET_h2s (&xor_hash), rd_payload_length);
380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
381 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
382 /* FIXME: keep return value to possibly cancel? */
383 GNUNET_DHT_put (dht_handle, &xor_hash,
384 DHT_GNS_REPLICATION_LEVEL,
386 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
390 DHT_OPERATION_TIMEOUT,
392 NULL); //cls for cont
394 num_public_records++;
397 * Reschedule periodic put
399 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
400 &update_zone_dht_next,
408 * Periodically iterate over our zone and store everything in dht
411 * @param tc task context
414 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
416 unsigned long long interval = 0;
418 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
421 if (0 == num_public_records)
424 * If no records are known (startup) or none present
425 * we can safely set the interval to 1s
427 record_put_interval = GNUNET_TIME_relative_multiply(
428 GNUNET_TIME_UNIT_SECONDS,
430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
431 "No records in db. Adjusted record put interval to 1s\n");
435 interval = max_record_put_interval/num_public_records;
438 record_put_interval = GNUNET_TIME_relative_multiply(
439 GNUNET_TIME_UNIT_SECONDS,
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
442 "Adjusted DHT update interval to %ds!\n",
446 /* start counting again */
447 num_public_records = 0;
448 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
450 GNUNET_NAMESTORE_RF_AUTHORITY,
451 GNUNET_NAMESTORE_RF_PRIVATE,
456 /* END DHT ZONE PROPAGATION */
459 * Send shorten response back to client
461 * @param cls the closure containing a client shorten handle
462 * @param name the shortened name result or NULL if cannot be shortened
465 send_shorten_response(void* cls, const char* name)
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
468 "SHORTEN_RESULT", name);
469 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
470 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
477 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
480 rmsg->id = csh->unique_id;
481 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
483 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
486 strcpy((char*)&rmsg[1], name);
488 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
489 (const struct GNUNET_MessageHeader *) rmsg,
491 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
494 GNUNET_free_non_null(csh->shorten_key);
501 process_shorten_zone_shorten (void *cls,
502 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
503 struct GNUNET_TIME_Absolute expiration,
505 unsigned int rd_count,
506 const struct GNUNET_NAMESTORE_RecordData *rd,
507 const struct GNUNET_CRYPTO_RsaSignature *signature)
509 struct ClientShortenHandle *csh = cls;
510 struct GNUNET_TIME_Relative remaining_time;
511 struct GNUNET_TIME_Absolute et;
513 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
515 if ((rd_count == 1) &&
516 (remaining_time.rel_value != 0))
518 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
519 et.abs_value = rd->expiration_time;
520 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
521 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
522 (remaining_time.rel_value != 0))
524 csh->shorten_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
527 gns_resolver_shorten_name (&csh->root_zone,
533 &send_shorten_response, csh);
539 process_private_zone_shorten (void *cls,
540 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
541 struct GNUNET_TIME_Absolute expiration,
543 unsigned int rd_count,
544 const struct GNUNET_NAMESTORE_RecordData *rd,
545 const struct GNUNET_CRYPTO_RsaSignature *signature)
547 struct GNUNET_TIME_Relative remaining_time;
548 struct ClientShortenHandle *csh = cls;
549 struct GNUNET_TIME_Absolute et;
551 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
553 if ((rd_count == 1) &&
554 (remaining_time.rel_value != 0))
556 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
557 et.abs_value = rd->expiration_time;
558 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
559 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
560 (remaining_time.rel_value != 0))
564 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
565 "Looking for shorten zone in private zone\n");
566 csh->private_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
567 GNUNET_NAMESTORE_lookup_record (namestore_handle,
570 GNUNET_GNS_RECORD_ANY,
571 &process_shorten_zone_shorten,
577 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
578 "No private zone found!\n");
579 gns_resolver_shorten_name (&csh->root_zone,
585 &send_shorten_response, csh);
590 * Lookup the zone infos and shorten name
592 * @param csh the shorten handle
596 start_shorten_name (struct ClientShortenHandle *csh)
598 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
599 "Looking for private zone in root zone\n");
601 GNUNET_NAMESTORE_lookup_record (namestore_handle,
604 GNUNET_GNS_RECORD_ANY,
605 &process_private_zone_shorten,
611 * Handle a shorten message from the api
613 * @param cls the closure
614 * @param client the client
615 * @param message the message
617 static void handle_shorten (void *cls,
618 struct GNUNET_SERVER_Client * client,
619 const struct GNUNET_MessageHeader * message)
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
624 struct ClientShortenHandle *csh;
625 char name[MAX_DNS_NAME_LENGTH];
626 char* nameptr = name;
628 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
636 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
637 (struct GNUNET_GNS_ClientShortenMessage *) message;
639 msg_size = ntohs(message->size);
641 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
644 GNUNET_SERVER_receive_done (client, GNUNET_OK);
648 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
649 csh->client = client;
650 csh->unique_id = sh_msg->id;
651 csh->shorten_key = NULL;
653 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
655 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
656 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
657 "SHORTEN: %s is too short", name);
658 send_shorten_response(csh, name);
662 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
664 "SHORTEN: %s is too long", name);
665 send_shorten_response(csh, name);
669 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "%s is not our domain. Returning\n", name);
673 send_shorten_response(csh, name);
677 strcpy (csh->name, name);
679 GNUNET_SERVER_notification_context_add (nc, client);
681 if (1 == ntohl(sh_msg->use_default_zone))
682 csh->root_zone = zone_hash; //Default zone
684 csh->root_zone = sh_msg->zone;
686 start_shorten_name (csh);
692 * Send get authority response back to client
694 * @param cls the closure containing a client get auth handle
695 * @param name the shortened name result or NULL if cannot be shortened
698 send_get_auth_response(void *cls, const char* name)
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
701 "GET_AUTH_RESULT", name);
702 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
703 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
710 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
713 rmsg->id = cah->unique_id;
714 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
716 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
719 strcpy((char*)&rmsg[1], name);
721 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
722 (const struct GNUNET_MessageHeader *) rmsg,
724 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
729 GNUNET_free_non_null(cah->name);
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
738 * Handle a get authority message from the api
740 * @param cls the closure
741 * @param client the client
742 * @param message the message
744 static void handle_get_authority(void *cls,
745 struct GNUNET_SERVER_Client * client,
746 const struct GNUNET_MessageHeader * message)
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
751 struct ClientGetAuthHandle *cah;
752 char name[MAX_DNS_NAME_LENGTH];
753 char* nameptr = name;
756 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
759 GNUNET_SERVER_receive_done (client, GNUNET_OK);
763 GNUNET_SERVER_notification_context_add (nc, client);
765 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
766 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
768 msg_size = ntohs(message->size);
770 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
777 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
780 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
781 cah->client = client;
782 cah->unique_id = sh_msg->id;
784 if (strlen(name) < strlen(GNUNET_GNS_TLD))
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "GET_AUTH: %s is too short. Returning\n", name);
789 send_get_auth_response(cah, name);
793 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
795 "GET_AUTH: %s is too long", name);
797 send_get_auth_response(cah, name);
801 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
802 GNUNET_GNS_TLD) != 0)
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "GET_AUTH: %s is not our domain. Returning\n", name);
807 send_get_auth_response(cah, name);
811 if (strcmp(name, GNUNET_GNS_TLD) == 0)
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "GET_AUTH: %s is us. Returning\n", name);
816 send_get_auth_response(cah, name);
820 cah->name = GNUNET_malloc(strlen(name)
821 - strlen(GNUNET_GNS_TLD) + 1);
823 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
824 memcpy(cah->name, name,
825 strlen(name)-strlen(GNUNET_GNS_TLD));
827 /* Start delegation resolution in our namestore */
828 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
834 * Reply to client with the result from our lookup.
836 * @param cls the closure (our client lookup handle)
837 * @param rd_count the number of records
838 * @param rd the record data
841 send_lookup_response(void* cls,
843 const struct GNUNET_NAMESTORE_RecordData *rd)
845 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
846 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
850 "LOOKUP_RESULT", rd_count);
852 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
853 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
855 rmsg->id = clh->unique_id;
856 rmsg->rd_count = htonl(rd_count);
857 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
859 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
861 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
863 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
864 (const struct GNUNET_MessageHeader *) rmsg,
866 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
869 GNUNET_free(clh->name);
871 if (NULL != clh->shorten_key)
872 GNUNET_free(clh->shorten_key);
880 * Handle lookup requests from client
882 * @param cls the closure
883 * @param client the client
884 * @param message the message
887 handle_lookup(void *cls,
888 struct GNUNET_SERVER_Client * client,
889 const struct GNUNET_MessageHeader * message)
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
895 char name[MAX_DNS_NAME_LENGTH];
896 struct ClientLookupHandle *clh;
897 char* nameptr = name;
899 struct GNUNET_CRYPTO_RsaPrivateKey *key;
900 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
903 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
906 GNUNET_SERVER_receive_done (client, GNUNET_OK);
910 GNUNET_SERVER_notification_context_add (nc, client);
912 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
913 (struct GNUNET_GNS_ClientLookupMessage *) message;
915 msg_size = ntohs(message->size);
917 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
920 GNUNET_SERVER_receive_done (client, GNUNET_OK);
924 if (1 == ntohl(sh_msg->have_key))
926 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
927 tmp_pkey = (char*)&sh_msg[1];
928 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
929 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
934 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
937 namelen = strlen(name)+1;
938 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
939 memset (clh, 0, sizeof (struct ClientLookupHandle));
940 clh->client = client;
941 clh->name = GNUNET_malloc(namelen);
942 strcpy(clh->name, name);
943 clh->unique_id = sh_msg->id;
944 clh->type = ntohl(sh_msg->type);
945 clh->shorten_key = key;
947 only_cached = ntohl(sh_msg->only_cached);
949 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
951 "LOOKUP: %s is too long", name);
953 send_lookup_response(clh, 0, NULL);
957 if ((clh->type == GNUNET_GNS_RECORD_A) &&
958 (GNUNET_OK != v4_enabled))
960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
961 "LOOKUP: Query for A record but AF_INET not supported!");
963 send_lookup_response(clh, 0, NULL);
967 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
968 (GNUNET_OK != v6_enabled))
970 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
971 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
973 send_lookup_response(clh, 0, NULL);
977 if (1 == ntohl(sh_msg->use_default_zone))
978 clh->zone = zone_hash; //Default zone
980 clh->zone = sh_msg->zone;
982 if (GNUNET_YES == auto_import_pkey)
984 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
986 default_lookup_timeout,
988 &send_lookup_response, clh);
992 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
994 default_lookup_timeout,
996 &send_lookup_response, clh);
1001 * Test if the given AF is supported by this system.
1004 * @return GNUNET_OK if the AF is supported
1011 s = socket (af, SOCK_STREAM, 0);
1014 if (EAFNOSUPPORT == errno)
1016 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1017 return GNUNET_SYSERR;
1024 * Process GNS requests.
1026 * @param cls closure)
1027 * @param server the initialized server
1028 * @param c configuration to use
1031 run (void *cls, struct GNUNET_SERVER_Handle *server,
1032 const struct GNUNET_CONFIGURATION_Handle *c)
1035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1038 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1039 unsigned long long max_parallel_bg_queries = 0;
1040 unsigned long long default_lookup_timeout_secs = 0;
1041 int ignore_pending = GNUNET_NO;
1043 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1044 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1045 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1046 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1051 v6_enabled = test_af (AF_INET6);
1052 v4_enabled = test_af (AF_INET);
1054 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1055 "ZONEKEY", &keyfile))
1057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1058 "No private key for root zone specified!\n");
1059 GNUNET_SCHEDULER_shutdown ();
1063 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1064 "Using keyfile %s for root zone.\n", keyfile);
1066 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1067 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1069 GNUNET_CRYPTO_short_hash(&pkey,
1070 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1072 GNUNET_free(keyfile);
1075 * handle to our local namestore
1077 namestore_handle = GNUNET_NAMESTORE_connect(c);
1079 if (NULL == namestore_handle)
1081 //FIXME do error handling;
1082 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1083 "Failed to connect to the namestore!\n");
1084 GNUNET_SCHEDULER_shutdown ();
1090 auto_import_pkey = GNUNET_NO;
1093 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1094 "AUTO_IMPORT_PKEY"))
1096 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1097 "Automatic PKEY import is enabled.\n");
1098 auto_import_pkey = GNUNET_YES;
1102 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1106 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1107 "Private zone id: %s\n", private_zone_id);
1110 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1114 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1115 "Shorten zone id: %s\n", shorten_zone_id);
1118 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1121 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1122 "ZONE_PUT_INTERVAL",
1123 &dht_max_update_interval))
1125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1126 "DHT zone update interval: %llu\n",
1127 dht_max_update_interval);
1130 max_record_put_interval = 1;
1133 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1134 "RECORD_PUT_INTERVAL",
1135 &max_record_put_interval))
1137 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1138 "Record put interval: %llu\n",
1139 max_record_put_interval);
1143 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1144 "MAX_PARALLEL_BACKGROUND_QUERIES",
1145 &max_parallel_bg_queries))
1147 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1148 "Number of allowed parallel background queries: %llu\n",
1149 max_parallel_bg_queries);
1153 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1154 "AUTO_IMPORT_CONFIRMATION_REQ"))
1156 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1157 "Auto import requires user confirmation\n");
1158 ignore_pending = GNUNET_YES;
1162 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1163 "DEFAULT_LOOKUP_TIMEOUT",
1164 &default_lookup_timeout_secs))
1166 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1167 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1168 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1169 GNUNET_TIME_UNIT_SECONDS,
1170 default_lookup_timeout_secs);
1176 dht_handle = GNUNET_DHT_connect(c,
1177 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1180 if (NULL == dht_handle)
1182 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1185 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1186 max_parallel_bg_queries,
1190 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1191 "Unable to initialize resolver!\n");
1192 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1197 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1199 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1200 "DNS hijacking enabled... connecting to service.\n");
1202 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1204 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1205 "Failed to enable the dns interceptor!\n");
1210 * Schedule periodic put
1212 * We have roughly an hour for all records;
1214 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1216 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1218 GNUNET_SERVER_add_handlers (server, handlers);
1221 //GNUNET_SERVER_disconnect_notify (server,
1222 // &client_disconnect_notification,
1225 nc = GNUNET_SERVER_notification_context_create (server, 1);
1227 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1233 * The main function for the GNS service.
1235 * @param argc number of arguments from the command line
1236 * @param argv command line arguments
1237 * @return 0 ok, 1 on error
1240 main (int argc, char *const *argv)
1246 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1251 /* end of gnunet-service-gns.c */