1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-09 21:34:20 +00:00

Improved IPv6 support

This commit is contained in:
Christophe Dumez 2010-11-07 10:40:47 +00:00
parent 8276c301c2
commit da95d5e0df
2 changed files with 311 additions and 360 deletions

View File

@ -34,7 +34,6 @@
#include <QThread> #include <QThread>
#include <QFile> #include <QFile>
#include <QDataStream> #include <QDataStream>
#include <QRegExp>
#include <QStringList> #include <QStringList>
#include <libtorrent/session.hpp> #include <libtorrent/session.hpp>
@ -46,376 +45,336 @@ using namespace std;
// P2B Stuff // P2B Stuff
#include <string.h> #include <string.h>
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
#include <Winsock2.h> #include <Winsock2.h>
#else #else
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
// End of P2B stuff // End of P2B stuff
class FilterParserThread : public QThread { class FilterParserThread : public QThread {
Q_OBJECT Q_OBJECT
private: private:
session *s; session *s;
ip_filter filter; ip_filter filter;
bool abort; bool abort;
QString filePath; QString filePath;
protected: protected:
void run(){ void run(){
qDebug("Processing filter file"); qDebug("Processing filter file");
if(filePath.endsWith(".dat", Qt::CaseInsensitive)) { if(filePath.endsWith(".dat", Qt::CaseInsensitive)) {
// eMule DAT file // eMule DAT file
parseDATFilterFile(filePath); parseDATFilterFile(filePath);
} else {
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) {
// PeerGuardian p2p file
parseP2PFilterFile(filePath);
} else { } else {
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) { if(filePath.endsWith(".p2b", Qt::CaseInsensitive)) {
// PeerGuardian p2p file // PeerGuardian p2b file
parseP2PFilterFile(filePath); parseP2BFilterFile(filePath);
} else { } else {
if(filePath.endsWith(".p2b", Qt::CaseInsensitive)) { // Default: eMule DAT format
// PeerGuardian p2b file parseDATFilterFile(filePath);
parseP2BFilterFile(filePath);
} else {
// Default: eMule DAT format
parseDATFilterFile(filePath);
}
} }
} }
s->set_ip_filter(filter);
qDebug("IP Filter thread: finished parsing, filter applied");
} }
s->set_ip_filter(filter);
public: qDebug("IP Filter thread: finished parsing, filter applied");
FilterParserThread(QObject* parent, session *s) : QThread(parent), s(s), abort(false) { }
} public:
FilterParserThread(QObject* parent, session *s) : QThread(parent), s(s), abort(false) {
~FilterParserThread(){
abort = true; }
wait();
} ~FilterParserThread(){
abort = true;
// Parser for eMule ip filter in DAT format wait();
void parseDATFilterFile(QString filePath) { }
const QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp);
const QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp); // Parser for eMule ip filter in DAT format
QString strStartIP, strEndIP; void parseDATFilterFile(QString filePath) {
bool IPv4 = true; QFile file(filePath);
QFile file(filePath); if (file.exists()){
if (file.exists()){ if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; return;
return;
}
unsigned int nbLine = 0;
while (!file.atEnd() && !abort) {
++nbLine;
QByteArray line = file.readLine();
// Ignoring empty lines
line = line.trimmed();
if(line.isEmpty()) continue;
// Ignoring commented lines
if(line.startsWith('#') || line.startsWith("//")) continue;
// Line is not commented
QList<QByteArray> partsList = line.split(',');
unsigned int nbElem = partsList.size();
// IP Range can be splitted by a dash or a comma...
// Check if there is a dash in first part
QByteArray firstPart = partsList.at(0);
int nbAccess = 0;
if(firstPart.contains('-')) {
// Range is splitted by a dash
QList<QByteArray> IPs = firstPart.split('-');
if(IPs.size() != 2) {
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
strStartIP = IPs.at(0).trimmed();
strEndIP = IPs.at(1).trimmed();
// Check if IPs are correct
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
IPv4 = true;
} else {
if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
IPv4 = false;
} else {
// Could not determine IP format
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
}
// Check if there is an access value (apparently not mandatory)
if(nbElem > 1) {
// There is possibly one
bool ok;
nbAccess = partsList.at(1).trimmed().toInt(&ok);
if(!ok){
nbAccess = 0;
}
}
} else {
// Range is probably splitted by a comma
unsigned int nbElem = partsList.size();
if(nbElem > 1) {
strStartIP = firstPart.trimmed();
strEndIP = partsList.at(1).trimmed();
// Check if IPs are correct
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
IPv4 = true;
} else {
if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
IPv4 = false;
} else {
// Could not determine IP format
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
}
// Check if there is an access value (apparently not mandatory)
if(nbElem > 2) {
// There is possibly one
bool ok;
nbAccess = partsList.at(2).trimmed().toInt(&ok);
if(!ok){
nbAccess = 0;
}
}
}
}
if(nbAccess > 127) {
// Ignoring this rule because access value is too high
continue;
}
// Now Add to the filter
QStringList IP;
try {
if(IPv4) {
//IPv4 addresses
IP = strStartIP.split('.');
if(IP.size() != 4)
throw exception();
address_v4 start((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt());
IP = strEndIP.split('.');
if(IP.size() != 4)
throw exception();
address_v4 last((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt());
// Apply to bittorrent session
filter.add_rule(start, last, ip_filter::blocked);
} else {
// IPv6, ex : 1fff:0000:0a88:85a3:0000:0000:ac1f:8001
IP = strStartIP.split(':');
address_v6 start = address_v6::from_string(strStartIP.remove(':', 0).toLocal8Bit().data());
IP = strEndIP.split(':');
address_v6 last = address_v6::from_string(strEndIP.remove(':', 0).toLocal8Bit().data());
// Apply to bittorrent session
filter.add_rule(start, last, ip_filter::blocked);
}
}catch(exception){
qDebug("Bad line in filter file, avoided crash...");
}
}
file.close();
} }
} unsigned int nbLine = 0;
while (!file.atEnd() && !abort) {
// Parser for PeerGuardian ip filter in p2p format ++nbLine;
void parseP2PFilterFile(QString filePath) { QByteArray line = file.readLine();
const QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp); // Ignoring empty lines
QFile file(filePath); line = line.trimmed();
QStringList IP; if(line.isEmpty()) continue;
if (file.exists()){ // Ignoring commented lines
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ if(line.startsWith('#') || line.startsWith("//")) continue;
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
return; // Line should be splitted by commas
QList<QByteArray> partsList = line.split(',');
const uint nbElem = partsList.size();
// IP Range should be splitted by a dash
QList<QByteArray> IPs = partsList.first().split('-');
if(IPs.size() != 2) {
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
qDebug("Line was %s", line.constData());
continue;
} }
unsigned int nbLine = 0;
while (!file.atEnd() && !abort) { boost::system::error_code ec;
++nbLine; const QString strStartIP = IPs.at(0).trimmed();
QByteArray line = file.readLine(); libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec);
// Ignoring empty lines if(ec) {
line = line.trimmed(); qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
if(line.isEmpty()) continue; qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP));
// Ignoring commented lines continue;
if(line.startsWith('#') || line.startsWith("//")) continue; }
// Line is not commented const QString strEndIP = IPs.at(1).trimmed();
QList<QByteArray> partsList = line.split(':'); libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec);
if(partsList.size() < 2){ if(ec) {
qDebug("p2p file: line %d is malformed.", nbLine); qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue; qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP));
} continue;
// Get IP range }
QList<QByteArray> IPs = partsList.last().split('-'); if(startAddr.is_v4() != endAddr.is_v4()) {
if(IPs.size() != 2) { qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
qDebug("p2p file: line %d is malformed.", nbLine); qDebug("One IP is IPv4 and the other is IPv6!");
continue; continue;
} }
QString strStartIP = IPs.at(0).trimmed();
QString strEndIP = IPs.at(1).trimmed(); // Check if there is an access value (apparently not mandatory)
// Check IPs format (IPv4 only) int nbAccess = 0;
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) { if(nbElem > 1) {
// IPv4 // There is possibly one
IP = strStartIP.split('.'); nbAccess = partsList.at(1).trimmed().toInt();
address_v4 start((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt()); }
IP = strEndIP.split('.');
address_v4 last((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt()); if(nbAccess > 127) {
// Apply to bittorrent session // Ignoring this rule because access value is too high
filter.add_rule(start, last, ip_filter::blocked); continue;
} else { }
qDebug("p2p file: line %d is malformed.", nbLine); // Now Add to the filter
continue; try {
} filter.add_rule(startAddr, endAddr, ip_filter::blocked);
}catch(exception){
qDebug("Bad line in filter file, avoided crash...");
} }
file.close();
} }
file.close();
} }
}
int getlineInStream(QDataStream& stream, string& name, char delim) {
char c; // Parser for PeerGuardian ip filter in p2p format
int total_read = 0; void parseP2PFilterFile(QString filePath) {
int read; QFile file(filePath);
do { QStringList IP;
read = stream.readRawData(&c, 1); if (file.exists()){
total_read += read; if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
if(read > 0) { std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
if(c != delim) { return;
name += c; }
} else { unsigned int nbLine = 0;
// Delim found while (!file.atEnd() && !abort) {
return total_read; ++nbLine;
QByteArray line = file.readLine().trimmed();
if(line.isEmpty()) continue;
// Ignoring commented lines
if(line.startsWith('#') || line.startsWith("//")) continue;
// Line is splitted by :
QList<QByteArray> partsList = line.split(':');
if(partsList.size() < 2){
qDebug("p2p file: line %d is malformed.", nbLine);
continue;
}
// Get IP range
QList<QByteArray> IPs = partsList.last().split('-');
if(IPs.size() != 2) {
qDebug("p2p file: line %d is malformed.", nbLine);
qDebug("line was: %s", line.constData());
continue;
}
boost::system::error_code ec;
QString strStartIP = IPs.at(0).trimmed();
libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec);
if(ec) {
qDebug("p2p file: line %d is malformed.", nbLine);
qDebug("Start IP is invalid: %s", qPrintable(strStartIP));
continue;
}
QString strEndIP = IPs.at(1).trimmed();
libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec);
if(ec) {
qDebug("p2p file: line %d is malformed.", nbLine);
qDebug("End IP is invalid: %s", qPrintable(strStartIP));
continue;
}
if(startAddr.is_v4() != endAddr.is_v4()) {
qDebug("p2p file: line %d is malformed.", nbLine);
qDebug("Line was: %s", line.constData());
continue;
}
try {
filter.add_rule(startAddr, endAddr, ip_filter::blocked);
} catch(std::exception&) {
qDebug("p2p file: line %d is malformed.", nbLine);
qDebug("Line was: %s", line.constData());
continue;
}
}
file.close();
}
}
int getlineInStream(QDataStream& stream, string& name, char delim) {
char c;
int total_read = 0;
int read;
do {
read = stream.readRawData(&c, 1);
total_read += read;
if(read > 0) {
if(c != delim) {
name += c;
} else {
// Delim found
return total_read;
}
}
} while(read > 0);
return total_read;
}
// Parser for PeerGuardian ip filter in p2p format
void parseP2BFilterFile(QString filePath) {
QFile file(filePath);
if (file.exists()){
if(!file.open(QIODevice::ReadOnly)){
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
return;
}
QDataStream stream(&file);
// Read header
char buf[7];
unsigned char version;
if(
!stream.readRawData(buf, sizeof(buf)) ||
memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) ||
!stream.readRawData((char*)&version, sizeof(version))
) {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return;
}
if(version==1 || version==2) {
qDebug ("p2b version 1 or 2");
unsigned int start, end;
string name;
while(getlineInStream(stream, name, '\0') && !abort) {
if(
!stream.readRawData((char*)&start, sizeof(start)) ||
!stream.readRawData((char*)&end, sizeof(end))
) {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return;
} }
// Network byte order to Host byte order
// asio address_v4 contructor expects it
// that way
address_v4 first(ntohl(start));
address_v4 last(ntohl(end));
// Apply to bittorrent session
filter.add_rule(first, last, ip_filter::blocked);
} }
} while(read > 0); }
return total_read; else if(version==3) {
} qDebug ("p2b version 3");
unsigned int namecount;
// Parser for PeerGuardian ip filter in p2p format if(!stream.readRawData((char*)&namecount, sizeof(namecount))) {
void parseP2BFilterFile(QString filePath) {
QFile file(filePath);
if (file.exists()){
if(!file.open(QIODevice::ReadOnly)){
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
return;
}
QDataStream stream(&file);
// Read header
char buf[7];
unsigned char version;
if(
!stream.readRawData(buf, sizeof(buf)) ||
memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) ||
!stream.readRawData((char*)&version, sizeof(version))
) {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return; return;
} }
namecount=ntohl(namecount);
if(version==1 || version==2) { // Reading names although, we don't really care about them
qDebug ("p2b version 1 or 2"); for(unsigned int i=0; i<namecount; i++) {
unsigned int start, end;
string name; string name;
while(getlineInStream(stream, name, '\0') && !abort) { if(!getlineInStream(stream, name, '\0')) {
if( std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
!stream.readRawData((char*)&start, sizeof(start)) || return;
!stream.readRawData((char*)&end, sizeof(end))
) {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return;
}
// Network byte order to Host byte order
// asio address_v4 contructor expects it
// that way
address_v4 first(ntohl(start));
address_v4 last(ntohl(end));
// Apply to bittorrent session
filter.add_rule(first, last, ip_filter::blocked);
} }
if(abort) return;
} }
else if(version==3) { // Reading the ranges
qDebug ("p2b version 3"); unsigned int rangecount;
unsigned int namecount; if(!stream.readRawData((char*)&rangecount, sizeof(rangecount))) {
if(!stream.readRawData((char*)&namecount, sizeof(namecount))) { std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; return;
return; }
} rangecount=ntohl(rangecount);
namecount=ntohl(namecount);
// Reading names although, we don't really care about them unsigned int name, start, end;
for(unsigned int i=0; i<namecount; i++) {
string name; for(unsigned int i=0; i<rangecount; i++) {
if(!getlineInStream(stream, name, '\0')) { if(
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return;
}
if(abort) return;
}
// Reading the ranges
unsigned int rangecount;
if(!stream.readRawData((char*)&rangecount, sizeof(rangecount))) {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return;
}
rangecount=ntohl(rangecount);
unsigned int name, start, end;
for(unsigned int i=0; i<rangecount; i++) {
if(
!stream.readRawData((char*)&name, sizeof(name)) || !stream.readRawData((char*)&name, sizeof(name)) ||
!stream.readRawData((char*)&start, sizeof(start)) || !stream.readRawData((char*)&start, sizeof(start)) ||
!stream.readRawData((char*)&end, sizeof(end)) !stream.readRawData((char*)&end, sizeof(end))
) { ) {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return; return;
}
// Network byte order to Host byte order
// asio address_v4 contructor expects it
// that way
address_v4 first(ntohl(start));
address_v4 last(ntohl(end));
// Apply to bittorrent session
filter.add_rule(first, last, ip_filter::blocked);
if(abort) return;
} }
} else { // Network byte order to Host byte order
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; // asio address_v4 contructor expects it
return; // that way
address_v4 first(ntohl(start));
address_v4 last(ntohl(end));
// Apply to bittorrent session
filter.add_rule(first, last, ip_filter::blocked);
if(abort) return;
} }
file.close(); } else {
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
return;
} }
file.close();
} }
}
// Process ip filter file
// Supported formats:
// * eMule IP list (DAT): http://wiki.phoenixlabs.org/wiki/DAT_Format
// * PeerGuardian Text (P2P): http://wiki.phoenixlabs.org/wiki/P2P_Format
// * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format
void processFilterFile(QString _filePath){
// First, import current filter
filter = s->get_ip_filter();
if(isRunning()) {
// Already parsing a filter, abort first
abort = true;
wait();
}
abort = false;
filePath = _filePath;
// Run it
start();
}
static void processFilterList(session *s, QStringList IPs) { // Process ip filter file
// First, import current filter // Supported formats:
ip_filter filter = s->get_ip_filter(); // * eMule IP list (DAT): http://wiki.phoenixlabs.org/wiki/DAT_Format
foreach(const QString &ip, IPs) { // * PeerGuardian Text (P2P): http://wiki.phoenixlabs.org/wiki/P2P_Format
qDebug("Manual ban of peer %s", ip.toLocal8Bit().constData()); // * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format
boost::system::error_code ec; void processFilterFile(QString _filePath){
address_v4 addr = address_v4::from_string(ip.toLocal8Bit().constData(), ec); // First, import current filter
Q_ASSERT(!ec); filter = s->get_ip_filter();
if(!ec) if(isRunning()) {
filter.add_rule(addr, addr, ip_filter::blocked); // Already parsing a filter, abort first
} abort = true;
s->set_ip_filter(filter); wait();
} }
abort = false;
filePath = _filePath;
// Run it
start();
}
static void processFilterList(session *s, QStringList IPs) {
// First, import current filter
ip_filter filter = s->get_ip_filter();
foreach(const QString &ip, IPs) {
qDebug("Manual ban of peer %s", ip.toLocal8Bit().constData());
boost::system::error_code ec;
address_v4 addr = address_v4::from_string(ip.toLocal8Bit().constData(), ec);
Q_ASSERT(!ec);
if(!ec)
filter.add_rule(addr, addr, ip_filter::blocked);
}
s->set_ip_filter(filter);
}
}; };

