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.
32 #include <QNetworkInterface>
34 #include <QSslCertificate>
39 #include "base/global.h"
45 bool isValidIP(const QString
&ip
)
47 return !QHostAddress(ip
).isNull();
50 std::optional
<Subnet
> parseSubnet(const QString
&subnetStr
)
52 const Subnet subnet
= QHostAddress::parseSubnet(subnetStr
);
53 const Subnet invalid
= {QHostAddress(), -1};
54 if (subnet
== invalid
)
59 bool isLoopbackAddress(const QHostAddress
&addr
)
61 return (addr
== QHostAddress::LocalHost
)
62 || (addr
== QHostAddress::LocalHostIPv6
)
63 || (addr
== QHostAddress(u
"::ffff:127.0.0.1"_s
));
66 bool isIPInSubnets(const QHostAddress
&addr
, const QVector
<Subnet
> &subnets
)
68 QHostAddress protocolEquivalentAddress
;
69 bool addrConversionOk
= false;
71 if (addr
.protocol() == QAbstractSocket::IPv4Protocol
)
74 protocolEquivalentAddress
= QHostAddress(addr
.toIPv6Address());
75 addrConversionOk
= true;
79 // only succeeds when addr is an ipv4-mapped ipv6 address
80 protocolEquivalentAddress
= QHostAddress(addr
.toIPv4Address(&addrConversionOk
));
83 return std::any_of(subnets
.begin(), subnets
.end(), [&](const Subnet
&subnet
)
85 return addr
.isInSubnet(subnet
)
86 || (addrConversionOk
&& protocolEquivalentAddress
.isInSubnet(subnet
));
90 QString
subnetToString(const Subnet
&subnet
)
92 return subnet
.first
.toString() + u
'/' + QString::number(subnet
.second
);
95 QHostAddress
canonicalIPv6Addr(const QHostAddress
&addr
)
97 // Link-local IPv6 textual address always contains a scope id (or zone index)
98 // The scope id is appended to the IPv6 address using the '%' character
99 // The scope id can be either a interface name or an interface number
101 // fe80::1%ethernet_17
103 // The interface number is the mandatory supported way
104 // Unfortunately for us QHostAddress::toString() outputs (at least on Windows)
105 // the interface name, and libtorrent/boost.asio only support an interface number
106 // as scope id. Furthermore, QHostAddress doesn't have any convenient method to
107 // affect this, so we jump through hoops here.
108 if (addr
.protocol() != QAbstractSocket::IPv6Protocol
)
109 return QHostAddress
{addr
.toIPv6Address()};
111 // QHostAddress::setScopeId(addr.scopeId()); // Even though the docs say that setScopeId
112 // will convert a name to a number, this doesn't happen. Probably a Qt bug.
113 const QString scopeIdTxt
= addr
.scopeId();
114 if (scopeIdTxt
.isEmpty())
117 const int id
= QNetworkInterface::interfaceIndexFromName(scopeIdTxt
);
118 if (id
== 0) // This failure might mean that the scope id was already a number
121 QHostAddress
canonical(addr
.toIPv6Address());
122 canonical
.setScopeId(QString::number(id
));
126 QList
<QSslCertificate
> loadSSLCertificate(const QByteArray
&data
)
128 const QList
<QSslCertificate
> certs
{QSslCertificate::fromData(data
)};
129 const bool hasInvalidCerts
= std::any_of(certs
.cbegin(), certs
.cend(), [](const QSslCertificate
&cert
)
131 return cert
.isNull();
133 return hasInvalidCerts
? QList
<QSslCertificate
>() : certs
;
136 bool isSSLCertificatesValid(const QByteArray
&data
)
138 return !loadSSLCertificate(data
).isEmpty();
141 QSslKey
loadSSLKey(const QByteArray
&data
)
143 // try different formats
144 const QSslKey key
{data
, QSsl::Rsa
};
147 return {data
, QSsl::Ec
};
150 bool isSSLKeyValid(const QByteArray
&data
)
152 return !loadSSLKey(data
).isNull();