mirror of https://github.com/aria2/aria2
2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Use array_wrapper instead of array_ptr. * src/DHTRoutingTableDeserializer.ccpull/1/head
parent
b521bd5752
commit
83caf2903b
|
@ -1,3 +1,8 @@
|
||||||
|
2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Use array_wrapper instead of array_ptr.
|
||||||
|
* src/DHTRoutingTableDeserializer.cc
|
||||||
|
|
||||||
2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Removed template<typename S> array_ptr(const array_ptr<S>& s)
|
Removed template<typename S> array_ptr(const array_ptr<S>& s)
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <cassert>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -65,6 +66,13 @@ const std::deque<SharedHandle<DHTNode> >& DHTRoutingTableDeserializer::getNodes(
|
||||||
return _nodes;
|
return _nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::istream& readBytes(unsigned char* buf, size_t buflen,
|
||||||
|
std::istream& in, size_t readlen)
|
||||||
|
{
|
||||||
|
assert(readlen <= buflen);
|
||||||
|
return in.read(reinterpret_cast<char*>(buf), readlen);
|
||||||
|
}
|
||||||
|
|
||||||
void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -97,11 +105,10 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
|
|
||||||
// If you change the code to read more than the size of buf, then
|
// If you change the code to read more than the size of buf, then
|
||||||
// expand the buf size here.
|
// expand the buf size here.
|
||||||
char* buf = new char[255];
|
array_wrapper<unsigned char, 255> buf;
|
||||||
array_ptr<char> holder(buf);
|
|
||||||
|
|
||||||
// header
|
// header
|
||||||
in.read(buf, 8);
|
readBytes(buf, buf.size(), in, 8);
|
||||||
if(memcmp(header, buf, 8) == 0) {
|
if(memcmp(header, buf, 8) == 0) {
|
||||||
version = 3;
|
version = 3;
|
||||||
} else if(memcmp(headerCompat, buf, 8) == 0) {
|
} else if(memcmp(headerCompat, buf, 8) == 0) {
|
||||||
|
@ -111,31 +118,34 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
(StringFormat("Failed to load DHT routing table. cause:%s",
|
(StringFormat("Failed to load DHT routing table. cause:%s",
|
||||||
"bad header").str());
|
"bad header").str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t temp32;
|
||||||
|
uint64_t temp64;
|
||||||
// time
|
// time
|
||||||
if(version == 2) {
|
if(version == 2) {
|
||||||
in.read(buf, 4);
|
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
|
||||||
_serializedTime.setTimeInSec(ntohl(*reinterpret_cast<uint32_t*>(buf)));
|
_serializedTime.setTimeInSec(ntohl(temp32));
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
in.read(buf, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
} else {
|
} else {
|
||||||
in.read(buf, 8);
|
in.read(reinterpret_cast<char*>(&temp64), sizeof(temp64));
|
||||||
_serializedTime.setTimeInSec(ntoh64(*reinterpret_cast<uint64_t*>(buf)));
|
_serializedTime.setTimeInSec(ntoh64(temp64));
|
||||||
}
|
}
|
||||||
|
|
||||||
// localnode
|
// localnode
|
||||||
// 8bytes reserved
|
// 8bytes reserved
|
||||||
in.read(buf, 8);
|
readBytes(buf, buf.size(), in, 8);
|
||||||
// localnode ID
|
// localnode ID
|
||||||
in.read(buf, DHT_ID_LENGTH);
|
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
|
||||||
SharedHandle<DHTNode> localNode(new DHTNode(reinterpret_cast<const unsigned char*>(buf)));
|
SharedHandle<DHTNode> localNode(new DHTNode(buf));
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
in.read(buf, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
|
||||||
// number of nodes
|
// number of nodes
|
||||||
in.read(buf, 4);
|
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
|
||||||
uint32_t numNodes = ntohl(*reinterpret_cast<uint32_t*>(buf));
|
uint32_t numNodes = ntohl(temp32);
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
in.read(buf, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
|
||||||
// nodes
|
// nodes
|
||||||
for(size_t i = 0; i < numNodes; ++i) {
|
for(size_t i = 0; i < numNodes; ++i) {
|
||||||
|
@ -145,37 +155,37 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
in >> peerInfoLen;
|
in >> peerInfoLen;
|
||||||
if(peerInfoLen != 6) {
|
if(peerInfoLen != 6) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
in.read(buf, 42+7+6);
|
readBytes(buf, buf.size(), in, 42+7+6);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 7bytes reserved
|
// 7bytes reserved
|
||||||
in.read(buf, 7);
|
readBytes(buf, buf.size(), in, 7);
|
||||||
// 6bytes compact peer info
|
// 6bytes compact peer info
|
||||||
in.read(buf, 6);
|
readBytes(buf, buf.size(), in, 6);
|
||||||
if(memcmp(zero, buf, 6) == 0) {
|
if(memcmp(zero, buf, 6) == 0) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
in.read(buf, 42);
|
readBytes(buf, buf.size(), in, 42);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::pair<std::string, uint16_t> peer =
|
std::pair<std::string, uint16_t> peer =
|
||||||
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
|
PeerMessageUtil::unpackcompact(buf);
|
||||||
if(peer.first.empty()) {
|
if(peer.first.empty()) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
in.read(buf, 42);
|
readBytes(buf, buf.size(), in, 42);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 2bytes reserved
|
// 2bytes reserved
|
||||||
in.read(buf, 2);
|
readBytes(buf, buf.size(), in, 2);
|
||||||
// 16byte reserved
|
// 16byte reserved
|
||||||
in.read(buf, 16);
|
readBytes(buf, buf.size(), in, 16);
|
||||||
// localnode ID
|
// localnode ID
|
||||||
in.read(buf, DHT_ID_LENGTH);
|
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
|
||||||
|
|
||||||
SharedHandle<DHTNode> node(new DHTNode(reinterpret_cast<const unsigned char*>(buf)));
|
SharedHandle<DHTNode> node(new DHTNode(buf));
|
||||||
node->setIPAddress(peer.first);
|
node->setIPAddress(peer.first);
|
||||||
node->setPort(peer.second);
|
node->setPort(peer.second);
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
in.read(buf, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
|
||||||
_nodes.push_back(node);
|
_nodes.push_back(node);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue