-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns.c
More file actions
180 lines (167 loc) · 3.56 KB
/
dns.c
File metadata and controls
180 lines (167 loc) · 3.56 KB
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
178
179
180
#include "dns.h"
#ifdef __KERNEL__
#include <linux/in.h>
#include "nameser.h"
#else
#include <string.h> /* NULL */
#define BIND_8_COMPAT
#include <arpa/nameser.h>
#endif /* __KERNEL__ */
/**
* \param ptr is a pointer to the current item we are ... (skipping?)
* \param start is a pointer to the beginning of the buffer we are examinating
* \param len is ...
*/
uint16_t advance_name(const u_char *ptr, const u_char *start, uint16_t len)
{
int has_pointer = 0;
const u_char *original_ptr = ptr;
/// while we are within the bounds of our buffer
for (; ptr - start < len; )
{
if ((*ptr & 0xc0) == 0xc0)
{
has_pointer = 1;
ptr += sizeof(uint16_t);
/* A pointer always terminates this loop */
break;
}
else
{
u_char label_len = *ptr;
ptr += label_len + 1;
if (!label_len)
{
/* An empty label indicates the end of the name
*/
break;
}
}
}
return ptr - original_ptr;
}
int parse_rr(const u_char *ptr, const u_char *start, uint16_t len,
uint16_t *ptype, uint16_t *pclass, uint32_t *pttl,
uint16_t *prdlength, const u_char **prdata)
{
const u_char *original_ptr = ptr;
int overrun = 0;
uint16_t rdlength;
ptr += advance_name(ptr, start, len);
if (ptr - original_ptr + sizeof(uint16_t) > len)
{
overrun = 1;
goto out;
}
if (ptype)
*ptype = htons(*(uint16_t *)ptr);
ptr += sizeof(uint16_t);
if (ptr - original_ptr + sizeof(uint16_t) > len)
{
overrun = 1;
goto out;
}
if (pclass)
*pclass = htons(*(uint16_t *)ptr);
ptr += sizeof(uint16_t);
if (ptr - original_ptr + sizeof(uint32_t) > len)
{
overrun = 1;
goto out;
}
if (pttl)
*pttl = htonl(*(uint32_t *)ptr);
ptr += sizeof(uint32_t);
if (ptr - original_ptr + sizeof(uint16_t) > len)
{
overrun = 1;
goto out;
}
rdlength = htons(*(uint16_t *)ptr);
if (prdlength)
*prdlength = rdlength;
ptr += sizeof(uint16_t);
if (ptr - original_ptr + rdlength > len)
{
overrun = 1;
goto out;
}
if (prdata)
*prdata = ptr;
out:
return overrun;
}
/**
* \param ptr points to the answers we have gotten
* \param len
* \param t The RR-type we are looking for (e.g. AAAA or A)
*
*/
int find_answer_of_type(const u_char *ptr, uint16_t len, uint16_t t,
uint16_t n, uint16_t *prdlength, const u_char **prdata)
{
const u_char *original_ptr = ptr;
const HEADER *header = (const HEADER *)ptr;
uint16_t qdcount, ancount;
int i, matching_answers, overrun = 0;
if (len < sizeof(HEADER))
{
overrun = 1;
goto out;
}
qdcount = ntohs(header->qdcount);
ancount = ntohs(header->ancount);
/* Advance past questions */
ptr += sizeof(HEADER);
for (i = 0; i < qdcount; i++)
{
ptr += advance_name(ptr, original_ptr, len);
if (ptr - original_ptr + sizeof(uint16_t) > len)
{
overrun = 1;
goto out;
}
ptr += sizeof(uint16_t);
if (ptr - original_ptr + sizeof(uint16_t) > len)
{
overrun = 1;
goto out;
}
ptr += sizeof(uint16_t);
}
/* Walk through answers, looking for nth instance of type t */
for (i = 0, matching_answers = 0; i < ancount; i++)
{
uint16_t type, rdlength;
const u_char *rdata;
overrun = parse_rr(ptr, original_ptr, len, &type, NULL, NULL,
&rdlength, &rdata);
if (!overrun)
{
ptr = rdata + rdlength;
if (type == t)
{
if (matching_answers == n)
{
/* Found the desired instance */
if (prdlength)
*prdlength = rdlength;
if (prdata)
*prdata = rdata;
break;
}
else
matching_answers++;
}
}
}
if (!overrun && i >= ancount)
{
/* This isn't really an overrun, but the desired instance
* wasn't found.
*/
overrun = 1;
}
out:
return overrun;
}