uip.c源程序

本文档详细介绍了uIP TCP/IP协议栈的源代码实现,包括IP、TCP和应用层的紧密耦合,以及如何通过uip_buf缓冲区进行数据交互。uIP库是一个小型的TCP/IP实现,适用于资源有限的设备。文章涵盖了从IP头到TCP选项的解析,以及如何处理不同TCP状态,如SYN_SENT、ESTABLISHED、LAST_ACK等。此外,还讨论了uIP初始化、连接建立、监听端口分配、数据处理和传输过程。
摘要由CSDN通过智能技术生成
 
 
 
/*
 * 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 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"
 
/*-----------------------------------------------------------------------------------*/
/* Variable definitions. */
 
u8_t 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];
                             /* 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_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;
#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)
#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;
 }
 
 
 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];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值