-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
177 lines (143 loc) · 4.71 KB
/
Copy pathparse.cpp
File metadata and controls
177 lines (143 loc) · 4.71 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
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <typeinfo>
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include "node.h"
#include "trie.h"
#include "record.h"
#include "edge.h"
extern Trie t;
int scanFile(std::string path);
int scanZoneFile(std::string path);
std::vector<std::string> splitDomain(std::string domain);
std::vector<std::string> split(const std::string &s, char delim);
std::string concatDomain(std::vector<std::string> domain);
int scanFile(std::string path) {
std::ifstream input(path.c_str());
std::string currLine;
std::vector<std::string> record;
while (std::getline(input, currLine)) {
std::transform(currLine.begin(), currLine.end(), currLine.begin(), ::toupper);
try {
boost::tokenizer<boost::escaped_list_separator<char>> tok(currLine, boost::escaped_list_separator<char>('\\', ' ', '\"'));
record.assign(tok.begin(), tok.end());
} catch (boost::escaped_list_error e) {
continue;
}
if (record.size() < 4) {
return 1;
}
std::string n = record[0].substr(0, record[0].size() - 1);
std::string c = record[4];
uint32_t tt = std::strtoul(record[1].c_str(), NULL, 0);
if (record[3].compare("A") == 0) {
std::unique_ptr<Record> newRecord (new Record(n, c, tt, Tins::DNS::A));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
if (record[3].compare("AAAA") == 0) {
std::unique_ptr<Record> newRecord(new Record(n, c, tt, Tins::DNS::AAAA));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
if (record[3].compare("NS") == 0) {
if (c.back() == '.') {
c = c.substr(0, c.size() - 1);
}
std::unique_ptr<Record> newRecord(new Record(n, c, tt, Tins::DNS::NS));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
if (record[3].compare("CNAME") == 0) {
if (c.back() == '.') {
c = c.substr(0, c.size() - 1);
}
std::unique_ptr<Record> newRecord(new Record(n, c, tt, Tins::DNS::CNAME));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
if (record[3].compare("TXT") == 0) {
std::unique_ptr<Record> newRecord(new Record(n, c, tt, Tins::DNS::TXT));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
if (record[3].compare("MX") == 0) {
c = record[5];
if (c.back() == '.') {
c = c.substr(0, c.size() - 1);
}
uint16_t priority = atoi(record[4].c_str());
std::unique_ptr<Record> newRecord(new Record(n, c, tt, priority, Tins::DNS::MX));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
if (record[3].compare("SOA") == 0) {
c = record[4];
if (c.back() == '.') {
c = c.substr(0, c.size() - 1);
}
std::string mail = record[5];
if (mail.back() == '.') {
mail = mail.substr(0, mail.size() - 1);
}
uint32_t serial = std::strtoul(record[6].c_str(), NULL, 0);
uint32_t refresh = std::strtoul(record[7].c_str(), NULL, 0);
uint32_t retry = std::strtoul(record[8].c_str(), NULL, 0);
uint32_t expire = std::strtoul(record[9].c_str(), NULL, 0);
uint32_t minttl = std::strtoul(record[10].c_str(), NULL, 0);
std::unique_ptr<Record> newRecord(new Record(n, c, tt, mail, serial, refresh, retry, expire, minttl, Tins::DNS::SOA));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
}
return 0;
}
// special function for performance testing - load Verisign's com/net rrset
int scanZoneFile(std::string path) {
char buffer[16384];
std::ifstream input(path.c_str());
input.rdbuf()->pubsetbuf(buffer, 16384);
std::string currLine;
std::vector<std::string> record;
std::string n;
std::string c;
while (std::getline(input, currLine)) {
boost::split(record, currLine, boost::is_any_of(" "));
if (record.size() > 3) {
continue;
}
// these are A records for nameservers
if (record[1] == "A") {
n = record[0];
n.append(".NET");
c = record[2];
std::unique_ptr<Record> newRecord(new Record(n, c, 86400, Tins::DNS::A));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
// these are AAAA record for nameservers
if (record[1] == "AAAA") {
n = record[0];
n.append(".NET");
c = record[2];
std::unique_ptr<Record> newRecord(new Record(n, c, 86400, Tins::DNS::AAAA));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
// these are NS records
if (record[1] == "NS") {
n = record[0];
n.append(".NET");
c = record[2];
boost::trim_right(c);
if (c.back() != '.') {
c.append(".NET");
} else {
c.pop_back();
}
std::unique_ptr<Record> newRecord(new Record(n, c, 86400, Tins::DNS::NS));
t.addRecord(std::move(newRecord), t.getRoot(), 0);
}
record.clear();
std::vector<std::string>().swap(record);
}
std::cout << "Finished.\n";
return 0;
}