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.
22 #include "lua-recursor4.hh"
25 #include "dnsparser.hh"
27 #include "namespaces.hh"
28 #include "rec_channel.hh"
29 #include "ednsoptions.hh"
30 #include "ednssubnet.hh"
31 #include "filterpo.hh"
32 #include "rec-snmp.hh"
33 #include <unordered_set>
35 RecursorLua4::RecursorLua4() { prepareContext(); }
37 boost::optional
<dnsheader
> RecursorLua4::DNSQuestion::getDH() const
41 return boost::optional
<dnsheader
>();
44 vector
<string
> RecursorLua4::DNSQuestion::getEDNSFlags() const
48 if (*ednsFlags
& EDNSOpts::DNSSECOK
)
54 bool RecursorLua4::DNSQuestion::getEDNSFlag(string flag
) const
57 if (flag
== "DO" && (*ednsFlags
& EDNSOpts::DNSSECOK
))
63 vector
<pair
<uint16_t, string
> > RecursorLua4::DNSQuestion::getEDNSOptions() const
68 return vector
<pair
<uint16_t,string
>>();
71 boost::optional
<string
> RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code
) const
74 for(const auto& o
: *ednsOptions
)
78 return boost::optional
<string
>();
81 boost::optional
<Netmask
> RecursorLua4::DNSQuestion::getEDNSSubnet() const
84 for(const auto& o
: *ednsOptions
) {
85 if(o
.first
==EDNSOptionCode::ECS
) {
87 if(getEDNSSubnetOptsFromString(o
.second
, &eso
))
94 return boost::optional
<Netmask
>();
97 std::vector
<std::pair
<int, ProxyProtocolValue
>> RecursorLua4::DNSQuestion::getProxyProtocolValues() const
99 std::vector
<std::pair
<int, ProxyProtocolValue
>> result
;
100 if (proxyProtocolValues
) {
101 result
.reserve(proxyProtocolValues
->size());
104 for (const auto& value
: *proxyProtocolValues
) {
105 result
.push_back({ idx
++, value
});
112 vector
<pair
<int, DNSRecord
> > RecursorLua4::DNSQuestion::getRecords() const
114 vector
<pair
<int, DNSRecord
> > ret
;
116 for(const auto& r
: records
) {
117 ret
.push_back({num
++, r
});
121 void RecursorLua4::DNSQuestion::setRecords(const vector
<pair
<int, DNSRecord
> >& recs
)
124 for(const auto& p
: recs
) {
125 records
.push_back(p
.second
);
129 void RecursorLua4::DNSQuestion::addRecord(uint16_t type
, const std::string
& content
, DNSResourceRecord::Place place
, boost::optional
<int> ttl
, boost::optional
<string
> name
)
132 dr
.d_name
=name
? DNSName(*name
) : qname
;
133 dr
.d_ttl
=ttl
.get_value_or(3600);
136 dr
.d_content
= DNSRecordContent::mastermake(type
, 1, content
);
137 records
.push_back(dr
);
140 void RecursorLua4::DNSQuestion::addAnswer(uint16_t type
, const std::string
& content
, boost::optional
<int> ttl
, boost::optional
<string
> name
)
142 addRecord(type
, content
, DNSResourceRecord::ANSWER
, ttl
, name
);
147 std::atomic
<unsigned long>* ptr
;
148 void inc() { (*ptr
)++; }
149 void incBy(unsigned int by
) { (*ptr
)+= by
; }
150 unsigned long get() { return *ptr
; }
151 void set(unsigned long val
) { *ptr
=val
; }
154 void RecursorLua4::postPrepareContext()
156 d_lw
->registerMember
<const DNSName (DNSQuestion::*)>("qname", [](const DNSQuestion
& dq
) -> const DNSName
& { return dq
.qname
; }, [](DNSQuestion
& dq
, const DNSName
& newName
) { (void) newName
; });
157 d_lw
->registerMember
<uint16_t (DNSQuestion::*)>("qtype", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.qtype
; }, [](DNSQuestion
& dq
, uint16_t newType
) { (void) newType
; });
158 d_lw
->registerMember
<bool (DNSQuestion::*)>("isTcp", [](const DNSQuestion
& dq
) -> bool { return dq
.isTcp
; }, [](DNSQuestion
& dq
, bool newTcp
) { (void) newTcp
; });
159 d_lw
->registerMember
<const ComboAddress (DNSQuestion::*)>("localaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
& { return dq
.local
; }, [](DNSQuestion
& dq
, const ComboAddress
& newLocal
) { (void) newLocal
; });
160 d_lw
->registerMember
<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
& { return dq
.remote
; }, [](DNSQuestion
& dq
, const ComboAddress
& newRemote
) { (void) newRemote
; });
161 d_lw
->registerMember
<vState (DNSQuestion::*)>("validationState", [](const DNSQuestion
& dq
) -> vState
{ return dq
.validationState
; }, [](DNSQuestion
& dq
, vState newState
) { (void) newState
; });
163 d_lw
->registerMember
<bool (DNSQuestion::*)>("variable", [](const DNSQuestion
& dq
) -> bool { return dq
.variable
; }, [](DNSQuestion
& dq
, bool newVariable
) { dq
.variable
= newVariable
; });
164 d_lw
->registerMember
<bool (DNSQuestion::*)>("wantsRPZ", [](const DNSQuestion
& dq
) -> bool { return dq
.wantsRPZ
; }, [](DNSQuestion
& dq
, bool newWantsRPZ
) { dq
.wantsRPZ
= newWantsRPZ
; });
165 d_lw
->registerMember
<bool (DNSQuestion::*)>("logResponse", [](const DNSQuestion
& dq
) -> bool { return dq
.logResponse
; }, [](DNSQuestion
& dq
, bool newLogResponse
) { dq
.logResponse
= newLogResponse
; });
167 d_lw
->registerMember("rcode", &DNSQuestion::rcode
);
168 d_lw
->registerMember("tag", &DNSQuestion::tag
);
169 d_lw
->registerMember("requestorId", &DNSQuestion::requestorId
);
170 d_lw
->registerMember("deviceId", &DNSQuestion::deviceId
);
171 d_lw
->registerMember("deviceName", &DNSQuestion::deviceName
);
172 d_lw
->registerMember("followupFunction", &DNSQuestion::followupFunction
);
173 d_lw
->registerMember("followupPrefix", &DNSQuestion::followupPrefix
);
174 d_lw
->registerMember("followupName", &DNSQuestion::followupName
);
175 d_lw
->registerMember("data", &DNSQuestion::data
);
176 d_lw
->registerMember("udpQuery", &DNSQuestion::udpQuery
);
177 d_lw
->registerMember("udpAnswer", &DNSQuestion::udpAnswer
);
178 d_lw
->registerMember("udpQueryDest", &DNSQuestion::udpQueryDest
);
179 d_lw
->registerMember("udpCallback", &DNSQuestion::udpCallback
);
180 d_lw
->registerMember("appliedPolicy", &DNSQuestion::appliedPolicy
);
181 d_lw
->registerMember
<DNSFilterEngine::Policy
, std::string
>("policyName",
182 [](const DNSFilterEngine::Policy
& pol
) -> std::string
{
183 return pol
.getName();
185 [](DNSFilterEngine::Policy
& pol
, const std::string
& name
) {
188 d_lw
->registerMember("policyKind", &DNSFilterEngine::Policy::d_kind
);
189 d_lw
->registerMember("policyType", &DNSFilterEngine::Policy::d_type
);
190 d_lw
->registerMember("policyTTL", &DNSFilterEngine::Policy::d_ttl
);
191 d_lw
->registerMember
<DNSFilterEngine::Policy
, std::string
>("policyCustom",
192 [](const DNSFilterEngine::Policy
& pol
) -> std::string
{
194 if (pol
.d_kind
!= DNSFilterEngine::PolicyKind::Custom
) {
198 for (const auto& dr
: pol
.d_custom
) {
199 if (!result
.empty()) {
202 result
+= dr
->getZoneRepresentation();
207 [](DNSFilterEngine::Policy
& pol
, const std::string
& content
) {
208 // Only CNAMES for now, when we ever add a d_custom_type, there will be pain
209 pol
.d_custom
.clear();
210 pol
.d_custom
.push_back(DNSRecordContent::mastermake(QType::CNAME
, QClass::IN
, content
));
213 d_lw
->registerFunction("getDH", &DNSQuestion::getDH
);
214 d_lw
->registerFunction("getEDNSOptions", &DNSQuestion::getEDNSOptions
);
215 d_lw
->registerFunction("getEDNSOption", &DNSQuestion::getEDNSOption
);
216 d_lw
->registerFunction("getEDNSSubnet", &DNSQuestion::getEDNSSubnet
);
217 d_lw
->registerFunction("getProxyProtocolValues", &DNSQuestion::getProxyProtocolValues
);
218 d_lw
->registerFunction("getEDNSFlags", &DNSQuestion::getEDNSFlags
);
219 d_lw
->registerFunction("getEDNSFlag", &DNSQuestion::getEDNSFlag
);
220 d_lw
->registerMember("name", &DNSRecord::d_name
);
221 d_lw
->registerMember("type", &DNSRecord::d_type
);
222 d_lw
->registerMember("ttl", &DNSRecord::d_ttl
);
223 d_lw
->registerMember("place", &DNSRecord::d_place
);
225 d_lw
->registerMember("size", &EDNSOptionViewValue::size
);
226 d_lw
->registerFunction
<std::string(EDNSOptionViewValue::*)()>("getContent", [](const EDNSOptionViewValue
& value
) { return std::string(value
.content
, value
.size
); });
227 d_lw
->registerFunction
<size_t(EDNSOptionView::*)()>("count", [](const EDNSOptionView
& option
) { return option
.values
.size(); });
228 d_lw
->registerFunction
<std::vector
<string
>(EDNSOptionView::*)()>("getValues", [] (const EDNSOptionView
& option
) {
229 std::vector
<string
> values
;
230 for (const auto& value
: option
.values
) {
231 values
.push_back(std::string(value
.content
, value
.size
));
236 /* pre 4.2 API compatibility, when we had only one value for a given EDNS option */
237 d_lw
->registerMember
<uint16_t(EDNSOptionView::*)>("size", [](const EDNSOptionView
& option
) -> uint16_t {
240 if (!option
.values
.empty()) {
241 result
= option
.values
.at(0).size
;
245 [](EDNSOptionView
& option
, uint16_t newSize
) { (void) newSize
; });
246 d_lw
->registerFunction
<std::string(EDNSOptionView::*)()>("getContent", [](const EDNSOptionView
& option
) {
247 if (option
.values
.empty()) {
248 return std::string();
250 return std::string(option
.values
.at(0).content
, option
.values
.at(0).size
); });
252 d_lw
->registerFunction
<string(DNSRecord::*)()>("getContent", [](const DNSRecord
& dr
) { return dr
.d_content
->getZoneRepresentation(); });
253 d_lw
->registerFunction
<boost::optional
<ComboAddress
>(DNSRecord::*)()>("getCA", [](const DNSRecord
& dr
) {
254 boost::optional
<ComboAddress
> ret
;
256 if(auto rec
= std::dynamic_pointer_cast
<ARecordContent
>(dr
.d_content
))
258 else if(auto aaaarec
= std::dynamic_pointer_cast
<AAAARecordContent
>(dr
.d_content
))
259 ret
=aaaarec
->getCA(53);
263 d_lw
->registerFunction
<const ProxyProtocolValue
, std::string()>("getContent", [](const ProxyProtocolValue
& value
) { return value
.content
; });
264 d_lw
->registerFunction
<const ProxyProtocolValue
, uint8_t()>("getType", [](const ProxyProtocolValue
& value
) { return value
.type
; });
266 d_lw
->registerFunction
<void(DNSRecord::*)(const std::string
&)>("changeContent", [](DNSRecord
& dr
, const std::string
& newContent
) { dr
.d_content
= DNSRecordContent::mastermake(dr
.d_type
, 1, newContent
); });
267 d_lw
->registerFunction("addAnswer", &DNSQuestion::addAnswer
);
268 d_lw
->registerFunction("addRecord", &DNSQuestion::addRecord
);
269 d_lw
->registerFunction("getRecords", &DNSQuestion::getRecords
);
270 d_lw
->registerFunction("setRecords", &DNSQuestion::setRecords
);
272 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::string
&)>("addPolicyTag", [](DNSQuestion
& dq
, const std::string
& tag
) { if (dq
.policyTags
) { dq
.policyTags
->insert(tag
); } });
273 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::vector
<std::pair
<int, std::string
> >&)>("setPolicyTags", [](DNSQuestion
& dq
, const std::vector
<std::pair
<int, std::string
> >& tags
) {
275 dq
.policyTags
->clear();
276 dq
.policyTags
->reserve(tags
.size());
277 for (const auto& tag
: tags
) {
278 dq
.policyTags
->insert(tag
.second
);
282 d_lw
->registerFunction
<std::vector
<std::pair
<int, std::string
> >(DNSQuestion::*)()>("getPolicyTags", [](const DNSQuestion
& dq
) {
283 std::vector
<std::pair
<int, std::string
> > ret
;
286 ret
.reserve(dq
.policyTags
->size());
287 for (const auto& tag
: *dq
.policyTags
) {
288 ret
.push_back({count
++, tag
});
294 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::string
&)>("discardPolicy", [](DNSQuestion
& dq
, const std::string
& policy
) {
295 if (dq
.discardedPolicies
) {
296 (*dq
.discardedPolicies
)[policy
] = true;
300 d_lw
->writeFunction("newDS", []() { return SuffixMatchNode(); });
301 d_lw
->registerFunction
<void(SuffixMatchNode::*)(boost::variant
<string
,DNSName
, vector
<pair
<unsigned int,string
> > >)>(
303 [](SuffixMatchNode
&smn
, const boost::variant
<string
,DNSName
,vector
<pair
<unsigned int,string
> > >& in
){
305 if(auto s
= boost::get
<string
>(&in
)) {
306 smn
.add(DNSName(*s
));
308 else if(auto v
= boost::get
<vector
<pair
<unsigned int, string
> > >(&in
)) {
309 for(const auto& entry
: *v
)
310 smn
.add(DNSName(entry
.second
));
313 smn
.add(boost::get
<DNSName
>(in
));
316 catch(std::exception
& e
) {
317 g_log
<<Logger::Error
<<e
.what()<<endl
;
322 d_lw
->registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName
&) const) &SuffixMatchNode::check
);
323 d_lw
->registerFunction("toString",(string (SuffixMatchNode::*)() const) &SuffixMatchNode::toString
);
325 d_pd
.push_back({"policykinds", in_t
{
326 {"NoAction", (int)DNSFilterEngine::PolicyKind::NoAction
},
327 {"Drop", (int)DNSFilterEngine::PolicyKind::Drop
},
328 {"NXDOMAIN", (int)DNSFilterEngine::PolicyKind::NXDOMAIN
},
329 {"NODATA", (int)DNSFilterEngine::PolicyKind::NODATA
},
330 {"Truncate", (int)DNSFilterEngine::PolicyKind::Truncate
},
331 {"Custom", (int)DNSFilterEngine::PolicyKind::Custom
}
334 for(const auto& n
: QType::names
)
335 d_pd
.push_back({n
.first
, n
.second
});
337 d_pd
.push_back({"validationstates", in_t
{
338 {"Indeterminate", Indeterminate
},
340 {"Insecure", Insecure
},
344 d_pd
.push_back({"now", &g_now
});
346 d_lw
->writeFunction("getMetric", [](const std::string
& str
) {
347 return DynMetric
{getDynMetric(str
)};
350 d_lw
->registerFunction("inc", &DynMetric::inc
);
351 d_lw
->registerFunction("incBy", &DynMetric::incBy
);
352 d_lw
->registerFunction("set", &DynMetric::set
);
353 d_lw
->registerFunction("get", &DynMetric::get
);
355 d_lw
->writeFunction("getStat", [](const std::string
& str
) {
357 optional
<uint64_t> value
= getStatByName(str
);
364 d_lw
->writeFunction("getRecursorThreadId", []() {
365 return getRecursorThreadId();
368 d_lw
->writeFunction("sendCustomSNMPTrap", [](const std::string
& str
) {
370 g_snmpAgent
->sendCustomTrap(str
);
374 d_lw
->writeFunction("getregisteredname", [](const DNSName
&dname
) {
375 return getRegisteredName(dname
);
379 void RecursorLua4::postLoad() {
380 d_prerpz
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("prerpz").get_value_or(0);
381 d_preresolve
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("preresolve").get_value_or(0);
382 d_nodata
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("nodata").get_value_or(0);
383 d_nxdomain
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("nxdomain").get_value_or(0);
384 d_postresolve
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("postresolve").get_value_or(0);
385 d_preoutquery
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("preoutquery").get_value_or(0);
386 d_maintenance
= d_lw
->readVariable
<boost::optional
<luamaintenance_t
>>("maintenance").get_value_or(0);
388 d_ipfilter
= d_lw
->readVariable
<boost::optional
<ipfilter_t
>>("ipfilter").get_value_or(0);
389 d_gettag
= d_lw
->readVariable
<boost::optional
<gettag_t
>>("gettag").get_value_or(0);
390 d_gettag_ffi
= d_lw
->readVariable
<boost::optional
<gettag_ffi_t
>>("gettag_ffi").get_value_or(0);
393 void RecursorLua4::getFeatures(Features
& features
) {
394 // Add key-values pairs below.
395 // Make sure you add string values explicitly converted to string.
396 // e.g. features.push_back(make_pair("somekey", string("stringvalue"));
397 // Both int and double end up as a lua number type.
398 features
.push_back(make_pair("PR8001_devicename", true));
401 void RecursorLua4::maintenance() const
408 bool RecursorLua4::prerpz(DNSQuestion
& dq
, int& ret
) const
410 return genhook(d_prerpz
, dq
, ret
);
413 bool RecursorLua4::preresolve(DNSQuestion
& dq
, int& ret
) const
415 return genhook(d_preresolve
, dq
, ret
);
418 bool RecursorLua4::nxdomain(DNSQuestion
& dq
, int& ret
) const
420 return genhook(d_nxdomain
, dq
, ret
);
423 bool RecursorLua4::nodata(DNSQuestion
& dq
, int& ret
) const
425 return genhook(d_nodata
, dq
, ret
);
428 bool RecursorLua4::postresolve(DNSQuestion
& dq
, int& ret
) const
430 return genhook(d_postresolve
, dq
, ret
);
433 bool RecursorLua4::preoutquery(const ComboAddress
& ns
, const ComboAddress
& requestor
, const DNSName
& query
, const QType
& qtype
, bool isTcp
, vector
<DNSRecord
>& res
, int& ret
) const
435 bool variableAnswer
= false;
436 bool wantsRPZ
= false;
437 bool logQuery
= false;
438 RecursorLua4::DNSQuestion
dq(ns
, requestor
, query
, qtype
.getCode(), isTcp
, variableAnswer
, wantsRPZ
, logQuery
);
439 dq
.currentRecords
= &res
;
441 return genhook(d_preoutquery
, dq
, ret
);
444 bool RecursorLua4::ipfilter(const ComboAddress
& remote
, const ComboAddress
& local
, const struct dnsheader
& dh
) const
447 return d_ipfilter(remote
, local
, dh
);
448 return false; // don't block
451 unsigned int RecursorLua4::gettag(const ComboAddress
& remote
, const Netmask
& ednssubnet
, const ComboAddress
& local
, const DNSName
& qname
, uint16_t qtype
, std::unordered_set
<std::string
>* policyTags
, LuaContext::LuaObject
& data
, const EDNSOptionViewMap
& ednsOptions
, bool tcp
, std::string
& requestorId
, std::string
& deviceId
, std::string
& deviceName
, std::string
& routingTag
, const std::vector
<ProxyProtocolValue
>& proxyProtocolValues
) const
454 std::vector
<std::pair
<int, const ProxyProtocolValue
*>> proxyProtocolValuesMap
;
455 proxyProtocolValuesMap
.reserve(proxyProtocolValues
.size());
457 for (const auto& value
: proxyProtocolValues
) {
458 proxyProtocolValuesMap
.emplace_back(num
++, &value
);
461 auto ret
= d_gettag(remote
, ednssubnet
, local
, qname
, qtype
, ednsOptions
, tcp
, proxyProtocolValuesMap
);
464 const auto& tags
= std::get
<1>(ret
);
466 policyTags
->reserve(policyTags
->size() + tags
->size());
467 for (const auto& tag
: *tags
) {
468 policyTags
->insert(tag
.second
);
472 const auto dataret
= std::get
<2>(ret
);
476 const auto reqIdret
= std::get
<3>(ret
);
478 requestorId
= *reqIdret
;
480 const auto deviceIdret
= std::get
<4>(ret
);
482 deviceId
= *deviceIdret
;
485 const auto deviceNameret
= std::get
<5>(ret
);
487 deviceName
= *deviceNameret
;
490 const auto routingTarget
= std::get
<6>(ret
);
492 routingTag
= *routingTarget
;
495 return std::get
<0>(ret
);
500 struct pdns_ffi_param
503 pdns_ffi_param(const DNSName
& qname_
, uint16_t qtype_
, const ComboAddress
& local_
, const ComboAddress
& remote_
, const Netmask
& ednssubnet_
, std::unordered_set
<std::string
>& policyTags_
, std::vector
<DNSRecord
>& records_
, const EDNSOptionViewMap
& ednsOptions_
, const std::vector
<ProxyProtocolValue
>& proxyProtocolValues_
, std::string
& requestorId_
, std::string
& deviceId_
, std::string
& deviceName_
, std::string
& routingTag_
, boost::optional
<int>& rcode_
, uint32_t& ttlCap_
, bool& variable_
, bool tcp_
, bool& logQuery_
, bool& logResponse_
, bool& followCNAMERecords_
): qname(qname_
), local(local_
), remote(remote_
), ednssubnet(ednssubnet_
), policyTags(policyTags_
), records(records_
), ednsOptions(ednsOptions_
), proxyProtocolValues(proxyProtocolValues_
), requestorId(requestorId_
), deviceId(deviceId_
), deviceName(deviceName_
), routingTag(routingTag_
), rcode(rcode_
), ttlCap(ttlCap_
), variable(variable_
), logQuery(logQuery_
), logResponse(logResponse_
), followCNAMERecords(followCNAMERecords_
), qtype(qtype_
), tcp(tcp_
)
507 std::unique_ptr
<std::string
> qnameStr
{nullptr};
508 std::unique_ptr
<std::string
> localStr
{nullptr};
509 std::unique_ptr
<std::string
> remoteStr
{nullptr};
510 std::unique_ptr
<std::string
> ednssubnetStr
{nullptr};
511 std::vector
<pdns_ednsoption_t
> ednsOptionsVect
;
512 std::vector
<pdns_proxyprotocol_value_t
> proxyProtocolValuesVect
;
514 const DNSName
& qname
;
515 const ComboAddress
& local
;
516 const ComboAddress
& remote
;
517 const Netmask
& ednssubnet
;
518 std::unordered_set
<std::string
>& policyTags
;
519 std::vector
<DNSRecord
>& records
;
520 const EDNSOptionViewMap
& ednsOptions
;
521 const std::vector
<ProxyProtocolValue
>& proxyProtocolValues
;
522 std::string
& requestorId
;
523 std::string
& deviceId
;
524 std::string
& deviceName
;
525 std::string
& routingTag
;
526 boost::optional
<int>& rcode
;
531 bool& followCNAMERecords
;
538 unsigned int RecursorLua4::gettag_ffi(const ComboAddress
& remote
, const Netmask
& ednssubnet
, const ComboAddress
& local
, const DNSName
& qname
, uint16_t qtype
, std::unordered_set
<std::string
>* policyTags
, std::vector
<DNSRecord
>& records
, LuaContext::LuaObject
& data
, const EDNSOptionViewMap
& ednsOptions
, bool tcp
, const std::vector
<ProxyProtocolValue
>& proxyProtocolValues
, std::string
& requestorId
, std::string
& deviceId
, std::string
& deviceName
, std::string
& routingTag
, boost::optional
<int>& rcode
, uint32_t& ttlCap
, bool& variable
, bool& logQuery
, bool& logResponse
, bool& followCNAMERecords
) const
541 pdns_ffi_param_t
param(qname
, qtype
, local
, remote
, ednssubnet
, *policyTags
, records
, ednsOptions
, proxyProtocolValues
, requestorId
, deviceId
, deviceName
, routingTag
, rcode
, ttlCap
, variable
, tcp
, logQuery
, logResponse
, followCNAMERecords
);
543 auto ret
= d_gettag_ffi(¶m
);
553 bool RecursorLua4::genhook(const luacall_t
& func
, DNSQuestion
& dq
, int& ret
) const
558 if (dq
.currentRecords
) {
559 dq
.records
= *dq
.currentRecords
;
564 dq
.followupFunction
.clear();
565 dq
.followupPrefix
.clear();
566 dq
.followupName
.clear();
568 dq
.udpAnswer
.clear();
569 dq
.udpCallback
.clear();
572 bool handled
=func(&dq
);
578 if(!dq
.followupFunction
.empty()) {
579 if(dq
.followupFunction
=="followCNAMERecords") {
580 ret
= followCNAMERecords(dq
.records
, QType(dq
.qtype
));
582 else if(dq
.followupFunction
=="getFakeAAAARecords") {
583 ret
=getFakeAAAARecords(dq
.followupName
, ComboAddress(dq
.followupPrefix
), dq
.records
);
585 else if(dq
.followupFunction
=="getFakePTRRecords") {
586 ret
=getFakePTRRecords(dq
.followupName
, dq
.records
);
588 else if(dq
.followupFunction
=="udpQueryResponse") {
589 dq
.udpAnswer
= GenUDPQueryResponse(dq
.udpQueryDest
, dq
.udpQuery
);
590 auto cbFunc
= d_lw
->readVariable
<boost::optional
<luacall_t
>>(dq
.udpCallback
).get_value_or(0);
592 g_log
<<Logger::Error
<<"Attempted callback for Lua UDP Query/Response which could not be found"<<endl
;
595 bool result
=cbFunc(&dq
);
602 if (dq
.currentRecords
) {
603 *dq
.currentRecords
= dq
.records
;
607 // see if they added followup work for us too
611 RecursorLua4::~RecursorLua4(){}
613 const char* pdns_ffi_param_get_qname(pdns_ffi_param_t
* ref
)
615 if (!ref
->qnameStr
) {
616 ref
->qnameStr
= std::unique_ptr
<std::string
>(new std::string(ref
->qname
.toStringNoDot()));
619 return ref
->qnameStr
->c_str();
622 void pdns_ffi_param_get_qname_raw(pdns_ffi_param_t
* ref
, const char** qname
, size_t* qnameSize
)
624 const auto& storage
= ref
->qname
.getStorage();
625 *qname
= storage
.data();
626 *qnameSize
= storage
.size();
629 uint16_t pdns_ffi_param_get_qtype(const pdns_ffi_param_t
* ref
)
634 const char* pdns_ffi_param_get_remote(pdns_ffi_param_t
* ref
)
636 if (!ref
->remoteStr
) {
637 ref
->remoteStr
= std::unique_ptr
<std::string
>(new std::string(ref
->remote
.toString()));
640 return ref
->remoteStr
->c_str();
643 static void pdns_ffi_comboaddress_to_raw(const ComboAddress
& ca
, const void** addr
, size_t* addrSize
)
646 *addr
= &ca
.sin4
.sin_addr
.s_addr
;
647 *addrSize
= sizeof(ca
.sin4
.sin_addr
.s_addr
);
650 *addr
= &ca
.sin6
.sin6_addr
.s6_addr
;
651 *addrSize
= sizeof(ca
.sin6
.sin6_addr
.s6_addr
);
655 void pdns_ffi_param_get_remote_raw(pdns_ffi_param_t
* ref
, const void** addr
, size_t* addrSize
)
657 pdns_ffi_comboaddress_to_raw(ref
->remote
, addr
, addrSize
);
660 uint16_t pdns_ffi_param_get_remote_port(const pdns_ffi_param_t
* ref
)
662 return ref
->remote
.getPort();
665 const char* pdns_ffi_param_get_local(pdns_ffi_param_t
* ref
)
667 if (!ref
->localStr
) {
668 ref
->localStr
= std::unique_ptr
<std::string
>(new std::string(ref
->local
.toString()));
671 return ref
->localStr
->c_str();
674 void pdns_ffi_param_get_local_raw(pdns_ffi_param_t
* ref
, const void** addr
, size_t* addrSize
)
676 pdns_ffi_comboaddress_to_raw(ref
->local
, addr
, addrSize
);
679 uint16_t pdns_ffi_param_get_local_port(const pdns_ffi_param_t
* ref
)
681 return ref
->local
.getPort();
684 const char* pdns_ffi_param_get_edns_cs(pdns_ffi_param_t
* ref
)
686 if (ref
->ednssubnet
.empty()) {
690 if (!ref
->ednssubnetStr
) {
691 ref
->ednssubnetStr
= std::unique_ptr
<std::string
>(new std::string(ref
->ednssubnet
.toStringNoMask()));
694 return ref
->ednssubnetStr
->c_str();
697 void pdns_ffi_param_get_edns_cs_raw(pdns_ffi_param_t
* ref
, const void** net
, size_t* netSize
)
699 if (ref
->ednssubnet
.empty()) {
705 pdns_ffi_comboaddress_to_raw(ref
->ednssubnet
.getNetwork(), net
, netSize
);
708 uint8_t pdns_ffi_param_get_edns_cs_source_mask(const pdns_ffi_param_t
* ref
)
710 return ref
->ednssubnet
.getBits();
713 static void fill_edns_option(const EDNSOptionViewValue
& value
, pdns_ednsoption_t
& option
)
715 option
.len
= value
.size
;
716 option
.data
= nullptr;
718 if (value
.size
> 0) {
719 option
.data
= value
.content
;
723 size_t pdns_ffi_param_get_edns_options(pdns_ffi_param_t
* ref
, const pdns_ednsoption_t
** out
)
725 if (ref
->ednsOptions
.empty()) {
729 size_t totalCount
= 0;
730 for (const auto& option
: ref
->ednsOptions
) {
731 totalCount
+= option
.second
.values
.size();
734 ref
->ednsOptionsVect
.resize(totalCount
);
737 for (const auto& option
: ref
->ednsOptions
) {
738 for (const auto& entry
: option
.second
.values
) {
739 fill_edns_option(entry
, ref
->ednsOptionsVect
.at(pos
));
740 ref
->ednsOptionsVect
.at(pos
).optionCode
= option
.first
;
745 *out
= ref
->ednsOptionsVect
.data();
750 size_t pdns_ffi_param_get_edns_options_by_code(pdns_ffi_param_t
* ref
, uint16_t optionCode
, const pdns_ednsoption_t
** out
)
752 const auto& it
= ref
->ednsOptions
.find(optionCode
);
753 if (it
== ref
->ednsOptions
.cend() || it
->second
.values
.empty()) {
757 ref
->ednsOptionsVect
.resize(it
->second
.values
.size());
760 for (const auto& entry
: it
->second
.values
) {
761 fill_edns_option(entry
, ref
->ednsOptionsVect
.at(pos
));
762 ref
->ednsOptionsVect
.at(pos
).optionCode
= optionCode
;
766 *out
= ref
->ednsOptionsVect
.data();
771 size_t pdns_ffi_param_get_proxy_protocol_values(pdns_ffi_param_t
* ref
, const pdns_proxyprotocol_value_t
** out
)
773 if (ref
->proxyProtocolValues
.empty()) {
777 ref
->proxyProtocolValuesVect
.resize(ref
->proxyProtocolValues
.size());
780 for (const auto& value
: ref
->proxyProtocolValues
) {
781 auto& dest
= ref
->proxyProtocolValuesVect
.at(pos
);
782 dest
.type
= value
.type
;
783 dest
.len
= value
.content
.size();
785 dest
.data
= value
.content
.data();
790 *out
= ref
->proxyProtocolValuesVect
.data();
792 return ref
->proxyProtocolValuesVect
.size();
795 void pdns_ffi_param_set_tag(pdns_ffi_param_t
* ref
, unsigned int tag
)
800 void pdns_ffi_param_add_policytag(pdns_ffi_param_t
*ref
, const char* name
)
802 ref
->policyTags
.insert(std::string(name
));
805 void pdns_ffi_param_set_requestorid(pdns_ffi_param_t
* ref
, const char* name
)
807 ref
->requestorId
= std::string(name
);
810 void pdns_ffi_param_set_devicename(pdns_ffi_param_t
* ref
, const char* name
)
812 ref
->deviceName
= std::string(name
);
815 void pdns_ffi_param_set_deviceid(pdns_ffi_param_t
* ref
, size_t len
, const void* name
)
817 ref
->deviceId
= std::string(reinterpret_cast<const char*>(name
), len
);
820 void pdns_ffi_param_set_routingtag(pdns_ffi_param_t
* ref
, const char* rtag
)
822 ref
->routingTag
= std::string(rtag
);
825 void pdns_ffi_param_set_variable(pdns_ffi_param_t
* ref
, bool variable
)
827 ref
->variable
= variable
;
830 void pdns_ffi_param_set_ttl_cap(pdns_ffi_param_t
* ref
, uint32_t ttl
)
835 void pdns_ffi_param_set_log_query(pdns_ffi_param_t
* ref
, bool logQuery
)
837 ref
->logQuery
= logQuery
;
840 void pdns_ffi_param_set_log_response(pdns_ffi_param_t
* ref
, bool logResponse
)
842 ref
->logResponse
= logResponse
;
845 void pdns_ffi_param_set_rcode(pdns_ffi_param_t
* ref
, int rcode
)
850 void pdns_ffi_param_set_follow_cname_records(pdns_ffi_param_t
* ref
, bool follow
)
852 ref
->followCNAMERecords
= follow
;
855 bool pdns_ffi_param_add_record(pdns_ffi_param_t
*ref
, const char* name
, uint16_t type
, uint32_t ttl
, const char* content
, size_t contentSize
, pdns_record_place_t place
)
859 dr
.d_name
= name
!= nullptr ? DNSName(name
) : ref
->qname
;
862 dr
.d_class
= QClass::IN
;
863 dr
.d_place
= DNSResourceRecord::Place(place
);
864 dr
.d_content
= DNSRecordContent::mastermake(type
, QClass::IN
, std::string(content
, contentSize
));
865 ref
->records
.push_back(std::move(dr
));
869 catch (const std::exception
& e
) {
870 g_log
<<Logger::Error
<<"Error attempting to add a record from Lua via pdns_ffi_param_add_record(): "<<e
.what()<<endl
;