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