|
|
@ -29,30 +29,29 @@ |
|
|
|
#include "peeraddress.h" |
|
|
|
#include "peeraddress.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <QString> |
|
|
|
#include <QString> |
|
|
|
#include <QStringList> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(QString peerAddressStr) |
|
|
|
BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(const QString &address) |
|
|
|
{ |
|
|
|
{ |
|
|
|
PeerAddress addr; |
|
|
|
QVector<QStringRef> ipPort; |
|
|
|
QStringList ipPort; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((peerAddressStr[0] == '[') && (peerAddressStr.indexOf("]:") != -1)) // IPv6
|
|
|
|
if (address.startsWith('[') && address.contains("]:")) { // IPv6
|
|
|
|
ipPort = peerAddressStr.remove(QChar('[')).split("]:"); |
|
|
|
ipPort = address.splitRef("]:"); |
|
|
|
else if (peerAddressStr.indexOf(':') != -1) // IPv4
|
|
|
|
ipPort[0] = ipPort[0].mid(1); // chop '['
|
|
|
|
ipPort = peerAddressStr.split(':'); |
|
|
|
} |
|
|
|
else |
|
|
|
else if (address.contains(':')) { // IPv4
|
|
|
|
return addr; |
|
|
|
ipPort = address.splitRef(':'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
return {}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QHostAddress ip(ipPort[0]); |
|
|
|
const QHostAddress ip {ipPort[0].toString()}; |
|
|
|
if (ip.isNull()) |
|
|
|
if (ip.isNull()) |
|
|
|
return addr; |
|
|
|
return {}; |
|
|
|
|
|
|
|
|
|
|
|
bool ok; |
|
|
|
const ushort port {ipPort[1].toUShort()}; |
|
|
|
int port = ipPort[1].toInt(&ok); |
|
|
|
if (port == 0) |
|
|
|
if (!ok || (port < 1) || (port > 65535)) |
|
|
|
return {}; |
|
|
|
return addr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addr.ip = ip; |
|
|
|
return {ip, port}; |
|
|
|
addr.port = port; |
|
|
|
|
|
|
|
return addr; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|