Skip to content

Commit 5e5c0b4

Browse files
committed
NetIO: improve performance
1 parent 154657b commit 5e5c0b4

File tree

9 files changed

+111
-106
lines changed

9 files changed

+111
-106
lines changed

src/common/netif/netif.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
#include "netif.h"
22

33
#ifndef _WIN32
4-
#include <net/if.h>
4+
#include <net/if.h>
5+
#else
6+
#define IF_NAMESIZE 16
7+
#endif
58

6-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1]);
9+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex);
10+
enum { IF_INDEX_UNINITIALIZED = (uint32_t) -1, IF_INDEX_INVALID = (uint32_t) -2 };
11+
static uint32_t ifIndex = IF_INDEX_UNINITIALIZED;
12+
static char ifName[IF_NAMESIZE + 1];
713

8-
const char* ffNetifGetDefaultRoute()
14+
static inline void init()
915
{
10-
static char iface[IF_NAMESIZE + 1];
11-
12-
if (*(uint16_t*) iface == 0)
13-
{
14-
if (!ffNetifGetDefaultRouteImpl(iface))
15-
iface[1] = 1;
16-
}
17-
18-
return iface;
16+
if (ifIndex == (uint32_t) IF_INDEX_UNINITIALIZED && !ffNetifGetDefaultRouteImpl(ifName, &ifIndex))
17+
ifIndex = (uint32_t) IF_INDEX_INVALID;
1918
}
20-
#else
21-
bool ffNetifGetDefaultRouteImpl(uint32_t* ifIndex);
2219

23-
uint32_t ffNetifGetDefaultRoute()
20+
const char* ffNetifGetDefaultRouteIfName()
2421
{
25-
static uint32_t ifIndex = (uint32_t) -1;
26-
27-
if (ifIndex == (uint32_t) -1)
28-
{
29-
if (!ffNetifGetDefaultRouteImpl(&ifIndex))
30-
ifIndex = (uint32_t) -2;
31-
}
22+
init();
23+
return ifName;
24+
}
3225

26+
uint32_t ffNetifGetDefaultRouteIfIndex()
27+
{
28+
init();
3329
return ifIndex;
3430
}
35-
#endif

src/common/netif/netif.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "fastfetch.h"
44

55
#ifndef _WIN32
6-
const char* ffNetifGetDefaultRoute();
7-
#else
8-
uint32_t ffNetifGetDefaultRoute();
6+
const char* ffNetifGetDefaultRouteIfName();
97
#endif
8+
9+
uint32_t ffNetifGetDefaultRouteIfIndex();

src/common/netif/netif_bsd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ get_rt_address(struct rt_msghdr *rtm, int desired)
3939
return NULL;
4040
}
4141

42-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1])
42+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
4343
{
4444
//https://github.com/hashPirate/copenheimer-masscan-fork/blob/36f1ed9f7b751a7dccd5ed27874e2e703db7d481/src/rawsock-getif.c#L104
4545

@@ -88,6 +88,7 @@ bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1])
8888
assert(sdl->sdl_nlen <= IF_NAMESIZE);
8989
memcpy(iface, sdl->sdl_data, sdl->sdl_nlen);
9090
iface[sdl->sdl_nlen] = '\0';
91+
*ifIndex = sdl->sdl_index;
9192
return true;
9293
}
9394
return false;

src/common/netif/netif_linux.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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])
10+
bool ffNetifGetDefaultRouteImpl(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;
@@ -21,6 +21,7 @@ bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1])
2121
while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2)
2222
{
2323
if (destination != 0) continue;
24+
*ifIndex = if_nametoindex(iface);
2425
return true;
2526
}
2627
return false;

src/common/netif/netif_windows.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <iphlpapi.h>
55

6-
bool ffNetifGetDefaultRouteImpl(uint32_t* ifIndex)
6+
bool ffNetifGetDefaultRouteImpl(char* iface/* unsupported */, uint32_t* ifIndex)
77
{
88
ULONG size = 0;
99
if (GetIpForwardTable(NULL, &size, TRUE) != ERROR_INSUFFICIENT_BUFFER)

src/detection/localip/localip_linux.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
5757
if(getifaddrs(&ifAddrStruct) < 0)
5858
return "getifaddrs(&ifAddrStruct) failed";
5959

60-
const char* defaultRouteIface = ffNetifGetDefaultRoute();
60+
const char* defaultRouteIfName = ffNetifGetDefaultRouteIfName();
6161

6262
for (struct ifaddrs* ifa = ifAddrStruct; ifa; ifa = ifa->ifa_next)
6363
{
6464
if (!ifa->ifa_addr || !(ifa->ifa_flags & IFF_RUNNING))
6565
continue;
6666

67-
bool isDefaultRoute = ffStrEquals(defaultRouteIface, ifa->ifa_name);
67+
bool isDefaultRoute = ffStrEquals(defaultRouteIfName, ifa->ifa_name);
6868
if (options->defaultRouteOnly && !isDefaultRoute)
6969
continue;
7070

src/detection/localip/localip_windows.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
7070
return "GetAdaptersAddresses() failed";
7171
}
7272

73-
uint32_t defaultRouteIfIndex = ffNetifGetDefaultRoute();
73+
uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteIfIndex();
7474

7575
// Iterate through all of the adapters
7676
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)

