blob: 2b2040f075cd3ef6e8506360d3cfc17182343d3f (
plain)
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
|
#include "dnsresolv.h"
#include "error.h"
/**
@file dns_packet.c
@author djb
@source ucspi-tcp
@brief DNS low level packet routine
@brief DNS should have used LZ77 instead of its own sophomoric compression algorithm.
*/
unsigned int dns_packet_copy(
const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
{
while (outlen) {
if (pos >= len) {
errno = EPROTO;
return 0;
}
*out = buf[pos++];
++out;
--outlen;
}
return pos;
}
unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos)
{
unsigned char ch;
for (;;) {
if (pos >= len) break;
ch = buf[pos++];
if (ch >= 192) return pos + 1;
if (ch >= 64) break;
if (!ch) return pos;
pos += ch;
}
errno = EPROTO;
return 0;
}
unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d)
{
unsigned int loop = 0;
unsigned int state = 0;
unsigned int firstcompress = 0;
unsigned int where;
unsigned char ch;
char name[255];
unsigned int namelen = 0;
for (;;) {
if (pos >= len) goto PROTO;
ch = buf[pos++];
if (++loop >= 1000) goto PROTO;
if (state) {
if (namelen + 1 > sizeof(name)) goto PROTO;
name[namelen++] = ch;
--state;
} else {
while (ch >= 192) {
where = ch;
where -= 192;
where <<= 8;
if (pos >= len) goto PROTO;
ch = buf[pos++];
if (!firstcompress) firstcompress = pos;
pos = where + ch;
if (pos >= len) goto PROTO;
ch = buf[pos++];
if (++loop >= 1000) goto PROTO;
}
if (ch >= 64) goto PROTO;
if (namelen + 1 > sizeof(name)) goto PROTO;
name[namelen++] = ch;
if (!ch) break;
state = ch;
}
}
if (!dns_domain_copy(d, name)) return 0;
if (firstcompress) return firstcompress;
return pos;
PROTO:
errno = EPROTO;
return 0;
}
|