Browse Source

Corrected bugs in the TCP logic. There might be a few more which need to be taken care of.

tags/v0.9.3
Nikhil Chandru Rao 18 years ago
parent
commit
8bf38fb400
1 changed files with 27 additions and 25 deletions
  1. 27
    25
      src/net/tcp.c

+ 27
- 25
src/net/tcp.c View File

326
 	conn->tcp_lstate = conn->tcp_state;
326
 	conn->tcp_lstate = conn->tcp_state;
327
 	conn->tcp_state = nxt_state;
327
 	conn->tcp_state = nxt_state;
328
 
328
 
329
-	printf ( "Transition from %s to %s\n", tcp_states[conn->tcp_lstate], tcp_states[conn->tcp_state] );
330
-
331
 	/* TODO: Check if this check is required */
329
 	/* TODO: Check if this check is required */
332
 	if ( conn->tcp_lstate == conn->tcp_state || 
330
 	if ( conn->tcp_lstate == conn->tcp_state || 
333
 	     conn->tcp_state == TCP_INVALID ) {
331
 	     conn->tcp_state == TCP_INVALID ) {
343
  * @v tcphdr	TCP header
341
  * @v tcphdr	TCP header
344
  */
342
  */
345
 void tcp_dump ( struct tcp_header *tcphdr ) {
343
 void tcp_dump ( struct tcp_header *tcphdr ) {
346
-/*
347
-	DBG ( "TCP header at %p+%d\n", tcphdr, sizeof ( *tcphdr ) );
348
-	DBG ( "\tSource port = %d, Destination port = %d\n",
349
-		ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ) );
350
-	DBG ( "\tSequence Number = %ld, Acknowledgement Number = %ld\n",
351
-		ntohl ( tcphdr->seq ), ntohl ( tcphdr->ack ) );
352
-	DBG ( "\tHeader length (/4) = %hd, Flags [..RAPUSF]= %#x\n",
353
-		( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ),
354
-		( tcphdr->flags & TCP_MASK_FLAGS ) );
355
-	DBG ( "\tAdvertised window = %ld, Checksum = %x, Urgent Pointer = %d\n",
356
-		ntohs ( tcphdr->win ), tcphdr->csum, ntohs ( tcphdr->urg ) );
357
-*/
358
-	DBG ( "TCP %p at %p src:%d dest:%d seq:%lld ack:%lld hlen:%hd flags:%#hx\n",
359
-		&tcp_protocol, tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
344
+	DBG ( "TCP %p src:%d dest:%d seq:%lld ack:%lld hlen:%hd flags:%#hx\n",
345
+		tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
360
 		ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
346
 		ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
361
 }
347
 }
362
 
348
 
437
 	return;
423
 	return;
438
 
424
 
439
   send_tcp_nomsg:
425
   send_tcp_nomsg:
440
-//	free_pkb ( conn->tx_pkb );
426
+	free_pkb ( conn->tx_pkb );
441
 	conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
427
 	conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
442
 	pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
428
 	pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
443
 	tcp_set_flags ( conn );
429
 	tcp_set_flags ( conn );
657
 	tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
643
 	tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
658
 	
644
 	
659
 	/* Dump the TCP header */
645
 	/* Dump the TCP header */
660
-	tcp_dump ( tcphdr );
661
 
646
 
662
 	/* Start the timer */
647
 	/* Start the timer */
663
 	if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
648
 	if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
685
 	struct tcp_header *tcphdr;
670
 	struct tcp_header *tcphdr;
686
 	uint32_t acked, toack;
671
 	uint32_t acked, toack;
687
 	int hlen;
672
 	int hlen;
688
-	int add = 0;
673
+	int add = 4;
689
 
674
 
690
 	/* Sanity check */
675
 	/* Sanity check */
691
 	if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
676
 	if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
709
 			return -EINVAL;
694
 			return -EINVAL;
710
 		}
695
 		}
711
 	}
696
 	}
712
-	
697
+
698
+
713
 	/* TODO: Verify checksum */
699
 	/* TODO: Verify checksum */
714
 	
700
 	
715
 	/* Demux TCP connection */
701
 	/* Demux TCP connection */
768
 				 */
754
 				 */
769
 				conn->snd_una = ntohl ( tcphdr->ack );
755
 				conn->snd_una = ntohl ( tcphdr->ack );
770
 				conn->tcp_op->connected ( conn );
