I need to resolve some hostnames from my application. Is there any alternative to gethostbyname where I can give as a parameter my own DNS server to use as a resolver?
I have already coded my own function, but I thought there may be one I don't know about.
I am using Linux/C language. My libc is uclibc. But I am also curious about GNU LibC.
Thanks.
You'll need to do your own query, but it isn't difficult.
To do this, you would want to use the
res_query()
family of functions, which allow you to specify the resolver through an environmental variable:IBM's docs go a little more in depth with how to set the variable:
Notes -
You should call
setenv()
prior to callingres_init()
, or the setting will not be picked up. Remember to unset it once done, if applicableIf you use hostnames for the search domains,
/etc/hosts
still takes precedence.res_init()
was made properly reentrant in uclibc in 2007, so I'm pretty sure you could just use it for whatever purpose.These functions have the additional benefit of being able to retrieve more detailed data (MX, etc) as well. Still, if you have something smaller that works equally well, there's no sense in not using it.