struct hostent
{
char *h_name; //正式主机名
char **h_aliases; //主机别名,别名,字符串数组
int h_addrtype; //协议类型:IPV4-AF_INET
int h_length; //网络地址大小,对于IPv4是四字节,即32位
char **h_addr_list; //指向网络地址的指针
};
用到的函数原型:
#include <netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#include <sys/socket.h> /* for AF_INET */
struct hostent *gethostbyaddr(const void *addr,
socklen_t len, int type);
void sethostent(int stayopen);
void endhostent(void);
void herror(const char *s);
const char *hstrerror(int err);
/* System V/POSIX extension */
struct hostent *gethostent(void);
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostent_r(
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyaddr_r(const void *addr, socklen_t len, int type,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
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);
域名服务器,查询域名返回的是IP地址:
例如:查询www.google.com返回的是66.249.89.99
使用 more /etc/hosts查看自己的域名:
andrew@andrew-Thurley:~/work/network$ more /etc/hosts
127.0.0.1 localhost
127.0.1.1 andrew-Thurley
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
可以看到其实环回地址就是127.0.0.1也就是localhost地址
gethostbyname仅仅支持IPV4并且在多线程中会出现问题,在多线程中可以考虑使用gethostent来解析域名:
使用gethostbyname解析dns信息代码:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>
void out_addr(struct hostent *h);
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("usage: %s host\n", argv[0]);
exit(1);
}
struct hostent *h;
h = gethostbyname(argv[1]);
if(h != NULL)
{
out_addr(h);
}
else
{
printf("no %s exits \n", argv[1]);
}
endhostent();
return 0;
}
void out_addr(struct hostent *h)
{
printf("hostname: %s\n", h->h_name);
printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPV4" : "IPV6");
char ip[16];
memset(ip, 0, sizeof(ip));
inet_ntop(h->h_addrtype, h->h_addr_list[0], ip, sizeof(ip));
printf("ip address: %s\n", ip);
int i = 0;
while(h->h_aliases[i] != NULL)
{
printf("aliase: %s\n", h->h_aliases[i]);
i++;
}
}
使用域名解析函数gethostent函数解析dns代码:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>
void out_addr(struct hostent *h);
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("usage: %s host\n", argv[0]);
exit(1);
}
struct hostent *h;
while((h = gethostent()) != NULL)
{
if(!strcmp(argv[1], h->h_name))
{
out_addr(h);
}
else
{
int i = 0;
while(h->h_aliases[i] != NULL)
{
if(!strcmp(argv[1], h->h_aliases[i]))
{
out_addr(h);
exit(0);
}
i++;
}
}
}
endhostent();
printf("no %s exist \n", argv[1]);
return 0;
}
void out_addr(struct hostent *h)
{
printf("hostname: %s\n", h->h_name);
printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPV4" : "IPV6");
char ip[16];
memset(ip, 0, sizeof(ip));
inet_ntop(h->h_addrtype, h->h_addr_list[0], ip, sizeof(ip));
printf("ip address: %s\n", ip);
}

本文介绍了域名解析的基础知识,并提供了使用gethostbyname和gethostent函数进行域名解析的示例代码。通过这些函数,可以将域名转换为IP地址。
1897

被折叠的 条评论
为什么被折叠?



