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

📄 ppp.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 3 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ip/ppp/ppp.c                                         *//*   Release: 2001.3                                                   *//*   Version: 2001.0                                                   *//*   Purpose: Send and receive PPP datagrams                           *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               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 "pppp.h"#if MAX_PPP_INTF/***********************************************************************//* Symbol Definitions                                                  *//***********************************************************************/#define HDLC_ALL_ADDR   0xFF    /* PPP HDLC address value */#define HDLC_UI         0x03    /* PPP HDLC control value *//***********************************************************************//* Global Data Declarations                                            *//***********************************************************************/PPP Ppp;        /* global PPP channel identifier */NetBuf *pppRcvBuf;  /* points to current receive buffer *//***********************************************************************//* Function Definitions                                                *//***********************************************************************//***********************************************************************//*       put16: Write a short to a byte array in network byte order    *//*                                                                     *//*      Inputs: dst = pointer to first position in byte array          *//*              word = 16-bit word to write                            *//*                                                                     *//*     Returns: Pointer to first entry following the four used entries *//*                                                                     *//***********************************************************************/void *put16(ui8 *dst, ui16 word){  *dst++ = (ui8)(word >> 8);  *dst++ = (ui8)word;  return dst;}/***********************************************************************//*       put32: Write a long to a byte array in network byte order     *//*                                                                     *//*      Inputs: dst = pointer to first position in byte array          *//*              lword = 32-bit word to write                           *//*                                                                     *//*     Returns: Pointer to first entry following the four used entries *//*                                                                     *//***********************************************************************/void *put32(ui8 *dst, ui32 lword){  *dst++ = (ui8)(lword >> 24);  *dst++ = (ui8)(lword >> 16);  *dst++ = (ui8)(lword >> 8);  *dst++ = (ui8)lword;  return dst;}/***********************************************************************//*    pullchar: Read 1 byte from packet                                *//*                                                                     *//***********************************************************************/int pullchar(NetBuf *buf){  /*-------------------------------------------------------------------*/  /* Ensure packet contains atleast 1 byte of data.                    */  /*-------------------------------------------------------------------*/  if (buf->length < 1)    return -1;  /*-------------------------------------------------------------------*/  /* Update buffer indices and return byte.                            */  /*-------------------------------------------------------------------*/  --buf->length;  return *buf->ip_pkt++;}/***********************************************************************//*      pull16: Read 16-bits from packet in network byte order         *//*                                                                     *//***********************************************************************/int pull16(NetBuf *buf){  ui8 *cp = buf->ip_pkt;  ui16 word;  /*-------------------------------------------------------------------*/  /* Ensure packet contains atleast 16-bits of data.                   */  /*-------------------------------------------------------------------*/  if (buf->length < 2)    return -1;  /*-------------------------------------------------------------------*/  /* Build 16-bit word from packet in network byte order.              */  /*-------------------------------------------------------------------*/  word = *cp++ << 8;  word |= *cp++;  /*-------------------------------------------------------------------*/  /* Update buffer indices and return word.                            */  /*-------------------------------------------------------------------*/  buf->ip_pkt = cp;  buf->length -= 2;  return word;}/***********************************************************************//*      pull32: Read 32-bits from packet in network byte order         *//*                                                                     *//***********************************************************************/int pull32(NetBuf *buf){  ui8 *cp = buf->ip_pkt;  ui32 lword;  int i;  /*-------------------------------------------------------------------*/  /* Ensure packet contains atleast 32-bits of data.                   */  /*-------------------------------------------------------------------*/  if (buf->length < 4)    return -1;  /*-------------------------------------------------------------------*/  /* Build 32-bit word from packet in network byte order.              */  /*-------------------------------------------------------------------*/  lword = *cp++;  for (i = 0; i < 3; ++i)  {    lword <<= 8;    lword |= *cp++;  }  /*-------------------------------------------------------------------*/  /* Update buffer indices and return word.                            */  /*-------------------------------------------------------------------*/  buf->ip_pkt = cp;  buf->length -= 4;  return lword;}#if PPP_TRACE/***********************************************************************//*     pppLogn: PPP log output preceded by channel name                *//*                                                                     *//***********************************************************************/void pppLogn(char *format, ...){  va_list ap;  printf("%s ", Ppp->public.ni.name);  va_start(ap, format);  vprintf(format, ap);  va_end(ap);  putchar('\n');}/***********************************************************************//*      pppLog: PPP log output                                         *//*                                                                     *//***********************************************************************/void pppLog(char *format, ...){  va_list ap;  if (Ppp->public.flags & PPPF_TRACE)  {    va_start(ap, format);    vprintf(format, ap);    va_end(ap);    putchar('\n');  }}/***********************************************************************//*     ppp_log: Input/output log routine                               *//*                                                                     *//***********************************************************************/static void ppp_log(char *comment){  pppLogn("PPP %s", comment);}#endif/***********************************************************************//*    ppp_send: Send IP datagram via PPP                               *//*                                                                     *//*      Inputs: buf = pointer to outbound buffer                       *//*              voidp = pointer to PPP control block                   *//*                                                                     *//***********************************************************************/void ppp_send(NetBuf *buf, void *voidp){  IpcpCB *ipcp = &Ppp->ipcp;  uint protocol = PPP_IP_PROTO;  /*-------------------------------------------------------------------*/  /* Set global PPP channel identifier before calling other routines.  */  /*-------------------------------------------------------------------*/  Ppp = voidp;  /*-------------------------------------------------------------------*/  /* Ensure state machine is open.                                     */  /*-------------------------------------------------------------------*/  if (Ppp->fsm[kIPCP].state != fsmOPENED)  {#if PPP_TRACE    ppp_log("not open for IP traffic");#endif    ++Ppp->txError;    tcpRetBuf(&buf);    return;  }  /*-------------------------------------------------------------------*/  /* If enabled, attempt TCP header compression.                       */  /*-------------------------------------------------------------------*/  if (ipcp->remote.options & IPCP_N_COMPRESS)    protocol = vjhc_compress(buf, &Ppp->comp, ipcp->remote.comp_slot);  /*-------------------------------------------------------------------*/  /* Prepend PPP header and queue for serial output.                   */  /*-------------------------------------------------------------------*/  PppOutput(protocol, buf);}/***********************************************************************//*   PppOutput: Prepend PPP header and queue buffer for serial output  *//*                                                                     *//*      Inputs: protocol = PPP protocol type field                     *//*              buf = pointer to buffer containing the packet to send  *//*                                                                     *//***********************************************************************/void PppOutput(uint protocol, NetBuf *buf){  LcpCB *lcp = &Ppp->lcp;  int is_lcp, full_ac, full_p, len;  ui8 *cp;  /*-------------------------------------------------------------------*/  /* Ensure line is up.                                                */  /*-------------------------------------------------------------------*/  if (Ppp->phase == pppDEAD)  {#if PPP_TRACE    ppp_log("line not up");#endif    ++Ppp->txError;    tcpRetBuf(&buf);    return;  }  /*-------------------------------------------------------------------*/  /* Update sum of transmitted bytes.                                  */  /*-------------------------------------------------------------------*/  Ppp->txOctetCount += buf->length + 2; /* count FCS bytes */  /*-------------------------------------------------------------------*/  /* Flag whether (1) protocol is LCP, (2) protocol may be compressed, */  /* and (3) address and control fields may be omitted.                */  /*-------------------------------------------------------------------*/  is_lcp = (protocol == PPP_LCP_PROTO);  full_p = is_lcp || !(lcp->remote.options & LCP_N_PFC) ||              protocol >= 0x00FF;  full_ac = is_lcp || !(lcp->remote.options & LCP_N_ACFC);  /*-------------------------------------------------------------------*/  /* Determine PPP header length.                                      */  /*-------------------------------------------------------------------*/  len = 1;  if (full_p)    ++len;  if (full_ac)    len += 2;  /*-------------------------------------------------------------------*/  /* Update buffer length and start of data.                           */  /*-------------------------------------------------------------------*/  buf->length += len;  buf->ip_pkt -= len;  /*-------------------------------------------------------------------*/  /* Prepend PPP header to IP packet.                                  */  /*-------------------------------------------------------------------*/  cp = buf->ip_pkt;  if (full_ac)  {    *cp++ = HDLC_ALL_ADDR;    *cp++ = HDLC_UI;  }  if (full_p)    *cp++ = (ui8)(protocol >> 8);  *cp++ = (ui8)protocol;  /*-------------------------------------------------------------------*/

⌨️ 快捷键说明

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