Shammer's Philosophy

My private adversaria

hostent 内容出力

gethostbyaddr man page - Shammerism要素数文字数不特定の文字列配列の扱い - Shammerismの情報が揃ったので、hostent 構造体の内容を出力するプログラムを書けるはず、ということで書いて実行してみました。

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/time.h>
#include <stdlib.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);
		char **alias = hp->h_aliases;
		for( ; *alias != NULL ; alias++ ){
		    printf("alias of %s is %s\n", ipstring, *alias);
		}
		printf("addrtype of %s is %d\n", ipstring, hp->h_addrtype);
		char **addrlist = hp->h_addr_list;
		for( ; *addrlist != NULL ; addrlist++ ){
		    printf("%s has IP, %s\n", hp->h_name, inet_ntoa(*((struct in_addr *)addrlist)));
		}
	    }
	}
    }
    else {
	printf("Usage: %s $IP_ADDRESS\n", args[0]);
    }
    return 0;
}

実行結果は以下。

$ gcc hostentsample.c
$ ./a.out 127.0.0.1
name associated with 127.0.0.1 is localhost
alias of 127.0.0.1 is 1.0.0.127.in-addr.arpa
addrtype of 127.0.0.1 is 2
localhost has IP, XXX.XXX.XXX.XXX
$

XXX.XXX.XXX.XXX は、実際に使用されているIP。addrtype は、AF_INET のことと予想される。
今度は、gethostbyname か gethostbyname2 を試してみるか。