Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor data provider macOS functions
Browse files Browse the repository at this point in the history
nbertoldo committed Jan 3, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 87e44cc commit 707b0b5
Showing 19 changed files with 602 additions and 424 deletions.
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
#define kIOMainPortDefault kIOMasterPortDefault
#endif

double getMhz(IOsPrimitivesMac* osPrimitives)
int getMhz(IOsPrimitivesMac* osPrimitives)
{
constexpr auto MHz{1000000};
uint64_t cpuHz = 0;
@@ -134,5 +134,5 @@ double getMhz(IOsPrimitivesMac* osPrimitives)
}
}

return static_cast<double>(cpuHz) / MHz;
return cpuHz / MHz;
}
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@

#include "hardwareWrapperImplMac.h"

double getMhz(IOsPrimitivesMac* osPrimitives)
int getMhz(IOsPrimitivesMac* osPrimitives)
{
constexpr auto MHz{1000000};
uint64_t cpuHz{0};
@@ -28,5 +28,5 @@ double getMhz(IOsPrimitivesMac* osPrimitives)
};
}

return static_cast<double>(cpuHz) / MHz;
return cpuHz / MHz;
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
#include "sharedDefs.h"


double getMhz(IOsPrimitivesMac* osPrimitives = nullptr);
int getMhz(IOsPrimitivesMac* osPrimitives = nullptr);

template <class TOsPrimitivesMac>
class OSHardwareWrapperMac final : public IOSHardwareWrapper, public TOsPrimitivesMac
@@ -35,7 +35,7 @@ class OSHardwareWrapperMac final : public IOSHardwareWrapper, public TOsPrimitiv

std::string boardSerial() const
{
std::string ret{UNKNOWN_VALUE};
std::string ret{EMPTY_VALUE};
const auto rawData{UtilsWrapperMac::exec("system_profiler SPHardwareDataType | grep Serial")};

if (!rawData.empty())
@@ -105,7 +105,7 @@ class OSHardwareWrapperMac final : public IOSHardwareWrapper, public TOsPrimitiv
return cores;
}

double cpuMhz()
int cpuMhz()
{
return getMhz(static_cast<IOsPrimitivesMac*>(this));
}
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ class IOSHardwareWrapper
virtual std::string boardSerial() const = 0;
virtual std::string cpuName() const = 0;
virtual int cpuCores() const = 0;
virtual double cpuMhz() = 0;
virtual int cpuMhz() = 0;
virtual uint64_t ramTotal() const = 0;
virtual uint64_t ramFree() const = 0;
virtual uint64_t ramUsage() const = 0;
67 changes: 34 additions & 33 deletions src/common/data_provider/src/network/networkBSDWrapper.h
Original file line number Diff line number Diff line change
@@ -58,9 +58,9 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper
return m_interfaceAddress->ifa_name ? Utils::substrOnFirstOccurrence(m_interfaceAddress->ifa_name, ":") : "";
}

std::string adapter() const override
void adapter(nlohmann::json& network) const override
{
return "";
network["adapter"] = UNKNOWN_VALUE;
}

int family() const override
@@ -84,12 +84,12 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper
&(reinterpret_cast<sockaddr_in*>(m_interfaceAddress->ifa_netmask))->sin_addr) : "";
}

std::string broadcast() const override
void broadcast(nlohmann::json& network) const override
{
return m_interfaceAddress->ifa_dstaddr ?
network["broadcast"] = m_interfaceAddress->ifa_dstaddr ?
Utils::NetworkHelper::IAddressToBinary(
m_interfaceAddress->ifa_dstaddr->sa_family,
&(reinterpret_cast<sockaddr_in*>(m_interfaceAddress->ifa_dstaddr))->sin_addr) : "";
&(reinterpret_cast<sockaddr_in*>(m_interfaceAddress->ifa_dstaddr))->sin_addr) : EMPTY_VALUE;
}

std::string addressV6() const override
@@ -108,17 +108,17 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper
&(reinterpret_cast<sockaddr_in6*>(m_interfaceAddress->ifa_netmask))->sin6_addr) : "";
}

std::string broadcastV6() const override
void broadcastV6(nlohmann::json& network) const override
{
return m_interfaceAddress->ifa_dstaddr ?
network["broadcast"] = m_interfaceAddress->ifa_dstaddr ?
Utils::NetworkHelper::IAddressToBinary(
m_interfaceAddress->ifa_dstaddr->sa_family,
&(reinterpret_cast<sockaddr_in6*>(m_interfaceAddress->ifa_dstaddr))->sin6_addr) : "";
&(reinterpret_cast<sockaddr_in6*>(m_interfaceAddress->ifa_dstaddr))->sin6_addr) : EMPTY_VALUE;
}

std::string gateway() const override
void gateway(nlohmann::json& network) const override
{
std::string retVal;
network["gateway"] = UNKNOWN_VALUE;
size_t tableSize { 0 };
int mib[] = { CTL_NET, AF_ROUTE, 0, AF_UNSPEC, NET_RT_FLAGS, RTF_UP | RTF_GATEWAY };

@@ -144,7 +144,7 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper

if (sock && AF_INET == sock->sa_family)
{
retVal = Utils::NetworkHelper::IAddressToBinary(AF_INET, &reinterpret_cast<sockaddr_in*>(sock)->sin_addr);
network["gateway"] = Utils::NetworkHelper::IAddressToBinary(AF_INET, &reinterpret_cast<sockaddr_in*>(sock)->sin_addr);
}

break;
@@ -154,28 +154,30 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper
}
}
}

