-
Notifications
You must be signed in to change notification settings - Fork 7
/
misc.cc
115 lines (97 loc) · 2.39 KB
/
misc.cc
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
/*
* This file is part of quantum-dns.
*
* (C) 2014-2018 by Sebastian Krahmer, sebastian [dot] krahmer [at] gmail [dot] com
*
* quantum-dns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* quantum-dns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with quantum-dns. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
namespace qdns {
using namespace std;
const uint8_t dns_max_label = 63;
/* "foo.bar" -> "\003foo\003bar\000"
* automatically splits labels larger than 63 byte into
* sub-domains
*/
int host2qname(const string &host, string &result)
{
string split_host = "";
string::size_type pos1 = 0, pos2 = 0;
for (;pos1 < host.size();) {
pos2 = host.find(".", pos1);
if (pos2 == string::npos) {
split_host += host.substr(pos1);
break;
}
if (pos2 - pos1 > dns_max_label) {
split_host += host.substr(pos1, dns_max_label);
pos1 += dns_max_label;
} else {
split_host += host.substr(pos1, pos2 - pos1);
pos1 = pos2 + 1;
}
split_host += ".";
}
try {
result.clear();
result.reserve(split_host.length() + 2);
result.resize(split_host.length() + 2);
} catch (...) {
return -1;
}
int i = 0, j = 0, k = 0, l = 0;
uint8_t how_much = 0;
while (i < (int)split_host.length()) {
l = i;
how_much = 0;
while (split_host[i] != '.' && i != (int)split_host.length()) {
++how_much;
++i;
}
result[j] = how_much;
++j;
i = l;
for (k = 0; k < how_much; j++, i++, k++)
result[j] = split_host[i];
++i;
}
result[j] = '\0';
return j + 1;
}
/* "\003foo\003bar\000" -> foo.bar
*/
int qname2host(const string &msg, string &result)
{
string::size_type i = 0;
uint8_t len = 0;
result = "";
string s = "";
try {
s.reserve(msg.length());
} catch (...) {
return -1;
}
while ((len = msg[i]) != 0) {
if (len > dns_max_label)
return -1;
if (len + i + 1 > msg.size())
return -1;
s += msg.substr(i + 1, len);
s += ".";
i += len + 1;
}
result = s;
return i + 1;
}
} // namespace