69,336
社区成员
发帖
与我相关
我的任务
分享
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
/*-
* Copyright (C), 1988-2010, XXX Co., Ltd.
*
* $Id$
*
* $Log$
*/
#ifndef lint
static const char rcsid[] =
"$Id$";
static const char relid[] =
"$" "Date: "__FILE__" "__DATE__" "__TIME__" $";
#endif /* not lint */
/**
* @file
* @brief
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int print_host(const char *name, struct hostent *host)
{
char **alias;
int count;
struct in_addr addr;
(void)printf("%s: %s\n", "Official name", host->h_name);
count = 0;
for (alias = host->h_aliases; *alias; alias++)
(void)printf("%s #%d: %s\n", "Aliases", ++count, *alias);
switch (host->h_addrtype) {
case AF_INET:
break;
default:
break;
}
count = 0;
if (AF_INET == host->h_addrtype) {
while (host->h_addr_list[count]) {
addr.s_addr = *(in_addr_t *)host->h_addr_list[count++];
(void)printf("%s #%d: %s\n", "IP Address", count, inet_ntoa(addr));
}
}
printf("\n");
return 0;
}
int main(int argc, char *argv[])
{
int count;
char name[HOST_NAME_MAX + 1];
struct hostent *host;
struct in_addr addr;
if (argc >= 2) {
for (count = 1; count < argc; count++)
if (inet_aton(argv[count], &addr)) {
if (!!(host = gethostbyaddr(&addr, sizeof(addr), AF_INET)))
(void)print_host(argv[count], host);
else
(void)printf("%s %s %s\n\n", "address", argv[count], "NOT FOUND");
} else {
if (!!(host = gethostbyname(argv[count])))
(void)print_host(argv[count], host);
else
(void)printf("%s %s %s\n\n", "name", argv[count], "NOT FOUND");
}
} else {
gethostname(name, sizeof(name));
if (NULL != (host = gethostbyname(name)))
(void)print_host(name, host);
else
(void)printf("%s %s %s\n\n", "name", argv[count], "NOT FOUND");
}
return 0;
}