📄 uip.lst
字号:
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE UIP
OBJECT MODULE PLACED IN uip.OBJ
COMPILER INVOKED BY: D:\Keil\C51\BIN\C51.EXE uip.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /**
2 * \addtogroup uip
3 * @{
4 */
5
6 /**
7 * \file
8 * The uIP TCP/IP stack code.
9 * \author Adam Dunkels <adam@dunkels.com>
10 */
11
12
13 #include "uip.h"
14 #include "uipopt.h"
15 #include "uip_arch.h"
16 #include "reg51.h"
17
18 sbit P10 = P1^0;
19 sbit P11 = P1^1;
20 sbit P12 = P1^2;
21 extern unsigned char connect_state;
22 /*-----------------------------------------------------------------------------------*/
23 /* Variable definitions. */
24
25 /* The IP address of this host. If it is defined to be fixed (by setting UIP_FIXEDADDR to 1 in uipopt.h),
26 the address is set here. Otherwise, the address */
27 #if UIP_FIXEDADDR > 0
28 const u16_t code uip_hostaddr[2] =
29 {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
30 HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
31 const u16_t code uip_arp_draddr[2] =
32 {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
33 HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
34 const u16_t code uip_arp_netmask[2] =
35 {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
36 HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
37 #else
u16_t idata uip_hostaddr[2];
u16_t idata uip_arp_draddr[2], uip_arp_netmask[2];
#endif /* UIP_FIXEDADDR */
41
42 u8_t xdata uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
43 incoming packets. */ // 改buffer xdata -> idata
44 volatile u8_t *uip_appdata; /*应用数据 The uip_appdata pointer points to
45 application data. */
46 volatile u8_t *uip_sappdata; /*将被发送的应用数据 The uip_appdata pointer points to the
47 application data which is to be sent. */
48
49
50 volatile u16_t idata uip_len, uip_slen;
51 /* The uip_len is either 8 or 16 bits,
52 depending on the maximum packet
53 size. */
54
55 volatile u8_t idata uip_flags; /* The uip_flags variable is used for
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 2
56 communication between the TCP/IP stack
57 and the application program.被使用在通信的TCP/IP栈和应用程序之间 */
58 struct uip_conn xdata *uip_conn; /* uip_conn always points to the current
59 // connection. */ // 改buffer xdata -> idata
60
61 struct uip_conn xdata uip_conns[UIP_CONNS]; // 改buffer xdata -> idata
62 /* The uip_conns array holds all TCP
63 connections. */
64 u16_t idata uip_listenports[UIP_LISTENPORTS];
65 /* The uip_listenports list all currently
66 listning ports. */
67 #if UIP_UDP //add2
struct uip_udp_conn idata *uip_udp_conn;
struct uip_udp_conn idata uip_udp_conns[UIP_UDP_CONNS];
#endif /* UIP_UDP */
71
72
73 static u16_t idata ipid; /* Ths ipid variable is an increasing
74 number that is used for the IP ID field. */
75
76 static u8_t idata iss[4]; /* The iss variable is used for the TCP
77 initial sequence number. */
78
79 #if UIP_ACTIVE_OPEN
80 static u16_t idata lastport; /* Keeps track of the last port used for
81 a new connection. */
82 #endif /* UIP_ACTIVE_OPEN */
83
84 /* Temporary variables. */
85 volatile u8_t uip_acc32[4];
86 static u8_t idata c, opt;
87 static u16_t idata tmp16;
88
89 /* Structures and definitions. */
90 #define TCP_FIN 0x01
91 #define TCP_SYN 0x02
92 #define TCP_RST 0x04
93 #define TCP_PSH 0x08
94 #define TCP_ACK 0x10
95 #define TCP_URG 0x20
96 #define TCP_CTL 0x3f
97
98 #define ICMP_ECHO_REPLY 0
99 #define ICMP_ECHO 8
100
101 /* Macros. */
102 #define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
103 #define FBUF ((uip_tcpip_hdr *)&uip_reassbuf[0])
104 #define ICMPBUF ((uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
105 #define UDPBUF ((uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
106
107 #if UIP_STATISTICS == 1
108 struct uip_stats uip_stat; // 定义UIP的状态量
109 #define UIP_STAT(s) s
110 #else
#define UIP_STAT(s)
#endif /* UIP_STATISTICS == 1 */
113
114 #if UIP_LOGGING == 1
#include <stdio.h>
void uip_log(char *msg);
#define UIP_LOG(m) uip_log(m)
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 3
#else
119 #define UIP_LOG(m)
120 #endif /* UIP_LOGGING == 1 */
121
122
123
124 /*---------------------------------------------------------------------------------------------*/
125 void
126 uip_init(void)
127 {
128 1 for(c = 0; c < UIP_LISTENPORTS; ++c) { //UIP_LISTENPORTS=2
129 2 uip_listenports[c] = 0;
130 2 }
131 1 for(c = 0; c < UIP_CONNS; ++c) {
132 2 uip_conns[c].tcpstateflags = CLOSED;
133 2 }
134 1 #if UIP_ACTIVE_OPEN
135 1 lastport = 1024;
136 1 #endif /* UIP_ACTIVE_OPEN */
137 1
138 1 #if UIP_UDP //add2
for(c = 0; c < UIP_UDP_CONNS; ++c) {
uip_udp_conns[c].lport = 0;
}
#endif /* UIP_UDP */
143 1
144 1 /* IPv4 initialization. */
145 1 #if UIP_FIXEDADDR == 0
uip_hostaddr[0] = uip_hostaddr[1] = 0;
#endif /* UIP_FIXEDADDR */
148 1
149 1 } //初始化完成
150 /*-----------------------------------------------------------------------------------------------*/
151 #if UIP_ACTIVE_OPEN //有TCP连接
152 struct uip_conn
153 *uip_connect(u16_t *ripaddr, u16_t rport) //within 0.5 seconds after the call to uip_connect().
154 {
155 1 register struct uip_conn *conn, *cconn;
156 1
157 1 /* Find an unused local port. */
158 1 again:
159 1 ++lastport;
160 1
161 1 if(lastport >= 32000) {
162 2 lastport = 4096;
163 2 }
164 1
165 1 /* Check if this port is already in use, and if so try to find
166 1 another one. */
167 1 for(c = 0; c < UIP_CONNS; ++c) {
168 2 conn = &uip_conns[c];
169 2 if(conn->tcpstateflags != CLOSED &&
170 2 conn->lport == htons(lastport)) {
171 3 goto again;
172 3 }
173 2 } //找空闲端口
174 1
175 1
176 1 conn = -1; //找空闲连接
177 1 for(c = 0; c < UIP_CONNS; ++c) {
178 2 cconn = &uip_conns[c];
179 2 if(cconn->tcpstateflags == CLOSED) {
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 4
180 3 conn = cconn;
181 3 break;
182 3 }
183 2 if(cconn->tcpstateflags == TIME_WAIT) {
184 3 if(conn == 0 ||
185 3 cconn->timer > uip_conn->timer) {
186 4 conn = cconn;
187 4 }
188 3 }
189 2 }
190 1
191 1 if(conn == -1) { //改0-》-1
192 2 return 0;
193 2 }
194 1
195 1 conn->tcpstateflags = SYN_SENT; //状态
196 1
197 1 conn->snd_nxt[0] = iss[0]; //TCP初始化序列号
198 1 conn->snd_nxt[1] = iss[1];
199 1 conn->snd_nxt[2] = iss[2];
200 1 conn->snd_nxt[3] = iss[3];
201 1
202 1 conn->initialmss = conn->mss = UIP_TCP_MSS; // 最大段大小:(UIP_BUFSIZE - UIP_LLH_LEN - 40)
203 1
204 1 conn->len = 1; /* TCP length of the SYN is one. */
205 1 conn->nrtx = 0; //最后一次重新传输的次数
206 1 conn->timer = 1; /* Send the SYN next time around. */
207 1 conn->rto = UIP_RTO; //重发的次数=3
208 1 conn->sa = 0; //重新传输超时计算状态变量
209 1 conn->sv = 16;
210 1 conn->lport = htons(lastport); //本地主机端口
211 1 conn->rport = rport; //远程主机的端口和IP地址
212 1 conn->ripaddr[0] = ripaddr[0];
213 1 conn->ripaddr[1] = ripaddr[1];
214 1 //经过
215 1 return conn;
216 1 }
217 #endif /* UIP_ACTIVE_OPEN */
218 /*-------------------------------------------------------------------------------*/
219
220 /*-------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -