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_GNS_REPLICATION_LEVEL 5
45 /* Ignore for now not used anyway and probably never will */
46 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
47 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
50 * Handle to a currenty pending resolution
52 struct GNUNET_GNS_ResolverHandle
54 /* The name to resolve */
57 /* the request handle to reply to */
58 struct GNUNET_DNS_RequestHandle *request_handle;
60 /* the dns parser packet received */
61 struct GNUNET_DNSPARSER_Packet *packet;
63 /* the query parsed from the packet */
65 struct GNUNET_DNSPARSER_Query *query;
67 /* has this query been answered? how many matches */
70 /* the authoritative zone to query */
71 GNUNET_HashCode authority;
73 /* the name of the authoritative zone to query */
77 * we have an authority in namestore that
78 * may be able to resolve
82 /* a handle for dht lookups. should be NULL if no lookups are in progress */
83 struct GNUNET_DHT_GetHandle *get_handle;
89 * Our handle to the DNS handler library
91 struct GNUNET_DNS_Handle *dns_handle;
94 * Our handle to the DHT
96 struct GNUNET_DHT_Handle *dht_handle;
99 * Our zone's private key
101 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
104 * Our handle to the namestore service
105 * FIXME maybe need a second handle for iteration
107 struct GNUNET_NAMESTORE_Handle *namestore_handle;
110 * Handle to iterate over our authoritative zone in namestore
112 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
115 * The configuration the GNS service is running with
117 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
120 * Our notification context.
122 static struct GNUNET_SERVER_NotificationContext *nc;
127 GNUNET_HashCode zone_hash;
130 * Our tld. Maybe get from config file
132 const char* gnunet_tld = ".gnunet";
135 * Useful for zone update for DHT put
137 static int num_public_records = 3600;
138 struct GNUNET_TIME_Relative dht_update_interval;
139 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
142 static void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer,
144 const struct GNUNET_NAMESTORE_RecordData *rd);
145 static void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
148 * Task run during shutdown.
154 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 /* Kill zone task for it may make the scheduler hang */
157 GNUNET_SCHEDULER_cancel(zone_update_taskid);
159 GNUNET_DNS_disconnect(dns_handle);
160 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
161 GNUNET_DHT_disconnect(dht_handle);
165 * Callback when record data is put into namestore
167 * @param cls the closure
168 * @param success GNUNET_OK on success
169 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
172 on_namestore_record_put_result(void *cls,
176 if (GNUNET_NO == success)
178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
181 else if (GNUNET_YES == success)
183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
184 "records successfully put in namestore\n");
188 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
189 "Error putting records into namestore: %s\n", emsg);
193 * Function called when we get a result from the dht
196 * @param cls the request handle
197 * @param exp lifetime
198 * @param key the key the record was stored under
199 * @param get_path get path
200 * @param get_path_length get path length
201 * @param put_path put path
202 * @param put_path_length put path length
203 * @param type the block type
204 * @param size the size of the record
205 * @param data the record data
208 process_authority_dht_result(void* cls,
209 struct GNUNET_TIME_Absolute exp,
210 const GNUNET_HashCode * key,
211 const struct GNUNET_PeerIdentity *get_path,
212 unsigned int get_path_length,
213 const struct GNUNET_PeerIdentity *put_path,
214 unsigned int put_path_length,
215 enum GNUNET_BLOCK_Type type,
216 size_t size, const void *data)
218 struct GNUNET_GNS_ResolverHandle *rh;
219 struct GNSNameRecordBlock *nrb;
220 uint32_t num_records;
222 char* rd_data = (char*) data;
225 GNUNET_HashCode zone, name_hash;
230 //FIXME check expiration?
232 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
233 nrb = (struct GNSNameRecordBlock*)data;
235 GNUNET_DHT_get_stop (rh->get_handle);
237 rh->get_handle = NULL;
238 num_records = ntohl(nrb->rd_count);
239 name = (char*)&nrb[1];
241 struct GNUNET_NAMESTORE_RecordData rd[num_records];
243 rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
244 rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
246 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
251 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
256 "Got name: %s (wanted %s)\n", name, rh->authority_name);
257 for (i=0; i<num_records; i++)
260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
261 "Got name: %s (wanted %s)\n", name, rh->authority_name);
262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
263 "Got type: %d (wanted %d)\n",
264 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
266 "Got data length: %d\n", rd[i].data_size);
267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
268 "Got flag %d\n", rd[i].flags);
270 if ((strcmp(name, rh->authority_name) == 0) &&
271 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
276 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
284 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
285 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
287 /* Save to namestore */
288 if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
290 GNUNET_NAMESTORE_record_put (namestore_handle,
297 &on_namestore_record_put_result, //cont
308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No authority in records\n");
309 reply_to_dns(rh, 0, NULL);
313 * Start DHT lookup for a name -> PKEY (compare NS) record in
314 * query->authority's zone
316 * @param rh the pending gns query
317 * @param name the name of the PKEY record
320 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh)
323 GNUNET_HashCode name_hash;
324 GNUNET_HashCode lookup_key;
326 GNUNET_CRYPTO_hash(rh->authority_name,
327 strlen(rh->authority_name),
329 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
332 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
333 //FIXME how long to wait for results?
334 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
335 DHT_OPERATION_TIMEOUT,
336 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
338 DHT_GNS_REPLICATION_LEVEL,
342 &process_authority_dht_result,
348 * Function called when we get a result from the dht
351 * @param cls the request handle
352 * @param exp lifetime
353 * @param key the key the record was stored under
354 * @param get_path get path
355 * @param get_path_length get path length
356 * @param put_path put path
357 * @param put_path_length put path length
358 * @param type the block type
359 * @param size the size of the record
360 * @param data the record data
363 process_name_dht_result(void* cls,
364 struct GNUNET_TIME_Absolute exp,
365 const GNUNET_HashCode * key,
366 const struct GNUNET_PeerIdentity *get_path,
367 unsigned int get_path_length,
368 const struct GNUNET_PeerIdentity *put_path,
369 unsigned int put_path_length,
370 enum GNUNET_BLOCK_Type type,
371 size_t size, const void *data)
373 struct GNUNET_GNS_ResolverHandle *rh;
374 struct GNSNameRecordBlock *nrb;
375 uint32_t num_records;
377 char* rd_data = (char*)data;
381 GNUNET_HashCode zone, name_hash;
382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
387 //FIXME maybe check expiration here, check block type
389 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
390 nrb = (struct GNSNameRecordBlock*)data;
392 GNUNET_DHT_get_stop (rh->get_handle);
394 rh->get_handle = NULL;
395 name = (char*)&nrb[1];
396 num_records = ntohl(nrb->rd_count);
398 struct GNUNET_NAMESTORE_RecordData rd[num_records];
400 rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
401 rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
403 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
408 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
412 for (i=0; i<num_records; i++)
414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
415 "Got name: %s (wanted %s)\n", name, rh->name);
416 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
417 "Got type: %d (wanted %d)\n",
418 rd[i].record_type, rh->query->type);
419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
420 "Got data length: %d\n", rd[i].data_size);
421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
422 "Got flag %d\n", rd[i].flags);
424 if ((strcmp(name, rh->name) == 0) &&
425 (rd[i].record_type == rh->query->type))
432 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
433 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
436 * FIXME check pubkey against existing key in namestore?
437 * https://gnunet.org/bugs/view.php?id=2179
440 /* Save to namestore */
441 GNUNET_NAMESTORE_record_put (namestore_handle,
448 &on_namestore_record_put_result, //cont
452 reply_to_dns(rh, num_records, rd);
454 reply_to_dns(rh, 0, NULL);
460 * Start DHT lookup for a (name -> query->record_type) record in
461 * query->authority's zone
463 * @param rh the pending gns query context
464 * @param name the name to query record
467 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
470 GNUNET_HashCode name_hash;
471 GNUNET_HashCode lookup_key;
472 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
474 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
475 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
476 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
479 "starting dht lookup for %s with key: %s\n",
480 name, (char*)&lookup_key_string);
482 xquery = htonl(rh->query->type);
483 //FIXME how long to wait for results?
484 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
485 DHT_OPERATION_TIMEOUT,
486 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
488 DHT_GNS_REPLICATION_LEVEL,
492 &process_name_dht_result,
499 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
502 * This is a callback function that should give us only PKEY
503 * records. Used to query the namestore for the authority (PKEY)
506 * @param cls the pending query
507 * @param key the key of the zone we did the lookup
508 * @param expiration expiration date of the record data set in the namestore
509 * @param name the name for which we need an authority
510 * @param rd_count the number of records with 'name'
511 * @param rd the record data
512 * @param signature the signature of the authority for the record data
515 process_authority_lookup(void* cls,
516 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
517 struct GNUNET_TIME_Absolute expiration,
519 unsigned int rd_count,
520 const struct GNUNET_NAMESTORE_RecordData *rd,
521 const struct GNUNET_CRYPTO_RsaSignature *signature)
523 struct GNUNET_GNS_ResolverHandle *rh;
524 struct GNUNET_TIME_Relative remaining_time;
525 GNUNET_HashCode zone;
527 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
528 GNUNET_CRYPTO_hash(key,
529 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
531 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
534 * No authority found in namestore.
539 * We did not find an authority in the namestore
540 * _IF_ the current authoritative zone is us we cannot resolve
541 * _ELSE_ we can still check the _expired_ dht
543 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
544 (remaining_time.rel_value == 0))
546 resolve_authority_dht(rh);
549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority %s unknown\n",
551 reply_to_dns(rh, 0, NULL);
555 //Note only 1 pkey should have been returned.. anything else would be strange
557 * We found an authority that may be able to help us
561 for (i=0; i<rd_count;i++)
564 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
567 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
570 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
571 if (remaining_time.rel_value == 0)
573 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
574 "This dht entry is expired. Refreshing\n");
575 resolve_authority_dht(rh);
582 * Resolve rest of query with new authority
584 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
585 GNUNET_CRYPTO_hash(rd[i].data,
586 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
596 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
597 "Authority lookup successful but no PKEY... never get here\n");
598 reply_to_dns(rh, 0, NULL);
603 * Reply to client with the result from our lookup.
605 * @param rh the request handle of the lookup
606 * @param rd_count the number of records to return
607 * @param rd the record data
610 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
611 const struct GNUNET_NAMESTORE_RecordData *rd)
617 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
618 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
619 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
620 packet->answers = answer_records;
621 packet->additional_records = additional_records;
624 * Put records in the DNS packet and modify it
627 len = sizeof(struct GNUNET_DNSPARSER_Record*);
628 for (i=0; i < rd_count; i++)
631 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
632 "Adding type %d to DNS response\n", rd[i].record_type);
633 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
634 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
635 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
636 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
638 if (rd[i].record_type == rh->query->type)
640 answer_records[i].name = rh->query->name;
641 answer_records[i].type = rd[i].record_type;
642 answer_records[i].data.raw.data_len = rd[i].data_size;
643 answer_records[i].data.raw.data = (char*)rd[i].data;
644 answer_records[i].expiration_time = rd[i].expiration;
645 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
649 additional_records[i].name = rh->query->name;
650 additional_records[i].type = rd[i].record_type;
651 additional_records[i].data.raw.data_len = rd[i].data_size;
652 additional_records[i].data.raw.data = (char*)rd[i].data;
653 additional_records[i].expiration_time = rd[i].expiration;
654 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
658 packet->num_answers = rh->answered;
659 packet->num_additional_records = rd_count-(rh->answered);
661 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
662 packet->flags.authoritative_answer = 1;
664 packet->flags.authoritative_answer = 0;
667 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
669 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
671 packet->flags.query_or_response = 1;
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
678 "Building DNS response\n");
679 ret = GNUNET_DNSPARSER_pack (packet,
680 1024, /* FIXME magic from dns redirector */
683 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
684 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
685 if (ret == GNUNET_OK)
687 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
688 "Answering DNS request\n");
689 GNUNET_DNS_request_answer(rh->request_handle,
692 //GNUNET_free(answer);
693 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
697 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
698 "Error building DNS response! (ret=%d)", ret);
701 GNUNET_free(rh->name);
707 * Namestore calls this function if we have record for this name.
708 * (or with rd_count=0 to indicate no matches)
710 * @param cls the pending query
711 * @param key the key of the zone we did the lookup
712 * @param expiration expiration date of the namestore entry
713 * @param name the name for which we need an authority
714 * @param rd_count the number of records with 'name'
715 * @param rd the record data
716 * @param signature the signature of the authority for the record data
719 process_authoritative_result(void* cls,
720 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
721 struct GNUNET_TIME_Absolute expiration,
722 const char *name, unsigned int rd_count,
723 const struct GNUNET_NAMESTORE_RecordData *rd,
724 const struct GNUNET_CRYPTO_RsaSignature *signature)
726 struct GNUNET_GNS_ResolverHandle *rh;
727 struct GNUNET_TIME_Relative remaining_time;
728 GNUNET_HashCode zone;
730 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
731 GNUNET_CRYPTO_hash(key,
732 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
734 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
739 * Lookup terminated and no results
740 * -> DHT Phase unless data is recent
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
743 "Namestore lookup for %s terminated without results\n", name);
746 * if this is not our zone we cannot rely on the namestore to be
747 * complete. -> Query DHT
749 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
751 if (remaining_time.rel_value == 0)
753 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
755 resolve_name_dht(rh, name);
760 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
761 "Record is still recent. No DHT lookup\n");
766 * Our zone and no result? Cannot resolve TT
768 GNUNET_assert(rh->answered == 0);
769 reply_to_dns(rh, 0, NULL);
776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
777 "Processing additional result %s from namestore\n", name);
779 for (i=0; i<rd_count;i++)
782 if ((strcmp(name, rh->query->name) == 0)
783 && (rd[i].record_type != rh->query->type))
786 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
799 * consult dht if expired
801 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
804 "This dht entry is old. Refreshing.\n");
805 resolve_name_dht(rh, name);
809 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
812 reply_to_dns(rh, rd_count, rd);
817 * Determine if this name is canonical.
819 * a.b.gnunet = not canonical
822 * @param name the name to test
823 * @return 1 if canonical
826 is_canonical(char* name)
828 uint32_t len = strlen(name);
831 for (i=0; i<len; i++)
833 if (*(name+i) == '.')
840 * Move one level up in the domain hierarchy and return the
841 * passed top level domain.
843 * @param name the domain
851 if (is_canonical(name))
854 for (len = strlen(name); len > 0; len--)
856 if (*(name+len) == '.')
870 * The first phase of resolution.
871 * First check if the name is canonical.
872 * If it is then try to resolve directly.
873 * If not then we first have to resolve the authoritative entities.
875 * @param rh the pending lookup
878 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
880 if (is_canonical(rh->name))
882 /* We only need to check the current zone's ns */
883 GNUNET_NAMESTORE_lookup_record(namestore_handle,
887 &process_authoritative_result,
892 /* We have to resolve the authoritative entity first */
893 rh->authority_name = pop_tld(rh->name);
894 GNUNET_NAMESTORE_lookup_record(namestore_handle,
897 GNUNET_GNS_RECORD_PKEY,
898 &process_authority_lookup,
904 * Entry point for name resolution
905 * Setup a new query and try to resolve
907 * @param request the request handle of the DNS request from a client
908 * @param p the DNS query packet we received
909 * @param q the DNS query we received parsed from p
912 start_resolution(struct GNUNET_DNS_RequestHandle *request,
913 struct GNUNET_DNSPARSER_Packet *p,
914 struct GNUNET_DNSPARSER_Query *q)
916 struct GNUNET_GNS_ResolverHandle *rh;
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting resolution for (%s)!\n",
921 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
924 rh->authority = zone_hash;
926 rh->name = GNUNET_malloc(strlen(q->name)
927 - strlen(gnunet_tld) + 1);
929 strlen(q->name)-strlen(gnunet_tld) + 1);
930 memcpy(rh->name, q->name,
931 strlen(q->name)-strlen(gnunet_tld));
933 rh->request_handle = request;
935 /* Start resolution in our zone */
940 * The DNS request handler
941 * Called for every incoming DNS request.
944 * @param rh request handle to user for reply
945 * @param request_length number of bytes in request
946 * @param request udp payload of the DNS request
949 handle_dns_request(void *cls,
950 struct GNUNET_DNS_RequestHandle *rh,
951 size_t request_length,
954 struct GNUNET_DNSPARSER_Packet *p;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
958 p = GNUNET_DNSPARSER_parse (request, request_length);
962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
963 "Received malformed DNS packet, leaving it untouched\n");
964 GNUNET_DNS_request_forward (rh);
969 * Check tld and decide if we or
970 * legacy dns is responsible
972 * FIXME now in theory there could be more than 1 query in the request
973 * but if this is case we get into trouble:
974 * either we query the GNS or the DNS. We cannot do both!
975 * So I suggest to either only allow a single query per request or
976 * only allow GNS or DNS requests.
977 * The way it is implemented here now is buggy and will lead to erratic
978 * behaviour (if multiple queries are present).
980 if (p->num_queries == 0)
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "No Queries in DNS packet... forwarding\n");
984 GNUNET_DNS_request_forward (rh);
987 if (p->num_queries > 1)
989 /* Note: We could also look for .gnunet */
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 ">1 queriy in DNS packet... odd. We only process #1\n");
998 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1000 while ((*tldoffset) != '.')
1003 if (0 == strcmp(tldoffset, gnunet_tld))
1005 start_resolution(rh, p, p->queries);
1010 * This request does not concern us. Forward to real DNS.
1012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1013 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1014 GNUNET_DNS_request_forward (rh);
1020 * test function that stores some data in the namestore
1021 * This will also be replaced by a test progrm that
1022 * directl interfaces with the namestore
1025 put_some_records(void)
1027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Populating namestore\n");
1028 /* put an A record into namestore FIXME use gnunet.org */
1029 char* ipB = "5.6.7.8";
1031 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
1032 struct GNUNET_NAMESTORE_RecordData rdb_web;
1034 GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
1036 rdb_web.data_size = sizeof(struct in_addr);
1038 rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
1039 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1041 GNUNET_NAMESTORE_record_create (namestore_handle,
1050 * Method called periodicattluy that triggers
1051 * iteration over root zone
1053 * @param cls closure
1054 * @param tc task context
1057 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1059 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1063 * Continuation for DHT put
1065 * @param cls closure
1066 * @param tc task context
1069 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1076 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1079 * Function used to put all records successively into the DHT.
1081 * @param cls the closure (NULL)
1082 * @param key the public key of the authority (ours)
1083 * @param expiration lifetime of the namestore entry
1084 * @param name the name of the records
1085 * @param rd_count the number of records in data
1086 * @param rd the record data
1087 * @param signature the signature for the record data
1090 put_gns_record(void *cls,
1091 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1092 struct GNUNET_TIME_Absolute expiration,
1094 unsigned int rd_count,
1095 const struct GNUNET_NAMESTORE_RecordData *rd,
1096 const struct GNUNET_CRYPTO_RsaSignature *signature)
1099 struct GNSNameRecordBlock *nrb;
1100 GNUNET_HashCode name_hash;
1101 GNUNET_HashCode xor_hash;
1102 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1103 uint32_t rd_payload_length;
1104 char* nrb_data = NULL;
1109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1110 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1111 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1116 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1117 "Putting records for %s into the DHT\n", name);
1119 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1121 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1122 + sizeof(struct GNSNameRecordBlock));
1124 if (signature != NULL)
1125 nrb->signature = *signature;
1127 nrb->public_key = *key;
1129 nrb->rd_count = htonl(rd_count);
1131 memset(&nrb[1], 0, strlen(name) + 1);
1132 memcpy(&nrb[1], name, strlen(name));
1134 nrb_data = (char*)&nrb[1];
1135 nrb_data += strlen(name) + 1;
1137 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1142 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Recor serialization failed!\n");
1147 * calculate DHT key: H(name) xor H(pubkey)
1149 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1150 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1151 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1152 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1153 "putting records for %s under key: %s with size %d\n",
1154 name, (char*)&xor_hash_string, rd_payload_length);
1156 GNUNET_DHT_put (dht_handle, &xor_hash,
1157 DHT_GNS_REPLICATION_LEVEL,
1159 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1163 DHT_OPERATION_TIMEOUT,
1164 &record_dht_put, //FIXME continuation needed? success check? yes ofc
1165 NULL); //cls for cont
1167 num_public_records++;
1170 * Reschedule periodic put
1172 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1173 &update_zone_dht_next,
1179 * Puts a single trusted entity into the
1180 * namestore. Will be replaced in a testcase
1181 * that directly interacts with a persistent
1184 * @param name name of entity
1185 * @param keyfile keyfile
1188 put_trusted(char* name, char* keyfile)
1190 struct GNUNET_NAMESTORE_RecordData rd;
1191 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1192 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1193 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1195 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1196 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1198 rd.expiration = GNUNET_TIME_absolute_get_forever ();
1199 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1200 rd.record_type = GNUNET_GNS_RECORD_PKEY;
1202 GNUNET_NAMESTORE_record_create (namestore_handle,
1213 * Periodically iterate over our zone and store everything in dht
1216 * @param tc task context
1219 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1222 if (0 == num_public_records)
1224 dht_update_interval = GNUNET_TIME_relative_multiply(
1225 GNUNET_TIME_UNIT_SECONDS,
1230 dht_update_interval = GNUNET_TIME_relative_multiply(
1231 GNUNET_TIME_UNIT_SECONDS,
1232 (3600/num_public_records));
1234 num_public_records = 0; //start counting again
1235 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1237 GNUNET_NAMESTORE_RF_AUTHORITY,
1238 GNUNET_NAMESTORE_RF_PRIVATE,
1244 * Process GNS requests.
1246 * @param cls closure
1247 * @param server the initialized server
1248 * @param c configuration to use
1251 run (void *cls, struct GNUNET_SERVER_Handle *server,
1252 const struct GNUNET_CONFIGURATION_Handle *c)
1255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1258 char* trusted_entities;
1259 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1261 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1262 "ZONEKEY", &keyfile))
1264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1265 "No private key for root zone specified%s!\n", keyfile);
1266 GNUNET_SCHEDULER_shutdown(0);
1270 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1271 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1273 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1276 nc = GNUNET_SERVER_notification_context_create (server, 1);
1278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1282 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1285 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1286 "DNS hijacking enabled... connecting to service.\n");
1288 * Do gnunet dns init here
1290 dns_handle = GNUNET_DNS_connect(c,
1291 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1292 &handle_dns_request, /* rh */
1293 NULL); /* Closure */
1294 if (NULL == dns_handle)
1296 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1297 "Failed to connect to the dnsservice!\n");
1304 * handle to our local namestore
1306 namestore_handle = GNUNET_NAMESTORE_connect(c);
1308 if (NULL == namestore_handle)
1310 //FIXME do error handling;
1311 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1312 "Failed to connect to the namestore!\n");
1313 GNUNET_SCHEDULER_shutdown(0);
1317 char* trusted_start;
1321 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1325 trusted_start = trusted_entities;
1326 trusted_len = strlen(trusted_entities);
1327 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1328 "Found trusted entities in config file, importing\n");
1329 while ((trusted_entities-trusted_start) < trusted_len)
1331 trusted_name = trusted_entities;
1332 while (*trusted_entities != ':')
1334 *trusted_entities = '\0';
1336 trusted_key = trusted_entities;
1337 while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1339 *trusted_entities = '\0';
1342 if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1344 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1347 put_trusted(trusted_name, trusted_key);
1351 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Keyfile %s does not exist!\n",
1353 //put_trusted(trusted_name, trusted_key); //FIXME for testing
1362 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1364 if (NULL == dht_handle)
1366 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1369 put_some_records(); //FIXME for testing
1372 * Schedule periodic put
1374 * We have roughly an hour for all records;
1376 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1378 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1384 * The main function for the GNS service.
1386 * @param argc number of arguments from the command line
1387 * @param argv command line arguments
1388 * @return 0 ok, 1 on error
1391 main (int argc, char *const *argv)
1397 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1402 /* end of gnunet-service-gns.c */