forked from sogou/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDnsMessage.cc
More file actions
204 lines (165 loc) · 3.93 KB
/
Copy pathDnsMessage.cc
File metadata and controls
204 lines (165 loc) · 3.93 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright (c) 2021 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Liu Kai ([email protected])
*/
#include <errno.h>
#include <arpa/inet.h>
#include "DnsMessage.h"
#define DNS_LABELS_MAX 63
#define DNS_MESSAGE_MAX_UDP_SIZE 512
#define DNS_HEADER_SIZE sizeof (struct dns_header)
namespace protocol
{
static inline void __append_uint8(std::string& s, uint8_t tmp)
{
s.append((const char *)&tmp, sizeof (uint8_t));
}
static inline void __append_uint16(std::string& s, uint16_t tmp)
{
tmp = htons(tmp);
s.append((const char *)&tmp, sizeof (uint16_t));
}
DnsMessage::DnsMessage(DnsMessage&& msg) :
ProtocolMessage(std::move(msg))
{
this->parser = msg.parser;
msg.parser = NULL;
this->cur_size = msg.cur_size;
msg.cur_size = 0;
}
DnsMessage& DnsMessage::operator = (DnsMessage&& msg)
{
if (&msg != this)
{
*(ProtocolMessage *)this = std::move(msg);
if (this->parser)
{
dns_parser_deinit(this->parser);
delete this->parser;
}
this->parser = msg.parser;
msg.parser = NULL;
this->cur_size = msg.cur_size;
msg.cur_size = 0;
}
return *this;
}
int DnsMessage::encode_reply()
{
struct dns_header h;
const char *name;
const char *p;
size_t len;
msgbuf.clear();
msgbuf.reserve(DNS_HEADER_SIZE);
msgsize = 0;
// TODO encode other field
// pointers can only be used for occurances of a domain name where
// the format is not class specific
h = this->parser->header;
h.id = htons(h.id);
h.qdcount = htons(1);
h.ancount = htons(0);
h.nscount = htons(0);
h.arcount = htons(0);
msgbuf.append((const char *)&h, sizeof (struct dns_header));
p = parser->question.qname ? parser->question.qname : ".";
while (*p)
{
name = p;
while (*p && *p != '.')
p++;
len = p - name;
if (len > DNS_LABELS_MAX || (len == 0 && *p && *(p + 1)))
{
errno = EINVAL;
return -1;
}
if (len > 0)
{
__append_uint8(msgbuf, len);
msgbuf.append(name, len);
}
if (*p == '.')
p++;
}
len = 0;
__append_uint8(msgbuf, len);
__append_uint16(msgbuf, parser->question.qtype);
__append_uint16(msgbuf, parser->question.qclass);
if (msgbuf.size() >= (1 << 16))
{
errno = EOVERFLOW;
return -1;
}
msgsize = htons(msgbuf.size());
return 0;
}
int DnsMessage::encode(struct iovec vectors[], int)
{
struct iovec *p = vectors;
if (this->encode_reply() < 0)
return -1;
// TODO
// if this is a request, it won't exceed the 512 bytes UDP limit
// if this is a response and exceed 512 bytes, we need a TrunCation reply
if (!this->is_single_packet())
{
p->iov_base = &this->msgsize;
p->iov_len = sizeof (uint16_t);
p++;
}
p->iov_base = (void *)this->msgbuf.data();
p->iov_len = msgbuf.size();
return p - vectors + 1;
}
int DnsMessage::append(const void *buf, size_t *size)
{
int ret = dns_parser_append_message(buf, size, this->parser);
if (ret >= 0)
{
this->cur_size += *size;
if (this->cur_size > this->size_limit)
{
errno = EMSGSIZE;
ret = -1;
}
}
else if (ret == -2)
{
errno = EBADMSG;
ret = -1;
}
return ret;
}
int DnsResponse::append(const void *buf, size_t *size)
{
int ret = this->DnsMessage::append(buf, size);
const char *qname = this->parser->question.qname;
if (ret >= 1 && (this->request_id != this->get_id() ||
strcasecmp(this->request_name.c_str(), qname) != 0))
{
if (!this->is_single_packet())
{
errno = EBADMSG;
ret = -1;
}
else
{
dns_parser_deinit(this->parser);
dns_parser_init(this->parser);
ret = 0;
}
}
return ret;
}
}