Skip to content

Commit

Permalink
Merge pull request #11 from hadriansecurity/massdns_json_output
Browse files Browse the repository at this point in the history
Change output to massdns-like output
  • Loading branch information
kalmjasper authored Aug 10, 2024
2 parents 1560322 + b072c02 commit 961c452
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 239 deletions.
6 changes: 3 additions & 3 deletions include/dns_json_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ template <>
struct glz::meta<ResourceRecord> {
using T = ResourceRecord;
static constexpr auto value =
glz::object("n", &T::name, "t", &T::q_type, "ttl", &T::ttl, "d", &T::r_data);
glz::object("name", &T::name, "type", &T::q_type, "ttl", &T::ttl, "data", &T::r_data);
};

/**
Expand Down Expand Up @@ -88,6 +88,6 @@ struct glz::meta<in6_addr> {
template <>
struct glz::meta<DNSPacket> {
using T = DNSPacket;
static constexpr auto value = glz::object("q", &T::question, "t", &T::q_type, "r",
&T::r_code, "ans", &T::ans, "auth", &T::auth, "add", &T::add);
static constexpr auto value = glz::object("name", &T::question, "type", &T::q_type, "status",
&T::r_code, "answers", &T::ans, "authorities", &T::auth, "additionals", &T::add);
};
20 changes: 10 additions & 10 deletions src/dns_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,51 +167,51 @@ tl::expected<ResourceRecord, DNSParseError> ParseResourceRecord(std::span<const
parsed_record.ttl = rte_be_to_cpu_32(response->ttl);
auto begin = reader;
switch (parsed_record.q_type) {
case DnsQType::T_A: {
case DnsQType::A: {
ARdata r_data;
r_data.ipv4_addr = *UNWRAP_OR_RETURN(AdvanceReader<InAddr>(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_AAAA: {
case DnsQType::AAAA: {
AAAARdata r_data;
r_data.ipv6_addr = *UNWRAP_OR_RETURN(AdvanceReader<In6Addr>(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_NS: {
case DnsQType::NS: {
NSRdata r_data;
r_data.nameserver = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_MX: {
case DnsQType::MX: {
MXRdata r_data;
r_data.preference = rte_be_to_cpu_16(
*UNWRAP_OR_RETURN(AdvanceReader<uint16_t>(bytes, reader)));
r_data.mailserver = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_CNAME: {
case DnsQType::CNAME: {
CNAMERdata r_data;
r_data.cname = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_DNAME: {
case DnsQType::DNAME: {
DNAMERdata r_data;
r_data.dname = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_PTR: {
case DnsQType::PTR: {
PTRRdata r_data;
r_data.ptr = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_TXT: {
case DnsQType::TXT: {
TXTRdata r_data;
r_data.txt.len = 0;

Expand Down Expand Up @@ -247,7 +247,7 @@ tl::expected<ResourceRecord, DNSParseError> ParseResourceRecord(std::span<const
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_SOA: {
case DnsQType::SOA: {
SOARdata r_data;
r_data.m_name = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
r_data.r_name = UNWRAP_OR_RETURN(ReadFromDNSNameFormat(bytes, reader));
Expand All @@ -268,7 +268,7 @@ tl::expected<ResourceRecord, DNSParseError> ParseResourceRecord(std::span<const
parsed_record.r_data = r_data;
break;
}
case DnsQType::T_OPT: {
case DnsQType::OPT: {
if (reader + rte_be_to_cpu_16(response->data_len) > bytes.end())
return tl::unexpected(DNSParseError::OutOfBounds);

Expand Down
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ std::optional<UserConfig> InitConfigFromArgs(int argc, char** argv) {

auto& rcodes_filters = parser["rcodes"]
.description("Only output results with these DNS return codes\n"
" Example: --rcodes R_NOERROR,R_SERVFAIL")
" Example: --rcodes NOERROR,SERVFAIL")
.type(po::string);

auto& prefix = parser["prefix"]
Expand Down Expand Up @@ -219,10 +219,10 @@ std::optional<UserConfig> InitConfigFromArgs(int argc, char** argv) {

auto& q_type = parser["q-type"]
.abbreviation('q')
.description("Question type\n (T_A, T_NS, T_CNAME, T_DNAME, T_SOA, "
"T_PTR, T_MX, T_TXT, T_AAAA, T_OPT)")
.description("Question type\n (A, NS, CNAME, DNAME, SOA, "
"PTR, MX, TXT, AAAA, OPT)")
.type(po::string)
.fallback("T_A");
.fallback("A");

if (!parser(argc, argv))
return std::nullopt;
Expand Down Expand Up @@ -361,8 +361,8 @@ std::optional<UserConfig> InitConfigFromArgs(int argc, char** argv) {
config.q_type = ({
std::optional res = GetQTypeFromString(q_type.get().string);
if (!res) {
fmt::print("{} invalid question type {}, choose from T_A, T_NS, T_CNAME, "
"T_DNAME, T_SOA, T_PTR, T_MX, T_TXT, T_AAAA, T_OPT\n",
fmt::print("{} invalid question type {}, choose from A, NS, CNAME, "
"DNAME, SOA, PTR, MX, TXT, AAAA, OPT\n",
error_str, q_type.get().string);
return std::nullopt;
}
Expand Down
6 changes: 3 additions & 3 deletions src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ void AddRequestsToReadyForSend(WorkerContext &ctx,
void HandleParsedPacket(WorkerContext &ctx, WorkerParams &param, const DNSPacket &pkt,
RTEMbuf<DefaultPacket> &raw_pkt, std::shared_ptr<spdlog::logger> out_logger) {
switch (pkt.r_code) {
case DnsRCode::R_NOERROR:
case DnsRCode::NOERROR:
param.counters[ctx.worker_id].noerror++;
break;
case DnsRCode::R_SERVFAIL:
case DnsRCode::SERVFAIL:
param.counters[ctx.worker_id].servfail++;
break;
case DnsRCode::R_NXDOMAIN:
case DnsRCode::NXDOMAIN:
param.counters[ctx.worker_id].nxdomain++;
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions test/dns_packet_constructor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ size_t ConstructExamplePacketIpv4(RTEMbuf<DefaultPacket>& pkt) {
strcpy(qname, DOMAIN_NAME_DNS);
// Include NULL terminator, must be present in DNS query
QuestionInfo* qinfo = (QuestionInfo*) (qname + strlen(DOMAIN_NAME_DNS) + 1);
qinfo->qclass = rte_cpu_to_be_16((unsigned short) DnsQType::T_A);
qinfo->qclass = rte_cpu_to_be_16((unsigned short) DnsQType::A);
qinfo->qtype = rte_cpu_to_be_16(1);

const size_t total_dns_len =
Expand Down Expand Up @@ -131,7 +131,7 @@ size_t ConstructExamplePacketIpv6(RTEMbuf<DefaultPacket>& pkt) {
strcpy(qname, DOMAIN_NAME_DNS);
// Include NULL terminator, must be present in DNS query
QuestionInfo* qinfo = (QuestionInfo*) (qname + strlen(DOMAIN_NAME_DNS) + 1);
qinfo->qclass = rte_cpu_to_be_16((unsigned short) DnsQType::T_A);
qinfo->qclass = rte_cpu_to_be_16((unsigned short) DnsQType::A);
qinfo->qtype = rte_cpu_to_be_16(1);

const size_t total_dns_len =
Expand Down Expand Up @@ -179,7 +179,7 @@ TEST(DnsPacketConstructorTest, Ipv4Packet) {

size_t test_pkt_size = DNSPacketConstructor::ConstructIpv4DNSPacket(test_pkt->get(),
SRC_MAC, DST_MAC, SRC_IP_IPV4, DST_IP_IPV4, SRC_PORT, DNS_ID, DOMAIN_NAME_REG,
strlen(DOMAIN_NAME_REG), DnsQType::T_A);
strlen(DOMAIN_NAME_REG), DnsQType::A);

size_t ref_pkt_size = ConstructExamplePacketIpv4(ref_pkt->get());

Expand Down Expand Up @@ -214,7 +214,7 @@ TEST(DnsPacketConstructorTest, Ipv6Packet) {

size_t test_pkt_size = DNSPacketConstructor::ConstructIpv6DNSPacket(test_pkt->get(),
SRC_MAC, DST_MAC, {{SRC_IP_IPV6}}, {{DST_IP_IPV6}}, SRC_PORT, DNS_ID, DOMAIN_NAME_REG,
strlen(DOMAIN_NAME_REG), DnsQType::T_A);
strlen(DOMAIN_NAME_REG), DnsQType::A);

size_t ref_pkt_size = ConstructExamplePacketIpv6(ref_pkt->get());

Expand Down
Loading

0 comments on commit 961c452

Please sign in to comment.