pppsys.c

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

C
786
字号
 *
 * RETURNS: void
 */

#ifdef NATIVE_PRINTF /* using Compiler's lib */
void
ConPrintf(const char * format, ...)
{
   va_list argList;
#else /* using iniche dprintf() for console output */
void
ConPrintf(const char * format, long a1, long a2, long a3, long a4, long a5)
{
#endif /* NATIVE_PRINTF or not */
   int   flen; /* length of format string */
   int   needlf;  /* TRUE if we want to append a linefeed */
   char  endchar; /* ending char of format string */

   if (!PPPDEBUG) /* don't bother iof no flags are set */
      return;

   flen = strlen((char*)format);
   endchar = *(format+(flen-1));

   /* decide if we want to append a newline? */
   if ((flen > 5) &&   /* long-ish string? */
       (endchar != '\n') &&
       (endchar != ':') &&
       (endchar != '=') &&
       (endchar != ' '))
   {
      needlf = TRUE;
   }
   else /* short string, or string ends in special char */
      needlf = FALSE;

   if (PPPDEBUG & PPPD_CONS)
   {
#ifdef NATIVE_PRINTF
      va_start(argList,format);
      vprintf(format,argList);
      va_end(argList);
#else
      dprintf((char*)format, a1, a2, a3, a4, a5);
#endif
      if (needlf)
         Printu_Net("\n");
   }

#ifdef PPP_LOGFILE
   if ((PPPDEBUG & PPPD_FILE) && ppplogfile)
   {
      /* timestamp log file entrys */
      if (lastchar == '\n')
         fprintf(ppplogfile, "[%lu] ", cticks);
#ifdef NATIVE_PRINTF
      va_start(argList,format);
      vfprintf(ppplogfile,format,argList);
      va_end(argList);
      if (needlf)
         fprintf(ppplogfile, "\n");
#else
      sprintf_t(logbuf, (char*)format, a1, a2, a3, a4, a5);
      if (strlen(logbuf) >= sizeof(logbuf))
         panic("ppp_logbuf");
      fputs(logbuf, ppplogfile);
#endif
   }

   /* remember the last character we printed */
   if (needlf)   /* did we force a newline char? */
      lastchar = '\n';
   else
      lastchar = endchar;
#endif   /* PPP_LOGFILE */
}

#endif   /* CONPRINTF_DPRINTF */


/* FUNCTION: ppp_logfile()
 *
 * ppp_logfile() - toggle open state of PPP log file & mask bit. This
 * is called from the menus in the ..\misclib\nrmenus.c file, so it's
 * syntax should not be changed unless the menus are changed also.
 *
 *
 * PARAM1: void * pio
 *
 * RETURNS:
 */

int
ppp_logfile(void * pio)
{
#ifdef PPP_LOGFILE
   if (ppplogfile)
   {
      fclose(ppplogfile);
      ppplogfile = NULL; /* use pointer as file open flag */
      PPPDEBUG &= ~PPPD_FILE;  /* clear bit in DEBUG mask */
      ns_printf(pio,"ppp file logging turned OFF\n");
   }
   else /* file logging was off, turn it on */
   {
      ppplogfile = fopen(logfilename, "w");
      if (!ppplogfile)
      {
         ns_printf(pio,"Can't open PPP log file\n");
         return -1;
      }
      PPPDEBUG |= PPPD_FILE;
      ns_printf(pio,"ppp file logging to %s ON\n", logfilename);
   }
#else
   /* pio is passed to stifle idiot compiler warnings about unused parms */
   dprintf("ppp_logfile: not supported on this build.\n", pio);
#endif   /* PPP_LOGFILE */
   return 0;
}



/* FUNCTION: ppplogcons()
 *
 * PARAM1: void * pio
 *
 * RETURNS:
 */

int
ppplogcons(void * pio)  /* toggle the console debug printfs */
{
   if (PPPDEBUG & PPPD_CONS)
   {
      PPPDEBUG &= ~PPPD_CONS;
      pppcfg.ppp_ConsoleLog = FALSE;
   }
   else
   {
      PPPDEBUG |= PPPD_CONS;
      pppcfg.ppp_ConsoleLog = TRUE;
   }

   ns_printf(pio,"ppp console logging turned %s\n",
    (PPPDEBUG & PPPD_CONS) ? "ON" : "OFF");
   return 0;
}


/* PPP "signal" routines to handle IP stack setup based on IPCP
 * negotiations. The return true if the callback is succesfull
 * (e.g. if is up, IP is set).
 */

/* the PPP interface has come up */

int
ppp_ifup(M_PPP mppp)
{
   if(mppp->ifp)
      mppp->ifp->n_mib->ifOperStatus = 1; /* iface is UP */
   return TRUE;
}

/* PPP link was assigned an IP address */

int
ppp_setaddr(M_PPP mppp, ip_addr our_addr)
{
   if (mppp->ifp->n_ipaddr != our_addr)
   {
      mppp->ifp->n_ipaddr = our_addr;
      mppp->pppflags |= IPCP_IPADDR;
   }
   return TRUE;
}

/* Verify that the peer is allowed to use the IP address he wants. */

int
ppp_authaddr(M_PPP mppp, ip_addr hisaddr)
{
   USE_ARG(mppp);
   USE_ARG(hisaddr);

   return TRUE;
}


/* Set an IP route to PPP peer's IP address. */

int
ppp_setroute(M_PPP mppp, ip_addr his_addr)
{
#ifdef MINI_IP
   USE_ARG(mppp);
   USE_ARG(his_addr);
#else
   int iface = if_netnumber(mppp->ifp);
   add_route(his_addr, 0xFFFFFFFF, his_addr, iface, IPRP_OTHER);
   mppp->pppflags |= IPCP_ROUTE;       /* route set by ipcp */
#endif
   return 0;
}

#ifdef PROXY_ARP
int
ppp_setproxyarp(M_PPP mppp, ip_addr ipaddr)
{
   USE_ARG(mppp);
   USE_ARG(ipaddr);
   dtrap("pppsys 2\n");

   return 0;
}
#endif   /* PROXY_ARP */


int
ppp_ifdown(M_PPP mppp)
{
   if(mppp->ifp)
      mppp->ifp->n_mib->ifOperStatus = 2; /* iface is DOWN */
   return 0;
}

/* clear all IP address info. This includes IP address in interface,
 * proxy arp, and routes that were set when the IPCP session was
 * established
 */

int
ppp_clearaddr(M_PPP mppp)
{
#ifdef MINI_IP
   USE_ARG(mppp);
#else /* not MINI_IP - delete route from full IP stack */

   if(mppp->pppflags & IPCP_IPADDR)
   {
      mppp->ifp->n_ipaddr = 0L;        /* clear IP in iface */
      mppp->pppflags &= ~IPCP_IPADDR;  /* clear flag */
   }

   if(mppp->pppflags & IPCP_ROUTE)       /* route set by ipcp? */
   {
      ip_addr his_addr;

      his_addr = mppp->ipcp_hisoptions.hisaddr;
      del_route(his_addr, 0xFFFFFFFF, if_netnumber(mppp->ifp));
      mppp->pppflags &= ~IPCP_ROUTE;  /* clear flag */
   }
#endif   /* MINI_IP */

   return 0;
}


#ifdef PPP_DNS

/* FUNCTION: ppp_setdnsaddr()
 *
 * Called by IPCP when IP link is up, if IPCP domain
 * name server negotiations have resulted in the PPP peer giving us
 * domain name server addresses (or configure-ack'ing our addresses).
 * sifdnsaddr() returns 1 if successful, and 0 if unsuccessful. An
 * unsuccessful return simply means that the nameserver address was
 * not used; this could be because the address was already known or
 * because the DNS client doesn't have room for new addresses.
 *
 *
 * PARAM1: M_PPP
 * PARAM2: u_long dnsaddr
 *
 * RETURNS: 1 if DNS address was removed, else 0
 */


int
ppp_setdnsaddr(M_PPP mppp, ip_addr dnsaddr)
{
   /* if no address given, return "un-success" indication */
   if (dnsaddr == 0)
      return 0;

#ifdef DNS_CLIENT
   {
      int   i;

      /* This code is specific to the InterNiche DNS client and
       * shares some of its assumptions: that valid name server
       * addresses are stored in dns_servers[] starting at [0] and
       * that an entry with zero value is both available for use and
       * indicates the present end of the valid name server address
       * list. So it looks for the first unused entry, and if it
       * finds one fills it in with the supplied nameserver address;
       * and it looks for the supplied nameserver address so to not
       * put duplicate entries in the DNS client's list. This
       * probably will not work if multiple entities are adding to
       * and removing from the DNS and may try to add the same
       * nameserver address(es).
       */
      for (i = 0; i < MAXDNSSERVERS; i++)
      {
         if (dns_servers[i] == 0)
         {
            dns_servers[i] = dnsaddr;
            return 1;
         }
         if (dns_servers[i] == dnsaddr)
            return 0;
      }
   }
#endif   /* DNS_CLIENT */

   USE_ARG(mppp);

   /* return un-success */
   return 0;
}


/* FUNCTION: cifdnsaddr()
 *
 * cifdnsaddr - Clear domain name server address previously installed
 * via sifdnsaddr().
 *
 *
 * PARAM1: int unit
 * PARAM2: u_long dnsaddr
 *
 * RETURNS:
 */

int
ppp_cleardnsaddr(M_PPP mppp, ip_addr dnsaddr)
{

   /* if no address given, return success indication */
   if (dnsaddr == 0)
      return 1;

#ifdef DNS_CLIENT
   {
      int   i;

      /* Look for the supplied nameserver address in the DNS client's
       * list, and if found, move subsequent entries down over it to
       * delete it and pad the end of the array with a 0 (unused
       * entry). See the comment in sifdnsaddr() for more information
       * on the assumptions behind this and limitations in this
       * approach.
       */
      for (i = 0; i < MAXDNSSERVERS; i++)
      {
         if (dns_servers[i] == dnsaddr)
         {
            while (i < MAXDNSSERVERS - 1)
            {
               dns_servers[i] = dns_servers[i + 1];
               i++;
            }
            dns_servers[i] = 0;
            return 1;
         }
         if (dns_servers[i] == 0)
            return 1;
      }
   }
#endif   /* DNS_CLIENT */

   USE_ARG(mppp);

   /* return success */
   return 1;
}
#endif   /* PPP_DNS */


u_long
ppp_magic(M_PPP mppp)
{
   return ((u_long)mppp + cticks);
}



#endif   /* USE_PPP */
/* end of file ppp_port.c */


⌨️ 快捷键说明

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