networking · intermediate · ~15 min

TLS record content type

Read the TLS record type byte.

Challenge

A TLS record header is type(1) + version(2) + length(2). The first byte is the content type (22 = handshake, 23 = application data). Read it.

Task

Implement int tls_record_type(const unsigned char *rec) that returns the record's content type byte, rec[0].

Input

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

Output

Returns the content type byte rec[0].

Example

{22, 0x03,0x03, 0x00,0x05}   ->   22   (handshake)
{23, 0x03,0x03, 0x01,0x00}   ->   23   (application data)

Input format

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

Output format

The content type byte rec[0].

Constraints

The content type is the first byte of the record.

Starter code

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

Common mistakes

Assuming input is well-formed; omitting validation on error return codes; leaking allocated resources on failure branches.

Edge cases to handle

NULL pointers or empty inputs; unexpected boundary values; malformed tokens or non-printable characters.

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