networking · intermediate · ~15 min

TLS record length

Read the big-endian record length.

Challenge

After the content type and 2-byte version, a TLS record header carries a big-endian 16-bit length at offset 3. It tells you how many bytes of record body follow the header. Read it.

Task

Implement int tls_record_length(const unsigned char *rec) that returns the big-endian length, (rec[3] << 8) | rec[4].

Input

A pointer rec to the TLS record bytes (at least 5 bytes).

Output

Returns the 16-bit record length.

Example

{22, 0x03,0x03, 0x00,0x05}   ->   5
{23, 0x03,0x03, 0x01,0x2C}   ->   300

Rules

  • The length is big-endian: high byte at offset 3, low byte at offset 4.

Input format

A pointer rec to the TLS record bytes (at least 5 bytes).

Output format

The 16-bit record length (rec[3] << 8) | rec[4].

Constraints

The length is a big-endian u16 at offset 3.

Starter code

int tls_record_length(const unsigned char *rec) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.