📄 uip.c
字号:
/*
This is a small implementation of the IP and TCP protocols (as well as
some basic ICMP stuff). The implementation couples the IP, TCP and the
application layers very tightly. To keep the size of the compiled code
down, this code also features heavy usage of the goto statement.
The principle is that we have a small buffer, called the uip_buf, in which
the device driver puts an incoming packet. The TCP/IP stack parses the
headers in the packet, and calls upon the application. If the remote
host has sent data to the application, this data is present in the uip_buf
and the application read the data from there. It is up to the
application to put this data into a byte stream if needed. The
application will not be fed with data that is out of sequence.
If the application whishes to send data to the peer, it should put its
data into the uip_buf, 40 bytes from the start of the buffer. The TCP/IP
stack will calculate the checksums, and fill in the necessary header
fields and finally send the packet back to the peer. */
#include "uip.h"
#include "uipopt.h"
#include "uip_arch.h"
#include <LCD\LCD.h>
/*-----------------------------------------------------------------------------------*/
/* Variable definitions. */
u8_t idata uip_buf[UIP_BUFSIZE] ; /* The packet buffer that contains
incoming packets. */
volatile u8_t *uip_appdata; /* The uip_appdata pointer points to
application data. */
#if UIP_BUFSIZE > 255
volatile u16_t uip_len; /* The uip_len is either 8 or 16 bits,
depending on the maximum packet
size. */
#else
volatile u8_t uip_len;
#endif /* UIP_BUFSIZE > 255 */
volatile u8_t uip_flags; /* The uip_flags variable is used for
communication between the TCP/IP stack
and the application program. */
struct uip_conn *uip_conn; /* uip_conn always points to the current
connection. */
//struct uip_conn uip_conns[UIP_CONNS];
struct uip_conn uip_conns[UIP_CONNS] _at_ 0x0001;
/* The uip_conns array holds all TCP
connections. */
//u16_t uip_listenports[UIP_LISTENPORTS];
u16_t idata uip_listenports[UIP_LISTENPORTS];
/* The uip_listenports list all currently
listning ports. */
//static u16_t ipid; /* Ths ipid variable is an increasing
static idata u16_t ipid; /* Ths ipid variable is an increasing
number that is used for the IP ID
field. */
static u8_t iss[4]; /* The iss variable is used for the TCP
initial sequence number. */
#if UIP_ACTIVE_OPEN
static u16_t lastport; /* Keeps track of the last port used for
a new connection. */
#endif /* UIP_ACTIVE_OPEN */
/* Temporary variables. */
static u8_t c, opt;
static u16_t tmpport;
/* Structures and definitions. */
typedef struct {
/* IP header. */
u8_t vhl,
tos,
len[2],
ipid[2],
ipoffset[2],
ttl,
proto;
u16_t ipchksum;
u16_t srcipaddr[2],
destipaddr[2];
/* ICMP (echo) header. */
u8_t type, icode;
u16_t icmpchksum;
u16_t id, seqno;
} ipicmphdr;
#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 IP_PROTO_ICMP 1
#define IP_PROTO_TCP 6
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO 8
/* Macros. */
#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define ICMPBUF ((ipicmphdr *)&uip_buf[UIP_LLH_LEN])
#if UIP_STATISTICS == 1
//struct uip_stats uip_stat; LEON;
struct uip_stats uip_stat={
{0,0,0,0,0,0,0,0,0},
{0,0,0,0},
{0,0,0,0,0,0,0,0,0},
};
#define UIP_STAT(s) s
#else
#define UIP_STAT(s)
#endif /* UIP_STATISTICS == 1 */
#if UIP_LOGGING == 1
#define UIP_LOG(m) printf("%s\n", m)
#else
#define UIP_LOG(m) //LCD_log(m) //leon;
#endif /* UIP_LOGGING == 1 */
/*-----------------------------------------------------------------------------------*/
void
uip_init(void)
{
for(c = 0; c < UIP_LISTENPORTS; ++c) {
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_ACTIVE_OPEN
struct uip_conn *
uip_connect(u16_t *ripaddr, u16_t rport)
{
struct uip_conn *conn;
/* Find an unused local port. */
again:
++lastport;
if(lastport >= 32000) {
lastport = 4096;
}
for(c = 0; c < UIP_CONNS; ++c) {
if(uip_conns[c].tcpstateflags != CLOSED &&
uip_conns[c].lport == lastport)
goto again;
}
lastport=APPport; //add by zhzhl to enforce the port number;
for(c = 0; c < UIP_CONNS; ++c) {
if(uip_conns[c].tcpstateflags == CLOSED)
goto found_unused;
}
for(c = 0; c < UIP_CONNS; ++c) {
if(uip_conns[c].tcpstateflags == TIME_WAIT)
goto found_unused;
}
return (void *)0;
found_unused:
conn = &uip_conns[c];
conn->tcpstateflags = SYN_SENT | UIP_OUTSTANDING;
conn->snd_nxt[0] = conn->ack_nxt[0] = iss[0];
conn->snd_nxt[1] = conn->ack_nxt[1] = iss[1];
conn->snd_nxt[2] = conn->ack_nxt[2] = iss[2];
conn->snd_nxt[3] = conn->ack_nxt[3] = iss[3];
if(++conn->ack_nxt[3] == 0) {
if(++conn->ack_nxt[2] == 0) {
if(++conn->ack_nxt[1] == 0) {
++conn->ack_nxt[0];
}
}
}
conn->nrtx = 0;
conn->timer = 1; /* Send the SYN next time around. */
conn->lport = htons(lastport);
conn->rport = htons(rport);
conn->ripaddr[0] = ripaddr[0];
conn->ripaddr[1] = ripaddr[1];
return conn;
}
#endif /* UIP_ACTIVE_OPEN */
/*-----------------------------------------------------------------------------------*/
void
uip_listen(u16_t port)
{
for(c = 0; c < UIP_LISTENPORTS; ++c) {
if(uip_listenports[c] == 0) {
uip_listenports[c] = htons(port);
break;
}
}
}
/*-----------------------------------------------------------------------------------*/
void
uip_process(u8_t flag)
{
uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
/* 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_conn->tcpstateflags == TIME_WAIT ||
uip_conn->tcpstateflags == FIN_WAIT_2) {
++(uip_conn->timer);
if(uip_conn->timer == UIP_TIME_WAIT_TIMEOUT) {
uip_conn->tcpstateflags = CLOSED;
}
} else if(uip_conn->tcpstateflags != CLOSED) {
/* 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_conn->tcpstateflags & UIP_OUTSTANDING) {
--(uip_conn->timer);
if(uip_conn->timer == 0) {
if(uip_conn->nrtx == UIP_MAXRTX) {
uip_conn->tcpstateflags = CLOSED;
/* 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();
/* We also send a reset packet to the remote host. */
BUF->flags = TCP_RST | TCP_ACK;
goto tcp_send_nodata;
}
/* Exponential backoff. */
uip_conn->timer = UIP_RTO << (uip_conn->nrtx > 4? 4: uip_conn->nrtx);
++(uip_conn->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);
switch(uip_conn->tcpstateflags & TS_MASK) {
case SYN_RCVD:
/* 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_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_conn->tcpstateflags & TS_MASK) == ESTABLISHED) {
/* If there was no need for a retransmission, we poll the
application for new data. */
uip_len = 0;
uip_flags = UIP_POLL;
UIP_APPCALL();
goto appsend;
}
}
goto drop;
}
/* This is where the input processing starts. */
UIP_STAT(++uip_stat.ip.recv);
/* 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 UIP_BUFSIZE > 255
if(BUF->len[0] != (uip_len >> 8)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.hblenerr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -