From 4c8928772e1c622e94a6aa3e1067911fbd61a7fc Mon Sep 17 00:00:00 2001 From: Aleksy Grabowski Date: Sun, 28 Apr 2024 01:35:15 +0200 Subject: [PATCH] Fix non responding in autoresponder --- autoresponder.c | 13 +++++++++++-- fsm/frontend.rl | 4 +++- multitasking/suspendables.c | 21 +++++++++++++-------- ucspi.c | 9 ++++----- utils.c | 1 + 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/autoresponder.c b/autoresponder.c index ee3b7ba..b59b2f2 100644 --- a/autoresponder.c +++ b/autoresponder.c @@ -58,21 +58,29 @@ static union frob_body construct_hardcoded_message_body(const struct config* con int autoresponder(const struct autoresponder_args* const args) { const struct config cfg = { + .supported_versions = { "160", "170" }, + .info = { + .version = "170", + .vendor = "TEST", + .device_type = "SIM", + .device_id = "0" + }, .parameters = load_d5_from_file(args->d5_path) }; const unsigned char* p; ssize_t bytes; unsigned char rsp_buf[1024]; while ((bytes = sus_borrow(args->in, (void**)&p)) >= 0) { + //LOGDXP(char tmp[4*bytes], "Received %zd bytes: %s", bytes, PRETTY(p, p + bytes, tmp)); if (bytes > 1) { const struct frob_msg msg = xparse_message(bytes - 1, p + 1); const struct frob_msg response = { .magic = FROB_MAGIC, .header = { .token = msg.header.token, - .type = msg.header.type + .type = msg.header.type + 1 }, - .body = construct_hardcoded_message_body(&cfg, msg.header.token) + .body = construct_hardcoded_message_body(&cfg, msg.header.type + 1) }; const ssize_t w = serialize(sizeof rsp_buf, rsp_buf, &response); if (w < 0) { @@ -82,6 +90,7 @@ int autoresponder(const struct autoresponder_args* const args) { LOGDXP(char tmp[4*w], "← % 4zd %s", sizeof w, PRETTY(rsp_buf, rsp_buf + w, tmp)); } } + //LOGDX("Returning buffer"); sus_return(args->in, p, bytes); } return -1; diff --git a/fsm/frontend.rl b/fsm/frontend.rl index 7f86985..8a18b7f 100644 --- a/fsm/frontend.rl +++ b/fsm/frontend.rl @@ -38,6 +38,7 @@ static int cs; LOGDXP(char tmp[4*1], "← % 4d %s", 1, PRETTY(&acknak, &acknak + 1, tmp)); } action Process { + LOGDXP(char tmp[4*(pe-p)], "Lending %zd bytes: %s", pe - p, PRETTY((unsigned char*)p, (unsigned char*)pe, tmp)); sus_lend(1, pe - p, (char*)p);// TODO: Remove this cast } action Forward { @@ -84,7 +85,8 @@ int fsm_frontend_foreign(struct fsm_frontend_foreign_args* const a) { ssize_t bytes; const char* p; while ((bytes = sus_borrow(0, (void**)&p)) >= 0) { - const char* const pe = p + 1; + LOGDX("Received %zd bytes", bytes); + const char* const pe = p + bytes; fsm_exec(p, pe); sus_return(0, p, bytes); } diff --git a/multitasking/suspendables.c b/multitasking/suspendables.c index 62e4451..2888e09 100644 --- a/multitasking/suspendables.c +++ b/multitasking/suspendables.c @@ -70,6 +70,13 @@ static void suspend_io(const int fd, const enum ioset set) { LOGDX("awaken %s at %s fd %d", s_current->name, method, fd); } +static void suspend_io_errno(const int fd, const enum ioset set) { + const char* const method = ioset_to_method(set); + LOGD("suspended %s at %s fd %d", s_current->name, method, fd); + suspend(); + LOGDX("awaken %s at %s fd %d", s_current->name, method, fd); +} + static void suspend_id(const char* const method, const int id) { LOGDX("suspended %s at %s id %d", s_current->name, method, id); suspend(); @@ -96,8 +103,7 @@ ssize_t sus_write(const int fd, const void* const data, const size_t size) { suspend_until_active(fd, IOSET_WRITE); const ssize_t r = write(fd, data, size); if (r < 0 && errno == EAGAIN) { - LOGD("write"); - suspend_io(fd, IOSET_WRITE); + suspend_io_errno(fd, IOSET_WRITE); goto again; } s_current->visited = 0; @@ -109,8 +115,7 @@ ssize_t sus_read(const int fd, void* const data, const size_t size) { suspend_until_active(fd, IOSET_READ); const ssize_t r = read(fd, data, size); if (r < 0 && errno == EAGAIN) { - LOGD("read"); - suspend_io(fd, IOSET_READ); + suspend_io_errno(fd, IOSET_READ); goto again; } s_current->visited = 0; @@ -129,9 +134,9 @@ void sus_lend(const uint8_t id, const size_t size, void* const data) { assert(s_iow[id].data == NULL); assert(s_iow[id].size == 0); s_iow[id] = (struct iowork){ .data = data, .size = size }; - //LOGDX("[%d] = %p", id, s_iow[id].data); + //LOGDX("s_iow[%d].data = %p, data = %p", id, s_iow[id].data, data); while (s_iow[id].data != NULL) { - //LOGDX("suspend [%d] = %p", id, s_iow[id].data); + //LOGDX("suspend because s_iow[%d].data (%p) != NULL", id, s_iow[id].data); suspend_id("lend", id); } //LOGDX("wakeup [%d] = %p", id, s_iow[id].data); @@ -141,7 +146,7 @@ void sus_lend(const uint8_t id, const size_t size, void* const data) { } ssize_t sus_borrow(const uint8_t id, void** const data) { - //LOGDX("[%d] = %p | %p", id, s_iow[id].data, data); + //LOGDX("s_iow[%d].data = %p, data = %p", id, s_iow[id].data, data); assert(data != NULL); assert(!s_iow[id].borrowed); if (s_iow[id].disabled) { @@ -149,7 +154,7 @@ ssize_t sus_borrow(const uint8_t id, void** const data) { return -1; } while (s_iow[id].data == NULL) { - //LOGDX("suspend [%d] = %p", id, s_iow[id].data); + //LOGDX("suspend because s_iow[%d].data (%p) == NULL", id, s_iow[id].data); if (s_iow[id].disabled) { errno = EIDRM; return -1; diff --git a/ucspi.c b/ucspi.c index dc77689..cb879d7 100644 --- a/ucspi.c +++ b/ucspi.c @@ -20,13 +20,12 @@ static void ucspi_log(const char* const proto, const char* const connnum) { for (size_t k = 0; k < lengthof(sn); k++) sv[k] = getenv(sn[k]); - LOGIX("UCSPI compatible environment detected (%s)", (connnum ? "server" : "client")); + LOGIX("UCSPI-%s compatible environment detected (%s)", proto, (connnum ? "server" : "client")); char* p = buf; - p += snprintfx(p, buf + sizeof buf - p, "PROTO: %s;", proto); for (size_t j = 0; j < lengthof(rl); j++) { if (!(ed[j][0] || ed[j][1] || ed[j][2] || ed[j][3])) continue; - p += snprintfx(p, buf + sizeof buf - p, " %s:", rl[j]); + p += snprintfx(p, buf + sizeof buf - p, "%s:", rl[j]); if (j == 0 && connnum) p += snprintfx(p, buf + sizeof buf - p, " #%s", connnum); if (ed[j][0]) @@ -35,9 +34,9 @@ static void ucspi_log(const char* const proto, const char* const connnum) { p += snprintfx(p, buf + sizeof buf - p, " (%s:%s)", ed[j][1], ed[j][2]); if (ed[j][3]) p += snprintfx(p, buf + sizeof buf - p, " [%s]", ed[j][3]); - p += snprintfx(p, buf + sizeof buf - p, ";"); + LOGDX("%s", buf); + p = buf; } - LOGDX("%s", buf); if (sv[0]) { p = buf; p += snprintfx(p, buf + sizeof buf - p, "%s: %s", sv[0], sv[1]); diff --git a/utils.c b/utils.c index 799c24d..2fd32bb 100644 --- a/utils.c +++ b/utils.c @@ -86,6 +86,7 @@ static char class_to_char(const enum FrobMessageType m) { case FROB_L: return 'L'; case FROB_B: return 'B'; } + LOGE("0x%x", m); assert(false); return '?'; }