summaryrefslogtreecommitdiffstats
path: root/src/common/impl/netif_haiku.c
blob: a32138c68ea7067ec23af239f80793a519268018 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "common/netif.h"
#include "common/io.h"
#include "common/mallocHelper.h"

#include <arpa/inet.h>
#include <net/if.h>
#include <net/route.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <stdio.h>

// loosely based on Haiku's src/bin/network/route/route.cpp

bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) {
    FF_AUTO_CLOSE_FD int pfRoute = socket(AF_INET, SOCK_RAW, AF_INET);
    if (pfRoute < 0) {
        return false;
    }

    struct ifconf config;
    config.ifc_len = sizeof(config.ifc_value);
    if (ioctl(pfRoute, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) {
        return false;
    }

    int size = config.ifc_value;
    if (size <= 0) {
        return false;
    }

    FF_AUTO_FREE void* buffer = malloc((size_t) size);
    if (buffer == NULL) {
        return false;
    }

    config.ifc_len = size;
    config.ifc_buf = buffer;
    if (ioctl(pfRoute, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) {
        return false;
    }

    struct ifreq* interface = (struct ifreq*) buffer;
    struct ifreq* end = (struct ifreq*) ((uint8_t*) buffer + size);

    while (interface < end) {
        if (interface->ifr_route.flags & RTF_DEFAULT) {
            // interface->ifr_metric?
            strlcpy(result->ifName, interface->ifr_name, IF_NAMESIZE);
            result->ifIndex = if_nametoindex(interface->ifr_name);
            if (interface->ifr_route.source) {
                result->preferredSourceAddrV4 = ((struct sockaddr_in*) interface->ifr_route.source)->sin_addr.s_addr;
            }
            return true;
        }

        size_t addressSize = 0;
        if (interface->ifr_route.destination != NULL) {
            addressSize += interface->ifr_route.destination->sa_len;
        }
        if (interface->ifr_route.mask != NULL) {
            addressSize += interface->ifr_route.mask->sa_len;
        }
        if (interface->ifr_route.gateway != NULL) {
            addressSize += interface->ifr_route.gateway->sa_len;
        }

        interface = (struct ifreq*) ((addr_t) interface + IF_NAMESIZE + sizeof(struct route_entry) + addressSize);
    }

    return false;
}

bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) {
    FF_AUTO_CLOSE_FD int pfRoute = socket(AF_INET6, SOCK_RAW, AF_INET6);
    if (pfRoute < 0) {
        return false;
    }

    struct ifconf config;
    config.ifc_len = sizeof(config.ifc_value);
    if (ioctl(pfRoute, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) {
        return false;
    }

    int size = config.ifc_value;
    if (size <= 0) {
        return false;
    }

    FF_AUTO_FREE void* buffer = malloc((size_t) size);
    if (buffer == NULL) {
        return false;
    }

    config.ifc_len = size;
    config.ifc_buf = buffer;
    if (ioctl(pfRoute, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) {
        return false;
    }

    struct ifreq* interface = (struct ifreq*) buffer;
    struct ifreq* end = (struct ifreq*) ((uint8_t*) buffer + size);

    while (interface < end) {
        if (interface->ifr_route.flags & RTF_DEFAULT) {
            strlcpy(result->ifName, interface->ifr_name, IF_NAMESIZE);
            result->ifIndex = if_nametoindex(interface->ifr_name);
            return true;
        }

        size_t addressSize = 0;
        if (interface->ifr_route.destination != NULL) {
            addressSize += interface->ifr_route.destination->sa_len;
        }
        if (interface->ifr_route.mask != NULL) {
            addressSize += interface->ifr_route.mask->sa_len;
        }
        if (interface->ifr_route.gateway != NULL) {
            addressSize += interface->ifr_route.gateway->sa_len;
        }

        interface = (struct ifreq*) ((addr_t) interface + IF_NAMESIZE + sizeof(struct route_entry) + addressSize);
    }
    return false;
}