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-09-22 00:46:56 +00:00
|
|
|
* $Id: main.c,v 1.80 1997/09/21 20:26:46 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 <signal.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>
|
1997-09-04 00:38:22 +00:00
|
|
|
#include <sysexits.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"
|
1997-05-26 00:44:10 +00:00
|
|
|
#include "loadalias.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#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"
|
1997-03-13 12:45:35 +00:00
|
|
|
#include "sig.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "server.h"
|
1997-08-21 16:21:39 +00:00
|
|
|
#include "lcpproto.h"
|
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();
|
1997-05-10 01:22:19 +00:00
|
|
|
extern void AsyncInput();
|
1997-08-25 00:29:32 +00:00
|
|
|
extern int SelectSystem();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
static void DoLoop(void);
|
1995-09-02 17:20:54 +00:00
|
|
|
static void TerminalStop();
|
1997-05-19 02:00:16 +00:00
|
|
|
static char *ex_desc();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +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-12-22 17:29:33 +00:00
|
|
|
static pid_t BGPid = 0;
|
1997-05-10 03:39:57 +00:00
|
|
|
static char pid_filename[MAXPATHLEN];
|
|
|
|
static char if_filename[MAXPATHLEN];
|
1997-05-04 02:39:04 +00:00
|
|
|
int tunno;
|
1997-06-29 13:54:31 +00:00
|
|
|
static int dial_up;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static void
|
1997-06-23 23:10:13 +00:00
|
|
|
TtyInit(int DontWantInt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct termios newtio;
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
stat = fcntl(0, F_GETFL, 0);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (stat > 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
stat |= O_NONBLOCK;
|
|
|
|
(void) fcntl(0, F_SETFL, stat);
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
newtio = oldtio;
|
1997-08-25 00:29:32 +00:00
|
|
|
newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
|
1995-01-31 06:29:58 +00:00
|
|
|
newtio.c_iflag = 0;
|
|
|
|
newtio.c_oflag &= ~OPOST;
|
|
|
|
newtio.c_cc[VEOF] = _POSIX_VDISABLE;
|
1997-06-23 23:10:13 +00:00
|
|
|
if (DontWantInt)
|
|
|
|
newtio.c_cc[VINTR] = _POSIX_VDISABLE;
|
1995-01-31 06:29:58 +00:00
|
|
|
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
|
1997-08-25 00:29:32 +00:00
|
|
|
TtyCommandMode(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);
|
1997-08-25 00:29:32 +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);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (stat > 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
stat |= O_NONBLOCK;
|
|
|
|
(void) fcntl(0, F_SETFL, stat);
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
TermMode = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
if (prompt)
|
|
|
|
Prompt();
|
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);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (stat > 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
stat &= ~O_NONBLOCK;
|
|
|
|
(void) fcntl(0, F_SETFL, stat);
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
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);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (stat > 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
stat &= ~O_NONBLOCK;
|
|
|
|
(void) fcntl(0, F_SETFL, stat);
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(0, TCSANOW, &oldtio);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
Cleanup(int excode)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
|
|
|
OsLinkdown();
|
1995-01-31 06:29:58 +00:00
|
|
|
OsCloseLink(1);
|
|
|
|
sleep(1);
|
1997-05-19 02:00:16 +00:00
|
|
|
if (mode & MODE_AUTO)
|
1997-03-13 21:22:09 +00:00
|
|
|
DeleteIfRoutes(1);
|
1997-08-25 00:29:32 +00:00
|
|
|
(void) unlink(pid_filename);
|
|
|
|
(void) unlink(if_filename);
|
1995-01-31 06:29:58 +00:00
|
|
|
OsInterfaceDown(1);
|
1997-03-13 21:22:09 +00:00
|
|
|
if (mode & MODE_BACKGROUND && BGFiledes[1] != -1) {
|
|
|
|
char c = EX_ERRDEAD;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
|
|
|
if (write(BGFiledes[1], &c, 1) == 1)
|
|
|
|
LogPrintf(LogPHASE, "Parent notified of failure.\n");
|
1997-03-13 21:22:09 +00:00
|
|
|
else
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogPHASE, "Failed to notify parent of failure.\n");
|
1997-03-13 21:22:09 +00:00
|
|
|
close(BGFiledes[1]);
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
|
1995-01-31 06:29:58 +00:00
|
|
|
LogClose();
|
1997-06-25 19:30:05 +00:00
|
|
|
ServerClose();
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyOldMode();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
exit(excode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
CloseConnection(int signo)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-06-23 23:10:13 +00:00
|
|
|
/* NOTE, these are manual, we've done a setsid() */
|
1997-07-01 21:31:28 +00:00
|
|
|
LogPrintf(LogPHASE, "Caught signal %d, abort connection\n", signo);
|
1997-08-25 00:29:32 +00:00
|
|
|
reconnectState = RECON_FALSE;
|
1997-08-25 01:52:11 +00:00
|
|
|
reconnectCount = 0;
|
|
|
|
DownConnection();
|
1997-06-29 13:54:31 +00:00
|
|
|
dial_up = FALSE;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
CloseSession(int signo)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
if (BGPid) {
|
|
|
|
kill(BGPid, SIGINT);
|
|
|
|
exit(EX_TERM);
|
|
|
|
}
|
|
|
|
LogPrintf(LogPHASE, "Signal %d, terminate.\n", signo);
|
|
|
|
reconnect(RECON_FALSE);
|
|
|
|
LcpClose();
|
|
|
|
Cleanup(EX_TERM);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1995-09-02 17:20:54 +00:00
|
|
|
static void
|
|
|
|
TerminalCont()
|
|
|
|
{
|
1997-03-13 12:45:35 +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
|
1997-08-25 00:29:32 +00:00
|
|
|
TerminalStop(int signo)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_signal(SIGCONT, TerminalCont);
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyOldMode();
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_signal(SIGTSTP, SIG_DFL);
|
1995-09-02 17:20:54 +00:00
|
|
|
kill(getpid(), signo);
|
|
|
|
}
|
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
SetUpServer(int signo)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
|
|
|
int res;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
|
|
|
if ((res = ServerTcpOpen(SERVER_PORT + tunno)) != 0)
|
1997-09-04 00:38:22 +00:00
|
|
|
LogPrintf(LogERROR, "SIGUSR1: Failed %d to open port %d\n",
|
|
|
|
res, SERVER_PORT + tunno);
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
1997-05-19 02:00:16 +00:00
|
|
|
static char *
|
|
|
|
ex_desc(int ex)
|
|
|
|
{
|
|
|
|
static char num[12];
|
1997-08-25 00:29:32 +00:00
|
|
|
static char *desc[] = {"normal", "start", "sock",
|
1997-05-19 02:00:16 +00:00
|
|
|
"modem", "dial", "dead", "done", "reboot", "errdead",
|
1997-08-25 00:29:32 +00:00
|
|
|
"hangup", "term", "nodial", "nologin"};
|
1997-05-19 02:00:16 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
if (ex >= 0 && ex < sizeof(desc) / sizeof(*desc))
|
1997-05-19 02:00:16 +00:00
|
|
|
return desc[ex];
|
|
|
|
snprintf(num, sizeof num, "%d", ex);
|
|
|
|
return num;
|
|
|
|
}
|
1995-09-02 17:20:54 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
|
|
|
Usage()
|
|
|
|
{
|
1996-12-03 21:38:52 +00:00
|
|
|
fprintf(stderr,
|
1997-08-25 00:29:32 +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)
|
1997-08-25 00:29:32 +00:00
|
|
|
mode |= MODE_BACKGROUND | MODE_AUTO;
|
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)
|
1997-08-25 00:29:32 +00:00
|
|
|
mode |= MODE_DDIAL | MODE_AUTO;
|
1996-12-12 14:39:47 +00:00
|
|
|
else if (strcmp(cp, "alias") == 0) {
|
1997-05-26 00:44:10 +00:00
|
|
|
if (loadAliasHandlers(&VarAliasHandlers) == 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
mode |= MODE_ALIAS;
|
1997-05-26 00:44:10 +00:00
|
|
|
else
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogWARN, "Cannot load alias library\n");
|
|
|
|
optc--; /* this option isn't exclusive */
|
|
|
|
} else
|
1995-01-31 06:29:58 +00:00
|
|
|
Usage();
|
|
|
|
optc++;
|
1997-08-25 00:29:32 +00:00
|
|
|
argv++;
|
|
|
|
argc--;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (argc > 1) {
|
|
|
|
fprintf(stderr, "specify only one system label.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (argc == 1)
|
|
|
|
dstsystem = *argv;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (optc > 1) {
|
|
|
|
fprintf(stderr, "specify only one mode.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Greetings()
|
|
|
|
{
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm) {
|
|
|
|
fprintf(VarTerm, "User Process PPP. Written by Toshiharu OHNO.\n");
|
|
|
|
fflush(VarTerm);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
int
|
1997-08-25 00:29:32 +00:00
|
|
|
main(int argc, char **argv)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-05-11 10:23:16 +00:00
|
|
|
FILE *lockfile;
|
1997-06-09 03:27:43 +00:00
|
|
|
char *name;
|
|
|
|
|
1997-06-11 03:57:51 +00:00
|
|
|
VarTerm = 0;
|
1997-06-09 03:27:43 +00:00
|
|
|
name = rindex(argv[0], '/');
|
1997-08-25 00:29:32 +00:00
|
|
|
LogOpen(name ? name + 1 : argv[0]);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-01-31 06:29:58 +00:00
|
|
|
mode = MODE_INTER; /* default operation is interactive mode */
|
1997-06-25 19:30:05 +00:00
|
|
|
netfd = modem = tun_in = -1;
|
|
|
|
server = -2;
|
1995-01-31 06:29:58 +00:00
|
|
|
ProcessArgs(argc, argv);
|
1997-09-04 00:38:22 +00:00
|
|
|
if (!(mode & MODE_DIRECT)) {
|
|
|
|
if (getuid() != 0) {
|
|
|
|
fprintf(stderr, "You may only run ppp in client mode as user id 0\n");
|
|
|
|
LogClose();
|
|
|
|
return EX_NOPERM;
|
|
|
|
}
|
1997-06-11 03:57:51 +00:00
|
|
|
VarTerm = stdout;
|
1997-09-04 00:38:22 +00:00
|
|
|
}
|
1997-06-11 03:57:51 +00:00
|
|
|
Greetings();
|
1995-01-31 06:29:58 +00:00
|
|
|
GetUid();
|
|
|
|
IpcpDefAddress();
|
1997-09-04 00:38:22 +00:00
|
|
|
LocalAuthInit();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
if (SelectSystem("default", CONFFILE) < 0 && VarTerm)
|
|
|
|
fprintf(VarTerm, "Warning: No default entry is given in config file.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (OpenTunnel(&tunno) < 0) {
|
1997-06-25 19:30:05 +00:00
|
|
|
LogPrintf(LogWARN, "open_tun: %s\n", strerror(errno));
|
|
|
|
return EX_START;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (mode & (MODE_AUTO | MODE_DIRECT | MODE_DEDICATED))
|
1995-01-31 06:29:58 +00:00
|
|
|
mode &= ~MODE_INTER;
|
|
|
|
if (mode & MODE_INTER) {
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "Interactive mode\n");
|
1997-06-16 19:59:41 +00:00
|
|
|
netfd = STDOUT_FILENO;
|
1995-01-31 06:29:58 +00:00
|
|
|
} else if (mode & MODE_AUTO) {
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "Automatic Dialer mode\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
if (dstsystem == NULL) {
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm)
|
1997-08-25 00:29:32 +00:00
|
|
|
fprintf(VarTerm, "Destination system must be specified in"
|
|
|
|
" auto, background or ddial mode.\n");
|
1997-06-25 19:30:05 +00:00
|
|
|
return EX_START;
|
1996-12-22 17:29:33 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
tcgetattr(0, &oldtio); /* Save original tty mode */
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-07-01 21:31:28 +00:00
|
|
|
pending_signal(SIGHUP, CloseSession);
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_signal(SIGTERM, CloseSession);
|
1997-07-01 21:31:28 +00:00
|
|
|
pending_signal(SIGINT, CloseConnection);
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_signal(SIGQUIT, CloseSession);
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGPIPE
|
1997-04-09 17:35:54 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_signal(SIGALRM, SIG_IGN);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1997-08-25 00:29:32 +00:00
|
|
|
if (mode & MODE_INTER) {
|
1995-09-02 17:20:54 +00:00
|
|
|
#ifdef SIGTSTP
|
1997-06-25 19:30:05 +00:00
|
|
|
pending_signal(SIGTSTP, TerminalStop);
|
1995-09-02 17:20:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
1997-06-25 19:30:05 +00:00
|
|
|
pending_signal(SIGTTIN, TerminalStop);
|
1995-09-02 17:20:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
1997-06-25 19:30:05 +00:00
|
|
|
pending_signal(SIGTTOU, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
if (mode != MODE_INTER)
|
|
|
|
pending_signal(SIGUSR1, SetUpServer);
|
1995-09-02 17:20:54 +00:00
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (dstsystem) {
|
|
|
|
if (SelectSystem(dstsystem, CONFFILE) < 0) {
|
1997-06-11 03:57:51 +00:00
|
|
|
LogPrintf(LogWARN, "Destination system not found in conf file.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
Cleanup(EX_START);
|
|
|
|
}
|
|
|
|
if ((mode & MODE_AUTO) && DefHisAddress.ipaddr.s_addr == INADDR_ANY) {
|
1997-06-11 03:57:51 +00:00
|
|
|
LogPrintf(LogWARN, "Must specify dstaddr with"
|
1997-08-25 00:29:32 +00:00
|
|
|
" auto, background or ddial mode.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
Cleanup(EX_START);
|
|
|
|
}
|
|
|
|
}
|
1997-05-19 02:00:16 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
if (!(mode & MODE_INTER)) {
|
1996-12-22 17:29:33 +00:00
|
|
|
if (mode & MODE_BACKGROUND) {
|
1997-08-25 00:29:32 +00:00
|
|
|
if (pipe(BGFiledes)) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "pipe: %s\n", strerror(errno));
|
1996-12-22 17:29:33 +00:00
|
|
|
Cleanup(EX_SOCK);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-06-25 19:30:05 +00:00
|
|
|
/* Create server socket and listen. */
|
1997-09-04 00:38:22 +00:00
|
|
|
if (server == -2)
|
|
|
|
ServerTcpOpen(SERVER_PORT + tunno);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (!(mode & MODE_DIRECT)) {
|
1996-12-22 17:29:33 +00:00
|
|
|
pid_t bgpid;
|
1995-10-08 14:57:32 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
bgpid = fork();
|
1996-12-22 17:29:33 +00:00
|
|
|
if (bgpid == -1) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "fork: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
Cleanup(EX_SOCK);
|
1996-12-22 17:29:33 +00:00
|
|
|
}
|
|
|
|
if (bgpid) {
|
|
|
|
char c = EX_NORMAL;
|
|
|
|
|
|
|
|
if (mode & MODE_BACKGROUND) {
|
|
|
|
/* Wait for our child to close its pipe before we exit. */
|
|
|
|
BGPid = bgpid;
|
1997-08-25 00:29:32 +00:00
|
|
|
close(BGFiledes[1]);
|
1997-05-19 02:00:16 +00:00
|
|
|
if (read(BGFiledes[0], &c, 1) != 1) {
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "Child exit, no status.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogPHASE, "Parent: Child exit, no status.\n");
|
1997-05-19 02:00:16 +00:00
|
|
|
} else if (c == EX_NORMAL) {
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "PPP enabled.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogPHASE, "Parent: PPP enabled.\n");
|
1997-05-19 02:00:16 +00:00
|
|
|
} else {
|
1997-08-25 00:29:32 +00:00
|
|
|
fprintf(VarTerm, "Child failed (%s).\n", ex_desc((int) c));
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "Parent: Child failed (%s).\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
ex_desc((int) c));
|
|
|
|
}
|
|
|
|
close(BGFiledes[0]);
|
1996-12-22 17:29:33 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return c;
|
1997-03-13 21:22:09 +00:00
|
|
|
} else if (mode & MODE_BACKGROUND)
|
1997-08-25 00:29:32 +00:00
|
|
|
close(BGFiledes[0]);
|
1997-05-11 10:23:16 +00:00
|
|
|
}
|
1997-05-10 03:39:57 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
VarTerm = 0; /* We know it's currently stdout */
|
1997-09-18 00:15:25 +00:00
|
|
|
close(1);
|
1997-06-16 13:24:59 +00:00
|
|
|
close(2);
|
1997-06-11 03:57:51 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DOTTYINIT
|
1997-08-25 00:29:32 +00:00
|
|
|
if (mode & (MODE_DIRECT | MODE_DEDICATED))
|
1995-01-31 06:29:58 +00:00
|
|
|
#else
|
1997-06-16 13:24:59 +00:00
|
|
|
if (mode & MODE_DIRECT)
|
1995-01-31 06:29:58 +00:00
|
|
|
#endif
|
1997-06-23 23:10:13 +00:00
|
|
|
TtyInit(1);
|
1997-06-16 13:24:59 +00:00
|
|
|
else {
|
|
|
|
setsid();
|
1997-09-18 00:15:25 +00:00
|
|
|
close(0);
|
1997-06-16 13:24:59 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1997-06-23 23:10:13 +00:00
|
|
|
TtyInit(0);
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-09-22 00:46:56 +00:00
|
|
|
|
|
|
|
snprintf(pid_filename, sizeof(pid_filename), "%stun%d.pid",
|
|
|
|
_PATH_VARRUN, tunno);
|
|
|
|
(void) unlink(pid_filename);
|
|
|
|
|
|
|
|
if ((lockfile = fopen(pid_filename, "w")) != NULL) {
|
|
|
|
fprintf(lockfile, "%d\n", (int) getpid());
|
|
|
|
fclose(lockfile);
|
|
|
|
} else
|
|
|
|
LogPrintf(LogALERT, "Warning: Can't create %s: %s\n",
|
|
|
|
pid_filename, strerror(errno));
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "PPP Started.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
do
|
1997-08-25 00:29:32 +00:00
|
|
|
DoLoop();
|
1995-01-31 06:29:58 +00:00
|
|
|
while (mode & MODE_DEDICATED);
|
|
|
|
|
|
|
|
Cleanup(EX_DONE);
|
1997-06-25 19:30:05 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "PacketMode: Not connected.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
AsyncInit();
|
|
|
|
VjInit();
|
|
|
|
LcpInit();
|
|
|
|
IpcpInit();
|
|
|
|
CcpInit();
|
|
|
|
LcpUp();
|
|
|
|
|
1997-05-17 16:08:48 +00:00
|
|
|
LcpOpen(VarOpenMode);
|
1997-08-25 00:29:32 +00:00
|
|
|
if ((mode & (MODE_INTER | MODE_AUTO)) == MODE_INTER) {
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(1);
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm) {
|
|
|
|
fprintf(VarTerm, "Packet mode.\n");
|
|
|
|
aft_cmd = 1;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ShowHelp()
|
|
|
|
{
|
1997-06-24 21:25:06 +00:00
|
|
|
fprintf(stderr, "The following commands are available:\r\n");
|
|
|
|
fprintf(stderr, " ~p\tEnter Packet mode\r\n");
|
|
|
|
fprintf(stderr, " ~-\tDecrease log level\r\n");
|
|
|
|
fprintf(stderr, " ~+\tIncrease log level\r\n");
|
|
|
|
fprintf(stderr, " ~t\tShow timers (only in \"log debug\" mode)\r\n");
|
|
|
|
fprintf(stderr, " ~m\tShow memory map (only in \"log debug\" mode)\r\n");
|
|
|
|
fprintf(stderr, " ~.\tTerminate program\r\n");
|
|
|
|
fprintf(stderr, " ~?\tThis help\r\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReadTty()
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char ch;
|
|
|
|
static int ttystate;
|
1997-06-09 03:27:43 +00:00
|
|
|
FILE *oVarTerm;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#define MAXLINESIZE 200
|
|
|
|
char linebuff[MAXLINESIZE];
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "termode = %d, netfd = %d, mode = %d\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
TermMode, netfd, mode);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (!TermMode) {
|
1997-08-25 00:29:32 +00:00
|
|
|
n = read(netfd, linebuff, sizeof(linebuff) - 1);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (n > 0) {
|
1997-06-09 03:27:43 +00:00
|
|
|
aft_cmd = 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
DecodeCommand(linebuff, n, 1);
|
1995-02-26 12:18:08 +00:00
|
|
|
} else {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "client connection closed.\n");
|
1997-04-09 17:35:54 +00:00
|
|
|
VarLocalAuth = LOCAL_NO_AUTH;
|
1997-06-09 03:27:43 +00:00
|
|
|
mode &= ~MODE_INTER;
|
|
|
|
oVarTerm = VarTerm;
|
|
|
|
VarTerm = 0;
|
|
|
|
if (oVarTerm && oVarTerm != stdout)
|
1997-08-25 00:29:32 +00:00
|
|
|
fclose(oVarTerm);
|
1995-01-31 06:29:58 +00:00
|
|
|
close(netfd);
|
|
|
|
netfd = -1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* We are in terminal mode, decode special sequences
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-06-09 03:27:43 +00:00
|
|
|
n = read(fileno(VarTerm), &ch, 1);
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
switch (ttystate) {
|
|
|
|
case 0:
|
|
|
|
if (ch == '~')
|
|
|
|
ttystate++;
|
|
|
|
else
|
|
|
|
write(modem, &ch, n);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (ch) {
|
|
|
|
case '?':
|
|
|
|
ShowHelp();
|
|
|
|
break;
|
|
|
|
case 'p':
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* XXX: Should check carrier.
|
|
|
|
*/
|
|
|
|
if (LcpFsm.state <= ST_CLOSED) {
|
|
|
|
VarOpenMode = OPEN_ACTIVE;
|
|
|
|
PacketMode();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
TermMode = 1;
|
1997-06-09 03:27:43 +00:00
|
|
|
aft_cmd = 1;
|
1995-09-02 17:20:54 +00:00
|
|
|
TtyCommandMode(1);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
1997-06-09 03:27:43 +00:00
|
|
|
case 't':
|
|
|
|
if (LogIsKept(LogDEBUG)) {
|
|
|
|
ShowTimers();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'm':
|
|
|
|
if (LogIsKept(LogDEBUG)) {
|
|
|
|
ShowMemMap();
|
|
|
|
break;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
default:
|
|
|
|
if (write(modem, &ch, n) < 0)
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogERROR, "error writing to modem.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
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 *
|
1997-08-25 00:29:32 +00:00
|
|
|
HdlcDetect(u_char * cp, int n)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1995-02-26 12:18:08 +00:00
|
|
|
char *ptr, *fp, **hp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
cp[n] = '\0'; /* be sure to null terminated */
|
1995-01-31 06:29:58 +00:00
|
|
|
ptr = NULL;
|
|
|
|
for (hp = FrameHeaders; *hp; hp++) {
|
1995-02-26 12:18:08 +00:00
|
|
|
fp = *hp;
|
|
|
|
if (DEV_IS_SYNC)
|
|
|
|
fp++;
|
1997-08-25 00:29:32 +00:00
|
|
|
ptr = strstr((char *) cp, fp);
|
1996-01-11 17:48:59 +00:00
|
|
|
if (ptr)
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return ((u_char *) ptr);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pppTimer RedialTimer;
|
|
|
|
|
|
|
|
static void
|
|
|
|
RedialTimeout()
|
|
|
|
{
|
|
|
|
StopTimer(&RedialTimer);
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "Redialing timer expired.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
StartRedialTimer(int Timeout)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
StopTimer(&RedialTimer);
|
1995-10-08 14:57:32 +00:00
|
|
|
|
1997-04-14 23:48:20 +00:00
|
|
|
if (Timeout) {
|
1995-10-08 14:57:32 +00:00
|
|
|
RedialTimer.state = TIMER_STOPPED;
|
|
|
|
|
1997-04-14 23:48:20 +00:00
|
|
|
if (Timeout > 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
RedialTimer.load = Timeout * SECTICKS;
|
1995-10-08 14:57:32 +00:00
|
|
|
else
|
1997-08-25 00:29:32 +00:00
|
|
|
RedialTimer.load = (random() % REDIAL_PERIOD) * SECTICKS;
|
1995-10-08 14:57:32 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "Enter pause (%d) for redialing.\n",
|
1997-04-14 23:48:20 +00:00
|
|
|
RedialTimer.load / SECTICKS);
|
|
|
|
|
1995-10-08 14:57:32 +00:00
|
|
|
RedialTimer.func = RedialTimeout;
|
|
|
|
StartTimer(&RedialTimer);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
DoLoop()
|
|
|
|
{
|
|
|
|
fd_set rfds, wfds, efds;
|
1997-03-10 06:21:02 +00:00
|
|
|
int pri, i, n, wfd, nfds;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct sockaddr_in hisaddr;
|
|
|
|
struct timeval timeout, *tp;
|
|
|
|
int ssize = sizeof(hisaddr);
|
|
|
|
u_char *cp;
|
|
|
|
u_char rbuff[MAX_MRU];
|
1995-10-08 14:57:32 +00:00
|
|
|
int tries;
|
1995-07-08 17:46:56 +00:00
|
|
|
int qlen;
|
1997-06-23 23:10:13 +00:00
|
|
|
int res;
|
1995-09-02 17:20:54 +00:00
|
|
|
pid_t pgroup;
|
|
|
|
|
|
|
|
pgroup = getpgrp();
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-05-19 02:00:16 +00:00
|
|
|
if (mode & MODE_DIRECT) {
|
1997-06-11 03:57:51 +00:00
|
|
|
LogPrintf(LogDEBUG, "Opening modem\n");
|
1997-09-16 23:15:16 +00:00
|
|
|
if (OpenModem(mode) < 0)
|
|
|
|
return;
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "Packet mode enabled\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
PacketMode();
|
|
|
|
} else if (mode & MODE_DEDICATED) {
|
1997-03-10 06:21:02 +00:00
|
|
|
if (modem < 0)
|
1997-09-16 23:15:16 +00:00
|
|
|
while (OpenModem(mode) < 0)
|
|
|
|
sleep(VarReconnectTimer);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
fflush(VarTerm);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1995-03-11 15:18:55 +00:00
|
|
|
timeout.tv_sec = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
timeout.tv_usec = 0;
|
1997-05-24 17:32:42 +00:00
|
|
|
reconnectState = RECON_UNKNOWN;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-03-13 21:22:09 +00:00
|
|
|
if (mode & MODE_BACKGROUND)
|
1997-08-25 00:29:32 +00:00
|
|
|
dial_up = TRUE; /* Bring the line up */
|
1997-03-13 21:22:09 +00:00
|
|
|
else
|
1997-08-25 00:29:32 +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 (;;) {
|
1997-03-10 06:21:02 +00:00
|
|
|
nfds = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&efds);
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
/*
|
|
|
|
* If the link is down and we're in DDIAL mode, bring it back up.
|
1996-12-03 21:38:52 +00:00
|
|
|
*/
|
|
|
|
if (mode & MODE_DDIAL && LcpFsm.state <= ST_CLOSED)
|
1997-08-25 00:29:32 +00:00
|
|
|
dial_up = TRUE;
|
1996-12-03 21:38:52 +00:00
|
|
|
|
1997-04-21 01:02:02 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* If we lost carrier and want to re-establish the connection due to the
|
|
|
|
* "set reconnect" value, we'd better bring the line back up.
|
1997-04-21 01:02:02 +00:00
|
|
|
*/
|
1997-05-19 02:00:16 +00:00
|
|
|
if (LcpFsm.state <= ST_CLOSED) {
|
1997-05-24 17:32:42 +00:00
|
|
|
if (dial_up != TRUE && reconnectState == RECON_TRUE) {
|
1997-08-25 00:29:32 +00:00
|
|
|
if (++reconnectCount <= VarReconnectTries) {
|
|
|
|
LogPrintf(LogPHASE, "Connection lost, re-establish (%d/%d)\n",
|
|
|
|
reconnectCount, VarReconnectTries);
|
1997-05-19 02:00:16 +00:00
|
|
|
StartRedialTimer(VarReconnectTimer);
|
1997-08-25 00:29:32 +00:00
|
|
|
dial_up = TRUE;
|
|
|
|
} else {
|
|
|
|
if (VarReconnectTries)
|
|
|
|
LogPrintf(LogPHASE, "Connection lost, maximum (%d) times\n",
|
|
|
|
VarReconnectTries);
|
|
|
|
reconnectCount = 0;
|
|
|
|
if (mode & MODE_BACKGROUND)
|
|
|
|
Cleanup(EX_DEAD);
|
|
|
|
}
|
|
|
|
reconnectState = RECON_ENVOKED;
|
1997-05-14 01:18:51 +00:00
|
|
|
}
|
1997-05-19 02:00:16 +00:00
|
|
|
}
|
1997-04-21 01:02:02 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
/*
|
|
|
|
* If Ip packet for output is enqueued and require dial up, Just do it!
|
|
|
|
*/
|
|
|
|
if (dial_up && RedialTimer.state != TIMER_RUNNING) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "going to dial: modem = %d\n", modem);
|
1997-09-16 23:15:16 +00:00
|
|
|
if (OpenModem(mode) < 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
tries++;
|
|
|
|
if (!(mode & MODE_DDIAL) && VarDialTries)
|
|
|
|
LogPrintf(LogCHAT, "Failed to open modem (attempt %u of %d)\n",
|
|
|
|
tries, VarDialTries);
|
|
|
|
else
|
|
|
|
LogPrintf(LogCHAT, "Failed to open modem (attempt %u)\n", tries);
|
1997-06-11 03:57:51 +00:00
|
|
|
|
1997-06-17 01:46:05 +00:00
|
|
|
if (!(mode & MODE_DDIAL) && VarDialTries && tries >= VarDialTries) {
|
1997-06-11 03:57:51 +00:00
|
|
|
if (mode & MODE_BACKGROUND)
|
1997-08-25 00:29:32 +00:00
|
|
|
Cleanup(EX_DIAL); /* Can't get the modem */
|
1997-06-11 03:57:51 +00:00
|
|
|
dial_up = FALSE;
|
1997-08-25 00:29:32 +00:00
|
|
|
reconnectState = RECON_UNKNOWN;
|
|
|
|
reconnectCount = 0;
|
1997-06-11 03:57:51 +00:00
|
|
|
tries = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
} else
|
1997-06-11 03:57:51 +00:00
|
|
|
StartRedialTimer(VarRedialTimeout);
|
1995-10-08 14:57:32 +00:00
|
|
|
} else {
|
1997-08-25 00:29:32 +00:00
|
|
|
tries++; /* Tries are per number, not per list of
|
|
|
|
* numbers. */
|
|
|
|
if (!(mode & MODE_DDIAL) && VarDialTries)
|
1997-06-17 01:46:05 +00:00
|
|
|
LogPrintf(LogCHAT, "Dial attempt %u of %d\n", tries, VarDialTries);
|
1997-08-25 00:29:32 +00:00
|
|
|
else
|
|
|
|
LogPrintf(LogCHAT, "Dial attempt %u\n", tries);
|
1997-06-17 01:46:05 +00:00
|
|
|
|
1997-06-23 23:10:13 +00:00
|
|
|
if ((res = DialModem()) == EX_DONE) {
|
1997-08-25 00:29:32 +00:00
|
|
|
sleep(1); /* little pause to allow peer starts */
|
1995-10-08 14:57:32 +00:00
|
|
|
ModemTimeout();
|
|
|
|
PacketMode();
|
|
|
|
dial_up = FALSE;
|
1997-08-25 00:29:32 +00:00
|
|
|
reconnectState = RECON_UNKNOWN;
|
1995-10-08 14:57:32 +00:00
|
|
|
tries = 0;
|
|
|
|
} else {
|
|
|
|
CloseModem();
|
1997-04-13 00:54:45 +00:00
|
|
|
if (mode & MODE_BACKGROUND) {
|
1997-06-23 23:10:13 +00:00
|
|
|
if (VarNextPhone == NULL || res == EX_SIG)
|
1997-08-25 00:29:32 +00:00
|
|
|
Cleanup(EX_DIAL); /* Tried all numbers - no luck */
|
1997-04-13 00:54:45 +00:00
|
|
|
else
|
1997-04-14 23:48:20 +00:00
|
|
|
/* Try all numbers in background mode */
|
|
|
|
StartRedialTimer(VarRedialNextTimeout);
|
1997-06-23 23:10:13 +00:00
|
|
|
} else if (!(mode & MODE_DDIAL) &&
|
1997-08-25 00:29:32 +00:00
|
|
|
((VarDialTries && tries >= VarDialTries) ||
|
|
|
|
res == EX_SIG)) {
|
1997-04-12 22:58:41 +00:00
|
|
|
/* I give up ! Can't get through :( */
|
1997-04-14 23:48:20 +00:00
|
|
|
StartRedialTimer(VarRedialTimeout);
|
1997-04-12 22:58:41 +00:00
|
|
|
dial_up = FALSE;
|
1997-08-25 00:29:32 +00:00
|
|
|
reconnectState = RECON_UNKNOWN;
|
|
|
|
reconnectCount = 0;
|
1997-04-12 22:58:41 +00:00
|
|
|
tries = 0;
|
|
|
|
} else if (VarNextPhone == NULL)
|
|
|
|
/* Dial failed. Keep quite during redial wait period. */
|
1997-04-14 23:48:20 +00:00
|
|
|
StartRedialTimer(VarRedialTimeout);
|
1997-04-12 22:58:41 +00:00
|
|
|
else
|
1997-04-14 23:48:20 +00:00
|
|
|
StartRedialTimer(VarRedialNextTimeout);
|
1995-10-08 14:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if (modem >= 0) {
|
|
|
|
if (modem + 1 > nfds)
|
|
|
|
nfds = modem + 1;
|
1995-03-11 15:18:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if (server >= 0) {
|
|
|
|
if (server + 1 > nfds)
|
|
|
|
nfds = server + 1;
|
|
|
|
FD_SET(server, &rfds);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
/*
|
|
|
|
* *** 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-01-31 06:29:58 +00:00
|
|
|
*/
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef SIGALRM
|
1995-01-31 06:29:58 +00:00
|
|
|
usleep(TICKUNIT);
|
|
|
|
TimerService();
|
1997-03-13 12:45:35 +00:00
|
|
|
#else
|
|
|
|
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. */
|
1997-03-10 06:21:02 +00:00
|
|
|
if (qlen < 20 && tun_in >= 0) {
|
|
|
|
if (tun_in + 1 > nfds)
|
|
|
|
nfds = tun_in + 1;
|
1995-09-18 12:41:52 +00:00
|
|
|
FD_SET(tun_in, &rfds);
|
1997-03-10 06:21:02 +00:00
|
|
|
}
|
|
|
|
if (netfd >= 0) {
|
|
|
|
if (netfd + 1 > nfds)
|
|
|
|
nfds = netfd + 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
FD_SET(netfd, &rfds);
|
|
|
|
FD_SET(netfd, &efds);
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef SIGALRM
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +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
|
|
|
*/
|
1997-08-25 00:29:32 +00:00
|
|
|
tp = (RedialTimer.state == TIMER_RUNNING) ? &timeout : NULL;
|
1997-03-10 06:21:02 +00:00
|
|
|
i = select(nfds, &rfds, &wfds, &efds, tp);
|
1995-02-26 12:18:08 +00:00
|
|
|
#else
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-05-30 03:57:47 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* When SIGALRM timer is running, a select function will be return -1 and
|
|
|
|
* EINTR after a Time Service signal hundler 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;
|
1997-03-10 06:21:02 +00:00
|
|
|
i = select(nfds, &rfds, &wfds, &efds, tp);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1997-01-29 01:27:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
if (i == 0) {
|
|
|
|
continue;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (i < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
handle_signals();
|
|
|
|
continue;
|
|
|
|
}
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
break;
|
1995-05-30 03:57:47 +00:00
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if ((netfd >= 0 && FD_ISSET(netfd, &efds)) || (modem >= 0 && FD_ISSET(modem, &efds))) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogALERT, "Exception detected.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if (server >= 0 && FD_ISSET(server, &rfds)) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "connected to client.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
wfd = accept(server, (struct sockaddr *) & hisaddr, &ssize);
|
1997-04-09 17:35:54 +00:00
|
|
|
if (wfd < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "DoLoop: accept(): %s\n", strerror(errno));
|
1997-04-09 17:35:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if (netfd >= 0) {
|
1995-01-31 06:29:58 +00:00
|
|
|
write(wfd, "already in use.\n", 16);
|
|
|
|
close(wfd);
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
netfd = wfd;
|
1997-06-09 03:27:43 +00:00
|
|
|
VarTerm = fdopen(netfd, "a+");
|
1995-01-31 06:29:58 +00:00
|
|
|
mode |= MODE_INTER;
|
|
|
|
Greetings();
|
|
|
|
(void) IsInteractive();
|
1997-05-10 01:22:19 +00:00
|
|
|
Prompt();
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if ((mode & MODE_INTER) && (netfd >= 0 && 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();
|
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
if (modem >= 0) {
|
1995-01-31 06:29:58 +00:00
|
|
|
if (FD_ISSET(modem, &wfds)) { /* ready to write into modem */
|
1997-08-25 00:29:32 +00:00
|
|
|
ModemStartOutput(modem);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
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
|
1997-08-25 00:29:32 +00:00
|
|
|
LogDumpBuff(LogASYNC, "ReadFromModem", rbuff, n);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (LcpFsm.state <= ST_CLOSED) {
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* In dedicated mode, we just discard input until LCP is started.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
if (!(mode & MODE_DEDICATED)) {
|
|
|
|
cp = HdlcDetect(rbuff, n);
|
|
|
|
if (cp) {
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* LCP packet is detected. Turn ourselves into packet mode.
|
|
|
|
*/
|
|
|
|
if (cp != rbuff) {
|
1997-08-25 00:29:32 +00:00
|
|
|
write(modem, rbuff, cp - rbuff);
|
|
|
|
write(modem, "\r\n", 2);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
PacketMode();
|
|
|
|
} else
|
1997-06-09 03:27:43 +00:00
|
|
|
write(fileno(VarTerm), rbuff, n);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n > 0)
|
|
|
|
AsyncInput(rbuff, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (tun_in >= 0 && FD_ISSET(tun_in, &rfds)) { /* something to read
|
|
|
|
* from tun */
|
1995-01-31 06:29:58 +00:00
|
|
|
n = read(tun_in, rbuff, sizeof(rbuff));
|
|
|
|
if (n < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "read from tun: %s\n", strerror(errno));
|
1995-01-31 06:29:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (((struct ip *) rbuff)->ip_dst.s_addr == IpcpInfo.want_ipaddr.s_addr) {
|
1997-08-21 16:21:39 +00:00
|
|
|
/* we've been asked to send something addressed *to* us :( */
|
|
|
|
if (VarLoopback) {
|
|
|
|
pri = PacketCheck(rbuff, n, FL_IN);
|
|
|
|
if (pri >= 0) {
|
|
|
|
struct mbuf *bp;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1997-08-21 16:21:39 +00:00
|
|
|
if (mode & MODE_ALIAS) {
|
|
|
|
VarPacketAliasIn(rbuff, sizeof rbuff);
|
1997-08-25 00:29:32 +00:00
|
|
|
n = ntohs(((struct ip *) rbuff)->ip_len);
|
1997-08-21 16:21:39 +00:00
|
|
|
}
|
|
|
|
bp = mballoc(n, MB_IPIN);
|
|
|
|
bcopy(rbuff, MBUF_CTOP(bp), n);
|
|
|
|
IpInput(bp);
|
|
|
|
LogPrintf(LogDEBUG, "Looped back packet addressed to myself\n");
|
|
|
|
}
|
|
|
|
continue;
|
1997-08-25 00:29:32 +00:00
|
|
|
} else
|
1997-08-21 16:21:39 +00:00
|
|
|
LogPrintf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* Process on-demand dialup. Output packets are queued within tunnel
|
|
|
|
* device until IPCP is opened.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
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) {
|
1997-05-26 00:44:10 +00:00
|
|
|
VarPacketAliasOut(rbuff, sizeof rbuff);
|
1997-08-25 00:29:32 +00:00
|
|
|
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);
|
1997-08-25 00:29:32 +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) {
|
1997-08-25 00:29:32 +00:00
|
|
|
if (mode & MODE_ALIAS) {
|
|
|
|
VarPacketAliasOut(rbuff, sizeof rbuff);
|
|
|
|
n = ntohs(((struct ip *) rbuff)->ip_len);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "Job (DoLoop) done.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|