[TCP / IP] network programming: 08 domain name and network address

Benpian document outlines the relationship between domain names and IP addresses and system.

Domain Name System

DNS (Domain Name System, the domain name system) is the IP address and domain name conversion system, the core of the DNS server.

What is a domain name

Providing network services through the IP address of the server is also distinguished, but the IP address in the form of cumbersome, usually more concise domain name instead of IP address. Another advantage of using the domain name service request is that the domain name servers generally will not easily change, but its IP address may need to change frequently.

DNS server

Enter the IP address in the Baidu website address bar of your browser 183.232.231.172 to browse Baidu home page, but we usually enter is Baidu's domain name www.baidu.com, so more convenient. From the results, two ways can enter Baidu's home page, and there is no other difference. A domain name is given to the server virtual address, rather than the actual address. Therefore, the need to convert the domain name to communicate properly to the actual IP address, DNS server is responsible tool for converting between domain names and IP addresses.

The DNS server then work is like? The computer usually have built-in default DNS server, but it is impossible to know all the domain names and IP address information. If the default DNS server can not resolve, it will ask other DNS servers, and the final results back to the user.

 DNS service request

The default DNS server receives a request that they can not be resolved, it will be asked to the superior DNS server. Through stepwise manner up request reaches the top level DNS server --- root DNS servers, that DNS server knows which lower the interrogation, the same way and finally the resolved IP address information back and transmitted to the requesting host. DNS is a distributed database system is a hierarchical management.

Conversion between IP addresses and domain names

The reason why the conversion between IP addresses and domain names needs, because to write the actual program is commonly used in a more stable domain name instead of IP addresses, which requires domain name and then converted into the corresponding IP address information. Seemingly redundant operation, in fact, is considered the reliability of the program and do protection.

Obtain an IP address using the domain name

#include <netdb.h> struct hostent the gethostbyname * ( const char * hostname);
     -> Back hostent structure pointer success, failure to return a NULL pointer // hostent structure is defined struct hostent 
{ char * h_name;         // Official name char ** h_aliases      // aliase List int h_addrtype;       // Host address type int h_length;         // address lengh char ** the h_addr_list;   // address List 
}

 



    
    
    
    
    

 hostent structure variable structure

 h_addr_list structure member

hostent结构体变量中最重要的或者说我们最关注的成员是h_addr_list,该变量以字符指针数组的形式保存域名对应的IP地址结构(使用char*而非in_addr*是为了兼容性的考虑,其实更为合适的类型是void*,不过当时void*还并未标准化)。之所以会存在多个IP地址,是考虑到大量用户情况下需要利用多个服务器进行负载均衡。gethostbyname函数的示例代码如下。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <arpa/inet.h>
 5 #include <netdb.h>
 6 void error_handling(char *message);
 7 
 8 int main(int argc, char *argv[])
 9 {
10     int i;
11     struct hostent *host;
12     if(argc!=2) {
13         printf("Usage : %s <addr>\n", argv[0]);
14         exit(1);
15     }
16     
17     host=gethostbyname(argv[1]);
18     if(!host)
19         error_handling("gethost... error");
20 
21     printf("Official name: %s \n", host->h_name);
22     
23     for(i=0; host->h_aliases[i]; i++)
24         printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);
25     
26     printf("Address type: %s \n", 
27         (host->h_addrtype==AF_INET)?"AF_INET":"AF_INET6");
28 
29     for(i=0; host->h_addr_list[i]; i++)
30         printf("IP addr %d: %s \n", i+1,
31                     inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
32     return 0;
33 }
34 
35 void error_handling(char *message)
36 {
37     fputs(message, stderr);
38     fputc('\n', stderr);
39     exit(1);
40 }
gethostbyname

运行结果

若输入我们所熟悉的百度域名,则可得到如下结果:

[root@XXX-Linux exercises]# ./ttt www.baidu.com

Official name: www.a.shifen.com 
Aliases 1: www.baidu.com 
Address type: AF_INET 
IP addr 1: 183.232.231.172 
IP addr 2: 183.232.231.174 

利用IP地址获取域名

#include <netdb.h>

struct hostent *gethostbyaddr(const char *addr, socklen_t len, int family);
    -> 成功时返回hostent结构体变量指针,失败时返回NULL指针

通过输入google的IP地址,gethostbyaddr函数的示例代码如下。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <arpa/inet.h>
 6 #include <netdb.h>
 7 void error_handling(char *message);
 8 
 9 int main(int argc, char *argv[])
10 {
11     int i;
12     struct hostent *host;
13     struct sockaddr_in addr;
14     if(argc!=2) {
15         printf("Usage : %s <IP>\n", argv[0]);
16         exit(1);
17     }
18 
19     memset(&addr, 0, sizeof(addr));
20     addr.sin_addr.s_addr=inet_addr(argv[1]);
21     host=gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
22     if(!host)
23         error_handling("gethost... error");
24 
25     printf("Official name: %s \n", host->h_name);
26 
27     for(i=0; host->h_aliases[i]; i++)
28         printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);
29     
30     printf("Address type: %s \n", 
31         (host->h_addrtype==AF_INET)?"AF_INET":"AF_INET6");
32 
33     for(i=0; host->h_addr_list[i]; i++)
34         printf("IP addr %d: %s \n", i+1,
35                     inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));    
36     return 0;
37 }
38 
39 void error_handling(char *message)
40 {
41     fputs(message, stderr);
42     fputc('\n', stderr);
43     exit(1);
44 }
gethostbyaddr

运行结果

Guess you like

Origin www.cnblogs.com/Glory-D/p/12116095.html