class TagAction : public DNSAction
{
public:
- TagAction(const std::string tag, std::string value): d_tag(tag), d_value(value)
+ TagAction(const std::string tag, const std::string value): d_tag(tag), d_value(value)
{
}
DNSAction::Action operator()(DNSQuestion* dq, string* ruleresult) const override
{
- if (dq->qTag == nullptr) {
+ if (!dq->qTag) {
dq->qTag = std::make_shared<QTag>();
}
- dq->qTag->add(d_tag, d_value);
+ dq->qTag->insert({d_tag, d_value});
return Action::None;
}
class TagResponseAction : public DNSResponseAction
{
public:
- TagResponseAction(const std::string tag, std::string value): d_tag(tag), d_value(value)
+ TagResponseAction(const std::string tag, const std::string value): d_tag(tag), d_value(value)
{
}
DNSResponseAction::Action operator()(DNSResponse* dr, string* ruleresult) const override
{
- if (dr->qTag == nullptr) {
+ if (!dr->qTag) {
dr->qTag = std::make_shared<QTag>();
}
- dr->qTag->add(d_tag, d_value);
+ dr->qTag->insert({d_tag, d_value});
return Action::None;
}
#endif /* HAVE_NET_SNMP */
});
g_lua.registerFunction<void(DNSQuestion::*)(std::string, std::string)>("setTag", [](DNSQuestion& dq, const std::string& strLabel, const std::string& strValue) {
-
if(dq.qTag == nullptr) {
dq.qTag = std::make_shared<QTag>();
}
- dq.qTag->add(strLabel, strValue);
-
+ dq.qTag->insert({strLabel, strValue});
});
g_lua.registerFunction<void(DNSQuestion::*)(vector<pair<string, string>>)>("setTagArray", [](DNSQuestion& dq, const vector<pair<string, string>>&tags) {
-
- if(dq.qTag == nullptr) {
+ if (!dq.qTag) {
dq.qTag = std::make_shared<QTag>();
}
for (const auto& tag : tags) {
- dq.qTag->add(tag.first, tag.second);
+ dq.qTag->insert({tag.first, tag.second});
}
-
});
g_lua.registerFunction<string(DNSQuestion::*)(std::string)>("getTag", [](const DNSQuestion& dq, const std::string& strLabel) {
+ if (!dq.qTag) {
+ return string();
+ }
std::string strValue;
- if(dq.qTag != nullptr) {
- strValue = dq.qTag->getMatch(strLabel);
+ const auto it = dq.qTag->find(strLabel);
+ if (it == dq.qTag->cend()) {
+ return string();
}
- return strValue;
+ return it->second;
});
- g_lua.registerFunction<std::unordered_map<string, string>(DNSQuestion::*)(void)>("getTagArray", [](const DNSQuestion& dq) {
-
- if(dq.qTag != nullptr) {
- return dq.qTag->tagData;
- } else {
- std::unordered_map<string, string> XX;
- return XX;
+ g_lua.registerFunction<QTag(DNSQuestion::*)(void)>("getTagArray", [](const DNSQuestion& dq) {
+ if (!dq.qTag) {
+ QTag empty;
+ return empty;
}
+
+ return *dq.qTag;
});
/* LuaWrapper doesn't support inheritance */
}
bool matches(const DNSQuestion* dq) const override
{
- if (dq->qTag == nullptr) {
+ if (!dq->qTag) {
return false;
}
- const auto got = dq->qTag->tagData.find(d_tag);
- if (got == dq->qTag->tagData.cend()) {
+ const auto it = dq->qTag->find(d_tag);
+ if (it == dq->qTag->cend()) {
return false;
}
return true;
}
- return got->second == *d_value;
+ return it->second == *d_value;
}
string toString() const override
extern uint16_t g_ECSSourcePrefixV6;
extern bool g_ECSOverride;
-class QTag
-{
-public:
- QTag()
- {
- }
-
- ~QTag()
- {
- }
-
- void add(const std::string& strLabel, const std::string& strValue)
- {
- tagData.insert({strLabel, strValue});
- return;
- }
-
- std::string getMatch(const std::string& strLabel) const
- {
- const auto got = tagData.find(strLabel);
- if (got == tagData.cend()) {
- return "";
- }
-
- return got->second;
- }
-
- std::string getEntry(size_t iEntry) const
- {
- std::string strEntry;
- size_t iCounter = 0;
-
- for (const auto& itr : tagData) {
- iCounter++;
- if(iCounter == iEntry) {
- strEntry = itr.first;
- strEntry += strSep;
- strEntry += itr.second;
- break;
- }
- }
-
- return strEntry;
- }
-
- size_t count() const
- {
- return tagData.size();
- }
-
- std::string dumpString() const
- {
- std::string strRet;
-
- for (const auto& itr : tagData) {
- strRet += itr.first;
- strRet += strSep;
- strRet += itr.second;
- strRet += "\n";
- }
- return strRet;
- }
-
- std::unordered_map<std::string, std::string> tagData;
-
-private:
- static constexpr char const *strSep = "\t";
-};
-
extern thread_local boost::uuids::random_generator t_uuidGenerator;
+typedef std::unordered_map<string, string> QTag;
+
struct DNSQuestion
{
DNSQuestion(const DNSName* name, uint16_t type, uint16_t class_, const ComboAddress* lc, const ComboAddress* rem, struct dnsheader* header, size_t bufferSize, uint16_t queryLen, bool isTcp, const struct timespec* queryTime_):
const uint16_t qclass;
const ComboAddress* local;
const ComboAddress* remote;
- std::shared_ptr<QTag> qTag;
+ std::shared_ptr<QTag> qTag{nullptr};
struct dnsheader* dh;
size_t size;
uint16_t len;