Skip to content

Commit 64568b3

Browse files
committed
Netif (Haiku): add preferred source addr
1 parent 02c9997 commit 64568b3

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

src/common/netif/netif_haiku.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
4141

4242
while (interface < end) {
4343
if (interface->ifr_route.flags & RTF_DEFAULT) {
44+
// interface->ifr_metric?
4445
strlcpy(result->ifName, interface->ifr_name, IF_NAMESIZE);
4546
result->ifIndex = if_nametoindex(interface->ifr_name);
46-
// TODO: Get the preferred source address
47+
if (interface->ifr_route.source)
48+
result->preferredSourceAddrV4 = ((struct sockaddr_in*)interface->ifr_route.source)->sin_addr.s_addr;
4749
return true;
4850
}
4951

@@ -55,15 +57,56 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
5557
if (interface->ifr_route.gateway != NULL)
5658
addressSize += interface->ifr_route.gateway->sa_len;
5759

58-
interface = (struct ifreq*)((addr_t)interface + IF_NAMESIZE
59-
+ sizeof(struct route_entry) + addressSize);
60+
interface = (struct ifreq*)((addr_t)interface + IF_NAMESIZE + sizeof(struct route_entry) + addressSize);
6061
}
6162

6263
return false;
6364
}
6465

6566
bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
6667
{
67-
// TODO: AF_INET6
68+
FF_AUTO_CLOSE_FD int pfRoute = socket(AF_INET, SOCK_RAW, AF_INET6);
69+
if (pfRoute < 0)
70+
return false;
71+
72+
struct ifconf config;
73+
config.ifc_len = sizeof(config.ifc_value);
74+
if (ioctl(pfRoute, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
75+
return false;
76+
77+
int size = config.ifc_value;
78+
if (size <= 0)
79+
return false;
80+
81+
FF_AUTO_FREE void *buffer = malloc((size_t) size);
82+
if (buffer == NULL) {
83+
return false;
84+
}
85+
86+
config.ifc_len = size;
87+
config.ifc_buf = buffer;
88+
if (ioctl(pfRoute, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
89+
return false;
90+
91+
struct ifreq *interface = (struct ifreq*)buffer;
92+
struct ifreq *end = (struct ifreq*)((uint8_t*)buffer + size);
93+
94+
while (interface < end) {
95+
if (interface->ifr_route.flags & RTF_DEFAULT) {
96+
strlcpy(result->ifName, interface->ifr_name, IF_NAMESIZE);
97+
result->ifIndex = if_nametoindex(interface->ifr_name);
98+
return true;
99+
}
100+
101+
size_t addressSize = 0;
102+
if (interface->ifr_route.destination != NULL)
103+
addressSize += interface->ifr_route.destination->sa_len;
104+
if (interface->ifr_route.mask != NULL)
105+
addressSize += interface->ifr_route.mask->sa_len;
106+
if (interface->ifr_route.gateway != NULL)
107+
addressSize += interface->ifr_route.gateway->sa_len;
108+
109+
interface = (struct ifreq*)((addr_t)interface + IF_NAMESIZE + sizeof(struct route_entry) + addressSize);
110+
}
68111
return false;
69112
}

0 commit comments

Comments
 (0)