packet_structs: check data length in layer2_decode_packet_header()

This commit is contained in:
Thomas Kolb 2024-07-04 21:37:05 +02:00
parent 153ff61dcd
commit 402e36dda5
2 changed files with 22 additions and 7 deletions

View file

@ -33,13 +33,27 @@ size_t layer2_encode_packet_header(const layer2_packet_header_t *header, uint8_t
}
bool layer2_decode_packet_header(const uint8_t *encoded, layer2_packet_header_t *header)
bool layer2_decode_packet_header(const uint8_t *encoded, size_t encoded_len, layer2_packet_header_t *header)
{
// check if there are enough bytes for the minimum header size:
// - 1 byte packet info
// - 1 byte sequence numbers
// - 2 bytes source address
// - 2 bytes destination address
if(encoded_len < 6) {
return false;
}
header->msg_type = (encoded[0] >> 5) & 0x7;
header->tx_request = (encoded[0] & 0x10) != 0;
header->src_addr.length = (encoded[0] >> 2) & 0x3 + 1;
header->dst_addr.length = (encoded[0] >> 0) & 0x3 + 1;
// check for the actually needed size
if(encoded_len < (2 + 2*header->src_addr.length + 2*header->dst_addr.length)) {
return false;
}
header->tx_seq_nr = (encoded[1] >> 4) & 0xF;
header->tx_seq_nr = (encoded[1] >> 0) & 0xF;

View file

@ -19,8 +19,8 @@ typedef struct layer2_packet_header_s {
layer2_message_type_t msg_type; //!< message type
bool tx_request; //!< transmission request (marks the end of the burst)
uint8_t tx_seq_nr; //!< sequence number of this packet
uint8_t rx_seq_nr; //!< sequence number expected next from he other side
uint8_t tx_seq_nr; //!< sequence number (0 to 15) of this packet
uint8_t rx_seq_nr; //!< sequence number (0 to 15) expected next from he other side
ham64_t src_addr; //!< source HAM-64 address
ham64_t dst_addr; //!< destination HAM-64 address
@ -35,10 +35,11 @@ size_t layer2_encode_packet_header(const layer2_packet_header_t *header, uint8_t
/*!\brief Decode a layer2 packet header from the received form.
* \param encoded A byte array containing the received header.
* \param encoded_len Length (in bytes) of the given encoded data.
* \param header The header structure where the decoded information is stored.
* \returns Whether the header was decoded successfully.
*/
bool layer2_decode_packet_header(const uint8_t *encoded, layer2_packet_header_t *header);
bool layer2_decode_packet_header(const uint8_t *encoded, size_t encoded_len, layer2_packet_header_t *header);
/* Data Packet Structs */