|
7 | 7 | #define FF_STR_INDIR(x) #x |
8 | 8 | #define FF_STR(x) FF_STR_INDIR(x) |
9 | 9 |
|
10 | | -bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex) |
| 10 | +static bool getDefaultRouteIPv4(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex) |
11 | 11 | { |
12 | 12 | FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/net/route", "r"); |
13 | 13 | if (!netRoute) return false; |
14 | 14 |
|
15 | 15 | // skip first line |
16 | 16 | FF_UNUSED(fscanf(netRoute, "%*[^\n]\n")); |
17 | 17 |
|
18 | | - unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu, |
| 18 | + unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu, ... |
19 | 19 | while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2) |
20 | 20 | { |
21 | 21 | if (destination != 0) continue; |
22 | 22 | *ifIndex = if_nametoindex(iface); |
23 | 23 | return true; |
24 | 24 | } |
| 25 | + iface[0] = '\0'; |
25 | 26 | return false; |
26 | 27 | } |
| 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