Skip to content

Commit

Permalink
Fix non responding in autoresponder
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed Apr 27, 2024
1 parent e44724d commit 4c89287
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
13 changes: 11 additions & 2 deletions autoresponder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion fsm/frontend.rl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 13 additions & 8 deletions multitasking/suspendables.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -141,15 +146,15 @@ 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) {
errno = EIDRM;
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;
Expand Down
9 changes: 4 additions & 5 deletions ucspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 '?';
}
Expand Down

0 comments on commit 4c89287

Please sign in to comment.