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.
23 #include "dnsdist-ecs.hh"
24 #include "dnsdist-lua.hh"
25 #include "dnsdist-protobuf.hh"
29 #include "ednsoptions.hh"
30 #include "fstrm_logger.hh"
31 #include "remote_logger.hh"
33 class DropAction
: public DNSAction
36 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
40 string
toString() const override
46 class AllowAction
: public DNSAction
49 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
53 string
toString() const override
60 class QPSAction
: public DNSAction
63 QPSAction(int limit
) : d_qps(limit
, limit
)
65 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
72 string
toString() const override
74 return "qps limit to "+std::to_string(d_qps
.getRate());
80 class DelayAction
: public DNSAction
83 DelayAction(int msec
) : d_msec(msec
)
85 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
87 *ruleresult
=std::to_string(d_msec
);
90 string
toString() const override
92 return "delay by "+std::to_string(d_msec
)+ " msec";
99 class TeeAction
: public DNSAction
102 TeeAction(const ComboAddress
& ca
, bool addECS
=false);
103 ~TeeAction() override
;
104 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
;
105 string
toString() const override
;
106 std::unordered_map
<string
, double> getStats() const override
;
109 ComboAddress d_remote
;
110 std::thread d_worker
;
114 mutable std::atomic
<unsigned long> d_senderrors
{0};
115 unsigned long d_recverrors
{0};
116 mutable std::atomic
<unsigned long> d_queries
{0};
117 unsigned long d_responses
{0};
118 unsigned long d_nxdomains
{0};
119 unsigned long d_servfails
{0};
120 unsigned long d_refuseds
{0};
121 unsigned long d_formerrs
{0};
122 unsigned long d_notimps
{0};
123 unsigned long d_noerrors
{0};
124 mutable unsigned long d_tcpdrops
{0};
125 unsigned long d_otherrcode
{0};
126 std::atomic
<bool> d_pleaseQuit
{false};
127 bool d_addECS
{false};
130 TeeAction::TeeAction(const ComboAddress
& ca
, bool addECS
) : d_remote(ca
), d_addECS(addECS
)
132 d_fd
=SSocket(d_remote
.sin4
.sin_family
, SOCK_DGRAM
, 0);
133 SConnect(d_fd
, d_remote
);
134 setNonBlocking(d_fd
);
135 d_worker
=std::thread(std::bind(&TeeAction::worker
, this));
138 TeeAction::~TeeAction()
146 DNSAction::Action
TeeAction::operator()(DNSQuestion
* dq
, string
* ruleresult
) const
157 uint16_t len
= dq
->len
;
158 bool ednsAdded
= false;
159 bool ecsAdded
= false;
160 query
.reserve(dq
->size
);
161 query
.assign((char*) dq
->dh
, len
);
163 if (!handleEDNSClientSubnet((char*) query
.c_str(), query
.capacity(), dq
->qname
->wirelength(), &len
, &ednsAdded
, &ecsAdded
, *dq
->remote
, dq
->ecsOverride
, dq
->ecsPrefixLength
)) {
164 return DNSAction::Action::None
;
167 res
= send(d_fd
, query
.c_str(), len
, 0);
170 res
= send(d_fd
, (char*)dq
->dh
, dq
->len
, 0);
176 return DNSAction::Action::None
;
179 string
TeeAction::toString() const
181 return "tee to "+d_remote
.toStringWithPort();
184 std::unordered_map
<string
,double> TeeAction::getStats() const
186 return {{"queries", d_queries
},
187 {"responses", d_responses
},
188 {"recv-errors", d_recverrors
},
189 {"send-errors", d_senderrors
},
190 {"noerrors", d_noerrors
},
191 {"nxdomains", d_nxdomains
},
192 {"refuseds", d_refuseds
},
193 {"servfails", d_servfails
},
194 {"other-rcode", d_otherrcode
},
195 {"tcp-drops", d_tcpdrops
}
199 void TeeAction::worker()
203 struct dnsheader
* dh
=(struct dnsheader
*)packet
;
205 res
=waitForData(d_fd
, 0, 250000);
214 res
=recv(d_fd
, packet
, sizeof(packet
), 0);
215 if(res
<= (int)sizeof(struct dnsheader
))
220 if(dh
->rcode
== RCode::NoError
)
222 else if(dh
->rcode
== RCode::ServFail
)
224 else if(dh
->rcode
== RCode::NXDomain
)
226 else if(dh
->rcode
== RCode::Refused
)
228 else if(dh
->rcode
== RCode::FormErr
)
230 else if(dh
->rcode
== RCode::NotImp
)
235 class PoolAction
: public DNSAction
238 PoolAction(const std::string
& pool
) : d_pool(pool
) {}
239 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
244 string
toString() const override
246 return "to pool "+d_pool
;
254 class QPSPoolAction
: public DNSAction
257 QPSPoolAction(unsigned int limit
, const std::string
& pool
) : d_qps(limit
, limit
), d_pool(pool
) {}
258 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
267 string
toString() const override
269 return "max " +std::to_string(d_qps
.getRate())+" to pool "+d_pool
;
277 class RCodeAction
: public DNSAction
280 RCodeAction(uint8_t rcode
) : d_rcode(rcode
) {}
281 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
283 dq
->dh
->rcode
= d_rcode
;
284 dq
->dh
->qr
= true; // for good measure
285 return Action::HeaderModify
;
287 string
toString() const override
289 return "set rcode "+std::to_string(d_rcode
);
296 class TCAction
: public DNSAction
299 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
301 return Action::Truncate
;
303 string
toString() const override
305 return "tc=1 answer";
309 DNSAction::Action
SpoofAction::operator()(DNSQuestion
* dq
, string
* ruleresult
) const
311 uint16_t qtype
= dq
->qtype
;
312 // do we even have a response?
313 if(d_cname
.empty() && !std::count_if(d_addrs
.begin(), d_addrs
.end(), [qtype
](const ComboAddress
& a
)
315 return (qtype
== QType::ANY
|| ((a
.sin4
.sin_family
== AF_INET
&& qtype
== QType::A
) ||
316 (a
.sin4
.sin_family
== AF_INET6
&& qtype
== QType::AAAA
)));
320 vector
<ComboAddress
> addrs
;
321 unsigned int totrdatalen
=0;
322 if (!d_cname
.empty()) {
323 qtype
= QType::CNAME
;
324 totrdatalen
+= d_cname
.toDNSString().size();
326 for(const auto& addr
: d_addrs
) {
327 if(qtype
!= QType::ANY
&& ((addr
.sin4
.sin_family
== AF_INET
&& qtype
!= QType::A
) ||
328 (addr
.sin4
.sin_family
== AF_INET6
&& qtype
!= QType::AAAA
))) {
331 totrdatalen
+= addr
.sin4
.sin_family
== AF_INET
? sizeof(addr
.sin4
.sin_addr
.s_addr
) : sizeof(addr
.sin6
.sin6_addr
.s6_addr
);
332 addrs
.push_back(addr
);
337 random_shuffle(addrs
.begin(), addrs
.end());
339 unsigned int consumed
=0;
340 DNSName
ignore((char*)dq
->dh
, dq
->len
, sizeof(dnsheader
), false, 0, 0, &consumed
);
342 if (dq
->size
< (sizeof(dnsheader
) + consumed
+ 4 + ((d_cname
.empty() ? 0 : 1) + addrs
.size())*12 /* recordstart */ + totrdatalen
)) {
346 dq
->len
= sizeof(dnsheader
) + consumed
+ 4; // there goes your EDNS
347 char* dest
= ((char*)dq
->dh
) + dq
->len
;
349 dq
->dh
->qr
= true; // for good measure
350 dq
->dh
->ra
= dq
->dh
->rd
; // for good measure
353 dq
->dh
->arcount
= 0; // for now, forget about your EDNS, we're marching over it
355 if(qtype
== QType::CNAME
) {
356 string wireData
= d_cname
.toDNSString(); // Note! This doesn't do compression!
357 const unsigned char recordstart
[]={0xc0, 0x0c, // compressed name
358 0, (unsigned char) qtype
,
361 0, (unsigned char)wireData
.length()};
362 static_assert(sizeof(recordstart
) == 12, "sizeof(recordstart) must be equal to 12, otherwise the above check is invalid");
364 memcpy(dest
, recordstart
, sizeof(recordstart
));
365 dest
+= sizeof(recordstart
);
366 memcpy(dest
, wireData
.c_str(), wireData
.length());
367 dq
->len
+= wireData
.length() + sizeof(recordstart
);
371 for(const auto& addr
: addrs
) {
372 unsigned char rdatalen
= addr
.sin4
.sin_family
== AF_INET
? sizeof(addr
.sin4
.sin_addr
.s_addr
) : sizeof(addr
.sin6
.sin6_addr
.s6_addr
);
373 const unsigned char recordstart
[]={0xc0, 0x0c, // compressed name
374 0, (unsigned char) (addr
.sin4
.sin_family
== AF_INET
? QType::A
: QType::AAAA
),
378 static_assert(sizeof(recordstart
) == 12, "sizeof(recordstart) must be equal to 12, otherwise the above check is invalid");
380 memcpy(dest
, recordstart
, sizeof(recordstart
));
381 dest
+= sizeof(recordstart
);
384 addr
.sin4
.sin_family
== AF_INET
? (void*)&addr
.sin4
.sin_addr
.s_addr
: (void*)&addr
.sin6
.sin6_addr
.s6_addr
,
387 dq
->len
+= rdatalen
+ sizeof(recordstart
);
392 dq
->dh
->ancount
= htons(dq
->dh
->ancount
);
394 return Action::HeaderModify
;
397 class MacAddrAction
: public DNSAction
400 MacAddrAction(uint16_t code
) : d_code(code
)
402 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
407 string mac
= getMACAddress(*dq
->remote
);
412 generateEDNSOption(d_code
, mac
, optRData
);
415 generateOptRR(optRData
, res
);
417 if ((dq
->size
- dq
->len
) < res
.length())
420 dq
->dh
->arcount
= htons(1);
421 char* dest
= ((char*)dq
->dh
) + dq
->len
;
422 memcpy(dest
, res
.c_str(), res
.length());
423 dq
->len
+= res
.length();
427 string
toString() const override
429 return "add EDNS MAC (code="+std::to_string(d_code
)+")";
435 class NoRecurseAction
: public DNSAction
438 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
443 string
toString() const override
449 class LogAction
: public DNSAction
, public boost::noncopyable
452 LogAction() : d_fp(0)
455 LogAction(const std::string
& str
, bool binary
=true, bool append
=false, bool buffered
=true) : d_fname(str
), d_binary(binary
)
460 d_fp
= fopen(str
.c_str(), "a+");
462 d_fp
= fopen(str
.c_str(), "w");
464 throw std::runtime_error("Unable to open file '"+str
+"' for logging: "+string(strerror(errno
)));
468 ~LogAction() override
473 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
476 vinfolog("Packet from %s for %s %s with id %d", dq
->remote
->toStringWithPort(), dq
->qname
->toString(), QType(dq
->qtype
).getName(), dq
->dh
->id
);
480 string out
= dq
->qname
->toDNSString();
481 fwrite(out
.c_str(), 1, out
.size(), d_fp
);
482 fwrite((void*)&dq
->qtype
, 1, 2, d_fp
);
485 fprintf(d_fp
, "Packet from %s for %s %s with id %d\n", dq
->remote
->toStringWithPort().c_str(), dq
->qname
->toString().c_str(), QType(dq
->qtype
).getName().c_str(), dq
->dh
->id
);
490 string
toString() const override
492 if (!d_fname
.empty()) {
493 return "log to " + d_fname
;
504 class DisableValidationAction
: public DNSAction
507 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
512 string
toString() const override
518 class SkipCacheAction
: public DNSAction
521 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
523 dq
->skipCache
= true;
526 string
toString() const override
532 class TempFailureCacheTTLAction
: public DNSAction
535 TempFailureCacheTTLAction(uint32_t ttl
) : d_ttl(ttl
)
537 TempFailureCacheTTLAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
539 dq
->tempFailureTTL
= d_ttl
;
542 string
toString() const override
544 return "set tempfailure cache ttl to "+std::to_string(d_ttl
);
550 class ECSPrefixLengthAction
: public DNSAction
553 ECSPrefixLengthAction(uint16_t v4Length
, uint16_t v6Length
) : d_v4PrefixLength(v4Length
), d_v6PrefixLength(v6Length
)
556 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
558 dq
->ecsPrefixLength
= dq
->remote
->sin4
.sin_family
== AF_INET
? d_v4PrefixLength
: d_v6PrefixLength
;
561 string
toString() const override
563 return "set ECS prefix length to " + std::to_string(d_v4PrefixLength
) + "/" + std::to_string(d_v6PrefixLength
);
566 uint16_t d_v4PrefixLength
;
567 uint16_t d_v6PrefixLength
;
570 class ECSOverrideAction
: public DNSAction
573 ECSOverrideAction(bool ecsOverride
) : d_ecsOverride(ecsOverride
)
576 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
578 dq
->ecsOverride
= d_ecsOverride
;
581 string
toString() const override
583 return "set ECS override to " + std::to_string(d_ecsOverride
);
590 class DisableECSAction
: public DNSAction
593 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
598 string
toString() const override
600 return "disable ECS";
604 class DnstapLogAction
: public DNSAction
, public boost::noncopyable
607 DnstapLogAction(const std::string
& identity
, std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSQuestion
&, DnstapMessage
*)> > alterFunc
): d_identity(identity
), d_logger(logger
), d_alterFunc(alterFunc
)
610 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
613 DnstapMessage
message(d_identity
, dq
->remote
, dq
->local
, dq
->tcp
, reinterpret_cast<const char*>(dq
->dh
), dq
->len
, dq
->queryTime
, nullptr);
616 std::lock_guard
<std::mutex
> lock(g_luamutex
);
617 (*d_alterFunc
)(*dq
, &message
);
621 message
.serialize(data
);
622 d_logger
->queueData(data
);
623 #endif /* HAVE_PROTOBUF */
626 string
toString() const override
628 return "remote log as dnstap to " + (d_logger
? d_logger
->toString() : "");
631 std::string d_identity
;
632 std::shared_ptr
<RemoteLoggerInterface
> d_logger
;
633 boost::optional
<std::function
<void(const DNSQuestion
&, DnstapMessage
*)> > d_alterFunc
;
636 class RemoteLogAction
: public DNSAction
, public boost::noncopyable
639 RemoteLogAction(std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSQuestion
&, DNSDistProtoBufMessage
*)> > alterFunc
): d_logger(logger
), d_alterFunc(alterFunc
)
642 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
646 dq
->uniqueId
= t_uuidGenerator();
649 DNSDistProtoBufMessage
message(*dq
);
652 std::lock_guard
<std::mutex
> lock(g_luamutex
);
653 (*d_alterFunc
)(*dq
, &message
);
657 message
.serialize(data
);
658 d_logger
->queueData(data
);
659 #endif /* HAVE_PROTOBUF */
662 string
toString() const override
664 return "remote log to " + (d_logger
? d_logger
->toString() : "");
667 std::shared_ptr
<RemoteLoggerInterface
> d_logger
;
668 boost::optional
<std::function
<void(const DNSQuestion
&, DNSDistProtoBufMessage
*)> > d_alterFunc
;
671 class SNMPTrapAction
: public DNSAction
674 SNMPTrapAction(const std::string
& reason
): d_reason(reason
)
677 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
679 if (g_snmpAgent
&& g_snmpTrapsEnabled
) {
680 g_snmpAgent
->sendDNSTrap(*dq
, d_reason
);
685 string
toString() const override
687 return "send SNMP trap";
690 std::string d_reason
;
693 class TagAction
: public DNSAction
696 TagAction(const std::string tag
, const std::string value
): d_tag(tag
), d_value(value
)
699 DNSAction::Action
operator()(DNSQuestion
* dq
, string
* ruleresult
) const override
702 dq
->qTag
= std::make_shared
<QTag
>();
705 dq
->qTag
->insert({d_tag
, d_value
});
709 string
toString() const override
711 return "set tag '" + d_tag
+ "' to value '" + d_value
+ "'";
718 class DnstapLogResponseAction
: public DNSResponseAction
, public boost::noncopyable
721 DnstapLogResponseAction(const std::string
& identity
, std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSResponse
&, DnstapMessage
*)> > alterFunc
): d_identity(identity
), d_logger(logger
), d_alterFunc(alterFunc
)
724 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
729 DnstapMessage
message(d_identity
, dr
->remote
, dr
->local
, dr
->tcp
, reinterpret_cast<const char*>(dr
->dh
), dr
->len
, dr
->queryTime
, &now
);
732 std::lock_guard
<std::mutex
> lock(g_luamutex
);
733 (*d_alterFunc
)(*dr
, &message
);
737 message
.serialize(data
);
738 d_logger
->queueData(data
);
739 #endif /* HAVE_PROTOBUF */
742 string
toString() const override
744 return "log response as dnstap to " + (d_logger
? d_logger
->toString() : "");
747 std::string d_identity
;
748 std::shared_ptr
<RemoteLoggerInterface
> d_logger
;
749 boost::optional
<std::function
<void(const DNSResponse
&, DnstapMessage
*)> > d_alterFunc
;
752 class RemoteLogResponseAction
: public DNSResponseAction
, public boost::noncopyable
755 RemoteLogResponseAction(std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSResponse
&, DNSDistProtoBufMessage
*)> > alterFunc
, bool includeCNAME
): d_logger(logger
), d_alterFunc(alterFunc
), d_includeCNAME(includeCNAME
)
758 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
762 dr
->uniqueId
= t_uuidGenerator();
765 DNSDistProtoBufMessage
message(*dr
, d_includeCNAME
);
768 std::lock_guard
<std::mutex
> lock(g_luamutex
);
769 (*d_alterFunc
)(*dr
, &message
);
773 message
.serialize(data
);
774 d_logger
->queueData(data
);
775 #endif /* HAVE_PROTOBUF */
778 string
toString() const override
780 return "remote log response to " + (d_logger
? d_logger
->toString() : "");
783 std::shared_ptr
<RemoteLoggerInterface
> d_logger
;
784 boost::optional
<std::function
<void(const DNSResponse
&, DNSDistProtoBufMessage
*)> > d_alterFunc
;
788 class DropResponseAction
: public DNSResponseAction
791 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
795 string
toString() const override
801 class AllowResponseAction
: public DNSResponseAction
804 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
806 return Action::Allow
;
808 string
toString() const override
814 class DelayResponseAction
: public DNSResponseAction
817 DelayResponseAction(int msec
) : d_msec(msec
)
819 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
821 *ruleresult
=std::to_string(d_msec
);
822 return Action::Delay
;
824 string
toString() const override
826 return "delay by "+std::to_string(d_msec
)+ " msec";
832 class SNMPTrapResponseAction
: public DNSResponseAction
835 SNMPTrapResponseAction(const std::string
& reason
): d_reason(reason
)
838 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
840 if (g_snmpAgent
&& g_snmpTrapsEnabled
) {
841 g_snmpAgent
->sendDNSTrap(*dr
, d_reason
);
846 string
toString() const override
848 return "send SNMP trap";
851 std::string d_reason
;
854 class TagResponseAction
: public DNSResponseAction
857 TagResponseAction(const std::string tag
, const std::string value
): d_tag(tag
), d_value(value
)
860 DNSResponseAction::Action
operator()(DNSResponse
* dr
, string
* ruleresult
) const override
863 dr
->qTag
= std::make_shared
<QTag
>();
866 dr
->qTag
->insert({d_tag
, d_value
});
870 string
toString() const override
872 return "set tag '" + d_tag
+ "' to value '" + d_value
+ "'";
879 template<typename T
, typename ActionT
>
880 static void addAction(GlobalStateHolder
<vector
<T
> > *someRulActions
, luadnsrule_t var
, std::shared_ptr
<ActionT
> action
, boost::optional
<luaruleparams_t
> params
) {
883 boost::uuids::uuid uuid
;
884 parseRuleParams(params
, uuid
);
886 auto rule
=makeRule(var
);
887 someRulActions
->modify([rule
, action
, uuid
](vector
<T
>& rulactions
){
888 rulactions
.push_back({rule
, action
, uuid
});
892 void setupLuaActions()
894 g_lua
.writeFunction("newRuleAction", [](luadnsrule_t dnsrule
, std::shared_ptr
<DNSAction
> action
, boost::optional
<luaruleparams_t
> params
) {
895 boost::uuids::uuid uuid
;
896 parseRuleParams(params
, uuid
);
898 auto rule
=makeRule(dnsrule
);
899 DNSDistRuleAction
ra({rule
, action
, uuid
});
900 return std::make_shared
<DNSDistRuleAction
>(ra
);
903 g_lua
.writeFunction("addAction", [](luadnsrule_t var
, boost::variant
<std::shared_ptr
<DNSAction
>, std::shared_ptr
<DNSResponseAction
> > era
, boost::optional
<luaruleparams_t
> params
) {
904 if (era
.type() != typeid(std::shared_ptr
<DNSAction
>)) {
905 throw std::runtime_error("addAction() can only be called with query-related actions, not response-related ones. Are you looking for addResponseAction()?");
908 addAction(&g_rulactions
, var
, boost::get
<std::shared_ptr
<DNSAction
> >(era
), params
);
911 g_lua
.writeFunction("addLuaAction", [](luadnsrule_t var
, LuaAction::func_t func
, boost::optional
<luaruleparams_t
> params
) {
912 addAction(&g_rulactions
, var
, std::make_shared
<LuaAction
>(func
), params
);
915 g_lua
.writeFunction("addLuaResponseAction", [](luadnsrule_t var
, LuaResponseAction::func_t func
, boost::optional
<luaruleparams_t
> params
) {
916 addAction(&g_resprulactions
, var
, std::make_shared
<LuaResponseAction
>(func
), params
);
919 g_lua
.writeFunction("addResponseAction", [](luadnsrule_t var
, boost::variant
<std::shared_ptr
<DNSAction
>, std::shared_ptr
<DNSResponseAction
> > era
, boost::optional
<luaruleparams_t
> params
) {
920 if (era
.type() != typeid(std::shared_ptr
<DNSResponseAction
>)) {
921 throw std::runtime_error("addResponseAction() can only be called with response-related actions, not query-related ones. Are you looking for addAction()?");
924 addAction(&g_resprulactions
, var
, boost::get
<std::shared_ptr
<DNSResponseAction
> >(era
), params
);
927 g_lua
.writeFunction("addCacheHitResponseAction", [](luadnsrule_t var
, boost::variant
<std::shared_ptr
<DNSAction
>, std::shared_ptr
<DNSResponseAction
>> era
, boost::optional
<luaruleparams_t
> params
) {
928 if (era
.type() != typeid(std::shared_ptr
<DNSResponseAction
>)) {
929 throw std::runtime_error("addCacheHitResponseAction() can only be called with response-related actions, not query-related ones. Are you looking for addAction()?");
932 addAction(&g_cachehitresprulactions
, var
, boost::get
<std::shared_ptr
<DNSResponseAction
> >(era
), params
);
935 g_lua
.writeFunction("addSelfAnsweredResponseAction", [](luadnsrule_t var
, boost::variant
<std::shared_ptr
<DNSAction
>, std::shared_ptr
<DNSResponseAction
>> era
, boost::optional
<luaruleparams_t
> params
) {
936 if (era
.type() != typeid(std::shared_ptr
<DNSResponseAction
>)) {
937 throw std::runtime_error("addSelfAnsweredResponseAction() can only be called with response-related actions, not query-related ones. Are you looking for addAction()?");
940 addAction(&g_selfansweredresprulactions
, var
, boost::get
<std::shared_ptr
<DNSResponseAction
> >(era
), params
);
943 g_lua
.registerFunction
<void(DNSAction::*)()>("printStats", [](const DNSAction
& ta
) {
944 setLuaNoSideEffect();
945 auto stats
= ta
.getStats();
946 for(const auto& s
: stats
) {
947 g_outputBuffer
+=s
.first
+"\t";
948 if((uint64_t)s
.second
== s
.second
)
949 g_outputBuffer
+= std::to_string((uint64_t)s
.second
)+"\n";
951 g_outputBuffer
+= std::to_string(s
.second
)+"\n";
955 g_lua
.writeFunction("getAction", [](unsigned int num
) {
956 setLuaNoSideEffect();
957 boost::optional
<std::shared_ptr
<DNSAction
>> ret
;
958 auto rulactions
= g_rulactions
.getCopy();
959 if(num
< rulactions
.size())
960 ret
=rulactions
[num
].d_action
;
964 g_lua
.registerFunction("getStats", &DNSAction::getStats
);
966 g_lua
.writeFunction("LuaAction", [](LuaAction::func_t func
) {
968 return std::shared_ptr
<DNSAction
>(new LuaAction(func
));
971 g_lua
.writeFunction("NoRecurseAction", []() {
972 return std::shared_ptr
<DNSAction
>(new NoRecurseAction
);
975 g_lua
.writeFunction("MacAddrAction", [](int code
) {
976 return std::shared_ptr
<DNSAction
>(new MacAddrAction(code
));
979 g_lua
.writeFunction("PoolAction", [](const string
& a
) {
980 return std::shared_ptr
<DNSAction
>(new PoolAction(a
));
983 g_lua
.writeFunction("QPSAction", [](int limit
) {
984 return std::shared_ptr
<DNSAction
>(new QPSAction(limit
));
987 g_lua
.writeFunction("QPSPoolAction", [](int limit
, const string
& a
) {
988 return std::shared_ptr
<DNSAction
>(new QPSPoolAction(limit
, a
));
991 g_lua
.writeFunction("SpoofAction", [](boost::variant
<string
,vector
<pair
<int, string
>>> inp
, boost::optional
<string
> b
) {
992 vector
<ComboAddress
> addrs
;
993 if(auto s
= boost::get
<string
>(&inp
))
994 addrs
.push_back(ComboAddress(*s
));
996 const auto& v
= boost::get
<vector
<pair
<int,string
>>>(inp
);
997 for(const auto& a
: v
)
998 addrs
.push_back(ComboAddress(a
.second
));
1001 addrs
.push_back(ComboAddress(*b
));
1002 return std::shared_ptr
<DNSAction
>(new SpoofAction(addrs
));
1005 g_lua
.writeFunction("SpoofCNAMEAction", [](const string
& a
) {
1006 return std::shared_ptr
<DNSAction
>(new SpoofAction(a
));
1009 g_lua
.writeFunction("DropAction", []() {
1010 return std::shared_ptr
<DNSAction
>(new DropAction
);
1013 g_lua
.writeFunction("AllowAction", []() {
1014 return std::shared_ptr
<DNSAction
>(new AllowAction
);
1017 g_lua
.writeFunction("DelayAction", [](int msec
) {
1018 return std::shared_ptr
<DNSAction
>(new DelayAction(msec
));
1021 g_lua
.writeFunction("TCAction", []() {
1022 return std::shared_ptr
<DNSAction
>(new TCAction
);
1025 g_lua
.writeFunction("DisableValidationAction", []() {
1026 return std::shared_ptr
<DNSAction
>(new DisableValidationAction
);
1029 g_lua
.writeFunction("LogAction", [](const std::string
& fname
, boost::optional
<bool> binary
, boost::optional
<bool> append
, boost::optional
<bool> buffered
) {
1030 return std::shared_ptr
<DNSAction
>(new LogAction(fname
, binary
? *binary
: true, append
? *append
: false, buffered
? *buffered
: false));
1033 g_lua
.writeFunction("RCodeAction", [](uint8_t rcode
) {
1034 return std::shared_ptr
<DNSAction
>(new RCodeAction(rcode
));
1037 g_lua
.writeFunction("SkipCacheAction", []() {
1038 return std::shared_ptr
<DNSAction
>(new SkipCacheAction
);
1041 g_lua
.writeFunction("TempFailureCacheTTLAction", [](int maxTTL
) {
1042 return std::shared_ptr
<DNSAction
>(new TempFailureCacheTTLAction(maxTTL
));
1045 g_lua
.writeFunction("DropResponseAction", []() {
1046 return std::shared_ptr
<DNSResponseAction
>(new DropResponseAction
);
1049 g_lua
.writeFunction("AllowResponseAction", []() {
1050 return std::shared_ptr
<DNSResponseAction
>(new AllowResponseAction
);
1053 g_lua
.writeFunction("DelayResponseAction", [](int msec
) {
1054 return std::shared_ptr
<DNSResponseAction
>(new DelayResponseAction(msec
));
1057 g_lua
.writeFunction("LuaResponseAction", [](LuaResponseAction::func_t func
) {
1059 return std::shared_ptr
<DNSResponseAction
>(new LuaResponseAction(func
));
1062 g_lua
.writeFunction("RemoteLogAction", [](std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSQuestion
&, DNSDistProtoBufMessage
*)> > alterFunc
) {
1063 // avoids potentially-evaluated-expression warning with clang.
1064 RemoteLoggerInterface
& rl
= *logger
.get();
1065 if (typeid(rl
) != typeid(RemoteLogger
)) {
1066 // We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
1067 throw std::runtime_error(std::string("RemoteLogAction only takes RemoteLogger. For other types, please look at DnstapLogAction."));
1069 #ifdef HAVE_PROTOBUF
1070 return std::shared_ptr
<DNSAction
>(new RemoteLogAction(logger
, alterFunc
));
1072 throw std::runtime_error("Protobuf support is required to use RemoteLogAction");
1076 g_lua
.writeFunction("RemoteLogResponseAction", [](std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSResponse
&, DNSDistProtoBufMessage
*)> > alterFunc
, boost::optional
<bool> includeCNAME
) {
1077 // avoids potentially-evaluated-expression warning with clang.
1078 RemoteLoggerInterface
& rl
= *logger
.get();
1079 if (typeid(rl
) != typeid(RemoteLogger
)) {
1080 // We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
1081 throw std::runtime_error("RemoteLogResponseAction only takes RemoteLogger. For other types, please look at DnstapLogResponseAction.");
1083 #ifdef HAVE_PROTOBUF
1084 return std::shared_ptr
<DNSResponseAction
>(new RemoteLogResponseAction(logger
, alterFunc
, includeCNAME
? *includeCNAME
: false));
1086 throw std::runtime_error("Protobuf support is required to use RemoteLogResponseAction");
1090 g_lua
.writeFunction("DnstapLogAction", [](const std::string
& identity
, std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSQuestion
&, DnstapMessage
*)> > alterFunc
) {
1091 #ifdef HAVE_PROTOBUF
1092 return std::shared_ptr
<DNSAction
>(new DnstapLogAction(identity
, logger
, alterFunc
));
1094 throw std::runtime_error("Protobuf support is required to use DnstapLogAction");
1098 g_lua
.writeFunction("DnstapLogResponseAction", [](const std::string
& identity
, std::shared_ptr
<RemoteLoggerInterface
> logger
, boost::optional
<std::function
<void(const DNSResponse
&, DnstapMessage
*)> > alterFunc
) {
1099 #ifdef HAVE_PROTOBUF
1100 return std::shared_ptr
<DNSResponseAction
>(new DnstapLogResponseAction(identity
, logger
, alterFunc
));
1102 throw std::runtime_error("Protobuf support is required to use DnstapLogResponseAction");
1106 g_lua
.writeFunction("TeeAction", [](const std::string
& remote
, boost::optional
<bool> addECS
) {
1107 return std::shared_ptr
<DNSAction
>(new TeeAction(ComboAddress(remote
, 53), addECS
? *addECS
: false));
1110 g_lua
.writeFunction("ECSPrefixLengthAction", [](uint16_t v4PrefixLength
, uint16_t v6PrefixLength
) {
1111 return std::shared_ptr
<DNSAction
>(new ECSPrefixLengthAction(v4PrefixLength
, v6PrefixLength
));
1114 g_lua
.writeFunction("ECSOverrideAction", [](bool ecsOverride
) {
1115 return std::shared_ptr
<DNSAction
>(new ECSOverrideAction(ecsOverride
));
1118 g_lua
.writeFunction("DisableECSAction", []() {
1119 return std::shared_ptr
<DNSAction
>(new DisableECSAction());
1122 g_lua
.writeFunction("SNMPTrapAction", [](boost::optional
<std::string
> reason
) {
1123 #ifdef HAVE_NET_SNMP
1124 return std::shared_ptr
<DNSAction
>(new SNMPTrapAction(reason
? *reason
: ""));
1126 throw std::runtime_error("NET SNMP support is required to use SNMPTrapAction()");
1127 #endif /* HAVE_NET_SNMP */
1130 g_lua
.writeFunction("SNMPTrapResponseAction", [](boost::optional
<std::string
> reason
) {
1131 #ifdef HAVE_NET_SNMP
1132 return std::shared_ptr
<DNSResponseAction
>(new SNMPTrapResponseAction(reason
? *reason
: ""));
1134 throw std::runtime_error("NET SNMP support is required to use SNMPTrapResponseAction()");
1135 #endif /* HAVE_NET_SNMP */
1138 g_lua
.writeFunction("TagAction", [](std::string tag
, std::string value
) {
1139 return std::shared_ptr
<DNSAction
>(new TagAction(tag
, value
));
1142 g_lua
.writeFunction("TagResponseAction", [](std::string tag
, std::string value
) {
1143 return std::shared_ptr
<DNSResponseAction
>(new TagResponseAction(tag
, value
));