多级中文域名解析系统-3

ROOT_DNS

根服务器的逻辑代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "defAndTools.h"

#define RRFILE "RRroot.txt"

int main(int argc, char *argv[])
{
char sendbuf[512];
char recvbuf[512];
int sendBufferPointer=0;
int recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);

int sockfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
addr.sin_family =AF_INET;
addr.sin_port =htons(SERVER_PORT);
addr.sin_addr.s_addr=inet_addr(ROOT_SERVER_IP);

bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
struct sockaddr_in cli;
socklen_t len=sizeof(cli);
while(1)
{
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&cli,&len);
printf("\n收到本地服务器请求:\n");
//读取,构造包,得到需要找的域名和类型
struct DNS_Header *recv_header;
recv_header = malloc(sizeof(DNS_HEAD));
decode_header(recv_header,recvbuf,&recvBufferPointer);
print_header(recv_header);

struct DNS_Query *query_section;
query_section = malloc(sizeof(DNS_QUERY));
decode_query_section(query_section,recvbuf,&recvBufferPointer);
print_query_section(query_section);

//开始查找与写缓冲
//调用初次搜索函数
int over = firstFindRR(query_section,RRFILE,sendbuf,&sendBufferPointer);

//第一次搜索没有查到结果,开始查询下一个该去哪个服务器
if(over==0)
{
printf("\n本服务器没有找到\n");
loopFindNS(query_section,RRFILE,sendbuf,&sendBufferPointer);
}
//发送
sendto(sockfd,sendbuf,sendBufferPointer,0,(struct sockaddr*)&cli,len);
//缓冲区重置
sendBufferPointer=0;
recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);
}
close(sockfd);
}

DNS1

第一级域名解析服务器主逻辑,以下四个中间级服务器逻辑都差不多

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "defAndTools.h"

#define SERVER_IP "127.0.0.4"
#define RRFILE "RRL1.txt"

int main(int argc, char *argv[])
{
char sendbuf[512];
char recvbuf[512];
int sendBufferPointer=0;
int recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);

int sockfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
addr.sin_family =AF_INET;
addr.sin_port =htons(SERVER_PORT);
addr.sin_addr.s_addr=inet_addr(SERVER_IP);

bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
struct sockaddr_in cli;
socklen_t len=sizeof(cli);
while(1)
{
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&cli,&len);
printf("\n收到本地服务器请求:\n");
//读取,构造包,得到需要找的域名和类型
struct DNS_Header *recv_header;
recv_header = malloc(sizeof(DNS_HEAD));
decode_header(recv_header,recvbuf,&recvBufferPointer);
print_header(recv_header);

struct DNS_Query *query_section;
query_section = malloc(sizeof(DNS_QUERY));
decode_query_section(query_section,recvbuf,&recvBufferPointer);
print_query_section(query_section);

//开始查找与写缓冲
//调用初次搜索函数
int over = firstFindRR(query_section,RRFILE,sendbuf,&sendBufferPointer);

//第一次搜索没有查到结果,开始查询下一个该去哪个服务器
if(over==0)
{
printf("\n本服务器没有找到\n");
loopFindNS(query_section,RRFILE,sendbuf,&sendBufferPointer);
}
//发送
sendto(sockfd,sendbuf,sendBufferPointer,0,(struct sockaddr*)&cli,len);
//缓冲区重置
sendBufferPointer=0;
recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);
}
close(sockfd);
}

DNS2

第二级域名解析服务器主逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "defAndTools.h"

#define SERVER_IP "127.0.0.5"
#define RRFILE "RRL2.txt"

int main(int argc, char *argv[])
{
char sendbuf[512];
char recvbuf[512];
int sendBufferPointer=0;
int recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);

int sockfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
addr.sin_family =AF_INET;
addr.sin_port =htons(SERVER_PORT);
addr.sin_addr.s_addr=inet_addr(SERVER_IP);

bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
struct sockaddr_in cli;
socklen_t len=sizeof(cli);
while(1)
{
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&cli,&len);
printf("\n收到本地服务器请求:\n");
//读取,构造包,得到需要找的域名和类型
struct DNS_Header *recv_header;
recv_header = malloc(sizeof(DNS_HEAD));
decode_header(recv_header,recvbuf,&recvBufferPointer);
print_header(recv_header);

