summaryrefslogtreecommitdiffstats
path: root/include/tlv.h
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-06-04 13:44:28 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-06-04 13:44:28 +0300
commit75c92f6776a69c1dddee3ee63af5d59e89184828 (patch)
tree8f852e5565f8dc3e984b19ae0d1cd2295cdcddee /include/tlv.h
parentfb220a85890b1de874061cc6b1b2102ba33ad43f (diff)
parentcaace1481ded27b03c47b822f8a7ec66837411c5 (diff)
downloadusurpation-75c92f6776a69c1dddee3ee63af5d59e89184828.tar.gz
usurpation-75c92f6776a69c1dddee3ee63af5d59e89184828.tar.bz2
usurpation-75c92f6776a69c1dddee3ee63af5d59e89184828.zip
Merge branch '35-Message-Output'
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'include/tlv.h')
-rw-r--r--include/tlv.h36
1 files changed, 28 insertions, 8 deletions
diff --git a/include/tlv.h b/include/tlv.h
index b52763a..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,15 +155,22 @@ 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 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);
@@ -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