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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
--- netstat.cpp.orig 2021-11-05 10:06:40 UTC
+++ netstat.cpp
@@ -26,8 +26,23 @@
#include "netstat.h"
#include "netstat_p.h"
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#if defined(HAVE_SYSCTL_H) && defined(HAVE_IF_H)
+extern "C"
+{
+ #include <net/if.h>
+ #include <net/if_mib.h>
+ #include <net/if_types.h>
+ #include <sys/socket.h> /* PF_LINK */
+ #include <sys/types.h>
+ #include <sys/sysctl.h>
+}
+#endif
+
namespace SysStat {
NetStatPrivate::NetStatPrivate(NetStat *parent)
@@ -37,6 +52,7 @@ NetStatPrivate::NetStatPrivate(NetStat *parent)
connect(mTimer, SIGNAL(timeout()), SLOT(timeout()));
+#ifndef HAVE_SYSCTL_H
QStringList rows(readAllFile("/proc/net/dev").split(QLatin1Char('\n'), Qt::SkipEmptyParts));
@@ -50,12 +66,79 @@ NetStatPrivate::NetStatPrivate(NetStat *parent)
mSources.append(tokens[0].trimmed());
}
+#else
+ int count;
+ size_t len;
+ int cntifmib[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT };// net.link.generic.system.ifcount;
+ len = sizeof(int);
+ if (sysctl(cntifmib, 5, &count, &len, NULL, 0) < 0)
+ perror("sysctl");
+
+
+ struct ifmibdata ifmd;
+ size_t len1 = sizeof(ifmd);
+ for (int i=1; i<=count;i++) {
+ int name[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, i, IFDATA_GENERAL };
+
+ if (sysctl(name, 6, &ifmd, &len1, NULL, 0) < 0) {
+ perror("sysctl");
+ }
+ if ((ifmd.ifmd_data.ifi_type == IFT_ETHER) || (ifmd.ifmd_data.ifi_type == IFT_IEEE80211)) {
+ const char *iface = ifmd.ifmd_name;
+ mSources.append(QString::fromLatin1(iface));
+ }
+ }
+#endif
}
NetStatPrivate::~NetStatPrivate() = default;
void NetStatPrivate::timeout()
{
+#if defined(HAVE_IF_H) && defined(HAVE_SYSCTL_H)
+ int count;
+ size_t len;
+ int name[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT };
+ struct ifmibdata ifmd;
+
+ len = sizeof(int);
+ if (sysctl(name, 5, &count, &len, NULL, 0) < 0)
+ return;
+
+ for (int i = 1; i <= count; i++)
+ {
+ len = sizeof(ifmd);
+ int name[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, i, IFDATA_GENERAL };
+
+ if (sysctl(name, 6, &ifmd, &len, NULL, 0) < 0)
+ break;
+
+ if ((ifmd.ifmd_data.ifi_type == IFT_ETHER) || (ifmd.ifmd_data.ifi_type == IFT_IEEE80211))
+ {
+ const char *iface = ifmd.ifmd_name;
+ QString interfaceName = QString::fromLatin1(iface);
+ if ((ifmd.ifmd_data.ifi_link_state == LINK_STATE_UP) && (ifmd.ifmd_data.ifi_ipackets > 0))
+ {
+
+
+ Values current;
+ current.received = ifmd.ifmd_data.ifi_ibytes;
+ current.transmitted = ifmd.ifmd_data.ifi_obytes;
+
+ if (!mPrevious.contains(interfaceName))
+ mPrevious.insert(interfaceName, Values());
+ const Values &previous = mPrevious[interfaceName];
+
+ if (interfaceName == mSource)
+ emit update((( current.received - previous.received ) * 1000 ) / mTimer->interval(), (( current.transmitted - previous.transmitted ) * 1000 ) / mTimer->interval());
+
+ mPrevious[interfaceName] = current;
+ } else if(interfaceName == mSource)
+ emit(update(0,0));
+
+ }
+ }
+#else
QStringList rows(readAllFile("/proc/net/dev").split(QLatin1Char('\n'), Qt::SkipEmptyParts));
@@ -97,6 +180,7 @@ void NetStatPrivate::timeout()
mPrevious[interfaceName] = current;
}
+#endif
}
QString NetStatPrivate::defaultSource()
|