From 9f6e34103aeb44fafdcd8d4878281c783b7c9ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 28 May 2019 20:36:14 +0300 Subject: Protocol: new functions and associated docs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The header declares new functions: * tlv_init() * tlv_destroy() * tlv_push() * tlv_get_raw() * tlv_raw_size() Signed-off-by: Ramūnas Mažeikis --- include/protocol.h | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'include/protocol.h') diff --git a/include/protocol.h b/include/protocol.h index 34ad4a3..38f5aa9 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -23,10 +23,11 @@ #include -#define E_TLV_OVERFLOW (1 << 0) +#define E_TLV_OVERFLOW (1 << 0) #define E_UNKNOWN_TYPE (1 << 1) -#define E_IVALID_DESCRIPTOR (1 << 2) -#define END_OF_PACKET (1 << 3) +#define END_OF_PACKET (1 << 2) + +#define TLV_SZ_MAX_RAW (MTU - 64) /** * Regular packets contain tlv's defined by tlv_type. @@ -122,16 +123,38 @@ int get_tlv(struct tlv_parser *parser, struct tlv *ret); * */ int push_data(struct tlv_packet *packet, enum tlv_type type, char *data); - /** * Resets offset to 0 and set entire buffer to 0. */ void clear_data(struct tlv_packet *packet); +/** + * Initialises tlv to sane values. + */ +void tlv_init(struct tlv *t); + +/** + * Frees data held in the tlv structure. The structure itself shall be freed as + * needed by calling code. + */ +void tlv_destroy(struct tlv *t); + +/** + * Tells amount of bytes needed to push tlv to a buffer + */ +size_t tlv_raw_size(struct tlv *t); + +/** + * Pushes tlv to buffer as contiguous data. Check tlv size with tlv_raw_size + * beforehand. If you don't do that and overflow --- tough tiddy. + */ +int tlv_get_raw(struct tlv *t, char *buf); /** - * Tells what size of buffer is needed for next tlv. + * Pushes data to tlv. Returns E_TLV_OVERFLOW if pushing data would cause the + * final size to be greater than TLV_SZ_MAX_RAW. In case of such error the data is left + * untouched. */ -size_t tlv_data_size(struct tlv_parser *parser); +int tlv_push(struct tlv *t, const char *data, size_t size); #endif /* USURPATION_PROTOCOL_H_INCLUDED */ -- cgit v1.2.3