😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭
🤣本文内容🤣:🍭介绍 gethostbyaddr 函数 🍭
😎金句分享😎:🍭你不能选择最好的,但最好的会来选择你——泰戈尔🍭
⏰发布时间⏰:2024-03-01 10:15:25
本文未经允许,不得转发!!!
目录
- 🎄一、概述
- 🎄二、gethostbyaddr 函数
- ✨2.1 gethostbyaddr 函数介绍
- ✨2.2 hostent 结构体说明
- ✨2.3 gethostbyaddr 函数的工作原理
 
- 🎄三、gethostbyaddr 函数使用例子
- 🎄四、总结

🎄一、概述
上篇文章介绍了 gethostbyname 函数,可以通过域名来获取到域名对应的IP地址。本文将介绍另一个函数 gethostbyaddr,它的功能与 gethostbyname 函数正好相反,可以通过二进制的IP地址找到相应的主机名。
下面将详细介绍 gethostbyaddr 函数,并且使用C语言例子演示 gethostbyaddr 函数的使用。

🎄二、gethostbyaddr 函数
✨2.1 gethostbyaddr 函数介绍
- 1、函数原型:#include <netdb.h> #include <sys/socket.h> /* for AF_INET */ struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);
- 2、函数描述:
 gethostbyaddr函数可以获取到指定的主机地址addr对应的主机信息,并通过struct hostent *返回。
- 3、函数参数: - addr:参数addr不是void*类型, 而是一个真正指向含有IPv4或IPv6地址的结构in_addr或in6_addr;
- len:第一个参数的结构大小,对于 IPv4地址为4,对于IPv6地址为16;
- type:协议族类型,IPv4用AF_INET,IPv6用AF_INET6;
 
- addr:参数addr不是void*类型, 而是一个真正指向含有IPv4或IPv6地址的结构
- 4、返回值:
 成功返回hostent结构体指针(看下一小节),失败返回NULL,且设置h_errno变量。注意,出错时不设置errno变量,而是设置h_errno变量,且提供了hstrerror函数来获取h_errno的值对应的字符串。
✨2.2 hostent 结构体说明
hostent结构体定义在 netdb.h 文件中,内容如下:
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 */
}
#define h_addr h_addr_list[0] /* for backward compatibility */

 结构体字段说明:
- h_name:官方域名(Official domain name)。官方域名代表某一主页,但实际上一些著名公司的域名并未用官方域名注册。
- h_aliases:主机的别名数组,以NULL指针结束,可以通过多个域名访问同一主机。同一 IP 地址可以绑定多个域名,因此除了当前域名还可以指定其他域名。
- h_addrtype:地址的类型;按照DNS的说法,gethostbyaddr只能返回IPv4(- AF_INET)的地址。
- h_length:保存IP地址长度。
- h_addr_list:指向主机IP地址的指针数组(按网络字节顺序),以NULL指针结束。需要通过- inet_ntop函数转换。
- h_addr:- h_addr_list中第一个地址。
✨2.3 gethostbyaddr 函数的工作原理
gethostbyaddr 函数的主要功能是根据输入的 IP 地址获取相应的主机名信息。它通过查询本地主机上的 DNS 解析器来查找 IP 地址对应的主机名,并返回一个包含有关主机名和IP地址的 struct hostent 类型的结构体。下面是 gethostbyaddr 函数的基本工作流程:
- 1、接受一个指向 struct in_addr类型的 IP 地址结构体的指针作为参数。
- 2、向 DNS 服务器发送查询请求,以获取该 IP 地址对应的主机名。
- 3、返回一个包含主机名和IP地址信息的 struct hostent结构体。

🎄三、gethostbyaddr 函数使用例子
// gethostbyaddr_sample.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>int main(int argc, char **argv)
{if (argc != 2) {printf("Use example: %s 127.0.0.1\n", *argv);return -1;}char *ptr, **pptr;struct in_addr addr;struct hostent *phost;char str[32] = {0};ptr = argv[1];printf("ip:%s\n", ptr);if (inet_pton(AF_INET, ptr, &addr) <= 0) {printf("inet_pton error:%s\n", strerror(errno));return -1;}phost = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);if (phost == NULL) {printf("gethostbyaddr error:%s\n", strerror(h_errno));return -1;}	printf("official hostname:%s\n", phost->h_name);   	//主机规范名return 0;
}
运行结果:
 

🎄四、总结
👉本文主要介绍将域名转换为IP地址的函数gethostbyaddr,以及提供使用例子。
通过本文的介绍,我们详细了解了 Linux 系统中 gethostbyaddr 函数的定义和使用场景。gethostbyaddr 函数在网络编程中具有重要意义,帮助开发人员根据 IP 地址查询对应的主机名,从而支持各种网络应用和通信需求。希望本文能够帮助您更好地理解并应用 gethostbyaddr 函数。

 如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁
参考资料:
 1、Linux的man手册
 2、《Unix网络编程卷1》