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

📄 tcp_bufs.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ip/tcp_bufs.c                                        *//*   Release: 2001.3                                                   *//*   Version: 2001.0                                                   *//*   Purpose: TCP/IP Buffer Manager                                    *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               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.         *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*   Outbound buffers may contain data in up to three sections. The    *//*   first section starts with the IP header. For all types except TCP *//*   the data follows the headers. TCP uses app_len and app_data to    *//*   indicate the start of application data in the socket's send       *//*   buffer. If the segment data wraps around to the beginning of the  *//*   send buffer, app_len2 and app_data2 mark the third section.       *//*                                                                     *//*   Inbound buffers contain data in up to two sections. The first     *//*   section starts with the IP header and is marked by ip_pkt. VJHC   *//*   decompression places the IP/TCP headers, aligned, in the buffer   *//*   after the application data and records the length and start of    *//*   application data using app_len and app_data.                      *//*                                                                     *//*   If not aligned by VJHC or the network driver, the IP, UDP, TCP,   *//*   ICMP headers are aligned within TargetTCP. UDP and TCP record the *//*   length and start of application data using app_len and app_data   *//*   before copying their headers. For inbound buffers, app_len2 will  *//*   always be zero.                                                   *//*                                                                     *//***********************************************************************/#include "tcp_ipp.h"/***********************************************************************//* Symbol Definitions                                                  *//***********************************************************************/#define TYPE_IN     0xEEEDDD11/***********************************************************************//* Type Definitions                                                    *//***********************************************************************/typedef struct{  NetBuf hdr;  ui8 data[TCP_SML_SIZE - 1]; /*lint -esym(754,data) */} BufTypeSml;typedef struct{  NetBuf hdr;  ui8  data[TCP_MED_SIZE - 1];} BufTypeMed;typedef struct{  NetBuf hdr;  ui8  data[TCP_BIG_SIZE - 1];} BufTypeBig;/***********************************************************************//* Global Variable Definitions                                         *//***********************************************************************/static int LowMarkSml, LowMarkMed, LowMarkBig;static int NumFreeSml, NumFreeMed, NumFreeBig;static BufTypeSml BufSml[TCP_SML_BUFS];static BufTypeMed BufMed[TCP_MED_BUFS];static BufTypeBig BufBig[TCP_BIG_BUFS];static NetBuf *SmlBufs[TCP_SML_BUFS];static NetBuf *MedBufs[TCP_MED_BUFS];static NetBuf *BigBufs[TCP_BIG_BUFS];static int SmlHead, MedHead, BigHead;static int SmlTail, MedTail, BigTail;/***********************************************************************//* Global Function Definitions                                         *//***********************************************************************//***********************************************************************//*  NetBufInit: Initialize TCP/IP buffer manager                       *//*                                                                     *//***********************************************************************/void NetBufInit(void){  int i;  /*-------------------------------------------------------------------*/  /* Record buffer allotment.                                          */  /*-------------------------------------------------------------------*/  LowMarkSml = NumFreeSml = TCP_SML_BUFS;  LowMarkMed = NumFreeMed = TCP_MED_BUFS;  LowMarkBig = NumFreeBig = TCP_BIG_BUFS;  /*-------------------------------------------------------------------*/  /* Initialize buffers and buffer pointers.                           */  /*-------------------------------------------------------------------*/  for (i = 0; i < TCP_SML_BUFS; ++i)  {    SmlBufs[i] = (NetBuf *)&BufSml[i];    SmlBufs[i]->type = TYPE_IN;    TcpAssert(((ui32)&SmlBufs[i]->data & 3) == 0);  }  for (i = 0; i < TCP_MED_BUFS; ++i)  {    MedBufs[i] = (NetBuf *)&BufMed[i];    MedBufs[i]->type = TYPE_IN;    TcpAssert(((ui32)&MedBufs[i]->data & 3) == 0);  }  for (i = 0; i < TCP_BIG_BUFS; ++i)  {    BigBufs[i] = (NetBuf *)&BufBig[i];    BigBufs[i]->type = TYPE_IN;    TcpAssert(((ui32)&BigBufs[i]->data & 3) == 0);  }}/***********************************************************************//*   tcpGetBuf: Allocate a TCP/IP buffer                               *//*                                                                     *//*       Input: length = length of requested buffer                    *//*                                                                     *//***********************************************************************/void *tcpGetBuf(int length){  NetBuf *buf;  /*-------------------------------------------------------------------*/  /* Check if small buffer is acceptable (mostly for TCP ACKs).        */  /*-------------------------------------------------------------------*/  if ((length <= TCP_SML_SIZE) && NumFreeSml)  {    buf = SmlBufs[SmlHead];    if (++SmlHead == TCP_SML_BUFS)      SmlHead = 0;    if (--NumFreeSml < LowMarkSml)      LowMarkSml = NumFreeSml;  }  /*-------------------------------------------------------------------*/  /* Check if medium buffer is acceptable (mostly for TCP data).       */  /*-------------------------------------------------------------------*/  else if (length <= TCP_MED_SIZE)  {    if (NumFreeMed)    {      buf = MedBufs[MedHead];      if (++MedHead == TCP_MED_BUFS)        MedHead = 0;      if (--NumFreeMed < LowMarkMed)        LowMarkMed = NumFreeMed;    }    else    {      ++Stats.GetBufFails;      return NULL;    }  }  /*-------------------------------------------------------------------*/  /* Check if big buffer is acceptable (mostly for UDP and recv bufs). */  /*-------------------------------------------------------------------*/  else if ((length <= TCP_BIG_SIZE) && NumFreeBig)  {    buf = BigBufs[BigHead];    if (++BigHead == TCP_BIG_BUFS)      BigHead = 0;    if (--NumFreeBig < LowMarkBig)      LowMarkBig = NumFreeBig;  }  /*-------------------------------------------------------------------*/  /* No buffer available, update failure count.                        */  /*-------------------------------------------------------------------*/  else  {    ++Stats.GetBufFails;    return NULL;  }  /*-------------------------------------------------------------------*/  /* If buffer contains data, flush it now.                            */  /*-------------------------------------------------------------------*/  if (buf->flush)    TcpFlush(buf);  /*-------------------------------------------------------------------*/  /* Initialize buffer and return buffer pointer.                      */  /*-------------------------------------------------------------------*/  TcpAssert(buf->type == TYPE_IN);  buf->type = ~TYPE_IN;  buf->ni = &Net.Local;  NetRstBuf(buf);  return buf;}/***********************************************************************//*   NetRstBuf: Reset previously allocated TCP/IP buffer               *//*                                                                     *//*       Input: buf = pointer to buffer being reset                    *//*                                                                     *//*        Note: Resets length to zero and pointers to original values  *//*                                                                     *//***********************************************************************/

⌨️ 快捷键说明

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