😁博客主頁😁:🚀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》