diff options
author | 2019-05-19 13:52:43 +0300 | |
---|---|---|
committer | 2019-05-19 13:52:43 +0300 | |
commit | b4165ee0b257aaa9064c5ac82200ff2569a02955 (patch) | |
tree | 5f56cb813bace9b885c0528b7b23342d4e625f6c /src/common/protocol.c | |
parent | 9c8ece1538d31dba9a416c9f29f88e18510f4c75 (diff) | |
download | usurpation-b4165ee0b257aaa9064c5ac82200ff2569a02955.tar.gz usurpation-b4165ee0b257aaa9064c5ac82200ff2569a02955.tar.bz2 usurpation-b4165ee0b257aaa9064c5ac82200ff2569a02955.zip |
Protocol: more work on protocol implementation.
Additional functions implemented for protocol and basic functions
to work with uuid. Source is buildable but actual build files are
not edited to accomodate the changes.
Signed-off-by: Ramūnas Mažeikis <ramunasnezinomas@gmail.com>
Diffstat (limited to 'src/common/protocol.c')
-rw-r--r-- | src/common/protocol.c | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/src/common/protocol.c b/src/common/protocol.c index 8243c8a..270fa81 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -23,10 +23,8 @@ #include "net.h" #include <stdlib.h> #include <string.h> +#include <time.h> -#define READ_INT(x) (*((int*)(x))) -#define READ_TLV_TYPE(x) (*((int*))(x)) -#define READ_SIZE_T(x) (*((size_t)(x))) #define READ_AS(from, type) (*(type*)(from)) #define GET_MSG_CHAR(i) (msg_buf[(i) % MSG_BUF_SIZE]) @@ -38,7 +36,7 @@ /* Begins at 2 because this way we'll have two null's at the beggining * indicating that going back is not possible. * */ -static const int msg_cursor = 2; +static int msg_cursor = 2; /* Two consecutive null's indicate that going back is not possible. * */ static char msg_buf[MSG_BUF_SIZE] = {0}; @@ -78,22 +76,45 @@ void push_string(char *str) { size_t size = strlen(str); - *((enum tlv_type*)packet_buf) = TEXT; - packet_cursor += sizeof(enum tlv_type); - *((size_t*)packet_buf) = strlen(str); - packet_cursor += sizeof(size_t); - push_bytes(); + push_tlv_header(TEXT, size); + push_bytes(str, size); } -void push_fpi1(fpi1_t num); +void push_fpi1(char *num) +{ + push_tlv_header(FPI1, sizeof(fpi1_t)); + push_bytes(num, sizeof(fpi1_t)); +} -void push_timestamp(char *data); +void push_timestamp(char *data) +{ + push_tlv_header(TIMESTAMP, sizeof(time_t)); + push_bytes(data, sizeof(time_t)); +} -void push_request(char *data); +void push_request(char *data) +{ + (void)data; +} -void push_reply(char *data); +void push_reply(char *data) +{ + (void)data; +} -void push_uuid(char *data); +void push_uuid(char *data) +{ + push_tlv_header(UUID, sizeof(uuid_t)); + push_bytes(data, sizeof(uuid_t)); +} + +void push_tlv_header(enum tlv_type type, size_t size) +{ + READ_AS(packet_buf, enum tlv_type) = type; + packet_cursor += sizeof(type); + READ_AS(packet_buf, size_t) = size; + packet_cursor += sizeof(size); +} void get_last_data() { @@ -107,15 +128,16 @@ void get_last_data() struct tlv * get_tlv(void) { - if (tlv_buf[tlv_cursor] != NULL) { - struct tlv *ret = tlv_buf[tlv_cursor++]; + struct tlv *ret; + if (tlv_buf + tlv_cursor != NULL) { + struct tlv *ret = &tlv_buf[tlv_cursor++]; } else { ret = NULL; } return ret; } -int tlv_count(const struct packet_data * const packet) +size_t tlv_count(const struct packet_data * const packet) { size_t cursor = 0; size_t length = 0; @@ -123,7 +145,7 @@ int tlv_count(const struct packet_data * const packet) while (cursor < packet->packet_size) { cursor += sizeof(enum tlv_type); - length = READ_SIZE_T(packet->data + cursor); + length = READ_AS(packet->data + cursor, enum tlv_type); cursor += sizeof(size_t) + length; ret++; } @@ -133,6 +155,18 @@ int tlv_count(const struct packet_data * const packet) return ret; } +/** + * Parses tlv's from packet data and writes them to a buffer of given size. + * + * Returns how many tlv's were actually parsed. + * + * Not yet implemented. + * + * @param data + * + * @return Number of tlv's actually parsed. Greter than or equal to buffer + * size, if an error occurs. + */ size_t get_tlvs( const struct packet_data * const data, const struct tlv *buf, size_t buf_size) @@ -145,7 +179,7 @@ size_t get_tlvs( const struct packet_data * const data, tlvs_read++; } - if (cursor > data->packet_length) { + if (cursor > data->packet_size) { errno = TLV_OVERFLOW; } @@ -166,4 +200,3 @@ size_t parse_tlv(char *data, size_t cursor, struct tlv *t) return data + cursor - begin + 1UL; } - |