mirror of https://github.com/OpenTTD/OpenTTD
Codechange: move more logic about packet size validity and reading into Packet
parent
470d8b6637
commit
c545cc9d70
|
@ -181,13 +181,32 @@ bool Packet::CanReadFromPacket(uint bytes_to_read)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the packet size from the raw packet and stores it in the packet->size
|
* Check whether the packet, given the position of the "write" pointer, has read
|
||||||
|
* enough of the packet to contain its size.
|
||||||
|
* @return True iff there is enough data in the packet to contain the packet's size.
|
||||||
*/
|
*/
|
||||||
void Packet::ReadRawPacketSize()
|
bool Packet::HasPacketSizeData() const
|
||||||
|
{
|
||||||
|
return this->pos >= sizeof(PacketSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the packet size from the raw packet and stores it in the packet->size
|
||||||
|
* @return True iff the packet size seems plausible.
|
||||||
|
*/
|
||||||
|
bool Packet::ParsePacketSize()
|
||||||
{
|
{
|
||||||
assert(this->cs != nullptr && this->next == nullptr);
|
assert(this->cs != nullptr && this->next == nullptr);
|
||||||
this->size = (PacketSize)this->buffer[0];
|
this->size = (PacketSize)this->buffer[0];
|
||||||
this->size += (PacketSize)this->buffer[1] << 8;
|
this->size += (PacketSize)this->buffer[1] << 8;
|
||||||
|
|
||||||
|
/* If the size of the packet is less than the bytes required for the size and type of
|
||||||
|
* the packet, or more than the allowed limit, then something is wrong with the packet.
|
||||||
|
* In those cases the packet can generally be regarded as containing garbage data. */
|
||||||
|
if (this->size < sizeof(PacketSize) + sizeof(PacketType) || this->size > SEND_MTU) return false;
|
||||||
|
|
||||||
|
this->pos = sizeof(PacketSize);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -195,8 +214,6 @@ void Packet::ReadRawPacketSize()
|
||||||
*/
|
*/
|
||||||
void Packet::PrepareToRead()
|
void Packet::PrepareToRead()
|
||||||
{
|
{
|
||||||
this->ReadRawPacketSize();
|
|
||||||
|
|
||||||
/* Put the position on the right place */
|
/* Put the position on the right place */
|
||||||
this->pos = sizeof(PacketSize);
|
this->pos = sizeof(PacketSize);
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,8 @@ public:
|
||||||
void Send_string(const char *data);
|
void Send_string(const char *data);
|
||||||
|
|
||||||
/* Reading/receiving of packets */
|
/* Reading/receiving of packets */
|
||||||
void ReadRawPacketSize();
|
bool HasPacketSizeData() const;
|
||||||
|
bool ParsePacketSize();
|
||||||
void PrepareToRead();
|
void PrepareToRead();
|
||||||
|
|
||||||
bool CanReadFromPacket (uint bytes_to_read);
|
bool CanReadFromPacket (uint bytes_to_read);
|
||||||
|
|
|
@ -155,8 +155,8 @@ Packet *NetworkTCPSocketHandler::ReceivePacket()
|
||||||
Packet *p = this->packet_recv;
|
Packet *p = this->packet_recv;
|
||||||
|
|
||||||
/* Read packet size */
|
/* Read packet size */
|
||||||
if (p->pos < sizeof(PacketSize)) {
|
if (!p->HasPacketSizeData()) {
|
||||||
while (p->pos < sizeof(PacketSize)) {
|
while (!p->HasPacketSizeData()) {
|
||||||
/* Read the size of the packet */
|
/* Read the size of the packet */
|
||||||
res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
|
res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
|
||||||
if (res == -1) {
|
if (res == -1) {
|
||||||
|
@ -178,10 +178,8 @@ Packet *NetworkTCPSocketHandler::ReceivePacket()
|
||||||
p->pos += res;
|
p->pos += res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read the packet size from the received packet */
|
/* Parse the size in the received packet and if not valid, close the connection. */
|
||||||
p->ReadRawPacketSize();
|
if (!p->ParsePacketSize()) {
|
||||||
|
|
||||||
if (p->size > SEND_MTU) {
|
|
||||||
this->CloseConnection();
|
this->CloseConnection();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,14 +134,14 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
NetworkAddress address(client_addr, client_len);
|
NetworkAddress address(client_addr, client_len);
|
||||||
p.PrepareToRead();
|
|
||||||
|
|
||||||
/* If the size does not match the packet must be corrupted.
|
/* If the size does not match the packet must be corrupted.
|
||||||
* Otherwise it will be marked as corrupted later on. */
|
* Otherwise it will be marked as corrupted later on. */
|
||||||
if (nbytes != p.size) {
|
if (!p.ParsePacketSize() || nbytes != p.size) {
|
||||||
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString().c_str());
|
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString().c_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
p.PrepareToRead();
|
||||||
|
|
||||||
/* Handle the packet */
|
/* Handle the packet */
|
||||||
this->HandleUDPPacket(&p, &address);
|
this->HandleUDPPacket(&p, &address);
|
||||||
|
|
Loading…
Reference in New Issue