-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDapNetworkMonitorWindows.cpp
More file actions
129 lines (119 loc) · 4.53 KB
/
DapNetworkMonitorWindows.cpp
File metadata and controls
129 lines (119 loc) · 4.53 KB
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
123
124
125
126
127
128
129
#include <QThread>
#include "DapNetworkMonitorWindows.h"
DapNetworkMonitorWindows::DapNetworkMonitorWindows(QObject *parent): DapNetworkMonitorAbstract(parent) {
qInfo() << "Dap Network Monitor started";
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QtConcurrent::run([this] { internalWorker(); });
#else
QtConcurrent::run(this, &DapNetworkMonitorWindows::internalWorker);
#endif
}
bool DapNetworkMonitorWindows::isTunDriverInstalled() const {
return (getTapGUID() != NULL);
}
bool DapNetworkMonitorWindows::monitoringStart() {
m_isMonitoringRunning.store(true);
return m_isMonitoringRunning;
}
bool DapNetworkMonitorWindows::monitoringStop() {
m_isMonitoringRunning.store(false);
return m_isMonitoringRunning;
}
void DapNetworkMonitorWindows::cbRouteChanged(void *, PMIB_IPFORWARD_ROW2 route, MIB_NOTIFICATION_TYPE type) {
if (!instance()->m_isMonitoringRunning.load()) {
return;
}
switch (type) {
case MibAddInstance:
if (route->NextHop.Ipv4.sin_addr.S_un.S_addr == 0) {
if (route->InterfaceIndex == instance()->m_TapAdapterIndex ||
route->InterfaceIndex == instance()->m_DefaultAdapterIndex) {
instance()->m_isTunGatewayDefined.store(true);
emit instance()->sigTunGatewayDefined();
}
qInfo() << "Set default gw on interface [ " << route->InterfaceIndex << " ]";
}
break;
case MibDeleteInstance:
if (route->NextHop.Ipv4.sin_addr.S_un.S_addr == 0) {
if (route->InterfaceIndex == instance()->m_TapAdapterIndex ||
route->InterfaceIndex == instance()->m_DefaultAdapterIndex) {
instance()->m_isTunGatewayDefined.store(false);
emit instance()->sigTunGatewayUndefined();
}
qInfo() << "Removed default gw on interface [ " << route->InterfaceIndex << " ]";
}
break;
case MibParameterNotification:
qInfo() << "Changed gw " << inet_ntoa(route->NextHop.Ipv4.sin_addr) <<
" on inteface [ " << route->InterfaceIndex << " ]";
break;
default:
break;
}
}
void DapNetworkMonitorWindows::cbIfaceChanged(void *, PMIB_IPINTERFACE_ROW row, MIB_NOTIFICATION_TYPE type) {
if (!instance()->m_isMonitoringRunning.load()) {
return;
}
switch (type) {
case MibAddInstance:
qWarning() << "Adapter [ " << row->InterfaceIndex << " ] enabled";
if (row->InterfaceIndex == instance()->m_TapAdapterIndex ||
row->InterfaceIndex == instance()->m_DefaultAdapterIndex) {
instance()->m_isInterfaceDefined.store(true);
emit instance()->sigInterfaceDefined();
}
break;
case MibDeleteInstance:
qWarning() << "Adapter [ " << row->InterfaceIndex << " ] disabled";
if (row->InterfaceIndex == instance()->m_TapAdapterIndex ||
row->InterfaceIndex == instance()->m_DefaultAdapterIndex) {
instance()->m_isInterfaceDefined.store(false);
emit instance()->sigInterfaceUndefined();
}
break;
/*case MibParameterNotification:
qWarning() << "Adapter [ " << row->InterfaceIndex << " ] settings changed";
if (row->InterfaceIndex == instance()->m_TapAdapterIndex ||
row->InterfaceIndex == instance()->m_DefaultAdapterIndex) {
if (row->Connected) {
qWarning() << "[ " << row->InterfaceIndex << " ] enabled";
emit instance()->sigInterfaceDefined();
} else {
qWarning() << "[ " << row->InterfaceIndex << " ] disabled";
emit instance()->sigInterfaceUndefined();
}
}
break;
*/
default:
break;
}
}
void DapNetworkMonitorWindows::internalWorker() {
HANDLE hAddrChange, hRouteChange;
unsigned int ctx = 0;
DWORD res = NotifyRouteChange2(AF_INET, cbRouteChanged, &ctx, TRUE, &hRouteChange);
if (res != NO_ERROR) {
qCritical() << "Can't trace route table, error " << res;
return;
}
res = NotifyIpInterfaceChange(AF_INET, cbIfaceChanged, &ctx, TRUE, &hAddrChange);
if (res != NO_ERROR) {
qCritical() << "Can't trace network interfaces, error " << res;
return;
}
}
void DapNetworkMonitorWindows::procErr(const int a_err, const QString &a_str) {
Q_UNUSED(a_str)
switch (a_err) {
case WSAEACCES:
case WSAENETUNREACH:
case WSAEHOSTUNREACH:
m_isHostReachable.store(false);
break;
default:
break;
}
}