From ce627de39dddb6ccd97759f8cdb3533041736ba9 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 17 May 2008 19:27:08 +0000 Subject: [PATCH] - FEATURE: Added support for PeerGuardian p2b filters (binary) --- Changelog | 3 +- src/options_imp.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++-- src/options_imp.h | 12 +++-- 3 files changed, 133 insertions(+), 9 deletions(-) diff --git a/Changelog b/Changelog index 0490d6fbe..879b2b6ac 100644 --- a/Changelog +++ b/Changelog @@ -9,7 +9,8 @@ - FEATURE: Display if UPnP/NAT-PMP was successful or not - FEATURE: Threadified torrent creation - FEATURE: Improved eMule DAT ip filter parser - - FEATURE: Added support for PeerGuardian p2p filters + - FEATURE: Added support for PeerGuardian p2p filters (text) + - FEATURE: Added support for PeerGuardian p2b filters (binary) - BUGFIX: Do not display seeds number in seeding list (always 0) - COSMETIC: Do not display progress bar in seeding list (always 100%) - COSMETIC: Added a progress bar for torrent creation diff --git a/src/options_imp.cpp b/src/options_imp.cpp index b25c31fe2..cfdedbc8a 100644 --- a/src/options_imp.cpp +++ b/src/options_imp.cpp @@ -39,6 +39,10 @@ #include #endif +// P2B Stuff +#include +// End of P2B stuff + #include "options_imp.h" #include "misc.h" @@ -1218,11 +1222,121 @@ void options_imp::parseP2PFilterFile(QString filePath) { } } +int options_imp::getlineInStream(QDataStream& stream, string& name, char delim) { + char c; + int total_read = 0; + do { + int 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 options_imp::parseP2BFilterFile(QString filePath) { + QFile file(filePath); + if (file.exists()){ + if(!file.open(QIODevice::ReadOnly)){ + QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath)); + 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)) + ) { + QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath)); + return; + } + + if(version==1 || version==2) { + unsigned int start, end; + + string name; + while(getlineInStream(stream, name, '\0')) { + if( + !stream.readRawData((char*)&start, sizeof(start)) || + !stream.readRawData((char*)&end, sizeof(end)) + ) { + QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath)); + 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); + } + } + else if(version==3) { + unsigned int namecount; + if(!stream.readRawData((char*)&namecount, sizeof(namecount))) { + QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath)); + return; + } + namecount=ntohl(namecount); + // Reading names although, we don't really care about them + for(unsigned int i=0; i +#include -/*#include -#include -#include "ui_options.h" -#include "ui_dialog.h" -#include */ +// P2B Stuff +#include +// End of P2B stuff #define HTTP 1 #define SOCKS5 2 @@ -42,6 +41,7 @@ #define SHOW_PROPERTIES 2 using namespace libtorrent; +using namespace std; class QCloseEvent; @@ -115,6 +115,7 @@ class options_imp : public QDialog, private Ui::Dialog { quint16 webUiPort() const; QString webUiUsername() const; QString webUiPassword() const; + int getlineInStream(QDataStream& stream, string& name, char delim); protected slots: void enableUploadLimit(int checkBoxValue); @@ -145,6 +146,7 @@ class options_imp : public QDialog, private Ui::Dialog { void enableWebUi(bool checkBoxValue); void parseDATFilterFile(QString filePath); void parseP2PFilterFile(QString filePath); + void parseP2BFilterFile(QString filePath); public slots: void setLocale(QString locale);