File: LinuxNetworkFunctions.cpp

package info (click to toggle)
veyon 4.9.7%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,028 kB
  • sloc: cpp: 52,043; ansic: 7,307; python: 228; makefile: 222; sh: 48
file content (232 lines) | stat: -rw-r--r-- 6,452 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * LinuxNetworkFunctions.cpp - implementation of LinuxNetworkFunctions class
 *
 * Copyright (c) 2017-2025 Tobias Junghans <tobydox@veyon.io>
 *
 * This file is part of Veyon - https://veyon.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include <netinet/in.h>
#include <netinet/tcp.h>

#include <QFile>
#include <QProcess>
#include <QRegularExpression>

#include "LinuxNetworkFunctions.h"
#include "ProcessHelper.h"

LinuxNetworkFunctions::PingResult LinuxNetworkFunctions::ping(const QString& hostAddress)
{
	QProcess pingProcess;
	pingProcess.start( QStringLiteral("ping"), { QStringLiteral("-c"), QStringLiteral("1"), QStringLiteral("-w"), QString::number( PingTimeout / 1000 ), hostAddress } );
	if (pingProcess.waitForFinished(PingProcessTimeout))
	{
		switch (pingProcess.exitCode())
		{
		case 0: return PingResult::ReplyReceived;
		case 1: return PingResult::TimedOut;
		case 2: return PingResult::NameResolutionFailed;
		default:
			break;
		}
	}

	return PingResult::Unknown;
}



bool LinuxNetworkFunctions::configureFirewallException( const QString& applicationPath, const QString& description, bool enabled )
{
	Q_UNUSED(applicationPath)
	Q_UNUSED(description)
	Q_UNUSED(enabled)

	return true;
}



bool LinuxNetworkFunctions::configureSocketKeepalive( Socket socket, bool enabled, int idleTime, int interval, int probes )
{
	int optval;
	socklen_t optlen = sizeof(optval);
	auto fd = static_cast<int>( socket );

	// Try to set the option active
	optval = enabled ? 1 : 0;
	if( setsockopt( fd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen ) < 0 )
	{
		vWarning() << "could not set SO_KEEPALIVE";
		return false;
	}

	optval = std::max<int>( 1, idleTime / 1000 );
	if( setsockopt( fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, optlen ) < 0 )
	{
		vWarning() << "could not set TCP_KEEPIDLE";
		return false;
	}

	optval = std::max<int>( 1, interval / 1000 );
	if( setsockopt( fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, optlen ) < 0 )
	{
		vWarning() << "could not set TCP_KEEPINTVL";
		return false;
	}

	optval = probes;
	if( setsockopt( fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, optlen ) < 0 )
	{
		vWarning() << "could not set TCP_KEEPCNT";
		return false;
	}

	return true;
}



QNetworkInterface LinuxNetworkFunctions::defaultRouteNetworkInterface()
{
	const auto routes = ProcessHelper(QStringLiteral("ip"), {QStringLiteral("route")}).runAndReadAll().split('\n') +
						ProcessHelper(QStringLiteral("ip"), {QStringLiteral("-6"), QStringLiteral("route")} ).runAndReadAll().split('\n');

	QNetworkInterface defaultNetworkInterface;

	for (const auto& route : routes)
	{
		const auto routeArgs = route.split(' ');
		const auto routeDestination = routeArgs.value(0);

		if (routeDestination == QByteArrayLiteral("default") ||
			routeDestination == QByteArrayLiteral("0.0.0.0") ||
			routeDestination == QByteArrayLiteral("::/0"))
		{
			for (int i = 1; i < routeArgs.size()-1; ++i )
			{
				if (routeArgs[i] == QByteArrayLiteral("dev"))
				{
					return QNetworkInterface::interfaceFromName(QString::fromLatin1(routeArgs[i+1]));
				}
			}
		}
	}

	return {};
}



static int getInterfaceSpeedFromNetworkManager(const QString& interfaceName)
{
	const auto speedInfo = ProcessHelper(QStringLiteral("nmcli"), {QStringLiteral("-f"),
																   QStringLiteral("CAPABILITIES.SPEED"),
																   QStringLiteral("dev"),
																   QStringLiteral("show"),
																   interfaceName})
						   .runAndReadAll();
	static const QRegularExpression capSpeedRX(QStringLiteral("CAPABILITIES.SPEED:[^\\d]*([\\d]+) Mb/s"));
	const auto capSpeedMatch = capSpeedRX.match(QString::fromUtf8(speedInfo));
	if (capSpeedMatch.hasMatch())
	{
		return capSpeedMatch.captured(1).toInt();
	}
	return 0;
}



static int getInterfaceSpeedFromSystemd(const QString& interfaceName)
{
	const auto interfaceStatus = ProcessHelper(QStringLiteral("networkctl"), {QStringLiteral("status"), interfaceName})
						   .runAndReadAll();
	static const QRegularExpression speedRX(QStringLiteral("Speed: ([\\d.]+)([MG])bps"));
	const auto speedMatch = speedRX.match(QString::fromUtf8(interfaceStatus));
	if (speedMatch.hasMatch())
	{
		if (speedMatch.captured(2) == QStringLiteral("G"))
		{
			return speedMatch.captured(1).toFloat() * 1000;
		}
		return speedMatch.captured(1).toFloat();
	}
	return 0;
}



static int getWirelessInterfaceSpeed(const QString& interfaceName)
{
	const auto wifiLinkInfo = ProcessHelper(QStringLiteral("iw"),
											{QStringLiteral("dev"), interfaceName, QStringLiteral("link")})
							  .runAndReadAll();
	static const QRegularExpression txBitrateRX(QStringLiteral("tx bitrate: ([\\d.]+) MBit/s"));
	const auto txBitrateMatch = txBitrateRX.match(QString::fromUtf8(wifiLinkInfo));
	if (txBitrateMatch.hasMatch())
	{
		return txBitrateMatch.captured(1).toFloat();
	}
	return 0;
}



static int getInterfaceSpeedFromSysFS(const QString& interfaceName)
{
	QFile speedFile(QStringLiteral("/sys/class/net/%1/speed").arg(interfaceName));
	if (speedFile.open(QFile::ReadOnly))
	{
		return speedFile.readAll().trimmed().toInt();
	}
	return 0;
}



int LinuxNetworkFunctions::networkInterfaceSpeedInMBitPerSecond(const QNetworkInterface& networkInterface)
{
	if (networkInterface.isValid())
	{
		const auto networkInterfaceName = networkInterface.name();

		if (const auto speed = getInterfaceSpeedFromNetworkManager(networkInterfaceName); speed > 0)
		{
			return speed;
		}

		if (const auto speed = getInterfaceSpeedFromSystemd(networkInterfaceName); speed > 0)
		{
			return speed;
		}

		if (const auto speed = getWirelessInterfaceSpeed(networkInterfaceName); speed > 0)
		{
			return speed;
		}

		if (const auto speed = getInterfaceSpeedFromSysFS(networkInterfaceName); speed > 0)
		{
			return speed;
		}
	}

	return 0;
}