[En-Nut-Discussion] NutDnsGetHostByName dotted quad hostname

Harald Kipp harald.kipp at egnite.de
Fri Feb 27 13:01:11 CET 2015


Hi Coleman,

On 24.02.2015 18:14, Coleman Brumley wrote:
> In working with DNS name resolution recently (4.10.3), I realized that
> NutDnsGetHostByName doesn't accept hostnames in IPv4 dotted quad format. 
> 
> Since gethostbyname() allows it, and the dotted quad format is an accepted
> hostname per RFC1123, I was thinking it would be nice if NutDnsGetHostByName
> did the same thing. 

Typically I implement my own local gethostbyname() and do not use
NutDnsGetHostByName() directly. The advantage is, that I have a single
routine to report any DNS error (see below).


> u_long NutDnsGetHostByName(CONST u_char * hostname)
> {
>     u_long ip=0;
>     ip=inet_addr(hostname);
>     if(ip==0)
>     	ip=NutDnsGetResource(hostname, 1);
>     return ip;
> }

This is not fully correct. inet_addr() returns not 0, but INADDR_NONE
((uint32_t)-1) in case of an error.

Your alternative, calling NutDnsGetResource() first and then inet_addr()
should be avoided. The latter is a short calculation while the first
initiates a lengthy DNS query.


uint32_t GetHostIp(const char *host)
{
    uint32_t ip = 0;

    if (host && *host) {
        ip = inet_addr(host);
        if (ip == (uint32_t) -1) {
            ip = NutDnsGetHostByName((uint8_t *) host);
            if (ip == 0) {
                syslog(LOG_WARN, "DNS query for %s failed", host);
            }
        }
    }
    return ip;
}

Anyway, the DNS part has some room for improvements. For example, the
host name parameter should have been a char pointer, not a pointer to
uint8_t. I think that this has been a leftover from a general fix.
Initially Nut/OS defined all strings as u_char, which was a bad decision.

But today's compilers are very picky about signed/unsigned mismatch.
Changing this now would cause a lot of trouble for existing code.

Regards,

Harald



More information about the En-Nut-Discussion mailing list