linux 域名轉ip的方法
使用gethostbyname函式,然後通過inet_ntoa得到ip地址,inet_ntoa函式功能將一個IP轉換成一個網際網路標準點分格式的字串;
或者使用gethostbyname_r函式;
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct hostent *h;
char hostname[40];
printf("請輸入域名/n");
scanf("%s",hostname);
getchar();
if((h=gethostbyname(hostname))==NULL)
{
fprintf(stderr,"不能得到IP/n");
exit(1);
}
printf("HostName :%s/n",h->h_name);
printf("IP Address :%s/n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return EXIT_SUCCESS;
}
//
使用gethostbyname_r 注意的地方,如果name是xxx.xxx.xxx.xxx,這樣的格式,返回值為0,不一定代表轉換的result正確;當name本身不是一個合格的ip地址後,根本沒有呼叫dns,也就是說我們用的時候,如果name是ip地址,那麼必須保證它是正確的,在判斷返回為0而且result!=NULL才表示函式呼叫成功!
關於gethostbyname_r的返回值
man手冊是這麼說的
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostbyname_r (const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r (const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
......
The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls. Copying the
struct hostent does not suffice, since it contains pointers - a deep
copy is required.
Glibc2 also has a gethostbyname2() that works like gethostbyname(),
but permits to specify the address family to which the address must
belong.
Glibc2 also has reentrant versions gethostbyname_r() and gethostby-
name2_r(). These return 0 on success and nonzero on error. The result
of the call is now stored in the struct with address ret. After the
call, *result will be NULL on error or point to the result on success.
Auxiliary data is stored in the buffer buf of length buflen. (If the
buffer is too small, these functions will return ERANGE.) No global
variable h_errno is modified, but the address of a variable in which
to store error numbers is passed in h_errnop.
按照man的說法
gethostbyname_r()呼叫成功時返回0
*result指向解析成功的資料結構, *result如果為NULL則表示解析出錯
man手冊是這麼說的
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostbyname_r (const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r (const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
......
The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls. Copying the
struct hostent does not suffice, since it contains pointers - a deep
copy is required.
Glibc2 also has a gethostbyname2() that works like gethostbyname(),
but permits to specify the address family to which the address must
belong.
Glibc2 also has reentrant versions gethostbyname_r() and gethostby-
name2_r(). These return 0 on success and nonzero on error. The result
of the call is now stored in the struct with address ret. After the
call, *result will be NULL on error or point to the result on success.
Auxiliary data is stored in the buffer buf of length buflen. (If the
buffer is too small, these functions will return ERANGE.) No global
variable h_errno is modified, but the address of a variable in which
to store error numbers is passed in h_errnop.
按照man的說法
gethostbyname_r()呼叫成功時返回0
*result指向解析成功的資料結構, *result如果為NULL則表示解析出錯
如果被解析的字串是類似"xxx.xxx.xxx.xxx"的 數字+點 字串
那麼gethostbyname_r並沒有發出DNS請求
而是直接去計算了
192.1468.80.38顯然不是合法的IP地址, 但gethostbyname_r()返回0
此時*result是NULL
那麼gethostbyname_r並沒有發出DNS請求
而是直接去計算了
192.1468.80.38顯然不是合法的IP地址, 但gethostbyname_r()返回0
此時*result是NULL
轉一篇文章:
相關文章
- Linux下域名IP互轉Linux
- vb中從域名得到IP及從IP得到域名 (轉)
- FreeBSD對IP地址和域名的控制(轉)
- linux配置IP的方法Linux
- Linux配置IP地址的方法Linux
- 解析域名得到IP
- 實現具有動態主機IP的域名解析 (轉)
- IP地址 、域名、 URL、 埠
- 【Java】從域名得到ipJava
- Azure Public IP DNS域名DNS
- ping ip、域名、埠
- 域名和IP地址收集
- 為什麼要並用域名和IP地址?域名和IP地址並用的理由
- 將某一主機域名解析為IP地址。 (轉)
- 將某一主機域名解析為IP地址 (轉)
- aws 的pod 有 ip 或者域名嗎
- linux系統設定IP的方法Linux
- 【Linux小知識】網址、域名和ip的關係與區別!Linux
- linux下查詢域名或IP註冊資訊的操作記錄(whois)Linux
- 解析域名,獲取域名ip並輸出到終端
- linux初學者自定義IP地址的方法Linux
- 你的域名是如何變成 IP 地址的?
- 5 個用於在 Linux 終端中查詢域名 IP 地址的命令Linux
- 看這位“翻譯官-DNS”如何轉換域名和IP地址DNS
- 域名和ip是多對多的關係
- 可線上查詢子域名和同IP域名的RapidDNS聯動GobyAPIDNSGo
- Linux下設定靜態IP和獲取動態IP的方法Linux
- 在Linux下配置TCP/IP(轉)LinuxTCP
- 14IP地址、主機名、域名
- Linux網路配置方法(DNS,IP,GW)LinuxDNS
- linux繫結多個ip(轉載)Linux
- 使用IP鏈建立Linux防火牆(轉)Linux防火牆
- 阿里雲如何繫結域名(阿里雲域名如何繫結ip)阿里
- 限制單個IP併發TCP連線的方法(轉)TCP
- 域名繫結動態IP實戰案例
- IP頁面訪問域名介面問題
- 最新Godaddy賬戶域名解鎖/獲取轉移碼的方法Go
- linux 下修改IP和閘道器方法 。Linux