Browse Source

Peer save/restore fix.

Adam Ierymenko 9 years ago
parent
commit
f9f60f89d9
2 changed files with 14 additions and 25 deletions
  1. 4 4
      node/Peer.hpp
  2. 10 21
      node/Topology.cpp

+ 4 - 4
node/Peer.hpp

@@ -470,7 +470,7 @@ public:
 		b.append((uint16_t)_vRevision);
 		b.append((uint16_t)_vRevision);
 		b.append((uint32_t)_latency);
 		b.append((uint32_t)_latency);
 
 
-		b.append((uint32_t)_numPaths);
+		b.append((uint16_t)_numPaths);
 		for(unsigned int i=0;i<_numPaths;++i)
 		for(unsigned int i=0;i<_numPaths;++i)
 			_paths[i].serialize(b);
 			_paths[i].serialize(b);
 
 
@@ -497,7 +497,7 @@ public:
 			}
 			}
 		}
 		}
 
 
-		b.setAt(recSizePos,(uint32_t)((b.size() - 4) - recSizePos)); // set size
+		b.template setAt<uint32_t>(recSizePos,(uint32_t)(b.size() - (recSizePos + 4))); // set size
 	}
 	}
 
 
 	/**
 	/**
@@ -511,7 +511,7 @@ public:
 	template<unsigned int C>
 	template<unsigned int C>
 	static inline SharedPtr<Peer> deserializeNew(const Identity &myIdentity,const Buffer<C> &b,unsigned int &p)
 	static inline SharedPtr<Peer> deserializeNew(const Identity &myIdentity,const Buffer<C> &b,unsigned int &p)
 	{
 	{
-		const uint32_t recSize = b.template at<uint32_t>(p); p += 4;
+		const unsigned int recSize = b.template at<uint32_t>(p); p += 4;
 		if ((p + recSize) > b.size())
 		if ((p + recSize) > b.size())
 			return SharedPtr<Peer>(); // size invalid
 			return SharedPtr<Peer>(); // size invalid
 		if (b.template at<uint16_t>(p) != 1)
 		if (b.template at<uint16_t>(p) != 1)
@@ -540,7 +540,7 @@ public:
 		np->_vRevision = b.template at<uint16_t>(p); p += 2;
 		np->_vRevision = b.template at<uint16_t>(p); p += 2;
 		np->_latency = b.template at<uint32_t>(p); p += 4;
 		np->_latency = b.template at<uint32_t>(p); p += 4;
 
 
-		const unsigned int numPaths = b.template at<uint32_t>(p); p += 4;
+		const unsigned int numPaths = b.template at<uint16_t>(p); p += 2;
 		for(unsigned int i=0;i<numPaths;++i) {
 		for(unsigned int i=0;i<numPaths;++i) {
 			if (i < ZT_MAX_PEER_NETWORK_PATHS) {
 			if (i < ZT_MAX_PEER_NETWORK_PATHS) {
 				p += np->_paths[np->_numPaths++].deserialize(b,p);
 				p += np->_paths[np->_numPaths++].deserialize(b,p);

+ 10 - 21
node/Topology.cpp

@@ -47,31 +47,20 @@ Topology::Topology(const RuntimeEnvironment *renv) :
 
 
 	unsigned int ptr = 0;
 	unsigned int ptr = 0;
 	while ((ptr + 4) < alls.size()) {
 	while ((ptr + 4) < alls.size()) {
-		// Each Peer serializes itself prefixed by a record length (not including the size of the length itself)
-		unsigned int reclen = (unsigned int)all[ptr] & 0xff;
-		reclen <<= 8;
-		reclen |= (unsigned int)all[ptr + 1] & 0xff;
-		reclen <<= 8;
-		reclen |= (unsigned int)all[ptr + 2] & 0xff;
-		reclen <<= 8;
-		reclen |= (unsigned int)all[ptr + 3] & 0xff;
-
-		if (((ptr + reclen) > alls.size())||(reclen > ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE))
-			break;
-
 		try {
 		try {
+			const unsigned int reclen = ( // each Peer serialized record is prefixed by a record length
+					((((unsigned int)all[ptr]) & 0xff) << 24) |
+					((((unsigned int)all[ptr + 1]) & 0xff) << 16) |
+					((((unsigned int)all[ptr + 2]) & 0xff) << 8) |
+					(((unsigned int)all[ptr + 3]) & 0xff)
+				);
 			unsigned int pos = 0;
 			unsigned int pos = 0;
-			SharedPtr<Peer> p(Peer::deserializeNew(RR->identity,Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>(all + ptr,reclen),pos));
-			if (pos != reclen)
-				break;
+			SharedPtr<Peer> p(Peer::deserializeNew(RR->identity,Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>(all + ptr,reclen + 4),pos));
 			ptr += pos;
 			ptr += pos;
-			if ((p)&&(p->address() != RR->identity.address())) {
-				_peers[p->address()] = p;
-			} else {
+			if (!p)
 				break; // stop if invalid records
 				break; // stop if invalid records
-			}
-		} catch (std::exception &exc) {
-			break;
+			if (p->address() != RR->identity.address())
+				_peers[p->address()] = p;
 		} catch ( ... ) {
 		} catch ( ... ) {
 			break; // stop if invalid records
 			break; // stop if invalid records
 		}
 		}