Skip to content

Commit

Permalink
MeasureNet: Consolidate and move network interface functions to Common
Browse files Browse the repository at this point in the history
  • Loading branch information
brianferguson committed Sep 9, 2021
1 parent 919a2f0 commit ef9f00c
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 150 deletions.
2 changes: 2 additions & 0 deletions Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<ClCompile Include="Gfx\Util\DWriteHelpers.cpp" />
<ClCompile Include="MathParser.cpp" />
<ClCompile Include="MenuTemplate.cpp" />
<ClCompile Include="NetworkUtil.cpp" />
<ClCompile Include="PathUtil.cpp" />
<ClCompile Include="Platform.cpp" />
<ClCompile Include="StdAfx.cpp">
Expand Down Expand Up @@ -121,6 +122,7 @@
<ClInclude Include="ScopedFunction.h" />
<ClInclude Include="MathParser.h" />
<ClInclude Include="MenuTemplate.h" />
<ClInclude Include="NetworkUtil.h" />
<ClInclude Include="PathUtil.h" />
<ClInclude Include="Platform.h" />
<ClInclude Include="RawString.h" />
Expand Down
2 changes: 2 additions & 0 deletions Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<ClCompile Include="CharacterEntityReference.cpp" />
<ClCompile Include="Dialog.cpp" />
<ClCompile Include="MenuTemplate.cpp" />
<ClCompile Include="NetworkUtil.cpp" />
<ClCompile Include="PathUtil.cpp" />
<ClCompile Include="Platform.cpp" />
<ClCompile Include="StringUtil.cpp" />
Expand Down Expand Up @@ -129,6 +130,7 @@
<ItemGroup>
<ClInclude Include="Dialog.h" />
<ClInclude Include="MenuTemplate.h" />
<ClInclude Include="NetworkUtil.h" />
<ClInclude Include="PathUtil.h" />
<ClInclude Include="Platform.h" />
<ClInclude Include="RawString.h" />
Expand Down
136 changes: 136 additions & 0 deletions Common/NetworkUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* Copyright (C) 2021 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */

#include "StdAfx.h"
#include "NetworkUtil.h"

ULONG NetworkUtil::s_InterfaceCount = 0UL;
MIB_IF_TABLE2* NetworkUtil::s_InterfaceTable = nullptr;

void NetworkUtil::Initialize()
{
}

void NetworkUtil::Finalize()
{
DisposeInterfaceTable();
}

bool NetworkUtil::UpdateInterfaceTable()
{
static ULONGLONG s_LastUpdateTickCount = 0ULL;
const ULONGLONG updateInterval = 250ULL; // ms

ULONGLONG tickCount = GetTickCount64();
if (tickCount >= (s_LastUpdateTickCount + updateInterval))
{
s_LastUpdateTickCount = tickCount;

DisposeInterfaceTable();
if (GetIfTable2(&s_InterfaceTable) == NO_ERROR)
{
s_InterfaceCount = s_InterfaceTable->NumEntries;
return true;
}
}
return false;
}

ULONG NetworkUtil::FindBestInterface(LPCWSTR interfaceName)
{
if (s_InterfaceTable)
{
if (_wcsicmp(interfaceName, L"BEST") == 0)
{
DWORD dwBestIndex = 0;
if (NO_ERROR == GetBestInterface(INADDR_ANY, &dwBestIndex))
{
return (ULONG)dwBestIndex;
}
}
else
{
for (size_t i = 0; i < s_InterfaceCount; ++i)
{
MIB_IF_ROW2* table = s_InterfaceTable->Table;
if (_wcsicmp(interfaceName, table[i].Description) == 0)
{
return table[i].InterfaceIndex;
}
}
}
}
return 0UL;
}

ULONG NetworkUtil::GetIndexFromIfIndex(const ULONG ifIndex)
{
if (s_InterfaceTable)
{
for (ULONG i = 0; i < s_InterfaceCount; ++i)
{
MIB_IF_ROW2* table = s_InterfaceTable->Table;
if (ifIndex == table[i].InterfaceIndex)
{
return i;
}
}
}
return 0UL;
}

LPCWSTR NetworkUtil::GetInterfaceTypeString(const ULONG ifIndex)
{
switch (ifIndex)
{
case IF_TYPE_ETHERNET_CSMACD: return L"Ethernet";
case IF_TYPE_FDDI: return L"Fiber";
case IF_TYPE_PPP: return L"PPP";
case IF_TYPE_SOFTWARE_LOOPBACK: return L"Loopback";
case IF_TYPE_IEEE80211: return L"Wireless";
case IF_TYPE_TUNNEL: return L"Tunnel";
case IF_TYPE_IEEE1394: return L"Firewire";
case IF_TYPE_IEEE80216_WMAN: return L"Mobile WiMax";
case IF_TYPE_WWANPP: return L"Mobile GSM";
case IF_TYPE_WWANPP2: return L"Mobile CDMA";
}
return L"Other";
}

LPCWSTR NetworkUtil::GetInterfaceMediaConnectionString(const NET_IF_MEDIA_CONNECT_STATE state)
{
switch (state)
{
case MediaConnectStateConnected: return L"Connected";
case MediaConnectStateDisconnected: return L"Disconnected";
}
return L"Unknown";
}

LPCWSTR NetworkUtil::GetInterfaceOperStatusString(const IF_OPER_STATUS status)
{
switch (status)
{
case IfOperStatusUp: return L"Up";
case IfOperStatusDown: return L"Down";
case IfOperStatusTesting: return L"Testing";
case IfOperStatusDormant: return L"Dormant";
case IfOperStatusNotPresent: return L"Not Present";
case IfOperStatusLowerLayerDown: return L"Lower Layer Down";
}
return L"Unknown";
}

void NetworkUtil::DisposeInterfaceTable()
{
if (s_InterfaceTable)
{
FreeMibTable(s_InterfaceTable);
s_InterfaceTable = nullptr;
}
s_InterfaceCount = 0UL;
}
46 changes: 46 additions & 0 deletions Common/NetworkUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (C) 2021 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */

#ifndef RM_COMMON_NETWORKUTIL_H_
#define RM_COMMON_NETWORKUTIL_H_

#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>

class __declspec(novtable) NetworkUtil final
{
public:
static void Initialize();
static void Finalize();

static bool UpdateInterfaceTable();

static const ULONG GetInterfaceCount() { return s_InterfaceCount; }
static MIB_IF_ROW2* GetInterfaceTable() { if (s_InterfaceTable) { return s_InterfaceTable->Table; } return nullptr; }

static ULONG FindBestInterface(LPCWSTR interfaceName);
static ULONG GetIndexFromIfIndex(const ULONG ifIndex);

static LPCWSTR GetInterfaceTypeString(const ULONG ifIndex);
static LPCWSTR GetInterfaceMediaConnectionString(const NET_IF_MEDIA_CONNECT_STATE state);
static LPCWSTR GetInterfaceOperStatusString(const IF_OPER_STATUS status);

private:
NetworkUtil() = delete;
~NetworkUtil() = delete;

NetworkUtil(const NetworkUtil& other) = delete;
NetworkUtil& operator=(NetworkUtil other) = delete;

static void DisposeInterfaceTable();

static ULONG s_InterfaceCount;
static MIB_IF_TABLE2* s_InterfaceTable;
};

#endif
Loading

0 comments on commit ef9f00c

Please sign in to comment.