📄 uip.c
字号:
#include "../CPU/CPU.h"
#include "../CFG/CFG.h"
#include "../UART/UART.h"
#include "uip.h"
#include "uipopt.h"
#include "uip_arch.h"
/*-----------------------------------------------------------------------------------*/
/* 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 uip_hostaddr[2];
u16_t uip_arp_draddr[2], uip_arp_netmask[2];
#endif /* UIP_FIXEDADDR */
u8_t uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
incoming packets. */
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. */
#if UIP_URGDATA > 0
volatile u8_t *uip_urgdata; /* The uip_urgdata pointer points to
urgent data (out-of-band data), if
present. */
volatile u8_t uip_urglen, uip_surglen;
#endif /* UIP_URGDATA > 0 */
volatile u16_t uip_len, uip_slen;
/* The uip_len is either 8 or 16 bits,
depending on the maximum packet
size. */
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];
/* The uip_conns array holds all TCP
connections. */
u16_t uip_listenports[UIP_LISTENPORTS];
/* The uip_listenports list all currently
listning ports. */
#if UIP_UDP
struct uip_udp_conn *uip_udp_conn;
struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
#endif /* UIP_UDP */
static 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. */
volatile u8_t uip_acc32[4];
static u8_t c, opt;
static u16_t 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;
#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[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
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
struct uip_conn *
uip_connect(u16_t *ripaddr, u16_t rport)
{
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 = 0;
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 == 0) {
return 0;
}
conn->tcpstateflags = SYN_SENT;
conn->snd_nxt[0] = iss[0];
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;
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;
conn->sa = 0;
conn->sv = 16;
conn->lport = htons(lastport);
conn->rport = rport;
conn->ripaddr[0] = ripaddr[0];
conn->ripaddr[1] = ripaddr[1];
return conn;
}
#endif /* UIP_ACTIVE_OPEN */
/*-----------------------------------------------------------------------------------*/
#if UIP_UDP
struct uip_udp_conn *
uip_udp_new(u16_t *ripaddr, u16_t rport)
{
register struct uip_udp_conn *conn;
/* Find an unused local port. */
again:
++lastport;
if(lastport >= 32000) {
lastport = 4096;
}
for(c = 0; c < UIP_UDP_CONNS; ++c) {
if(uip_udp_conns[c].lport == lastport) {
goto again;
}
}
conn = 0;
for(c = 0; c < UIP_UDP_CONNS; ++c) {
if(uip_udp_conns[c].lport == 0) {
conn = &uip_udp_conns[c];
break;
}
}
if(conn == 0) {
return 0;
}
conn->lport = HTONS(lastport);
conn->rport = HTONS(rport);
conn->ripaddr[0] = ripaddr[0];
conn->ripaddr[1] = ripaddr[1];
return conn;
}
#endif /* UIP_UDP */
/*-----------------------------------------------------------------------------------*/
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;
}
}
}
/*-----------------------------------------------------------------------------------*/
/* XXX: IP fragment reassembly: not well-tested. */
#if UIP_REASSEMBLY
#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
static const u8_t code bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
0x0f, 0x07, 0x03, 0x01};
static u16_t uip_reasslen;
static u8_t uip_reassflags;
#define UIP_REASS_FLAG_LASTFRAG 0x01
static u8_t uip_reasstmr;
#define IP_HLEN 20
#define IP_MF 0x20
static u8_t
uip_reass(void)
{
u16_t offset, len;
u16_t i;
/* If ip_reasstmr is zero, no packet is present in the buffer, so we
write the IP header of the fragment into the reassembly
buffer. The timer is updated with the maximum age. */
if(uip_reasstmr == 0) {
memcpy(uip_reassbuf, &BUF->vhl, IP_HLEN);
uip_reasstmr = UIP_REASS_MAXAGE;
uip_reassflags = 0;
/* Clear the bitmap. */
memset(uip_reassbitmap, sizeof(uip_reassbitmap), 0);
}
/* Check if the incoming fragment matches the one currently present
in the reasembly buffer. If so, we proceed with copying the
fragment into the buffer. */
if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
BUF->destipaddr[0] == FBUF->destipaddr[0] &&
BUF->destipaddr[1] == FBUF->destipaddr[1] &&
BUF->ipid[0] == FBUF->ipid[0] &&
BUF->ipid[1] == FBUF->ipid[1]) {
len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
/* If the offset or the offset + fragment length overflows the
reassembly buffer, we discard the entire packet. */
if(offset > UIP_REASS_BUFSIZE ||
offset + len > UIP_REASS_BUFSIZE) {
uip_reasstmr = 0;
goto nullreturn;
}
/* Copy the fragment into the reassembly buffer, at the right
offset. */
memcpy(&uip_reassbuf[IP_HLEN + offset],
(char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
len);
/* Update the bitmap. */
if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
/* If the two endpoints are in the same byte, we only update
that byte. */
uip_reassbitmap[offset / (8 * 8)] |=
bitmap_bits[(offset / 8 ) & 7] &
~bitmap_bits[((offset + len) / 8 ) & 7];
} else {
/* If the two endpoints are in different bytes, we update the
bytes in the endpoints and fill the stuff inbetween with
0xff. */
uip_reassbitmap[offset / (8 * 8)] |=
bitmap_bits[(offset / 8 ) & 7];
for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
uip_reassbitmap[i] = 0xff;
}
uip_reassbitmap[(offset + len) / (8 * 8)] |=
~bitmap_bits[((offset + len) / 8 ) & 7];
}
/* If this fragment has the More Fragments flag set to zero, we
know that this is the last fragment, so we can calculate the
size of the entire packet. We also set the
IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
the final fragment. */
if((BUF->ipoffset[0] & IP_MF) == 0) {
uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
uip_reasslen = offset + len;
}
/* Finally, we check if we have a full packet in the buffer. We do
this by checking if we have the last fragment and if all bits
in the bitmap are set. */
if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
/* Check all bytes up to and including all but the last byte in
the bitmap. */
for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
if(uip_reassbitmap[i] != 0xff) {
goto nullreturn;
}
}
/* Check the last byte in the bitmap. It should contain just the
right amount of bits. */
if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
(u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
goto nullreturn;
}
/* If we have come this far, we have a full packet in the
buffer, so we allocate a pbuf and copy the packet into it. We
also reset the timer. */
uip_reasstmr = 0;
memcpy(BUF, FBUF, uip_reasslen);
/* Pretend to be a "normal" (i.e., not fragmented) IP packet
from now on. */
BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
BUF->len[0] = uip_reasslen >> 8;
BUF->len[1] = uip_reasslen & 0xff;
BUF->ipchksum = 0;
BUF->ipchksum = ~(uip_ipchksum());
return uip_reasslen;
}
}
nullreturn:
return 0;
}
#endif /* UIP_REASSEMBL */
/*-----------------------------------------------------------------------------------*/
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 *uip_connr = uip_conn;
uip_sappdata=uip_appdata=&uip_buf[40+UIP_LLH_LEN];
/* Check if we were invoked because of the perodic timer fireing. */
if(flag == UIP_TIMER) {
#if UIP_REASSEMBLY
if(uip_reasstmr != 0) {
--uip_reasstmr;
}
#endif /* UIP_REASSEMBLY */
/* 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 == FIN_WAIT_2) {
++(uip_connr->timer);
if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
uip_connr->tcpstateflags = CLOSED;
}
} else if(uip_connr->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_outstanding(uip_connr)) {
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;
/* 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_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
4:
uip_connr->nrtx);
++(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);
switch(uip_connr->tcpstateflags & TS_MASK) {
case SYN_RCVD:
/* In the SYN_RCVD state, we should retransmit our
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -