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);
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,
212 /* Kill zone task for it may make the scheduler hang */
213 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
214 GNUNET_SCHEDULER_cancel(zone_update_taskid);
216 GNUNET_SERVER_notification_context_destroy (nc);
218 gns_interceptor_stop();
219 gns_resolver_cleanup(&on_resolver_cleanup);
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;
503 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
505 if ((rd_count == 1) &&
506 (remaining_time.rel_value != 0))
508 remaining_time = GNUNET_TIME_absolute_get_remaining (rd->expiration);
509 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
510 (remaining_time.rel_value != 0))
512 csh->shorten_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
515 gns_resolver_shorten_name (&csh->root_zone,
521 &send_shorten_response, csh);
526 process_private_zone_shorten (void *cls,
527 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
528 struct GNUNET_TIME_Absolute expiration,
530 unsigned int rd_count,
531 const struct GNUNET_NAMESTORE_RecordData *rd,
532 const struct GNUNET_CRYPTO_RsaSignature *signature)
534 struct GNUNET_TIME_Relative remaining_time;
535 struct ClientShortenHandle *csh = cls;
537 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
539 if ((rd_count == 1) &&
540 (remaining_time.rel_value != 0))
542 remaining_time = GNUNET_TIME_absolute_get_remaining (rd->expiration);
543 if ((rd->record_type == GNUNET_GNS_RECORD_PKEY) &&
544 (remaining_time.rel_value != 0))
548 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
549 "Looking for shorten zone in private zone\n");
550 csh->private_zone = *((struct GNUNET_CRYPTO_ShortHashCode*)rd->data);
551 GNUNET_NAMESTORE_lookup_record (namestore_handle,
554 GNUNET_GNS_RECORD_ANY,
555 &process_shorten_zone_shorten,
561 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
562 "No private zone found!\n");
563 gns_resolver_shorten_name (&csh->root_zone,
569 &send_shorten_response, csh);
574 * Lookup the zone infos and shorten name
576 * @param csh the shorten handle
580 start_shorten_name (struct ClientShortenHandle *csh)
582 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
583 "Looking for private zone in root zone\n");
585 GNUNET_NAMESTORE_lookup_record (namestore_handle,
588 GNUNET_GNS_RECORD_ANY,
589 &process_private_zone_shorten,
595 * Handle a shorten message from the api
597 * @param cls the closure
598 * @param client the client
599 * @param message the message
601 static void handle_shorten (void *cls,
602 struct GNUNET_SERVER_Client * client,
603 const struct GNUNET_MessageHeader * message)
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
608 struct ClientShortenHandle *csh;
609 char name[MAX_DNS_NAME_LENGTH];
610 char* nameptr = name;
612 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
615 GNUNET_SERVER_receive_done (client, GNUNET_OK);
620 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
621 (struct GNUNET_GNS_ClientShortenMessage *) message;
623 msg_size = ntohs(message->size);
625 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
632 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
633 csh->client = client;
634 csh->unique_id = sh_msg->id;
635 csh->shorten_key = NULL;
637 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
639 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
640 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
641 "SHORTEN: %s is too short", name);
642 send_shorten_response(csh, name);
646 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
647 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
648 "SHORTEN: %s is too long", name);
649 send_shorten_response(csh, name);
653 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "%s is not our domain. Returning\n", name);
657 send_shorten_response(csh, name);
661 strcpy (csh->name, name);
663 GNUNET_SERVER_notification_context_add (nc, client);
665 if (1 == ntohl(sh_msg->use_default_zone))
666 csh->root_zone = zone_hash; //Default zone
668 csh->root_zone = sh_msg->zone;
670 start_shorten_name (csh);
676 * Send get authority response back to client
678 * @param cls the closure containing a client get auth handle
679 * @param name the shortened name result or NULL if cannot be shortened
682 send_get_auth_response(void *cls, const char* name)
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
685 "GET_AUTH_RESULT", name);
686 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
687 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
694 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
697 rmsg->id = cah->unique_id;
698 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
700 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
703 strcpy((char*)&rmsg[1], name);
705 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
706 (const struct GNUNET_MessageHeader *) rmsg,
708 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
713 GNUNET_free_non_null(cah->name);
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
722 * Handle a get authority message from the api
724 * @param cls the closure
725 * @param client the client
726 * @param message the message
728 static void handle_get_authority(void *cls,
729 struct GNUNET_SERVER_Client * client,
730 const struct GNUNET_MessageHeader * message)
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
735 struct ClientGetAuthHandle *cah;
736 char name[MAX_DNS_NAME_LENGTH];
737 char* nameptr = name;
740 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
743 GNUNET_SERVER_receive_done (client, GNUNET_OK);
747 GNUNET_SERVER_notification_context_add (nc, client);
749 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
750 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
752 msg_size = ntohs(message->size);
754 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
761 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
764 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
765 cah->client = client;
766 cah->unique_id = sh_msg->id;
768 if (strlen(name) < strlen(GNUNET_GNS_TLD))
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "GET_AUTH: %s is too short. Returning\n", name);
773 send_get_auth_response(cah, name);
777 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
779 "GET_AUTH: %s is too long", name);
781 send_get_auth_response(cah, name);
785 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
786 GNUNET_GNS_TLD) != 0)
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "GET_AUTH: %s is not our domain. Returning\n", name);
791 send_get_auth_response(cah, name);
795 if (strcmp(name, GNUNET_GNS_TLD) == 0)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "GET_AUTH: %s is us. Returning\n", name);
800 send_get_auth_response(cah, name);
804 cah->name = GNUNET_malloc(strlen(name)
805 - strlen(GNUNET_GNS_TLD) + 1);
807 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
808 memcpy(cah->name, name,
809 strlen(name)-strlen(GNUNET_GNS_TLD));
811 /* Start delegation resolution in our namestore */
812 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
818 * Reply to client with the result from our lookup.
820 * @param cls the closure (our client lookup handle)
821 * @param rd_count the number of records
822 * @param rd the record data
825 send_lookup_response(void* cls,
827 const struct GNUNET_NAMESTORE_RecordData *rd)
829 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
830 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
834 "LOOKUP_RESULT", rd_count);
836 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
837 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
839 rmsg->id = clh->unique_id;
840 rmsg->rd_count = htonl(rd_count);
841 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
843 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
845 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
847 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
848 (const struct GNUNET_MessageHeader *) rmsg,
850 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
853 GNUNET_free(clh->name);
855 if (NULL != clh->shorten_key)
856 GNUNET_free(clh->shorten_key);
864 * Handle lookup requests from client
866 * @param cls the closure
867 * @param client the client
868 * @param message the message
871 handle_lookup(void *cls,
872 struct GNUNET_SERVER_Client * client,
873 const struct GNUNET_MessageHeader * message)
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
879 char name[MAX_DNS_NAME_LENGTH];
880 struct ClientLookupHandle *clh;
881 char* nameptr = name;
883 struct GNUNET_CRYPTO_RsaPrivateKey *key;
884 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
887 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
890 GNUNET_SERVER_receive_done (client, GNUNET_OK);
894 GNUNET_SERVER_notification_context_add (nc, client);
896 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
897 (struct GNUNET_GNS_ClientLookupMessage *) message;
899 msg_size = ntohs(message->size);
901 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
904 GNUNET_SERVER_receive_done (client, GNUNET_OK);
908 if (1 == ntohl(sh_msg->have_key))
910 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
911 tmp_pkey = (char*)&sh_msg[1];
912 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
913 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
918 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
921 namelen = strlen(name)+1;
922 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
923 clh->client = client;
924 clh->name = GNUNET_malloc(namelen);
925 strcpy(clh->name, name);
926 clh->unique_id = sh_msg->id;
927 clh->type = ntohl(sh_msg->type);
928 clh->shorten_key = key;
930 only_cached = ntohl(sh_msg->only_cached);
932 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
933 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
934 "LOOKUP: %s is too long", name);
936 send_lookup_response(clh, 0, NULL);
940 if (1 == ntohl(sh_msg->use_default_zone))
941 clh->zone = zone_hash; //Default zone
943 clh->zone = sh_msg->zone;
945 if (GNUNET_YES == auto_import_pkey)
947 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
949 default_lookup_timeout,
951 &send_lookup_response, clh);
955 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
957 default_lookup_timeout,
959 &send_lookup_response, clh);
966 * Process GNS requests.
968 * @param cls closure)
969 * @param server the initialized server
970 * @param c configuration to use
973 run (void *cls, struct GNUNET_SERVER_Handle *server,
974 const struct GNUNET_CONFIGURATION_Handle *c)
977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
980 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
981 unsigned long long max_parallel_bg_queries = 0;
982 unsigned long long default_lookup_timeout_secs = 0;
983 int ignore_pending = GNUNET_NO;
985 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
986 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
987 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
988 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
993 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
994 "ZONEKEY", &keyfile))
996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
997 "No private key for root zone specified!\n");
998 GNUNET_SCHEDULER_shutdown ();
1002 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1003 "Using keyfile %s for root zone.\n", keyfile);
1005 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1006 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1008 GNUNET_CRYPTO_short_hash(&pkey,
1009 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1011 GNUNET_free(keyfile);
1014 * handle to our local namestore
1016 namestore_handle = GNUNET_NAMESTORE_connect(c);
1018 if (NULL == namestore_handle)
1020 //FIXME do error handling;
1021 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1022 "Failed to connect to the namestore!\n");
1023 GNUNET_SCHEDULER_shutdown ();
1029 auto_import_pkey = GNUNET_NO;
1032 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1033 "AUTO_IMPORT_PKEY"))
1035 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1036 "Automatic PKEY import is enabled.\n");
1037 auto_import_pkey = GNUNET_YES;
1041 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1045 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1046 "Private zone id: %s\n", private_zone_id);
1049 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1053 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1054 "Shorten zone id: %s\n", shorten_zone_id);
1057 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
1060 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1061 "ZONE_PUT_INTERVAL",
1062 &dht_max_update_interval))
1064 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1065 "DHT zone update interval: %d\n",
1066 dht_max_update_interval);
1069 max_record_put_interval = 1;
1072 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1073 "RECORD_PUT_INTERVAL",
1074 &max_record_put_interval))
1076 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1077 "Record put interval: %d\n",
1078 max_record_put_interval);
1082 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1083 "MAX_PARALLEL_BACKGROUND_QUERIES",
1084 &max_parallel_bg_queries))
1086 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1087 "Number of allowed parallel background queries: %d\n",
1088 max_parallel_bg_queries);
1092 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1093 "AUTO_IMPORT_CONFIRMATION_REQ"))
1095 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1096 "Auto import requires user confirmation\n");
1097 ignore_pending = GNUNET_YES;
1101 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1102 "DEFAULT_LOOKUP_TIMEOUT",
1103 &default_lookup_timeout_secs))
1105 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1106 "Default lookup timeout: %ds\n", default_lookup_timeout_secs);
1107 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1108 GNUNET_TIME_UNIT_SECONDS,
1109 default_lookup_timeout_secs);
1115 dht_handle = GNUNET_DHT_connect(c,
1116 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1119 if (NULL == dht_handle)
1121 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1124 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash,
1125 max_parallel_bg_queries,
1129 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1130 "Unable to initialize resolver!\n");
1131 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1136 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1138 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1139 "DNS hijacking enabled... connecting to service.\n");
1141 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1143 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1144 "Failed to enable the dns interceptor!\n");
1149 * Schedule periodic put
1151 * We have roughly an hour for all records;
1153 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1155 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1157 GNUNET_SERVER_add_handlers (server, handlers);
1160 //GNUNET_SERVER_disconnect_notify (server,
1161 // &client_disconnect_notification,
1164 nc = GNUNET_SERVER_notification_context_create (server, 1);
1166 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1172 * The main function for the GNS service.
1174 * @param argc number of arguments from the command line
1175 * @param argv command line arguments
1176 * @return 0 ok, 1 on error
1179 main (int argc, char *const *argv)
1185 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1190 /* end of gnunet-service-gns.c */