util.cpp (da0b1d4627d7b71fe24e962b383401b4ed63c5c9) util.cpp (1c77602280a0079f998e093a19d9160f60454c6e)
1#include "util.hpp"
2
3#include "config_parser.hpp"
4#include "types.hpp"
5
6#include <arpa/inet.h>
7#include <dirent.h>
1#include "util.hpp"
2
3#include "config_parser.hpp"
4#include "types.hpp"
5
6#include <arpa/inet.h>
7#include <dirent.h>
8#include <fmt/compile.h>
9#include <fmt/format.h>
8#include <net/if.h>
9#include <sys/wait.h>
10
11#include <algorithm>
12#include <cctype>
13#include <cstdlib>
14#include <cstring>
15#include <filesystem>

--- 612 unchanged lines hidden (view full) ---

628 elog<InternalFailure>();
629 }
630
631 std::variant<std::string> value;
632 reply.read(value);
633 return fromString(std::get<std::string>(value));
634}
635
10#include <net/if.h>
11#include <sys/wait.h>
12
13#include <algorithm>
14#include <cctype>
15#include <cstdlib>
16#include <cstring>
17#include <filesystem>

--- 612 unchanged lines hidden (view full) ---

630 elog<InternalFailure>();
631 }
632
633 std::variant<std::string> value;
634 reply.read(value);
635 return fromString(std::get<std::string>(value));
636}
637
636ether_addr fromString(std::string_view str)
638ether_addr fromString(const char* str)
637{
639{
638 std::string mac_str{};
639 ether_addr mac{};
640 std::string genstr;
640
641
641 auto mac_c_str = str.data();
642
643 // MAC address without colons
642 // MAC address without colons
644 if (str.size() == 12 && str.find(":") == std::string::npos)
643 std::string_view strv = str;
644 if (strv.size() == 12 && strv.find(":") == strv.npos)
645 {
645 {
646 mac_str = std::string(str.substr(0, 2)) + ":" +
647 std::string(str.substr(2, 2)) + ":" +
648 std::string(str.substr(4, 2)) + ":" +
649 std::string(str.substr(6, 2)) + ":" +
650 std::string(str.substr(8, 2)) + ":" +
651 std::string(str.substr(10, 2));
652 mac_c_str = mac_str.c_str();
646 genstr =
647 fmt::format(FMT_COMPILE("{}:{}:{}:{}:{}:{}"), strv.substr(0, 2),
648 strv.substr(2, 2), strv.substr(4, 2), strv.substr(6, 2),
649 strv.substr(8, 2), strv.substr(10, 2));
650 str = genstr.c_str();
653 }
654
651 }
652
655 if (ether_aton_r(mac_c_str, &mac) != nullptr)
653 ether_addr addr;
654 if (ether_aton_r(str, &addr) == nullptr)
656 {
655 {
657 return mac;
656 throw std::invalid_argument("Invalid MAC Address");
658 }
657 }
659
660 throw std::invalid_argument("Invalid MAC Address");
658 return addr;
661}
662
663std::string toString(const ether_addr& mac)
664{
665 char buf[18] = {0};
666 snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac.ether_addr_octet[0],
667 mac.ether_addr_octet[1], mac.ether_addr_octet[2],
668 mac.ether_addr_octet[3], mac.ether_addr_octet[4],

--- 22 unchanged lines hidden ---
659}
660
661std::string toString(const ether_addr& mac)
662{
663 char buf[18] = {0};
664 snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac.ether_addr_octet[0],
665 mac.ether_addr_octet[1], mac.ether_addr_octet[2],
666 mac.ether_addr_octet[3], mac.ether_addr_octet[4],

--- 22 unchanged lines hidden ---