Skip to content

Commit 8565431

Browse files
committed
NetIF (OpenBSD): fix default route detection
1 parent 8ecd58d commit 8565431

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Features:
88
* Improve accuracy of HDR support on Windows 11 24H2 (Display, Windows)
99
* Improve performance of SSID detection on macOS Sequoia (Wifi, macOS, #1597)
1010
* Support warp terminal version detection on Windows (Terminal, Windows)
11+
* Support default route detection for OpenBSD (LocalIP, OpenBSD)
1112

1213
Logo:
1314
* Add Common Torizon OS

src/common/netif/netif_bsd.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#include "netif.h"
22

33
#include "common/io/io.h"
4+
#include "util/mallocHelper.h"
45

56
#include <net/if.h>
67
#include <net/if_dl.h>
78
#include <net/route.h>
89
#include <netinet/in.h>
910
#include <sys/socket.h>
1011

12+
#ifdef __OpenBSD__
13+
#include <sys/sysctl.h>
14+
#endif
15+
1116
#define ROUNDUP2(a, n) ((a) > 0 ? (1 + (((a) - 1U) | ((n) - 1))) : (n))
1217

1318
#if defined(__APPLE__)
@@ -47,6 +52,39 @@ get_rt_address(struct rt_msghdr *rtm, int desired)
4752

4853
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
4954
{
55+
#if defined(__OpenBSD__)
56+
int mib[6] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY};
57+
size_t needed;
58+
59+
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
60+
return false;
61+
62+
FF_AUTO_FREE char* buf = malloc(needed);
63+
64+
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
65+
return false;
66+
67+
char* lim = buf + needed;
68+
struct rt_msghdr* rtm;
69+
for (char* next = buf; next < lim; next += rtm->rtm_msglen)
70+
{
71+
rtm = (struct rt_msghdr *)next;
72+
struct sockaddr* sa = (struct sockaddr *)(rtm + 1);
73+
74+
if ((rtm->rtm_flags & RTF_GATEWAY) && (sa->sa_family == AF_INET))
75+
{
76+
struct sockaddr_dl* sdl = (struct sockaddr_dl *)get_rt_address(rtm, RTA_IFP);
77+
if (sdl->sdl_family == AF_LINK)
78+
{
79+
assert(sdl->sdl_nlen <= IF_NAMESIZE);
80+
memcpy(iface, sdl->sdl_data, sdl->sdl_nlen);
81+
iface[sdl->sdl_nlen] = '\0';
82+
*ifIndex = sdl->sdl_index;
83+
return true;
84+
}
85+
}
86+
}
87+
#else
5088
//https://github.com/hashPirate/copenheimer-masscan-fork/blob/36f1ed9f7b751a7dccd5ed27874e2e703db7d481/src/rawsock-getif.c#L104
5189

5290
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)
106144
return false;
107145
}
108146
}
147+
#endif
148+
109149
return false;
110150
}

src/modules/localip/localip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ void ffInitLocalIpOptions(FFLocalIpOptions* options)
525525
ffOptionInitModuleArg(&options->moduleArgs, "󰩟");
526526

527527
options->showType = FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_PREFIX_LEN_BIT
528-
#if !__ANDROID__ /*Permission denied*/ && !__OpenBSD__ /*Report invalid argument for some reason*/ && !__DragonFly__ /*Doesn't work*/
528+
#if !__ANDROID__ /*Permission denied*/ && !__DragonFly__ /*Doesn't work*/
529529
| FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT
530530
#endif
531531
;

0 commit comments

Comments
 (0)