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 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
773 rh->authority_chain_tail,
780 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
781 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
783 /* Save to namestore */
784 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
786 GNUNET_NAMESTORE_record_put (namestore_handle,
793 &on_namestore_record_put_result, //cont
802 if (strcmp(rh->name, "") == 0)
803 rh->proc(rh->proc_cls, rh, 0, NULL);
805 resolve_delegation_from_dht(rh);
810 * should never get here unless false dht key/put
811 * block plugin should handle this
814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
820 * Process DHT lookup result for record.
822 * @param cls the closure
823 * @param rh resolver handle
824 * @param rd_count number of results (always 0)
825 * @param rd record data (always NULL)
828 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
829 unsigned int rd_count,
830 const struct GNUNET_NAMESTORE_RecordData *rd)
834 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
835 "No records for %s found in DHT. Aborting\n",
837 /* give up, cannot resolve */
838 reply_to_dns(NULL, rh, 0, NULL);
842 /* results found yay */
843 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
844 "Record resolved from namestore!");
845 reply_to_dns(NULL, rh, rd_count, rd);
851 * Process namestore lookup result for record.
853 * @param cls the closure
854 * @param rh resolver handle
855 * @param rd_count number of results (always 0)
856 * @param rd record data (always NULL)
859 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
860 unsigned int rd_count,
861 const struct GNUNET_NAMESTORE_RecordData *rd)
865 /* ns entry expired. try dht */
866 if (rh->status & (EXPIRED | !EXISTS))
868 rh->proc = &process_record_result_dht;
869 resolve_record_from_dht(rh);
872 /* give up, cannot resolve */
873 reply_to_dns(NULL, rh, 0, NULL);
877 /* results found yay */
878 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
879 "Record resolved from namestore!");
880 reply_to_dns(NULL, rh, rd_count, rd);
886 * Determine if this name is canonical.
888 * a.b.gnunet = not canonical
891 * @param name the name to test
892 * @return 1 if canonical
895 is_canonical(char* name)
897 uint32_t len = strlen(name);
900 for (i=0; i<len; i++)
902 if (*(name+i) == '.')
909 * Move one level up in the domain hierarchy and return the
910 * passed top level domain.
912 * @param name the domain
913 * @param dest the destination where the tld will be put
916 pop_tld(char* name, char* dest)
920 if (is_canonical(name))
927 for (len = strlen(name); len > 0; len--)
929 if (*(name+len) == '.')
939 strcpy(dest, (name+len+1));
943 * DHT resolution for delegation finished. Processing result.
945 * @param cls the closure
946 * @param rh resolver handle
947 * @param rd_count number of results (always 0)
948 * @param rd record data (always NULL)
951 process_dht_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
952 unsigned int rd_count,
953 const struct GNUNET_NAMESTORE_RecordData *rd)
955 if (strcmp(rh->name, "") == 0)
957 /* We resolved full name for delegation. resolving record */
958 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
959 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
960 rh->proc = &process_record_result_ns;
961 resolve_record_from_ns(rh);
966 * we still have some left
968 if (is_canonical(rh->name))
970 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
971 "Resolving canonical record %s in ns\n", rh->name);
972 rh->proc = &process_record_result_ns;
973 resolve_record_from_ns(rh);
976 /* give up, cannot resolve */
977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
978 "Cannot fully resolve delegation for %s via DHT!\n",
980 reply_to_dns(NULL, rh, 0, NULL);
985 * Start DHT lookup for a name -> PKEY (compare NS) record in
986 * rh->authority's zone
988 * @param rh the pending gns query
989 * @param name the name of the PKEY record
992 resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
995 GNUNET_HashCode name_hash;
996 GNUNET_HashCode lookup_key;
998 GNUNET_CRYPTO_hash(rh->authority_name,
999 strlen(rh->authority_name),
1001 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1003 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1004 &dht_authority_lookup_timeout,
1007 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1009 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1010 DHT_OPERATION_TIMEOUT,
1011 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1013 DHT_GNS_REPLICATION_LEVEL,
1017 &process_authority_dht_result,
1024 * Namestore resolution for delegation finished. Processing result.
1026 * @param cls the closure
1027 * @param rh resolver handle
1028 * @param rd_count number of results (always 0)
1029 * @param rd record data (always NULL)
1032 process_ns_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1033 unsigned int rd_count,
1034 const struct GNUNET_NAMESTORE_RecordData *rd)
1036 if (strcmp(rh->name, "") == 0)
1038 /* We resolved full name for delegation. resolving record */
1039 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1040 "Resolved full name for delegation. resolving record ''\n");
1041 rh->proc = &process_record_result_ns;
1042 resolve_record_from_ns(rh);
1047 * we still have some left
1048 * check if ns entry is fresh
1050 if (rh->status & (EXISTS | !EXPIRED))
1052 if (is_canonical(rh->name))
1054 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1055 "Resolving canonical record %s\n", rh->name);
1056 rh->proc = &process_record_result_ns;
1057 resolve_record_from_ns(rh);
1061 /* give up, cannot resolve */
1062 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1063 "Cannot fully resolve delegation for %s!\n",
1065 reply_to_dns(NULL, rh, 0, NULL);
1070 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1071 "Trying to resolve delegation for %s via DHT\n",
1073 rh->proc = &process_dht_delegation_dns;
1074 resolve_delegation_from_dht(rh);
1078 static void resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh);
1081 * This is a callback function that should give us only PKEY
1082 * records. Used to query the namestore for the authority (PKEY)
1083 * for 'name'. It will recursively try to resolve the
1084 * authority for a given name from the namestore.
1086 * @param cls the pending query
1087 * @param key the key of the zone we did the lookup
1088 * @param expiration expiration date of the record data set in the namestore
1089 * @param name the name for which we need an authority
1090 * @param rd_count the number of records with 'name'
1091 * @param rd the record data
1092 * @param signature the signature of the authority for the record data
1095 process_authority_lookup_ns(void* cls,
1096 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1097 struct GNUNET_TIME_Absolute expiration,
1099 unsigned int rd_count,
1100 const struct GNUNET_NAMESTORE_RecordData *rd,
1101 const struct GNUNET_CRYPTO_RsaSignature *signature)
1103 struct GNUNET_GNS_ResolverHandle *rh;
1104 struct GNUNET_TIME_Relative remaining_time;
1105 GNUNET_HashCode zone;
1108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1111 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1112 GNUNET_CRYPTO_hash(key,
1113 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1115 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1121 rh->status |= EXISTS;
1124 if (remaining_time.rel_value == 0)
1126 rh->status |= EXPIRED;
1130 * No authority found in namestore.
1135 * We did not find an authority in the namestore
1140 * Promote this authority back to a name maybe it is
1143 if (strcmp(rh->name, "") == 0)
1145 /* simply promote back */
1146 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1147 "Promoting %s back to name\n", rh->authority_name);
1148 strcpy(rh->name, rh->authority_name);
1152 /* add back to existing name */
1153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1154 "Adding %s back to %s\n",
1155 rh->authority_name, rh->name);
1156 new_name = GNUNET_malloc(strlen(rh->name)
1157 + strlen(rh->authority_name) + 2);
1158 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1159 strcpy(new_name, rh->name);
1160 strcpy(new_name+strlen(new_name)+1, ".");
1161 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1162 GNUNET_free(rh->name);
1163 rh->name = new_name;
1165 rh->proc(rh->proc_cls, rh, 0, NULL);
1169 //Note only 1 pkey should have been returned.. anything else would be strange
1171 * We found an authority that may be able to help us
1172 * move on with query
1175 for (i=0; i<rd_count;i++)
1178 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1181 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1185 if (remaining_time.rel_value == 0)
1187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1188 "This dht entry is expired.\n");
1189 rh->authority_chain_head->fresh = 0;
1190 rh->proc(rh->proc_cls, rh, 0, NULL);
1198 * Resolve rest of query with new authority
1200 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1201 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1202 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1203 auth->zone = rh->authority;
1204 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1205 rh->authority_chain_tail,
1209 * We are done with PKEY resolution if name is empty
1210 * else resolve again with new authority
1212 if (strcmp(rh->name, "") == 0)
1213 rh->proc(rh->proc_cls, rh, 0, NULL);
1215 resolve_delegation_from_ns(rh);
1222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1223 "Authority lookup successful but no PKEY... never get here\n");
1224 rh->proc(rh->proc_cls, rh, 0, NULL);
1229 * Resolve the delegation chain for the request
1231 * @param rh the resolver handle
1234 resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
1237 pop_tld(rh->name, rh->authority_name);
1238 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1241 GNUNET_GNS_RECORD_PKEY,
1242 &process_authority_lookup_ns,
1248 * Entry point for name resolution
1249 * Setup a new query and try to resolve
1251 * @param request the request handle of the DNS request from a client
1252 * @param p the DNS query packet we received
1253 * @param q the DNS query we received parsed from p
1256 start_resolution_from_dns(struct GNUNET_DNS_RequestHandle *request,
1257 struct GNUNET_DNSPARSER_Packet *p,
1258 struct GNUNET_DNSPARSER_Query *q)
1260 struct GNUNET_GNS_ResolverHandle *rh;
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 "Starting resolution for %s (type=%d)!\n",
1266 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1269 rh->authority = zone_hash;
1272 rh->name = GNUNET_malloc(strlen(q->name)
1273 - strlen(gnunet_tld) + 1);
1275 strlen(q->name)-strlen(gnunet_tld) + 1);
1276 memcpy(rh->name, q->name,
1277 strlen(q->name)-strlen(gnunet_tld));
1279 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1281 rh->request_handle = request;
1283 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1284 rh->authority_chain_tail = rh->authority_chain_head;
1285 rh->authority_chain_head->zone = zone_hash;
1287 /* Start resolution in our zone */
1288 rh->proc = &process_ns_delegation_dns;
1289 resolve_delegation_from_ns(rh);
1293 * The DNS request handler
1294 * Called for every incoming DNS request.
1296 * @param cls closure
1297 * @param rh request handle to user for reply
1298 * @param request_length number of bytes in request
1299 * @param request udp payload of the DNS request
1302 handle_dns_request(void *cls,
1303 struct GNUNET_DNS_RequestHandle *rh,
1304 size_t request_length,
1305 const char *request)
1307 struct GNUNET_DNSPARSER_Packet *p;
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1311 p = GNUNET_DNSPARSER_parse (request, request_length);
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1316 "Received malformed DNS packet, leaving it untouched\n");
1317 GNUNET_DNS_request_forward (rh);
1322 * Check tld and decide if we or
1323 * legacy dns is responsible
1325 * FIXME now in theory there could be more than 1 query in the request
1326 * but if this is case we get into trouble:
1327 * either we query the GNS or the DNS. We cannot do both!
1328 * So I suggest to either only allow a single query per request or
1329 * only allow GNS or DNS requests.
1330 * The way it is implemented here now is buggy and will lead to erratic
1331 * behaviour (if multiple queries are present).
1333 if (p->num_queries == 0)
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "No Queries in DNS packet... forwarding\n");
1337 GNUNET_DNS_request_forward (rh);
1340 if (p->num_queries > 1)
1342 /* Note: We could also look for .gnunet */
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344 ">1 queriy in DNS packet... odd. We only process #1\n");
1351 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1353 while ((*tldoffset) != '.')
1356 if (0 == strcmp(tldoffset, gnunet_tld))
1358 start_resolution_from_dns(rh, p, p->queries);
1363 * This request does not concern us. Forward to real DNS.
1365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1366 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1367 GNUNET_DNS_request_forward (rh);
1373 * Method called periodicattluy that triggers
1374 * iteration over root zone
1376 * @param cls closure
1377 * @param tc task context
1380 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1382 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1386 * Continuation for DHT put
1388 * @param cls closure
1389 * @param tc task context
1392 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1394 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1399 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1402 * Function used to put all records successively into the DHT.
1404 * @param cls the closure (NULL)
1405 * @param key the public key of the authority (ours)
1406 * @param expiration lifetime of the namestore entry
1407 * @param name the name of the records
1408 * @param rd_count the number of records in data
1409 * @param rd the record data
1410 * @param signature the signature for the record data
1413 put_gns_record(void *cls,
1414 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1415 struct GNUNET_TIME_Absolute expiration,
1417 unsigned int rd_count,
1418 const struct GNUNET_NAMESTORE_RecordData *rd,
1419 const struct GNUNET_CRYPTO_RsaSignature *signature)
1422 struct GNSNameRecordBlock *nrb;
1423 GNUNET_HashCode name_hash;
1424 GNUNET_HashCode xor_hash;
1425 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1426 uint32_t rd_payload_length;
1427 char* nrb_data = NULL;
1432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1433 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1434 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1440 "Putting records for %s into the DHT\n", name);
1442 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1444 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1445 + sizeof(struct GNSNameRecordBlock));
1447 if (signature != NULL)
1448 nrb->signature = *signature;
1450 nrb->public_key = *key;
1452 nrb->rd_count = htonl(rd_count);
1454 memset(&nrb[1], 0, strlen(name) + 1);
1455 memcpy(&nrb[1], name, strlen(name));
1457 nrb_data = (char*)&nrb[1];
1458 nrb_data += strlen(name) + 1;
1460 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1463 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1468 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1475 * calculate DHT key: H(name) xor H(pubkey)
1477 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1478 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1479 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1481 "putting records for %s under key: %s with size %d\n",
1482 name, (char*)&xor_hash_string, rd_payload_length);
1484 GNUNET_DHT_put (dht_handle, &xor_hash,
1485 DHT_GNS_REPLICATION_LEVEL,
1487 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1491 DHT_OPERATION_TIMEOUT,
1493 NULL); //cls for cont
1495 num_public_records++;
1498 * Reschedule periodic put
1500 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1501 &update_zone_dht_next,
1507 * Periodically iterate over our zone and store everything in dht
1510 * @param tc task context
1513 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1516 if (0 == num_public_records)
1518 dht_update_interval = GNUNET_TIME_relative_multiply(
1519 GNUNET_TIME_UNIT_SECONDS,
1524 dht_update_interval = GNUNET_TIME_relative_multiply(
1525 GNUNET_TIME_UNIT_SECONDS,
1526 (3600/num_public_records));
1528 num_public_records = 0; //start counting again
1529 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1531 GNUNET_NAMESTORE_RF_AUTHORITY,
1532 GNUNET_NAMESTORE_RF_PRIVATE,
1538 static void send_shorten_response(const char* name,
1539 struct ClientShortenHandle *csh);
1541 process_shorten_pseu_lookup_ns(void *cls,
1542 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1543 struct GNUNET_TIME_Absolute expire,
1545 unsigned int rd_len,
1546 const struct GNUNET_NAMESTORE_RecordData *rd,
1547 const struct GNUNET_CRYPTO_RsaSignature *signature)
1549 struct GNUNET_GNS_ResolverHandle *rh =
1550 (struct GNUNET_GNS_ResolverHandle *)cls;
1551 struct GNUNET_TIME_Relative remaining_time;
1558 rh->status |= EXISTS;
1561 if (remaining_time.rel_value == 0)
1563 rh->status |= EXPIRED;
1568 rh->proc(cls, rh, rd_len, rd);
1573 * Start DHT lookup for a PSEUdonym record in
1574 * rh->authority's zone
1576 * @param rh the pending gns query
1577 * @param name the name of the PKEY record
1580 resolve_pseu_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1583 GNUNET_HashCode name_hash;
1584 GNUNET_HashCode lookup_key;
1587 GNUNET_CRYPTO_hash("",
1591 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1593 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1594 &dht_lookup_timeout,
1597 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1599 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1600 DHT_OPERATION_TIMEOUT,
1601 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1603 DHT_GNS_REPLICATION_LEVEL,
1607 &process_authority_dht_result,
1614 handle_shorten_pseu_ns_result(void* cls,
1615 struct GNUNET_GNS_ResolverHandle *rh,
1617 const struct GNUNET_NAMESTORE_RecordData *rd);
1620 handle_shorten_zone_to_name(void *cls,
1621 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1622 struct GNUNET_TIME_Absolute expire,
1624 unsigned int rd_len,
1625 const struct GNUNET_NAMESTORE_RecordData *rd,
1626 const struct GNUNET_CRYPTO_RsaSignature *signature)
1628 struct GNUNET_GNS_ResolverHandle *rh =
1629 (struct GNUNET_GNS_ResolverHandle *)cls;
1630 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1634 /* we found a match in our own zone */
1637 result = GNUNET_malloc(strlen(rh->name) + strlen(name) + 1);
1638 memset(result, 0, strlen(rh->name) + strlen(name) + 1);
1639 memcpy(result, rh->name, strlen(rh->name));
1640 memcpy(result+strlen(rh->name)+1, name, strlen(name));
1642 send_shorten_response(result, csh);
1644 GNUNET_free(result);
1649 * Nothing in our zone
1650 * check PSEU for this authority in namestore
1652 rh->proc = &handle_shorten_pseu_ns_result;
1653 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1654 &rh->authority_chain_head->zone,
1656 GNUNET_GNS_RECORD_PSEU,
1657 &process_shorten_pseu_lookup_ns,
1663 * Process result from namestore delegation lookup
1664 * for shorten operation
1666 * @param cls the client shorten handle
1667 * @param rh the resolver handle
1668 * @param rd_count number of results (0)
1669 * @param rd data (NULL)
1672 handle_shorten_pseu_dht_result(void* cls,
1673 struct GNUNET_GNS_ResolverHandle *rh,
1675 const struct GNUNET_NAMESTORE_RecordData *rd)
1677 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1678 struct AuthorityChain *auth_chain;
1689 for (i=0; i < rd_len; i++)
1691 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1695 pseu = (char*) rd[i].data;
1696 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1697 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1698 memcpy(result, rh->name, strlen(rh->name));
1699 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1701 send_shorten_response(result, csh);
1703 GNUNET_free(result);
1709 * continue with next authority
1712 auth_chain = rh->authority_chain_head;
1714 if ((auth_chain->next->next == NULL) &&
1715 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1720 result = GNUNET_malloc(strlen(rh->name) + strlen(auth_chain->name) + 2);
1721 memset(result, 0, strlen(rh->name) + strlen(auth_chain->name) + 2);
1722 strcpy(result, rh->name);
1723 strcpy(result+strlen(rh->name)+1, ".");
1724 strcpy(result+strlen(rh->name)+2, auth_chain->name);
1725 send_shorten_response(result, csh);
1731 * Continue with next authority
1733 new_name = GNUNET_malloc(strlen(rh->name)+
1734 strlen(auth_chain->name) + 2);
1735 memset(new_name, 0, strlen(rh->name)+
1736 strlen(auth_chain->name) + 2);
1737 strcpy(new_name, rh->name);
1738 strcpy(new_name+strlen(rh->name)+1, ".");
1739 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1740 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1741 rh->authority_chain_tail,
1744 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1746 &rh->authority_chain_head->zone,
1747 &handle_shorten_zone_to_name,
1755 * Process result from namestore PSEU lookup
1756 * for shorten operation
1757 * FIXME do we need to check for own zone here?
1759 * @param cls the client shorten handle
1760 * @param rh the resolver handle
1761 * @param rd_count number of results (0 if none found)
1762 * @param rd data (NULL if none found)
1765 handle_shorten_pseu_ns_result(void* cls,
1766 struct GNUNET_GNS_ResolverHandle *rh,
1768 const struct GNUNET_NAMESTORE_RecordData *rd)
1770 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1771 struct AuthorityChain *auth_chain;
1782 for (i=0; i < rd_len; i++)
1784 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1788 pseu = (char*) rd[i].data;
1789 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1790 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1791 memcpy(result, rh->name, strlen(rh->name));
1792 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1794 send_shorten_response(result, csh);
1796 GNUNET_free(result);
1801 * No PSEU found. Ask DHT if expired.
1802 * Else contunue with next authority
1804 if (rh->status & (EXISTS | !EXPIRED))
1809 auth_chain = rh->authority_chain_head;
1810 new_name = GNUNET_malloc(strlen(rh->name)+
1811 strlen(auth_chain->name) + 2);
1812 memset(new_name, 0, strlen(rh->name)+
1813 strlen(auth_chain->name) + 2);
1814 strcpy(new_name, rh->name);
1815 strcpy(new_name+strlen(rh->name)+1, ".");
1816 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1818 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1819 rh->authority_chain_tail,
1822 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1824 &rh->authority_chain_head->zone,
1825 &handle_shorten_zone_to_name,
1833 rh->authority = rh->authority_chain_head->zone;
1834 rh->proc = &handle_shorten_pseu_dht_result;
1835 resolve_pseu_from_dht(rh);
1842 * Process result from namestore delegation lookup
1843 * for shorten operation
1845 * @param cls the client shorten handle
1846 * @param rh the resolver handle
1847 * @param rd_count number of results (0)
1848 * @param rd data (NULL)
1851 handle_shorten_delegation_result(void* cls,
1852 struct GNUNET_GNS_ResolverHandle *rh,
1854 const struct GNUNET_NAMESTORE_RecordData *rd)
1856 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1857 struct AuthorityChain *auth_chain;
1860 * At this point rh->name contains the part of the name
1861 * that we do not have a PKEY in our namestore to resolve.
1862 * The authority chain in the resolver handle is now
1863 * useful to backtrack if needed
1866 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1867 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
1869 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
1873 * This is our zone append .gnunet unless name is empty
1874 * (it shouldn't be, usually FIXME what happens if we
1875 * shorten to our zone to a "" record??)
1877 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
1881 csh->offset++; //FIXME needed?
1882 auth_chain = rh->authority_chain_head;
1883 /* backtrack authorities for pseu */
1884 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1887 &handle_shorten_zone_to_name,
1892 typedef void (*ShortenResponseProc) (void* cls, const char* name);
1895 * Shorten a given name
1897 * @param name the name to shorten
1898 * @param proc the processor to call when finished
1899 * @praram cls the closure to the processor
1902 shorten_name(char* name, struct ClientShortenHandle* csh)
1905 struct GNUNET_GNS_ResolverHandle *rh;
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "Starting resolution for %s (type=%d)!\n",
1909 name, GNUNET_GNS_RECORD_PKEY);
1911 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1912 rh->authority = zone_hash;
1914 rh->name = GNUNET_malloc(strlen(name)
1915 - strlen(gnunet_tld) + 1);
1917 strlen(name)-strlen(gnunet_tld) + 1);
1918 memcpy(rh->name, name,
1919 strlen(name)-strlen(gnunet_tld));
1921 csh->name = GNUNET_malloc(strlen(name)
1922 - strlen(gnunet_tld) + 1);
1924 strlen(name)-strlen(gnunet_tld) + 1);
1925 memcpy(rh->name, name,
1926 strlen(name)-strlen(gnunet_tld));
1928 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1930 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1931 rh->authority_chain_tail = rh->authority_chain_head;
1932 rh->authority_chain_head->zone = zone_hash;
1933 rh->proc = &handle_shorten_delegation_result;
1934 rh->proc_cls = (void*)csh;
1936 /* Start delegation resolution in our namestore */
1937 resolve_delegation_from_ns(rh);
1942 * Send shorten response back to client
1943 * FIXME this is without .gnunet!
1945 * @param cls the client handle in closure
1946 * @param name the shortened name result or NULL if cannot be shortened
1949 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n",
1953 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
1960 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage *)
1963 rmsg->unique_id = csh->unique_id;
1964 rmsg->key = csh->key;
1965 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
1967 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage *) +
1970 strcpy((char*)&rmsg[1], name);
1972 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
1973 (const struct GNUNET_MessageHeader *) &rmsg,
1976 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
1984 * Handle a shorten message from the api
1986 * @param cls the closure
1987 * @param client the client
1988 * @param message the message
1990 static void handle_shorten(void *cls,
1991 struct GNUNET_SERVER_Client * client,
1992 const struct GNUNET_MessageHeader * message)
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
1996 size_t msg_size = 0;
1997 struct ClientShortenHandle *csh;
1999 if (ntohs (message->size) != sizeof (struct GNUNET_GNS_ClientShortenMessage))
2001 GNUNET_break_op (0);
2002 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2006 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2007 (struct GNUNET_GNS_ClientShortenMessage *) message;
2009 msg_size = ntohs(message->size);
2011 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2013 GNUNET_break_op (0);
2014 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2018 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2019 csh->client = client;
2020 csh->unique_id = sh_msg->unique_id;
2021 csh->key = sh_msg->key;
2023 //Offset in original name
2027 shorten_name((char*)&sh_msg[1], csh);
2035 handle_lookup(void *cls,
2036 struct GNUNET_SERVER_Client * client,
2037 const struct GNUNET_MessageHeader * message)
2042 * Process GNS requests.
2044 * @param cls closure)
2045 * @param server the initialized server
2046 * @param c configuration to use
2049 run (void *cls, struct GNUNET_SERVER_Handle *server,
2050 const struct GNUNET_CONFIGURATION_Handle *c)
2053 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2056 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2058 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2059 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2060 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2063 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2064 "ZONEKEY", &keyfile))
2066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2067 "No private key for root zone specified%s!\n", keyfile);
2068 GNUNET_SCHEDULER_shutdown(0);
2072 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2073 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2075 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2080 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2083 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2084 "DNS hijacking enabled... connecting to service.\n");
2086 * Do gnunet dns init here
2088 dns_handle = GNUNET_DNS_connect(c,
2089 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2090 &handle_dns_request, /* rh */
2091 NULL); /* Closure */
2092 if (NULL == dns_handle)
2094 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2095 "Failed to connect to the dnsservice!\n");
2102 * handle to our local namestore
2104 namestore_handle = GNUNET_NAMESTORE_connect(c);
2106 if (NULL == namestore_handle)
2108 //FIXME do error handling;
2109 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2110 "Failed to connect to the namestore!\n");
2111 GNUNET_SCHEDULER_shutdown(0);
2118 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2120 if (NULL == dht_handle)
2122 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2125 //put_some_records(); //FIXME for testing
2128 * Schedule periodic put
2130 * We have roughly an hour for all records;
2132 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2134 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2136 GNUNET_SERVER_add_handlers (server, handlers);
2139 //GNUNET_SERVER_disconnect_notify (server,
2140 // &client_disconnect_notification,
2143 nc = GNUNET_SERVER_notification_context_create (server, 1);
2145 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2151 * The main function for the GNS service.
2153 * @param argc number of arguments from the command line
2154 * @param argv command line arguments
2155 * @return 0 ok, 1 on error
2158 main (int argc, char *const *argv)
2164 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2169 /* end of gnunet-service-gns.c */