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 * - Think about mixed dns queries (.gnunet and .org)
26 * - The smaller FIXME issues all around
28 * @file gns/gnunet-service-gns.c
29 * @brief GNUnet GNS service
30 * @author Martin Schanzenbach
33 #include "gnunet_util_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_dht_service.h"
38 #include "gnunet_namestore_service.h"
39 #include "gnunet_gns_service.h"
42 /* Ignore for now not used anyway and probably never will */
43 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
46 struct GNUNET_GNS_QueryRecordList
51 struct GNUNET_GNS_QueryRecordList * next;
52 struct GNUNET_GNS_QueryRecordList * prev;
54 struct GNUNET_NAMESTORE_RecordData * record;
58 * A result list for namestore queries
60 struct GNUNET_GNS_ResolverHandle
62 /* the answer packet */
63 struct GNUNET_DNSPARSER_Packet *answer;
65 /* records to put into answer packet */
66 struct GNUNET_GNS_QueryRecordList * records_head;
67 struct GNUNET_GNS_QueryRecordList * records_tail;
70 int num_authority_records; //FIXME are all of our replies auth?
74 /* the request handle to reply to */
75 struct GNUNET_DNS_RequestHandle *request_handle;
77 /* hast this query been answered? */
80 /* the authoritative zone to query */
81 GNUNET_HashCode *authority;
83 /* we have an authority in namestore that
84 * may be able to resolve
88 struct GNUNET_DNSPARSER_Packet *packet;
90 struct GNUNET_DNSPARSER_Query *query;
95 * Our handle to the DNS handler library
97 struct GNUNET_DNS_Handle *dns_handle;
100 * Our handle to the DHT
102 struct GNUNET_DHT_Handle *dht_handle;
105 * Our zone's private key
107 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
110 * Our handle to the namestore service
112 struct GNUNET_NAMESTORE_Handle *namestore_handle;
114 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
117 * The configuration the GNS service is running with
119 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
122 * Our notification context.
124 static struct GNUNET_SERVER_NotificationContext *nc;
129 GNUNET_HashCode zone_hash;
132 * Our tld. Maybe get from config file
134 const char* gnunet_tld = ".gnunet";
137 * Useful for zone update for DHT put
139 static int num_public_records = 3600;
140 struct GNUNET_TIME_Relative dht_update_interval;
143 void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer, uint32_t rd_count,
144 const struct GNUNET_NAMESTORE_RecordData *rd);
145 void resolve_name(struct GNUNET_GNS_ResolverHandle *query,
146 GNUNET_HashCode *zone);
149 * Task run during shutdown.
155 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 GNUNET_DNS_disconnect(dns_handle);
158 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
159 GNUNET_DHT_disconnect(dht_handle);
163 on_namestore_record_put_result(void *cls,
167 if (GNUNET_NO == success)
169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
172 else if (GNUNET_YES == success)
174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
175 "records successfully put in namestore\n");
179 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
180 "Error putting records into namestore: %s\n", emsg);
184 * Function called when we get a result from the dht
187 * @param cls the query handle
188 * @param exp lifetime
189 * @param key the key the record was stored under
190 * @param get_path get path
191 * @param get_path_length get path length
192 * @param put_path put path
193 * @param put_path_length put path length
194 * @param type the block type
195 * @param size the size of the record
196 * @param data the record data
199 process_authority_dht_result(void* cls,
200 struct GNUNET_TIME_Absolute exp,
201 const GNUNET_HashCode * key,
202 const struct GNUNET_PeerIdentity *get_path,
203 unsigned int get_path_length,
204 const struct GNUNET_PeerIdentity *put_path,
205 unsigned int put_path_length,
206 enum GNUNET_BLOCK_Type type,
207 size_t size, const void *data)
209 struct GNUNET_GNS_ResolverHandle *rh;
210 uint32_t num_records;
213 struct GNUNET_CRYPTO_RsaSignature *signature;
214 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
217 GNUNET_HashCode zone, name_hash;
222 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
225 num_records = ntohl(*pos);
226 struct GNUNET_NAMESTORE_RecordData rd[num_records];
228 pos += sizeof(uint32_t);
230 for (i=0; i<num_records; i++)
232 namelen = ntohs(*pos);
233 pos += sizeof(uint16_t);
235 //name must be 0 terminated
239 rd[i].record_type = ntohl(*pos);
240 pos += sizeof(uint32_t);
242 rd[i].data_size = ntohl(*pos);
243 pos += sizeof(uint32_t);
246 pos += rd[i].data_size;
248 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
249 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
250 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
252 rd[i].flags = ntohs(*pos);
253 pos += sizeof(uint16_t);
256 if (strcmp(name, rh->query->name) && rd[i].record_type == rh->query->type)
263 if ((((char*)data)-pos) <
264 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
265 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
267 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
268 "Cannot parse signature/key in DHT response. Corrupted or Missing");
272 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
273 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
275 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
276 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
277 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
280 GNUNET_NAMESTORE_record_put (namestore_handle,
287 &on_namestore_record_put_result, //cont
293 memcpy(rh->authority, &zone, sizeof(GNUNET_HashCode));
294 resolve_name(rh, rh->authority);
297 * data is a serialized PKEY record (probably)
298 * parse, put into namestore
299 * namestore zone hash is in query.
300 * Then adjust query->name and call resolve_name
301 * with new zone (the one just received)
303 * query->authority = new_authority
304 * resolve_name(query, new_authority);
309 * Start DHT lookup for a name -> PKEY (compare NS) record in
310 * query->authority's zone
312 * @param query the pending gns query
313 * @param name the name of the PKEY record
316 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
318 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
319 struct GNUNET_TIME_Relative timeout;
320 GNUNET_HashCode name_hash;
321 GNUNET_HashCode lookup_key;
323 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
324 GNUNET_CRYPTO_hash_xor(&name_hash, rh->authority, &lookup_key);
326 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
328 //FIXME how long to wait for results?
329 GNUNET_DHT_get_start(dht_handle, timeout,
330 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
332 5, //Replication level FIXME
334 &rtype, //xquery FIXME this is bad
335 sizeof(GNUNET_GNS_RECORD_PKEY),
336 &process_authority_dht_result,
342 * Function called when we get a result from the dht
345 * @param cls the query handle
346 * @param exp lifetime
347 * @param key the key the record was stored under
348 * @param get_path get path
349 * @param get_path_length get path length
350 * @param put_path put path
351 * @param put_path_length put path length
352 * @param type the block type
353 * @param size the size of the record
354 * @param data the record data
357 process_name_dht_result(void* cls,
358 struct GNUNET_TIME_Absolute exp,
359 const GNUNET_HashCode * key,
360 const struct GNUNET_PeerIdentity *get_path,
361 unsigned int get_path_length,
362 const struct GNUNET_PeerIdentity *put_path,
363 unsigned int put_path_length,
364 enum GNUNET_BLOCK_Type type,
365 size_t size, const void *data)
367 struct GNUNET_GNS_ResolverHandle *rh;
368 uint32_t num_records;
371 struct GNUNET_CRYPTO_RsaSignature *signature;
372 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
375 GNUNET_HashCode zone, name_hash;
380 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
383 num_records = ntohl(*pos);
384 struct GNUNET_NAMESTORE_RecordData rd[num_records];
386 pos += sizeof(uint32_t);
388 for (i=0; i<num_records; i++)
390 namelen = ntohs(*pos);
391 pos += sizeof(uint16_t);
393 //name must be 0 terminated
397 rd[i].record_type = ntohl(*pos);
398 pos += sizeof(uint32_t);
400 rd[i].data_size = ntohl(*pos);
401 pos += sizeof(uint32_t);
404 pos += rd[i].data_size;
406 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
407 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
408 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
410 rd[i].flags = ntohs(*pos);
411 pos += sizeof(uint16_t);
414 if (strcmp(name, rh->query->name) && rd[i].record_type == rh->query->type)
421 if ((((char*)data)-pos) <
422 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
423 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
425 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
426 "Cannot parse signature/key in DHT response. Corrupted or Missing");
430 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
431 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
433 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
435 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
436 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
439 GNUNET_NAMESTORE_record_put (namestore_handle,
446 &on_namestore_record_put_result, //cont
451 //FIXME: add records to query handle, but on stack!
452 //do we need records in query handle? can't we just
453 //pass them to reply_to_dns?
454 reply_to_dns(rh, num_records, rd);
458 * data is a serialized GNS record of type
459 * Check if record type and name match in query and reply
465 * Start DHT lookup for a (name -> query->record_type) record in
466 * query->authority's zone
468 * @param query the pending gns query
469 * @param name the name to query record
472 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
474 struct GNUNET_TIME_Relative timeout;
475 GNUNET_HashCode name_hash;
476 GNUNET_HashCode lookup_key;
478 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
479 GNUNET_CRYPTO_hash_xor(&name_hash, rh->authority, &lookup_key);
481 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
483 //FIXME how long to wait for results?
484 GNUNET_DHT_get_start(dht_handle, timeout,
485 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
487 5, //Replication level FIXME
489 &rh->query->type, //xquery
490 sizeof(rh->query->type),
491 &process_name_dht_result,
498 resolve_name(struct GNUNET_GNS_ResolverHandle *query, GNUNET_HashCode *zone);
501 * This is a callback function that should give us only PKEY
502 * records. Used to query the namestore for the authority (PKEY)
505 * @param cls the pending query
506 * @param zone our zone hash
507 * @param name the name for which we need an authority
508 * @param record_type the type of record (PKEY)
509 * @param expiration expiration date of the record
510 * @param flags namestore record flags
511 * @param sig_loc the location of the record in the signature tree
512 * @param size the size of the record
513 * @param data the record data
516 process_authority_lookup(void* cls,
517 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
518 struct GNUNET_TIME_Absolute expiration,
520 unsigned int rd_count,
521 const struct GNUNET_NAMESTORE_RecordData *rd,
522 const struct GNUNET_CRYPTO_RsaSignature *signature)
524 struct GNUNET_GNS_ResolverHandle *rh;
525 GNUNET_HashCode zone;
527 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
528 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
531 * No authority found in namestore.
536 * We did not find an authority in the namestore
537 * _IF_ the current authoritative zone is us we cannot resolve
538 * _ELSE_ we can still check the dht
540 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
542 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
543 //FIXME return NX answer
550 resolve_authority_dht(rh, name);
554 //Note only 1 pkey should have been returned.. anything else would be strange
556 * We found an authority that may be able to help us
560 GNUNET_assert(rd->record_type == GNUNET_GNS_RECORD_PKEY);
561 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
562 GNUNET_CRYPTO_hash(rd->data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
563 GNUNET_free_non_null(rh->authority);
564 rh->authority = pkey_hash;
565 resolve_name(rh, rh->authority);
571 * Reply to client with the result from our lookup.
573 * @param answer the pending query used in the lookup
576 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
577 const struct GNUNET_NAMESTORE_RecordData *rd)
579 struct GNUNET_DNSPARSER_Flags dnsflags;
584 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
585 struct GNUNET_DNSPARSER_Record answer_records[rh->num_records];
586 packet->answers = answer_records;
588 len = sizeof(struct GNUNET_DNSPARSER_Record*);
589 for (i=0; i < rd_count; i++)
591 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
592 "Adding type %d to DNS response\n", rd[i].record_type);
593 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
595 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
596 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
597 answer_records[i].name = rh->query->name;
598 answer_records[i].type = rd[i].record_type;
599 answer_records[i].data.raw.data_len = rd[i].data_size;
600 answer_records[i].data.raw.data = (char*)rd[i].data;
601 answer_records[i].expiration_time = rd[i].expiration;
602 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
603 //GNUNET_free(i->record); DO this later!
607 * FIXME how to handle auth, additional etc
608 * PKEY might be auth, != name,record_type additional
610 //packet->num_queries = 0;
611 packet->num_additional_records = 0;
612 packet->num_answers = rd_count; //answer->num_records;
613 //packet.num_authority_records = 0;//answer->num_authority_records;
615 if (NULL == rh->authority)
616 dnsflags.authoritative_answer = 1;
618 dnsflags.authoritative_answer = 0;
620 dnsflags.message_truncated = 0;
621 dnsflags.recursion_desired = 0;
622 dnsflags.authenticated_data = 0;
623 dnsflags.checking_disabled = 1;
625 dnsflags.recursion_available = 0;
626 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
629 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
631 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
633 dnsflags.query_or_response = 1;
634 packet->flags = dnsflags;
636 //FIXME this is silently discarded
637 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
638 "Building DNS response\n");
639 ret = GNUNET_DNSPARSER_pack (packet,
640 1024, /* FIXME magic from dns redirector */
643 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
644 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
645 if (ret == GNUNET_OK)
647 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
648 "Answering DNS request\n");
649 GNUNET_DNS_request_answer(rh->request_handle,
652 //GNUNET_free(answer);
653 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
654 //FIXME return code, free datastructures
658 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
659 "Error building DNS response! (ret=%d)", ret);
662 //FIXME into free_resolver(rh)
663 //GNUNET_DNSPARSER_free_packet(rh->packet);
664 //GNUNET_free(rh->name);
670 * Namestore calls this function if we have an entry for this name.
671 * (or data=null to indicate the lookup has finished)
673 * @param cls the pending query
674 * @param zone the zone of the lookup
675 * @param name the name looked up
676 * @param record_type the record type
677 * @param expiration lifetime of the record
678 * @param flags record flags
679 * @param sig_loc location of the record in the signature tree
680 * @param size the size of the record
681 * @param data the record data
684 process_authoritative_result(void* cls,
685 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
686 struct GNUNET_TIME_Absolute expiration,
687 const char *name, unsigned int rd_count,
688 const struct GNUNET_NAMESTORE_RecordData *rd,
689 const struct GNUNET_CRYPTO_RsaSignature *signature)
691 struct GNUNET_GNS_ResolverHandle *rh;
692 struct GNUNET_GNS_QueryRecordList *qrecord;
693 struct GNUNET_NAMESTORE_RecordData *record;
694 struct GNUNET_TIME_Relative remaining_time;
695 GNUNET_HashCode zone;
697 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
698 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
699 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
701 //FIXME Handle results in rd
707 * Lookup terminated and no results
708 * -> DHT Phase unless data is recent
710 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
711 "Namestore lookup for %s terminated without results\n", name);
714 * if this is not our zone we cannot rely on the namestore to be
715 * complete. -> Query DHT
717 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
719 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
720 if (remaining_time.rel_value == 0)
722 resolve_name_dht(rh, name);
727 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record is still recent. No DHT lookup\n");
732 * Our zone and no result? Cannot resolve TT
733 * FIXME modify query to say NX
735 GNUNET_assert(rh->answered == 0);
736 reply_to_dns(rh, 0, NULL); //answered should be 0
745 * FIXME Check record expiration and dht expiration
746 * consult dht if necessary
748 if (remaining_time.rel_value == 0)
750 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
751 "This dht entry is old. Refreshing.\n");
752 resolve_name_dht(rh, name);
755 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
756 "Processing additional result %s from namestore\n", name);
758 for (i=0; i<rd_count;i++)
760 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
763 //FIXME there is a catch here...
764 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
767 // A time will come when this has to be freed
768 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
769 record = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
770 qrecord->record = record;
772 //fixme into gns_util
773 //parse_record(rd[i]->data, rd[i]->data_size, 0, record);
774 GNUNET_CONTAINER_DLL_insert(rh->records_head,
780 //we need to resolve to the original name in the end though...
781 //keep in mind. This can also be done later probably
782 //record->name = (char*)query->original_name;
785 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
788 reply_to_dns(rh, rd_count, rd);
793 * Determine if this name is canonical.
795 * a.b.gnunet = not canonical
798 * @param name the name to test
799 * @return 1 if canonical
802 is_canonical(char* name)
804 uint32_t len = strlen(name);
807 for (i=0; i<len; i++)
809 if (*(name+i) == '.')
816 * Move one level up in the domain hierarchy and return the
817 * passed top level domain.
818 * FIXME this needs a better name
820 * @param name the domain
823 char* pop_tld(char* name)
827 if (is_canonical(name))
830 for (len = strlen(name); len > 0; len--)
832 if (*(name+len) == '.')
839 name[len] = '\0'; //terminate string
846 * The first phase of resolution.
847 * First check if the name is canonical.
848 * If it is then try to resolve directly.
849 * If not then first have to resolve the authoritative entities.
851 * @param query the pending lookup
852 * @param zone the zone we are currently resolving in
855 resolve_name(struct GNUNET_GNS_ResolverHandle *rh, GNUNET_HashCode *zone)
857 if (is_canonical(rh->name))
859 //We only need to check this zone's ns
860 GNUNET_NAMESTORE_lookup_record(namestore_handle,
864 &process_authoritative_result,
869 //We have to resolve the authoritative entity
870 char *new_authority = pop_tld(rh->name);
871 GNUNET_NAMESTORE_lookup_record(namestore_handle,
874 GNUNET_GNS_RECORD_PKEY,
875 &process_authority_lookup,
881 * Entry point for name resolution
882 * Lookup local namestore of our zone.
884 * Setup a new query and try to resolve
886 * @param rh the request handle of the DNS request from a client
887 * @param p the DNS query packet we received
888 * @param name the name to look up
889 * @param id the id of the dns request (for the reply)
890 * @param type the record type to look for
893 start_resolution(struct GNUNET_DNS_RequestHandle *request,
894 struct GNUNET_DNSPARSER_Packet *p,
895 struct GNUNET_DNSPARSER_Query *q)
897 struct GNUNET_GNS_ResolverHandle *rh;
899 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
902 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
905 rh->authority = NULL;
907 //FIXME do not forget to free!!
908 rh->name = GNUNET_malloc(strlen(q->name)
909 - strlen(gnunet_tld) + 1);
911 strlen(q->name)-strlen(gnunet_tld) + 1);
912 memcpy(rh->name, q->name,
913 strlen(q->name)-strlen(gnunet_tld));
915 rh->request_handle = request;
917 //Start resolution in our zone
918 resolve_name(rh, &zone_hash);
922 * The DNS request handler
923 * Called for every incoming DNS request.
926 * @param rh request handle to user for reply
927 * @param request_length number of bytes in request
928 * @param request udp payload of the DNS request
931 handle_dns_request(void *cls,
932 struct GNUNET_DNS_RequestHandle *rh,
933 size_t request_length,
936 struct GNUNET_DNSPARSER_Packet *p;
939 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
940 p = GNUNET_DNSPARSER_parse (request, request_length);
944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
945 "Received malformed DNS packet, leaving it untouched\n");
946 GNUNET_DNS_request_forward (rh);
951 * Check tld and decide if we or
952 * legacy dns is responsible
954 * FIXME now in theory there could be more than 1 query in the request
955 * but if this is case we get into trouble:
956 * either we query the GNS or the DNS. We cannot do both!
957 * So I suggest to either only allow a single query per request or
958 * only allow GNS or DNS requests.
959 * The way it is implemented here now is buggy and will lead to erratic
960 * behaviour (if multiple queries are present).
962 if (p->num_queries == 0)
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "No Queries in DNS packet... forwarding\n");
966 GNUNET_DNS_request_forward (rh);
969 if (p->num_queries > 1)
971 //Note: We could also look for .gnunet
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 ">1 queriy in DNS packet... odd. We only process #1\n");
977 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
979 while ((*tldoffset) != '.')
982 if (0 == strcmp(tldoffset, gnunet_tld))
984 start_resolution(rh, p, p->queries);
989 * This request does not concern us. Forward to real DNS.
991 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
992 "Request for %s is forwarded to DNS\n", p->queries[0].name);
993 GNUNET_DNS_request_forward (rh);
999 * test function that stores some data in the namestore
1002 put_some_records(void)
1004 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
1005 /* put a few records into namestore */
1006 char* ipA = "1.2.3.4";
1007 char* ipB = "5.6.7.8";
1008 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create (); struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
1009 bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1011 GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
1013 GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
1015 GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
1017 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
1018 struct in_addr *bob_web = GNUNET_malloc(sizeof(struct in_addr));
1019 struct GNUNET_NAMESTORE_RecordData rda;
1020 struct GNUNET_NAMESTORE_RecordData rdb;
1021 struct GNUNET_NAMESTORE_RecordData rdb_web;
1023 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
1024 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob_web));
1026 rda.data_size = sizeof(struct in_addr);
1027 rdb_web.data_size = sizeof(struct in_addr);
1028 rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1031 rdb_web.data = bob_web;
1032 rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
1033 rdb_web.record_type = GNUNET_GNS_RECORD_TYPE_A;
1034 rdb.record_type = GNUNET_GNS_RECORD_PKEY;
1035 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1036 rda.expiration = GNUNET_TIME_absolute_get_forever ();
1037 rdb.expiration = GNUNET_TIME_absolute_get_forever ();
1039 //alice.gnunet A IN 1.2.3.4
1040 GNUNET_NAMESTORE_record_create (namestore_handle,
1047 //www.bob.gnunet A IN 5.6.7.8
1048 GNUNET_NAMESTORE_record_create (namestore_handle,
1054 GNUNET_NAMESTORE_record_put(namestore_handle,
1057 GNUNET_TIME_absolute_get_forever (),
1066 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1068 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1072 * Function used to put all records successively into the DHT.
1074 * @param cls the closure (NULL)
1075 * @param zone our root zone hash
1076 * @param name the name of the record
1077 * @param record_type the type of the record
1078 * @param expiration lifetime of the record
1079 * @param flags flags of the record
1080 * @param sig_loc location of record in signature tree
1081 * @param size size of the record
1082 * @param record_data the record data
1085 put_gns_record(void *cls,
1086 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1087 struct GNUNET_TIME_Absolute expiration,
1089 unsigned int rd_count,
1090 const struct GNUNET_NAMESTORE_RecordData *rd,
1091 const struct GNUNET_CRYPTO_RsaSignature *signature)
1093 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1094 struct GNUNET_TIME_Relative timeout;
1095 GNUNET_HashCode name_hash;
1096 GNUNET_HashCode xor_hash;
1098 if (NULL == name) //We're done
1100 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1104 * FIXME magic number 20 move to config file
1106 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1107 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1108 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1109 GNUNET_DHT_put (dht_handle, &xor_hash,
1110 5, //replication level
1112 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1117 NULL, //FIXME continuation needed? success check? yes ofc
1118 NULL); //cls for cont
1120 num_public_records++;
1123 * Reschedule periodic put
1125 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1126 &update_zone_dht_next,
1132 * Periodically iterate over our zone and store everything in dht
1135 * @param tc task context
1138 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1140 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1141 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1142 (3600/num_public_records));
1143 num_public_records = 0; //start counting again
1144 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1146 GNUNET_NAMESTORE_RF_AUTHORITY,
1147 GNUNET_NAMESTORE_RF_PRIVATE,
1153 * Process GNS requests.
1155 * @param cls closure
1156 * @param server the initialized server
1157 * @param c configuration to use
1160 run (void *cls, struct GNUNET_SERVER_Handle *server,
1161 const struct GNUNET_CONFIGURATION_Handle *c)
1164 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1165 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1167 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1169 nc = GNUNET_SERVER_notification_context_create (server, 1);
1171 /* FIXME - do some config parsing
1172 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1175 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1178 * Do gnunet dns init here
1180 dns_handle = GNUNET_DNS_connect(c,
1181 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1182 &handle_dns_request, /* rh */
1183 NULL); /* Closure */
1185 if (NULL == dns_handle)
1187 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1188 "Failed to connect to the dnsservice!\n");
1192 * handle to our local namestore
1194 namestore_handle = GNUNET_NAMESTORE_connect(c);
1196 if (NULL == namestore_handle)
1198 //FIXME do error handling;
1199 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1200 "Failed to connect to the namestore!\n");
1206 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1208 if (NULL == dht_handle)
1210 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1213 put_some_records(); //FIXME for testing
1216 * Schedule periodic put
1218 * We have roughly an hour for all records;
1220 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1221 60); //FIXME from cfg
1222 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1223 &update_zone_dht_start,
1225 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1231 * The main function for the GNS service.
1233 * @param argc number of arguments from the command line
1234 * @param argv command line arguments
1235 * @return 0 ok, 1 on error
1238 main (int argc, char *const *argv)
1244 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1249 /* end of gnunet-service-gns.c */