src/detection/netio/netio_bsd.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
1616
{
17+
uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteIfIndex();
18+
1719
size_t bufSize = 0;
18-
if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, NULL, &bufSize, 0, 0) < 0)
19-
return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, NULL, &bufSize, 0, 0) failed";
20+
if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, (options->defaultRouteOnly ? (int) defaultRouteIfIndex : 0) }, 6, NULL, &bufSize, 0, 0) < 0)
21+
return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, ifIndex }, 6, NULL, &bufSize, 0, 0) failed";
2022

2123
FF_AUTO_FREE struct if_msghdr2* buf = (struct if_msghdr2*) malloc(bufSize);
22-
if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, buf, &bufSize, 0, 0) < 0)
23-
return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, buf, &bufSize, 0, 0) failed";
24-
25-
const char* defaultRouteIface = ffNetifGetDefaultRoute();
24+
if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, (options->defaultRouteOnly ? (int) defaultRouteIfIndex : 0) }, 6, buf, &bufSize, 0, 0) < 0)
25+
return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, ifIndex }, 6, buf, &bufSize, 0, 0) failed";
2626

2727
for (struct if_msghdr2* ifm = buf;
2828
ifm < (struct if_msghdr2*) ((uint8_t*) buf + bufSize);
@@ -36,10 +36,6 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
3636

3737
sdl->sdl_data[sdl->sdl_nlen] = 0;
3838

39-
bool isDefaultRoute = ffStrEquals(sdl->sdl_data, defaultRouteIface);
40-
if(options->defaultRouteOnly && !isDefaultRoute)
41-
continue;
42-
4339
if (options->namePrefix.length && strncmp(sdl->sdl_data, options->namePrefix.chars, options->namePrefix.length) != 0)
4440
continue;
4541

@@ -54,7 +50,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
5450
.rxErrors = ifm->ifm_data.ifi_ierrors,
5551
.txDrops = 0, // unsupported
5652
.rxDrops = ifm->ifm_data.ifi_iqdrops,
57-
.defaultRoute = isDefaultRoute,
53+
.defaultRoute = sdl->sdl_index == defaultRouteIfIndex,
5854
};
5955
}
6056

src/detection/netio/netio_linux.c

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,60 @@
66

77
#include <net/if.h>
88

