Network: drop module

This commit is contained in:
李通洲
2023-05-03 21:49:01 +08:00
parent 8cf033002c
commit dc2a3492f0
15 changed files with 2 additions and 429 deletions
-7
View File
@@ -115,7 +115,6 @@ static void defaultConfig(FFinstance* instance)
initModuleArg(&instance->config.bluetooth);
initModuleArg(&instance->config.sound);
initModuleArg(&instance->config.gamepad);
initModuleArg(&instance->config.network);
ffStrbufInitA(&instance->config.libPCI, 0);
ffStrbufInitA(&instance->config.libVulkan, 0);
@@ -204,9 +203,6 @@ static void defaultConfig(FFinstance* instance)
);
ffListInit(&instance->config.commandKeys, sizeof(FFstrbuf));
ffListInit(&instance->config.commandTexts, sizeof(FFstrbuf));
ffStrbufInit(&instance->config.networkType);
instance->config.networkAll = false;
}
void ffInitInstance(FFinstance* instance)
@@ -387,7 +383,6 @@ static void destroyConfig(FFinstance* instance)
destroyModuleArg(&instance->config.bluetooth);
destroyModuleArg(&instance->config.sound);
destroyModuleArg(&instance->config.gamepad);
destroyModuleArg(&instance->config.network);
ffStrbufDestroy(&instance->config.libPCI);
ffStrbufDestroy(&instance->config.libVulkan);
@@ -431,8 +426,6 @@ static void destroyConfig(FFinstance* instance)
FF_LIST_FOR_EACH(FFstrbuf, item, instance->config.commandTexts)
ffStrbufDestroy(item);
ffListDestroy(&instance->config.commandTexts);
ffStrbufDestroy(&instance->config.networkType);
}
static void destroyState(FFinstance* instance)
-12
View File
@@ -176,18 +176,6 @@
# Default is 0 (disabled).
#--public-ip-timeout 0
# Network show all interfaces option
# Sets if all interfaces (including down) should be printed
# Must be either true or false
# Default is false.
#--network-all false
# Network show interface type option
# Sets which type of interfaces should be printed
# Must be a string
# Default is empty.
#--network-type
# Weather output format option:
# Sets the weather format to be used. It must be URI encoded.
# See: https://github.com/chubin/wttr.in#one-line-output
-2
View File
@@ -132,8 +132,6 @@ Module specific options:
--localip-v6first <?value>: Set if ipv6 should be printed first. Default is false
--public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
--public-ip-url: The URL of public IP detection server to be used.
--network-all: Set if all interfaces (including down) should be printed. Default is false
--network-type: Set which type of interfaces should be printed. Default is empty
--weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0)
--weather-output-format: The output weather format to be used. It must be URI encoded.
--player-name: The name of the player to use
-19
View File
@@ -1,19 +0,0 @@
#pragma once
#ifndef FF_INCLUDED_detection_network_network
#define FF_INCLUDED_detection_network_network
#include "fastfetch.h"
typedef struct FFNetworkResult
{
FFstrbuf name;
FFstrbuf type;
FFstrbuf address;
uint32_t mtu;
bool up;
} FFNetworkResult;
const char* ffDetectNetwork(FFinstance* instance, FFlist* result);
#endif
-43
View File
@@ -1,43 +0,0 @@
#include "detection/network/network.h"
#include "util/apple/cf_helpers.h"
#include <SystemConfiguration/SystemConfiguration.h>
const char* ffDetectNetwork(FFinstance* instance, FFlist* result)
{
FF_STRBUF_AUTO_DESTROY sbType;
ffStrbufInit(&sbType);
CFArrayRef FF_CFTYPE_AUTO_RELEASE array = SCNetworkInterfaceCopyAll();
for (CFIndex i = 0, length = CFArrayGetCount(array); i < length; ++i)
{
SCNetworkInterfaceRef ni = CFArrayGetValueAtIndex(array, i), parent;
while ((parent = SCNetworkInterfaceGetInterface(ni)))
ni = parent;
CFStringRef cfType = SCNetworkInterfaceGetInterfaceType(ni);
if (cfType)
ffCfStrGetString(cfType, &sbType);
else
ffStrbufAppendS(&sbType, "Unknown");
if (instance->config.networkType.length && !ffStrbufContainIgnCase(&instance->config.networkType, &sbType))
continue;
FFNetworkResult* item = (FFNetworkResult*) ffListAdd(result);
ffStrbufInitMove(&item->type, &sbType);
ffStrbufInit(&item->name);
ffStrbufInit(&item->address);
item->mtu = 0;
item->up = true;
ffCfStrGetString(SCNetworkInterfaceGetLocalizedDisplayName(ni), &item->name);
ffCfStrGetString(SCNetworkInterfaceGetHardwareAddressString(ni), &item->address);
SCNetworkInterfaceCopyMTU(ni, (int*) &item->mtu, nil, nil);
}
if (result->length == 0)
return "No network interfaces found";
return NULL;
}
-50
View File
@@ -1,50 +0,0 @@
#include "common/io/io.h"
#include "detection/network/network.h"
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
const char* ffDetectNetwork(FF_MAYBE_UNUSED FFinstance* instance, FFlist* result)
{
struct ifaddrs* ifAddrStruct = NULL;
if(getifaddrs(&ifAddrStruct) < 0)
return "getifaddrs(&ifAddrStruct) failed";
int FF_AUTO_CLOSE_FD sockfd = socket(AF_INET, SOCK_DGRAM, 0);
for (struct ifaddrs* ifa = ifAddrStruct; ifa; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_LINK)
continue;
if (ifa->ifa_flags & IFF_LOOPBACK || !(ifa->ifa_flags & IFF_RUNNING))
continue;
bool isUp = !!(ifa->ifa_flags & IFF_UP);
if (!isUp && !instance->config.networkAll)
continue;
FFNetworkResult* item = (FFNetworkResult*) ffListAdd(result);
ffStrbufInit(&item->type);
ffStrbufInitS(&item->name, ifa->ifa_name);
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr);
ffStrbufInitF(&item->address, "%02x:%02x:%02x:%02x:%02x:%02x",
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
if (sockfd > 0)
{
struct ifreq ifr;
strcpy(ifr.ifr_name, ifa->ifa_name);
item->mtu = ioctl(sockfd, SIOCGIFMTU, &ifr) >= 0 ? (uint32_t) ifr.ifr_mtu : 0;
}
item->up = isUp;
}
freeifaddrs(ifAddrStruct);
return NULL;
}
-111
View File
@@ -1,111 +0,0 @@
#include "common/io/io.h"
#include "detection/network/network.h"
#include <net/if.h>
#include <linux/if_arp.h>
const char* ffDetectNetwork(FFinstance* instance, FFlist* result)
{
FF_STRBUF_AUTO_DESTROY sbType;
ffStrbufInit(&sbType);
struct if_nameindex* infs = if_nameindex();
if(!infs)
return "if_nameindex() failed";
FF_STRBUF_AUTO_DESTROY path;
ffStrbufInit(&path);
FF_STRBUF_AUTO_DESTROY fileContent;
ffStrbufInit(&fileContent);
for(struct if_nameindex* i = infs; !(i->if_index == 0 && i->if_name == NULL); ++i)
{
if (strcmp(i->if_name, "lo") == 0)
continue;
ffStrbufSetF(&path, "/sys/class/net/%s/operstate", i->if_name);
if (!ffReadFileBuffer(path.chars, &fileContent))
continue;
bool isUp = ffStrbufEqualS(&fileContent, "up");
if (!isUp && !instance->config.networkAll)
continue;
ffStrbufSetF(&path, "/sys/class/net/%s/type", i->if_name);
if (!ffReadFileBuffer(path.chars, &fileContent))
continue;
const char* strType = NULL;
switch (ffStrbufToUInt16(&fileContent, 0))
{
case ARPHRD_ETHER:
case ARPHRD_EETHER:
strType = "Ethernet";
break;
case ARPHRD_AX25:
strType = "AX25";
break;
case ARPHRD_PRONET:
strType = "TokenRing";
break;
case ARPHRD_CHAOS:
strType = "Chaosnet";
break;
case ARPHRD_IEEE802:
strType = "IEEE802";
break;
case ARPHRD_ARCNET:
strType = "ARCnet";
break;
case ARPHRD_APPLETLK:
strType = "APPLEtalk";
break;
case ARPHRD_DLCI:
strType = "DLCI";
break;
case ARPHRD_ATM:
strType = "ATM";
break;
case ARPHRD_METRICOM:
strType = "Metricom";
break;
case ARPHRD_IEEE1394:
strType = "IEEE1394";
break;
case ARPHRD_EUI64:
strType = "EUI64";
break;
case ARPHRD_INFINIBAND:
strType = "InfiniBand";
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);
ffStrbufInitS(&item->name, i->if_name);
ffStrbufInit(&item->address);
item->mtu = 0;
item->up = isUp;
ffStrbufSetF(&path, "/sys/class/net/%s/address", i->if_name);
ffReadFileBuffer(path.chars, &item->address);
ffStrbufSetF(&path, "/sys/class/net/%s/mtu", i->if_name);
if (ffReadFileBuffer(path.chars, &fileContent))
item->mtu = ffStrbufToUInt16(&fileContent, 0);
}
if_freenameindex(infs);
if (result->length == 0)
return "No network interfaces found";
return NULL;
}
-103
View File
@@ -1,103 +0,0 @@
#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 isUp = adapter->OperStatus == IfOperStatusUp;
if (!isUp && !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 = "TokenRing";
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 = adapter->Mtu;
item->up = isUp;
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;
}
-6
View File
@@ -1341,10 +1341,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
}
else if(strcasecmp(key, "--percent-type") == 0)
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)
@@ -1520,8 +1516,6 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
ffPrintSound(instance);
else if(strcasecmp(line, "gamepad") == 0)
ffPrintGamepad(instance);
else if(strcasecmp(line, "network") == 0)
ffPrintNetwork(instance);
else
ffPrintErrorString(instance, line, 0, NULL, NULL, "<no implementation provided>");
}
-5
View File
@@ -163,7 +163,6 @@ typedef struct FFconfig
FFModuleArgs bluetooth;
FFModuleArgs sound;
FFModuleArgs gamepad;
FFModuleArgs network;
FFstrbuf libPCI;
FFstrbuf libVulkan;
@@ -244,9 +243,6 @@ typedef struct FFconfig
FFstrbuf commandShell;
FFlist commandKeys;
FFlist commandTexts;
FFstrbuf networkType;
bool networkAll;
} FFconfig;
typedef struct FFstate
@@ -353,6 +349,5 @@ void ffPrintCommand(FFinstance* instance);
void ffPrintBluetooth(FFinstance* instance);
void ffPrintSound(FFinstance* instance);
void ffPrintGamepad(FFinstance* instance);
void ffPrintNetwork(FFinstance* instance);
#endif
-60
View File
@@ -1,60 +0,0 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/network/network.h"
#define FF_NETWORK_MODULE_NAME "Network"
#define FF_NETWORK_NUM_FORMAT_ARGS 5
void ffPrintNetwork(FFinstance* instance)
{
FF_LIST_AUTO_DESTROY networks;
ffListInit(&networks, sizeof(FFNetworkResult));
const char* error = ffDetectNetwork(instance, &networks);
if(error != NULL)
{
ffPrintError(instance, FF_NETWORK_MODULE_NAME, 0, &instance->config.network, "%s", error);
return;
}
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
FF_LIST_FOR_EACH(FFNetworkResult, network, networks)
{
if(instance->config.network.key.length == 0)
{
if (network->type.length > 0)
ffStrbufSetF(&key, "%s (%s)", FF_NETWORK_MODULE_NAME, network->type.chars);
else
ffStrbufSetS(&key, FF_NETWORK_MODULE_NAME);
}
else
{
ffParseFormatString(&key, &instance->config.network.key, 1, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &network->type},
{FF_FORMAT_ARG_TYPE_STRBUF, &network->name},
});
}
if (instance->config.network.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
printf("%s - %s\n", network->name.chars, network->address.chars);
}
else
{
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.network.outputFormat, FF_NETWORK_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRING, network->up ? "UP" : "DOWN"},
{FF_FORMAT_ARG_TYPE_STRBUF, &network->type},
{FF_FORMAT_ARG_TYPE_STRBUF, &network->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &network->address},
{FF_FORMAT_ARG_TYPE_INT, &network->mtu},
});
}
ffStrbufDestroy(&network->type);
ffStrbufDestroy(&network->name);
ffStrbufDestroy(&network->address);
}
}