struct DNS_Query *query_section;
query_section = malloc(sizeof(DNS_QUERY));
decode_query_section(query_section,recvbuf,&recvBufferPointer);
print_query_section(query_section);

//开始查找与写缓冲
//调用初次搜索函数
int over = firstFindRR(query_section,RRFILE,sendbuf,&sendBufferPointer);

//第一次搜索没有查到结果,开始查询下一个该去哪个服务器
if(over==0)
{
printf("\n本服务器没有找到\n");
loopFindNS(query_section,RRFILE,sendbuf,&sendBufferPointer);
}
//发送
sendto(sockfd,sendbuf,sendBufferPointer,0,(struct sockaddr*)&cli,len);
//缓冲区重置
sendBufferPointer=0;
recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);
}
close(sockfd);
}

DNS3

第三级域名解析服务器主逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "defAndTools.h"

#define SERVER_IP "127.0.0.6"
#define RRFILE "RRL3.txt"

int main(int argc, char *argv[])
{
char sendbuf[512];
char recvbuf[512];
int sendBufferPointer=0;
int recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);

int sockfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
addr.sin_family =AF_INET;
addr.sin_port =htons(SERVER_PORT);
addr.sin_addr.s_addr=inet_addr(SERVER_IP);

bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
struct sockaddr_in cli;
socklen_t len=sizeof(cli);
while(1)
{
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&cli,&len);
printf("\n收到本地服务器请求:\n");
//读取,构造包,得到需要找的域名和类型
struct DNS_Header *recv_header;
recv_header = malloc(sizeof(DNS_HEAD));
decode_header(recv_header,recvbuf,&recvBufferPointer);
print_header(recv_header);

struct DNS_Query *query_section;
query_section = malloc(sizeof(DNS_QUERY));
decode_query_section(query_section,recvbuf,&recvBufferPointer);
print_query_section(query_section);

//开始查找与写缓冲
//调用初次搜索函数
int over = firstFindRR(query_section,RRFILE,sendbuf,&sendBufferPointer);

//第一次搜索没有查到结果,开始查询下一个该去哪个服务器
if(over==0)
{
printf("\n本服务器没有找到\n");
loopFindNS(query_section,RRFILE,sendbuf,&sendBufferPointer);
}
//发送
sendto(sockfd,sendbuf,sendBufferPointer,0,(struct sockaddr*)&cli,len);
//缓冲区重置
sendBufferPointer=0;
recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);
}
close(sockfd);
}

DNS4

第四级域名解析服务器主逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "defAndTools.h"

#define SERVER_IP "127.0.0.7"
#define RRFILE "RRL4.txt"

int main(int argc, char *argv[])
{
char sendbuf[512];
char recvbuf[512];
int sendBufferPointer=0;
int recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);

int sockfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
addr.sin_family =AF_INET;
addr.sin_port =htons(SERVER_PORT);
addr.sin_addr.s_addr=inet_addr(SERVER_IP);

bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
struct sockaddr_in cli;
socklen_t len=sizeof(cli);
while(1)
{
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&cli,&len);
printf("\n收到本地服务器请求:\n");
//读取,构造包,得到需要找的域名和类型
struct DNS_Header *recv_header;
recv_header = malloc(sizeof(DNS_HEAD));
decode_header(recv_header,recvbuf,&recvBufferPointer);
print_header(recv_header);

struct DNS_Query *query_section;
query_section = malloc(sizeof(DNS_QUERY));
decode_query_section(query_section,recvbuf,&recvBufferPointer);
print_query_section(query_section);

//开始查找与写缓冲
//调用初次搜索函数
int over = firstFindRR(query_section,RRFILE,sendbuf,&sendBufferPointer);

//第一次搜索没有查到结果,开始查询下一个该去哪个服务器
if(over==0)
{
printf("\n本服务器没有找到\n");
loopFindNS(query_section,RRFILE,sendbuf,&sendBufferPointer);
}
//发送
sendto(sockfd,sendbuf,sendBufferPointer,0,(struct sockaddr*)&cli,len);
//缓冲区重置
sendBufferPointer=0;
recvBufferPointer=0;
memset(sendbuf,0,512);
memset(recvbuf,0,512);
}
close(sockfd);
}

参数与设置

1
#define LOCAL_SERVER_IP "127.0.0.2"