Skip to content

Commit

Permalink
Merge pull request #2706 from cesanta/tlspoll
Browse files Browse the repository at this point in the history
poll TLS to process outstanding data in receive buffer
  • Loading branch information
scaprile authored Apr 15, 2024
2 parents af6a52e + 08cac80 commit db6730c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
22 changes: 14 additions & 8 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -5497,6 +5497,17 @@ long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
return (long) len;
}

static void handle_tls_recv(struct mg_connection *c, struct mg_iobuf *io) {
long n = mg_tls_recv(c, &io->buf[io->len], io->size - io->len);
if (n == MG_IO_ERR) {
mg_error(c, "TLS recv error");
} else if (n > 0) {
// Decrypted successfully - trigger MG_EV_READ
io->len += (size_t) n;
mg_call(c, MG_EV_READ, &n);
}
}

static void read_conn(struct mg_connection *c, struct pkt *pkt) {
struct connstate *s = (struct connstate *) (c + 1);
struct mg_iobuf *io = c->is_tls ? &c->rtls : &c->recv;
Expand Down Expand Up @@ -5575,14 +5586,7 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
mg_error(c, "oom");
} else {
// Decrypt data directly into c->recv
long n = mg_tls_recv(c, &io->buf[io->len], io->size - io->len);
if (n == MG_IO_ERR) {
mg_error(c, "TLS recv error");
} else if (n > 0) {
// Decrypted successfully - trigger MG_EV_READ
io->len += (size_t) n;
mg_call(c, MG_EV_READ, &n);
}
handle_tls_recv(c, io);
}
} else {
// Plain text connection, data is already in c->recv, trigger
Expand Down Expand Up @@ -5993,6 +5997,8 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
MG_VERBOSE(("%lu .. %c%c%c%c%c", c->id, c->is_tls ? 'T' : 't',
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
if (c->is_tls && mg_tls_pending(c) > 0)
handle_tls_recv(c, (struct mg_iobuf *) &c->rtls);
if (can_write(c)) write_conn(c);
if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN)
init_closure(c);
Expand Down
22 changes: 14 additions & 8 deletions src/net_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,17 @@ long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
return (long) len;
}

static void handle_tls_recv(struct mg_connection *c, struct mg_iobuf *io) {
long n = mg_tls_recv(c, &io->buf[io->len], io->size - io->len);
if (n == MG_IO_ERR) {
mg_error(c, "TLS recv error");
} else if (n > 0) {
// Decrypted successfully - trigger MG_EV_READ
io->len += (size_t) n;
mg_call(c, MG_EV_READ, &n);
}
}

static void read_conn(struct mg_connection *c, struct pkt *pkt) {
struct connstate *s = (struct connstate *) (c + 1);
struct mg_iobuf *io = c->is_tls ? &c->rtls : &c->recv;
Expand Down Expand Up @@ -691,14 +702,7 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
mg_error(c, "oom");
} else {
// Decrypt data directly into c->recv
long n = mg_tls_recv(c, &io->buf[io->len], io->size - io->len);
if (n == MG_IO_ERR) {
mg_error(c, "TLS recv error");
} else if (n > 0) {
// Decrypted successfully - trigger MG_EV_READ
io->len += (size_t) n;
mg_call(c, MG_EV_READ, &n);
}
handle_tls_recv(c, io);
}
} else {
// Plain text connection, data is already in c->recv, trigger
Expand Down Expand Up @@ -1109,6 +1113,8 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
MG_VERBOSE(("%lu .. %c%c%c%c%c", c->id, c->is_tls ? 'T' : 't',
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
if (c->is_tls && mg_tls_pending(c) > 0)
handle_tls_recv(c, (struct mg_iobuf *) &c->rtls);
if (can_write(c)) write_conn(c);
if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN)
init_closure(c);
Expand Down

0 comments on commit db6730c

Please sign in to comment.