mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
LocalIP: backport mac address support
Also change `--*-ip-*` to `--*ip-*` to be consistant to my config branch
This commit is contained in:
parent
9107d0e107
commit
ad026f6105
@ -32,8 +32,8 @@ __fastfetch_complete_help()
|
||||
"battery-format"
|
||||
"poweradapter-format"
|
||||
"locale-format"
|
||||
"local-ip-format"
|
||||
"public-ip-format"
|
||||
"localip-format"
|
||||
"publicip-format"
|
||||
"player-format"
|
||||
"media-format"
|
||||
"datetime-format"
|
||||
@ -235,8 +235,8 @@ __fastfetch_completion()
|
||||
"--set-keyless"
|
||||
"--player-name"
|
||||
"--percent-type"
|
||||
"--public-ip-url"
|
||||
"--public-ip-timeout"
|
||||
"--publicip-url"
|
||||
"--publicip-timeout"
|
||||
"--weather-output-format"
|
||||
"--weather-timeout"
|
||||
"--os-key"
|
||||
@ -317,12 +317,12 @@ __fastfetch_completion()
|
||||
"--locale-key"
|
||||
"--locale-format"
|
||||
"--locale-error"
|
||||
"--local-ip-key"
|
||||
"--local-ip-format"
|
||||
"--local-ip-error"
|
||||
"--public-ip-key"
|
||||
"--public-ip-format"
|
||||
"--public-ip-error"
|
||||
"--localip-key"
|
||||
"--localip-format"
|
||||
"--localip-error"
|
||||
"--publicip-key"
|
||||
"--publicip-format"
|
||||
"--publicip-error"
|
||||
"--wifi-key"
|
||||
"--wifi-format"
|
||||
"--wifi-error"
|
||||
|
@ -174,12 +174,8 @@ static void defaultConfig(FFinstance* instance)
|
||||
|
||||
ffStrbufInitA(&instance->config.separatorString, 0);
|
||||
|
||||
instance->config.localIpShowIpV4 = true;
|
||||
instance->config.localIpShowIpV6 = false;
|
||||
instance->config.localIpShowLoop = false;
|
||||
instance->config.localIpV6First = false;
|
||||
instance->config.localIpShowType = FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
ffStrbufInit(&instance->config.localIpNamePrefix);
|
||||
instance->config.localIpCompactType = FF_LOCALIP_COMPACT_TYPE_NONE;
|
||||
|
||||
instance->config.publicIpTimeout = 0;
|
||||
ffStrbufInit(&instance->config.publicIpUrl);
|
||||
|
@ -168,13 +168,13 @@
|
||||
# Sets the URL of public IP detection server to be used.
|
||||
# Only HTTP protocol is supported, and the value should not contain "http://" prefix.
|
||||
# Default is "ipinfo.io/ip".
|
||||
#--public-ip-url "ipinfo.io/ip"
|
||||
#--publicip-url "ipinfo.io/ip"
|
||||
|
||||
# Public IP timeout option:
|
||||
# Sets the time to wait for the public ip server to respond.
|
||||
# Must be a positive integer.
|
||||
# Default is 0 (disabled).
|
||||
#--public-ip-timeout 0
|
||||
#--publicip-timeout 0
|
||||
|
||||
# Weather output format option:
|
||||
# Sets the weather format to be used. It must be URI encoded.
|
||||
@ -321,8 +321,8 @@
|
||||
#--battery-key Battery {1}
|
||||
#--poweradapter-key Power Adapter {1}
|
||||
#--locale-key Locale
|
||||
#--local-ip-key Local IP ({1})
|
||||
#--public-ip-key Public IP
|
||||
#--localip-key Local IP ({1})
|
||||
#--publicip-key Public IP
|
||||
#--wifi-key Wifi
|
||||
#--weather-key Weather
|
||||
#--player-key Media Player
|
||||
@ -369,8 +369,8 @@
|
||||
#--battery-format
|
||||
#--poweradapter-format
|
||||
#--locale-format
|
||||
#--local-ip-format
|
||||
#--public-ip-format
|
||||
#--localip-format
|
||||
#--publicip-format
|
||||
#--weather-format
|
||||
#--player-format
|
||||
#--media-format
|
||||
@ -416,8 +416,8 @@
|
||||
#--battery-error
|
||||
#--poweradapter-error
|
||||
#--locale-error
|
||||
#--local-ip-error
|
||||
#--public-ip-error
|
||||
#--localip-error
|
||||
#--publicip-error
|
||||
#--weather-error
|
||||
#--player-error
|
||||
#--media-error
|
||||
|
@ -126,12 +126,12 @@ Module specific options:
|
||||
--battery-temp <?value>: Detect and display Battery temperature if supported. Default is false
|
||||
--localip-show-ipv4 <?value>: Show IPv4 addresses in local ip module. Default is true
|
||||
--localip-show-ipv6 <?value>: Show IPv6 addresses in local ip module. Default is false
|
||||
--localip-show-mac <?value>: Show mac addresses in local ip module. Default is false
|
||||
--localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false
|
||||
--localip-name-prefix <str>: Show IPs with given name prefix only. Default is empty
|
||||
--localip-compact-type <str>: Show IPs in one line. Should be either none, oneline, multiline. Default is none
|
||||
--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.
|
||||
--localip-compact <?value>: Show all IPs in one line. Default is false
|
||||
--publicip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
|
||||
--publicip-url: The URL of public IP detection server to be used.
|
||||
--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
|
||||
|
@ -8,8 +8,9 @@
|
||||
typedef struct FFLocalIpResult
|
||||
{
|
||||
FFstrbuf name;
|
||||
FFstrbuf addr;
|
||||
bool ipv6;
|
||||
FFstrbuf ipv4;
|
||||
FFstrbuf ipv6;
|
||||
FFstrbuf mac;
|
||||
} FFLocalIpResult;
|
||||
|
||||
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results);
|
||||
|
@ -7,12 +7,43 @@
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static void addNewIp(FFlist* list, const char* name, const char* addr, bool ipv6)
|
||||
#if defined(__FreeBSD__) || defined(__APPLE__)
|
||||
#include <net/if_dl.h>
|
||||
#else
|
||||
#include <linux/if_packet.h>
|
||||
#endif
|
||||
|
||||
static void addNewIp(FFlist* list, const char* name, const char* addr, int type)
|
||||
{
|
||||
FFLocalIpResult* ip = (FFLocalIpResult*) ffListAdd(list);
|
||||
ffStrbufInitS(&ip->name, name);
|
||||
ffStrbufInitS(&ip->addr, addr);
|
||||
ip->ipv6 = ipv6;
|
||||
FFLocalIpResult* ip = NULL;
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, temp, *list)
|
||||
{
|
||||
if (!ffStrbufEqualS(&temp->name, name)) continue;
|
||||
ip = temp;
|
||||
break;
|
||||
}
|
||||
if (!ip)
|
||||
{
|
||||
ip = (FFLocalIpResult*) ffListAdd(list);
|
||||
ffStrbufInitS(&ip->name, name);
|
||||
ffStrbufInit(&ip->ipv4);
|
||||
ffStrbufInit(&ip->ipv6);
|
||||
ffStrbufInit(&ip->mac);
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case AF_INET:
|
||||
ffStrbufSetS(&ip->ipv4, addr);
|
||||
break;
|
||||
case AF_INET6:
|
||||
ffStrbufSetS(&ip->ipv6, addr);
|
||||
break;
|
||||
case -1:
|
||||
ffStrbufSetS(&ip->mac, addr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
@ -26,8 +57,7 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
if (!ifa->ifa_addr || !(ifa->ifa_flags & IFF_RUNNING))
|
||||
continue;
|
||||
|
||||
// loop back
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK) && !instance->config.localIpShowLoop)
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK) && !(instance->config.localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT))
|
||||
continue;
|
||||
|
||||
if (instance->config.localIpNamePrefix.length && strncmp(ifa->ifa_name, instance->config.localIpNamePrefix.chars, instance->config.localIpNamePrefix.length) != 0)
|
||||
@ -35,26 +65,49 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
|
||||
if (ifa->ifa_addr->sa_family == AF_INET)
|
||||
{
|
||||
// IPv4
|
||||
if (!instance->config.localIpShowIpV4)
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT))
|
||||
continue;
|
||||
|
||||
struct sockaddr_in* ipv4 = (struct sockaddr_in*) ifa->ifa_addr;
|
||||
char addressBuffer[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
|
||||
addNewIp(results, ifa->ifa_name, addressBuffer, false);
|
||||
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET);
|
||||
}
|
||||
else if (ifa->ifa_addr->sa_family == AF_INET6)
|
||||
{
|
||||
// IPv6
|
||||
if (!instance->config.localIpShowIpV6)
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT))
|
||||
continue;
|
||||
|
||||
struct sockaddr_in6* ipv6 = (struct sockaddr_in6 *)ifa->ifa_addr;
|
||||
char addressBuffer[INET6_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
|
||||
addNewIp(results, ifa->ifa_name, addressBuffer, true);
|
||||
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6);
|
||||
}
|
||||
#if defined(__FreeBSD__) || defined(__APPLE__)
|
||||
else if (ifa->ifa_addr->sa_family == AF_LINK)
|
||||
{
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT))
|
||||
continue;
|
||||
|
||||
char addressBuffer[32];
|
||||
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr);
|
||||
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
|
||||
addNewIp(results, ifa->ifa_name, addressBuffer, -1);
|
||||
}
|
||||
#else
|
||||
else if (ifa->ifa_addr->sa_family == AF_PACKET)
|
||||
{
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT))
|
||||
continue;
|
||||
|
||||
char addressBuffer[32];
|
||||
uint8_t* ptr = ((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr;
|
||||
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
|
||||
addNewIp(results, ifa->ifa_name, addressBuffer, -1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
|
||||
|
@ -1,21 +1,45 @@
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "util/mallocHelper.h"
|
||||
#include "util/windows/unicode.h"
|
||||
#include "localip.h"
|
||||
|
||||
static void addNewIp(FFlist* list, const char* name, const char* addr, bool ipv6)
|
||||
static void addNewIp(FFlist* list, const char* name, const char* value, int type, bool newIp)
|
||||
{
|
||||
FFLocalIpResult* ip = (FFLocalIpResult*) ffListAdd(list);
|
||||
ffStrbufInitS(&ip->name, name);
|
||||
ffStrbufInitS(&ip->addr, addr);
|
||||
ip->ipv6 = ipv6;
|
||||
FFLocalIpResult* ip = NULL;
|
||||
|
||||
if (newIp)
|
||||
{
|
||||
ip = (FFLocalIpResult*) ffListAdd(list);
|
||||
ffStrbufInitS(&ip->name, name);
|
||||
ffStrbufInit(&ip->ipv4);
|
||||
ffStrbufInit(&ip->ipv6);
|
||||
ffStrbufInit(&ip->mac);
|
||||
}
|
||||
else
|
||||
{
|
||||
ip = ffListGet(list, list->length - 1);
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case AF_INET:
|
||||
ffStrbufSetS(&ip->ipv4, value);
|
||||
break;
|
||||
case AF_INET6:
|
||||
ffStrbufSetS(&ip->ipv6, value);
|
||||
break;
|
||||
case -1:
|
||||
ffStrbufSetS(&ip->mac, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
{
|
||||
IP_ADAPTER_ADDRESSES* adapter_addresses = NULL;
|
||||
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
|
||||
@ -27,11 +51,10 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
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,
|
||||
instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT
|
||||
? instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
|
||||
: AF_INET6,
|
||||
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
|
||||
NULL,
|
||||
adapter_addresses,
|
||||
&adapter_addresses_buffer_size);
|
||||
@ -47,41 +70,47 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
// Iterate through all of the adapters
|
||||
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
|
||||
{
|
||||
if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK && !instance->config.localIpShowLoop)
|
||||
bool isLoop = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK;
|
||||
if (isLoop && !(instance->config.localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT))
|
||||
continue;
|
||||
|
||||
bool newIp = true;
|
||||
|
||||
char name[128];
|
||||
WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, sizeof(name), NULL, NULL);
|
||||
if (instance->config.localIpNamePrefix.length && strncmp(name, instance->config.localIpNamePrefix.chars, instance->config.localIpNamePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
if (instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT && adapter->PhysicalAddressLength == 6)
|
||||
{
|
||||
char addressBuffer[32];
|
||||
uint8_t* ptr = adapter->PhysicalAddress;
|
||||
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
|
||||
addNewIp(results, name, addressBuffer, -1, newIp);
|
||||
newIp = false;
|
||||
}
|
||||
|
||||
for (IP_ADAPTER_UNICAST_ADDRESS* ifa = adapter->FirstUnicastAddress; ifa; ifa = ifa->Next)
|
||||
{
|
||||
if (ifa->Address.lpSockaddr->sa_family == AF_INET)
|
||||
{
|
||||
// IPv4
|
||||
if (!instance->config.localIpShowIpV4)
|
||||
continue;
|
||||
|
||||
SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr;
|
||||
char addressBuffer[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
|
||||
addNewIp(results, name, addressBuffer, false);
|
||||
addNewIp(results, name, addressBuffer, AF_INET, newIp);
|
||||
newIp = false;
|
||||
}
|
||||
else if (ifa->Address.lpSockaddr->sa_family == AF_INET6)
|
||||
{
|
||||
// IPv6
|
||||
if (!instance->config.localIpShowIpV6)
|
||||
continue;
|
||||
|
||||
SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr;
|
||||
char addressBuffer[INET6_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
|
||||
addNewIp(results, name, addressBuffer, true);
|
||||
addNewIp(results, name, addressBuffer, AF_INET6, newIp);
|
||||
newIp = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(adapter_addresses);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -15,6 +15,8 @@
|
||||
#include "util/windows/getline.h"
|
||||
#endif
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
|
||||
typedef struct CustomValue
|
||||
{
|
||||
bool printKey;
|
||||
@ -1159,8 +1161,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(optionParseModuleArgs(key, value, "battery", &instance->config.battery)) {}
|
||||
else if(optionParseModuleArgs(key, value, "poweradapter", &instance->config.powerAdapter)) {}
|
||||
else if(optionParseModuleArgs(key, value, "locale", &instance->config.locale)) {}
|
||||
else if(optionParseModuleArgs(key, value, "local-ip", &instance->config.localIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "public-ip", &instance->config.publicIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "localip", &instance->config.localIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "publicip", &instance->config.publicIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "weather", &instance->config.weather)) {}
|
||||
else if(optionParseModuleArgs(key, value, "player", &instance->config.player)) {}
|
||||
else if(optionParseModuleArgs(key, value, "media", &instance->config.media)) {}
|
||||
@ -1295,32 +1297,25 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
optionParseString(key, value, &instance->config.batteryDir);
|
||||
else if(strcasecmp(key, "--separator-string") == 0)
|
||||
optionParseString(key, value, &instance->config.separatorString);
|
||||
else if(strcasecmp(key, "--localip-v6first") == 0)
|
||||
instance->config.localIpV6First = optionParseBoolean(value);
|
||||
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
|
||||
instance->config.localIpShowIpV4 = optionParseBoolean(value);
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_IPV4_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_IPV4_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-ipv6") == 0)
|
||||
instance->config.localIpShowIpV6 = optionParseBoolean(value);
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_IPV6_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_IPV6_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-mac") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_MAC_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_MAC_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-loop") == 0)
|
||||
instance->config.localIpShowLoop = optionParseBoolean(value);
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_LOOP_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_LOOP_BIT);
|
||||
else if(strcasecmp(key, "--localip-compact") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_COMPACT_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_COMPACT_BIT);
|
||||
else if(strcasecmp(key, "--localip-name-prefix") == 0)
|
||||
optionParseString(key, value, &instance->config.localIpNamePrefix);
|
||||
else if(strcasecmp(key, "--localip-compact-type") == 0)
|
||||
{
|
||||
optionParseEnum(key, value, &instance->config.localIpCompactType,
|
||||
"none", FF_LOCALIP_COMPACT_TYPE_NONE,
|
||||
"oneline", FF_LOCALIP_COMPACT_TYPE_ONELINE,
|
||||
"multiline", FF_LOCALIP_COMPACT_TYPE_MULTILINE,
|
||||
NULL
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(key, "--os-file") == 0)
|
||||
optionParseString(key, value, &instance->config.osFile);
|
||||
else if(strcasecmp(key, "--player-name") == 0)
|
||||
optionParseString(key, value, &instance->config.playerName);
|
||||
else if(strcasecmp(key, "--public-ip-url") == 0)
|
||||
else if(strcasecmp(key, "--publicip-url") == 0)
|
||||
optionParseString(key, value, &instance->config.publicIpUrl);
|
||||
else if(strcasecmp(key, "--public-ip-timeout") == 0)
|
||||
else if(strcasecmp(key, "--publicip-timeout") == 0)
|
||||
instance->config.publicIpTimeout = optionParseUInt32(key, value);
|
||||
else if(strcasecmp(key, "--weather-output-format") == 0)
|
||||
optionParseString(key, value, &instance->config.weatherOutputFormat);
|
||||
|
@ -77,6 +77,18 @@ typedef struct FFModuleArgs
|
||||
FFstrbuf errorFormat;
|
||||
} FFModuleArgs;
|
||||
|
||||
typedef enum FFLocalIpType
|
||||
{
|
||||
FF_LOCALIP_TYPE_NONE,
|
||||
FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0,
|
||||
FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1,
|
||||
FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2,
|
||||
FF_LOCALIP_TYPE_MAC_BIT = 1 << 3,
|
||||
FF_LOCALIP_TYPE_TYPE_BIT = 1 << 4,
|
||||
|
||||
FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10,
|
||||
} FFLocalIpType;
|
||||
|
||||
typedef struct FFconfig
|
||||
{
|
||||
struct
|
||||
@ -220,12 +232,8 @@ typedef struct FFconfig
|
||||
|
||||
FFstrbuf separatorString;
|
||||
|
||||
bool localIpShowLoop;
|
||||
bool localIpV6First;
|
||||
bool localIpShowIpV4;
|
||||
bool localIpShowIpV6;
|
||||
FFstrbuf localIpNamePrefix;
|
||||
FFLocalIpCompactType localIpCompactType;
|
||||
FFLocalIpType localIpShowType;
|
||||
|
||||
FFstrbuf publicIpUrl;
|
||||
uint32_t publicIpTimeout;
|
||||
|
@ -5,28 +5,9 @@
|
||||
#define FF_LOCALIP_MODULE_NAME "Local IP"
|
||||
#define FF_LOCALIP_NUM_FORMAT_ARGS 2
|
||||
|
||||
static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
static int sortIps(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
{
|
||||
int name = ffStrbufComp(&left->name, &right->name);
|
||||
if (name != 0)
|
||||
return name;
|
||||
|
||||
if (left->ipv6 != right->ipv6)
|
||||
return left->ipv6 == false ? -1 : 1;
|
||||
|
||||
return ffStrbufComp(&left->addr, &right->addr);
|
||||
}
|
||||
|
||||
static int sortIpsV6First(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
{
|
||||
int name = ffStrbufComp(&left->name, &right->name);
|
||||
if (name != 0)
|
||||
return name;
|
||||
|
||||
if (left->ipv6 != right->ipv6)
|
||||
return left->ipv6 == true ? -1 : 1;
|
||||
|
||||
return ffStrbufComp(&left->addr, &right->addr);
|
||||
return ffStrbufComp(&left->name, &right->name);
|
||||
}
|
||||
|
||||
static void formatKey(const FFinstance* instance, const FFLocalIpResult* ip, FFstrbuf* key)
|
||||
@ -47,32 +28,27 @@ static void formatKey(const FFinstance* instance, const FFLocalIpResult* ip, FFs
|
||||
}
|
||||
}
|
||||
|
||||
static void printIpsOneline(FFlist* ips)
|
||||
static void printIp(FFLocalIpResult* ip)
|
||||
{
|
||||
uint32_t index = 0;
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, *ips)
|
||||
bool flag = false;
|
||||
if (ip->ipv4.length)
|
||||
{
|
||||
if (index++ > 0)
|
||||
putchar(' ');
|
||||
ffStrbufWriteTo(&ip->addr, stdout);
|
||||
ffStrbufWriteTo(&ip->ipv4, stdout);
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
static void printIpsWithKey(FFinstance* instance, FFlist* ips)
|
||||
{
|
||||
FFLocalIpResult* ip = (FFLocalIpResult*) ffListGet(ips, 0);
|
||||
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_MULTILINE)
|
||||
if (ip->ipv6.length)
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
formatKey(instance, ip, &key);
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
if (flag) putchar(' ');
|
||||
ffStrbufWriteTo(&ip->ipv6, stdout);
|
||||
flag = true;
|
||||
}
|
||||
if (ip->mac.length)
|
||||
{
|
||||
if (flag)
|
||||
printf(" (%s)", ip->mac.chars);
|
||||
else
|
||||
ffStrbufWriteTo(&ip->mac, stdout);
|
||||
}
|
||||
printIpsOneline(ips);
|
||||
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE)
|
||||
printf(" (%s) ", ip->name.chars);
|
||||
else
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void ffPrintLocalIp(FFinstance* instance)
|
||||
@ -94,26 +70,18 @@ void ffPrintLocalIp(FFinstance* instance)
|
||||
return;
|
||||
}
|
||||
|
||||
ffListSort(&results, instance->config.localIpV6First ? (void*) sortIpsV6First : (void*) sortIpsV4First);
|
||||
ffListSort(&results, (void*) sortIps);
|
||||
|
||||
if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE)
|
||||
if (instance->config.localIpShowType & FF_LOCALIP_TYPE_COMPACT_BIT)
|
||||
{
|
||||
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE)
|
||||
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
|
||||
|
||||
FF_LIST_AUTO_DESTROY ips;
|
||||
ffListInit(&ips, sizeof(FFLocalIpResult)); // weak references
|
||||
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
if (ips.length > 0 && !ffStrbufEqual(&ip->name, &ip[-1].name))
|
||||
{
|
||||
printIpsWithKey(instance, &ips);
|
||||
ips.length = 0;
|
||||
}
|
||||
*(FFLocalIpResult*)ffListAdd(&ips) = *ip;
|
||||
if ((void*) ip != (void*) results.data)
|
||||
fputs(" - ", stdout);
|
||||
printIp(ip);
|
||||
}
|
||||
printIpsWithKey(instance, &ips);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -126,18 +94,26 @@ void ffPrintLocalIp(FFinstance* instance)
|
||||
if(instance->config.localIP.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
ffStrbufPutTo(&ip->addr, stdout);
|
||||
printIp(ip);
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->addr},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, ip->ipv6 ? "IPv6" : "IPv4"}
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->ipv4},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->ipv6},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->mac},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name},
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&ip->name);
|
||||
ffStrbufDestroy(&ip->addr);
|
||||
}
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
ffStrbufDestroy(&ip->name);
|
||||
ffStrbufDestroy(&ip->ipv4);
|
||||
ffStrbufDestroy(&ip->ipv6);
|
||||
ffStrbufDestroy(&ip->mac);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
static inline void ffWrapFree(void* pPtr)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user