summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/protocol.c91
1 files changed, 20 insertions, 71 deletions
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