-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdnscvt.cc
More file actions
101 lines (87 loc) · 2.43 KB
/
dnscvt.cc
File metadata and controls
101 lines (87 loc) · 2.43 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
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
#include "queryfile.h"
// EDNS flag constants
constexpr uint16_t EDNS_DO_BIT = 0x8000; // DNSSEC OK bit
// via https://stackoverflow.com/a/2072890/6782
inline bool ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
std::string output_file_from_input(const std::string& input)
{
std::string output = input;
if (ends_with(output, ".txt")) {
output.erase(output.length() - 4);
}
output += ".raw";
return output;
}
void usage(const char* progname) {
std::cerr << "usage: " << progname << " [-e] [-D] [-o output] <txtfile>" << std::endl;
std::cerr << " -e Add EDNS OPT RR to queries" << std::endl;
std::cerr << " -D Add EDNS OPT RR with DO (DNSSEC OK) bit" << std::endl;
std::cerr << " -o output Specify output file (default: input.raw)" << std::endl;
}
int main(int argc, char *argv[])
{
bool add_edns = false;
bool add_dnssec = false;
std::string output_file;
bool has_output = false;
int opt;
while ((opt = getopt(argc, argv, "eDo:h")) != -1) {
switch (opt) {
case 'e':
add_edns = true;
break;
case 'D':
add_dnssec = true;
break;
case 'o':
output_file = optarg;
has_output = true;
break;
case 'h':
case '?':
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
if (optind >= argc) {
usage(argv[0]);
return EXIT_FAILURE;
}
try {
QueryFile qf;
// determine output filename
std::string input(argv[optind]);
std::string output = has_output ? output_file : output_file_from_input(input);
// start the conversion
qf.read_txt(input);
// add EDNS if requested (-D overrides -e)
if (add_dnssec) {
qf.edns(4096, EDNS_DO_BIT); // 4KB buffer, DO bit set
} else if (add_edns) {
qf.edns(4096, 0); // 4KB buffer, no flags
}
qf.write_raw(output);
} catch (std::runtime_error& e) {
std::cerr << "error: " << e.what() << std::endl;
}
}