summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-03-31 16:15:23 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-03-31 16:15:23 +0000
commitfc2307d47c525fb87bbfb166cfd35a2a2c0e9f56 (patch)
treea417b71c14bf58f03a181bcd84073f807ef3a3b4 /src
parent3d3363a544290906b577c43583908ca00b2b0b0f (diff)
* src/Util.cc (isPowerOf): New function.
* src/Util.h (isPowerOf): New function. * src/PeerMessageUtil.cc (checkLength): Added a check for length whether or not it is power of 2.
Diffstat (limited to 'src')
-rw-r--r--src/CookieBox.cc8
-rw-r--r--src/CookieBox.h10
-rw-r--r--src/FtpConnection.cc4
-rw-r--r--src/FtpConnection.h4
-rw-r--r--src/Peer.h18
-rw-r--r--src/PeerMessageUtil.cc9
-rw-r--r--src/PeerMessageUtil.h2
-rw-r--r--src/Piece.h2
-rw-r--r--src/TorrentMan.h7
-rw-r--r--src/Util.cc30
-rw-r--r--src/Util.h19
11 files changed, 56 insertions, 57 deletions
diff --git a/src/CookieBox.cc b/src/CookieBox.cc
index 1371afb0..85e58613 100644
--- a/src/CookieBox.cc
+++ b/src/CookieBox.cc
@@ -30,13 +30,13 @@ void CookieBox::add(const Cookie& cookie) {
cookies.push_back(cookie);
}
-void CookieBox::add(string cookieStr) {
+void CookieBox::add(const string& cookieStr) {
Cookie c;
parse(c, cookieStr);
cookies.push_back(c);
}
-void CookieBox::setField(Cookie& cookie, string name, string value) const {
+void CookieBox::setField(Cookie& cookie, const string& name, const string& value) const {
if(name.size() == string("secure").size() &&
strcasecmp(name.c_str(), "secure") == 0) {
cookie.secure = true;
@@ -52,7 +52,7 @@ void CookieBox::setField(Cookie& cookie, string name, string value) const {
}
}
-void CookieBox::parse(Cookie& cookie, string cookieStr) const {
+void CookieBox::parse(Cookie& cookie, const string& cookieStr) const {
cookie.clear();
Strings terms;
Util::slice(terms, cookieStr, ';');
@@ -63,7 +63,7 @@ void CookieBox::parse(Cookie& cookie, string cookieStr) const {
}
}
-Cookies CookieBox::criteriaFind(string host, string dir, bool secure) const {
+Cookies CookieBox::criteriaFind(const string& host, const string& dir, bool secure) const {
Cookies result;
for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
const Cookie& c = *itr;
diff --git a/src/CookieBox.h b/src/CookieBox.h
index 6892670a..f1d7770f 100644
--- a/src/CookieBox.h
+++ b/src/CookieBox.h
@@ -37,7 +37,7 @@ public:
string domain;
bool secure;
public:
- Cookie(string name, string value, string expires, string path, string domain, bool secure):name(name), value(value), expires(expires), path(path), domain(domain), secure(secure) {}
+ Cookie(const string& name, const string& value, const string& expires, const string& path, const string& domain, bool secure):name(name), value(value), expires(expires), path(path), domain(domain), secure(secure) {}
Cookie():secure(false) {}
~Cookie() {}
string toString() const {
@@ -54,15 +54,15 @@ typedef deque<Cookie> Cookies;
class CookieBox {
private:
Cookies cookies;
- void setField(Cookie& cookie, string name, string value) const;
+ void setField(Cookie& cookie, const string& name, const string& value) const;
public:
CookieBox();
~CookieBox();
void clear();
void add(const Cookie& cookie);
- void add(string cookieStr);
- void parse(Cookie& cookie, string cookieStr) const;
- Cookies criteriaFind(string host, string dir, bool secure) const;
+ void add(const string& cookieStr);
+ void parse(Cookie& cookie, const string& cookieStr) const;
+ Cookies criteriaFind(const string& host, const string& dir, bool secure) const;
};
#endif // _D_COOKIE_BOX_H_
diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc
index dac5a387..e265beb1 100644
--- a/src/FtpConnection.cc
+++ b/src/FtpConnection.cc
@@ -108,7 +108,7 @@ void FtpConnection::sendRetr() const {
socket->writeData(request);
}
-int FtpConnection::getStatus(string response) const {
+int FtpConnection::getStatus(const string& response) const {
int status;
// When the response is not like "%d %*s",
// we return 0.
@@ -123,7 +123,7 @@ int FtpConnection::getStatus(string response) const {
}
}
-bool FtpConnection::isEndOfResponse(int status, string response) const {
+bool FtpConnection::isEndOfResponse(int status, const string& response) const {
if(response.size() <= 4) {
return false;
}
diff --git a/src/FtpConnection.h b/src/FtpConnection.h
index 7b5ec17e..7e37506a 100644
--- a/src/FtpConnection.h
+++ b/src/FtpConnection.h
@@ -42,8 +42,8 @@ private:
string strbuf;
- int getStatus(string response) const;
- bool isEndOfResponse(int status, string response) const;
+ int getStatus(const string& response) const;
+ bool isEndOfResponse(int status, const string& response) const;
bool bulkReceiveResponse(pair<int, string>& response);
public:
FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger);
diff --git a/src/Peer.h b/src/Peer.h
index 41b323ac..ace51d38 100644
--- a/src/Peer.h
+++ b/src/Peer.h
@@ -45,9 +45,7 @@ public:
int cuid;
private:
char peerId[PEER_ID_LENGTH];
- //unsigned char* bitfield;
BitfieldMan* bitfield;
- //int bitfieldLength;
long long int peerUpload;
long long int peerDownload;
int pieceLength;
@@ -58,7 +56,7 @@ public:
amChocking(true), amInterested(false),
peerChoking(true), peerInterested(false),
tryCount(0), error(0), cuid(0),
- bitfield(NULL),// bitfieldLength(0),
+ bitfield(NULL),
peerUpload(0), peerDownload(0),
pieceLength(pieceLength), totalLength(totalLength) {
this->bitfield = new BitfieldMan(pieceLength, totalLength);
@@ -66,7 +64,7 @@ public:
~Peer() {
if(bitfield != NULL) {
- delete /*[]*/ bitfield;
+ delete bitfield;
}
}
@@ -77,22 +75,10 @@ public:
void setBitfield(const unsigned char* bitfield, int bitfieldLength) {
this->bitfield->setBitfield(bitfield, bitfieldLength);
- /*
- if(this->bitfield != NULL) {
- delete [] this->bitfield;
- }
- this->bitfieldLength = bitfieldLength;
- this->bitfield = new unsigned char[this->bitfieldLength];
- memcpy(this->bitfield, bitfield, this->bitfieldLength);
- */
}
const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
- /*
- void initBitfield(int pieces);
- */
-
/**
* operation = 1: set index-th bit 1
* operation = 0: set index-th bit 0
diff --git a/src/PeerMessageUtil.cc b/src/PeerMessageUtil.cc
index 0585866a..29b1768d 100644
--- a/src/PeerMessageUtil.cc
+++ b/src/PeerMessageUtil.cc
@@ -151,8 +151,13 @@ void PeerMessageUtil::checkPieceOffset(const PeerMessage* message, int pieceLeng
}
void PeerMessageUtil::checkLength(const PeerMessage* message) {
- if(message->getLength() > 128*1024) {
- throw new DlAbortEx("too large length %d > 128KB", message->getLength());
+ if(message->getLength() > MAX_BLOCK_LENGTH) {
+ throw new DlAbortEx("too large length %d > %dKB", message->getLength(),
+ MAX_BLOCK_LENGTH/1024);
+ }
+ if(!Util::isPowerOf(message->getLength(), 2)) {
+ throw new DlAbortEx("invalid length %d, which is not power of 2",
+ message->getLength());
}
}
diff --git a/src/PeerMessageUtil.h b/src/PeerMessageUtil.h
index 47e74049..ca8ae522 100644
--- a/src/PeerMessageUtil.h
+++ b/src/PeerMessageUtil.h
@@ -24,6 +24,8 @@
#include "PeerConnection.h"
+#define MAX_BLOCK_LENGTH (128*1024)
+
class PeerMessageUtil {
private:
PeerMessageUtil() {}
diff --git a/src/Piece.h b/src/Piece.h
index d8d7d95d..9d77576f 100644
--- a/src/Piece.h
+++ b/src/Piece.h
@@ -25,7 +25,7 @@
#include "BitfieldMan.h"
#include "common.h"
-#define BLOCK_LENGTH 16*1024
+#define BLOCK_LENGTH (16*1024)
class Piece {
private:
diff --git a/src/TorrentMan.h b/src/TorrentMan.h
index 866ffda7..a4f6cedd 100644
--- a/src/TorrentMan.h
+++ b/src/TorrentMan.h
@@ -35,18 +35,11 @@
using namespace std;
-#define DEFAULT_BLOCK_LEN 16*1024;
-#define MAX_BLOCK_LEN 128*1024;
-
#define INFO_HASH_LENGTH 20
-
-#define IS_NULL_PIECE(X) (X.index == 0 && X.length == 0)
-
#define DEFAULT_ANNOUNCE_INTERVAL 300
#define DEFAULT_ANNOUNCE_MIN_INTERVAL 300
#define MAX_PEERS 55
#define MAX_PEER_UPDATE 15
-
#define MAX_PEER_LIST_SIZE 250
#define END_GAME_PIECE_NUM 20
#define MAX_PEER_ERROR 5
diff --git a/src/Util.cc b/src/Util.cc
index 5396d60c..ee7f5693 100644
--- a/src/Util.cc
+++ b/src/Util.cc
@@ -59,7 +59,7 @@ string Util::llitos(long long int value, bool comma)
return str;
}
-string Util::trim(string src) {
+string Util::trim(const string& src) {
string::size_type sp = src.find_first_not_of(" ");
string::size_type ep = src.find_last_not_of(" ");
if(sp == string::npos || ep == string::npos) {
@@ -69,7 +69,7 @@ string Util::trim(string src) {
}
}
-void Util::split(pair<string, string>& hp, string src, char delim) {
+void Util::split(pair<string, string>& hp, const string& src, char delim) {
hp.first = "";
hp.second = "";
string::size_type p = src.find(delim);
@@ -90,7 +90,7 @@ long long int Util::difftv(struct timeval tv1, struct timeval tv2) {
tv1.tv_usec-tv2.tv_usec);
}
-void Util::slice(Strings& result, string src, char delim) {
+void Util::slice(Strings& result, const string& src, char delim) {
string::size_type p = 0;
while(1) {
string::size_type np = src.find(delim, p);
@@ -107,7 +107,7 @@ void Util::slice(Strings& result, string src, char delim) {
}
}
-bool Util::startsWith(string target, string part) {
+bool Util::startsWith(const string& target, const string& part) {
if(target.size() < part.size()) {
return false;
}
@@ -121,7 +121,7 @@ bool Util::startsWith(string target, string part) {
}
}
-bool Util::endsWith(string target, string part) {
+bool Util::endsWith(const string& target, const string& part) {
if(target.size() < part.size()) {
return false;
}
@@ -135,7 +135,7 @@ bool Util::endsWith(string target, string part) {
}
}
-string Util::replace(string target, string oldstr, string newstr) {
+string Util::replace(const string& target, const string& oldstr, const string& newstr) {
if(target == "" || oldstr == "" ) {
return target;
}
@@ -186,17 +186,17 @@ string Util::toHex(const unsigned char* src, int len) {
return hex;
}
-FILE* Util::openFile(string filename, string mode) {
+FILE* Util::openFile(const string& filename, const string& mode) {
FILE* file = fopen(filename.c_str(), mode.c_str());
return file;
}
-void Util::fileCopy(string dest, string src) {
+void Util::fileCopy(const string& dest, const string& src) {
File file(src);
rangedFileCopy(dest, src, 0, file.size());
}
-void Util::rangedFileCopy(string dest, string src, long long int srcOffset, long long int length) {
+void Util::rangedFileCopy(const string& dest, const string& src, long long int srcOffset, long long int length) {
int bufSize = 4096;
char buf[bufSize];
int destFd = -1;
@@ -246,3 +246,15 @@ void Util::rangedFileCopy(string dest, string src, long long int srcOffset, long
}
}
+bool Util::isPowerOf(int num, int base) {
+ if(base <= 0) { return false; }
+ if(base == 1) { return true; }
+
+ while(num%base == 0) {
+ num /= base;
+ if(num == 1) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/src/Util.h b/src/Util.h
index 6b96b6a1..8375c65b 100644
--- a/src/Util.h
+++ b/src/Util.h
@@ -34,7 +34,7 @@ using namespace std;
class Util {
public:
- static void split(pair<string, string>& hp, string src, char delim);
+ static void split(pair<string, string>& hp, const string& src, char delim);
static string llitos(long long int value, bool comma = false);
static string itos(int value, bool comma = false);
/**
@@ -47,26 +47,27 @@ public:
* Take a string src which is a deliminated list and add its elements
* into result. result is not cleared before conversion begins.
*/
- static void slice(Strings& result, string src, char delim);
+ static void slice(Strings& result, const string& src, char delim);
- static string trim(string src);
+ static string trim(const string& src);
- static bool startsWith(string target, string part);
+ static bool startsWith(const string& target, const string& part);
- static bool endsWith(string target, string part);
+ static bool endsWith(const string& target, const string& part);
- static string replace(string target, string oldstr, string newstr);
+ static string replace(const string& target, const string& oldstr, const string& newstr);
static string urlencode(const unsigned char* target, int len);
static string toHex(const unsigned char* src, int len);
- static FILE* openFile(string filename, string mode);
+ static FILE* openFile(const string& filename, const string& mode);
- static void fileCopy(string destFile, string src);
+ static void fileCopy(const string& destFile, const string& src);
- static void rangedFileCopy(string destFile, string src, long long int srcOffset, long long int length);
+ static void rangedFileCopy(const string& destFile, const string& src, long long int srcOffset, long long int length);
+ static bool isPowerOf(int num, int base);
};
#endif // _D_UTIL_H_