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, 3)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
47 /* Ignore for now not used anyway and probably never will */
48 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
49 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
50 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
51 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
56 struct AuthorityChain *prev;
58 struct AuthorityChain *next;
62 /* (local) name of the authority */
65 /* was the ns entry fresh */
69 struct GNUNET_GNS_ResolverHandle;
71 typedef void (*ResolutionResultProcessor) (void *cls,
72 struct GNUNET_GNS_ResolverHandle *rh,
74 const struct GNUNET_NAMESTORE_RecordData *rd);
83 * Handle to a currenty pending resolution
85 struct GNUNET_GNS_ResolverHandle
87 /* The name to resolve */
90 /* the request handle to reply to */
91 struct GNUNET_DNS_RequestHandle *request_handle;
93 /* the dns parser packet received */
94 struct GNUNET_DNSPARSER_Packet *packet;
96 /* the query parsed from the packet */
98 struct GNUNET_DNSPARSER_Query *query;
100 /* has this query been answered? how many matches */
103 /* the authoritative zone to query */
104 GNUNET_HashCode authority;
106 /* the name of the authoritative zone to query */
107 char *authority_name;
110 * we have an authority in namestore that
111 * may be able to resolve
115 /* a handle for dht lookups. should be NULL if no lookups are in progress */
116 struct GNUNET_DHT_GetHandle *get_handle;
118 /* timeout task for dht lookups */
119 GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
121 ResolutionResultProcessor proc;
125 struct AuthorityChain *authority_chain_head;
126 struct AuthorityChain *authority_chain_tail;
128 enum ResolutionStatus status;
132 struct ClientShortenHandle
134 struct GNUNET_SERVER_Client *client;
142 * Our handle to the DNS handler library
144 struct GNUNET_DNS_Handle *dns_handle;
147 * Our handle to the DHT
149 struct GNUNET_DHT_Handle *dht_handle;
152 * Our zone's private key
154 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
157 * Our handle to the namestore service
158 * FIXME maybe need a second handle for iteration
160 struct GNUNET_NAMESTORE_Handle *namestore_handle;
163 * Handle to iterate over our authoritative zone in namestore
165 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
168 * The configuration the GNS service is running with
170 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
173 * Our notification context.
175 static struct GNUNET_SERVER_NotificationContext *nc;
180 GNUNET_HashCode zone_hash;
183 * Our tld. Maybe get from config file
185 const char* gnunet_tld = ".gnunet";
188 * Useful for zone update for DHT put
190 static int num_public_records = 3600;
191 struct GNUNET_TIME_Relative dht_update_interval;
192 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
194 //static void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
197 * Reply to client with the result from our lookup.
199 * @param rh the request handle of the lookup
200 * @param rd_count the number of records to return
201 * @param rd the record data
204 reply_to_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
205 const struct GNUNET_NAMESTORE_RecordData *rd)
211 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
212 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
213 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
214 packet->answers = answer_records;
215 packet->additional_records = additional_records;
218 * Put records in the DNS packet and modify it
221 len = sizeof(struct GNUNET_DNSPARSER_Record*);
222 for (i=0; i < rd_count; i++)
225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
226 "Adding type %d to DNS response\n", rd[i].record_type);
227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
232 if (rd[i].record_type == rh->query->type)
234 answer_records[i].name = rh->query->name;
235 answer_records[i].type = rd[i].record_type;
236 answer_records[i].data.raw.data_len = rd[i].data_size;
237 answer_records[i].data.raw.data = (char*)rd[i].data;
238 answer_records[i].expiration_time = rd[i].expiration;
239 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
243 additional_records[i].name = rh->query->name;
244 additional_records[i].type = rd[i].record_type;
245 additional_records[i].data.raw.data_len = rd[i].data_size;
246 additional_records[i].data.raw.data = (char*)rd[i].data;
247 additional_records[i].expiration_time = rd[i].expiration;
248 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
252 packet->num_answers = rh->answered;
253 packet->num_additional_records = rd_count-(rh->answered);
255 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
256 packet->flags.authoritative_answer = 1;
258 packet->flags.authoritative_answer = 0;
261 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
263 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
265 packet->flags.query_or_response = 1;
271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
272 "Building DNS response\n");
273 ret = GNUNET_DNSPARSER_pack (packet,
274 1024, /* FIXME magic from dns redirector */
277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
278 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
279 if (ret == GNUNET_OK)
281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
282 "Answering DNS request\n");
283 GNUNET_DNS_request_answer(rh->request_handle,
286 //GNUNET_free(answer);
287 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
291 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
292 "Error building DNS response! (ret=%d)", ret);
295 GNUNET_free(rh->name);
301 * Task run during shutdown.
307 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 /* Kill zone task for it may make the scheduler hang */
310 if (zone_update_taskid)
311 GNUNET_SCHEDULER_cancel(zone_update_taskid);
313 GNUNET_DNS_disconnect(dns_handle);
314 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
315 GNUNET_DHT_disconnect(dht_handle);
319 * Callback when record data is put into namestore
321 * @param cls the closure
322 * @param success GNUNET_OK on success
323 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
326 on_namestore_record_put_result(void *cls,
330 if (GNUNET_NO == success)
332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
335 else if (GNUNET_YES == success)
337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
338 "records successfully put in namestore\n");
342 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
343 "Error putting records into namestore: %s\n", emsg);
347 * Handle timeout for DHT requests
349 * @param cls the request handle as closure
350 * @param tc the task context
353 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct GNUNET_GNS_ResolverHandle *rh = cls;
357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
358 "dht lookup for query %s (type=%d) timed out.\n",
359 rh->name, rh->query->type);
361 GNUNET_DHT_get_stop (rh->get_handle);
362 rh->proc(rh->proc_cls, rh, 0, NULL);
368 * Function called when we get a result from the dht
371 * @param cls the request handle
372 * @param exp lifetime
373 * @param key the key the record was stored under
374 * @param get_path get path
375 * @param get_path_length get path length
376 * @param put_path put path
377 * @param put_path_length put path length
378 * @param type the block type
379 * @param size the size of the record
380 * @param data the record data
383 process_record_dht_result(void* cls,
384 struct GNUNET_TIME_Absolute exp,
385 const GNUNET_HashCode * key,
386 const struct GNUNET_PeerIdentity *get_path,
387 unsigned int get_path_length,
388 const struct GNUNET_PeerIdentity *put_path,
389 unsigned int put_path_length,
390 enum GNUNET_BLOCK_Type type,
391 size_t size, const void *data)
393 struct GNUNET_GNS_ResolverHandle *rh;
394 struct GNSNameRecordBlock *nrb;
395 uint32_t num_records;
397 char* rd_data = (char*)data;
401 GNUNET_HashCode zone, name_hash;
402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
407 //FIXME maybe check expiration here, check block type
409 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
410 nrb = (struct GNSNameRecordBlock*)data;
412 /* stop lookup and timeout task */
413 GNUNET_DHT_get_stop (rh->get_handle);
414 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
416 rh->get_handle = NULL;
417 name = (char*)&nrb[1];
418 num_records = ntohl(nrb->rd_count);
420 struct GNUNET_NAMESTORE_RecordData rd[num_records];
422 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
423 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
425 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
430 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
434 for (i=0; i<num_records; i++)
436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
437 "Got name: %s (wanted %s)\n", name, rh->name);
438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
439 "Got type: %d (wanted %d)\n",
440 rd[i].record_type, rh->query->type);
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
442 "Got data length: %d\n", rd[i].data_size);
443 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
444 "Got flag %d\n", rd[i].flags);
446 if ((strcmp(name, rh->name) == 0) &&
447 (rd[i].record_type == rh->query->type))
454 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
455 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
458 * FIXME check pubkey against existing key in namestore?
459 * https://gnunet.org/bugs/view.php?id=2179
462 /* Save to namestore */
463 GNUNET_NAMESTORE_record_put (namestore_handle,
470 &on_namestore_record_put_result, //cont
474 rh->proc(rh->proc_cls, rh, num_records, rd);
476 rh->proc(rh->proc_cls, rh, 0, NULL);
483 * Start DHT lookup for a (name -> query->record_type) record in
484 * rh->authority's zone
486 * @param rh the pending gns query context
487 * @param name the name to query record
490 resolve_record_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
493 GNUNET_HashCode name_hash;
494 GNUNET_HashCode lookup_key;
495 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
497 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
498 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
499 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
502 "starting dht lookup for %s with key: %s\n",
503 rh->name, (char*)&lookup_key_string);
505 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
506 &dht_lookup_timeout, rh);
508 xquery = htonl(rh->query->type);
509 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
510 DHT_OPERATION_TIMEOUT,
511 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
513 DHT_GNS_REPLICATION_LEVEL,
517 &process_record_dht_result,
524 * Namestore calls this function if we have record for this name.
525 * (or with rd_count=0 to indicate no matches)
527 * @param cls the pending query
528 * @param key the key of the zone we did the lookup
529 * @param expiration expiration date of the namestore entry
530 * @param name the name for which we need an authority
531 * @param rd_count the number of records with 'name'
532 * @param rd the record data
533 * @param signature the signature of the authority for the record data
536 process_record_lookup_ns(void* cls,
537 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
538 struct GNUNET_TIME_Absolute expiration,
539 const char *name, unsigned int rd_count,
540 const struct GNUNET_NAMESTORE_RecordData *rd,
541 const struct GNUNET_CRYPTO_RsaSignature *signature)
543 struct GNUNET_GNS_ResolverHandle *rh;
544 struct GNUNET_TIME_Relative remaining_time;
545 GNUNET_HashCode zone;
547 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
548 GNUNET_CRYPTO_hash(key,
549 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
551 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
557 rh->status |= EXISTS;
560 if (remaining_time.rel_value == 0)
562 rh->status |= EXPIRED;
568 * Lookup terminated and no results
570 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
571 "Namestore lookup for %s terminated without results\n", name);
575 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
576 "Record %s unknown in namestore\n",
579 * Our zone and no result? Cannot resolve TT
581 rh->proc(rh->proc_cls, rh, 0, NULL);
588 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
589 "Processing additional result %s from namestore\n", name);
591 for (i=0; i<rd_count;i++)
594 if (rd[i].record_type != rh->query->type)
597 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
600 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
611 if (rh->answered == 0)
613 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
614 "No answers found. This is odd!\n");
615 rh->proc(rh->proc_cls, rh, 0, NULL);
619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
622 rh->proc(rh->proc_cls, rh, rd_count, rd);
627 * The final phase of resolution.
628 * This is a name that is canonical and we do not have a delegation.
630 * @param rh the pending lookup
633 resolve_record_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
637 * Try to resolve this record in our namestore.
638 * The name to resolve is now in rh->authority_name
639 * since we tried to resolve it to an authority
642 GNUNET_NAMESTORE_lookup_record(namestore_handle,
646 &process_record_lookup_ns,
653 * Handle timeout for DHT requests
655 * @param cls the request handle as closure
656 * @param tc the task context
659 dht_authority_lookup_timeout(void *cls,
660 const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct GNUNET_GNS_ResolverHandle *rh = cls;
664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
665 "dht lookup for query %s (type=%d) timed out.\n",
666 rh->name, rh->query->type);
668 GNUNET_DHT_get_stop (rh->get_handle);
669 if (strcmp(rh->name, "") == 0)
672 * promote authority back to name and try to resolve record
674 strcpy(rh->name, rh->authority_name);
676 rh->proc(rh->proc_cls, rh, 0, NULL);
680 static void resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh);
683 * Function called when we get a result from the dht
684 * for our query. Recursively tries to resolve PKEYs
687 * @param cls the request handle
688 * @param exp lifetime
689 * @param key the key the record was stored under
690 * @param get_path get path
691 * @param get_path_length get path length
692 * @param put_path put path
693 * @param put_path_length put path length
694 * @param type the block type
695 * @param size the size of the record
696 * @param data the record data
699 process_authority_dht_result(void* cls,
700 struct GNUNET_TIME_Absolute exp,
701 const GNUNET_HashCode * key,
702 const struct GNUNET_PeerIdentity *get_path,
703 unsigned int get_path_length,
704 const struct GNUNET_PeerIdentity *put_path,
705 unsigned int put_path_length,
706 enum GNUNET_BLOCK_Type type,
707 size_t size, const void *data)
709 struct GNUNET_GNS_ResolverHandle *rh;
710 struct GNSNameRecordBlock *nrb;
711 uint32_t num_records;
713 char* rd_data = (char*) data;
716 GNUNET_HashCode zone, name_hash;
721 //FIXME check expiration?
723 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
724 nrb = (struct GNSNameRecordBlock*)data;
726 /* stop dht lookup and timeout task */
727 GNUNET_DHT_get_stop (rh->get_handle);
728 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
730 rh->get_handle = NULL;
731 num_records = ntohl(nrb->rd_count);
732 name = (char*)&nrb[1];
734 struct GNUNET_NAMESTORE_RecordData rd[num_records];
736 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
737 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
739 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
744 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
748 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
749 "Got name: %s (wanted %s)\n", name, rh->authority_name);
750 for (i=0; i<num_records; i++)
753 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
754 "Got name: %s (wanted %s)\n", name, rh->authority_name);
755 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
756 "Got type: %d (wanted %d)\n",
757 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
759 "Got data length: %d\n", rd[i].data_size);
760 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
761 "Got flag %d\n", rd[i].flags);
763 if ((strcmp(name, rh->authority_name) == 0) &&
764 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
768 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
769 struct AuthorityChain *auth =
770 GNUNET_malloc(sizeof(struct AuthorityChain));
771 auth->zone = rh->authority;
772 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
773 memset(auth->name, 0, strlen(rh->authority_name)+1);
774 strcpy(auth->name, rh->authority_name);
775 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
776 rh->authority_chain_tail,
783 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
784 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
786 /* Save to namestore */
787 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
789 GNUNET_NAMESTORE_record_put (namestore_handle,
796 &on_namestore_record_put_result, //cont
805 if (strcmp(rh->name, "") == 0)
806 rh->proc(rh->proc_cls, rh, 0, NULL);
808 resolve_delegation_from_dht(rh);
813 * should never get here unless false dht key/put
814 * block plugin should handle this
817 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
823 * Process DHT lookup result for record.
825 * @param cls the closure
826 * @param rh resolver handle
827 * @param rd_count number of results (always 0)
828 * @param rd record data (always NULL)
831 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
832 unsigned int rd_count,
833 const struct GNUNET_NAMESTORE_RecordData *rd)
837 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
838 "No records for %s found in DHT. Aborting\n",
840 /* give up, cannot resolve */
841 reply_to_dns(NULL, rh, 0, NULL);
845 /* results found yay */
846 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
847 "Record resolved from namestore!");
848 reply_to_dns(NULL, rh, rd_count, rd);
854 * Process namestore lookup result for record.
856 * @param cls the closure
857 * @param rh resolver handle
858 * @param rd_count number of results (always 0)
859 * @param rd record data (always NULL)
862 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
863 unsigned int rd_count,
864 const struct GNUNET_NAMESTORE_RecordData *rd)
868 /* ns entry expired. try dht */
869 if (rh->status & (EXPIRED | !EXISTS))
871 rh->proc = &process_record_result_dht;
872 resolve_record_from_dht(rh);
875 /* give up, cannot resolve */
876 reply_to_dns(NULL, rh, 0, NULL);
880 /* results found yay */
881 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
882 "Record resolved from namestore!");
883 reply_to_dns(NULL, rh, rd_count, rd);
889 * Determine if this name is canonical.
891 * a.b.gnunet = not canonical
894 * @param name the name to test
895 * @return 1 if canonical
898 is_canonical(char* name)
900 uint32_t len = strlen(name);
903 for (i=0; i<len; i++)
905 if (*(name+i) == '.')
912 * Move one level up in the domain hierarchy and return the
913 * passed top level domain.
915 * @param name the domain
916 * @param dest the destination where the tld will be put
919 pop_tld(char* name, char* dest)
923 if (is_canonical(name))
930 for (len = strlen(name); len > 0; len--)
932 if (*(name+len) == '.')
942 strcpy(dest, (name+len+1));
946 * DHT resolution for delegation finished. Processing result.
948 * @param cls the closure
949 * @param rh resolver handle
950 * @param rd_count number of results (always 0)
951 * @param rd record data (always NULL)
954 process_dht_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
955 unsigned int rd_count,
956 const struct GNUNET_NAMESTORE_RecordData *rd)
958 if (strcmp(rh->name, "") == 0)
960 /* We resolved full name for delegation. resolving record */
961 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
962 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
963 rh->proc = &process_record_result_ns;
964 resolve_record_from_ns(rh);
969 * we still have some left
971 if (is_canonical(rh->name))
973 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
974 "Resolving canonical record %s in ns\n", rh->name);
975 rh->proc = &process_record_result_ns;
976 resolve_record_from_ns(rh);
979 /* give up, cannot resolve */
980 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
981 "Cannot fully resolve delegation for %s via DHT!\n",
983 reply_to_dns(NULL, rh, 0, NULL);
988 * Start DHT lookup for a name -> PKEY (compare NS) record in
989 * rh->authority's zone
991 * @param rh the pending gns query
992 * @param name the name of the PKEY record
995 resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
998 GNUNET_HashCode name_hash;
999 GNUNET_HashCode lookup_key;
1001 GNUNET_CRYPTO_hash(rh->authority_name,
1002 strlen(rh->authority_name),
1004 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1006 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1007 &dht_authority_lookup_timeout,
1010 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1012 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1013 DHT_OPERATION_TIMEOUT,
1014 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1016 DHT_GNS_REPLICATION_LEVEL,
1020 &process_authority_dht_result,
1027 * Namestore resolution for delegation finished. Processing result.
1029 * @param cls the closure
1030 * @param rh resolver handle
1031 * @param rd_count number of results (always 0)
1032 * @param rd record data (always NULL)
1035 process_ns_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1036 unsigned int rd_count,
1037 const struct GNUNET_NAMESTORE_RecordData *rd)
1039 if (strcmp(rh->name, "") == 0)
1041 /* We resolved full name for delegation. resolving record */
1042 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1043 "Resolved full name for delegation. resolving record ''\n");
1044 rh->proc = &process_record_result_ns;
1045 resolve_record_from_ns(rh);
1050 * we still have some left
1051 * check if ns entry is fresh
1053 if (rh->status & (EXISTS | !EXPIRED))
1055 if (is_canonical(rh->name))
1057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1058 "Resolving canonical record %s\n", rh->name);
1059 rh->proc = &process_record_result_ns;
1060 resolve_record_from_ns(rh);
1064 /* give up, cannot resolve */
1065 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1066 "Cannot fully resolve delegation for %s!\n",
1068 reply_to_dns(NULL, rh, 0, NULL);
1073 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1074 "Trying to resolve delegation for %s via DHT\n",
1076 rh->proc = &process_dht_delegation_dns;
1077 resolve_delegation_from_dht(rh);
1081 static void resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh);
1084 * This is a callback function that should give us only PKEY
1085 * records. Used to query the namestore for the authority (PKEY)
1086 * for 'name'. It will recursively try to resolve the
1087 * authority for a given name from the namestore.
1089 * @param cls the pending query
1090 * @param key the key of the zone we did the lookup
1091 * @param expiration expiration date of the record data set in the namestore
1092 * @param name the name for which we need an authority
1093 * @param rd_count the number of records with 'name'
1094 * @param rd the record data
1095 * @param signature the signature of the authority for the record data
1098 process_authority_lookup_ns(void* cls,
1099 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1100 struct GNUNET_TIME_Absolute expiration,
1102 unsigned int rd_count,
1103 const struct GNUNET_NAMESTORE_RecordData *rd,
1104 const struct GNUNET_CRYPTO_RsaSignature *signature)
1106 struct GNUNET_GNS_ResolverHandle *rh;
1107 struct GNUNET_TIME_Relative remaining_time;
1108 GNUNET_HashCode zone;
1111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1114 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1115 GNUNET_CRYPTO_hash(key,
1116 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1118 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1124 rh->status |= EXISTS;
1127 if (remaining_time.rel_value == 0)
1129 rh->status |= EXPIRED;
1133 * No authority found in namestore.
1138 * We did not find an authority in the namestore
1143 * Promote this authority back to a name maybe it is
1146 if (strcmp(rh->name, "") == 0)
1148 /* simply promote back */
1149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1150 "Promoting %s back to name\n", rh->authority_name);
1151 strcpy(rh->name, rh->authority_name);
1155 /* add back to existing name */
1156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1157 "Adding %s back to %s\n",
1158 rh->authority_name, rh->name);
1159 new_name = GNUNET_malloc(strlen(rh->name)
1160 + strlen(rh->authority_name) + 2);
1161 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1162 strcpy(new_name, rh->name);
1163 strcpy(new_name+strlen(new_name)+1, ".");
1164 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1165 GNUNET_free(rh->name);
1166 rh->name = new_name;
1168 rh->proc(rh->proc_cls, rh, 0, NULL);
1172 //Note only 1 pkey should have been returned.. anything else would be strange
1174 * We found an authority that may be able to help us
1175 * move on with query
1178 for (i=0; i<rd_count;i++)
1181 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1184 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1188 if (remaining_time.rel_value == 0)
1190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1191 "This dht entry is expired.\n");
1192 rh->authority_chain_head->fresh = 0;
1193 rh->proc(rh->proc_cls, rh, 0, NULL);
1201 * Resolve rest of query with new authority
1203 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1204 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1205 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1206 auth->zone = rh->authority;
1207 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1208 memset(auth->name, 0, strlen(rh->authority_name)+1);
1209 strcpy(auth->name, rh->authority_name);
1210 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1211 rh->authority_chain_tail,
1215 * We are done with PKEY resolution if name is empty
1216 * else resolve again with new authority
1218 if (strcmp(rh->name, "") == 0)
1219 rh->proc(rh->proc_cls, rh, 0, NULL);
1221 resolve_delegation_from_ns(rh);
1228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1229 "Authority lookup successful but no PKEY... never get here\n");
1230 rh->proc(rh->proc_cls, rh, 0, NULL);
1235 * Resolve the delegation chain for the request
1237 * @param rh the resolver handle
1240 resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
1243 pop_tld(rh->name, rh->authority_name);
1244 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1247 GNUNET_GNS_RECORD_PKEY,
1248 &process_authority_lookup_ns,
1254 * Entry point for name resolution
1255 * Setup a new query and try to resolve
1257 * @param request the request handle of the DNS request from a client
1258 * @param p the DNS query packet we received
1259 * @param q the DNS query we received parsed from p
1262 start_resolution_from_dns(struct GNUNET_DNS_RequestHandle *request,
1263 struct GNUNET_DNSPARSER_Packet *p,
1264 struct GNUNET_DNSPARSER_Query *q)
1266 struct GNUNET_GNS_ResolverHandle *rh;
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Starting resolution for %s (type=%d)!\n",
1272 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1275 rh->authority = zone_hash;
1278 rh->name = GNUNET_malloc(strlen(q->name)
1279 - strlen(gnunet_tld) + 1);
1281 strlen(q->name)-strlen(gnunet_tld) + 1);
1282 memcpy(rh->name, q->name,
1283 strlen(q->name)-strlen(gnunet_tld));
1285 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1287 rh->request_handle = request;
1289 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1290 rh->authority_chain_tail = rh->authority_chain_head;
1291 rh->authority_chain_head->zone = zone_hash;
1293 /* Start resolution in our zone */
1294 rh->proc = &process_ns_delegation_dns;
1295 resolve_delegation_from_ns(rh);
1299 * The DNS request handler
1300 * Called for every incoming DNS request.
1302 * @param cls closure
1303 * @param rh request handle to user for reply
1304 * @param request_length number of bytes in request
1305 * @param request udp payload of the DNS request
1308 handle_dns_request(void *cls,
1309 struct GNUNET_DNS_RequestHandle *rh,
1310 size_t request_length,
1311 const char *request)
1313 struct GNUNET_DNSPARSER_Packet *p;
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1317 p = GNUNET_DNSPARSER_parse (request, request_length);
1321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1322 "Received malformed DNS packet, leaving it untouched\n");
1323 GNUNET_DNS_request_forward (rh);
1328 * Check tld and decide if we or
1329 * legacy dns is responsible
1331 * FIXME now in theory there could be more than 1 query in the request
1332 * but if this is case we get into trouble:
1333 * either we query the GNS or the DNS. We cannot do both!
1334 * So I suggest to either only allow a single query per request or
1335 * only allow GNS or DNS requests.
1336 * The way it is implemented here now is buggy and will lead to erratic
1337 * behaviour (if multiple queries are present).
1339 if (p->num_queries == 0)
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "No Queries in DNS packet... forwarding\n");
1343 GNUNET_DNS_request_forward (rh);
1346 if (p->num_queries > 1)
1348 /* Note: We could also look for .gnunet */
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 ">1 queriy in DNS packet... odd. We only process #1\n");
1357 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1359 while ((*tldoffset) != '.')
1362 if (0 == strcmp(tldoffset, gnunet_tld))
1364 start_resolution_from_dns(rh, p, p->queries);
1369 * This request does not concern us. Forward to real DNS.
1371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1372 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1373 GNUNET_DNS_request_forward (rh);
1379 * Method called periodicattluy that triggers
1380 * iteration over root zone
1382 * @param cls closure
1383 * @param tc task context
1386 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1388 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1392 * Continuation for DHT put
1394 * @param cls closure
1395 * @param tc task context
1398 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1400 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1405 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1408 * Function used to put all records successively into the DHT.
1410 * @param cls the closure (NULL)
1411 * @param key the public key of the authority (ours)
1412 * @param expiration lifetime of the namestore entry
1413 * @param name the name of the records
1414 * @param rd_count the number of records in data
1415 * @param rd the record data
1416 * @param signature the signature for the record data
1419 put_gns_record(void *cls,
1420 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1421 struct GNUNET_TIME_Absolute expiration,
1423 unsigned int rd_count,
1424 const struct GNUNET_NAMESTORE_RecordData *rd,
1425 const struct GNUNET_CRYPTO_RsaSignature *signature)
1428 struct GNSNameRecordBlock *nrb;
1429 GNUNET_HashCode name_hash;
1430 GNUNET_HashCode xor_hash;
1431 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1432 uint32_t rd_payload_length;
1433 char* nrb_data = NULL;
1438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1439 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1440 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1446 "Putting records for %s into the DHT\n", name);
1448 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1450 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1451 + sizeof(struct GNSNameRecordBlock));
1453 if (signature != NULL)
1454 nrb->signature = *signature;
1456 nrb->public_key = *key;
1458 nrb->rd_count = htonl(rd_count);
1460 memset(&nrb[1], 0, strlen(name) + 1);
1461 memcpy(&nrb[1], name, strlen(name));
1463 nrb_data = (char*)&nrb[1];
1464 nrb_data += strlen(name) + 1;
1466 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1469 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1474 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1481 * calculate DHT key: H(name) xor H(pubkey)
1483 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1484 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1485 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1486 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1487 "putting records for %s under key: %s with size %d\n",
1488 name, (char*)&xor_hash_string, rd_payload_length);
1490 GNUNET_DHT_put (dht_handle, &xor_hash,
1491 DHT_GNS_REPLICATION_LEVEL,
1493 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1497 DHT_OPERATION_TIMEOUT,
1499 NULL); //cls for cont
1501 num_public_records++;
1504 * Reschedule periodic put
1506 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1507 &update_zone_dht_next,
1513 * Periodically iterate over our zone and store everything in dht
1516 * @param tc task context
1519 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1522 if (0 == num_public_records)
1524 dht_update_interval = GNUNET_TIME_relative_multiply(
1525 GNUNET_TIME_UNIT_SECONDS,
1530 dht_update_interval = GNUNET_TIME_relative_multiply(
1531 GNUNET_TIME_UNIT_SECONDS,
1532 (3600/num_public_records));
1534 num_public_records = 0; //start counting again
1535 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1537 GNUNET_NAMESTORE_RF_AUTHORITY,
1538 GNUNET_NAMESTORE_RF_PRIVATE,
1544 static void send_shorten_response(const char* name,
1545 struct ClientShortenHandle *csh);
1547 process_shorten_pseu_lookup_ns(void *cls,
1548 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1549 struct GNUNET_TIME_Absolute expire,
1551 unsigned int rd_len,
1552 const struct GNUNET_NAMESTORE_RecordData *rd,
1553 const struct GNUNET_CRYPTO_RsaSignature *signature)
1555 struct GNUNET_GNS_ResolverHandle *rh =
1556 (struct GNUNET_GNS_ResolverHandle *)cls;
1557 struct GNUNET_TIME_Relative remaining_time;
1564 rh->status |= EXISTS;
1567 if (remaining_time.rel_value == 0)
1569 rh->status |= EXPIRED;
1572 rh->proc(cls, rh, rd_len, rd);
1577 * Start DHT lookup for a PSEUdonym record in
1578 * rh->authority's zone
1580 * @param rh the pending gns query
1581 * @param name the name of the PKEY record
1584 resolve_pseu_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1587 GNUNET_HashCode name_hash;
1588 GNUNET_HashCode lookup_key;
1591 GNUNET_CRYPTO_hash("",
1595 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1597 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1598 &dht_lookup_timeout,
1601 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1603 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1604 DHT_OPERATION_TIMEOUT,
1605 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1607 DHT_GNS_REPLICATION_LEVEL,
1611 &process_authority_dht_result,
1618 handle_shorten_pseu_ns_result(void* cls,
1619 struct GNUNET_GNS_ResolverHandle *rh,
1621 const struct GNUNET_NAMESTORE_RecordData *rd);
1624 handle_shorten_zone_to_name(void *cls,
1625 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1626 struct GNUNET_TIME_Absolute expire,
1628 unsigned int rd_len,
1629 const struct GNUNET_NAMESTORE_RecordData *rd,
1630 const struct GNUNET_CRYPTO_RsaSignature *signature)
1632 struct GNUNET_GNS_ResolverHandle *rh =
1633 (struct GNUNET_GNS_ResolverHandle *)cls;
1634 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1638 /* we found a match in our own zone */
1641 result = GNUNET_malloc(strlen(rh->name) + strlen(name) + 1);
1642 memset(result, 0, strlen(rh->name) + strlen(name) + 1);
1643 memcpy(result, rh->name, strlen(rh->name));
1644 memcpy(result+strlen(rh->name)+1, name, strlen(name));
1646 send_shorten_response(result, csh);
1648 GNUNET_free(result);
1653 * Nothing in our zone
1654 * check PSEU for this authority in namestore
1656 rh->proc = &handle_shorten_pseu_ns_result;
1657 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1658 &rh->authority_chain_head->zone,
1660 GNUNET_GNS_RECORD_PSEU,
1661 &process_shorten_pseu_lookup_ns,
1667 * Process result from namestore delegation lookup
1668 * for shorten operation
1670 * @param cls the client shorten handle
1671 * @param rh the resolver handle
1672 * @param rd_count number of results (0)
1673 * @param rd data (NULL)
1676 handle_shorten_pseu_dht_result(void* cls,
1677 struct GNUNET_GNS_ResolverHandle *rh,
1679 const struct GNUNET_NAMESTORE_RecordData *rd)
1681 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1682 struct AuthorityChain *auth_chain;
1693 for (i=0; i < rd_len; i++)
1695 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1699 pseu = (char*) rd[i].data;
1700 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1701 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1702 memcpy(result, rh->name, strlen(rh->name));
1703 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1705 send_shorten_response(result, csh);
1707 GNUNET_free(result);
1713 * continue with next authority
1716 auth_chain = rh->authority_chain_head;
1718 if ((auth_chain->next->next == NULL) &&
1719 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1724 result = GNUNET_malloc(strlen(rh->name) + strlen(auth_chain->name) + 2);
1725 memset(result, 0, strlen(rh->name) + strlen(auth_chain->name) + 2);
1726 strcpy(result, rh->name);
1727 strcpy(result+strlen(rh->name)+1, ".");
1728 strcpy(result+strlen(rh->name)+2, auth_chain->name);
1729 send_shorten_response(result, csh);
1735 * Continue with next authority
1737 new_name = GNUNET_malloc(strlen(rh->name)+
1738 strlen(auth_chain->name) + 2);
1739 memset(new_name, 0, strlen(rh->name)+
1740 strlen(auth_chain->name) + 2);
1741 strcpy(new_name, rh->name);
1742 strcpy(new_name+strlen(rh->name)+1, ".");
1743 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1744 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1745 rh->authority_chain_tail,
1747 GNUNET_free(auth_chain->name);
1748 GNUNET_free(auth_chain);
1749 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1751 &rh->authority_chain_head->zone,
1752 &handle_shorten_zone_to_name,
1760 * Process result from namestore PSEU lookup
1761 * for shorten operation
1762 * FIXME do we need to check for own zone here?
1764 * @param cls the client shorten handle
1765 * @param rh the resolver handle
1766 * @param rd_count number of results (0 if none found)
1767 * @param rd data (NULL if none found)
1770 handle_shorten_pseu_ns_result(void* cls,
1771 struct GNUNET_GNS_ResolverHandle *rh,
1773 const struct GNUNET_NAMESTORE_RecordData *rd)
1775 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1776 struct AuthorityChain *auth_chain;
1787 for (i=0; i < rd_len; i++)
1789 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1793 pseu = (char*) rd[i].data;
1794 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1795 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1796 memcpy(result, rh->name, strlen(rh->name));
1797 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1799 send_shorten_response(result, csh);
1801 GNUNET_free(result);
1806 * No PSEU found. Ask DHT if expired.
1807 * Else contunue with next authority
1809 if (rh->status & (EXISTS | !EXPIRED))
1814 auth_chain = rh->authority_chain_head;
1815 new_name = GNUNET_malloc(strlen(rh->name)+
1816 strlen(auth_chain->name) + 2);
1817 memset(new_name, 0, strlen(rh->name)+
1818 strlen(auth_chain->name) + 2);
1819 strcpy(new_name, rh->name);
1820 strcpy(new_name+strlen(rh->name)+1, ".");
1821 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1823 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1824 rh->authority_chain_tail,
1827 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1829 &rh->authority_chain_head->zone,
1830 &handle_shorten_zone_to_name,
1838 rh->authority = rh->authority_chain_head->zone;
1839 rh->proc = &handle_shorten_pseu_dht_result;
1840 resolve_pseu_from_dht(rh);
1847 * Process result from namestore delegation lookup
1848 * for shorten operation
1850 * @param cls the client shorten handle
1851 * @param rh the resolver handle
1852 * @param rd_count number of results (0)
1853 * @param rd data (NULL)
1856 handle_shorten_delegation_result(void* cls,
1857 struct GNUNET_GNS_ResolverHandle *rh,
1859 const struct GNUNET_NAMESTORE_RecordData *rd)
1861 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1862 struct AuthorityChain *auth_chain;
1865 * At this point rh->name contains the part of the name
1866 * that we do not have a PKEY in our namestore to resolve.
1867 * The authority chain in the resolver handle is now
1868 * useful to backtrack if needed
1871 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1872 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
1874 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
1878 * This is our zone append .gnunet unless name is empty
1879 * (it shouldn't be, usually FIXME what happens if we
1880 * shorten to our zone to a "" record??)
1882 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
1886 csh->offset++; //FIXME needed?
1887 auth_chain = rh->authority_chain_head;
1888 /* backtrack authorities for pseu */
1889 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1892 &handle_shorten_zone_to_name,
1897 typedef void (*ShortenResponseProc) (void* cls, const char* name);
1900 * Shorten a given name
1902 * @param name the name to shorten
1903 * @param proc the processor to call when finished
1904 * @praram cls the closure to the processor
1907 shorten_name(char* name, struct ClientShortenHandle* csh)
1910 struct GNUNET_GNS_ResolverHandle *rh;
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "Starting resolution for %s (type=%d)!\n",
1914 name, GNUNET_GNS_RECORD_PKEY);
1916 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1917 rh->authority = zone_hash;
1919 rh->name = GNUNET_malloc(strlen(name)
1920 - strlen(gnunet_tld) + 1);
1922 strlen(name)-strlen(gnunet_tld) + 1);
1923 memcpy(rh->name, name,
1924 strlen(name)-strlen(gnunet_tld));
1926 csh->name = GNUNET_malloc(strlen(name)
1927 - strlen(gnunet_tld) + 1);
1929 strlen(name)-strlen(gnunet_tld) + 1);
1930 memcpy(rh->name, name,
1931 strlen(name)-strlen(gnunet_tld));
1933 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1935 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1936 rh->authority_chain_tail = rh->authority_chain_head;
1937 rh->authority_chain_head->zone = zone_hash;
1938 rh->proc = &handle_shorten_delegation_result;
1939 rh->proc_cls = (void*)csh;
1941 /* Start delegation resolution in our namestore */
1942 resolve_delegation_from_ns(rh);
1947 * Send shorten response back to client
1948 * FIXME this is without .gnunet!
1950 * @param cls the client handle in closure
1951 * @param name the shortened name result or NULL if cannot be shortened
1954 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n",
1958 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
1965 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage *)
1968 rmsg->unique_id = csh->unique_id;
1969 rmsg->key = csh->key;
1970 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
1972 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage *) +
1975 strcpy((char*)&rmsg[1], name);
1977 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
1978 (const struct GNUNET_MessageHeader *) &rmsg,
1981 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
1989 * Handle a shorten message from the api
1991 * @param cls the closure
1992 * @param client the client
1993 * @param message the message
1995 static void handle_shorten(void *cls,
1996 struct GNUNET_SERVER_Client * client,
1997 const struct GNUNET_MessageHeader * message)
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2001 size_t msg_size = 0;
2002 struct ClientShortenHandle *csh;
2004 if (ntohs (message->size) != sizeof (struct GNUNET_GNS_ClientShortenMessage))
2006 GNUNET_break_op (0);
2007 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2011 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2012 (struct GNUNET_GNS_ClientShortenMessage *) message;
2014 msg_size = ntohs(message->size);
2016 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2018 GNUNET_break_op (0);
2019 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2023 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2024 csh->client = client;
2025 csh->unique_id = sh_msg->unique_id;
2026 csh->key = sh_msg->key;
2028 //Offset in original name
2032 shorten_name((char*)&sh_msg[1], csh);
2040 handle_lookup(void *cls,
2041 struct GNUNET_SERVER_Client * client,
2042 const struct GNUNET_MessageHeader * message)
2047 * Process GNS requests.
2049 * @param cls closure)
2050 * @param server the initialized server
2051 * @param c configuration to use
2054 run (void *cls, struct GNUNET_SERVER_Handle *server,
2055 const struct GNUNET_CONFIGURATION_Handle *c)
2058 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2061 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2063 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2064 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2065 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2068 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2069 "ZONEKEY", &keyfile))
2071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2072 "No private key for root zone specified%s!\n", keyfile);
2073 GNUNET_SCHEDULER_shutdown(0);
2077 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2078 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2080 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2085 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2088 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2089 "DNS hijacking enabled... connecting to service.\n");
2091 * Do gnunet dns init here
2093 dns_handle = GNUNET_DNS_connect(c,
2094 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2095 &handle_dns_request, /* rh */
2096 NULL); /* Closure */
2097 if (NULL == dns_handle)
2099 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2100 "Failed to connect to the dnsservice!\n");
2107 * handle to our local namestore
2109 namestore_handle = GNUNET_NAMESTORE_connect(c);
2111 if (NULL == namestore_handle)
2113 //FIXME do error handling;
2114 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2115 "Failed to connect to the namestore!\n");
2116 GNUNET_SCHEDULER_shutdown(0);
2123 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2125 if (NULL == dht_handle)
2127 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2130 //put_some_records(); //FIXME for testing
2133 * Schedule periodic put
2135 * We have roughly an hour for all records;
2137 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2139 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2141 GNUNET_SERVER_add_handlers (server, handlers);
2144 //GNUNET_SERVER_disconnect_notify (server,
2145 // &client_disconnect_notification,
2148 nc = GNUNET_SERVER_notification_context_create (server, 1);
2150 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2156 * The main function for the GNS service.
2158 * @param argc number of arguments from the command line
2159 * @param argv command line arguments
2160 * @return 0 ok, 1 on error
2163 main (int argc, char *const *argv)
2169 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2174 /* end of gnunet-service-gns.c */