Shammer's Philosophy

My private adversaria

gethostbyaddr man page

man gethostbyaddr で得られた情報の一部(Description)。

DESCRIPTION
     The getaddrinfo(3) and getnameinfo(3) functions are preferred over the
     gethostbyname(), gethostbyname2(), and gethostbyaddr() functions.

     The gethostbyname(), gethostbyname2() and gethostbyaddr() functions each
     return a pointer to an object with the following structure describing an
     internet host referenced by name or by address, respectively.

     The name argument passed to gethostbyname() or gethostbyname2() should
     point to a NUL-terminated hostname.  The addr argument passed to
     gethostbyaddr() should point to an address which is len bytes long, in
     binary form (i.e., not an IP address in human readable ASCII form).  The
     type argument specifies the address family (e.g. AF_INET, AF_INET6, etc.)
     of this address.

     The structure returned contains information obtained from
     mDNSResponder(8), including records in /etc/hosts.

     struct  hostent {
             char    *h_name;        /* official name of host */
             char    **h_aliases;    /* alias list */
             int     h_addrtype;     /* host address type */
             int     h_length;       /* length of address */
             char    **h_addr_list;  /* list of addresses from name server */
     };
     #define h_addr  h_addr_list[0]  /* address, for backward compatibility */

     The members of this structure are:

     h_name       Official name of the host.

     h_aliases    A NULL-terminated array of alternate names for the host.

     h_addrtype   The type of address being returned; usually AF_INET.

     h_length     The length, in bytes, of the address.

     h_addr_list  A NULL-terminated array of network addresses for the host.
                  Host addresses are returned in network byte order.

     h_addr       The first address in h_addr_list; this is for backward compatibility.

     When using the nameserver, gethostbyname() and gethostbyname2() will
     search for the named host in the current domain and its parents unless
     the name ends in a dot.

     The gethostbyname2() function is an evolution of gethostbyname() which is
     intended to allow lookups in address families other than AF_INET, for
     example AF_INET6.

     The herror() function writes a message to the diagnostic output consist-
     ing of the string argument string, the constant string ": ", and a mes-
     sage corresponding to the value of h_errno.

     The hstrerror() function returns a string which is the message text cor-
     responding to the value of the err argument.

FILES
     /etc/hosts
     /etc/resolv.conf

EXAMPLES
     Print out the hostname associated with a specific IP address:

           const char *ipstr = "127.0.0.1";
           struct in_addr ip;
           struct hostent *hp;

           if (!inet_aton(ipstr, &ip))
                   errx(1, "can't parse IP address %s", ipstr);

           if ((hp = gethostbyaddr((const void *)&ip,
               sizeof ip, AF_INET)) == NULL)
                   errx(1, "no name associated with %s", ipstr);

           printf("name associated with %s is %s\n", ipstr, hp->h_name);

Examples があったので試してみよう。IP アドレスからホスト名を調べる、つまり、RDNS の動作だ。
サンプル実装は以下。

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/time.h>

int main(int argc, char *args[]){
    if( argc == 2 ){
	char *ipstring = args[1];
	struct in_addr ip;
	struct hostent *hp;
	if( !inet_aton(ipstring, &ip) ){
	    printf("Can't parse IP address, %s\n", ipstring);
	}
	else {
	    hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET);
	    if( hp == NULL ){
		printf("No name associated with %s\n", ipstring);
	    }
	    else {
		printf("name associated with %s is %s\n", ipstring, hp->h_name);
	    }
	}
    }
    else {
	printf("Usage: %s $IP_ADDRESS\n", args[0]);
    }
    return 0;
}

実行結果は以下。

$ gcc reversedns.c
$ ./a.out localhost
Can't parse IP address, localhost
$ ./a.out 127.0.0.1
name associated with 127.0.0.1 is localhost
$

なるほど。hostent 構造体の内容もわかった。この内容を表示させることに挑戦してみよう。char **h_addr_list 以外は簡単にできるだろう。gethostbyname や、gethostbyname2 を使用すれば、ホスト名からIPアドレスを取得っていうのができそうな気がする。