Codechange: allow different limits in packet sizes

This commit is contained in:
Rubidium 2021-04-18 14:42:06 +02:00 committed by rubidium42
parent 97288bc286
commit 8b302761d4
4 changed files with 23 additions and 14 deletions

View File

@ -19,6 +19,7 @@
/** /**
* Create a packet that is used to read from a network socket. * Create a packet that is used to read from a network socket.
* @param cs The socket handler associated with the socket we are reading from. * @param cs The socket handler associated with the socket we are reading from.
* @param limit The maximum size of packets to accept.
* @param initial_read_size The initial amount of data to transfer from the socket into the * @param initial_read_size The initial amount of data to transfer from the socket into the
* packet. This defaults to just the required bytes to determine the * packet. This defaults to just the required bytes to determine the
* packet's size. That default is the wanted for streams such as TCP * packet's size. That default is the wanted for streams such as TCP
@ -27,7 +28,7 @@
* loose some the data of the packet, so there you pass the maximum * loose some the data of the packet, so there you pass the maximum
* size for the packet you expect from the network. * size for the packet you expect from the network.
*/ */
Packet::Packet(NetworkSocketHandler *cs, size_t initial_read_size) : next(nullptr), pos(0) Packet::Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size) : next(nullptr), pos(0), limit(limit)
{ {
assert(cs != nullptr); assert(cs != nullptr);
@ -37,9 +38,13 @@ Packet::Packet(NetworkSocketHandler *cs, size_t initial_read_size) : next(nullpt
/** /**
* Creates a packet to send * Creates a packet to send
* @param type of the packet to send * @param type The type of the packet to send
* @param limit The maximum number of bytes the packet may have. Default is SEND_MTU.
* Be careful of compatibility with older clients/servers when changing
* the limit as it might break things if the other side is not expecting
* much larger packets than what they support.
*/ */
Packet::Packet(PacketType type) : next(nullptr), pos(0), cs(nullptr) Packet::Packet(PacketType type, size_t limit) : next(nullptr), pos(0), cs(nullptr)
{ {
/* Allocate space for the the size so we can write that in just before sending the packet. */ /* Allocate space for the the size so we can write that in just before sending the packet. */
this->Send_uint16(0); this->Send_uint16(0);
@ -93,7 +98,7 @@ void Packet::PrepareToSend()
*/ */
bool Packet::CanWriteToPacket(size_t bytes_to_write) bool Packet::CanWriteToPacket(size_t bytes_to_write)
{ {
return this->Size() + bytes_to_write < SEND_MTU; return this->Size() + bytes_to_write < this->limit;
} }
/* /*
@ -191,7 +196,7 @@ void Packet::Send_string(const char *data)
*/ */
size_t Packet::Send_bytes(const byte *begin, const byte *end) size_t Packet::Send_bytes(const byte *begin, const byte *end)
{ {
size_t amount = std::min<size_t>(end - begin, SEND_MTU - this->Size()); size_t amount = std::min<size_t>(end - begin, this->limit - this->Size());
this->buffer.insert(this->buffer.end(), begin, begin + amount); this->buffer.insert(this->buffer.end(), begin, begin + amount);
return amount; return amount;
} }
@ -260,7 +265,7 @@ bool Packet::ParsePacketSize()
/* If the size of the packet is less than the bytes required for the size and type of /* 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. * 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. */ * In those cases the packet can generally be regarded as containing garbage data. */
if (size < sizeof(PacketSize) + sizeof(PacketType) || size > SEND_MTU) return false; if (size < sizeof(PacketSize) + sizeof(PacketType) || size > this->limit) return false;
this->buffer.resize(size); this->buffer.resize(size);
this->pos = sizeof(PacketSize); this->pos = sizeof(PacketSize);

View File

@ -25,10 +25,11 @@ typedef uint8 PacketType; ///< Identifier for the packet
* Internal entity of a packet. As everything is sent as a packet, * Internal entity of a packet. As everything is sent as a packet,
* all network communication will need to call the functions that * all network communication will need to call the functions that
* populate the packet. * populate the packet.
* Every packet can be at most SEND_MTU bytes. Overflowing this * Every packet can be at most a limited number bytes set in the
* limit will give an assertion when sending (i.e. writing) the * constructor. Overflowing this limit will give an assertion when
* packet. Reading past the size of the packet when receiving * sending (i.e. writing) the packet. Reading past the size of the
* will return all 0 values and "" in case of the string. * packet when receiving will return all 0 values and "" in case of
* the string.
* *
* --- Points of attention --- * --- Points of attention ---
* - all > 1 byte integral values are written in little endian, * - all > 1 byte integral values are written in little endian,
@ -47,13 +48,15 @@ private:
PacketSize pos; PacketSize pos;
/** The buffer of this packet. */ /** The buffer of this packet. */
std::vector<byte> buffer; std::vector<byte> buffer;
/** The limit for the packet size. */
size_t limit;
/** Socket we're associated with. */ /** Socket we're associated with. */
NetworkSocketHandler *cs; NetworkSocketHandler *cs;
public: public:
Packet(NetworkSocketHandler *cs, size_t initial_read_size = sizeof(PacketSize)); Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = sizeof(PacketSize));
Packet(PacketType type); Packet(PacketType type, size_t limit = SEND_MTU);
static void AddToQueue(Packet **queue, Packet *packet); static void AddToQueue(Packet **queue, Packet *packet);
static Packet *PopFromQueue(Packet **queue); static Packet *PopFromQueue(Packet **queue);

View File

@ -126,7 +126,7 @@ Packet *NetworkTCPSocketHandler::ReceivePacket()
if (!this->IsConnected()) return nullptr; if (!this->IsConnected()) return nullptr;
if (this->packet_recv == nullptr) { if (this->packet_recv == nullptr) {
this->packet_recv = new Packet(this); this->packet_recv = new Packet(this, SEND_MTU);
} }
Packet *p = this->packet_recv; Packet *p = this->packet_recv;

View File

@ -119,7 +119,8 @@ void NetworkUDPSocketHandler::ReceivePackets()
struct sockaddr_storage client_addr; struct sockaddr_storage client_addr;
memset(&client_addr, 0, sizeof(client_addr)); memset(&client_addr, 0, sizeof(client_addr));
Packet p(this, SEND_MTU); /* The limit is SEND_MTU, but also allocate that much as we need to read the whole packet in one go. */
Packet p(this, SEND_MTU, SEND_MTU);
socklen_t client_len = sizeof(client_addr); socklen_t client_len = sizeof(client_addr);
/* Try to receive anything */ /* Try to receive anything */