]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lua-recursor4.cc
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 followCNAMERecords(vector
<DNSRecord
>& ret
, const QType
& qtype
)
39 vector
<DNSRecord
> resolved
;
41 for(const DNSRecord
& rr
: ret
) {
42 if(rr
.d_type
== QType::CNAME
) {
43 auto rec
= getRR
<CNAMERecordContent
>(rr
);
45 target
=rec
->getTarget();
53 int rcode
=directResolve(target
, qtype
, 1, resolved
); // 1 == class
55 for(const DNSRecord
& rr
: resolved
) {
62 static int getFakeAAAARecords(const DNSName
& qname
, const std::string
& prefix
, vector
<DNSRecord
>& ret
)
64 int rcode
=directResolve(qname
, QType(QType::A
), 1, ret
);
66 ComboAddress
prefixAddress(prefix
);
68 // Remove double CNAME records
69 std::set
<DNSName
> seenCNAMEs
;
70 ret
.erase(std::remove_if(
73 [&seenCNAMEs
](DNSRecord
& rr
) {
74 if (rr
.d_type
== QType::CNAME
) {
75 auto target
= getRR
<CNAMERecordContent
>(rr
);
76 if (target
== nullptr) {
79 if (seenCNAMEs
.count(target
->getTarget()) > 0) {
80 // We've had this CNAME before, remove it
83 seenCNAMEs
.insert(target
->getTarget());
90 for(DNSRecord
& rr
: ret
)
92 if(rr
.d_type
== QType::A
&& rr
.d_place
==DNSResourceRecord::ANSWER
) {
93 if(auto rec
= getRR
<ARecordContent
>(rr
)) {
94 ComboAddress
ipv4(rec
->getCA());
96 memcpy((void*)&tmp
, &ipv4
.sin4
.sin_addr
.s_addr
, 4);
98 memcpy(((char*)&prefixAddress
.sin6
.sin6_addr
.s6_addr
)+12, &tmp
, 4);
99 rr
.d_content
= std::make_shared
<AAAARecordContent
>(prefixAddress
);
100 rr
.d_type
= QType::AAAA
;
107 // We've seen an A in the ANSWER section, so there is no need to keep any
108 // SOA in the AUTHORITY section as this is not a NODATA response.
109 ret
.erase(std::remove_if(
113 return (rr
.d_type
== QType::SOA
&& rr
.d_place
==DNSResourceRecord::AUTHORITY
);
120 static int getFakePTRRecords(const DNSName
& qname
, const std::string
& prefix
, vector
<DNSRecord
>& ret
)
122 /* qname has a reverse ordered IPv6 address, need to extract the underlying IPv4 address from it
123 and turn it into an IPv4 in-addr.arpa query */
125 vector
<string
> parts
= qname
.getRawLabels();
131 for(int n
= 0; n
< 4; ++n
) {
133 std::to_string(stoll(parts
[n
*2], 0, 16) + 16*stoll(parts
[n
*2+1], 0, 16));
134 newquery
.append(1,'.');
136 newquery
+= "in-addr.arpa.";
139 int rcode
= directResolve(DNSName(newquery
), QType(QType::PTR
), 1, ret
);
140 for(DNSRecord
& rr
: ret
)
142 if(rr
.d_type
== QType::PTR
&& rr
.d_place
==DNSResourceRecord::ANSWER
) {
150 boost::optional
<dnsheader
> RecursorLua4::DNSQuestion::getDH() const
154 return boost::optional
<dnsheader
>();
157 vector
<string
> RecursorLua4::DNSQuestion::getEDNSFlags() const
161 if (*ednsFlags
& EDNSOpts::DNSSECOK
)
167 bool RecursorLua4::DNSQuestion::getEDNSFlag(string flag
) const
170 if (flag
== "DO" && (*ednsFlags
& EDNSOpts::DNSSECOK
))
176 vector
<pair
<uint16_t, string
> > RecursorLua4::DNSQuestion::getEDNSOptions() const
181 return vector
<pair
<uint16_t,string
>>();
184 boost::optional
<string
> RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code
) const
187 for(const auto& o
: *ednsOptions
)
191 return boost::optional
<string
>();
194 boost::optional
<Netmask
> RecursorLua4::DNSQuestion::getEDNSSubnet() const
197 for(const auto& o
: *ednsOptions
) {
198 if(o
.first
==EDNSOptionCode::ECS
) {
200 if(getEDNSSubnetOptsFromString(o
.second
, &eso
))
207 return boost::optional
<Netmask
>();
211 vector
<pair
<int, DNSRecord
> > RecursorLua4::DNSQuestion::getRecords() const
213 vector
<pair
<int, DNSRecord
> > ret
;
215 for(const auto& r
: records
) {
216 ret
.push_back({num
++, r
});
220 void RecursorLua4::DNSQuestion::setRecords(const vector
<pair
<int, DNSRecord
> >& recs
)
223 for(const auto& p
: recs
) {
224 records
.push_back(p
.second
);
228 void RecursorLua4::DNSQuestion::addRecord(uint16_t type
, const std::string
& content
, DNSResourceRecord::Place place
, boost::optional
<int> ttl
, boost::optional
<string
> name
)
231 dr
.d_name
=name
? DNSName(*name
) : qname
;
232 dr
.d_ttl
=ttl
.get_value_or(3600);
235 dr
.d_content
= DNSRecordContent::mastermake(type
, 1, content
);
236 records
.push_back(dr
);
239 void RecursorLua4::DNSQuestion::addAnswer(uint16_t type
, const std::string
& content
, boost::optional
<int> ttl
, boost::optional
<string
> name
)
241 addRecord(type
, content
, DNSResourceRecord::ANSWER
, ttl
, name
);
246 std::atomic
<unsigned long>* ptr
;
247 void inc() { (*ptr
)++; }
248 void incBy(unsigned int by
) { (*ptr
)+= by
; }
249 unsigned long get() { return *ptr
; }
250 void set(unsigned long val
) { *ptr
=val
; }
253 void RecursorLua4::postPrepareContext()
255 d_lw
->registerMember
<const DNSName (DNSQuestion::*)>("qname", [](const DNSQuestion
& dq
) -> const DNSName
& { return dq
.qname
; }, [](DNSQuestion
& dq
, const DNSName
& newName
) { (void) newName
; });
256 d_lw
->registerMember
<uint16_t (DNSQuestion::*)>("qtype", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.qtype
; }, [](DNSQuestion
& dq
, uint16_t newType
) { (void) newType
; });
257 d_lw
->registerMember
<bool (DNSQuestion::*)>("isTcp", [](const DNSQuestion
& dq
) -> bool { return dq
.isTcp
; }, [](DNSQuestion
& dq
, bool newTcp
) { (void) newTcp
; });
258 d_lw
->registerMember
<const ComboAddress (DNSQuestion::*)>("localaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
& { return dq
.local
; }, [](DNSQuestion
& dq
, const ComboAddress
& newLocal
) { (void) newLocal
; });
259 d_lw
->registerMember
<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
& { return dq
.remote
; }, [](DNSQuestion
& dq
, const ComboAddress
& newRemote
) { (void) newRemote
; });
260 d_lw
->registerMember
<vState (DNSQuestion::*)>("validationState", [](const DNSQuestion
& dq
) -> vState
{ return dq
.validationState
; }, [](DNSQuestion
& dq
, vState newState
) { (void) newState
; });
262 d_lw
->registerMember
<bool (DNSQuestion::*)>("variable", [](const DNSQuestion
& dq
) -> bool { return dq
.variable
; }, [](DNSQuestion
& dq
, bool newVariable
) { dq
.variable
= newVariable
; });
263 d_lw
->registerMember
<bool (DNSQuestion::*)>("wantsRPZ", [](const DNSQuestion
& dq
) -> bool { return dq
.wantsRPZ
; }, [](DNSQuestion
& dq
, bool newWantsRPZ
) { dq
.wantsRPZ
= newWantsRPZ
; });
264 d_lw
->registerMember
<bool (DNSQuestion::*)>("logResponse", [](const DNSQuestion
& dq
) -> bool { return dq
.logResponse
; }, [](DNSQuestion
& dq
, bool newLogResponse
) { dq
.logResponse
= newLogResponse
; });
266 d_lw
->registerMember("rcode", &DNSQuestion::rcode
);
267 d_lw
->registerMember("tag", &DNSQuestion::tag
);
268 d_lw
->registerMember("requestorId", &DNSQuestion::requestorId
);
269 d_lw
->registerMember("deviceId", &DNSQuestion::deviceId
);
270 d_lw
->registerMember("deviceName", &DNSQuestion::deviceName
);
271 d_lw
->registerMember("followupFunction", &DNSQuestion::followupFunction
);
272 d_lw
->registerMember("followupPrefix", &DNSQuestion::followupPrefix
);
273 d_lw
->registerMember("followupName", &DNSQuestion::followupName
);
274 d_lw
->registerMember("data", &DNSQuestion::data
);
275 d_lw
->registerMember("udpQuery", &DNSQuestion::udpQuery
);
276 d_lw
->registerMember("udpAnswer", &DNSQuestion::udpAnswer
);
277 d_lw
->registerMember("udpQueryDest", &DNSQuestion::udpQueryDest
);
278 d_lw
->registerMember("udpCallback", &DNSQuestion::udpCallback
);
279 d_lw
->registerMember("appliedPolicy", &DNSQuestion::appliedPolicy
);
280 d_lw
->registerMember
<DNSFilterEngine::Policy
, std::string
>("policyName",
281 [](const DNSFilterEngine::Policy
& pol
) -> std::string
{
284 return std::string();
286 [](DNSFilterEngine::Policy
& pol
, const std::string
& name
) {
287 pol
.d_name
= std::make_shared
<std::string
>(name
);
289 d_lw
->registerMember("policyKind", &DNSFilterEngine::Policy::d_kind
);
290 d_lw
->registerMember("policyTTL", &DNSFilterEngine::Policy::d_ttl
);
291 d_lw
->registerMember
<DNSFilterEngine::Policy
, std::string
>("policyCustom",
292 [](const DNSFilterEngine::Policy
& pol
) -> std::string
{
294 if (pol
.d_kind
!= DNSFilterEngine::PolicyKind::Custom
) {
298 for (const auto& dr
: pol
.d_custom
) {
299 if (!result
.empty()) {
302 result
+= dr
->getZoneRepresentation();
307 [](DNSFilterEngine::Policy
& pol
, const std::string
& content
) {
308 // Only CNAMES for now, when we ever add a d_custom_type, there will be pain
309 pol
.d_custom
.clear();
310 pol
.d_custom
.push_back(DNSRecordContent::mastermake(QType::CNAME
, QClass::IN
, content
));
313 d_lw
->registerFunction("getDH", &DNSQuestion::getDH
);
314 d_lw
->registerFunction("getEDNSOptions", &DNSQuestion::getEDNSOptions
);
315 d_lw
->registerFunction("getEDNSOption", &DNSQuestion::getEDNSOption
);
316 d_lw
->registerFunction("getEDNSSubnet", &DNSQuestion::getEDNSSubnet
);
317 d_lw
->registerFunction("getEDNSFlags", &DNSQuestion::getEDNSFlags
);
318 d_lw
->registerFunction("getEDNSFlag", &DNSQuestion::getEDNSFlag
);
319 d_lw
->registerMember("name", &DNSRecord::d_name
);
320 d_lw
->registerMember("type", &DNSRecord::d_type
);
321 d_lw
->registerMember("ttl", &DNSRecord::d_ttl
);
322 d_lw
->registerMember("place", &DNSRecord::d_place
);
324 d_lw
->registerMember("size", &EDNSOptionViewValue::size
);
325 d_lw
->registerFunction
<std::string(EDNSOptionViewValue::*)()>("getContent", [](const EDNSOptionViewValue
& value
) { return std::string(value
.content
, value
.size
); });
326 d_lw
->registerFunction
<size_t(EDNSOptionView::*)()>("count", [](const EDNSOptionView
& option
) { return option
.values
.size(); });
327 d_lw
->registerFunction
<std::vector
<string
>(EDNSOptionView::*)()>("getValues", [] (const EDNSOptionView
& option
) {
328 std::vector
<string
> values
;
329 for (const auto& value
: option
.values
) {
330 values
.push_back(std::string(value
.content
, value
.size
));
335 /* pre 4.2 API compatibility, when we had only one value for a given EDNS option */
336 d_lw
->registerMember
<uint16_t(EDNSOptionView::*)>("size", [](const EDNSOptionView
& option
) -> uint16_t {
339 if (!option
.values
.empty()) {
340 result
= option
.values
.at(0).size
;
344 [](EDNSOptionView
& option
, uint16_t newSize
) { (void) newSize
; });
345 d_lw
->registerFunction
<std::string(EDNSOptionView::*)()>("getContent", [](const EDNSOptionView
& option
) {
346 if (option
.values
.empty()) {
347 return std::string();
349 return std::string(option
.values
.at(0).content
, option
.values
.at(0).size
); });
351 d_lw
->registerFunction
<string(DNSRecord::*)()>("getContent", [](const DNSRecord
& dr
) { return dr
.d_content
->getZoneRepresentation(); });
352 d_lw
->registerFunction
<boost::optional
<ComboAddress
>(DNSRecord::*)()>("getCA", [](const DNSRecord
& dr
) {
353 boost::optional
<ComboAddress
> ret
;
355 if(auto rec
= std::dynamic_pointer_cast
<ARecordContent
>(dr
.d_content
))
357 else if(auto aaaarec
= std::dynamic_pointer_cast
<AAAARecordContent
>(dr
.d_content
))
358 ret
=aaaarec
->getCA(53);
363 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
); });
364 d_lw
->registerFunction("addAnswer", &DNSQuestion::addAnswer
);
365 d_lw
->registerFunction("addRecord", &DNSQuestion::addRecord
);
366 d_lw
->registerFunction("getRecords", &DNSQuestion::getRecords
);
367 d_lw
->registerFunction("setRecords", &DNSQuestion::setRecords
);
369 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::string
&)>("addPolicyTag", [](DNSQuestion
& dq
, const std::string
& tag
) { if (dq
.policyTags
) { dq
.policyTags
->push_back(tag
); } });
370 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
) {
372 dq
.policyTags
->clear();
373 for (const auto& tag
: tags
) {
374 dq
.policyTags
->push_back(tag
.second
);
378 d_lw
->registerFunction
<std::vector
<std::pair
<int, std::string
> >(DNSQuestion::*)()>("getPolicyTags", [](const DNSQuestion
& dq
) {
379 std::vector
<std::pair
<int, std::string
> > ret
;
382 for (const auto& tag
: *dq
.policyTags
) {
383 ret
.push_back({count
++, tag
});
389 d_lw
->registerFunction
<void(DNSQuestion::*)(const std::string
&)>("discardPolicy", [](DNSQuestion
& dq
, const std::string
& policy
) {
390 if (dq
.discardedPolicies
) {
391 (*dq
.discardedPolicies
)[policy
] = true;
395 d_lw
->writeFunction("newDS", []() { return SuffixMatchNode(); });
396 d_lw
->registerFunction
<void(SuffixMatchNode::*)(boost::variant
<string
,DNSName
, vector
<pair
<unsigned int,string
> > >)>(
398 [](SuffixMatchNode
&smn
, const boost::variant
<string
,DNSName
,vector
<pair
<unsigned int,string
> > >& in
){
400 if(auto s
= boost::get
<string
>(&in
)) {
401 smn
.add(DNSName(*s
));
403 else if(auto v
= boost::get
<vector
<pair
<unsigned int, string
> > >(&in
)) {
404 for(const auto& entry
: *v
)
405 smn
.add(DNSName(entry
.second
));
408 smn
.add(boost::get
<DNSName
>(in
));
411 catch(std::exception
& e
) {
412 g_log
<<Logger::Error
<<e
.what()<<endl
;
417 d_lw
->registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName
&) const) &SuffixMatchNode::check
);
418 d_lw
->registerFunction("toString",(string (SuffixMatchNode::*)() const) &SuffixMatchNode::toString
);
420 d_pd
.push_back({"policykinds", in_t
{
421 {"NoAction", (int)DNSFilterEngine::PolicyKind::NoAction
},
422 {"Drop", (int)DNSFilterEngine::PolicyKind::Drop
},
423 {"NXDOMAIN", (int)DNSFilterEngine::PolicyKind::NXDOMAIN
},
424 {"NODATA", (int)DNSFilterEngine::PolicyKind::NODATA
},
425 {"Truncate", (int)DNSFilterEngine::PolicyKind::Truncate
},
426 {"Custom", (int)DNSFilterEngine::PolicyKind::Custom
}
429 for(const auto& n
: QType::names
)
430 d_pd
.push_back({n
.first
, n
.second
});
432 d_pd
.push_back({"validationstates", in_t
{
433 {"Indeterminate", Indeterminate
},
435 {"Insecure", Insecure
},
439 d_pd
.push_back({"now", &g_now
});
441 d_lw
->writeFunction("getMetric", [](const std::string
& str
) {
442 return DynMetric
{getDynMetric(str
)};
445 d_lw
->registerFunction("inc", &DynMetric::inc
);
446 d_lw
->registerFunction("incBy", &DynMetric::incBy
);
447 d_lw
->registerFunction("set", &DynMetric::set
);
448 d_lw
->registerFunction("get", &DynMetric::get
);
450 d_lw
->writeFunction("getStat", [](const std::string
& str
) {
452 optional
<uint64_t> value
= getStatByName(str
);
459 d_lw
->writeFunction("getRecursorThreadId", []() {
460 return getRecursorThreadId();
463 d_lw
->writeFunction("sendCustomSNMPTrap", [](const std::string
& str
) {
465 g_snmpAgent
->sendCustomTrap(str
);
469 d_lw
->writeFunction("getregisteredname", [](const DNSName
&dname
) {
470 return getRegisteredName(dname
);
474 void RecursorLua4::postLoad() {
475 d_prerpz
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("prerpz").get_value_or(0);
476 d_preresolve
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("preresolve").get_value_or(0);
477 d_nodata
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("nodata").get_value_or(0);
478 d_nxdomain
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("nxdomain").get_value_or(0);
479 d_postresolve
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("postresolve").get_value_or(0);
480 d_preoutquery
= d_lw
->readVariable
<boost::optional
<luacall_t
>>("preoutquery").get_value_or(0);
481 d_maintenance
= d_lw
->readVariable
<boost::optional
<luamaintenance_t
>>("maintenance").get_value_or(0);
483 d_ipfilter
= d_lw
->readVariable
<boost::optional
<ipfilter_t
>>("ipfilter").get_value_or(0);
484 d_gettag
= d_lw
->readVariable
<boost::optional
<gettag_t
>>("gettag").get_value_or(0);
485 d_gettag_ffi
= d_lw
->readVariable
<boost::optional
<gettag_ffi_t
>>("gettag_ffi").get_value_or(0);
488 void RecursorLua4::maintenance() const
495 bool RecursorLua4::prerpz(DNSQuestion
& dq
, int& ret
) const
497 return genhook(d_prerpz
, dq
, ret
);
500 bool RecursorLua4::preresolve(DNSQuestion
& dq
, int& ret
) const
502 return genhook(d_preresolve
, dq
, ret
);
505 bool RecursorLua4::nxdomain(DNSQuestion
& dq
, int& ret
) const
507 return genhook(d_nxdomain
, dq
, ret
);
510 bool RecursorLua4::nodata(DNSQuestion
& dq
, int& ret
) const
512 return genhook(d_nodata
, dq
, ret
);
515 bool RecursorLua4::postresolve(DNSQuestion
& dq
, int& ret
) const
517 return genhook(d_postresolve
, dq
, ret
);
520 bool RecursorLua4::preoutquery(const ComboAddress
& ns
, const ComboAddress
& requestor
, const DNSName
& query
, const QType
& qtype
, bool isTcp
, vector
<DNSRecord
>& res
, int& ret
) const
522 bool variableAnswer
= false;
523 bool wantsRPZ
= false;
524 bool logQuery
= false;
525 RecursorLua4::DNSQuestion
dq(ns
, requestor
, query
, qtype
.getCode(), isTcp
, variableAnswer
, wantsRPZ
, logQuery
);
526 dq
.currentRecords
= &res
;
528 return genhook(d_preoutquery
, dq
, ret
);
531 bool RecursorLua4::ipfilter(const ComboAddress
& remote
, const ComboAddress
& local
, const struct dnsheader
& dh
) const
534 return d_ipfilter(remote
, local
, dh
);
535 return false; // don't block
538 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
541 auto ret
= d_gettag(remote
, ednssubnet
, local
, qname
, qtype
, ednsOptions
, tcp
);
544 const auto& tags
= std::get
<1>(ret
);
546 for (const auto& tag
: *tags
) {
547 policyTags
->push_back(tag
.second
);
551 const auto dataret
= std::get
<2>(ret
);
555 const auto reqIdret
= std::get
<3>(ret
);
557 requestorId
= *reqIdret
;
559 const auto deviceIdret
= std::get
<4>(ret
);
561 deviceId
= *deviceIdret
;
564 const auto deviceNameret
= std::get
<5>(ret
);
566 deviceName
= *deviceNameret
;
568 return std::get
<0>(ret
);
573 struct pdns_ffi_param
576 pdns_ffi_param(const DNSName
& qname_
, uint16_t qtype_
, const ComboAddress
& local_
, const ComboAddress
& remote_
, const Netmask
& ednssubnet_
, std::vector
<std::string
>& policyTags_
, const EDNSOptionViewMap
& ednsOptions_
, std::string
& requestorId_
, std::string
& deviceId_
, std::string
& deviceName_
, uint32_t& ttlCap_
, bool& variable_
, bool tcp_
, bool& logQuery_
): qname(qname_
), local(local_
), remote(remote_
), ednssubnet(ednssubnet_
), policyTags(policyTags_
), ednsOptions(ednsOptions_
), requestorId(requestorId_
), deviceId(deviceId_
), deviceName(deviceName_
), ttlCap(ttlCap_
), variable(variable_
), logQuery(logQuery_
), qtype(qtype_
), tcp(tcp_
)
580 std::unique_ptr
<std::string
> qnameStr
{nullptr};
581 std::unique_ptr
<std::string
> localStr
{nullptr};
582 std::unique_ptr
<std::string
> remoteStr
{nullptr};
583 std::unique_ptr
<std::string
> ednssubnetStr
{nullptr};
584 std::vector
<pdns_ednsoption_t
> ednsOptionsVect
;
586 const DNSName
& qname
;
587 const ComboAddress
& local
;
588 const ComboAddress
& remote
;
589 const Netmask
& ednssubnet
;
590 std::vector
<std::string
>& policyTags
;
591 const EDNSOptionViewMap
& ednsOptions
;
592 std::string
& requestorId
;
593 std::string
& deviceId
;
594 std::string
& deviceName
;
604 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
, LuaContext::LuaObject
& data
, const EDNSOptionViewMap
& ednsOptions
, bool tcp
, std::string
& requestorId
, std::string
& deviceId
, std::string
& deviceName
, uint32_t& ttlCap
, bool& variable
, bool& logQuery
) const
607 pdns_ffi_param_t
param(qname
, qtype
, local
, remote
, ednssubnet
, *policyTags
, ednsOptions
, requestorId
, deviceId
, deviceName
, ttlCap
, variable
, tcp
, logQuery
);
609 auto ret
= d_gettag_ffi(¶m
);
619 bool RecursorLua4::genhook(const luacall_t
& func
, DNSQuestion
& dq
, int& ret
) const
624 if (dq
.currentRecords
) {
625 dq
.records
= *dq
.currentRecords
;
630 dq
.followupFunction
.clear();
631 dq
.followupPrefix
.clear();
632 dq
.followupName
.clear();
634 dq
.udpAnswer
.clear();
635 dq
.udpCallback
.clear();
638 bool handled
=func(&dq
);
644 if(!dq
.followupFunction
.empty()) {
645 if(dq
.followupFunction
=="followCNAMERecords") {
646 ret
= followCNAMERecords(dq
.records
, QType(dq
.qtype
));
648 else if(dq
.followupFunction
=="getFakeAAAARecords") {
649 ret
=getFakeAAAARecords(dq
.followupName
, dq
.followupPrefix
, dq
.records
);
651 else if(dq
.followupFunction
=="getFakePTRRecords") {
652 ret
=getFakePTRRecords(dq
.followupName
, dq
.followupPrefix
, dq
.records
);
654 else if(dq
.followupFunction
=="udpQueryResponse") {
655 dq
.udpAnswer
= GenUDPQueryResponse(dq
.udpQueryDest
, dq
.udpQuery
);
656 auto cbFunc
= d_lw
->readVariable
<boost::optional
<luacall_t
>>(dq
.udpCallback
).get_value_or(0);
658 g_log
<<Logger::Error
<<"Attempted callback for Lua UDP Query/Response which could not be found"<<endl
;
661 bool result
=cbFunc(&dq
);
668 if (dq
.currentRecords
) {
669 *dq
.currentRecords
= dq
.records
;
673 // see if they added followup work for us too
677 RecursorLua4::~RecursorLua4(){}
679 const char* pdns_ffi_param_get_qname(pdns_ffi_param_t
* ref
)
681 if (!ref
->qnameStr
) {
682 ref
->qnameStr
= std::unique_ptr
<std::string
>(new std::string(ref
->qname
.toStringNoDot()));
685 return ref
->qnameStr
->c_str();
688 void pdns_ffi_param_get_qname_raw(pdns_ffi_param_t
* ref
, const char** qname
, size_t* qnameSize
)
690 const auto& storage
= ref
->qname
.getStorage();
691 *qname
= storage
.data();
692 *qnameSize
= storage
.size();
695 uint16_t pdns_ffi_param_get_qtype(const pdns_ffi_param_t
* ref
)
700 const char* pdns_ffi_param_get_remote(pdns_ffi_param_t
* ref
)
702 if (!ref
->remoteStr
) {
703 ref
->remoteStr
= std::unique_ptr
<std::string
>(new std::string(ref
->remote
.toString()));
706 return ref
->remoteStr
->c_str();
709 static void pdns_ffi_comboaddress_to_raw(const ComboAddress
& ca
, const void** addr
, size_t* addrSize
)
712 *addr
= &ca
.sin4
.sin_addr
.s_addr
;
713 *addrSize
= sizeof(ca
.sin4
.sin_addr
.s_addr
);
716 *addr
= &ca
.sin6
.sin6_addr
.s6_addr
;
717 *addrSize
= sizeof(ca
.sin6
.sin6_addr
.s6_addr
);
721 void pdns_ffi_param_get_remote_raw(pdns_ffi_param_t
* ref
, const void** addr
, size_t* addrSize
)
723 pdns_ffi_comboaddress_to_raw(ref
->remote
, addr
, addrSize
);
726 uint16_t pdns_ffi_param_get_remote_port(const pdns_ffi_param_t
* ref
)
728 return ref
->remote
.getPort();
731 const char* pdns_ffi_param_get_local(pdns_ffi_param_t
* ref
)
733 if (!ref
->localStr
) {
734 ref
->localStr
= std::unique_ptr
<std::string
>(new std::string(ref
->local
.toString()));
737 return ref
->localStr
->c_str();
740 void pdns_ffi_param_get_local_raw(pdns_ffi_param_t
* ref
, const void** addr
, size_t* addrSize
)
742 pdns_ffi_comboaddress_to_raw(ref
->local
, addr
, addrSize
);
745 uint16_t pdns_ffi_param_get_local_port(const pdns_ffi_param_t
* ref
)
747 return ref
->local
.getPort();
750 const char* pdns_ffi_param_get_edns_cs(pdns_ffi_param_t
* ref
)
752 if (ref
->ednssubnet
.empty()) {
756 if (!ref
->ednssubnetStr
) {
757 ref
->ednssubnetStr
= std::unique_ptr
<std::string
>(new std::string(ref
->ednssubnet
.toStringNoMask()));
760 return ref
->ednssubnetStr
->c_str();
763 void pdns_ffi_param_get_edns_cs_raw(pdns_ffi_param_t
* ref
, const void** net
, size_t* netSize
)
765 if (ref
->ednssubnet
.empty()) {
771 pdns_ffi_comboaddress_to_raw(ref
->ednssubnet
.getNetwork(), net
, netSize
);
774 uint8_t pdns_ffi_param_get_edns_cs_source_mask(const pdns_ffi_param_t
* ref
)
776 return ref
->ednssubnet
.getBits();
779 static void fill_edns_option(const EDNSOptionViewValue
& value
, pdns_ednsoption_t
& option
)
781 option
.len
= value
.size
;
782 option
.data
= nullptr;
784 if (value
.size
> 0) {
785 option
.data
= value
.content
;
789 size_t pdns_ffi_param_get_edns_options(pdns_ffi_param_t
* ref
, const pdns_ednsoption_t
** out
)
791 if (ref
->ednsOptions
.empty()) {
795 size_t totalCount
= 0;
796 for (const auto& option
: ref
->ednsOptions
) {
797 totalCount
+= option
.second
.values
.size();
800 ref
->ednsOptionsVect
.resize(totalCount
);
803 for (const auto& option
: ref
->ednsOptions
) {
804 for (const auto& entry
: option
.second
.values
) {
805 fill_edns_option(entry
, ref
->ednsOptionsVect
.at(pos
));
806 ref
->ednsOptionsVect
.at(pos
).optionCode
= option
.first
;
811 *out
= ref
->ednsOptionsVect
.data();
816 size_t pdns_ffi_param_get_edns_options_by_code(pdns_ffi_param_t
* ref
, uint16_t optionCode
, const pdns_ednsoption_t
** out
)
818 const auto& it
= ref
->ednsOptions
.find(optionCode
);
819 if (it
== ref
->ednsOptions
.cend() || it
->second
.values
.empty()) {
823 ref
->ednsOptionsVect
.resize(it
->second
.values
.size());
826 for (const auto& entry
: it
->second
.values
) {
827 fill_edns_option(entry
, ref
->ednsOptionsVect
.at(pos
));
828 ref
->ednsOptionsVect
.at(pos
).optionCode
= optionCode
;
832 *out
= ref
->ednsOptionsVect
.data();
837 void pdns_ffi_param_set_tag(pdns_ffi_param_t
* ref
, unsigned int tag
)
842 void pdns_ffi_param_add_policytag(pdns_ffi_param_t
*ref
, const char* name
)
844 ref
->policyTags
.push_back(std::string(name
));
847 void pdns_ffi_param_set_requestorid(pdns_ffi_param_t
* ref
, const char* name
)
849 ref
->requestorId
= std::string(name
);
852 void pdns_ffi_param_set_devicename(pdns_ffi_param_t
* ref
, const char* name
)
854 ref
->deviceName
= std::string(name
);
857 void pdns_ffi_param_set_deviceid(pdns_ffi_param_t
* ref
, size_t len
, const void* name
)
859 ref
->deviceId
= std::string(reinterpret_cast<const char*>(name
), len
);
862 void pdns_ffi_param_set_variable(pdns_ffi_param_t
* ref
, bool variable
)
864 ref
->variable
= variable
;
867 void pdns_ffi_param_set_ttl_cap(pdns_ffi_param_t
* ref
, uint32_t ttl
)
872 void pdns_ffi_param_set_log_query(pdns_ffi_param_t
* ref
, bool logQuery
)
874 ref
->logQuery
= logQuery
;