summaryrefslogtreecommitdiffstats
path: root/src/detection/netio/netio_haiku.cpp
blob: 88a123d1d42faa21d63869b7283699e6619c156e (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
48
49
50
51
52
extern "C" {
#include "netio.h"
#include "common/netif.h"
}

#include <NetworkInterface.h>
#include <NetworkRoster.h>

const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) {
    BNetworkRoster& roster = BNetworkRoster::Default();

    BNetworkInterface interface;
    uint32 cookie = 0;

    uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteV4()->ifIndex;

    while (roster.GetNextInterface(&cookie, interface) == B_OK) {
        if (!interface.Exists()) {
            continue;
        }

        bool defaultRoute = interface.Index() == defaultRouteIfIndex;
        if (options->defaultRouteOnly && !defaultRoute) {
            continue;
        }

        if (options->namePrefix.length && strncmp(interface.Name(), options->namePrefix.chars, options->namePrefix.length) != 0) {
            continue;
        }

        ifreq_stats stats = {};
        if (interface.GetStats(stats) != B_OK) {
            continue;
        }

        FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result);
        *counters = (FFNetIOResult) {
            .name = ffStrbufCreateS(interface.Name()),
            .defaultRoute = defaultRoute,
            .txBytes = stats.send.bytes,
            .rxBytes = stats.receive.bytes,
            .txPackets = stats.send.packets,
            .rxPackets = stats.receive.packets,
            .rxErrors = stats.receive.errors,
            .txErrors = stats.send.errors,
            .rxDrops = stats.receive.dropped,
            .txDrops = stats.send.dropped
        };
    }

    return NULL;
}