Skip to content

Commit ad026f6

Browse files
committed
LocalIP: backport mac address support
Also change `--*-ip-*` to `--*ip-*` to be consistant to my config branch
1 parent 9107d0e commit ad026f6

File tree

11 files changed

+210
-151
lines changed

11 files changed

+210
-151
lines changed

completions/bash

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ __fastfetch_complete_help()
3232
"battery-format"
3333
"poweradapter-format"
3434
"locale-format"
35-
"local-ip-format"
36-
"public-ip-format"
35+
"localip-format"
36+
"publicip-format"
3737
"player-format"
3838
"media-format"
3939
"datetime-format"
@@ -235,8 +235,8 @@ __fastfetch_completion()
235235
"--set-keyless"
236236
"--player-name"
237237
"--percent-type"
238-
"--public-ip-url"
239-
"--public-ip-timeout"
238+
"--publicip-url"
239+
"--publicip-timeout"
240240
"--weather-output-format"
241241
"--weather-timeout"
242242
"--os-key"
@@ -317,12 +317,12 @@ __fastfetch_completion()
317317
"--locale-key"
318318
"--locale-format"
319319
"--locale-error"
320-
"--local-ip-key"
321-
"--local-ip-format"
322-
"--local-ip-error"
323-
"--public-ip-key"
324-
"--public-ip-format"
325-
"--public-ip-error"
320+
"--localip-key"
321+
"--localip-format"
322+
"--localip-error"
323+
"--publicip-key"
324+
"--publicip-format"
325+
"--publicip-error"
326326
"--wifi-key"
327327
"--wifi-format"
328328
"--wifi-error"

src/common/init.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,8 @@ static void defaultConfig(FFinstance* instance)
174174

175175
ffStrbufInitA(&instance->config.separatorString, 0);
176176

177-
instance->config.localIpShowIpV4 = true;
178-
instance->config.localIpShowIpV6 = false;
179-
instance->config.localIpShowLoop = false;
180-
instance->config.localIpV6First = false;
177+
instance->config.localIpShowType = FF_LOCALIP_TYPE_IPV4_BIT;
181178
ffStrbufInit(&instance->config.localIpNamePrefix);
182-
instance->config.localIpCompactType = FF_LOCALIP_COMPACT_TYPE_NONE;
183179

184180
instance->config.publicIpTimeout = 0;
185181
ffStrbufInit(&instance->config.publicIpUrl);

src/data/config_user.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@
168168
# Sets the URL of public IP detection server to be used.
169169
# Only HTTP protocol is supported, and the value should not contain "http://" prefix.
170170
# Default is "ipinfo.io/ip".
171-
#--public-ip-url "ipinfo.io/ip"
171+
#--publicip-url "ipinfo.io/ip"
172172

173173
# Public IP timeout option:
174174
# Sets the time to wait for the public ip server to respond.
175175
# Must be a positive integer.
176176
# Default is 0 (disabled).
177-
#--public-ip-timeout 0
177+
#--publicip-timeout 0
178178

179179
# Weather output format option:
180180
# Sets the weather format to be used. It must be URI encoded.
@@ -321,8 +321,8 @@
321321
#--battery-key Battery {1}
322322
#--poweradapter-key Power Adapter {1}
323323
#--locale-key Locale
324-
#--local-ip-key Local IP ({1})
325-
#--public-ip-key Public IP
324+
#--localip-key Local IP ({1})
325+
#--publicip-key Public IP
326326
#--wifi-key Wifi
327327
#--weather-key Weather
328328
#--player-key Media Player
@@ -369,8 +369,8 @@
369369
#--battery-format
370370
#--poweradapter-format
371371
#--locale-format
372-
#--local-ip-format
373-
#--public-ip-format
372+
#--localip-format
373+
#--publicip-format
374374
#--weather-format
375375
#--player-format
376376
#--media-format
@@ -416,8 +416,8 @@
416416
#--battery-error
417417
#--poweradapter-error
418418
#--locale-error
419-
#--local-ip-error
420-
#--public-ip-error
419+
#--localip-error
420+
#--publicip-error
421421
#--weather-error
422422
#--player-error
423423
#--media-error

src/data/help.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ Module specific options:
126126
--battery-temp <?value>: Detect and display Battery temperature if supported. Default is false
127127
--localip-show-ipv4 <?value>: Show IPv4 addresses in local ip module. Default is true
128128
--localip-show-ipv6 <?value>: Show IPv6 addresses in local ip module. Default is false
129+
--localip-show-mac <?value>: Show mac addresses in local ip module. Default is false
129130
--localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false
130131
--localip-name-prefix <str>: Show IPs with given name prefix only. Default is empty
131-
--localip-compact-type <str>: Show IPs in one line. Should be either none, oneline, multiline. Default is none
132-
--localip-v6first <?value>: Set if ipv6 should be printed first. Default is false
133-
--public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
134-
--public-ip-url: The URL of public IP detection server to be used.
132+
--localip-compact <?value>: Show all IPs in one line. Default is false
133+
--publicip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
134+
--publicip-url: The URL of public IP detection server to be used.
135135
--weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0)
136136
--weather-output-format: The output weather format to be used. It must be URI encoded.
137137
--player-name: The name of the player to use

