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);
197 GNUNET_DHT_disconnect(dht_handle);
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);
225 * Method called periodicattluy that triggers
226 * iteration over root zone
229 * @param tc task context
232 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
234 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
235 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
239 * Continuation for DHT put
242 * @param success GNUNET_OK if the PUT was transmitted,
243 * GNUNET_NO on timeout,
244 * GNUNET_SYSERR on disconnect from service
245 * after the PUT message was transmitted
246 * (so we don't know if it was received or not)
249 record_dht_put(void *cls, int success)
251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
256 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
259 * Function used to put all records successively into the DHT.
261 * @param cls the closure (NULL)
262 * @param key the public key of the authority (ours)
263 * @param expiration lifetime of the namestore entry
264 * @param name the name of the records
265 * @param rd_count the number of records in data
266 * @param rd the record data
267 * @param signature the signature for the record data
270 put_gns_record(void *cls,
271 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
272 struct GNUNET_TIME_Absolute expiration,
274 unsigned int rd_count,
275 const struct GNUNET_NAMESTORE_RecordData *rd,
276 const struct GNUNET_CRYPTO_RsaSignature *signature)
279 struct GNSNameRecordBlock *nrb;
280 struct GNUNET_CRYPTO_ShortHashCode name_hash;
281 struct GNUNET_CRYPTO_ShortHashCode zhash;
282 struct GNUNET_HashCode xor_hash;
283 struct GNUNET_HashCode name_hash_double;
284 struct GNUNET_HashCode zone_hash_double;
285 uint32_t rd_payload_length;
286 char* nrb_data = NULL;
292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
293 "Zone iteration finished. Rescheduling put in %ds\n",
294 dht_max_update_interval);
295 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
296 GNUNET_TIME_relative_multiply(
297 GNUNET_TIME_UNIT_SECONDS,
298 dht_max_update_interval
300 &update_zone_dht_start,
305 namelen = strlen(name) + 1;
307 if (signature == NULL)
309 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
310 "No signature for %s record data provided! Skipping...\n",
312 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
319 "Putting records for %s into the DHT\n", name);
321 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
323 nrb = GNUNET_malloc(rd_payload_length + namelen
324 + sizeof(struct GNSNameRecordBlock));
326 nrb->signature = *signature;
328 nrb->public_key = *key;
330 nrb->rd_count = htonl(rd_count);
332 memcpy(&nrb[1], name, namelen);
334 nrb_data = (char*)&nrb[1];
337 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
339 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
344 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
345 "Record serialization failed! Skipping...\n");
347 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
354 * calculate DHT key: H(name) xor H(pubkey)
356 GNUNET_CRYPTO_short_hash(key,
357 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
359 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
360 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
361 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
362 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
365 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
368 "putting records for %s under key: %s with size %d\n",
369 name, GNUNET_h2s (&xor_hash), rd_payload_length);
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
372 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
373 /* FIXME: keep return value to possibly cancel? */
374 GNUNET_DHT_put (dht_handle, &xor_hash,
375 DHT_GNS_REPLICATION_LEVEL,
377 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
381 DHT_OPERATION_TIMEOUT,
383 NULL); //cls for cont
385 num_public_records++;
388 * Reschedule periodic put
390 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
391 &update_zone_dht_next,
399 * Periodically iterate over our zone and store everything in dht
402 * @param tc task context
405 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
407 unsigned long long interval = 0;
409 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
412 if (0 == num_public_records)
415 * If no records are known (startup) or none present
416 * we can safely set the interval to 1s
418 record_put_interval = GNUNET_TIME_relative_multiply(
419 GNUNET_TIME_UNIT_SECONDS,
421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
422 "No records in db. Adjusted record put interval to 1s\n");
426 interval = max_record_put_interval/num_public_records;
429 record_put_interval = GNUNET_TIME_relative_multiply(
430 GNUNET_TIME_UNIT_SECONDS,
432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
433 "Adjusted DHT update interval to %ds!\n",
437 /* start counting again */
438 num_public_records = 0;
439 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
441 GNUNET_NAMESTORE_RF_AUTHORITY,
442 GNUNET_NAMESTORE_RF_PRIVATE,
447 /* END DHT ZONE PROPAGATION */
450 * Send shorten response back to client
452 * @param cls the closure containing a client shorten handle
453 * @param name the shortened name result or NULL if cannot be shortened
456 send_shorten_response(void* cls, const char* name)
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
459 "SHORTEN_RESULT", name);
460 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
461 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
468 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
471 rmsg->id = csh->unique_id;
472 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
474 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
477 strcpy((char*)&rmsg[1], name);
479 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
480 (const struct GNUNET_MessageHeader *) rmsg,
482 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
485 GNUNET_free_non_null(csh->shorten_key);
492 process_shorten_zone_shorten (void *cls,
493 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
494 struct GNUNET_TIME_Absolute expiration,
496 unsigned int rd_count,
497 const struct GNUNET_NAMESTORE_RecordData *rd,
498 const struct GNUNET_CRYPTO_RsaSignature *signature)
500 struct ClientShortenHandle *csh = cls;
501 struct GNUNET_TIME_Relative remaining_time;
502 struct GNUNET_TIME_Absolute et;
504 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
506 if ((rd_count == 1) &&
507 (remaining_time.rel_value != 0))
509 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
510 et.abs_value = rd->expiration_time;
511 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
512 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
513 (remaining_time.rel_value != 0))
515 csh->shorten_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
518 gns_resolver_shorten_name (&csh->root_zone,
524 &send_shorten_response, csh);
530 process_private_zone_shorten (void *cls,
531 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
532 struct GNUNET_TIME_Absolute expiration,
534 unsigned int rd_count,
535 const struct GNUNET_NAMESTORE_RecordData *rd,
536 const struct GNUNET_CRYPTO_RsaSignature *signature)
538 struct GNUNET_TIME_Relative remaining_time;
539 struct ClientShortenHandle *csh = cls;
540 struct GNUNET_TIME_Absolute et;
542 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
544 if ((rd_count == 1) &&
545 (remaining_time.rel_value != 0))
547 GNUNET_break (0 == (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
548 et.abs_value = rd->expiration_time;
549 remaining_time = GNUNET_TIME_absolute_get_remaining (et);
550 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
551 (remaining_time.rel_value != 0))
555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
556 "Looking for shorten zone in private zone\n");
557 csh->private_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
558 GNUNET_NAMESTORE_lookup_record (namestore_handle,
561 GNUNET_GNS_RECORD_ANY,
562 &process_shorten_zone_shorten,
568 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
569 "No private zone found!\n");
570 gns_resolver_shorten_name (&csh->root_zone,
576 &send_shorten_response, csh);
581 * Lookup the zone infos and shorten name
583 * @param csh the shorten handle
587 start_shorten_name (struct ClientShortenHandle *csh)
589 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
590 "Looking for private zone in root zone\n");
592 GNUNET_NAMESTORE_lookup_record (namestore_handle,
595 GNUNET_GNS_RECORD_ANY,
596 &process_private_zone_shorten,
602 * Handle a shorten message from the api
604 * @param cls the closure
605 * @param client the client
606 * @param message the message
608 static void handle_shorten (void *cls,
609 struct GNUNET_SERVER_Client * client,
610 const struct GNUNET_MessageHeader * message)
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
615 struct ClientShortenHandle *csh;
616 char name[MAX_DNS_NAME_LENGTH];
617 char* nameptr = name;
619 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
627 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
628 (struct GNUNET_GNS_ClientShortenMessage *) message;
630 msg_size = ntohs(message->size);
632 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
635 GNUNET_SERVER_receive_done (client, GNUNET_OK);
639 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
640 csh->client = client;
641 csh->unique_id = sh_msg->id;
642 csh->shorten_key = NULL;
644 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
646 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
647 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
648 "SHORTEN: %s is too short", name);
649 send_shorten_response(csh, name);
653 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
654 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
655 "SHORTEN: %s is too long", name);
656 send_shorten_response(csh, name);
660 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "%s is not our domain. Returning\n", name);
664 send_shorten_response(csh, name);
668 strcpy (csh->name, name);
670 GNUNET_SERVER_notification_context_add (nc, client);
672 if (1 == ntohl(sh_msg->use_default_zone))
673 csh->root_zone = zone_hash; //Default zone
675 csh->root_zone = sh_msg->zone;
677 start_shorten_name (csh);
683 * Send get authority response back to client
685 * @param cls the closure containing a client get auth handle
686 * @param name the shortened name result or NULL if cannot be shortened
689 send_get_auth_response(void *cls, const char* name)
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
692 "GET_AUTH_RESULT", name);
693 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
694 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
701 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
704 rmsg->id = cah->unique_id;
705 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
707 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
710 strcpy((char*)&rmsg[1], name);
712 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
713 (const struct GNUNET_MessageHeader *) rmsg,
715 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
720 GNUNET_free_non_null(cah->name);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
729 * Handle a get authority message from the api
731 * @param cls the closure
732 * @param client the client
733 * @param message the message
735 static void handle_get_authority(void *cls,
736 struct GNUNET_SERVER_Client * client,
737 const struct GNUNET_MessageHeader * message)
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
742 struct ClientGetAuthHandle *cah;
743 char name[MAX_DNS_NAME_LENGTH];
744 char* nameptr = name;
747 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
750 GNUNET_SERVER_receive_done (client, GNUNET_OK);
754 GNUNET_SERVER_notification_context_add (nc, client);
756 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
757 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
759 msg_size = ntohs(message->size);
761 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
764 GNUNET_SERVER_receive_done (client, GNUNET_OK);
768 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
771 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
772 cah->client = client;
773 cah->unique_id = sh_msg->id;
775 if (strlen(name) < strlen(GNUNET_GNS_TLD))
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "GET_AUTH: %s is too short. Returning\n", name);
780 send_get_auth_response(cah, name);
784 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
786 "GET_AUTH: %s is too long", name);
788 send_get_auth_response(cah, name);
792 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
793 GNUNET_GNS_TLD) != 0)
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "GET_AUTH: %s is not our domain. Returning\n", name);
798 send_get_auth_response(cah, name);
802 if (strcmp(name, GNUNET_GNS_TLD) == 0)
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "GET_AUTH: %s is us. Returning\n", name);
807 send_get_auth_response(cah, name);
811 cah->name = GNUNET_malloc(strlen(name)
812 - strlen(GNUNET_GNS_TLD) + 1);
814 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
815 memcpy(cah->name, name,
816 strlen(name)-strlen(GNUNET_GNS_TLD));
818 /* Start delegation resolution in our namestore */
819 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
825 * Reply to client with the result from our lookup.
827 * @param cls the closure (our client lookup handle)
828 * @param rd_count the number of records
829 * @param rd the record data
832 send_lookup_response(void* cls,
834 const struct GNUNET_NAMESTORE_RecordData *rd)
836 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
837 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
841 "LOOKUP_RESULT", rd_count);
843 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
844 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
846 rmsg->id = clh->unique_id;
847 rmsg->rd_count = htonl(rd_count);
848 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
850 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
852 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
854 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
855 (const struct GNUNET_MessageHeader *) rmsg,
857 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
860 GNUNET_free(clh->name);
862 if (NULL != clh->shorten_key)
863 GNUNET_free(clh->shorten_key);
871 * Handle lookup requests from client
873 * @param cls the closure
874 * @param client the client
875 * @param message the message
878 handle_lookup(void *cls,
879 struct GNUNET_SERVER_Client * client,
880 const struct GNUNET_MessageHeader * message)
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
886 char name[MAX_DNS_NAME_LENGTH];
887 struct ClientLookupHandle *clh;
888 char* nameptr = name;
890 struct GNUNET_CRYPTO_RsaPrivateKey *key;
891 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
894 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
901 GNUNET_SERVER_notification_context_add (nc, client);
903 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
904 (struct GNUNET_GNS_ClientLookupMessage *) message;
906 msg_size = ntohs(message->size);
908 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
911 GNUNET_SERVER_receive_done (client, GNUNET_OK);
915 if (1 == ntohl(sh_msg->have_key))
917 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
918 tmp_pkey = (char*)&sh_msg[1];
919 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
920 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
925 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
928 namelen = strlen(name)+1;
929 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
930 clh->client = client;
931 clh->name = GNUNET_malloc(namelen);
932 strcpy(clh->name, name);
933 clh->unique_id = sh_msg->id;
934 clh->type = ntohl(sh_msg->type);
935 clh->shorten_key = key;
937 only_cached = ntohl(sh_msg->only_cached);
939 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
940 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
941 "LOOKUP: %s is too long", name);
943 send_lookup_response(clh, 0, NULL);
947 if (1 == ntohl(sh_msg->use_default_zone))
948 clh->zone = zone_hash; //Default zone
950 clh->zone = sh_msg->zone;
952 if (GNUNET_YES == auto_import_pkey)
954 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
956 default_lookup_timeout,
958 &send_lookup_response, clh);
962 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
964 default_lookup_timeout,
966 &send_lookup_response, clh);
973 * Process GNS requests.
975 * @param cls closure)
976 * @param server the initialized server
977 * @param c configuration to use
980 run (void *cls, struct GNUNET_SERVER_Handle *server,
981 const struct GNUNET_CONFIGURATION_Handle *c)
984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
987 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
988 unsigned long long max_parallel_bg_queries = 0;
989 unsigned long long default_lookup_timeout_secs = 0;
990 int ignore_pending = GNUNET_NO;
992 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
993 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
994 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
995 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1000 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1001 "ZONEKEY", &keyfile))
1003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1004 "No private key for root zone specified!\n");
1005 GNUNET_SCHEDULER_shutdown ();
1009 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1010 "Using keyfile %s for root zone.\n", keyfile);
1012 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1013 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1015 GNUNET_CRYPTO_short_hash(&pkey,
1016 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1018 GNUNET_free(keyfile);
1021 * handle to our local namestore
1023 namestore_handle = GNUNET_NAMESTORE_connect(c);
1025 if (NULL == namestore_handle)
1027 //FIXME do error handling;
1028 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1029 "Failed to connect to the namestore!\n");
1030 GNUNET_SCHEDULER_shutdown ();
1036 auto_import_pkey = GNUNET_NO;
1039 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1040 "AUTO_IMPORT_PKEY"))
1042 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1043 "Automatic PKEY import is enabled.\n");
1044 auto_import_pkey = GNUNET_YES;
1048 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1052 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1053 "Private zone id: %s\n", private_zone_id);
1056 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1060 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1061 "Shorten zone id: %s\n", shorten_zone_id);
1064 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1067 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1068 "ZONE_PUT_INTERVAL",
1069 &dht_max_update_interval))
1071 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1072 "DHT zone update interval: %d\n",
1073 dht_max_update_interval);
1076 max_record_put_interval = 1;
1079 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1080 "RECORD_PUT_INTERVAL",
1081 &max_record_put_interval))
1083 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1084 "Record put interval: %d\n",
1085 max_record_put_interval);
1089 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1090 "MAX_PARALLEL_BACKGROUND_QUERIES",
1091 &max_parallel_bg_queries))
1093 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1094 "Number of allowed parallel background queries: %d\n",
1095 max_parallel_bg_queries);
1099 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1100 "AUTO_IMPORT_CONFIRMATION_REQ"))
1102 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1103 "Auto import requires user confirmation\n");
1104 ignore_pending = GNUNET_YES;
1108 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1109 "DEFAULT_LOOKUP_TIMEOUT",
1110 &default_lookup_timeout_secs))
1112 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1113 "Default lookup timeout: %ds\n", default_lookup_timeout_secs);
1114 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1115 GNUNET_TIME_UNIT_SECONDS,
1116 default_lookup_timeout_secs);
1122 dht_handle = GNUNET_DHT_connect(c,
1123 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1126 if (NULL == dht_handle)
1128 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1131 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1132 max_parallel_bg_queries,
1136 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1137 "Unable to initialize resolver!\n");
1138 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1143 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1145 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1146 "DNS hijacking enabled... connecting to service.\n");
1148 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1150 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1151 "Failed to enable the dns interceptor!\n");
1156 * Schedule periodic put
1158 * We have roughly an hour for all records;
1160 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1162 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1164 GNUNET_SERVER_add_handlers (server, handlers);
1167 //GNUNET_SERVER_disconnect_notify (server,
1168 // &client_disconnect_notification,
1171 nc = GNUNET_SERVER_notification_context_create (server, 1);
1173 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1179 * The main function for the GNS service.
1181 * @param argc number of arguments from the command line
1182 * @param argv command line arguments
1183 * @return 0 ok, 1 on error
1186 main (int argc, char *const *argv)
1192 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1197 /* end of gnunet-service-gns.c */