From 6e68124405eba85b0fe12ebec87b1c48eae257cb Mon Sep 17 00:00:00 2001 From: cpq Date: Fri, 8 Dec 2023 00:30:09 +0000 Subject: [PATCH] Move TLS API to tls.h, fix builtin handshake codepath --- mongoose.c | 8 ++++---- mongoose.h | 10 +++++----- src/net.h | 5 ----- src/net_builtin.c | 8 ++++---- src/tls.h | 5 +++++ 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/mongoose.c b/mongoose.c index f76fc454..4a75fbe6 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5000,8 +5000,7 @@ static size_t trim_len(struct mg_connection *c, size_t len) { } // Ensure the MTU isn't lower than the minimum allowed value if (ifp->mtu < min_mtu) { - MG_ERROR(("MTU is lower than minimum possible value. Setting it to %d.", - min_mtu)); + MG_ERROR(("MTU is lower than minimum, capping to %lu", min_mtu)); ifp->mtu = (uint16_t) min_mtu; } // If the total packet size exceeds the MTU, trim the length @@ -5112,7 +5111,9 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { if (s->ttype != MIP_TTYPE_ACK) settmout(c, MIP_TTYPE_ACK); #endif - if (c->is_tls) { + if (c->is_tls && c->is_tls_hs) { + mg_tls_handshake(c); + } else if (c->is_tls) { // TLS connection. Make room for decrypted data in c->recv io = &c->recv; if (io->size - io->len < pkt->pay.len && @@ -5526,7 +5527,6 @@ 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_hs) mg_tls_handshake(c); if (can_write(c)) write_conn(c); if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN) init_closure(c); diff --git a/mongoose.h b/mongoose.h index 08a9bdff..8e6d1ab2 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1293,11 +1293,6 @@ bool mg_open_listener(struct mg_connection *c, const char *url); struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds, unsigned flags, void (*fn)(void *), void *arg); -// Low-level IO primives used by TLS layer -enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 }; -long mg_io_send(struct mg_connection *c, const void *buf, size_t len); -long mg_io_recv(struct mg_connection *c, void *buf, size_t len); - @@ -1402,6 +1397,11 @@ void mg_tls_handshake(struct mg_connection *); void mg_tls_ctx_init(struct mg_mgr *); void mg_tls_ctx_free(struct mg_mgr *); +// Low-level IO primives used by TLS layer +enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 }; +long mg_io_send(struct mg_connection *c, const void *buf, size_t len); +long mg_io_recv(struct mg_connection *c, void *buf, size_t len); + diff --git a/src/net.h b/src/net.h index 03b916d7..ac2ddc26 100644 --- a/src/net.h +++ b/src/net.h @@ -99,8 +99,3 @@ bool mg_open_listener(struct mg_connection *c, const char *url); // Utility functions struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds, unsigned flags, void (*fn)(void *), void *arg); - -// Low-level IO primives used by TLS layer -enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 }; -long mg_io_send(struct mg_connection *c, const void *buf, size_t len); -long mg_io_recv(struct mg_connection *c, void *buf, size_t len); diff --git a/src/net_builtin.c b/src/net_builtin.c index d036875a..2e208231 100644 --- a/src/net_builtin.c +++ b/src/net_builtin.c @@ -557,8 +557,7 @@ static size_t trim_len(struct mg_connection *c, size_t len) { } // Ensure the MTU isn't lower than the minimum allowed value if (ifp->mtu < min_mtu) { - MG_ERROR(("MTU is lower than minimum possible value. Setting it to %d.", - min_mtu)); + MG_ERROR(("MTU is lower than minimum, capping to %lu", min_mtu)); ifp->mtu = (uint16_t) min_mtu; } // If the total packet size exceeds the MTU, trim the length @@ -669,7 +668,9 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { if (s->ttype != MIP_TTYPE_ACK) settmout(c, MIP_TTYPE_ACK); #endif - if (c->is_tls) { + if (c->is_tls && c->is_tls_hs) { + mg_tls_handshake(c); + } else if (c->is_tls) { // TLS connection. Make room for decrypted data in c->recv io = &c->recv; if (io->size - io->len < pkt->pay.len && @@ -1083,7 +1084,6 @@ 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_hs) mg_tls_handshake(c); if (can_write(c)) write_conn(c); if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN) init_closure(c); diff --git a/src/tls.h b/src/tls.h index a8623d2f..0ccd5e37 100644 --- a/src/tls.h +++ b/src/tls.h @@ -31,3 +31,8 @@ void mg_tls_handshake(struct mg_connection *); // Private void mg_tls_ctx_init(struct mg_mgr *); void mg_tls_ctx_free(struct mg_mgr *); + +// Low-level IO primives used by TLS layer +enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 }; +long mg_io_send(struct mg_connection *c, const void *buf, size_t len); +long mg_io_recv(struct mg_connection *c, void *buf, size_t len);