From e801c0c7204df9c566b503a7c4c2d828166796f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Sun, 2 Jun 2019 13:55:38 +0300 Subject: Protocol: fixed typos and permissions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- include/tlv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/tlv.h b/include/tlv.h index b52763a..63507c1 100644 --- a/include/tlv.h +++ b/include/tlv.h @@ -150,7 +150,7 @@ struct tlv_parser { /** * Fills tlv structure to represent the next tlv in the packet. * - * Returns END_OF_PACKET if all tlv's were read of E_TLV_OVERFLOW, if the last + * Returns END_OF_PACKET if all tlv's were read or E_TLV_OVERFLOW, if the last * tlv, according to its declared size should not fit in a packet. */ int tlv_get(struct tlv_parser *parser, struct tlv *ret); -- cgit v1.2.3 From 9d8c56601d37e420143ef5aeec98d9ab9c5a60fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 4 Jun 2019 10:21:03 +0300 Subject: Protocol: reimplementation of tlv_get according to new spec. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- include/tlv.h | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/tlv.h b/include/tlv.h index 63507c1..75aa133 100644 --- a/include/tlv.h +++ b/include/tlv.h @@ -62,6 +62,8 @@ A few things to note: #include +#include +#include #define E_TLV_OVERFLOW (1 << 0) #define E_UNKNOWN_TYPE (1 << 1) @@ -79,7 +81,18 @@ extern "C" { */ typedef unsigned int msg_idx_t; +#if INT32_MAX == INT_MAX +#define READ_ENUM(data) htonl((data)) +#else INT_MAX == INT16_MAX +#define READ_ENUM(data) htons((data)) +#endif + enum tlv_type { + /** + * Explicitly states that this tlv is no longer valid for reading. + */ + INVALID = 0, + /** * NULL-terminated string. To be put in a queue to display on the * screen. @@ -132,8 +145,8 @@ enum tlv_type { * */ struct tlv { enum tlv_type type; - size_t length; - void *data; + uint16_t length; + const void *data; }; /** @@ -142,11 +155,18 @@ struct tlv { * Related functions return one tlv at a time. */ struct tlv_parser { - char *data; - size_t offset; - size_t size; + const char *data; + uint16_t offset; }; +/** + * Initialises parser to begin parsing the data. + * + * Returns the first tlv which should be a meta-tlv. If it is not --- you got + * problems. + */ +int tlv_parser_init(struct tlv_parser *parser, char *data, struct tlv *ret); + /** * Fills tlv structure to represent the next tlv in the packet. * @@ -175,14 +195,14 @@ size_t tlv_raw_size(const 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); +int tlv_get_raw(const struct tlv *t, char *buf); /** * 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. */ -int tlv_push_data(struct tlv *t, const char *data, size_t size); +int tlv_push_data(struct tlv *t, const char *data, uint16_t size); /** * Pushes a sub-tlv into the packet. 't' can only be REGURAL, HEARTBEAT or -- cgit v1.2.3