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

📄 pppchat.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 4 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ip/pppchat.c                                         *//*   Release: 2001.3                                                   *//*   Version: 2000.1                                                   *//*   Purpose: Chat -- a program for automatic session establishment    *//*            (initializing modem, dialing phone, and logging in)      *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               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"#include <stdlib.h>#if MAX_PPP_INTF/***********************************************************************//* Configuration                                                       *//***********************************************************************/#define DEFAULT_TIMEOUT 45#define STR_LEN         32#define RBUF_LEN        256/***********************************************************************//* Symbol Definitions                                                  *//***********************************************************************/#define SBUF_LEN        64#define SBUF_START      0#define RBUF_START      SBUF_LEN#define STR_START       (SBUF_LEN + RBUF_LEN)/*** CHAT Minor States*/#define INTERPRETING    0#define SENDING         1#define WAIT_FOR_SPACE  2#define EXPECTING       3#define WAIT_FOR_CHAR   4#define WAIT_FOR_IDLE   5/*** CHAT Major States*/#define STARTING        0#define INITIALIZING    1#define DIALING         2#define LOGGING_IN      3#define FINISHING       4/***********************************************************************//* Function Prototypes                                                 *//***********************************************************************/static int  put_char(int ch);static void next_state(void);static int  received(Match *match);static int  short_chat(void);static void chat_up(void);/***********************************************************************//* Local Function Definitions                                          *//***********************************************************************/#if PPP_TRACE/***********************************************************************//*      string: Copy and NULL-terminate a command string parameter     *//*                                                                     *//***********************************************************************/static char *string(char *script){  int i;  char *buf = (char *)&Ppp->chat.buf->data[STR_START];  /*-------------------------------------------------------------------*/  /* Copy initial '.                                                   */  /*-------------------------------------------------------------------*/  buf[0] = *script++;  /*-------------------------------------------------------------------*/  /* Copy remainder of string, up to buffer limit.                     */  /*-------------------------------------------------------------------*/  for (i = 1; *script && (*script != '\'') && (i < STR_LEN - 2); ++i)    buf[i] = *script++;  /*-------------------------------------------------------------------*/  /* Copy ending ', NULL-terminate, and return pointer to string.      */  /*-------------------------------------------------------------------*/  buf[i] = '\'';  buf[i + 1] = '\0';  return buf;}#endif/***********************************************************************//* fatal_error: Fatal CHAT error occurred                              *//*                                                                     *//***********************************************************************/static void fatal_error(char *msg){#if PPP_TRACE  /*-------------------------------------------------------------------*/  /* Log error message if trace is enabled.                            */  /*-------------------------------------------------------------------*/  pppLogn("CHAT %s", msg);#endif  /*-------------------------------------------------------------------*/  /* Post down event and end all PPP activity on this channel.         */  /*-------------------------------------------------------------------*/  pppFinish();}/***********************************************************************//*   chat_retry: Timeout or abort occurred during "Expect" command     *//*                                                                     *//***********************************************************************/static void chat_retry(void){  /*-------------------------------------------------------------------*/  /* If number of allowed trys is exceeded, fail.                      */  /*-------------------------------------------------------------------*/  if (++Ppp->chat.num_trys > Ppp->public.chat.max_trys)    fatal_error("allowed retries exceeded, shutting down");  /*-------------------------------------------------------------------*/  /* Else disconnect and, after 3 seconds, restart CHAT processing.    */  /*-------------------------------------------------------------------*/  else  {#if PPP_TRACE    pppLogn("CHAT restarting");#endif    Ppp->public.disconnect();    Ppp->chat.timer.action = chatStart;    NetTimerStart(&Ppp->chat.timer, TICKS_PER_SEC * 3);  }}/***********************************************************************//* chat_timeout: Timeout occurred during CHAT "Expect" command         *//*                                                                     *//***********************************************************************/static void chat_timeout(void *handle){  Ppp = handle;  /*-------------------------------------------------------------------*/  /* Log that expect command timed out if trace is enabled.            */  /*-------------------------------------------------------------------*/#if PPP_TRACE  pppLogn("CHAT expect timed out");#endif  /*-------------------------------------------------------------------*/  /* If coming up, restart CHAT. Else shutdown.                        */  /*-------------------------------------------------------------------*/  if (Ppp->chat.major_state != FINISHING)    chat_retry();  else    pppFinish();}/***********************************************************************//*     do_chat: Chat until error or wait                               *//*                                                                     *//***********************************************************************/static void do_chat(void *handle){  /*-------------------------------------------------------------------*/  /* Set global PPP channel handle.                                    */  /*-------------------------------------------------------------------*/  Ppp = handle;  /*-------------------------------------------------------------------*/  /* Call short_chat() until it returns 0 for error or wait.           */  /*-------------------------------------------------------------------*/  while (short_chat())  {    /*-----------------------------------------------------------------*/    /* Ensure commands are properly separated with white space.        */    /*-----------------------------------------------------------------*/    if ((Ppp->chat.minor_state == INTERPRETING) && *Ppp->chat.script        && (*Ppp->chat.script != ' ') && (*Ppp->chat.script != '\t'))    {      fatal_error("illegal command");      return;    }  }}/***********************************************************************//*  short_chat: Chat until error, state change, new command, or wait   *//*                                                                     *//***********************************************************************/static int short_chat(void){  int i, ch;  char *cp;  switch (Ppp->chat.minor_state)  {    case INTERPRETING:      /*---------------------------------------------------------------*/      /* Process script until ending NULL.                             */      /*---------------------------------------------------------------*/      while (*Ppp->chat.script != '\0')      {        /*-------------------------------------------------------------*/        /* Skip space and tab characters.                              */        /*-------------------------------------------------------------*/        if (*Ppp->chat.script == ' ' || *Ppp->chat.script == '\t')        {          ++Ppp->chat.script;          continue;        }        /*-------------------------------------------------------------*/        /* Parse CHAT command.                                         */        /*-------------------------------------------------------------*/        switch (*Ppp->chat.script)        {          /*-----------------------------------------------------------*/          /* Check for "Abort" command.                                */          /*-----------------------------------------------------------*/          case 'A':            /*---------------------------------------------------------*/            /* Ensure opening '\'' is present.                         */            /*---------------------------------------------------------*/            if (*++Ppp->chat.script != '\'')            {              fatal_error("missing \'");              return 0;            }#if PPP_TRACE            /*---------------------------------------------------------*/            /* Log new abort string if trace is enabled.               */            /*---------------------------------------------------------*/            pppLogn("CHAT aborting on %s", string(Ppp->chat.script));#endif            /*---------------------------------------------------------*/            /* Find unused abort string pointer.                       */            /*---------------------------------------------------------*/            for (i = 0;; ++i)            {              /*-------------------------------------------------------*/              /* If unused, point to abort string and then break.      */              /*-------------------------------------------------------*/              if (Ppp->chat.abort[i].string == NULL)              {                Ppp->chat.abort[i].string = Ppp->chat.script + 1;                Ppp->chat.abort[i].compare = Ppp->chat.match.compare;                break;              }              /*-------------------------------------------------------*/              /* Error if none are free.                               */              /*-------------------------------------------------------*/              if (i == CHAT_ABORTS - 1)              {                fatal_error("too many ABORT strings");                return 0;              }            }            /*---------------------------------------------------------*/            /* Advance script pointer to next command.                 */            /*---------------------------------------------------------*/            Ppp->chat.abort[i].length = 0;            while (*++Ppp->chat.script != '\'')            {              if (*Ppp->chat.script == '\0')              {                fatal_error("missing \'");                return 0;              }              ++Ppp->chat.abort[i].length;            }            ++Ppp->chat.script;            /*---------------------------------------------------------*/            /* Return and then start the next command.                 */            /*---------------------------------------------------------*/            return 1;          /*-----------------------------------------------------------*/          /* Check for "Delay" command.                                */          /*-----------------------------------------------------------*/          case 'D':            /*---------------------------------------------------------*/            /* Read number of seconds to delay from script.            */            /*---------------------------------------------------------*/            i = strtol(Ppp->chat.script + 1, &Ppp->chat.script, 10);            if (i <= 0)              i = 1;#if PPP_TRACE            /*---------------------------------------------------------*/            /* Log delay period if trace is enabled.                   */            /*---------------------------------------------------------*/            pppLogn("CHAT delaying %u seconds", i);#endif

⌨️ 快捷键说明

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