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.
24 * - Write xquery and block plugin
25 * - The smaller FIXME issues all around
27 * @file gns/gnunet-service-gns.c
28 * @brief GNUnet GNS service
29 * @author Martin Schanzenbach
32 #include "gnunet_util_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_gns_service.h"
39 #include "block_gns.h"
42 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
46 /* Ignore for now not used anyway and probably never will */
47 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
48 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
51 * Handle to a currenty pending resolution
53 struct GNUNET_GNS_ResolverHandle
55 /* The name to resolve */
58 /* the request handle to reply to */
59 struct GNUNET_DNS_RequestHandle *request_handle;
61 /* the dns parser packet received */
62 struct GNUNET_DNSPARSER_Packet *packet;
64 /* the query parsed from the packet */
66 struct GNUNET_DNSPARSER_Query *query;
68 /* has this query been answered? how many matches */
71 /* the authoritative zone to query */
72 GNUNET_HashCode authority;
74 /* the name of the authoritative zone to query */
78 * we have an authority in namestore that
79 * may be able to resolve
83 /* a handle for dht lookups. should be NULL if no lookups are in progress */
84 struct GNUNET_DHT_GetHandle *get_handle;
86 /* timeout task for dht lookups */
87 GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
93 * Our handle to the DNS handler library
95 struct GNUNET_DNS_Handle *dns_handle;
98 * Our handle to the DHT
100 struct GNUNET_DHT_Handle *dht_handle;
103 * Our zone's private key
105 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
108 * Our handle to the namestore service
109 * FIXME maybe need a second handle for iteration
111 struct GNUNET_NAMESTORE_Handle *namestore_handle;
114 * Handle to iterate over our authoritative zone in namestore
116 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
119 * The configuration the GNS service is running with
121 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
124 * Our notification context.
126 static struct GNUNET_SERVER_NotificationContext *nc;
131 GNUNET_HashCode zone_hash;
134 * Our tld. Maybe get from config file
136 const char* gnunet_tld = ".gnunet";
139 * Useful for zone update for DHT put
141 static int num_public_records = 3600;
142 struct GNUNET_TIME_Relative dht_update_interval;
143 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
146 static void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer,
148 const struct GNUNET_NAMESTORE_RecordData *rd);
149 static void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
152 * Task run during shutdown.
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 /* Kill zone task for it may make the scheduler hang */
161 if (zone_update_taskid)
162 GNUNET_SCHEDULER_cancel(zone_update_taskid);
164 GNUNET_DNS_disconnect(dns_handle);
165 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
166 GNUNET_DHT_disconnect(dht_handle);
170 * Callback when record data is put into namestore
172 * @param cls the closure
173 * @param success GNUNET_OK on success
174 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
177 on_namestore_record_put_result(void *cls,
181 if (GNUNET_NO == success)
183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
186 else if (GNUNET_YES == success)
188 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
189 "records successfully put in namestore\n");
193 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
194 "Error putting records into namestore: %s\n", emsg);
198 * Handle timeout for DHT requests
200 * @param cls the request handle as closure
201 * @param tc the task context
204 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
206 struct GNUNET_GNS_ResolverHandle *rh = cls;
208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
209 "dht lookup for query %s (type=%d) timed out.\n",
210 rh->name, rh->query->type);
212 GNUNET_DHT_get_stop (rh->get_handle);
213 reply_to_dns(rh, 0, NULL);
217 * Function called when we get a result from the dht
220 * @param cls the request handle
221 * @param exp lifetime
222 * @param key the key the record was stored under
223 * @param get_path get path
224 * @param get_path_length get path length
225 * @param put_path put path
226 * @param put_path_length put path length
227 * @param type the block type
228 * @param size the size of the record
229 * @param data the record data
232 process_authority_dht_result(void* cls,
233 struct GNUNET_TIME_Absolute exp,
234 const GNUNET_HashCode * key,
235 const struct GNUNET_PeerIdentity *get_path,
236 unsigned int get_path_length,
237 const struct GNUNET_PeerIdentity *put_path,
238 unsigned int put_path_length,
239 enum GNUNET_BLOCK_Type type,
240 size_t size, const void *data)
242 struct GNUNET_GNS_ResolverHandle *rh;
243 struct GNSNameRecordBlock *nrb;
244 uint32_t num_records;
246 char* rd_data = (char*) data;
249 GNUNET_HashCode zone, name_hash;
254 //FIXME check expiration?
256 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
257 nrb = (struct GNSNameRecordBlock*)data;
259 /* stop dht lookup and timeout task */
260 GNUNET_DHT_get_stop (rh->get_handle);
261 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
263 rh->get_handle = NULL;
264 num_records = ntohl(nrb->rd_count);
265 name = (char*)&nrb[1];
267 struct GNUNET_NAMESTORE_RecordData rd[num_records];
269 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
270 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
272 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
277 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
282 "Got name: %s (wanted %s)\n", name, rh->authority_name);
283 for (i=0; i<num_records; i++)
286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
287 "Got name: %s (wanted %s)\n", name, rh->authority_name);
288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
289 "Got type: %d (wanted %d)\n",
290 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
292 "Got data length: %d\n", rd[i].data_size);
293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
294 "Got flag %d\n", rd[i].flags);
296 if ((strcmp(name, rh->authority_name) == 0) &&
297 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
299 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
302 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
310 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
311 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
313 /* Save to namestore */
314 if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
316 GNUNET_NAMESTORE_record_put (namestore_handle,
323 &on_namestore_record_put_result, //cont
334 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No authority in records\n");
335 reply_to_dns(rh, 0, NULL);
339 * Start DHT lookup for a name -> PKEY (compare NS) record in
340 * query->authority's zone
342 * @param rh the pending gns query
343 * @param name the name of the PKEY record
346 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh)
349 GNUNET_HashCode name_hash;
350 GNUNET_HashCode lookup_key;
352 GNUNET_CRYPTO_hash(rh->authority_name,
353 strlen(rh->authority_name),
355 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
357 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
358 &dht_lookup_timeout, rh);
360 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
361 //FIXME how long to wait for results?
362 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
363 DHT_OPERATION_TIMEOUT,
364 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
366 DHT_GNS_REPLICATION_LEVEL,
370 &process_authority_dht_result,
376 * Function called when we get a result from the dht
379 * @param cls the request handle
380 * @param exp lifetime
381 * @param key the key the record was stored under
382 * @param get_path get path
383 * @param get_path_length get path length
384 * @param put_path put path
385 * @param put_path_length put path length
386 * @param type the block type
387 * @param size the size of the record
388 * @param data the record data
391 process_name_dht_result(void* cls,
392 struct GNUNET_TIME_Absolute exp,
393 const GNUNET_HashCode * key,
394 const struct GNUNET_PeerIdentity *get_path,
395 unsigned int get_path_length,
396 const struct GNUNET_PeerIdentity *put_path,
397 unsigned int put_path_length,
398 enum GNUNET_BLOCK_Type type,
399 size_t size, const void *data)
401 struct GNUNET_GNS_ResolverHandle *rh;
402 struct GNSNameRecordBlock *nrb;
403 uint32_t num_records;
405 char* rd_data = (char*)data;
409 GNUNET_HashCode zone, name_hash;
410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
415 //FIXME maybe check expiration here, check block type
417 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
418 nrb = (struct GNSNameRecordBlock*)data;
420 /* stop lookup and timeout task */
421 GNUNET_DHT_get_stop (rh->get_handle);
422 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
424 rh->get_handle = NULL;
425 name = (char*)&nrb[1];
426 num_records = ntohl(nrb->rd_count);
428 struct GNUNET_NAMESTORE_RecordData rd[num_records];
430 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
431 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
433 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
438 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
442 for (i=0; i<num_records; i++)
444 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
445 "Got name: %s (wanted %s)\n", name, rh->name);
446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
447 "Got type: %d (wanted %d)\n",
448 rd[i].record_type, rh->query->type);
449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
450 "Got data length: %d\n", rd[i].data_size);
451 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
452 "Got flag %d\n", rd[i].flags);
454 if ((strcmp(name, rh->name) == 0) &&
455 (rd[i].record_type == rh->query->type))
462 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
463 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
466 * FIXME check pubkey against existing key in namestore?
467 * https://gnunet.org/bugs/view.php?id=2179
470 /* Save to namestore */
471 GNUNET_NAMESTORE_record_put (namestore_handle,
478 &on_namestore_record_put_result, //cont
482 reply_to_dns(rh, num_records, rd);
484 reply_to_dns(rh, 0, NULL);
493 * Start DHT lookup for a (name -> query->record_type) record in
494 * query->authority's zone
496 * @param rh the pending gns query context
497 * @param name the name to query record
500 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
503 GNUNET_HashCode name_hash;
504 GNUNET_HashCode lookup_key;
505 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
510 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
511 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
512 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
515 "starting dht lookup for %s with key: %s\n",
516 name, (char*)&lookup_key_string);
518 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
519 &dht_lookup_timeout, rh);
521 xquery = htonl(rh->query->type);
522 //FIXME how long to wait for results?
523 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
524 DHT_OPERATION_TIMEOUT,
525 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
527 DHT_GNS_REPLICATION_LEVEL,
531 &process_name_dht_result,
538 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
541 * This is a callback function that should give us only PKEY
542 * records. Used to query the namestore for the authority (PKEY)
545 * @param cls the pending query
546 * @param key the key of the zone we did the lookup
547 * @param expiration expiration date of the record data set in the namestore
548 * @param name the name for which we need an authority
549 * @param rd_count the number of records with 'name'
550 * @param rd the record data
551 * @param signature the signature of the authority for the record data
554 process_authority_lookup(void* cls,
555 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
556 struct GNUNET_TIME_Absolute expiration,
558 unsigned int rd_count,
559 const struct GNUNET_NAMESTORE_RecordData *rd,
560 const struct GNUNET_CRYPTO_RsaSignature *signature)
562 struct GNUNET_GNS_ResolverHandle *rh;
563 struct GNUNET_TIME_Relative remaining_time;
564 GNUNET_HashCode zone;
566 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
569 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
570 GNUNET_CRYPTO_hash(key,
571 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
573 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
576 * No authority found in namestore.
581 * We did not find an authority in the namestore
582 * _IF_ the current authoritative zone is us we cannot resolve
583 * _ELSE_ we can still check the _expired_ dht
585 if ((0 != GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash)) &&
586 (remaining_time.rel_value == 0))
588 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
589 "Authority %s unknown in namestore, trying dht\n",
591 resolve_authority_dht(rh);
594 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority %s unknown\n",
596 reply_to_dns(rh, 0, NULL);
600 //Note only 1 pkey should have been returned.. anything else would be strange
602 * We found an authority that may be able to help us
606 for (i=0; i<rd_count;i++)
609 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
612 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
615 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
616 if (remaining_time.rel_value == 0)
618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
619 "This dht entry is expired. Refreshing\n");
620 resolve_authority_dht(rh);
627 * Resolve rest of query with new authority
629 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
630 GNUNET_CRYPTO_hash(rd[i].data,
631 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
640 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
641 "Authority lookup successful but no PKEY... never get here\n");
642 reply_to_dns(rh, 0, NULL);
647 * Reply to client with the result from our lookup.
649 * @param rh the request handle of the lookup
650 * @param rd_count the number of records to return
651 * @param rd the record data
654 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
655 const struct GNUNET_NAMESTORE_RecordData *rd)
661 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
662 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
663 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
664 packet->answers = answer_records;
665 packet->additional_records = additional_records;
668 * Put records in the DNS packet and modify it
671 len = sizeof(struct GNUNET_DNSPARSER_Record*);
672 for (i=0; i < rd_count; i++)
675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
676 "Adding type %d to DNS response\n", rd[i].record_type);
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
679 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
680 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
682 if (rd[i].record_type == rh->query->type)
684 answer_records[i].name = rh->query->name;
685 answer_records[i].type = rd[i].record_type;
686 answer_records[i].data.raw.data_len = rd[i].data_size;
687 answer_records[i].data.raw.data = (char*)rd[i].data;
688 answer_records[i].expiration_time = rd[i].expiration;
689 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
693 additional_records[i].name = rh->query->name;
694 additional_records[i].type = rd[i].record_type;
695 additional_records[i].data.raw.data_len = rd[i].data_size;
696 additional_records[i].data.raw.data = (char*)rd[i].data;
697 additional_records[i].expiration_time = rd[i].expiration;
698 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
702 packet->num_answers = rh->answered;
703 packet->num_additional_records = rd_count-(rh->answered);
705 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
706 packet->flags.authoritative_answer = 1;
708 packet->flags.authoritative_answer = 0;
711 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
713 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
715 packet->flags.query_or_response = 1;
721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
722 "Building DNS response\n");
723 ret = GNUNET_DNSPARSER_pack (packet,
724 1024, /* FIXME magic from dns redirector */
727 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
728 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
729 if (ret == GNUNET_OK)
731 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
732 "Answering DNS request\n");
733 GNUNET_DNS_request_answer(rh->request_handle,
736 //GNUNET_free(answer);
737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
741 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
742 "Error building DNS response! (ret=%d)", ret);
745 GNUNET_free(rh->name);
751 * Namestore calls this function if we have record for this name.
752 * (or with rd_count=0 to indicate no matches)
754 * @param cls the pending query
755 * @param key the key of the zone we did the lookup
756 * @param expiration expiration date of the namestore entry
757 * @param name the name for which we need an authority
758 * @param rd_count the number of records with 'name'
759 * @param rd the record data
760 * @param signature the signature of the authority for the record data
763 process_authoritative_result(void* cls,
764 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
765 struct GNUNET_TIME_Absolute expiration,
766 const char *name, unsigned int rd_count,
767 const struct GNUNET_NAMESTORE_RecordData *rd,
768 const struct GNUNET_CRYPTO_RsaSignature *signature)
770 struct GNUNET_GNS_ResolverHandle *rh;
771 struct GNUNET_TIME_Relative remaining_time;
772 GNUNET_HashCode zone;
774 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
775 GNUNET_CRYPTO_hash(key,
776 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
778 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
783 * Lookup terminated and no results
784 * -> DHT Phase unless data is recent
786 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
787 "Namestore lookup for %s terminated without results\n", name);
790 * if this is not our zone we cannot rely on the namestore to be
791 * complete. -> Query DHT
793 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
795 if (remaining_time.rel_value == 0)
797 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
799 resolve_name_dht(rh, rh->name);
804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
805 "Record is still recent. No DHT lookup\n");
810 * Our zone and no result? Cannot resolve TT
812 GNUNET_assert(rh->answered == 0);
813 reply_to_dns(rh, 0, NULL);
820 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
821 "Processing additional result %s from namestore\n", name);
823 for (i=0; i<rd_count;i++)
826 if ((strcmp(name, rh->query->name) == 0)
827 && (rd[i].record_type != rh->query->type))
830 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
833 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
843 * consult dht if expired
845 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
848 "This dht entry is old. Refreshing.\n");
849 resolve_name_dht(rh, name);
853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
856 reply_to_dns(rh, rd_count, rd);
861 * Determine if this name is canonical.
863 * a.b.gnunet = not canonical
866 * @param name the name to test
867 * @return 1 if canonical
870 is_canonical(char* name)
872 uint32_t len = strlen(name);
875 for (i=0; i<len; i++)
877 if (*(name+i) == '.')
884 * Move one level up in the domain hierarchy and return the
885 * passed top level domain.
887 * @param name the domain
895 if (is_canonical(name))
898 for (len = strlen(name); len > 0; len--)
900 if (*(name+len) == '.')
914 * The first phase of resolution.
915 * First check if the name is canonical.
916 * If it is then try to resolve directly.
917 * If not then we first have to resolve the authoritative entities.
919 * @param rh the pending lookup
922 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
924 if (is_canonical(rh->name))
926 /* We only need to check the current zone's ns */
927 GNUNET_NAMESTORE_lookup_record(namestore_handle,
931 &process_authoritative_result,
936 /* We have to resolve the authoritative entity first */
937 rh->authority_name = pop_tld(rh->name);
938 GNUNET_NAMESTORE_lookup_record(namestore_handle,
941 GNUNET_GNS_RECORD_PKEY,
942 &process_authority_lookup,
948 * Entry point for name resolution
949 * Setup a new query and try to resolve
951 * @param request the request handle of the DNS request from a client
952 * @param p the DNS query packet we received
953 * @param q the DNS query we received parsed from p
956 start_resolution(struct GNUNET_DNS_RequestHandle *request,
957 struct GNUNET_DNSPARSER_Packet *p,
958 struct GNUNET_DNSPARSER_Query *q)
960 struct GNUNET_GNS_ResolverHandle *rh;
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "Starting resolution for %s (type=%d)!\n",
966 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
969 rh->authority = zone_hash;
971 rh->name = GNUNET_malloc(strlen(q->name)
972 - strlen(gnunet_tld) + 1);
974 strlen(q->name)-strlen(gnunet_tld) + 1);
975 memcpy(rh->name, q->name,
976 strlen(q->name)-strlen(gnunet_tld));
978 rh->request_handle = request;
980 /* Start resolution in our zone */
985 * The DNS request handler
986 * Called for every incoming DNS request.
989 * @param rh request handle to user for reply
990 * @param request_length number of bytes in request
991 * @param request udp payload of the DNS request
994 handle_dns_request(void *cls,
995 struct GNUNET_DNS_RequestHandle *rh,
996 size_t request_length,
999 struct GNUNET_DNSPARSER_Packet *p;
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1003 p = GNUNET_DNSPARSER_parse (request, request_length);
1007 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1008 "Received malformed DNS packet, leaving it untouched\n");
1009 GNUNET_DNS_request_forward (rh);
1014 * Check tld and decide if we or
1015 * legacy dns is responsible
1017 * FIXME now in theory there could be more than 1 query in the request
1018 * but if this is case we get into trouble:
1019 * either we query the GNS or the DNS. We cannot do both!
1020 * So I suggest to either only allow a single query per request or
1021 * only allow GNS or DNS requests.
1022 * The way it is implemented here now is buggy and will lead to erratic
1023 * behaviour (if multiple queries are present).
1025 if (p->num_queries == 0)
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "No Queries in DNS packet... forwarding\n");
1029 GNUNET_DNS_request_forward (rh);
1032 if (p->num_queries > 1)
1034 /* Note: We could also look for .gnunet */
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 ">1 queriy in DNS packet... odd. We only process #1\n");
1043 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1045 while ((*tldoffset) != '.')
1048 if (0 == strcmp(tldoffset, gnunet_tld))
1050 start_resolution(rh, p, p->queries);
1055 * This request does not concern us. Forward to real DNS.
1057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1058 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1059 GNUNET_DNS_request_forward (rh);
1065 * Method called periodicattluy that triggers
1066 * iteration over root zone
1068 * @param cls closure
1069 * @param tc task context
1072 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1074 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1078 * Continuation for DHT put
1080 * @param cls closure
1081 * @param tc task context
1084 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1086 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1091 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1094 * Function used to put all records successively into the DHT.
1096 * @param cls the closure (NULL)
1097 * @param key the public key of the authority (ours)
1098 * @param expiration lifetime of the namestore entry
1099 * @param name the name of the records
1100 * @param rd_count the number of records in data
1101 * @param rd the record data
1102 * @param signature the signature for the record data
1105 put_gns_record(void *cls,
1106 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1107 struct GNUNET_TIME_Absolute expiration,
1109 unsigned int rd_count,
1110 const struct GNUNET_NAMESTORE_RecordData *rd,
1111 const struct GNUNET_CRYPTO_RsaSignature *signature)
1114 struct GNSNameRecordBlock *nrb;
1115 GNUNET_HashCode name_hash;
1116 GNUNET_HashCode xor_hash;
1117 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1118 uint32_t rd_payload_length;
1119 char* nrb_data = NULL;
1124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1125 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1126 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1132 "Putting records for %s into the DHT\n", name);
1134 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1136 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1137 + sizeof(struct GNSNameRecordBlock));
1139 if (signature != NULL)
1140 nrb->signature = *signature;
1142 nrb->public_key = *key;
1144 nrb->rd_count = htonl(rd_count);
1146 memset(&nrb[1], 0, strlen(name) + 1);
1147 memcpy(&nrb[1], name, strlen(name));
1149 nrb_data = (char*)&nrb[1];
1150 nrb_data += strlen(name) + 1;
1152 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1155 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1160 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1167 * calculate DHT key: H(name) xor H(pubkey)
1169 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1170 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1171 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1173 "putting records for %s under key: %s with size %d\n",
1174 name, (char*)&xor_hash_string, rd_payload_length);
1176 GNUNET_DHT_put (dht_handle, &xor_hash,
1177 DHT_GNS_REPLICATION_LEVEL,
1179 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1183 DHT_OPERATION_TIMEOUT,
1185 NULL); //cls for cont
1187 num_public_records++;
1190 * Reschedule periodic put
1192 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1193 &update_zone_dht_next,
1199 * Periodically iterate over our zone and store everything in dht
1202 * @param tc task context
1205 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1207 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1208 if (0 == num_public_records)
1210 dht_update_interval = GNUNET_TIME_relative_multiply(
1211 GNUNET_TIME_UNIT_SECONDS,
1216 dht_update_interval = GNUNET_TIME_relative_multiply(
1217 GNUNET_TIME_UNIT_SECONDS,
1218 (3600/num_public_records));
1220 num_public_records = 0; //start counting again
1221 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1223 GNUNET_NAMESTORE_RF_AUTHORITY,
1224 GNUNET_NAMESTORE_RF_PRIVATE,
1230 * Process GNS requests.
1232 * @param cls closure
1233 * @param server the initialized server
1234 * @param c configuration to use
1237 run (void *cls, struct GNUNET_SERVER_Handle *server,
1238 const struct GNUNET_CONFIGURATION_Handle *c)
1241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1244 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1246 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1247 "ZONEKEY", &keyfile))
1249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1250 "No private key for root zone specified%s!\n", keyfile);
1251 GNUNET_SCHEDULER_shutdown(0);
1255 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1256 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1258 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1261 nc = GNUNET_SERVER_notification_context_create (server, 1);
1263 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1267 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1270 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1271 "DNS hijacking enabled... connecting to service.\n");
1273 * Do gnunet dns init here
1275 dns_handle = GNUNET_DNS_connect(c,
1276 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1277 &handle_dns_request, /* rh */
1278 NULL); /* Closure */
1279 if (NULL == dns_handle)
1281 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1282 "Failed to connect to the dnsservice!\n");
1289 * handle to our local namestore
1291 namestore_handle = GNUNET_NAMESTORE_connect(c);
1293 if (NULL == namestore_handle)
1295 //FIXME do error handling;
1296 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1297 "Failed to connect to the namestore!\n");
1298 GNUNET_SCHEDULER_shutdown(0);
1305 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1307 if (NULL == dht_handle)
1309 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1312 //put_some_records(); //FIXME for testing
1315 * Schedule periodic put
1317 * We have roughly an hour for all records;
1319 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1321 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1327 * The main function for the GNS service.
1329 * @param argc number of arguments from the command line
1330 * @param argv command line arguments
1331 * @return 0 ok, 1 on error
1334 main (int argc, char *const *argv)
1340 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1345 /* end of gnunet-service-gns.c */