From 0889370e3050561372c3816dfb2902771ca83765 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Fri, 1 Feb 2013 23:15:01 -0600 Subject: Rename many classes and header files to avoid conflicts with KDE4 --- libktorrent/interfaces/filetreediritem.h | 2 +- libktorrent/interfaces/filetreeitem.h | 2 +- libktorrent/pluginmanagerprefpage.cpp | 2 +- libktorrent/torrent/Makefile.am | 4 +- libktorrent/torrent/chuntdeselector.cpp | 4 +- libktorrent/torrent/downloader.cpp | 2 +- libktorrent/torrent/ipblocklist.cpp | 400 +++++++++++++++++++++++++++++++ libktorrent/torrent/ipblocklist.h | 175 ++++++++++++++ libktorrent/torrent/ipbloctdelist.cpp | 400 ------------------------------- libktorrent/torrent/ipbloctdelist.h | 175 -------------- 10 files changed, 583 insertions(+), 583 deletions(-) create mode 100644 libktorrent/torrent/ipblocklist.cpp create mode 100644 libktorrent/torrent/ipblocklist.h delete mode 100644 libktorrent/torrent/ipbloctdelist.cpp delete mode 100644 libktorrent/torrent/ipbloctdelist.h (limited to 'libktorrent') diff --git a/libktorrent/interfaces/filetreediritem.h b/libktorrent/interfaces/filetreediritem.h index 640916e..6b35031 100644 --- a/libktorrent/interfaces/filetreediritem.h +++ b/libktorrent/interfaces/filetreediritem.h @@ -20,7 +20,7 @@ #ifndef KTFILETREEDIRITEM_H #define KTFILETREEDIRITEM_H -#include +#include #include #include diff --git a/libktorrent/interfaces/filetreeitem.h b/libktorrent/interfaces/filetreeitem.h index 9d32253..5cc17f9 100644 --- a/libktorrent/interfaces/filetreeitem.h +++ b/libktorrent/interfaces/filetreeitem.h @@ -20,7 +20,7 @@ #ifndef KTFILETREEITEM_H #define KTFILETREEITEM_H -#include +#include #include using namespace bt; diff --git a/libktorrent/pluginmanagerprefpage.cpp b/libktorrent/pluginmanagerprefpage.cpp index 145d4de..54f5aaa 100644 --- a/libktorrent/pluginmanagerprefpage.cpp +++ b/libktorrent/pluginmanagerprefpage.cpp @@ -19,7 +19,7 @@ ***************************************************************************/ #include #include -#include +#include #include #include #include diff --git a/libktorrent/torrent/Makefile.am b/libktorrent/torrent/Makefile.am index d546228..92a170d 100644 --- a/libktorrent/torrent/Makefile.am +++ b/libktorrent/torrent/Makefile.am @@ -8,7 +8,7 @@ libtorrent_la_LDFLAGS = $(all_libraries) noinst_HEADERS = advancedchokealgorithm.h announcelist.h authenticate.h \ authenticatebase.h authenticationmonitor.h bdecoder.h bencoder.h bnode.h cache.h \ cachefile.h cap.h choker.h chunk.h chunkcounter.h chunkdownload.h chunkmanager.h \ - chunkselector.h dndfile.h downloadcap.h downloader.h globals.h httptracker.h \ + chuntdeselector.h dndfile.h downloadcap.h downloader.h globals.h httptracker.h \ ipblocklist.h movedatafilesjob.h multifilecache.h newchokealgorithm.h \ oldchokealgorithm.h packet.h packetreader.h packetwriter.h peer.h peerdownloader.h peerid.h \ peermanager.h peersourcemanager.h peeruploader.h piece.h preallocationthread.h \ @@ -20,7 +20,7 @@ noinst_HEADERS = advancedchokealgorithm.h announcelist.h authenticate.h \ libtorrent_la_SOURCES = advancedchokealgorithm.cpp announcelist.cpp \ authenticate.cpp authenticatebase.cpp authenticationmonitor.cpp bdecoder.cpp \ bencoder.cpp bnode.cpp cache.cpp cachefile.cpp cap.cpp choker.cpp chunk.cpp \ - chunkcounter.cpp chunkdownload.cpp chunkmanager.cpp chunkselector.cpp dndfile.cpp \ + chunkcounter.cpp chunkdownload.cpp chunkmanager.cpp chuntdeselector.cpp dndfile.cpp \ downloadcap.cpp downloader.cpp globals.cpp httptracker.cpp ipblocklist.cpp \ movedatafilesjob.cpp multifilecache.cpp newchokealgorithm.cpp packet.cpp packetreader.cpp \ packetwriter.cpp peer.cpp peerdownloader.cpp peerid.cpp peermanager.cpp \ diff --git a/libktorrent/torrent/chuntdeselector.cpp b/libktorrent/torrent/chuntdeselector.cpp index b1c42fa..5bbd140 100644 --- a/libktorrent/torrent/chuntdeselector.cpp +++ b/libktorrent/torrent/chuntdeselector.cpp @@ -23,7 +23,7 @@ #include #include #include "chunkcounter.h" -#include "chunkselector.h" +#include "chuntdeselector.h" #include "chunkmanager.h" #include "downloader.h" #include "peerdownloader.h" @@ -158,7 +158,7 @@ namespace bt // lets do a safety check first if (from >= cman.getNumChunks() || to >= cman.getNumChunks()) { - Out(SYS_DIO|LOG_NOTICE) << "Internal error in chunkselector" << endl; + Out(SYS_DIO|LOG_NOTICE) << "Internal error in chuntdeselector" << endl; return; } diff --git a/libktorrent/torrent/downloader.cpp b/libktorrent/torrent/downloader.cpp index 582fa7e..4b81f1b 100644 --- a/libktorrent/torrent/downloader.cpp +++ b/libktorrent/torrent/downloader.cpp @@ -33,7 +33,7 @@ #include #include #include "packetwriter.h" -#include "chunkselector.h" +#include "chuntdeselector.h" #include "ipblocklist.h" #include "ktversion.h" diff --git a/libktorrent/torrent/ipblocklist.cpp b/libktorrent/torrent/ipblocklist.cpp new file mode 100644 index 0000000..f524d6b --- /dev/null +++ b/libktorrent/torrent/ipblocklist.cpp @@ -0,0 +1,400 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * ivasic@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "ipblocklist.h" +#include +#include +#include +#include +#include +#include "globals.h" +#include + + +namespace bt +{ + Uint32 toUint32(const TQString& ip, bool* ok) + { + bool test; + *ok = true; + + Uint32 ret = ip.section('.',0,0).toULongLong(&test); + if(!test) *ok=false; + ret <<= 8; + ret |= ip.section('.',1,1).toULong(&test); + if(!test) *ok=false; + ret <<= 8; + ret |= ip.section('.',2,2).toULong(&test); + if(!test) *ok=false; + ret <<= 8; + ret |= ip.section('.',3,3).toULong(&test); + if(!test) *ok=false; + + if(*ok) + { + // Out() << "IP: " << ip << " parsed: " << ret << endl; + return ret; + } + else + { + // Out() << "Could not parse IP " << ip << ". IP blocklist might not be working." << endl; + return 0; + } + } + + IPBlocklist::IPBlocklist() + { + this->pluginInterface = 0; + insert("0.0.0.0",3); + addRange("3.*.*.*"); + } + + IPBlocklist::IPBlocklist(const IPBlocklist & ) {} + + void IPBlocklist::insert( TQString ip, int state ) + { + bool ok; + Uint32 ipi = toUint32(ip, &ok); + if(!ok) + return; + IPKey key(ipi,0xFFFFFFFF); //-- you can test ranges here. Just specify your mask. + insertRangeIP(key, state); + Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " banned." << endl; + } + + void IPBlocklist::addRange(TQString ip) + { + bool ok; + int tmp = 0; + Uint32 addr = 0; + Uint32 mask = 0xFFFFFFFF; + + tmp = ip.section('.',0,0).toInt(&ok); + if(!ok) + { + if(ip.section('.',0,0) == "*") + mask &= 0x00FFFFFF; + else return; //illegal character + } + else + addr = tmp; + + tmp = ip.section('.',1,1).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',1,1) == "*") + mask &= 0xFF00FFFF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',2,2).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',2,2) == "*") + mask &= 0xFFFF00FF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',3,3).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',3,3) == "*") + mask &=0xFFFFFF00; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + IPKey key(addr, mask); + this->insertRangeIP(key); + } + + void IPBlocklist::insertRangeIP(IPKey& key, int state) + { +// Out() << "Blocked range: " << key.m_ip << " - " << key.m_mask << endl; + TQMap::iterator it; + if ((it = m_peers.find(key)) != m_peers.end()) + { + + if(it.key().m_mask != key.m_mask) + { + int st = it.data(); + IPKey key1(key.m_ip, it.key().m_mask | key.m_mask); + m_peers.insert(key1, state+st); + return; + } + m_peers[key]+= state; + } + else + m_peers.insert(key,state); + } + + void IPBlocklist::removeRange(TQString ip) + { + bool ok; + int tmp = 0; + Uint32 addr = 0; + Uint32 mask = 0xFFFFFFFF; + + tmp = ip.section('.',0,0).toInt(&ok); + if(!ok) + { + if(ip.section('.',0,0) == "*") + mask &= 0x00FFFFFF; + else return; //illegal character + } + else + addr = tmp; + + tmp = ip.section('.',1,1).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',1,1) == "*") + mask &= 0xFF00FFFF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',2,2).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',2,2) == "*") + mask &= 0xFFFF00FF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',3,3).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',3,3) == "*") + mask &=0xFFFFFF00; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + IPKey key(addr, mask); + + TQMap::iterator it = m_peers.find(key); + if (it == m_peers.end()) + return; + + m_peers.remove(key); + } + + void IPBlocklist::setPluginInterfacePtr( kt::IPBlockingInterface* ptr ) + { + this->pluginInterface = ptr; + } + + bool IPBlocklist::isBlocked(const TQString& ip ) + { + //First check local filter list + if(isBlockedLocal(ip)) + { + Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " is blacklisted. Connection denied." << endl; + return true; + } + + //Then we ask plugin + if(isBlockedPlugin(ip)) + { + Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " is blacklisted. Connection denied." << endl; + return true; + } + + return false; + } + + bool IPBlocklist::isBlockedLocal(const TQString& ip ) + { + bool ok; + Uint32 ipi = toUint32(ip,&ok); + if (!ok) + return false; + IPKey key(ipi); + + TQMap::iterator it; + it = m_peers.find(key); + if (it==m_peers.end()) + return false; + + return m_peers[key] >= 3; + } + + bool IPBlocklist::isBlockedPlugin(const TQString& ip ) + { + if (pluginInterface == 0) //the plugin is not loaded + return false; + else + return pluginInterface->isBlockedIP(ip); + } + + TQStringList* IPBlocklist::getBlocklist() + { + TQStringList* ret = new TQStringList(); + TQMap::iterator it = m_peers.begin(); + for( ;it!=m_peers.end();++it) + { + IPKey key = it.key(); + *ret << key.toString(); + } + + return ret; + } + + void IPBlocklist::setBlocklist(TQStringList* list) + { + m_peers.clear(); + for (TQStringList::Iterator it = list->begin(); it != list->end(); ++it ) + addRange(*it); + } + + /*** IPKey *****************************************************************************************************************/ + + IPKey::IPKey() + { + m_ip = 0; + m_mask = 0xFFFFFFFF; + } + + IPKey::IPKey(TQString& ip, Uint32 mask) + : m_mask(mask) + { + bool ok; + this->m_ip = toUint32(ip, &ok); + } + + IPKey::IPKey(const IPKey& ip) + { + m_ip = ip.m_ip; + m_mask = ip.m_mask; + } + + IPKey::IPKey(Uint32 ip, Uint32 mask) + : m_ip(ip), m_mask(mask) + {} + + TQString IPKey::toString() + { + Uint32 tmp, tmpmask; + Uint32 ip = m_ip; + Uint32 mask = m_mask; + TQString out; + + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*"); + else + out.prepend(TQString("%1").arg(tmp)); + ip >>= 8; + mask >>= 8; + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*."); + else + out.prepend(TQString("%1.").arg(tmp)); + ip >>= 8; + mask >>= 8; + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*."); + else + out.prepend(TQString("%1.").arg(tmp)); + ip >>= 8; + mask >>= 8; + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*."); + else + out.prepend(TQString("%1.").arg(tmp)); + + return out; + } + + bool IPKey::operator ==(const IPKey& ip) const + { + return (m_ip & m_mask) == m_mask & ip.m_ip; + } + + bool IPKey::operator !=(const IPKey& ip) const + { + return (m_ip & m_mask) != m_mask & ip.m_ip; + } + + bool IPKey::operator < (const IPKey& ip) const + { + return (m_ip & m_mask) < (m_mask & ip.m_ip); + } + + IPKey& IPKey::operator =(const IPKey& ip) + { + m_ip = ip.m_ip; + m_mask = ip.m_mask; + return *this; + } + + IPKey::~ IPKey() + {} +} diff --git a/libktorrent/torrent/ipblocklist.h b/libktorrent/torrent/ipblocklist.h new file mode 100644 index 0000000..6439972 --- /dev/null +++ b/libktorrent/torrent/ipblocklist.h @@ -0,0 +1,175 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * ivasic@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef IPBLOCKLIST_H +#define IPBLOCKLIST_H + +#include + +#include +#include +#include + +class TQString; + +namespace bt +{ + class IPKey + { + public: + IPKey(); + IPKey(TQString& ip, Uint32 mask = 0xFFFFFFFF); + IPKey(Uint32 ip, Uint32 mask = 0xFFFFFFFF); + IPKey(const IPKey& ip); + ~IPKey(); + + bool operator== (const IPKey& ip) const; + bool operator!= (const IPKey& ip) const; + bool operator < (const IPKey & ip) const; + IPKey& operator= (const IPKey& ip); + + TQString toString(); + + Uint32 m_ip; + Uint32 m_mask; + }; + + /** + * @author Ivan Vasic + * @brief Keeps track of blocked peers + * + * This class is used for keeping the IP addresses list of peers that + * have sent bad chunks. + * + * Peers that have sent >= 3 bad chunks are blocked. + */ + class IPBlocklist + { + IPBlocklist(); + IPBlocklist(const IPBlocklist & ); + const IPBlocklist& operator=(const IPBlocklist&); + + public: + + inline static IPBlocklist & instance() + { + static IPBlocklist singleton; + return singleton; + } + + /** + * @brief Adds ip address to the list. + * It also increases the number of times this IP appeared in the list. + * @param ip TQString containing the peer IP address + * @param state int number of bad chunks client from ip sent. Basically this parameter + * is used only to permanently block some IP (by setting this param to 3) + */ + void insert(TQString ip, int state=1); + + /** + * @brief Adds IP range to the list + * It is used for blocking plugin. For single IP use insert() instead. + * @param ip TQString peer IP address. Uses ''*" for ranges. + **/ + void addRange(TQString ip); + + + /** + * @brief Removes IP range from list + * It is used for blocking plugin. + * @param ip TQString peer IP address. Uses ''*" for ranges. + **/ + void removeRange(TQString ip); + + /** + * Checks if IP is in the blocking list + * @param ip - IP address to check + * @returns true if IP is blocked + */ + bool isBlocked(const TQString& ip); + + /** + * @brief Sets the pointer to the IPBlockingInterface (IPBlocking plugin) + * Call this function from IPBlocking plugin when it gets loaded. + * @arg ptr - pointer to be set + */ + void setPluginInterfacePtr(kt::IPBlockingInterface* ptr); + + /** + * @brief Unsets the interface pointer + * Call this when IPBlockingPlugin gets unloaded or deleted + */ + void unsetPluginInterfacePtr() { pluginInterface = 0; } + + + /** + * @brief This function will fill TQStringList with all banned peer IP addresses. + * @return TQStringList filled with blacklisted peers. + * It will create a new TQStringList object so don't forget to delete it after using. + */ + TQStringList* getBlocklist(); + + + /** + * @brief This function will load blacklisted peers to IPFilter. + * @param list TQStringList containing all banned peers. + * @note This function will remove current peers from blocklist before setting new list!!! + */ + void setBlocklist(TQStringList* list); + + private: + + /** + * Pointer to the IPBlocking plugin which implements IPBlockingInterface + * Used to provide a way to use this plugin functions from within this class + */ + kt::IPBlockingInterface* pluginInterface; + + /** + * @param IPKey - Key: Peer IP address and bit mask if it is a range + * @param int - Number of bad chunks sent. + **/ + TQMap m_peers; + + /** + * @brief Adds IP range to the list. + * @param key IPKey that represents this IP range + * @param state int Number of 'warnings' for the range. + * Default is 3 - that means range is blocked permanently. + */ + void insertRangeIP(IPKey& key, int state=3); + + + /** + * Checks if IP is listed in local database (IPBlocklist::m_peers) + * @return TRUE if IP is to be blocked + */ + bool isBlockedLocal(const TQString& ip); + + /** + * Checks if IP is listed in plugins antip2p file + * @return TRUE if IP is to be blocked + */ + bool isBlockedPlugin(const TQString& ip); + }; +} + +#endif + diff --git a/libktorrent/torrent/ipbloctdelist.cpp b/libktorrent/torrent/ipbloctdelist.cpp deleted file mode 100644 index f524d6b..0000000 --- a/libktorrent/torrent/ipbloctdelist.cpp +++ /dev/null @@ -1,400 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2005 by Joris Guisson * - * joris.guisson@gmail.com * - * ivasic@gmail.com * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ - -#include "ipblocklist.h" -#include -#include -#include -#include -#include -#include "globals.h" -#include - - -namespace bt -{ - Uint32 toUint32(const TQString& ip, bool* ok) - { - bool test; - *ok = true; - - Uint32 ret = ip.section('.',0,0).toULongLong(&test); - if(!test) *ok=false; - ret <<= 8; - ret |= ip.section('.',1,1).toULong(&test); - if(!test) *ok=false; - ret <<= 8; - ret |= ip.section('.',2,2).toULong(&test); - if(!test) *ok=false; - ret <<= 8; - ret |= ip.section('.',3,3).toULong(&test); - if(!test) *ok=false; - - if(*ok) - { - // Out() << "IP: " << ip << " parsed: " << ret << endl; - return ret; - } - else - { - // Out() << "Could not parse IP " << ip << ". IP blocklist might not be working." << endl; - return 0; - } - } - - IPBlocklist::IPBlocklist() - { - this->pluginInterface = 0; - insert("0.0.0.0",3); - addRange("3.*.*.*"); - } - - IPBlocklist::IPBlocklist(const IPBlocklist & ) {} - - void IPBlocklist::insert( TQString ip, int state ) - { - bool ok; - Uint32 ipi = toUint32(ip, &ok); - if(!ok) - return; - IPKey key(ipi,0xFFFFFFFF); //-- you can test ranges here. Just specify your mask. - insertRangeIP(key, state); - Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " banned." << endl; - } - - void IPBlocklist::addRange(TQString ip) - { - bool ok; - int tmp = 0; - Uint32 addr = 0; - Uint32 mask = 0xFFFFFFFF; - - tmp = ip.section('.',0,0).toInt(&ok); - if(!ok) - { - if(ip.section('.',0,0) == "*") - mask &= 0x00FFFFFF; - else return; //illegal character - } - else - addr = tmp; - - tmp = ip.section('.',1,1).toInt(&ok); - if(!ok) - { - addr <<= 8; - if(ip.section('.',1,1) == "*") - mask &= 0xFF00FFFF; - else return; //illegal character - } - else - { - addr <<= 8; - addr |= tmp; - } - - tmp = ip.section('.',2,2).toInt(&ok); - if(!ok) - { - addr <<= 8; - if(ip.section('.',2,2) == "*") - mask &= 0xFFFF00FF; - else return; //illegal character - } - else - { - addr <<= 8; - addr |= tmp; - } - - tmp = ip.section('.',3,3).toInt(&ok); - if(!ok) - { - addr <<= 8; - if(ip.section('.',3,3) == "*") - mask &=0xFFFFFF00; - else return; //illegal character - } - else - { - addr <<= 8; - addr |= tmp; - } - - IPKey key(addr, mask); - this->insertRangeIP(key); - } - - void IPBlocklist::insertRangeIP(IPKey& key, int state) - { -// Out() << "Blocked range: " << key.m_ip << " - " << key.m_mask << endl; - TQMap::iterator it; - if ((it = m_peers.find(key)) != m_peers.end()) - { - - if(it.key().m_mask != key.m_mask) - { - int st = it.data(); - IPKey key1(key.m_ip, it.key().m_mask | key.m_mask); - m_peers.insert(key1, state+st); - return; - } - m_peers[key]+= state; - } - else - m_peers.insert(key,state); - } - - void IPBlocklist::removeRange(TQString ip) - { - bool ok; - int tmp = 0; - Uint32 addr = 0; - Uint32 mask = 0xFFFFFFFF; - - tmp = ip.section('.',0,0).toInt(&ok); - if(!ok) - { - if(ip.section('.',0,0) == "*") - mask &= 0x00FFFFFF; - else return; //illegal character - } - else - addr = tmp; - - tmp = ip.section('.',1,1).toInt(&ok); - if(!ok) - { - addr <<= 8; - if(ip.section('.',1,1) == "*") - mask &= 0xFF00FFFF; - else return; //illegal character - } - else - { - addr <<= 8; - addr |= tmp; - } - - tmp = ip.section('.',2,2).toInt(&ok); - if(!ok) - { - addr <<= 8; - if(ip.section('.',2,2) == "*") - mask &= 0xFFFF00FF; - else return; //illegal character - } - else - { - addr <<= 8; - addr |= tmp; - } - - tmp = ip.section('.',3,3).toInt(&ok); - if(!ok) - { - addr <<= 8; - if(ip.section('.',3,3) == "*") - mask &=0xFFFFFF00; - else return; //illegal character - } - else - { - addr <<= 8; - addr |= tmp; - } - - IPKey key(addr, mask); - - TQMap::iterator it = m_peers.find(key); - if (it == m_peers.end()) - return; - - m_peers.remove(key); - } - - void IPBlocklist::setPluginInterfacePtr( kt::IPBlockingInterface* ptr ) - { - this->pluginInterface = ptr; - } - - bool IPBlocklist::isBlocked(const TQString& ip ) - { - //First check local filter list - if(isBlockedLocal(ip)) - { - Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " is blacklisted. Connection denied." << endl; - return true; - } - - //Then we ask plugin - if(isBlockedPlugin(ip)) - { - Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " is blacklisted. Connection denied." << endl; - return true; - } - - return false; - } - - bool IPBlocklist::isBlockedLocal(const TQString& ip ) - { - bool ok; - Uint32 ipi = toUint32(ip,&ok); - if (!ok) - return false; - IPKey key(ipi); - - TQMap::iterator it; - it = m_peers.find(key); - if (it==m_peers.end()) - return false; - - return m_peers[key] >= 3; - } - - bool IPBlocklist::isBlockedPlugin(const TQString& ip ) - { - if (pluginInterface == 0) //the plugin is not loaded - return false; - else - return pluginInterface->isBlockedIP(ip); - } - - TQStringList* IPBlocklist::getBlocklist() - { - TQStringList* ret = new TQStringList(); - TQMap::iterator it = m_peers.begin(); - for( ;it!=m_peers.end();++it) - { - IPKey key = it.key(); - *ret << key.toString(); - } - - return ret; - } - - void IPBlocklist::setBlocklist(TQStringList* list) - { - m_peers.clear(); - for (TQStringList::Iterator it = list->begin(); it != list->end(); ++it ) - addRange(*it); - } - - /*** IPKey *****************************************************************************************************************/ - - IPKey::IPKey() - { - m_ip = 0; - m_mask = 0xFFFFFFFF; - } - - IPKey::IPKey(TQString& ip, Uint32 mask) - : m_mask(mask) - { - bool ok; - this->m_ip = toUint32(ip, &ok); - } - - IPKey::IPKey(const IPKey& ip) - { - m_ip = ip.m_ip; - m_mask = ip.m_mask; - } - - IPKey::IPKey(Uint32 ip, Uint32 mask) - : m_ip(ip), m_mask(mask) - {} - - TQString IPKey::toString() - { - Uint32 tmp, tmpmask; - Uint32 ip = m_ip; - Uint32 mask = m_mask; - TQString out; - - tmp = ip; - tmpmask = mask; - tmp &= 0x000000FF; - tmpmask &= 0x000000FF; - if(tmpmask == 0) - out.prepend("*"); - else - out.prepend(TQString("%1").arg(tmp)); - ip >>= 8; - mask >>= 8; - tmp = ip; - tmpmask = mask; - tmp &= 0x000000FF; - tmpmask &= 0x000000FF; - if(tmpmask == 0) - out.prepend("*."); - else - out.prepend(TQString("%1.").arg(tmp)); - ip >>= 8; - mask >>= 8; - tmp = ip; - tmpmask = mask; - tmp &= 0x000000FF; - tmpmask &= 0x000000FF; - if(tmpmask == 0) - out.prepend("*."); - else - out.prepend(TQString("%1.").arg(tmp)); - ip >>= 8; - mask >>= 8; - tmp = ip; - tmpmask = mask; - tmp &= 0x000000FF; - tmpmask &= 0x000000FF; - if(tmpmask == 0) - out.prepend("*."); - else - out.prepend(TQString("%1.").arg(tmp)); - - return out; - } - - bool IPKey::operator ==(const IPKey& ip) const - { - return (m_ip & m_mask) == m_mask & ip.m_ip; - } - - bool IPKey::operator !=(const IPKey& ip) const - { - return (m_ip & m_mask) != m_mask & ip.m_ip; - } - - bool IPKey::operator < (const IPKey& ip) const - { - return (m_ip & m_mask) < (m_mask & ip.m_ip); - } - - IPKey& IPKey::operator =(const IPKey& ip) - { - m_ip = ip.m_ip; - m_mask = ip.m_mask; - return *this; - } - - IPKey::~ IPKey() - {} -} diff --git a/libktorrent/torrent/ipbloctdelist.h b/libktorrent/torrent/ipbloctdelist.h deleted file mode 100644 index 6439972..0000000 --- a/libktorrent/torrent/ipbloctdelist.h +++ /dev/null @@ -1,175 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2005 by Joris Guisson * - * joris.guisson@gmail.com * - * ivasic@gmail.com * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ -#ifndef IPBLOCKLIST_H -#define IPBLOCKLIST_H - -#include - -#include -#include -#include - -class TQString; - -namespace bt -{ - class IPKey - { - public: - IPKey(); - IPKey(TQString& ip, Uint32 mask = 0xFFFFFFFF); - IPKey(Uint32 ip, Uint32 mask = 0xFFFFFFFF); - IPKey(const IPKey& ip); - ~IPKey(); - - bool operator== (const IPKey& ip) const; - bool operator!= (const IPKey& ip) const; - bool operator < (const IPKey & ip) const; - IPKey& operator= (const IPKey& ip); - - TQString toString(); - - Uint32 m_ip; - Uint32 m_mask; - }; - - /** - * @author Ivan Vasic - * @brief Keeps track of blocked peers - * - * This class is used for keeping the IP addresses list of peers that - * have sent bad chunks. - * - * Peers that have sent >= 3 bad chunks are blocked. - */ - class IPBlocklist - { - IPBlocklist(); - IPBlocklist(const IPBlocklist & ); - const IPBlocklist& operator=(const IPBlocklist&); - - public: - - inline static IPBlocklist & instance() - { - static IPBlocklist singleton; - return singleton; - } - - /** - * @brief Adds ip address to the list. - * It also increases the number of times this IP appeared in the list. - * @param ip TQString containing the peer IP address - * @param state int number of bad chunks client from ip sent. Basically this parameter - * is used only to permanently block some IP (by setting this param to 3) - */ - void insert(TQString ip, int state=1); - - /** - * @brief Adds IP range to the list - * It is used for blocking plugin. For single IP use insert() instead. - * @param ip TQString peer IP address. Uses ''*" for ranges. - **/ - void addRange(TQString ip); - - - /** - * @brief Removes IP range from list - * It is used for blocking plugin. - * @param ip TQString peer IP address. Uses ''*" for ranges. - **/ - void removeRange(TQString ip); - - /** - * Checks if IP is in the blocking list - * @param ip - IP address to check - * @returns true if IP is blocked - */ - bool isBlocked(const TQString& ip); - - /** - * @brief Sets the pointer to the IPBlockingInterface (IPBlocking plugin) - * Call this function from IPBlocking plugin when it gets loaded. - * @arg ptr - pointer to be set - */ - void setPluginInterfacePtr(kt::IPBlockingInterface* ptr); - - /** - * @brief Unsets the interface pointer - * Call this when IPBlockingPlugin gets unloaded or deleted - */ - void unsetPluginInterfacePtr() { pluginInterface = 0; } - - - /** - * @brief This function will fill TQStringList with all banned peer IP addresses. - * @return TQStringList filled with blacklisted peers. - * It will create a new TQStringList object so don't forget to delete it after using. - */ - TQStringList* getBlocklist(); - - - /** - * @brief This function will load blacklisted peers to IPFilter. - * @param list TQStringList containing all banned peers. - * @note This function will remove current peers from blocklist before setting new list!!! - */ - void setBlocklist(TQStringList* list); - - private: - - /** - * Pointer to the IPBlocking plugin which implements IPBlockingInterface - * Used to provide a way to use this plugin functions from within this class - */ - kt::IPBlockingInterface* pluginInterface; - - /** - * @param IPKey - Key: Peer IP address and bit mask if it is a range - * @param int - Number of bad chunks sent. - **/ - TQMap m_peers; - - /** - * @brief Adds IP range to the list. - * @param key IPKey that represents this IP range - * @param state int Number of 'warnings' for the range. - * Default is 3 - that means range is blocked permanently. - */ - void insertRangeIP(IPKey& key, int state=3); - - - /** - * Checks if IP is listed in local database (IPBlocklist::m_peers) - * @return TRUE if IP is to be blocked - */ - bool isBlockedLocal(const TQString& ip); - - /** - * Checks if IP is listed in plugins antip2p file - * @return TRUE if IP is to be blocked - */ - bool isBlockedPlugin(const TQString& ip); - }; -} - -#endif - -- cgit v1.2.1