Merge pull request #1911 from fastfetch-cli/dev

Release: v2.50.2
This commit is contained in:
Carter Li
2025-08-16 21:50:39 +08:00
committed by GitHub
11 changed files with 669 additions and 275 deletions
+3
View File
@@ -4,6 +4,9 @@ on:
- push
- pull_request
env:
CMAKE_BUILD_TYPE: ${{ vars.CMAKE_BUILD_TYPE || 'RelWithDebInfo' }}
jobs:
spellcheck:
runs-on: ubuntu-latest
+8
View File
@@ -1,3 +1,11 @@
# 2.50.2
Bugfixes:
* Fixes linglong package detection V2 (#1903, Packages, Linux)
* Fixes building with `-DENABLE_SYSTEM_YYJSON=ON` (#1904)
* Fixes `showMac` does not honor `defaultRouteOnly` (#1902, LocalIP, Linux)
* Fixes failing to acquire default route on Linux in certain cases (#1902, LocalIP, Linux)
# 2.50.1
Bugfixes:
+1 -1
View File
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.12.0) # target_link_libraries with OBJECT libs & project homepage url
project(fastfetch
VERSION 2.50.1
VERSION 2.50.2
LANGUAGES C
DESCRIPTION "Fast neofetch-like system information tool"
HOMEPAGE_URL "https://github.com/fastfetch-cli/fastfetch"
+106 -45
View File
@@ -1,34 +1,41 @@
#include "netif.h"
#include "common/io/io.h"
#include "util/mallocHelper.h"
#include "util/debug.h"
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#define FF_STR_INDIR(x) #x
#define FF_STR(x) FF_STR_INDIR(x)
bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
{
FF_DEBUG("Starting IPv4 default route detection");
FF_AUTO_CLOSE_FD int sock_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (sock_fd < 0)
{
FF_DEBUG("Failed to create netlink socket: %s", strerror(errno));
return false;
}
FF_DEBUG("Created netlink socket: fd=%d", sock_fd);
unsigned pid = (unsigned) getpid();
FF_DEBUG("Process PID: %u", pid);
// Bind socket
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_pid = pid,
.nl_pid = 0, // Let kernel choose PID
.nl_groups = 0, // No multicast groups
};
if (bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
FF_DEBUG("Failed to bind socket: %s", strerror(errno));
return false;
}
FF_DEBUG("Successfully bound socket");
struct {
struct __attribute__((__packed__)) {
struct nlmsghdr nlh;
struct rtmsg rtm;
struct rtattr rta;
@@ -72,39 +79,37 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
(struct sockaddr*)&dest_addr, sizeof(dest_addr));
if (sent != sizeof(req)) {
FF_DEBUG("Failed to send netlink request: sent=%zd, expected=%zu", sent, sizeof(req));
return false;
}
FF_DEBUG("Sent netlink request: %zd bytes", sent);
struct sockaddr_nl src_addr = {};
socklen_t src_addr_len = sizeof(src_addr);
struct iovec iov = {};
struct msghdr msg = {
.msg_name = &src_addr,
.msg_namelen = sizeof(src_addr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
uint8_t buffer[1024 * 16]; // 16 KB buffer should be sufficient
ssize_t peek_size = recvmsg(sock_fd, &msg, MSG_PEEK | MSG_TRUNC);
if (peek_size < 0) {
return false;
}
FF_AUTO_FREE uint8_t* buffer = malloc((size_t)peek_size);
ssize_t received = recvfrom(sock_fd, buffer, (size_t)peek_size, 0,
ssize_t received = recvfrom(sock_fd, buffer, sizeof(buffer), 0,
(struct sockaddr*)&src_addr, &src_addr_len);
if (received != peek_size) {
if (received < 0) {
FF_DEBUG("Failed to receive netlink response: %s", strerror(errno));
return false;
}
if (received >= (ssize_t)sizeof(buffer)) {
FF_DEBUG("Failed to receive complete message (possible truncation)");
return false;
}
FF_DEBUG("Received netlink response: %zd bytes", received);
struct {
uint32_t metric;
uint32_t ifindex;
uint32_t prefsrc;
} entry;
uint32_t minMetric = UINT32_MAX;
int routeCount = 0;
for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer;
NLMSG_OK(nlh, received);
@@ -112,11 +117,21 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
if (nlh->nlmsg_seq != 1 || nlh->nlmsg_pid != pid)
continue;
if (nlh->nlmsg_type == NLMSG_DONE)
{
FF_DEBUG("Received NLMSG_DONE, processed %d routes", routeCount);
break;
}
if (nlh->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(nlh);
FF_DEBUG("Netlink reports error: %s", strerror(-err->error));
continue;
}
if (nlh->nlmsg_type != RTM_NEWROUTE)
continue;
routeCount++;
struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh);
if (rtm->rtm_family != AF_INET)
continue;
@@ -124,7 +139,8 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
if (rtm->rtm_dst_len != 0)
continue;
entry = (__typeof__(entry)) { .metric = UINT32_MAX };
FF_DEBUG("Processing IPv4 default route candidate #%d", routeCount);
entry = (__typeof__(entry)) { }; // Default to zero metric (no RTA_PRIORITY found)
// Parse route attributes
size_t rtm_len = RTM_PAYLOAD(nlh);
@@ -138,62 +154,84 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
uint32_t rta_data = *(uint32_t*) RTA_DATA(rta);
switch (rta->rta_type) {
case RTA_DST:
if (rta_data != 0) goto next;
break;
FF_DEBUG("Unexpected RTA_DST: %s (len=%u)", inet_ntoa((struct in_addr) { .s_addr = rta_data }), rtm->rtm_dst_len);
goto next;
case RTA_OIF:
entry.ifindex = rta_data;
FF_DEBUG("Found interface index: %u", entry.ifindex);
break;
case RTA_GATEWAY:
FF_DEBUG("Found gateway: %s", inet_ntoa(*(struct in_addr*)&rta_data));
if (rta_data == 0) goto next;
break;
case RTA_PRIORITY:
FF_DEBUG("Found metric: %u", rta_data);
if (rta_data >= minMetric) goto next;
entry.metric = rta_data;
break;
case RTA_PREFSRC:
entry.prefsrc = rta_data;
FF_DEBUG("Found preferred source: %s", inet_ntoa(*(struct in_addr*)&rta_data));
break;
}
}
if (entry.metric >= minMetric)
if (entry.ifindex == 0 || entry.metric >= minMetric)
{
next:
FF_DEBUG("Skipping route: ifindex=%u, metric=%u", entry.ifindex, entry.metric);
continue;
}
minMetric = entry.metric;
result->ifIndex = entry.ifindex;
FF_DEBUG("Updated best route: ifindex=%u, metric=%u, prefsrc=%x", entry.ifindex, entry.metric, entry.prefsrc);
result->preferredSourceAddrV4 = entry.prefsrc;
if (minMetric == 0)
{
FF_DEBUG("Found zero metric route, stopping further processing");
break; // Stop processing if we found a zero metric route
}
}
if (minMetric < UINT32_MAX)
{
if_indextoname(result->ifIndex, result->ifName);
FF_DEBUG("Found default IPv4 route: interface=%s, index=%u, metric=%u", result->ifName, result->ifIndex, minMetric);
return true;
}
FF_DEBUG("No IPv4 default route found");
return false;
}
bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
{
FF_DEBUG("Starting IPv6 default route detection");
FF_AUTO_CLOSE_FD int sock_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (sock_fd < 0)
{
FF_DEBUG("Failed to create netlink socket: %s", strerror(errno));
return false;
}
FF_DEBUG("Created netlink socket: fd=%d", sock_fd);
unsigned pid = (unsigned) getpid();
FF_DEBUG("Process PID: %u", pid);
// Bind socket
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_pid = pid,
.nl_pid = 0, // Let kernel choose PID
.nl_groups = 0, // No multicast groups
};
if (bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
FF_DEBUG("Failed to bind socket: %s", strerror(errno));
return false;
}
FF_DEBUG("Successfully bound socket");
struct {
struct __attribute__((__packed__)) {
struct nlmsghdr nlh;
struct rtmsg rtm;
struct rtattr rta;
@@ -237,30 +275,26 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
(struct sockaddr*)&dest_addr, sizeof(dest_addr));
if (sent != sizeof(req)) {
FF_DEBUG("Failed to send netlink request: sent=%zd, expected=%zu", sent, sizeof(req));
return false;
}
FF_DEBUG("Sent netlink request: %zd bytes", sent);
struct sockaddr_nl src_addr = {};
socklen_t src_addr_len = sizeof(src_addr);
struct iovec iov = {};
struct msghdr msg = {
.msg_name = &src_addr,
.msg_namelen = sizeof(src_addr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
uint8_t buffer[1024 * 16]; // 16 KB buffer should be sufficient
ssize_t peek_size = recvmsg(sock_fd, &msg, MSG_PEEK | MSG_TRUNC);
if (peek_size < 0) {
ssize_t received = recvfrom(sock_fd, buffer, sizeof(buffer), 0,
(struct sockaddr*)&src_addr, &src_addr_len);
if (received < 0) {
FF_DEBUG("Failed to receive netlink response: %s", strerror(errno));
return false;
}
FF_AUTO_FREE uint8_t* buffer = malloc((size_t)peek_size);
ssize_t received = recvfrom(sock_fd, buffer, (size_t)peek_size, 0,
(struct sockaddr*)&src_addr, &src_addr_len);
if (received != peek_size) {
if (received >= (ssize_t)sizeof(buffer)) {
FF_DEBUG("Failed to receive complete message (possible truncation)");
return false;
}
@@ -269,6 +303,7 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
uint32_t ifindex;
} entry;
uint32_t minMetric = UINT32_MAX;
int routeCount = 0;
for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer;
NLMSG_OK(nlh, received);
@@ -276,11 +311,21 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
if (nlh->nlmsg_seq != 1 || nlh->nlmsg_pid != pid)
continue;
if (nlh->nlmsg_type == NLMSG_DONE)
{
FF_DEBUG("Received NLMSG_DONE, processed %d routes", routeCount);
break;
}
if (nlh->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(nlh);
FF_DEBUG("Netlink reports error: %s", strerror(-err->error));
continue;
}
if (nlh->nlmsg_type != RTM_NEWROUTE)
continue;
routeCount++;
struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh);
if (rtm->rtm_family != AF_INET6)
continue;
@@ -288,7 +333,8 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
if (rtm->rtm_dst_len != 0)
continue;
entry = (__typeof__(entry)) { .metric = UINT32_MAX };
FF_DEBUG("Processing IPv6 default route candidate #%d", routeCount);
entry = (__typeof__(entry)) { }; // Default to zero metric (no RTA_PRIORITY found)
// Parse route attributes
size_t rtm_len = RTM_PAYLOAD(nlh);
@@ -299,24 +345,29 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
switch (rta->rta_type) {
case RTA_DST:
if (RTA_PAYLOAD(rta) >= sizeof(struct in6_addr)) {
struct in6_addr* dst = (struct in6_addr*) RTA_DATA(rta);
if (!IN6_IS_ADDR_UNSPECIFIED(dst)) goto next;
FF_MAYBE_UNUSED char str[INET6_ADDRSTRLEN];
FF_DEBUG("Unexpected RTA_DST: %s", inet_ntop(AF_INET6, RTA_DATA(rta), str, sizeof(str)));
goto next;
}
break;
case RTA_OIF:
if (RTA_PAYLOAD(rta) >= sizeof(uint32_t)) {
entry.ifindex = *(uint32_t*) RTA_DATA(rta);
FF_DEBUG("Found interface index: %u", entry.ifindex);
}
break;
case RTA_GATEWAY:
if (RTA_PAYLOAD(rta) >= sizeof(struct in6_addr)) {
struct in6_addr* gw = (struct in6_addr*) RTA_DATA(rta);
if (IN6_IS_ADDR_UNSPECIFIED(gw)) goto next;
FF_MAYBE_UNUSED char str[INET6_ADDRSTRLEN];
FF_DEBUG("Found gateway: %s", inet_ntop(AF_INET6, gw, str, sizeof(str)));
}
break;
case RTA_PRIORITY:
if (RTA_PAYLOAD(rta) >= sizeof(uint32_t)) {
uint32_t metric = *(uint32_t*) RTA_DATA(rta);
FF_DEBUG("Found metric: %u", metric);
if (metric >= minMetric) goto next;
entry.metric = metric;
}
@@ -324,19 +375,29 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
}
}
if (entry.metric >= minMetric)
if (entry.ifindex == 0 || entry.metric >= minMetric)
{
next:
FF_DEBUG("Skipping route: ifindex=%u, metric=%u", entry.ifindex, entry.metric);
continue;
}
minMetric = entry.metric;
result->ifIndex = entry.ifindex;
FF_DEBUG("Updated best route: ifindex=%u, metric=%u", entry.ifindex, entry.metric);
if (minMetric == 0)
{
FF_DEBUG("Found zero metric route, stopping further processing");
break; // Stop processing if we found a zero metric route
}
}
if (minMetric < UINT32_MAX)
{
if_indextoname(result->ifIndex, result->ifName);
FF_DEBUG("Found default IPv6 route: interface=%s, index=%u, metric=%u", result->ifName, result->ifIndex, minMetric);
return true;
}
FF_DEBUG("No IPv6 default route found");
return false;
}
+1 -1
View File
@@ -12,7 +12,7 @@ typedef struct FFLocalIpResult
FFstrbuf flags;
int32_t mtu;
int32_t speed; // in Mbps
bool defaultRoute;
FFLocalIpType defaultRoute;
} FFLocalIpResult;
typedef struct FFLocalIpNIFlag
+390 -147
View File
@@ -2,6 +2,7 @@
#include "common/io/io.h"
#include "common/netif/netif.h"
#include "util/stringUtils.h"
#include "util/debug.h"
#include <string.h>
#include <ctype.h>
@@ -108,58 +109,24 @@ static const FFLocalIpNIFlag niFlagOptions[] = {
{},
};
static void addNewIp(FFlist* list, const char* name, const char* addr, int type, bool defaultRoute, uint64_t flags, bool firstOnly)
typedef enum __attribute__((__packed__)) FFIPv6Type
{
FFLocalIpResult* ip = NULL;
FF_IPV6_Other,
FF_IPV6_GUA = 0b0001,
FF_IPV6_GUA_SECONDARY = 0b0101,
FF_IPV6_ULA = 0b0010,
FF_IPV6_ULA_SECONDARY = 0b0110,
FF_IPV6_TYPE_MASK = 0b0011,
FF_IPV6_SECONDARY_FLAG = 0b1100,
FF_IPV6_PREFERRED = UINT8_MAX,
} FFIPv6Type;
FF_LIST_FOR_EACH(FFLocalIpResult, temp, *list)
{
if (!ffStrbufEqualS(&temp->name, name)) continue;
ip = temp;
if (defaultRoute) ip->defaultRoute = true;
break;
}
if (!ip)
{
ip = (FFLocalIpResult*) ffListAdd(list);
ffStrbufInitS(&ip->name, name);
ffStrbufInit(&ip->ipv4);
ffStrbufInit(&ip->ipv6);
ffStrbufInit(&ip->mac);
ffStrbufInit(&ip->flags);
ip->defaultRoute = defaultRoute;
ip->mtu = -1;
ip->speed = -1;
ffLocalIpFillNIFlags(&ip->flags, flags, niFlagOptions);
}
switch (type)
{
case AF_INET:
if (ip->ipv4.length)
{
if (firstOnly) return;
ffStrbufAppendC(&ip->ipv4, ',');
}
ffStrbufAppendS(&ip->ipv4, addr);
break;
case AF_INET6:
if (ip->ipv6.length)
{
if (firstOnly) return;
ffStrbufAppendC(&ip->ipv6, ',');
}
ffStrbufAppendS(&ip->ipv6, addr);
break;
case -1:
ffStrbufSetS(&ip->mac, addr);
break;
}
}
static bool isIPv6AddressPreferred(struct ifaddrs* ifa, struct sockaddr_in6* addr)
static FFIPv6Type getIpType(struct ifaddrs* ifa)
{
struct sockaddr_in6* addr = (struct sockaddr_in6*) ifa->ifa_addr;
FF_DEBUG("Checking IPv6 type for interface %s", ifa->ifa_name);
#ifndef IN6_IS_ADDR_GLOBAL
#define IN6_IS_ADDR_GLOBAL(a) \
((((const uint32_t *) (a))[0] & htonl(0x70000000)) == htonl(0x20000000))
@@ -168,8 +135,22 @@ static bool isIPv6AddressPreferred(struct ifaddrs* ifa, struct sockaddr_in6* add
#define IN6_IS_ADDR_UNIQUE_LOCAL(a) \
((((const uint32_t *) (a))[0] & htonl(0xfe000000)) == htonl(0xfc000000))
#endif
if (!IN6_IS_ADDR_GLOBAL(&addr->sin6_addr) && !IN6_IS_ADDR_UNIQUE_LOCAL(&addr->sin6_addr))
return false;
FFIPv6Type result = FF_IPV6_Other;
if (IN6_IS_ADDR_GLOBAL(&addr->sin6_addr))
{
result = FF_IPV6_GUA;
FF_DEBUG("Interface %s has Global Unicast Address", ifa->ifa_name);
}
else if (IN6_IS_ADDR_UNIQUE_LOCAL(&addr->sin6_addr))
{
result = FF_IPV6_ULA;
FF_DEBUG("Interface %s has Unique Local Address", ifa->ifa_name);
}
else
{
FF_DEBUG("Interface %s has other IPv6 address type", ifa->ifa_name);
return FF_IPV6_Other;
}
#ifdef SIOCGIFAFLAG_IN6
static int sockfd = 0;
@@ -184,33 +165,33 @@ static bool isIPv6AddressPreferred(struct ifaddrs* ifa, struct sockaddr_in6* add
if (sockfd > 0) fcntl(sockfd, F_SETFD, FD_CLOEXEC);
#endif
}
if (sockfd < 0) return true; // Give up
if (sockfd < 0) return result;
struct in6_ifreq ifr6 = {};
ffStrCopy(ifr6.ifr_name, ifa->ifa_name, IFNAMSIZ);
ifr6.ifr_addr = *addr;
if (ioctl(sockfd, SIOCGIFAFLAG_IN6, &ifr6) != 0)
return true;
return result;
#ifdef IN6_IFF_PREFER_SOURCE
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_PREFER_SOURCE)
return true;
return FF_IPV6_PREFERRED;
#endif
return !(ifr6.ifr_ifru.ifru_flags6 & (IN6_IFF_DEPRECATED | IN6_IFF_TEMPORARY | IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED
if (ifr6.ifr_ifru.ifru_flags6 & (IN6_IFF_DEPRECATED | IN6_IFF_TEMPORARY | IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED
#ifdef IN6_IFF_OPTIMISTIC
| IN6_IFF_OPTIMISTIC
#endif
));
)) result |= FF_IPV6_SECONDARY_FLAG;
return result;
#elif __linux__
FF_UNUSED(ifa);
static FFlist addresses = {};
if (addresses.elementSize == 0)
{
ffListInit(&addresses, sizeof(struct in6_addr));
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
if (!ffReadFileBuffer("/proc/net/if_inet6", &buffer))
return true;
return result;
char* line = NULL;
size_t len = 0;
@@ -230,146 +211,374 @@ static bool isIPv6AddressPreferred(struct ifaddrs* ifa, struct sockaddr_in6* add
--addresses.length;
}
}
if (addresses.capacity == 0) return true; // Give up
if (addresses.capacity == 0) return result;
FF_LIST_FOR_EACH(struct in6_addr, entry, addresses)
{
if (memcmp(&addr->sin6_addr, entry, sizeof(struct in6_addr)) == 0)
return true;
return result;
}
return false;
result |= FF_IPV6_SECONDARY_FLAG;
return result;
#elif __sun
if (ifa->ifa_flags & IFF_PREFERRED)
return true;
return !(ifa->ifa_flags & (IFF_DEPRECATED | IFF_TEMPORARY | IFF_DUPLICATE));
return FF_IPV6_PREFERRED;
if (ifa->ifa_flags & (IFF_DEPRECATED | IFF_TEMPORARY | IFF_DUPLICATE))
result |= FF_IPV6_SECONDARY_FLAG;
return result;
#else
return true;
return result;
#endif
}
typedef struct {
struct ifaddrs* mac;
FFlist /*<struct ifaddrs*>*/ ipv4;
FFlist /*<struct ifaddrs*>*/ ipv6;
} FFAdapter;
static void appendIpv4(const FFLocalIpOptions* options, FFstrbuf* buffer, const struct ifaddrs* ifa)
{
struct sockaddr_in* ipv4 = (struct sockaddr_in*) ifa->ifa_addr;
char addressBuffer[INET_ADDRSTRLEN + 16];
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
FF_DEBUG("Adding IPv4 address %s for interface %s", addressBuffer, ifa->ifa_name);
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
{
struct sockaddr_in* netmask = (struct sockaddr_in*) ifa->ifa_netmask;
int cidr = __builtin_popcount(netmask->sin_addr.s_addr);
if (cidr != 0)
{
size_t len = strlen(addressBuffer);
snprintf(addressBuffer + len, 16, "/%d", cidr);
}
}
if (buffer->length) ffStrbufAppendC(buffer, ',');
ffStrbufAppendS(buffer, addressBuffer);
}
static void appendIpv6(const FFLocalIpOptions* options, FFstrbuf* buffer, const struct ifaddrs* ifa)
{
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*) ifa->ifa_addr;
char addressBuffer[INET6_ADDRSTRLEN + 16];
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
FF_DEBUG("Adding IPv6 address %s for interface %s", addressBuffer, ifa->ifa_name);
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
{
struct sockaddr_in6* netmask = (struct sockaddr_in6*) ifa->ifa_netmask;
int cidr = 0;
static_assert(sizeof(netmask->sin6_addr) % sizeof(uint64_t) == 0, "");
for (uint32_t i = 0; i < sizeof(netmask->sin6_addr) / sizeof(uint64_t); ++i)
cidr += __builtin_popcountll(((uint64_t*) &netmask->sin6_addr)[i]);
if (cidr != 0)
{
size_t len = strlen(addressBuffer);
snprintf(addressBuffer + len, 16, "/%d", cidr);
}
}
if (buffer->length) ffStrbufAppendC(buffer, ',');
ffStrbufAppendS(buffer, addressBuffer);
}
const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
{
FF_DEBUG("Starting local IP detection with showType=0x%x, namePrefix='%s'",
options->showType, options->namePrefix.chars);
struct ifaddrs* ifAddrStruct = NULL;
if(getifaddrs(&ifAddrStruct) < 0)
{
FF_DEBUG("getifaddrs() failed");
return "getifaddrs(&ifAddrStruct) failed";
}
FF_DEBUG("Successfully retrieved interface addresses");
FF_LIST_AUTO_DESTROY adapters = ffListCreate(sizeof(FFAdapter));
for (struct ifaddrs* ifa = ifAddrStruct; ifa; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr)
{
FF_DEBUG("Skipping interface %s (no address)", ifa->ifa_name);
continue;
}
#ifdef IFF_RUNNING
#ifdef IFF_RUNNING
if (!(ifa->ifa_flags & IFF_RUNNING))
{
FF_DEBUG("Skipping interface %s (not running)", ifa->ifa_name);
continue;
#endif
}
#endif
if ((ifa->ifa_flags & IFF_LOOPBACK) && !(options->showType & FF_LOCALIP_TYPE_LOOP_BIT))
{
FF_DEBUG("Skipping loopback interface %s", ifa->ifa_name);
continue;
}
if (options->namePrefix.length && strncmp(ifa->ifa_name, options->namePrefix.chars, options->namePrefix.length) != 0)
continue;
uint64_t flags = options->showType & FF_LOCALIP_TYPE_FLAGS_BIT ? ifa->ifa_flags : 0;
if (ifa->ifa_addr->sa_family == AF_INET)
{
if (!(options->showType & FF_LOCALIP_TYPE_IPV4_BIT))
continue;
FF_DEBUG("Skipping interface %s (doesn't match prefix '%s')",
ifa->ifa_name, options->namePrefix.chars);
continue;
}
struct sockaddr_in* ipv4 = (struct sockaddr_in*) ifa->ifa_addr;
if (!(options->showType & FF_LOCALIP_TYPE_MAC_BIT) &&
ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6)
{
FF_DEBUG("Skipping interface %s (unsupported address family %d)",
ifa->ifa_name, ifa->ifa_addr->sa_family);
continue;
}
if (options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT)
{
// If the interface is not the default route for either IPv4 or IPv6, skip it
if (!((options->showType & FF_LOCALIP_TYPE_IPV4_BIT) && ffStrEquals(ffNetifGetDefaultRouteV4()->ifName, ifa->ifa_name)) &&
!((options->showType & FF_LOCALIP_TYPE_IPV6_BIT) && ffStrEquals(ffNetifGetDefaultRouteV6()->ifName, ifa->ifa_name)))
{
FF_DEBUG("Skipping interface %s (not default route interface)", ifa->ifa_name);
continue;
}
}
FF_DEBUG("Processing interface %s (family=%d, flags=0x%x)",
ifa->ifa_name, ifa->ifa_addr->sa_family, ifa->ifa_flags);
FFAdapter* adapter = NULL;
FF_LIST_FOR_EACH(FFAdapter, x, adapters)
{
if (ffStrEquals(x->mac->ifa_name, ifa->ifa_name))
{
adapter = x;
break;
}
}
if (!adapter)
{
adapter = ffListAdd(&adapters);
*adapter = (FFAdapter) {
.mac = ifa,
.ipv4 = ffListCreate(sizeof(struct ifaddrs*)),
.ipv6 = ffListCreate(sizeof(struct ifaddrs*)),
};
FF_DEBUG("Created new adapter entry for interface %s", ifa->ifa_name);
}
switch (ifa->ifa_addr->sa_family)
{
case AF_INET:
if (options->showType & FF_LOCALIP_TYPE_IPV4_BIT)
{
*FF_LIST_ADD(struct ifaddrs*, adapter->ipv4) = ifa;
FF_DEBUG("Added IPv4 entry for interface %s", ifa->ifa_name);
}
break;
case AF_INET6:
if (options->showType & FF_LOCALIP_TYPE_IPV6_BIT)
{
*FF_LIST_ADD(struct ifaddrs*, adapter->ipv6) = ifa;
FF_DEBUG("Added IPv6 entry for interface %s", ifa->ifa_name);
}
break;
#if __FreeBSD__ || __OpenBSD__ || __APPLE__ || __NetBSD__ || __HAIKU__
case AF_LINK:
adapter->mac = ifa;
FF_DEBUG("Updated MAC entry for interface %s", ifa->ifa_name);
break;
#elif !__sun
case AF_PACKET:
adapter->mac = ifa;
FF_DEBUG("Updated MAC entry for interface %s", ifa->ifa_name);
break;
#endif
}
}
FF_DEBUG("Found %u network adapters", adapters.length);
FF_LIST_FOR_EACH(FFAdapter, adapter, adapters)
{
FF_DEBUG("Processing adapter %s (IPv4 entries: %u, IPv6 entries: %u)",
adapter->mac->ifa_name, adapter->ipv4.length, adapter->ipv6.length);
FFLocalIpResult* item = FF_LIST_ADD(FFLocalIpResult, *results);
ffStrbufInitS(&item->name, adapter->mac->ifa_name);
ffStrbufInit(&item->ipv4);
ffStrbufInit(&item->ipv6);
ffStrbufInit(&item->mac);
ffStrbufInit(&item->flags);
item->defaultRoute = FF_LOCALIP_TYPE_NONE;
item->mtu = -1;
item->speed = -1;
if (options->showType & FF_LOCALIP_TYPE_FLAGS_BIT)
{
ffLocalIpFillNIFlags(&item->flags, adapter->mac->ifa_flags, niFlagOptions);
FF_DEBUG("Added flags for interface %s: %s", adapter->mac->ifa_name, item->flags.chars);
}
if ((options->showType & FF_LOCALIP_TYPE_IPV4_BIT))
{
const FFNetifDefaultRouteResult* defaultRouteV4 = ffNetifGetDefaultRouteV4();
bool isDefaultRouteIf = ffStrEquals(defaultRouteV4->ifName, adapter->mac->ifa_name);
if (isDefaultRouteIf)
{
item->defaultRoute |= FF_LOCALIP_TYPE_IPV4_BIT;
FF_DEBUG("Interface %s is IPv4 default route", adapter->mac->ifa_name);
}
const FFNetifDefaultRouteResult* defaultRoute = ffNetifGetDefaultRouteV4();
bool isDefaultRouteIf = ffStrEquals(defaultRoute->ifName, ifa->ifa_name);
if (options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT)
{
if (!isDefaultRouteIf)
continue;
if (!(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT))
{
bool isPreferredSourceAddr = defaultRoute->preferredSourceAddrV4 == 0 || ipv4->sin_addr.s_addr == defaultRoute->preferredSourceAddrV4;
if (!isPreferredSourceAddr)
continue;
FF_DEBUG("Skipping IPv4 for interface %s (not default route)", adapter->mac->ifa_name);
goto v6;
}
}
char addressBuffer[INET_ADDRSTRLEN + 16];
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
if (!(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT))
{
struct sockaddr_in* netmask = (struct sockaddr_in*) ifa->ifa_netmask;
int cidr = __builtin_popcount(netmask->sin_addr.s_addr);
if (cidr != 0)
struct ifaddrs* ifa = NULL;
if (isDefaultRouteIf && defaultRouteV4->preferredSourceAddrV4 != 0)
{
size_t len = strlen(addressBuffer);
snprintf(addressBuffer + len, 16, "/%d", cidr);
FF_LIST_FOR_EACH(struct ifaddrs*, pifa, adapter->ipv4)
{
struct sockaddr_in* ipv4 = (struct sockaddr_in*) (*pifa)->ifa_addr;
if (ipv4->sin_addr.s_addr == defaultRouteV4->preferredSourceAddrV4)
{
ifa = *pifa;
FF_DEBUG("Found preferred IPv4 source address for interface %s", adapter->mac->ifa_name);
break;
}
}
}
if (ifa)
appendIpv4(options, &item->ipv4, ifa);
else if (adapter->ipv4.length > 0)
{
appendIpv4(options, &item->ipv4, *FF_LIST_GET(struct ifaddrs*, adapter->ipv4, 0));
FF_DEBUG("Using first IPv4 address for interface %s", adapter->mac->ifa_name);
}
}
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET, isDefaultRouteIf, flags, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
if (!(options->showType & FF_LOCALIP_TYPE_IPV6_BIT))
continue;
const FFNetifDefaultRouteResult* defaultRoute = ffNetifGetDefaultRouteV6();
bool isDefaultRouteIf = ffStrEquals(defaultRoute->ifName, ifa->ifa_name);
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRouteIf)
continue;
struct sockaddr_in6* ipv6 = (struct sockaddr_in6 *)ifa->ifa_addr;
if (!(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT) && !isIPv6AddressPreferred(ifa, ipv6))
continue;
char addressBuffer[INET6_ADDRSTRLEN + 16];
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
else
{
struct sockaddr_in6* netmask = (struct sockaddr_in6*) ifa->ifa_netmask;
int cidr = 0;
static_assert(sizeof(netmask->sin6_addr) % sizeof(uint64_t) == 0, "");
for (uint32_t i = 0; i < sizeof(netmask->sin6_addr) / sizeof(uint64_t); ++i)
cidr += __builtin_popcountll(((uint64_t*) &netmask->sin6_addr)[i]);
if (cidr != 0)
FF_DEBUG("Adding all IPv4 addresses for interface %s", adapter->mac->ifa_name);
FF_LIST_FOR_EACH(struct ifaddrs*, pifa, adapter->ipv4)
appendIpv4(options, &item->ipv4, *pifa);
}
}
v6:
if ((options->showType & FF_LOCALIP_TYPE_IPV6_BIT))
{
const FFNetifDefaultRouteResult* defaultRouteV6 = ffNetifGetDefaultRouteV6();
bool isDefaultRouteIf = ffStrEquals(defaultRouteV6->ifName, adapter->mac->ifa_name);
if (isDefaultRouteIf)
{
item->defaultRoute |= FF_LOCALIP_TYPE_IPV6_BIT;
FF_DEBUG("Interface %s is IPv6 default route", adapter->mac->ifa_name);
}
if (options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT)
{
if (!isDefaultRouteIf)
{
size_t len = strlen(addressBuffer);
snprintf(addressBuffer + len, 16, "/%d", cidr);
FF_DEBUG("Skipping IPv6 for interface %s (not default route)", adapter->mac->ifa_name);
goto mac;
}
}
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRouteIf, flags, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
}
#if __FreeBSD__ || __OpenBSD__ || __APPLE__ || __NetBSD__ || __HAIKU__
else if (ifa->ifa_addr->sa_family == AF_LINK)
{
if (!(options->showType & FF_LOCALIP_TYPE_MAC_BIT))
continue;
if (!(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT))
{
struct ifaddrs* selected = NULL;
struct ifaddrs* secondary = NULL;
char addressBuffer[32];
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr);
snprintf(addressBuffer, ARRAY_SIZE(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, false, flags, false);
FF_LIST_FOR_EACH(struct ifaddrs*, pifa, adapter->ipv6)
{
FFIPv6Type type = getIpType(*pifa);
if (type == FF_IPV6_PREFERRED)
{
selected = *pifa;
FF_DEBUG("Found preferred IPv6 address for interface %s", adapter->mac->ifa_name);
break;
}
else if (type == FF_IPV6_GUA && !selected)
{
selected = *pifa;
FF_DEBUG("Found GUA IPv6 address for interface %s", adapter->mac->ifa_name);
}
else if (type == FF_IPV6_ULA && !secondary)
{
secondary = *pifa;
FF_DEBUG("Found ULA IPv6 address for interface %s", adapter->mac->ifa_name);
}
}
if (!selected) selected = secondary;
if (selected)
appendIpv6(options, &item->ipv6, selected);
else if (adapter->ipv6.length > 0)
{
appendIpv6(options, &item->ipv6, *FF_LIST_GET(struct ifaddrs*, adapter->ipv6, 0));
FF_DEBUG("Using first IPv6 address for interface %s", adapter->mac->ifa_name);
}
}
else
{
FF_DEBUG("Adding all IPv6 addresses for interface %s", adapter->mac->ifa_name);
FF_LIST_FOR_EACH(struct ifaddrs*, pifa, adapter->ipv6)
appendIpv6(options, &item->ipv6, *pifa);
}
}
mac:
#ifndef __sun
if (options->showType & FF_LOCALIP_TYPE_MAC_BIT)
{
if (adapter->mac->ifa_addr)
{
#if __FreeBSD__ || __OpenBSD__ || __APPLE__ || __NetBSD__ || __HAIKU__
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)adapter->mac->ifa_addr);
#else
uint8_t* ptr = ((struct sockaddr_ll *)adapter->mac->ifa_addr)->sll_addr;
#endif
ffStrbufSetF(&item->mac, "%02x:%02x:%02x:%02x:%02x:%02x",
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
FF_DEBUG("Added MAC address %s for interface %s", item->mac.chars, adapter->mac->ifa_name);
}
else
{
FF_DEBUG("No MAC address available for interface %s", adapter->mac->ifa_name);
}
}
#else
else if (ifa->ifa_addr->sa_family == AF_PACKET)
{
if (!(options->showType & FF_LOCALIP_TYPE_MAC_BIT))
continue;
char addressBuffer[32];
uint8_t* ptr = ((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr;
snprintf(addressBuffer, ARRAY_SIZE(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, false, flags, false);
}
(void) adapter;
#endif
}
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
FF_LIST_FOR_EACH(FFAdapter, adapter, adapters)
{
ffListDestroy(&adapter->ipv4);
ffListDestroy(&adapter->ipv6);
}
if (ifAddrStruct)
{
freeifaddrs(ifAddrStruct);
ifAddrStruct = NULL;
FF_DEBUG("Cleaned up interface address structures");
}
if ((options->showType & FF_LOCALIP_TYPE_MTU_BIT) || (options->showType & FF_LOCALIP_TYPE_SPEED_BIT)
#ifdef __sun
@@ -377,18 +586,26 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
#endif
)
{
FF_DEBUG("Retrieving additional interface properties (MTU/Speed/MAC)");
FF_AUTO_CLOSE_FD int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd > 0)
{
FF_LIST_FOR_EACH(FFLocalIpResult, iface, *results)
{
struct ifreq ifr;
struct ifreq ifr = {};
ffStrCopy(ifr.ifr_name, iface->name.chars, IFNAMSIZ);
if (options->showType & FF_LOCALIP_TYPE_MTU_BIT)
{
if (ioctl(sockfd, SIOCGIFMTU, &ifr) == 0)
{
iface->mtu = (int32_t) ifr.ifr_mtu;
FF_DEBUG("Interface %s MTU: %d", iface->name.chars, iface->mtu);
}
else
{
FF_DEBUG("Failed to get MTU for interface %s", iface->name.chars);
}
}
if (options->showType & FF_LOCALIP_TYPE_SPEED_BIT)
@@ -397,12 +614,21 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
struct ethtool_cmd edata = { .cmd = ETHTOOL_GSET };
ifr.ifr_data = (void*) &edata;
if (ioctl(sockfd, SIOCETHTOOL, &ifr) == 0)
iface->speed = (edata.speed_hi << 16) | edata.speed; // ethtool_cmd_speed is not available on Android
{
iface->speed = (edata.speed_hi << 16) | edata.speed;
FF_DEBUG("Interface %s speed: %d Mbps", iface->name.chars, iface->speed);
}
else
{
// ethtool_cmd_speed is not available on Android
FF_DEBUG("Failed to get speed for interface %s via ethtool", iface->name.chars);
}
#elif __FreeBSD__ || __APPLE__ || __OpenBSD__ || __NetBSD__
struct ifmediareq ifmr = {};
ffStrCopy(ifmr.ifm_name, iface->name.chars, IFNAMSIZ);
if (ioctl(sockfd, SIOCGIFMEDIA, &ifmr) == 0 && (IFM_TYPE(ifmr.ifm_active) & IFM_ETHER))
{
FF_DEBUG("Interface %s media type: 0x%x", iface->name.chars, IFM_SUBTYPE(ifmr.ifm_active));
switch (IFM_SUBTYPE(ifmr.ifm_active))
{
#ifdef IFM_HPNA_1
@@ -767,7 +993,15 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
iface->speed = 56000; break;
default:
iface->speed = -1;
FF_DEBUG("Unknown media subtype for interface %s", iface->name.chars);
break;
}
if (iface->speed > 0)
FF_DEBUG("Interface %s speed: %d Mbps", iface->name.chars, iface->speed);
}
else
{
FF_DEBUG("Failed to get media info for interface %s", iface->name.chars);
}
#endif
}
@@ -778,6 +1012,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
const uint8_t* ptr = (uint8_t*) ifr.ifr_addr.sa_data; // NOT ifr_enaddr
ffStrbufSetF(&iface->mac, "%02x:%02x:%02x:%02x:%02x:%02x",
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
FF_DEBUG("Added MAC address %s for interface %s (Solaris)", iface->mac.chars, iface->name.chars);
}
if (options->showType & FF_LOCALIP_TYPE_SPEED_BIT)
{
@@ -791,7 +1026,10 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
{
kstat_named_t* ifspeed = (kstat_named_t*) kstat_data_lookup(ks, "ifspeed");
if (ifspeed)
{
iface->speed = (int32_t) (ifspeed->value.ui64 / 1000 / 1000);
FF_DEBUG("Interface %s speed: %d Mbps (kstat)", iface->name.chars, iface->speed);
}
}
break;
}
@@ -800,7 +1038,12 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
#endif
}
}
else
{
FF_DEBUG("Failed to create socket for interface property retrieval");
}
}
FF_DEBUG("Local IP detection completed, found %u interfaces", results->length);
return NULL;
}
+135 -71
View File
@@ -4,6 +4,7 @@
#include "common/netif/netif.h"
#include "util/mallocHelper.h"
#include "util/windows/unicode.h"
#include "util/debug.h"
#include "localip.h"
#define FF_LOCALIP_NIFLAG(name) { IP_ADAPTER_##name, #name }
@@ -23,45 +24,11 @@ static const FFLocalIpNIFlag niFlagOptions[] = {
{},
};
static void addNewIp(FFlist* list, const char* name, const char* addr, int type, bool newIp, bool defaultRoute)
{
FFLocalIpResult* ip = NULL;
if (newIp)
{
ip = (FFLocalIpResult*) ffListAdd(list);
ffStrbufInitS(&ip->name, name);
ffStrbufInit(&ip->ipv4);
ffStrbufInit(&ip->ipv6);
ffStrbufInit(&ip->mac);
ffStrbufInit(&ip->flags);
ip->defaultRoute = defaultRoute;
ip->speed = -1;
ip->mtu = -1;
}
else
{
ip = FF_LIST_GET(FFLocalIpResult, *list, list->length - 1);
}
switch (type)
{
case AF_INET:
if (ip->ipv4.length) ffStrbufAppendC(&ip->ipv4, ',');
ffStrbufAppendS(&ip->ipv4, addr);
break;
case AF_INET6:
if (ip->ipv6.length) ffStrbufAppendC(&ip->ipv6, ',');
ffStrbufAppendS(&ip->ipv6, addr);
break;
case -1:
ffStrbufSetS(&ip->mac, addr);
break;
}
}
const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
{
FF_DEBUG("Starting local IP detection with showType=0x%X, namePrefix='%.*s'",
options->showType, (int)options->namePrefix.length, options->namePrefix.chars);
IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL;
// Multiple attempts in case interfaces change while
@@ -69,72 +36,134 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
DWORD adapter_addresses_buffer_size = 0;
for (int attempts = 0;; ++attempts)
{
FF_DEBUG("Attempt %d to get adapter addresses, buffer size: %lu", attempts + 1, adapter_addresses_buffer_size);
if (adapter_addresses_buffer_size)
{
adapter_addresses = (IP_ADAPTER_ADDRESSES*)realloc(adapter_addresses, adapter_addresses_buffer_size);
assert(adapter_addresses);
}
DWORD family = options->showType & FF_LOCALIP_TYPE_IPV4_BIT
? options->showType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
: AF_INET6;
FF_DEBUG("Calling GetAdaptersAddresses with family=%u, flags=0x%X", (unsigned)family,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER);
DWORD error = GetAdaptersAddresses(
options->showType & FF_LOCALIP_TYPE_IPV4_BIT
? options->showType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
: AF_INET6,
family,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
NULL,
adapter_addresses,
&adapter_addresses_buffer_size);
if (error == ERROR_SUCCESS)
{
FF_DEBUG("GetAdaptersAddresses succeeded on attempt %d", attempts + 1);
break;
}
else if (ERROR_BUFFER_OVERFLOW == error && attempts < 4)
{
FF_DEBUG("Buffer overflow, need %lu bytes, retrying", adapter_addresses_buffer_size);
continue;
}
else
{
FF_DEBUG("GetAdaptersAddresses failed with error %lu after %d attempts", error, attempts + 1);
return "GetAdaptersAddresses() failed";
}
}
uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteV4()->ifIndex;
int adapterCount = 0;
int processedCount = 0;
// Iterate through all of the adapters
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
{
if (adapter->OperStatus != IfOperStatusUp)
continue;
adapterCount++;
bool isDefaultRoute = adapter->IfIndex == defaultRouteIfIndex;
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRoute)
FF_DEBUG("Processing adapter %d: IfIndex=%u, IfType=%u, OperStatus=%u",
adapterCount, (unsigned)adapter->IfIndex, (unsigned)adapter->IfType, (unsigned)adapter->OperStatus);
if (adapter->OperStatus != IfOperStatusUp)
{
FF_DEBUG("Skipping adapter %u (not operational, status=%d)", (unsigned)adapter->IfIndex, adapter->OperStatus);
continue;
}
bool isLoop = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK;
if (isLoop && !(options->showType & FF_LOCALIP_TYPE_LOOP_BIT))
continue;
FF_DEBUG("Adapter %u: isLoopback=%s", (unsigned)adapter->IfIndex, isLoop ? "true" : "false");
bool newIp = true;
if (isLoop && !(options->showType & FF_LOCALIP_TYPE_LOOP_BIT))
{
FF_DEBUG("Skipping loopback adapter %u (loopback not requested)", (unsigned)adapter->IfIndex);
continue;
}
char name[128];
WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, ARRAY_SIZE(name), NULL, NULL);
if (options->namePrefix.length && strncmp(name, options->namePrefix.chars, options->namePrefix.length) != 0)
continue;
FF_DEBUG("Adapter %u name: '%s'", (unsigned)adapter->IfIndex, name);
if (options->showType & FF_LOCALIP_TYPE_MAC_BIT && adapter->PhysicalAddressLength == 6)
if (options->namePrefix.length && strncmp(name, options->namePrefix.chars, options->namePrefix.length) != 0)
{
char addressBuffer[32];
uint8_t* ptr = adapter->PhysicalAddress;
snprintf(addressBuffer, ARRAY_SIZE(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, isDefaultRoute);
newIp = false;
FF_DEBUG("Skipping adapter %u (name doesn't match prefix '%.*s')",
(unsigned)adapter->IfIndex, (int)options->namePrefix.length, options->namePrefix.chars);
continue;
}
if (options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT)
{
if (!((options->showType & FF_LOCALIP_TYPE_IPV4_BIT) && ffNetifGetDefaultRouteV4()->ifIndex == adapter->IfIndex) &&
!((options->showType & FF_LOCALIP_TYPE_IPV6_BIT) && ffNetifGetDefaultRouteV6()->ifIndex == adapter->IfIndex))
{
FF_DEBUG("Skipping interface %u (not default route interface)", (unsigned)adapter->IfIndex);
continue;
}
}
processedCount++;
FF_DEBUG("Creating result item for adapter %u ('%s')", (unsigned)adapter->IfIndex, name);
FFLocalIpResult* item = (FFLocalIpResult*) ffListAdd(results);
ffStrbufInitS(&item->name, name);
ffStrbufInit(&item->ipv4);
ffStrbufInit(&item->ipv6);
ffStrbufInit(&item->mac);
ffStrbufInit(&item->flags);
item->defaultRoute = FF_LOCALIP_TYPE_NONE;
item->speed = -1;
item->mtu = -1;
uint32_t typesToAdd = options->showType & (FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_IPV6_BIT | FF_LOCALIP_TYPE_ALL_IPS_BIT);
FF_DEBUG("Types to add for adapter %u: 0x%X", (unsigned)adapter->IfIndex, typesToAdd);
int ipv4Count = 0, ipv6Count = 0;
for (IP_ADAPTER_UNICAST_ADDRESS* ifa = adapter->FirstUnicastAddress; ifa; ifa = ifa->Next)
{
FF_DEBUG("Processing unicast address: family=%d, DadState=%d",
ifa->Address.lpSockaddr->sa_family, ifa->DadState);
if (!(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT) && ifa->DadState != IpDadStatePreferred)
{
FF_DEBUG("Skipping address (DadState=%d, not preferred)", ifa->DadState);
continue;
}
if (ifa->Address.lpSockaddr->sa_family == AF_INET)
{
if (!(typesToAdd & (FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_ALL_IPS_BIT))) continue;
if (!(typesToAdd & (FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_ALL_IPS_BIT)))
{
FF_DEBUG("Skipping IPv4 address (not requested in typesToAdd=0x%X)", typesToAdd);
continue;
}
bool isDefaultRoute = ((options->showType & FF_LOCALIP_TYPE_IPV4_BIT) && ffNetifGetDefaultRouteV4()->ifIndex == adapter->IfIndex);
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRoute)
{
FF_DEBUG("Skipping IPv4 address (not on default route interface)");
continue;
}
SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr;
char addressBuffer[INET_ADDRSTRLEN + 6];
@@ -146,15 +175,30 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
snprintf(addressBuffer + len, 6, "/%u", (unsigned) ifa->OnLinkPrefixLength);
}
addNewIp(results, name, addressBuffer, AF_INET, newIp, isDefaultRoute);
newIp = false;
FF_DEBUG("Adding IPv4 address: %s (isDefaultRoute=%s)", addressBuffer, isDefaultRoute ? "true" : "false");
if (item->ipv4.length) ffStrbufAppendC(&item->ipv4, ',');
ffStrbufAppendS(&item->ipv4, addressBuffer);
if (isDefaultRoute) item->defaultRoute |= FF_LOCALIP_TYPE_IPV4_BIT;
ipv4Count++;
typesToAdd &= ~(unsigned) FF_LOCALIP_TYPE_IPV4_BIT;
if (typesToAdd == 0) break;
}
else if (ifa->Address.lpSockaddr->sa_family == AF_INET6)
{
if (!(typesToAdd & (FF_LOCALIP_TYPE_IPV6_BIT | FF_LOCALIP_TYPE_ALL_IPS_BIT))) continue;
if (!(typesToAdd & (FF_LOCALIP_TYPE_IPV6_BIT | FF_LOCALIP_TYPE_ALL_IPS_BIT)))
{
FF_DEBUG("Skipping IPv6 address (not requested in typesToAdd=0x%X)", typesToAdd);
continue;
}
bool isDefaultRoute = ((options->showType & FF_LOCALIP_TYPE_IPV6_BIT) && ffNetifGetDefaultRouteV6()->ifIndex == adapter->IfIndex);
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRoute)
{
FF_DEBUG("Skipping IPv6 address (not on default route interface)");
continue;
}
SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr;
char addressBuffer[INET6_ADDRSTRLEN + 6];
@@ -166,25 +210,45 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
snprintf(addressBuffer + len, 6, "/%u", (unsigned) ifa->OnLinkPrefixLength);
}
addNewIp(results, name, addressBuffer, AF_INET6, newIp, isDefaultRoute);
newIp = false;
FF_DEBUG("Adding IPv6 address: %s (isDefaultRoute=%s)", addressBuffer, isDefaultRoute ? "true" : "false");
if (item->ipv6.length) ffStrbufAppendC(&item->ipv6, ',');
ffStrbufAppendS(&item->ipv6, addressBuffer);
if (isDefaultRoute) item->defaultRoute |= FF_LOCALIP_TYPE_IPV6_BIT;
ipv6Count++;
typesToAdd &= ~(unsigned) FF_LOCALIP_TYPE_IPV6_BIT;
if (typesToAdd == 0) break;
}
}
if (!newIp)
FF_DEBUG("Adapter %u: collected %d IPv4 and %d IPv6 addresses", (unsigned)adapter->IfIndex, ipv4Count, ipv6Count);
if (options->showType & FF_LOCALIP_TYPE_SPEED_BIT)
{
FFLocalIpResult* result = FF_LIST_GET(FFLocalIpResult, *results, results->length - 1);
if (options->showType & FF_LOCALIP_TYPE_SPEED_BIT)
result->speed = (int32_t) (adapter->ReceiveLinkSpeed / 1000000);
if (options->showType & FF_LOCALIP_TYPE_MTU_BIT)
result->mtu = (int32_t) adapter->Mtu;
if (options->showType & FF_LOCALIP_TYPE_FLAGS_BIT)
ffLocalIpFillNIFlags(&result->flags, adapter->Flags, niFlagOptions);
item->speed = (int32_t) (adapter->ReceiveLinkSpeed / 1000000);
FF_DEBUG("Adapter %u speed: %d Mbps (raw: %llu)", (unsigned)adapter->IfIndex, item->speed, adapter->ReceiveLinkSpeed);
}
if (options->showType & FF_LOCALIP_TYPE_MTU_BIT)
{
item->mtu = (int32_t) adapter->Mtu;
FF_DEBUG("Adapter %u MTU: %d", (unsigned)adapter->IfIndex, item->mtu);
}
if (options->showType & FF_LOCALIP_TYPE_FLAGS_BIT)
{
ffLocalIpFillNIFlags(&item->flags, adapter->Flags, niFlagOptions);
FF_DEBUG("Adapter %u flags: 0x%lX -> '%s'", (unsigned)adapter->IfIndex, adapter->Flags, item->flags.chars);
}
if (options->showType & FF_LOCALIP_TYPE_MAC_BIT && adapter->PhysicalAddressLength == 6)
{
uint8_t* ptr = adapter->PhysicalAddress;
ffStrbufSetF(&item->mac, "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
FF_DEBUG("Adapter %u MAC: %s", (unsigned)adapter->IfIndex, item->mac.chars);
}
}
FF_DEBUG("Local IP detection completed: scanned %d adapters, processed %d, results count: %u",
adapterCount, processedCount, results->length);
return NULL;
}
+1 -6
View File
@@ -446,12 +446,7 @@ static void getPackageCounts(FFstrbuf* baseDir, FFPackagesResult* packageCounts,
{
packageCounts->guixSystem += getGuixPackages(baseDir, "/run/current-system/profile");
}
if (!(options->disabled & FF_PACKAGES_FLAG_LINGLONG_BIT))
{
packageCounts->linglong += getNumElements(baseDir, "/var/lib/linglong/repo/refs/heads/main", true);
if (packageCounts->linglong == 0)
packageCounts->linglong += getNumElements(baseDir, "/var/lib/linglong/repo/refs/remotes/stable/main", true);
}
if (!(options->disabled & FF_PACKAGES_FLAG_LINGLONG_BIT)) packageCounts->linglong += getNumElements(baseDir, "/var/lib/linglong/layers", true);
if (!(options->disabled & FF_PACKAGES_FLAG_PACSTALL_BIT)) packageCounts->pacstall += getNumElements(baseDir, "/var/lib/pacstall/metadata", false);
if (!(options->disabled & FF_PACKAGES_FLAG_PISI_BIT)) packageCounts->pisi += getNumElements(baseDir, "/var/lib/pisi/package", true);
if (!(options->disabled & FF_PACKAGES_FLAG_PKGSRC_BIT)) packageCounts->pkgsrc += getNumElements(baseDir, "/usr/pkg/pkgdb", DT_DIR);
+12 -3
View File
@@ -91,9 +91,11 @@ static void printIp(FFLocalIpResult* ip, bool markDefaultRoute, FFstrbuf* buffer
}
if (ip->flags.length)
{
if (buffer->length) ffStrbufAppendS(buffer, " <");
bool flag = buffer->length > 0;
if (flag) ffStrbufAppendS(buffer, " <");
ffStrbufAppend(buffer, &ip->flags);
ffStrbufAppendC(buffer, '>');
if (flag)
ffStrbufAppendC(buffer, '>');
}
if (markDefaultRoute && ip->defaultRoute)
ffStrbufAppendS(buffer, " *");
@@ -342,7 +344,14 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs
{
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &ip->name);
yyjson_mut_obj_add_bool(doc, obj, "defaultRoute", ip->defaultRoute);
if (options->showType & (FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_IPV6_BIT))
{
yyjson_mut_val* defaultRoute = yyjson_mut_obj_add_obj(doc, obj, "defaultRoute");
if (options->showType & FF_LOCALIP_TYPE_IPV4_BIT)
yyjson_mut_obj_add_bool(doc, defaultRoute, "ipv4", !!(ip->defaultRoute & FF_LOCALIP_TYPE_IPV4_BIT));
if (options->showType & FF_LOCALIP_TYPE_IPV6_BIT)
yyjson_mut_obj_add_bool(doc, defaultRoute, "ipv6", !!(ip->defaultRoute & FF_LOCALIP_TYPE_IPV6_BIT));
}
if (options->showType & FF_LOCALIP_TYPE_IPV4_BIT)
yyjson_mut_obj_add_strbuf(doc, obj, "ipv4", &ip->ipv4);
if (options->showType & FF_LOCALIP_TYPE_IPV6_BIT)
+6
View File
@@ -120,3 +120,9 @@ static inline void ffListClear(FFlist* list)
assert((listVar).capacity > (index)); \
(itemType*)(listVar).data + (index); \
})
#define FF_LIST_ADD(itemType, listVar) \
({ \
assert(sizeof(itemType) == (listVar).elementSize); \
(itemType*) ffListAdd(&(listVar)); \
})
+6 -1
View File
@@ -9,7 +9,12 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "3rdparty/yyjson/yyjson.h"
#ifdef FF_USE_SYSTEM_YYJSON
#include <yyjson.h>
#else
#include "3rdparty/yyjson/yyjson.h"
#endif
#ifdef _WIN32
// #include <shlwapi.h>