From: Charles-Henri Bruyand Date: Thu, 13 Jan 2022 14:58:39 +0000 (+0100) Subject: dnsdist: adds possibilty to store mac address in query rings X-Git-Tag: auth-4.7.0-alpha1~71^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F11184%2Fhead;p=thirdparty%2Fpdns.git dnsdist: adds possibilty to store mac address in query rings --- diff --git a/pdns/dnsdist-rings.hh b/pdns/dnsdist-rings.hh index 0ee4f22be9..6ee3f5d562 100644 --- a/pdns/dnsdist-rings.hh +++ b/pdns/dnsdist-rings.hh @@ -44,6 +44,10 @@ struct Rings { uint16_t qtype; // incoming protocol dnsdist::Protocol protocol; +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + char macaddress[6]; + bool hasmac{false}; +#endif }; struct Response { @@ -120,11 +124,22 @@ struct Rings { void insertQuery(const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, uint16_t size, const struct dnsheader& dh, dnsdist::Protocol protocol) { +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + char macaddress[6]; + bool hasmac{false}; + if (getMACAddress(requestor, macaddress, sizeof(macaddress)) == 0) { + hasmac = true; + } +#endif for (size_t idx = 0; idx < d_nbLockTries; idx++) { auto& shard = getOneShard(); auto lock = shard->queryRing.try_lock(); if (lock.owns_lock()) { +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol, macaddress, sizeof(macaddress), hasmac); +#else insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol); +#endif return; } if (d_keepLockingStats) { @@ -138,7 +153,11 @@ struct Rings { } auto& shard = getOneShard(); auto lock = shard->queryRing.lock(); +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol, macaddress, sizeof(macaddress), hasmac); +#else insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol); +#endif } void insertResponse(const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, unsigned int usec, unsigned int size, const struct dnsheader& dh, const ComboAddress& backend, dnsdist::Protocol protocol) @@ -201,12 +220,24 @@ private: return d_shards[getShardId()]; } +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + void insertQueryLocked(boost::circular_buffer& ring, const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, uint16_t size, const struct dnsheader& dh, dnsdist::Protocol protocol, const char* macaddress, size_t maclen, const bool hasmac) +#else void insertQueryLocked(boost::circular_buffer& ring, const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, uint16_t size, const struct dnsheader& dh, dnsdist::Protocol protocol) +#endif { if (!ring.full()) { d_nbQueryEntries++; } +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + Rings::Query query({requestor, name, when, dh, size, qtype, protocol, "", hasmac}); + if (hasmac) { + memcpy(query.macaddress, macaddress, maclen); + } + ring.push_back(std::move(query)); +#else ring.push_back({requestor, name, when, dh, size, qtype, protocol}); +#endif } void insertResponseLocked(boost::circular_buffer& ring, const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, unsigned int usec, unsigned int size, const struct dnsheader& dh, const ComboAddress& backend, dnsdist::Protocol protocol) diff --git a/pdns/dnsdistdist/test-dnsdistrings_cc.cc b/pdns/dnsdistdist/test-dnsdistrings_cc.cc index db665bb5fd..18f6d04f83 100644 --- a/pdns/dnsdistdist/test-dnsdistrings_cc.cc +++ b/pdns/dnsdistdist/test-dnsdistrings_cc.cc @@ -205,7 +205,11 @@ BOOST_AUTO_TEST_CASE(test_Rings_Threaded) { dnsdist::Protocol outgoingProtocol = dnsdist::Protocol::DoUDP; Rings rings(numberOfEntries, numberOfShards, lockAttempts, true); +#if defined(DNSDIST_RINGS_WITH_MACADDRESS) + Rings::Query query({requestor, qname, now, dh, size, qtype, protocol, "", false}); +#else Rings::Query query({requestor, qname, now, dh, size, qtype, protocol}); +#endif Rings::Response response({requestor, server, qname, now, dh, latency, size, qtype, outgoingProtocol}); std::atomic done(false); diff --git a/pdns/misc.cc b/pdns/misc.cc index c243386a35..ef02cdf14f 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1111,31 +1111,40 @@ bool setCloseOnExec(int sock) return true; } -string getMACAddress(const ComboAddress& ca) +int getMACAddress(const ComboAddress& ca, char* dest, size_t len) { - string ret; #ifdef __linux__ ifstream ifs("/proc/net/arp"); - if(!ifs) - return ret; + if (len < 6) { + return EINVAL; + } + if (!ifs) { + return EIO; + } string line; - string match=ca.toString()+' '; + string match = ca.toString() + ' '; while(getline(ifs, line)) { if(boost::starts_with(line, match)) { vector parts; stringtok(parts, line, " \n\t\r"); - if(parts.size() < 4) - return ret; - unsigned int tmp[6]; - if (sscanf(parts[3].c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5) != 6) { - return ret; + if (parts.size() < 4) + return ENOENT; + if (sscanf(parts[3].c_str(), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", dest, dest+1, dest+2, dest+3, dest+4, dest+5) != 6) { + return ENOENT; } - for(unsigned int i : tmp) - ret.append(1, (char)i); - return ret; + return 0; } } #endif + return ENOENT; +} +string getMACAddress(const ComboAddress& ca) +{ + string ret; + char tmp[6]; + if (getMACAddress(ca, tmp, sizeof(tmp)) == 0) { + ret.append(tmp, sizeof(tmp)); + } return ret; } diff --git a/pdns/misc.hh b/pdns/misc.hh index f08be7591c..e83702d2d3 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -577,6 +577,7 @@ uint64_t getCPUTimeSystem(const std::string&); uint64_t getCPUIOWait(const std::string&); uint64_t getCPUSteal(const std::string&); std::string getMACAddress(const ComboAddress& ca); +int getMACAddress(const ComboAddress& ca, char* dest, size_t len); template const T& defTer(const T& a, const T& b)