aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-05-30 17:48:27 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-05-30 17:48:27 +0200
commit47bbee8b8a5d7eae452b76f58f30d6a70d265ebe (patch)
treecbcf00e1a947c9a168cd4bf245f6d93a688f271c
parentb231e9d808f40aef0895787ea09624787b10addd (diff)
fix wild bug
-rw-r--r--client/CMakeLists.txt1
-rw-r--r--client/sock.cpp1
-rw-r--r--i2ctcp/i2ctcpv1.c28
-rw-r--r--i2ctcp/include.cmake3
4 files changed, 21 insertions, 12 deletions
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index cb891a6..d838266 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -29,4 +29,3 @@ target_link_libraries(pbc
readline # this is such a common library that I did not bother adding it as a submodule
)
-
diff --git a/client/sock.cpp b/client/sock.cpp
index 638bf9d..95a3685 100644
--- a/client/sock.cpp
+++ b/client/sock.cpp
@@ -73,6 +73,7 @@ void PBSocket::send(const char * buf, size_t buf_sz) {
void PBSocket::sock_task() {
i2ctcp_msg_t input;
+ i2ctcp_read_reset(&input);
while(1) {
char buf[80];
diff --git a/i2ctcp/i2ctcpv1.c b/i2ctcp/i2ctcpv1.c
index 36a5dbd..b406d8d 100644
--- a/i2ctcp/i2ctcpv1.c
+++ b/i2ctcp/i2ctcpv1.c
@@ -7,10 +7,6 @@
#include "i2ctcpv1.h"
int i2ctcp_read(i2ctcp_msg_t * target, const char * buf, size_t buf_sz) {
- // a new reader is used per buffer block passed to this function
- mpack_reader_t reader;
- mpack_reader_init_data(&reader, buf, buf_sz);
-
// at start of message
if (target->_rdata == 0) {
// NOTE: The entire start of a message needs to be readable from the buffer
@@ -21,17 +17,27 @@ int i2ctcp_read(i2ctcp_msg_t * target, const char * buf, size_t buf_sz) {
// message. The check here is kind of optional.
if (buf_sz < 4) return -1;
+ // mpack reader is used for the first buffer block, as it contains the data
+ // size info
+ mpack_reader_t reader;
+ mpack_reader_init_data(&reader, buf, buf_sz);
+
target->addr = mpack_expect_u16(&reader);
target->length = target->_rdata = mpack_expect_bin(&reader);
target->data = (char *) malloc(target->length);
- }
- // continue reading chunks of target->data until the amount of bytes
- // specified in target->length
- size_t to_read = MIN(mpack_reader_remaining(&reader, NULL), target->_rdata);
- char * data = target->data + target->length - target->_rdata;
- mpack_read_bytes(&reader, data, to_read);
- target->_rdata -= to_read;
+ // read remaining data in (header) packet
+ size_t to_read = mpack_reader_remaining(&reader, NULL);
+ mpack_read_bytes(&reader, target->data, to_read);
+ target->_rdata -= to_read;
+ } else {
+ // continue reading chunks of target->data until the amount of bytes
+ // specified in target->length
+ size_t to_read = MIN(buf_sz, target->_rdata);
+ char * data = target->data + target->length - target->_rdata;
+ memcpy(data, buf, to_read);
+ target->_rdata -= to_read;
+ }
// if rdata = 0, the message was completely read
return target->_rdata;
diff --git a/i2ctcp/include.cmake b/i2ctcp/include.cmake
index d755b57..b61b2a4 100644
--- a/i2ctcp/include.cmake
+++ b/i2ctcp/include.cmake
@@ -14,3 +14,6 @@ add_library(mpack STATIC
${CMAKE_CURRENT_LIST_DIR}/lib/mpack/src/mpack/mpack-writer.c
)
+# causes some wild crashes, please leave off
+add_compile_definitions(MPACK_READ_TRACKING=0)
+