From df19a970e1f49e362dabd4f848b778d6bc2b7518 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 8 Nov 2014 15:51:49 +0300 Subject: [PATCH] dns-example: allow to set ns from args We can't do this using resolv.conf: $ dns-example -v -c <(echo nameserver 127.0.0.1:10053) ya.ru Because of how evutil_read_file_() works (using fstat()) And actually glibc for example will not use port from nameserver line, and because of inet_aton() it will fail if nameserver will have arbitary port: (gdb) p inet_aton("127.0.0.1", malloc(10000)) $1 = 1 (gdb) p inet_aton("127.0.0.1:53", malloc(10000)) $2 = 0 From glibc/resolv/res_init.c: if (MATCH(buf, "nameserver") && nserv < MAXNS) { struct in_addr a; cp = buf + sizeof("nameserver") - 1; while (*cp == ' ' || *cp == '\t') cp++; if ((*cp != '\0') && (*cp != '\n') && __inet_aton(cp, &a)) { statp->nsaddr_list[nservall].sin_addr = a; statp->nsaddr_list[nservall].sin_family = AF_INET; statp->nsaddr_list[nservall].sin_port = htons(NAMESERVER_PORT); --- sample/dns-example.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sample/dns-example.c b/sample/dns-example.c index 5e951183..67a6ace6 100644 --- a/sample/dns-example.c +++ b/sample/dns-example.c @@ -151,24 +151,26 @@ main(int c, char **v) { int use_getaddrinfo; int servertest; const char *resolv_conf; + const char *ns; } o = { }; char opt; struct event_base *event_base = NULL; struct evdns_base *evdns_base = NULL; if (c < 2) { - fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] hostname\n", v[0]); + fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] [-s ns] hostname\n", v[0]); fprintf(stderr, "syntax: %s [-T]\n", v[0]); return 1; } - while ((opt = getopt(c, v, "xvc:T")) != -1) { + while ((opt = getopt(c, v, "xvc:Ts:")) != -1) { switch (opt) { case 'x': o.reverse = 1; break; case 'v': ++verbose; break; case 'g': o.use_getaddrinfo = 1; break; case 'T': o.servertest = 1; break; case 'c': o.resolv_conf = optarg; break; + case 's': o.ns = optarg; break; default : fprintf(stderr, "Unknown option %c\n", opt); break; } } @@ -205,10 +207,13 @@ main(int c, char **v) { if (optind < c) { int res; #ifdef _WIN32 - if (o.resolv_conf == NULL) + if (o.resolv_conf == NULL && !o.ns) res = evdns_base_config_windows_nameservers(evdns_base); else #endif + if (o.ns) + res = evdns_base_nameserver_ip_add(evdns_base, o.ns); + else res = evdns_base_resolv_conf_parse(evdns_base, DNS_OPTION_NAMESERVERS, o.resolv_conf); -- 2.40.0