2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
26 #include <sys/types.h>
28 #include "ednssubnet.hh"
29 #include <unordered_set>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
34 #include <arpa/inet.h>
44 #include "pdnsexception.hh"
45 #include "dnsrecords.hh"
51 //! This class represents DNS packets, either received or to be sent.
55 DNSPacket(bool isQuery);
56 DNSPacket(const DNSPacket &orig) = default;
57 DNSPacket & operator=(const DNSPacket &) = default;
59 int noparse(const char *mesg, size_t len); //!< just suck the data inward
60 int parse(const char *mesg, size_t len); //!< parse a raw UDP or TCP packet and suck the data inward
61 const string& getString(); //!< for serialization - just passes the whole packet
63 // address & socket manipulation
64 void setRemote(const ComboAddress*);
65 ComboAddress getRemote() const;
66 Netmask getRealRemote() const;
67 ComboAddress getLocal() const
70 socklen_t len=sizeof(ca);
71 getsockname(d_socket, (sockaddr*)&ca, &len);
74 uint16_t getRemotePort() const;
76 boost::optional<ComboAddress> d_anyLocal;
78 Utility::sock_t getSocket() const
82 void setSocket(Utility::sock_t sock);
85 // these manipulate 'd'
86 void setA(bool); //!< make this packet authoritative - manipulates 'd'
87 void setID(uint16_t); //!< set the DNS id of this packet - manipulates 'd'
88 void setRA(bool); //!< set the Recursion Available flag - manipulates 'd'
89 void setRD(bool); //!< set the Recursion Desired flag - manipulates 'd'
90 void setAnswer(bool); //!< Make this packet an answer - clears the 'stringbuffer' first, if passed 'true', does nothing otherwise, manipulates 'd'
92 void setOpcode(uint16_t); //!< set the Opcode of this packet - manipulates 'd'
93 void setRcode(int v); //!< set the Rcode of this packet - manipulates 'd'
95 void clearRecords(); //!< when building a packet, wipe all previously added records (clears 'rrs')
97 /** Add a DNSZoneRecord to this packet. A DNSPacket (as does a DNS Packet) has 4 kinds of resource records. Questions,
98 Answers, Authority and Additional. See RFC 1034 and 1035 for details. You can specify where a record needs to go in the
99 DNSZoneRecord d_place field */
100 void addRecord(DNSZoneRecord&&); // adds to 'rrs'
102 void setQuestion(int op, const DNSName &qdomain, int qtype); // wipes 'd', sets a random id, creates start of packet (domain, type, class etc)
104 DTime d_dt; //!< the time this packet was created. replyPacket() copies this in for you, so d_dt becomes the time spent processing the question+answer
105 void wrapup(); // writes out queued rrs, and generates the binary packet. also shuffles. also rectifies dnsheader 'd', and copies it to the stringbuffer
106 void spoofQuestion(const DNSPacket& qd); //!< paste in the exact right case of the question. Useful for PacketCache
107 unsigned int getMinTTL(); //!< returns lowest TTL of any record in the packet
108 bool isEmpty(); //!< returns true if there are no rrs in the packet
110 vector<DNSZoneRecord*> getAPRecords(); //!< get a vector with DNSZoneRecords that need additional processing
111 vector<DNSZoneRecord*> getAnswerRecords(); //!< get a vector with DNSZoneRecords that are answers
112 void setCompress(bool compress);
114 std::unique_ptr<DNSPacket> replyPacket() const; //!< convenience function that creates a virgin answer packet to this question
116 void commitD(); //!< copies 'd' into the stringbuffer
117 unsigned int getMaxReplyLen(); //!< retrieve the maximum length of the packet we should send in response
118 void setMaxReplyLen(int bytes); //!< set the max reply len (used when retrieving from the packet cache, and this changed)
120 bool couldBeCached() const; //!< returns 0 if this query should bypass the packet cache
121 bool hasEDNSSubnet() const;
122 bool hasEDNS() const;
123 uint8_t getEDNSVersion() const { return d_ednsversion; };
124 void setEDNSRcode(uint16_t extRCode)
126 // WARNING: this is really 12 bits
127 d_ednsrcode=extRCode;
129 uint8_t getEDNSRCode() const { return d_ednsrcode; };
130 uint32_t getHash() const { return d_hash; };
131 void setHash(uint32_t hash) { d_hash = hash; };
135 DNSName qdomain; //!< qname of the question 4 - unsure how this is used
136 DNSName qdomainwild; //!< wildcard matched by qname, used by LuaPolicyEngine
137 DNSName qdomainzone; //!< zone name for the answer (as reflected in SOA for negative responses), used by LuaPolicyEngine
138 string d_peer_principal;
139 const DNSName& getTSIGKeyname() const;
141 struct dnsheader d; //!< dnsheader at the start of the databuffer 12
143 TSIGRecordContent d_trc; //72
145 ComboAddress d_remote; //28
146 TSIGHashEnum d_tsig_algo{TSIG_MD5}; //4
148 int d_ednsRawPacketSizeLimit{-1}; // only used for Lua record
149 uint16_t qclass{QClass::IN}; //!< class of the question - should always be INternet 2
150 QType qtype; //!< type of the question 2
153 bool d_dnssecOk{false};
154 bool d_havetsig{false};
156 bool getTSIGDetails(TSIGRecordContent* tr, DNSName* keyname, uint16_t* tsigPos=nullptr) const;
157 void setTSIGDetails(const TSIGRecordContent& tr, const DNSName& keyname, const string& secret, const string& previous, bool timersonly=false);
158 bool getTKEYRecord(TKEYRecordContent* tr, DNSName* keyname) const;
160 vector<DNSZoneRecord>& getRRS() { return d_rrs; }
161 bool checkForCorrectTSIG(UeberBackend* B, DNSName* keyname, string* secret, TSIGRecordContent* trc) const;
163 static uint16_t s_udpTruncationThreshold;
164 static bool s_doEDNSSubnetProcessing;
167 void pasteQ(const char *question, int length); //!< set the question of this packet, useful for crafting replies
170 DNSName d_tsigkeyname;
171 string d_tsigprevious;
173 vector<DNSZoneRecord> d_rrs; // 8
174 std::unordered_set<size_t> d_dedup;
175 string d_rawpacket; // this is where everything lives 8
176 EDNSSubnetOpts d_eso;
178 int d_maxreplylen{0};
179 int d_socket{-1}; // 4
181 // WARNING! This is really 12 bits
182 uint16_t d_ednsrcode{0};
183 uint8_t d_ednsversion{0};
185 bool d_wrapped{false}; // 1
186 bool d_compress{true}; // 1
187 bool d_tsigtimersonly{false};
188 bool d_wantsnsid{false};
189 bool d_haveednssubnet{false};
190 bool d_haveednssection{false};