Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / utils / net.cpp
blob40247e38c6ecec25a377a8a5d3c376a0cfa5ba67
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Alexandr Milovantsev <dzmat@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "net.h"
31 #include <QList>
32 #include <QNetworkInterface>
33 #include <QSslCertificate>
34 #include <QSslKey>
35 #include <QString>
37 #include "base/global.h"
39 namespace Utils
41 namespace Net
43 bool isValidIP(const QString &ip)
45 return !QHostAddress(ip).isNull();
48 std::optional<Subnet> parseSubnet(const QString &subnetStr)
50 const Subnet subnet = QHostAddress::parseSubnet(subnetStr);
51 const Subnet invalid = {QHostAddress(), -1};
52 if (subnet == invalid)
53 return std::nullopt;
54 return subnet;
57 bool isIPInSubnets(const QHostAddress &addr, const QList<Subnet> &subnets)
59 QHostAddress protocolEquivalentAddress;
60 bool addrConversionOk = false;
62 if (addr.protocol() == QAbstractSocket::IPv4Protocol)
64 // always succeeds
65 protocolEquivalentAddress = QHostAddress(addr.toIPv6Address());
66 addrConversionOk = true;
68 else
70 // only succeeds when addr is an ipv4-mapped ipv6 address
71 protocolEquivalentAddress = QHostAddress(addr.toIPv4Address(&addrConversionOk));
74 return std::any_of(subnets.begin(), subnets.end(), [&](const Subnet &subnet)
76 return addr.isInSubnet(subnet)
77 || (addrConversionOk && protocolEquivalentAddress.isInSubnet(subnet));
78 });
81 QString subnetToString(const Subnet &subnet)
83 return subnet.first.toString() + u'/' + QString::number(subnet.second);
86 QHostAddress canonicalIPv6Addr(const QHostAddress &addr)
88 // Link-local IPv6 textual address always contains a scope id (or zone index)
89 // The scope id is appended to the IPv6 address using the '%' character
90 // The scope id can be either a interface name or an interface number
91 // Examples:
92 // fe80::1%ethernet_17
93 // fe80::1%13
94 // The interface number is the mandatory supported way
95 // Unfortunately for us QHostAddress::toString() outputs (at least on Windows)
96 // the interface name, and libtorrent/boost.asio only support an interface number
97 // as scope id. Furthermore, QHostAddress doesn't have any convenient method to
98 // affect this, so we jump through hoops here.
99 if (addr.protocol() != QAbstractSocket::IPv6Protocol)
100 return QHostAddress{addr.toIPv6Address()};
102 // QHostAddress::setScopeId(addr.scopeId()); // Even though the docs say that setScopeId
103 // will convert a name to a number, this doesn't happen. Probably a Qt bug.
104 const QString scopeIdTxt = addr.scopeId();
105 if (scopeIdTxt.isEmpty())
106 return addr;
108 const int id = QNetworkInterface::interfaceIndexFromName(scopeIdTxt);
109 if (id == 0) // This failure might mean that the scope id was already a number
110 return addr;
112 QHostAddress canonical(addr.toIPv6Address());
113 canonical.setScopeId(QString::number(id));
114 return canonical;
117 QList<QSslCertificate> loadSSLCertificate(const QByteArray &data)
119 const QList<QSslCertificate> certs {QSslCertificate::fromData(data)};
120 const bool hasInvalidCerts = std::any_of(certs.cbegin(), certs.cend(), [](const QSslCertificate &cert)
122 return cert.isNull();
124 return hasInvalidCerts ? QList<QSslCertificate>() : certs;
127 bool isSSLCertificatesValid(const QByteArray &data)
129 return !loadSSLCertificate(data).isEmpty();