How to use "dns_new()" (I get "dns_new: Assertion `(copy->dnsc_flags & DNS_INITED)' failed")

Iñaki Baz Castillo ibc at aliax.net
Sat Feb 5 20:51:45 MSK 2011


2011/2/5 Michael Tokarev <mjt at tls.msk.ru>:
>> If I omit the line "dns_init(dns_context, 0)" then it doesn't work
>> well (please check my other mail with subject "Problem when non using
>> default ctx" in which I report it).
>
> dns_new() returns initialized but not open context.
> In the above sequence there's no need to call dns_init()
> twice.

This is what I expected, but if I remove the second "dns_init()" line
then strange things happen when the queried domain doesn't exist (it
takes 31 seconds to end the script).

Please check the following code:

--------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/poll.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "udns.h"


int main(int argc, char **argv) {
  struct dns_ctx *dns_context;
  struct dns_rr_a4 *res_A;
  char *domain;
  in_addr_t ip_binary;
  char *ip = (char *)malloc(sizeof(char) * 16);
  int i;

  if (argc != 2) {
    fprintf(stderr, "ERROR: a domain must be provided as argument\n");
    return 1;
  }
  domain = argv[1];

  if (dns_init(NULL, 0) < 0) {
    fprintf(stderr, "FATAL: unable to initialize dns library\n");
    return 1;
  }
  dns_context = dns_new(NULL);
  dns_init(dns_context, 0);  // <------------------ COMMENT / UNCOMMENT
  dns_open(dns_context);

  res_A = dns_resolve_a4(dns_context, domain, 0);

  if (! res_A) {
    fprintf(stderr, "ERROR: no DNS result\n");
    return 2;
  }

  ip = (char *)dns_ntop(AF_INET, &res_A->dnsa4_addr[0].s_addr, ip, 16);
  printf("- IP: %s\n", ip);

  return 0;
}
--------------------------


If you leave the second dns_init() uncommeted and query a non existing
domain you get an instant "ERROR: no DNS result".

But if you comment the second dns_init() it takes 31 seconds to
terminate and performs invalid/malformed DNS queries after receiving
the response "0011 No such name"). I've checked it with tcpdump and
wireshark.

NOTE: I use udns 0.9 version (Ubuntu). Could this issue make reference
to a change in 1.0?:

0.1 (Dec 2010)
 - bugfix: udns_new(old) - when actually cloning another context -
   makes the new context referencing memory from old, which leads
   to crashes when old is modified later


-- 
Iñaki Baz Castillo
<ibc at aliax.net>


More information about the udns mailing list