From b4165ee0b257aaa9064c5ac82200ff2569a02955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Sun, 19 May 2019 13:52:43 +0300 Subject: Protocol: more work on protocol implementation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- include/protocol.h | 23 ++++++++++++-- include/utils.h | 15 ++++++++- src/common/protocol.c | 73 +++++++++++++++++++++++++++++++------------ src/common/protocol_private.h | 40 ++++++++++++++++++++---- src/common/utils.c | 17 ++++++++++ 5 files changed, 138 insertions(+), 30 deletions(-) diff --git a/include/protocol.h b/include/protocol.h index c1b786c..5872fa3 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifdef PROTOCOL_H_INCLUDED +#ifndef PROTOCOL_H_INCLUDED #define PROTOCOL_H_INCLUDED #include @@ -33,11 +33,28 @@ enum packet_type { }; enum tlv_type { - TEXT, /* NULL-terminated string. */ - FPI1, /* Fixed point. 1 decimal digit of precision. */ + /* NULL-terminated string. */ + TEXT, + + /* Fixed point. 1 decimal digit of precision. */ + FPI1, + + /* Literally time_t*/ TIMESTAMP, + + /* 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 + * message after that. + */ REPLY, + + /* UUID that represents a particular device. + */ UUID }; diff --git a/include/utils.h b/include/utils.h index ca2d2ee..daea864 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,6 +1,19 @@ -#ifdef PROJECT_UTILS_H +#ifndef PROJECT_UTILS_H #define PROJECT_UTILS_H +#include + +typedef struct uuid_s { + uint64_t low; + uint64_t hi; +} 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 8243c8a..270fa81 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -23,10 +23,8 @@ #include "net.h" #include #include +#include -#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; } - diff --git a/src/common/protocol_private.h b/src/common/protocol_private.h index 3a4b1b2..653d19c 100644 --- a/src/common/protocol_private.h +++ b/src/common/protocol_private.h @@ -18,10 +18,12 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifdef PROTOCOL_H_PRIVATE +#ifndef PROTOCOL_H_PRIVATE #define PROTOCOL_H_PRIVATE -#define TLV_ARR_SIZE (16) +#include "utils.h" + +#define TLV_BUF_SIZE (16) /* Pease don't rape the buffer with long messages, daemon-kun. */ #define MSG_BUF_SIZE (257) @@ -38,10 +40,22 @@ * */ size_t tlv_count(const struct packet_data * const packet); -/* Parses a data packet, fills buffer of tlv's of size `buf_size` and returns - * the number of tlv's actually parsed. + +/** + * 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. * */ size_t get_tlvs( const struct packet_data * const data, const struct tlv *buf, @@ -51,16 +65,30 @@ size_t get_tlvs( const struct packet_data * const data, * */ void push_string(char *str); -void push_fpi1(fpi1_t num); +/* Reinterprets char * as fpi1_t * and appends it to the outgoing packet as a + * tlv. + */ +void push_fpi1(char *num); +/* Reinterprets char * as time_t * and appends it to the outgoing packet as a + * tlv. + */ void push_timestamp(char *data); +/* Not implemented yet. */ void push_request(char *data); +/* Not implemented yet. */ void push_reply(char *data); +/* Reinterprets char * as uuid_t * and appends it to the outgoing packet as a + * tlv. + */ void 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); #endif /* PROTOCOL_H_PRIVATE */ diff --git a/src/common/utils.c b/src/common/utils.c index 7bcf767..7e4caa5 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1,5 +1,22 @@ #include "utils.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; + } +} + fpi1_t add(fpi1_t a, fpi1_t b) { return a + b; -- cgit v1.2.3