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 static int getFakeAAAARecords(const DNSName
& qname
, const std::string
& prefix
, vector
<DNSRecord
>& ret
)
39 int rcode
=directResolve(qname
, QType(QType::A
), 1, ret
);
41 ComboAddress
prefixAddress(prefix
);
43 // Remove double CNAME records
44 std::set
<DNSName
> seenCNAMEs
;
45 ret
.erase(std::remove_if(
48 [&seenCNAMEs
](DNSRecord
& rr
) {
49 if (rr
.d_type
== QType::CNAME
) {
50 auto target
= getRR
<CNAMERecordContent
>(rr
);
51 if (target
== nullptr) {
54 if (seenCNAMEs
.count(target
->getTarget()) > 0) {
55 // We've had this CNAME before, remove it
58 seenCNAMEs
.insert(target
->getTarget());
65 for(DNSRecord
& rr
: ret
)
67 if(rr
.d_type
== QType::A
&& rr
.d_place
==DNSResourceRecord::ANSWER
) {
68 if(auto rec
= getRR
<ARecordContent
>(rr
)) {
69 ComboAddress
ipv4(rec
->getCA());
71 memcpy((void*)&tmp
, &ipv4
.sin4
.sin_addr
.s_addr
, 4);
73 memcpy(((char*)&prefixAddress
.sin6
.sin6_addr
.s6_addr
)+12, &tmp
, 4);
74 rr
.d_content
= std::make_shared
<AAAARecordContent
>(prefixAddress
);
75 rr
.d_type
= QType::AAAA
;
82 // We've seen an A in the ANSWER section, so there is no need to keep any
83 // SOA in the AUTHORITY section as this is not a NODATA response.
84 ret
.erase(std::remove_if(
88 return (rr
.d_type
== QType::SOA
&& rr
.d_place
==DNSResourceRecord::AUTHORITY
);
95 static int getFakePTRRecords(const DNSName
& qname
, const std::string
& prefix
, vector
<DNSRecord
>& ret
)
97 /* qname has a reverse ordered IPv6 address, need to extract the underlying IPv4 address from it
98 and turn it into an IPv4 in-addr.arpa query */
100 vector
<string
> parts
= qname
.getRawLabels();
106 for(int n
= 0; n
< 4; ++n
) {
108 std::to_string(stoll(parts
[n
*2], 0, 16) + 16*stoll(parts
[n
*2+1], 0, 16));
109 newquery
.append(1,'.');
111 newquery
+= "in-addr.arpa.";
114 int rcode
= directResolve(DNSName(newquery
), QType(QType::PTR
), 1, ret
);
115 for(DNSRecord
& rr
: ret
)
117 if(rr
.d_type
== QType::PTR
&& rr
.d_place
==DNSResourceRecord::ANSWER
) {
125 boost::optional
<dnsheader
> RecursorLua4::DNSQuestion::getDH() const
129 return boost::optional
<dnsheader
>();
132 vector
<string
> RecursorLua4::DNSQuestion::getEDNSFlags() const
136 if (*ednsFlags
& EDNSOpts::DNSSECOK
)
142 bool RecursorLua4::DNSQuestion::getEDNSFlag(string flag
) const
145 if (flag
== "DO" && (*ednsFlags
& EDNSOpts::DNSSECOK
))
151 vector
<pair
<uint16_t, string
> > RecursorLua4::DNSQuestion::getEDNSOptions() const
156 return vector
<pair
<uint16_t,string
>>();
159 boost::optional
<string
> RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code
) const
162 for(const auto& o
: *ednsOptions
)
166 return boost::optional
<string
>();
169 boost::optional
<Netmask
> RecursorLua4::DNSQuestion::getEDNSSubnet() const
172 for(const auto& o
: *ednsOptions
) {
173 if(o
.first
==EDNSOptionCode::ECS
) {
175 if(getEDNSSubnetOptsFromString(o
.second
, &eso
))
182 return boost::optional
<Netmask
>();
186 vector
<pair
<int, DNSRecord
> > RecursorLua4::DNSQuestion::getRecords() const
188 vector
<pair
<int, DNSRecord
> > ret
;
190 for(const auto& r
: records
) {
191 ret
.push_back({num
++, r
});
195 void RecursorLua4::DNSQuestion::setRecords(const vector
<pair
<int, DNSRecord
> >& recs
)
198 for(const auto& p
: recs
) {
199 records
.push_back(p
.second
);
203 void RecursorLua4::DNSQuestion::addRecord(uint16_t type
, const std::string
& content
, DNSResourceRecord::Place place
, boost::optional
<int> ttl
, boost::optional
<string
> name
)
206 dr
.d_name
=name
? DNSName(*name
) : qname
;
207 dr
.d_ttl
=ttl
.get_value_or(3600);
210 dr
.d_content
= DNSRecordContent::mastermake(type
, 1, content
);
211 records
.push_back(dr
);
214 void RecursorLua4::DNSQuestion::addAnswer(uint16_t type
, const std::string
& content
, boost::optional
<int> ttl
, boost::optional
<string
> name
)
216 addRecord(type
, content
, DNSResourceRecord::ANSWER
, ttl
, name
);
221 std::atomic
<unsigned long>* ptr
;
222 void inc() { (*ptr
)++; }
223 void incBy(unsigned int by
) { (*ptr
)+= by
; }
224 unsigned long get() { return *ptr
; }
225 void set(unsigned long val
) { *ptr
=val
; }
228 void RecursorLua4::postPrepareContext()
230 d_lw
->registerMember
<const DNSName (DNSQuestion::*)>("qname", [](const DNSQuestion
& dq
) -> const DNSName
& { return dq
.qname
; }, [](DNSQuestion
& dq
, const DNSName
& newName
) { (void) newName
; });
231 d_lw
->registerMember
<uint16_t (DNSQuestion::*)>("qtype", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.qtype
; }, [](DNSQuestion
& dq
, uint16_t newType
) { (void) newType
; });
232 d_lw
->registerMember
<bool (DNSQuestion::*)>("isTcp", [](const DNSQuestion
& dq
) -> bool { return dq
.isTcp
; }, [](DNSQuestion
& dq
, bool newTcp
) { (void) newTcp
; });
233 d_lw
->registerMember
<const ComboAddress (DNSQuestion::*)>("localaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
& { return dq
.local
; }, [](DNSQuestion
& dq
, const ComboAddress
& newLocal
) { (void) newLocal
; });
234 d_lw
->registerMember
<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
& { return dq
.remote
; }, [](DNSQuestion
& dq
, const ComboAddress
& newRemote
) { (void) newRemote
; });
235 d_lw
->registerMember
<vState (DNSQuestion::*)>("validationState", [](const DNSQuestion
& dq
) -> vState
{ return dq
.validationState
; }, [](DNSQuestion
& dq
, vState newState
) { (void) newState
; });
237 d_lw
->registerMember
<bool (DNSQuestion::*)>("variable", [](const DNSQuestion
& dq
) -> bool { return dq
.variable
; }, [](DNSQuestion
& dq
, bool newVariable
) { dq
.variable
= newVariable
; });
238 d_lw
->registerMember
<bool (DNSQuestion::*)>("wantsRPZ", [](const DNSQuestion
& dq
) -> bool { return dq
.wantsRPZ
; }, [](DNSQuestion
& dq
, bool newWantsRPZ
) { dq
.wantsRPZ
= newWantsRPZ
; });
239 d_lw
->registerMember
<bool (DNSQuestion::*)>("logResponse", [](const DNSQuestion
& dq
) -> bool { return dq
.logResponse
; }, [](DNSQuestion
& dq
, bool newLogResponse
) { dq
.logResponse
= newLogResponse
; });
241 d_lw
->registerMember("rcode", &DNSQuestion::rcode
);
242 d_lw
->registerMember("tag", &DNSQuestion::tag
);
243 d_lw
->registerMember("requestorId", &DNSQuestion::requestorId
);
244 d_lw
->registerMember("deviceId", &DNSQuestion::deviceId
);
245 d_lw
->registerMember("deviceName", &DNSQuestion::deviceName
);
246 d_lw
->registerMember("followupFunction", &DNSQuestion::followupFunction
);
247 d_lw
->registerMember("followupPrefix", &DNSQuestion::followupPrefix
);
248 d_lw
->registerMember("followupName", &DNSQuestion::followupName
);
249 d_lw
->registerMember("data", &DNSQuestion::data
);
250 d_lw
->registerMember("udpQuery", &DNSQuestion::udpQuery
);
251 d_lw
->registerMember("udpAnswer", &DNSQuestion::udpAnswer
);
252 d_lw
->registerMember("udpQueryDest", &DNSQuestion::udpQueryDest
);
253 d_lw
->registerMember("udpCallback", &DNSQuestion::udpCallback
);
254 d_lw
->registerMember("appliedPolicy", &DNSQuestion::appliedPolicy
);
255 d_lw
->registerMember
<DNSFilterEngine::Policy
, std::string
>("policyName",
256 [](const DNSFilterEngine::Policy
& pol
) -> std::string
{
259 return std::string();
261 [](DNSFilterEngine::Policy
& pol
, const std::string
& name
) {
262 pol
.d_name
= std::make_shared
<std::string
>(name
);
264 d_lw
->registerMember("policyKind", &DNSFilterEngine::Policy::d_kind
);
265 d_lw
->registerMember("policyTTL", &DNSFilterEngine::Policy::d_ttl
);
266 d_lw
->registerMember
<DNSFilterEngine::Policy
, std::string
>("policyCustom",
267 [](const DNSFilterEngine::Policy
& pol
) -> std::string
{
269 if (pol
.d_kind
!= DNSFilterEngine::PolicyKind::Custom
) {
273 for (const auto& dr
: pol
.d_custom
) {
274 if (!result
.empty()) {
277 result
+= dr
->getZoneRepresentation();
282 [](DNSFilterEngine::Policy
& pol
, const std::string
& content
) {
283 // Only CNAMES for now, when we ever add a d_custom_type, there will be pain
284 pol
.d_custom
.clear();
285 pol
.d_custom
.push_back(DNSRecordContent::mastermake(QType::CNAME
, QClass::IN
, content
));
288 d_lw
->registerFunction("getDH", &DNSQuestion::getDH
);
289 d_lw
->registerFunction("getEDNSOptions", &DNSQuestion::getEDNSOptions
);
290 d_lw
->registerFunction("getEDNSOption", &DNSQuestion::getEDNSOption
);
291 d_lw
->registerFunction("getEDNSSubnet", &DNSQuestion::getEDNSSubnet
);
292 d_lw
->registerFunction("getEDNSFlags", &DNSQuestion::getEDNSFlags
);
293 d_lw
->registerFunction("getEDNSFlag", &DNSQuestion::getEDNSFlag
);
294 d_lw
->registerMember("name", &DNSRecord::d_name
);
295 d_lw
->registerMember("type", &DNSRecord::d_type
);
296 d_lw
->registerMember("ttl", &DNSRecord::d_ttl
);
297 d_lw
->registerMember("place", &DNSRecord::d_place
);
299 d_lw
->registerMember("size", &EDNSOptionViewValue::size
);
300 d_lw
->registerFunction
<std::string(EDNSOptionViewValue::*)()>("getContent", [](const EDNSOptionViewValue
& value
) { return std::string(value
.content
, value
.size
); });
301 d_lw
->registerFunction
<size_t(EDNSOptionView::*)()>("count", [](const EDNSOptionView
& option
) { return option
.values
.size(); });
302 d_lw
->registerFunction
<std::vector
<string
>(EDNSOptionView::*)()>("getValues", [] (const EDNSOptionView
& option
) {
303 std::vector
<string
> values
;
304 for (const auto& value
: option
.values
) {
305 values
.push_back(std::string(value
.content
, value
.size
));
310 /* pre 4.2 API compatibility, when we had only one value for a given EDNS option */
311 d_lw
->registerMember
<uint16_t(EDNSOptionView::*)>("size", [](const EDNSOptionView
& option
) -> uint16_t {
314 if (!option
.values
.empty()) {
315 result
= option
.values
.at(0).size
;
319 [](EDNSOptionView
& option
, uint16_t newSize
) { (void) newSize
; });
320 d_lw
->registerFunction
<std::string(EDNSOptionView::*)()>("getContent", [](const EDNSOptionView
& option
) {
321 if (option
.values
.empty()) {
322 return std::string();
324 return std::string(option
.values
.at(0).content
, option
.values
.at(0).size
); });
326 d_lw
->registerFunction
<string(DNSRecord::*)()>("getContent", [](const DNSRecord
& dr
) { return dr
.d_content
->getZoneRepresentation(); });
327 d_lw
->registerFunction
<boost::optional
<ComboAddress
>(DNSRecord::*)()>("getCA", [](const DNSRecord
& dr
) {
328 boost::optional
<ComboAddress
> ret
;
330 if(auto rec
= std::dynamic_pointer_cast
<ARecordContent
>(dr
.d_content
))
332 else if(auto aaaarec
= std::dynamic_pointer_cast
<AAAARecordContent
>(dr
.d_content
))
333 ret
=aaaarec
->getCA(53);
338 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
); });
339 d_lw
->registerFunction("addAnswer", &DNSQuestion::addAnswer
);
340 d_lw
->registerFunction("addRecord", &DNSQuestion::addRecord
);
341 d_lw
->registerFunction("getRecords", &DNSQuestion::getRecords
);
342 d_lw
->registerFunction("setRecords", &DNSQuestion::setRecords
);
344 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::string
&)>("addPolicyTag", [](DNSQuestion
& dq
, const std::string
& tag
) { if (dq
.policyTags
) { dq
.policyTags
->push_back(tag
); } });
345 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
) {
347 dq
.policyTags
->clear();
348 for (const auto& tag
: tags
) {
349 dq
.policyTags
->push_back(tag
.second
);
353 d_lw
->registerFunction
<std::vector
<std::pair
<int, std::string
> >(DNSQuestion::*)()>("getPolicyTags", [](const DNSQuestion
& dq
) {
354 std::vector
<std::pair
<int, std::string
> > ret
;
357 for (const auto& tag
: *dq
.policyTags
) {
358 ret
.push_back({count
++, tag
});
364 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::string
&)>("discardPolicy", [](DNSQuestion
& dq
, const std::string
& policy
) {
365 if (dq
.discardedPolicies
) {
366 (*dq
.discardedPolicies
)[policy
] = true;
370 d_lw
->writeFunction("newDS", []() { return SuffixMatchNode(); });
371 d_lw
->registerFunction
<void(SuffixMatchNode::*)(boost::variant
<string
,DNSName
, vector
<pair
<unsigned int,string
> > >)>(
373 [](SuffixMatchNode
&smn
, const boost::variant
<string
,DNSName
,vector
<pair
<unsigned int,string
> > >& in
){
375 if(auto s
= boost::get
<string
>(&in
)) {
376 smn
.add(DNSName(*s
));
378 else if(auto v
= boost::get
<vector
<pair
<unsigned int, string
> > >(&in
)) {
379 for(const auto& entry
: *v
)
380 smn
.add(DNSName(entry
.second
));
383 smn
.add(boost::get
<DNSName
>(in
));
386 catch(std::exception
& e
) {
387 g_log
<<Logger::Error
<<e
.what()<<endl
;
392 d_lw
->registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName
&) const) &SuffixMatchNode::check
);
393 d_lw
->registerFunction("toString",(string (SuffixMatchNode::*)() const) &SuffixMatchNode::toString
);
395 d_pd
.push_back({"policykinds", in_t
{
396 {"NoAction", (int)DNSFilterEngine::PolicyKind::NoAction
},
397 {"Drop", (int)DNSFilterEngine::PolicyKind::Drop
},
398 {"NXDOMAIN", (int)DNSFilterEngine::PolicyKind::NXDOMAIN
},
399 {"NODATA", (int)DNSFilterEngine::PolicyKind::NODATA
},
400 {"Truncate", (int)DNSFilterEngine::PolicyKind::Truncate
},
401 {"Custom", (int)DNSFilterEngine::PolicyKind::Custom
}
404 for(const auto& n
: QType::names
)
405 d_pd
.push_back({n
.first
, n
.second
});
407 d_pd
.push_back({"validationstates", in_t
{
408 {"Indeterminate", Indeterminate
},
410 {"Insecure", Insecure
},
414 d_pd
.push_back({"now", &g_now
});
416 d_lw
->writeFunction("getMetric", [](const std::string
& str
) {
417 return DynMetric
{getDynMetric(str
)};
420 d_lw
->registerFunction("inc", &DynMetric::inc
);
421 d_lw
->registerFunction("incBy", &DynMetric::incBy
);
422 d_lw
->registerFunction("set", &DynMetric::set
);
423 d_lw
->registerFunction("get", &DynMetric::get
);
425 d_lw
->writeFunction("getStat", [](const std::string
& str
) {
427 optional
<uint64_t> value
= getStatByName(str
);
434 d_lw
->writeFunction("getRecursorThreadId", []() {
435 return getRecursorThreadId();
438 d_lw
->writeFunction("sendCustomSNMPTrap", [](const std::string
& str
) {
440 g_snmpAgent
->sendCustomTrap(str
);
444 d_lw
->writeFunction("getregisteredname", [](const DNSName
&dname
) {
445 return getRegisteredName(dname
);
449 void RecursorLua4::postLoad() {
450 d_prerpz
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("prerpz").get_value_or(0);
451 d_preresolve
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("preresolve").get_value_or(0);
452 d_nodata
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("nodata").get_value_or(0);
453 d_nxdomain
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("nxdomain").get_value_or(0);
454 d_postresolve
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("postresolve").get_value_or(0);
455 d_preoutquery
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("preoutquery").get_value_or(0);
456 d_maintenance
= d_lw
->readVariable
<boost::optional
<luamaintenance_t
>>("maintenance").get_value_or(0);
458 d_ipfilter
= d_lw
->readVariable
<boost::optional
<ipfilter_t
>>("ipfilter").get_value_or(0);
459 d_gettag
= d_lw
->readVariable
<boost::optional
<gettag_t
>>("gettag").get_value_or(0);
460 d_gettag_ffi
= d_lw
->readVariable
<boost::optional
<gettag_ffi_t
>>("gettag_ffi").get_value_or(0);
463 void RecursorLua4::maintenance() const
470 bool RecursorLua4::prerpz(DNSQuestion
& dq
, int& ret
) const
472 return genhook(d_prerpz
, dq
, ret
);
475 bool RecursorLua4::preresolve(DNSQuestion
& dq
, int& ret
) const
477 return genhook(d_preresolve
, dq
, ret
);
480 bool RecursorLua4::nxdomain(DNSQuestion
& dq
, int& ret
) const
482 return genhook(d_nxdomain
, dq
, ret
);
485 bool RecursorLua4::nodata(DNSQuestion
& dq
, int& ret
) const
487 return genhook(d_nodata
, dq
, ret
);
490 bool RecursorLua4::postresolve(DNSQuestion
& dq
, int& ret
) const
492 return genhook(d_postresolve
, dq
, ret
);
495 bool RecursorLua4::preoutquery(const ComboAddress
& ns
, const ComboAddress
& requestor
, const DNSName
& query
, const QType
& qtype
, bool isTcp
, vector
<DNSRecord
>& res
, int& ret
) const
497 bool variableAnswer
= false;
498 bool wantsRPZ
= false;
499 bool logQuery
= false;
500 RecursorLua4::DNSQuestion
dq(ns
, requestor
, query
, qtype
.getCode(), isTcp
, variableAnswer
, wantsRPZ
, logQuery
);
501 dq
.currentRecords
= &res
;
503 return genhook(d_preoutquery
, dq
, ret
);
506 bool RecursorLua4::ipfilter(const ComboAddress
& remote
, const ComboAddress
& local
, const struct dnsheader
& dh
) const
509 return d_ipfilter(remote
, local
, dh
);
510 return false; // don't block
513 unsigned int RecursorLua4::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
& ednsOptions
, bool tcp
, std::string
& requestorId
, std::string
& deviceId
, std::string
& deviceName
) const
516 auto ret
= d_gettag(remote
, ednssubnet
, local
, qname
, qtype
, ednsOptions
, tcp
);
519 const auto& tags
= std::get
<1>(ret
);
521 for (const auto& tag
: *tags
) {
522 policyTags
->push_back(tag
.second
);
526 const auto dataret
= std::get
<2>(ret
);
530 const auto reqIdret
= std::get
<3>(ret
);
532 requestorId
= *reqIdret
;
534 const auto deviceIdret
= std::get
<4>(ret
);
536 deviceId
= *deviceIdret
;
539 const auto deviceNameret
= std::get
<5>(ret
);
541 deviceName
= *deviceNameret
;
543 return std::get
<0>(ret
);
548 struct pdns_ffi_param
551 pdns_ffi_param(const DNSName
& qname_
, uint16_t qtype_
, const ComboAddress
& local_
, const ComboAddress
& remote_
, const Netmask
& ednssubnet_
, std::vector
<std::string
>& policyTags_
, std::vector
<DNSRecord
>& records_
, const EDNSOptionViewMap
& ednsOptions_
, std::string
& requestorId_
, std::string
& deviceId_
, std::string
& deviceName_
, 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_
), requestorId(requestorId_
), deviceId(deviceId_
), deviceName(deviceName_
), rcode(rcode_
), ttlCap(ttlCap_
), variable(variable_
), logQuery(logQuery_
), logResponse(logResponse_
), followCNAMERecords(followCNAMERecords_
), qtype(qtype_
), tcp(tcp_
)
555 std::unique_ptr
<std::string
> qnameStr
{nullptr};
556 std::unique_ptr
<std::string
> localStr
{nullptr};
557 std::unique_ptr
<std::string
> remoteStr
{nullptr};
558 std::unique_ptr
<std::string
> ednssubnetStr
{nullptr};
559 std::vector
<pdns_ednsoption_t
> ednsOptionsVect
;
561 const DNSName
& qname
;
562 const ComboAddress
& local
;
563 const ComboAddress
& remote
;
564 const Netmask
& ednssubnet
;
565 std::vector
<std::string
>& policyTags
;
566 std::vector
<DNSRecord
>& records
;
567 const EDNSOptionViewMap
& ednsOptions
;
568 std::string
& requestorId
;
569 std::string
& deviceId
;
570 std::string
& deviceName
;
571 boost::optional
<int>& rcode
;
576 bool& followCNAMERecords
;
583 unsigned int RecursorLua4::gettag_ffi(const ComboAddress
& remote
, const Netmask
& ednssubnet
, const ComboAddress
& local
, const DNSName
& qname
, uint16_t qtype
, std::vector
<std::string
>* policyTags
, std::vector
<DNSRecord
>& records
, LuaContext::LuaObject
& data
, const EDNSOptionViewMap
& ednsOptions
, bool tcp
, std::string
& requestorId
, std::string
& deviceId
, std::string
& deviceName
, boost::optional
<int>& rcode
, uint32_t& ttlCap
, bool& variable
, bool& logQuery
, bool& logResponse
, bool& followCNAMERecords
) const
586 pdns_ffi_param_t
param(qname
, qtype
, local
, remote
, ednssubnet
, *policyTags
, records
, ednsOptions
, requestorId
, deviceId
, deviceName
, rcode
, ttlCap
, variable
, tcp
, logQuery
, logResponse
, followCNAMERecords
);
588 auto ret
= d_gettag_ffi(¶m
);
598 bool RecursorLua4::genhook(const luacall_t
& func
, DNSQuestion
& dq
, int& ret
) const
603 if (dq
.currentRecords
) {
604 dq
.records
= *dq
.currentRecords
;
609 dq
.followupFunction
.clear();
610 dq
.followupPrefix
.clear();
611 dq
.followupName
.clear();
613 dq
.udpAnswer
.clear();
614 dq
.udpCallback
.clear();
617 bool handled
=func(&dq
);
623 if(!dq
.followupFunction
.empty()) {
624 if(dq
.followupFunction
=="followCNAMERecords") {
625 ret
= followCNAMERecords(dq
.records
, QType(dq
.qtype
));
627 else if(dq
.followupFunction
=="getFakeAAAARecords") {
628 ret
=getFakeAAAARecords(dq
.followupName
, dq
.followupPrefix
, dq
.records
);
630 else if(dq
.followupFunction
=="getFakePTRRecords") {
631 ret
=getFakePTRRecords(dq
.followupName
, dq
.followupPrefix
, dq
.records
);
633 else if(dq
.followupFunction
=="udpQueryResponse") {
634 dq
.udpAnswer
= GenUDPQueryResponse(dq
.udpQueryDest
, dq
.udpQuery
);
635 auto cbFunc
= d_lw
->readVariable
<boost::optional
<luacall_t
>>(dq
.udpCallback
).get_value_or(0);
637 g_log
<<Logger::Error
<<"Attempted callback for Lua UDP Query/Response which could not be found"<<endl
;
640 bool result
=cbFunc(&dq
);
647 if (dq
.currentRecords
) {
648 *dq
.currentRecords
= dq
.records
;
652 // see if they added followup work for us too
656 RecursorLua4::~RecursorLua4(){}
658 const char* pdns_ffi_param_get_qname(pdns_ffi_param_t
* ref
)
660 if (!ref
->qnameStr
) {
661 ref
->qnameStr
= std::unique_ptr
<std::string
>(new std::string(ref
->qname
.toStringNoDot()));
664 return ref
->qnameStr
->c_str();
667 void pdns_ffi_param_get_qname_raw(pdns_ffi_param_t
* ref
, const char** qname
, size_t* qnameSize
)
669 const auto& storage
= ref
->qname
.getStorage();
670 *qname
= storage
.data();
671 *qnameSize
= storage
.size();
674 uint16_t pdns_ffi_param_get_qtype(const pdns_ffi_param_t
* ref
)
679 const char* pdns_ffi_param_get_remote(pdns_ffi_param_t
* ref
)
681 if (!ref
->remoteStr
) {
682 ref
->remoteStr
= std::unique_ptr
<std::string
>(new std::string(ref
->remote
.toString()));
685 return ref
->remoteStr
->c_str();
688 static void pdns_ffi_comboaddress_to_raw(const ComboAddress
& ca
, const void** addr
, size_t* addrSize
)
691 *addr
= &ca
.sin4
.sin_addr
.s_addr
;
692 *addrSize
= sizeof(ca
.sin4
.sin_addr
.s_addr
);
695 *addr
= &ca
.sin6
.sin6_addr
.s6_addr
;
696 *addrSize
= sizeof(ca
.sin6
.sin6_addr
.s6_addr
);
700 void pdns_ffi_param_get_remote_raw(pdns_ffi_param_t
* ref
, const void** addr
, size_t* addrSize
)
702 pdns_ffi_comboaddress_to_raw(ref
->remote
, addr
, addrSize
);
705 uint16_t pdns_ffi_param_get_remote_port(const pdns_ffi_param_t
* ref
)
707 return ref
->remote
.getPort();
710 const char* pdns_ffi_param_get_local(pdns_ffi_param_t
* ref
)
712 if (!ref
->localStr
) {
713 ref
->localStr
= std::unique_ptr
<std::string
>(new std::string(ref
->local
.toString()));
716 return ref
->localStr
->c_str();
719 void pdns_ffi_param_get_local_raw(pdns_ffi_param_t
* ref
, const void** addr
, size_t* addrSize
)
721 pdns_ffi_comboaddress_to_raw(ref
->local
, addr
, addrSize
);
724 uint16_t pdns_ffi_param_get_local_port(const pdns_ffi_param_t
* ref
)
726 return ref
->local
.getPort();
729 const char* pdns_ffi_param_get_edns_cs(pdns_ffi_param_t
* ref
)
731 if (ref
->ednssubnet
.empty()) {
735 if (!ref
->ednssubnetStr
) {
736 ref
->ednssubnetStr
= std::unique_ptr
<std::string
>(new std::string(ref
->ednssubnet
.toStringNoMask()));
739 return ref
->ednssubnetStr
->c_str();
742 void pdns_ffi_param_get_edns_cs_raw(pdns_ffi_param_t
* ref
, const void** net
, size_t* netSize
)
744 if (ref
->ednssubnet
.empty()) {
750 pdns_ffi_comboaddress_to_raw(ref
->ednssubnet
.getNetwork(), net
, netSize
);
753 uint8_t pdns_ffi_param_get_edns_cs_source_mask(const pdns_ffi_param_t
* ref
)
755 return ref
->ednssubnet
.getBits();
758 static void fill_edns_option(const EDNSOptionViewValue
& value
, pdns_ednsoption_t
& option
)
760 option
.len
= value
.size
;
761 option
.data
= nullptr;
763 if (value
.size
> 0) {
764 option
.data
= value
.content
;
768 size_t pdns_ffi_param_get_edns_options(pdns_ffi_param_t
* ref
, const pdns_ednsoption_t
** out
)
770 if (ref
->ednsOptions
.empty()) {
774 size_t totalCount
= 0;
775 for (const auto& option
: ref
->ednsOptions
) {
776 totalCount
+= option
.second
.values
.size();
779 ref
->ednsOptionsVect
.resize(totalCount
);
782 for (const auto& option
: ref
->ednsOptions
) {
783 for (const auto& entry
: option
.second
.values
) {
784 fill_edns_option(entry
, ref
->ednsOptionsVect
.at(pos
));
785 ref
->ednsOptionsVect
.at(pos
).optionCode
= option
.first
;
790 *out
= ref
->ednsOptionsVect
.data();
795 size_t pdns_ffi_param_get_edns_options_by_code(pdns_ffi_param_t
* ref
, uint16_t optionCode
, const pdns_ednsoption_t
** out
)
797 const auto& it
= ref
->ednsOptions
.find(optionCode
);
798 if (it
== ref
->ednsOptions
.cend() || it
->second
.values
.empty()) {
802 ref
->ednsOptionsVect
.resize(it
->second
.values
.size());
805 for (const auto& entry
: it
->second
.values
) {
806 fill_edns_option(entry
, ref
->ednsOptionsVect
.at(pos
));
807 ref
->ednsOptionsVect
.at(pos
).optionCode
= optionCode
;
811 *out
= ref
->ednsOptionsVect
.data();
816 void pdns_ffi_param_set_tag(pdns_ffi_param_t
* ref
, unsigned int tag
)
821 void pdns_ffi_param_add_policytag(pdns_ffi_param_t
*ref
, const char* name
)
823 ref
->policyTags
.push_back(std::string(name
));
826 void pdns_ffi_param_set_requestorid(pdns_ffi_param_t
* ref
, const char* name
)
828 ref
->requestorId
= std::string(name
);
831 void pdns_ffi_param_set_devicename(pdns_ffi_param_t
* ref
, const char* name
)
833 ref
->deviceName
= std::string(name
);
836 void pdns_ffi_param_set_deviceid(pdns_ffi_param_t
* ref
, size_t len
, const void* name
)
838 ref
->deviceId
= std::string(reinterpret_cast<const char*>(name
), len
);
841 void pdns_ffi_param_set_variable(pdns_ffi_param_t
* ref
, bool variable
)
843 ref
->variable
= variable
;
846 void pdns_ffi_param_set_ttl_cap(pdns_ffi_param_t
* ref
, uint32_t ttl
)
851 void pdns_ffi_param_set_log_query(pdns_ffi_param_t
* ref
, bool logQuery
)
853 ref
->logQuery
= logQuery
;
856 void pdns_ffi_param_set_log_response(pdns_ffi_param_t
* ref
, bool logResponse
)
858 ref
->logResponse
= logResponse
;
861 void pdns_ffi_param_set_rcode(pdns_ffi_param_t
* ref
, int rcode
)
866 void pdns_ffi_param_set_follow_cname_records(pdns_ffi_param_t
* ref
, bool follow
)
868 ref
->followCNAMERecords
= follow
;
871 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
)
875 dr
.d_name
= name
!= nullptr ? DNSName(name
) : ref
->qname
;
878 dr
.d_class
= QClass::IN
;
879 dr
.d_place
= DNSResourceRecord::Place(place
);
880 dr
.d_content
= DNSRecordContent::mastermake(type
, QClass::IN
, std::string(content
, contentSize
));
881 ref
->records
.push_back(std::move(dr
));
885 catch (const std::exception
& e
) {
886 g_log
<<Logger::Error
<<"Error attempting to add a record from Lua via pdns_ffi_param_add_record(): "<<e
.what()<<endl
;