• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 19741624072

27 Nov 2025 03:45PM UTC coverage: 73.086% (+0.02%) from 73.065%
19741624072

Pull #16570

github

web-flow
Merge 08a2cdb1d into f94a3f63f
Pull Request #16570: rec: rewrite all unwrap calls in web.rs

38523 of 63408 branches covered (60.75%)

Branch coverage included in aggregate %.

128044 of 164496 relevant lines covered (77.84%)

6531485.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.22
/pdns/protozero.cc
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22

23
#include "protozero.hh"
24

25
#ifndef DISABLE_PROTOBUF
26
#include "dnsparser.hh"
27

28
void pdns::ProtoZero::Message::encodeComboAddress(const protozero::pbf_tag_type type, const ComboAddress& address)
29
{
443✔
30
  if (address.sin4.sin_family == AF_INET) {
443✔
31
    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
441✔
32
  }
441✔
33
  else if (address.sin4.sin_family == AF_INET6) {
2!
34
    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
2✔
35
  }
2✔
36
}
443✔
37

38
void pdns::ProtoZero::Message::encodeNetmask(const protozero::pbf_tag_type type, const Netmask& subnet, uint8_t mask)
39
{
147✔
40
  if (!subnet.empty()) {
147✔
41
    ComboAddress address(subnet.getNetwork());
6✔
42
    address.truncate(mask);
6✔
43
    if (address.sin4.sin_family == AF_INET) {
6!
44
      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
6✔
45
    }
6✔
46
    else if (address.sin4.sin_family == AF_INET6) {
×
47
      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
×
48
    }
×
49
  }
6✔
50
}
147✔
51

52
void pdns::ProtoZero::Message::encodeDNSName(protozero::pbf_writer& pbf, std::string& buffer, const protozero::pbf_tag_type type, const DNSName& name)
53
{
304✔
54
  // this will append the tag, mark the current position then reserve enough place to write the size
55
  protozero::pbf_writer pbf_name{pbf, type};
304✔
56
  // we append the name to the buffer
57
  name.toString(buffer);
304✔
58
  // leaving the block will cause the sub writer to compute how much was written based on the new size and update the size accordingly
59
}
304✔
60

61
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)
62
{
127✔
63
  setMessageIdentity(uniqueId);
127✔
64
  setSocketFamily(requestor.sin4.sin_family);
127✔
65
  setSocketProtocol(proto);
127✔
66
  setFrom(requestor);
127✔
67
  setTo(local);
127✔
68
  setInBytes(len);
127✔
69
  setTime();
127✔
70
  setId(qid);
127✔
71
  setQuestion(qname, qtype, qclass);
127✔
72
  setFromPort(requestor.getPort());
127✔
73
  setToPort(local.getPort());
127✔
74
}
127✔
75

76
void pdns::ProtoZero::Message::setResponse(const DNSName& qname, uint16_t qtype, uint16_t qclass)
77
{
40✔
78
  setType(pdns::ProtoZero::Message::MessageType::DNSResponseType);
40✔
79
  setQuestion(qname, qtype, qclass);
40✔
80
}
40✔
81

82
void pdns::ProtoZero::Message::addRRsFromPacket(const char* packet, const size_t len, bool includeCNAME)
83
{
42✔
84
  if (len < sizeof(struct dnsheader)) {
42!
85
    return;
×
86
  }
×
87

88
  const dnsheader_aligned header(packet);
42✔
89

90
  if (ntohs(header->ancount) == 0) {
42✔
91
    return;
2✔
92
  }
2✔
93

94
  if (ntohs(header->qdcount) == 0) {
40!
95
    return;
×
96
  }
×
97

98
  PacketReader packetReader(std::string_view(packet, len));
40✔
99

100
  size_t idx = 0;
40✔
101
  DNSName rrname;
40✔
102
  uint16_t qdcount = ntohs(header->qdcount);
40✔
103
  uint16_t ancount = ntohs(header->ancount);
40✔
104
  uint16_t rrtype{};
40✔
105
  uint16_t rrclass{};
40✔
106
  string blob;
40✔
107
  dnsrecordheader recordHeader{};
40✔
108

109
  rrname = packetReader.getName();
40✔
110
  rrtype = packetReader.get16BitInt();
40✔
111
  rrclass = packetReader.get16BitInt();
40✔
112
  (void)rrtype;
40✔
113
  (void)rrclass;
40✔
114

115
  /* consume remaining qd if any */
116
  if (qdcount > 1) {
40!
117
    for (idx = 1; idx < qdcount; idx++) {
×
118
      rrname = packetReader.getName();
×
119
      rrtype = packetReader.get16BitInt();
×
120
      rrclass = packetReader.get16BitInt();
×
121
      (void)rrtype;
×
122
      (void)rrclass;
×
123
    }
×
124
  }
×
125

126
  /* parse AN */
127
  for (idx = 0; idx < ancount; idx++) {
104✔
128
    rrname = packetReader.getName();
64✔
129
    packetReader.getDnsrecordheader(recordHeader);
64✔
130

131
    if (recordHeader.d_type == QType::A || recordHeader.d_type == QType::AAAA) {
64✔
132
      packetReader.xfrBlob(blob);
37✔
133

134
      addRR(rrname, recordHeader.d_type, recordHeader.d_class, recordHeader.d_ttl, blob);
37✔
135
    }
37✔
136
    else if (recordHeader.d_type == QType::CNAME && includeCNAME) {
27✔
137
      protozero::pbf_writer pbf_rr{d_response, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::ResponseField::rrs)};
6✔
138

139
      encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::name), rrname);
6✔
140
      pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), recordHeader.d_type);
6✔
141
      pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), recordHeader.d_class);
6✔
142
      pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), recordHeader.d_ttl);
6✔
143
      DNSName target;
6✔
144
      packetReader.xfrName(target, true);
6✔
145
      encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), target);
6✔
146
    }
6✔
147
    else {
21✔
148
      packetReader.xfrBlob(blob);
21✔
149
    }
21✔
150
  }
64✔
151
}
40✔
152

153
void pdns::ProtoZero::Message::addRR(const DNSName& name, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& blob)
154
{
39✔
155
  protozero::pbf_writer pbf_rr{d_response, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::ResponseField::rrs)};
39✔
156
  encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::name), name);
39✔
157
  pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), uType);
39✔
158
  pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), uClass);
39✔
159
  pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), uTTL);
39✔
160
  pbf_rr.add_string(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), blob);
39✔
161
}
39✔
162

163
#endif /* DISABLE_PROTOBUF */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc