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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "resolver.h"
#include "addr.h"
#include "binding.h"
#include "../io/log.h"
static bool lookup(
Question* question,
Packet* response,
BindingType type,
SocketAddr addr
) {
INIT_LOG_BUFFER(log)
LOGONLY(print_socket_addr(&addr, log);)
TRACE("Attempting lookup on fallback dns %s", log);
Connection request;
if (!create_request(type, &addr, &request)) {
return false;
}
Packet req;
memset(&req, 0, sizeof(Packet));
req.header.id = response->header.id;
req.header.opcode = response->header.opcode;
req.header.questions = 1;
req.header.recursion_desired = true;
req.questions = malloc(sizeof(Question));
req.questions[0] = *question;
if (!request_packet(&request, &req, response)) {
free_request(&request);
free(req.questions);
ERROR("Failed to request fallback dns: %s", strerror(errno));
return false;
}
free_request(&request);
free(req.questions);
return true;
}
static bool search(Question* question, Packet* result, BindingType type) {
IpAddr addr;
char ip[4] = {1, 1, 1, 1};
create_ip_addr(ip, &addr);
uint16_t port = 53;
SocketAddr saddr;
create_socket_addr(port, addr, &saddr);
while(1) {
if (!lookup(question, result, type, saddr)) {
return false;
}
if (result->header.answers > 0 && result->header.rescode == NOERROR) {
return true;
}
if (result->header.rescode == NXDOMAIN) {
return true;
}
if (get_resolved_ns(result, question->domain, &addr)) {
continue;
}
Question new_question;
if (!get_unresoled_ns(result, question->domain, &new_question)) {
return true;
}
Packet recurse;
if (!search(&new_question, &recurse, type)) {
return false;
}
free_question(&new_question);
IpAddr random;
if (!get_random_a(&recurse, &random)) {
free_packet(&recurse);
return true;
} else {
free_packet(&recurse);
addr = random;
}
}
}
static void push_records(Record* from, uint8_t from_len, Record** to, uint8_t to_len) {
if(from_len < 1) return;
*to = realloc(*to, sizeof(Record) * (from_len + to_len));
memcpy(*to + to_len, from, from_len * sizeof(Record));
}
static void push_questions(Question* from, uint8_t from_len, Question** to, uint8_t to_len) {
if(from_len < 1) return;
*to = realloc(*to, sizeof(Question) * (from_len + to_len));
memcpy(*to + to_len, from, from_len * sizeof(Question));
}
void handle_query(Packet* request, Packet* response, BindingType type) {
memset(response, 0, sizeof(Packet));
response->header.id = request->header.id;
response->header.opcode = request->header.opcode;
response->header.recursion_desired = true;
response->header.recursion_available = true;
response->header.response = true;
if (request->header.questions < 1) {
response->header.response = FORMERR;
return;
}
for (uint16_t i = 0; i < request->header.questions; i++) {
Packet result;
memset(&result, 0, sizeof(Packet));
result.header.id = response->header.id;
if (!search(&request->questions[i], &result, type)) {
response->header.response = SERVFAIL;
break;
}
push_questions(
result.questions,
result.header.questions,
&response->questions,
response->header.questions
);
response->header.questions += result.header.questions;
push_records(
result.answers,
result.header.answers,
&response->answers,
response->header.answers
);
response->header.answers += result.header.answers;
push_records(
result.authorities,
result.header.authoritative_entries,
&response->authorities,
response->header.authoritative_entries
);
response->header.authoritative_entries += result.header.authoritative_entries;
push_records(
result.resources,
result.header.resource_entries,
&response->resources,
response->header.resource_entries
);
response->header.resource_entries += result.header.resource_entries;
free(result.questions);
free(result.answers);
free(result.authorities);
free(result.resources);
}
}
|