📄 uip.lst
字号:
udp_found:
C51 COMPILER V8.15 UIP 08/11/2009 15:07:52 PAGE 14
uip_len = uip_len - 28;
uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
uip_flags = UIP_NEWDATA;
uip_slen = 0;
UIP_UDP_APPCALL();
udp_send:
if(uip_slen == 0) {
goto drop;
}
uip_len = uip_slen + 28;
BUF->len[0] = (uip_len >> 8);
BUF->len[1] = (uip_len & 0xff);
BUF->proto = UIP_PROTO_UDP;
UDPBUF->udplen = HTONS(uip_slen + 8);
UDPBUF->udpchksum = 0;
#if UIP_UDP_CHECKSUMS
/* Calculate UDP checksum. */
UDPBUF->udpchksum = ~(uip_udpchksum());
if(UDPBUF->udpchksum == 0) {
UDPBUF->udpchksum = 0xffff;
}
#endif /* UIP_UDP_CHECKSUMS */
BUF->srcport = uip_udp_conn->lport;
BUF->destport = uip_udp_conn->rport;
BUF->srcipaddr[0] = uip_hostaddr[0];
BUF->srcipaddr[1] = uip_hostaddr[1];
BUF->destipaddr[0] = uip_udp_conn->ripaddr[0];
BUF->destipaddr[1] = uip_udp_conn->ripaddr[1];
uip_appdata = &uip_buf[UIP_LLH_LEN + 40];
goto ip_send_nolen;
#endif /* UIP_UDP */
835 1
836 1 /* TCP input processing. */
837 1 tcp_input:
838 1 UIP_STAT(++uip_stat.tcp.recv);
839 1
840 1 /* Start of TCP input header processing code. */
841 1
842 1 if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
843 2 checksum. */
844 2 UIP_STAT(++uip_stat.tcp.drop);
845 2 UIP_STAT(++uip_stat.tcp.chkerr);
846 2 Printf_String("\r\n[MSG:] tcp: bad checksum.");
847 2 goto drop;
848 2 }
849 1
850 1 /* Demultiplex this segment. */
851 1 /* First check any active connections. */
852 1 for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
853 2 if(uip_connr->tcpstateflags != CLOSED &&
854 2 BUF->destport == uip_connr->lport &&
855 2 BUF->srcport == uip_connr->rport &&
856 2 BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
857 2 BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
858 3 goto found;
859 3 }
C51 COMPILER V8.15 UIP 08/11/2009 15:07:52 PAGE 15
860 2 }
861 1
862 1 /* If we didn't find and active connection that expected the packet,
863 1 either this packet is an old duplicate, or this is a SYN packet
864 1 destined for a connection in LISTEN. If the SYN flag isn't set,
865 1 it is an old packet and we send a RST. */
866 1 if((BUF->flags & TCP_CTL) != TCP_SYN)
867 1 goto reset;
868 1
869 1 tmp16 = BUF->destport;
870 1 /* Next, check listening connections. */
871 1 for(c = 0; c < UIP_LISTENPORTS; ++c) {
872 2 if(tmp16 == uip_listenports[c])
873 2 goto found_listen;
874 2 }
875 1
876 1 /* No matching connection found, so we send a RST packet. */
877 1 UIP_STAT(++uip_stat.tcp.synrst);
878 1 reset:
879 1
880 1 /* We do not send resets in response to resets. */
881 1 if(BUF->flags & TCP_RST)
882 1 goto drop;
883 1
884 1 UIP_STAT(++uip_stat.tcp.rst);
885 1
886 1 BUF->flags = TCP_RST | TCP_ACK;
887 1 uip_len = 40;
888 1 BUF->tcpoffset = 5 << 4;
889 1
890 1 /* Flip the seqno and ackno fields in the TCP header. */
891 1 c = BUF->seqno[3];
892 1 BUF->seqno[3] = BUF->ackno[3];
893 1 BUF->ackno[3] = c;
894 1
895 1 c = BUF->seqno[2];
896 1 BUF->seqno[2] = BUF->ackno[2];
897 1 BUF->ackno[2] = c;
898 1
899 1 c = BUF->seqno[1];
900 1 BUF->seqno[1] = BUF->ackno[1];
901 1 BUF->ackno[1] = c;
902 1
903 1 c = BUF->seqno[0];
904 1 BUF->seqno[0] = BUF->ackno[0];
905 1 BUF->ackno[0] = c;
906 1
907 1 /* We also have to increase the sequence number we are
908 1 acknowledging. If the least significant byte overflowed, we need
909 1 to propagate the carry to the other bytes as well. */
910 1 if(++BUF->ackno[3] == 0) {
911 2 if(++BUF->ackno[2] == 0) {
912 3 if(++BUF->ackno[1] == 0) {
913 4 ++BUF->ackno[0];
914 4 }
915 3 }
916 2 }
917 1
918 1 /* Swap port numbers. */
919 1 tmp16 = BUF->srcport;
920 1 BUF->srcport = BUF->destport;
921 1 BUF->destport = tmp16;
C51 COMPILER V8.15 UIP 08/11/2009 15:07:52 PAGE 16
922 1
923 1 /* Swap IP addresses. */
924 1 tmp16 = BUF->destipaddr[0];
925 1 BUF->destipaddr[0] = BUF->srcipaddr[0];
926 1 BUF->srcipaddr[0] = tmp16;
927 1 tmp16 = BUF->destipaddr[1];
928 1 BUF->destipaddr[1] = BUF->srcipaddr[1];
929 1 BUF->srcipaddr[1] = tmp16;
930 1
931 1
932 1 /* And send out the RST packet! */
933 1 goto tcp_send_noconn;
934 1
935 1 /* This label will be jumped to if we matched the incoming packet
936 1 with a connection in LISTEN. In that case, we should create a new
937 1 connection and send a SYNACK in return. */
938 1 found_listen:
939 1 /* First we check if there are any connections avaliable. Unused
940 1 connections are kept in the same table as used connections, but
941 1 unused ones have the tcpstate set to CLOSED. Also, connections in
942 1 TIME_WAIT are kept track of and we'll use the oldest one if no
943 1 CLOSED connections are found. Thanks to Eddie C. Dost for a very
944 1 nice algorithm for the TIME_WAIT search. */
945 1 uip_connr = 0;
946 1 for(c = 0; c < UIP_CONNS; ++c) {
947 2 if(uip_conns[c].tcpstateflags == CLOSED) {
948 3 uip_connr = &uip_conns[c];
949 3 break;
950 3 }
951 2 if(uip_conns[c].tcpstateflags == TIME_WAIT) {
952 3 if(uip_connr == 0 ||
953 3 uip_conns[c].timer > uip_connr->timer) {
954 4 uip_connr = &uip_conns[c];
955 4 }
956 3 }
957 2 }
958 1
959 1 if(uip_connr == 0) {
960 2 /* All connections are used already, we drop packet and hope that
961 2 the remote end will retransmit the packet at a time when we
962 2 have more spare connections. */
963 2 UIP_STAT(++uip_stat.tcp.syndrop);
964 2 Printf_String("\r\n[MSG:] tcp: found no unused connections.");
965 2 goto drop;
966 2 }
967 1 uip_conn = uip_connr;
968 1
969 1 /* Fill in the necessary fields for the new connection. */
970 1 uip_connr->rto = uip_connr->timer = UIP_RTO;
971 1 uip_connr->sa = 0;
972 1 uip_connr->sv = 4;
973 1 uip_connr->nrtx = 0;
974 1 uip_connr->lport = BUF->destport;
975 1 uip_connr->rport = BUF->srcport;
976 1 uip_connr->ripaddr[0] = BUF->srcipaddr[0];
977 1 uip_connr->ripaddr[1] = BUF->srcipaddr[1];
978 1 uip_connr->tcpstateflags = SYN_RCVD;
979 1
980 1 uip_connr->snd_nxt[0] = iss[0];
981 1 uip_connr->snd_nxt[1] = iss[1];
982 1 uip_connr->snd_nxt[2] = iss[2];
983 1 uip_connr->snd_nxt[3] = iss[3];
C51 COMPILER V8.15 UIP 08/11/2009 15:07:52 PAGE 17
984 1 uip_connr->len = 1;
985 1
986 1 /* rcv_nxt should be the seqno from the incoming packet + 1. */
987 1 uip_connr->rcv_nxt[3] = BUF->seqno[3];
988 1 uip_connr->rcv_nxt[2] = BUF->seqno[2];
989 1 uip_connr->rcv_nxt[1] = BUF->seqno[1];
990 1 uip_connr->rcv_nxt[0] = BUF->seqno[0];
991 1 uip_add_rcv_nxt(1);
992 1
993 1 /* Parse the TCP MSS option, if present. */
994 1 if((BUF->tcpoffset & 0xf0) > 0x50) {
995 2 for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
996 3 opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
997 3 if(opt == 0x00) {
998 4 /* End of options. */
999 4 break;
1000 4 } else if(opt == 0x01) {
1001 4 ++c;
1002 4 /* NOP option. */
1003 4 } else if(opt == 0x02 &&
1004 3 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
1005 4 /* An MSS option with the right option length. */
1006 4 tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1007 4 (u16_t)uip_buf[40 + UIP_LLH_LEN + 3 + c];
1008 4 uip_connr->initialmss = uip_connr->mss =
1009 4 tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1010 4
1011 4 /* And we are done processing options. */
1012 4 break;
1013 4 } else {
1014 4 /* All other options have a length field, so that we easily
1015 4 can skip past them. */
1016 4 if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1017 5 /* If the length field is zero, the options are malformed
1018 5 and we don't process them further. */
1019 5 break;
1020 5 }
1021 4 c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1022 4 }
1023 3 }
1024 2 }
1025 1
1026 1 /* Our response will be a SYNACK. */
1027 1 #if UIP_ACTIVE_OPEN
tcp_send_synack:
BUF->flags = TCP_ACK;
tcp_send_syn:
BUF->flags |= TCP_SYN;
#else /* UIP_ACTIVE_OPEN */
1034 1 tcp_send_synack:
1035 1 BUF->flags = TCP_SYN | TCP_ACK;
1036 1 #endif /* UIP_ACTIVE_OPEN */
1037 1
1038 1 /* We send out the TCP Maximum Segment Size option with our
1039 1 SYNACK. */
1040 1 BUF->optdata[0] = 2;
1041 1 BUF->optdata[1] = 4;
1042 1 BUF->optdata[2] = (UIP_TCP_MSS) / 256;
1043 1 BUF->optdata[3] = (UIP_TCP_MSS) & 255;
1044 1 uip_len = 44;
1045 1 BUF->tcpoffset = 6 << 4;
C51 COMPILER V8.15 UIP 08/11/2009 15:07:52 PAGE 18
1046 1 goto tcp_send;
1047 1
1048 1 /* This label will be jumped to if we found an active connection. */
1049 1 found:
1050 1 uip_conn = uip_connr;
1051 1 uip_flags = 0;
1052 1
1053 1 /* We do a very naive form of TCP reset processing; we just accept
1054 1 any RST and kill our connection. We should in fact check if the
1055 1 sequence number of this reset is wihtin our advertised window
1056 1 before we accept the reset. */
1057 1 if(BUF->flags & TCP_RST) {
1058 2 uip_connr->tcpstateflags = CLOSED;
1059 2 UIP_LOG("\r\n[MSG:] tcp: got reset, aborting connection.");
1060 2 uip_flags = UIP_ABORT;
1061 2 UIP_APPCALL();
1062 2 goto drop;
1063 2 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -