Commit 37c78e13 authored by Joris Guisson's avatar Joris Guisson
Browse files

Merged rev 798201 from trunk (blocklist fix)


svn path=/branches/ktorrent/3.0/; revision=798204
parent 96001771
......@@ -13,6 +13,7 @@ Changes in 3.0.2 :
- Fix crash when removing 2 torrents at the same time
- Show real IP address of peers when using socks proxies
- Make sure that the current torrent changes when the group is changed of a view
- Sort and merge block list before writing to level1.dat file
Changes in 3.0.1 :
- Fix sessionTTL not being able to be bigger then 99
......
......@@ -50,8 +50,8 @@ namespace kt
class AntiP2P
{
public:
AntiP2P();
virtual ~AntiP2P();
AntiP2P();
virtual ~AntiP2P();
/**
* Checks if anti-p2p file is present. Used to check if we should use level1 list
......
......@@ -38,14 +38,9 @@ using namespace bt;
namespace kt
{
typedef struct
{
bt::Uint32 ip1;
bt::Uint32 ip2;
} ipblock;
Uint32 toUint32(QString& ip)
Uint32 toUint32(const QString& ip)
{
bool test;
Uint32 ret = ip.section('.',0,0).toULongLong(&test);
......@@ -59,9 +54,9 @@ namespace kt
return ret;
}
ipblock toBlock(QString& range)
IPBlock IPRangeToBlock(const QString & range)
{
ipblock block;
IPBlock block;
QStringList ls = range.split('-');
block.ip1 = toUint32(ls[0]);
block.ip2 = toUint32(ls[1]);
......@@ -121,15 +116,74 @@ namespace kt
if ( v.validate( ip_part, poz ) != QValidator::Acceptable )
continue;
else
input += ip_part;
input += IPRangeToBlock(ip_part);
}
source.close();
Out(SYS_IPF|LOG_NOTICE) << "Loaded " << input.count() << " lines" << endl;
dlg->progress(100,100);
}
static bool LessThan(const IPBlock & a,const IPBlock & b)
{
if (a.ip2 < b.ip1) // a range is before b range
return true;
else if (b.ip2 < a.ip1) // b range is before a range
return false;
else
return a.ip1 < b.ip1;// a and b intersect
}
static QString IPToString(Uint32 ip)
{
return QString("%1.%2.%3.%4")
.arg((ip & 0xFF000000) >> 24)
.arg((ip & 0x00FF0000) >> 16)
.arg((ip & 0x0000FF00) >> 8)
.arg((ip & 0x000000FF));
}
void ConvertThread::sort()
{
qSort(input.begin(),input.end(),LessThan);
}
void ConvertThread::merge()
{
if (input.count() < 2) // noting to merge
return;
QList<IPBlock>::iterator i = input.begin();
QList<IPBlock>::iterator j = i;
j++;
while (j != input.end() && i != input.end())
{
IPBlock & a = *i;
IPBlock & b = *j;
if (a.ip2 < b.ip1 || b.ip2 < a.ip1)
{
// separate ranges, so go to the next pair
i = j;
j++;
}
else
{
// merge b into a
a.ip1 = (a.ip1 < b.ip1) ? a.ip1 : b.ip1;
a.ip2 = (a.ip2 > b.ip2) ? a.ip2 : b.ip2;
// remove b
j = input.erase(j);
}
}
}
void ConvertThread::writeOutput()
{
sort(); // sort the block
merge(); // merge neigbhouring blocks
QFile target(dat_file);
if (!target.open(QIODevice::WriteOnly))
{
......@@ -141,14 +195,13 @@ namespace kt
Out(SYS_IPF|LOG_NOTICE) << "Loading finished, starting conversion..." << endl;
dlg->message(i18n( "Converting..." ));
QStringList::iterator iter;
int i = 0;
int tot = input.count();
foreach (QString line,input)
foreach (IPBlock block,input)
{
dlg->progress(i,tot);
ipblock block = toBlock(line);
target.write( ( char* ) & block, sizeof( ipblock ) );
target.write( ( char* ) & block, sizeof( IPBlock ) );
if (abort)
{
......
......@@ -22,6 +22,7 @@
#define KTCONVERTTHREAD_H
#include <QThread>
#include "antip2p.h"
class KJob;
......@@ -50,6 +51,8 @@ namespace kt
void readInput();
void writeOutput();
void cleanUp(bool failed);
void sort();
void merge();
private:
ConvertDialog* dlg;
......@@ -57,7 +60,7 @@ namespace kt
QString txt_file;
QString dat_file;
QString tmp_file;
QStringList input;
QList<IPBlock> input;
QString failure_reason;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment