|  | 
| 1 | 1 | #include "netif.h" | 
| 2 | 2 | 
 | 
| 3 | 3 | #include "common/io/io.h" | 
|  | 4 | +#include "util/mallocHelper.h" | 
| 4 | 5 | 
 | 
| 5 | 6 | #include <net/if.h> | 
| 6 | 7 | #include <net/if_dl.h> | 
| 7 | 8 | #include <net/route.h> | 
| 8 | 9 | #include <netinet/in.h> | 
| 9 | 10 | #include <sys/socket.h> | 
| 10 | 11 | 
 | 
|  | 12 | +#ifdef __OpenBSD__ | 
|  | 13 | +    #include <sys/sysctl.h> | 
|  | 14 | +#endif | 
|  | 15 | + | 
| 11 | 16 | #define ROUNDUP2(a, n)       ((a) > 0 ? (1 + (((a) - 1U) | ((n) - 1))) : (n)) | 
| 12 | 17 | 
 | 
| 13 | 18 | #if defined(__APPLE__) | 
| @@ -47,6 +52,39 @@ get_rt_address(struct rt_msghdr *rtm, int desired) | 
| 47 | 52 | 
 | 
| 48 | 53 | bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex) | 
| 49 | 54 | { | 
|  | 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 | 
| 50 | 88 |     //https://github.com/hashPirate/copenheimer-masscan-fork/blob/36f1ed9f7b751a7dccd5ed27874e2e703db7d481/src/rawsock-getif.c#L104 | 
| 51 | 89 | 
 | 
| 52 | 90 |     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) | 
| 106 | 144 |             return false; | 
| 107 | 145 |         } | 
| 108 | 146 |     } | 
|  | 147 | +    #endif | 
|  | 148 | + | 
| 109 | 149 |     return false; | 
| 110 | 150 | } | 
0 commit comments