* 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.
pull/1/head
Tatsuhiro Tsujikawa 2006-03-31 16:15:23 +00:00
parent 3d3363a544
commit fc2307d47c
12 changed files with 61 additions and 57 deletions

View File

@ -24,6 +24,11 @@
* src/SendMessageQueue.cc (cancelAllRequest): Fixed. * src/SendMessageQueue.cc (cancelAllRequest): Fixed.
* 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.
2006-03-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2006-03-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added new class SendMessageQueue that includes PendingMessages and Added new class SendMessageQueue that includes PendingMessages and

View File

@ -30,13 +30,13 @@ void CookieBox::add(const Cookie& cookie) {
cookies.push_back(cookie); cookies.push_back(cookie);
} }
void CookieBox::add(string cookieStr) { void CookieBox::add(const string& cookieStr) {
Cookie c; Cookie c;
parse(c, cookieStr); parse(c, cookieStr);
cookies.push_back(c); 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() && if(name.size() == string("secure").size() &&
strcasecmp(name.c_str(), "secure") == 0) { strcasecmp(name.c_str(), "secure") == 0) {
cookie.secure = true; 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(); cookie.clear();
Strings terms; Strings terms;
Util::slice(terms, cookieStr, ';'); 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; Cookies result;
for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) { for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
const Cookie& c = *itr; const Cookie& c = *itr;

View File

@ -37,7 +37,7 @@ public:
string domain; string domain;
bool secure; bool secure;
public: 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():secure(false) {}
~Cookie() {} ~Cookie() {}
string toString() const { string toString() const {
@ -54,15 +54,15 @@ typedef deque<Cookie> Cookies;
class CookieBox { class CookieBox {
private: private:
Cookies cookies; Cookies cookies;
void setField(Cookie& cookie, string name, string value) const; void setField(Cookie& cookie, const string& name, const string& value) const;
public: public:
CookieBox(); CookieBox();
~CookieBox(); ~CookieBox();
void clear(); void clear();
void add(const Cookie& cookie); void add(const Cookie& cookie);
void add(string cookieStr); void add(const string& cookieStr);
void parse(Cookie& cookie, string cookieStr) const; void parse(Cookie& cookie, const string& cookieStr) const;
Cookies criteriaFind(string host, string dir, bool secure) const; Cookies criteriaFind(const string& host, const string& dir, bool secure) const;
}; };
#endif // _D_COOKIE_BOX_H_ #endif // _D_COOKIE_BOX_H_

View File

@ -108,7 +108,7 @@ void FtpConnection::sendRetr() const {
socket->writeData(request); socket->writeData(request);
} }
int FtpConnection::getStatus(string response) const { int FtpConnection::getStatus(const string& response) const {
int status; int status;
// When the response is not like "%d %*s", // When the response is not like "%d %*s",
// we return 0. // 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) { if(response.size() <= 4) {
return false; return false;
} }

View File

@ -42,8 +42,8 @@ private:
string strbuf; string strbuf;
int getStatus(string response) const; int getStatus(const string& response) const;
bool isEndOfResponse(int status, string response) const; bool isEndOfResponse(int status, const string& response) const;
bool bulkReceiveResponse(pair<int, string>& response); bool bulkReceiveResponse(pair<int, string>& response);
public: public:
FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger); FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger);

View File

@ -45,9 +45,7 @@ public:
int cuid; int cuid;
private: private:
char peerId[PEER_ID_LENGTH]; char peerId[PEER_ID_LENGTH];
//unsigned char* bitfield;
BitfieldMan* bitfield; BitfieldMan* bitfield;
//int bitfieldLength;
long long int peerUpload; long long int peerUpload;
long long int peerDownload; long long int peerDownload;
int pieceLength; int pieceLength;
@ -58,7 +56,7 @@ public:
amChocking(true), amInterested(false), amChocking(true), amInterested(false),
peerChoking(true), peerInterested(false), peerChoking(true), peerInterested(false),
tryCount(0), error(0), cuid(0), tryCount(0), error(0), cuid(0),
bitfield(NULL),// bitfieldLength(0), bitfield(NULL),
peerUpload(0), peerDownload(0), peerUpload(0), peerDownload(0),
pieceLength(pieceLength), totalLength(totalLength) { pieceLength(pieceLength), totalLength(totalLength) {
this->bitfield = new BitfieldMan(pieceLength, totalLength); this->bitfield = new BitfieldMan(pieceLength, totalLength);
@ -66,7 +64,7 @@ public:
~Peer() { ~Peer() {
if(bitfield != NULL) { if(bitfield != NULL) {
delete /*[]*/ bitfield; delete bitfield;
} }
} }
@ -77,22 +75,10 @@ public:
void setBitfield(const unsigned char* bitfield, int bitfieldLength) { void setBitfield(const unsigned char* bitfield, int bitfieldLength) {
this->bitfield->setBitfield(bitfield, 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(); } const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
int getBitfieldLength() const { return bitfield->getBitfieldLength(); } int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
/*
void initBitfield(int pieces);
*/
/** /**
* operation = 1: set index-th bit 1 * operation = 1: set index-th bit 1
* operation = 0: set index-th bit 0 * operation = 0: set index-th bit 0

View File

@ -151,8 +151,13 @@ void PeerMessageUtil::checkPieceOffset(const PeerMessage* message, int pieceLeng
} }
void PeerMessageUtil::checkLength(const PeerMessage* message) { void PeerMessageUtil::checkLength(const PeerMessage* message) {
if(message->getLength() > 128*1024) { if(message->getLength() > MAX_BLOCK_LENGTH) {
throw new DlAbortEx("too large length %d > 128KB", message->getLength()); 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());
} }
} }

View File

@ -24,6 +24,8 @@
#include "PeerConnection.h" #include "PeerConnection.h"
#define MAX_BLOCK_LENGTH (128*1024)
class PeerMessageUtil { class PeerMessageUtil {
private: private:
PeerMessageUtil() {} PeerMessageUtil() {}

View File

@ -25,7 +25,7 @@
#include "BitfieldMan.h" #include "BitfieldMan.h"
#include "common.h" #include "common.h"
#define BLOCK_LENGTH 16*1024 #define BLOCK_LENGTH (16*1024)
class Piece { class Piece {
private: private:

View File

@ -35,18 +35,11 @@
using namespace std; using namespace std;
#define DEFAULT_BLOCK_LEN 16*1024;
#define MAX_BLOCK_LEN 128*1024;
#define INFO_HASH_LENGTH 20 #define INFO_HASH_LENGTH 20
#define IS_NULL_PIECE(X) (X.index == 0 && X.length == 0)
#define DEFAULT_ANNOUNCE_INTERVAL 300 #define DEFAULT_ANNOUNCE_INTERVAL 300
#define DEFAULT_ANNOUNCE_MIN_INTERVAL 300 #define DEFAULT_ANNOUNCE_MIN_INTERVAL 300
#define MAX_PEERS 55 #define MAX_PEERS 55
#define MAX_PEER_UPDATE 15 #define MAX_PEER_UPDATE 15
#define MAX_PEER_LIST_SIZE 250 #define MAX_PEER_LIST_SIZE 250
#define END_GAME_PIECE_NUM 20 #define END_GAME_PIECE_NUM 20
#define MAX_PEER_ERROR 5 #define MAX_PEER_ERROR 5

View File

@ -59,7 +59,7 @@ string Util::llitos(long long int value, bool comma)
return str; 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 sp = src.find_first_not_of(" ");
string::size_type ep = src.find_last_not_of(" "); string::size_type ep = src.find_last_not_of(" ");
if(sp == string::npos || ep == string::npos) { 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.first = "";
hp.second = ""; hp.second = "";
string::size_type p = src.find(delim); 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); 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; string::size_type p = 0;
while(1) { while(1) {
string::size_type np = src.find(delim, p); 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()) { if(target.size() < part.size()) {
return false; 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()) { if(target.size() < part.size()) {
return false; 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 == "" ) { if(target == "" || oldstr == "" ) {
return target; return target;
} }
@ -186,17 +186,17 @@ string Util::toHex(const unsigned char* src, int len) {
return hex; 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()); FILE* file = fopen(filename.c_str(), mode.c_str());
return file; return file;
} }
void Util::fileCopy(string dest, string src) { void Util::fileCopy(const string& dest, const string& src) {
File file(src); File file(src);
rangedFileCopy(dest, src, 0, file.size()); 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; int bufSize = 4096;
char buf[bufSize]; char buf[bufSize];
int destFd = -1; 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;
}

View File

@ -34,7 +34,7 @@ using namespace std;
class Util { class Util {
public: 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 llitos(long long int value, bool comma = false);
static string itos(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 * Take a string src which is a deliminated list and add its elements
* into result. result is not cleared before conversion begins. * 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 urlencode(const unsigned char* target, int len);
static string toHex(const unsigned char* src, 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_ #endif // _D_UTIL_H_