views:

295

answers:

2

I want to write some portable (as possible) C code to look up DNS TXT records. I would also prefer not to have dependencies on libraries that don't ship with the machine.

What is the best way to do this on Unix-like machines using standard libraries?

I found some sample code that works using libresolv, but it's not reentrant (not thread safe) and is very ugly. Is there a better way?

Also, what about Windows? If there were a way that worked there too that would be perfect.

A: 

Hi, you can use res_query which uses the standard libresolv.

There's an example here from clamav:

if((len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ)) < 0) {
  mprintf("@Can't query %s\n", domain);
  return NULL;
}
Yes, that's the sort of code that I found. You then have to parse the answer quite a bit.But it's not reentrant. If you used it from multiple threads, you would have to surround it with a static mutex.There are reentrant calls for getting A, AAAA, and CNAME addresses via getaddrinfo() and pals. I was curious if there's anything I don't know that does something similar for TXT records.
AdamIerymenko