#ifndef DISABLE_PROTOBUF
#include "dnsparser.hh"
-void pdns::ProtoZero::Message::encodeComboAddress(const protozero::pbf_tag_type type, const ComboAddress& ca)
+void pdns::ProtoZero::Message::encodeComboAddress(const protozero::pbf_tag_type type, const ComboAddress& address)
{
- if (ca.sin4.sin_family == AF_INET) {
- d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin4.sin_addr.s_addr), sizeof(ca.sin4.sin_addr.s_addr));
+ if (address.sin4.sin_family == AF_INET) {
+ d_message.add_bytes(type, reinterpret_cast<const char*>(&address.sin4.sin_addr.s_addr), sizeof(address.sin4.sin_addr.s_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
- else if (ca.sin4.sin_family == AF_INET6) {
- d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin6.sin6_addr.s6_addr), sizeof(ca.sin6.sin6_addr.s6_addr));
+ else if (address.sin4.sin_family == AF_INET6) {
+ d_message.add_bytes(type, reinterpret_cast<const char*>(&address.sin6.sin6_addr.s6_addr), sizeof(address.sin6.sin6_addr.s6_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
}
void pdns::ProtoZero::Message::encodeNetmask(const protozero::pbf_tag_type type, const Netmask& subnet, uint8_t mask)
{
if (!subnet.empty()) {
- ComboAddress ca(subnet.getNetwork());
- ca.truncate(mask);
- if (ca.sin4.sin_family == AF_INET) {
- d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin4.sin_addr.s_addr), sizeof(ca.sin4.sin_addr.s_addr));
+ ComboAddress address(subnet.getNetwork());
+ address.truncate(mask);
+ if (address.sin4.sin_family == AF_INET) {
+ d_message.add_bytes(type, reinterpret_cast<const char*>(&address.sin4.sin_addr.s_addr), sizeof(address.sin4.sin_addr.s_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
- else if (ca.sin4.sin_family == AF_INET6) {
- d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin6.sin6_addr.s6_addr), sizeof(ca.sin6.sin6_addr.s6_addr));
+ else if (address.sin4.sin_family == AF_INET6) {
+ d_message.add_bytes(type, reinterpret_cast<const char*>(&address.sin6.sin6_addr.s6_addr), sizeof(address.sin6.sin6_addr.s6_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
}
}
// leaving the block will cause the sub writer to compute how much was written based on the new size and update the size accordingly
}
-void pdns::ProtoZero::Message::setRequest(const boost::uuids::uuid& uniqueId, const ComboAddress& requestor, const ComboAddress& local, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t id, pdns::ProtoZero::Message::TransportProtocol proto, size_t len)
+void pdns::ProtoZero::Message::setRequest(const boost::uuids::uuid& uniqueId, const ComboAddress& requestor, const ComboAddress& local, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t qid, pdns::ProtoZero::Message::TransportProtocol proto, size_t len)
{
setMessageIdentity(uniqueId);
setSocketFamily(requestor.sin4.sin_family);
setTo(local);
setInBytes(len);
setTime();
- setId(id);
+ setId(qid);
setQuestion(qname, qtype, qclass);
setFromPort(requestor.getPort());
setToPort(local.getPort());
return;
}
- const dnsheader_aligned dh(packet);
+ const dnsheader_aligned header(packet);
- if (ntohs(dh->ancount) == 0) {
+ if (ntohs(header->ancount) == 0) {
return;
}
- if (ntohs(dh->qdcount) == 0) {
+ if (ntohs(header->qdcount) == 0) {
return;
}
- PacketReader pr(std::string_view(packet, len));
+ PacketReader packetReader(std::string_view(packet, len));
size_t idx = 0;
DNSName rrname;
- uint16_t qdcount = ntohs(dh->qdcount);
- uint16_t ancount = ntohs(dh->ancount);
- uint16_t rrtype;
- uint16_t rrclass;
+ uint16_t qdcount = ntohs(header->qdcount);
+ uint16_t ancount = ntohs(header->ancount);
+ uint16_t rrtype{};
+ uint16_t rrclass{};
string blob;
- struct dnsrecordheader ah;
+ dnsrecordheader recordHeader{};
- rrname = pr.getName();
- rrtype = pr.get16BitInt();
- rrclass = pr.get16BitInt();
+ rrname = packetReader.getName();
+ rrtype = packetReader.get16BitInt();
+ rrclass = packetReader.get16BitInt();
(void)rrtype;
(void)rrclass;
/* consume remaining qd if any */
if (qdcount > 1) {
for (idx = 1; idx < qdcount; idx++) {
- rrname = pr.getName();
- rrtype = pr.get16BitInt();
- rrclass = pr.get16BitInt();
+ rrname = packetReader.getName();
+ rrtype = packetReader.get16BitInt();
+ rrclass = packetReader.get16BitInt();
(void)rrtype;
(void)rrclass;
}
/* parse AN */
for (idx = 0; idx < ancount; idx++) {
- rrname = pr.getName();
- pr.getDnsrecordheader(ah);
+ rrname = packetReader.getName();
+ packetReader.getDnsrecordheader(recordHeader);
- if (ah.d_type == QType::A || ah.d_type == QType::AAAA) {
- pr.xfrBlob(blob);
+ if (recordHeader.d_type == QType::A || recordHeader.d_type == QType::AAAA) {
+ packetReader.xfrBlob(blob);
- addRR(rrname, ah.d_type, ah.d_class, ah.d_ttl, blob);
+ addRR(rrname, recordHeader.d_type, recordHeader.d_class, recordHeader.d_ttl, blob);
}
- else if (ah.d_type == QType::CNAME && includeCNAME) {
+ else if (recordHeader.d_type == QType::CNAME && includeCNAME) {
protozero::pbf_writer pbf_rr{d_response, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::ResponseField::rrs)};
encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::name), rrname);
- pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), ah.d_type);
- pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), ah.d_class);
- pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), ah.d_ttl);
+ pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), recordHeader.d_type);
+ pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), recordHeader.d_class);
+ pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), recordHeader.d_ttl);
DNSName target;
- pr.xfrName(target, true);
+ packetReader.xfrName(target, true);
encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), target);
}
else {
- pr.xfrBlob(blob);
+ packetReader.xfrBlob(blob);
}
}
}
d_buffer(buffer), d_message{d_buffer}
{
}
-
+ ~Message() = default;
Message(const Message&) = delete;
Message(Message&&) = delete;
Message& operator=(const Message&) = delete;
Message& operator=(Message&&) = delete;
- void setRequest(const boost::uuids::uuid& uniqueId, const ComboAddress& requestor, const ComboAddress& local, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t id, TransportProtocol proto, size_t len);
+ void setRequest(const boost::uuids::uuid& uniqueId, const ComboAddress& requestor, const ComboAddress& local, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t qid, TransportProtocol proto, size_t len);
void setResponse(const DNSName& qname, uint16_t qtype, uint16_t qclass);
void setType(MessageType mtype)
void setMessageIdentity(const boost::uuids::uuid& uniqueId)
{
- add_bytes(d_message, Field::messageId, reinterpret_cast<const char*>(uniqueId.begin()), uniqueId.size());
+ add_bytes(d_message, Field::messageId, reinterpret_cast<const char*>(uniqueId.begin()), uniqueId.size()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
void setServerIdentity(const std::string& serverIdentity)
add_enum(d_message, Field::socketProtocol, static_cast<int32_t>(proto));
}
- void setFrom(const ComboAddress& ca)
+ void setFrom(const ComboAddress& address)
{
- encodeComboAddress(static_cast<protozero::pbf_tag_type>(Field::from), ca);
+ encodeComboAddress(static_cast<protozero::pbf_tag_type>(Field::from), address);
}
- void setTo(const ComboAddress& ca)
+ void setTo(const ComboAddress& address)
{
- encodeComboAddress(static_cast<protozero::pbf_tag_type>(Field::to), ca);
+ encodeComboAddress(static_cast<protozero::pbf_tag_type>(Field::to), address);
}
void setInBytes(uint64_t len)
void setTime()
{
- struct timespec ts;
- gettime(&ts, true);
+ timespec timesp{};
+ gettime(×p, true);
- setTime(ts.tv_sec, ts.tv_nsec / 1000);
+ setTime(timesp.tv_sec, timesp.tv_nsec / 1000);
}
void setTime(time_t sec, uint32_t usec)
add_uint32(d_message, Field::timeUsec, usec);
}
- void setId(uint16_t id)
+ void setId(uint16_t qid)
{
- add_uint32(d_message, Field::id, ntohs(id));
+ add_uint32(d_message, Field::id, ntohs(qid));
}
void setQuestion(const DNSName& qname, uint16_t qtype, uint16_t qclass)
protozero::pbf_writer pbf_meta{d_message, static_cast<protozero::pbf_tag_type>(Field::meta)};
pbf_meta.add_string(static_cast<protozero::pbf_tag_type>(MetaField::key), key);
protozero::pbf_writer pbf_meta_value{pbf_meta, static_cast<protozero::pbf_tag_type>(MetaField::value)};
- for (const auto& s : stringVal) {
- pbf_meta_value.add_string(static_cast<protozero::pbf_tag_type>(MetaValueField::stringVal), s);
+ for (const auto& str : stringVal) {
+ pbf_meta_value.add_string(static_cast<protozero::pbf_tag_type>(MetaValueField::stringVal), str);
}
- for (const auto& i : intVal) {
- pbf_meta_value.add_uint64(static_cast<protozero::pbf_tag_type>(MetaValueField::intVal), i);
+ for (const auto& val : intVal) {
+ pbf_meta_value.add_uint64(static_cast<protozero::pbf_tag_type>(MetaValueField::intVal), val);
}
}
- void setEDNSSubnet(const Netmask& nm, uint8_t mask)
+ void setEDNSSubnet(const Netmask& netmask, uint8_t mask)
{
- encodeNetmask(static_cast<protozero::pbf_tag_type>(Field::originalRequestorSubnet), nm, mask);
+ encodeNetmask(static_cast<protozero::pbf_tag_type>(Field::originalRequestorSubnet), netmask, mask);
}
void setRequestorId(const std::string& req)
void setInitialRequestID(const boost::uuids::uuid& uniqueId)
{
- add_bytes(d_message, Field::initialRequestId, reinterpret_cast<const char*>(uniqueId.begin()), uniqueId.size());
+ add_bytes(d_message, Field::initialRequestId, reinterpret_cast<const char*>(uniqueId.begin()), uniqueId.size()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
- void setDeviceId(const std::string& id)
+ void setDeviceId(const std::string& deviceId)
{
- if (!id.empty()) {
- add_string(d_message, Field::deviceId, id);
+ if (!deviceId.empty()) {
+ add_string(d_message, Field::deviceId, deviceId);
}
}
d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::queryTimeUsec), usec);
}
- void addRRsFromPacket(const char* packet, const size_t len, bool includeCNAME = false);
+ void addRRsFromPacket(const char* packet, size_t len, bool includeCNAME = false);
void addRR(const DNSName& name, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& blob);
protected:
- void encodeComboAddress(protozero::pbf_tag_type type, const ComboAddress& ca);
+ void encodeComboAddress(protozero::pbf_tag_type type, const ComboAddress& address);
void encodeNetmask(protozero::pbf_tag_type type, const Netmask& subnet, uint8_t mask);
- void encodeDNSName(protozero::pbf_writer& pbf, std::string& buffer, protozero::pbf_tag_type type, const DNSName& name);
+ static void encodeDNSName(protozero::pbf_writer& pbf, std::string& buffer, protozero::pbf_tag_type type, const DNSName& name);
static void add_enum(protozero::pbf_writer& writer, Field type, int32_t value)
{
writer.add_string(static_cast<protozero::pbf_tag_type>(type), str);
}
+ // NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes)
std::string& d_buffer;
protozero::pbf_writer d_message;
protozero::pbf_writer d_response;
+ // NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes)
};
};
};