master ohmu/fastfetch-openbsd / src / common / impl / netif.c
ohmu  ·  2026-10-15
 1#include "common/netif.h"
 2#include "common/FFcache.h"
 3
 4#include <string.h>
 5
 6// netio, localip and dns resolve the default route on every round, and it can change while a
 7// `--dynamic-interval` run is going on: a laptop switching from Wi-Fi to Ethernet, a VPN coming up.
 8static FFNetifDefaultRouteResult defaultRouteV4;
 9static FFNetifDefaultRouteResult defaultRouteV6;
10
11static void resetDefaultRoute(void* storage) {
12    // The impls only write the fields they found, so without this a round that finds no default
13    // route would keep reporting the interface of the previous one.
14    memset(storage, 0, sizeof(FFNetifDefaultRouteResult));
15}
16
17static void initDefaultRouteV4(void* storage) {
18    FFNetifDefaultRouteResult* result = storage;
19    result->status = ffNetifGetDefaultRouteImplV4(result) ? FF_NETIF_OK : FF_NETIF_INVALID;
20}
21
22static void initDefaultRouteV6(void* storage) {
23    FFNetifDefaultRouteResult* result = storage;
24    result->status = ffNetifGetDefaultRouteImplV6(result) ? FF_NETIF_OK : FF_NETIF_INVALID;
25}
26
27static FFcacheEntry ffCacheEntryDefaultRouteV4 = {
28    .name = "default route V4",
29    .storage = &defaultRouteV4,
30    .init = initDefaultRouteV4,
31    .destroy = resetDefaultRoute,
32};
33
34static FFcacheEntry ffCacheEntryDefaultRouteV6 = {
35    .name = "default route V6",
36    .storage = &defaultRouteV6,
37    .init = initDefaultRouteV6,
38    .destroy = resetDefaultRoute,
39};
40
41const FFNetifDefaultRouteResult* ffNetifGetDefaultRouteV4(void) {
42    return ffCacheGet(&ffCacheEntryDefaultRouteV4);
43}
44
45const FFNetifDefaultRouteResult* ffNetifGetDefaultRouteV6(void) {
46    return ffCacheGet(&ffCacheEntryDefaultRouteV6);
47}