Skip to content

Commit e968bca

Browse files
yelnineiCarterLi
authored andcommitted
Fork netif_linux to netif_gnu.
netif_linux now uses linux specifics which do not work on GNU.
1 parent 71fbe04 commit e968bca

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ elseif(GNU)
11981198
list(APPEND LIBFASTFETCH_SRC
11991199
src/common/dbus.c
12001200
src/common/io/io_unix.c
1201-
src/common/netif/netif_linux.c
1201+
src/common/netif/netif_gnu.c
12021202
src/common/networking/networking_linux.c
12031203
src/common/processing_linux.c
12041204
src/detection/battery/battery_nosupport.c

src/common/netif/netif_gnu.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "netif.h"
2+
#include "common/io/io.h"
3+
4+
#include <net/if.h>
5+
#include <stdio.h>
6+
7+
#define FF_STR_INDIR(x) #x
8+
#define FF_STR(x) FF_STR_INDIR(x)
9+
10+
static bool getDefaultRouteIPv4(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
11+
{
12+
if (preferredSourceAddr)
13+
*preferredSourceAddr = 0;
14+
// Based on netif_linux.c before 5e770dc8b019702ca458cc0cad46161ebec608a4
15+
FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/route", "r");
16+
17+
if (!netRoute) return false;
18+
19+
// skip first line
20+
FF_UNUSED(fscanf(netRoute, "%*[^\n]\n"));
21+
22+
unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu, ...
23+
while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2)
24+
{
25+
if (destination != 0) continue;
26+
*ifIndex = if_nametoindex(iface);
27+
return true;
28+
}
29+
iface[0] = '\0';
30+
return false;
31+
}
32+
33+
static bool getDefaultRouteIPv6(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
34+
{
35+
// TODO ipv6
36+
return false;
37+
}
38+
39+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
40+
{
41+
if (getDefaultRouteIPv4(iface, ifIndex, preferredSourceAddr))
42+
return true;
43+
44+
return getDefaultRouteIPv6(iface, ifIndex, preferredSourceAddr);
45+
}

0 commit comments

Comments
 (0)