return retVal;
}

std::string metrics() const override
void metrics(nlohmann::json& network) const override
{
return "";
network["metric"] = UNKNOWN_VALUE;
}

std::string metricsV6() const override
void metricsV6(nlohmann::json& network) const override
{
return "";
network["metric"] = UNKNOWN_VALUE;
}

std::string dhcp() const override
void dhcp(nlohmann::json& network) const override
{
return "unknown";
network["dhcp"] = UNKNOWN_VALUE;
}

uint32_t mtu() const override
void mtu(nlohmann::json& network) const override
{
return m_interfaceAddress->ifa_data ? reinterpret_cast<if_data*>(m_interfaceAddress->ifa_data)->ifi_mtu : 0;
network["mtu"] = UNKNOWN_VALUE;
if(m_interfaceAddress->ifa_data)
{
network["mtu"] = reinterpret_cast<if_data*>(m_interfaceAddress->ifa_data)->ifi_mtu;
}
}

LinkStats stats() const override
@@ -197,28 +199,29 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper
return retVal;
}

std::string type() const override
void type(nlohmann::json& network) const override
{
std::string retVal { UNKNOWN_VALUE };
network["type"] = UNKNOWN_VALUE;

if (m_interfaceAddress->ifa_addr)
{
auto sdl { reinterpret_cast<struct sockaddr_dl*>(m_interfaceAddress->ifa_addr) };
const auto type { Utils::NetworkHelper::getNetworkTypeStringCode(sdl->sdl_type, NETWORK_INTERFACE_TYPE) };
retVal = type.empty() ? UNKNOWN_VALUE : type;
if(!type.empty())
{
network["type"] = type;
}
}

return retVal;
}

std::string state() const override
void state(nlohmann::json& network) const override
{
return m_interfaceAddress->ifa_flags & IFF_UP ? "up" : "down";
network["state"] = m_interfaceAddress->ifa_flags & IFF_UP ? "up" : "down";
}

std::string MAC() const override
void MAC(nlohmann::json& network) const override
{
std::string retVal { "00:00:00:00:00:00" };
network["mac"] = UNKNOWN_VALUE;
auto sdl { reinterpret_cast<struct sockaddr_dl*>(m_interfaceAddress->ifa_addr) };
std::stringstream ss;

@@ -239,11 +242,9 @@ class NetworkBSDInterface final : public INetworkInterfaceWrapper
}
}

retVal = ss.str();
network["mac"] = ss.str();
}
}

return retVal;
}
};

24 changes: 12 additions & 12 deletions src/common/data_provider/src/network/networkInterfaceBSD.cpp
Original file line number Diff line number Diff line change
@@ -55,9 +55,9 @@ void BSDNetworkImpl<AF_INET>::buildNetworkData(nlohmann::json& network)
nlohmann::json ipv4JS {};
ipv4JS["address"] = address;
ipv4JS["netmask"] = m_interfaceAddress->netmask();
ipv4JS["broadcast"] = m_interfaceAddress->broadcast();
ipv4JS["metric"] = m_interfaceAddress->metrics();
ipv4JS["dhcp"] = m_interfaceAddress->dhcp();
m_interfaceAddress->broadcast(ipv4JS);
m_interfaceAddress->metrics(ipv4JS);
m_interfaceAddress->dhcp(ipv4JS);

network["IPv4"].push_back(ipv4JS);
}
@@ -76,9 +76,9 @@ void BSDNetworkImpl<AF_INET6>::buildNetworkData(nlohmann::json& network)
nlohmann::json ipv6JS {};
ipv6JS["address"] = address;
ipv6JS["netmask"] = m_interfaceAddress->netmaskV6();
ipv6JS["broadcast"] = m_interfaceAddress->broadcastV6();
ipv6JS["metric"] = m_interfaceAddress->metricsV6();
ipv6JS["dhcp"] = m_interfaceAddress->dhcp();
m_interfaceAddress->broadcastV6(ipv6JS);
m_interfaceAddress->metricsV6(ipv6JS);
m_interfaceAddress->dhcp(ipv6JS);

network["IPv6"].push_back(ipv6JS);
}
@@ -93,10 +93,10 @@ void BSDNetworkImpl<AF_LINK>::buildNetworkData(nlohmann::json& network)
/* Get stats of interface */

network["name"] = m_interfaceAddress->name();
network["adapter"] = m_interfaceAddress->adapter();
network["state"] = m_interfaceAddress->state();
network["type"] = m_interfaceAddress->type();
network["mac"] = m_interfaceAddress->MAC();
m_interfaceAddress->adapter(network);
m_interfaceAddress->state(network);
m_interfaceAddress->type(network);
m_interfaceAddress->MAC(network);

const auto stats { m_interfaceAddress->stats() };

@@ -109,6 +109,6 @@ void BSDNetworkImpl<AF_LINK>::buildNetworkData(nlohmann::json& network)
network["tx_dropped"] = stats.txDropped;
network["rx_dropped"] = stats.rxDropped;

network["mtu"] = m_interfaceAddress->mtu();
network["gateway"] = m_interfaceAddress->gateway();
m_interfaceAddress->mtu(network);
m_interfaceAddress->gateway(network);
}
Loading

0 comments on commit 707b0b5

Please sign in to comment.