pppsys.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 786 行 · 第 1/2 页

C
786
字号
/*
 * FILENAME: pppsys.c
 *
 * Copyright 2001 By InterNiche Technologies Inc. All rights reserved
 *
 * InterNiche Multilink PPP layer's system (IP and OS) routines.
 *
 * MODULE: PPP
 *
 *
 * PORTABLE: within limits.
 */


#define  _PPP_PORT_C    1  /* for ConPrintf() declaration in ppp_port.h */

#include "ipport.h"

#ifdef USE_PPP          /* whole file can be ifdeffed out */

#include "ppp_port.h"
#include "mppp.h"
#include "ip.h"
#include "nvparms.h"

#include "minip.h"      /* (yaxon add) */

#ifdef NATIVE_PRINTF
#include <stdio.h>
#include <stdarg.h>
#endif

#ifdef INCLUDE_SNMP
#include "snmpport.h"
#endif

#ifdef USE_MODEM
#include "mdmport.h"
#else
#include UART_INCLUDE
#endif

#ifdef DNS_CLIENT
#include "dns.h"
#endif

#include "ppp_loop.h"

#ifdef   USE_PPPOE
#include "pppoe.h"
#endif

/* USE_PPPOE */

/* default type for new PPP interfaces. This may be overwritten
 * by application code prior to callin ip_startup(). Setting it
 * to LN_PORTSET will cause the setup code to get the type with
 * a callback to ppp_type_callback().
 */

#ifdef USE_MODEM
int   ppp_default_type = LN_ATMODEM;      /* default to modem */
#else
int   ppp_default_type = LN_UART;
#endif

int   PPPDEBUG;      /* debug options bit mask */


int   (*ppp_type_callback)(LINEP);

extern   int   ppp_client_tmo;


u_short
ppp_getshort(u_char * cp)
{
   u_short ret;

   ret = *(cp + 1);
   ret |= (*cp) << 8;

   return ret;
}


u_long
ppp_getlong( u_char * cp)
{
   u_long ret;

   ret =  (u_long)(*(cp + 3));
   ret |= (u_long)(*(cp + 2)) << 8;
   ret |= (u_long)(*(cp + 1)) << 16;
   ret |= (u_long)(*cp) << 24;

   return ret;
}


u_char *
ppp_putshort(u_char * cp, u_short value)
{
   *(cp + 1) = (u_char)(value & 0xFF);
   *(cp) = (u_char)(value >> 8);

   return(cp + 2);
}


u_char *
ppp_putlong( u_char * cp, u_long  value)
{

   *(cp + 3) = (u_char)(value & 0x000000FF);
   *(cp + 2) = (u_char)((value & 0x0000FF00) >> 8);
   *(cp + 1) = (u_char)((value & 0x00FF0000) >> 16);
   *(cp) = (u_char)((value & 0xFF000000) >> 24);

   return(cp + 4);
}


#ifdef PPP_CHARIO

/* FUNCTION: ppp_direct_in()
 *
 * Line ln_getc() callback for
 * PPP on a PC (or similar) UART. This does HDLC like decoding, handles
 * PPP input flags, and reassembles the PPP packet. All protocols (LCP,
 * IPCP, and IP can arrive through here on UART like devices.
 *
 *
 * PARAM1: int unit
 * PARAM2: int chr
 *
 * RETURNS: 0 if OK, else ENP_ error code.
 */

int
ppp_direct_in(struct com_line * line, int chr)
{
   M_PPP mppp;

#ifdef NPDEBUG
   if(line->upper_type != LN_PPP)
   {
      dtrap("pppsys 0\n");
      dprintf("PPP: Bad line type\n");
      return ENP_LOGIC;
   }
#endif /* NPDEBUG */

   mppp = (M_PPP)line->upper_unit;

   /* If PPP state is down, start 1it up as a server */
   if (mppp->states[LCP_STATE] == ST_INITIAL)
   {
      /* direct connect */
      ppp_lowerup(mppp, LCP_STATE);    /* tell PPP lower layer is up */
      ppp_open(mppp, LCP_STATE);       /* start LCP */
   }
   ppp_inchar(mppp, chr);    /* hand char we got to ppp code */
   return 0;
}
#endif   /* PPP_CHARIO */


