本代码实现功能:获取www.baidu.com对应的IP地址。就是DNS干的事情。根据测试代码查看man手册获取详细说明。
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
extern int h_errno;
int main(){
char **ptemp;
char str[100];
struct hostent *p = gethostbyname("www.baidu.com");
printf("official:%s\n",p->h_name);
ptemp = p->h_aliases;
for(;*ptemp != NULL;ptemp++){
printf("aliases = %s\n",*ptemp);
}
switch(p->h_addrtype){
case AF_INET:
ptemp = p->h_addr_list;
for(;*ptemp != NULL;ptemp++){
printf("*ptemp = %s\n",*ptemp);
memset(str,0,sizeof(str));
printf("ip = %s\n",inet_ntop(p->h_addrtype,*ptemp,str,sizeof(str)/sizeof(str[0])));
printf("*str = %s\n",str);
}
}
return 0;
}
运行结果:
lkmao@ubuntu:~$ ./a.out
official:www.a.shifen.com
aliases = www.baidu.com
*ptemp = ױ'www.ױ&ifen.com
ip = 14.215.177.39
*str = 14.215.177.39
*ptemp = ױ&ifen.com
ip = 14.215.177.38
*str = 14.215.177.38
lkmao@ubuntu:~$
使用nslookup命令验证测试代码的直接结果。对比后,完全一致。
lkmao@ubuntu:~$ nslookup www.baidu.com
Server: 127.0.1.1
Address: 127.0.1.1#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 14.215.177.38
Name: www.a.shifen.com
Address: 14.215.177.39
lkmao@ubuntu:~$
详细信息完全可以查看man手册。
#include <netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
结构体:struct hostent
The hostent structure is defined in <netdb.h> as follows:
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 */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.
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src,
char *dst, socklen_t size);
函数inet_ntop执行成功返回指向dst的指针,在测试结果中,就是IP地址的字符串形式。失败返回NULL;
DESCRIPTION
This function converts the network address structure src in the af address family into a character string. The resulting string is copied to the buffer pointed to by dst, which must be a non-null
pointer. The caller specifies the number of bytes available in this buffer in the argument size.inet_ntop() extends the inet_ntoa(3) function to support multiple address families, inet_ntoa(3) is now considered to be deprecated in favor of inet_ntop(). The following address families are cur‐
rently supported:AF_INET
src points to a struct in_addr (in network byte order) which is converted to an IPv4 network address in the dotted-decimal format, "ddd.ddd.ddd.ddd". The buffer dst must be at least
INET_ADDRSTRLEN bytes long.AF_INET6
src points to a struct in6_addr (in network byte order) which is converted to a representation of this address in the most appropriate IPv6 network address format for this address. The buffer
dst must be at least INET6_ADDRSTRLEN bytes long.
RETURN VALUE
On success, inet_ntop() returns a non-null pointer to dst. NULL is returned if there was an error, with errno set to indicate the error.