2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-01-28 11:05:23 +01:00
|
|
|
|
|
|
|
|
#include "port.h"
|
|
|
|
|
|
2022-07-27 12:38:07 +02:00
|
|
|
#include "qtcassert.h"
|
2022-05-12 08:42:51 +02:00
|
|
|
#include "stringutils.h"
|
|
|
|
|
|
2023-03-31 14:07:57 +02:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
|
2022-07-27 12:38:07 +02:00
|
|
|
#include <limits>
|
|
|
|
|
|
2023-05-22 14:38:47 +02:00
|
|
|
/*!
|
|
|
|
|
\class Utils::Port
|
|
|
|
|
\inmodule QtCreator
|
2016-01-28 11:05:23 +01:00
|
|
|
|
|
|
|
|
\brief The Port class implements a wrapper around a 16 bit port number
|
|
|
|
|
to be used in conjunction with IP addresses.
|
|
|
|
|
*/
|
2022-05-12 08:42:51 +02:00
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
2022-07-27 12:38:07 +02:00
|
|
|
Port::Port(int port)
|
|
|
|
|
: m_port((port < 0 || port > std::numeric_limits<quint16>::max()) ? -1 : port)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Port::Port(uint port)
|
|
|
|
|
: m_port(port > std::numeric_limits<quint16>::max() ? -1 : port)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint16 Port::number() const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(isValid(), return -1); return quint16(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 09:01:01 +02:00
|
|
|
QList<Port> Port::parseFromCommandOutput(const QByteArray &output)
|
2022-05-12 08:42:51 +02:00
|
|
|
{
|
|
|
|
|
QList<Port> ports;
|
|
|
|
|
const QList<QByteArray> lines = output.split('\n');
|
|
|
|
|
for (const QByteArray &line : lines) {
|
2022-05-24 12:14:24 +02:00
|
|
|
const Port port(parseUsedPortFromNetstatOutput(line));
|
2022-05-12 08:42:51 +02:00
|
|
|
if (port.isValid() && !ports.contains(port))
|
|
|
|
|
ports.append(port);
|
|
|
|
|
}
|
|
|
|
|
return ports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // Utils
|