1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* User Process PPP
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1997-03-08 10:04:21 +00:00
|
|
|
* $Id: main.c,v 1.33 1997/02/25 14:05:03 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
* o Add commands for traffic summary, version display, etc.
|
|
|
|
* o Add signal handler for misc controls.
|
|
|
|
*/
|
|
|
|
#include "fsm.h"
|
|
|
|
#include <fcntl.h>
|
1995-10-08 14:57:32 +00:00
|
|
|
#include <paths.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <termios.h>
|
1997-02-25 14:05:17 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <signal.h>
|
1997-02-19 01:14:41 +00:00
|
|
|
#include "sig.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
1996-10-07 04:21:09 +00:00
|
|
|
#include <unistd.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
1996-12-12 14:39:47 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "modem.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "hdlc.h"
|
1996-01-11 17:48:59 +00:00
|
|
|
#include "ccp.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "lcp.h"
|
|
|
|
#include "ipcp.h"
|
|
|
|
#include "vars.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
#include "auth.h"
|
1995-03-11 15:18:55 +00:00
|
|
|
#include "filter.h"
|
1996-01-11 17:48:59 +00:00
|
|
|
#include "systems.h"
|
|
|
|
#include "ip.h"
|
1996-12-12 14:39:47 +00:00
|
|
|
#include "alias.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
|
1995-02-27 10:57:54 +00:00
|
|
|
#define LAUTH_M1 "Warning: No password entry for this host in ppp.secret\n"
|
1995-07-06 02:58:57 +00:00
|
|
|
#define LAUTH_M2 "Warning: All manipulation is allowed by anyone in the world\n"
|
1995-02-27 10:57:54 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
extern void VjInit(), AsyncInit();
|
|
|
|
extern void AsyncInput(), IpOutput();
|
|
|
|
extern int SelectSystem();
|
|
|
|
|
|
|
|
extern void DecodeCommand(), Prompt();
|
1996-10-12 16:20:34 +00:00
|
|
|
extern int aft_cmd;
|
1995-01-31 06:29:58 +00:00
|
|
|
extern int IsInteractive();
|
|
|
|
extern struct in_addr ifnetmask;
|
|
|
|
static void DoLoop(void);
|
1995-09-02 17:20:54 +00:00
|
|
|
static void TerminalStop();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static struct termios oldtio; /* Original tty mode */
|
|
|
|
static struct termios comtio; /* Command level tty mode */
|
1996-03-08 09:03:09 +00:00
|
|
|
int TermMode;
|
1996-01-10 21:28:04 +00:00
|
|
|
static int server;
|
1996-12-22 17:29:33 +00:00
|
|
|
static pid_t BGPid = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct sockaddr_in ifsin;
|
1995-10-08 14:57:32 +00:00
|
|
|
char pid_filename[128];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
TtyInit()
|
|
|
|
{
|
|
|
|
struct termios newtio;
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
stat = fcntl(0, F_GETFL, 0);
|
|
|
|
stat |= O_NONBLOCK;
|
|
|
|
fcntl(0, F_SETFL, stat);
|
|
|
|
newtio = oldtio;
|
|
|
|
newtio.c_lflag &= ~(ECHO|ISIG|ICANON);
|
|
|
|
newtio.c_iflag = 0;
|
|
|
|
newtio.c_oflag &= ~OPOST;
|
|
|
|
newtio.c_cc[VEOF] = _POSIX_VDISABLE;
|
|
|
|
newtio.c_cc[VINTR] = _POSIX_VDISABLE;
|
|
|
|
newtio.c_cc[VMIN] = 1;
|
|
|
|
newtio.c_cc[VTIME] = 0;
|
|
|
|
newtio.c_cflag |= CS8;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(0, TCSADRAIN, &newtio);
|
1995-01-31 06:29:58 +00:00
|
|
|
comtio = newtio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set tty into command mode. We allow canonical input and echo processing.
|
|
|
|
*/
|
1995-09-02 17:20:54 +00:00
|
|
|
void
|
|
|
|
TtyCommandMode(prompt)
|
|
|
|
int prompt;
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct termios newtio;
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
if (!(mode & MODE_INTER))
|
|
|
|
return;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(0, &newtio);
|
1995-09-02 17:20:54 +00:00
|
|
|
newtio.c_lflag |= (ECHO|ISIG|ICANON);
|
1995-01-31 06:29:58 +00:00
|
|
|
newtio.c_iflag = oldtio.c_iflag;
|
|
|
|
newtio.c_oflag |= OPOST;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(0, TCSADRAIN, &newtio);
|
1995-01-31 06:29:58 +00:00
|
|
|
stat = fcntl(0, F_GETFL, 0);
|
|
|
|
stat |= O_NONBLOCK;
|
|
|
|
fcntl(0, F_SETFL, stat);
|
|
|
|
TermMode = 0;
|
1995-09-02 17:20:54 +00:00
|
|
|
if(prompt) Prompt(0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set tty into terminal mode which is used while we invoke term command.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TtyTermMode()
|
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(0, TCSADRAIN, &comtio);
|
1995-01-31 06:29:58 +00:00
|
|
|
stat = fcntl(0, F_GETFL, 0);
|
|
|
|
stat &= ~O_NONBLOCK;
|
|
|
|
fcntl(0, F_SETFL, stat);
|
|
|
|
TermMode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyOldMode()
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
stat = fcntl(0, F_GETFL, 0);
|
|
|
|
stat &= ~O_NONBLOCK;
|
|
|
|
fcntl(0, F_SETFL, stat);
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(0, TCSANOW, &oldtio);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cleanup(excode)
|
|
|
|
int excode;
|
|
|
|
{
|
|
|
|
|
|
|
|
OsLinkdown();
|
1995-01-31 06:29:58 +00:00
|
|
|
OsCloseLink(1);
|
|
|
|
sleep(1);
|
1995-10-08 14:57:32 +00:00
|
|
|
if (mode & MODE_AUTO) {
|
1995-01-31 06:29:58 +00:00
|
|
|
DeleteIfRoutes(1);
|
1996-12-22 17:29:33 +00:00
|
|
|
}
|
|
|
|
if (mode & (MODE_AUTO | MODE_BACKGROUND)) {
|
1995-10-08 14:57:32 +00:00
|
|
|
unlink(pid_filename);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
OsInterfaceDown(1);
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "PPP Terminated.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
LogClose();
|
|
|
|
if (server > 0)
|
|
|
|
close(server);
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyOldMode();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
exit(excode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1996-03-30 18:27:45 +00:00
|
|
|
Hangup(signo)
|
|
|
|
int signo;
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1996-07-09 17:40:36 +00:00
|
|
|
if (signo == SIGSEGV) {
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Signal %d, core dump.\n", signo);
|
|
|
|
LogClose();
|
|
|
|
abort();
|
|
|
|
}
|
1996-12-22 17:29:33 +00:00
|
|
|
if (BGPid) {
|
|
|
|
kill (BGPid, SIGHUP);
|
|
|
|
exit (EX_HANGUP);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Signal %d, hangup.\n", signo);
|
|
|
|
Cleanup(EX_HANGUP);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1996-03-30 18:27:45 +00:00
|
|
|
CloseSession(signo)
|
|
|
|
int signo;
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1996-12-22 17:29:33 +00:00
|
|
|
if (BGPid) {
|
|
|
|
kill (BGPid, SIGINT);
|
|
|
|
exit (EX_TERM);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Signal %d, terminate.\n", signo);
|
|
|
|
LcpClose();
|
|
|
|
Cleanup(EX_TERM);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1995-09-02 17:20:54 +00:00
|
|
|
static void
|
|
|
|
TerminalCont()
|
|
|
|
{
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGCONT, SIG_DFL);
|
|
|
|
pending_signal(SIGTSTP, TerminalStop);
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(getpgrp() == tcgetpgrp(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TerminalStop(signo)
|
|
|
|
int signo;
|
|
|
|
{
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGCONT, TerminalCont);
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyOldMode();
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGTSTP, SIG_DFL);
|
1995-09-02 17:20:54 +00:00
|
|
|
kill(getpid(), signo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
|
|
|
Usage()
|
|
|
|
{
|
1996-12-03 21:38:52 +00:00
|
|
|
fprintf(stderr,
|
1996-12-22 17:29:33 +00:00
|
|
|
"Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ] [ -alias ] [system]\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessArgs(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int optc;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
optc = 0;
|
|
|
|
while (argc > 0 && **argv == '-') {
|
|
|
|
cp = *argv + 1;
|
|
|
|
if (strcmp(cp, "auto") == 0)
|
|
|
|
mode |= MODE_AUTO;
|
1996-12-22 17:29:33 +00:00
|
|
|
else if (strcmp(cp, "background") == 0)
|
|
|
|
mode |= MODE_BACKGROUND;
|
1995-01-31 06:29:58 +00:00
|
|
|
else if (strcmp(cp, "direct") == 0)
|
|
|
|
mode |= MODE_DIRECT;
|
|
|
|
else if (strcmp(cp, "dedicated") == 0)
|
|
|
|
mode |= MODE_DEDICATED;
|
1996-12-03 21:38:52 +00:00
|
|
|
else if (strcmp(cp, "ddial") == 0)
|
|
|
|
mode |= MODE_DDIAL|MODE_AUTO;
|
1996-12-12 14:39:47 +00:00
|
|
|
else if (strcmp(cp, "alias") == 0) {
|
|
|
|
mode |= MODE_ALIAS;
|
|
|
|
optc--; /* this option isn't exclusive */
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
else
|
|
|
|
Usage();
|
|
|
|
optc++;
|
|
|
|
argv++; argc--;
|
|
|
|
}
|
|
|
|
if (argc > 1) {
|
|
|
|
fprintf(stderr, "specify only one system label.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
if (argc == 1) dstsystem = *argv;
|
|
|
|
|
|
|
|
if (optc > 1) {
|
|
|
|
fprintf(stderr, "specify only one mode.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Greetings()
|
|
|
|
{
|
|
|
|
printf("User Process PPP. Written by Toshiharu OHNO.\r\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
int tunno;
|
|
|
|
|
|
|
|
argc--; argv++;
|
|
|
|
|
|
|
|
mode = MODE_INTER; /* default operation is interactive mode */
|
|
|
|
netfd = -1;
|
|
|
|
ProcessArgs(argc, argv);
|
|
|
|
Greetings();
|
|
|
|
GetUid();
|
|
|
|
IpcpDefAddress();
|
1996-12-12 14:39:47 +00:00
|
|
|
InitAlias();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (SelectSystem("default", CONFFILE) < 0) {
|
|
|
|
fprintf(stderr, "Warning: No default entry is given in config file.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LogOpen())
|
|
|
|
exit(EX_START);
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
switch ( LocalAuthInit() ) {
|
|
|
|
case NOT_FOUND:
|
1995-02-27 10:57:54 +00:00
|
|
|
fprintf(stderr,LAUTH_M1);
|
|
|
|
fprintf(stderr,LAUTH_M2);
|
|
|
|
fflush (stderr);
|
|
|
|
/* Fall down */
|
|
|
|
case VALID:
|
1995-02-26 12:18:08 +00:00
|
|
|
VarLocalAuth = LOCAL_AUTH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (OpenTunnel(&tunno) < 0) {
|
|
|
|
perror("open_tun");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
1996-12-22 17:29:33 +00:00
|
|
|
if (mode & (MODE_AUTO|MODE_DIRECT|MODE_DEDICATED|MODE_BACKGROUND))
|
1995-01-31 06:29:58 +00:00
|
|
|
mode &= ~MODE_INTER;
|
|
|
|
if (mode & MODE_INTER) {
|
|
|
|
printf("Interactive mode\n");
|
|
|
|
netfd = 0;
|
|
|
|
} else if (mode & MODE_AUTO) {
|
1996-12-03 21:38:52 +00:00
|
|
|
printf("Automatic Dialer mode\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
if (dstsystem == NULL) {
|
1996-12-03 21:38:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Destination system must be specified in auto or ddial mode.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
1996-12-22 17:29:33 +00:00
|
|
|
} else if (mode & MODE_BACKGROUND) {
|
|
|
|
printf("Background mode\n");
|
|
|
|
if (dstsystem == NULL) {
|
|
|
|
fprintf(stderr, "Destination system must be specified in background mode.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(0, &oldtio); /* Save original tty mode */
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGHUP, Hangup);
|
1997-03-08 10:04:21 +00:00
|
|
|
signal(SIGTERM, CloseSession);
|
|
|
|
signal(SIGINT, CloseSession);
|
|
|
|
signal(SIGQUIT, CloseSession);
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGSEGV
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGSEGV, Hangup);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGPIPE, Hangup);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
1997-03-08 10:04:21 +00:00
|
|
|
signal(SIGALRM, SIG_IGN);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1995-09-02 17:20:54 +00:00
|
|
|
if(mode & MODE_INTER)
|
|
|
|
{
|
|
|
|
#ifdef SIGTSTP
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGTSTP, TerminalStop);
|
1995-09-02 17:20:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGTTIN, TerminalStop);
|
1995-09-02 17:20:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
1997-02-19 01:14:41 +00:00
|
|
|
pending_signal(SIGTTOU, SIG_IGN);
|
1995-09-02 17:20:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (dstsystem) {
|
|
|
|
if (SelectSystem(dstsystem, CONFFILE) < 0) {
|
|
|
|
fprintf(stderr, "Destination system not found in conf file.\n");
|
|
|
|
Cleanup(EX_START);
|
|
|
|
}
|
|
|
|
if ((mode & MODE_AUTO) && DefHisAddress.ipaddr.s_addr == INADDR_ANY) {
|
1996-12-03 21:38:52 +00:00
|
|
|
fprintf(stderr, "Must specify dstaddr with auto or ddial mode.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
Cleanup(EX_START);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode & MODE_DIRECT)
|
|
|
|
printf("Packet mode enabled.\n");
|
|
|
|
|
|
|
|
#ifdef notdef
|
|
|
|
if (mode & MODE_AUTO) {
|
|
|
|
OsSetIpaddress(IpcpInfo.want_ipaddr, IpcpInfo.his_ipaddr, ifnetmask);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!(mode & MODE_INTER)) {
|
1996-12-22 17:29:33 +00:00
|
|
|
int port = SERVER_PORT + tunno;
|
|
|
|
if (mode & MODE_BACKGROUND) {
|
|
|
|
if (pipe (BGFiledes)) {
|
|
|
|
perror("pipe");
|
|
|
|
Cleanup(EX_SOCK);
|
|
|
|
}
|
|
|
|
server = -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1996-12-22 17:29:33 +00:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Create server socket and listen at there.
|
|
|
|
*/
|
|
|
|
server = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (server < 0) {
|
|
|
|
perror("socket");
|
|
|
|
Cleanup(EX_SOCK);
|
|
|
|
}
|
|
|
|
ifsin.sin_family = AF_INET;
|
|
|
|
ifsin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
ifsin.sin_port = htons(port);
|
|
|
|
if (bind(server, (struct sockaddr *) &ifsin, sizeof(ifsin)) < 0) {
|
|
|
|
perror("bind");
|
|
|
|
if (errno == EADDRINUSE)
|
|
|
|
fprintf(stderr, "Wait for a while, then try again.\n");
|
|
|
|
Cleanup(EX_SOCK);
|
|
|
|
}
|
|
|
|
listen(server, 5);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DupLog();
|
|
|
|
if (!(mode & MODE_DIRECT)) {
|
1995-10-08 14:57:32 +00:00
|
|
|
int fd;
|
|
|
|
char pid[32];
|
1996-12-22 17:29:33 +00:00
|
|
|
pid_t bgpid;
|
1995-10-08 14:57:32 +00:00
|
|
|
|
1996-12-22 17:29:33 +00:00
|
|
|
bgpid = fork ();
|
|
|
|
if (bgpid == -1) {
|
|
|
|
perror ("fork");
|
|
|
|
Cleanup (EX_SOCK);
|
|
|
|
}
|
|
|
|
if (bgpid) {
|
|
|
|
char c = EX_NORMAL;
|
|
|
|
|
|
|
|
if (mode & MODE_BACKGROUND) {
|
|
|
|
/* Wait for our child to close its pipe before we exit. */
|
|
|
|
BGPid = bgpid;
|
|
|
|
read (BGFiledes[0], &c, 1);
|
|
|
|
if (c == EX_NORMAL)
|
|
|
|
LogPrintf (LOG_CHAT, "PPP enabled.\n");
|
|
|
|
}
|
|
|
|
exit(c);
|
|
|
|
}
|
1995-10-08 14:57:32 +00:00
|
|
|
|
1996-12-22 17:29:33 +00:00
|
|
|
snprintf(pid_filename, sizeof (pid_filename), "%s/ppp.tun%d.pid",
|
|
|
|
_PATH_VARRUN, tunno);
|
1995-10-08 14:57:32 +00:00
|
|
|
unlink(pid_filename);
|
1997-01-10 07:53:28 +00:00
|
|
|
snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
|
1995-10-08 14:57:32 +00:00
|
|
|
|
|
|
|
if ((fd = open(pid_filename, O_RDWR|O_CREAT, 0666)) != -1)
|
|
|
|
{
|
|
|
|
write(fd, pid, strlen(pid));
|
|
|
|
close(fd);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1996-12-22 17:29:33 +00:00
|
|
|
if (server > 0)
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Listening at %d.\n", port);
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DOTTYINIT
|
1995-02-26 12:18:08 +00:00
|
|
|
if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */
|
1995-01-31 06:29:58 +00:00
|
|
|
#else
|
|
|
|
if (mode & MODE_DIRECT) {
|
|
|
|
#endif
|
|
|
|
TtyInit();
|
|
|
|
} else {
|
1996-03-09 08:18:41 +00:00
|
|
|
int fd;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
setsid(); /* detach control tty */
|
1996-03-09 08:18:41 +00:00
|
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
|
|
|
|
(void)dup2(fd, STDIN_FILENO);
|
|
|
|
(void)dup2(fd, STDOUT_FILENO);
|
|
|
|
(void)dup2(fd, STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
(void)close (fd);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server = -1;
|
|
|
|
TtyInit();
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "PPP Started.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
DoLoop();
|
|
|
|
while (mode & MODE_DEDICATED);
|
|
|
|
|
|
|
|
Cleanup(EX_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-12-22 17:29:33 +00:00
|
|
|
* Turn into packet mode, where we speak PPP.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
PacketMode()
|
|
|
|
{
|
|
|
|
if (RawModem(modem) < 0) {
|
|
|
|
fprintf(stderr, "Not connected.\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncInit();
|
|
|
|
VjInit();
|
|
|
|
LcpInit();
|
|
|
|
IpcpInit();
|
|
|
|
CcpInit();
|
|
|
|
LcpUp();
|
|
|
|
|
|
|
|
if (mode & (MODE_DIRECT|MODE_DEDICATED))
|
|
|
|
LcpOpen(OPEN_ACTIVE);
|
|
|
|
else
|
|
|
|
LcpOpen(VarOpenMode);
|
|
|
|
if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER) {
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(1);
|
1995-01-31 06:29:58 +00:00
|
|
|
fprintf(stderr, "Packet mode.\r\n");
|
1996-10-12 16:20:34 +00:00
|
|
|
aft_cmd = 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ShowHelp()
|
|
|
|
{
|
1995-09-02 17:20:54 +00:00
|
|
|
fprintf(stderr, "The following commands are available:\r\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
fprintf(stderr, " ~p\tEnter to Packet mode\r\n");
|
1996-03-08 09:03:09 +00:00
|
|
|
fprintf(stderr, " ~-\tDecrease log level\r\n");
|
|
|
|
fprintf(stderr, " ~+\tIncrease log level\r\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
fprintf(stderr, " ~.\tTerminate program\r\n");
|
1996-03-08 09:03:09 +00:00
|
|
|
fprintf(stderr, " ~?\tThis help\r\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReadTty()
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char ch;
|
|
|
|
static int ttystate;
|
|
|
|
#define MAXLINESIZE 200
|
|
|
|
char linebuff[MAXLINESIZE];
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("termode = %d, netfd = %d, mode = %d\n", TermMode, netfd, mode);
|
|
|
|
#endif
|
|
|
|
if (!TermMode) {
|
|
|
|
n = read(netfd, linebuff, sizeof(linebuff)-1);
|
1996-10-12 16:20:34 +00:00
|
|
|
aft_cmd = 1;
|
1995-02-26 12:18:08 +00:00
|
|
|
if (n > 0) {
|
1995-01-31 06:29:58 +00:00
|
|
|
DecodeCommand(linebuff, n, 1);
|
1995-02-26 12:18:08 +00:00
|
|
|
} else {
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("connection closed.\n");
|
|
|
|
#endif
|
|
|
|
close(netfd);
|
|
|
|
netfd = -1;
|
|
|
|
mode &= ~MODE_INTER;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are in terminal mode, decode special sequences
|
|
|
|
*/
|
|
|
|
n = read(0, &ch, 1);
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("got %d bytes\n", n);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
switch (ttystate) {
|
|
|
|
case 0:
|
|
|
|
if (ch == '~')
|
|
|
|
ttystate++;
|
|
|
|
else
|
|
|
|
write(modem, &ch, n);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (ch) {
|
|
|
|
case '?':
|
|
|
|
ShowHelp();
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (loglevel > 0) {
|
|
|
|
loglevel--;
|
|
|
|
fprintf(stderr, "New loglevel is %d\r\n", loglevel);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
loglevel++;
|
|
|
|
fprintf(stderr, "New loglevel is %d\r\n", loglevel);
|
|
|
|
break;
|
|
|
|
#ifdef DEBUG
|
|
|
|
case 'm':
|
|
|
|
ShowMemMap();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'p':
|
|
|
|
/*
|
|
|
|
* XXX: Should check carrier.
|
|
|
|
*/
|
|
|
|
if (LcpFsm.state <= ST_CLOSED) {
|
|
|
|
VarOpenMode = OPEN_ACTIVE;
|
|
|
|
PacketMode();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef DEBUG
|
|
|
|
case 't':
|
|
|
|
ShowTimers();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case '.':
|
|
|
|
TermMode = 1;
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(1);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (write(modem, &ch, n) < 0)
|
|
|
|
fprintf(stderr, "err in write.\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ttystate = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, we'll try to detect HDLC frame
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *FrameHeaders[] = {
|
1995-02-26 12:18:08 +00:00
|
|
|
"\176\377\003\300\041",
|
|
|
|
"\176\377\175\043\300\041",
|
|
|
|
"\176\177\175\043\100\041",
|
|
|
|
"\176\175\337\175\043\300\041",
|
|
|
|
"\176\175\137\175\043\100\041",
|
1995-01-31 06:29:58 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
HdlcDetect(cp, n)
|
|
|
|
u_char *cp;
|
|
|
|
int n;
|
|
|
|
{
|
1995-02-26 12:18:08 +00:00
|
|
|
char *ptr, *fp, **hp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
cp[n] = '\0'; /* be sure to null terminated */
|
|
|
|
ptr = NULL;
|
|
|
|
for (hp = FrameHeaders; *hp; hp++) {
|
1995-02-26 12:18:08 +00:00
|
|
|
fp = *hp;
|
|
|
|
if (DEV_IS_SYNC)
|
|
|
|
fp++;
|
1996-01-11 17:48:59 +00:00
|
|
|
ptr = strstr((char *)cp, fp);
|
|
|
|
if (ptr)
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return((u_char *)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pppTimer RedialTimer;
|
|
|
|
|
|
|
|
static void
|
|
|
|
RedialTimeout()
|
|
|
|
{
|
|
|
|
StopTimer(&RedialTimer);
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Redialing timer expired.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
StartRedialTimer()
|
|
|
|
{
|
|
|
|
StopTimer(&RedialTimer);
|
1995-10-08 14:57:32 +00:00
|
|
|
|
|
|
|
if (VarRedialTimeout) {
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Enter pause for redialing.\n");
|
1995-10-08 14:57:32 +00:00
|
|
|
RedialTimer.state = TIMER_STOPPED;
|
|
|
|
|
|
|
|
if (VarRedialTimeout > 0)
|
|
|
|
RedialTimer.load = VarRedialTimeout * SECTICKS;
|
|
|
|
else
|
|
|
|
RedialTimer.load = (random() % REDIAL_PERIOD) * SECTICKS;
|
|
|
|
|
|
|
|
RedialTimer.func = RedialTimeout;
|
|
|
|
StartTimer(&RedialTimer);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
DoLoop()
|
|
|
|
{
|
|
|
|
fd_set rfds, wfds, efds;
|
|
|
|
int pri, i, n, wfd;
|
|
|
|
struct sockaddr_in hisaddr;
|
|
|
|
struct timeval timeout, *tp;
|
|
|
|
int ssize = sizeof(hisaddr);
|
|
|
|
u_char *cp;
|
|
|
|
u_char rbuff[MAX_MRU];
|
1995-03-11 15:18:55 +00:00
|
|
|
int dial_up;
|
1995-10-08 14:57:32 +00:00
|
|
|
int tries;
|
1995-07-08 17:46:56 +00:00
|
|
|
int qlen;
|
1995-09-02 17:20:54 +00:00
|
|
|
pid_t pgroup;
|
|
|
|
|
|
|
|
pgroup = getpgrp();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (mode & MODE_DIRECT) {
|
|
|
|
modem = OpenModem(mode);
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Packet mode enabled\n");
|
1996-10-12 16:20:34 +00:00
|
|
|
fflush(stderr);
|
1995-01-31 06:29:58 +00:00
|
|
|
PacketMode();
|
|
|
|
} else if (mode & MODE_DEDICATED) {
|
|
|
|
if (!modem)
|
|
|
|
modem = OpenModem(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
1995-03-11 15:18:55 +00:00
|
|
|
timeout.tv_sec = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
1995-03-11 15:18:55 +00:00
|
|
|
dial_up = FALSE; /* XXXX */
|
1995-10-08 14:57:32 +00:00
|
|
|
tries = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
for (;;) {
|
|
|
|
FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1996-12-03 21:38:52 +00:00
|
|
|
/*
|
|
|
|
* If the link is down and we're in DDIAL mode, bring it back
|
|
|
|
* up.
|
|
|
|
*/
|
|
|
|
if (mode & MODE_DDIAL && LcpFsm.state <= ST_CLOSED)
|
|
|
|
dial_up = TRUE;
|
|
|
|
|
1995-03-11 15:18:55 +00:00
|
|
|
/*
|
|
|
|
* If Ip packet for output is enqueued and require dial up,
|
|
|
|
* Just do it!
|
|
|
|
*/
|
|
|
|
if ( dial_up && RedialTimer.state != TIMER_RUNNING ) { /* XXX */
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("going to dial: modem = %d\n", modem);
|
|
|
|
#endif
|
1995-10-08 14:57:32 +00:00
|
|
|
modem = OpenModem(mode);
|
|
|
|
if (modem < 0) {
|
|
|
|
modem = 0; /* Set intial value for next OpenModem */
|
|
|
|
StartRedialTimer();
|
|
|
|
} else {
|
|
|
|
tries++;
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_CHAT_BIT, "Dial attempt %u\n", tries);
|
1995-10-08 14:57:32 +00:00
|
|
|
if (DialModem()) {
|
|
|
|
sleep(1); /* little pause to allow peer starts */
|
|
|
|
ModemTimeout();
|
|
|
|
PacketMode();
|
|
|
|
dial_up = FALSE;
|
|
|
|
tries = 0;
|
|
|
|
} else {
|
|
|
|
CloseModem();
|
|
|
|
/* Dial failed. Keep quite during redial wait period. */
|
|
|
|
StartRedialTimer();
|
|
|
|
|
|
|
|
if (VarDialTries && tries >= VarDialTries) {
|
|
|
|
dial_up = FALSE;
|
|
|
|
tries = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
1995-07-08 17:46:56 +00:00
|
|
|
qlen = ModemQlen();
|
1996-01-30 11:08:50 +00:00
|
|
|
|
|
|
|
if (qlen == 0) {
|
|
|
|
IpStartOutput();
|
|
|
|
qlen = ModemQlen();
|
|
|
|
}
|
|
|
|
|
1995-03-11 15:18:55 +00:00
|
|
|
if (modem) {
|
|
|
|
FD_SET(modem, &rfds);
|
|
|
|
FD_SET(modem, &efds);
|
1995-07-08 17:46:56 +00:00
|
|
|
if (qlen > 0) {
|
1995-03-11 15:18:55 +00:00
|
|
|
FD_SET(modem, &wfds);
|
|
|
|
}
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (server > 0) FD_SET(server, &rfds);
|
|
|
|
|
|
|
|
/* *** IMPORTANT ***
|
|
|
|
*
|
|
|
|
* CPU is serviced every TICKUNIT micro seconds.
|
|
|
|
* This value must be chosen with great care. If this values is
|
|
|
|
* too big, it results loss of characters from modem and poor responce.
|
|
|
|
* If this values is too small, ppp process eats many CPU time.
|
|
|
|
*/
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef SIGALRM
|
1995-01-31 06:29:58 +00:00
|
|
|
usleep(TICKUNIT);
|
|
|
|
TimerService();
|
1997-01-30 00:49:50 +00:00
|
|
|
#else
|
1997-02-19 01:14:41 +00:00
|
|
|
handle_signals();
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1995-09-18 12:41:52 +00:00
|
|
|
|
|
|
|
/* If there are aren't many packets queued, look for some more. */
|
|
|
|
if (qlen < 20)
|
|
|
|
FD_SET(tun_in, &rfds);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (netfd > -1) {
|
|
|
|
FD_SET(netfd, &rfds);
|
|
|
|
FD_SET(netfd, &efds);
|
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef SIGALRM
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
1995-03-11 15:18:55 +00:00
|
|
|
* Normally, select() will not block because modem is writable.
|
|
|
|
* In AUTO mode, select will block until we find packet from tun
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
tp = (RedialTimer.state == TIMER_RUNNING)? &timeout : NULL;
|
|
|
|
i = select(tun_in+10, &rfds, &wfds, &efds, tp);
|
1995-02-26 12:18:08 +00:00
|
|
|
#else
|
1995-05-30 03:57:47 +00:00
|
|
|
/*
|
1995-03-11 15:18:55 +00:00
|
|
|
* When SIGALRM timer is running, a select function will be
|
1995-05-30 03:57:47 +00:00
|
|
|
* return -1 and EINTR after a Time Service signal hundler
|
1995-10-08 14:57:32 +00:00
|
|
|
* is done. If the redial timer is not running and we are
|
|
|
|
* trying to dial, poll with a 0 value timer.
|
1995-03-11 15:18:55 +00:00
|
|
|
*/
|
1995-10-08 14:57:32 +00:00
|
|
|
tp = (dial_up && RedialTimer.state != TIMER_RUNNING) ? &timeout : NULL;
|
|
|
|
i = select(tun_in+10, &rfds, &wfds, &efds, tp);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1997-01-29 01:27:58 +00:00
|
|
|
|
1995-03-11 15:18:55 +00:00
|
|
|
if ( i == 0 ) {
|
|
|
|
continue;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
|
1997-01-30 00:49:50 +00:00
|
|
|
if ( i < 0 ) {
|
|
|
|
if ( errno == EINTR ) {
|
1997-02-19 01:14:41 +00:00
|
|
|
handle_signals();
|
|
|
|
continue;
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
perror("select");
|
|
|
|
break;
|
1995-05-30 03:57:47 +00:00
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if ((netfd > 0 && FD_ISSET(netfd, &efds)) || FD_ISSET(modem, &efds)) {
|
|
|
|
logprintf("Exception detected.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server > 0 && FD_ISSET(server, &rfds)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("connected to client.\n");
|
|
|
|
#endif
|
|
|
|
wfd = accept(server, (struct sockaddr *)&hisaddr, &ssize);
|
|
|
|
if (netfd > 0) {
|
|
|
|
write(wfd, "already in use.\n", 16);
|
|
|
|
close(wfd);
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
netfd = wfd;
|
|
|
|
if (dup2(netfd, 1) < 0)
|
|
|
|
perror("dup2");
|
|
|
|
mode |= MODE_INTER;
|
|
|
|
Greetings();
|
1995-02-27 10:57:54 +00:00
|
|
|
switch ( LocalAuthInit() ) {
|
|
|
|
case NOT_FOUND:
|
|
|
|
fprintf(stdout,LAUTH_M1);
|
|
|
|
fprintf(stdout,LAUTH_M2);
|
|
|
|
fflush(stdout);
|
|
|
|
/* Fall down */
|
|
|
|
case VALID:
|
|
|
|
VarLocalAuth = LOCAL_AUTH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
(void) IsInteractive();
|
|
|
|
Prompt(0);
|
|
|
|
}
|
|
|
|
|
1995-09-02 17:20:54 +00:00
|
|
|
if ((mode & MODE_INTER) && FD_ISSET(netfd, &rfds) &&
|
1995-09-17 16:14:49 +00:00
|
|
|
((mode & MODE_AUTO) || pgroup == tcgetpgrp(0))) {
|
1995-01-31 06:29:58 +00:00
|
|
|
/* something to read from tty */
|
|
|
|
ReadTty();
|
|
|
|
}
|
|
|
|
if (modem) {
|
|
|
|
if (FD_ISSET(modem, &wfds)) { /* ready to write into modem */
|
|
|
|
ModemStartOutput(modem);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(modem, &rfds)) { /* something to read from modem */
|
1995-02-26 12:18:08 +00:00
|
|
|
if (LcpFsm.state <= ST_CLOSED)
|
|
|
|
usleep(10000);
|
1995-01-31 06:29:58 +00:00
|
|
|
n = read(modem, rbuff, sizeof(rbuff));
|
|
|
|
if ((mode & MODE_DIRECT) && n <= 0) {
|
|
|
|
DownConnection();
|
|
|
|
} else
|
|
|
|
LogDumpBuff(LOG_ASYNC, "ReadFromModem", rbuff, n);
|
|
|
|
|
|
|
|
if (LcpFsm.state <= ST_CLOSED) {
|
|
|
|
/*
|
|
|
|
* In dedicated mode, we just discard input until LCP is started.
|
|
|
|
*/
|
|
|
|
if (!(mode & MODE_DEDICATED)) {
|
|
|
|
cp = HdlcDetect(rbuff, n);
|
|
|
|
if (cp) {
|
|
|
|
/*
|
|
|
|
* LCP packet is detected. Turn ourselves into packet mode.
|
|
|
|
*/
|
|
|
|
if (cp != rbuff) {
|
|
|
|
write(1, rbuff, cp - rbuff);
|
|
|
|
write(1, "\r\n", 2);
|
|
|
|
}
|
|
|
|
PacketMode();
|
|
|
|
#ifdef notdef
|
|
|
|
AsyncInput(cp, n - (cp - rbuff));
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
write(1, rbuff, n);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n > 0)
|
|
|
|
AsyncInput(rbuff, n);
|
|
|
|
#ifdef notdef
|
|
|
|
continue; /* THIS LINE RESULT AS POOR PERFORMANCE */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (FD_ISSET(tun_in, &rfds)) { /* something to read from tun */
|
|
|
|
n = read(tun_in, rbuff, sizeof(rbuff));
|
|
|
|
if (n < 0) {
|
|
|
|
perror("read from tun");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Process on-demand dialup. Output packets are queued within tunnel
|
|
|
|
* device until IPCP is opened.
|
|
|
|
*/
|
|
|
|
if (LcpFsm.state <= ST_CLOSED && (mode & MODE_AUTO)) {
|
1995-03-11 15:18:55 +00:00
|
|
|
pri = PacketCheck(rbuff, n, FL_DIAL);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (pri >= 0) {
|
1996-12-12 14:39:47 +00:00
|
|
|
if (mode & MODE_ALIAS) {
|
1996-12-19 00:41:42 +00:00
|
|
|
PacketAliasOut((struct ip *)rbuff);
|
|
|
|
n = ntohs(((struct ip *)rbuff)->ip_len);
|
1996-12-12 14:39:47 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
IpEnqueue(pri, rbuff, n);
|
1996-12-12 14:39:47 +00:00
|
|
|
dial_up = TRUE; /* XXX */
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
pri = PacketCheck(rbuff, n, FL_OUT);
|
1996-12-12 14:39:47 +00:00
|
|
|
if (pri >= 0) {
|
|
|
|
if (mode & MODE_ALIAS) {
|
1996-12-19 00:41:42 +00:00
|
|
|
PacketAliasOut((struct ip *)rbuff);
|
|
|
|
n = ntohs(((struct ip *)rbuff)->ip_len);
|
1996-12-12 14:39:47 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
IpEnqueue(pri, rbuff, n);
|
1996-12-12 14:39:47 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
logprintf("job done.\n");
|
|
|
|
}
|