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;
194 on_resolver_cleanup(void)
196 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
201 * Task run during shutdown.
207 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
214 GNUNET_SERVER_notification_context_destroy (nc);
216 gns_interceptor_stop();
217 gns_resolver_cleanup(&on_resolver_cleanup);
218 /* Kill zone task for it may make the scheduler hang */
219 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
220 GNUNET_SCHEDULER_cancel(zone_update_taskid);
221 GNUNET_DHT_disconnect(dht_handle);
226 * Method called periodicattluy that triggers
227 * iteration over root zone
230 * @param tc task context
233 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
236 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
240 * Continuation for DHT put
243 * @param success GNUNET_OK if the PUT was transmitted,
244 * GNUNET_NO on timeout,
245 * GNUNET_SYSERR on disconnect from service
246 * after the PUT message was transmitted
247 * (so we don't know if it was received or not)
250 record_dht_put(void *cls, int success)
252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
257 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
260 * Function used to put all records successively into the DHT.
262 * @param cls the closure (NULL)
263 * @param key the public key of the authority (ours)
264 * @param expiration lifetime of the namestore entry
265 * @param name the name of the records
266 * @param rd_count the number of records in data
267 * @param rd the record data
268 * @param signature the signature for the record data
271 put_gns_record(void *cls,
272 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
273 struct GNUNET_TIME_Absolute expiration,
275 unsigned int rd_count,
276 const struct GNUNET_NAMESTORE_RecordData *rd,
277 const struct GNUNET_CRYPTO_RsaSignature *signature)
280 struct GNSNameRecordBlock *nrb;
281 struct GNUNET_CRYPTO_ShortHashCode name_hash;
282 struct GNUNET_CRYPTO_ShortHashCode zhash;
283 struct GNUNET_HashCode xor_hash;
284 struct GNUNET_HashCode name_hash_double;
285 struct GNUNET_HashCode zone_hash_double;
286 uint32_t rd_payload_length;
287 char* nrb_data = NULL;
293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
294 "Zone iteration finished. Rescheduling put in %ds\n",
295 dht_max_update_interval);
296 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
297 GNUNET_TIME_relative_multiply(
298 GNUNET_TIME_UNIT_SECONDS,
299 dht_max_update_interval
301 &update_zone_dht_start,
306 namelen = strlen(name) + 1;
308 if (signature == NULL)
310 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
311 "No signature for %s record data provided! Skipping...\n",
313 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
320 "Putting records for %s into the DHT\n", name);
322 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
324 nrb = GNUNET_malloc(rd_payload_length + namelen
325 + sizeof(struct GNSNameRecordBlock));
327 nrb->signature = *signature;
329 nrb->public_key = *key;
331 nrb->rd_count = htonl(rd_count);
333 memcpy(&nrb[1], name, namelen);
335 nrb_data = (char*)&nrb[1];
338 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
340 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
345 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
346 "Record serialization failed! Skipping...\n");
348 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
355 * calculate DHT key: H(name) xor H(pubkey)
357 GNUNET_CRYPTO_short_hash(key,
358 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
360 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
361 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
362 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
363 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
366 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
369 "putting records for %s under key: %s with size %d\n",
370 name, GNUNET_h2s (&xor_hash), rd_payload_length);
372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
373 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
374 /* FIXME: keep return value to possibly cancel? */
375 GNUNET_DHT_put (dht_handle, &xor_hash,
376 DHT_GNS_REPLICATION_LEVEL,
378 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
382 DHT_OPERATION_TIMEOUT,
384 NULL); //cls for cont
386 num_public_records++;
389 * Reschedule periodic put
391 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
392 &update_zone_dht_next,
400 * Periodically iterate over our zone and store everything in dht
403 * @param tc task context
406 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 unsigned long long interval = 0;
410 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
413 if (0 == num_public_records)
416 * If no records are known (startup) or none present
417 * we can safely set the interval to 1s
419 record_put_interval = GNUNET_TIME_relative_multiply(
420 GNUNET_TIME_UNIT_SECONDS,
422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
423 "No records in db. Adjusted record put interval to 1s\n");
427 interval = max_record_put_interval/num_public_records;
430 record_put_interval = GNUNET_TIME_relative_multiply(
431 GNUNET_TIME_UNIT_SECONDS,
433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
434 "Adjusted DHT update interval to %ds!\n",
438 /* start counting again */
439 num_public_records = 0;
440 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
442 GNUNET_NAMESTORE_RF_AUTHORITY,
443 GNUNET_NAMESTORE_RF_PRIVATE,
448 /* END DHT ZONE PROPAGATION */
451 * Send shorten response back to client
453 * @param cls the closure containing a client shorten handle
454 * @param name the shortened name result or NULL if cannot be shortened
457 send_shorten_response(void* cls, const char* name)
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
460 "SHORTEN_RESULT", name);
461 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
462 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
469 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
472 rmsg->id = csh->unique_id;
473 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
475 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
478 strcpy((char*)&rmsg[1], name);
480 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
481 (const struct GNUNET_MessageHeader *) rmsg,
483 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
486 GNUNET_free_non_null(csh->shorten_key);
493 process_shorten_zone_shorten (void *cls,
494 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
495 struct GNUNET_TIME_Absolute expiration,
497 unsigned int rd_count,
498 const struct GNUNET_NAMESTORE_RecordData *rd,
499 const struct GNUNET_CRYPTO_RsaSignature *signature)
501 struct ClientShortenHandle *csh = cls;
502 struct GNUNET_TIME_Relative remaining_time;
503 struct GNUNET_TIME_Absolute et;
505 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
507 if ((rd_count == 1) &&
508 (remaining_time.rel_value != 0))
510 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
511 et.abs_value = rd->expiration_time;
512 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
513 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
514 (remaining_time.rel_value != 0))
516 csh->shorten_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
519 gns_resolver_shorten_name (&csh->root_zone,
525 &send_shorten_response, csh);
531 process_private_zone_shorten (void *cls,
532 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
533 struct GNUNET_TIME_Absolute expiration,
535 unsigned int rd_count,
536 const struct GNUNET_NAMESTORE_RecordData *rd,
537 const struct GNUNET_CRYPTO_RsaSignature *signature)
539 struct GNUNET_TIME_Relative remaining_time;
540 struct ClientShortenHandle *csh = cls;
541 struct GNUNET_TIME_Absolute et;
543 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
545 if ((rd_count == 1) &&
546 (remaining_time.rel_value != 0))
548 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
549 et.abs_value = rd->expiration_time;
550 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
551 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
552 (remaining_time.rel_value != 0))
556 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
557 "Looking for shorten zone in private zone\n");
558 csh->private_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
559 GNUNET_NAMESTORE_lookup_record (namestore_handle,
562 GNUNET_GNS_RECORD_ANY,
563 &process_shorten_zone_shorten,
569 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
570 "No private zone found!\n");
571 gns_resolver_shorten_name (&csh->root_zone,
577 &send_shorten_response, csh);
582 * Lookup the zone infos and shorten name
584 * @param csh the shorten handle
588 start_shorten_name (struct ClientShortenHandle *csh)
590 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
591 "Looking for private zone in root zone\n");
593 GNUNET_NAMESTORE_lookup_record (namestore_handle,
596 GNUNET_GNS_RECORD_ANY,
597 &process_private_zone_shorten,
603 * Handle a shorten message from the api
605 * @param cls the closure
606 * @param client the client
607 * @param message the message
609 static void handle_shorten (void *cls,
610 struct GNUNET_SERVER_Client * client,
611 const struct GNUNET_MessageHeader * message)
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
616 struct ClientShortenHandle *csh;
617 char name[MAX_DNS_NAME_LENGTH];
618 char* nameptr = name;
620 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
623 GNUNET_SERVER_receive_done (client, GNUNET_OK);
628 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
629 (struct GNUNET_GNS_ClientShortenMessage *) message;
631 msg_size = ntohs(message->size);
633 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
636 GNUNET_SERVER_receive_done (client, GNUNET_OK);
640 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
641 csh->client = client;
642 csh->unique_id = sh_msg->id;
643 csh->shorten_key = NULL;
645 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
647 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
648 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
649 "SHORTEN: %s is too short", name);
650 send_shorten_response(csh, name);
654 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
655 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
656 "SHORTEN: %s is too long", name);
657 send_shorten_response(csh, name);
661 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "%s is not our domain. Returning\n", name);
665 send_shorten_response(csh, name);
669 strcpy (csh->name, name);
671 GNUNET_SERVER_notification_context_add (nc, client);
673 if (1 == ntohl(sh_msg->use_default_zone))
674 csh->root_zone = zone_hash; //Default zone
676 csh->root_zone = sh_msg->zone;
678 start_shorten_name (csh);
684 * Send get authority response back to client
686 * @param cls the closure containing a client get auth handle
687 * @param name the shortened name result or NULL if cannot be shortened
690 send_get_auth_response(void *cls, const char* name)
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
693 "GET_AUTH_RESULT", name);
694 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
695 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
702 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
705 rmsg->id = cah->unique_id;
706 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
708 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
711 strcpy((char*)&rmsg[1], name);
713 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
714 (const struct GNUNET_MessageHeader *) rmsg,
716 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
721 GNUNET_free_non_null(cah->name);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
730 * Handle a get authority message from the api
732 * @param cls the closure
733 * @param client the client
734 * @param message the message
736 static void handle_get_authority(void *cls,
737 struct GNUNET_SERVER_Client * client,
738 const struct GNUNET_MessageHeader * message)
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
743 struct ClientGetAuthHandle *cah;
744 char name[MAX_DNS_NAME_LENGTH];
745 char* nameptr = name;
748 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
751 GNUNET_SERVER_receive_done (client, GNUNET_OK);
755 GNUNET_SERVER_notification_context_add (nc, client);
757 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
758 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
760 msg_size = ntohs(message->size);
762 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
769 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
772 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
773 cah->client = client;
774 cah->unique_id = sh_msg->id;
776 if (strlen(name) < strlen(GNUNET_GNS_TLD))
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "GET_AUTH: %s is too short. Returning\n", name);
781 send_get_auth_response(cah, name);
785 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
786 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
787 "GET_AUTH: %s is too long", name);
789 send_get_auth_response(cah, name);
793 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
794 GNUNET_GNS_TLD) != 0)
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "GET_AUTH: %s is not our domain. Returning\n", name);
799 send_get_auth_response(cah, name);
803 if (strcmp(name, GNUNET_GNS_TLD) == 0)
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "GET_AUTH: %s is us. Returning\n", name);
808 send_get_auth_response(cah, name);
812 cah->name = GNUNET_malloc(strlen(name)
813 - strlen(GNUNET_GNS_TLD) + 1);
815 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
816 memcpy(cah->name, name,
817 strlen(name)-strlen(GNUNET_GNS_TLD));
819 /* Start delegation resolution in our namestore */
820 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
826 * Reply to client with the result from our lookup.
828 * @param cls the closure (our client lookup handle)
829 * @param rd_count the number of records
830 * @param rd the record data
833 send_lookup_response(void* cls,
835 const struct GNUNET_NAMESTORE_RecordData *rd)
837 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
838 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
842 "LOOKUP_RESULT", rd_count);
844 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
845 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
847 rmsg->id = clh->unique_id;
848 rmsg->rd_count = htonl(rd_count);
849 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
851 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
853 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
855 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
856 (const struct GNUNET_MessageHeader *) rmsg,
858 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
861 GNUNET_free(clh->name);
863 if (NULL != clh->shorten_key)
864 GNUNET_free(clh->shorten_key);
872 * Handle lookup requests from client
874 * @param cls the closure
875 * @param client the client
876 * @param message the message
879 handle_lookup(void *cls,
880 struct GNUNET_SERVER_Client * client,
881 const struct GNUNET_MessageHeader * message)
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
887 char name[MAX_DNS_NAME_LENGTH];
888 struct ClientLookupHandle *clh;
889 char* nameptr = name;
891 struct GNUNET_CRYPTO_RsaPrivateKey *key;
892 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
895 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
902 GNUNET_SERVER_notification_context_add (nc, client);
904 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
905 (struct GNUNET_GNS_ClientLookupMessage *) message;
907 msg_size = ntohs(message->size);
909 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
912 GNUNET_SERVER_receive_done (client, GNUNET_OK);
916 if (1 == ntohl(sh_msg->have_key))
918 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
919 tmp_pkey = (char*)&sh_msg[1];
920 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
921 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
926 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
929 namelen = strlen(name)+1;
930 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
931 clh->client = client;
932 clh->name = GNUNET_malloc(namelen);
933 strcpy(clh->name, name);
934 clh->unique_id = sh_msg->id;
935 clh->type = ntohl(sh_msg->type);
936 clh->shorten_key = key;
938 only_cached = ntohl(sh_msg->only_cached);
940 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
941 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
942 "LOOKUP: %s is too long", name);
944 send_lookup_response(clh, 0, NULL);
948 if (1 == ntohl(sh_msg->use_default_zone))
949 clh->zone = zone_hash; //Default zone
951 clh->zone = sh_msg->zone;
953 if (GNUNET_YES == auto_import_pkey)
955 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
957 default_lookup_timeout,
959 &send_lookup_response, clh);
963 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
965 default_lookup_timeout,
967 &send_lookup_response, clh);
974 * Process GNS requests.
976 * @param cls closure)
977 * @param server the initialized server
978 * @param c configuration to use
981 run (void *cls, struct GNUNET_SERVER_Handle *server,
982 const struct GNUNET_CONFIGURATION_Handle *c)
985 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
988 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
989 unsigned long long max_parallel_bg_queries = 0;
990 unsigned long long default_lookup_timeout_secs = 0;
991 int ignore_pending = GNUNET_NO;
993 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
994 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
995 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
996 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1001 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1002 "ZONEKEY", &keyfile))
1004 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1005 "No private key for root zone specified!\n");
1006 GNUNET_SCHEDULER_shutdown ();
1010 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1011 "Using keyfile %s for root zone.\n", keyfile);
1013 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1014 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1016 GNUNET_CRYPTO_short_hash(&pkey,
1017 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1019 GNUNET_free(keyfile);
1022 * handle to our local namestore
1024 namestore_handle = GNUNET_NAMESTORE_connect(c);
1026 if (NULL == namestore_handle)
1028 //FIXME do error handling;
1029 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1030 "Failed to connect to the namestore!\n");
1031 GNUNET_SCHEDULER_shutdown ();
1037 auto_import_pkey = GNUNET_NO;
1040 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1041 "AUTO_IMPORT_PKEY"))
1043 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1044 "Automatic PKEY import is enabled.\n");
1045 auto_import_pkey = GNUNET_YES;
1049 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1053 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1054 "Private zone id: %s\n", private_zone_id);
1057 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1061 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1062 "Shorten zone id: %s\n", shorten_zone_id);
1065 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1068 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1069 "ZONE_PUT_INTERVAL",
1070 &dht_max_update_interval))
1072 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1073 "DHT zone update interval: %d\n",
1074 dht_max_update_interval);
1077 max_record_put_interval = 1;
1080 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1081 "RECORD_PUT_INTERVAL",
1082 &max_record_put_interval))
1084 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1085 "Record put interval: %d\n",
1086 max_record_put_interval);
1090 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1091 "MAX_PARALLEL_BACKGROUND_QUERIES",
1092 &max_parallel_bg_queries))
1094 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1095 "Number of allowed parallel background queries: %d\n",
1096 max_parallel_bg_queries);
1100 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1101 "AUTO_IMPORT_CONFIRMATION_REQ"))
1103 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1104 "Auto import requires user confirmation\n");
1105 ignore_pending = GNUNET_YES;
1109 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1110 "DEFAULT_LOOKUP_TIMEOUT",
1111 &default_lookup_timeout_secs))
1113 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1114 "Default lookup timeout: %ds\n", default_lookup_timeout_secs);
1115 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1116 GNUNET_TIME_UNIT_SECONDS,
1117 default_lookup_timeout_secs);
1123 dht_handle = GNUNET_DHT_connect(c,
1124 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1127 if (NULL == dht_handle)
1129 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1132 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1133 max_parallel_bg_queries,
1137 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1138 "Unable to initialize resolver!\n");
1139 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1144 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1146 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1147 "DNS hijacking enabled... connecting to service.\n");
1149 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1151 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1152 "Failed to enable the dns interceptor!\n");
1157 * Schedule periodic put
1159 * We have roughly an hour for all records;
1161 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1163 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1165 GNUNET_SERVER_add_handlers (server, handlers);
1168 //GNUNET_SERVER_disconnect_notify (server,
1169 // &client_disconnect_notification,
1172 nc = GNUNET_SERVER_notification_context_create (server, 1);
1174 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1180 * The main function for the GNS service.
1182 * @param argc number of arguments from the command line
1183 * @param argv command line arguments
1184 * @return 0 ok, 1 on error
1187 main (int argc, char *const *argv)
1193 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1198 /* end of gnunet-service-gns.c */