From 41a61ced89f9adbea523f295434768c0ccac3e05 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 7 Nov 2010 11:36:37 +0000 Subject: [PATCH] Improved IP address parsing --- src/filterparserthread.h | 37 +++++++++++++++++++++++++++++++---- src/properties/peeraddition.h | 10 +++++++--- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/filterparserthread.h b/src/filterparserthread.h index 3ad6c2594..9ffc3e3b8 100644 --- a/src/filterparserthread.h +++ b/src/filterparserthread.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -61,6 +62,14 @@ private: QString filePath; protected: + QString cleanupIPAddress(QString _ip) { + QHostAddress ip(_ip.trimmed()); + if(ip.isNull()) { + return QString(); + } + return ip.toString(); + } + void run(){ qDebug("Processing filter file"); if(filePath.endsWith(".dat", Qt::CaseInsensitive)) { @@ -125,14 +134,24 @@ public: } boost::system::error_code ec; - const QString strStartIP = IPs.at(0).trimmed(); + const QString strStartIP = cleanupIPAddress(IPs.at(0)); + if(strStartIP.isEmpty()) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP)); + continue; + } libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec); if(ec) { qDebug("Ipfilter.dat: line %d is malformed.", nbLine); qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP)); continue; } - const QString strEndIP = IPs.at(1).trimmed(); + const QString strEndIP = cleanupIPAddress(IPs.at(1)); + if(strEndIP.isEmpty()) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP)); + continue; + } libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec); if(ec) { qDebug("Ipfilter.dat: line %d is malformed.", nbLine); @@ -197,14 +216,24 @@ public: continue; } boost::system::error_code ec; - QString strStartIP = IPs.at(0).trimmed(); + QString strStartIP = cleanupIPAddress(IPs.at(0)); + if(strStartIP.isEmpty()) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("Start IP is invalid: %s", qPrintable(strStartIP)); + continue; + } 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(); + QString strEndIP = cleanupIPAddress(IPs.at(1)); + if(strEndIP.isEmpty()) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("End IP is invalid: %s", qPrintable(strStartIP)); + continue; + } libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec); if(ec) { qDebug("p2p file: line %d is malformed.", nbLine); diff --git a/src/properties/peeraddition.h b/src/properties/peeraddition.h index 33e29c69f..9c01d6f1a 100644 --- a/src/properties/peeraddition.h +++ b/src/properties/peeraddition.h @@ -60,7 +60,12 @@ public: ~PeerAdditionDlg(){} QString getIP() const { - return lineIP->text(); + QHostAddress ip(lineIP->text()); + if(!ip.isNull()) { + // QHostAddress::toString() cleans up the IP for libtorrent + return ip.toString(); + } + return QString(); } unsigned short getPort() const { @@ -87,8 +92,7 @@ public: protected slots: void validateInput() { - QHostAddress ip(getIP()); - if(ip.isNull()) { + if(getIP().isEmpty()) { QMessageBox::warning(this, tr("Invalid IP"), tr("The IP you provided is invalid."), QMessageBox::Ok);