Merge pull request #2244 from zerotier/lel-amri-fix-mac-handling-in-rules-parser

Fix mac handling in rules parser
This commit is contained in:
Adam Ierymenko 2024-09-11 14:56:10 -04:00 committed by GitHub
commit 98e532de20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View File

@ -315,12 +315,14 @@ static bool _parseRule(json &r,ZT_VirtualNetworkRule &rule)
return true;
} else if (t == "MATCH_MAC_SOURCE") {
rule.t |= ZT_NETWORK_RULE_MATCH_MAC_SOURCE;
const std::string mac(OSUtils::jsonString(r["mac"],"0"));
std::string mac(OSUtils::jsonString(r["mac"],"0"));
Utils::cleanMac(mac);
Utils::unhex(mac.c_str(),(unsigned int)mac.length(),rule.v.mac,6);
return true;
} else if (t == "MATCH_MAC_DEST") {
rule.t |= ZT_NETWORK_RULE_MATCH_MAC_DEST;
const std::string mac(OSUtils::jsonString(r["mac"],"0"));
std::string mac(OSUtils::jsonString(r["mac"],"0"));
Utils::cleanMac(mac);
Utils::unhex(mac.c_str(),(unsigned int)mac.length(),rule.v.mac,6);
return true;
} else if (t == "MATCH_IPV4_SOURCE") {

View File

@ -24,6 +24,7 @@
#include <stdexcept>
#include <vector>
#include <map>
#include <algorithm>
#if defined(__FreeBSD__)
#include <sys/endian.h>
@ -849,6 +850,19 @@ public:
* Hexadecimal characters 0-f
*/
static const char HEXCHARS[16];
/*
* Remove `-` and `:` from a MAC address (in-place).
*
* @param mac The MAC address
*/
static inline void cleanMac(std::string& mac)
{
auto start = mac.begin();
auto end = mac.end();
auto new_end = std::remove_if(start, end, [](char c) { return c == 45 || c == 58; });
mac.erase(new_end, end);
}
};
} // namespace ZeroTier