📄 uip.c
字号:
/* * Copyright (c) 2001-2002, Adam Dunkels. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Adam Dunkels. * 4. The name of the author may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This file is part of the uIP TCP/IP stack. * * $Id: uip.c,v 1.34 2002/01/15 17:54:54 adam Exp $ * *//*This is a small implementation of the IP and TCP protocols (as well assome basic ICMP stuff). The implementation couples the IP, TCP and theapplication layers very tightly. To keep the size of the compiled codedown, this code also features heavy usage of the goto statement.The principle is that we have a small buffer, called the uip_buf, in whichthe device driver puts an incoming packet. The TCP/IP stack parses theheaders in the packet, and calls upon the application. If the remotehost has sent data to the application, this data is present in the uip_bufand the application read the data from there. It is up to theapplication to put this data into a byte stream if needed. Theapplication will not be fed with data that is out of sequence.If the application whishes to send data to the peer, it should put itsdata into the uip_buf, 40 bytes from the start of the buffer. The TCP/IPstack will calculate the checksums, and fill in the necessary headerfields and finally send the packet back to the peer. */#include "uip.h"#include "uip_arch.h"char tempStr[50];/*-----------------------------------------------------------------------------------*//* Variable definitions. */u8_t uip_buf[UIP_BUFSIZE]; /* The packet buffer that contains incoming packets. */u8_t *uip_appdata; /* The uip_appdata pointer points to application data. */#if UIP_BUFSIZE >= 256u16_t uip_len; /* The uip_len is either 8 or 16 bits, depending on the maximum packet size. */#elseu8_t uip_len;#endif /* UIP_BUFSIZE >= 256 */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. */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_OPENstatic 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. */struct ipicmp_t { /* 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, icmpseqno;} ;#define ipicmphdr struct ipicmp_t#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]) uip_tcpip_hdr * BUF; ipicmphdr * ICMPBUF;#if UIP_STATISTICS == 1struct uip_stats uip_stat;#define UIP_STAT(s) s#else#define UIP_STAT(s)#endif /* UIP_STATISTICS == 1 */#if UIP_LOGGING == 1char debugStr[3000];#define UIP_LOG(m) strcat(debugStr,m)#else#define UIP_LOG(m)#endif /* UIP_LOGGING == 1 *//*-----------------------------------------------------------------------------------*/voiduip_init(void){#if UIP_LOGGING == 1 memset(debugStr, 0, sizeof(debugStr));#endif memset(uip_stat, 0, sizeof(uip_stat)); 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_OPENstruct 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; } 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 *//*-----------------------------------------------------------------------------------*/voiduip_listen(u16_t port){ for(c = 0; c < UIP_LISTENPORTS; ++c) { if(uip_listenports[c] == 0) { uip_listenports[c] = htons(port); break; } }}/*-----------------------------------------------------------------------------------*/voiduip_process(u8_t flag){ uip_appdata = &uip_buf[40 + UIP_LLH_LEN]; BUF = &uip_buf[UIP_LLH_LEN]; ICMPBUF = &uip_buf[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; UIP_LOG("CLOSED due to timeout."); } } 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; UIP_LOG("CLOSED due to MAXRTX."); /* 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -