qBittorrent
Utils::Net Namespace Reference

Typedefs

using Subnet = QPair< QHostAddress, int >
 

Functions

bool isValidIP (const QString &ip)
 
Subnet parseSubnet (const QString &subnetStr, bool *ok)
 
bool canParseSubnet (const QString &subnetStr)
 
bool isLoopbackAddress (const QHostAddress &addr)
 
bool isIPInRange (const QHostAddress &addr, const QVector< Subnet > &subnets)
 
QString subnetToString (const Subnet &subnet)
 
QHostAddress canonicalIPv6Addr (const QHostAddress &addr)
 
QList< QSslCertificate > loadSSLCertificate (const QByteArray &data)
 
bool isSSLCertificatesValid (const QByteArray &data)
 
QSslKey loadSSLKey (const QByteArray &data)
 
bool isSSLKeyValid (const QByteArray &data)
 

Variables

const int MAX_SSL_FILE_SIZE = 1024 * 1024
 

Typedef Documentation

◆ Subnet

using Utils::Net::Subnet = typedef QPair<QHostAddress, int>

Definition at line 40 of file net.h.

Function Documentation

◆ canonicalIPv6Addr()

QHostAddress Utils::Net::canonicalIPv6Addr ( const QHostAddress &  addr)

Definition at line 100 of file net.cpp.

101  {
102  // Link-local IPv6 textual address always contains a scope id (or zone index)
103  // The scope id is appended to the IPv6 address using the '%' character
104  // The scope id can be either a interface name or an interface number
105  // Examples:
106  // fe80::1%ethernet_17
107  // fe80::1%13
108  // The interface number is the mandatory supported way
109  // Unfortunately for us QHostAddress::toString() outputs (at least on Windows)
110  // the interface name, and libtorrent/boost.asio only support an interface number
111  // as scope id. Furthermore, QHostAddress doesn't have any convenient method to
112  // affect this, so we jump through hoops here.
113  if (addr.protocol() != QAbstractSocket::IPv6Protocol)
114  return QHostAddress{addr.toIPv6Address()};
115 
116  // QHostAddress::setScopeId(addr.scopeId()); // Even though the docs say that setScopeId
117  // will convert a name to a number, this doesn't happen. Probably a Qt bug.
118  const QString scopeIdTxt = addr.scopeId();
119  if (scopeIdTxt.isEmpty())
120  return addr;
121 
122  const int id = QNetworkInterface::interfaceIndexFromName(scopeIdTxt);
123  if (id == 0) // This failure might mean that the scope id was already a number
124  return addr;
125 
126  QHostAddress canonical(addr.toIPv6Address());
127  canonical.setScopeId(QString::number(id));
128  return canonical;
129  }

Referenced by BitTorrent::Session::configureNetworkInterfaces(), AppController::networkInterfaceAddressListAction(), and AdvancedSettings::updateInterfaceAddressCombo().

Here is the caller graph for this function:

◆ canParseSubnet()

bool Utils::Net::canParseSubnet ( const QString &  subnetStr)

Definition at line 57 of file net.cpp.

58  {
59  bool ok = false;
60  parseSubnet(subnetStr, &ok);
61  return ok;
62  }
Subnet parseSubnet(const QString &subnetStr, bool *ok)
Definition: net.cpp:48

References parseSubnet().

Referenced by IPSubnetWhitelistOptionsDialog::on_txtIPSubnet_textChanged().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isIPInRange()

bool Utils::Net::isIPInRange ( const QHostAddress &  addr,
const QVector< Subnet > &  subnets 
)

Definition at line 71 of file net.cpp.

72  {
73  QHostAddress protocolEquivalentAddress;
74  bool addrConversionOk = false;
75 
76  if (addr.protocol() == QAbstractSocket::IPv4Protocol)
77  {
78  // always succeeds
79  protocolEquivalentAddress = QHostAddress(addr.toIPv6Address());
80  addrConversionOk = true;
81  }
82  else
83  {
84  // only succeeds when addr is an ipv4-mapped ipv6 address
85  protocolEquivalentAddress = QHostAddress(addr.toIPv4Address(&addrConversionOk));
86  }
87 
88  for (const Subnet &subnet : subnets)
89  if (addr.isInSubnet(subnet) || (addrConversionOk && protocolEquivalentAddress.isInSubnet(subnet)))
90  return true;
91 
92  return false;
93  }
QPair< QHostAddress, int > Subnet
Definition: net.h:40

