Correct byte order for node ports

This commit is contained in:
momo5502 2016-06-26 17:28:47 +02:00
parent b83d85b651
commit 3746bceec4
4 changed files with 17 additions and 14 deletions

View File

@ -114,12 +114,12 @@ namespace Components
void Network::Address::Serialize(Proto::Network::Address* protoAddress)
{
protoAddress->set_ip(this->GetIP().full);
protoAddress->set_port(htons(this->GetPort()) & 0xFFFF);
protoAddress->set_port(this->GetPort() & 0xFFFF);
}
void Network::Address::Deserialize(const Proto::Network::Address& protoAddress)
{
this->SetIP(protoAddress.ip());
this->SetPort(ntohs(static_cast<uint16_t>(protoAddress.port() & 0xFFFF)));
this->SetPort(static_cast<uint16_t>(protoAddress.port()));
this->SetType(Game::netadrtype_t::NA_IP);
}

View File

@ -143,6 +143,7 @@ namespace Components
Proto::Node::List list;
list.set_is_dedi(Dedicated::IsDedicated());
list.set_protocol(PROTOCOL);
list.set_version(NODE_VERSION);
for (auto node : Node::Nodes)
{
@ -165,22 +166,13 @@ namespace Components
void Node::DeleteInvalidSessions()
{
std::vector<Node::ClientSession> cleanSessions;
for (auto session : Node::Sessions)
for (auto i = Node::Sessions.begin(); i != Node::Sessions.end(); ++i)
{
if (session.lastTime > 0 && (Game::Com_Milliseconds() - session.lastTime) <= SESSION_TIMEOUT)
if (i->lastTime <= 0 || (Game::Com_Milliseconds() - i->lastTime) > SESSION_TIMEOUT)
{
cleanSessions.push_back(session);
i = Node::Sessions.erase(i);
}
}
if (cleanSessions.size() != Node::Sessions.size())
{
//Node::Sessions.clear();
//Utils::Merge(&Node::Sessions, cleanSessions);
Node::Sessions = cleanSessions;
}
}
void Node::DeleteInvalidNodes()
@ -692,6 +684,7 @@ namespace Components
entry->isDedi = list.is_dedi();
entry->protocol = list.protocol();
entry->version = list.version();
entry->state = Node::STATE_VALID;
entry->lastTime = Game::Com_Milliseconds();
@ -704,6 +697,12 @@ namespace Components
{
Network::Address _addr(list.address(i));
// Version 0 sends port in the wrong byte order!
if (entry->version <= 0)
{
_addr.SetPort(ntohs(_addr.GetPort()));
}
// if (!Node::FindNode(_addr) && _addr.GetPort() >= 1024 && _addr.GetPort() - 20 < 1024)
// {
// std::string a1 = _addr.GetString();

View File

@ -7,6 +7,8 @@
#define NODE_STORE_INTERVAL 1000 * 60* 1 // Store nodes every minute
#define SESSION_TIMEOUT 1000 * 10 // 10 seconds session timeout
#define NODE_VERSION 1
namespace Components
{
class Node : public Component
@ -47,6 +49,7 @@ namespace Components
// This is only relevant for clients
bool isDedi;
uint32_t protocol;
uint32_t version;
};
struct ClientSession

View File

@ -15,4 +15,5 @@ message List
bool is_dedi = 1;
repeated Network.Address address = 2;
uint32 protocol = 3;
uint32 version = 4;
}