diff options
author | 2019-05-19 17:50:53 +0300 | |
---|---|---|
committer | 2019-05-19 17:50:53 +0300 | |
commit | 031caaf9b7745bfc552cc86fb475de1f18d0fd6d (patch) | |
tree | c58ce9782d781f2984fefc587c59ea37cf75a73d | |
parent | b4165ee0b257aaa9064c5ac82200ff2569a02955 (diff) | |
download | usurpation-031caaf9b7745bfc552cc86fb475de1f18d0fd6d.tar.gz usurpation-031caaf9b7745bfc552cc86fb475de1f18d0fd6d.tar.bz2 usurpation-031caaf9b7745bfc552cc86fb475de1f18d0fd6d.zip |
Protocol: Implemented protocol error reporting.
Now public functions of the protocol can return an error code.
More work is done on actual logic.
Protocol code has bare-bones doxygen documentation.
Signed-off-by: Ramūnas Mažeikis <ramunasnezinomas@gmail.com>
-rw-r--r-- | include/protocol.h | 32 | ||||
-rw-r--r-- | include/utils.h | 7 | ||||
-rw-r--r-- | src/common/protocol.c | 73 | ||||
-rw-r--r-- | src/common/protocol_private.h | 44 | ||||
-rw-r--r-- | src/common/utils.c | 16 |
5 files changed, 88 insertions, 84 deletions
diff --git a/include/protocol.h b/include/protocol.h index 5872fa3..9d5b518 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -23,8 +23,8 @@ #include <errno.h> -#define SUCCESS (0) -#define TLV_OVERFLOW (1 << 0) +#define E_TLV_OVERFLOW (1 << 0) +#define E_UNKNOWN_TYPE (1 << 1) enum packet_type { REGURAL, @@ -33,27 +33,27 @@ enum packet_type { }; enum tlv_type { - /* NULL-terminated string. */ + /** NULL-terminated string. */ TEXT, - - /* Fixed point. 1 decimal digit of precision. */ + + /** Fixed point. 1 decimal digit of precision. */ FPI1, - - /* Literally time_t*/ + + /** Literally time_t*/ TIMESTAMP, - - /* Represents a request for lost message. Data is unsigned integer - * that uniquely identifies the message. + + /** Represents a request for lost message. Data is unsigned integer + * that uniquely identifies the message. */ REQUEST, - - /* Response to request. Begins with unsigned integer that represents - * which message is begin repeated and the actual null-terminated + + /** Response to request. Begins with unsigned integer that represents + * which message is being repeated and the actual null-terminated * message after that. */ REPLY, - - /* UUID that represents a particular device. + + /** UUID that represents a particular device. */ UUID }; @@ -88,7 +88,7 @@ void clear_data(void); /* Appends data to the next packet to be sent. Type of data is determined by * enum tlv_type. * */ -void push_data(char *data, enum tlv_type); +int push_data(const char *data, enum tlv_type); /* Sends packet towards the other end. * */ diff --git a/include/utils.h b/include/utils.h index daea864..e4feb9a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -4,16 +4,11 @@ #include <stdint.h> typedef struct uuid_s { - uint64_t low; - uint64_t hi; + char bytes[16]; } uuid_t; int cmp_uuid(uuid_t *first, uuid_t *second); -/* Prints uuid in cannonical format. - */ -void uuid_to_str(uuid_t *to_print, char *buf); - typedef int fpi1_t; fpi1_t add(fpi1_t a, fpi1_t b); diff --git a/src/common/protocol.c b/src/common/protocol.c index 270fa81..2ac8280 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -49,10 +49,14 @@ static size_t packet_cursor = 0; * */ static char packet_buf[PACKET_MAX_SIZE] = {0}; -static void push_bytes(const void * const data, const size_t size) +static int push_bytes(const void * const data, const size_t size) { + if (packet_cursor + size >= PACKET_MAX_SIZE) { + return E_PACKET_OVERFLOW; + } memcpy(packet_buf + packet_cursor, data, size); packet_cursor += size; + return 0; } void clear_data(void) @@ -60,60 +64,81 @@ void clear_data(void) packet_cursor = 0; } -void push_data(char *data, enum tlv_type type) +int push_data(const char *data, enum tlv_type type) { + int ret = E_UNKNOWN_TYPE; switch (type) { case TEXT: + ret = push_string(data); + break; case FPI1: + ret = push_fpi1(data); + break; case TIMESTAMP: + ret = push_timestamp(data); + break; case REQUEST: + ret = push_request(data); + break; case REPLY: + ret = push_reply(data); + break; case UUID: + ret = push_uuid(data); + break; } + return ret; } -void push_string(char *str) +static int push_string(char *str) { size_t size = strlen(str); push_tlv_header(TEXT, size); push_bytes(str, size); + return 0; } -void push_fpi1(char *num) +static int push_fpi1(char *num) { - push_tlv_header(FPI1, sizeof(fpi1_t)); - push_bytes(num, sizeof(fpi1_t)); + return push_tlv_header(FPI1, sizeof(fpi1_t)) + | push_bytes(num, sizeof(fpi1_t)); } -void push_timestamp(char *data) +static int push_timestamp(char *data) { - push_tlv_header(TIMESTAMP, sizeof(time_t)); - push_bytes(data, sizeof(time_t)); + return push_tlv_header(TIMESTAMP, sizeof(time_t)) + | push_bytes(data, sizeof(time_t)); } -void push_request(char *data) +static int push_request(char *data) { (void)data; + return 0; } -void push_reply(char *data) +static int push_reply(char *data) { (void)data; + return 0; } -void push_uuid(char *data) +static int push_uuid(char *data) { - push_tlv_header(UUID, sizeof(uuid_t)); - push_bytes(data, sizeof(uuid_t)); + return push_tlv_header(UUID, sizeof(uuid_t)) + | push_bytes(data, sizeof(uuid_t)); } -void push_tlv_header(enum tlv_type type, size_t size) +int push_tlv_header(enum tlv_type type, size_t size) { + if (size + sizeof(type) + packet_cursor >= PACKET_MAX_SIZE) { + return E_PACKET_OVERFLOW; + } READ_AS(packet_buf, enum tlv_type) = type; packet_cursor += sizeof(type); READ_AS(packet_buf, size_t) = size; packet_cursor += sizeof(size); + return 0; } void get_last_data() @@ -150,23 +175,11 @@ size_t tlv_count(const struct packet_data * const packet) ret++; } if (cursor != packet->packet_size) { - errno = TLV_OVERFLOW; + errno = E_TLV_OVERFLOW; } 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) @@ -180,14 +193,12 @@ size_t get_tlvs( const struct packet_data * const data, } if (cursor > data->packet_size) { - errno = TLV_OVERFLOW; + errno = E_TLV_OVERFLOW; } return tlvs_read; } -/* Returns how many bytes were consumed by parser. - * */ size_t parse_tlv(char *data, size_t cursor, struct tlv *t) { char *begin = data + cursor; diff --git a/src/common/protocol_private.h b/src/common/protocol_private.h index 653d19c..0c0b7f9 100644 --- a/src/common/protocol_private.h +++ b/src/common/protocol_private.h @@ -23,37 +23,44 @@ #include "utils.h" +#define E_PACKET_OVERFLOW (1 << 0) + +/** Read as "A maximum of 16 tlv's per packet". */ #define TLV_BUF_SIZE (16) /* Pease don't rape the buffer with long messages, daemon-kun. */ #define MSG_BUF_SIZE (257) -/* UDP can carry bigger packets but memory is hard to come by and more won't be - * needed anyway. +/* Maximum size of packet that can fit into the packet buffer. UDP can carry + * bigger packets but memory is hard to come by and more won't be needed + * anyway. * */ #define PACKET_MAX_SIZE (512) -/* Returns the amount of tlv's int a packet. +/** Returns the amount of tlv's int a packet. * * If a tlv reports length that goes beyond the end of a packet, errno is set * to TLV_OVERFLOW. To check this, set errno to 0 first. + * + * @param packet data parsed from a packet. Function cannot use raw packets. + * + * @return Amount of tlv's in a packet. * */ -size_t tlv_count(const struct packet_data * const packet); - +size_t tlv_count(const struct packet_data * const packet_data); /** * Parses tlv's from packet data and writes them to a buffer of given size. - * + * * Returns how many tlv's were actually parsed. * * To check for errors, set errno to 0 and check after calling. - * + * * Not yet implemented. - * + * * @param data Data from network packet * @param buf Buffer to store parsed tlv's * @param buf_size Size of buffer used to store tlv's - * + * * @return Number of tlv's actually parsed. Greter than or equal to buffer * size, if an error occurs. * */ @@ -61,34 +68,37 @@ size_t get_tlvs( const struct packet_data * const data, const struct tlv *buf, size_t buf_size); -/* Takes a null-terminated string and appends it to the next outgoing packet. +/** Takes a null-terminated string and appends it to the next outgoing packet. + * + * @return Returns 0 on success. Otherwise: E_PACKET_OVERFLOW. + * * */ -void push_string(char *str); +int push_string(char *str); /* Reinterprets char * as fpi1_t * and appends it to the outgoing packet as a * tlv. */ -void push_fpi1(char *num); +int push_fpi1(char *num); /* Reinterprets char * as time_t * and appends it to the outgoing packet as a * tlv. */ -void push_timestamp(char *data); +int push_timestamp(char *data); /* Not implemented yet. */ -void push_request(char *data); +int push_request(char *data); /* Not implemented yet. */ -void push_reply(char *data); +int push_reply(char *data); /* Reinterprets char * as uuid_t * and appends it to the outgoing packet as a * tlv. */ -void push_uuid(char *data); +int push_uuid(char *data); /* Appends tlv_type and size of data to a packet effectively creating a tlv * header. */ -void push_tlv_header(enum tlv_type type, size_t size); +int push_tlv_header(enum tlv_type type, size_t size); #endif /* PROTOCOL_H_PRIVATE */ diff --git a/src/common/utils.c b/src/common/utils.c index 7e4caa5..956ef1b 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1,20 +1,9 @@ #include "utils.h" +#include "string.h" int cmp_uuid(uuid_t *first, uuid_t *second) { - if (first->hi == second->hi) { - if (first->low == second->low) { - return 0; - } else if (first->low < second->low) { - return -1; - } else { - return 1; - } - } else if (first->hi < second->hi) { - return -1; - } else { - return 1; - } + return memcmp(first, second, sizeof(*first)); } fpi1_t add(fpi1_t a, fpi1_t b) @@ -36,4 +25,3 @@ fpi1_t div(fpi1_t a, fpi1_t b) { return (fpi1_t)(((long)a * 10) / ((long)b * 10)); } - |