summaryrefslogtreecommitdiffstats
path: root/src/detection/localip/localip.h
blob: d3b9015bf8e59e294f059c3c7215a4e2ed3227e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include "fastfetch.h"
#include "modules/localip/option.h"

#ifndef IN6_IS_ADDR_GLOBAL
    /* Global Unicast: 2000::/3 (001...) */
    #define IN6_IS_ADDR_GLOBAL(a) (((a)->s6_addr[0] & 0xE0) == 0x20)
#endif
#ifndef IN6_IS_ADDR_UNIQUE_LOCAL
    /* Unique Local: fc00::/7 (1111 110...) */
    #define IN6_IS_ADDR_UNIQUE_LOCAL(a) (((a)->s6_addr[0] & 0xFE) == 0xFC)
#endif
#ifndef IN6_IS_ADDR_LINKLOCAL
    /* Link-Local: fe80::/10 (1111 1110 10...) */
    #define IN6_IS_ADDR_LINKLOCAL(a) (((a)->s6_addr[0] == 0xFE) && (((a)->s6_addr[1] & 0xC0) == 0x80))
#endif

typedef struct FFLocalIpResult {
    FFstrbuf name;
    FFstrbuf ipv4;
    FFstrbuf ipv6;
    FFstrbuf mac;
    FFstrbuf flags;
    int32_t mtu;
    int32_t speed; // in Mbps
    FFLocalIpType defaultRoute;
} FFLocalIpResult;

typedef struct FFLocalIpNIFlag {
    uint32_t flag;
    const char* name;
} FFLocalIpNIFlag;

static inline void ffLocalIpFillNIFlags(FFstrbuf* buf, uint64_t flag, const FFLocalIpNIFlag names[]) {
    for (const FFLocalIpNIFlag* nf = names; flag && nf->name; ++nf) {
        if (flag & nf->flag) {
            if (buf->length > 0) {
                ffStrbufAppendC(buf, ',');
            }
            ffStrbufAppendS(buf, nf->name);
            flag &= ~nf->flag;
        }
    }
}

const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results);