756
 				conn->tcp_op->connected ( conn );
757
+				out_flags |= TCP_ACK;
771
 				tcp_senddata ( conn );
758
 				tcp_senddata ( conn );
759
+				return;
772
 			} else {
760
 			} else {
773
 				tcp_trans ( conn, TCP_SYN_RCVD );
761
 				tcp_trans ( conn, TCP_SYN_RCVD );
774
 				out_flags |= TCP_SYN;
762
 				out_flags |= TCP_SYN;
763
+				goto send_tcp_nomsg;
775
 			}
764
 			}
776
-			/* Send SYN,ACK or ACK packet */
777
-			goto send_tcp_nomsg;
778
 		}
765
 		}
779
 		/* Unexpected packet */
766
 		/* Unexpected packet */
780
 		goto unexpected;
767
 		goto unexpected;
792
 			 */
779
 			 */
793
 			conn->snd_una = tcphdr->ack - 1;
780
 			conn->snd_una = tcphdr->ack - 1;
794
 			conn->tcp_op->connected ( conn );
781
 			conn->tcp_op->connected ( conn );
795
-			tcp_senddata ( conn );
796
 			return 0;
782
 			return 0;
797
 		}
783
 		}
798
 		/* Unexpected packet */
784
 		/* Unexpected packet */
799
 		goto unexpected;
785
 		goto unexpected;
800
 	case TCP_ESTABLISHED:
786
 	case TCP_ESTABLISHED:
787
+#if 0
801
 		if ( tcphdr->flags & TCP_FIN ) {
788
 		if ( tcphdr->flags & TCP_FIN ) {
802
 			tcp_trans ( conn, TCP_CLOSE_WAIT );
789
 			tcp_trans ( conn, TCP_CLOSE_WAIT );
803
 			/* FIN consumes one byte */
790
 			/* FIN consumes one byte */
806
 			/* Send an acknowledgement */
793
 			/* Send an acknowledgement */
807
 			goto send_tcp_nomsg;
794
 			goto send_tcp_nomsg;
808
 		}
795
 		}
796
+#endif
809
 		/* Packet might contain data */
797
 		/* Packet might contain data */
810
 		break;
798
 		break;
811
 	case TCP_FIN_WAIT_1:
799
 	case TCP_FIN_WAIT_1:
874
 		if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
862
 		if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
875
 			conn->rcv_nxt += toack;
863
 			conn->rcv_nxt += toack;
876
 			conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ) + add, toack );
864
 			conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ) + add, toack );
865
+		} else {
866
+			printf ( "Unexpected sequence number %ld\n", 
867
+				ntohl ( tcphdr->seq ) );
877
 		}
868
 		}
878
 
869
 
879
 		/* Acknowledge new data */
870
 		/* Acknowledge new data */
892
 		}
883
 		}
893
 		/* Advance snd stream */
884
 		/* Advance snd stream */
894
 		conn->snd_una += acked;
885
 		conn->snd_una += acked;
895
-		/* Set the ACK flag */
896
-		conn->tcp_flags |= TCP_ACK;
897
 		/* Invoke the acked() callback function */
886
 		/* Invoke the acked() callback function */
898
 		conn->tcp_op->acked ( conn, acked );
887
 		conn->tcp_op->acked ( conn, acked );
899
 		/* Invoke the senddata() callback function */
888
 		/* Invoke the senddata() callback function */
900
 		tcp_senddata ( conn );
889
 		tcp_senddata ( conn );
901
 	}
890
 	}
891
+
892
+	/* If the connection is in ESTABLISHED and it receives a FIN */
893
+	if ( conn->tcp_state == ESTABLISHED && ( tcphdr->flags & TCP_FIN ) ) {
894
+		tcp_trans ( conn, TCP_CLOSE_WAIT );
895
+		conn->tcp_op->closed ( conn, CONN_SNDCLOSE );	
896
+		conn->rcv_nxt++;
897
+		if ( ! ( tcphdr->flags & TCP_ACK ) ) {
898
+			out_flags |= TCP_ACK;
899
+			/* Send an acknowledgement */
900
+			goto send_tcp_nomsg;
901
+		}
902
+		/* Otherwise, the packet has been ACKed already */
903
+	}
902
 	return 0;
904
 	return 0;
903
 
905
 
904
   send_tcp_nomsg:
906
   send_tcp_nomsg:

Loading…
Cancel
Save