2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Make room for future enchancements for IPv6 addresses.
	Bump up version number of dht.dat file format to 2.
	Not compatible with previous version.
	* src/DHTRoutingTableSerializer.cc
	* src/DHTRoutingTableDeserializer.cc
	* test/DHTRoutingTableSerializerTest.cc
This commit is contained in:
Tatsuhiro Tsujikawa 2008-02-21 02:19:55 +00:00
parent 24b91983e1
commit 0d130b77f4
4 changed files with 73 additions and 7 deletions

View file

@ -1,3 +1,12 @@
2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Make room for future enchancements for IPv6 addresses.
Bump up version number of dht.dat file format to 2.
Not compatible with previous version.
* src/DHTRoutingTableSerializer.cc
* src/DHTRoutingTableDeserializer.cc
* test/DHTRoutingTableSerializerTest.cc
2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added handling for the return values from Added handling for the return values from

View file

@ -73,7 +73,7 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
header[2] = 0x02; header[2] = 0x02;
// version // version
header[6] = 0; header[6] = 0;
header[7] = 0x01; header[7] = 0x02;
char zero[8]; char zero[8];
memset(zero, 0, sizeof(zero)); memset(zero, 0, sizeof(zero));
@ -108,21 +108,34 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
// nodes // nodes
for(size_t i = 0; i < numNodes; ++i) { for(size_t i = 0; i < numNodes; ++i) {
// Currently, only IPv4 addresses are supported.
// 1byte compact peer info length
uint8_t peerInfoLen;
in >> peerInfoLen;
if(peerInfoLen != 6) {
// skip this entry
in.read(buf, 42+7+6);
continue;
}
// 7bytes reserved
in.read(buf, 7);
// 6bytes compact peer info // 6bytes compact peer info
in.read(buf, 6); in.read(buf, 6);
if(memcmp(zero, buf, 6) == 0) { if(memcmp(zero, buf, 6) == 0) {
// skip this entry // skip this entry
in.read(buf, 26); 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(buf);
if(peer.first.empty()) { if(peer.first.empty()) {
// skip this entry // skip this entry
in.read(buf, 26); in.read(buf, 42);
continue; continue;
} }
// 2bytes reserved // 2bytes reserved
in.read(buf, 2); in.read(buf, 2);
// 16byte reserved
in.read(buf, 16);
// localnode ID // localnode ID
in.read(buf, DHT_ID_LENGTH); in.read(buf, DHT_ID_LENGTH);

View file

@ -70,9 +70,9 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
header[2] = 0x02; header[2] = 0x02;
// version // version
header[6] = 0; header[6] = 0;
header[7] = 0x01; header[7] = 0x02;
char zero[8]; char zero[16];
memset(zero, 0, sizeof(zero)); memset(zero, 0, sizeof(zero));
try { try {
o.write(header, 8); o.write(header, 8);
@ -99,14 +99,22 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
// nodes // nodes
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _nodes.begin(); i != _nodes.end(); ++i) { for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _nodes.begin(); i != _nodes.end(); ++i) {
const SharedHandle<DHTNode>& node = *i; const SharedHandle<DHTNode>& node = *i;
// 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]; 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);
} }
// 1byte compact peer format length
o << static_cast<uint8_t>(sizeof(compactPeer));
// 7bytes reserved
o.write(zero, 7);
// 6 bytes compact peer
o.write(compactPeer, 6); o.write(compactPeer, 6);
// 2bytes reserved // 2bytes reserved
o.write(zero, 2); o.write(zero, 2);
// 16bytes reserved
o.write(zero, 16);
// 20bytes: node ID // 20bytes: node ID
o.write(reinterpret_cast<const char*>(node->getID()), DHT_ID_LENGTH); o.write(reinterpret_cast<const char*>(node->getID()), DHT_ID_LENGTH);
// 4bytes reserved // 4bytes reserved

View file

@ -48,7 +48,7 @@ void DHTRoutingTableSerializerTest::testSerialize()
std::stringstream ss; std::stringstream ss;
s.serialize(ss); s.serialize(ss);
char zero[8]; char zero[16];
memset(zero, 0, sizeof(zero)); memset(zero, 0, sizeof(zero));
char buf[20]; char buf[20];
@ -65,7 +65,7 @@ void DHTRoutingTableSerializerTest::testSerialize()
CPPUNIT_ASSERT((char)0x00 == buf[5]); CPPUNIT_ASSERT((char)0x00 == buf[5]);
// version // version
CPPUNIT_ASSERT((char)0x00 == buf[6]); CPPUNIT_ASSERT((char)0x00 == buf[6]);
CPPUNIT_ASSERT((char)0x01 == buf[7]); CPPUNIT_ASSERT((char)0x02 == buf[7]);
// time // time
ss.read(buf, 4); ss.read(buf, 4);
@ -95,6 +95,15 @@ void DHTRoutingTableSerializerTest::testSerialize()
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0); CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[0] // node[0]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)6, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 6bytes compact peer info // 6bytes compact peer info
ss.read(buf, 6); ss.read(buf, 6);
{ {
@ -105,6 +114,9 @@ void DHTRoutingTableSerializerTest::testSerialize()
// 2bytes reserved // 2bytes reserved
ss.read(buf, 2); ss.read(buf, 2);
CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0); CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0);
// 16bytes reserved
ss.read(buf, 16);
CPPUNIT_ASSERT(memcmp(zero, buf, 16) == 0);
// localnode ID // localnode ID
ss.read(buf, DHT_ID_LENGTH); ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[0]->getID(), buf, DHT_ID_LENGTH) == 0); CPPUNIT_ASSERT(memcmp(nodes[0]->getID(), buf, DHT_ID_LENGTH) == 0);
@ -113,6 +125,15 @@ void DHTRoutingTableSerializerTest::testSerialize()
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0); CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[1] // node[1]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)6, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 6bytes compact peer info // 6bytes compact peer info
ss.read(buf, 6); ss.read(buf, 6);
// zero filled because node[1]'s hostname is not numerical form // zero filled because node[1]'s hostname is not numerical form
@ -121,6 +142,9 @@ void DHTRoutingTableSerializerTest::testSerialize()
// 2bytes reserved // 2bytes reserved
ss.read(buf, 2); ss.read(buf, 2);
CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0); CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0);
// 16bytes reserved
ss.read(buf, 16);
CPPUNIT_ASSERT(memcmp(zero, buf, 16) == 0);
// localnode ID // localnode ID
ss.read(buf, DHT_ID_LENGTH); ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[1]->getID(), buf, DHT_ID_LENGTH) == 0); CPPUNIT_ASSERT(memcmp(nodes[1]->getID(), buf, DHT_ID_LENGTH) == 0);
@ -129,6 +153,15 @@ void DHTRoutingTableSerializerTest::testSerialize()
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0); CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[2] // node[2]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)6, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 6bytes compact peer info // 6bytes compact peer info
ss.read(buf, 6); ss.read(buf, 6);
{ {
@ -139,6 +172,9 @@ void DHTRoutingTableSerializerTest::testSerialize()
// 2bytes reserved // 2bytes reserved
ss.read(buf, 2); ss.read(buf, 2);
CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0); CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0);
// 16bytes reserved
ss.read(buf, 16);
CPPUNIT_ASSERT(memcmp(zero, buf, 16) == 0);
// localnode ID // localnode ID
ss.read(buf, DHT_ID_LENGTH); ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[2]->getID(), buf, DHT_ID_LENGTH) == 0); CPPUNIT_ASSERT(memcmp(nodes[2]->getID(), buf, DHT_ID_LENGTH) == 0);