]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/lua-recursor4.hh
Merge pull request #5523 from rubenk/fix-typos-in-logmessage
[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
23#include "iputils.hh"
24#include "dnsname.hh"
25#include "namespaces.hh"
a3e7b735 26#include "dnsrecords.hh"
db486de5 27#include "filterpo.hh"
00b8cadc
RG
28#include "ednsoptions.hh"
29
6b8b26c8 30#include <unordered_map>
00b8cadc 31
c672b54a
RG
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
f90c7544 36string GenUDPQueryResponse(const ComboAddress& dest, const string& query);
b4015453 37unsigned int getRecursorThreadId();
808c5ef7 38
39class LuaContext;
3dcc3fde 40
5fd2577f
PD
41#if defined(HAVE_LUA)
42#undef L
43#include "ext/luawrapper/include/LuaContext.hpp"
44#define L theL()
45#endif
46
a3e7b735 47class RecursorLua4 : public boost::noncopyable
808c5ef7 48{
3dcc3fde 49private:
50 std::unique_ptr<LuaContext> d_lw; // this is way on top because it must get destroyed _last_
51
808c5ef7 52public:
53 explicit RecursorLua4(const std::string& fname);
3dcc3fde 54 ~RecursorLua4(); // this is so unique_ptr works with an incomplete type
a3e7b735 55
a3e7b735 56 struct DNSQuestion
57 {
ba21fcfe
RG
58 DNSQuestion(const ComboAddress& rem, const ComboAddress& loc, const DNSName& query, uint16_t type, bool tcp, bool& variable_, bool& wantsRPZ_): qname(query), qtype(type), local(loc), remote(rem), isTcp(tcp), variable(variable_), wantsRPZ(wantsRPZ_)
59 {
60 }
61 const DNSName& qname;
62 const uint16_t qtype;
63 const ComboAddress& local;
64 const ComboAddress& remote;
621e4e59 65 const struct dnsheader* dh{nullptr};
ba21fcfe
RG
66 const bool isTcp;
67 const std::vector<pair<uint16_t, string>>* ednsOptions{nullptr};
e2fb3504 68 const uint16_t* ednsFlags{nullptr};
ba21fcfe
RG
69 vector<DNSRecord>* currentRecords{nullptr};
70 DNSFilterEngine::Policy* appliedPolicy{nullptr};
71 std::vector<std::string>* policyTags{nullptr};
72 std::unordered_map<std::string,bool>* discardedPolicies{nullptr};
67e31ebe 73 std::string requestorId;
ba21fcfe
RG
74 bool& variable;
75 bool& wantsRPZ;
b673817a 76 unsigned int tag{0};
ba21fcfe 77
aee72a7b 78 void addAnswer(uint16_t type, const std::string& content, boost::optional<int> ttl, boost::optional<string> name);
79 void addRecord(uint16_t type, const std::string& content, DNSResourceRecord::Place place, boost::optional<int> ttl, boost::optional<string> name);
ba21fcfe 80 vector<pair<int,DNSRecord> > getRecords() const;
621e4e59 81 boost::optional<dnsheader> getDH() const;
ba21fcfe
RG
82 vector<pair<uint16_t, string> > getEDNSOptions() const;
83 boost::optional<string> getEDNSOption(uint16_t code) const;
84 boost::optional<Netmask> getEDNSSubnet() const;
e2fb3504
PL
85 vector<string> getEDNSFlags() const;
86 bool getEDNSFlag(string flag) const;
a3e7b735 87 void setRecords(const vector<pair<int,DNSRecord> >& records);
ba21fcfe
RG
88
89 int rcode{0};
90 // struct dnsheader, packet length would be great
91 vector<DNSRecord> records;
a3e7b735 92
93 string followupFunction;
94 string followupPrefix;
f90c7544 95
96 string udpQuery;
97 ComboAddress udpQueryDest;
98 string udpAnswer;
99 string udpCallback;
100
5fd2577f 101 LuaContext::LuaObject data;
a3e7b735 102 DNSName followupName;
103 };
104
67e31ebe 105 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 std::map<uint16_t, EDNSOptionView>&, bool tcp, std::string& requestorId);
ba21fcfe 106
6e505c5e
RG
107 bool prerpz(DNSQuestion& dq, int& ret);
108 bool preresolve(DNSQuestion& dq, int& ret);
109 bool nxdomain(DNSQuestion& dq, int& ret);
110 bool nodata(DNSQuestion& dq, int& ret);
111 bool postresolve(DNSQuestion& dq, int& ret);
ba21fcfe
RG
112
113 bool preoutquery(const ComboAddress& ns, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool isTcp, vector<DNSRecord>& res, int& ret);
114 bool ipfilter(const ComboAddress& remote, const ComboAddress& local, const struct dnsheader&);
115
116 bool needDQ() const
117 {
118 return (d_prerpz ||
119 d_preresolve ||
120 d_nxdomain ||
121 d_nodata ||
122 d_postresolve);
123 }
124
67e31ebe 125 typedef std::function<std::tuple<unsigned int,boost::optional<std::unordered_map<int,string> >,boost::optional<LuaContext::LuaObject>,boost::optional<std::string> >(ComboAddress, Netmask, ComboAddress, DNSName, uint16_t, const std::map<uint16_t, EDNSOptionView>&, bool)> gettag_t;
ba21fcfe
RG
126 gettag_t d_gettag; // public so you can query if we have this hooked
127
128private:
1c567515 129 typedef std::function<bool(DNSQuestion*)> luacall_t;
0a273054 130 luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery;
6e505c5e 131 bool genhook(luacall_t& func, DNSQuestion& dq, int& ret);
f5062066 132 typedef std::function<bool(ComboAddress,ComboAddress, struct dnsheader)> ipfilter_t;
a3e7b735 133 ipfilter_t d_ipfilter;
808c5ef7 134};
135