diff options
author | 2019-05-28 20:36:14 +0300 | |
---|---|---|
committer | 2019-05-28 20:36:14 +0300 | |
commit | 9f6e34103aeb44fafdcd8d4878281c783b7c9ed2 (patch) | |
tree | d76140d168d73f71606c343c6abb961e8574410d | |
parent | f267021479ab1f020c7956378d5bf23405ee3e65 (diff) | |
download | usurpation-9f6e34103aeb44fafdcd8d4878281c783b7c9ed2.tar.gz usurpation-9f6e34103aeb44fafdcd8d4878281c783b7c9ed2.tar.bz2 usurpation-9f6e34103aeb44fafdcd8d4878281c783b7c9ed2.zip |
Protocol: new functions and associated docs.
The header declares new functions:
* tlv_init()
* tlv_destroy()
* tlv_push()
* tlv_get_raw()
* tlv_raw_size()
Signed-off-by: Ramūnas Mažeikis <ramunasnezinomas@gmail.com>
-rw-r--r-- | include/protocol.h | 35 | ||||
-rw-r--r-- | src/common/protocol.c | 32 |
2 files changed, 61 insertions, 6 deletions
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 <errno.h> -#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 */ diff --git a/src/common/protocol.c b/src/common/protocol.c index b2e0d40..edaf491 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -131,3 +131,35 @@ size_t tlv_data_size(struct tlv_parser *parser) } return size; } + +void tlv_destroy(struct tlv *t) +{ + free(t->data); + t->length = 0; +} + +size_t tlv_raw_size(struct tlv *t) +{ + return sizeof(*t) + t->length; +} + +int tlv_push(struct tlv *t, const char *data, size_t size) +{ + int ret = 0; + size_t final_size = tlv_raw_size(t) + size; + if (final_size > TLV_SZ_MAX_RAW) { + ret = E_TLV_OVERFLOW; + } else { + t->data = realloc(t->data, final_size); + memcpy(t->data + t->length, data, size); + t->length = final_size; + } + return ret; +} + +void tlv_init(struct tlv *t) +{ + t->type = TEXT; + t->length = 0; + t->data = NULL; +}
\ No newline at end of file |