]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-lua-bindings-dnsquestion.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.
23 #include "dnsdist-ecs.hh"
24 #include "dnsdist-lua.hh"
25 #include "dnsparser.hh"
27 void setupLuaBindingsDNSQuestion()
30 /* PowerDNS DNSQuestion compat */
31 g_lua
.registerMember
<const ComboAddress (DNSQuestion::*)>("localaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
{ return *dq
.local
; }, [](DNSQuestion
& dq
, const ComboAddress newLocal
) { (void) newLocal
; });
32 g_lua
.registerMember
<const DNSName (DNSQuestion::*)>("qname", [](const DNSQuestion
& dq
) -> const DNSName
{ return *dq
.qname
; }, [](DNSQuestion
& dq
, const DNSName newName
) { (void) newName
; });
33 g_lua
.registerMember
<uint16_t (DNSQuestion::*)>("qtype", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.qtype
; }, [](DNSQuestion
& dq
, uint16_t newType
) { (void) newType
; });
34 g_lua
.registerMember
<uint16_t (DNSQuestion::*)>("qclass", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.qclass
; }, [](DNSQuestion
& dq
, uint16_t newClass
) { (void) newClass
; });
35 g_lua
.registerMember
<int (DNSQuestion::*)>("rcode", [](const DNSQuestion
& dq
) -> int { return dq
.dh
->rcode
; }, [](DNSQuestion
& dq
, int newRCode
) { dq
.dh
->rcode
= newRCode
; });
36 g_lua
.registerMember
<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion
& dq
) -> const ComboAddress
{ return *dq
.remote
; }, [](DNSQuestion
& dq
, const ComboAddress newRemote
) { (void) newRemote
; });
37 /* DNSDist DNSQuestion */
38 g_lua
.registerMember("dh", &DNSQuestion::dh
);
39 g_lua
.registerMember
<uint16_t (DNSQuestion::*)>("len", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.len
; }, [](DNSQuestion
& dq
, uint16_t newlen
) { dq
.len
= newlen
; });
40 g_lua
.registerMember
<uint8_t (DNSQuestion::*)>("opcode", [](const DNSQuestion
& dq
) -> uint8_t { return dq
.dh
->opcode
; }, [](DNSQuestion
& dq
, uint8_t newOpcode
) { (void) newOpcode
; });
41 g_lua
.registerMember
<size_t (DNSQuestion::*)>("size", [](const DNSQuestion
& dq
) -> size_t { return dq
.size
; }, [](DNSQuestion
& dq
, size_t newSize
) { (void) newSize
; });
42 g_lua
.registerMember
<bool (DNSQuestion::*)>("tcp", [](const DNSQuestion
& dq
) -> bool { return dq
.tcp
; }, [](DNSQuestion
& dq
, bool newTcp
) { (void) newTcp
; });
43 g_lua
.registerMember
<bool (DNSQuestion::*)>("skipCache", [](const DNSQuestion
& dq
) -> bool { return dq
.skipCache
; }, [](DNSQuestion
& dq
, bool newSkipCache
) { dq
.skipCache
= newSkipCache
; });
44 g_lua
.registerMember
<bool (DNSQuestion::*)>("useECS", [](const DNSQuestion
& dq
) -> bool { return dq
.useECS
; }, [](DNSQuestion
& dq
, bool useECS
) { dq
.useECS
= useECS
; });
45 g_lua
.registerMember
<bool (DNSQuestion::*)>("ecsOverride", [](const DNSQuestion
& dq
) -> bool { return dq
.ecsOverride
; }, [](DNSQuestion
& dq
, bool ecsOverride
) { dq
.ecsOverride
= ecsOverride
; });
46 g_lua
.registerMember
<uint16_t (DNSQuestion::*)>("ecsPrefixLength", [](const DNSQuestion
& dq
) -> uint16_t { return dq
.ecsPrefixLength
; }, [](DNSQuestion
& dq
, uint16_t newPrefixLength
) { dq
.ecsPrefixLength
= newPrefixLength
; });
47 g_lua
.registerMember
<boost::optional
<uint32_t> (DNSQuestion::*)>("tempFailureTTL",
48 [](const DNSQuestion
& dq
) -> boost::optional
<uint32_t> {
49 return dq
.tempFailureTTL
;
51 [](DNSQuestion
& dq
, boost::optional
<uint32_t> newValue
) {
52 dq
.tempFailureTTL
= newValue
;
55 g_lua
.registerFunction
<bool(DNSQuestion::*)()>("getDO", [](const DNSQuestion
& dq
) {
56 return getEDNSZ(dq
) & EDNS_HEADER_FLAG_DO
;
59 g_lua
.registerFunction
<std::map
<uint16_t, EDNSOptionView
>(DNSQuestion::*)()>("getEDNSOptions", [](DNSQuestion
& dq
) {
60 if (dq
.ednsOptions
== nullptr) {
64 return *dq
.ednsOptions
;
66 g_lua
.registerFunction
<std::string(DNSQuestion::*)(void)>("getTrailingData", [](const DNSQuestion
& dq
) {
67 const char* message
= reinterpret_cast<const char*>(dq
.dh
);
68 const uint16_t messageLen
= getDNSPacketLength(message
, dq
.len
);
69 const std::string tail
= std::string(message
+ messageLen
, dq
.len
- messageLen
);
72 g_lua
.registerFunction
<bool(DNSQuestion::*)(std::string
)>("setTrailingData", [](DNSQuestion
& dq
, const std::string
& tail
) {
73 char* message
= reinterpret_cast<char*>(dq
.dh
);
74 const uint16_t messageLen
= getDNSPacketLength(message
, dq
.len
);
75 const uint16_t tailLen
= tail
.size();
76 if(tailLen
> (dq
.size
- messageLen
)) {
80 /* Update length and copy data from the Lua string. */
81 dq
.len
= messageLen
+ tailLen
;
83 tail
.copy(message
+ messageLen
, tailLen
);
88 g_lua
.registerFunction
<std::string(DNSQuestion::*)()>("getServerNameIndication", [](const DNSQuestion
& dq
) {
92 g_lua
.registerFunction
<void(DNSQuestion::*)(std::string
)>("sendTrap", [](const DNSQuestion
& dq
, boost::optional
<std::string
> reason
) {
94 if (g_snmpAgent
&& g_snmpTrapsEnabled
) {
95 g_snmpAgent
->sendDNSTrap(dq
, reason
? *reason
: "");
97 #endif /* HAVE_NET_SNMP */
99 g_lua
.registerFunction
<void(DNSQuestion::*)(std::string
, std::string
)>("setTag", [](DNSQuestion
& dq
, const std::string
& strLabel
, const std::string
& strValue
) {
100 if(dq
.qTag
== nullptr) {
101 dq
.qTag
= std::make_shared
<QTag
>();
103 dq
.qTag
->insert({strLabel
, strValue
});
105 g_lua
.registerFunction
<void(DNSQuestion::*)(vector
<pair
<string
, string
>>)>("setTagArray", [](DNSQuestion
& dq
, const vector
<pair
<string
, string
>>&tags
) {
107 dq
.qTag
= std::make_shared
<QTag
>();
110 for (const auto& tag
: tags
) {
111 dq
.qTag
->insert({tag
.first
, tag
.second
});
114 g_lua
.registerFunction
<string(DNSQuestion::*)(std::string
)>("getTag", [](const DNSQuestion
& dq
, const std::string
& strLabel
) {
119 std::string strValue
;
120 const auto it
= dq
.qTag
->find(strLabel
);
121 if (it
== dq
.qTag
->cend()) {
126 g_lua
.registerFunction
<QTag(DNSQuestion::*)(void)>("getTagArray", [](const DNSQuestion
& dq
) {
135 /* LuaWrapper doesn't support inheritance */
136 g_lua
.registerMember
<const ComboAddress (DNSResponse::*)>("localaddr", [](const DNSResponse
& dq
) -> const ComboAddress
{ return *dq
.local
; }, [](DNSResponse
& dq
, const ComboAddress newLocal
) { (void) newLocal
; });
137 g_lua
.registerMember
<const DNSName (DNSResponse::*)>("qname", [](const DNSResponse
& dq
) -> const DNSName
{ return *dq
.qname
; }, [](DNSResponse
& dq
, const DNSName newName
) { (void) newName
; });
138 g_lua
.registerMember
<uint16_t (DNSResponse::*)>("qtype", [](const DNSResponse
& dq
) -> uint16_t { return dq
.qtype
; }, [](DNSResponse
& dq
, uint16_t newType
) { (void) newType
; });
139 g_lua
.registerMember
<uint16_t (DNSResponse::*)>("qclass", [](const DNSResponse
& dq
) -> uint16_t { return dq
.qclass
; }, [](DNSResponse
& dq
, uint16_t newClass
) { (void) newClass
; });
140 g_lua
.registerMember
<int (DNSResponse::*)>("rcode", [](const DNSResponse
& dq
) -> int { return dq
.dh
->rcode
; }, [](DNSResponse
& dq
, int newRCode
) { dq
.dh
->rcode
= newRCode
; });
141 g_lua
.registerMember
<const ComboAddress (DNSResponse::*)>("remoteaddr", [](const DNSResponse
& dq
) -> const ComboAddress
{ return *dq
.remote
; }, [](DNSResponse
& dq
, const ComboAddress newRemote
) { (void) newRemote
; });
142 g_lua
.registerMember
<dnsheader
* (DNSResponse::*)>("dh", [](const DNSResponse
& dr
) -> dnsheader
* { return dr
.dh
; }, [](DNSResponse
& dr
, dnsheader
* newdh
) { dr
.dh
= newdh
; });
143 g_lua
.registerMember
<uint16_t (DNSResponse::*)>("len", [](const DNSResponse
& dq
) -> uint16_t { return dq
.len
; }, [](DNSResponse
& dq
, uint16_t newlen
) { dq
.len
= newlen
; });
144 g_lua
.registerMember
<uint8_t (DNSResponse::*)>("opcode", [](const DNSResponse
& dq
) -> uint8_t { return dq
.dh
->opcode
; }, [](DNSResponse
& dq
, uint8_t newOpcode
) { (void) newOpcode
; });
145 g_lua
.registerMember
<size_t (DNSResponse::*)>("size", [](const DNSResponse
& dq
) -> size_t { return dq
.size
; }, [](DNSResponse
& dq
, size_t newSize
) { (void) newSize
; });
146 g_lua
.registerMember
<bool (DNSResponse::*)>("tcp", [](const DNSResponse
& dq
) -> bool { return dq
.tcp
; }, [](DNSResponse
& dq
, bool newTcp
) { (void) newTcp
; });
147 g_lua
.registerMember
<bool (DNSResponse::*)>("skipCache", [](const DNSResponse
& dq
) -> bool { return dq
.skipCache
; }, [](DNSResponse
& dq
, bool newSkipCache
) { dq
.skipCache
= newSkipCache
; });
148 g_lua
.registerFunction
<void(DNSResponse::*)(std::function
<uint32_t(uint8_t section
, uint16_t qclass
, uint16_t qtype
, uint32_t ttl
)> editFunc
)>("editTTLs", [](const DNSResponse
& dr
, std::function
<uint32_t(uint8_t section
, uint16_t qclass
, uint16_t qtype
, uint32_t ttl
)> editFunc
) {
149 editDNSPacketTTL((char*) dr
.dh
, dr
.len
, editFunc
);
151 g_lua
.registerFunction
<std::string(DNSResponse::*)(void)>("getTrailingData", [](const DNSResponse
& dq
) {
152 const char* message
= reinterpret_cast<const char*>(dq
.dh
);
153 const uint16_t messageLen
= getDNSPacketLength(message
, dq
.len
);
154 const std::string tail
= std::string(message
+ messageLen
, dq
.len
- messageLen
);
157 g_lua
.registerFunction
<bool(DNSResponse::*)(std::string
)>("setTrailingData", [](DNSResponse
& dq
, const std::string
& tail
) {
158 char* message
= reinterpret_cast<char*>(dq
.dh
);
159 const uint16_t messageLen
= getDNSPacketLength(message
, dq
.len
);
160 const uint16_t tailLen
= tail
.size();
161 if(tailLen
> (dq
.size
- messageLen
)) {
165 /* Update length and copy data from the Lua string. */
166 dq
.len
= messageLen
+ tailLen
;
168 tail
.copy(message
+ messageLen
, tailLen
);
172 g_lua
.registerFunction
<void(DNSResponse::*)(std::string
)>("sendTrap", [](const DNSResponse
& dr
, boost::optional
<std::string
> reason
) {
174 if (g_snmpAgent
&& g_snmpTrapsEnabled
) {
175 g_snmpAgent
->sendDNSTrap(dr
, reason
? *reason
: "");
177 #endif /* HAVE_NET_SNMP */
180 #ifdef HAVE_DNS_OVER_HTTPS
181 g_lua
.registerFunction
<std::string(DNSQuestion::*)(void)>("getHTTPPath", [](const DNSQuestion
& dq
) {
182 if (dq
.du
== nullptr) {
183 return std::string();
185 return dq
.du
->getHTTPPath();
188 g_lua
.registerFunction
<std::string(DNSQuestion::*)(void)>("getHTTPQueryString", [](const DNSQuestion
& dq
) {
189 if (dq
.du
== nullptr) {
190 return std::string();
192 return dq
.du
->getHTTPQueryString();
195 g_lua
.registerFunction
<std::string(DNSQuestion::*)(void)>("getHTTPHost", [](const DNSQuestion
& dq
) {
196 if (dq
.du
== nullptr) {
197 return std::string();
199 return dq
.du
->getHTTPHost();
202 g_lua
.registerFunction
<std::string(DNSQuestion::*)(void)>("getHTTPScheme", [](const DNSQuestion
& dq
) {
203 if (dq
.du
== nullptr) {
204 return std::string();
206 return dq
.du
->getHTTPScheme();
209 g_lua
.registerFunction
<std::unordered_map
<std::string
, std::string
>(DNSQuestion::*)(void)>("getHTTPHeaders", [](const DNSQuestion
& dq
) {
210 if (dq
.du
== nullptr) {
211 return std::unordered_map
<std::string
, std::string
>();
213 return dq
.du
->getHTTPHeaders();
216 g_lua
.registerFunction
<void(DNSQuestion::*)(uint16_t statusCode
, std::string body
, std::string contentType
)>("setHTTPResponse", [](DNSQuestion
& dq
, uint16_t statusCode
, std::string body
, boost::optional
<std::string
> contentType
) {
217 if (dq
.du
== nullptr) {
220 dq
.du
->setHTTPResponse(statusCode
, body
, contentType
? *contentType
: "");
222 #endif /* HAVE_DNS_OVER_HTTPS */