NetIF (OpenBSD): fix default route detection

This commit is contained in:
李通洲
2025-03-14 16:55:07 +08:00
committed by Carter Li
parent 8ecd58dd14
commit 85654316c7
3 changed files with 42 additions and 1 deletions
+1
View File
@@ -8,6 +8,7 @@ Features:
* Improve accuracy of HDR support on Windows 11 24H2 (Display, Windows)
* Improve performance of SSID detection on macOS Sequoia (Wifi, macOS, #1597)
* Support warp terminal version detection on Windows (Terminal, Windows)
* Support default route detection for OpenBSD (LocalIP, OpenBSD)
Logo:
* Add Common Torizon OS
+40
View File
@@ -1,6 +1,7 @@
#include "netif.h"
#include "common/io/io.h"
#include "util/mallocHelper.h"
#include <net/if.h>
#include <net/if_dl.h>
@@ -8,6 +9,10 @@
#include <netinet/in.h>
#include <sys/socket.h>
#ifdef __OpenBSD__
#include <sys/sysctl.h>
#endif
#define ROUNDUP2(a, n) ((a) > 0 ? (1 + (((a) - 1U) | ((n) - 1))) : (n))
#if defined(__APPLE__)
@@ -47,6 +52,39 @@ get_rt_address(struct rt_msghdr *rtm, int desired)
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
{
#if defined(__OpenBSD__)
int mib[6] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY};
size_t needed;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
return false;
FF_AUTO_FREE char* buf = malloc(needed);
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
return false;
char* lim = buf + needed;
struct rt_msghdr* rtm;
for (char* next = buf; next < lim; next += rtm->rtm_msglen)
{
rtm = (struct rt_msghdr *)next;
struct sockaddr* sa = (struct sockaddr *)(rtm + 1);
if ((rtm->rtm_flags & RTF_GATEWAY) && (sa->sa_family == AF_INET))
{
struct sockaddr_dl* sdl = (struct sockaddr_dl *)get_rt_address(rtm, RTA_IFP);
if (sdl->sdl_family == AF_LINK)
{
assert(sdl->sdl_nlen <= IF_NAMESIZE);
memcpy(iface, sdl->sdl_data, sdl->sdl_nlen);
iface[sdl->sdl_nlen] = '\0';
*ifIndex = sdl->sdl_index;
return true;
}
}
}
#else
//https://github.com/hashPirate/copenheimer-masscan-fork/blob/36f1ed9f7b751a7dccd5ed27874e2e703db7d481/src/rawsock-getif.c#L104
FF_AUTO_CLOSE_FD int pfRoute = socket(PF_ROUTE, SOCK_RAW, AF_INET);
@@ -106,5 +144,7 @@ bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
return false;
}
}
#endif
return false;
}
+1 -1
View File
@@ -525,7 +525,7 @@ void ffInitLocalIpOptions(FFLocalIpOptions* options)
ffOptionInitModuleArg(&options->moduleArgs, "󰩟");
options->showType = FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_PREFIX_LEN_BIT
#if !__ANDROID__ /*Permission denied*/ && !__OpenBSD__ /*Report invalid argument for some reason*/ && !__DragonFly__ /*Doesn't work*/
#if !__ANDROID__ /*Permission denied*/ && !__DragonFly__ /*Doesn't work*/
| FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT
#endif
;