OpenVPN
control_msg.c File Reference
#include "syshead.h"
#include "control_msg.h"
Include dependency graph for control_msg.c:

Go to the source code of this file.

Functions

bool ctrl_msg_tlv_write_header (struct buffer *buf, uint16_t type, bool optional, uint16_t value_len)
 Write a TLV header (type + optional flag + value length) to buf.
bool ctrl_msg_tlv_write_u16 (struct buffer *buf, uint16_t type, bool optional, uint16_t value)
 Write a complete TLV whose value is a single 16-bit integer to buf.
bool ctrl_msg_tlv_peek_header (const struct buffer *buf, struct ctrl_msg_tlv_header *hdr)
 Read a TLV header from buf without consuming it.
bool ctrl_msg_tlv_next (struct buffer *buf, struct ctrl_msg_tlv_header *hdr, struct buffer *value)
 Read the next TLV from buf, advancing past its header and value.
bool ctrl_msg_find_tlv (struct buffer *payload, uint16_t wanted_type, struct buffer *value)
 Scan payload for the TLV of type wanted_type, skipping any other TLV type that is marked optional.

Function Documentation

◆ ctrl_msg_find_tlv()

bool ctrl_msg_find_tlv ( struct buffer * payload,
uint16_t wanted_type,
struct buffer * value )

Scan payload for the TLV of type wanted_type, skipping any other TLV type that is marked optional.

This is for messages that carry exactly one mandatory TLV, which is what the probe messages are. Any other TLV that is not marked optional invalidates the message wherever it appears, as does a second TLV of wanted_type, so the whole sequence is walked even once the wanted TLV has been found. A message carrying several mandatory TLVs walks them with ctrl_msg_tlv_next() instead.

On success value covers exactly the found TLV's value bytes; as in ctrl_msg_tlv_next(), a header claiming more bytes than payload holds is rejected rather than reported as found. payload is consumed as it is read.

Parameters
payloadbuffer positioned at a TLV header
wanted_typethe TLV type to look for
valueset to a buffer covering the found TLV's value; it points into payload and owns no storage
Returns
true if the TLV was found, false if it is absent or present more than once, if a TLV header or value is malformed or truncated, or if a TLV we do not understand is not marked optional.

Definition at line 96 of file control_msg.c.

References BLEN, ctrl_msg_tlv_next(), ctrl_msg_tlv_header::optional, and ctrl_msg_tlv_header::type.

Referenced by test_find_tlv_empty_payload(), test_find_tlv_rejects_duplicate(), test_find_tlv_rejects_unknown_mandatory(), test_find_tlv_skips_optional_after_wanted(), test_find_tlv_trailing_bytes(), test_find_tlv_value_truncated(), test_probe_reply_flags_contradict(), test_probe_reply_roundtrip(), test_probe_request_forward_compat(), and test_probe_request_roundtrip().

◆ ctrl_msg_tlv_next()

bool ctrl_msg_tlv_next ( struct buffer * buf,
struct ctrl_msg_tlv_header * hdr,
struct buffer * value )

Read the next TLV from buf, advancing past its header and value.

Call it while BLEN(buf) > 0 to walk a whole payload.

The length from the header is validated against buf, so on success the whole value is present: a header claiming more bytes than buf holds is rejected. Nothing is consumed unless the whole TLV is there, so a failed read never leaves a partial TLV behind.

Parameters
bufbuffer positioned at a TLV header
hdrfilled with the TLV's type, optional flag and value length
valueset to a buffer covering exactly the TLV's value; it points into buf and owns no storage
Returns
true on success, false if the header or the value is truncated, with buf left untouched.

Definition at line 76 of file control_msg.c.

References ASSERT, BLEN, buf_advance(), buf_read_alloc(), buf_set_read(), CTRL_MSG_TLV_HEADER_LEN, ctrl_msg_tlv_peek_header(), and ctrl_msg_tlv_header::value_len.

Referenced by ctrl_msg_find_tlv(), parse_early_negotiation_tlvs(), test_generate_reset_packet_resend_wkc(), test_tlv_header_truncated(), test_tlv_next_value_truncated(), and test_tlv_next_walks_payload().

◆ ctrl_msg_tlv_peek_header()

bool ctrl_msg_tlv_peek_header ( const struct buffer * buf,
struct ctrl_msg_tlv_header * hdr )

Read a TLV header from buf without consuming it.

Parameters
bufbuffer positioned at the TLV header
hdrfilled with the type, optional flag and value length on success
Returns
true on success, false if there are not enough bytes for a header.

Definition at line 60 of file control_msg.c.

References BLEN, buf_read_u16(), CTRL_MSG_TLV_HEADER_LEN, CTRL_MSG_TLV_OPTIONAL_FLAG, CTRL_MSG_TLV_TYPE_MASK, ctrl_msg_tlv_header::optional, ctrl_msg_tlv_header::type, and ctrl_msg_tlv_header::value_len.

Referenced by ctrl_msg_tlv_next(), test_probe_reply_roundtrip(), test_probe_request_roundtrip(), test_tlv_header_roundtrip(), and test_tlv_header_truncated().

◆ ctrl_msg_tlv_write_header()

bool ctrl_msg_tlv_write_header ( struct buffer * buf,
uint16_t type,
bool optional,
uint16_t value_len )

Write a TLV header (type + optional flag + value length) to buf.

type must fit in the 15 bits of the type field.

Nothing is written unless the header and the value_len bytes of value that follow it both fit, so a failed write never leaves a partial TLV behind.

Returns
true on success, false if buf has insufficient space.

Definition at line 32 of file control_msg.c.

References ASSERT, buf_safe(), buf_write_u16(), CTRL_MSG_TLV_HEADER_LEN, CTRL_MSG_TLV_OPTIONAL_FLAG, and CTRL_MSG_TLV_TYPE_MASK.

Referenced by ctrl_msg_tlv_write_u16(), probe_reply_write(), probe_request_write(), test_find_tlv_rejects_duplicate(), test_find_tlv_rejects_unknown_mandatory(), test_find_tlv_skips_optional_after_wanted(), test_find_tlv_value_truncated(), test_probe_request_forward_compat(), test_tlv_header_roundtrip(), test_tlv_next_value_truncated(), test_tlv_next_walks_payload(), and test_tlv_write_no_room().

◆ ctrl_msg_tlv_write_u16()

bool ctrl_msg_tlv_write_u16 ( struct buffer * buf,
uint16_t type,
bool optional,
uint16_t value )

Write a complete TLV whose value is a single 16-bit integer to buf.

Returns
true on success, false if buf has insufficient space.

Definition at line 53 of file control_msg.c.

References buf_write_u16(), and ctrl_msg_tlv_write_header().

Referenced by test_tlv_next_walks_payload(), test_tlv_write_no_room(), test_tlv_write_u16_wire_format(), and tls_reset_standalone().