mirror of https://github.com/aria2/aria2
2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Use unsigned char for data store.pull/1/head
parent
7578d5fb22
commit
fc0fa4203a
|
@ -1,3 +1,7 @@
|
||||||
|
2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
|
Use unsigned char for data store.
|
||||||
|
|
||||||
2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
Bump up version number to 0.13.0+1
|
Bump up version number to 0.13.0+1
|
||||||
|
|
|
@ -91,7 +91,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* Processes the repsponse from the tracker.
|
* Processes the repsponse from the tracker.
|
||||||
*/
|
*/
|
||||||
virtual void processAnnounceResponse(const char* trackerResponse,
|
virtual void processAnnounceResponse(const unsigned char* trackerResponse,
|
||||||
size_t trackerResponseLength) = 0;
|
size_t trackerResponseLength) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -69,7 +69,7 @@ bool BtDependency::resolve()
|
||||||
DiskAdaptorHandle diskAdaptor = dependee->getPieceStorage()->getDiskAdaptor();
|
DiskAdaptorHandle diskAdaptor = dependee->getPieceStorage()->getDiskAdaptor();
|
||||||
diskAdaptor->openExistingFile();
|
diskAdaptor->openExistingFile();
|
||||||
std::string content = Util::toString(diskAdaptor);
|
std::string content = Util::toString(diskAdaptor);
|
||||||
btContext->loadFromMemory(content.c_str(), content.size(),
|
btContext->loadFromMemory(content,
|
||||||
File(dependee->getFilePath()).getBasename());
|
File(dependee->getFilePath()).getBasename());
|
||||||
if(_option->defined(PREF_PEER_ID_PREFIX)) {
|
if(_option->defined(PREF_PEER_ID_PREFIX)) {
|
||||||
btContext->setPeerIdPrefix(_option->get(PREF_PEER_ID_PREFIX));
|
btContext->setPeerIdPrefix(_option->get(PREF_PEER_ID_PREFIX));
|
||||||
|
|
|
@ -97,12 +97,12 @@ std::string BtExtendedMessage::toString() const {
|
||||||
BtExtendedMessageHandle
|
BtExtendedMessageHandle
|
||||||
BtExtendedMessage::create(const BtContextHandle& btContext,
|
BtExtendedMessage::create(const BtContextHandle& btContext,
|
||||||
const PeerHandle& peer,
|
const PeerHandle& peer,
|
||||||
const char* data, size_t dataLength)
|
const unsigned char* data, size_t dataLength)
|
||||||
{
|
{
|
||||||
if(dataLength < 2) {
|
if(dataLength < 2) {
|
||||||
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength);
|
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength);
|
||||||
}
|
}
|
||||||
int8_t id = PeerMessageUtil::getId((const unsigned char*)data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID);
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID);
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ public:
|
||||||
|
|
||||||
static BtExtendedMessageHandle create(const SharedHandle<BtContext>& btContext,
|
static BtExtendedMessageHandle create(const SharedHandle<BtContext>& btContext,
|
||||||
const SharedHandle<Peer>& peer,
|
const SharedHandle<Peer>& peer,
|
||||||
const char* data,
|
const unsigned char* data,
|
||||||
size_t dataLength);
|
size_t dataLength);
|
||||||
|
|
||||||
virtual int8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
|
@ -73,8 +73,7 @@ RequestGroups BtPostDownloadHandler::getNextRequestGroups(RequestGroup* requestG
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
DefaultBtContextHandle btContext = new DefaultBtContext();
|
DefaultBtContextHandle btContext = new DefaultBtContext();
|
||||||
btContext->loadFromMemory(content.c_str(), content.size(),
|
btContext->loadFromMemory(content, File(requestGroup->getFilePath()).getBasename());
|
||||||
File(requestGroup->getFilePath()).getBasename());
|
|
||||||
if(op->defined(PREF_PEER_ID_PREFIX)) {
|
if(op->defined(PREF_PEER_ID_PREFIX)) {
|
||||||
btContext->setPeerIdPrefix(op->get(PREF_PEER_ID_PREFIX));
|
btContext->setPeerIdPrefix(op->get(PREF_PEER_ID_PREFIX));
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ namespace aria2 {
|
||||||
|
|
||||||
ChunkedEncoding::ChunkedEncoding() {
|
ChunkedEncoding::ChunkedEncoding() {
|
||||||
strbufSize = 4096;
|
strbufSize = 4096;
|
||||||
strbuf = new char[strbufSize];
|
strbuf = new unsigned char[strbufSize];
|
||||||
strbufTail = strbuf;
|
strbufTail = strbuf;
|
||||||
state = READ_SIZE;
|
state = READ_SIZE;
|
||||||
chunkSize = 0;
|
chunkSize = 0;
|
||||||
|
@ -63,9 +63,10 @@ bool ChunkedEncoding::finished() {
|
||||||
|
|
||||||
void ChunkedEncoding::end() {}
|
void ChunkedEncoding::end() {}
|
||||||
|
|
||||||
void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen) {
|
void ChunkedEncoding::inflate(unsigned char* outbuf, int32_t& outlen,
|
||||||
|
const unsigned char* inbuf, int32_t inlen) {
|
||||||
addBuffer(inbuf, inlen);
|
addBuffer(inbuf, inlen);
|
||||||
char* p = strbuf;
|
unsigned char* p = strbuf;
|
||||||
int32_t clen = 0;
|
int32_t clen = 0;
|
||||||
while(1) {
|
while(1) {
|
||||||
if(state == READ_SIZE) {
|
if(state == READ_SIZE) {
|
||||||
|
@ -98,7 +99,7 @@ void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf,
|
||||||
} else {
|
} else {
|
||||||
// copy string between [p, strbufTail]
|
// copy string between [p, strbufTail]
|
||||||
int32_t unreadSize = strbufTail-p;
|
int32_t unreadSize = strbufTail-p;
|
||||||
char* temp = new char[strbufSize];
|
unsigned char* temp = new unsigned char[strbufSize];
|
||||||
memcpy(temp, p, unreadSize);
|
memcpy(temp, p, unreadSize);
|
||||||
delete [] strbuf;
|
delete [] strbuf;
|
||||||
strbuf = temp;
|
strbuf = temp;
|
||||||
|
@ -107,7 +108,10 @@ void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf,
|
||||||
outlen = clen;
|
outlen = clen;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ChunkedEncoding::readData(char** pp, char* buf, int32_t& len, int32_t maxlen) {
|
int32_t ChunkedEncoding::readData(unsigned char** pp,
|
||||||
|
unsigned char* buf, int32_t& len,
|
||||||
|
int32_t maxlen)
|
||||||
|
{
|
||||||
if(buf+len == buf+maxlen) {
|
if(buf+len == buf+maxlen) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -131,9 +135,9 @@ int32_t ChunkedEncoding::readData(char** pp, char* buf, int32_t& len, int32_t ma
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ChunkedEncoding::readDataEOL(char** pp) {
|
int32_t ChunkedEncoding::readDataEOL(unsigned char** pp) {
|
||||||
char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
|
unsigned char* np = reinterpret_cast<unsigned char*>(memchr(*pp, '\n', strbufTail-*pp));
|
||||||
char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
|
unsigned char* rp = reinterpret_cast<unsigned char*>(memchr(*pp, '\r', strbufTail-*pp));
|
||||||
if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
|
if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
|
||||||
*pp += 2;
|
*pp += 2;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -144,18 +148,18 @@ int32_t ChunkedEncoding::readDataEOL(char** pp) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ChunkedEncoding::readChunkSize(char** pp) {
|
int32_t ChunkedEncoding::readChunkSize(unsigned char** pp) {
|
||||||
// we read chunk-size from *pp
|
// we read chunk-size from *pp
|
||||||
char* p;
|
unsigned char* p;
|
||||||
char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
|
unsigned char* np = reinterpret_cast<unsigned char*>(memchr(*pp, '\n', strbufTail-*pp));
|
||||||
char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
|
unsigned char* rp = reinterpret_cast<unsigned char*>(memchr(*pp, '\r', strbufTail-*pp));
|
||||||
if(np == NULL || rp == NULL || np-rp != 1) {
|
if(np == NULL || rp == NULL || np-rp != 1) {
|
||||||
// \r\n is not found. Return -1
|
// \r\n is not found. Return -1
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
p = rp;
|
p = rp;
|
||||||
// We ignore chunk-extension
|
// We ignore chunk-extension
|
||||||
char* exsp = (char*)memchr(*pp, ';', strbufTail-*pp);
|
unsigned char* exsp = reinterpret_cast<unsigned char*>(memchr(*pp, ';', strbufTail-*pp));
|
||||||
if(exsp == 0 || p < exsp) {
|
if(exsp == 0 || p < exsp) {
|
||||||
exsp = p;
|
exsp = p;
|
||||||
}
|
}
|
||||||
|
@ -168,14 +172,14 @@ int32_t ChunkedEncoding::readChunkSize(char** pp) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChunkedEncoding::addBuffer(const char* inbuf, int32_t inlen) {
|
void ChunkedEncoding::addBuffer(const unsigned char* inbuf, int32_t inlen) {
|
||||||
int32_t realbufSize = strbufTail-strbuf;
|
int32_t realbufSize = strbufTail-strbuf;
|
||||||
if(realbufSize+inlen >= strbufSize) {
|
if(realbufSize+inlen >= strbufSize) {
|
||||||
if(realbufSize+inlen > MAX_BUFSIZE) {
|
if(realbufSize+inlen > MAX_BUFSIZE) {
|
||||||
throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
|
throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
|
||||||
}
|
}
|
||||||
strbufSize = realbufSize+inlen;
|
strbufSize = realbufSize+inlen;
|
||||||
char* temp = new char[strbufSize];
|
unsigned char* temp = new unsigned char[strbufSize];
|
||||||
memcpy(temp, strbuf, realbufSize);
|
memcpy(temp, strbuf, realbufSize);
|
||||||
delete [] strbuf;
|
delete [] strbuf;
|
||||||
strbuf = temp;
|
strbuf = temp;
|
||||||
|
|
|
@ -48,18 +48,19 @@ private:
|
||||||
};
|
};
|
||||||
int32_t chunkSize;
|
int32_t chunkSize;
|
||||||
int32_t state;
|
int32_t state;
|
||||||
char* strbuf;
|
unsigned char* strbuf;
|
||||||
int32_t strbufSize;
|
int32_t strbufSize;
|
||||||
char* strbufTail;
|
unsigned char* strbufTail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns 0 if the size of chunk is retrieved successfully,
|
* Returns 0 if the size of chunk is retrieved successfully,
|
||||||
* otherwise returns non-zero value.
|
* otherwise returns non-zero value.
|
||||||
*/
|
*/
|
||||||
int32_t readChunkSize(char** pp);
|
int32_t readChunkSize(unsigned char** pp);
|
||||||
int32_t readData(char** pp, char* buf, int32_t& len, int32_t maxlen);
|
int32_t readData(unsigned char** pp, unsigned char* buf, int32_t& len,
|
||||||
void addBuffer(const char* inbuf, int32_t inlen);
|
int32_t maxlen);
|
||||||
int32_t readDataEOL(char** pp);
|
void addBuffer(const unsigned char* inbuf, int32_t inlen);
|
||||||
|
int32_t readDataEOL(unsigned char** pp);
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -67,7 +68,8 @@ public:
|
||||||
~ChunkedEncoding();
|
~ChunkedEncoding();
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen);
|
void inflate(unsigned char* outbuf, int32_t& outlen,
|
||||||
|
const unsigned char* inbuf, int32_t inlen);
|
||||||
bool finished();
|
bool finished();
|
||||||
void end();
|
void end();
|
||||||
};
|
};
|
||||||
|
|
|
@ -66,7 +66,7 @@ std::string DHTAbstractMessage::getBencodedMessage()
|
||||||
void DHTAbstractMessage::send()
|
void DHTAbstractMessage::send()
|
||||||
{
|
{
|
||||||
std::string message = getBencodedMessage();
|
std::string message = getBencodedMessage();
|
||||||
_connection->sendMessage(message.c_str(),
|
_connection->sendMessage(reinterpret_cast<const unsigned char*>(message.c_str()),
|
||||||
message.size(),
|
message.size(),
|
||||||
_remoteNode->getIPAddress(),
|
_remoteNode->getIPAddress(),
|
||||||
_remoteNode->getPort());
|
_remoteNode->getPort());
|
||||||
|
|
|
@ -44,9 +44,9 @@ class DHTConnection {
|
||||||
public:
|
public:
|
||||||
virtual ~DHTConnection() {}
|
virtual ~DHTConnection() {}
|
||||||
|
|
||||||
virtual ssize_t receiveMessage(char* data, size_t len, std::string& host, uint16_t& port) = 0;
|
virtual ssize_t receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port) = 0;
|
||||||
|
|
||||||
virtual void sendMessage(const char* data, size_t len, const std::string& host, uint16_t port) = 0;
|
virtual void sendMessage(const unsigned char* data, size_t len, const std::string& host, uint16_t port) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -73,7 +73,7 @@ uint16_t DHTConnectionImpl::bind(uint16_t port)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t DHTConnectionImpl::receiveMessage(char* data, size_t len, std::string& host, uint16_t& port)
|
ssize_t DHTConnectionImpl::receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port)
|
||||||
{
|
{
|
||||||
if(_socket->isReadable(0)) {
|
if(_socket->isReadable(0)) {
|
||||||
std::pair<std::string, uint16_t> remoteHost;
|
std::pair<std::string, uint16_t> remoteHost;
|
||||||
|
@ -86,7 +86,7 @@ ssize_t DHTConnectionImpl::receiveMessage(char* data, size_t len, std::string& h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHTConnectionImpl::sendMessage(const char* data, size_t len, const std::string& host, uint16_t port)
|
void DHTConnectionImpl::sendMessage(const unsigned char* data, size_t len, const std::string& host, uint16_t port)
|
||||||
{
|
{
|
||||||
_socket->writeData(data, len, host, port);
|
_socket->writeData(data, len, host, port);
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,9 +58,9 @@ public:
|
||||||
|
|
||||||
uint16_t bind(uint16_t port);
|
uint16_t bind(uint16_t port);
|
||||||
|
|
||||||
virtual ssize_t receiveMessage(char* data, size_t len, std::string& host, uint16_t& port);
|
virtual ssize_t receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port);
|
||||||
|
|
||||||
virtual void sendMessage(const char* data, size_t len, const std::string& host, uint16_t port);
|
virtual void sendMessage(const unsigned char* data, size_t len, const std::string& host, uint16_t port);
|
||||||
|
|
||||||
SharedHandle<SocketCore> getSocket() const;
|
SharedHandle<SocketCore> getSocket() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -66,10 +66,10 @@ void DHTFindNodeReplyMessage::doReceivedAction()
|
||||||
Dictionary* DHTFindNodeReplyMessage::getResponse()
|
Dictionary* DHTFindNodeReplyMessage::getResponse()
|
||||||
{
|
{
|
||||||
Dictionary* a = new Dictionary();
|
Dictionary* a = new Dictionary();
|
||||||
a->put("id", new Data(reinterpret_cast<const char*>(_localNode->getID()),
|
a->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH));
|
||||||
DHT_ID_LENGTH));
|
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
char buffer[DHTBucket::K*26];
|
unsigned char buffer[DHTBucket::K*26];
|
||||||
|
// TODO if _closestKNodes.size() > DHTBucket::K ??
|
||||||
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
|
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
|
||||||
SharedHandle<DHTNode> node = *i;
|
SharedHandle<DHTNode> node = *i;
|
||||||
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
|
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
|
||||||
|
|
|
@ -67,22 +67,21 @@ void DHTGetPeersReplyMessage::doReceivedAction()
|
||||||
Dictionary* DHTGetPeersReplyMessage::getResponse()
|
Dictionary* DHTGetPeersReplyMessage::getResponse()
|
||||||
{
|
{
|
||||||
Dictionary* r = new Dictionary();
|
Dictionary* r = new Dictionary();
|
||||||
r->put("id", new Data(reinterpret_cast<const char*>(_localNode->getID()),
|
r->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH));
|
||||||
DHT_ID_LENGTH));
|
|
||||||
r->put("token", new Data(_token));
|
r->put("token", new Data(_token));
|
||||||
if(_values.size()) {
|
if(_values.size()) {
|
||||||
List* valuesList = new List();
|
List* valuesList = new List();
|
||||||
r->put("values", valuesList);
|
r->put("values", valuesList);
|
||||||
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin(); i != _values.end(); ++i) {
|
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin(); i != _values.end(); ++i) {
|
||||||
const SharedHandle<Peer>& peer = *i;
|
const SharedHandle<Peer>& peer = *i;
|
||||||
char buffer[6];
|
unsigned char buffer[6];
|
||||||
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
|
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
|
||||||
valuesList->add(new Data(buffer, sizeof(buffer)));
|
valuesList->add(new Data(buffer, sizeof(buffer)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
char buffer[DHTBucket::K*26];
|
unsigned char buffer[DHTBucket::K*26];
|
||||||
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
|
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
|
||||||
SharedHandle<DHTNode> node = *i;
|
SharedHandle<DHTNode> node = *i;
|
||||||
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
|
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
|
||||||
|
|
|
@ -52,7 +52,7 @@ DHTMessage::~DHTMessage() {}
|
||||||
|
|
||||||
void DHTMessage::generateTransactionID()
|
void DHTMessage::generateTransactionID()
|
||||||
{
|
{
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
_transactionID = std::string(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
_transactionID = std::string(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,8 +108,8 @@ public:
|
||||||
const std::string& transactionID) = 0;
|
const std::string& transactionID) = 0;
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createUnknownMessage(const char* data, size_t length, const std::string& ipaddr,
|
createUnknownMessage(const unsigned char* data, size_t length,
|
||||||
uint16_t port) = 0;
|
const std::string& ipaddr, uint16_t port) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -160,7 +160,7 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage(const Diction
|
||||||
}
|
}
|
||||||
const Data* id = getData(getDictionary(d, "a"), "id");
|
const Data* id = getData(getDictionary(d, "a"), "id");
|
||||||
validateID(id);
|
validateID(id);
|
||||||
SharedHandle<DHTNode> remoteNode = getRemoteNode((const unsigned char*)id->toString().c_str(), ipaddr, port);
|
SharedHandle<DHTNode> remoteNode = getRemoteNode(id->getData(), ipaddr, port);
|
||||||
std::string messageType = q->toString();
|
std::string messageType = q->toString();
|
||||||
std::string transactionID = t->toString();
|
std::string transactionID = t->toString();
|
||||||
if(messageType == "ping") {
|
if(messageType == "ping") {
|
||||||
|
@ -168,32 +168,29 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage(const Diction
|
||||||
} else if(messageType == "find_node") {
|
} else if(messageType == "find_node") {
|
||||||
const Data* targetNodeID = getData(a, "target");
|
const Data* targetNodeID = getData(a, "target");
|
||||||
validateID(targetNodeID);
|
validateID(targetNodeID);
|
||||||
return createFindNodeMessage(remoteNode,
|
return createFindNodeMessage(remoteNode, targetNodeID->getData(),
|
||||||
(const unsigned char*)targetNodeID->getData(),
|
|
||||||
transactionID);
|
transactionID);
|
||||||
} else if(messageType == "get_peers") {
|
} else if(messageType == "get_peers") {
|
||||||
const Data* infoHash = getData(a, "info_hash");
|
const Data* infoHash = getData(a, "info_hash");
|
||||||
validateID(infoHash);
|
validateID(infoHash);
|
||||||
return createGetPeersMessage(remoteNode,
|
return createGetPeersMessage(remoteNode,
|
||||||
(const unsigned char*)infoHash->getData(),
|
infoHash->getData(), transactionID);
|
||||||
transactionID);
|
|
||||||
} else if(messageType == "announce_peer") {
|
} else if(messageType == "announce_peer") {
|
||||||
const Data* infoHash = getData(a, "info_hash");
|
const Data* infoHash = getData(a, "info_hash");
|
||||||
validateID(infoHash);
|
validateID(infoHash);
|
||||||
const Data* port = getData(a, "port");
|
const Data* port = getData(a, "port");
|
||||||
validatePort(port);
|
validatePort(port);
|
||||||
const Data* token = getData(a, "token");
|
const Data* token = getData(a, "token");
|
||||||
return createAnnouncePeerMessage(remoteNode,
|
return createAnnouncePeerMessage(remoteNode, infoHash->getData(),
|
||||||
(const unsigned char*)infoHash->getData(),
|
static_cast<uint16_t>(port->toInt()),
|
||||||
(uint16_t)port->toInt(),
|
token->toString(), transactionID);
|
||||||
token->toString(),
|
|
||||||
transactionID);
|
|
||||||
} else {
|
} else {
|
||||||
throw new DlAbortEx("Unsupported message type: %s", messageType.c_str());
|
throw new DlAbortEx("Unsupported message type: %s", messageType.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<DHTMessage> DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
|
SharedHandle<DHTMessage>
|
||||||
|
DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
|
||||||
const Dictionary* d,
|
const Dictionary* d,
|
||||||
const SharedHandle<DHTNode>& remoteNode)
|
const SharedHandle<DHTNode>& remoteNode)
|
||||||
{
|
{
|
||||||
|
@ -217,11 +214,11 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createResponseMessage(const std:
|
||||||
const Dictionary* r = getDictionary(d, "r");
|
const Dictionary* r = getDictionary(d, "r");
|
||||||
const Data* id = getData(r, "id");
|
const Data* id = getData(r, "id");
|
||||||
validateID(id);
|
validateID(id);
|
||||||
validateIDMatch(remoteNode->getID(),
|
validateIDMatch(remoteNode->getID(), id->getData());
|
||||||
(const unsigned char*)id->toString().c_str());
|
|
||||||
std::string transactionID = t->toString();
|
std::string transactionID = t->toString();
|
||||||
if(messageType == "ping") {
|
if(messageType == "ping") {
|
||||||
return createPingReplyMessage(remoteNode, (const unsigned char*)id->getData(), transactionID);
|
return createPingReplyMessage(remoteNode,
|
||||||
|
id->getData(), transactionID);
|
||||||
} else if(messageType == "find_node") {
|
} else if(messageType == "find_node") {
|
||||||
return createFindNodeReplyMessage(remoteNode, d, transactionID);
|
return createFindNodeReplyMessage(remoteNode, d, transactionID);
|
||||||
} else if(messageType == "get_peers") {
|
} else if(messageType == "get_peers") {
|
||||||
|
@ -289,14 +286,15 @@ DHTMessageFactoryImpl::createFindNodeReplyMessage(const SharedHandle<DHTNode>& r
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::deque<SharedHandle<DHTNode> > DHTMessageFactoryImpl::extractNodes(const char* src, size_t length)
|
std::deque<SharedHandle<DHTNode> >
|
||||||
|
DHTMessageFactoryImpl::extractNodes(const unsigned char* src, size_t length)
|
||||||
{
|
{
|
||||||
if(length%26 != 0) {
|
if(length%26 != 0) {
|
||||||
throw new DlAbortEx("Nodes length is not multiple of 26");
|
throw new DlAbortEx("Nodes length is not multiple of 26");
|
||||||
}
|
}
|
||||||
std::deque<SharedHandle<DHTNode> > nodes;
|
std::deque<SharedHandle<DHTNode> > nodes;
|
||||||
for(size_t offset = 0; offset < length; offset += 26) {
|
for(size_t offset = 0; offset < length; offset += 26) {
|
||||||
SharedHandle<DHTNode> node = new DHTNode(reinterpret_cast<const unsigned char*>(src+offset));
|
SharedHandle<DHTNode> node = new DHTNode(src+offset);
|
||||||
std::pair<std::string, uint16_t> addr =
|
std::pair<std::string, uint16_t> addr =
|
||||||
PeerMessageUtil::unpackcompact(src+offset+DHT_ID_LENGTH);
|
PeerMessageUtil::unpackcompact(src+offset+DHT_ID_LENGTH);
|
||||||
if(addr.first.empty()) {
|
if(addr.first.empty()) {
|
||||||
|
@ -419,7 +417,7 @@ DHTMessageFactoryImpl::createAnnouncePeerReplyMessage(const SharedHandle<DHTNode
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
DHTMessageFactoryImpl::createUnknownMessage(const char* data, size_t length,
|
DHTMessageFactoryImpl::createUnknownMessage(const unsigned char* data, size_t length,
|
||||||
const std::string& ipaddr, uint16_t port)
|
const std::string& ipaddr, uint16_t port)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
@ -74,7 +74,7 @@ private:
|
||||||
|
|
||||||
void validatePort(const Data* i) const;
|
void validatePort(const Data* i) const;
|
||||||
|
|
||||||
std::deque<SharedHandle<DHTNode> > extractNodes(const char* src, size_t length);
|
std::deque<SharedHandle<DHTNode> > extractNodes(const unsigned char* src, size_t length);
|
||||||
|
|
||||||
void setCommonProperty(const SharedHandle<DHTAbstractMessage>& m);
|
void setCommonProperty(const SharedHandle<DHTAbstractMessage>& m);
|
||||||
|
|
||||||
|
@ -156,8 +156,8 @@ public:
|
||||||
const std::string& transactionID);
|
const std::string& transactionID);
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createUnknownMessage(const char* data, size_t length, const std::string& ipaddr,
|
createUnknownMessage(const unsigned char* data, size_t length,
|
||||||
uint16_t port);
|
const std::string& ipaddr, uint16_t port);
|
||||||
|
|
||||||
void setRoutingTable(const WeakHandle<DHTRoutingTable>& routingTable);
|
void setRoutingTable(const WeakHandle<DHTRoutingTable>& routingTable);
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
|
||||||
{
|
{
|
||||||
std::string remoteAddr;
|
std::string remoteAddr;
|
||||||
uint16_t remotePort;
|
uint16_t remotePort;
|
||||||
char data[64*1024];
|
unsigned char data[64*1024];
|
||||||
ssize_t length = _connection->receiveMessage(data, sizeof(data),
|
ssize_t length = _connection->receiveMessage(data, sizeof(data),
|
||||||
remoteAddr,
|
remoteAddr,
|
||||||
remotePort);
|
remotePort);
|
||||||
|
@ -126,7 +126,8 @@ void DHTMessageReceiver::handleTimeout()
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
DHTMessageReceiver::handleUnknownMessage(const char* data, size_t length,
|
DHTMessageReceiver::handleUnknownMessage(const unsigned char* data,
|
||||||
|
size_t length,
|
||||||
const std::string& remoteAddr,
|
const std::string& remoteAddr,
|
||||||
uint16_t remotePort)
|
uint16_t remotePort)
|
||||||
{
|
{
|
||||||
|
|
|
@ -61,7 +61,7 @@ private:
|
||||||
const Logger* _logger;
|
const Logger* _logger;
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
handleUnknownMessage(const char* data, size_t length,
|
handleUnknownMessage(const unsigned char* data, size_t length,
|
||||||
const std::string& remoteAddr, uint16_t remotePort);
|
const std::string& remoteAddr, uint16_t remotePort);
|
||||||
public:
|
public:
|
||||||
DHTMessageReceiver(const SharedHandle<DHTMessageTracker>& tracker);
|
DHTMessageReceiver(const SharedHandle<DHTMessageTracker>& tracker);
|
||||||
|
|
|
@ -126,7 +126,8 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
in.read(buf, 42);
|
in.read(buf, 42);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
|
std::pair<std::string, uint16_t> peer =
|
||||||
|
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
|
||||||
if(peer.first.empty()) {
|
if(peer.first.empty()) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
in.read(buf, 42);
|
in.read(buf, 42);
|
||||||
|
|
|
@ -101,7 +101,7 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
|
||||||
const SharedHandle<DHTNode>& node = *i;
|
const SharedHandle<DHTNode>& node = *i;
|
||||||
// Currently, only IPv4 address and IPv4-mapped address are saved.
|
// Currently, only IPv4 address and IPv4-mapped address are saved.
|
||||||
// 6bytes: write IP address + port in Compact IP-address/port info form.
|
// 6bytes: write IP address + port in Compact IP-address/port info form.
|
||||||
char compactPeer[6];
|
unsigned char compactPeer[6];
|
||||||
if(!PeerMessageUtil::createcompact(compactPeer, node->getIPAddress(), node->getPort())) {
|
if(!PeerMessageUtil::createcompact(compactPeer, node->getIPAddress(), node->getPort())) {
|
||||||
memset(compactPeer, 0, 6);
|
memset(compactPeer, 0, 6);
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
|
||||||
// 7bytes reserved
|
// 7bytes reserved
|
||||||
o.write(zero, 7);
|
o.write(zero, 7);
|
||||||
// 6 bytes compact peer
|
// 6 bytes compact peer
|
||||||
o.write(compactPeer, 6);
|
o.write(reinterpret_cast<const char*>(compactPeer), 6);
|
||||||
// 2bytes reserved
|
// 2bytes reserved
|
||||||
o.write(zero, 2);
|
o.write(zero, 2);
|
||||||
// 16bytes reserved
|
// 16bytes reserved
|
||||||
|
|
|
@ -48,7 +48,7 @@ DHTTokenTracker::DHTTokenTracker()
|
||||||
memcpy(_secret[1], _secret[0], SECRET_SIZE);
|
memcpy(_secret[1], _secret[0], SECRET_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
DHTTokenTracker::DHTTokenTracker(const char* initialSecret)
|
DHTTokenTracker::DHTTokenTracker(const unsigned char* initialSecret)
|
||||||
{
|
{
|
||||||
memcpy(_secret[0], initialSecret, SECRET_SIZE);
|
memcpy(_secret[0], initialSecret, SECRET_SIZE);
|
||||||
memcpy(_secret[1], initialSecret, SECRET_SIZE);
|
memcpy(_secret[1], initialSecret, SECRET_SIZE);
|
||||||
|
@ -58,9 +58,9 @@ DHTTokenTracker::~DHTTokenTracker() {}
|
||||||
|
|
||||||
std::string DHTTokenTracker::generateToken(const unsigned char* infoHash,
|
std::string DHTTokenTracker::generateToken(const unsigned char* infoHash,
|
||||||
const std::string& ipaddr, uint16_t port,
|
const std::string& ipaddr, uint16_t port,
|
||||||
const char* secret) const
|
const unsigned char* secret) const
|
||||||
{
|
{
|
||||||
char src[DHT_ID_LENGTH+6+SECRET_SIZE];
|
unsigned char src[DHT_ID_LENGTH+6+SECRET_SIZE];
|
||||||
if(!PeerMessageUtil::createcompact(src+DHT_ID_LENGTH, ipaddr, port)) {
|
if(!PeerMessageUtil::createcompact(src+DHT_ID_LENGTH, ipaddr, port)) {
|
||||||
throw new DlAbortEx("Token generation failed: ipaddr=%s, port=%u",
|
throw new DlAbortEx("Token generation failed: ipaddr=%s, port=%u",
|
||||||
ipaddr.c_str(), port);
|
ipaddr.c_str(), port);
|
||||||
|
|
|
@ -44,15 +44,15 @@ class DHTTokenTracker {
|
||||||
private:
|
private:
|
||||||
static const size_t SECRET_SIZE = 4;
|
static const size_t SECRET_SIZE = 4;
|
||||||
|
|
||||||
char _secret[2][SECRET_SIZE];
|
unsigned char _secret[2][SECRET_SIZE];
|
||||||
|
|
||||||
std::string generateToken(const unsigned char* infoHash,
|
std::string generateToken(const unsigned char* infoHash,
|
||||||
const std::string& ipaddr, uint16_t port,
|
const std::string& ipaddr, uint16_t port,
|
||||||
const char* secret) const;
|
const unsigned char* secret) const;
|
||||||
public:
|
public:
|
||||||
DHTTokenTracker();
|
DHTTokenTracker();
|
||||||
|
|
||||||
DHTTokenTracker(const char* initialSecret);
|
DHTTokenTracker(const unsigned char* initialSecret);
|
||||||
|
|
||||||
~DHTTokenTracker();
|
~DHTTokenTracker();
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
DHTUnknownMessage::DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
|
DHTUnknownMessage::DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
|
||||||
const char* data, size_t length,
|
const unsigned char* data, size_t length,
|
||||||
const std::string& ipaddr, uint16_t port):
|
const std::string& ipaddr, uint16_t port):
|
||||||
DHTMessage(localNode, 0),
|
DHTMessage(localNode, 0),
|
||||||
_length(length),
|
_length(length),
|
||||||
|
@ -50,7 +50,7 @@ DHTUnknownMessage::DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
|
||||||
if(_length == 0) {
|
if(_length == 0) {
|
||||||
_data = 0;
|
_data = 0;
|
||||||
} else {
|
} else {
|
||||||
_data = new char[length];
|
_data = new unsigned char[length];
|
||||||
memcpy(_data, data, length);
|
memcpy(_data, data, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,14 +41,14 @@ namespace aria2 {
|
||||||
|
|
||||||
class DHTUnknownMessage:public DHTMessage {
|
class DHTUnknownMessage:public DHTMessage {
|
||||||
private:
|
private:
|
||||||
char* _data;
|
unsigned char* _data;
|
||||||
size_t _length;
|
size_t _length;
|
||||||
std::string _ipaddr;
|
std::string _ipaddr;
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
public:
|
public:
|
||||||
// _remoteNode is always null
|
// _remoteNode is always null
|
||||||
DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
|
DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
|
||||||
const char* data, size_t length,
|
const unsigned char* data, size_t length,
|
||||||
const std::string& ipaddr, uint16_t port);
|
const std::string& ipaddr, uint16_t port);
|
||||||
|
|
||||||
virtual ~DHTUnknownMessage();
|
virtual ~DHTUnknownMessage();
|
||||||
|
|
|
@ -48,7 +48,7 @@ namespace aria2 {
|
||||||
|
|
||||||
void DHTUtil::generateRandomKey(unsigned char* key)
|
void DHTUtil::generateRandomKey(unsigned char* key)
|
||||||
{
|
{
|
||||||
char bytes[40];
|
unsigned char bytes[40];
|
||||||
generateRandomData(bytes, sizeof(bytes));
|
generateRandomData(bytes, sizeof(bytes));
|
||||||
MessageDigestHelper::digest(key, 20, "sha1", bytes, sizeof(bytes));
|
MessageDigestHelper::digest(key, 20, "sha1", bytes, sizeof(bytes));
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,11 +47,6 @@ public:
|
||||||
|
|
||||||
static void generateRandomData(unsigned char* data, size_t length);
|
static void generateRandomData(unsigned char* data, size_t length);
|
||||||
|
|
||||||
static void generateRandomData(char* data, size_t length)
|
|
||||||
{
|
|
||||||
return generateRandomData(reinterpret_cast<unsigned char*>(data), length);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flipBit(unsigned char* data, size_t length, size_t bitIndex);
|
static void flipBit(unsigned char* data, size_t length, size_t bitIndex);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
15
src/Data.cc
15
src/Data.cc
|
@ -38,23 +38,24 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
Data::Data(const char* data, int32_t len, bool number):number(number) {
|
Data::Data(const unsigned char* data, int32_t len, bool number):number(number)
|
||||||
|
{
|
||||||
init(data, len);
|
init(data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
Data::Data(const unsigned char* data, int32_t len, bool number):number(number) {
|
Data::Data(const char* data, int32_t len, bool number):number(number) {
|
||||||
init(reinterpret_cast<const char*>(data), len);
|
init(reinterpret_cast<const unsigned char*>(data), len);
|
||||||
}
|
}
|
||||||
|
|
||||||
Data::Data(const std::string& data, bool number):number(number)
|
Data::Data(const std::string& data, bool number):number(number)
|
||||||
{
|
{
|
||||||
init(data.c_str(), data.size());
|
init(reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Data::init(const char* src, int32_t slen)
|
void Data::init(const unsigned char* src, int32_t slen)
|
||||||
{
|
{
|
||||||
if(src) {
|
if(src) {
|
||||||
data = new char[slen];
|
data = new unsigned char[slen];
|
||||||
memcpy(data, src, slen);
|
memcpy(data, src, slen);
|
||||||
len = slen;
|
len = slen;
|
||||||
} else {
|
} else {
|
||||||
|
@ -71,7 +72,7 @@ std::string Data::toString() const {
|
||||||
return std::string(&data[0], &data[len]);
|
return std::string(&data[0], &data[len]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Data::getData() const {
|
const unsigned char* Data::getData() const {
|
||||||
if(this->len == 0) {
|
if(this->len == 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -43,10 +43,10 @@ namespace aria2 {
|
||||||
class Data : public MetaEntry {
|
class Data : public MetaEntry {
|
||||||
private:
|
private:
|
||||||
int32_t len;
|
int32_t len;
|
||||||
char* data;
|
unsigned char* data;
|
||||||
bool number;
|
bool number;
|
||||||
|
|
||||||
void init(const char* data, int32_t len);
|
void init(const unsigned char* data, int32_t len);
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* This class stores the copy of data. So caller must take care of freeing
|
* This class stores the copy of data. So caller must take care of freeing
|
||||||
|
@ -64,7 +64,7 @@ public:
|
||||||
int32_t toInt() const;
|
int32_t toInt() const;
|
||||||
int64_t toLLInt() const;
|
int64_t toLLInt() const;
|
||||||
|
|
||||||
const char* getData() const;
|
const unsigned char* getData() const;
|
||||||
int32_t getLen() const;
|
int32_t getLen() const;
|
||||||
bool isNumber() const;
|
bool isNumber() const;
|
||||||
|
|
||||||
|
|
|
@ -158,8 +158,7 @@ std::string DefaultBtAnnounce::getAnnounceUrl() {
|
||||||
url += std::string("&")+"event="+event;
|
url += std::string("&")+"event="+event;
|
||||||
}
|
}
|
||||||
if(!trackerId.empty()) {
|
if(!trackerId.empty()) {
|
||||||
url += std::string("&")+"trackerid="+Util::torrentUrlencode((const unsigned char*)trackerId.c_str(),
|
url += std::string("&")+"trackerid="+Util::torrentUrlencode(trackerId);
|
||||||
trackerId.size());
|
|
||||||
}
|
}
|
||||||
if(option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
|
if(option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
|
||||||
url += "&requirecrypto=1";
|
url += "&requirecrypto=1";
|
||||||
|
@ -193,7 +192,7 @@ void DefaultBtAnnounce::resetAnnounce() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
DefaultBtAnnounce::processAnnounceResponse(const char* trackerResponse,
|
DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
|
||||||
size_t trackerResponseLength)
|
size_t trackerResponseLength)
|
||||||
{
|
{
|
||||||
SharedHandle<MetaEntry> entry(MetaFileUtil::bdecoding(trackerResponse,
|
SharedHandle<MetaEntry> entry(MetaFileUtil::bdecoding(trackerResponse,
|
||||||
|
|
|
@ -107,7 +107,7 @@ public:
|
||||||
|
|
||||||
virtual void resetAnnounce();
|
virtual void resetAnnounce();
|
||||||
|
|
||||||
virtual void processAnnounceResponse(const char* trackerResponse,
|
virtual void processAnnounceResponse(const unsigned char* trackerResponse,
|
||||||
size_t trackerResponseLength);
|
size_t trackerResponseLength);
|
||||||
|
|
||||||
virtual bool noMoreAnnounce();
|
virtual bool noMoreAnnounce();
|
||||||
|
|
|
@ -258,7 +258,9 @@ void DefaultBtContext::extractNodes(const List* nodes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::loadFromMemory(const char* content, int32_t length, const std::string& defaultName)
|
void DefaultBtContext::loadFromMemory(const unsigned char* content,
|
||||||
|
int32_t length,
|
||||||
|
const std::string& defaultName)
|
||||||
{
|
{
|
||||||
SharedHandle<MetaEntry> rootEntry = MetaFileUtil::bdecoding(content, length);
|
SharedHandle<MetaEntry> rootEntry = MetaFileUtil::bdecoding(content, length);
|
||||||
const Dictionary* rootDic = dynamic_cast<const Dictionary*>(rootEntry.get());
|
const Dictionary* rootDic = dynamic_cast<const Dictionary*>(rootEntry.get());
|
||||||
|
@ -310,8 +312,7 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
|
||||||
}
|
}
|
||||||
pieceLength = pieceLengthData->toInt();
|
pieceLength = pieceLengthData->toInt();
|
||||||
// retrieve piece hashes
|
// retrieve piece hashes
|
||||||
extractPieceHash((unsigned char*)pieceHashData->getData(),
|
extractPieceHash(pieceHashData->getData(), pieceHashData->getLen(),
|
||||||
pieceHashData->getLen(),
|
|
||||||
PIECE_HASH_LENGTH);
|
PIECE_HASH_LENGTH);
|
||||||
const Data* privateFlag = dynamic_cast<const Data*>(infoDic->get("private"));
|
const Data* privateFlag = dynamic_cast<const Data*>(infoDic->get("private"));
|
||||||
if(privateFlag) {
|
if(privateFlag) {
|
||||||
|
@ -386,7 +387,7 @@ std::string DefaultBtContext::getActualBasePath() const
|
||||||
std::deque<int32_t> DefaultBtContext::computeFastSet(const std::string& ipaddr, int32_t fastSetSize)
|
std::deque<int32_t> DefaultBtContext::computeFastSet(const std::string& ipaddr, int32_t fastSetSize)
|
||||||
{
|
{
|
||||||
std::deque<int32_t> fastSet;
|
std::deque<int32_t> fastSet;
|
||||||
char compact[6];
|
unsigned char compact[6];
|
||||||
if(!PeerMessageUtil::createcompact(compact, ipaddr, 0)) {
|
if(!PeerMessageUtil::createcompact(compact, ipaddr, 0)) {
|
||||||
return fastSet;
|
return fastSet;
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,14 @@ private:
|
||||||
|
|
||||||
virtual void load(const std::string& torrentFile);
|
virtual void load(const std::string& torrentFile);
|
||||||
|
|
||||||
void loadFromMemory(const char* content, int32_t length, const std::string& defaultName);
|
void loadFromMemory(const unsigned char* content, int32_t length,
|
||||||
|
const std::string& defaultName);
|
||||||
|
|
||||||
|
void loadFromMemory(const std::string& context, const std::string& defaultName)
|
||||||
|
{
|
||||||
|
loadFromMemory(reinterpret_cast<const unsigned char*>(context.c_str()),
|
||||||
|
context.size(), defaultName);
|
||||||
|
}
|
||||||
|
|
||||||
virtual std::string getName() const;
|
virtual std::string getName() const;
|
||||||
|
|
||||||
|
@ -133,7 +140,7 @@ private:
|
||||||
if(peerId == "") {
|
if(peerId == "") {
|
||||||
peerId = generatePeerId();
|
peerId = generatePeerId();
|
||||||
}
|
}
|
||||||
return (const unsigned char*)peerId.c_str();
|
return reinterpret_cast<const unsigned char*>(peerId.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::deque<int32_t> computeFastSet(const std::string& ipaddr, int32_t fastSetSize);
|
virtual std::deque<int32_t> computeFastSet(const std::string& ipaddr, int32_t fastSetSize);
|
||||||
|
|
|
@ -187,7 +187,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, int32_t data
|
||||||
}
|
}
|
||||||
case BtExtendedMessage::ID: {
|
case BtExtendedMessage::ID: {
|
||||||
if(peer->isExtendedMessagingEnabled()) {
|
if(peer->isExtendedMessagingEnabled()) {
|
||||||
msg = BtExtendedMessage::create(btContext, peer, (const char*)data, dataLength);
|
msg = BtExtendedMessage::create(btContext, peer, data, dataLength);
|
||||||
} else {
|
} else {
|
||||||
throw new DlAbortEx("Received extended message from peer during a session with extended messaging disabled.");
|
throw new DlAbortEx("Received extended message from peer during a session with extended messaging disabled.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ DefaultExtensionMessageFactory::DefaultExtensionMessageFactory(const BtContextHa
|
||||||
DefaultExtensionMessageFactory::~DefaultExtensionMessageFactory() {}
|
DefaultExtensionMessageFactory::~DefaultExtensionMessageFactory() {}
|
||||||
|
|
||||||
ExtensionMessageHandle
|
ExtensionMessageHandle
|
||||||
DefaultExtensionMessageFactory::createMessage(const char* data, size_t length)
|
DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t length)
|
||||||
{
|
{
|
||||||
uint8_t extensionMessageID = *data;
|
uint8_t extensionMessageID = *data;
|
||||||
if(extensionMessageID == 0) {
|
if(extensionMessageID == 0) {
|
||||||
|
|
|
@ -60,7 +60,7 @@ public:
|
||||||
virtual ~DefaultExtensionMessageFactory();
|
virtual ~DefaultExtensionMessageFactory();
|
||||||
|
|
||||||
virtual SharedHandle<ExtensionMessage>
|
virtual SharedHandle<ExtensionMessage>
|
||||||
createMessage(const char* data, size_t length);
|
createMessage(const unsigned char* data, size_t length);
|
||||||
|
|
||||||
void setBtContext(const SharedHandle<BtContext>& btContext);
|
void setBtContext(const SharedHandle<BtContext>& btContext);
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ bool DownloadCommand::executeInternal() {
|
||||||
SegmentHandle segment = _segments.front();
|
SegmentHandle segment = _segments.front();
|
||||||
|
|
||||||
int32_t BUFSIZE = 16*1024;
|
int32_t BUFSIZE = 16*1024;
|
||||||
char buf[BUFSIZE];
|
unsigned char buf[BUFSIZE];
|
||||||
int32_t bufSize;
|
int32_t bufSize;
|
||||||
if(segment->getLength() > 0 && segment->getLength()-segment->getWrittenLength() < BUFSIZE) {
|
if(segment->getLength() > 0 && segment->getLength()-segment->getWrittenLength() < BUFSIZE) {
|
||||||
bufSize = segment->getLength()-segment->getWrittenLength();
|
bufSize = segment->getLength()-segment->getWrittenLength();
|
||||||
|
@ -120,7 +120,7 @@ bool DownloadCommand::executeInternal() {
|
||||||
socket->readData(buf, bufSize);
|
socket->readData(buf, bufSize);
|
||||||
|
|
||||||
if(transferDecoder.isNull()) {
|
if(transferDecoder.isNull()) {
|
||||||
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData((const unsigned char*)buf, bufSize,
|
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData(buf, bufSize,
|
||||||
segment->getPositionToWrite());
|
segment->getPositionToWrite());
|
||||||
//logger->debug("bufSize = %d, posToWrite = %lld", bufSize, segment->getPositionToWrite());
|
//logger->debug("bufSize = %d, posToWrite = %lld", bufSize, segment->getPositionToWrite());
|
||||||
segment->updateWrittenLength(bufSize);
|
segment->updateWrittenLength(bufSize);
|
||||||
|
@ -131,9 +131,9 @@ bool DownloadCommand::executeInternal() {
|
||||||
peerStat->updateDownloadLength(bufSize);
|
peerStat->updateDownloadLength(bufSize);
|
||||||
} else {
|
} else {
|
||||||
int32_t infbufSize = 16*1024;
|
int32_t infbufSize = 16*1024;
|
||||||
char infbuf[infbufSize];
|
unsigned char infbuf[infbufSize];
|
||||||
transferDecoder->inflate(infbuf, infbufSize, buf, bufSize);
|
transferDecoder->inflate(infbuf, infbufSize, buf, bufSize);
|
||||||
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData((const unsigned char*)infbuf, infbufSize,
|
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData(infbuf, infbufSize,
|
||||||
segment->getPositionToWrite());
|
segment->getPositionToWrite());
|
||||||
segment->updateWrittenLength(infbufSize);
|
segment->updateWrittenLength(infbufSize);
|
||||||
//segment->writtenLength += infbufSize;
|
//segment->writtenLength += infbufSize;
|
||||||
|
|
|
@ -47,7 +47,7 @@ public:
|
||||||
virtual ~ExtensionMessageFactory() {}
|
virtual ~ExtensionMessageFactory() {}
|
||||||
|
|
||||||
virtual SharedHandle<ExtensionMessage>
|
virtual SharedHandle<ExtensionMessage>
|
||||||
createMessage(const char* data, size_t length) = 0;
|
createMessage(const unsigned char* data, size_t length) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<ExtensionMessageFactory> ExtensionMessageFactoryHandle;
|
typedef SharedHandle<ExtensionMessageFactory> ExtensionMessageFactoryHandle;
|
||||||
|
|
|
@ -132,7 +132,7 @@ uint8_t HandshakeExtensionMessage::getExtensionMessageID(const std::string& name
|
||||||
}
|
}
|
||||||
|
|
||||||
HandshakeExtensionMessageHandle
|
HandshakeExtensionMessageHandle
|
||||||
HandshakeExtensionMessage::create(const char* data, size_t length)
|
HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
|
||||||
{
|
{
|
||||||
if(length < 1) {
|
if(length < 1) {
|
||||||
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,
|
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,
|
||||||
|
@ -140,7 +140,7 @@ HandshakeExtensionMessage::create(const char* data, size_t length)
|
||||||
}
|
}
|
||||||
HandshakeExtensionMessageHandle msg = new HandshakeExtensionMessage();
|
HandshakeExtensionMessageHandle msg = new HandshakeExtensionMessage();
|
||||||
msg->_logger->debug("Creating HandshakeExtensionMessage from %s",
|
msg->_logger->debug("Creating HandshakeExtensionMessage from %s",
|
||||||
Util::urlencode((const unsigned char*)data, length).c_str());
|
Util::urlencode(data, length).c_str());
|
||||||
SharedHandle<MetaEntry> root = MetaFileUtil::bdecoding(data+1, length-1);
|
SharedHandle<MetaEntry> root = MetaFileUtil::bdecoding(data+1, length-1);
|
||||||
Dictionary* d = dynamic_cast<Dictionary*>(root.get());
|
Dictionary* d = dynamic_cast<Dictionary*>(root.get());
|
||||||
if(d == 0) {
|
if(d == 0) {
|
||||||
|
|
|
@ -120,9 +120,8 @@ public:
|
||||||
|
|
||||||
void setBtContext(const SharedHandle<BtContext>& btContext);
|
void setBtContext(const SharedHandle<BtContext>& btContext);
|
||||||
|
|
||||||
static HandshakeExtensionMessageHandle create(const char* data,
|
static HandshakeExtensionMessageHandle create(const unsigned char* data,
|
||||||
size_t dataLength);
|
size_t dataLength);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<HandshakeExtensionMessage> HandshakeExtensionMessageHandle;
|
typedef SharedHandle<HandshakeExtensionMessage> HandshakeExtensionMessageHandle;
|
||||||
|
|
|
@ -117,7 +117,7 @@ HttpResponseHandle HttpConnection::receiveResponse()
|
||||||
HttpRequestEntryHandle entry = outstandingHttpRequests.front();
|
HttpRequestEntryHandle entry = outstandingHttpRequests.front();
|
||||||
HttpHeaderProcessorHandle proc = entry->getHttpHeaderProcessor();
|
HttpHeaderProcessorHandle proc = entry->getHttpHeaderProcessor();
|
||||||
|
|
||||||
char buf[512];
|
unsigned char buf[512];
|
||||||
int32_t size = sizeof(buf);
|
int32_t size = sizeof(buf);
|
||||||
socket->peekData(buf, size);
|
socket->peekData(buf, size);
|
||||||
if(size == 0) {
|
if(size == 0) {
|
||||||
|
|
|
@ -45,10 +45,10 @@ HttpHeaderProcessor::HttpHeaderProcessor():_limit(4096) {}
|
||||||
|
|
||||||
HttpHeaderProcessor::~HttpHeaderProcessor() {}
|
HttpHeaderProcessor::~HttpHeaderProcessor() {}
|
||||||
|
|
||||||
void HttpHeaderProcessor::update(const char* data, int32_t length)
|
void HttpHeaderProcessor::update(const unsigned char* data, int32_t length)
|
||||||
{
|
{
|
||||||
checkHeaderLimit(length);
|
checkHeaderLimit(length);
|
||||||
strm.write(data, length);
|
strm.write(reinterpret_cast<const char*>(data), length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpHeaderProcessor::update(const std::string& data)
|
void HttpHeaderProcessor::update(const std::string& data)
|
||||||
|
|
|
@ -56,7 +56,7 @@ public:
|
||||||
|
|
||||||
~HttpHeaderProcessor();
|
~HttpHeaderProcessor();
|
||||||
|
|
||||||
void update(const char* data, int32_t length);
|
void update(const unsigned char* data, int32_t length);
|
||||||
|
|
||||||
void update(const std::string& data);
|
void update(const std::string& data);
|
||||||
|
|
||||||
|
|
|
@ -45,11 +45,11 @@ namespace aria2 {
|
||||||
|
|
||||||
MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
|
MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
|
||||||
File f(file);
|
File f(file);
|
||||||
int32_t len = f.size();
|
size_t len = f.size();
|
||||||
char* buf = new char[len];
|
unsigned char* buf = new unsigned char[len];
|
||||||
FILE* fp = fopen(file.c_str(), "r+b");
|
FILE* fp = fopen(file.c_str(), "r+b");
|
||||||
try {
|
try {
|
||||||
if(fp == NULL) {
|
if(!fp) {
|
||||||
throw new DlAbortEx("cannot open metainfo file");
|
throw new DlAbortEx("cannot open metainfo file");
|
||||||
}
|
}
|
||||||
if(fread(buf, len, 1, fp) != 1) {
|
if(fread(buf, len, 1, fp) != 1) {
|
||||||
|
@ -57,35 +57,29 @@ MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
|
||||||
throw new DlAbortEx("cannot read metainfo");
|
throw new DlAbortEx("cannot read metainfo");
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
fp = NULL;
|
fp = 0;
|
||||||
MetaEntry* entry = bdecoding(buf, len);
|
MetaEntry* entry = bdecoding(buf, len);
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
return entry;
|
return entry;
|
||||||
} catch(RecoverableException* ex) {
|
} catch(RecoverableException* ex) {
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
if(fp != NULL) {
|
if(fp) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MetaEntry* MetaFileUtil::bdecoding(const char* buf, int32_t len) {
|
MetaEntry* MetaFileUtil::bdecoding(const unsigned char* buf, size_t len)
|
||||||
MetaEntry* entry = NULL;
|
{
|
||||||
try{
|
const unsigned char* p = buf;
|
||||||
const char* p = buf;
|
const unsigned char* end = buf+len;
|
||||||
const char* end = buf+len;
|
return bdecodingR(&p, end);
|
||||||
entry = bdecodingR(&p, end);
|
|
||||||
return entry;
|
|
||||||
} catch(RecoverableException* ex) {
|
|
||||||
if(entry != NULL) {
|
|
||||||
delete entry;
|
|
||||||
}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MetaEntry* MetaFileUtil::bdecodingR(const char** pp, const char* end) {
|
MetaEntry*
|
||||||
|
MetaFileUtil::bdecodingR(const unsigned char** pp, const unsigned char* end)
|
||||||
|
{
|
||||||
if(*pp >= end) {
|
if(*pp >= end) {
|
||||||
throw new DlAbortEx("Malformed metainfo");
|
throw new DlAbortEx("Malformed metainfo");
|
||||||
}
|
}
|
||||||
|
@ -109,7 +103,9 @@ MetaEntry* MetaFileUtil::bdecodingR(const char** pp, const char* end) {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary* MetaFileUtil::parseDictionaryTree(const char** pp, const char* end) {
|
Dictionary*
|
||||||
|
MetaFileUtil::parseDictionaryTree(const unsigned char** pp, const unsigned char* end)
|
||||||
|
{
|
||||||
if(*pp >= end) {
|
if(*pp >= end) {
|
||||||
throw new DlAbortEx("Malformed metainfo");
|
throw new DlAbortEx("Malformed metainfo");
|
||||||
}
|
}
|
||||||
|
@ -131,7 +127,9 @@ Dictionary* MetaFileUtil::parseDictionaryTree(const char** pp, const char* end)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List* MetaFileUtil::parseListTree(const char** pp, const char* end) {
|
List*
|
||||||
|
MetaFileUtil::parseListTree(const unsigned char** pp, const unsigned char* end)
|
||||||
|
{
|
||||||
if(*pp >= end) {
|
if(*pp >= end) {
|
||||||
throw new DlAbortEx("Malformed metainfo");
|
throw new DlAbortEx("Malformed metainfo");
|
||||||
}
|
}
|
||||||
|
@ -152,37 +150,42 @@ List* MetaFileUtil::parseListTree(const char** pp, const char* end) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Data* MetaFileUtil::decodeInt(const char** pp, const char* end) {
|
Data*
|
||||||
|
MetaFileUtil::decodeInt(const unsigned char** pp, const unsigned char* end)
|
||||||
|
{
|
||||||
if(*pp >= end) {
|
if(*pp >= end) {
|
||||||
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
||||||
}
|
}
|
||||||
char* endTerm = (char*)memchr(*pp, 'e', end-*pp);
|
unsigned char* endTerm = reinterpret_cast<unsigned char*>(memchr(*pp, 'e', end-*pp));
|
||||||
// TODO if endTerm is null
|
// TODO if endTerm is null
|
||||||
if(endTerm == NULL) {
|
if(!endTerm) {
|
||||||
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
||||||
}
|
}
|
||||||
int32_t numSize = endTerm-*pp;
|
size_t numSize = endTerm-*pp;
|
||||||
|
|
||||||
Data* data = new Data(*pp, numSize, true);
|
Data* data = new Data(*pp, numSize, true);
|
||||||
*pp += numSize+1;
|
*pp += numSize+1;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data* MetaFileUtil::decodeWord(const char** pp, const char* end) {
|
Data*
|
||||||
|
MetaFileUtil::decodeWord(const unsigned char** pp, const unsigned char* end)
|
||||||
|
{
|
||||||
if(*pp >= end) {
|
if(*pp >= end) {
|
||||||
throw new DlAbortEx("Malformed metainfo");
|
throw new DlAbortEx("Malformed metainfo");
|
||||||
}
|
}
|
||||||
char* delim = (char*)memchr(*pp, ':', end-*pp);
|
unsigned char* delim = reinterpret_cast<unsigned char*>(memchr(*pp, ':', end-*pp));
|
||||||
// TODO if delim is null
|
// TODO if delim is null
|
||||||
if(delim == *pp || delim == NULL) {
|
if(delim == *pp || !delim) {
|
||||||
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
||||||
}
|
}
|
||||||
int32_t numSize = delim-*pp;
|
size_t numSize = delim-*pp;
|
||||||
char* temp = new char[numSize+1];
|
unsigned char* temp = new unsigned char[numSize+1];
|
||||||
memcpy(temp, *pp, numSize);
|
memcpy(temp, *pp, numSize);
|
||||||
temp[numSize] = '\0';
|
temp[numSize] = '\0';
|
||||||
char* endptr;
|
char* endptr;
|
||||||
int32_t size = strtol(temp, &endptr, 10);
|
unsigned long int size = strtoul(reinterpret_cast<const char*>(temp),
|
||||||
|
&endptr, 10);
|
||||||
if(*endptr != '\0') {
|
if(*endptr != '\0') {
|
||||||
delete [] temp;
|
delete [] temp;
|
||||||
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
throw new DlAbortEx(EX_MALFORMED_META_INFO);
|
||||||
|
@ -198,7 +201,9 @@ Data* MetaFileUtil::decodeWord(const char** pp, const char* end) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MetaFileUtil::decodeWordAsString(const char** pp, const char* end) {
|
std::string
|
||||||
|
MetaFileUtil::decodeWordAsString(const unsigned char** pp, const unsigned char* end)
|
||||||
|
{
|
||||||
Data* data = decodeWord(pp, end);
|
Data* data = decodeWord(pp, end);
|
||||||
std::string str = data->toString();
|
std::string str = data->toString();
|
||||||
delete data;
|
delete data;
|
||||||
|
|
|
@ -49,16 +49,22 @@ class MetaFileUtil {
|
||||||
private:
|
private:
|
||||||
MetaFileUtil() {}
|
MetaFileUtil() {}
|
||||||
|
|
||||||
static MetaEntry* bdecodingR(const char** pp, const char* end);
|
static MetaEntry* bdecodingR(const unsigned char** pp, const unsigned char* end);
|
||||||
static Dictionary* parseDictionaryTree(const char** pp, const char* end);
|
static Dictionary* parseDictionaryTree(const unsigned char** pp, const unsigned char* end);
|
||||||
static List* parseListTree(const char** pp, const char* end);
|
static List* parseListTree(const unsigned char** pp, const unsigned char* end);
|
||||||
static Data* decodeWord(const char** pp, const char* end);
|
static Data* decodeWord(const unsigned char** pp, const unsigned char* end);
|
||||||
static Data* decodeInt(const char** pp, const char* end);
|
static Data* decodeInt(const unsigned char** pp, const unsigned char* end);
|
||||||
static std::string decodeWordAsString(const char** pp, const char* end);
|
static std::string decodeWordAsString(const unsigned char** pp, const unsigned char* end);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static MetaEntry* parseMetaFile(const std::string& file);
|
static MetaEntry* parseMetaFile(const std::string& file);
|
||||||
static MetaEntry* bdecoding(const char* buf, int32_t len);
|
static MetaEntry* bdecoding(const unsigned char* buf, size_t len);
|
||||||
|
|
||||||
|
static MetaEntry* bdecoding(const std::string& content)
|
||||||
|
{
|
||||||
|
return bdecoding(reinterpret_cast<const unsigned char*>(content.c_str()),
|
||||||
|
content.size());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -160,14 +160,13 @@ bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength,
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerConnection::readData(char* data, int32_t& length, bool encryption)
|
void PeerConnection::readData(unsigned char* data, int32_t& length, bool encryption)
|
||||||
{
|
{
|
||||||
if(encryption) {
|
if(encryption) {
|
||||||
unsigned char* cdata = reinterpret_cast<unsigned char*>(data);
|
|
||||||
unsigned char temp[MAX_PAYLOAD_LEN];
|
unsigned char temp[MAX_PAYLOAD_LEN];
|
||||||
assert(MAX_PAYLOAD_LEN >= length);
|
assert(MAX_PAYLOAD_LEN >= length);
|
||||||
socket->readData(temp, length);
|
socket->readData(temp, length);
|
||||||
_decryptor->decrypt(cdata, length, temp, length);
|
_decryptor->decrypt(data, length, temp, length);
|
||||||
} else {
|
} else {
|
||||||
socket->readData(data, length);
|
socket->readData(data, length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,17 +57,17 @@ private:
|
||||||
const Option* option;
|
const Option* option;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
|
|
||||||
char resbuf[MAX_PAYLOAD_LEN];
|
unsigned char resbuf[MAX_PAYLOAD_LEN];
|
||||||
int32_t resbufLength;
|
int32_t resbufLength;
|
||||||
int32_t currentPayloadLength;
|
int32_t currentPayloadLength;
|
||||||
char lenbuf[4];
|
unsigned char lenbuf[4];
|
||||||
int32_t lenbufLength;
|
int32_t lenbufLength;
|
||||||
|
|
||||||
bool _encryptionEnabled;
|
bool _encryptionEnabled;
|
||||||
SharedHandle<ARC4Encryptor> _encryptor;
|
SharedHandle<ARC4Encryptor> _encryptor;
|
||||||
SharedHandle<ARC4Decryptor> _decryptor;
|
SharedHandle<ARC4Decryptor> _decryptor;
|
||||||
|
|
||||||
void readData(char* data, int32_t& length, bool encryption);
|
void readData(unsigned char* data, int32_t& length, bool encryption);
|
||||||
|
|
||||||
void sendData(const unsigned char* data, size_t length, bool encryption);
|
void sendData(const unsigned char* data, size_t length, bool encryption);
|
||||||
|
|
||||||
|
|
|
@ -125,7 +125,8 @@ void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
|
||||||
msg[4] = messageId;
|
msg[4] = messageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerMessageUtil::createcompact(char* compact, const std::string& addr, uint16_t port)
|
bool
|
||||||
|
PeerMessageUtil::createcompact(unsigned char* compact, const std::string& addr, uint16_t port)
|
||||||
{
|
{
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo* res;
|
struct addrinfo* res;
|
||||||
|
@ -144,7 +145,8 @@ bool PeerMessageUtil::createcompact(char* compact, const std::string& addr, uint
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<std::string, uint16_t> PeerMessageUtil::unpackcompact(const char* compact)
|
std::pair<std::string, uint16_t>
|
||||||
|
PeerMessageUtil::unpackcompact(const unsigned char* compact)
|
||||||
{
|
{
|
||||||
struct sockaddr_in in;
|
struct sockaddr_in in;
|
||||||
memset(&in, 0, sizeof(in));
|
memset(&in, 0, sizeof(in));
|
||||||
|
|
|
@ -78,9 +78,9 @@ public:
|
||||||
* The example of failure reason is that addr is not numbers-and-dots
|
* The example of failure reason is that addr is not numbers-and-dots
|
||||||
* notation.
|
* notation.
|
||||||
*/
|
*/
|
||||||
static bool createcompact(char* compact, const std::string& addr, uint16_t port);
|
static bool createcompact(unsigned char* compact, const std::string& addr, uint16_t port);
|
||||||
|
|
||||||
static std::pair<std::string, uint16_t> unpackcompact(const char* compact);
|
static std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -190,6 +190,12 @@ public:
|
||||||
|
|
||||||
void writeData(const char* data, size_t len, const std::string& host, uint16_t port);
|
void writeData(const char* data, size_t len, const std::string& host, uint16_t port);
|
||||||
|
|
||||||
|
void writeData(const unsigned char* data, size_t len, const std::string& host,
|
||||||
|
uint16_t port)
|
||||||
|
{
|
||||||
|
writeData(reinterpret_cast<const char*>(data), len, host, port);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads up to len bytes from this socket.
|
* Reads up to len bytes from this socket.
|
||||||
* data is a pointer pointing the first
|
* data is a pointer pointing the first
|
||||||
|
@ -209,10 +215,17 @@ public:
|
||||||
readData(reinterpret_cast<char*>(data), len);
|
readData(reinterpret_cast<char*>(data), len);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t readDataFrom(char*, size_t len,
|
ssize_t readDataFrom(char* data, size_t len,
|
||||||
std::pair<std::string /* numerichost */,
|
std::pair<std::string /* numerichost */,
|
||||||
uint16_t /* port */>& sender);
|
uint16_t /* port */>& sender);
|
||||||
|
|
||||||
|
ssize_t readDataFrom(unsigned char* data, size_t len,
|
||||||
|
std::pair<std::string /* numerichost */,
|
||||||
|
uint16_t /* port */>& sender)
|
||||||
|
{
|
||||||
|
return readDataFrom(reinterpret_cast<char*>(data), len, sender);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads up to len bytes from this socket, but bytes are not removed from
|
* Reads up to len bytes from this socket, but bytes are not removed from
|
||||||
* this socket.
|
* this socket.
|
||||||
|
|
|
@ -125,11 +125,11 @@ bool TrackerWatcherCommand::execute() {
|
||||||
std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle& requestGroup)
|
std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle& requestGroup)
|
||||||
{
|
{
|
||||||
std::stringstream strm;
|
std::stringstream strm;
|
||||||
char data[2048];
|
unsigned char data[2048];
|
||||||
requestGroup->getPieceStorage()->getDiskAdaptor()->openFile();
|
requestGroup->getPieceStorage()->getDiskAdaptor()->openFile();
|
||||||
while(1) {
|
while(1) {
|
||||||
int32_t dataLength = requestGroup->getPieceStorage()->getDiskAdaptor()->readData((unsigned char*)data, sizeof(data), strm.tellp());
|
int32_t dataLength = requestGroup->getPieceStorage()->getDiskAdaptor()->readData(data, sizeof(data), strm.tellp());
|
||||||
strm.write(data, dataLength);
|
strm.write(reinterpret_cast<const char*>(data), dataLength);
|
||||||
if(dataLength == 0) {
|
if(dataLength == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -140,7 +140,7 @@ std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle&
|
||||||
// TODO we have to deal with the exception thrown By BtAnnounce
|
// TODO we have to deal with the exception thrown By BtAnnounce
|
||||||
void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerResponse)
|
void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerResponse)
|
||||||
{
|
{
|
||||||
btAnnounce->processAnnounceResponse(trackerResponse.c_str(),
|
btAnnounce->processAnnounceResponse(reinterpret_cast<const unsigned char*>(trackerResponse.c_str()),
|
||||||
trackerResponse.size());
|
trackerResponse.size());
|
||||||
while(!btRuntime->isHalt() && btRuntime->lessThanMinPeer()) {
|
while(!btRuntime->isHalt() && btRuntime->lessThanMinPeer()) {
|
||||||
PeerHandle peer = peerStorage->getUnusedPeer();
|
PeerHandle peer = peerStorage->getUnusedPeer();
|
||||||
|
|
|
@ -45,7 +45,8 @@ class TransferEncoding {
|
||||||
public:
|
public:
|
||||||
virtual ~TransferEncoding() {}
|
virtual ~TransferEncoding() {}
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
virtual void inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen) = 0;
|
virtual void inflate(unsigned char* outbuf, int32_t& outlen,
|
||||||
|
const unsigned char* inbuf, int32_t inlen) = 0;
|
||||||
virtual bool finished() = 0;
|
virtual bool finished() = 0;
|
||||||
virtual void end() = 0;
|
virtual void end() = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -76,7 +76,7 @@ std::pair<std::string, std::string> UTPexExtensionMessage::createCompactPeerList
|
||||||
std::string addrstring;
|
std::string addrstring;
|
||||||
std::string flagstring;
|
std::string flagstring;
|
||||||
for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); ++itr) {
|
for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); ++itr) {
|
||||||
char compact[6];
|
unsigned char compact[6];
|
||||||
if(PeerMessageUtil::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
|
if(PeerMessageUtil::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
|
||||||
addrstring.append(&compact[0], &compact[6]);
|
addrstring.append(&compact[0], &compact[6]);
|
||||||
flagstring += (*itr)->isSeeder() ? "2" : "0";
|
flagstring += (*itr)->isSeeder() ? "2" : "0";
|
||||||
|
@ -123,7 +123,7 @@ void UTPexExtensionMessage::setBtContext(const BtContextHandle& btContext)
|
||||||
|
|
||||||
UTPexExtensionMessageHandle
|
UTPexExtensionMessageHandle
|
||||||
UTPexExtensionMessage::create(const BtContextHandle& btContext,
|
UTPexExtensionMessage::create(const BtContextHandle& btContext,
|
||||||
const char* data, size_t len)
|
const unsigned char* data, size_t len)
|
||||||
{
|
{
|
||||||
if(len < 1) {
|
if(len < 1) {
|
||||||
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,
|
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,
|
||||||
|
|
|
@ -93,7 +93,7 @@ public:
|
||||||
void setBtContext(const SharedHandle<BtContext>& btContext);
|
void setBtContext(const SharedHandle<BtContext>& btContext);
|
||||||
|
|
||||||
static UTPexExtensionMessageHandle create(const SharedHandle<BtContext>& btContext,
|
static UTPexExtensionMessageHandle create(const SharedHandle<BtContext>& btContext,
|
||||||
const char* data, size_t len);
|
const unsigned char* data, size_t len);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<UTPexExtensionMessage> UTPexExtensionMessageHandle;
|
typedef SharedHandle<UTPexExtensionMessage> UTPexExtensionMessageHandle;
|
||||||
|
|
|
@ -137,6 +137,12 @@ public:
|
||||||
|
|
||||||
static std::string torrentUrlencode(const unsigned char* target, int32_t len);
|
static std::string torrentUrlencode(const unsigned char* target, int32_t len);
|
||||||
|
|
||||||
|
static std::string torrentUrlencode(const std::string& target)
|
||||||
|
{
|
||||||
|
return torrentUrlencode(reinterpret_cast<const unsigned char*>(target.c_str()),
|
||||||
|
target.size());
|
||||||
|
}
|
||||||
|
|
||||||
static std::string toHex(const unsigned char* src, int32_t len);
|
static std::string toHex(const unsigned char* src, int32_t len);
|
||||||
|
|
||||||
static std::string toHex(const std::string& src)
|
static std::string toHex(const std::string& src)
|
||||||
|
|
|
@ -43,7 +43,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( AnnounceListTest );
|
||||||
|
|
||||||
void AnnounceListTest::testSingleElementList() {
|
void AnnounceListTest::testSingleElementList() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -89,7 +89,7 @@ void AnnounceListTest::testSingleElementList() {
|
||||||
|
|
||||||
void AnnounceListTest::testMultiElementList() {
|
void AnnounceListTest::testMultiElementList() {
|
||||||
std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
|
std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1, tracker2, tracker3 ] ]
|
// [ [ tracker1, tracker2, tracker3 ] ]
|
||||||
AnnounceList announceList(announces);
|
AnnounceList announceList(announces);
|
||||||
|
@ -121,7 +121,7 @@ void AnnounceListTest::testMultiElementList() {
|
||||||
|
|
||||||
void AnnounceListTest::testSingleAndMulti() {
|
void AnnounceListTest::testSingleAndMulti() {
|
||||||
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1, tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1, tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -147,7 +147,7 @@ void AnnounceListTest::testSingleAndMulti() {
|
||||||
|
|
||||||
void AnnounceListTest::testNoGroup() {
|
void AnnounceListTest::testNoGroup() {
|
||||||
std::string peersString = "llee";
|
std::string peersString = "llee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
AnnounceList announceList(announces);
|
AnnounceList announceList(announces);
|
||||||
|
|
||||||
|
@ -156,7 +156,7 @@ void AnnounceListTest::testNoGroup() {
|
||||||
|
|
||||||
void AnnounceListTest::testNextEventIfAfterStarted() {
|
void AnnounceListTest::testNextEventIfAfterStarted() {
|
||||||
std::string peersString = "ll8:tracker1ee";
|
std::string peersString = "ll8:tracker1ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ] ]
|
// [ [ tracker1 ] ]
|
||||||
|
@ -176,7 +176,7 @@ void AnnounceListTest::testNextEventIfAfterStarted() {
|
||||||
|
|
||||||
void AnnounceListTest::testEvent() {
|
void AnnounceListTest::testEvent() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -200,7 +200,7 @@ void AnnounceListTest::testEvent() {
|
||||||
|
|
||||||
void AnnounceListTest::testCountStoppedAllowedTier() {
|
void AnnounceListTest::testCountStoppedAllowedTier() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -227,7 +227,7 @@ void AnnounceListTest::testCountStoppedAllowedTier() {
|
||||||
|
|
||||||
void AnnounceListTest::testCountCompletedAllowedTier() {
|
void AnnounceListTest::testCountCompletedAllowedTier() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
|
|
@ -66,7 +66,7 @@ void BtExtendedMessageTest::testCreate() {
|
||||||
|
|
||||||
// payload:{4:name3:foo}->11bytes
|
// payload:{4:name3:foo}->11bytes
|
||||||
std::string payload = "4:name3:foo";
|
std::string payload = "4:name3:foo";
|
||||||
char msg[17];// 6+11bytes
|
unsigned char msg[17];// 6+11bytes
|
||||||
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20);
|
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20);
|
||||||
msg[5] = 1; // Set dummy extended message ID 1
|
msg[5] = 1; // Set dummy extended message ID 1
|
||||||
memcpy(msg+6, payload.c_str(), payload.size());
|
memcpy(msg+6, payload.c_str(), payload.size());
|
||||||
|
@ -77,8 +77,8 @@ void BtExtendedMessageTest::testCreate() {
|
||||||
|
|
||||||
// case: payload size is wrong
|
// case: payload size is wrong
|
||||||
try {
|
try {
|
||||||
char msg[5];
|
unsigned char msg[5];
|
||||||
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 1, 20);
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 20);
|
||||||
BtExtendedMessage::create(ctx, peer, &msg[4], 1);
|
BtExtendedMessage::create(ctx, peer, &msg[4], 1);
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
|
@ -87,8 +87,8 @@ void BtExtendedMessageTest::testCreate() {
|
||||||
}
|
}
|
||||||
// case: id is wrong
|
// case: id is wrong
|
||||||
try {
|
try {
|
||||||
char msg[6];
|
unsigned char msg[6];
|
||||||
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 2, 21);
|
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 21);
|
||||||
BtExtendedMessage::create(ctx, peer, &msg[4], 2);
|
BtExtendedMessage::create(ctx, peer, &msg[4], 2);
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
|
@ -101,13 +101,11 @@ void BtExtendedMessageTest::testGetMessage() {
|
||||||
std::string payload = "4:name3:foo";
|
std::string payload = "4:name3:foo";
|
||||||
uint8_t extendedMessageID = 1;
|
uint8_t extendedMessageID = 1;
|
||||||
SharedHandle<MockExtensionMessage> exmsg =
|
SharedHandle<MockExtensionMessage> exmsg =
|
||||||
new MockExtensionMessage("charlie", extendedMessageID,
|
new MockExtensionMessage("charlie", extendedMessageID, payload);
|
||||||
payload.c_str(),
|
|
||||||
payload.size());
|
|
||||||
BtExtendedMessage msg(exmsg);
|
BtExtendedMessage msg(exmsg);
|
||||||
|
|
||||||
char data[17];
|
unsigned char data[17];
|
||||||
PeerMessageUtil::createPeerMessageString((unsigned char*)data, sizeof(data), 13, 20);
|
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 20);
|
||||||
*(data+5) = extendedMessageID;
|
*(data+5) = extendedMessageID;
|
||||||
memcpy(data+6, payload.c_str(), payload.size());
|
memcpy(data+6, payload.c_str(), payload.size());
|
||||||
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
|
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
|
||||||
|
@ -115,7 +113,7 @@ void BtExtendedMessageTest::testGetMessage() {
|
||||||
|
|
||||||
void BtExtendedMessageTest::testDoReceivedAction() {
|
void BtExtendedMessageTest::testDoReceivedAction() {
|
||||||
SharedHandle<MockExtensionMessage> exmsg =
|
SharedHandle<MockExtensionMessage> exmsg =
|
||||||
new MockExtensionMessage("charlie", 1, "", 0);
|
new MockExtensionMessage("charlie", 1, "");
|
||||||
BtExtendedMessage msg(exmsg);
|
BtExtendedMessage msg(exmsg);
|
||||||
msg.doReceivedAction();
|
msg.doReceivedAction();
|
||||||
CPPUNIT_ASSERT(exmsg->_doReceivedActionCalled);
|
CPPUNIT_ASSERT(exmsg->_doReceivedActionCalled);
|
||||||
|
@ -125,9 +123,7 @@ void BtExtendedMessageTest::testToString() {
|
||||||
std::string payload = "4:name3:foo";
|
std::string payload = "4:name3:foo";
|
||||||
uint8_t extendedMessageID = 1;
|
uint8_t extendedMessageID = 1;
|
||||||
SharedHandle<MockExtensionMessage> exmsg =
|
SharedHandle<MockExtensionMessage> exmsg =
|
||||||
new MockExtensionMessage("charlie", extendedMessageID,
|
new MockExtensionMessage("charlie", extendedMessageID, payload);
|
||||||
payload.c_str(),
|
|
||||||
payload.size());
|
|
||||||
BtExtendedMessage msg(exmsg);
|
BtExtendedMessage msg(exmsg);
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("extended charlie"), msg.toString());
|
CPPUNIT_ASSERT_EQUAL(std::string("extended charlie"), msg.toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,17 +30,17 @@ CPPUNIT_TEST_SUITE_REGISTRATION( ChunkedEncodingTest );
|
||||||
|
|
||||||
void ChunkedEncodingTest::testInflate1() {
|
void ChunkedEncodingTest::testInflate1() {
|
||||||
std::string msg = "a\r\n1234567890\r\n";
|
std::string msg = "a\r\n1234567890\r\n";
|
||||||
char buf[100];
|
unsigned char buf[100];
|
||||||
int32_t len = sizeof(buf);
|
int32_t len = sizeof(buf);
|
||||||
enc->inflate(buf, len, msg.c_str(), msg.size());
|
enc->inflate(buf, len,
|
||||||
buf[len] = '\0';
|
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), std::string(buf));
|
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), std::string(&buf[0], &buf[len]));
|
||||||
// second pass
|
// second pass
|
||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
msg = "3;extensionIgnored\r\n123\r\n0\r\n";
|
msg = "3;extensionIgnored\r\n123\r\n0\r\n";
|
||||||
enc->inflate(buf, len, msg.c_str(), msg.size());
|
enc->inflate(buf, len,
|
||||||
buf[len] = '\0';
|
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("123"), std::string(buf));
|
CPPUNIT_ASSERT_EQUAL(std::string("123"), std::string(&buf[0], &buf[len]));
|
||||||
// input is over
|
// input is over
|
||||||
CPPUNIT_ASSERT(enc->finished());
|
CPPUNIT_ASSERT(enc->finished());
|
||||||
}
|
}
|
||||||
|
@ -53,16 +53,16 @@ void ChunkedEncodingTest::testInflateLargeChunk() {
|
||||||
}
|
}
|
||||||
std::string body;
|
std::string body;
|
||||||
is >> body;
|
is >> body;
|
||||||
char buf[4097];
|
unsigned char buf[4097];
|
||||||
int32_t len = sizeof(buf);
|
int32_t len = sizeof(buf);
|
||||||
for(int i = 0; i < 2; i++) {
|
for(int i = 0; i < 2; i++) {
|
||||||
std::string msg = "1000\r\n"+body+"\r\n";
|
std::string msg = "1000\r\n"+body+"\r\n";
|
||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
enc->inflate(buf, len, msg.c_str(), msg.size());
|
enc->inflate(buf, len,
|
||||||
buf[len] = '\0';
|
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(body, std::string(buf));
|
CPPUNIT_ASSERT_EQUAL(body, std::string(&buf[0], &buf[len]));
|
||||||
}
|
}
|
||||||
enc->inflate(buf, len, "0\r\n", 3);
|
enc->inflate(buf, len, reinterpret_cast<const unsigned char*>("0\r\n"), 3);
|
||||||
CPPUNIT_ASSERT_EQUAL((int32_t)0, len);
|
CPPUNIT_ASSERT_EQUAL((int32_t)0, len);
|
||||||
CPPUNIT_ASSERT(enc->finished());
|
CPPUNIT_ASSERT(enc->finished());
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage()
|
||||||
cm->put("q", new Data("announce_peer"));
|
cm->put("q", new Data("announce_peer"));
|
||||||
Dictionary* a = new Dictionary();
|
Dictionary* a = new Dictionary();
|
||||||
cm->put("a", a);
|
cm->put("a", a);
|
||||||
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
|
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
|
||||||
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
|
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
|
||||||
a->put("port", new Data(Util::uitos(port), true));
|
a->put("port", new Data(Util::uitos(port), true));
|
||||||
a->put("token", new Data(token));
|
a->put("token", new Data(token));
|
||||||
|
@ -85,7 +85,7 @@ void DHTAnnouncePeerMessageTest::testDoReceivedAction()
|
||||||
remoteNode->setIPAddress("192.168.0.1");
|
remoteNode->setIPAddress("192.168.0.1");
|
||||||
remoteNode->setPort(6881);
|
remoteNode->setPort(6881);
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ void DHTAnnouncePeerReplyMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
|
|
@ -30,16 +30,17 @@ void DHTConnectionImplTest::testWriteAndReadData()
|
||||||
uint16_t con2port = con2.bind(0);
|
uint16_t con2port = con2.bind(0);
|
||||||
|
|
||||||
std::string message1 = "hello world.";
|
std::string message1 = "hello world.";
|
||||||
con1.sendMessage(message1.c_str(), message1.size(), "127.0.0.1", con2port);
|
con1.sendMessage(reinterpret_cast<const unsigned char*>(message1.c_str()),
|
||||||
|
message1.size(), "127.0.0.1", con2port);
|
||||||
|
|
||||||
char readbuffer[100];
|
unsigned char readbuffer[100];
|
||||||
std::string remoteHost;
|
std::string remoteHost;
|
||||||
uint16_t remotePort;
|
uint16_t remotePort;
|
||||||
{
|
{
|
||||||
ssize_t rlength = con2.receiveMessage(readbuffer, sizeof(readbuffer), remoteHost, remotePort);
|
ssize_t rlength = con2.receiveMessage(readbuffer, sizeof(readbuffer), remoteHost, remotePort);
|
||||||
CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
|
CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
|
||||||
readbuffer[rlength] = '\0';
|
CPPUNIT_ASSERT_EQUAL(message1,
|
||||||
CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer));
|
std::string(&readbuffer[0], &readbuffer[rlength]));
|
||||||
}
|
}
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
|
|
@ -51,7 +51,7 @@ void DHTFindNodeMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
@ -67,8 +67,8 @@ void DHTFindNodeMessageTest::testGetBencodedMessage()
|
||||||
cm->put("q", new Data("find_node"));
|
cm->put("q", new Data("find_node"));
|
||||||
Dictionary* a = new Dictionary();
|
Dictionary* a = new Dictionary();
|
||||||
cm->put("a", a);
|
cm->put("a", a);
|
||||||
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
|
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
|
||||||
a->put("target", new Data(reinterpret_cast<const char*>(targetNode->getID()), DHT_ID_LENGTH));
|
a->put("target", new Data(targetNode->getID(), DHT_ID_LENGTH));
|
||||||
|
|
||||||
BencodeVisitor v;
|
BencodeVisitor v;
|
||||||
cm->accept(&v);
|
cm->accept(&v);
|
||||||
|
@ -81,7 +81,7 @@ void DHTFindNodeMessageTest::testDoReceivedAction()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
|
||||||
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
||||||
nodes[i]->setPort(6881+i);
|
nodes[i]->setPort(6881+i);
|
||||||
|
|
||||||
char buf[6];
|
unsigned char buf[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
||||||
compactNodeInfo +=
|
compactNodeInfo +=
|
||||||
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
||||||
|
@ -61,7 +61,7 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
|
||||||
cm->put("y", new Data("r"));
|
cm->put("y", new Data("r"));
|
||||||
Dictionary* r = new Dictionary();
|
Dictionary* r = new Dictionary();
|
||||||
cm->put("r", r);
|
cm->put("r", r);
|
||||||
r->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
|
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
|
||||||
r->put("nodes", new Data(compactNodeInfo));
|
r->put("nodes", new Data(compactNodeInfo));
|
||||||
|
|
||||||
BencodeVisitor v;
|
BencodeVisitor v;
|
||||||
|
|
|
@ -69,7 +69,7 @@ void DHTGetPeersMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ void DHTGetPeersMessageTest::testGetBencodedMessage()
|
||||||
cm->put("q", new Data("get_peers"));
|
cm->put("q", new Data("get_peers"));
|
||||||
Dictionary* a = new Dictionary();
|
Dictionary* a = new Dictionary();
|
||||||
cm->put("a", a);
|
cm->put("a", a);
|
||||||
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
|
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
|
||||||
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
|
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
|
||||||
|
|
||||||
BencodeVisitor v;
|
BencodeVisitor v;
|
||||||
|
@ -103,7 +103,7 @@ void DHTGetPeersMessageTest::testDoReceivedAction()
|
||||||
remoteNode->setIPAddress("192.168.0.1");
|
remoteNode->setIPAddress("192.168.0.1");
|
||||||
remoteNode->setPort(6881);
|
remoteNode->setPort(6881);
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
|
||||||
cm->put("y", new Data("r"));
|
cm->put("y", new Data("r"));
|
||||||
Dictionary* r = new Dictionary();
|
Dictionary* r = new Dictionary();
|
||||||
cm->put("r", r);
|
cm->put("r", r);
|
||||||
r->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
|
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
|
||||||
r->put("token", new Data(token));
|
r->put("token", new Data(token));
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -59,7 +59,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
|
||||||
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
||||||
nodes[i]->setPort(6881+i);
|
nodes[i]->setPort(6881+i);
|
||||||
|
|
||||||
char buf[6];
|
unsigned char buf[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
||||||
compactNodeInfo +=
|
compactNodeInfo +=
|
||||||
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
||||||
|
@ -84,7 +84,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
|
||||||
r->put("values", values);
|
r->put("values", values);
|
||||||
for(size_t i = 0; i < 4; ++i) {
|
for(size_t i = 0; i < 4; ++i) {
|
||||||
SharedHandle<Peer> peer = new Peer("192.168.0."+Util::uitos(i+1), 6881+i);
|
SharedHandle<Peer> peer = new Peer("192.168.0."+Util::uitos(i+1), 6881+i);
|
||||||
char buffer[6];
|
unsigned char buffer[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
|
||||||
values->add(new Data(buffer, sizeof(buffer)));
|
values->add(new Data(buffer, sizeof(buffer)));
|
||||||
peers.push_back(peer);
|
peers.push_back(peer);
|
||||||
|
|
|
@ -163,7 +163,7 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
|
||||||
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
||||||
nodes[i]->setPort(6881+i);
|
nodes[i]->setPort(6881+i);
|
||||||
|
|
||||||
char buf[6];
|
unsigned char buf[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
||||||
compactNodeInfo +=
|
compactNodeInfo +=
|
||||||
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
||||||
|
@ -232,7 +232,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
|
||||||
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
|
||||||
nodes[i]->setPort(6881+i);
|
nodes[i]->setPort(6881+i);
|
||||||
|
|
||||||
char buf[6];
|
unsigned char buf[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
|
||||||
compactNodeInfo +=
|
compactNodeInfo +=
|
||||||
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
|
||||||
|
@ -276,7 +276,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
|
||||||
r->put("values", values);
|
r->put("values", values);
|
||||||
for(size_t i = 0; i < 4; ++i) {
|
for(size_t i = 0; i < 4; ++i) {
|
||||||
SharedHandle<Peer> peer = new Peer("192.168.0."+Util::uitos(i+1), 6881+i);
|
SharedHandle<Peer> peer = new Peer("192.168.0."+Util::uitos(i+1), 6881+i);
|
||||||
char buffer[6];
|
unsigned char buffer[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
|
||||||
values->add(new Data(buffer, sizeof(buffer)));
|
values->add(new Data(buffer, sizeof(buffer)));
|
||||||
peers.push_back(peer);
|
peers.push_back(peer);
|
||||||
|
|
|
@ -48,7 +48,7 @@ void DHTPingMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ void DHTPingMessageTest::testGetBencodedMessage()
|
||||||
cm->put("q", new Data("ping"));
|
cm->put("q", new Data("ping"));
|
||||||
Dictionary* a = new Dictionary();
|
Dictionary* a = new Dictionary();
|
||||||
cm->put("a", a);
|
cm->put("a", a);
|
||||||
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
|
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
|
||||||
|
|
||||||
BencodeVisitor v;
|
BencodeVisitor v;
|
||||||
cm->accept(&v);
|
cm->accept(&v);
|
||||||
|
@ -75,7 +75,7 @@ void DHTPingMessageTest::testDoReceivedAction()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
|
|
|
@ -31,14 +31,14 @@ void DHTPingReplyMessageTest::testGetBencodedMessage()
|
||||||
SharedHandle<DHTNode> localNode = new DHTNode();
|
SharedHandle<DHTNode> localNode = new DHTNode();
|
||||||
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
SharedHandle<DHTNode> remoteNode = new DHTNode();
|
||||||
|
|
||||||
char tid[DHT_TRANSACTION_ID_LENGTH];
|
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
|
||||||
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
|
||||||
|
|
||||||
char id[DHT_ID_LENGTH];
|
unsigned char id[DHT_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(id, DHT_ID_LENGTH);
|
DHTUtil::generateRandomData(id, DHT_ID_LENGTH);
|
||||||
|
|
||||||
DHTPingReplyMessage msg(localNode, remoteNode, (const unsigned char*)id, transactionID);
|
DHTPingReplyMessage msg(localNode, remoteNode, id, transactionID);
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,8 @@ void DHTRoutingTableSerializerTest::testSerialize()
|
||||||
// 6bytes compact peer info
|
// 6bytes compact peer info
|
||||||
ss.read(buf, 6);
|
ss.read(buf, 6);
|
||||||
{
|
{
|
||||||
std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
|
std::pair<std::string, uint16_t> peer =
|
||||||
|
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer.first);
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer.first);
|
||||||
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peer.second);
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peer.second);
|
||||||
}
|
}
|
||||||
|
@ -166,7 +167,8 @@ void DHTRoutingTableSerializerTest::testSerialize()
|
||||||
// 6bytes compact peer info
|
// 6bytes compact peer info
|
||||||
ss.read(buf, 6);
|
ss.read(buf, 6);
|
||||||
{
|
{
|
||||||
std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
|
std::pair<std::string, uint16_t> peer =
|
||||||
|
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peer.first);
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peer.first);
|
||||||
CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peer.second);
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peer.second);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DHTTokenTrackerTest);
|
||||||
void DHTTokenTrackerTest::testGenerateToken()
|
void DHTTokenTrackerTest::testGenerateToken()
|
||||||
{
|
{
|
||||||
unsigned char infohash[DHT_ID_LENGTH];
|
unsigned char infohash[DHT_ID_LENGTH];
|
||||||
DHTUtil::generateRandomData(reinterpret_cast<char*>(infohash), DHT_ID_LENGTH);
|
DHTUtil::generateRandomData(infohash, DHT_ID_LENGTH);
|
||||||
std::string ipaddr = "192.168.0.1";
|
std::string ipaddr = "192.168.0.1";
|
||||||
uint16_t port = 6881;
|
uint16_t port = 6881;
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,9 @@ void DHTUnknownMessageTest::testToString()
|
||||||
{
|
{
|
||||||
// data.size() > 8
|
// data.size() > 8
|
||||||
std::string data = "chocolate";
|
std::string data = "chocolate";
|
||||||
DHTUnknownMessage msg(localNode, data.c_str(), data.size(),
|
DHTUnknownMessage msg(localNode,
|
||||||
|
reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size(),
|
||||||
ipaddr, port);
|
ipaddr, port);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("dht unknown Remote:192.168.0.1:6881 length=9, first 8 bytes(hex)=63686f636f6c6174"), msg.toString());
|
CPPUNIT_ASSERT_EQUAL(std::string("dht unknown Remote:192.168.0.1:6881 length=9, first 8 bytes(hex)=63686f636f6c6174"), msg.toString());
|
||||||
|
@ -38,7 +40,9 @@ void DHTUnknownMessageTest::testToString()
|
||||||
{
|
{
|
||||||
// data.size() == 3
|
// data.size() == 3
|
||||||
std::string data = "foo";
|
std::string data = "foo";
|
||||||
DHTUnknownMessage msg(localNode, data.c_str(), data.size(),
|
DHTUnknownMessage msg(localNode,
|
||||||
|
reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size(),
|
||||||
ipaddr, port);
|
ipaddr, port);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("dht unknown Remote:192.168.0.1:6881 length=3, first 8 bytes(hex)=666f6f"), msg.toString());
|
CPPUNIT_ASSERT_EQUAL(std::string("dht unknown Remote:192.168.0.1:6881 length=3, first 8 bytes(hex)=666f6f"), msg.toString());
|
||||||
|
|
|
@ -41,7 +41,7 @@ void DataTest::testGetData() {
|
||||||
CPPUNIT_ASSERT_EQUAL((int32_t)5, data.getLen());
|
CPPUNIT_ASSERT_EQUAL((int32_t)5, data.getLen());
|
||||||
|
|
||||||
Data null(reinterpret_cast<const char*>(0), 0);
|
Data null(reinterpret_cast<const char*>(0), 0);
|
||||||
CPPUNIT_ASSERT_EQUAL((const char*)NULL, null.getData());
|
CPPUNIT_ASSERT(null.getData() == 0);
|
||||||
CPPUNIT_ASSERT_EQUAL((int32_t)0, null.getLen());
|
CPPUNIT_ASSERT_EQUAL((int32_t)0, null.getLen());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ public:
|
||||||
_btContext->setInfoHash(infoHash);
|
_btContext->setInfoHash(infoHash);
|
||||||
_btContext->setTotalLength(totalLength);
|
_btContext->setTotalLength(totalLength);
|
||||||
_btContext->setPieceLength(pieceLength);
|
_btContext->setPieceLength(pieceLength);
|
||||||
_btContext->setPeerId((const unsigned char*)peerId.c_str());
|
_btContext->setPeerId(reinterpret_cast<const unsigned char*>(peerId.c_str()));
|
||||||
|
|
||||||
_pieceStorage = new MockPieceStorage();
|
_pieceStorage = new MockPieceStorage();
|
||||||
_pieceStorage->setTotalLength(totalLength);
|
_pieceStorage->setTotalLength(totalLength);
|
||||||
|
@ -288,7 +288,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse_malformed()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
std::string res = "i123e";
|
std::string res = "i123e";
|
||||||
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(res.c_str(), res.size());
|
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -300,7 +300,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse_failureReason()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
std::string res = "d14:failure reason11:hello worlde";
|
std::string res = "d14:failure reason11:hello worlde";
|
||||||
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(res.c_str(), res.size());
|
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -320,7 +320,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse()
|
||||||
"e";
|
"e";
|
||||||
|
|
||||||
DefaultBtAnnounce an(new MockBtContext(), _option);
|
DefaultBtAnnounce an(new MockBtContext(), _option);
|
||||||
an.processAnnounceResponse(res.c_str(), res.size());
|
an.processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("foo"), an.getTrackerID());
|
CPPUNIT_ASSERT_EQUAL(std::string("foo"), an.getTrackerID());
|
||||||
CPPUNIT_ASSERT_EQUAL(3000, an.getInterval());
|
CPPUNIT_ASSERT_EQUAL(3000, an.getInterval());
|
||||||
CPPUNIT_ASSERT_EQUAL(1800, an.getMinInterval());
|
CPPUNIT_ASSERT_EQUAL(1800, an.getMinInterval());
|
||||||
|
|
|
@ -323,7 +323,7 @@ void DefaultBtContextTest::testLoadFromMemory()
|
||||||
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee";
|
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee";
|
||||||
|
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
||||||
|
|
||||||
|
@ -338,7 +338,7 @@ void DefaultBtContextTest::testLoadFromMemory_somethingMissing()
|
||||||
try {
|
try {
|
||||||
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
|
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -358,7 +358,7 @@ void DefaultBtContextTest::testGetNodes()
|
||||||
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
||||||
"ee";
|
"ee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
||||||
btContext.getNodes();
|
btContext.getNodes();
|
||||||
|
@ -379,7 +379,7 @@ void DefaultBtContextTest::testGetNodes()
|
||||||
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
||||||
"ee";
|
"ee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
||||||
btContext.getNodes();
|
btContext.getNodes();
|
||||||
|
@ -398,7 +398,7 @@ void DefaultBtContextTest::testGetNodes()
|
||||||
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
||||||
"ee";
|
"ee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
||||||
btContext.getNodes();
|
btContext.getNodes();
|
||||||
|
@ -417,7 +417,7 @@ void DefaultBtContextTest::testGetNodes()
|
||||||
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
||||||
"ee";
|
"ee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
||||||
btContext.getNodes();
|
btContext.getNodes();
|
||||||
|
@ -435,7 +435,7 @@ void DefaultBtContextTest::testGetNodes()
|
||||||
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
||||||
"ee";
|
"ee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
||||||
btContext.getNodes();
|
btContext.getNodes();
|
||||||
|
@ -452,7 +452,7 @@ void DefaultBtContextTest::testGetNodes()
|
||||||
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
|
||||||
"ee";
|
"ee";
|
||||||
DefaultBtContext btContext;
|
DefaultBtContext btContext;
|
||||||
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
|
btContext.loadFromMemory(memory, "default");
|
||||||
|
|
||||||
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
const std::deque<std::pair<std::string, uint16_t> >& nodes =
|
||||||
btContext.getNodes();
|
btContext.getNodes();
|
||||||
|
|
|
@ -69,7 +69,8 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_unknown()
|
||||||
std::string data = std::string(&id[0], &id[1]);
|
std::string data = std::string(&id[0], &id[1]);
|
||||||
try {
|
try {
|
||||||
// this test fails because localhost doesn't have extension id = 255.
|
// this test fails because localhost doesn't have extension id = 255.
|
||||||
factory.createMessage(data.c_str(), data.size());
|
factory.createMessage(reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -87,7 +88,8 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_Handshake()
|
||||||
|
|
||||||
std::string data = std::string(&id[0], &id[1])+"d1:v5:aria2e";
|
std::string data = std::string(&id[0], &id[1])+"d1:v5:aria2e";
|
||||||
SharedHandle<HandshakeExtensionMessage> m =
|
SharedHandle<HandshakeExtensionMessage> m =
|
||||||
factory.createMessage(data.c_str(), data.size());
|
factory.createMessage(reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,10 +99,10 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
|
||||||
factory.setBtContext(_btContext);
|
factory.setBtContext(_btContext);
|
||||||
factory.setPeer(_peer);
|
factory.setPeer(_peer);
|
||||||
|
|
||||||
char c1[6];
|
unsigned char c1[6];
|
||||||
char c2[6];
|
unsigned char c2[6];
|
||||||
char c3[6];
|
unsigned char c3[6];
|
||||||
char c4[6];
|
unsigned char c4[6];
|
||||||
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
|
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
|
||||||
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
|
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
|
||||||
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
|
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
|
||||||
|
@ -115,7 +117,8 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
|
||||||
"e";
|
"e";
|
||||||
|
|
||||||
SharedHandle<UTPexExtensionMessage> m =
|
SharedHandle<UTPexExtensionMessage> m =
|
||||||
factory.createMessage(data.c_str(), data.size());
|
factory.createMessage(reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(factory.getExtensionMessageID("ut_pex"),
|
CPPUNIT_ASSERT_EQUAL(factory.getExtensionMessageID("ut_pex"),
|
||||||
m->getExtensionMessageID());
|
m->getExtensionMessageID());
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ void DefaultPeerListProcessorTest::testExtractPeer() {
|
||||||
DefaultPeerListProcessor proc(1024*1024, 10*1024*1024);
|
DefaultPeerListProcessor proc(1024*1024, 10*1024*1024);
|
||||||
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
|
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
|
||||||
|
|
||||||
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
CPPUNIT_ASSERT(proc.canHandle(dic->get("peers")));
|
CPPUNIT_ASSERT(proc.canHandle(dic->get("peers")));
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ void DefaultPeerListProcessorTest::testExtract2Peers() {
|
||||||
DefaultPeerListProcessor proc(1024*1024, 10*1024*1024);
|
DefaultPeerListProcessor proc(1024*1024, 10*1024*1024);
|
||||||
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
|
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
|
||||||
|
|
||||||
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
|
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString);
|
||||||
|
|
||||||
std::deque<SharedHandle<Peer> > peers = proc.extractPeer(dic->get("peers"));
|
std::deque<SharedHandle<Peer> > peers = proc.extractPeer(dic->get("peers"));
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
|
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
|
||||||
|
|
|
@ -95,7 +95,8 @@ void HandshakeExtensionMessageTest::testCreate()
|
||||||
{
|
{
|
||||||
std::string in = "0d1:pi6881e1:v5:aria21:md6:ut_pexi1eee";
|
std::string in = "0d1:pi6881e1:v5:aria21:md6:ut_pexi1eee";
|
||||||
SharedHandle<HandshakeExtensionMessage> m =
|
SharedHandle<HandshakeExtensionMessage> m =
|
||||||
HandshakeExtensionMessage::create(in.c_str(), in.size());
|
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
|
||||||
|
in.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
|
||||||
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort());
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort());
|
||||||
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex"));
|
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex"));
|
||||||
|
@ -103,7 +104,8 @@ void HandshakeExtensionMessageTest::testCreate()
|
||||||
try {
|
try {
|
||||||
// bad payload format
|
// bad payload format
|
||||||
std::string in = "011:hello world";
|
std::string in = "011:hello world";
|
||||||
HandshakeExtensionMessage::create(in.c_str(), in.size());
|
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
|
||||||
|
in.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -112,7 +114,8 @@ void HandshakeExtensionMessageTest::testCreate()
|
||||||
try {
|
try {
|
||||||
// malformed dencoded message
|
// malformed dencoded message
|
||||||
std::string in = "011:hello";
|
std::string in = "011:hello";
|
||||||
HandshakeExtensionMessage::create(in.c_str(), in.size());
|
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
|
||||||
|
in.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -121,7 +124,8 @@ void HandshakeExtensionMessageTest::testCreate()
|
||||||
try {
|
try {
|
||||||
// 0 length data
|
// 0 length data
|
||||||
std::string in = "";
|
std::string in = "";
|
||||||
HandshakeExtensionMessage::create(in.c_str(), in.size());
|
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
|
||||||
|
in.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
@ -133,7 +137,8 @@ void HandshakeExtensionMessageTest::testCreate_stringnum()
|
||||||
{
|
{
|
||||||
std::string in = "0d1:p4:68811:v5:aria21:md6:ut_pex1:1ee";
|
std::string in = "0d1:p4:68811:v5:aria21:md6:ut_pex1:1ee";
|
||||||
SharedHandle<HandshakeExtensionMessage> m =
|
SharedHandle<HandshakeExtensionMessage> m =
|
||||||
HandshakeExtensionMessage::create(in.c_str(), in.size());
|
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
|
||||||
|
in.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
|
||||||
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort());
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort());
|
||||||
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex"));
|
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex"));
|
||||||
|
|
|
@ -78,12 +78,13 @@ void HttpHeaderProcessorTest::testGetPutBackDataLength()
|
||||||
void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
|
void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
|
||||||
{
|
{
|
||||||
HttpHeaderProcessor proc;
|
HttpHeaderProcessor proc;
|
||||||
proc.update("HTTP/1.1 200 OK\r\n"
|
const char* x = "HTTP/1.1 200 OK\r\n"
|
||||||
"foo: foo\0bar\r\n"
|
"foo: foo\0bar\r\n"
|
||||||
"\r\nputbackme", 35+7);
|
"\r\nputbackme";
|
||||||
|
std::string hd1(&x[0], &x[42]);
|
||||||
|
proc.update(hd1);
|
||||||
CPPUNIT_ASSERT(proc.eoh());
|
CPPUNIT_ASSERT(proc.eoh());
|
||||||
CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
|
CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpHeaderProcessorTest::testGetHttpStatusHeader()
|
void HttpHeaderProcessorTest::testGetHttpStatusHeader()
|
||||||
|
|
|
@ -35,8 +35,8 @@ void MetaFileUtilTest::testParseMetaFile() {
|
||||||
|
|
||||||
void MetaFileUtilTest::testBdecoding() {
|
void MetaFileUtilTest::testBdecoding() {
|
||||||
try {
|
try {
|
||||||
const char* str = "5:abcd";
|
std::string str = "5:abcd";
|
||||||
MetaFileUtil::bdecoding(str, strlen(str));
|
MetaFileUtil::bdecoding(str);
|
||||||
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
||||||
} catch(DlAbortEx* ex) {
|
} catch(DlAbortEx* ex) {
|
||||||
delete ex;
|
delete ex;
|
||||||
|
@ -45,8 +45,8 @@ void MetaFileUtilTest::testBdecoding() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const char* str = "i1234";
|
std::string str = "i1234";
|
||||||
MetaFileUtil::bdecoding(str, strlen(str));
|
MetaFileUtil::bdecoding(str);
|
||||||
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
||||||
} catch(DlAbortEx* ex) {
|
} catch(DlAbortEx* ex) {
|
||||||
delete ex;
|
delete ex;
|
||||||
|
@ -55,8 +55,8 @@ void MetaFileUtilTest::testBdecoding() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const char* str = "5abcd";
|
const std::string str = "5abcd";
|
||||||
MetaFileUtil::bdecoding(str, strlen(str));
|
MetaFileUtil::bdecoding(str);
|
||||||
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
||||||
} catch(DlAbortEx* ex) {
|
} catch(DlAbortEx* ex) {
|
||||||
delete ex;
|
delete ex;
|
||||||
|
@ -65,8 +65,8 @@ void MetaFileUtilTest::testBdecoding() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const char* str = "d";
|
const std::string str = "d";
|
||||||
MetaFileUtil::bdecoding(str, strlen(str));
|
MetaFileUtil::bdecoding(str);
|
||||||
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
|
||||||
} catch(DlAbortEx* ex) {
|
} catch(DlAbortEx* ex) {
|
||||||
delete ex;
|
delete ex;
|
||||||
|
|
|
@ -42,7 +42,7 @@ public:
|
||||||
|
|
||||||
virtual void resetAnnounce() {}
|
virtual void resetAnnounce() {}
|
||||||
|
|
||||||
virtual void processAnnounceResponse(const char* trackerResponse,
|
virtual void processAnnounceResponse(const unsigned char* trackerResponse,
|
||||||
size_t trackerResponseLength) {}
|
size_t trackerResponseLength) {}
|
||||||
|
|
||||||
virtual bool noMoreAnnounce() {
|
virtual bool noMoreAnnounce() {
|
||||||
|
|
|
@ -112,8 +112,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createUnknownMessage(const char* data, size_t length, const std::string& ipaddr,
|
createUnknownMessage(const unsigned char* data, size_t length,
|
||||||
uint16_t port)
|
const std::string& ipaddr, uint16_t port)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,20 @@ public:
|
||||||
public:
|
public:
|
||||||
MockExtensionMessage(const std::string& extensionName,
|
MockExtensionMessage(const std::string& extensionName,
|
||||||
uint8_t extensionMessageID,
|
uint8_t extensionMessageID,
|
||||||
const char* data,
|
const unsigned char* data,
|
||||||
size_t length):_extensionName(extensionName),
|
size_t length):_extensionName(extensionName),
|
||||||
_extensionMessageID(extensionMessageID),
|
_extensionMessageID(extensionMessageID),
|
||||||
_data(&data[0], &data[length]),
|
_data(&data[0], &data[length]),
|
||||||
_doReceivedActionCalled(false) {}
|
_doReceivedActionCalled(false) {}
|
||||||
|
|
||||||
|
MockExtensionMessage(const std::string& extensionName,
|
||||||
|
uint8_t extensionMessageID,
|
||||||
|
const std::string& data):
|
||||||
|
_extensionName(extensionName),
|
||||||
|
_extensionMessageID(extensionMessageID),
|
||||||
|
_data(data),
|
||||||
|
_doReceivedActionCalled(false) {}
|
||||||
|
|
||||||
virtual ~MockExtensionMessage() {}
|
virtual ~MockExtensionMessage() {}
|
||||||
|
|
||||||
virtual std::string getBencodedData()
|
virtual std::string getBencodedData()
|
||||||
|
|
|
@ -10,7 +10,7 @@ class MockExtensionMessageFactory:public ExtensionMessageFactory {
|
||||||
public:
|
public:
|
||||||
virtual ~MockExtensionMessageFactory() {}
|
virtual ~MockExtensionMessageFactory() {}
|
||||||
|
|
||||||
virtual SharedHandle<ExtensionMessage> createMessage(const char* data,
|
virtual SharedHandle<ExtensionMessage> createMessage(const unsigned char* data,
|
||||||
size_t length)
|
size_t length)
|
||||||
{
|
{
|
||||||
return new MockExtensionMessage("a2_mock", *data, data+1, length-1);
|
return new MockExtensionMessage("a2_mock", *data, data+1, length-1);
|
||||||
|
|
|
@ -41,7 +41,7 @@ void createNLengthMessage(char* msg, int msgLen, int payloadLen, int id) {
|
||||||
|
|
||||||
void PeerMessageUtilTest::testCreateCompact()
|
void PeerMessageUtilTest::testCreateCompact()
|
||||||
{
|
{
|
||||||
char compact[6];
|
unsigned char compact[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(compact, "::ffff:127.0.0.1", 6881));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(compact, "::ffff:127.0.0.1", 6881));
|
||||||
|
|
||||||
std::pair<std::string, uint16_t> p = PeerMessageUtil::unpackcompact(compact);
|
std::pair<std::string, uint16_t> p = PeerMessageUtil::unpackcompact(compact);
|
||||||
|
|
|
@ -83,10 +83,10 @@ void UTPexExtensionMessageTest::testGetBencodedData()
|
||||||
SharedHandle<Peer> p4 = new Peer("10.1.1.3", 10000);
|
SharedHandle<Peer> p4 = new Peer("10.1.1.3", 10000);
|
||||||
msg.addDroppedPeer(p4);
|
msg.addDroppedPeer(p4);
|
||||||
|
|
||||||
char c1[6];
|
unsigned char c1[6];
|
||||||
char c2[6];
|
unsigned char c2[6];
|
||||||
char c3[6];
|
unsigned char c3[6];
|
||||||
char c4[6];
|
unsigned char c4[6];
|
||||||
PeerMessageUtil::createcompact(c1, p1->ipaddr, p1->port);
|
PeerMessageUtil::createcompact(c1, p1->ipaddr, p1->port);
|
||||||
PeerMessageUtil::createcompact(c2, p2->ipaddr, p2->port);
|
PeerMessageUtil::createcompact(c2, p2->ipaddr, p2->port);
|
||||||
PeerMessageUtil::createcompact(c3, p3->ipaddr, p3->port);
|
PeerMessageUtil::createcompact(c3, p3->ipaddr, p3->port);
|
||||||
|
@ -153,10 +153,10 @@ void UTPexExtensionMessageTest::testCreate()
|
||||||
_btContext->setPieceLength(256*1024);
|
_btContext->setPieceLength(256*1024);
|
||||||
_btContext->setTotalLength(1024*1024);
|
_btContext->setTotalLength(1024*1024);
|
||||||
|
|
||||||
char c1[6];
|
unsigned char c1[6];
|
||||||
char c2[6];
|
unsigned char c2[6];
|
||||||
char c3[6];
|
unsigned char c3[6];
|
||||||
char c4[6];
|
unsigned char c4[6];
|
||||||
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
|
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
|
||||||
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
|
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
|
||||||
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
|
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
|
||||||
|
@ -171,7 +171,9 @@ void UTPexExtensionMessageTest::testCreate()
|
||||||
"e";
|
"e";
|
||||||
|
|
||||||
SharedHandle<UTPexExtensionMessage> msg =
|
SharedHandle<UTPexExtensionMessage> msg =
|
||||||
UTPexExtensionMessage::create(_btContext, data.c_str(), data.size());
|
UTPexExtensionMessage::create(_btContext,
|
||||||
|
reinterpret_cast<const unsigned char*>(data.c_str()),
|
||||||
|
data.size());
|
||||||
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg->getExtensionMessageID());
|
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg->getExtensionMessageID());
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getFreshPeers().size());
|
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getFreshPeers().size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), msg->getFreshPeers()[0]->ipaddr);
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), msg->getFreshPeers()[0]->ipaddr);
|
||||||
|
@ -186,7 +188,9 @@ void UTPexExtensionMessageTest::testCreate()
|
||||||
try {
|
try {
|
||||||
// 0 length data
|
// 0 length data
|
||||||
std::string in = "";
|
std::string in = "";
|
||||||
UTPexExtensionMessage::create(_btContext, in.c_str(), in.size());
|
UTPexExtensionMessage::create(_btContext,
|
||||||
|
reinterpret_cast<const unsigned char*>(in.c_str()),
|
||||||
|
in.size());
|
||||||
CPPUNIT_FAIL("exception must be thrown.");
|
CPPUNIT_FAIL("exception must be thrown.");
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
|
|
Loading…
Reference in New Issue