summaryrefslogtreecommitdiffstats
path: root/src/common/protocol.c
diff options
context:
space:
mode:
authorGravatar Ramūnas Mažeikis <ramunasnezinomas@gmail.com> 2019-05-19 17:50:53 +0300
committerGravatar Ramūnas Mažeikis <ramunasnezinomas@gmail.com> 2019-05-19 17:50:53 +0300
commit031caaf9b7745bfc552cc86fb475de1f18d0fd6d (patch)
treec58ce9782d781f2984fefc587c59ea37cf75a73d /src/common/protocol.c
parentb4165ee0b257aaa9064c5ac82200ff2569a02955 (diff)
downloadusurpation-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>
Diffstat (limited to 'src/common/protocol.c')
-rw-r--r--src/common/protocol.c73
1 files changed, 42 insertions, 31 deletions
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;