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;
504 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
506 if ((rd_count == 1) &&
507 (remaining_time.rel_value != 0))
509 remaining_time = GNUNET_TIME_absolute_get_remaining (rd->expiration);
510 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
511 (remaining_time.rel_value != 0))
513 csh->shorten_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
516 gns_resolver_shorten_name (&csh->root_zone,
522 &send_shorten_response, csh);
527 process_private_zone_shorten (void *cls,
528 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
529 struct GNUNET_TIME_Absolute expiration,
531 unsigned int rd_count,
532 const struct GNUNET_NAMESTORE_RecordData *rd,
533 const struct GNUNET_CRYPTO_RsaSignature *signature)
535 struct GNUNET_TIME_Relative remaining_time;
536 struct ClientShortenHandle *csh = cls;
538 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
540 if ((rd_count == 1) &&
541 (remaining_time.rel_value != 0))
543 remaining_time = GNUNET_TIME_absolute_get_remaining (rd->expiration);
544 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
545 (remaining_time.rel_value != 0))
549 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
550 "Looking for shorten zone in private zone\n");
551 csh->private_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
552 GNUNET_NAMESTORE_lookup_record (namestore_handle,
555 GNUNET_GNS_RECORD_ANY,
556 &process_shorten_zone_shorten,
562 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
563 "No private zone found!\n");
564 gns_resolver_shorten_name (&csh->root_zone,
570 &send_shorten_response, csh);
575 * Lookup the zone infos and shorten name
577 * @param csh the shorten handle
581 start_shorten_name (struct ClientShortenHandle *csh)
583 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
584 "Looking for private zone in root zone\n");
586 GNUNET_NAMESTORE_lookup_record (namestore_handle,
589 GNUNET_GNS_RECORD_ANY,
590 &process_private_zone_shorten,
596 * Handle a shorten message from the api
598 * @param cls the closure
599 * @param client the client
600 * @param message the message
602 static void handle_shorten (void *cls,
603 struct GNUNET_SERVER_Client * client,
604 const struct GNUNET_MessageHeader * message)
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
609 struct ClientShortenHandle *csh;
610 char name[MAX_DNS_NAME_LENGTH];
611 char* nameptr = name;
613 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
616 GNUNET_SERVER_receive_done (client, GNUNET_OK);
621 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
622 (struct GNUNET_GNS_ClientShortenMessage *) message;
624 msg_size = ntohs(message->size);
626 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
629 GNUNET_SERVER_receive_done (client, GNUNET_OK);
633 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
634 csh->client = client;
635 csh->unique_id = sh_msg->id;
636 csh->shorten_key = NULL;
638 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
640 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
642 "SHORTEN: %s is too short", name);
643 send_shorten_response(csh, name);
647 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
648 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
649 "SHORTEN: %s is too long", name);
650 send_shorten_response(csh, name);
654 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "%s is not our domain. Returning\n", name);
658 send_shorten_response(csh, name);
662 strcpy (csh->name, name);
664 GNUNET_SERVER_notification_context_add (nc, client);
666 if (1 == ntohl(sh_msg->use_default_zone))
667 csh->root_zone = zone_hash; //Default zone
669 csh->root_zone = sh_msg->zone;
671 start_shorten_name (csh);
677 * Send get authority response back to client
679 * @param cls the closure containing a client get auth handle
680 * @param name the shortened name result or NULL if cannot be shortened
683 send_get_auth_response(void *cls, const char* name)
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
686 "GET_AUTH_RESULT", name);
687 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
688 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
695 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
698 rmsg->id = cah->unique_id;
699 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
701 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
704 strcpy((char*)&rmsg[1], name);
706 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
707 (const struct GNUNET_MessageHeader *) rmsg,
709 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
714 GNUNET_free_non_null(cah->name);
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
723 * Handle a get authority message from the api
725 * @param cls the closure
726 * @param client the client
727 * @param message the message
729 static void handle_get_authority(void *cls,
730 struct GNUNET_SERVER_Client * client,
731 const struct GNUNET_MessageHeader * message)
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
736 struct ClientGetAuthHandle *cah;
737 char name[MAX_DNS_NAME_LENGTH];
738 char* nameptr = name;
741 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
748 GNUNET_SERVER_notification_context_add (nc, client);
750 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
751 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
753 msg_size = ntohs(message->size);
755 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
758 GNUNET_SERVER_receive_done (client, GNUNET_OK);
762 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
765 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
766 cah->client = client;
767 cah->unique_id = sh_msg->id;
769 if (strlen(name) < strlen(GNUNET_GNS_TLD))
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "GET_AUTH: %s is too short. Returning\n", name);
774 send_get_auth_response(cah, name);
778 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
779 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
780 "GET_AUTH: %s is too long", name);
782 send_get_auth_response(cah, name);
786 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
787 GNUNET_GNS_TLD) != 0)
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "GET_AUTH: %s is not our domain. Returning\n", name);
792 send_get_auth_response(cah, name);
796 if (strcmp(name, GNUNET_GNS_TLD) == 0)
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "GET_AUTH: %s is us. Returning\n", name);
801 send_get_auth_response(cah, name);
805 cah->name = GNUNET_malloc(strlen(name)
806 - strlen(GNUNET_GNS_TLD) + 1);
808 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
809 memcpy(cah->name, name,
810 strlen(name)-strlen(GNUNET_GNS_TLD));
812 /* Start delegation resolution in our namestore */
813 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
819 * Reply to client with the result from our lookup.
821 * @param cls the closure (our client lookup handle)
822 * @param rd_count the number of records
823 * @param rd the record data
826 send_lookup_response(void* cls,
828 const struct GNUNET_NAMESTORE_RecordData *rd)
830 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
831 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
835 "LOOKUP_RESULT", rd_count);
837 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
838 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
840 rmsg->id = clh->unique_id;
841 rmsg->rd_count = htonl(rd_count);
842 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
844 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
846 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
848 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
849 (const struct GNUNET_MessageHeader *) rmsg,
851 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
854 GNUNET_free(clh->name);
856 if (NULL != clh->shorten_key)
857 GNUNET_free(clh->shorten_key);
865 * Handle lookup requests from client
867 * @param cls the closure
868 * @param client the client
869 * @param message the message
872 handle_lookup(void *cls,
873 struct GNUNET_SERVER_Client * client,
874 const struct GNUNET_MessageHeader * message)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
880 char name[MAX_DNS_NAME_LENGTH];
881 struct ClientLookupHandle *clh;
882 char* nameptr = name;
884 struct GNUNET_CRYPTO_RsaPrivateKey *key;
885 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
888 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
891 GNUNET_SERVER_receive_done (client, GNUNET_OK);
895 GNUNET_SERVER_notification_context_add (nc, client);
897 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
898 (struct GNUNET_GNS_ClientLookupMessage *) message;
900 msg_size = ntohs(message->size);
902 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
905 GNUNET_SERVER_receive_done (client, GNUNET_OK);
909 if (1 == ntohl(sh_msg->have_key))
911 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
912 tmp_pkey = (char*)&sh_msg[1];
913 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
914 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
919 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
922 namelen = strlen(name)+1;
923 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
924 clh->client = client;
925 clh->name = GNUNET_malloc(namelen);
926 strcpy(clh->name, name);
927 clh->unique_id = sh_msg->id;
928 clh->type = ntohl(sh_msg->type);
929 clh->shorten_key = key;
931 only_cached = ntohl(sh_msg->only_cached);
933 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
934 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
935 "LOOKUP: %s is too long", name);
937 send_lookup_response(clh, 0, NULL);
941 if (1 == ntohl(sh_msg->use_default_zone))
942 clh->zone = zone_hash; //Default zone
944 clh->zone = sh_msg->zone;
946 if (GNUNET_YES == auto_import_pkey)
948 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
950 default_lookup_timeout,
952 &send_lookup_response, clh);
956 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
958 default_lookup_timeout,
960 &send_lookup_response, clh);
967 * Process GNS requests.
969 * @param cls closure)
970 * @param server the initialized server
971 * @param c configuration to use
974 run (void *cls, struct GNUNET_SERVER_Handle *server,
975 const struct GNUNET_CONFIGURATION_Handle *c)
978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
981 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
982 unsigned long long max_parallel_bg_queries = 0;
983 unsigned long long default_lookup_timeout_secs = 0;
984 int ignore_pending = GNUNET_NO;
986 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
987 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
988 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
989 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
994 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
995 "ZONEKEY", &keyfile))
997 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
998 "No private key for root zone specified!\n");
999 GNUNET_SCHEDULER_shutdown ();
1003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1004 "Using keyfile %s for root zone.\n", keyfile);
1006 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1007 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1009 GNUNET_CRYPTO_short_hash(&pkey,
1010 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1012 GNUNET_free(keyfile);
1015 * handle to our local namestore
1017 namestore_handle = GNUNET_NAMESTORE_connect(c);
1019 if (NULL == namestore_handle)
1021 //FIXME do error handling;
1022 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1023 "Failed to connect to the namestore!\n");
1024 GNUNET_SCHEDULER_shutdown ();
1030 auto_import_pkey = GNUNET_NO;
1033 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1034 "AUTO_IMPORT_PKEY"))
1036 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1037 "Automatic PKEY import is enabled.\n");
1038 auto_import_pkey = GNUNET_YES;
1042 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1046 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1047 "Private zone id: %s\n", private_zone_id);
1050 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1054 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1055 "Shorten zone id: %s\n", shorten_zone_id);
1058 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1061 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1062 "ZONE_PUT_INTERVAL",
1063 &dht_max_update_interval))
1065 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1066 "DHT zone update interval: %d\n",
1067 dht_max_update_interval);
1070 max_record_put_interval = 1;
1073 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1074 "RECORD_PUT_INTERVAL",
1075 &max_record_put_interval))
1077 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1078 "Record put interval: %d\n",
1079 max_record_put_interval);
1083 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1084 "MAX_PARALLEL_BACKGROUND_QUERIES",
1085 &max_parallel_bg_queries))
1087 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1088 "Number of allowed parallel background queries: %d\n",
1089 max_parallel_bg_queries);
1093 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1094 "AUTO_IMPORT_CONFIRMATION_REQ"))
1096 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1097 "Auto import requires user confirmation\n");
1098 ignore_pending = GNUNET_YES;
1102 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1103 "DEFAULT_LOOKUP_TIMEOUT",
1104 &default_lookup_timeout_secs))
1106 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1107 "Default lookup timeout: %ds\n", default_lookup_timeout_secs);
1108 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1109 GNUNET_TIME_UNIT_SECONDS,
1110 default_lookup_timeout_secs);
1116 dht_handle = GNUNET_DHT_connect(c,
1117 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1120 if (NULL == dht_handle)
1122 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1125 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1126 max_parallel_bg_queries,
1130 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1131 "Unable to initialize resolver!\n");
1132 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1137 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1139 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1140 "DNS hijacking enabled... connecting to service.\n");
1142 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1144 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1145 "Failed to enable the dns interceptor!\n");
1150 * Schedule periodic put
1152 * We have roughly an hour for all records;
1154 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1156 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1158 GNUNET_SERVER_add_handlers (server, handlers);
1161 //GNUNET_SERVER_disconnect_notify (server,
1162 // &client_disconnect_notification,
1165 nc = GNUNET_SERVER_notification_context_create (server, 1);
1167 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1173 * The main function for the GNS service.
1175 * @param argc number of arguments from the command line
1176 * @param argv command line arguments
1177 * @return 0 ok, 1 on error
1180 main (int argc, char *const *argv)
1186 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1191 /* end of gnunet-service-gns.c */