mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Network (Windows): add support
This commit is contained in:
@@ -516,6 +516,7 @@ elseif(WIN32)
|
||||
src/detection/gamepad/gamepad_windows.c
|
||||
src/detection/media/media_nosupport.c
|
||||
src/detection/memory/memory_windows.c
|
||||
src/detection/network/network_windows.c
|
||||
src/detection/opengl/opengl_windows.c
|
||||
src/detection/os/os_windows.cpp
|
||||
src/detection/packages/packages_windows.c
|
||||
|
||||
@@ -206,6 +206,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffListInit(&instance->config.commandTexts, sizeof(FFstrbuf));
|
||||
|
||||
ffStrbufInit(&instance->config.networkType);
|
||||
instance->config.networkAll = false;
|
||||
}
|
||||
|
||||
void ffInitInstance(FFinstance* instance)
|
||||
|
||||
@@ -11,6 +11,7 @@ typedef struct FFNetworkResult
|
||||
FFstrbuf type;
|
||||
FFstrbuf address;
|
||||
int32_t mtu;
|
||||
bool on;
|
||||
} FFNetworkResult;
|
||||
|
||||
const char* ffDetectNetwork(FFinstance* instance, FFlist* result);
|
||||
|
||||
@@ -29,6 +29,7 @@ const char* ffDetectNetwork(FFinstance* instance, FFlist* result)
|
||||
ffStrbufInit(&item->name);
|
||||
ffStrbufInit(&item->address);
|
||||
item->mtu = 0;
|
||||
item->on = true;
|
||||
|
||||
ffCfStrGetString(SCNetworkInterfaceGetLocalizedDisplayName(ni), &item->name);
|
||||
ffCfStrGetString(SCNetworkInterfaceGetHardwareAddressString(ni), &item->address);
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "detection/network/network.h"
|
||||
#include "util/windows/unicode.h"
|
||||
#include "util/mallocHelper.h"
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <wchar.h>
|
||||
|
||||
const char* ffDetectNetwork(FFinstance* instance, FFlist* result)
|
||||
{
|
||||
IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL;
|
||||
|
||||
// Start with a 16 KB buffer and resize if needed -
|
||||
// multiple attempts in case interfaces change while
|
||||
// we are in the middle of querying them.
|
||||
DWORD adapter_addresses_buffer_size = 16 * 1024;
|
||||
for (int attempts = 0; attempts != 3; ++attempts)
|
||||
{
|
||||
adapter_addresses = (IP_ADAPTER_ADDRESSES*)realloc(adapter_addresses, adapter_addresses_buffer_size);
|
||||
assert(adapter_addresses);
|
||||
|
||||
DWORD error = GetAdaptersAddresses(
|
||||
AF_UNSPEC,
|
||||
GAA_FLAG_SKIP_ANYCAST |
|
||||
GAA_FLAG_SKIP_MULTICAST |
|
||||
GAA_FLAG_SKIP_DNS_SERVER |
|
||||
GAA_FLAG_SKIP_FRIENDLY_NAME,
|
||||
NULL,
|
||||
adapter_addresses,
|
||||
&adapter_addresses_buffer_size);
|
||||
|
||||
if (error == ERROR_SUCCESS)
|
||||
break;
|
||||
else if (ERROR_BUFFER_OVERFLOW == error)
|
||||
continue;
|
||||
else
|
||||
return "GetAdaptersAddresses() failed";
|
||||
}
|
||||
|
||||
// Iterate through all of the adapters
|
||||
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
|
||||
{
|
||||
if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
|
||||
continue;
|
||||
|
||||
bool isOn = adapter->OperStatus == IfOperStatusUp;
|
||||
if (!isOn && !instance->config.networkAll)
|
||||
continue;
|
||||
|
||||
const char* strType = NULL;
|
||||
switch (adapter->IfType)
|
||||
{
|
||||
case IF_TYPE_ETHERNET_CSMACD:
|
||||
strType = "Ethernet";
|
||||
break;
|
||||
case IF_TYPE_ISO88025_TOKENRING:
|
||||
strType = "ISO88025";
|
||||
break;
|
||||
case IF_TYPE_PPP:
|
||||
strType = "PPP";
|
||||
break;
|
||||
case IF_TYPE_ATM:
|
||||
strType = "ATM";
|
||||
break;
|
||||
case IF_TYPE_IEEE80211:
|
||||
strType = "IEEE80211";
|
||||
break;
|
||||
case IF_TYPE_TUNNEL:
|
||||
strType = "Tunnel";
|
||||
break;
|
||||
case IF_TYPE_IEEE1394:
|
||||
strType = "Fireware";
|
||||
break;
|
||||
case IF_TYPE_MODEM:
|
||||
strType = "Modem";
|
||||
break;
|
||||
default:
|
||||
strType = "Other";
|
||||
break;
|
||||
}
|
||||
|
||||
if (instance->config.networkType.length && !ffStrbufContainIgnCaseS(&instance->config.networkType, strType))
|
||||
continue;
|
||||
|
||||
FFNetworkResult* item = (FFNetworkResult*) ffListAdd(result);
|
||||
ffStrbufInitS(&item->type, strType);
|
||||
ffStrbufInit(&item->name);
|
||||
ffStrbufInit(&item->address);
|
||||
item->mtu = (int32_t) adapter->Mtu;
|
||||
item->on = isOn;
|
||||
|
||||
ffStrbufSetWS(&item->name, adapter->FriendlyName);
|
||||
for (ULONG i = 0; i < adapter->PhysicalAddressLength; i++)
|
||||
ffStrbufAppendF(&item->address, "%.2X-", (int) adapter->PhysicalAddress[i]);
|
||||
ffStrbufTrimRight(&item->address, '-');
|
||||
}
|
||||
|
||||
if (result->length == 0)
|
||||
return "No network interfaces found";
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -1343,6 +1343,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
instance->config.percentType = optionParseUInt32(key, value);
|
||||
else if(strcasecmp(key, "--network-type") == 0)
|
||||
optionParseString(key, value, &instance->config.networkType);
|
||||
else if(strcasecmp(key, "--network-all") == 0)
|
||||
instance->config.networkAll = optionParseBoolean(value);
|
||||
else if(strcasecmp(key, "--command-shell") == 0)
|
||||
optionParseString(key, value, &instance->config.commandShell);
|
||||
else if(strcasecmp(key, "--command-key") == 0)
|
||||
|
||||
@@ -246,6 +246,7 @@ typedef struct FFconfig
|
||||
FFlist commandTexts;
|
||||
|
||||
FFstrbuf networkType;
|
||||
bool networkAll;
|
||||
} FFconfig;
|
||||
|
||||
typedef struct FFstate
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "detection/network/network.h"
|
||||
|
||||
#define FF_NETWORK_MODULE_NAME "Network"
|
||||
#define FF_NETWORK_NUM_FORMAT_ARGS 4
|
||||
#define FF_NETWORK_NUM_FORMAT_ARGS 5
|
||||
|
||||
void ffPrintNetwork(FFinstance* instance)
|
||||
{
|
||||
@@ -42,6 +42,7 @@ void ffPrintNetwork(FFinstance* instance)
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.network.outputFormat, FF_NETWORK_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRING, network->on ? "ON" : "OFF"},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &network->type},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &network->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &network->address},
|
||||
|
||||
Reference in New Issue
Block a user