-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.h
122 lines (92 loc) · 2.17 KB
/
dns.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef DNS_PJ_DNS_H
#define DNS_PJ_DNS_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
#include <time.h>
#pragma pack(push, 1)
typedef struct DNSHeader
{
unsigned short usTransID;
unsigned char RD : 1;
unsigned char TC : 1;
unsigned char AA : 1;
unsigned char opcode : 4;
unsigned char QR : 1;
unsigned char rcode : 4;
unsigned char zero : 3;
unsigned char RA : 1;
unsigned short Questions;
unsigned short AnswerRRs;
unsigned short AuthorityRRs;
unsigned short AdditionalRRs;
} DNSHeader;
#pragma pack(pop)
typedef struct DNSQuery
{
char name[256];
unsigned short type;
unsigned short class;
} DNSQuery;
typedef struct DNSRr
{
char name[256];
unsigned short type;
unsigned short class;
unsigned int ttl;
unsigned short dataLength;
char data[256];
} DNSRr;
typedef struct DNSBody
{
DNSHeader dnsHeader;
DNSQuery *query;
DNSRr *answer;
DNSRr *authority;
DNSRr *additional;
} DNSBody;
typedef struct List
{
char *content;
size_t length;
struct List *next;
} List;
#define T_A 1 // ipv4
#define T_NS 2 // 域名服务器
#define T_CNAME 5 // 规范名称
#define T_SOA 6 // 开始授权
#define T_PTR 12 // ip转域名
#define T_MX 15 // 邮件服务器
#define C_IN 1;
#define DNS_PORT 53
#define BUF_SIZE 1024
typedef struct DNSCache
{
char *query;
unsigned int ttl;
unsigned short type;//in
unsigned short class_; // A, MX, CNAME
char *result;
struct DNSCache *next;
} DNSCache;
void checkMalloc(void *ptr);
char *serilizeQuery(DNSQuery *dnsQuery, int *length);
char *serilizeRr(DNSRr *dnsRr, int *length);
char *serilizeDNS(DNSBody dnsBody, size_t *packetLength);
DNSBody *deserializeDNS(char *data, size_t length);
void releaseDNS(DNSBody dnsBody);
// www.baidu.com to 03www05baidu03com
char *toInternetFormat(char *name);
// 03www05baidu03com to www.baidu.com
char *toLocalFormat(char *name);
#endif //DNS_PJ_DNS_H