From 9f6e34103aeb44fafdcd8d4878281c783b7c9ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 28 May 2019 20:36:14 +0300 Subject: Protocol: new functions and associated docs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The header declares new functions: * tlv_init() * tlv_destroy() * tlv_push() * tlv_get_raw() * tlv_raw_size() Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c index b2e0d40..edaf491 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -131,3 +131,35 @@ size_t tlv_data_size(struct tlv_parser *parser) } return size; } + +void tlv_destroy(struct tlv *t) +{ + free(t->data); + t->length = 0; +} + +size_t tlv_raw_size(struct tlv *t) +{ + return sizeof(*t) + t->length; +} + +int tlv_push(struct tlv *t, const char *data, size_t size) +{ + int ret = 0; + size_t final_size = tlv_raw_size(t) + size; + if (final_size > TLV_SZ_MAX_RAW) { + ret = E_TLV_OVERFLOW; + } else { + t->data = realloc(t->data, final_size); + memcpy(t->data + t->length, data, size); + t->length = final_size; + } + return ret; +} + +void tlv_init(struct tlv *t) +{ + t->type = TEXT; + t->length = 0; + t->data = NULL; +} \ No newline at end of file -- cgit v1.2.3 From 2b2eb7556424c2b85668f44c49602370bb6014ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 28 May 2019 20:58:44 +0300 Subject: Protocol: renamed functions to fit the "namespace". MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c index edaf491..3f7bd24 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -32,7 +32,8 @@ #include "net.h" #include "utils.h" -int push_tlv(struct tlv_packet *packet, enum tlv_type type, char *data) +#if 0 +int tlv_push(struct tlv_packet *packet, enum tlv_type type, char *data) { int ret = 0; size_t size; @@ -64,7 +65,9 @@ int push_tlv(struct tlv_packet *packet, enum tlv_type type, char *data) return ret; } -void clear_data(struct tlv_packet *packet) +#endif + +void tlv_clear_data(struct tlv_packet *packet) { packet->offset = 0; packet->type = 0; @@ -98,7 +101,7 @@ static int push_tlv_header(struct tlv_packet *packet, enum tlv_type type, size_t return ret; } -int get_tlv(struct tlv_parser *parser, struct tlv *ret) +int tlv_get(struct tlv_parser *parser, struct tlv *ret) { int ret = 0; @@ -157,9 +160,9 @@ int tlv_push(struct tlv *t, const char *data, size_t size) return ret; } -void tlv_init(struct tlv *t) +void tlv_init(struct tlv *t, enum tlv_type type) { - t->type = TEXT; + t->type = type; t->length = 0; t->data = NULL; } \ No newline at end of file -- cgit v1.2.3 From 77c5d53360058b85ab1df84961b23f40a78fe3a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 28 May 2019 21:01:52 +0300 Subject: Protocol: fixed a stupid. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Had a local variable with the same name as argument. Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c index 3f7bd24..ed636d2 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -103,24 +103,24 @@ static int push_tlv_header(struct tlv_packet *packet, enum tlv_type type, size_t int tlv_get(struct tlv_parser *parser, struct tlv *ret) { - int ret = 0; + int retval = 0; if (parser->offset + sizeof(ret->type) + sizeof(ret->length) >= parser->size) { - ret = E_TLV_OVERFLOW; + retval = E_TLV_OVERFLOW; } else if (parser -> offset == parser->size) { - ret = END_OF_PACKET; + retval = END_OF_PACKET; } else { ret->type = memcpy(&ret->type, parser->data + parser->offset, sizeof(ret->type)); parser->size += sizeof(ret->type); ret->length = memcpy(&ret->length, parser->data + parser->offset, sizeof(ret->length)); parser->offset += sizeof(ret->length); if (parser->offset + ret->length >= parser->size) { - ret = E_TLV_OVERFLOW; + retval = E_TLV_OVERFLOW; } else { memcpy(ret->data, parser->data, ret->length); } } - return ret; + return retval; } size_t tlv_data_size(struct tlv_parser *parser) -- cgit v1.2.3 From fbecca4da87b47da4d72ea2ec4571d6c9287961d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 29 May 2019 12:32:04 +0300 Subject: Protocol: more code and a tutorial. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 91 +++++++++++---------------------------------------- 1 file changed, 20 insertions(+), 71 deletions(-) (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c index ed636d2..90dfc92 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -32,75 +32,6 @@ #include "net.h" #include "utils.h" -#if 0 -int tlv_push(struct tlv_packet *packet, enum tlv_type type, char *data) -{ - int ret = 0; - size_t size; - - switch (type) { - case TEXT: - size = strlen(data) + 1; - break; - case FPI1: - size = sizeof(fpi1_t); - break; - case TIMESTAMP: - size = sizeof(time_t); - break; - case REQUEST: - size = sizeof(msg_idx_t); - break; - case REPLY: - size = sizeof(msg_idx_t) + strlen(data + sizeof(msg_idx_t)); - break; - case UUID: - size = sizeof(uuid_t); - break; - default: - ret = E_UNKNOWN_TYPE; - } - ret |= push_tlv_header(packet, type, size); - ret |= push_bytes(packet, data, size); - return ret; -} - -#endif - -void tlv_clear_data(struct tlv_packet *packet) -{ - packet->offset = 0; - packet->type = 0; - memset(packet->data, 0, packet->size); -} - -static int push_bytes(struct tlv_packet *packet, char *data, size_t size) -{ - int ret = 0; - - if (packet->offset + size >= packet->size) { - ret = E_TLV_OVERFLOW; - } else { - memcpy(packet->data + packet->offset, data, size); - packet->offset += size; - } -} - -static int push_tlv_header(struct tlv_packet *packet, enum tlv_type type, size_t size) -{ - int ret = 0; - - if (packet->offset + sizeof(type) + sizeof(size) >= packet->size) { - ret = E_TLV_OVERFLOW; - } else { - memcpy(packet->data + packet->size, type, sizeof(type)); - packet->offset += sizeof(type); - memcpy(packet->data + packet->offset, size, sizeof(size)); - packet->offset += sizeof(size); - } - return ret; -} - int tlv_get(struct tlv_parser *parser, struct tlv *ret) { int retval = 0; @@ -141,12 +72,12 @@ void tlv_destroy(struct tlv *t) t->length = 0; } -size_t tlv_raw_size(struct tlv *t) +size_t tlv_raw_size(const struct tlv *t) { return sizeof(*t) + t->length; } -int tlv_push(struct tlv *t, const char *data, size_t size) +int tlv_push_data(struct tlv *t, const char *data, size_t size) { int ret = 0; size_t final_size = tlv_raw_size(t) + size; @@ -165,4 +96,22 @@ void tlv_init(struct tlv *t, enum tlv_type type) t->type = type; t->length = 0; t->data = NULL; +} + +int tlv_push_tlv(struct tlv *t, const struct tlv *other) +{ + int ret = 0; + size_t other_size; + size_t final_size; + + other_size = tlv_raw_size(other); + final_size = tlv_raw_size(t) + other_size; + if (final_size > TLV_SZ_MAX_RAW) { + ret = E_TLV_OVERFLOW; + } else { + tlv_get_raw(other, t->data + t->length); + t->length = final_size; + } + + return ret; } \ No newline at end of file -- cgit v1.2.3 From 1b9c085c208d92254f146741f8b8c012a600f73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 29 May 2019 13:50:07 +0300 Subject: Protocol: new and improved tlv_destroy. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c index 90dfc92..0002a96 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -68,8 +68,24 @@ size_t tlv_data_size(struct tlv_parser *parser) void tlv_destroy(struct tlv *t) { - free(t->data); - t->length = 0; + size_t i = 0; + size_t tlv_count; + struct tlv *arr; + switch (t->type) + { + case REGURAL: + case HEARTBEAT: + case DISCOVERY: + tlv_count = t->length / sizeof(struct tlv); + arr = t->data; + for (i = 0; i < tlv_count; i++) { + tlv_destroy(&arr[i]); + } + default: + free(t->data); + t->length = 0; + break; + } } size_t tlv_raw_size(const struct tlv *t) -- cgit v1.2.3 From 743f4857ede9f48def462c1ea2cee8be71356fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 29 May 2019 13:53:50 +0300 Subject: Protocol: renamed some files. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 133 -------------------------------------------------- 1 file changed, 133 deletions(-) delete mode 100644 src/common/protocol.c (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c deleted file mode 100644 index 0002a96..0000000 --- a/src/common/protocol.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Usurpataion --- clinet-server protocol implementation. - * - * Copyright (C) 2019 Ramūnas Mažeikis - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; version 2.1 - * of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -/** - * Common parts of protocol implementation. Handling of anything that actually - * deals with connection descriptor has to be implemented by device and daemon - * separately. - */ - -#include -#include -#include -#include "protocol.h" -#include "protocol_private.h" -#include "net.h" -#include "utils.h" - -int tlv_get(struct tlv_parser *parser, struct tlv *ret) -{ - int retval = 0; - - if (parser->offset + sizeof(ret->type) + sizeof(ret->length) >= parser->size) { - retval = E_TLV_OVERFLOW; - } else if (parser -> offset == parser->size) { - retval = END_OF_PACKET; - } else { - ret->type = memcpy(&ret->type, parser->data + parser->offset, sizeof(ret->type)); - parser->size += sizeof(ret->type); - ret->length = memcpy(&ret->length, parser->data + parser->offset, sizeof(ret->length)); - parser->offset += sizeof(ret->length); - if (parser->offset + ret->length >= parser->size) { - retval = E_TLV_OVERFLOW; - } else { - memcpy(ret->data, parser->data, ret->length); - } - } - return retval; -} - -size_t tlv_data_size(struct tlv_parser *parser) -{ - size_t size; - - if (parser->offset + sizeof(enum tlv_type) + sizeof(size_t) >= parser->size) { - size = 0; - } else { - memcpy(&size, parser->data + parser->offset + sizeof(enum tlv_type), sizeof(size_t)); - } - return size; -} - -void tlv_destroy(struct tlv *t) -{ - size_t i = 0; - size_t tlv_count; - struct tlv *arr; - switch (t->type) - { - case REGURAL: - case HEARTBEAT: - case DISCOVERY: - tlv_count = t->length / sizeof(struct tlv); - arr = t->data; - for (i = 0; i < tlv_count; i++) { - tlv_destroy(&arr[i]); - } - default: - free(t->data); - t->length = 0; - break; - } -} - -size_t tlv_raw_size(const struct tlv *t) -{ - return sizeof(*t) + t->length; -} - -int tlv_push_data(struct tlv *t, const char *data, size_t size) -{ - int ret = 0; - size_t final_size = tlv_raw_size(t) + size; - if (final_size > TLV_SZ_MAX_RAW) { - ret = E_TLV_OVERFLOW; - } else { - t->data = realloc(t->data, final_size); - memcpy(t->data + t->length, data, size); - t->length = final_size; - } - return ret; -} - -void tlv_init(struct tlv *t, enum tlv_type type) -{ - t->type = type; - t->length = 0; - t->data = NULL; -} - -int tlv_push_tlv(struct tlv *t, const struct tlv *other) -{ - int ret = 0; - size_t other_size; - size_t final_size; - - other_size = tlv_raw_size(other); - final_size = tlv_raw_size(t) + other_size; - if (final_size > TLV_SZ_MAX_RAW) { - ret = E_TLV_OVERFLOW; - } else { - tlv_get_raw(other, t->data + t->length); - t->length = final_size; - } - - return ret; -} \ No newline at end of file -- cgit v1.2.3