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

📄 ppplcp.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 4 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ip/ppp/ppplcp.c                                      *//*   Release: 2001.3                                                   *//*   Version: 2001.0                                                   *//*   Purpose: Negotiate PPP data link options                          *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               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"#include <stdlib.h>#if MAX_PPP_INTF/***********************************************************************//* Configuration                                                       *//***********************************************************************/#define LCP_REQ_LIMIT       10  /* max REQ sends */#define LCP_NAK_LIMIT       5   /* max NAK sends */#define LCP_TERM_LIMIT      3   /* max TERM REQ sends */#define LCP_TIMEOUT         3   /* seconds to wait for response *//***********************************************************************//* Symbol Definitions                                                  *//***********************************************************************/#define LCP_ACCM_DEFAULT    0xFFFFFFFF#define LCP_MRU_DEFAULT     1500#define MD5_ALG             0x05/***********************************************************************//* Function Prototypes                                                 *//***********************************************************************/static void lcp_starting(FSM *fsm);static void lcp_up(FSM *fsm);static void lcp_down(FSM *fsm);static void lcp_finished(FSM *fsm);static NetBuf *make_req(FSM *fsm);static int  check_req(FSM *fsm, ConfigHdr *config);static int  check_ack(FSM *fsm, ConfigHdr *ackcnf);static int  nak_check(FSM *fsm, ConfigHdr *nakcnf);static int  check_rej(FSM *fsm, ConfigHdr *rejcnf);/***********************************************************************//* Constant Data Definitions                                           *//***********************************************************************/static const struct fsm_constants LcpConstants ={  "LCP",  PPP_LCP_PROTO,  0x0FFE,       /* codes 1-11 recognized */  kLCP,  LCP_REQ_LIMIT,  LCP_NAK_LIMIT,  LCP_TERM_LIMIT,  LCP_TIMEOUT * TICKS_PER_SEC,  lcp_starting,  lcp_up,  lcp_down,  lcp_finished,  make_req,  check_req,  check_ack,  nak_check,  check_rej};/***********************************************************************//* Local Function Definitions                                          *//***********************************************************************//***********************************************************************//*    lcp_rand: Returns a 32-bit random magic number                   *//*                                                                     *//***********************************************************************/static ui32 lcp_rand(void){  return (rand() << 16) | rand();}/***********************************************************************//*  add_option: Write specified option to reply buffer                 *//*                                                                     *//***********************************************************************/static void add_option(NetBuf *buf, LcpOpts *opts, int otype){  int olen;  ui8 *cp = buf->app_data + 2; /* point past type and length */  /*-------------------------------------------------------------------*/  /* Parse option type and write response to buffer.                   */  /*-------------------------------------------------------------------*/  switch (otype)  {    case LCP_MRU:      cp = put16(cp, opts->mru);#if PPP_TRACE      pppLog("   making MRU=%u", opts->mru);#endif      break;    case LCP_ACCM:      cp = put32(cp, opts->accm);#if PPP_TRACE      pppLog("   making ACCM=0x%08X", opts->accm);#endif      break;    case LCP_AUTHENT:      cp = put16(cp, opts->auth_proto);#if PPP_TRACE      pppLog("   making Auth Protocol=0x%04X", opts->auth_proto);#endif      switch (opts->auth_proto)      {        case PPP_PAP_PROTO:          break;        case PPP_CHAP_PROTO:          *cp++ = MD5_ALG;          break;      }      break;    case LCP_MAGIC:      cp = put32(cp, opts->magic);#if PPP_TRACE      pppLog("   making Magic Number=0x%08X", opts->magic);#endif      break;    case LCP_PFC:#if PPP_TRACE      pppLog("   making Protocol compression");#endif      break;    case LCP_ACFC:#if PPP_TRACE      pppLog("   making Addr/Ctrl compression");#endif      break;    default:#if PPP_TRACE      pppLog("   making unimplemented type %02X", otype);#endif      break;  }  /*-------------------------------------------------------------------*/  /* Record option type and length.                                    */  /*-------------------------------------------------------------------*/  olen = cp - buf->app_data;  buf->app_data[0] = (ui8)otype;  buf->app_data[1] = (ui8)olen;  /*-------------------------------------------------------------------*/  /* Update buffer indices.                                            */  /*-------------------------------------------------------------------*/  buf->app_data = cp;  buf->length += olen;}/***********************************************************************//*   make_opts: Build list of LCP options. Used to build unsolicated   *//*              NAK list and our configuration requests                *//*                                                                     *//***********************************************************************/static void make_opts(NetBuf *buf, LcpOpts *opts, uint negotiating){  int otype;  /*-------------------------------------------------------------------*/  /* Add each option in turn.                                          */  /*-------------------------------------------------------------------*/  for (otype = 1; otype <= LCP_OPT_LIMIT; ++otype)  {    if (negotiating & (1 << otype))      add_option(buf, opts, otype);  }}/***********************************************************************//*    make_req: Build a request to send to peer                        *//*                                                                     *//***********************************************************************/static NetBuf *make_req(FSM *fsm){  NetBuf *rbuf;  LcpCB *lcp = &Ppp->lcp;  /*-------------------------------------------------------------------*/  /* Attempt to allocate buffer. Return NULL if unable.                */  /*-------------------------------------------------------------------*/  rbuf = tcpGetBuf(NIMHLEN + PPP_MTU + CRC16_LEN);  if (rbuf == NULL)    return NULL;  /*-------------------------------------------------------------------*/  /* Build packet with all options currently being negotiated.         */  /*-------------------------------------------------------------------*/  make_opts(rbuf, &lcp->local, lcp->local.options);  return rbuf;}/***********************************************************************//* check_req_opt: Check requested option, updating our remote value    *//*                                                                     *//*      Inputs: remote = negotiation parameters for remote side        *//*              opt = pointer to received option header                *//*                                                                     *//*     Returns: -1 if ran out of data, ACK/NAK/REJ as appropriate      *//*                                                                     *//***********************************************************************/static int check_req_opt(LcpOpts *remote, OptHdr *opt){  int toss = opt->len - OPTION_HDR_LEN;  int result = CONFIG_ACK;      /* assume good values */  /*-------------------------------------------------------------------*/  /* Parse option type.                                                */  /*-------------------------------------------------------------------*/  switch (opt->type)  {    case LCP_MRU:      /*---------------------------------------------------------------*/      /* Verify MRU option length is correct.                          */      /*---------------------------------------------------------------*/      if (opt->len != 4)      {        opt->len = 4;        result = CONFIG_NAK;        if (toss < 0)          toss = 0;        break;      }      /*---------------------------------------------------------------*/      /* Read the peer's requested MRU.                                */      /*---------------------------------------------------------------*/      remote->mru = pull16(pppRcvBuf);      toss -= 2;#if PPP_TRACE      pppLog("   checking MRU=%u", remote->mru);#endif      /*---------------------------------------------------------------*/      /* Ensure the requested MRU is not too small.                    */      /*---------------------------------------------------------------*/      if (remote->mru < LCP_MRU_LO)      {        remote->mru = LCP_MRU_LO;        result = CONFIG_NAK;      }      break;    case LCP_ACCM:      /*---------------------------------------------------------------*/      /* Verify ACCM option length is correct.                         */      /*---------------------------------------------------------------*/      if (opt->len != 6)      {        opt->len = 6;        result = CONFIG_NAK;        if (toss < 0)          toss = 0;        break;      }      /*---------------------------------------------------------------*/      /* Read the peer's requested ACCM.                               */      /*---------------------------------------------------------------*/      remote->accm = pull32(pppRcvBuf);      toss -= 4;#if PPP_TRACE      pppLog("   checking ACCM=0x%08X", remote->accm);#endif      /*---------------------------------------------------------------*/      /* Peer must escape the characters we require.                   */      /*---------------------------------------------------------------*/      if (remote->accm != (remote->accm | Ppp->public.asyncmap))      {        remote->accm |= Ppp->public.asyncmap;        result = CONFIG_NAK;      }      break;    case LCP_AUTHENT:      /*---------------------------------------------------------------*/      /* Read peer's requested authentication method.                  */      /*---------------------------------------------------------------*/      remote->auth_proto = pull16(pppRcvBuf);      toss -= 2;#if PPP_TRACE      pppLog("   checking Auth Protocol=0x%04X", remote->auth_proto);#endif

⌨️ 快捷键说明

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