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

📄 pppfsm.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 3 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ip/ppp/pppfsm.c                                      *//*   Release: 2001.3                                                   *//*   Version: 2000.1                                                   *//*   Purpose: PPP State Machine                                        *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               Copyright 2000, 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/***********************************************************************//* Constant Data Definitions                                           *//***********************************************************************/static char * const fsmStates[] ={  "Initial",  "Starting",  "Closed",  "Stopped",  "Closing",  "Stopping",  "ReqSent",  "AckRcvd",  "AckSent",  "Opened",};char * const fsmCodes[] ={  NULL,  "ConfReq",  "ConfAck",  "ConfNak",  "ConfRej",  "TermReq",  "TermAck",  "CodeRej",  "ProtRej",  "EchoReq",  "EchoRply",  "DiscReq",};/***********************************************************************//* Local Function Definitions                                          *//***********************************************************************//***********************************************************************//*     wr_conf: Convert header to network form and prepend             *//*                                                                     *//***********************************************************************/static void wr_conf(const ConfigHdr *cnf, NetBuf *buf){  ui8 *cp;  /*-------------------------------------------------------------------*/  /* Update buffer indices.                                            */  /*-------------------------------------------------------------------*/  buf->ip_pkt -= CONFIG_HDR_LEN;  buf->length += CONFIG_HDR_LEN;  /*-------------------------------------------------------------------*/  /* Prepend configuration header.                                     */  /*-------------------------------------------------------------------*/  cp = buf->ip_pkt;  *cp++ = cnf->code;  *cp++ = cnf->id;  *cp++ = (ui8)(cnf->len >> 8);  *cp++ = (ui8)(cnf->len);}/***********************************************************************//*   fsm_timer: Set a timer in case an expected event does not occur   *//*                                                                     *//***********************************************************************/static void fsm_timer(FSM *fsm){  NetTimerStart(&fsm->timer, fsm->pdc->timeout);}/***********************************************************************//* send_term_req: send a termination request                           *//*                                                                     *//***********************************************************************/static void send_term_req(FSM *fsm){  NetBuf *buf;  /*-------------------------------------------------------------------*/  /* Just return if retry limit exceeded.                              */  /*-------------------------------------------------------------------*/  if ((fsm->retry == 0) || (fsm->rcn_cnt >= RCN_LIMIT))    return;  /*-------------------------------------------------------------------*/  /* Count this try and start timeout timer.                           */  /*-------------------------------------------------------------------*/  --fsm->retry;  fsm_timer(fsm);  /*-------------------------------------------------------------------*/  /* If able to allocate buffer, send request.                         */  /*-------------------------------------------------------------------*/  buf = tcpGetBuf(NIMHLEN + CONFIG_HDR_LEN + CRC16_LEN);  if (buf)    pppFsmSend(fsm, TERM_REQ, 0, buf);}/***********************************************************************//* send_term_ack: send terminate ack                                   *//*                                                                     *//***********************************************************************/static void send_term_ack(FSM *fsm, ui8 id){  NetBuf *buf = tcpGetBuf(NIMHLEN + CONFIG_HDR_LEN + CRC16_LEN);  if (buf)    pppFsmSend(fsm, TERM_ACK, id, buf);}/***********************************************************************//* this_layer_up: Configuration negotiation complete                   *//*                                                                     *//***********************************************************************/static void this_layer_up(FSM *fsm){#if PPP_TRACE  pppFsmLog(fsm, "Opened");#endif  NetTimerStop(&fsm->timer);  fsm->state = fsmOPENED;  fsm->pdc->up(fsm);}/***********************************************************************//* fsm_timeout: Timeout while waiting for reply from remote host       *//*                                                                     *//***********************************************************************/static void fsm_timeout(void *object){  FSM *fsm = object;  int fsmi = fsm->pdc->fsmi;  /*-------------------------------------------------------------------*/  /* Set global PPP channel identifier before calling other routines.  */  /*-------------------------------------------------------------------*/  Ppp = (PPP)((ui8 *)fsm - offsetof(struct PppCB, fsm[fsmi]));  /*-------------------------------------------------------------------*/  /* Optionally record event.                                          */  /*-------------------------------------------------------------------*/#if PPP_TRACE  pppFsmLog(fsm, "Timeout");#endif  /*-------------------------------------------------------------------*/  /* Process timeout according to state.                               */  /*-------------------------------------------------------------------*/  switch (fsm->state)  {    case fsmINITIAL:    case fsmSTARTING:    case fsmCLOSED:    case fsmSTOPPED:    case fsmOPENED:#if PPP_TRACE      pppFsmLog(fsm, "Illegal transition");#endif      break;    case fsmCLOSING:    case fsmSTOPPING:      if (fsm->retry)      {        send_term_req(fsm);      }      else      {#if PPP_TRACE        pppFsmLog(fsm, "Terminate retry exceeded");#endif        fsm->state -= 2;  /* Closing -> Closed and Stopping -> Stopped */        fsm->pdc->finished(fsm);      }      break;    case fsmReqSent:    case fsmAckRcvd:    case fsmAckSent:      if (fsm->retry && (fsm->rcn_cnt < RCN_LIMIT))      {        if (fsm->state == fsmAckRcvd)          fsm->state = fsmReqSent;        fsmSendReq(fsm);      }      else      {#if PPP_TRACE        pppFsmLog(fsm, "Request retry exceeded");#endif        fsm->state = fsmSTOPPED;        if ((Ppp->public.flags & PPPF_PASSIVE) == FALSE)          fsm->pdc->finished(fsm);      }      break;  }}/***********************************************************************//* set_retry_count: Set FSM retry count, enforcing repetition limit    *//*                                                                     *//***********************************************************************/static void set_retry_count(FSM *fsm, int count){  fsm->retry = count;  ++fsm->rcn_cnt;}/***********************************************************************//* Global Function Definitions                                         *//***********************************************************************//***********************************************************************//*   pppRdConf: Extract configuration header from received packet      *//*                                                                     *//***********************************************************************/int pppRdConf(ConfigHdr *hdr, NetBuf *buf){  ui8 *cp = buf->ip_pkt;  /*-------------------------------------------------------------------*/  /* Ensure header data is held in buffer.                             */  /*-------------------------------------------------------------------*/  if (buf->length < CONFIG_HDR_LEN)    return -1;  /*-------------------------------------------------------------------*/  /* Read configuration header from packet.                            */  /*-------------------------------------------------------------------*/  hdr->code = cp[0];  hdr->id = cp[1];  hdr->len = (cp[2] << 8) | cp[3];  /*-------------------------------------------------------------------*/  /* Ensure all packet data is held in buffer.                         */  /*-------------------------------------------------------------------*/  if (buf->length < hdr->len)    return -1;  /*-------------------------------------------------------------------*/  /* Subtract length of header from length of message.                 */  /*-------------------------------------------------------------------*/  hdr->len -= CONFIG_HDR_LEN;    /* length includes header */  /*-------------------------------------------------------------------*/  /* Update buffer indices and return success.                         */  /*-------------------------------------------------------------------*/  buf->ip_pkt += CONFIG_HDR_LEN;  buf->length -= CONFIG_HDR_LEN;  return 0;}/***********************************************************************//*    pppRdOpt: Extract option header from received packet buffer      *//*                                                                     *//***********************************************************************/int pppRdOpt(OptHdr *opt){  ui8 *cp = pppRcvBuf->ip_pkt;  /*-------------------------------------------------------------------*/  /* Ensure packet is not a runt.                                      */  /*-------------------------------------------------------------------*/  if (pppRcvBuf->length < OPTION_HDR_LEN)    return -1;  /*-------------------------------------------------------------------*/  /* Read option header from packet.                                   */  /*-------------------------------------------------------------------*/  opt->type = cp[0];  opt->len = cp[1];  /*-------------------------------------------------------------------*/  /* Update buffer indices and return success.                         */  /*-------------------------------------------------------------------*/  pppRcvBuf->ip_pkt += OPTION_HDR_LEN;  pppRcvBuf->length -= OPTION_HDR_LEN;  return 0;}/***********************************************************************//*                                                                     *//***********************************************************************/void PppFsmNoAction(FSM *fsm){}/***********************************************************************//* PppFsmNoCheck:                                                      *//*                                                                     *//***********************************************************************/int PppFsmNoCheck(FSM *fsm, ConfigHdr *hdr){  return 0;}#if PPP_TRACE/***********************************************************************//*    pppFsmLog: FSM log routine                                       *//*                                                                     *//***********************************************************************/void pppFsmLog(FSM *fsm, char *msg){  pppLogn("PPP/%s %s; %s", fsm->pdc->name, fsmStates[fsm->state], msg);}#endif/***********************************************************************//*  pppFsmSend: Send a packet to the remote host                       *//*                                                                     *//*        Note: The input buffer length should not include the config- *//*              ation header. That length is added by this routine.    *//*                                                                     *//***********************************************************************/void pppFsmSend(FSM *fsm, ui8 code, ui8 id, NetBuf *buf){  ConfigHdr hdr;

⌨️ 快捷键说明

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