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 GNUNET_NAMESTORE_disconnect(namestore_handle);
203 GNUNET_DHT_disconnect(dht_handle);
207 * Task run during shutdown.
213 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
220 GNUNET_SERVER_notification_context_destroy (nc);
222 gns_interceptor_stop();
223 gns_resolver_cleanup(&on_resolver_cleanup);
224 /* Kill zone task for it may make the scheduler hang */
225 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
226 GNUNET_SCHEDULER_cancel(zone_update_taskid);
231 * Method called periodicattluy that triggers
232 * iteration over root zone
235 * @param tc task context
238 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
241 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
245 * Continuation for DHT put
248 * @param success GNUNET_OK if the PUT was transmitted,
249 * GNUNET_NO on timeout,
250 * GNUNET_SYSERR on disconnect from service
251 * after the PUT message was transmitted
252 * (so we don't know if it was received or not)
255 record_dht_put(void *cls, int success)
257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
262 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
265 * Function used to put all records successively into the DHT.
267 * @param cls the closure (NULL)
268 * @param key the public key of the authority (ours)
269 * @param expiration lifetime of the namestore entry
270 * @param name the name of the records
271 * @param rd_count the number of records in data
272 * @param rd the record data
273 * @param signature the signature for the record data
276 put_gns_record(void *cls,
277 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
278 struct GNUNET_TIME_Absolute expiration,
280 unsigned int rd_count,
281 const struct GNUNET_NAMESTORE_RecordData *rd,
282 const struct GNUNET_CRYPTO_RsaSignature *signature)
285 struct GNSNameRecordBlock *nrb;
286 struct GNUNET_CRYPTO_ShortHashCode name_hash;
287 struct GNUNET_CRYPTO_ShortHashCode zhash;
288 struct GNUNET_HashCode xor_hash;
289 struct GNUNET_HashCode name_hash_double;
290 struct GNUNET_HashCode zone_hash_double;
291 uint32_t rd_payload_length;
292 char* nrb_data = NULL;
298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
299 "Zone iteration finished. Rescheduling put in %ds\n",
300 dht_max_update_interval);
301 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
302 GNUNET_TIME_relative_multiply(
303 GNUNET_TIME_UNIT_SECONDS,
304 dht_max_update_interval
306 &update_zone_dht_start,
311 namelen = strlen(name) + 1;
313 if (signature == NULL)
315 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
316 "No signature for %s record data provided! Skipping...\n",
318 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
325 "Putting records for %s into the DHT\n", name);
327 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
329 nrb = GNUNET_malloc(rd_payload_length + namelen
330 + sizeof(struct GNSNameRecordBlock));
332 nrb->signature = *signature;
334 nrb->public_key = *key;
336 nrb->rd_count = htonl(rd_count);
338 memcpy(&nrb[1], name, namelen);
340 nrb_data = (char*)&nrb[1];
343 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
345 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
350 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
351 "Record serialization failed! Skipping...\n");
353 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
360 * calculate DHT key: H(name) xor H(pubkey)
362 GNUNET_CRYPTO_short_hash(key,
363 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
365 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
366 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
367 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
368 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
371 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
373 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
374 "putting records for %s under key: %s with size %d\n",
375 name, GNUNET_h2s (&xor_hash), rd_payload_length);
377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
378 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
379 /* FIXME: keep return value to possibly cancel? */
380 GNUNET_DHT_put (dht_handle, &xor_hash,
381 DHT_GNS_REPLICATION_LEVEL,
383 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
387 DHT_OPERATION_TIMEOUT,
389 NULL); //cls for cont
391 num_public_records++;
394 * Reschedule periodic put
396 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
397 &update_zone_dht_next,
405 * Periodically iterate over our zone and store everything in dht
408 * @param tc task context
411 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
413 unsigned long long interval = 0;
415 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
418 if (0 == num_public_records)
421 * If no records are known (startup) or none present
422 * we can safely set the interval to 1s
424 record_put_interval = GNUNET_TIME_relative_multiply(
425 GNUNET_TIME_UNIT_SECONDS,
427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
428 "No records in db. Adjusted record put interval to 1s\n");
432 interval = max_record_put_interval/num_public_records;
435 record_put_interval = GNUNET_TIME_relative_multiply(
436 GNUNET_TIME_UNIT_SECONDS,
438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
439 "Adjusted DHT update interval to %ds!\n",
443 /* start counting again */
444 num_public_records = 0;
445 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
447 GNUNET_NAMESTORE_RF_AUTHORITY,
448 GNUNET_NAMESTORE_RF_PRIVATE,
453 /* END DHT ZONE PROPAGATION */
456 * Send shorten response back to client
458 * @param cls the closure containing a client shorten handle
459 * @param name the shortened name result or NULL if cannot be shortened
462 send_shorten_response(void* cls, const char* name)
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
465 "SHORTEN_RESULT", name);
466 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
467 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
474 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
477 rmsg->id = csh->unique_id;
478 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
480 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
483 strcpy((char*)&rmsg[1], name);
485 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
486 (const struct GNUNET_MessageHeader *) rmsg,
488 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
491 GNUNET_free_non_null(csh->shorten_key);
498 process_shorten_zone_shorten (void *cls,
499 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
500 struct GNUNET_TIME_Absolute expiration,
502 unsigned int rd_count,
503 const struct GNUNET_NAMESTORE_RecordData *rd,
504 const struct GNUNET_CRYPTO_RsaSignature *signature)
506 struct ClientShortenHandle *csh = cls;
507 struct GNUNET_TIME_Relative remaining_time;
508 struct GNUNET_TIME_Absolute et;
510 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
512 if ((rd_count == 1) &&
513 (remaining_time.rel_value != 0))
515 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
516 et.abs_value = rd->expiration_time;
517 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
518 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
519 (remaining_time.rel_value != 0))
521 csh->shorten_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
524 gns_resolver_shorten_name (&csh->root_zone,
530 &send_shorten_response, csh);
536 process_private_zone_shorten (void *cls,
537 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
538 struct GNUNET_TIME_Absolute expiration,
540 unsigned int rd_count,
541 const struct GNUNET_NAMESTORE_RecordData *rd,
542 const struct GNUNET_CRYPTO_RsaSignature *signature)
544 struct GNUNET_TIME_Relative remaining_time;
545 struct ClientShortenHandle *csh = cls;
546 struct GNUNET_TIME_Absolute et;
548 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
550 if ((rd_count == 1) &&
551 (remaining_time.rel_value != 0))
553 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
554 et.abs_value = rd->expiration_time;
555 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
556 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
557 (remaining_time.rel_value != 0))
561 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
562 "Looking for shorten zone in private zone\n");
563 csh->private_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
564 GNUNET_NAMESTORE_lookup_record (namestore_handle,
567 GNUNET_GNS_RECORD_ANY,
568 &process_shorten_zone_shorten,
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
575 "No private zone found!\n");
576 gns_resolver_shorten_name (&csh->root_zone,
582 &send_shorten_response, csh);
587 * Lookup the zone infos and shorten name
589 * @param csh the shorten handle
593 start_shorten_name (struct ClientShortenHandle *csh)
595 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
596 "Looking for private zone in root zone\n");
598 GNUNET_NAMESTORE_lookup_record (namestore_handle,
601 GNUNET_GNS_RECORD_ANY,
602 &process_private_zone_shorten,
608 * Handle a shorten message from the api
610 * @param cls the closure
611 * @param client the client
612 * @param message the message
614 static void handle_shorten (void *cls,
615 struct GNUNET_SERVER_Client * client,
616 const struct GNUNET_MessageHeader * message)
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
621 struct ClientShortenHandle *csh;
622 char name[MAX_DNS_NAME_LENGTH];
623 char* nameptr = name;
625 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
633 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
634 (struct GNUNET_GNS_ClientShortenMessage *) message;
636 msg_size = ntohs(message->size);
638 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
641 GNUNET_SERVER_receive_done (client, GNUNET_OK);
645 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
646 csh->client = client;
647 csh->unique_id = sh_msg->id;
648 csh->shorten_key = NULL;
650 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
652 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
653 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
654 "SHORTEN: %s is too short", name);
655 send_shorten_response(csh, name);
659 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
660 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
661 "SHORTEN: %s is too long", name);
662 send_shorten_response(csh, name);
666 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "%s is not our domain. Returning\n", name);
670 send_shorten_response(csh, name);
674 strcpy (csh->name, name);
676 GNUNET_SERVER_notification_context_add (nc, client);
678 if (1 == ntohl(sh_msg->use_default_zone))
679 csh->root_zone = zone_hash; //Default zone
681 csh->root_zone = sh_msg->zone;
683 start_shorten_name (csh);
689 * Send get authority response back to client
691 * @param cls the closure containing a client get auth handle
692 * @param name the shortened name result or NULL if cannot be shortened
695 send_get_auth_response(void *cls, const char* name)
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
698 "GET_AUTH_RESULT", name);
699 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
700 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
707 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
710 rmsg->id = cah->unique_id;
711 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
713 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
716 strcpy((char*)&rmsg[1], name);
718 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
719 (const struct GNUNET_MessageHeader *) rmsg,
721 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
726 GNUNET_free_non_null(cah->name);
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
735 * Handle a get authority message from the api
737 * @param cls the closure
738 * @param client the client
739 * @param message the message
741 static void handle_get_authority(void *cls,
742 struct GNUNET_SERVER_Client * client,
743 const struct GNUNET_MessageHeader * message)
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
748 struct ClientGetAuthHandle *cah;
749 char name[MAX_DNS_NAME_LENGTH];
750 char* nameptr = name;
753 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
760 GNUNET_SERVER_notification_context_add (nc, client);
762 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
763 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
765 msg_size = ntohs(message->size);
767 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
770 GNUNET_SERVER_receive_done (client, GNUNET_OK);
774 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
777 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
778 cah->client = client;
779 cah->unique_id = sh_msg->id;
781 if (strlen(name) < strlen(GNUNET_GNS_TLD))
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "GET_AUTH: %s is too short. Returning\n", name);
786 send_get_auth_response(cah, name);
790 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
791 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
792 "GET_AUTH: %s is too long", name);
794 send_get_auth_response(cah, name);
798 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
799 GNUNET_GNS_TLD) != 0)
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "GET_AUTH: %s is not our domain. Returning\n", name);
804 send_get_auth_response(cah, name);
808 if (strcmp(name, GNUNET_GNS_TLD) == 0)
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "GET_AUTH: %s is us. Returning\n", name);
813 send_get_auth_response(cah, name);
817 cah->name = GNUNET_malloc(strlen(name)
818 - strlen(GNUNET_GNS_TLD) + 1);
820 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
821 memcpy(cah->name, name,
822 strlen(name)-strlen(GNUNET_GNS_TLD));
824 /* Start delegation resolution in our namestore */
825 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
831 * Reply to client with the result from our lookup.
833 * @param cls the closure (our client lookup handle)
834 * @param rd_count the number of records
835 * @param rd the record data
838 send_lookup_response(void* cls,
840 const struct GNUNET_NAMESTORE_RecordData *rd)
842 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
843 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
847 "LOOKUP_RESULT", rd_count);
849 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
850 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
852 rmsg->id = clh->unique_id;
853 rmsg->rd_count = htonl(rd_count);
854 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
856 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
858 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
860 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
861 (const struct GNUNET_MessageHeader *) rmsg,
863 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
866 GNUNET_free(clh->name);
868 if (NULL != clh->shorten_key)
869 GNUNET_free(clh->shorten_key);
877 * Handle lookup requests from client
879 * @param cls the closure
880 * @param client the client
881 * @param message the message
884 handle_lookup(void *cls,
885 struct GNUNET_SERVER_Client * client,
886 const struct GNUNET_MessageHeader * message)
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
892 char name[MAX_DNS_NAME_LENGTH];
893 struct ClientLookupHandle *clh;
894 char* nameptr = name;
896 struct GNUNET_CRYPTO_RsaPrivateKey *key;
897 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
900 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
903 GNUNET_SERVER_receive_done (client, GNUNET_OK);
907 GNUNET_SERVER_notification_context_add (nc, client);
909 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
910 (struct GNUNET_GNS_ClientLookupMessage *) message;
912 msg_size = ntohs(message->size);
914 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
917 GNUNET_SERVER_receive_done (client, GNUNET_OK);
921 if (1 == ntohl(sh_msg->have_key))
923 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
924 tmp_pkey = (char*)&sh_msg[1];
925 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
926 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
931 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
934 namelen = strlen(name)+1;
935 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
936 clh->client = client;
937 clh->name = GNUNET_malloc(namelen);
938 strcpy(clh->name, name);
939 clh->unique_id = sh_msg->id;
940 clh->type = ntohl(sh_msg->type);
941 clh->shorten_key = key;
943 only_cached = ntohl(sh_msg->only_cached);
945 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
946 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
947 "LOOKUP: %s is too long", name);
949 send_lookup_response(clh, 0, NULL);
953 if ((clh->type == GNUNET_GNS_RECORD_TYPE_A) &&
954 (GNUNET_OK != v4_enabled))
956 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
957 "LOOKUP: Query for A record but AF_INET not supported!");
959 send_lookup_response(clh, 0, NULL);
963 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
964 (GNUNET_OK != v6_enabled))
966 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
967 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
969 send_lookup_response(clh, 0, NULL);
973 if (1 == ntohl(sh_msg->use_default_zone))
974 clh->zone = zone_hash; //Default zone
976 clh->zone = sh_msg->zone;
978 if (GNUNET_YES == auto_import_pkey)
980 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
982 default_lookup_timeout,
984 &send_lookup_response, clh);
988 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
990 default_lookup_timeout,
992 &send_lookup_response, clh);
997 * Test if the given AF is supported by this system.
1000 * @return GNUNET_OK if the AF is supported
1007 s = socket (af, SOCK_STREAM, 0);
1010 if (EAFNOSUPPORT == errno)
1012 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1013 return GNUNET_SYSERR;
1020 * Process GNS requests.
1022 * @param cls closure)
1023 * @param server the initialized server
1024 * @param c configuration to use
1027 run (void *cls, struct GNUNET_SERVER_Handle *server,
1028 const struct GNUNET_CONFIGURATION_Handle *c)
1031 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1034 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1035 unsigned long long max_parallel_bg_queries = 0;
1036 unsigned long long default_lookup_timeout_secs = 0;
1037 int ignore_pending = GNUNET_NO;
1039 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1040 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1041 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1042 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1047 v6_enabled = test_af (AF_INET6);
1048 v4_enabled = test_af (AF_INET);
1050 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1051 "ZONEKEY", &keyfile))
1053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1054 "No private key for root zone specified!\n");
1055 GNUNET_SCHEDULER_shutdown ();
1059 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1060 "Using keyfile %s for root zone.\n", keyfile);
1062 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1063 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1065 GNUNET_CRYPTO_short_hash(&pkey,
1066 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1068 GNUNET_free(keyfile);
1071 * handle to our local namestore
1073 namestore_handle = GNUNET_NAMESTORE_connect(c);
1075 if (NULL == namestore_handle)
1077 //FIXME do error handling;
1078 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1079 "Failed to connect to the namestore!\n");
1080 GNUNET_SCHEDULER_shutdown ();
1086 auto_import_pkey = GNUNET_NO;
1089 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1090 "AUTO_IMPORT_PKEY"))
1092 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1093 "Automatic PKEY import is enabled.\n");
1094 auto_import_pkey = GNUNET_YES;
1098 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1102 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1103 "Private zone id: %s\n", private_zone_id);
1106 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1110 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1111 "Shorten zone id: %s\n", shorten_zone_id);
1114 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1117 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1118 "ZONE_PUT_INTERVAL",
1119 &dht_max_update_interval))
1121 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1122 "DHT zone update interval: %llu\n",
1123 dht_max_update_interval);
1126 max_record_put_interval = 1;
1129 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1130 "RECORD_PUT_INTERVAL",
1131 &max_record_put_interval))
1133 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1134 "Record put interval: %llu\n",
1135 max_record_put_interval);
1139 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1140 "MAX_PARALLEL_BACKGROUND_QUERIES",
1141 &max_parallel_bg_queries))
1143 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1144 "Number of allowed parallel background queries: %llu\n",
1145 max_parallel_bg_queries);
1149 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1150 "AUTO_IMPORT_CONFIRMATION_REQ"))
1152 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1153 "Auto import requires user confirmation\n");
1154 ignore_pending = GNUNET_YES;
1158 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1159 "DEFAULT_LOOKUP_TIMEOUT",
1160 &default_lookup_timeout_secs))
1162 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1163 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1164 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1165 GNUNET_TIME_UNIT_SECONDS,
1166 default_lookup_timeout_secs);
1172 dht_handle = GNUNET_DHT_connect(c,
1173 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1176 if (NULL == dht_handle)
1178 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1181 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1182 max_parallel_bg_queries,
1186 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1187 "Unable to initialize resolver!\n");
1188 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1193 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1195 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1196 "DNS hijacking enabled... connecting to service.\n");
1198 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1200 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1201 "Failed to enable the dns interceptor!\n");
1206 * Schedule periodic put
1208 * We have roughly an hour for all records;
1210 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1212 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1214 GNUNET_SERVER_add_handlers (server, handlers);
1217 //GNUNET_SERVER_disconnect_notify (server,
1218 // &client_disconnect_notification,
1221 nc = GNUNET_SERVER_notification_context_create (server, 1);
1223 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1229 * The main function for the GNS service.
1231 * @param argc number of arguments from the command line
1232 * @param argv command line arguments
1233 * @return 0 ok, 1 on error
1236 main (int argc, char *const *argv)
1242 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1247 /* end of gnunet-service-gns.c */