📄 ne2000.lst
字号:
844 2
845 2 break;
846 2
847 2 case SYN_RCVD:
848 2 trans_str("S");
849 2
850 2 // Check if addressed for this socket
851 2
852 2 if (port!=tcp_source_port || ip[0]!=source_ip[0] || ip[1]!=source_ip[1] ||
853 2 ip[2]!=source_ip[2] || ip[3]!= source_ip[3])
854 2 break;
855 2
856 2 // We are expecting an ACK without a SYN
857 2 if (flags & TCP_ACK) {
858 3 // ACK set
859 3 // We should check to be sure an SYN wasn't also sent
860 3 if (flags & TCP_SYN) {
C51 COMPILER V7.09 NE2000 03/27/2005 16:17:14 PAGE 15
861 4 break; // Ignore it for now. We'll add code to handle later
862 4 }
863 3 // We shouldn't have a RST or FIN with an ACK
864 3 if (flags & TCP_RST || flags & TCP_FIN) {
865 4 break; // Ignore it for now. We'll add code to hanlde later
866 4 }
867 3 // Everything looks okay so handle the ACK response
868 3
869 3 tcp_syn_rcvd();
870 3
871 3 }
872 2
873 2 break;
874 2
875 2
876 2 case ESTAB:
877 2 trans_str("E");
878 2
879 2 // We are expecting an ACK without or SYN
880 2 // We could receive a FIN
881 2 if (flags & TCP_ACK) {
882 3 // ACK set
883 3 // We should check to be sure an SYN wasn't also sent
884 3 if (flags & TCP_SYN) {
885 4 break; // Ignore it for now. We'll add code to handle later
886 4 }
887 3 // We shouldn't have a RST with an ACK
888 3 if (flags & TCP_RST ) {
889 4 break; // Ignore it for now. We'll add code to hanlde later
890 4 }
891 3
892 3
893 3 // ************************************************************
894 3 // *** - check received sequence number to be sure it's ***
895 3 // *** what we expected ***
896 3 // *** - check acknowledgement number to be sure it's what ***
897 3 // *** we are going to send ***
898 3 // *** - check source ip address and source port to be sure ***
899 3 // *** they are correct ***
900 3 // ************************************************************
901 3
902 3
903 3
904 3 if (seq[0] != xm_ack[0] || seq[1] != xm_ack[1] ||
905 3 ack[0] != xm_seq[0] || ack[1] != xm_seq[1])
906 3 break;
907 3
908 3
909 3
910 3 if (port != tcp_source_port)
911 3 break;
912 3
913 3
914 3
915 3 if (ip[0] != source_ip[0] || ip[1] != source_ip[1] ||
916 3 ip[2] != source_ip[2] || ip[3] != source_ip[3])
917 3 break;
918 3
919 3 trans_str("I-");
920 3
921 3 // Everything looks okay so handle the ACK response
922 3
C51 COMPILER V7.09 NE2000 03/27/2005 16:17:14 PAGE 16
923 3 tcp_estab();
924 3
925 3 }
926 2
927 2 }
928 1
929 1 }
*** WARNING C280 IN LINE 819 OF NE2000.C: 'i': unreferenced local variable
930
931 // ********************************************************
932 void load_TCP_header(u16_t data_flags, u16_t tcp_chksum, u16_t tcp_length) {
933 1
934 1 u16_t prev_chksum;
935 1
936 1 chksum = tcp_chksum;
937 1
938 1 // calculate checksum of pseudo header
939 1
940 1 prev_chksum = chksum;
941 1 chksum += ((u16_t)my_ip[0]<<8) + my_ip[1];
942 1 if (chksum < prev_chksum) chksum++;
943 1
944 1 prev_chksum = chksum;
945 1 chksum += ((u16_t)my_ip[2]<<8) + my_ip[3];
946 1 if (chksum < prev_chksum) chksum++;
947 1
948 1 prev_chksum = chksum;
949 1 chksum += ((u16_t)source_ip[0]<<8) + source_ip[1];
950 1 if (chksum < prev_chksum) chksum++;
951 1
952 1 prev_chksum = chksum;
953 1 chksum += ((u16_t)source_ip[2]<<8) + source_ip[3];
954 1 if (chksum < prev_chksum) chksum++;
955 1
956 1 prev_chksum = chksum;
957 1 chksum += TCP;
958 1 if (chksum < prev_chksum) chksum++;
959 1
960 1 prev_chksum = chksum;
961 1 chksum += tcp_length;
962 1 if (chksum < prev_chksum) chksum++;
963 1
964 1 // source port
965 1
966 1 myoutportw(NIC_DATA, tcp_dest_port);
967 1
968 1 // destination port
969 1
970 1 myoutportw(NIC_DATA, tcp_source_port);
971 1
972 1 // sequence number
973 1
974 1 myoutportw(NIC_DATA, xm_seq[0]);
975 1 myoutportw(NIC_DATA, xm_seq[1]);
976 1
977 1 // acknowledment number
978 1
979 1 myoutportw(NIC_DATA, xm_ack[0]);
980 1 myoutportw(NIC_DATA, xm_ack[1]);
981 1
982 1 // data offset and flags
983 1
C51 COMPILER V7.09 NE2000 03/27/2005 16:17:14 PAGE 17
984 1 myoutportw(NIC_DATA, data_flags);
985 1
986 1 // window0
987 1
988 1 myoutportw(NIC_DATA, 1460);
989 1
990 1 // checksum
991 1
992 1 myoutportw(NIC_DATA, ~chksum);
993 1
994 1 // urgent pointer
995 1
996 1 myoutportw(NIC_DATA, 0);
997 1
998 1
999 1 }
1000
1001 // ********************************************************
1002 void tcp_listen(void) {
1003 1
1004 1 u16_t i;
1005 1
1006 1 xm_ack[0] = seq[0];
1007 1 xm_ack[1] = seq[1];
1008 1
1009 1 port = tcp_source_port;
1010 1
1011 1 // Save the source IP address
1012 1
1013 1 for (i=0 ; i<4 ; i++)
1014 1 ip[i] = source_ip[i];
1015 1
1016 1 // Calculate data length
1017 1
1018 1 tcp_data_len = IP_length - (hdr_len + offset);
1019 1
1020 1 // Increment sequence to account for SYN and add data
1021 1
1022 1 xm_ack[1] += tcp_data_len + 1;
1023 1 if (xm_ack[1] < seq[1] )
1024 1 xm_ack[0]++;
1025 1
1026 1 // *** Need to handle data if any here.
1027 1
1028 1 // Let's reply with our on SYN and an ACK
1029 1 // We should include a Max Recieve window0 option
1030 1
1031 1 xm_seq[1]++;
1032 1 if (xm_seq[1] == 0)
1033 1 xm_seq[0]++;
1034 1
1035 1 load_ethernet_header(); // 12 bytes (2 bytes for protocol included in load_IP..)
1036 1
1037 1 load_IP_header(48,TCP); // Number of bytes included in call (add 2 for protocol)
1038 1 // 20 for IP header
1039 1 // 20 for TCP header
1040 1 // 8 for Options
1041 1
1042 1
1043 1 load_TCP_header( 0x7000 | TCP_SYN | TCP_ACK, 0x07B8, 28);
1044 1 // Number of bytes include in load_IP_header call
1045 1
C51 COMPILER V7.09 NE2000 03/27/2005 16:17:14 PAGE 18
1046 1 // Send options
1047 1
1048 1 myoutportb(NIC_DATA,0x02);
1049 1 myoutportb(NIC_DATA,0x04);
1050 1 myoutportb(NIC_DATA,0x05);
1051 1 myoutportb(NIC_DATA,0xb4);
1052 1 myoutportb(NIC_DATA,0x00);
1053 1 myoutportb(NIC_DATA,0x00);
1054 1 myoutportb(NIC_DATA,0x00);
1055 1 myoutportb(NIC_DATA,0x00);
1056 1
1057 1 send_packet(62);
1058 1
1059 1 // Calculate the next sequence number to send
1060 1 // In this case we add 1 since we only sent a SYN
1061 1
1062 1 xm_seq[1]++;
1063 1 if (xm_seq[1] == 0)
1064 1 xm_seq[0]++;
1065 1
1066 1 html_socket = SYN_RCVD;
1067 1
1068 1 }
1069
1070 // ********************************************************
1071 void tcp_syn_rcvd(void) {
1072 1
1073 1
1074 1 // Calculate data length
1075 1
1076 1 tcp_data_len = IP_length - (hdr_len + offset);
1077 1
1078 1 // Increment sequence to account for data
1079 1
1080 1 xm_ack[1] += tcp_data_len;
1081 1 if (xm_ack[1] < seq[1] )
1082 1 xm_ack[0]++;
1083 1
1084 1 // *** Need to handle data if any here.
1085 1 // We'll assume there isn't any for now so no response is needed
1086 1
1087 1 html_socket = ESTAB;
1088 1
1089 1 }
1090
1091 // ********************************************************
1092 void tcp_estab(void) {
1093 1
1094 1 tcp_data_len = IP_length - (hdr_len + offset);
1095 1
1096 1 // This is the next sequence number we expect
1097 1
1098 1 xm_ack[1] += tcp_data_len;
1099 1 if (xm_ack[1] < seq[1] )
1100 1 xm_ack[0]++;
1101 1
1102 1 // Let's cheat and send an HTML response
1103 1
1104 1 load_ethernet_header(); // 12 bytes (2 bytes for protocol included in load_IP..)
1105 1
1106 1 load_IP_header(188,TCP); // Number of bytes included in call (add 2 for protocol)
1107 1 // 20 for IP header
C51 COMPILER V7.09 NE2000 03/27/2005 16:17:14 PAGE 19
1108 1 // 20 for TCP header
1109 1 // 148 for Data
1110 1
1111 1
1112 1 load_TCP_header( 0x5000 | TCP_ACK | TCP_FIN, 0x07AC, 148);
1113 1 // Number of bytes include in load_IP_header call
1114 1
1115 1
1116 1 myoutportstr(NIC_DATA,"HTTP/1.0 200 OK\r\n"
1117 1 "Content-type: text/html\r\n\r\n"
1118 1 "<HTML><body>SimmStick® Web Server<br>"
1119 1 "using a PIC 16F874<br>"
1120 1 "by David C. Witt (12/24/00)</body></HTML>");
1121 1
1122 1 xm_ack[1] += 148;
1123 1 if (xm_ack[1] < seq[1])
1124 1 xm_ack[0]++;
1125 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -