目录
域名系统(Domain Name System,DNS)是对 IP 地址和域名进行相互转换的系统,其核心是 DNS 服务器;
一般来说,IP 地址比较难记且经常变化,而域名容易记且易表述,并不会轻易改变域名;(Naver 网站的 IP 地址是222.122.195.5 而其域名为 www.naver.com)
域名是赋予服务器端的虚拟地址,并非实际地址,因此需要通过 DNS 服务器将虚拟的域名地址转换为实际的 IP 地址;
通常,计算机会向内置的默认 DNS 服务器请求获得域名对应的 IP 地址,若默认 DNS 服务器无法解析,则默认 DNS 服务器会不断向上级 DNS 服务器询问,直至获得域名对应的 IP 地址;
程序中使用域名的必要性:
① IP 地址比域名发生变更的概率要高,因此一般不会利用 IP 地址来编写程序;
② 域名一般不会变更,通过域名编写程序,在程序中根据域名来获取 IP 地址,再通过转换的 IP 地址接入相应的服务器,就比直接使用 IP 地址显得更为高效;
- #include
- struct hostent* gethostbyname(const char* hostname);
- // 成功时返回 hostent 结构体地址,失败时返回 NULL 指针
-
- // hosttent结构体的定义如下:
- struct hostent{
- char* h_name;
- char** h_aliases;
- int h_addrtype;
- int h_length;
- char** h_addr_list;
- }
在上述 hostent 结构体中,h_name 表示官方域名;h_aliases 表示其它域名信息(因为多个域名可以访问同一主页,同一个 IP 也可以绑定多个域名);h_addrtype 表示地址族信息(例如 IPv4,则对应为 AF_INET); h_length 表示 IP 地址的长度(IPv4 则为 4,IPv6 则为 16);h_addr_list 表示已整数形式保存的 IP 地址;
- #include
- struct hostent* gethostbyaddr(const char* addr, socklen_t len, int family);
- // 成功时返回 hostent 结构体变量地址值,失败时返回 NULL 指针
- // addr 表示函数 IP 地址信息的 in_addr 结构体指针,为了同时传递 IPv4 地址之外的其他信息,该变量的类型声明为 char 指针
- // len 表示向第一个参数传递的地址信息的字节数,IPv4 为 4,IPv6 为 16
- // family 表示传递的地址族信息,IPv4 为 AF_INET,IPv6 为 AF_INET6
- // gcc gethostbyname.c -o gethostbyname
- // ./gethostbyname www.baidu.com
-
- #include
- #include
- #include
- #include
- #include
- #include
-
- void error_handling(char *message){
- fputs(message, stderr);
- fputc('\n', stderr);
- exit(1);
- }
-
- int main(int argc, char *argv[]){
- int i;
- struct hostent *host;
- if(argc != 2){
- printf("Usage : %s
\n" , argv[0]); - exit(1);
- }
-
- host = gethostbyname(argv[1]);
- if(!host){
- error_handling("gethost... error");
- }
-
- printf("Official name: %s \n", host->h_name);
- for(i = 0; host->h_aliases[i]; i++){
- printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);
- }
- printf("Address type: %s \n", (host->h_addrtype == AF_INET)?"AF_INET":"AF_INET6");
- for(i = 0; host->h_addr_list[i]; i++){
- printf("IP addr %d: %s \n", i+1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
- }
- return 0;
- }

- // gcc gethostbyaddr.c -o gethostbyaddr
- // ./gethostbyaddr 199.59.148.206
-
- #include
- #include
- #include
- #include
- #include
- #include
-
- void error_handling(char *message){
- fputs(message, stderr);
- fputc('\n', stderr);
- exit(1);
- }
-
- int main(int argc, char *argv[]){
- int i;
- struct hostent *host;
- struct sockaddr_in addr;
- if(argc != 2){
- printf("Usage : %s
\n" , argv[0]); - exit(1);
- }
-
- memset(&addr, 0, sizeof(addr));
- addr.sin_addr.s_addr = inet_addr(argv[1]);
- host = gethostbyaddr((char*) &addr.sin_addr, 4, AF_INET);
- if(!host){
- error_handling("gethost... error");
- }
-
- printf("Official name: %s \n", host->h_name);
- for(i = 0; host->h_aliases[i]; i++){
- printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);
- }
- printf("Address type: %s \n", (host->h_addrtype == AF_INET)?"AF_INET":"AF_INET6");
- for(i = 0; host->h_addr_list[i]; i++){
- printf("IP addr %d: %s \n", i+1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
- }
- return 0;
- }
