Skip to content

Commit

Permalink
Linux: simplify ffNetifGetDefaultRouteImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
apocelipes committed Oct 12, 2024
1 parent 986d36c commit e3a3b53
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/common/netif/netif_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

#define FF_STR_INDIR(x) #x
#define FF_STR(x) FF_STR_INDIR(x)
// /proc/net/route has a fixed 128 chars line size, +1 for the NUL
#define NET_ROUTE_LINE_SIZE 129

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

// skip first line
flockfile(netRoute);
while (getc_unlocked(netRoute) != '\n');
funlockfile(netRoute);
unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu,
char skippedLine[NET_ROUTE_LINE_SIZE];
if (!fgets(skippedLine, NET_ROUTE_LINE_SIZE, netRoute)) return false;

unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu,
while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2)
{
if (destination != 0) continue;
Expand Down

0 comments on commit e3a3b53

Please sign in to comment.