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 #include "dnsparser.hh"
25
encodeComboAddress(const protozero::pbf_tag_type type,const ComboAddress & ca)26 void pdns::ProtoZero::Message::encodeComboAddress(const protozero::pbf_tag_type type, const ComboAddress& ca)
27 {
28 if (ca.sin4.sin_family == AF_INET) {
29 d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin4.sin_addr.s_addr), sizeof(ca.sin4.sin_addr.s_addr));
30 }
31 else if (ca.sin4.sin_family == AF_INET6) {
32 d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin6.sin6_addr.s6_addr), sizeof(ca.sin6.sin6_addr.s6_addr));
33 }
34 }
35
encodeNetmask(const protozero::pbf_tag_type type,const Netmask & subnet,uint8_t mask)36 void pdns::ProtoZero::Message::encodeNetmask(const protozero::pbf_tag_type type, const Netmask& subnet, uint8_t mask)
37 {
38 if (!subnet.empty()) {
39 ComboAddress ca(subnet.getNetwork());
40 ca.truncate(mask);
41 if (ca.sin4.sin_family == AF_INET) {
42 d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin4.sin_addr.s_addr), sizeof(ca.sin4.sin_addr.s_addr));
43 }
44 else if (ca.sin4.sin_family == AF_INET6) {
45 d_message.add_bytes(type, reinterpret_cast<const char*>(&ca.sin6.sin6_addr.s6_addr), sizeof(ca.sin6.sin6_addr.s6_addr));
46 }
47 }
48 }
49
encodeDNSName(protozero::pbf_writer & pbf,std::string & buffer,const protozero::pbf_tag_type type,const DNSName & name)50 void pdns::ProtoZero::Message::encodeDNSName(protozero::pbf_writer& pbf, std::string& buffer, const protozero::pbf_tag_type type, const DNSName& name)
51 {
52 // this will append the tag, mark the current position then reserve enough place to write the size
53 protozero::pbf_writer pbf_name{pbf, type};
54 // we append the name to the buffer
55 name.toString(buffer);
56 // leaving the block will cause the sub writer to compute how much was written based on the new size and update the size accordingly
57 }
58
setRequest(const boost::uuids::uuid & uniqueId,const ComboAddress & requestor,const ComboAddress & local,const DNSName & qname,uint16_t qtype,uint16_t qclass,uint16_t id,bool tcp,size_t len)59 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, bool tcp, size_t len)
60 {
61 setMessageIdentity(uniqueId);
62 setSocketFamily(requestor.sin4.sin_family);
63 setSocketProtocol(tcp);
64 setFrom(requestor);
65 setTo(local);
66 setInBytes(len);
67 setTime();
68 setId(id);
69 setQuestion(qname, qtype, qclass);
70 setFromPort(requestor.getPort());
71 setToPort(local.getPort());
72 }
73
setResponse(const DNSName & qname,uint16_t qtype,uint16_t qclass)74 void pdns::ProtoZero::Message::setResponse(const DNSName& qname, uint16_t qtype, uint16_t qclass)
75 {
76 setType(pdns::ProtoZero::Message::MessageType::DNSResponseType);
77 setQuestion(qname, qtype, qclass);
78 }
79
addRRsFromPacket(const char * packet,const size_t len,bool includeCNAME)80 void pdns::ProtoZero::Message::addRRsFromPacket(const char* packet, const size_t len, bool includeCNAME)
81 {
82 if (len < sizeof(struct dnsheader)) {
83 return;
84 }
85
86 const struct dnsheader* dh = reinterpret_cast<const struct dnsheader*>(packet);
87
88 if (ntohs(dh->ancount) == 0) {
89 return;
90 }
91
92 if (ntohs(dh->qdcount) == 0) {
93 return;
94 }
95
96 PacketReader pr(pdns_string_view(packet, len));
97
98 size_t idx = 0;
99 DNSName rrname;
100 uint16_t qdcount = ntohs(dh->qdcount);
101 uint16_t ancount = ntohs(dh->ancount);
102 uint16_t rrtype;
103 uint16_t rrclass;
104 string blob;
105 struct dnsrecordheader ah;
106
107 rrname = pr.getName();
108 rrtype = pr.get16BitInt();
109 rrclass = pr.get16BitInt();
110 (void) rrtype;
111 (void) rrclass;
112
113 /* consume remaining qd if any */
114 if (qdcount > 1) {
115 for(idx = 1; idx < qdcount; idx++) {
116 rrname = pr.getName();
117 rrtype = pr.get16BitInt();
118 rrclass = pr.get16BitInt();
119 (void) rrtype;
120 (void) rrclass;
121 }
122 }
123
124 /* parse AN */
125 for (idx = 0; idx < ancount; idx++) {
126 rrname = pr.getName();
127 pr.getDnsrecordheader(ah);
128
129 if (ah.d_type == QType::A || ah.d_type == QType::AAAA) {
130 pr.xfrBlob(blob);
131
132 addRR(rrname, ah.d_type, ah.d_class, ah.d_ttl, blob);
133
134 } else if (ah.d_type == QType::CNAME && includeCNAME) {
135 protozero::pbf_writer pbf_rr{d_response, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::ResponseField::rrs)};
136
137 encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::name), rrname);
138 pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), ah.d_type);
139 pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), ah.d_class);
140 pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), ah.d_ttl);
141 DNSName target;
142 pr.xfrName(target, true);
143 encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), target);
144 }
145 else {
146 pr.xfrBlob(blob);
147 }
148 }
149 }
150
addRR(const DNSName & name,uint16_t uType,uint16_t uClass,uint32_t uTTL,const std::string & blob)151 void pdns::ProtoZero::Message::addRR(const DNSName& name, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& blob)
152 {
153 protozero::pbf_writer pbf_rr{d_response, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::ResponseField::rrs)};
154 encodeDNSName(pbf_rr, d_buffer, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::name), name);
155 pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), uType);
156 pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), uClass);
157 pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), uTTL);
158 pbf_rr.add_string(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), blob);
159 }
160