uip.lst
来自「基于STC单片机与MICROCHIP ENC28J60的TCPIP以太网控制器」· LST 代码 · 共 1,174 行 · 第 1/5 页
LST
1,174 行
903 2 data into the uip_buf. If the UIP_ACKDATA flag is set, the
904 2 application should put new data into the buffer, otherwise we are
905 2 retransmitting an old segment, and the application should put that
906 2 data into the buffer.
907 2
908 2 If the incoming packet is a FIN, we should close the connection on
909 2 this side as well, and we send out a FIN and enter the LAST_ACK
910 2 state. We require that there is no outstanding data; otherwise the
911 2 sequence numbers will be screwed up. */
912 2
913 2 if(BUF->flags & TCP_FIN) { //代表要发送的数据已经结束了
914 3 if(uip_outstanding(uip_connr)) {
915 4 goto drop;
916 4 }
917 3
918 3 uip_add_rcv_nxt(1 + uip_len);
919 3
920 3 uip_flags = UIP_CLOSE;
921 3 if(uip_len > 0) {
922 4 uip_flags |= UIP_NEWDATA;
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 16
923 4 }
924 3 UIP_APPCALL();
925 3 uip_connr->len = 1;
926 3 uip_connr->tcpstateflags = LAST_ACK;
927 3 uip_connr->nrtx = 0;
928 3 tcp_send_finack:
929 3 connect_state=0; //add8
930 3 BUF->flags = TCP_FIN | TCP_ACK; //0x11
931 3 goto tcp_send_nodata;
932 3 }
933 2
934 2 /* Check the URG flag. If this is set, the segment carries urgent
935 2 data that we must pass to the application. */
936 2 if(BUF->flags & TCP_URG) {
937 3 #if UIP_URGDATA > 0
uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
if(uip_urglen > uip_len) {
/* There is more urgent data in the next segment to come. */
uip_urglen = uip_len;
}
uip_add_rcv_nxt(uip_urglen);
uip_len -= uip_urglen;
uip_urgdata = uip_appdata;
uip_appdata += uip_urglen;
} else {
uip_urglen = 0;
#endif /* UIP_URGDATA > 0 */
950 3 uip_appdata += (BUF->urgp[0] << 8) | BUF->urgp[1];
951 3 uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
952 3 }
953 2
954 2
955 2 /* If uip_len > 0 we have TCP data in the packet, and we flag this
956 2 by setting the UIP_NEWDATA flag and update the sequence number
957 2 we acknowledge. If the application has stopped the dataflow
958 2 using uip_stop(), we must not accept any data packets from the
959 2 remote host. */
960 2 if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
961 3 uip_flags |= UIP_NEWDATA;
962 3 uip_add_rcv_nxt(uip_len);
963 3 }
964 2
965 2 /* Check if the available buffer space advertised by the other end
966 2 is smaller than the initial MSS for this connection. If so, we
967 2 set the current MSS to the window size to ensure that the
968 2 application does not send more data than the other end can
969 2 handle.
970 2
971 2 If the remote host advertises a zero window, we set the MSS to
972 2 the initial MSS so that the application will send an entire MSS
973 2 of data. This data will not be acknowledged by the receiver,
974 2 and the application will retransmit it. This is called the
975 2 "persistent timer" and uses the retransmission mechanim.
976 2 */
977 2 tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
978 2 if(tmp16 > uip_connr->initialmss ||
979 2 tmp16 == 0) {
980 3 tmp16 = uip_connr->initialmss;
981 3 }
982 2 uip_connr->mss = tmp16;
983 2
984 2 /* If this packet constitutes建立 an ACK for outstanding data (flagged
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 17
985 2 by the UIP_ACKDATA flag, we should call the application since it
986 2 might want to send more data. If the incoming packet had data
987 2 from the peer (as flagged by the UIP_NEWDATA flag), the
988 2 application must also be notified.
989 2
990 2 When the application is called, the global variable uip_len
991 2 contains the length of the incoming data. The application can
992 2 access the incoming data through the global pointer
993 2 uip_appdata, which usually points 40 bytes into the uip_buf
994 2 array.
995 2
996 2 If the application wishes to send any data, this data should be
997 2 put into the uip_appdata and the length of the data should be
998 2 put into uip_len. If the application don't have any data to
999 2 send, uip_len must be set to 0. */
1000 2 if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
1001 3 uip_slen = 0;
1002 3 UIP_APPCALL();
1003 3
1004 3 appsend:
1005 3 //在此加入程序
1006 3 if(uip_flags & UIP_ABORT) {
1007 4 uip_slen = 0;
1008 4 uip_connr->tcpstateflags = CLOSED;
1009 4 BUF->flags = TCP_RST | TCP_ACK;
1010 4 // P11=~P11; //add8
1011 4 goto tcp_send_nodata;
1012 4 }
1013 3
1014 3 if(uip_flags & UIP_CLOSE) {
1015 4 uip_slen = 0;
1016 4 uip_connr->len = 1;
1017 4 uip_connr->tcpstateflags = FIN_WAIT_1;
1018 4 uip_connr->nrtx = 0;
1019 4 BUF->flags = TCP_FIN | TCP_ACK;
1020 4 goto tcp_send_nodata;
1021 4 }
1022 3
1023 3 /* If uip_slen > 0, the application has data to be sent. */
1024 3 if(uip_slen > 0) {
1025 4
1026 4 /* If the connection has acknowledged data, the contents of
1027 4 the uip->len variable should be discarded. */
1028 4 if((uip_flags & UIP_ACKDATA) != 0) {
1029 5 uip_connr->len = 0;
1030 5 }
1031 4
1032 4 /* If the ->len variable is non-zero the connection has
1033 4 already data in transit and cannot send anymore right
1034 4 now. */
1035 4 if(uip_connr->len == 0) { //表示已经可以传送了
1036 5
1037 5 /* The application cannot send more than what is allowed by
1038 5 the mss (the minumum of the MSS and the available
1039 5 window). */
1040 5 if(uip_slen > uip_connr->mss) {
1041 6 uip_slen = uip_connr->mss;
1042 6 }
1043 5
1044 5 /* Remember how much data we send out now so that we know
1045 5 when everything has been acknowledged. */
1046 5 uip_connr->len = uip_slen;
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 18
1047 5 } else {
1048 5
1049 5 /* If the application already had unacknowledged data, we
1050 5 make sure that the application does not send (i.e.,
1051 5 retransmit) out more than it previously sent out. */
1052 5 uip_slen = uip_connr->len;
1053 5 }
1054 4 } else {
1055 4 uip_connr->len = 0;
1056 4 }
1057 3
1058 3 uip_connr->nrtx = 0;
1059 3
1060 3 apprexmit:
1061 3 uip_appdata = uip_sappdata;
1062 3
1063 3 /* If the application has data to be sent, or if the incoming
1064 3 packet had new data in it, we must send out a packet. */
1065 3 if(uip_slen > 0 && uip_connr->len > 0) {
1066 4
1067 4 /* Add the length of the IP and TCP headers. */
1068 4 uip_len = uip_connr->len + UIP_TCPIP_HLEN;
1069 4
1070 4 /* We always set the ACK flag in response packets. */
1071 4 BUF->flags = TCP_ACK | TCP_PSH;
1072 4
1073 4 /* Send the packet. */
1074 4 goto tcp_send_noopts;
1075 4 }
1076 3 /* If there is no data to send, just send out a pure ACK if
1077 3 there is newdata. */
1078 3 if(uip_flags & UIP_NEWDATA) { //单独的ACK发送
1079 4 uip_len = UIP_TCPIP_HLEN;
1080 4 BUF->flags = TCP_ACK;
1081 4 goto tcp_send_noopts;
1082 4 }
1083 3 }
1084 2 goto drop;
1085 2 case LAST_ACK:
1086 2 /* We can close this connection if the peer has acknowledged our
1087 2 FIN. This is indicated by the UIP_ACKDATA flag. */
1088 2 if(uip_flags & UIP_ACKDATA) {
1089 3 uip_connr->tcpstateflags = CLOSED;
1090 3 uip_flags = UIP_CLOSE;
1091 3 UIP_APPCALL();
1092 3 }
1093 2 break;
1094 2
1095 2 case FIN_WAIT_1: //发送的时候用
1096 2 /* The application has closed the connection, but the remote host
1097 2 hasn't closed its end yet. Thus we do nothing but wait for a
1098 2 FIN from the other side. */
1099 2 if(uip_len > 0) {
1100 3 uip_add_rcv_nxt(uip_len);
1101 3 }
1102 2 if(BUF->flags & TCP_FIN) {
1103 3 if(uip_flags & UIP_ACKDATA) {
1104 4 uip_connr->tcpstateflags = TIME_WAIT;
1105 4 uip_connr->timer = 0;
1106 4 uip_connr->len = 0;
1107 4 } else {
1108 4 uip_connr->tcpstateflags = CLOSING;
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 19
1109 4 }
1110 3 uip_add_rcv_nxt(1);
1111 3 uip_flags = UIP_CLOSE;
1112 3 UIP_APPCALL();
1113 3 goto tcp_send_ack;
1114 3 } else if(uip_flags & UIP_ACKDATA) {
1115 3 uip_connr->tcpstateflags = FIN_WAIT_2;
1116 3 uip_connr->len = 0;
1117 3 goto drop;
1118 3 }
1119 2 if(uip_len > 0) {
1120 3 goto tcp_send_ack;
1121 3 }
1122 2 goto drop;
1123 2
1124 2 case FIN_WAIT_2:
1125 2 if(uip_len > 0) {
1126 3 uip_add_rcv_nxt(uip_len);
1127 3 }
1128
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?