From 06d3ec2c3f3b015a9a204cef29389d7dd6afdcf3 Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Fri, 1 Aug 2025 15:31:54 +0200 Subject: [PATCH 1/4] Fix handling of option size in received TCP packets --- src/nwk.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/nwk.c b/src/nwk.c index e18c14a..21c9d8b 100644 --- a/src/nwk.c +++ b/src/nwk.c @@ -664,9 +664,10 @@ void nwk_downstream(void) * Check flags. */ _flags = 0; - // XXX - Assumes no TCP options! Fix! - // Like it is now, it can't communicate to another instance of itself! - data_size -= 40; + // XXX Correct buffer offset processing to handle variable + // header lengths + data_ofs=((IPH(ver_length)&0x0f)<<2)+((TCPH(hlen)>>4)<<2); + data_size -= data_ofs; // No data to return, unless we discover otherwise @@ -752,9 +753,6 @@ void nwk_downstream(void) */ if(data_size > _sckt->rx_size) data_size = _sckt->rx_size; - // XXX Correct buffer offset processing to handle variable - // header lengths - data_ofs=((IPH(ver_length)&0x0f)<<2)+((TCPH(hlen)>>4)<<2); if (rel_sequence.d>_sckt->rx_size || rel_sequence.d+data_size>_sckt->rx_size) { From 0499081f271713bb12e08bf731cec09600676014 Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Fri, 1 Aug 2025 15:34:50 +0200 Subject: [PATCH 2/4] Stop retransmitting packets when everything is ACKed --- src/nwk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nwk.c b/src/nwk.c index 21c9d8b..80190d6 100644 --- a/src/nwk.c +++ b/src/nwk.c @@ -155,6 +155,7 @@ byte_t nwk_tick (byte_t sig) for_each(_sockets, _sckt) { if(_sckt->type != SOCKET_TCP) continue; // UDP socket or unused. + if(_sckt->state == _CONNECT) continue; // Not waiting for the peer to ack anything // if(_sckt->time == 0) continue; // does not have timing requirements. From f4f3b40a4320e06cbe1e7d061b265464bb6b3e22 Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Fri, 1 Aug 2025 16:50:22 +0200 Subject: [PATCH 3/4] Fix handing of sequence number on TCP transmit The variable _sckt->timeout is used for the sole purpose of determining whether the sequence number should be incremented, or whether this is a retransmit. However, the updating of this variable didn't really work correctly since nwk_upstream can be called through other means than from nwk_tick (there is for example a "Reschedule 50ms later for eventual further processing") which is where the variable would be updated. The result being that the sequence number would be incorrectly incremented on any retransmit caused by a call to nwk_upstream not coming from nwk_tick. Change this to the more robust approach of setting timeout whenever the sequence number is incremented, and clearing it only when there is new data to transmit. This variable could probably use a new name as well. :-) --- src/nwk.c | 4 +--- src/socket.c | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/nwk.c b/src/nwk.c index 80190d6..65f0552 100644 --- a/src/nwk.c +++ b/src/nwk.c @@ -201,14 +201,12 @@ byte_t nwk_tick (byte_t sig) default: _sckt->toSend = ACK; _sckt->time = SOCKET_TIMEOUT(_sckt); - _sckt->timeout = FALSE; break; } if(_sckt->toSend) { /* * Force nwk_upstream() to execute. */ - _sckt->timeout = TRUE; #ifdef INSTANT_ACK nwk_upstream(0); #endif @@ -437,6 +435,7 @@ byte_t nwk_upstream (byte_t sig) if(_sckt->toSend & (SYN | FIN)) seq.d++; if( (_sckt->toSend & (SYN | ACK)) == (SYN|ACK) ) seq.d++; _sckt->seq.d = seq.d; + _sckt->timeout = TRUE; } } @@ -492,7 +491,6 @@ byte_t nwk_upstream (byte_t sig) eth_packet_send(); _sckt->toSend = 0; - _sckt->timeout = FALSE; _sckt->time = SOCKET_TIMEOUT(_sckt); } else { diff --git a/src/socket.c b/src/socket.c index eeef8b5..e2afb3d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -240,6 +240,7 @@ socket_send _sckt->tx_size = size; _sckt->toSend = ACK | PSH; _sckt->retry = RETRIES_TCP; + _sckt->timeout = FALSE; task_cancel(nwk_upstream); task_add(nwk_upstream, 0, 0,"upstream"); return TRUE; From d2d17df4e2b318926d24adccb5eb9e1a4578aef7 Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Fri, 1 Aug 2025 16:59:22 +0200 Subject: [PATCH 4/4] Remove disconnect event prior to completion of handshake If the callback responded to the disconnect event by doing socket_release, then then handshake would never complete. Therefore, hold off on the event until after the handshake has completed. --- src/nwk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nwk.c b/src/nwk.c index 65f0552..5fe4f9f 100644 --- a/src/nwk.c +++ b/src/nwk.c @@ -999,7 +999,7 @@ void nwk_downstream(void) #endif _sckt->state = _FIN_REC; _sckt->toSend = ACK | FIN; - ev = WEEIP_EV_DISCONNECT; // TESTE + // ev = WEEIP_EV_DISCONNECT; // TESTE break; }