-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDapNetworkMonitorLinux.cpp
More file actions
172 lines (149 loc) · 6.47 KB
/
DapNetworkMonitorLinux.cpp
File metadata and controls
172 lines (149 loc) · 6.47 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "DapNetworkMonitorLinux.h"
void DapNetworkMonitorLinux::cbMonitorNotification(const dap_network_notification_t notification)
{
auto instance = DapNetworkMonitorLinux::instance();
switch(notification.type) {
case IP_ADDR_ADD:
case IP_ADDR_REMOVE: {
qInfo() << QString("Interface %1 %2 has IP address %3")
.arg(notification.addr.interface_name)
.arg((notification.type == IP_ADDR_ADD ? "now" : "no longer"))
.arg(notification.addr.s_ip);
if(notification.type == IP_ADDR_ADD)
emit instance->sigInterfaceDefined();
else
emit instance->sigInterfaceUndefined();
break;
}
case IP_ROUTE_ADD:
case IP_ROUTE_REMOVE: {
emit instance->sigRouteChanged();
qDebug() << QString("%1 route to destination --> %2/%3 proto %4 and gateway %5")
.arg((notification.type == IP_ROUTE_ADD ? "Add" : "Delete"))
.arg(notification.route.s_destination_address)
.arg(notification.route.netmask)
.arg(notification.route.protocol)
.arg(notification.route.s_gateway_address);
if (notification.type == IP_ROUTE_REMOVE) {
if(notification.route.destination_address == DAP_ADRESS_UNDEFINED &&
notification.route.gateway_address != DAP_ADRESS_UNDEFINED) {
QString gatewayAddr(notification.route.s_gateway_address);
if(gatewayAddr == instance->m_tunnelGateway) {
if (checkTunnelGw()) {
qInfo() << "Tunnel gateway is still defined";
emit instance->sigTunGatewayDefined();
} else {
qInfo() << "Tunnel gateway is undefined";
emit instance->sigTunGatewayUndefined();
}
} else {
qInfo() << "Other gateway is undefined";
emit instance->sigOtherGatewayUndefined();
}
} else if(notification.route.destination_address != DAP_ADRESS_UNDEFINED &&
notification.route.gateway_address != DAP_ADRESS_UNDEFINED) {
if(instance->isUpstreamRoute(notification.route.s_destination_address,
notification.route.s_gateway_address)) {
qInfo() << "Upstream route is undefined";
emit instance->sigUpstreamRouteUndefined();
}
}
} else if (notification.type == IP_ROUTE_ADD) {
if(notification.route.destination_address == DAP_ADRESS_UNDEFINED &&
notification.route.gateway_address != DAP_ADRESS_UNDEFINED) {
QString gatewayAddr(notification.route.s_gateway_address);
if(gatewayAddr == instance->m_tunnelGateway) {
qInfo() << "Tunnel gateway is defined";
emit instance->sigTunGatewayDefined();
} else {
qInfo() << "Other gateway is defined";
emit instance->sigOtherGatewayDefined(gatewayAddr);
}
} else if(notification.route.destination_address != DAP_ADRESS_UNDEFINED &&
notification.route.gateway_address != DAP_ADRESS_UNDEFINED) {
if(instance->isUpstreamRoute(notification.route.s_destination_address,
notification.route.s_gateway_address)) {
qInfo() << "Upstream route is defined";
emit instance->sigUpstreamRouteDefined();
}
}
}
break;
}
case IP_LINK_NEW:
case IP_LINK_DEL: {
qInfo() << QString("Interface %1 is %2 %3 %4")
.arg(notification.link.interface_name)
.arg(( (notification.type == IP_LINK_NEW && notification.link.is_up) ? "UP" : "DOWN") )
.arg((notification.type == IP_LINK_NEW ? "and" : ""))
.arg((notification.type == IP_LINK_NEW ?
(notification.link.is_running ? "RUNNING" : "NOT RUNNING")
: ""));
if(notification.type == IP_LINK_DEL)
emit instance->sigInterfaceUndefined();
else
emit instance->sigInterfaceDefined();
break;
}
default:
qWarning() << "Unknown notification type received";
}
}
bool DapNetworkMonitorLinux::checkTunnelGw()
{
auto instance = DapNetworkMonitorLinux::instance();
int ret;
if(instance->m_tunnelGateway.size() > 0) {
ret = ::system(QString("netstat -rn | grep 'UG ' | grep %1 > /dev/null ")
.arg(instance->m_tunnelGateway)
.toLatin1().constData());
return (ret == 0);
}
return false;
}
bool DapNetworkMonitorLinux::handleNetworkFailure() {
qDebug() << "Attempting to recover network connectivity...";
QProcess process;
QString command = "sudo ifconfig eth0 down && sudo ifconfig eth0 up";
qDebug() << "Executing command:" << command;
process.start("bash", QStringList() << "-c" << command);
process.waitForFinished();
if (process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0) {
qDebug() << "Network interface eth0 restarted successfully.";
} else {
qWarning() << "Failed to restart network interface eth0:" << process.readAllStandardError();
}
emit sigInterfaceDefined();
return true;
}
DapNetworkMonitorLinux::DapNetworkMonitorLinux(QObject *parent):
DapNetworkMonitorAbstract(parent)
{
m_isTunGatewayDefined.store(true);
m_isInterfaceDefined.store(true);
m_isHostReachable.store(true);
m_isOtherGatewayDefined.store(true);
}
bool DapNetworkMonitorLinux::isTunDriverInstalled() const
{
return true;
}
bool DapNetworkMonitorLinux::monitoringStart()
{
qDebug() << "Start network monitoring";
if(m_isMonitoringRunning) {
qWarning() << "Network monitoring already running";
return true;
}
if(dap_network_monitor_init(cbMonitorNotification) == 0) {
m_isMonitoringRunning = true;
}
return m_isMonitoringRunning;
}
bool DapNetworkMonitorLinux::monitoringStop()
{
qDebug() << "Stop network monitoring";
dap_network_monitor_deinit();
m_isMonitoringRunning = false;
return true;
}