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_PendingQuery
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?
76 /* the dns request id */
77 int id; // FIXME can handle->request_id also be used here?
79 /* the request handle to reply to */
80 struct GNUNET_DNS_RequestHandle *request_handle;
82 /* hast this query been answered? */
85 /* the authoritative zone to query */
86 GNUNET_HashCode *authority;
88 /* we have an authority in namestore that
89 * may be able to resolve
93 struct GNUNET_DNSPARSER_Packet *p;
98 * Our handle to the DNS handler library
100 struct GNUNET_DNS_Handle *dns_handle;
103 * Our handle to the DHT
105 struct GNUNET_DHT_Handle *dht_handle;
108 * Our zone's private key
110 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
113 * Our handle to the namestore service
115 struct GNUNET_NAMESTORE_Handle *namestore_handle;
117 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
120 * The configuration the GNS service is running with
122 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
125 * Our notification context.
127 static struct GNUNET_SERVER_NotificationContext *nc;
132 GNUNET_HashCode zone_hash;
135 * Our tld. Maybe get from config file
137 const char* gnunet_tld = ".gnunet";
140 * Useful for zone update for DHT put
142 static int num_public_records = 3600;
143 struct GNUNET_TIME_Relative dht_update_interval;
146 void reply_to_dns(struct GNUNET_GNS_PendingQuery *answer, uint32_t rd_count,
147 const struct GNUNET_NAMESTORE_RecordData *rd);
148 void resolve_name(struct GNUNET_GNS_PendingQuery *query,
149 GNUNET_HashCode *zone);
152 * Task run during shutdown.
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_DNS_disconnect(dns_handle);
161 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
162 GNUNET_DHT_disconnect(dht_handle);
166 on_namestore_record_put_result(void *cls,
170 if (GNUNET_NO == success)
172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
175 else if (GNUNET_YES == success)
177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
178 "records successfully put in namestore\n");
182 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
183 "Error putting records into namestore: %s\n", emsg);
187 * Function called when we get a result from the dht
190 * @param cls the query handle
191 * @param exp lifetime
192 * @param key the key the record was stored under
193 * @param get_path get path
194 * @param get_path_length get path length
195 * @param put_path put path
196 * @param put_path_length put path length
197 * @param type the block type
198 * @param size the size of the record
199 * @param data the record data
202 process_authority_dht_result(void* cls,
203 struct GNUNET_TIME_Absolute exp,
204 const GNUNET_HashCode * key,
205 const struct GNUNET_PeerIdentity *get_path,
206 unsigned int get_path_length,
207 const struct GNUNET_PeerIdentity *put_path,
208 unsigned int put_path_length,
209 enum GNUNET_BLOCK_Type type,
210 size_t size, const void *data)
212 struct GNUNET_GNS_PendingQuery *query;
213 uint32_t num_records;
216 struct GNUNET_CRYPTO_RsaSignature *signature;
217 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
220 GNUNET_HashCode zone, name_hash;
225 query = (struct GNUNET_GNS_PendingQuery *)cls;
228 num_records = ntohl(*pos);
229 struct GNUNET_NAMESTORE_RecordData rd[num_records];
231 pos += sizeof(uint32_t);
233 for (i=0; i<num_records; i++)
235 namelen = ntohs(*pos);
236 pos += sizeof(uint16_t);
238 //name must be 0 terminated
242 rd[i].record_type = ntohl(*pos);
243 pos += sizeof(uint32_t);
245 rd[i].data_size = ntohl(*pos);
246 pos += sizeof(uint32_t);
249 pos += rd[i].data_size;
251 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
252 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
253 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
255 rd[i].flags = ntohs(*pos);
256 pos += sizeof(uint16_t);
259 if (strcmp(name, query->name) && rd[i].record_type == query->type)
266 if ((((char*)data)-pos) <
267 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
268 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
270 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
271 "Cannot parse signature/key in DHT response. Corrupted or Missing");
275 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
276 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
278 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
279 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
280 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
283 GNUNET_NAMESTORE_record_put (namestore_handle,
290 &on_namestore_record_put_result, //cont
296 memcpy(query->authority, &zone, sizeof(GNUNET_HashCode));
297 resolve_name(query, query->authority);
300 * data is a serialized PKEY record (probably)
301 * parse, put into namestore
302 * namestore zone hash is in query.
303 * Then adjust query->name and call resolve_name
304 * with new zone (the one just received)
306 * query->authority = new_authority
307 * resolve_name(query, new_authority);
312 * Start DHT lookup for a name -> PKEY (compare NS) record in
313 * query->authority's zone
315 * @param query the pending gns query
316 * @param name the name of the PKEY record
319 resolve_authority_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
321 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
322 struct GNUNET_TIME_Relative timeout;
323 GNUNET_HashCode name_hash;
324 GNUNET_HashCode lookup_key;
326 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
327 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
329 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
331 //FIXME how long to wait for results?
332 GNUNET_DHT_get_start(dht_handle, timeout,
333 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
335 5, //Replication level FIXME
337 &rtype, //xquery FIXME this is bad
338 sizeof(GNUNET_GNS_RECORD_PKEY),
339 &process_authority_dht_result,
345 * Function called when we get a result from the dht
348 * @param cls the query handle
349 * @param exp lifetime
350 * @param key the key the record was stored under
351 * @param get_path get path
352 * @param get_path_length get path length
353 * @param put_path put path
354 * @param put_path_length put path length
355 * @param type the block type
356 * @param size the size of the record
357 * @param data the record data
360 process_name_dht_result(void* cls,
361 struct GNUNET_TIME_Absolute exp,
362 const GNUNET_HashCode * key,
363 const struct GNUNET_PeerIdentity *get_path,
364 unsigned int get_path_length,
365 const struct GNUNET_PeerIdentity *put_path,
366 unsigned int put_path_length,
367 enum GNUNET_BLOCK_Type type,
368 size_t size, const void *data)
370 struct GNUNET_GNS_PendingQuery *query;
371 uint32_t num_records;
374 struct GNUNET_CRYPTO_RsaSignature *signature;
375 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
378 GNUNET_HashCode zone, name_hash;
383 query = (struct GNUNET_GNS_PendingQuery *)cls;
386 num_records = ntohl(*pos);
387 struct GNUNET_NAMESTORE_RecordData rd[num_records];
389 pos += sizeof(uint32_t);
391 for (i=0; i<num_records; i++)
393 namelen = ntohs(*pos);
394 pos += sizeof(uint16_t);
396 //name must be 0 terminated
400 rd[i].record_type = ntohl(*pos);
401 pos += sizeof(uint32_t);
403 rd[i].data_size = ntohl(*pos);
404 pos += sizeof(uint32_t);
407 pos += rd[i].data_size;
409 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
410 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
411 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
413 rd[i].flags = ntohs(*pos);
414 pos += sizeof(uint16_t);
417 if (strcmp(name, query->name) && rd[i].record_type == query->type)
424 if ((((char*)data)-pos) <
425 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
426 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
428 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
429 "Cannot parse signature/key in DHT response. Corrupted or Missing");
433 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
434 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
436 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
438 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
439 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
442 GNUNET_NAMESTORE_record_put (namestore_handle,
449 &on_namestore_record_put_result, //cont
454 //FIXME: add records to query handle, but on stack!
455 //do we need records in query handle? can't we just
456 //pass them to reply_to_dns?
457 reply_to_dns(query, num_records, rd);
461 * data is a serialized GNS record of type
462 * Check if record type and name match in query and reply
468 * Start DHT lookup for a (name -> query->record_type) record in
469 * query->authority's zone
471 * @param query the pending gns query
472 * @param name the name to query record
475 resolve_name_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
477 struct GNUNET_TIME_Relative timeout;
478 GNUNET_HashCode name_hash;
479 GNUNET_HashCode lookup_key;
481 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
482 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
484 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
486 //FIXME how long to wait for results?
487 GNUNET_DHT_get_start(dht_handle, timeout,
488 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
490 5, //Replication level FIXME
492 &query->type, //xquery
494 &process_name_dht_result,
501 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone);
504 * This is a callback function that should give us only PKEY
505 * records. Used to query the namestore for the authority (PKEY)
508 * @param cls the pending query
509 * @param zone our zone hash
510 * @param name the name for which we need an authority
511 * @param record_type the type of record (PKEY)
512 * @param expiration expiration date of the record
513 * @param flags namestore record flags
514 * @param sig_loc the location of the record in the signature tree
515 * @param size the size of the record
516 * @param data the record data
519 process_authority_lookup(void* cls,
520 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
521 struct GNUNET_TIME_Absolute expiration,
523 unsigned int rd_count,
524 const struct GNUNET_NAMESTORE_RecordData *rd,
525 const struct GNUNET_CRYPTO_RsaSignature *signature)
527 struct GNUNET_GNS_PendingQuery *query;
528 GNUNET_HashCode zone;
530 query = (struct GNUNET_GNS_PendingQuery *)cls;
531 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
534 * No authority found in namestore.
539 * We did not find an authority in the namestore
540 * _IF_ the current authoritative zone is us we cannot resolve
541 * _ELSE_ we can still check the dht
543 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
545 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
546 //FIXME return NX answer
553 resolve_authority_dht(query, name);
557 //Note only 1 pkey should have been returned.. anything else would be strange
559 * We found an authority that may be able to help us
563 GNUNET_assert(rd->record_type == GNUNET_GNS_RECORD_PKEY);
564 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
565 GNUNET_CRYPTO_hash(rd->data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
566 GNUNET_free_non_null(query->authority);
567 query->authority = pkey_hash;
568 resolve_name(query, query->authority);
574 * Reply to client with the result from our lookup.
576 * @param answer the pending query used in the lookup
579 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer, uint32_t rd_count,
580 const struct GNUNET_NAMESTORE_RecordData *rd)
582 struct GNUNET_DNSPARSER_Flags dnsflags;
587 struct GNUNET_DNSPARSER_Packet *packet = answer->p;
588 struct GNUNET_DNSPARSER_Record answer_records[answer->num_records];
589 packet->answers = answer_records;
591 len = sizeof(struct GNUNET_DNSPARSER_Record*);
592 for (i=0; i < rd_count; i++)
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
595 "Adding type %d to DNS response\n", rd[i].record_type);
596 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", answer->name);
597 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "OName: %s\n", answer->original_name);
598 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
599 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
600 answer_records[i].name = answer->original_name; //FIXME yes?
601 answer_records[i].type = rd[i].record_type;
602 answer_records[i].data.raw.data_len = rd[i].data_size;
603 answer_records[i].data.raw.data = (char*)rd[i].data;
604 answer_records[i].expiration_time = rd[i].expiration;
605 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
606 //GNUNET_free(i->record); DO this later!
610 * FIXME how to handle auth, additional etc
611 * PKEY might be auth, != name,record_type additional
613 packet->num_queries = 0;
614 packet->num_additional_records = 0;
615 packet->num_answers = rd_count; //answer->num_records;
616 //packet.num_authority_records = 0;//answer->num_authority_records;
618 dnsflags.authoritative_answer = 1;
619 dnsflags.message_truncated = 0;
620 dnsflags.recursion_desired = 0;
621 dnsflags.authenticated_data = 0;
622 dnsflags.checking_disabled = 1;
624 dnsflags.recursion_available = 0;
625 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
627 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
629 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
630 dnsflags.query_or_response = 1;
631 packet->flags = dnsflags;
633 //FIXME this is silently discarded
634 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
635 "Building DNS response\n");
636 ret = GNUNET_DNSPARSER_pack (packet,
637 1024, /* FIXME magic from dns redirector */
640 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
641 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
642 if (ret == GNUNET_OK)
644 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
645 "Answering DNS request\n");
646 GNUNET_DNS_request_answer(answer->request_handle,
649 //GNUNET_free(answer);
650 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
651 //FIXME return code, free datastructures
655 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
656 "Error building DNS response! (ret=%d)", ret);
662 * Namestore calls this function if we have an entry for this name.
663 * (or data=null to indicate the lookup has finished)
665 * @param cls the pending query
666 * @param zone the zone of the lookup
667 * @param name the name looked up
668 * @param record_type the record type
669 * @param expiration lifetime of the record
670 * @param flags record flags
671 * @param sig_loc location of the record in the signature tree
672 * @param size the size of the record
673 * @param data the record data
676 process_authoritative_result(void* cls,
677 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
678 struct GNUNET_TIME_Absolute expiration,
679 const char *name, unsigned int rd_count,
680 const struct GNUNET_NAMESTORE_RecordData *rd,
681 const struct GNUNET_CRYPTO_RsaSignature *signature)
683 struct GNUNET_GNS_PendingQuery *query;
684 struct GNUNET_GNS_QueryRecordList *qrecord;
685 struct GNUNET_NAMESTORE_RecordData *record;
686 struct GNUNET_TIME_Relative remaining_time;
687 GNUNET_HashCode zone;
689 query = (struct GNUNET_GNS_PendingQuery *) cls;
690 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
691 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
693 //FIXME Handle results in rd
699 * Lookup terminated and no results
700 * -> DHT Phase unless data is recent
702 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
703 "Namestore lookup for %s terminated without results\n", name);
706 * if this is not our zone we cannot rely on the namestore to be
707 * complete. -> Query DHT
709 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
711 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
712 if (remaining_time.rel_value == 0)
714 resolve_name_dht(query, name);
719 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record is still recent. No DHT lookup\n");
724 * Our zone and no result? Cannot resolve TT
725 * FIXME modify query to say NX
727 GNUNET_assert(query->answered == 0);
728 reply_to_dns(query, 0, NULL); //answered should be 0
737 * FIXME Check record expiration and dht expiration
738 * consult dht if necessary
740 if (remaining_time.rel_value == 0)
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
743 "This dht entry is old. Refreshing.\n");
744 resolve_name_dht(query, name);
747 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
748 "Processing additional result %s from namestore\n", name);
750 for (i=0; i<rd_count;i++)
752 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
755 //FIXME there is a catch here...
756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
759 // A time will come when this has to be freed
760 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
761 record = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
762 qrecord->record = record;
764 //fixme into gns_util
765 //parse_record(rd[i]->data, rd[i]->data_size, 0, record);
766 GNUNET_CONTAINER_DLL_insert(query->records_head,
769 query->num_records++;
772 //we need to resolve to the original name in the end though...
773 //keep in mind. This can also be done later probably
774 //record->name = (char*)query->original_name;
777 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
780 reply_to_dns(query, rd_count, rd);
785 * Determine if this name is canonical.
787 * a.b.gnunet = not canonical
790 * @param name the name to test
791 * @return 1 if canonical
794 is_canonical(char* name)
796 uint32_t len = strlen(name);
799 for (i=0; i<len; i++)
801 if (*(name+i) == '.')
808 * Move one level up in the domain hierarchy and return the
809 * passed top level domain.
810 * FIXME this needs a better name
812 * @param name the domain
815 char* pop_tld(char* name)
819 if (is_canonical(name))
822 for (len = strlen(name); len > 0; len--)
824 if (*(name+len) == '.')
831 name[len] = '\0'; //terminate string
838 * The first phase of resolution.
839 * First check if the name is canonical.
840 * If it is then try to resolve directly.
841 * If not then first have to resolve the authoritative entities.
843 * @param query the pending lookup
844 * @param zone the zone we are currently resolving in
847 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone)
849 if (is_canonical(query->name))
851 //We only need to check this zone's ns
852 GNUNET_NAMESTORE_lookup_record(namestore_handle,
856 &process_authoritative_result,
861 //We have to resolve the authoritative entity
862 char *new_authority = pop_tld(query->name);
863 GNUNET_NAMESTORE_lookup_record(namestore_handle,
866 GNUNET_GNS_RECORD_PKEY,
867 &process_authority_lookup,
873 * Entry point for name resolution
874 * Lookup local namestore of our zone.
876 * Setup a new query and try to resolve
878 * @param rh the request handle of the DNS request from a client
879 * @param name the name to look up
880 * @param id the id of the dns request (for the reply)
881 * @param type the record type to look for
884 start_resolution(struct GNUNET_DNS_RequestHandle *rh,
885 struct GNUNET_DNSPARSER_Packet *p,
886 char* name, uint16_t id, uint16_t type)
888 struct GNUNET_GNS_PendingQuery *query;
890 //FIXME remove .gnunet here from name
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
892 query = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
894 query->original_name = name; //Full name of original query
897 //FIXME do not forget to free!!
898 query->name = GNUNET_malloc(strlen(name)-strlen(gnunet_tld) + 1);
899 memset(query->name, 0, strlen(name)-strlen(gnunet_tld) + 1);
900 memcpy(query->name, name, strlen(name)-strlen(gnunet_tld));
903 query->request_handle = rh;
905 //Start resolution in our zone
906 resolve_name(query, &zone_hash);
910 * The DNS request handler
911 * Called for every incoming DNS request.
914 * @param rh request handle to user for reply
915 * @param request_length number of bytes in request
916 * @param request udp payload of the DNS request
919 handle_dns_request(void *cls,
920 struct GNUNET_DNS_RequestHandle *rh,
921 size_t request_length,
924 struct GNUNET_DNSPARSER_Packet *p;
928 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
929 p = GNUNET_DNSPARSER_parse (request, request_length);
933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
934 "Received malformed DNS packet, leaving it untouched\n");
935 GNUNET_DNS_request_forward (rh);
940 * Check tld and decide if we or
941 * legacy dns is responsible
943 * FIXME now in theory there could be more than 1 query in the request
944 * but if this is case we get into trouble:
945 * either we query the GNS or the DNS. We cannot do both!
946 * So I suggest to either only allow a single query per request or
947 * only allow GNS or DNS requests.
948 * The way it is implemented here now is buggy and will lead to erratic
949 * behaviour (if multiple queries are present).
951 for (i=0;i<p->num_queries;i++)
953 tldoffset = p->queries[i].name + strlen(p->queries[i].name);
955 while ((*tldoffset) != '.')
958 if (0 == strcmp(tldoffset, gnunet_tld))
960 start_resolution(rh, p, p->queries[i].name, p->id, p->queries[i].type);
965 * This request does not concern us. Forward to real DNS.
967 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
968 "Request for %s is forwarded to DNS\n", p->queries[i].name);
969 GNUNET_DNS_request_forward (rh);
975 * test function that stores some data in the namestore
978 put_some_records(void)
980 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
981 /* put a few records into namestore */
982 char* ipA = "1.2.3.4";
983 char* ipB = "5.6.7.8";
984 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create (); struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
985 bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
987 GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
989 GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
991 GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
993 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
994 struct in_addr *bob_web = GNUNET_malloc(sizeof(struct in_addr));
995 struct GNUNET_NAMESTORE_RecordData rda;
996 struct GNUNET_NAMESTORE_RecordData rdb;
997 struct GNUNET_NAMESTORE_RecordData rdb_web;
999 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
1000 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob_web));
1002 rda.data_size = sizeof(struct in_addr);
1003 rdb_web.data_size = sizeof(struct in_addr);
1004 rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1007 rdb_web.data = bob_web;
1008 rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
1009 rdb_web.record_type = GNUNET_GNS_RECORD_TYPE_A;
1010 rdb.record_type = GNUNET_GNS_RECORD_PKEY;
1011 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1012 rda.expiration = GNUNET_TIME_absolute_get_forever ();
1013 rdb.expiration = GNUNET_TIME_absolute_get_forever ();
1015 //alice.gnunet A IN 1.2.3.4
1016 GNUNET_NAMESTORE_record_create (namestore_handle,
1023 //www.bob.gnunet A IN 5.6.7.8
1024 GNUNET_NAMESTORE_record_create (namestore_handle,
1030 GNUNET_NAMESTORE_record_put(namestore_handle,
1033 GNUNET_TIME_absolute_get_forever (),
1042 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1044 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1048 * Function used to put all records successively into the DHT.
1050 * @param cls the closure (NULL)
1051 * @param zone our root zone hash
1052 * @param name the name of the record
1053 * @param record_type the type of the record
1054 * @param expiration lifetime of the record
1055 * @param flags flags of the record
1056 * @param sig_loc location of record in signature tree
1057 * @param size size of the record
1058 * @param record_data the record data
1061 put_gns_record(void *cls,
1062 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1063 struct GNUNET_TIME_Absolute expiration,
1065 unsigned int rd_count,
1066 const struct GNUNET_NAMESTORE_RecordData *rd,
1067 const struct GNUNET_CRYPTO_RsaSignature *signature)
1069 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1070 struct GNUNET_TIME_Relative timeout;
1071 GNUNET_HashCode name_hash;
1072 GNUNET_HashCode xor_hash;
1074 if (NULL == name) //We're done
1076 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1080 * FIXME magic number 20 move to config file
1082 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1083 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1084 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1085 GNUNET_DHT_put (dht_handle, &xor_hash,
1086 5, //replication level
1088 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1093 NULL, //FIXME continuation needed? success check? yes ofc
1094 NULL); //cls for cont
1096 num_public_records++;
1099 * Reschedule periodic put
1101 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1102 &update_zone_dht_next,
1108 * Periodically iterate over our zone and store everything in dht
1111 * @param tc task context
1114 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1116 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1117 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1118 (3600/num_public_records));
1119 num_public_records = 0; //start counting again
1120 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1122 GNUNET_NAMESTORE_RF_AUTHORITY,
1123 GNUNET_NAMESTORE_RF_PRIVATE,
1129 * Process GNS requests.
1131 * @param cls closure
1132 * @param server the initialized server
1133 * @param c configuration to use
1136 run (void *cls, struct GNUNET_SERVER_Handle *server,
1137 const struct GNUNET_CONFIGURATION_Handle *c)
1140 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1141 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1143 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1145 nc = GNUNET_SERVER_notification_context_create (server, 1);
1147 /* FIXME - do some config parsing
1148 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1151 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1154 * Do gnunet dns init here
1156 dns_handle = GNUNET_DNS_connect(c,
1157 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1158 &handle_dns_request, /* rh */
1159 NULL); /* Closure */
1161 if (NULL == dns_handle)
1163 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1164 "Failed to connect to the dnsservice!\n");
1168 * handle to our local namestore
1170 namestore_handle = GNUNET_NAMESTORE_connect(c);
1172 if (NULL == namestore_handle)
1174 //FIXME do error handling;
1175 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1176 "Failed to connect to the namestore!\n");
1182 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1184 if (NULL == dht_handle)
1186 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1189 put_some_records(); //FIXME for testing
1192 * Schedule periodic put
1194 * We have roughly an hour for all records;
1196 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1197 60); //FIXME from cfg
1198 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1199 &update_zone_dht_start,
1201 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1207 * The main function for the GNS service.
1209 * @param argc number of arguments from the command line
1210 * @param argv command line arguments
1211 * @return 0 ok, 1 on error
1214 main (int argc, char *const *argv)
1220 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1225 /* end of gnunet-service-gns.c */