src/detection/localip/localip.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
typedef struct FFLocalIpResult
99
{
1010
FFstrbuf name;
11-
FFstrbuf addr;
12-
bool ipv6;
11+
FFstrbuf ipv4;
12+
FFstrbuf ipv6;
13+
FFstrbuf mac;
1314
} FFLocalIpResult;
1415

1516
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results);

src/detection/localip/localip_linux.c

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,43 @@
77
#include <netinet/in.h>
88
#include <arpa/inet.h>
99

10-
static void addNewIp(FFlist* list, const char* name, const char* addr, bool ipv6)
10+
#if defined(__FreeBSD__) || defined(__APPLE__)
11+
#include <net/if_dl.h>
12+
#else
13+
#include <linux/if_packet.h>
14+
#endif
15+
16+
static void addNewIp(FFlist* list, const char* name, const char* addr, int type)
1117
{
12-
FFLocalIpResult* ip = (FFLocalIpResult*) ffListAdd(list);
13-
ffStrbufInitS(&ip->name, name);
14-
ffStrbufInitS(&ip->addr, addr);
15-
ip->ipv6 = ipv6;
18+
FFLocalIpResult* ip = NULL;
19+
20+
FF_LIST_FOR_EACH(FFLocalIpResult, temp, *list)
21+
{
22+
if (!ffStrbufEqualS(&temp->name, name)) continue;
23+
ip = temp;
24+
break;
25+
}
26+
if (!ip)
27+
{
28+
ip = (FFLocalIpResult*) ffListAdd(list);
29+
ffStrbufInitS(&ip->name, name);
30+
ffStrbufInit(&ip->ipv4);
31+
ffStrbufInit(&ip->ipv6);
32+
ffStrbufInit(&ip->mac);
33+
}
34+
35+
switch (type)
36+
{
37+
case AF_INET:
38+
ffStrbufSetS(&ip->ipv4, addr);
39+
break;
40+
case AF_INET6:
41+
ffStrbufSetS(&ip->ipv6, addr);
42+
break;
43+
case -1:
44+
ffStrbufSetS(&ip->mac, addr);
45+
break;
46+
}
1647
}
1748

1849
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
@@ -26,35 +57,57 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
2657
if (!ifa->ifa_addr || !(ifa->ifa_flags & IFF_RUNNING))
2758
continue;
2859

29-
// loop back
30-
if ((ifa->ifa_flags & IFF_LOOPBACK) && !instance->config.localIpShowLoop)
60+
if ((ifa->ifa_flags & IFF_LOOPBACK) && !(instance->config.localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT))
3161
continue;
3262

3363
if (instance->config.localIpNamePrefix.length && strncmp(ifa->ifa_name, instance->config.localIpNamePrefix.chars, instance->config.localIpNamePrefix.length) != 0)
3464
continue;
3565

3666
if (ifa->ifa_addr->sa_family == AF_INET)
3767
{
38-
// IPv4
39-
if (!instance->config.localIpShowIpV4)
68+
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT))
4069
continue;
4170

4271
struct sockaddr_in* ipv4 = (struct sockaddr_in*) ifa->ifa_addr;
4372
char addressBuffer[INET_ADDRSTRLEN];
4473
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
45-
addNewIp(results, ifa->ifa_name, addressBuffer, false);
74+
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET);
4675
}
4776
else if (ifa->ifa_addr->sa_family == AF_INET6)
4877
{
49-
// IPv6
50-
if (!instance->config.localIpShowIpV6)
78+
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT))
5179
continue;
5280

5381
struct sockaddr_in6* ipv6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5482
char addressBuffer[INET6_ADDRSTRLEN];
5583
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
56-
addNewIp(results, ifa->ifa_name, addressBuffer, true);
84+
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6);
85+
}
86+
#if defined(__FreeBSD__) || defined(__APPLE__)
87+
else if (ifa->ifa_addr->sa_family == AF_LINK)
88+
{
89+
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT))
90+
continue;
91+
92+
char addressBuffer[32];
93+
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr);
94+
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
95+
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
96+
addNewIp(results, ifa->ifa_name, addressBuffer, -1);
97+
}
98+
#else
99+
else if (ifa->ifa_addr->sa_family == AF_PACKET)
100+
{
101+
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT))
102+
continue;
103+
104+
char addressBuffer[32];
105+
uint8_t* ptr = ((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr;
106+
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
107+
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
108+
addNewIp(results, ifa->ifa_name, addressBuffer, -1);
57109
}
110+
#endif
58111
}
59112

