Skip to content

Commit 8de9699

Browse files
committed
fix TLS multiple-record-per-frame handling in bare metal
1 parent bf592a2 commit 8de9699

File tree

5 files changed

+25
-39
lines changed

5 files changed

+25
-39
lines changed

mongoose.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,9 +4769,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
47694769
}
47704770
tx_tcp(c->mgr->ifp, s->mac, rem_ip, flags, c->loc.port, c->rem.port,
47714771
mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
4772-
} else if (pkt->pay.len == 0) { // this is an ACK
4773-
if (s->fin_rcvd && s->ttype == MIP_TTYPE_FIN)
4774-
s->twclosure = true;
4772+
} else if (pkt->pay.len == 0) { // this is an ACK
4773+
if (s->fin_rcvd && s->ttype == MIP_TTYPE_FIN) s->twclosure = true;
47754774
} else if (seq != s->ack) {
47764775
uint32_t ack = (uint32_t) (mg_htonl(pkt->tcp->seq) + pkt->pay.len);
47774776
if (s->ack == ack) {
@@ -5242,17 +5241,20 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
52425241
tmp = c->next;
52435242
struct connstate *s = (struct connstate *) (c + 1);
52445243
mg_call(c, MG_EV_POLL, &now);
5245-
MG_VERBOSE(("%lu .. %c%c%c%c%c", c->id, c->is_tls ? 'T' : 't',
5244+
MG_VERBOSE(("%lu .. %c%c%c%c%c %lu %lu", c->id, c->is_tls ? 'T' : 't',
52465245
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
5247-
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
5248-
// order is important, TLS conn close with > 1 record in buffer
5249-
if (c->is_tls && mg_tls_pending(c) > 0) handle_tls_recv(c);
5246+
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c',
5247+
mg_tls_pending(c), c->rtls.len));
5248+
// order is important, TLS conn close with > 1 record in buffer (below)
5249+
if (c->is_tls && (c->rtls.len > 0 || mg_tls_pending(c) > 0))
5250+
handle_tls_recv(c);
52505251
if (can_write(c)) write_conn(c);
52515252
if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN)
52525253
init_closure(c);
52535254
// For non-TLS, close immediately upon completing the 3-way closure
5254-
// For TLS, process any pending data until MIP_TTYPE_FIN timeout expires
5255-
if (s->twclosure && (!c->is_tls || mg_tls_pending(c) == 0))
5255+
// For TLS, handle any pending data (above) until MIP_TTYPE_FIN expires
5256+
if (s->twclosure &&
5257+
(!c->is_tls || (c->rtls.len == 0 && mg_tls_pending(c) == 0)))
52565258
c->is_closing = 1;
52575259
if (c->is_closing) close_conn(c);
52585260
}

mongoose.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,25 +2690,6 @@ bool mg_ota_end(void); // Stop writing
26902690

26912691

26922692

2693-
#if MG_OTA != MG_OTA_NONE && MG_OTA != MG_OTA_CUSTOM
2694-
2695-
struct mg_flash {
2696-
void *start; // Address at which flash starts
2697-
size_t size; // Flash size
2698-
size_t secsz; // Sector size
2699-
size_t align; // Write alignment
2700-
bool (*write_fn)(void *, const void *, size_t); // Write function
2701-
bool (*swap_fn)(void); // Swap partitions
2702-
};
2703-
2704-
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
2705-
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
2706-
bool mg_ota_flash_end(struct mg_flash *flash);
2707-
2708-
#endif
2709-
2710-
2711-
27122693

27132694

27142695

src/net_builtin.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
689689
}
690690
tx_tcp(c->mgr->ifp, s->mac, rem_ip, flags, c->loc.port, c->rem.port,
691691
mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
692-
} else if (pkt->pay.len == 0) { // this is an ACK
693-
if (s->fin_rcvd && s->ttype == MIP_TTYPE_FIN)
694-
s->twclosure = true;
692+
} else if (pkt->pay.len == 0) { // this is an ACK
693+
if (s->fin_rcvd && s->ttype == MIP_TTYPE_FIN) s->twclosure = true;
695694
} else if (seq != s->ack) {
696695
uint32_t ack = (uint32_t) (mg_htonl(pkt->tcp->seq) + pkt->pay.len);
697696
if (s->ack == ack) {
@@ -1162,17 +1161,20 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
11621161
tmp = c->next;
11631162
struct connstate *s = (struct connstate *) (c + 1);
11641163
mg_call(c, MG_EV_POLL, &now);
1165-
MG_VERBOSE(("%lu .. %c%c%c%c%c", c->id, c->is_tls ? 'T' : 't',
1164+
MG_VERBOSE(("%lu .. %c%c%c%c%c %lu %lu", c->id, c->is_tls ? 'T' : 't',
11661165
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
1167-
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
1168-
// order is important, TLS conn close with > 1 record in buffer
1169-
if (c->is_tls && mg_tls_pending(c) > 0) handle_tls_recv(c);
1166+
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c',
1167+
mg_tls_pending(c), c->rtls.len));
1168+
// order is important, TLS conn close with > 1 record in buffer (below)
1169+
if (c->is_tls && (c->rtls.len > 0 || mg_tls_pending(c) > 0))
1170+
handle_tls_recv(c);
11701171
if (can_write(c)) write_conn(c);
11711172
if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN)
11721173
init_closure(c);
11731174
// For non-TLS, close immediately upon completing the 3-way closure
1174-
// For TLS, process any pending data until MIP_TTYPE_FIN timeout expires
1175-
if (s->twclosure && (!c->is_tls || mg_tls_pending(c) == 0))
1175+
// For TLS, handle any pending data (above) until MIP_TTYPE_FIN expires
1176+
if (s->twclosure &&
1177+
(!c->is_tls || (c->rtls.len == 0 && mg_tls_pending(c) == 0)))
11761178
c->is_closing = 1;
11771179
if (c->is_closing) close_conn(c);
11781180
}

test/mip_tap_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ static int fetch(struct mg_mgr *mgr, char *buf, const char *url,
198198
mg_vprintf(c, fmt, &ap);
199199
va_end(ap);
200200
buf[0] = '\0';
201+
// - TLS: multiple (small) records: allow enough loops so mg_mgr_poll can
202+
// process buffered records when no more frames are coming in
201203
for (i = 0; i < 500 && buf[0] == '\0' && !fd.closed; i++) {
202204
mg_mgr_poll(mgr, 0);
203205
usleep(5000); // 5 ms. Slow down poll loop to ensure packet transit, but
@@ -403,7 +405,6 @@ int main(void) {
403405

404406
// Events
405407
struct mg_mgr mgr; // Event manager
406-
mg_log_set(MG_LL_DEBUG);
407408
mg_mgr_init(&mgr); // Initialise event manager
408409

409410
// MIP driver

test/tls_multirec/patched_mongoose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11300,7 +11300,7 @@ void mg_tls_free(struct mg_connection *c) {
1130011300
long mg_tls_send(struct mg_connection *c, const void *buf, size_t len) {
1130111301
struct tls_data *tls = (struct tls_data *) c->tls;
1130211302
long n = MG_IO_WAIT;
11303-
size_t maxsize = 1024, encrypted = 0;
11303+
size_t maxsize = 256, encrypted = 0;
1130411304
if (len > MG_IO_SIZE) len = MG_IO_SIZE;
1130511305
if (len > 16384) len = 16384;
1130611306
while (encrypted < len) {

0 commit comments

Comments
 (0)