Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mac handling in rules parser #2244

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions controller/EmbeddedNetworkController.cpp
Expand Up @@ -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") {
Expand Down
14 changes: 14 additions & 0 deletions node/Utils.hpp
Expand Up @@ -24,6 +24,7 @@
#include <stdexcept>
#include <vector>
#include <map>
#include <algorithm>

#if defined(__FreeBSD__)
#include <sys/endian.h>
Expand Down Expand Up @@ -849,6 +850,19 @@ class Utils
* 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
Expand Down