📄 login.c
字号:
/*
* FILENAME: login.c
*
* Copyright 1997- 2001 By InterNiche Technologies Inc. All rights reserved
* Copyright 1996 by NetPort Software.
*
* Login script parser. This allows the netport TCP/IP stack code to
* exec a variety of commands over a comm link after the link is up (i.e.
* modem connects) but before PPP or SLIP starts up.
*
* MODULE: MODEM
*
* ROUTINES: login(), logserver(), do_script(), log_input(),
* ROUTINES: log_output(),
*
* PORTABLE: yes
*/
#include "mdmport.h"
#ifdef USE_MODEM /* whole file can be ifdeffed out */
#ifdef USE_LOGINSCRIPT
/*#endif USE_LOGINSCRIPT (yaxon del) */
/* scratch local buffer for file reading */
#define LINELENGTH 180
static char linebuf[LINELENGTH];
int log_input(MODEMP, char * string, int count);
void log_output(MODEMP, char * string);
int do_script(MODEMP, char * sfilename);
/* FUNCTION: login()
*
* login() - called by the link control code (e.g. the dialer)
* when a link connection has been established. This executes a login
* script if required, else just marks loggedin == TRUE;
*
* PARAM1: MODEMP modemp
*
* RETURNS: void
*/
int loggedin = FALSE;
void
login(MODEMP modemp)
{
if(pppcfg.loginfile[0] <= ' ') /* no login file specified */
{
/* Discard the characters which may be coming in fropm the
* modem, or else once PPP gets started it reads the chars as
* response to its requests to the PPP peer and as a result
* gets confused
*/
while (modem_getc(modemp, 1) != -1);
loggedin = TRUE; /* just mark this TRUE for now */
return;
}
if(do_script(modemp, pppcfg.loginfile) == 0)
loggedin = TRUE;
else
loggedin = FALSE;
}
/* FUNCTION: logserver()
*
* This is called by dialer to handle logging on
* users who dial in.
*
* For now, this just runs a script. May get smarter later about
* multiple users, execing TTYs, etc.
*
* Leaves loggedin set to TRUE or FALSE.
*
* PARAM1: MODEMP modemp
*
* RETURNS: void
*/
void
logserver(MODEMP modemp)
{
if(pppcfg.logservefile[0] <= ' ') /* no login server file specified */
{
loggedin = TRUE; /* just mark this TRUE for now */
return;
}
if(do_script(modemp, pppcfg.logservefile) == 0)
loggedin = TRUE;
else
loggedin = FALSE;
}
/* FUNCTION: do_script()
*
* do_script(filename) - engine to open a text file and treat it
* as a script for logging in to a host we have just dialed into.
*
* The script file provides a user configurable script to log into
* various hosts. It contains text for output strings to the modem,
* and text for expected replys from the modem; including timeouts.
*
* Special characters (when at start of line)
* # - Comment character
*
* Reserved words:
*
* input secs string - get characters from modem until string is matched
* or secs seconds has elapsed. If secs is reached before string is found,
* the script is aborted. string should be UPPERCASE.
*
* output string - send string to modem
*
* echo - send text to user console (or log)
*
* verbose (=on | =off) - select verbose mode
*
* PARAM1: MODEMP modemp
* PARAM2: char * sfilename
*
* RETURNS: 0 if OK else, non-zero error code.
*/
int
do_script(MODEMP modemp, char * sfilename)
{
FILE * fp; /* file with script text */
int line; /* line number of script file currently being processed */
int tmpnum; /* line's numeric parameter */
char * tmpstring; /* line's string parameter */
char * cp; /* scrtach char pointer */
char * err = NULL; /* script file error, if any */
fp = vfopen(sfilename, "r");
if(!fp)
{
//ConPrintf("Unable to open script file name \"%s\"\n", sfilename);
return -1;
}
for(line = 1; ; line++)
{
#ifdef INCLUDE_NVPARMS
if(nv_fgets(linebuf, LINELENGTH, fp) != linebuf)
break;
#else
if(fgets(linebuf, LINELENGTH, fp) != linebuf)
break;
#endif /* INCLUDE_NVPARMS */
if(linebuf[0] == '#')
continue;
//ConPrintf("script: %s\n", linebuf);
/* Make sure this is one of the reserved words */
if(strncmp(linebuf, "input", 5) == 0)
{
cp = nextarg(linebuf);
tmpnum = atoi(cp); /* should be max seconds */
if(!tmpnum)
{
err = "no timeout on input";
break;
}
tmpstring = cp = nextarg(cp); /* should be string to match */
if(!(*cp))
{
err = "input needs string";
break;
}
/* don't pass unprintable chars to input */
while(*cp >= ' ') /* find 1st unprintable char */
cp++;
*cp = 0; /* NULL terminate tmpstring */
if(log_input(modemp, tmpstring, tmpnum) != TRUE)
{
err = "unexpected/missing input";
break;
}
}
else if(strncmp(linebuf, "output", 6) == 0)
{
cp = nextarg(linebuf);
if(!(*cp))
{
err = "output cmd needs string";
break;
}
log_output(modemp, cp);
}
else if(strncmp(linebuf, "echo", 4) == 0)
{
//ConPrintf("login echo: %s\n", linebuf+6);
}
else
{
//ConPrintf("login: unknown format: %s\n", linebuf);
}
}
vfclose(fp);
if(err)
{
//ConPrintf("script error in file %s, line %u: %s\n", sfilename, line, err);
return -1;
}
return 0; /* good return - no error */
}
/* FUNCTION: log_input()
*
* Get string from modem and check that it matches
* the passed string.
*
* PARAM1: MODEMP modemp
* PARAM2: char * string
* PARAM3: int secs
*
* RETURNS: TRUE if string was matched within secs seconds,
* else returns FALSE. Leaves input string in modem_in[].
*/
int
log_input(MODEMP modemp, char * string, int secs)
{
u_long timeout;
int c; /* character storage, may also be 16 bit -1 */
int i; /* index */
timeout = cticks + (u_long)(secs) * TPS;
while(cticks <= timeout)
{
/* read line of text from connected host */
for(i = 0; i < MODEM_BUF_SIZE-1; i++)
{
c = modem_getc(modemp, 1); /* give uart 1 sec to get next char */
if(c == -1) /* quit if uart buffer is empty */
break;
/* if what we are getting from the other end appears to be an end
* of line, terminate this loop so we can do the string compare below.
*/
if ((c == 0x0d) || (c == 0x0a))
break;
modemp->modem_in[i] = (u_char)c; /* save uart char in modem buffer */
}
/* i is 0 if no chars came from uart, else i is line length */
modemp->modem_in[i] = 0; /* null terminate modem string */
if(i)
;//ConPrintf("log_input: %s\n", modemp->modem_in);
if(strstr((char*)(modemp->modem_in), string) != NULL) /* found pattern */
return TRUE;
}
return FALSE;
}
/* FUNCTION: log_output()
*
* PARAM1: MODEMP modemp
* PARAM2: char * string
*
* RETURNS: void
*/
void
log_output(MODEMP modemp, char * string)
{
int modem_unit = modemp->unit;
char c;
//ConPrintf("log_output: %s\n", string);
while(*string >= ' ') /* only send visable text */
{
if(uart_ready(modem_unit))
{
c = *string++; /* get next char from string */
if(c == '\\') /* If char is escaped, do conversion */
{
c = *string++; /* get next char from string */
switch (c)
{
case 'n': /* newline */
c = 10;
break;
case 'r': /* return */
c = 13;
break;
case 't': /* Tab */
c = 9;
break;
default: /* All other chars pass thru */
break;
}
}
uart_putc(modem_unit, c);
}
else
tk_yield();
}
}
#endif /* USE_LOGINSCRIPT */ /* (yaxon del) */
#endif /* USE_MODEM */
/* end of file login.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -