From a9c6b5df9ca4264b792b47e2ca0ab6a13fa71bc8 Mon Sep 17 00:00:00 2001 From: Atsushi Murai Date: Sun, 8 Oct 1995 14:57:32 +0000 Subject: [PATCH] 1. Add a settable redial timer and logging of the process id in a file. A settable redial timer helps to avoid the problem where both ends of a link want to dial at the same time and the line winds up busy for both ends. The process id is logged in /var/run/PPP.system where system is the name of the called system. When both ends of a link are running in demand dial mode, you need an easy way to get the pid of the ppp on the called end so it can be killed and re-started with -direct or pppd started to handle the incoming ppp session. 2. Add secret description for "set timeout" to man. Reviewed by: Atsushi Murai Submitted by: John Capo --- usr.sbin/ppp/command.c | 98 ++++++++++++++++++++++++++++++++++++------ usr.sbin/ppp/defs.h | 4 +- usr.sbin/ppp/main.c | 93 ++++++++++++++++++++++++++------------- usr.sbin/ppp/ppp.8 | 52 ++++++++++++++++++---- usr.sbin/ppp/ppp.8.m4 | 52 ++++++++++++++++++---- usr.sbin/ppp/vars.c | 6 +-- usr.sbin/ppp/vars.h | 6 ++- 7 files changed, 248 insertions(+), 63 deletions(-) diff --git a/usr.sbin/ppp/command.c b/usr.sbin/ppp/command.c index e7c005a0a050..4a21bb6a9b18 100644 --- a/usr.sbin/ppp/command.c +++ b/usr.sbin/ppp/command.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: command.c,v 1.7 1995/07/08 08:28:00 amurai Exp $ + * $Id: command.c,v 1.9 1995/09/02 17:20:50 amurai Exp $ * */ #include @@ -114,6 +114,8 @@ struct cmdtab *cmdlist; int argc; char **argv; { + int tries; + if (LcpFsm.state > ST_CLOSED) { printf("LCP state is [%s]\n", StateNames[LcpFsm.state]); return(1); @@ -126,17 +128,22 @@ char **argv; return(1); } } - modem = OpenModem(mode); - if (modem < 0) { - printf("failed to open modem.\n"); - modem = 0; - return(1); - } - if (DialModem()) { - sleep(1); - ModemTimeout(); - PacketMode(); - } + tries = 0; + do { + printf("Dial attempt %u\n", ++tries); + modem = OpenModem(mode); + if (modem < 0) { + printf("failed to open modem.\n"); + modem = 0; + break; + } + if (DialModem()) { + sleep(1); + ModemTimeout(); + PacketMode(); + break; + } + } while (VarDialTries == 0 || tries < VarDialTries); return(1); } @@ -321,6 +328,26 @@ static int ShowLogList() return(1); } +static int ShowRedial() +{ + printf(" Redial Timer: "); + + if (VarRedialTimeout >= 0) { + printf(" %d seconds, ", VarRedialTimeout); + } + else { + printf(" Random 0 - %d seconds, ", REDIAL_PERIOD); + } + + if (VarDialTries) + printf("%d dial tries", VarDialTries); + + printf("\n"); + + return(1); +} + + extern int ShowIfilter(), ShowOfilter(), ShowDfilter(), ShowAfilter(); struct cmdtab ShowCommands[] = { @@ -360,6 +387,8 @@ struct cmdtab ShowCommands[] = { "Show routing table", StrNull}, { "timeout", NULL, ShowTimeout, LOCAL_AUTH, "Show Idle timeout value", StrNull}, + { "redial", NULL, ShowRedial, LOCAL_AUTH, + "Show Redial timeout value", StrNull}, { "version", NULL, ShowVersion, LOCAL_NO_AUTH | LOCAL_AUTH, "Show version string", StrNull}, { "help", "?", HelpCommand, LOCAL_NO_AUTH | LOCAL_AUTH, @@ -556,6 +585,49 @@ char **argv; return(1); } +static int SetRedialTimeout(list, argc, argv) +struct cmdtab *list; +int argc; +char **argv; +{ + int timeout; + int tries; + + if (argc == 1 || argc == 2 ) { + if (strcasecmp(argv[0], "random") == 0) { + VarRedialTimeout = -1; + printf("Using random redial timeout.\n"); + srandom(time(0)); + } + else { + timeout = atoi(argv[0]); + + if (timeout >= 0) { + VarRedialTimeout = timeout; + } + else { + printf("invalid redial timeout\n"); + printf("Usage: %s %s\n", list->name, list->syntax); + } + } + if (argc == 2) { + tries = atoi(argv[1]); + + if (tries >= 0) { + VarDialTries = tries; + } + else { + printf("invalid retry value\n"); + printf("Usage: %s %s\n", list->name, list->syntax); + } + } + } + else { + printf("Usage: %s %s\n", list->name, list->syntax); + } + return(1); +} + static int SetModemParity(list, argc, argv) struct cmdtab *list; int argc; @@ -830,6 +902,8 @@ struct cmdtab SetCommands[] = { "Set modem speed", "speed"}, { "timeout", NULL, SetIdleTimeout, LOCAL_AUTH, "Set Idle timeout", StrValue}, + { "redial", NULL, SetRedialTimeout, LOCAL_AUTH, + "Set Redial timeout", "value|random [dial_attempts]"}, { "help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH, "Display this message", StrNull, (void *)SetCommands}, { NULL, NULL, NULL }, diff --git a/usr.sbin/ppp/defs.h b/usr.sbin/ppp/defs.h index b5dc08a64f61..c67971c59c36 100644 --- a/usr.sbin/ppp/defs.h +++ b/usr.sbin/ppp/defs.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: defs.h,v 1.2 1995/02/26 12:17:25 amurai Exp $ + * $Id: defs.h,v 1.3 1995/09/02 17:20:51 amurai Exp $ * * TODO: */ @@ -45,7 +45,7 @@ #define MODEM_SPEED B38400 /* tty speed */ #define SERVER_PORT 3000 /* Base server port no. */ -#define REDIAL_PERIOD 30 /* Hold time to redial */ +#define REDIAL_PERIOD 30 /* Default Hold time to redial */ #define CONFFILE "ppp.conf" #define LINKFILE "ppp.linkup" diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index 67030856de3c..42ef92ebfbf2 100644 --- a/usr.sbin/ppp/main.c +++ b/usr.sbin/ppp/main.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: main.c,v 1.9 1995/09/17 16:14:48 amurai Exp $ + * $Id: main.c,v 1.10 1995/09/18 12:41:52 bde Exp $ * * TODO: * o Add commands for traffic summary, version display, etc. @@ -25,6 +25,7 @@ */ #include "fsm.h" #include +#include #include #include #include @@ -66,6 +67,7 @@ static struct termios comtio; /* Command level tty mode */ static int TermMode; static int server, update; struct sockaddr_in ifsin; +char pid_filename[128]; static void TtyInit() @@ -148,8 +150,10 @@ int excode; OsLinkdown(); OsCloseLink(1); sleep(1); - if (mode & MODE_AUTO) + if (mode & MODE_AUTO) { DeleteIfRoutes(1); + unlink(pid_filename); + } OsInterfaceDown(1); LogPrintf(LOG_PHASE, "PPP Terminated.\n"); LogClose(); @@ -366,8 +370,22 @@ char **argv; DupLog(); if (!(mode & MODE_DIRECT)) { + int fd; + char pid[32]; + if (fork()) exit(0); + + snprintf(pid_filename, sizeof (pid_filename), "%s/PPP.%s", + _PATH_VARRUN, dstsystem); + unlink(pid_filename); + sprintf(pid, "%d\n", getpid()); + + if ((fd = open(pid_filename, O_RDWR|O_CREAT, 0666)) != -1) + { + write(fd, pid, strlen(pid)); + close(fd); + } } LogPrintf(LOG_PHASE, "Listening at %d.\n", port); #ifdef DOTTYINIT @@ -567,12 +585,20 @@ RedialTimeout() static void StartRedialTimer() { - LogPrintf(LOG_PHASE, "Enter pause for redialing.\n"); StopTimer(&RedialTimer); - RedialTimer.state = TIMER_STOPPED; - RedialTimer.load = REDIAL_PERIOD * SECTICKS; - RedialTimer.func = RedialTimeout; - StartTimer(&RedialTimer); + + if (VarRedialTimeout) { + LogPrintf(LOG_PHASE, "Enter pause for redialing.\n"); + RedialTimer.state = TIMER_STOPPED; + + if (VarRedialTimeout > 0) + RedialTimer.load = VarRedialTimeout * SECTICKS; + else + RedialTimer.load = (random() % REDIAL_PERIOD) * SECTICKS; + + RedialTimer.func = RedialTimeout; + StartTimer(&RedialTimer); + } } @@ -587,6 +613,7 @@ DoLoop() u_char *cp; u_char rbuff[MAX_MRU]; int dial_up; + int tries; int qlen; pid_t pgroup; @@ -603,13 +630,11 @@ DoLoop() fflush(stdout); -#ifdef SIGALRM timeout.tv_sec = 0; -#else timeout.tv_usec = 0; -#endif dial_up = FALSE; /* XXXX */ + tries = 0; for (;;) { if ( modem ) IpStartOutput(); @@ -623,22 +648,30 @@ DoLoop() #ifdef DEBUG logprintf("going to dial: modem = %d\n", modem); #endif - modem = OpenModem(mode); - if (modem < 0) { - modem = 0; /* Set intial value for next OpenModem */ - StartRedialTimer(); - } else { - if (DialModem()) { - sleep(1); /* little pause to allow peer starts */ - ModemTimeout(); - PacketMode(); - dial_up = FALSE; - } else { - CloseModem(); - /* Dial failed. Keep quite during redial wait period. */ - StartRedialTimer(); - } - } + modem = OpenModem(mode); + if (modem < 0) { + modem = 0; /* Set intial value for next OpenModem */ + StartRedialTimer(); + } else { + tries++; + LogPrintf(LOG_CHAT, "Dial attempt %u\n", tries); + 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; + } + } + } } qlen = ModemQlen(); if (modem) { @@ -682,9 +715,11 @@ DoLoop() /* * When SIGALRM timer is running, a select function will be * return -1 and EINTR after a Time Service signal hundler - * is done. + * is done. If the redial timer is not running and we are + * trying to dial, poll with a 0 value timer. */ - i = select(tun_in+10, &rfds, &wfds, &efds, NULL); + tp = (dial_up && RedialTimer.state != TIMER_RUNNING) ? &timeout : NULL; + i = select(tun_in+10, &rfds, &wfds, &efds, tp); #endif if ( i == 0 ) { continue; @@ -692,7 +727,7 @@ DoLoop() if ( i < 0 ) { if ( errno == EINTR ) { - continue; /* Got SIGALRM, Do check a queue for dailing */ + continue; /* Got SIGALRM, Do check a queue for dialing */ } perror("select"); break; diff --git a/usr.sbin/ppp/ppp.8 b/usr.sbin/ppp/ppp.8 index 0647e97009bd..9a49587284e0 100644 --- a/usr.sbin/ppp/ppp.8 +++ b/usr.sbin/ppp/ppp.8 @@ -1,5 +1,5 @@ .\" manual page [] for ppp 0.94 beta2 + alpha -.\" $Id: ppp.8,v 1.10 1995/09/17 16:14:49 amurai Exp $ +.\" $Id: ppp.8,v 1.11 1995/09/24 18:15:14 nate Exp $ .Dd 20 September 1995 .Os FreeBSD .Dt PPP 8 @@ -314,10 +314,41 @@ daemon has an associated port number which is computed as "3000 + tunnel_device_number". If 3000 is not good base number, edit defs.h in the ppp sources ( .Pa /usr/src/usr.sbin/ppp ) -and recompile it. When an outgoing packet is detected, +and recompile it. + +When an outgoing packet is detected, .Nm -will perform the dialing action (chat script) and try to connect with -the peer. If dialing fails, it will wait for 30 seconds and retry. +will perform the dialing action (chat script) and try to connect +with the peer. + +If the connect fails, the default behaviour is to wait 30 seconds +and then attempt to connect when another outgoing packet is detected. +This behaviour can be changed with +.Bd -literal -offset indent +set redial seconds|random [dial_attempts] +.Ed +.Pp +Seconds is the number of seconds to wait before attempting +to connect again. If the argument is +.Sq random , +the delay period is a random value between 0 and 30 seconds. +.Sq dial_attempts +is the number of times to try to connect for each outgoing packet +that is received. The previous value is unchanged if this parameter +is omitted. +.Bd -literal -offset indent +set redial 10 4 +.Ed +.Pp +will attempt to connect 4 times for each outgoing packet that is +detected with a 10 second delay between each attempt. + +Modifying the dial delay is very useful when running +.Nm +in demand +dial mode on both ends of the link. If each end has the same timeout, +both ends wind up calling each other at the same time if the link +drops and both ends have packets queued. To terminate the program, type @@ -448,19 +479,21 @@ work with stdin and stdout. You can also telnet to port 3000 to get command mode control in the same manner as client-side .Nm . -.Sh SETTING IDLE TIMER +.Sh SETTING IDLE, LINE QUALITY REQUEST, RETRY TIMER To check/set idletimer, use the .Dq show timeout and -.Dq set timeout +.Dq set timeout [lqrtimer [retrytimer]] commands. Ex: .Dl ppp ON tama> set timeout 600 -The timeout period is measured in seconds, the default value for which -is 180 or 3 min. To disable the idle timer function, use the command +The timeout period is measured in seconds, the default values for which +are timeout = 180 or 3 min, lqrtimer = 30sec and retrytimer = 3sec. +To disable the idle timer function, +use the command .Dq set timeout 0 . In @@ -688,6 +721,9 @@ Logging and debugging information file. .It /var/spool/lock/Lck..* tty port locking file. +.It /var/run/PPP.system +Holds the pid for ppp -auto system. + .It /etc/services Get port number if port number is using service name. .El diff --git a/usr.sbin/ppp/ppp.8.m4 b/usr.sbin/ppp/ppp.8.m4 index 0647e97009bd..9a49587284e0 100644 --- a/usr.sbin/ppp/ppp.8.m4 +++ b/usr.sbin/ppp/ppp.8.m4 @@ -1,5 +1,5 @@ .\" manual page [] for ppp 0.94 beta2 + alpha -.\" $Id: ppp.8,v 1.10 1995/09/17 16:14:49 amurai Exp $ +.\" $Id: ppp.8,v 1.11 1995/09/24 18:15:14 nate Exp $ .Dd 20 September 1995 .Os FreeBSD .Dt PPP 8 @@ -314,10 +314,41 @@ daemon has an associated port number which is computed as "3000 + tunnel_device_number". If 3000 is not good base number, edit defs.h in the ppp sources ( .Pa /usr/src/usr.sbin/ppp ) -and recompile it. When an outgoing packet is detected, +and recompile it. + +When an outgoing packet is detected, .Nm -will perform the dialing action (chat script) and try to connect with -the peer. If dialing fails, it will wait for 30 seconds and retry. +will perform the dialing action (chat script) and try to connect +with the peer. + +If the connect fails, the default behaviour is to wait 30 seconds +and then attempt to connect when another outgoing packet is detected. +This behaviour can be changed with +.Bd -literal -offset indent +set redial seconds|random [dial_attempts] +.Ed +.Pp +Seconds is the number of seconds to wait before attempting +to connect again. If the argument is +.Sq random , +the delay period is a random value between 0 and 30 seconds. +.Sq dial_attempts +is the number of times to try to connect for each outgoing packet +that is received. The previous value is unchanged if this parameter +is omitted. +.Bd -literal -offset indent +set redial 10 4 +.Ed +.Pp +will attempt to connect 4 times for each outgoing packet that is +detected with a 10 second delay between each attempt. + +Modifying the dial delay is very useful when running +.Nm +in demand +dial mode on both ends of the link. If each end has the same timeout, +both ends wind up calling each other at the same time if the link +drops and both ends have packets queued. To terminate the program, type @@ -448,19 +479,21 @@ work with stdin and stdout. You can also telnet to port 3000 to get command mode control in the same manner as client-side .Nm . -.Sh SETTING IDLE TIMER +.Sh SETTING IDLE, LINE QUALITY REQUEST, RETRY TIMER To check/set idletimer, use the .Dq show timeout and -.Dq set timeout +.Dq set timeout [lqrtimer [retrytimer]] commands. Ex: .Dl ppp ON tama> set timeout 600 -The timeout period is measured in seconds, the default value for which -is 180 or 3 min. To disable the idle timer function, use the command +The timeout period is measured in seconds, the default values for which +are timeout = 180 or 3 min, lqrtimer = 30sec and retrytimer = 3sec. +To disable the idle timer function, +use the command .Dq set timeout 0 . In @@ -688,6 +721,9 @@ Logging and debugging information file. .It /var/spool/lock/Lck..* tty port locking file. +.It /var/run/PPP.system +Holds the pid for ppp -auto system. + .It /etc/services Get port number if port number is using service name. .El diff --git a/usr.sbin/ppp/vars.c b/usr.sbin/ppp/vars.c index 32168b2149da..4ebe68296747 100644 --- a/usr.sbin/ppp/vars.c +++ b/usr.sbin/ppp/vars.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: vars.c,v 1.3 1995/05/30 03:51:01 rgrimes Exp $ + * $Id: vars.c,v 1.5 1995/09/02 17:20:54 amurai Exp $ * */ #include "fsm.h" @@ -29,7 +29,7 @@ #include "defs.h" char VarVersion[] = "Version 0.94"; -char VarLocalVersion[] = "$Date$"; +char VarLocalVersion[] = "$Date: 1995/09/02 17:20:54 $"; /* * Order of conf option is important. See vars.h. @@ -48,7 +48,7 @@ struct confdesc pppConfs[] = { struct pppvars pppVars = { DEF_MRU, 0, MODEM_SPEED, CS8, 180, 30, 3, - MODEM_DEV, OPEN_PASSIVE, LOCAL_NO_AUTH, + REDIAL_PERIOD, 1, MODEM_DEV, OPEN_PASSIVE, LOCAL_NO_AUTH, }; int diff --git a/usr.sbin/ppp/vars.h b/usr.sbin/ppp/vars.h index 9794d8a68140..c497267a4f5b 100644 --- a/usr.sbin/ppp/vars.h +++ b/usr.sbin/ppp/vars.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: vars.h,v 1.2 1995/02/26 12:18:06 amurai Exp $ + * $Id: vars.h,v 1.3 1995/03/11 15:18:55 amurai Exp $ * * TODO: */ @@ -59,6 +59,8 @@ struct pppvars { int idle_timeout; /* Idle timeout value */ int lqr_timeout; /* LQR timeout value */ int retry_timeout; /* Retry timeout value */ + int redial_timeout; /* Redial timeout value */ + int dial_tries; /* Dial attempts before giving up, 0 == forever */ char modem_dev[20]; /* Name of device */ int open_mode; /* LCP open mode */ #define LOCAL_AUTH 0x01 @@ -90,6 +92,8 @@ struct pppvars { #define VarAuthName pppVars.auth_name #define VarPhone pppVars.phone_number #define VarShortHost pppVars.shostname +#define VarRedialTimeout pppVars.redial_timeout +#define VarDialTries pppVars.dial_tries #define DEV_IS_SYNC (VarSpeed == 0)