Skip to content

Commit

Permalink
Move TLS API to tls.h, fix builtin handshake codepath
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Dec 8, 2023
1 parent 4fc43cf commit 6e68124
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);




Expand Down Expand Up @@ -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);




Expand Down
5 changes: 0 additions & 5 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
8 changes: 4 additions & 4 deletions src/net_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 6e68124

Please sign in to comment.