@@ -120,7 +120,7 @@ static bool if_poll(struct mg_tcpip_if *ifp, bool s1) {
120
120
121
121
static size_t if_rx (void * buf , size_t len , struct mg_tcpip_if * ifp ) {
122
122
struct driver_data * driver_data = (struct driver_data * ) ifp -> driver_data ;
123
- if (! driver_data -> len ) return 0 ;
123
+ if (driver_data -> len == 0 ) return 0 ;
124
124
if (len > driver_data -> len ) len = driver_data -> len ;
125
125
memcpy (buf , driver_data -> buf , len );
126
126
driver_data -> len = 0 ; // cleaning up the buffer
@@ -142,10 +142,10 @@ static void create_tcp_pkt(struct eth *e, struct ip *ip, uint32_t seq,
142
142
t .ack = mg_htonl (ack );
143
143
t .off = 5 << 4 ;
144
144
memcpy (s_driver_data .buf , e , sizeof (* e ));
145
+ ip -> len = mg_htons ((uint16_t )(sizeof (* ip ) + sizeof (struct tcp ) + payload_len ));
145
146
memcpy (s_driver_data .buf + sizeof (* e ), ip , sizeof (* ip ));
146
- memcpy (s_driver_data .buf + sizeof (* e ) + sizeof (* ip ), & t , sizeof (struct tcp ));
147
- s_driver_data .len =
148
- sizeof (* e ) + sizeof (* ip ) + sizeof (struct tcp ) + payload_len ;
147
+ memcpy (s_driver_data .buf + sizeof (* e ) + sizeof (* ip ), & t , sizeof (t ));
148
+ s_driver_data .len = sizeof (* e ) + sizeof (* ip ) + sizeof (t ) + payload_len ;
149
149
}
150
150
151
151
static void init_tcp_handshake (struct eth * e , struct ip * ip , struct tcp * tcp ,
@@ -199,13 +199,10 @@ static void test_retransmit(void) {
199
199
// setting the IP header
200
200
memset (& ip , 0 , sizeof (ip ));
201
201
ip .ver = 4 << 4 , ip .proto = 6 ;
202
- ip .len = mg_htons (sizeof (ip ) + sizeof (struct tcp ));
203
202
204
203
init_tcp_handshake (& e , & ip , t , & mgr );
205
204
206
205
// packet with seq_no = 1001
207
- ip .len =
208
- mg_htons (sizeof (struct ip ) + sizeof (struct tcp ) + /* TCP Payload */ 2 );
209
206
create_tcp_pkt (& e , & ip , 1001 , 1 , TH_PUSH | TH_ACK , 2 );
210
207
mg_mgr_poll (& mgr , 0 );
211
208
while (!received_response (& s_driver_data )) mg_mgr_poll (& mgr , 0 );
@@ -253,6 +250,39 @@ static void test_retransmit(void) {
253
250
ASSERT ((t -> flags == TH_ACK ));
254
251
ASSERT ((t -> ack == mg_htonl (1005 ))); // OK
255
252
253
+ // packet with seq_no = 1005 got delayed, send FIN with seq_no = 1007
254
+ create_tcp_pkt (& e , & ip , 1007 , 1 , TH_FIN , 0 );
255
+ mg_mgr_poll (& mgr , 0 );
256
+ start = mg_millis ();
257
+ while (!received_response (& s_driver_data )) {
258
+ mg_mgr_poll (& mgr , 0 );
259
+ now = mg_millis () - start ;
260
+ if (now > 2 * MIP_TCP_ACK_MS )
261
+ ASSERT (0 ); // response should have been received by now
262
+ }
263
+ t = (struct tcp * ) (s_driver_data .buf + sizeof (struct eth ) +
264
+ sizeof (struct ip ));
265
+ ASSERT ((t -> flags == TH_ACK ));
266
+ ASSERT ((t -> ack == mg_htonl (1005 ))); // dup ACK
267
+
268
+ // retransmitting packet with seq_no = 1005
269
+ create_tcp_pkt (& e , & ip , 1005 , 1 , TH_PUSH | TH_ACK , 2 );
270
+ mg_mgr_poll (& mgr , 0 );
271
+ while (!received_response (& s_driver_data )) mg_mgr_poll (& mgr , 0 );
272
+ t = (struct tcp * ) (s_driver_data .buf + sizeof (struct eth ) +
273
+ sizeof (struct ip ));
274
+ ASSERT ((t -> flags == TH_ACK ));
275
+ ASSERT ((t -> ack == mg_htonl (1007 ))); // OK
276
+
277
+ // retransmitting FIN packet with seq_no = 1007
278
+ create_tcp_pkt (& e , & ip , 1007 , 1 , TH_FIN | TH_ACK , 0 );
279
+ mg_mgr_poll (& mgr , 0 );
280
+ while (!received_response (& s_driver_data )) mg_mgr_poll (& mgr , 0 );
281
+ t = (struct tcp * ) (s_driver_data .buf + sizeof (struct eth ) +
282
+ sizeof (struct ip ));
283
+ ASSERT ((t -> flags == (TH_FIN | TH_ACK ))); // check we respond with FIN ACK
284
+ ASSERT ((t -> ack == mg_htonl (1008 ))); // OK
285
+
256
286
s_driver_data .len = 0 ;
257
287
mg_mgr_free (& mgr );
258
288
}
@@ -285,12 +315,10 @@ static void test_frag_recv_path(void) {
285
315
// setting the IP header
286
316
memset (& ip , 0 , sizeof (ip ));
287
317
ip .ver = 0x45 , ip .proto = 6 ;
288
- ip .len = mg_htons (sizeof (ip ) + sizeof (struct tcp ));
289
318
290
319
init_tcp_handshake (& e , & ip , t , & mgr );
291
320
292
321
// send fragmented TCP packet
293
- ip .len = mg_htons (sizeof (struct ip ) + sizeof (struct tcp ) + 1000 );
294
322
ip .frag |= IP_MORE_FRAGS_MSK ; // setting More Fragments bit to 1
295
323
create_tcp_pkt (& e , & ip , 1001 , 1 , TH_PUSH | TH_ACK , 1000 );
296
324
s_sent_fragment = 1 ; // "enable" fn
0 commit comments