9+
static void getData(FFstrbuf* buffer, const char* ifName, bool isDefaultRoute, FFstrbuf* path, FFlist* result)
10+
{
11+
ffStrbufSetF(path, "/sys/class/net/%s/operstate", ifName);
12+
if(!ffReadFileBuffer(path->chars, buffer) || !ffStrbufEqualS(buffer, "up"))
13+
return;
14+
15+
FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result);
16+
ffStrbufInitS(&counters->name, ifName);
17+
counters->defaultRoute = isDefaultRoute;
18+
19+
ffStrbufSetF(path, "/sys/class/net/%s/statistics/", ifName);
20+
uint32_t statLen = path->length;
21+
22+
ffStrbufAppendS(path, "rx_bytes");
23+
if (ffReadFileBuffer(path->chars, buffer))
24+
counters->rxBytes = ffStrbufToUInt(buffer, 0);
25+
ffStrbufSubstrBefore(path, statLen);
26+
27+
ffStrbufAppendS(path, "tx_bytes");
28+
if (ffReadFileBuffer(path->chars, buffer))
29+
counters->txBytes = ffStrbufToUInt(buffer, 0);
30+
ffStrbufSubstrBefore(path, statLen);
31+
32+
ffStrbufAppendS(path, "rx_packets");
33+
if (ffReadFileBuffer(path->chars, buffer))
34+
counters->rxPackets = ffStrbufToUInt(buffer, 0);
35+
ffStrbufSubstrBefore(path, statLen);
36+
37+
ffStrbufAppendS(path, "tx_packets");
38+
if (ffReadFileBuffer(path->chars, buffer))
39+
counters->txPackets = ffStrbufToUInt(buffer, 0);
40+
ffStrbufSubstrBefore(path, statLen);
41+
42+
ffStrbufAppendS(path, "rx_errors");
43+
if (ffReadFileBuffer(path->chars, buffer))
44+
counters->rxErrors = ffStrbufToUInt(buffer, 0);
45+
ffStrbufSubstrBefore(path, statLen);
46+
47+
ffStrbufAppendS(path, "tx_errors");
48+
if (ffReadFileBuffer(path->chars, buffer))
49+
counters->txErrors = ffStrbufToUInt(buffer, 0);
50+
ffStrbufSubstrBefore(path, statLen);
51+
52+
ffStrbufAppendS(path, "rx_dropped");
53+
if (ffReadFileBuffer(path->chars, buffer))
54+
counters->rxDrops = ffStrbufToUInt(buffer, 0);
55+
ffStrbufSubstrBefore(path, statLen);
56+
57+
ffStrbufAppendS(path, "tx_dropped");
58+
if (ffReadFileBuffer(path->chars, buffer))
59+
counters->txDrops = ffStrbufToUInt(buffer, 0);
60+
ffStrbufSubstrBefore(path, statLen);
61+
}
62+
963
const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
1064
{
1165
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/net");
@@ -14,71 +68,29 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
1468
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateA(64);
1569
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
1670

17-
const char* defaultRouteIface = ffNetifGetDefaultRoute();
71+
const char* defaultRouteIfName = ffNetifGetDefaultRouteIfName();
1872

19-
struct dirent* entry;
20-
while((entry = readdir(dirp)) != NULL)
73+
if (options->defaultRouteOnly)
74+
{
75+
if (options->namePrefix.length && strncmp(defaultRouteIfName, options->namePrefix.chars, options->namePrefix.length) != 0)
76+
return NULL;
77+
78+
getData(&buffer, defaultRouteIfName, true, &path, result);
79+
}
80+
else
2181
{
22-
if(entry->d_name[0] == '.')
23-
continue;
24-
25-
bool isDefaultRoute = ffStrEquals(entry->d_name, defaultRouteIface);
26-
if(options->defaultRouteOnly && !isDefaultRoute)
27-
continue;
28-
29-
if (options->namePrefix.length && strncmp(entry->d_name, options->namePrefix.chars, options->namePrefix.length) != 0)
30-
continue;
31-
32-
ffStrbufSetF(&path, "/sys/class/net/%s/operstate", entry->d_name);
33-
if(!ffReadFileBuffer(path.chars, &buffer) || !ffStrbufEqualS(&buffer, "up"))
34-
continue;
35-
36-
FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result);
37-
ffStrbufInitS(&counters->name, entry->d_name);
38-
counters->defaultRoute = isDefaultRoute;
39-
40-
ffStrbufSetF(&path, "/sys/class/net/%s/statistics/", entry->d_name);
41-
uint32_t statLen = path.length;
42-
43-
ffStrbufAppendS(&path, "rx_bytes");
44-
if (ffReadFileBuffer(path.chars, &buffer))
45-
counters->rxBytes = ffStrbufToUInt(&buffer, 0);
46-
ffStrbufSubstrBefore(&path, statLen);
47-
48-
ffStrbufAppendS(&path, "tx_bytes");
49-
if (ffReadFileBuffer(path.chars, &buffer))
50-
counters->txBytes = ffStrbufToUInt(&buffer, 0);
51-
ffStrbufSubstrBefore(&path, statLen);
52-
53-
ffStrbufAppendS(&path, "rx_packets");
54-
if (ffReadFileBuffer(path.chars, &buffer))
55-
counters->rxPackets = ffStrbufToUInt(&buffer, 0);
56-
ffStrbufSubstrBefore(&path, statLen);
57-
58-
ffStrbufAppendS(&path, "tx_packets");
59-
if (ffReadFileBuffer(path.chars, &buffer))
60-
counters->txPackets = ffStrbufToUInt(&buffer, 0);
61-
ffStrbufSubstrBefore(&path, statLen);
62-
63-
ffStrbufAppendS(&path, "rx_errors");
64-
if (ffReadFileBuffer(path.chars, &buffer))
65-
counters->rxErrors = ffStrbufToUInt(&buffer, 0);
66-
ffStrbufSubstrBefore(&path, statLen);
67-
68-
ffStrbufAppendS(&path, "tx_errors");
69-
if (ffReadFileBuffer(path.chars, &buffer))
70-
counters->txErrors = ffStrbufToUInt(&buffer, 0);
71-
ffStrbufSubstrBefore(&path, statLen);
72-
73-
ffStrbufAppendS(&path, "rx_dropped");
74-
if (ffReadFileBuffer(path.chars, &buffer))
75-
counters->rxDrops = ffStrbufToUInt(&buffer, 0);
76-
ffStrbufSubstrBefore(&path, statLen);
77-
78-
ffStrbufAppendS(&path, "tx_dropped");
79-
if (ffReadFileBuffer(path.chars, &buffer))
80-
counters->txDrops = ffStrbufToUInt(&buffer, 0);
81-
ffStrbufSubstrBefore(&path, statLen);
82+
struct dirent* entry;
83+
while((entry = readdir(dirp)) != NULL)
84+
{
85+
const char* ifName = entry->d_name;
86+
if(ifName[0] == '.')
87+
continue;
88+
89+
if (options->namePrefix.length && strncmp(ifName, options->namePrefix.chars, options->namePrefix.length) != 0)
90+
continue;
91+
92+
getData(&buffer, ifName, ffStrEquals(ifName, defaultRouteIfName), &path, result);
93+
}
8294
}
8395

8496
return NULL;

0 commit comments

Comments
 (0)