View File

@ -34,6 +34,7 @@
#include <QDialog> #include <QDialog>
#include <QRegExp> #include <QRegExp>
#include <QMessageBox> #include <QMessageBox>
#include <QHostAddress>
#include "ui_peer.h" #include "ui_peer.h"
#include <libtorrent/session.hpp> #include <libtorrent/session.hpp>
@ -70,16 +71,15 @@ public:
libtorrent::asio::ip::tcp::endpoint ep; libtorrent::asio::ip::tcp::endpoint ep;
PeerAdditionDlg dlg; PeerAdditionDlg dlg;
if(dlg.exec() == QDialog::Accepted) { if(dlg.exec() == QDialog::Accepted) {
const QRegExp is_ipv6(QString::fromUtf8("[0-9a-f]{4}(:[0-9a-f]{4}){7}"), Qt::CaseInsensitive, QRegExp::RegExp); QString ip = dlg.getIP();
const QRegExp is_ipv4(QString::fromUtf8("(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}"), Qt::CaseInsensitive, QRegExp::RegExp); boost::system::error_code ec;
QString IP = dlg.getIP(); libtorrent::address addr = libtorrent::address::from_string(qPrintable(ip), ec);
if(is_ipv4.exactMatch(IP)) { if(ec) {
// IPv4 qDebug("Unable to parse the provided IP: %s", qPrintable(ip));
ep = libtorrent::asio::ip::tcp::endpoint(libtorrent::asio::ip::address_v4::from_string(IP.toLocal8Bit().data()), dlg.getPort()); return ep;
} else {
// IPv6
ep = libtorrent::asio::ip::tcp::endpoint(libtorrent::asio::ip::address_v6::from_string(IP.toLocal8Bit().data()), dlg.getPort());
} }
qDebug("Provided IP is correct");
ep = libtorrent::asio::ip::tcp::endpoint(addr, dlg.getPort());
} }
return ep; return ep;
} }
@ -87,21 +87,13 @@ public:
protected slots: protected slots:
void validateInput() { void validateInput() {
const QRegExp is_ipv6(QString::fromUtf8("[0-9a-f]{4}(:[0-9a-f]{4}){7}"), Qt::CaseInsensitive, QRegExp::RegExp); QHostAddress ip(getIP());
const QRegExp is_ipv4(QString::fromUtf8("(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}"), Qt::CaseInsensitive, QRegExp::RegExp); if(ip.isNull()) {
QString IP = getIP(); QMessageBox::warning(this, tr("Invalid IP"),
if(is_ipv4.exactMatch(IP)) { tr("The IP you provided is invalid."),
qDebug("Detected IPv4 address: %s", IP.toLocal8Bit().data()); QMessageBox::Ok);
accept();
} else { } else {
if(is_ipv6.exactMatch(IP)) { accept();
qDebug("Detected IPv6 address: %s", IP.toLocal8Bit().data());
accept();
} else {
QMessageBox::warning(this, tr("Invalid IP"),
tr("The IP you provided is invalid."),
QMessageBox::Ok);
}
} }
} }
}; };