⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_ip.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 4 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ip/tcp_ip.c                                          *//*   Release: 2001.3                                                   *//*   Version: 2001.1                                                   *//*   Purpose: TCP/IP Main Routines                                     *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               Copyright 2001, Blunk Microsystems                    *//*                      ALL RIGHTS RESERVED                            *//*                                                                     *//*   Licensees have the non-exclusive right to use, modify, or extract *//*   this computer program for software development at a single site.  *//*   This program may be resold or disseminated in executable format   *//*   only. The source code may not be redistributed or resold.         *//*                                                                     *//***********************************************************************/#include "tcp_ipp.h"#include <string.h>#include "ip/ip.h"#include "tcp/tcp.h"/***********************************************************************//* Symbol Definitions                                                  *//***********************************************************************/#define MSG_QUE_SIZE    (MAX_NUM_SOCKETS + 10)/***********************************************************************//* Type Definitions                                                    *//***********************************************************************/typedef struct{  void (*func)(void *ptr);  void *ptr;  ui32 msg;} NetMsg;/***********************************************************************//* Global Variable Definitions                                         *//***********************************************************************/struct socket Socks[MAX_NUM_SOCKETS];static TASK Daemon;static NetMsg MsgQue[MSG_QUE_SIZE];static volatile int QueCount, TmrCount;static int DaemonBlocked;static NetMsg *QueGet;static NetMsg *QuePut;static void *MsgPtr;static Ni * volatile NiAttnHead, * volatile NiAttnTail;struct tcp_globals Net;CircLink NetSockList;StatType Stats;NetBuf *RxBuf;/***********************************************************************//* Function Prototypes                                                 *//***********************************************************************/static void tcpipd(ui32 unused);/***********************************************************************//* Constant Data Definitions                                           *//***********************************************************************/static const struct err_struct{  ui32 err_code;  char *err_msg;} err_entry[] ={#if OS_INC_ERR_STRINGS  {EADDRINUSE, "address already in use"},  {EADDRNOTAVAIL, "cannot assign requested address"},  {EAFNOSUPPORT, "address family not supported"},  {ECOLL, "two select()'s on same socket"},  {ECONNABORTED, "aborted due to unacknowledged sends"},  {ECONNREFUSED, "connection refused"},  {ECONNRESET, "connection reset by peer"},  {EDESTADDRREQ, "destination address required"},  {EDNS_FAILED, "address resolution failed"},  {EDNS_SERVER, "no DNS server or no response"},  {EHOSTUNREACH, "no route to host"},  {EINPROGRESS, "operation now in progress"},  {EISCONN, "socket already connected"},  {EMSGSIZE, "message too long"},  {ENETDOWN, "network is down"},  {ENETUNREACH, "network is unreachable"},  {ENOBUFS, "no buffers available"},  {ENOPROTOOPT, "bad protocol option"},  {ENOTCONN, "socket is not connected"},  {ENOTSOCK, "not a socket"},  {EOPNOTSUPP, "operation not supported"},  {EPFNOSUPPORT, "protocol family not supported"},  {EPROTOTYPE, "protocol wrong type for socket"},  {ESHUTDOWN, "socket is shutdown"},  {ESOCKTNOSUPPORT, "socket type not supported"},  {ETIMEDOUT, "operation timed out"},  {EWOULDBLOCK, "operation would block"},#endif  {0, NULL},};/***********************************************************************//* Local Function Definitions                                          *//***********************************************************************//***********************************************************************//*      tcpipd: TCP/IP processing task                                 *//*                                                                     *//***********************************************************************/static void tcpipd(ui32 unused){  /*-------------------------------------------------------------------*/  /* Acquire exclusive access to protocol internals.                   */  /*-------------------------------------------------------------------*/  semPend(Net.IntSem, WAIT_FOREVER);  /*-------------------------------------------------------------------*/  /* TCP/IP daemon main loop                                           */  /*-------------------------------------------------------------------*/  for (;;)  {    /*-----------------------------------------------------------------*/    /* Block if there is no work to be done.                           */    /*-----------------------------------------------------------------*/    isrMask();    if ((NiAttnHead == NULL) && (QueCount == 0) && (TmrCount == 0))    {      DaemonBlocked = TRUE;      semPost(Net.IntSem);      taskSleep(WAIT_FOREVER);      semPend(Net.IntSem, WAIT_FOREVER);    }    isrUnmask();    /*-----------------------------------------------------------------*/    /* Do the work.                                                    */    /*-----------------------------------------------------------------*/    tcpPoll();  }}/***********************************************************************//*       timer: TCP/IP timer                                           *//*                                                                     *//***********************************************************************/static int timer(ui32 period){  /*-------------------------------------------------------------------*/  /* Increment the TCP/IP tick count.                                  */  /*-------------------------------------------------------------------*/  ++TmrCount;  /*-------------------------------------------------------------------*/  /* If the TCP/IP daemon is blocked, make it ready.                   */  /*-------------------------------------------------------------------*/  if (DaemonBlocked)  {    DaemonBlocked = FALSE;    taskWake(Daemon);  }  return period;}/***********************************************************************//* Global Function Definitions                                         *//***********************************************************************//***********************************************************************//*        init: TCP initialization                                     *//*                                                                     *//***********************************************************************/void TargetTcpInit(void){  int i;  TMR ip_timer;  /*-------------------------------------------------------------------*/  /* Verify TCP structure offsets.                                     */  /*-------------------------------------------------------------------*//*lint -save -e774 -e506 */  TcpAssert(offsetof(Tcp, src_port) == 0);  TcpAssert(offsetof(Tcp, dst_port) == 2);  TcpAssert(offsetof(Tcp, seq_num) == 4);  TcpAssert(offsetof(Tcp, ack_num) == 8);  TcpAssert(offsetof(Tcp, offset) == 12);  TcpAssert(offsetof(Tcp, flags) == 13);  TcpAssert(offsetof(Tcp, window) == 14);  TcpAssert(offsetof(Tcp, cksum) == 16);  TcpAssert(offsetof(Tcp, urg_ptr) == 18);  TcpAssert(offsetof(Tcp, options) == 20);  /*-------------------------------------------------------------------*/  /* Verify UDP structure offsets.                                     */  /*-------------------------------------------------------------------*/  TcpAssert(offsetof(Udp, src_port) == 0);  TcpAssert(offsetof(Udp, dst_port) == 2);  TcpAssert(offsetof(Udp, length) == 4);  TcpAssert(offsetof(Udp, checksum) == 6);  TcpAssert(offsetof(Udp, data) == 8);  /*-------------------------------------------------------------------*/  /* Verify IP structure offsets.                                      */  /*-------------------------------------------------------------------*/  TcpAssert(offsetof(Ip, ver_len) == 0);  TcpAssert(offsetof(Ip, ip_tos) == 1);  TcpAssert(offsetof(Ip, length) == 2);  TcpAssert(offsetof(Ip, ip_id) == 4);  TcpAssert(offsetof(Ip, frag_off) == 6);  TcpAssert(offsetof(Ip, ttl) == 8);  TcpAssert(offsetof(Ip, protocol) == 9);  TcpAssert(offsetof(Ip, checksum) == 10);  TcpAssert(offsetof(Ip, src_ip) == 12);  TcpAssert(offsetof(Ip, dst_ip) == 16);  TcpAssert(offsetof(Ip, options) == 20);/*lint -restore */  /*-------------------------------------------------------------------*/  /* Initialize the different network modules.                         */  /*-------------------------------------------------------------------*/  NetBufInit();  NetTimerInit();  ArpInit();  RtInit();  IpFragInit();  UdpInit();  TcpInit();  /*-------------------------------------------------------------------*/  /* Create TCP/IP internals access semaphore and daemon.              */  /*-------------------------------------------------------------------*/  Net.IntSem = semCreate("tcpintrn", 1, OS_FIFO);  Daemon = taskCreate("tcp/ipd", STACK_2KB, 59, tcpipd, 0, 0);  /*-------------------------------------------------------------------*/  /* Create TCP/IP timer and schedule its periodic tick.               */  /*-------------------------------------------------------------------*/  ip_timer = tmrCreate("tcptimer");  i = OsTicksPerSec / 10;  tmrCallAfter(i, timer, i, ip_timer);  /*-------------------------------------------------------------------*/  /* Create DNS client access semaphore.                               */  /*-------------------------------------------------------------------*/  Net.DnsSem = semCreate("dnss", 1, OS_FIFO);  /*-------------------------------------------------------------------*/  /* Link socket structures onto free list.                            */  /*-------------------------------------------------------------------*/  NetSockList.next_fwd = NetSockList.next_bck = &NetSockList;  for (i = 0; i < MAX_NUM_SOCKETS; ++i)  {    Socks[i].link.next_fwd = NetSockList.next_fwd;    Socks[i].link.next_bck = &NetSockList;    NetSockList.next_fwd->next_bck = &Socks[i].link;    NetSockList.next_fwd = &Socks[i].link;  }  Net.SockMin = Net.SockCount = MAX_NUM_SOCKETS;  /*-------------------------------------------------------------------*/  /* Initialize local network interface.                               */  /*-------------------------------------------------------------------*/  Net.Local.name = "local";  Net.Local.mtu  = 2000;  Net.Local.ip_addr = htonl(INADDR_LOOPBACK);  Net.Local.next = NULL;  Net.Local.flags = NIF_UP;  /*-------------------------------------------------------------------*/  /* Add limited broadcast route.                                      */  /*-------------------------------------------------------------------*/  RtAdd(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, &Net.Local, RT_BRDCST);  /*-------------------------------------------------------------------*/  /* Add loopback route.                                               */  /*-------------------------------------------------------------------*/  RtAdd(0xFFFFFFFF, htonl(0xFF000000), htonl(INADDR_LOOPBACK),        &Net.Local, 0);  /*-------------------------------------------------------------------*/  /* Initialize global variables and set "initialized" flag.           */  /*-------------------------------------------------------------------*/  QuePut = QueGet = &MsgQue[0];  Net.Initialized = TRUE;  /*-------------------------------------------------------------------*/  /* Initialize DHCP client.                                           */  /*-------------------------------------------------------------------*/  DhcpInit();}/***********************************************************************//*     tcpPoll: Perform TCP/IP daemon processing                       *//*                                                                     *//***********************************************************************/void tcpPoll(void)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -