]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/lua-recursor4.hh
limit compression pointers to 14 bits
[thirdparty/pdns.git] / pdns / lua-recursor4.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 */
808c5ef7 22#pragma once
00b8cadc 23
c672b54a
RG
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
1921a4c2
RG
28#include "iputils.hh"
29#include "dnsname.hh"
30#include "namespaces.hh"
31#include "dnsrecords.hh"
32#include "filterpo.hh"
33#include "ednsoptions.hh"
34#include "validate.hh"
70c21c40
AT
35#include "lua-base4.hh"
36#include <unordered_map>
1921a4c2 37
70fb28d9
RG
38#include "lua-recursor4-ffi.hh"
39
f90c7544 40string GenUDPQueryResponse(const ComboAddress& dest, const string& query);
8fb594ba 41unsigned int getRecursorThreadId();
808c5ef7 42
4a639c36
RG
43// pdns_ffi_param_t is a lightuserdata
44template<>
45struct LuaContext::Pusher<pdns_ffi_param*> {
46 static const int minSize = 1;
47 static const int maxSize = 1;
48
49 static PushedObject push(lua_State* state, pdns_ffi_param* ptr) noexcept {
50 lua_pushlightuserdata(state, ptr);
51 return PushedObject{state, 1};
52 }
53};
54
70c21c40 55class RecursorLua4 : public BaseLua4
808c5ef7 56{
57public:
9694e14f 58 RecursorLua4();
3dcc3fde 59 ~RecursorLua4(); // this is so unique_ptr works with an incomplete type
a3e7b735 60
a3e7b735 61 struct DNSQuestion
62 {
f1c7929a 63 DNSQuestion(const ComboAddress& rem, const ComboAddress& loc, const DNSName& query, uint16_t type, bool tcp, bool& variable_, bool& wantsRPZ_, bool& logResponse_): qname(query), qtype(type), local(loc), remote(rem), isTcp(tcp), variable(variable_), wantsRPZ(wantsRPZ_), logResponse(logResponse_)
ba21fcfe
RG
64 {
65 }
66 const DNSName& qname;
67 const uint16_t qtype;
68 const ComboAddress& local;
69 const ComboAddress& remote;
621e4e59 70 const struct dnsheader* dh{nullptr};
ba21fcfe
RG
71 const bool isTcp;
72 const std::vector<pair<uint16_t, string>>* ednsOptions{nullptr};
e2fb3504 73 const uint16_t* ednsFlags{nullptr};
ba21fcfe
RG
74 vector<DNSRecord>* currentRecords{nullptr};
75 DNSFilterEngine::Policy* appliedPolicy{nullptr};
76 std::vector<std::string>* policyTags{nullptr};
77 std::unordered_map<std::string,bool>* discardedPolicies{nullptr};
67e31ebe 78 std::string requestorId;
590388d2 79 std::string deviceId;
0a6a45c8 80 std::string deviceName;
1921a4c2 81 vState validationState{Indeterminate};
ba21fcfe
RG
82 bool& variable;
83 bool& wantsRPZ;
f1c7929a 84 bool& logResponse;
b673817a 85 unsigned int tag{0};
ba21fcfe 86
aee72a7b 87 void addAnswer(uint16_t type, const std::string& content, boost::optional<int> ttl, boost::optional<string> name);
88 void addRecord(uint16_t type, const std::string& content, DNSResourceRecord::Place place, boost::optional<int> ttl, boost::optional<string> name);
ba21fcfe 89 vector<pair<int,DNSRecord> > getRecords() const;
621e4e59 90 boost::optional<dnsheader> getDH() const;
ba21fcfe
RG
91 vector<pair<uint16_t, string> > getEDNSOptions() const;
92 boost::optional<string> getEDNSOption(uint16_t code) const;
93 boost::optional<Netmask> getEDNSSubnet() const;
e2fb3504
PL
94 vector<string> getEDNSFlags() const;
95 bool getEDNSFlag(string flag) const;
a3e7b735 96 void setRecords(const vector<pair<int,DNSRecord> >& records);
ba21fcfe
RG
97
98 int rcode{0};
99 // struct dnsheader, packet length would be great
100 vector<DNSRecord> records;
a3e7b735 101
102 string followupFunction;
103 string followupPrefix;
f90c7544 104
105 string udpQuery;
106 ComboAddress udpQueryDest;
107 string udpAnswer;
108 string udpCallback;
109
5fd2577f 110 LuaContext::LuaObject data;
a3e7b735 111 DNSName followupName;
112 };
113
0a6a45c8
CHB
114 unsigned int gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, LuaContext::LuaObject& data, const EDNSOptionViewMap&, bool tcp, std::string& requestorId, std::string& deviceId, std::string& deviceName) const;
115 unsigned int gettag_ffi(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, LuaContext::LuaObject& data, const EDNSOptionViewMap&, bool tcp, std::string& requestorId, std::string& deviceId, std::string& deviceName, uint32_t& ttlCap, bool& variable, bool& logQuery) const;
ba21fcfe 116
a2f87dd1 117 void maintenance() const;
5899ee54
RG
118 bool prerpz(DNSQuestion& dq, int& ret) const;
119 bool preresolve(DNSQuestion& dq, int& ret) const;
120 bool nxdomain(DNSQuestion& dq, int& ret) const;
121 bool nodata(DNSQuestion& dq, int& ret) const ;
122 bool postresolve(DNSQuestion& dq, int& ret) const;
ba21fcfe 123
5899ee54
RG
124 bool preoutquery(const ComboAddress& ns, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool isTcp, vector<DNSRecord>& res, int& ret) const;
125 bool ipfilter(const ComboAddress& remote, const ComboAddress& local, const struct dnsheader&) const;
ba21fcfe
RG
126
127 bool needDQ() const
128 {
129 return (d_prerpz ||
130 d_preresolve ||
131 d_nxdomain ||
132 d_nodata ||
133 d_postresolve);
134 }
135
0a6a45c8 136 typedef std::function<std::tuple<unsigned int,boost::optional<std::unordered_map<int,string> >,boost::optional<LuaContext::LuaObject>,boost::optional<std::string>,boost::optional<std::string>,boost::optional<std::string> >(ComboAddress, Netmask, ComboAddress, DNSName, uint16_t, const EDNSOptionViewMap&, bool)> gettag_t;
ba21fcfe 137 gettag_t d_gettag; // public so you can query if we have this hooked
70fb28d9
RG
138 typedef std::function<boost::optional<LuaContext::LuaObject>(pdns_ffi_param_t*)> gettag_ffi_t;
139 gettag_ffi_t d_gettag_ffi;
140
70c21c40 141protected:
9694e14f
AT
142 virtual void postPrepareContext() override;
143 virtual void postLoad() override;
ba21fcfe 144private:
a2f87dd1
CHB
145 typedef std::function<void()> luamaintenance_t;
146 luamaintenance_t d_maintenance;
1c567515 147 typedef std::function<bool(DNSQuestion*)> luacall_t;
0a273054 148 luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery;
5899ee54 149 bool genhook(const luacall_t& func, DNSQuestion& dq, int& ret) const;
f5062066 150 typedef std::function<bool(ComboAddress,ComboAddress, struct dnsheader)> ipfilter_t;
a3e7b735 151 ipfilter_t d_ipfilter;
808c5ef7 152};
153