Skip to content

Commit d0ca944

Browse files
committed
Netif (Linux): support default route detection for ipv6
1 parent 3d7c631 commit d0ca944

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/common/netif/netif_linux.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,46 @@
77
#define FF_STR_INDIR(x) #x
88
#define FF_STR(x) FF_STR_INDIR(x)
99

10-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
10+
static bool getDefaultRouteIPv4(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
1111
{
1212
FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/net/route", "r");
1313
if (!netRoute) return false;
1414

1515
// skip first line
1616
FF_UNUSED(fscanf(netRoute, "%*[^\n]\n"));
1717

18-
unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu,
18+
unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu, ...
1919
while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2)
2020
{
2121
if (destination != 0) continue;
2222
*ifIndex = if_nametoindex(iface);
2323
return true;
2424
}
25+
iface[0] = '\0';
2526
return false;
2627
}
28+
29+
static bool getDefaultRouteIPv6(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
30+
{
31+
FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/net/ipv6_route", "r");
32+
if (!netRoute) return false;
33+
34+
uint32_t prefixLen;
35+
//destination, dest_prefix_len, source, src_prefix_len, next hop, metric, ref counter, use counter, flags, iface
36+
while (fscanf(netRoute, "%*s %x %*s %*s %*s %*s %*s %*s %*s %" FF_STR(IF_NAMESIZE) "s", &prefixLen, iface) == 2)
37+
{
38+
if (prefixLen != 0) continue;
39+
*ifIndex = if_nametoindex(iface);
40+
return true;
41+
}
42+
iface[0] = '\0';
43+
return false;
44+
}
45+
46+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
47+
{
48+
if (getDefaultRouteIPv4(iface, ifIndex))
49+
return true;
50+
51+
return getDefaultRouteIPv6(iface, ifIndex);
52+
}

0 commit comments

Comments
 (0)