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);
This commit is contained in:
Azat Khuzhin 2014-11-08 15:51:49 +03:00
parent 32f8592c8b
commit df19a970e1

View File

@ -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);