Referenced by WebApplication::isAuthNeeded().

Here is the caller graph for this function:

◆ isLoopbackAddress()

bool Utils::Net::isLoopbackAddress ( const QHostAddress &  addr)

Definition at line 64 of file net.cpp.

65  {
66  return (addr == QHostAddress::LocalHost)
67  || (addr == QHostAddress::LocalHostIPv6)
68  || (addr == QHostAddress(QLatin1String("::ffff:127.0.0.1")));
69  }

Referenced by WebApplication::isAuthNeeded().

Here is the caller graph for this function:

◆ isSSLCertificatesValid()

bool Utils::Net::isSSLCertificatesValid ( const QByteArray &  data)

Definition at line 139 of file net.cpp.

140  {
141  return !loadSSLCertificate(data).isEmpty();
142  }
QList< QSslCertificate > loadSSLCertificate(const QByteArray &data)
Definition: net.cpp:131

References loadSSLCertificate().

Referenced by OptionsDialog::webUIHttpsCertChanged().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isSSLKeyValid()

bool Utils::Net::isSSLKeyValid ( const QByteArray &  data)

Definition at line 153 of file net.cpp.

154  {
155  return !loadSSLKey(data).isNull();
156  }
QSslKey loadSSLKey(const QByteArray &data)
Definition: net.cpp:144

References loadSSLKey().

Referenced by OptionsDialog::webUIHttpsKeyChanged().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isValidIP()

bool Utils::Net::isValidIP ( const QString &  ip)

Definition at line 43 of file net.cpp.

44  {
45  return !QHostAddress(ip).isNull();
46  }

Referenced by BanListOptionsDialog::on_buttonBanIP_clicked(), BanListOptionsDialog::on_txtIP_textChanged(), and BitTorrent::Session::setBannedIPs().

Here is the caller graph for this function:

◆ loadSSLCertificate()

QList< QSslCertificate > Utils::Net::loadSSLCertificate ( const QByteArray &  data)

Definition at line 131 of file net.cpp.

132  {
133  const QList<QSslCertificate> certs {QSslCertificate::fromData(data)};
134  if (std::any_of(certs.cbegin(), certs.cend(), [](const QSslCertificate &c) { return c.isNull(); }))
135  return {};
136  return certs;
137  }

Referenced by isSSLCertificatesValid(), and Http::Server::setupHttps().

Here is the caller graph for this function:

◆ loadSSLKey()

QSslKey Utils::Net::loadSSLKey ( const QByteArray &  data)

Definition at line 144 of file net.cpp.

145  {
146  // try different formats
147  QSslKey key {data, QSsl::Rsa};
148  if (!key.isNull())
149  return key;
150  return {data, QSsl::Ec};
151  }

Referenced by isSSLKeyValid(), and Http::Server::setupHttps().

Here is the caller graph for this function:

◆ parseSubnet()

Subnet Utils::Net::parseSubnet ( const QString &  subnetStr,
bool *  ok 
)

Definition at line 48 of file net.cpp.

49  {
50  const Subnet invalid = qMakePair(QHostAddress(), -1);
51  const Subnet subnet = QHostAddress::parseSubnet(subnetStr);
52  if (ok)
53  *ok = (subnet != invalid);
54  return subnet;
55  }

Referenced by canParseSubnet(), Preferences::getWebUiAuthSubnetWhitelist(), IPSubnetWhitelistOptionsDialog::on_buttonWhitelistIPSubnet_clicked(), and Preferences::setWebUiAuthSubnetWhitelist().

Here is the caller graph for this function:

◆ subnetToString()

QString Utils::Net::subnetToString ( const Subnet subnet)

Definition at line 95 of file net.cpp.

96  {
97  return subnet.first.toString() + '/' + QString::number(subnet.second);
98  }

Referenced by IPSubnetWhitelistOptionsDialog::IPSubnetWhitelistOptionsDialog(), IPSubnetWhitelistOptionsDialog::on_buttonWhitelistIPSubnet_clicked(), and AppController::preferencesAction().

Here is the caller graph for this function:

Variable Documentation

◆ MAX_SSL_FILE_SIZE

const int Utils::Net::MAX_SSL_FILE_SIZE = 1024 * 1024