master ohmu/fastfetch-openbsd / src / common / impl / netif_bsd.c
ohmu  ·  2026-10-15
  1#include "common/netif.h"
  2#include "common/io.h"
  3#include "common/mallocHelper.h"
  4
  5#include <net/if.h>
  6#include <net/if_dl.h>
  7#include <net/route.h>
  8#include <netinet/in.h>
  9#include <sys/socket.h>
 10#include <sys/sysctl.h>
 11
 12#define ROUNDUP2(a, n) ((a) > 0 ? (1 + (((a) - 1U) | ((n) - 1))) : (n))
 13
 14#if __DragonFly__
 15    // https://github.com/DragonFlyBSD/DragonFlyBSD/blob/cf0aa2f1e47a3f0a6055fe427563cb3f3e627064/sys/net/route.h#L315C9-L315C19
 16    #define ROUNDUP(a) ROUNDUP2((a), sizeof(long))
 17#elif __FreeBSD__
 18    // https://github.com/freebsd/freebsd-src/blob/e4c0ecba44b20ebb2e4d80978c2cb6d16b730cb9/sys/net/route.h#L368C9-L368C16
 19    #define ROUNDUP(a) ROUNDUP2((a), sizeof(long))
 20#elif __NetBSD__
 21    // https://github.com/NetBSD/src/blob/29beb637d057520c0ed37ac2cde966f7cc0cadf4/sys/net/route.h#L330
 22    #define ROUNDUP(a) ROUNDUP2((a), sizeof(uint64_t))
 23#elif __OpenBSD__
 24    // https://github.com/openbsd/src/blob/ca647cfa4ec3ccb8360714bc0ebc32a394f7fb6a/regress/sys/netinet/bindconnect/bindconnect.c#L250
 25    #define ROUNDUP(a) ROUNDUP2((a), sizeof(long))
 26#else
 27    #error unknown platform
 28#endif
 29
 30static struct sockaddr*
 31get_rt_address(struct rt_msghdr* rtm, int desired) {
 32    struct sockaddr* sa = (struct sockaddr*) (rtm + 1);
 33
 34    for (int i = 0; i < RTAX_MAX; i++) {
 35        if (rtm->rtm_addrs & (1 << i)) {
 36            if ((1 << i) == desired) {
 37                return sa;
 38            }
 39            sa = (struct sockaddr*) (ROUNDUP(sa->sa_len) + (char*) sa);
 40        }
 41    }
 42    return nullptr;
 43}
 44
 45bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) {
 46    int mib[6] = { CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY };
 47    size_t needed;
 48
 49    if (sysctl(mib, 6, nullptr, &needed, nullptr, 0) < 0 || needed == 0) {
 50        return false;
 51    }
 52
 53    FF_AUTO_FREE char* buf = malloc(needed);
 54
 55    if (sysctl(mib, 6, buf, &needed, nullptr, 0) < 0) {
 56        return false;
 57    }
 58
 59    char* lim = buf + needed;
 60    struct rt_msghdr* rtm;
 61    for (char* next = buf; next < lim; next += rtm->rtm_msglen) {
 62        rtm = (struct rt_msghdr*) next;
 63        struct sockaddr* sa = (struct sockaddr*) (rtm + 1);
 64
 65        if ((rtm->rtm_flags & RTF_GATEWAY) && !(rtm->rtm_flags & RTF_REJECT) && (sa->sa_family == AF_INET)) {
 66            struct sockaddr_dl* sdl = (struct sockaddr_dl*) get_rt_address(rtm, RTA_IFP);
 67            if (sdl && sdl->sdl_family == AF_LINK) {
 68                if (sdl->sdl_nlen > IF_NAMESIZE) {
 69                    continue;
 70                }
 71                memcpy(result->ifName, sdl->sdl_data, sdl->sdl_nlen);
 72                result->ifName[sdl->sdl_nlen] = '\0';
 73                result->ifIndex = sdl->sdl_index;
 74
 75                // Get the preferred source address
 76                struct sockaddr_in* src = (struct sockaddr_in*) get_rt_address(rtm, RTA_IFA);
 77                if (src && src->sin_family == AF_INET) {
 78                    result->preferredSourceAddrV4 = src->sin_addr.s_addr;
 79                }
 80
 81                return true;
 82            }
 83        }
 84    }
 85    return false;
 86}
 87
 88bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) {
 89    int mib[6] = { CTL_NET, PF_ROUTE, 0, AF_INET6, NET_RT_FLAGS, RTF_GATEWAY };
 90    size_t needed;
 91
 92    if (sysctl(mib, 6, nullptr, &needed, nullptr, 0) < 0 || needed == 0) {
 93        return false;
 94    }
 95
 96    FF_AUTO_FREE char* buf = malloc(needed);
 97
 98    if (sysctl(mib, 6, buf, &needed, nullptr, 0) < 0) {
 99        return false;
100    }
101
102    char* lim = buf + needed;
103    struct rt_msghdr* rtm;
104    for (char* next = buf; next < lim; next += rtm->rtm_msglen) {
105        rtm = (struct rt_msghdr*) next;
106        struct sockaddr* sa = (struct sockaddr*) (rtm + 1);
107
108        if ((rtm->rtm_flags & RTF_GATEWAY) && !(rtm->rtm_flags & RTF_REJECT) && (sa->sa_family == AF_INET6)) {
109            struct sockaddr_dl* sdl = (struct sockaddr_dl*) get_rt_address(rtm, RTA_IFP);
110            if (sdl && sdl->sdl_family == AF_LINK) {
111                if (sdl->sdl_nlen > IF_NAMESIZE) {
112                    continue;
113                }
114                memcpy(result->ifName, sdl->sdl_data, sdl->sdl_nlen);
115                result->ifName[sdl->sdl_nlen] = '\0';
116                result->ifIndex = sdl->sdl_index;
117
118                return true;
119            }
120        }
121    }
122
123    return false;
124}