diff --git a/CHANGELOG.md b/CHANGELOG.md index 7444b2189..f453ed0ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/common/netif/netif_bsd.c b/src/common/netif/netif_bsd.c index 91a270905..4e43614f4 100644 --- a/src/common/netif/netif_bsd.c +++ b/src/common/netif/netif_bsd.c @@ -1,6 +1,7 @@ #include "netif.h" #include "common/io/io.h" +#include "util/mallocHelper.h" #include #include @@ -8,6 +9,10 @@ #include #include +#ifdef __OpenBSD__ + #include +#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; } diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 855380929..9ba08f027 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -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 ;