misc/54189: DNS resolver should resolve hostnames with underscores

Jonathan Lennox lennox at cs.columbia.edu
Mon Jul 28 09:40:58 PDT 2003

On , July 19 2003, "Mário Freitas" wrote to "freebsd-bugs at FreeBSD.org, lennox at cs.columbia.edu" saying:

> The problem you submitted is due to mozilla's gethostbyname() own(bad)
> implementation. FreeBSD's resolver can deal with underscores in
> hostnames without any problem at all. I think you should submit that
> problem to mozilla's bug tracking system(yes I succeded resolving that
> hostname in FreeBSD 4.8 and 5.1).

Not so, at least if you go through the gethostbyname() or getaddrinfo()
APIs.  I've attached a small program that exercises both APIs, compiled
it on both FreeBSD 4.8-RELEASE and on Red Hat Linux 7.1, and executed both
on the same FreeBSD 4.8 machine (the Linux binary running under emulation):

conrail $ ./gethostbyname dear_raed.blogspot.com
dear_raed.blogspot.com: gethostbyname lookup failed: Unknown server error (3)
dear_raed.blogspot.com: getaddrinfo lookup failed: Non-recoverable failure in name resolution (4)
conrail $ ./gethostbyname-linux dear_raed.blogspot.com
dear_raed.blogspot.com [ghbn]: 
dear_raed.blogspot.com [gai]: 

The FreeBSD 'nslookup' and 'host' programs, which bypass these APIs and do
DNS queries directly, can indeed resolve the hostname:

conrail $ nslookup dear_raed.blogspot.com
Server:  sutton.cs.columbia.edu

Non-authoritative answer:
Name:    dear_raed.blogspot.com
conrail $ host dear_raed.blogspot.com
dear_raed.blogspot.com has address

-------------- next part --------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include <stdlib.h>

#include <netdb.h>

int main(int argc, char *argv[])
  int i;

  struct addrinfo hints;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s hostname [...]", argv[0]);

  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;

  for (i = 1; i < argc; i++) {
    int ret;
    struct hostent *he;
    struct addrinfo *ai;

    he = gethostbyname(argv[i]);
    if (he == NULL) {
      printf("%s: gethostbyname lookup failed: %s (%d)\n", argv[i],
             hstrerror(h_errno), h_errno);
    else {
      int j;

      printf("%s [ghbn]: ", argv[i]);
      for (j = 0; he->h_addr_list[j] != NULL; j++) {
        printf("%s ", inet_ntoa(*(struct in_addr*)(he->h_addr_list[j])));
    ret = getaddrinfo(argv[i], NULL, &hints, &ai);
    if (ret != 0) {
      printf("%s: getaddrinfo lookup failed: %s (%d)\n", argv[i],
             gai_strerror(ret), ret);
    else {
      struct addrinfo* this_ai;

      printf("%s [gai]: ", argv[i]);
      for (this_ai = ai; this_ai != NULL; this_ai = this_ai->ai_next) {
        printf("%s ",
               inet_ntoa(((struct sockaddr_in*)(this_ai->ai_addr))->sin_addr));


  return 0;
-------------- next part --------------

Jonathan Lennox
lennox at cs.columbia.edu

More information about the freebsd-bugs mailing list