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
56 struct ClientShortenHandle *next;
59 struct ClientShortenHandle *prev;
61 /* the requesting client that */
62 struct GNUNET_SERVER_Client *client;
68 enum GNUNET_GNS_RecordType type;
71 char name[MAX_DNS_NAME_LENGTH];
73 /* name of private zone (relative to root) */
74 char private_zone_id[MAX_DNS_NAME_LENGTH];
76 /* name of shorten zone (relative to root) */
77 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
80 struct GNUNET_CRYPTO_ShortHashCode root_zone;
83 struct GNUNET_CRYPTO_ShortHashCode private_zone;
86 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
88 /* Namestore lookup task */
89 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
95 * Handle to a get auhtority operation from api
97 struct ClientGetAuthHandle
99 /* the requesting client that */
100 struct GNUNET_SERVER_Client *client;
105 /* name to lookup authority */
112 * Handle to a lookup operation from api
114 struct ClientLookupHandle
117 /* the requesting client that */
118 struct GNUNET_SERVER_Client *client;
120 /* The zone we look up in */
121 struct GNUNET_CRYPTO_ShortHashCode zone;
123 /* Do we only want to lookup from local cache? */
130 enum GNUNET_GNS_RecordType type;
132 /* optional zone private key used for shorten */
133 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
135 /* the name to look up */
136 char* name; //Needed?
140 * Our handle to the DHT
142 static struct GNUNET_DHT_Handle *dht_handle;
145 * Our zone's private key
147 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
150 * Our handle to the namestore service
151 * FIXME maybe need a second handle for iteration
153 struct GNUNET_NAMESTORE_Handle *namestore_handle;
156 * Handle to iterate over our authoritative zone in namestore
158 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
161 * The configuration the GNS service is running with
163 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
166 * Our notification context.
168 static struct GNUNET_SERVER_NotificationContext *nc;
173 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
176 * Useful for zone update for DHT put
178 static int num_public_records;
181 * update interval in seconds
183 static unsigned long long max_record_put_interval;
185 static unsigned long long dht_max_update_interval;
187 /* dht update interval FIXME define? */
188 static struct GNUNET_TIME_Relative record_put_interval;
190 /* zone update task */
191 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
193 /* automatic pkey import for name shortening */
194 static int auto_import_pkey;
197 static struct GNUNET_TIME_Relative default_lookup_timeout;
200 static int v6_enabled;
203 static int v4_enabled;
205 /* Shorten DLL for cancelling NS requests */
206 static struct ClientShortenHandle *csh_head;
208 /* Shorten DLL for cancelling NS requests */
209 static struct ClientShortenHandle *csh_tail;
212 * Send shorten response back to client
214 * @param cls the closure containing a client shorten handle
215 * @param name the shortened name result or NULL if cannot be shortened
218 send_shorten_response(void* cls, const char* name);
225 on_resolver_cleanup(void)
227 if (NULL != namestore_iter)
228 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
229 GNUNET_NAMESTORE_disconnect(namestore_handle);
230 GNUNET_DHT_disconnect(dht_handle);
234 * Task run during shutdown.
240 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 struct ClientShortenHandle *csh_tmp = csh_head;
244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
247 while (csh_tmp != NULL)
249 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
250 send_shorten_response (csh_tmp, csh_tmp->name);
253 GNUNET_SERVER_notification_context_destroy (nc);
255 gns_interceptor_stop();
256 gns_resolver_cleanup(&on_resolver_cleanup);
257 /* Kill zone task for it may make the scheduler hang */
258 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
259 GNUNET_SCHEDULER_cancel(zone_update_taskid);
264 * Method called periodicattluy that triggers
265 * iteration over root zone
268 * @param tc task context
271 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
273 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
274 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
278 * Continuation for DHT put
281 * @param success GNUNET_OK if the PUT was transmitted,
282 * GNUNET_NO on timeout,
283 * GNUNET_SYSERR on disconnect from service
284 * after the PUT message was transmitted
285 * (so we don't know if it was received or not)
288 record_dht_put(void *cls, int success)
290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
295 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
298 * Function used to put all records successively into the DHT.
300 * @param cls the closure (NULL)
301 * @param key the public key of the authority (ours)
302 * @param expiration lifetime of the namestore entry
303 * @param name the name of the records
304 * @param rd_count the number of records in data
305 * @param rd the record data
306 * @param signature the signature for the record data
309 put_gns_record(void *cls,
310 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
311 struct GNUNET_TIME_Absolute expiration,
313 unsigned int rd_count,
314 const struct GNUNET_NAMESTORE_RecordData *rd,
315 const struct GNUNET_CRYPTO_RsaSignature *signature)
318 struct GNSNameRecordBlock *nrb;
319 struct GNUNET_CRYPTO_ShortHashCode name_hash;
320 struct GNUNET_CRYPTO_ShortHashCode zhash;
321 struct GNUNET_HashCode xor_hash;
322 struct GNUNET_HashCode name_hash_double;
323 struct GNUNET_HashCode zone_hash_double;
324 uint32_t rd_payload_length;
325 char* nrb_data = NULL;
331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
332 "Zone iteration finished. Rescheduling put in %ds\n",
333 dht_max_update_interval);
334 namestore_iter = NULL;
335 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
336 GNUNET_TIME_relative_multiply(
337 GNUNET_TIME_UNIT_SECONDS,
338 dht_max_update_interval
340 &update_zone_dht_start,
345 namelen = strlen(name) + 1;
347 if (signature == NULL)
349 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
350 "No signature for %s record data provided! Skipping...\n",
352 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
359 "Putting records for %s into the DHT\n", name);
361 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
363 nrb = GNUNET_malloc(rd_payload_length + namelen
364 + sizeof(struct GNSNameRecordBlock));
366 nrb->signature = *signature;
368 nrb->public_key = *key;
370 nrb->rd_count = htonl(rd_count);
372 memcpy(&nrb[1], name, namelen);
374 nrb_data = (char*)&nrb[1];
377 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
379 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
385 "Record serialization failed! Skipping...\n");
387 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
394 * calculate DHT key: H(name) xor H(pubkey)
396 GNUNET_CRYPTO_short_hash(key,
397 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
399 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
400 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
401 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
402 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
405 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
408 "putting records for %s under key: %s with size %d\n",
409 name, GNUNET_h2s (&xor_hash), rd_payload_length);
411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
412 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
413 /* FIXME: keep return value to possibly cancel? */
414 GNUNET_DHT_put (dht_handle, &xor_hash,
415 DHT_GNS_REPLICATION_LEVEL,
417 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
421 DHT_OPERATION_TIMEOUT,
423 NULL); //cls for cont
425 num_public_records++;
428 * Reschedule periodic put
430 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
431 &update_zone_dht_next,
439 * Periodically iterate over our zone and store everything in dht
442 * @param tc task context
445 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
447 unsigned long long interval = 0;
449 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
451 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
452 if (0 == num_public_records)
455 * If no records are known (startup) or none present
456 * we can safely set the interval to 1s
458 record_put_interval = GNUNET_TIME_relative_multiply(
459 GNUNET_TIME_UNIT_SECONDS,
461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
462 "No records in db. Adjusted record put interval to 1s\n");
466 interval = max_record_put_interval/num_public_records;
469 record_put_interval = GNUNET_TIME_relative_multiply(
470 GNUNET_TIME_UNIT_SECONDS,
472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
473 "Adjusted DHT update interval to %ds!\n",
477 /* start counting again */
478 num_public_records = 0;
479 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
481 GNUNET_NAMESTORE_RF_AUTHORITY,
482 GNUNET_NAMESTORE_RF_PRIVATE,
487 /* END DHT ZONE PROPAGATION */
490 * Send shorten response back to client
492 * @param cls the closure containing a client shorten handle
493 * @param name the shortened name result or NULL if cannot be shortened
496 send_shorten_response(void* cls, const char* name)
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
499 "SHORTEN_RESULT", name);
500 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
501 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
508 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
511 rmsg->id = csh->unique_id;
512 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
514 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
517 strcpy((char*)&rmsg[1], name);
519 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
520 (const struct GNUNET_MessageHeader *) rmsg,
522 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
524 if (NULL != csh->namestore_task)
525 GNUNET_NAMESTORE_cancel (csh->namestore_task);
534 * Lookup the zone infos and shorten name
536 * @param csh the shorten handle
540 process_shorten_in_private_zone_lookup (void *cls,
541 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
542 struct GNUNET_TIME_Absolute expiration,
544 unsigned int rd_count,
545 const struct GNUNET_NAMESTORE_RecordData *rd,
546 const struct GNUNET_CRYPTO_RsaSignature *signature)
548 struct ClientShortenHandle *csh = cls;
549 csh->namestore_task = NULL;
550 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
551 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
553 if (0 == strcmp (csh->private_zone_id, ""))
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "No shorten zone in private zone!\n");
561 strcpy (csh->shorten_zone_id, "");
568 GNUNET_assert (rd_count == 1);
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "Shorten zone %s found in private zone %s\n",
572 name, csh->private_zone_id);
574 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
577 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
579 gns_resolver_shorten_name (&csh->root_zone,
583 csh->private_zone_id,
584 csh->shorten_zone_id,
585 &send_shorten_response, csh);
591 * Lookup the zone infos and shorten name
593 * @param csh the shorten handle
597 process_shorten_in_root_zone_lookup (void *cls,
598 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
599 struct GNUNET_TIME_Absolute expiration,
601 unsigned int rd_count,
602 const struct GNUNET_NAMESTORE_RecordData *rd,
603 const struct GNUNET_CRYPTO_RsaSignature *signature)
605 struct ClientShortenHandle *csh = cls;
606 csh->namestore_task = NULL;
607 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
608 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
610 if (0 == strcmp (csh->private_zone_id, ""))
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "No shorten zone in zone and no private zone!\n");
618 strcpy (csh->shorten_zone_id, "");
620 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
623 gns_resolver_shorten_name (&csh->root_zone,
627 csh->private_zone_id,
628 csh->shorten_zone_id,
629 &send_shorten_response, csh);
633 GNUNET_assert (rd_count == 1);
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Private zone %s found in root zone\n", name);
638 strcpy (csh->private_zone_id, name);
640 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
643 &process_shorten_in_private_zone_lookup,
649 * Lookup the zone infos and shorten name
651 * @param csh the shorten handle
655 process_private_in_root_zone_lookup (void *cls,
656 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
657 struct GNUNET_TIME_Absolute expiration,
659 unsigned int rd_count,
660 const struct GNUNET_NAMESTORE_RecordData *rd,
661 const struct GNUNET_CRYPTO_RsaSignature *signature)
663 struct ClientShortenHandle *csh = cls;
664 csh->namestore_task = NULL;
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "No private zone in root zone\n");
671 strcpy (csh->private_zone_id, "");
673 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
676 &process_shorten_in_root_zone_lookup,
681 GNUNET_assert (rd_count == 1);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Private zone %s found in root zone\n", name);
686 strcpy (csh->private_zone_id, name);
688 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
691 &process_shorten_in_private_zone_lookup,
696 * Lookup the zone infos and shorten name
698 * @param csh the shorten handle
702 start_shorten_name (struct ClientShortenHandle *csh)
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
705 "Looking for private zone name in root zone\n");
707 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
710 &process_private_in_root_zone_lookup,
716 * Handle a shorten message from the api
718 * @param cls the closure
719 * @param client the client
720 * @param message the message
722 static void handle_shorten (void *cls,
723 struct GNUNET_SERVER_Client * client,
724 const struct GNUNET_MessageHeader * message)
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
729 struct ClientShortenHandle *csh;
730 char name[MAX_DNS_NAME_LENGTH];
731 char* nameptr = name;
733 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
736 GNUNET_SERVER_receive_done (client, GNUNET_OK);
741 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
742 (struct GNUNET_GNS_ClientShortenMessage *) message;
744 msg_size = ntohs(message->size);
746 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
753 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
754 csh->client = client;
755 csh->unique_id = sh_msg->id;
757 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
759 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
761 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
763 "SHORTEN: %s is too short", name);
764 send_shorten_response(csh, name);
768 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
769 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
770 "SHORTEN: %s is too long", name);
771 send_shorten_response(csh, name);
775 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "%s is not our domain. Returning\n", name);
779 send_shorten_response(csh, name);
783 csh->shorten_zone = sh_msg->shorten_zone;
784 csh->private_zone = sh_msg->private_zone;
786 strcpy (csh->name, name);
788 GNUNET_SERVER_notification_context_add (nc, client);
790 if (1 == ntohl(sh_msg->use_default_zone))
791 csh->root_zone = zone_hash; //Default zone
793 csh->root_zone = sh_msg->zone;
795 start_shorten_name (csh);
801 * Send get authority response back to client
803 * @param cls the closure containing a client get auth handle
804 * @param name the shortened name result or NULL if cannot be shortened
807 send_get_auth_response(void *cls, const char* name)
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
810 "GET_AUTH_RESULT", name);
811 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
812 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
819 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
822 rmsg->id = cah->unique_id;
823 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
825 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
828 strcpy((char*)&rmsg[1], name);
830 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
831 (const struct GNUNET_MessageHeader *) rmsg,
833 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
838 GNUNET_free_non_null(cah->name);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
847 * Handle a get authority message from the api
849 * @param cls the closure
850 * @param client the client
851 * @param message the message
853 static void handle_get_authority(void *cls,
854 struct GNUNET_SERVER_Client * client,
855 const struct GNUNET_MessageHeader * message)
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
860 struct ClientGetAuthHandle *cah;
861 char name[MAX_DNS_NAME_LENGTH];
862 char* nameptr = name;
865 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
872 GNUNET_SERVER_notification_context_add (nc, client);
874 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
875 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
877 msg_size = ntohs(message->size);
879 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
882 GNUNET_SERVER_receive_done (client, GNUNET_OK);
886 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
889 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
890 cah->client = client;
891 cah->unique_id = sh_msg->id;
893 if (strlen(name) < strlen(GNUNET_GNS_TLD))
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "GET_AUTH: %s is too short. Returning\n", name);
898 send_get_auth_response(cah, name);
902 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
904 "GET_AUTH: %s is too long", name);
906 send_get_auth_response(cah, name);
910 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
911 GNUNET_GNS_TLD) != 0)
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "GET_AUTH: %s is not our domain. Returning\n", name);
916 send_get_auth_response(cah, name);
920 if (strcmp(name, GNUNET_GNS_TLD) == 0)
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "GET_AUTH: %s is us. Returning\n", name);
925 send_get_auth_response(cah, name);
929 cah->name = GNUNET_malloc(strlen(name)
930 - strlen(GNUNET_GNS_TLD) + 1);
932 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
933 memcpy(cah->name, name,
934 strlen(name)-strlen(GNUNET_GNS_TLD));
936 /* Start delegation resolution in our namestore */
937 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
943 * Reply to client with the result from our lookup.
945 * @param cls the closure (our client lookup handle)
946 * @param rd_count the number of records
947 * @param rd the record data
950 send_lookup_response(void* cls,
952 const struct GNUNET_NAMESTORE_RecordData *rd)
954 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
955 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
959 "LOOKUP_RESULT", rd_count);
961 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
962 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
964 rmsg->id = clh->unique_id;
965 rmsg->rd_count = htonl(rd_count);
966 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
968 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
970 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
972 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
973 (const struct GNUNET_MessageHeader *) rmsg,
975 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
978 GNUNET_free(clh->name);
980 if (NULL != clh->shorten_key)
981 GNUNET_free(clh->shorten_key);
989 * Handle lookup requests from client
991 * @param cls the closure
992 * @param client the client
993 * @param message the message
996 handle_lookup(void *cls,
997 struct GNUNET_SERVER_Client * client,
998 const struct GNUNET_MessageHeader * message)
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1002 size_t msg_size = 0;
1004 char name[MAX_DNS_NAME_LENGTH];
1005 struct ClientLookupHandle *clh;
1006 char* nameptr = name;
1008 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1009 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1012 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1014 GNUNET_break_op (0);
1015 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1019 GNUNET_SERVER_notification_context_add (nc, client);
1021 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1022 (struct GNUNET_GNS_ClientLookupMessage *) message;
1024 msg_size = ntohs(message->size);
1026 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1028 GNUNET_break_op (0);
1029 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1033 if (1 == ntohl(sh_msg->have_key))
1035 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1036 tmp_pkey = (char*)&sh_msg[1];
1037 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1038 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1043 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1046 namelen = strlen(name)+1;
1047 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1048 memset (clh, 0, sizeof (struct ClientLookupHandle));
1049 clh->client = client;
1050 clh->name = GNUNET_malloc(namelen);
1051 strcpy(clh->name, name);
1052 clh->unique_id = sh_msg->id;
1053 clh->type = ntohl(sh_msg->type);
1054 clh->shorten_key = key;
1056 only_cached = ntohl(sh_msg->only_cached);
1058 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1059 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1060 "LOOKUP: %s is too long", name);
1062 send_lookup_response(clh, 0, NULL);
1066 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1067 (GNUNET_OK != v4_enabled))
1069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1070 "LOOKUP: Query for A record but AF_INET not supported!");
1072 send_lookup_response(clh, 0, NULL);
1076 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1077 (GNUNET_OK != v6_enabled))
1079 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1080 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1082 send_lookup_response(clh, 0, NULL);
1086 if (1 == ntohl(sh_msg->use_default_zone))
1087 clh->zone = zone_hash; //Default zone
1089 clh->zone = sh_msg->zone;
1091 if (GNUNET_YES == auto_import_pkey)
1093 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1095 default_lookup_timeout,
1097 &send_lookup_response, clh);
1101 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1103 default_lookup_timeout,
1105 &send_lookup_response, clh);
1110 * Test if the given AF is supported by this system.
1113 * @return GNUNET_OK if the AF is supported
1120 s = socket (af, SOCK_STREAM, 0);
1123 if (EAFNOSUPPORT == errno)
1125 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1126 return GNUNET_SYSERR;
1133 * Process GNS requests.
1135 * @param cls closure)
1136 * @param server the initialized server
1137 * @param c configuration to use
1140 run (void *cls, struct GNUNET_SERVER_Handle *server,
1141 const struct GNUNET_CONFIGURATION_Handle *c)
1144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1147 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1148 unsigned long long max_parallel_bg_queries = 0;
1149 unsigned long long default_lookup_timeout_secs = 0;
1150 int ignore_pending = GNUNET_NO;
1152 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1153 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1154 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1155 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1160 v6_enabled = test_af (AF_INET6);
1161 v4_enabled = test_af (AF_INET);
1163 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1164 "ZONEKEY", &keyfile))
1166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1167 "No private key for root zone specified!\n");
1168 GNUNET_SCHEDULER_shutdown ();
1172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1173 "Using keyfile %s for root zone.\n", keyfile);
1175 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1176 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1178 GNUNET_CRYPTO_short_hash(&pkey,
1179 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1181 GNUNET_free(keyfile);
1184 * handle to our local namestore
1186 namestore_handle = GNUNET_NAMESTORE_connect(c);
1188 if (NULL == namestore_handle)
1190 //FIXME do error handling;
1191 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1192 "Failed to connect to the namestore!\n");
1193 GNUNET_SCHEDULER_shutdown ();
1199 auto_import_pkey = GNUNET_NO;
1202 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1203 "AUTO_IMPORT_PKEY"))
1205 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1206 "Automatic PKEY import is enabled.\n");
1207 auto_import_pkey = GNUNET_YES;
1211 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1214 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1215 "ZONE_PUT_INTERVAL",
1216 &dht_max_update_interval))
1218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1219 "DHT zone update interval: %llu\n",
1220 dht_max_update_interval);
1223 max_record_put_interval = 1;
1226 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1227 "RECORD_PUT_INTERVAL",
1228 &max_record_put_interval))
1230 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1231 "Record put interval: %llu\n",
1232 max_record_put_interval);
1236 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1237 "MAX_PARALLEL_BACKGROUND_QUERIES",
1238 &max_parallel_bg_queries))
1240 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1241 "Number of allowed parallel background queries: %llu\n",
1242 max_parallel_bg_queries);
1246 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1247 "AUTO_IMPORT_CONFIRMATION_REQ"))
1249 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1250 "Auto import requires user confirmation\n");
1251 ignore_pending = GNUNET_YES;
1255 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1256 "DEFAULT_LOOKUP_TIMEOUT",
1257 &default_lookup_timeout_secs))
1259 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1260 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1261 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1262 GNUNET_TIME_UNIT_SECONDS,
1263 default_lookup_timeout_secs);
1269 dht_handle = GNUNET_DHT_connect(c,
1270 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1273 if (NULL == dht_handle)
1275 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1278 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1279 max_parallel_bg_queries,
1283 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1284 "Unable to initialize resolver!\n");
1285 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1290 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1292 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1293 "DNS hijacking enabled... connecting to service.\n");
1295 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1297 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1298 "Failed to enable the dns interceptor!\n");
1303 * Schedule periodic put
1305 * We have roughly an hour for all records;
1307 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1309 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1311 GNUNET_SERVER_add_handlers (server, handlers);
1314 //GNUNET_SERVER_disconnect_notify (server,
1315 // &client_disconnect_notification,
1318 nc = GNUNET_SERVER_notification_context_create (server, 1);
1320 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1326 * The main function for the GNS service.
1328 * @param argc number of arguments from the command line
1329 * @param argv command line arguments
1330 * @return 0 ok, 1 on error
1333 main (int argc, char *const *argv)
1339 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1344 /* end of gnunet-service-gns.c */