]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnswriter.hh
Merge pull request #9099 from PowerDNS/omoerbeek-patch-1
[thirdparty/pdns.git] / pdns / dnswriter.hh
CommitLineData
12471842
PL
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 */
e8c59f2d 22#pragma once
a0a276c2
BH
23#include <string>
24#include <vector>
bca6643b 25#include <map>
5a57d2ea 26#include "dns.hh"
4a51ff72 27#include "dnsname.hh"
10f4eea8 28#include "namespaces.hh"
f4352636 29#include "iputils.hh"
1bc3ebb6 30#include <arpa/inet.h>
802a93d0 31
32
950cfe0f 33/** this class can be used to write DNS packets. It knows about DNS in the sense that it makes
a0a276c2
BH
34 the packet header and record headers.
35
36 The model is:
37
38 packetheader (recordheader recordcontent)*
39
40 The packetheader needs to be updated with the amount of packets of each kind (answer, auth, additional)
950cfe0f 41
a0a276c2
BH
42 Each recordheader contains the length of a dns record.
43
44 Calling convention:
45
46 vector<uint8_t> content;
703761cc 47 DNSPacketWriter dpw(content, const string& qname, uint16_t qtype, uint16_t qclass=QClass:IN); // sets the question
7738a23f 48 dpw.startrecord("this.is.an.ip.address.", ns_t_a); // does nothing, except store qname and qtype
a0a276c2 49 dpw.xfr32BitInt(0x01020304); // adds 4 bytes (0x01020304) to the record buffer
7738a23f 50 dpw.startrecord("this.is.an.ip.address.", ns_t_a); // aha! writes out dnsrecord header containing qname and qtype and length 4, plus the recordbuffer, which gets emptied
a0a276c2
BH
51 // new qname and qtype are stored
52 dpw.xfr32BitInt(0x04030201); // adds 4 bytes (0x04030201) to the record buffer
53 dpw.commit(); // writes out dnsrecord header containing qname and qtype and length 4, plus the recordbuffer
54
55 // content now contains the ready packet, with 1 question and 2 answers
56
57*/
58
f3f4938f 59class DNSPacketWriter : public boost::noncopyable
a0a276c2 60{
a2ce25e4 61
a0a276c2 62public:
bca6643b 63 //! Start a DNS Packet in the vector passed, with question qname, qtype and qclass
c2f3be9d 64 DNSPacketWriter(vector<uint8_t>& content, const DNSName& qname, uint16_t qtype, uint16_t qclass=QClass::IN, uint8_t opcode=0);
950cfe0f
PD
65
66 /** Start a new DNS record within this packet for namq, qtype, ttl, class and in the requested place. Note that packets can only be written in natural order -
bca6643b 67 ANSWER, AUTHORITY, ADDITIONAL */
e693ff5a 68 void startRecord(const DNSName& name, uint16_t qtype, uint32_t ttl=3600, uint16_t qclass=QClass::IN, DNSResourceRecord::Place place=DNSResourceRecord::ANSWER, bool compress=true);
bca6643b
BH
69
70 /** Shorthand way to add an Opt-record, for example for EDNS0 purposes */
7f7b8d55 71 typedef vector<pair<uint16_t,std::string> > optvect_t;
d6c335ab 72 void addOpt(const uint16_t udpsize, const uint16_t extRCode, const uint16_t ednsFlags, const optvect_t& options=optvect_t(), const uint8_t version=0);
a0a276c2 73
bca6643b
BH
74 /** needs to be called after the last record is added, but can be called again and again later on. Is called internally by startRecord too.
75 The content of the vector<> passed to the constructor is inconsistent until commit is called.
76 */
77 void commit();
78
dffbaa08 79 uint32_t size(); // needs to be 32 bit because otherwise we don't see the wrap coming when it happened!
10321a98
BH
80
81 /** Should the packet have grown too big for the writer's liking, rollback removes the record currently being written */
82 void rollback();
83
add935a2
PD
84 /** Discard all content except the question section */
85 void truncate();
86
341930bb 87 void xfr48BitInt(uint64_t val);
a0a276c2
BH
88 void xfr32BitInt(uint32_t val);
89 void xfr16BitInt(uint16_t val);
8bf26468
BH
90 void xfrType(uint16_t val)
91 {
92 xfr16BitInt(val);
93 }
cbf0e7f3
BH
94 void xfrIP(const uint32_t& val)
95 {
ea634573 96 xfr32BitInt(htonl(val));
cbf0e7f3 97 }
950cfe0f 98 void xfrIP6(const std::string& val)
b9b28916
AT
99 {
100 xfrBlob(val,16);
101 }
f4352636
PD
102
103 void xfrCAWithoutPort(uint8_t version, ComboAddress &val)
104 {
105 if (version == 4) xfrIP(val.sin4.sin_addr.s_addr);
106 else if (version == 6) {
107 string blob;
108 blob.assign((const char*)val.sin6.sin6_addr.s6_addr, 16);
109 xfrBlob(blob, 16);
110 }
111 else throw runtime_error("invalid IP protocol");
112 }
113
114 void xfrCAPort(ComboAddress &val)
115 {
116 uint16_t port;
117 port = val.sin4.sin_port;
118 xfr16BitInt(port);
119 }
120
8bf26468
BH
121 void xfrTime(const uint32_t& val)
122 {
123 xfr32BitInt(val);
124 }
125
a0a276c2
BH
126 void xfr8BitInt(uint8_t val);
127
f21fc0aa 128 void xfrName(const DNSName& label, bool compress=false, bool noDot=false);
84e1142d 129 void xfrText(const string& text, bool multi=false, bool lenField=true);
948a927f 130 void xfrUnquotedText(const string& text, bool lenField);
06ffdc52 131 void xfrBlob(const string& blob, int len=-1);
2fe9d6f7 132 void xfrBlobNoSpaces(const string& blob, int len=-1);
e4090157 133 void xfrHexBlob(const string& blob, bool keepReading=false);
bca6643b 134
8e97e9a3 135 dnsheader* getHeader();
e636cab2 136 void getRecordPayload(string& records); // call __before commit__
ea634573 137
950cfe0f 138 void setCanonic(bool val)
f3f4938f
BH
139 {
140 d_canonic=val;
141 }
142
950cfe0f 143 void setLowercase(bool val)
7f5bf0ba
BH
144 {
145 d_lowerCase=val;
146 }
3f45f34d
BH
147 vector <uint8_t>& getContent()
148 {
149 return d_content;
150 }
d476d7fb 151 bool eof() { return true; } // we don't know how long the record should be
7f5bf0ba 152
ddb79bca
AT
153 const string getRemaining() const {
154 return "";
155 }
a0a276c2 156private:
802a93d0 157 uint16_t lookupName(const DNSName& name, uint16_t* matchlen);
fea4599a 158 vector<uint16_t> d_namepositions;
ef2ea4bf 159 // We declare 1 uint_16 in the public section, these 3 align on a 8-byte boundary
78817f22
PL
160 uint16_t d_sor;
161 uint16_t d_rollbackmarker; // start of last complete packet, for rollback
162
57e5f5f7 163 vector <uint8_t>& d_content;
c2f3be9d 164 DNSName d_qname;
78817f22 165
add935a2 166 uint16_t d_truncatemarker; // end of header, for truncate
e693ff5a 167 DNSResourceRecord::Place d_recordplace;
02e7763f 168 bool d_canonic, d_lowerCase, d_compress{false};
a0a276c2 169};
213f6de6
BH
170
171typedef vector<pair<string::size_type, string::size_type> > labelparts_t;
c2f3be9d 172// bool labeltokUnescape(labelparts_t& parts, const DNSName& label);
bac8f21b 173std::vector<string> segmentDNSText(const string& text); // from dnslabeltext.rl
e8c59f2d 174std::deque<string> segmentDNSName(const string& input); // from dnslabeltext.rl