Linux:通过域名,获取IP地址
参考链接:源码:
https://blog.csdn.net/qq_21794823/article/details/53068351
https://blog.csdn.net/daiyudong2020/article/details/51946080
#include
#include
#include
#include
#include
#include
#include
#include
#include #define pri(fmt, ...)printf("["__FILE__"][%s]-%d: " ,__FUNCTION__,__LINE__ );
\
printf(fmt, ##__VA_ARGS__);
extern int h_errno;
int main(int argc, char *argv[])
{
if(2 != argc)
{
printf("Param Num Error ! \n");
return -1;
}
char *name = argv[1];
struct hostent *host = gethostbyname(name);
int i;
if(NULL == host)
{
pri("gethostbyname h_errno : %d \n", h_errno);
return 0;
}
pri("host->h_name \t\t:%s \n", host->h_name);
for(i=0;
host->h_aliases[i];
i++)
{
pri("host->h_aliases[%d] \t\t:%s \n", i,(host->h_aliases[i]));
}
pri("host->h_addrtype \t\t:%s\n", (host->h_addrtype==AF_INET) ? "AF_INET": "AF_INET6");
pri("host->h_length \t\t:%d \n", host->h_length);
for(i=0;
host->h_addr_list[i];
i++)
{
pri("host->h_addr_list[%d] \t:%s \n", i,inet_ntoa(*(struct in_addr*)(host->h_addr_list[i])));
} return 0;
}
编译运行测试:
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname# gcc -o test test.c
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname# ./testwww.baidu.com
[test.c][main]-34: host->h_name:www.a.shifen.com
[test.c][main]-37: host->h_aliases[0]:www.baidu.com
[test.c][main]-39: host->h_addrtype:AF_INET
[test.c][main]-40: host->h_length:4
[test.c][main]-44: host->h_addr_list[0]:14.215.177.39
[test.c][main]-44: host->h_addr_list[1]:14.215.177.38
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname# ./testwww.qq.com
[test.c][main]-34: host->h_name:public.sparta.mig.tencent-cloud.net
[test.c][main]-37: host->h_aliases[0]:www.qq.com
[test.c][main]-39: host->h_addrtype:AF_INET
[test.c][main]-40: host->h_length:4
[test.c][main]-44: host->h_addr_list[0]:14.18.175.154
[test.c][main]-44: host->h_addr_list[1]:113.96.232.215
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname# ./testwww.alibaba.com
[test.c][main]-34: host->h_name:hz-scproxy.alibaba.com.gds.alibabadns.com
[test.c][main]-37: host->h_aliases[0]:www.alibaba.com
[test.c][main]-37: host->h_aliases[1]:www.alibaba.com.gds.alibabadns.com
[test.c][main]-37: host->h_aliases[2]:hz-scproxy.alibaba.com
[test.c][main]-39: host->h_addrtype:AF_INET
[test.c][main]-40: host->h_length:4
[test.c][main]-44: host->h_addr_list[0]:106.11.208.151
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname#
gethostbyname
函数介绍(可以使用 man gethostbyname
获取下述内容):头文件:
#include
函数原型:
struct hostent *gethostbyname(const char *name);
函数描述:
The gethostbyname() function returns a structure of type hostent for the given host name.Here name is either a hostname, or an IPv4 address in standard dot notation(asfor
inet_addr(3)), or an IPv6 address in colon (and possibly dot) notation.(See RFC 1884 for the description of IPv6 addresses.)If name is an IPv4 or IPv6 address, no lookup is
performed and gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into the h_addr_list[0] field of the returnedhostentstructure.If
namedoesn'tendina dot and the environment variable HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first be searched for name (see hostname(7) for the
file format).The current domain and its parents are searched unless name ends in a dot.
我理解的大致意思是 :
函数根据输入的测试如下:name
返回一个结构体hostent
。
输入的name
可以是主机域名,也可以点分十进制格式(IPV4/IPV6
)。
如果输入的是IPV4/IPV6
格式,那么在gethostbyname
函数中,只是简单的把name
复制给h_name
(hostent
结构体中的元素)。
。。。
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname# ./test106.11.208.151
[test.c][main]-34: host->h_name:106.11.208.151
[test.c][main]-39: host->h_addrtype:AF_INET
[test.c][main]-40: host->h_length:4
[test.c][main]-44: host->h_addr_list[0]:106.11.208.151
root@ubuntu:/home/samba/03_test/02_Linux/gethostbyname#
hostent 结构体:
The hostent structure is defined in as follows:struct hostent {
char*h_name;
/* official name of host */
char **h_aliases;
/* alias list */
inth_addrtype;
/* host address type */
inth_length;
/* length of address */
char **h_addr_list;
/* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */The members of the hostent structure are:h_name The official name of the host.h_aliases
An array of alternative names for the host, terminated by a NULL pointer.h_addrtype
The type of address;
always AF_INET or AF_INET6 at present.h_length
The length of the address in bytes.h_addr_list
An array of pointers to network addresses for the host (in network byte order), terminated by a NULL pointer.h_addr The first address in h_addr_list for backward compatibility.
返回值
RETURN VALUE
The gethostbyname() and gethostbyaddr() functions return the hostent structure or a NULL pointer if an error occurs.On error, the h_errnovariableholdsanerrornumber.
When non-NULL, the return value may point at static data, see the notes below.
【Linux:通过域名,获取IP地址函数使用】失败,返回NULL,错误值保存在h_errno中。
推荐阅读
- Linux|109 个实用 shell 脚本
- linux笔记|linux 常用命令汇总(面向面试)
- Linux|Linux--网络基础
- linux|apt update和apt upgrade命令 - 有什么区别()
- linux|2022年云原生趋势
- Go|Docker后端部署详解(Go+Nginx)
- 开源生态|GPL、MIT、Apache...开发者如何选择开源协议(一文讲清根本区别)
- GitHub|7 款可替代 top 命令的工具