-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessor.c
177 lines (154 loc) · 5.21 KB
/
processor.c
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
167
168
169
170
171
172
173
174
175
176
177
#include "config.h"
#include "globals.h"
#include "processor.h"
#include "dns_header.h"
#include "lookupdb.h"
static void qname2str(const char * qname, char * str);
static void prepare_response_found(data_queue_element * dqe, lookupdb_addr * ldb_addr);
static void prepare_response_not_found(data_queue_element * dqe);
void * processor_thread_fn(void * args) {
processor_thread_fn_arg_t * prc_arg = (processor_thread_fn_arg_t *) args;
dns_header h;
data_queue_element dqe;
dns_question dnsq;
char name[TESTDNSD_MAX_PACKET_SIZE];
lookupdb_addr * ldb_addr;
int found;
XXLOG("PRC[%d]: Started", prc_arg->prc_id);
while(1) {
if(data_queue_get_data(&INQ, &dqe)) {
/* Error getting data from input queue. Ignore */
#if TESTDNSD_DEBUG == 1
XXLOG_DEBUG("PRC[%d]: Error getting data from input queue. Ignoring", prc_arg->prc_id);
#endif /* TESTDNSD_DEBUG == 1 */
continue;
}
found = 0;
if(dqe.data_len >= sizeof(dns_header)) {
/* Get the header data (big/little endian transform) */
h.id = ntohs(((dns_header *) dqe.data)->id);
h.flags = ntohs(((dns_header *) dqe.data)->flags);
h.qdcount = ntohs(((dns_header *) dqe.data)->qdcount);
h.ancount = ntohs(((dns_header *) dqe.data)->ancount);
h.nscount = ntohs(((dns_header *) dqe.data)->nscount);
h.arcount = ntohs(((dns_header *) dqe.data)->arcount);
#if TESTDNSD_DEBUG == 1
XXLOG_DEBUG("PRC[%d]: got id=0x%.4x, flags=0x%.4x, qdcount=%u ancount=%u nscount=%u arcount=%u",
prc_arg->prc_id,
h.id,
h.flags,
h.qdcount,
h.ancount,
h.nscount,
h.arcount);
XXLOG_DEBUG("PRC[%d]: %d %d %d", prc_arg->prc_id, (h.flags & 0x8000), (h.flags & 0x7800), (h.flags & 0x0200));
#endif /* TESTDNSD_DEBUG == 1 */
if((h.flags & 0x8000) == 0 /* QR == 0 - it is a query */
&& (h.flags & 0x7800) == 0 /* OPCODE == 0 - it is standart query */
&& (h.flags & 0x0200) == 0 /* TR == 0 - not truncated */
&& h.qdcount == 1 /* Number of questions in query. Only supporting single question for now */
&& h.ancount == 0 /* No support for this */
&& h.nscount == 0 /* No support for this */
&& h.arcount == 0 /* No support for this */
)
{
/* Check the dns question contents */
if(!dns_question_load((void *)(dqe.data + sizeof(dns_header)), &dnsq)) {
qname2str(dnsq.qname, name);
#if TESTDNSD_DEBUG == 1
XXLOG_DEBUG("PRC[%d]: qname='%s' qtype=0x%x qclass=0x%x",
prc_arg->prc_id,
name,
dnsq.qtype,
dnsq.qclass);
#endif /* TESTDNSD_DEBUG == 1 */
if(dnsq.qtype == 1 /* 'A' - the only one supported for now */
&& dnsq.qclass == 1 /* 'IN' */) {
/* Search for a data in DB */
ldb_addr = lookupdb_search(&DB, name);
if(ldb_addr != NULL) {
#if TESTDNSD_DEBUG == 1
XXLOG_DEBUG("PRC[%d]: FOUND", prc_arg->prc_id);
#endif /* TESTDNSD_DEBUG == 1 */
found = 1;
}
else {
#if TESTDNSD_DEBUG == 1
XXLOG_DEBUG("PRC[%d]: NOT FOUND", prc_arg->prc_id);
#endif /* TESTDNSD_DEBUG == 1 */
}
}
}
}
}
if(found) {
prepare_response_found(&dqe, ldb_addr);
}
else {
prepare_response_not_found(&dqe);
}
#if TESTDNSD_DEBUG == 1
XXLOG_DEBUG("PRC[%d]: Put the response in output queue", prc_arg->prc_id);
#endif /* TESTDNSD_DEBUG == 1 */
if(data_queue_put_data(&OUTQ, &(dqe.addr), (void *) dqe.data, dqe.data_len)) {
XXLOG("PRC[%d]: Error putting data to output queue", prc_arg->prc_id);
}
}
return NULL;
}
/* static */
void qname2str(const char * qname, char * str) {
int i = 0;
int z = 0;
while(qname[i] != 0) {
if(z == 0) {
if(i != 0) {
str[i-1] = '.';
}
z = (unsigned char) qname[i];
}
else {
str[i-1] = qname[i];
z--;
}
i++;
}
str[i-1] = 0;
}
/* static */
void prepare_response_found(data_queue_element * dqe, lookupdb_addr * ldb_addr) {
int ancount = 0;
int off = dqe->data_len; /* Initial offset to the end of original data */
dns_header * ph = (dns_header *) dqe->data;
struct in_addr * addr;
ph->flags = htons(0x8100); /* Set response flags in header */
while(ldb_addr != NULL &&
off <= TESTDNSD_MAX_PACKET_SIZE - 12 /* ref to name - 2 bytes */ - sizeof(struct in_addr)) {
dqe->data[off++] = 0xc0; /* reference to name in question (2 bytes) */
dqe->data[off++] = 0x0c; /* ... */
dqe->data[off++] = 0x00; /* type = A (2 bytes) */
dqe->data[off++] = 0x01; /* ... */
dqe->data[off++] = 0x00; /* class = IN (2 bytes) */
dqe->data[off++] = 0x01; /* ... */
dqe->data[off++] = 0x00; /* ttl = 0 (4 bytes) */
dqe->data[off++] = 0x00; /* ... */
dqe->data[off++] = 0x00; /* ... */
dqe->data[off++] = 0x00; /* ... */
dqe->data[off++] = 0x00; /* address length (2 bytes) */
dqe->data[off++] = 0x04; /* ... */
addr = (struct in_addr *) &(dqe->data[off]);
*addr = ldb_addr->addr;
off += sizeof(struct in_addr);
ldb_addr = ldb_addr->next; /* Go to the next address */
ancount ++;
}
/* Update answers count in header */
ph->ancount = htons(ancount);
/* Update the size of the message */
dqe->data_len = off;
}
/* static */
static void prepare_response_not_found(data_queue_element * dqe) {
dns_header * ph = (dns_header *) dqe->data;
ph->flags = htons(0x8103); /* Set NOT-FOUND response flags in header */
}