From 8a005e8e3049e8f0ae0253293b1dff923e1fabb7 Mon Sep 17 00:00:00 2001 From: Christian Giese Date: Thu, 5 May 2022 12:01:03 +0000 Subject: [PATCH] fix lwIP TCP issues --- code/bngblaster/src/bbl_tcp.c | 58 +++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/code/bngblaster/src/bbl_tcp.c b/code/bngblaster/src/bbl_tcp.c index 4d4d1fee..519809e7 100644 --- a/code/bngblaster/src/bbl_tcp.c +++ b/code/bngblaster/src/bbl_tcp.c @@ -46,6 +46,11 @@ void bbl_tcp_close(bbl_tcp_ctx_t *tcpc) { if(tcpc) { if(tcpc->pcb) { + tcp_arg(tcpc->pcb, NULL); + tcp_sent(tcpc->pcb, NULL); + tcp_recv(tcpc->pcb, NULL); + tcp_err(tcpc->pcb, NULL); + tcp_poll(tcpc->pcb, NULL, 0); tcp_close(tcpc->pcb); } tcpc->state = BBL_TCP_STATE_CLOSED; @@ -100,17 +105,23 @@ bbl_tcp_ctx_new(bbl_interface_s *interface) { err_t bbl_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb, u16_t len) { bbl_tcp_ctx_t *tcpc = arg; - uint16_t tx = tpcb->snd_buf; + uint16_t tx = tcp_sndbuf(tpcb); + err_t result = ERR_OK; UNUSED(len); if(tcpc->tx.offset < tcpc->tx.len) { - if((tcpc->tx.offset + tx) > tcpc->tx.len) { - tx = tcpc->tx.len - tcpc->tx.offset; - } - if(tcp_write(tpcb, tcpc->tx.buf + tcpc->tx.offset, tx, tcpc->tx.flags) == ERR_OK) { - tcpc->state = BBL_TCP_STATE_SENDING; - tcpc->tx.offset += tx; + if(tx) { + if((tcpc->tx.offset + tx) > tcpc->tx.len) { + tx = tcpc->tx.len - tcpc->tx.offset; + } + result = tcp_write(tpcb, tcpc->tx.buf + tcpc->tx.offset, tx, tcpc->tx.flags); + if(result == ERR_OK) { + tcpc->state = BBL_TCP_STATE_SENDING; + tcpc->tx.offset += tx; + } + } else { + result = ERR_MEM; } } else if(tcpc->pcb->unacked == NULL && tcpc->pcb->unsent == NULL) { /* Idle means that it is save to replace buffer. */ @@ -119,7 +130,11 @@ bbl_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb, u16_t len) { (tcpc->idle_cb)(tcpc->arg); } } - return ERR_OK; + + if (result == ERR_MEM) { + tcp_output(tpcb); + } + return result; } err_t @@ -127,23 +142,23 @@ bbl_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { bbl_tcp_ctx_t *tcpc = arg; struct pbuf *_p; - UNUSED(err); /* TODO!!! */ - if(p) { - if(tcpc->receive_cb) { - _p = p; - while(_p) { - (tcpc->receive_cb)(tcpc->arg, p->payload, p->len); - _p = _p->next; + if(err == ERR_OK) { + if(tcpc->receive_cb) { + _p = p; + while(_p) { + (tcpc->receive_cb)(tcpc->arg, p->payload, p->len); + _p = _p->next; + } + /* Signal application that read is finished. */ + (tcpc->receive_cb)(tcpc->arg, NULL, 0); } - /* Signal application that read is finished. */ - (tcpc->receive_cb)(tcpc->arg, NULL, 0); + tcpc->bytes_rx += p->tot_len; + tcp_recved(tpcb, p->tot_len); } - tcpc->bytes_rx += p->tot_len; - tcp_recved(tpcb, p->tot_len); pbuf_free(p); } - return ERR_OK; + return err; } /** @@ -263,6 +278,9 @@ bbl_tcp_ipv4_connect(bbl_interface_s *interface, ipv4addr_t *src, ipv4addr_t *ds /* Bind local IP address and port */ tcp_bind(tcpc->pcb, (const ip_addr_t*)src, 0); + /* Disable nagle algorithm */ + tcp_nagle_disable(tcpc->pcb); + /* Connect session */ if(tcp_connect(tcpc->pcb, (const ip_addr_t*)dst, port, bbl_tcp_connected) != ERR_OK) { bbl_tcp_ctx_free(tcpc);