📄 uip.c
字号:
/**
* \addtogroup uip
* @{
*/
/**
* \file
* The uIP TCP/IP stack code.
* \author Adam Dunkels <adam@dunkels.com>
*/
#include "uip.h"
#include "uipopt.h"
#include "uip_arch.h"
#include "reg51.h"
sbit P10 = P1^0;
sbit P11 = P1^1;
sbit P12 = P1^2;
extern unsigned char connect_state;
/*-----------------------------------------------------------------------------------*/
/* Variable definitions. */
/* The IP address of this host. If it is defined to be fixed (by setting UIP_FIXEDADDR to 1 in uipopt.h),
the address is set here. Otherwise, the address */
#if UIP_FIXEDADDR > 0
const u16_t code uip_hostaddr[2] =
{HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
const u16_t code uip_arp_draddr[2] =
{HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
const u16_t code uip_arp_netmask[2] =
{HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
#else
u16_t idata uip_hostaddr[2];
u16_t idata uip_arp_draddr[2], uip_arp_netmask[2];
#endif /* UIP_FIXEDADDR */
u8_t xdata uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
incoming packets. */ // 改buffer xdata -> idata
volatile u8_t *uip_appdata; /*应用数据 The uip_appdata pointer points to
application data. */
volatile u8_t *uip_sappdata; /*将被发送的应用数据 The uip_appdata pointer points to the
application data which is to be sent. */
volatile u16_t idata uip_len, uip_slen;
/* The uip_len is either 8 or 16 bits,
depending on the maximum packet
size. */
volatile u8_t idata uip_flags; /* The uip_flags variable is used for
communication between the TCP/IP stack
and the application program.被使用在通信的TCP/IP栈和应用程序之间 */
struct uip_conn xdata *uip_conn; /* uip_conn always points to the current
// connection. */ // 改buffer xdata -> idata
struct uip_conn xdata uip_conns[UIP_CONNS]; // 改buffer xdata -> idata
/* The uip_conns array holds all TCP
connections. */
u16_t idata uip_listenports[UIP_LISTENPORTS];
/* The uip_listenports list all currently
listning ports. */
#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 */
static u16_t idata ipid; /* Ths ipid variable is an increasing
number that is used for the IP ID field. */
static u8_t idata iss[4]; /* The iss variable is used for the TCP
initial sequence number. */
#if UIP_ACTIVE_OPEN
static u16_t idata lastport; /* Keeps track of the last port used for
a new connection. */
#endif /* UIP_ACTIVE_OPEN */
/* Temporary variables. */
volatile u8_t uip_acc32[4];
static u8_t idata c, opt;
static u16_t idata tmp16;
/* Structures and definitions. */
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TCP_CTL 0x3f
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO 8
/* Macros. */
#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define FBUF ((uip_tcpip_hdr *)&uip_reassbuf[0])
#define ICMPBUF ((uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define UDPBUF ((uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
#if UIP_STATISTICS == 1
struct uip_stats uip_stat; // 定义UIP的状态量
#define UIP_STAT(s) s
#else
#define UIP_STAT(s)
#endif /* UIP_STATISTICS == 1 */
#if UIP_LOGGING == 1
#include <stdio.h>
void uip_log(char *msg);
#define UIP_LOG(m) uip_log(m)
#else
#define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */
/*---------------------------------------------------------------------------------------------*/
void
uip_init(void)
{
for(c = 0; c < UIP_LISTENPORTS; ++c) { //UIP_LISTENPORTS=2
uip_listenports[c] = 0;
}
for(c = 0; c < UIP_CONNS; ++c) {
uip_conns[c].tcpstateflags = CLOSED;
}
#if UIP_ACTIVE_OPEN
lastport = 1024;
#endif /* UIP_ACTIVE_OPEN */
#if UIP_UDP //add2
for(c = 0; c < UIP_UDP_CONNS; ++c) {
uip_udp_conns[c].lport = 0;
}
#endif /* UIP_UDP */
/* IPv4 initialization. */
#if UIP_FIXEDADDR == 0
uip_hostaddr[0] = uip_hostaddr[1] = 0;
#endif /* UIP_FIXEDADDR */
} //初始化完成
/*-----------------------------------------------------------------------------------------------*/
#if UIP_ACTIVE_OPEN //有TCP连接
struct uip_conn
*uip_connect(u16_t *ripaddr, u16_t rport) //within 0.5 seconds after the call to uip_connect().
{
register struct uip_conn *conn, *cconn;
/* Find an unused local port. */
again:
++lastport;
if(lastport >= 32000) {
lastport = 4096;
}
/* Check if this port is already in use, and if so try to find
another one. */
for(c = 0; c < UIP_CONNS; ++c) {
conn = &uip_conns[c];
if(conn->tcpstateflags != CLOSED &&
conn->lport == htons(lastport)) {
goto again;
}
} //找空闲端口
conn = -1; //找空闲连接
for(c = 0; c < UIP_CONNS; ++c) {
cconn = &uip_conns[c];
if(cconn->tcpstateflags == CLOSED) {
conn = cconn;
break;
}
if(cconn->tcpstateflags == TIME_WAIT) {
if(conn == 0 ||
cconn->timer > uip_conn->timer) {
conn = cconn;
}
}
}
if(conn == -1) { //改0-》-1
return 0;
}
conn->tcpstateflags = SYN_SENT; //状态
conn->snd_nxt[0] = iss[0]; //TCP初始化序列号
conn->snd_nxt[1] = iss[1];
conn->snd_nxt[2] = iss[2];
conn->snd_nxt[3] = iss[3];
conn->initialmss = conn->mss = UIP_TCP_MSS; // 最大段大小:(UIP_BUFSIZE - UIP_LLH_LEN - 40)
conn->len = 1; /* TCP length of the SYN is one. */
conn->nrtx = 0; //最后一次重新传输的次数
conn->timer = 1; /* Send the SYN next time around. */
conn->rto = UIP_RTO; //重发的次数=3
conn->sa = 0; //重新传输超时计算状态变量
conn->sv = 16;
conn->lport = htons(lastport); //本地主机端口
conn->rport = rport; //远程主机的端口和IP地址
conn->ripaddr[0] = ripaddr[0];
conn->ripaddr[1] = ripaddr[1];
//经过
return conn;
}
#endif /* UIP_ACTIVE_OPEN */
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------
void
uip_unlisten(u16_t port) //关闭监听端口
{
for(c = 0; c < UIP_LISTENPORTS; ++c) {
if(uip_listenports[c] == port) {
uip_listenports[c] = 0;
return;
}
}
}
-----------------------------------------------------------------------------------------------*/
void
uip_listen(u16_t port) //创建新的端口
{
for(c = 0; c < UIP_LISTENPORTS; ++c) {
if(uip_listenports[c] == 0) {
uip_listenports[c] = port;
return;
}
}
}
/*----------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
static void
uip_add_rcv_nxt(u16_t n) //想要接收下个的序列号
{
uip_add32(uip_conn->rcv_nxt, n); //产生这个序列号的函数
uip_conn->rcv_nxt[0] = uip_acc32[0];
uip_conn->rcv_nxt[1] = uip_acc32[1];
uip_conn->rcv_nxt[2] = uip_acc32[2];
uip_conn->rcv_nxt[3] = uip_acc32[3];
}
/*----------------------------------------------------------------------------------------------------*/
void
uip_process(u8_t flag)
{
struct uip_conn xdata *uip_connr = uip_conn; //目前的连接
uip_appdata = &uip_buf[40 + UIP_LLH_LEN]; // uip_appdata首地址 //add5
/* Check if we were invoked because of the perodic timer fireing.未接到数据*/
if(flag == UIP_TIMER)
{
/* Increase the initial sequence number. */
if(++iss[3] == 0)
{ if(++iss[2] == 0)
{ if(++iss[1] == 0)
{ ++iss[0];
}
}
}
uip_len = 0; //无数据
if(uip_connr->tcpstateflags == TIME_WAIT || //判断uip_connr->tcpstateflags
uip_connr->tcpstateflags == FIN_WAIT_2)
{ ++(uip_connr->timer);
if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT)
{ uip_connr->tcpstateflags = CLOSED; //超时,中断连接
}
}
else if(uip_connr->tcpstateflags != CLOSED)
//判断uip_connr->tcpstateflags
/* If the connection has outstanding data, we increase the
connection's timer and see if it has reached the RTO value
in which case we retransmit. */
{ if(uip_outstanding(uip_connr)) //uip_outstanding(conn)=((conn)->len)
{
if((uip_connr->timer == 3)||(uip_connr->timer == 1))
{Timer0on();} //add6
else
if(uip_connr->timer-- == 0)
{
if(uip_connr->nrtx == UIP_MAXRTX || //最后一次重传的次数
((uip_connr->tcpstateflags == SYN_SENT ||
uip_connr->tcpstateflags == SYN_RCVD) &&
uip_connr->nrtx == UIP_MAXSYNRTX))
{
uip_connr->tcpstateflags = CLOSED; //关闭的关键 add5
/* We call UIP_APPCALL() with uip_flags set to
UIP_TIMEDOUT to inform the application that the
connection has timed out. */
uip_flags = UIP_TIMEDOUT;
UIP_APPCALL();
//不经过此地add8
/* We also send a reset packet to the remote host. */ //发送一个重来的包给远程主机
BUF->flags = TCP_RST | TCP_ACK; //TCP_ACK=0x10;TCP_RST=0x04
goto tcp_send_nodata;
}
/* Exponential backoff. */
uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4? //UIP_RT0和timer的关系?
4:
uip_connr->nrtx);
if(uip_connr->timer!=0)
{Timer0on();} //add6
++(uip_connr->nrtx);
/* Ok, so we need to retransmit. We do this differently
depending on which state we are in. In ESTABLISHED, we
call upon the application so that it may prepare the
data for the retransmit. In SYN_RCVD, we resend the
SYNACK that we sent earlier and in LAST_ACK we have to
retransmit our FINACK. */
UIP_STAT(++uip_stat.tcp.rexmit); //UIP_STAT(s)=s
switch(uip_connr->tcpstateflags & TS_MASK)
{ //TS_TASK=0x0f;
case SYN_RCVD: //add5 2次
/* In the SYN_RCVD state, we should retransmit our
SYNACK. */
goto tcp_send_synack;
#if UIP_ACTIVE_OPEN
case SYN_SENT:
/* In the SYN_SENT state, we retransmit out SYN. */
BUF->flags = 0;
goto tcp_send_syn;
#endif /* UIP_ACTIVE_OPEN */
case ESTABLISHED:
/* In the ESTABLISHED state, we call upon the application
to do the actual retransmit after which we jump into
the code for sending out the packet (the apprexmit
label). */
uip_len = 0;
uip_slen = 0;
uip_flags = UIP_REXMIT;
UIP_APPCALL();
goto apprexmit;
case FIN_WAIT_1:
case CLOSING:
case LAST_ACK: //发送完经过
/* In all these states we should retransmit a FINACK. */
goto tcp_send_finack;
}
}
}
else if((uip_connr->tcpstateflags & TS_MASK) == ESTABLISHED)
{
//判断uip_connr->tcpstateflags
/* If there was no need for a retransmission, we poll the
application for new data. */
uip_len = 0;
uip_slen = 0;
uip_flags = UIP_POLL;
UIP_APPCALL();
goto appsend;
}
}
goto drop; //drop表示结束
}
/* This is where the input processing starts. 接收包处理******************************************************/
UIP_STAT(++uip_stat.ip.recv);
/* Start of IPv4 input header processing code. */
/* Check validity of the IP header. */
if(BUF->vhl != 0x45) { /* IP version and header length. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.vhlerr);
UIP_LOG("ip: invalid version or header length.");
goto drop;
}
/* Check the size of the packet. If the size reported to us in
uip_len doesn't match the size reported in the IP header, there
has been a transmission error and we drop the packet. */
if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */
//uip_len里面的长度是整个的长度减去以太网包的长度
uip_len = (uip_len & 0xff) | (BUF->len[0] << 8); //重新把包的大小给uip_len
}
if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */
uip_len = (uip_len & 0xff00) | BUF->len[1]; //重新把包的大小给uip_len
}
/* Check the fragment flag. */
if((BUF->ipoffset[0] & 0x3f) != 0 ||
BUF->ipoffset[1] != 0) {
#if UIP_REASSEMBLY //未用到
uip_len = uip_reass();
if(uip_len == 0) {
goto drop;
}
#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -