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 GNUNET_SCHEDULER_cancel(zone_update_taskid);
163 GNUNET_DNS_disconnect(dns_handle);
164 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
165 GNUNET_DHT_disconnect(dht_handle);
169 * Callback when record data is put into namestore
171 * @param cls the closure
172 * @param success GNUNET_OK on success
173 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
176 on_namestore_record_put_result(void *cls,
180 if (GNUNET_NO == success)
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
185 else if (GNUNET_YES == success)
187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
188 "records successfully put in namestore\n");
192 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
193 "Error putting records into namestore: %s\n", emsg);
197 * Handle timeout for DHT requests
199 * @param cls the request handle as closure
200 * @param tc the task context
203 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 struct GNUNET_GNS_ResolverHandle *rh = cls;
207 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
208 "dht lookup for query %s (type=%d) timed out.\n",
209 rh->name, rh->query->type);
211 GNUNET_DHT_get_stop (rh->get_handle);
215 * Function called when we get a result from the dht
218 * @param cls the request handle
219 * @param exp lifetime
220 * @param key the key the record was stored under
221 * @param get_path get path
222 * @param get_path_length get path length
223 * @param put_path put path
224 * @param put_path_length put path length
225 * @param type the block type
226 * @param size the size of the record
227 * @param data the record data
230 process_authority_dht_result(void* cls,
231 struct GNUNET_TIME_Absolute exp,
232 const GNUNET_HashCode * key,
233 const struct GNUNET_PeerIdentity *get_path,
234 unsigned int get_path_length,
235 const struct GNUNET_PeerIdentity *put_path,
236 unsigned int put_path_length,
237 enum GNUNET_BLOCK_Type type,
238 size_t size, const void *data)
240 struct GNUNET_GNS_ResolverHandle *rh;
241 struct GNSNameRecordBlock *nrb;
242 uint32_t num_records;
244 char* rd_data = (char*) data;
247 GNUNET_HashCode zone, name_hash;
252 //FIXME check expiration?
254 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
255 nrb = (struct GNSNameRecordBlock*)data;
257 /* stop dht lookup and timeout task */
258 GNUNET_DHT_get_stop (rh->get_handle);
259 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
261 rh->get_handle = NULL;
262 num_records = ntohl(nrb->rd_count);
263 name = (char*)&nrb[1];
265 struct GNUNET_NAMESTORE_RecordData rd[num_records];
267 rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
268 rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
270 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
275 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
279 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
280 "Got name: %s (wanted %s)\n", name, rh->authority_name);
281 for (i=0; i<num_records; i++)
284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
285 "Got name: %s (wanted %s)\n", name, rh->authority_name);
286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
287 "Got type: %d (wanted %d)\n",
288 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
290 "Got data length: %d\n", rd[i].data_size);
291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
292 "Got flag %d\n", rd[i].flags);
294 if ((strcmp(name, rh->authority_name) == 0) &&
295 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
300 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
308 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
309 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
311 /* Save to namestore */
312 if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
314 GNUNET_NAMESTORE_record_put (namestore_handle,
321 &on_namestore_record_put_result, //cont
332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No authority in records\n");
333 reply_to_dns(rh, 0, NULL);
337 * Start DHT lookup for a name -> PKEY (compare NS) record in
338 * query->authority's zone
340 * @param rh the pending gns query
341 * @param name the name of the PKEY record
344 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh)
347 GNUNET_HashCode name_hash;
348 GNUNET_HashCode lookup_key;
350 GNUNET_CRYPTO_hash(rh->authority_name,
351 strlen(rh->authority_name),
353 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
355 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
356 &dht_lookup_timeout, rh);
358 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
359 //FIXME how long to wait for results?
360 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
361 DHT_OPERATION_TIMEOUT,
362 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
364 DHT_GNS_REPLICATION_LEVEL,
368 &process_authority_dht_result,
374 * Function called when we get a result from the dht
377 * @param cls the request handle
378 * @param exp lifetime
379 * @param key the key the record was stored under
380 * @param get_path get path
381 * @param get_path_length get path length
382 * @param put_path put path
383 * @param put_path_length put path length
384 * @param type the block type
385 * @param size the size of the record
386 * @param data the record data
389 process_name_dht_result(void* cls,
390 struct GNUNET_TIME_Absolute exp,
391 const GNUNET_HashCode * key,
392 const struct GNUNET_PeerIdentity *get_path,
393 unsigned int get_path_length,
394 const struct GNUNET_PeerIdentity *put_path,
395 unsigned int put_path_length,
396 enum GNUNET_BLOCK_Type type,
397 size_t size, const void *data)
399 struct GNUNET_GNS_ResolverHandle *rh;
400 struct GNSNameRecordBlock *nrb;
401 uint32_t num_records;
403 char* rd_data = (char*)data;
407 GNUNET_HashCode zone, name_hash;
408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
413 //FIXME maybe check expiration here, check block type
415 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
416 nrb = (struct GNSNameRecordBlock*)data;
418 /* stop lookup and timeout task */
419 GNUNET_DHT_get_stop (rh->get_handle);
420 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
422 rh->get_handle = NULL;
423 name = (char*)&nrb[1];
424 num_records = ntohl(nrb->rd_count);
426 struct GNUNET_NAMESTORE_RecordData rd[num_records];
428 rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
429 rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
431 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
436 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
440 for (i=0; i<num_records; i++)
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
443 "Got name: %s (wanted %s)\n", name, rh->name);
444 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
445 "Got type: %d (wanted %d)\n",
446 rd[i].record_type, rh->query->type);
447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
448 "Got data length: %d\n", rd[i].data_size);
449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
450 "Got flag %d\n", rd[i].flags);
452 if ((strcmp(name, rh->name) == 0) &&
453 (rd[i].record_type == rh->query->type))
460 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
461 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
464 * FIXME check pubkey against existing key in namestore?
465 * https://gnunet.org/bugs/view.php?id=2179
468 /* Save to namestore */
469 GNUNET_NAMESTORE_record_put (namestore_handle,
476 &on_namestore_record_put_result, //cont
480 reply_to_dns(rh, num_records, rd);
482 reply_to_dns(rh, 0, NULL);
491 * Start DHT lookup for a (name -> query->record_type) record in
492 * query->authority's zone
494 * @param rh the pending gns query context
495 * @param name the name to query record
498 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
501 GNUNET_HashCode name_hash;
502 GNUNET_HashCode lookup_key;
503 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
505 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
506 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
507 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
509 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
510 "starting dht lookup for %s with key: %s\n",
511 name, (char*)&lookup_key_string);
513 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
514 &dht_lookup_timeout, rh);
516 xquery = htonl(rh->query->type);
517 //FIXME how long to wait for results?
518 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
519 DHT_OPERATION_TIMEOUT,
520 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
522 DHT_GNS_REPLICATION_LEVEL,
526 &process_name_dht_result,
533 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
536 * This is a callback function that should give us only PKEY
537 * records. Used to query the namestore for the authority (PKEY)
540 * @param cls the pending query
541 * @param key the key of the zone we did the lookup
542 * @param expiration expiration date of the record data set in the namestore
543 * @param name the name for which we need an authority
544 * @param rd_count the number of records with 'name'
545 * @param rd the record data
546 * @param signature the signature of the authority for the record data
549 process_authority_lookup(void* cls,
550 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
551 struct GNUNET_TIME_Absolute expiration,
553 unsigned int rd_count,
554 const struct GNUNET_NAMESTORE_RecordData *rd,
555 const struct GNUNET_CRYPTO_RsaSignature *signature)
557 struct GNUNET_GNS_ResolverHandle *rh;
558 struct GNUNET_TIME_Relative remaining_time;
559 GNUNET_HashCode zone;
561 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
562 GNUNET_CRYPTO_hash(key,
563 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
565 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
568 * No authority found in namestore.
573 * We did not find an authority in the namestore
574 * _IF_ the current authoritative zone is us we cannot resolve
575 * _ELSE_ we can still check the _expired_ dht
577 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
578 (remaining_time.rel_value == 0))
580 resolve_authority_dht(rh);
583 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority %s unknown\n",
585 reply_to_dns(rh, 0, NULL);
589 //Note only 1 pkey should have been returned.. anything else would be strange
591 * We found an authority that may be able to help us
595 for (i=0; i<rd_count;i++)
598 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
601 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
605 if (remaining_time.rel_value == 0)
607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
608 "This dht entry is expired. Refreshing\n");
609 resolve_authority_dht(rh);
616 * Resolve rest of query with new authority
618 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
619 GNUNET_CRYPTO_hash(rd[i].data,
620 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
630 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
631 "Authority lookup successful but no PKEY... never get here\n");
632 reply_to_dns(rh, 0, NULL);
637 * Reply to client with the result from our lookup.
639 * @param rh the request handle of the lookup
640 * @param rd_count the number of records to return
641 * @param rd the record data
644 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
645 const struct GNUNET_NAMESTORE_RecordData *rd)
651 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
652 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
653 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
654 packet->answers = answer_records;
655 packet->additional_records = additional_records;
658 * Put records in the DNS packet and modify it
661 len = sizeof(struct GNUNET_DNSPARSER_Record*);
662 for (i=0; i < rd_count; i++)
665 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
666 "Adding type %d to DNS response\n", rd[i].record_type);
667 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
672 if (rd[i].record_type == rh->query->type)
674 answer_records[i].name = rh->query->name;
675 answer_records[i].type = rd[i].record_type;
676 answer_records[i].data.raw.data_len = rd[i].data_size;
677 answer_records[i].data.raw.data = (char*)rd[i].data;
678 answer_records[i].expiration_time = rd[i].expiration;
679 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
683 additional_records[i].name = rh->query->name;
684 additional_records[i].type = rd[i].record_type;
685 additional_records[i].data.raw.data_len = rd[i].data_size;
686 additional_records[i].data.raw.data = (char*)rd[i].data;
687 additional_records[i].expiration_time = rd[i].expiration;
688 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
692 packet->num_answers = rh->answered;
693 packet->num_additional_records = rd_count-(rh->answered);
695 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
696 packet->flags.authoritative_answer = 1;
698 packet->flags.authoritative_answer = 0;
701 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
703 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
705 packet->flags.query_or_response = 1;
711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
712 "Building DNS response\n");
713 ret = GNUNET_DNSPARSER_pack (packet,
714 1024, /* FIXME magic from dns redirector */
717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
718 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
719 if (ret == GNUNET_OK)
721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
722 "Answering DNS request\n");
723 GNUNET_DNS_request_answer(rh->request_handle,
726 //GNUNET_free(answer);
727 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
731 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
732 "Error building DNS response! (ret=%d)", ret);
735 GNUNET_free(rh->name);
741 * Namestore calls this function if we have record for this name.
742 * (or with rd_count=0 to indicate no matches)
744 * @param cls the pending query
745 * @param key the key of the zone we did the lookup
746 * @param expiration expiration date of the namestore entry
747 * @param name the name for which we need an authority
748 * @param rd_count the number of records with 'name'
749 * @param rd the record data
750 * @param signature the signature of the authority for the record data
753 process_authoritative_result(void* cls,
754 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
755 struct GNUNET_TIME_Absolute expiration,
756 const char *name, unsigned int rd_count,
757 const struct GNUNET_NAMESTORE_RecordData *rd,
758 const struct GNUNET_CRYPTO_RsaSignature *signature)
760 struct GNUNET_GNS_ResolverHandle *rh;
761 struct GNUNET_TIME_Relative remaining_time;
762 GNUNET_HashCode zone;
764 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
765 GNUNET_CRYPTO_hash(key,
766 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
768 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
773 * Lookup terminated and no results
774 * -> DHT Phase unless data is recent
776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
777 "Namestore lookup for %s terminated without results\n", name);
780 * if this is not our zone we cannot rely on the namestore to be
781 * complete. -> Query DHT
783 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
785 if (remaining_time.rel_value == 0)
787 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
789 resolve_name_dht(rh, name);
794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
795 "Record is still recent. No DHT lookup\n");
800 * Our zone and no result? Cannot resolve TT
802 GNUNET_assert(rh->answered == 0);
803 reply_to_dns(rh, 0, NULL);
810 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
811 "Processing additional result %s from namestore\n", name);
813 for (i=0; i<rd_count;i++)
816 if ((strcmp(name, rh->query->name) == 0)
817 && (rd[i].record_type != rh->query->type))
820 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
823 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
833 * consult dht if expired
835 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
837 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
838 "This dht entry is old. Refreshing.\n");
839 resolve_name_dht(rh, name);
843 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
846 reply_to_dns(rh, rd_count, rd);
851 * Determine if this name is canonical.
853 * a.b.gnunet = not canonical
856 * @param name the name to test
857 * @return 1 if canonical
860 is_canonical(char* name)
862 uint32_t len = strlen(name);
865 for (i=0; i<len; i++)
867 if (*(name+i) == '.')
874 * Move one level up in the domain hierarchy and return the
875 * passed top level domain.
877 * @param name the domain
885 if (is_canonical(name))
888 for (len = strlen(name); len > 0; len--)
890 if (*(name+len) == '.')
904 * The first phase of resolution.
905 * First check if the name is canonical.
906 * If it is then try to resolve directly.
907 * If not then we first have to resolve the authoritative entities.
909 * @param rh the pending lookup
912 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
914 if (is_canonical(rh->name))
916 /* We only need to check the current zone's ns */
917 GNUNET_NAMESTORE_lookup_record(namestore_handle,
921 &process_authoritative_result,
926 /* We have to resolve the authoritative entity first */
927 rh->authority_name = pop_tld(rh->name);
928 GNUNET_NAMESTORE_lookup_record(namestore_handle,
931 GNUNET_GNS_RECORD_PKEY,
932 &process_authority_lookup,
938 * Entry point for name resolution
939 * Setup a new query and try to resolve
941 * @param request the request handle of the DNS request from a client
942 * @param p the DNS query packet we received
943 * @param q the DNS query we received parsed from p
946 start_resolution(struct GNUNET_DNS_RequestHandle *request,
947 struct GNUNET_DNSPARSER_Packet *p,
948 struct GNUNET_DNSPARSER_Query *q)
950 struct GNUNET_GNS_ResolverHandle *rh;
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting resolution for (%s)!\n",
955 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
958 rh->authority = zone_hash;
960 rh->name = GNUNET_malloc(strlen(q->name)
961 - strlen(gnunet_tld) + 1);
963 strlen(q->name)-strlen(gnunet_tld) + 1);
964 memcpy(rh->name, q->name,
965 strlen(q->name)-strlen(gnunet_tld));
967 rh->request_handle = request;
969 /* Start resolution in our zone */
974 * The DNS request handler
975 * Called for every incoming DNS request.
978 * @param rh request handle to user for reply
979 * @param request_length number of bytes in request
980 * @param request udp payload of the DNS request
983 handle_dns_request(void *cls,
984 struct GNUNET_DNS_RequestHandle *rh,
985 size_t request_length,
988 struct GNUNET_DNSPARSER_Packet *p;
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
992 p = GNUNET_DNSPARSER_parse (request, request_length);
996 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
997 "Received malformed DNS packet, leaving it untouched\n");
998 GNUNET_DNS_request_forward (rh);
1003 * Check tld and decide if we or
1004 * legacy dns is responsible
1006 * FIXME now in theory there could be more than 1 query in the request
1007 * but if this is case we get into trouble:
1008 * either we query the GNS or the DNS. We cannot do both!
1009 * So I suggest to either only allow a single query per request or
1010 * only allow GNS or DNS requests.
1011 * The way it is implemented here now is buggy and will lead to erratic
1012 * behaviour (if multiple queries are present).
1014 if (p->num_queries == 0)
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "No Queries in DNS packet... forwarding\n");
1018 GNUNET_DNS_request_forward (rh);
1021 if (p->num_queries > 1)
1023 /* Note: We could also look for .gnunet */
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 ">1 queriy in DNS packet... odd. We only process #1\n");
1032 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1034 while ((*tldoffset) != '.')
1037 if (0 == strcmp(tldoffset, gnunet_tld))
1039 start_resolution(rh, p, p->queries);
1044 * This request does not concern us. Forward to real DNS.
1046 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1047 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1048 GNUNET_DNS_request_forward (rh);
1054 * test function that stores some data in the namestore
1055 * This will also be replaced by a test progrm that
1056 * directl interfaces with the namestore
1059 put_some_records(void)
1061 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Populating namestore\n");
1062 /* put an A record into namestore FIXME use gnunet.org */
1063 char* ipB = "5.6.7.8";
1065 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
1066 struct GNUNET_NAMESTORE_RecordData rdb_web;
1068 GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
1070 rdb_web.data_size = sizeof(struct in_addr);
1072 rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
1073 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1075 GNUNET_NAMESTORE_record_create (namestore_handle,
1084 * Method called periodicattluy that triggers
1085 * iteration over root zone
1087 * @param cls closure
1088 * @param tc task context
1091 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1093 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1097 * Continuation for DHT put
1099 * @param cls closure
1100 * @param tc task context
1103 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1110 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1113 * Function used to put all records successively into the DHT.
1115 * @param cls the closure (NULL)
1116 * @param key the public key of the authority (ours)
1117 * @param expiration lifetime of the namestore entry
1118 * @param name the name of the records
1119 * @param rd_count the number of records in data
1120 * @param rd the record data
1121 * @param signature the signature for the record data
1124 put_gns_record(void *cls,
1125 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1126 struct GNUNET_TIME_Absolute expiration,
1128 unsigned int rd_count,
1129 const struct GNUNET_NAMESTORE_RecordData *rd,
1130 const struct GNUNET_CRYPTO_RsaSignature *signature)
1133 struct GNSNameRecordBlock *nrb;
1134 GNUNET_HashCode name_hash;
1135 GNUNET_HashCode xor_hash;
1136 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1137 uint32_t rd_payload_length;
1138 char* nrb_data = NULL;
1143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1144 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1145 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1151 "Putting records for %s into the DHT\n", name);
1153 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1155 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1156 + sizeof(struct GNSNameRecordBlock));
1158 if (signature != NULL)
1159 nrb->signature = *signature;
1161 nrb->public_key = *key;
1163 nrb->rd_count = htonl(rd_count);
1165 memset(&nrb[1], 0, strlen(name) + 1);
1166 memcpy(&nrb[1], name, strlen(name));
1168 nrb_data = (char*)&nrb[1];
1169 nrb_data += strlen(name) + 1;
1171 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1176 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Recor serialization failed!\n");
1181 * calculate DHT key: H(name) xor H(pubkey)
1183 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1184 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1185 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1187 "putting records for %s under key: %s with size %d\n",
1188 name, (char*)&xor_hash_string, rd_payload_length);
1190 GNUNET_DHT_put (dht_handle, &xor_hash,
1191 DHT_GNS_REPLICATION_LEVEL,
1193 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1197 DHT_OPERATION_TIMEOUT,
1199 NULL); //cls for cont
1201 num_public_records++;
1204 * Reschedule periodic put
1206 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1207 &update_zone_dht_next,
1213 * Puts a single trusted entity into the
1214 * namestore. Will be replaced in a testcase
1215 * that directly interacts with a persistent
1218 * @param name name of entity
1219 * @param keyfile keyfile
1222 put_trusted(char* name, char* keyfile)
1224 struct GNUNET_NAMESTORE_RecordData rd;
1225 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1226 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1227 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1229 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1230 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1232 rd.expiration = GNUNET_TIME_absolute_get_forever ();
1233 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1234 rd.record_type = GNUNET_GNS_RECORD_PKEY;
1236 GNUNET_NAMESTORE_record_create (namestore_handle,
1247 * Periodically iterate over our zone and store everything in dht
1250 * @param tc task context
1253 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1256 if (0 == num_public_records)
1258 dht_update_interval = GNUNET_TIME_relative_multiply(
1259 GNUNET_TIME_UNIT_SECONDS,
1264 dht_update_interval = GNUNET_TIME_relative_multiply(
1265 GNUNET_TIME_UNIT_SECONDS,
1266 (3600/num_public_records));
1268 num_public_records = 0; //start counting again
1269 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1271 GNUNET_NAMESTORE_RF_AUTHORITY,
1272 GNUNET_NAMESTORE_RF_PRIVATE,
1278 * Process GNS requests.
1280 * @param cls closure
1281 * @param server the initialized server
1282 * @param c configuration to use
1285 run (void *cls, struct GNUNET_SERVER_Handle *server,
1286 const struct GNUNET_CONFIGURATION_Handle *c)
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1292 char* trusted_entities;
1293 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1295 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1296 "ZONEKEY", &keyfile))
1298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299 "No private key for root zone specified%s!\n", keyfile);
1300 GNUNET_SCHEDULER_shutdown(0);
1304 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1305 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1307 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1310 nc = GNUNET_SERVER_notification_context_create (server, 1);
1312 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1316 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1319 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1320 "DNS hijacking enabled... connecting to service.\n");
1322 * Do gnunet dns init here
1324 dns_handle = GNUNET_DNS_connect(c,
1325 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1326 &handle_dns_request, /* rh */
1327 NULL); /* Closure */
1328 if (NULL == dns_handle)
1330 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1331 "Failed to connect to the dnsservice!\n");
1338 * handle to our local namestore
1340 namestore_handle = GNUNET_NAMESTORE_connect(c);
1342 if (NULL == namestore_handle)
1344 //FIXME do error handling;
1345 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1346 "Failed to connect to the namestore!\n");
1347 GNUNET_SCHEDULER_shutdown(0);
1351 char* trusted_start;
1355 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1359 trusted_start = trusted_entities;
1360 trusted_len = strlen(trusted_entities);
1361 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1362 "Found trusted entities in config file, importing\n");
1363 while ((trusted_entities-trusted_start) < trusted_len)
1365 trusted_name = trusted_entities;
1366 while (*trusted_entities != ':')
1368 *trusted_entities = '\0';
1370 trusted_key = trusted_entities;
1371 while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1373 *trusted_entities = '\0';
1376 if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1378 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1381 put_trusted(trusted_name, trusted_key);
1385 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Keyfile %s does not exist!\n",
1387 //put_trusted(trusted_name, trusted_key); //FIXME for testing
1396 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1398 if (NULL == dht_handle)
1400 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1403 put_some_records(); //FIXME for testing
1406 * Schedule periodic put
1408 * We have roughly an hour for all records;
1410 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1412 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1418 * The main function for the GNS service.
1420 * @param argc number of arguments from the command line
1421 * @param argv command line arguments
1422 * @return 0 ok, 1 on error
1425 main (int argc, char *const *argv)
1431 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1436 /* end of gnunet-service-gns.c */