diff options
author | 2019-06-04 10:21:03 +0300 | |
---|---|---|
committer | 2019-06-04 10:21:03 +0300 | |
commit | 9d8c56601d37e420143ef5aeec98d9ab9c5a60fd (patch) | |
tree | 3c63662c9562eba85cbad17c2db698a31f7a5b88 /include/tlv.h | |
parent | e801c0c7204df9c566b503a7c4c2d828166796f8 (diff) | |
download | usurpation-9d8c56601d37e420143ef5aeec98d9ab9c5a60fd.tar.gz usurpation-9d8c56601d37e420143ef5aeec98d9ab9c5a60fd.tar.bz2 usurpation-9d8c56601d37e420143ef5aeec98d9ab9c5a60fd.zip |
Protocol: reimplementation of tlv_get according to new spec.
Signed-off-by: Ramūnas Mažeikis <ramunasnezinomas@gmail.com>
Diffstat (limited to 'include/tlv.h')
-rw-r--r-- | include/tlv.h | 34 |
1 files changed, 27 insertions, 7 deletions
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 <errno.h> +#include <stdint.h> +#include <limits.h> #define E_TLV_OVERFLOW (1 << 0) #define E_UNKNOWN_TYPE (1 << 1) @@ -79,8 +81,19 @@ 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,12 +155,19 @@ 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. * * Returns END_OF_PACKET if all tlv's were read or E_TLV_OVERFLOW, if the last @@ -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 |