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"
41 /* Ignore for now not used anyway and probably never will */
42 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
43 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
46 * A result list for namestore queries
48 struct GNUNET_GNS_ResolverHandle
50 /* The name to resolve */
53 /* the request handle to reply to */
54 struct GNUNET_DNS_RequestHandle *request_handle;
56 /* the dns parser packet received */
57 struct GNUNET_DNSPARSER_Packet *packet;
59 /* the query parsed from the packet */
61 struct GNUNET_DNSPARSER_Query *query;
63 /* has this query been answered? how many matches */
66 /* the authoritative zone to query */
67 GNUNET_HashCode *authority;
70 * we have an authority in namestore that
71 * may be able to resolve
75 /* a handle for dht lookups. should be NULL if no lookups are in progress */
76 struct GNUNET_DHT_GetHandle *get_handle;
82 * Our handle to the DNS handler library
84 struct GNUNET_DNS_Handle *dns_handle;
87 * Our handle to the DHT
89 struct GNUNET_DHT_Handle *dht_handle;
92 * Our zone's private key
94 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
97 * Our handle to the namestore service
98 * FIXME maybe need a second handle for iteration
100 struct GNUNET_NAMESTORE_Handle *namestore_handle;
103 * Handle to iterate over our authoritative zone in namestore
105 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
108 * The configuration the GNS service is running with
110 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
113 * Our notification context.
115 static struct GNUNET_SERVER_NotificationContext *nc;
120 GNUNET_HashCode zone_hash;
123 * Our tld. Maybe get from config file
125 const char* gnunet_tld = ".gnunet";
128 * Useful for zone update for DHT put
130 static int num_public_records = 3600;
131 struct GNUNET_TIME_Relative dht_update_interval;
135 void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer, uint32_t rd_count,
136 const struct GNUNET_NAMESTORE_RecordData *rd);
137 void resolve_name(struct GNUNET_GNS_ResolverHandle *query,
138 GNUNET_HashCode *zone);
141 * Task run during shutdown.
147 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 GNUNET_DNS_disconnect(dns_handle);
150 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
151 GNUNET_DHT_disconnect(dht_handle);
155 * Callback when record data is put into namestore
157 * @param cls the closure
158 * @param success GNUNET_OK on success
159 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
162 on_namestore_record_put_result(void *cls,
166 if (GNUNET_NO == success)
168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
171 else if (GNUNET_YES == success)
173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
174 "records successfully put in namestore\n");
178 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
179 "Error putting records into namestore: %s\n", emsg);
183 * Function called when we get a result from the dht
186 * @param cls the request handle
187 * @param exp lifetime
188 * @param key the key the record was stored under
189 * @param get_path get path
190 * @param get_path_length get path length
191 * @param put_path put path
192 * @param put_path_length put path length
193 * @param type the block type
194 * @param size the size of the record
195 * @param data the record data
198 process_authority_dht_result(void* cls,
199 struct GNUNET_TIME_Absolute exp,
200 const GNUNET_HashCode * key,
201 const struct GNUNET_PeerIdentity *get_path,
202 unsigned int get_path_length,
203 const struct GNUNET_PeerIdentity *put_path,
204 unsigned int put_path_length,
205 enum GNUNET_BLOCK_Type type,
206 size_t size, const void *data)
208 struct GNUNET_GNS_ResolverHandle *rh;
209 uint32_t num_records;
212 struct GNUNET_CRYPTO_RsaSignature *signature;
213 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
216 GNUNET_HashCode zone, name_hash;
221 //FIXME check expiration?
222 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
225 GNUNET_DHT_get_stop (rh->get_handle);
226 rh->get_handle = NULL;
227 num_records = ntohl(*pos);
228 struct GNUNET_NAMESTORE_RecordData rd[num_records];
230 pos += sizeof(uint32_t);
232 for (i=0; i<num_records; i++)
234 namelen = ntohs(*pos);
235 pos += sizeof(uint16_t);
237 //name must be 0 terminated
241 rd[i].record_type = ntohl(*pos);
242 pos += sizeof(uint32_t);
244 rd[i].data_size = ntohl(*pos);
245 pos += sizeof(uint32_t);
248 pos += rd[i].data_size;
250 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
251 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
252 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
254 rd[i].flags = ntohs(*pos);
255 pos += sizeof(uint16_t);
257 if (strcmp(name, rh->query->name) && rd[i].record_type == rh->query->type)
264 if ((((char*)data)-pos) <
265 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
266 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
268 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
269 "Cannot parse signature/key in DHT response. Corrupted or Missing");
273 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
274 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
276 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
277 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
278 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
281 GNUNET_NAMESTORE_record_put (namestore_handle,
288 &on_namestore_record_put_result, //cont
294 memcpy(rh->authority, &zone, sizeof(GNUNET_HashCode));
295 resolve_name(rh, rh->authority);
298 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No authority in records\n");
299 reply_to_dns(rh, 0, NULL);
303 * Start DHT lookup for a name -> PKEY (compare NS) record in
304 * query->authority's zone
306 * @param query the pending gns query
307 * @param name the name of the PKEY record
310 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
312 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
313 struct GNUNET_TIME_Relative timeout;
314 GNUNET_HashCode name_hash;
315 GNUNET_HashCode lookup_key;
317 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
318 GNUNET_CRYPTO_hash_xor(&name_hash, rh->authority, &lookup_key);
320 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
322 //FIXME how long to wait for results?
323 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
324 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
326 5, //Replication level FIXME
328 &rtype, //xquery FIXME this is bad
329 sizeof(GNUNET_GNS_RECORD_PKEY),
330 &process_authority_dht_result,
336 * Function called when we get a result from the dht
339 * @param cls the request handle
340 * @param exp lifetime
341 * @param key the key the record was stored under
342 * @param get_path get path
343 * @param get_path_length get path length
344 * @param put_path put path
345 * @param put_path_length put path length
346 * @param type the block type
347 * @param size the size of the record
348 * @param data the record data
351 process_name_dht_result(void* cls,
352 struct GNUNET_TIME_Absolute exp,
353 const GNUNET_HashCode * key,
354 const struct GNUNET_PeerIdentity *get_path,
355 unsigned int get_path_length,
356 const struct GNUNET_PeerIdentity *put_path,
357 unsigned int put_path_length,
358 enum GNUNET_BLOCK_Type type,
359 size_t size, const void *data)
361 struct GNUNET_GNS_ResolverHandle *rh;
362 uint32_t num_records;
365 struct GNUNET_CRYPTO_RsaSignature *signature;
366 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
369 GNUNET_HashCode zone, name_hash;
374 //FIXME maybe check expiration here
376 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
380 GNUNET_DHT_get_stop (rh->get_handle);
381 rh->get_handle = NULL;
382 num_records = ntohl(*pos);
383 struct GNUNET_NAMESTORE_RecordData rd[num_records];
385 pos += sizeof(uint32_t);
387 for (i=0; i<num_records; i++)
389 namelen = ntohs(*pos);
390 pos += sizeof(uint16_t);
392 //name must be 0 terminated
396 rd[i].record_type = ntohl(*pos);
397 pos += sizeof(uint32_t);
399 rd[i].data_size = ntohl(*pos);
400 pos += sizeof(uint32_t);
403 pos += rd[i].data_size;
405 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
406 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
407 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
409 rd[i].flags = ntohs(*pos);
410 pos += sizeof(uint16_t);
413 if (strcmp(name, rh->query->name) && rd[i].record_type == rh->query->type)
420 if ((((char*)data)-pos) <
421 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
422 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
424 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
425 "Cannot parse signature/key in DHT response. Corrupted or Missing");
429 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
430 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
432 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
434 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
435 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
437 //FIXME check pubkey against existing key in namestore?
438 //https://gnunet.org/bugs/view.php?id=2179
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);
459 * Start DHT lookup for a (name -> query->record_type) record in
460 * query->authority's zone
462 * @param query the pending gns query
463 * @param name the name to query record
466 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
468 struct GNUNET_TIME_Relative timeout;
469 GNUNET_HashCode name_hash;
470 GNUNET_HashCode lookup_key;
472 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
473 GNUNET_CRYPTO_hash_xor(&name_hash, rh->authority, &lookup_key);
475 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
477 //FIXME how long to wait for results?
478 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
479 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
481 5, //Replication level FIXME
483 &rh->query->type, //xquery
484 sizeof(rh->query->type),
485 &process_name_dht_result,
492 resolve_name(struct GNUNET_GNS_ResolverHandle *query, GNUNET_HashCode *zone);
495 * This is a callback function that should give us only PKEY
496 * records. Used to query the namestore for the authority (PKEY)
499 * @param cls the pending query
500 * @param key the key of the zone we did the lookup
501 * @param expiration expiration date of the record data set in the namestore
502 * @param name the name for which we need an authority
503 * @param rd_count the number of records with 'name'
504 * @param data the record data
505 * @param signature the signature of the authority for the record data
508 process_authority_lookup(void* cls,
509 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
510 struct GNUNET_TIME_Absolute expiration,
512 unsigned int rd_count,
513 const struct GNUNET_NAMESTORE_RecordData *rd,
514 const struct GNUNET_CRYPTO_RsaSignature *signature)
516 struct GNUNET_GNS_ResolverHandle *rh;
517 struct GNUNET_TIME_Relative remaining_time;
518 GNUNET_HashCode zone;
520 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
521 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
522 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
525 * No authority found in namestore.
530 * We did not find an authority in the namestore
531 * _IF_ the current authoritative zone is us we cannot resolve
532 * _ELSE_ we can still check the _expired_ dht
534 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
535 (remaining_time.rel_value == 0))
537 resolve_authority_dht(rh, name);
540 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
541 reply_to_dns(rh, 0, NULL);
545 //Note only 1 pkey should have been returned.. anything else would be strange
547 * We found an authority that may be able to help us
551 for (i=0; i<rd_count;i++)
554 if (strcmp(name, rh->query->name) && rd[i].record_type
555 != GNUNET_GNS_RECORD_PKEY)
558 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
561 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
562 if (remaining_time.rel_value == 0)
564 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
565 "This dht entry is expired. Refreshing\n");
566 resolve_authority_dht(rh, name);
572 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
573 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
574 GNUNET_CRYPTO_hash(rd[i].data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
575 GNUNET_free_non_null(rh->authority);
576 rh->authority = pkey_hash;
577 resolve_name(rh, rh->authority);
586 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
587 "Authority lookup successful but no PKEY... never get here?\n");
588 reply_to_dns(rh, 0, NULL);
593 * Reply to client with the result from our lookup.
595 * @param rh the request handle of the lookup
596 * @param rd_count the number of records to return
597 * @param rd the record data
600 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
601 const struct GNUNET_NAMESTORE_RecordData *rd)
607 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
608 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
609 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
610 packet->answers = answer_records;
611 packet->additional_records = additional_records;
613 len = sizeof(struct GNUNET_DNSPARSER_Record*);
614 for (i=0; i < rd_count; i++)
616 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
617 "Adding type %d to DNS response\n", rd[i].record_type);
618 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
619 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
620 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
621 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
622 if (rd[i].record_type == rh->query->type)
624 answer_records[i].name = rh->query->name;
625 answer_records[i].type = rd[i].record_type;
626 answer_records[i].data.raw.data_len = rd[i].data_size;
627 answer_records[i].data.raw.data = (char*)rd[i].data;
628 answer_records[i].expiration_time = rd[i].expiration;
629 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
633 additional_records[i].name = rh->query->name;
634 additional_records[i].type = rd[i].record_type;
635 additional_records[i].data.raw.data_len = rd[i].data_size;
636 additional_records[i].data.raw.data = (char*)rd[i].data;
637 additional_records[i].expiration_time = rd[i].expiration;
638 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
640 //GNUNET_free(i->record); DO this later!
643 packet->num_answers = rh->answered; //answer->num_records;
644 packet->num_additional_records = rd_count-(rh->answered);
646 if (NULL == rh->authority)
647 packet->flags.authoritative_answer = 1;
649 packet->flags.authoritative_answer = 0;
652 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
654 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
656 packet->flags.query_or_response = 1;
658 //FIXME this is silently discarded
659 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
660 "Building DNS response\n");
661 ret = GNUNET_DNSPARSER_pack (packet,
662 1024, /* FIXME magic from dns redirector */
665 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
666 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
667 if (ret == GNUNET_OK)
669 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
670 "Answering DNS request\n");
671 GNUNET_DNS_request_answer(rh->request_handle,
674 //GNUNET_free(answer);
675 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
676 //FIXME return code, free datastructures
680 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
681 "Error building DNS response! (ret=%d)", ret);
684 //FIXME into free_resolver(rh)
685 //GNUNET_DNSPARSER_free_packet(rh->packet);
686 //GNUNET_free(rh->name);
692 * Namestore calls this function if we have an entry for this name.
693 * (or data=null to indicate the lookup has finished
695 * @param cls the pending query
696 * @param key the key of the zone we did the lookup
697 * @param expiration expiration date of the namestore entry
698 * @param name the name for which we need an authority
699 * @param rd_count the number of records with 'name'
700 * @param data the record data
701 * @param signature the signature of the authority for the record data
704 process_authoritative_result(void* cls,
705 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
706 struct GNUNET_TIME_Absolute expiration,
707 const char *name, unsigned int rd_count,
708 const struct GNUNET_NAMESTORE_RecordData *rd,
709 const struct GNUNET_CRYPTO_RsaSignature *signature)
711 struct GNUNET_GNS_ResolverHandle *rh;
712 struct GNUNET_TIME_Relative remaining_time;
713 GNUNET_HashCode zone;
715 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
716 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
717 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
719 //FIXME Handle results in rd
724 * Lookup terminated and no results
725 * -> DHT Phase unless data is recent
727 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
728 "Namestore lookup for %s terminated without results\n", name);
731 * if this is not our zone we cannot rely on the namestore to be
732 * complete. -> Query DHT
734 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
736 if (remaining_time.rel_value == 0)
738 resolve_name_dht(rh, name);
743 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
744 "Record is still recent. No DHT lookup\n");
749 * Our zone and no result? Cannot resolve TT
751 GNUNET_assert(rh->answered == 0);
752 reply_to_dns(rh, 0, NULL); //answered should be 0
759 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
760 "Processing additional result %s from namestore\n", name);
762 for (i=0; i<rd_count;i++)
765 if (strcmp(name, rh->query->name) && rd[i].record_type != rh->query->type)
768 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
771 //FIXME there is a catch here...
772 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
782 * consult dht if expired
784 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
786 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
787 "This dht entry is old. Refreshing.\n");
788 resolve_name_dht(rh, name);
792 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found %d answer(s) to query!\n",
795 reply_to_dns(rh, rd_count, rd);
800 * Determine if this name is canonical.
802 * a.b.gnunet = not canonical
805 * @param name the name to test
806 * @return 1 if canonical
809 is_canonical(char* name)
811 uint32_t len = strlen(name);
814 for (i=0; i<len; i++)
816 if (*(name+i) == '.')
823 * Move one level up in the domain hierarchy and return the
824 * passed top level domain.
826 * @param name the domain
829 char* pop_tld(char* name)
833 if (is_canonical(name))
836 for (len = strlen(name); len > 0; len--)
838 if (*(name+len) == '.')
845 name[len] = '\0'; //terminate string
852 * The first phase of resolution.
853 * First check if the name is canonical.
854 * If it is then try to resolve directly.
855 * If not then first have to resolve the authoritative entities.
857 * @param query the pending lookup
858 * @param zone the zone we are currently resolving in
861 resolve_name(struct GNUNET_GNS_ResolverHandle *rh, GNUNET_HashCode *zone)
863 if (is_canonical(rh->name))
865 //We only need to check this zone's ns
866 GNUNET_NAMESTORE_lookup_record(namestore_handle,
870 &process_authoritative_result,
875 //We have to resolve the authoritative entity
876 char *new_authority = pop_tld(rh->name);
877 GNUNET_NAMESTORE_lookup_record(namestore_handle,
880 GNUNET_GNS_RECORD_PKEY,
881 &process_authority_lookup,
887 * Entry point for name resolution
888 * Lookup local namestore of our zone.
890 * Setup a new query and try to resolve
892 * @param rh the request handle of the DNS request from a client
893 * @param p the DNS query packet we received
894 * @param q the DNS query we received parsed from p
897 start_resolution(struct GNUNET_DNS_RequestHandle *request,
898 struct GNUNET_DNSPARSER_Packet *p,
899 struct GNUNET_DNSPARSER_Query *q)
901 struct GNUNET_GNS_ResolverHandle *rh;
903 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
906 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
909 rh->authority = NULL;
911 //FIXME do not forget to free!!
912 rh->name = GNUNET_malloc(strlen(q->name)
913 - strlen(gnunet_tld) + 1);
915 strlen(q->name)-strlen(gnunet_tld) + 1);
916 memcpy(rh->name, q->name,
917 strlen(q->name)-strlen(gnunet_tld));
919 rh->request_handle = request;
921 //Start resolution in our zone
922 resolve_name(rh, &zone_hash);
926 * The DNS request handler
927 * Called for every incoming DNS request.
930 * @param rh request handle to user for reply
931 * @param request_length number of bytes in request
932 * @param request udp payload of the DNS request
935 handle_dns_request(void *cls,
936 struct GNUNET_DNS_RequestHandle *rh,
937 size_t request_length,
940 struct GNUNET_DNSPARSER_Packet *p;
943 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
944 p = GNUNET_DNSPARSER_parse (request, request_length);
948 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
949 "Received malformed DNS packet, leaving it untouched\n");
950 GNUNET_DNS_request_forward (rh);
955 * Check tld and decide if we or
956 * legacy dns is responsible
958 * FIXME now in theory there could be more than 1 query in the request
959 * but if this is case we get into trouble:
960 * either we query the GNS or the DNS. We cannot do both!
961 * So I suggest to either only allow a single query per request or
962 * only allow GNS or DNS requests.
963 * The way it is implemented here now is buggy and will lead to erratic
964 * behaviour (if multiple queries are present).
966 if (p->num_queries == 0)
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "No Queries in DNS packet... forwarding\n");
970 GNUNET_DNS_request_forward (rh);
973 if (p->num_queries > 1)
975 //Note: We could also look for .gnunet
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 ">1 queriy in DNS packet... odd. We only process #1\n");
981 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
983 while ((*tldoffset) != '.')
986 if (0 == strcmp(tldoffset, gnunet_tld))
988 start_resolution(rh, p, p->queries);
993 * This request does not concern us. Forward to real DNS.
995 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
996 "Request for %s is forwarded to DNS\n", p->queries[0].name);
997 GNUNET_DNS_request_forward (rh);
1003 * test function that stores some data in the namestore
1006 put_some_records(void)
1008 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
1009 /* put a few records into namestore */
1010 char* ipA = "1.2.3.4";
1011 char* ipB = "5.6.7.8";
1012 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create (); struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
1013 bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1015 GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
1017 GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
1019 GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
1021 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
1022 struct in_addr *bob_web = GNUNET_malloc(sizeof(struct in_addr));
1023 struct GNUNET_NAMESTORE_RecordData rda;
1024 struct GNUNET_NAMESTORE_RecordData rdb;
1025 struct GNUNET_NAMESTORE_RecordData rdb_web;
1027 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
1028 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob_web));
1030 rda.data_size = sizeof(struct in_addr);
1031 rdb_web.data_size = sizeof(struct in_addr);
1032 rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1035 rdb_web.data = bob_web;
1036 rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
1037 rdb_web.record_type = GNUNET_GNS_RECORD_TYPE_A;
1038 rdb.record_type = GNUNET_GNS_RECORD_PKEY;
1039 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1040 rda.expiration = GNUNET_TIME_absolute_get_forever ();
1041 rdb.expiration = GNUNET_TIME_absolute_get_forever ();
1043 //alice.gnunet A IN 1.2.3.4
1044 GNUNET_NAMESTORE_record_create (namestore_handle,
1051 //www.bob.gnunet A IN 5.6.7.8
1052 GNUNET_NAMESTORE_record_create (namestore_handle,
1058 GNUNET_NAMESTORE_record_put(namestore_handle,
1061 GNUNET_TIME_absolute_get_forever (),
1070 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1076 * Function used to put all records successively into the DHT.
1079 * @param cls the closure (NULL)
1080 * @param key the public key of the authority (ours)
1081 * @param expiration lifetime of the namestore entry
1082 * @param name the name of the records
1083 * @param rd_count the number of records in data
1084 * @param rd the record data
1085 * @param signature the signature for the record data
1088 put_gns_record(void *cls,
1089 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1090 struct GNUNET_TIME_Absolute expiration,
1092 unsigned int rd_count,
1093 const struct GNUNET_NAMESTORE_RecordData *rd,
1094 const struct GNUNET_CRYPTO_RsaSignature *signature)
1096 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1097 struct GNUNET_TIME_Relative timeout;
1098 GNUNET_HashCode name_hash;
1099 GNUNET_HashCode xor_hash;
1101 if (NULL == name) //We're done
1103 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1107 * FIXME magic number 20 move to config file
1109 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1110 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1111 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1112 GNUNET_DHT_put (dht_handle, &xor_hash,
1113 5, //replication level
1115 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1120 NULL, //FIXME continuation needed? success check? yes ofc
1121 NULL); //cls for cont
1123 num_public_records++;
1126 * Reschedule periodic put
1128 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1129 &update_zone_dht_next,
1135 * Periodically iterate over our zone and store everything in dht
1138 * @param tc task context
1141 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1143 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1144 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1145 (3600/num_public_records));
1146 num_public_records = 0; //start counting again
1147 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1149 GNUNET_NAMESTORE_RF_AUTHORITY,
1150 GNUNET_NAMESTORE_RF_PRIVATE,
1156 * Process GNS requests.
1158 * @param cls closure
1159 * @param server the initialized server
1160 * @param c configuration to use
1163 run (void *cls, struct GNUNET_SERVER_Handle *server,
1164 const struct GNUNET_CONFIGURATION_Handle *c)
1167 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1168 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1170 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1172 nc = GNUNET_SERVER_notification_context_create (server, 1);
1174 /* FIXME - do some config parsing
1175 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1178 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1181 * Do gnunet dns init here
1183 dns_handle = GNUNET_DNS_connect(c,
1184 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1185 &handle_dns_request, /* rh */
1186 NULL); /* Closure */
1188 if (NULL == dns_handle)
1190 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1191 "Failed to connect to the dnsservice!\n");
1195 * handle to our local namestore
1197 namestore_handle = GNUNET_NAMESTORE_connect(c);
1199 if (NULL == namestore_handle)
1201 //FIXME do error handling;
1202 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1203 "Failed to connect to the namestore!\n");
1209 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1211 if (NULL == dht_handle)
1213 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1216 put_some_records(); //FIXME for testing
1219 * Schedule periodic put
1221 * We have roughly an hour for all records;
1223 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1224 60); //FIXME from cfg
1225 //GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1226 // &update_zone_dht_start,
1228 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1234 * The main function for the GNS service.
1236 * @param argc number of arguments from the command line
1237 * @param argv command line arguments
1238 * @return 0 ok, 1 on error
1241 main (int argc, char *const *argv)
1247 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1252 /* end of gnunet-service-gns.c */