int      init_calls  =  0;
unsigned long ppp_keepalive = 0;


/* FUNCTION: ppp_line_init()
 *
 * Do basic ppp line initialization. This is called once for each
 * static unit set in prep_ppp() and also once for dynamically created
 * units.  Things which need to be reset each time the line connects
 * (as opposed to just once when it's created) should go in
 * ppp_fsm_init(), not here.
 *
 * PARAM1: M_PPP mppp; pointer to
 * PARAM2: int ppp_type;
 *
 * RETURNS: 0 if OK, else ENP_ error code.
 */

int
ppp_line_init(M_PPP mppp, int ppp_type)
{
   int   iface;
   int   e  =  0; /* error holder */


   /* set options from nv file */

   /* switch on any debug logging options */
   if (pppcfg.ppp_ConsoleLog)
      PPPDEBUG |= PPPD_CONS;
   if (pppcfg.ppp_FileLog)
      PPPDEBUG |= PPPD_FILE;

   ppp_keepalive = pppcfg.ppp_keepalive;

   /* set default IP address for iface - this may be changed by
    * IPCP or DHCP later.
    */
   if(mppp->ifp)     /* skip if no iface yet (ML server) */
   {
      iface = if_netnumber(mppp->ifp);
#ifdef INCLUDE_NVPARMS
      /* get from NV storage */
      mppp->ipcp_wantoptions.ouraddr = inet_nvparms.ifs[iface].ipaddr;
#else
      mppp->ipcp_wantoptions.ouraddr = mppp->ifp->n_ipaddr;
#endif   /* INCLUDE_NVPARMS */
   }

#ifdef PPP_MULTILINK
   /* see if system flag is set to allow ML by default */
   if(ppp_allow_ml)
      mppp->pppflags |= ALLOW_ML;
#endif  /* PPP_MULTILINK */

#ifdef AUTH_CALLBACK
   ppp_auth_callback(mppp);     /* call user routine to set auth info */
#else  /* no Auth callback routine; set defaults from NV system */
#ifdef CHAP_SUPPORT
   mppp->username = pppcfg.username;     /* name for CHAP/UPAP messages */
   mppp->password = pppcfg.secret;       /* UPAP paddword or CHAP secret */
   mppp->lcp_wantoptions.neg_chap = (pppcfg.require_chap);
   /* report with dprintf() since ConPrintf() is not defined in this file */
   dprintf("CHAP setup: required: %d, secret: %s, username: %s\n",
    (int)mppp->lcp_wantoptions.neg_chap, pppcfg.secret, mppp->username);
#endif   /* CHAP_SUPPORT */

#ifdef PAP_SUPPORT
   mppp->lcp_wantoptions.neg_upap = (pppcfg.require_pap);
   mppp->username = pppcfg.username;
   mppp->password = pppcfg.password;

   /* For PAP, add the modem/dialer user to database. This user &
    * password may be user to verify incoming calls or log in to an ISP.
    */
   if (init_calls == 0)
   {
      add_user(mppp->username, mppp->password, NULL);
   }
   dprintf("PAP setup: required: %d, username: %s, password: %s\n",
      (int)mppp->lcp_wantoptions.neg_upap, mppp->username, mppp->password);
#endif   /* PAP_SUPPORT */
#endif /* no AUTH_CALLBACK */

#ifdef PPP_CLIENT_TMO
   if (pppcfg.ppp_client_tmo)
      ppp_client_tmo = pppcfg.ppp_client_tmo;  /* overwrite default */
#endif

   mppp->line.upper_type = LN_PPP;     /* we are always PPP type */

   /* Set the type of the lower-level hardware interface. This could be
    * UART, Modem, PPPoE, LB-xover test, etc. Usually this is the passed
    * type, however ports may set esoteric types by passing a type of
    * LN_PORTSET and providing a callback routine to set the type.
    */

   if(ppp_type != LN_PORTSET)
      mppp->line.lower_type = ppp_type;
   else if(ppp_type_callback)
   {
      e = ppp_type_callback(&mppp->line);
      if(e)
      {
         dprintf("ppp_line_init: error getting type\n");
         return ENP_LOGIC;
      }
   }
   else
   {
      dprintf("ppp_line_init: no type callback\n");
      return ENP_LOGIC;
   }

   /* line type is now set, use it to set up line and line table */
   switch(mppp->line.lower_type)
   {

#ifdef  USE_COMPORT
   case LN_UART:
      /* map PPP session direct to a UART (no modem) */
      mppp->line.ln_connect = ln_uconnect;
      mppp->line.ln_disconnect = ln_udisconnect;
      mppp->line.ln_write = NULL;    /* use ln_uputc() */
      mppp->line.ln_putc = ln_uputc;
      mppp->line.ln_getc = ppp_direct_in;
      mppp->line.ln_state = LN_INITIAL;
      e = ln_uinit(&mppp->line);
      break;
#endif   /* USE_COMPORT */

#ifdef LB_XOVER
   case  LN_LBXOVER:
      /* set up ppp loopback driver as line */
      mppp->line.ln_connect = pl_connect;
      mppp->line.ln_disconnect = pl_hangup;

      /* Setup loopback-crossover driver for char or packet testing depending
       * on system ifdefs:
       */
#ifdef PPP_PACKETS
#ifndef PPP_LBCHAR
      mppp->line.ln_write = pl_write;
      mppp->lcp_wantoptions.neg_asyncmap = 0;
#endif /* PPP_LBCHAR */
#endif /* LB_PACKETS */

#ifdef PPP_CHARIO
      mppp->line.ln_putc = pl_putc;
      mppp->lcp_wantoptions.neg_asyncmap = 1;
      mppp->line.ln_getc = ppp_direct_in;
#endif

      mppp->line.ln_state = LN_INITIAL;
      e = pl_init(&mppp->line);
      break;
#endif   /* LB_XOVER */

#ifdef   USE_MODEM
   case LN_ATMODEM:
      /* use AT modem dialer library */
      mppp->line.ln_connect = modem_connect;
      mppp->line.ln_disconnect = modem_hangup;
      mppp->line.ln_write = NULL;
      mppp->line.ln_putc = modem_putc;
      mppp->line.ln_getc = ppp_direct_in;
      mppp->line.ln_state = LN_INITIAL;
      e = modem_init(&mppp->line);
      break;
#endif   /* USE_MODEM */
#ifdef   USE_PPPOE
   case LN_PPPOE:
      /* use PPP over ethernet */
      mppp->line.ln_connect = poe_connect;
      mppp->line.ln_disconnect = poe_ln_disconnect;
      mppp->line.ln_write = poe_write;
      mppp->line.ln_putc = NULL;
      mppp->line.ln_getc = NULL;
      mppp->line.ln_state = LN_INITIAL;
      mppp->lcp_wantoptions.neg_asyncmap = 0;
      e = poe_init(mppp);   /* set up ethernet hooks */
      break;
#endif   /* USE_PPPOE */
   default:
#ifdef   USE_PORTLINKSETUP    /* port supplied link setup routine? */
      e = ppp_portlinksetup(mppp);
      break;
#else
      dtrap("pppsys 1\n"); /* no valid link type */
      return ENP_LOGIC;
#endif   /* USE_PORTLINKTYPE */
   }

   init_calls++;
   return e;
}

/* code to implement ConPrintf(), the PPP debug printf routine, into
 * DOS console & file system.
 */

#ifdef PPP_LOGFILE
static char * logfilename = "ppp.log";
FILE * ppplogfile;
#ifndef NATIVE_PRINTF
static char logbuf[256];
#endif   /* NATIVE_PRINTF */
static char lastchar;  /* last character printed, often '\n' */
#endif   /* PPP_LOGFILE */


#ifndef CONPRINTF_DPRINTF  /* only do this is not using dprintf as ConPrintf */

/* FUNCTION: ConPrintf()
 *
 * ConPrintf() - All PPP ports need to provide this printf-like
 * routine to record DEBUG messages to a log. This log should be a
 * device such as a file or UDP "LOG" server. This log will help both
 * developers and end users during complex installations. In the Demo
 * package, this uses user-seletable options to either print the
 * messages to the system console or to a disk file, or both.
 *
 *
 * PARAM1: const char * format
 * PARAM2: ...

⌨️ 快捷键说明

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