60113
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
#include <winsock2.h>
22
#include <ws2tcpip.h>
33
#include <iphlpapi.h>
4-
#include <wchar.h>
54

5+
#include "util/mallocHelper.h"
6+
#include "util/windows/unicode.h"
67
#include "localip.h"
78

8-
static void addNewIp(FFlist* list, const char* name, const char* addr, bool ipv6)
9+
static void addNewIp(FFlist* list, const char* name, const char* value, int type, bool newIp)
910
{
10-
FFLocalIpResult* ip = (FFLocalIpResult*) ffListAdd(list);
11-
ffStrbufInitS(&ip->name, name);
12-
ffStrbufInitS(&ip->addr, addr);
13-
ip->ipv6 = ipv6;
11+
FFLocalIpResult* ip = NULL;
12+
13+
if (newIp)
14+
{
15+
ip = (FFLocalIpResult*) ffListAdd(list);
16+
ffStrbufInitS(&ip->name, name);
17+
ffStrbufInit(&ip->ipv4);
18+
ffStrbufInit(&ip->ipv6);
19+
ffStrbufInit(&ip->mac);
20+
}
21+
else
22+
{
23+
ip = ffListGet(list, list->length - 1);
24+
}
25+
26+
switch (type)
27+
{
28+
case AF_INET:
29+
ffStrbufSetS(&ip->ipv4, value);
30+
break;
31+
case AF_INET6:
32+
ffStrbufSetS(&ip->ipv6, value);
33+
break;
34+
case -1:
35+
ffStrbufSetS(&ip->mac, value);
36+
break;
37+
}
1438
}
1539

1640
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
1741
{
18-
IP_ADAPTER_ADDRESSES* adapter_addresses = NULL;
42+
IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL;
1943

2044
// Start with a 16 KB buffer and resize if needed -
2145
// multiple attempts in case interfaces change while
@@ -27,11 +51,10 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
2751
assert(adapter_addresses);
2852

2953
DWORD error = GetAdaptersAddresses(
30-
AF_UNSPEC,
31-
GAA_FLAG_SKIP_ANYCAST |
32-
GAA_FLAG_SKIP_MULTICAST |
33-
GAA_FLAG_SKIP_DNS_SERVER |
34-
GAA_FLAG_SKIP_FRIENDLY_NAME,
54+
instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT
55+
? instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
56+
: AF_INET6,
57+
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
3558
NULL,
3659
adapter_addresses,
3760
&adapter_addresses_buffer_size);
@@ -47,41 +70,47 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
4770
// Iterate through all of the adapters
4871
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
4972
{
50-
if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK && !instance->config.localIpShowLoop)
73+
bool isLoop = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK;
74+
if (isLoop && !(instance->config.localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT))
5175
continue;
5276

77+
bool newIp = true;
78+
5379
char name[128];
5480
WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, sizeof(name), NULL, NULL);
5581
if (instance->config.localIpNamePrefix.length && strncmp(name, instance->config.localIpNamePrefix.chars, instance->config.localIpNamePrefix.length) != 0)
5682
continue;
5783

84+
if (instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT && adapter->PhysicalAddressLength == 6)
85+
{
86+
char addressBuffer[32];
87+
uint8_t* ptr = adapter->PhysicalAddress;
88+
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
89+
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
90+
addNewIp(results, name, addressBuffer, -1, newIp);
91+
newIp = false;
92+
}
93+
5894
for (IP_ADAPTER_UNICAST_ADDRESS* ifa = adapter->FirstUnicastAddress; ifa; ifa = ifa->Next)
5995
{
6096
if (ifa->Address.lpSockaddr->sa_family == AF_INET)
6197
{
62-
// IPv4
63-
if (!instance->config.localIpShowIpV4)
64-
continue;
65-
6698
SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr;
6799
char addressBuffer[INET_ADDRSTRLEN];
68100
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
69-
addNewIp(results, name, addressBuffer, false);
101+
addNewIp(results, name, addressBuffer, AF_INET, newIp);
102+
newIp = false;
70103
}
71104
else if (ifa->Address.lpSockaddr->sa_family == AF_INET6)
72105
{
73-
// IPv6
74-
if (!instance->config.localIpShowIpV6)
75-
continue;
76-
77106
SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr;
78107
char addressBuffer[INET6_ADDRSTRLEN];
79108
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
80-
addNewIp(results, name, addressBuffer, true);
109+
addNewIp(results, name, addressBuffer, AF_INET6, newIp);
110+
newIp = false;
81111
}
82112
}
83113
}
84114

85-
free(adapter_addresses);
86115
return NULL;
87116
}

0 commit comments

Comments
 (0)