Re: Using /etc/hosts, not dns

From: 0x1eef <0x1eef_at_protonmail.com>
Date: Sat, 05 Aug 2023 18:19:45 UTC
> Correct, Linux still works.  After all these years and my extensive Unix
> experience, I am abandoning FreeBSD.  You have completely abandoned
> common sense.

I thought this might be an interesting bit of information to add to the
thread. 

On OpenBSD, /etc/resolv.conf supports a 'lookup' keyword that FreeBSD
has not implemented (AFAICT). 

From the man page:

     lookup      This keyword is used by the library routines gethostbyname(3)
                 and gethostbyaddr(3).  It specifies which databases should be
                 searched, and the order to do so.  The legal space-separated
                 values are:

                       bind  Query a domain name server.
                       file  Search for entries in /etc/hosts.

                 If the lookup keyword is not used in the system's resolv.conf
                 file then the assumed order is bind file.  Furthermore, if
                 the system's resolv.conf file does not exist, then the only
                 database used is file.

But the 'host' command is the same as FreeBSD, and does not consult /etc/hosts.
It should still be possible to write a small C program that uses gethostbyname(3),
and that should respect the lookup order. As long as resolv.conf has 
'lookup file bind' it would consult /etc/hosts first and then consult 
name servers second.

I wrote a proof of concept (I'm trying to learn C, so this was a good task 
for me):

#include <netdb.h>
#include <stdio.h>
#include <sys/socket.h>

#define IPv4_FORMAT "%hhu.%hhu.%hhu.%hhu"

int
main(void) {
  struct hostent *ent;
  ent = gethostbyname2("read.amazon.com", AF_INET);
  if (h_errno == NETDB_SUCCESS) {
    char *addr = ent->h_addr_list[0];
    fprintf(stdout, IPv4_FORMAT, addr[0], addr[1], addr[2], addr[3]);
    fprintf(stdout, "\n");
  } else {
    fprintf(stderr, "error: %s", hstrerror(h_errno));
  }

  return 0;
}

0x1eef