From 37f62bb432a79506cffaa1663e5e1bd6b6c96bea Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 5 Mar 2018 21:08:53 +0100 Subject: [PATCH] migrate gnunet-dns2gns to new .gnu-less GNS API --- doc/man/gnunet-dns2gns.1 | 20 +- src/gns/gnunet-dns2gns.c | 382 ++++++++++++--------------------------- 2 files changed, 118 insertions(+), 284 deletions(-) diff --git a/doc/man/gnunet-dns2gns.1 b/doc/man/gnunet-dns2gns.1 index 03f471ed8..04fcb7f53 100644 --- a/doc/man/gnunet-dns2gns.1 +++ b/doc/man/gnunet-dns2gns.1 @@ -1,4 +1,4 @@ -.TH GNUNET\-DNS2GNS 1 "Oct 25, 2012" "GNUnet" +.TH GNUNET\-DNS2GNS 1 "March 5, 2018" "GNUnet" .SH NAME gnunet\-dns2gns \- run a DNS-to-GNS proxy @@ -9,13 +9,9 @@ gnunet\-dns2gns \- run a DNS-to-GNS proxy .br .SH DESCRIPTION -Most users will not want to run an DNS to GADS proxy/gateway and thus will not need this program. +Most users will not want to run an DNS to GNS proxy/gateway and thus will not need this program. -\fBgnunet\-dns2gns\fP runs a DNS resolver which delegates requests to the ".gads" and ".zkey" zones to GADS. All other requests are forwarded to DNS. This DNS proxy is useful for enabling non-personalized GADS\-resolution to an entire network or to offer GADS\-resolution to DNS users. - -A DNS\-to\-GNS proxy using gnunet\-dns2gns is available at ".zkey.eu" - -If you do not want to specify a public key and are the owner of the zone that gnunet\-dns2gns will use for GNS lookups, you need to first create a pseudonym (using "gnunet\-identity \-C NAME"), and then assign it to be used for the "dns2gns" service using "gnunet\-identity \-e NAME \-s dns2gns". After that, you can start the dns2gns service (possibly using gnunet\-arm) without specifying a public key using "\-z". +\fBgnunet\-dns2gns\fP runs a DNS resolver which delegates requests GNS if the TLD matches one configured for GNS. All other requests are forwarded to DNS. This DNS proxy is useful for enabling non-personalized GNS\-resolution to an entire network or to offer GNS\-resolution to DNS users. .SH OPTIONS .B @@ -25,24 +21,14 @@ Use the configuration file FILENAME. .IP "\-d IP, \-\-dns=IP" IP address of a recursive DNS resolver that should be used for non-GADS hostnames. .B -.IP "\-f NAME, \-\-fcfs=NAME" -Authoritative FCFS suffix to use. This is the name under which the local zone's names will be made available. Default is "fcfs.zkey.eu". -.B .IP "\-h, \-\-help" Print short help on options. .B .IP "\-L LOGLEVEL, \-\-loglevel=LOGLEVEL" Use LOGLEVEL for logging. Valid values are DEBUG, INFO, WARNING and ERROR. .B -.IP "\-s SUFFIX, \-\-suffix=SUFFIX" -Authoritative DNS suffix to use. This is the name under which the GADS ".zkey" zone is mapped into the DNS namespace. Default is "zkey.eu". -.B .IP "\-v, \-\-version" Print GNUnet version number. -.B -.IP "\-z PUBLICKEY, \-\-zone=PUBLICKEY" -Use PUBLICKEY for the zone to resolve GNS names in. The PUBLICKEY must be encoded in the text format which can be obtained using gnunet\-ecc, seen in PKEY records and output by gnunet\-identity. If this option is not specified, the default ego associated by gnunet\-identity for the "dns2gns" subsystem will be used. - .SH BUGS Report bugs by using Mantis or by sending electronic mail to diff --git a/src/gns/gnunet-dns2gns.c b/src/gns/gnunet-dns2gns.c index 2f0de030d..3d16cd773 100644 --- a/src/gns/gnunet-dns2gns.c +++ b/src/gns/gnunet-dns2gns.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "gns.h" @@ -35,16 +34,6 @@ */ #define TIMEOUT GNUNET_TIME_UNIT_MINUTES -/** - * Default suffix - */ -#define DNS_SUFFIX ".zkey.eu" - -/** - * FCFS suffix - */ -#define FCFS_SUFFIX "fcfs.zkey.eu" - /** * Data kept per request. */ @@ -69,7 +58,7 @@ struct Request /** * Our GNS request handle. */ - struct GNUNET_GNS_LookupRequest *lookup; + struct GNUNET_GNS_LookupWithTldRequest *lookup; /** * Our DNS request handle @@ -80,13 +69,22 @@ struct Request * Task run on timeout or shutdown to clean up without * response. */ - struct GNUNET_SCHEDULER_Task * timeout_task; + struct GNUNET_SCHEDULER_Task *timeout_task; /** - * Number of bytes in 'addr'. + * Original UDP request message. + */ + char *udp_msg; + + /** + * Number of bytes in @e addr. */ size_t addr_len; + /** + * Number of bytes in @e udp_msg. + */ + size_t udp_msg_size; }; @@ -113,22 +111,12 @@ static struct GNUNET_NETWORK_Handle *listen_socket6; /** * Task for IPv4 socket. */ -static struct GNUNET_SCHEDULER_Task * t4; +static struct GNUNET_SCHEDULER_Task *t4; /** * Task for IPv6 socket. */ -static struct GNUNET_SCHEDULER_Task * t6; - -/** - * DNS suffix, suffix of this gateway in DNS; defaults to '.zkey.eu' - */ -static char *dns_suffix; - -/** - * FCFS suffix, suffix of FCFS-authority in DNS; defaults to 'fcfs.zkey.eu'. - */ -static char *fcfs_suffix; +static struct GNUNET_SCHEDULER_Task *t6; /** * IP of DNS server @@ -140,31 +128,11 @@ static char *dns_ip; */ static unsigned int listen_port = 53; -/** - * Which GNS zone do we translate incoming DNS requests to? - */ -static struct GNUNET_CRYPTO_EcdsaPublicKey my_zone; - -/** - * '-z' option with the main zone to use. - */ -static char *gns_zone_str; - /** * Configuration to use. */ static const struct GNUNET_CONFIGURATION_Handle *cfg; -/** - * Connection to identity service. - */ -static struct GNUNET_IDENTITY_Handle *identity; - -/** - * Request for our ego. - */ -static struct GNUNET_IDENTITY_Operation *id_op; - /** * Task run on shutdown. Cleans up everything. @@ -174,6 +142,7 @@ static struct GNUNET_IDENTITY_Operation *id_op; static void do_shutdown (void *cls) { + (void) cls; if (NULL != t4) { GNUNET_SCHEDULER_cancel (t4); @@ -194,16 +163,6 @@ do_shutdown (void *cls) GNUNET_NETWORK_socket_close (listen_socket6); listen_socket6 = NULL; } - if (NULL != id_op) - { - GNUNET_IDENTITY_cancel (id_op); - id_op = NULL; - } - if (NULL != identity) - { - GNUNET_IDENTITY_disconnect (identity); - identity = NULL; - } if (NULL != gns) { GNUNET_GNS_disconnect (gns); @@ -227,6 +186,7 @@ send_response (struct Request *request) { char *buf; size_t size; + ssize_t sret; if (GNUNET_SYSERR == GNUNET_DNSPARSER_pack (request->packet, @@ -239,16 +199,20 @@ send_response (struct Request *request) } else { - if (size != - GNUNET_NETWORK_socket_sendto (request->lsock, - buf, size, - request->addr, - request->addr_len)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); + sret = GNUNET_NETWORK_socket_sendto (request->lsock, + buf, + size, + request->addr, + request->addr_len); + if ( (sret < 0) || + (size != (size_t) sret) ) + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, + "sendto"); GNUNET_free (buf); } GNUNET_SCHEDULER_cancel (request->timeout_task); GNUNET_DNSPARSER_free_packet (request->packet); + GNUNET_free (request->udp_msg); GNUNET_free (request); } @@ -266,9 +230,10 @@ do_timeout (void *cls) if (NULL != request->packet) GNUNET_DNSPARSER_free_packet (request->packet); if (NULL != request->lookup) - GNUNET_GNS_lookup_cancel (request->lookup); + GNUNET_GNS_lookup_with_tld_cancel (request->lookup); if (NULL != request->dns_lookup) GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup); + GNUNET_free (request->udp_msg); GNUNET_free (request); } @@ -289,7 +254,9 @@ dns_result_processor (void *cls, { struct Request *request = cls; - request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r); + (void) rs; + request->packet = GNUNET_DNSPARSER_parse ((char*)dns, + r); send_response (request); } @@ -298,11 +265,13 @@ dns_result_processor (void *cls, * Iterator called on obtained result for a GNS lookup. * * @param cls closure + * @param was_gns #GNUNET_NO if the TLD is not configured for GNS * @param rd_count number of records in @a rd * @param rd the records in reply */ static void result_processor (void *cls, + int was_gns, uint32_t rd_count, const struct GNUNET_GNSRECORD_Data *rd) { @@ -312,6 +281,22 @@ result_processor (void *cls, struct GNUNET_DNSPARSER_Record rec; request->lookup = NULL; + if (GNUNET_NO == was_gns) + { + /* TLD not configured for GNS, fall back to DNS */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Using DNS resolver IP `%s' to resolve `%s'\n", + dns_ip, + request->packet->queries[0].name); + GNUNET_DNSPARSER_free_packet (request->packet); + request->packet = NULL; + request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub, + request->udp_msg, + request->udp_msg_size, + &dns_result_processor, + request); + return; + } packet = request->packet; packet->flags.query_or_response = 1; packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR; @@ -396,19 +381,16 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock, { struct Request *request; struct GNUNET_DNSPARSER_Packet *packet; - char *name; - size_t name_len; - int type; - int use_gns; - packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size); + packet = GNUNET_DNSPARSER_parse (udp_msg, + udp_msg_size); if (NULL == packet) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Cannot parse DNS request from %s\n"), - GNUNET_a2s (addr, addr_len)); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Cannot parse DNS request from %s\n"), + GNUNET_a2s (addr, addr_len)); + return; + } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n", packet->queries[0].name, @@ -419,88 +401,45 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock, if ( (0 != packet->flags.query_or_response) || (0 != packet->num_answers) || (0 != packet->num_authority_records)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Received malformed DNS request from %s\n"), - GNUNET_a2s (addr, addr_len)); - GNUNET_DNSPARSER_free_packet (packet); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Received malformed DNS request from %s\n"), + GNUNET_a2s (addr, addr_len)); + GNUNET_DNSPARSER_free_packet (packet); + return; + } if ( (1 != packet->num_queries) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Received unsupported DNS request from %s\n"), - GNUNET_a2s (addr, addr_len)); - GNUNET_DNSPARSER_free_packet (packet); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Received unsupported DNS request from %s\n"), + GNUNET_a2s (addr, + addr_len)); + GNUNET_DNSPARSER_free_packet (packet); + return; + } request = GNUNET_malloc (sizeof (struct Request) + addr_len); request->lsock = lsock; request->packet = packet; request->addr = &request[1]; request->addr_len = addr_len; - GNUNET_memcpy (&request[1], addr, addr_len); + GNUNET_memcpy (&request[1], + addr, + addr_len); + request->udp_msg_size = udp_msg_size; + request->udp_msg = GNUNET_memdup (udp_msg, + udp_msg_size); request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_timeout, request); - name = GNUNET_strdup (packet->queries[0].name); - name_len = strlen (name); - use_gns = GNUNET_NO; - - - if ( (name_len > strlen (fcfs_suffix)) && - (0 == strcasecmp (fcfs_suffix, - &name[name_len - strlen (fcfs_suffix)])) ) - { - /* replace ".fcfs.zkey.eu" with ".gnu" */ - strcpy (&name[name_len - strlen (fcfs_suffix)], - ".gnu"); - use_gns = GNUNET_YES; - } - else if ( (name_len > strlen (dns_suffix)) && - (0 == strcasecmp (dns_suffix, - &name[name_len - strlen (dns_suffix)])) ) - { - /* replace ".zkey.eu" with ".zkey" */ - strcpy (&name[name_len - strlen (dns_suffix)], - ".zkey"); - use_gns = GNUNET_YES; - } else if ( (name_len > strlen (".gnu")) && - (0 == strcasecmp (".gnu", - &name[name_len - strlen (".gnu")])) ) - { - /* name is in GNS */ - use_gns = GNUNET_YES; - } - if (GNUNET_YES == use_gns) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Calling GNS on `%s'\n", - name); - type = packet->queries[0].type; - request->lookup = GNUNET_GNS_lookup (gns, - name, - &my_zone, - type, - GNUNET_NO, - &result_processor, - request); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Using DNS resolver IP `%s' to resolve `%s'\n", - dns_ip, - name); - GNUNET_DNSPARSER_free_packet (request->packet); - request->packet = NULL; - request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub, - udp_msg, - udp_msg_size, - &dns_result_processor, - request); - } - GNUNET_free (name); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Calling GNS on `%s'\n", + packet->queries[0].name); + request->lookup = GNUNET_GNS_lookup_with_tld (gns, + packet->queries[0].name, + packet->queries[0].type, + GNUNET_NO, + &result_processor, + request); } @@ -547,7 +486,7 @@ read_dns4 (void *cls) "recvfrom"); return; } - GNUNET_break (size == (size_t) sret); + GNUNET_break (size == sret); handle_request (listen_socket4, &v4, addrlen, @@ -600,7 +539,7 @@ read_dns6 (void *cls) "recvfrom"); return; } - GNUNET_break (size == (size_t) sret); + GNUNET_break (size == sret); handle_request (listen_socket6, &v6, addrlen, @@ -611,15 +550,31 @@ read_dns6 (void *cls) /** - * Start DNS daemon. + * Main function that will be run. + * + * @param cls closure + * @param args remaining command-line arguments + * @param cfgfile name of the configuration file used (for saving, can be NULL!) + * @param c configuration */ static void -run_dnsd () +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *c) { - if (NULL == dns_suffix) - dns_suffix = DNS_SUFFIX; - if (NULL == fcfs_suffix) - fcfs_suffix = FCFS_SUFFIX; + (void) cls; + (void) args; + (void) cfgfile; + cfg = c; + if (NULL == dns_ip) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("No DNS server specified!\n")); + return; + } + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + NULL); if (NULL == (gns = GNUNET_GNS_connect (cfg))) return; if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip))) @@ -696,92 +651,6 @@ run_dnsd () } -/** - * Method called to inform about the egos of this peer. - * - * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get, - * this function is only called ONCE, and 'NULL' being passed in - * @a ego does indicate an error (i.e. name is taken or no default - * value is known). If @a ego is non-NULL and if '*ctx' - * is set in those callbacks, the value WILL be passed to a subsequent - * call to the identity callback of #GNUNET_IDENTITY_connect (if - * that one was not NULL). - * - * @param cls closure, NULL - * @param ego ego handle - * @param ctx context for application to store data for this ego - * (during the lifetime of this process, initially NULL) - * @param name name assigned by the user for this ego, - * NULL if the user just deleted the ego and it - * must thus no longer be used - */ -static void -identity_cb (void *cls, - struct GNUNET_IDENTITY_Ego *ego, - void **ctx, - const char *name) -{ - id_op = NULL; - if (NULL == ego) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("No ego configured for `dns2gns` subsystem\n")); - return; - } - GNUNET_IDENTITY_ego_get_public_key (ego, - &my_zone); - run_dnsd (); -} - - -/** - * Main function that will be run. - * - * @param cls closure - * @param args remaining command-line arguments - * @param cfgfile name of the configuration file used (for saving, can be NULL!) - * @param c configuration - */ -static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *c) -{ - cfg = c; - - if (NULL == dns_ip) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("No DNS server specified!\n")); - return; - } - GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); - if (NULL == gns_zone_str) - { - identity = GNUNET_IDENTITY_connect (cfg, - NULL, NULL); - id_op = GNUNET_IDENTITY_get (identity, - "dns2gns", - &identity_cb, - NULL); - return; - } - if ( (NULL == gns_zone_str) || - (GNUNET_OK != - GNUNET_CRYPTO_ecdsa_public_key_from_string (gns_zone_str, - strlen (gns_zone_str), - &my_zone)) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("No valid GNS zone specified!\n")); - GNUNET_SCHEDULER_shutdown (); - return; - } - run_dnsd (); -} - - /** * The main function for the dns2gns daemon. * @@ -794,37 +663,16 @@ main (int argc, char *const *argv) { struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_option_string ('d', "dns", "IP", gettext_noop ("IP of recursive DNS resolver to use (required)"), &dns_ip), - - GNUNET_GETOPT_option_string ('f', - "fcfs", - "NAME", - gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), - &fcfs_suffix), - - GNUNET_GETOPT_option_string ('s', - "suffix", - "SUFFIX", - gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), - &dns_suffix), - GNUNET_GETOPT_option_uint ('p', - "port", - "UDPPORT", - gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"), - &listen_port), - - GNUNET_GETOPT_option_string ('z', - "zone", - "PUBLICKEY", - gettext_noop ("Public key of the GNS zone to use (overrides default)"), - &gns_zone_str), - + "port", + "UDPPORT", + gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"), + &listen_port), GNUNET_GETOPT_OPTION_END }; int ret; -- 2.25.1