Don't waste time scanning tun_in+10 descriptors, scan exactly what

we need now.
Don't assume that file descriptor can't be 0 (many places)
Protect FD_* macros from being used with negative descriptors
Shorten MS EXT show help to fit 80 cols
This commit is contained in:
Andrey A. Chernov 1997-03-10 06:21:02 +00:00
parent a34dbe44cb
commit 780700e5ed
3 changed files with 45 additions and 36 deletions

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
* $Id: command.c,v 1.32 1997/02/22 16:10:08 peter Exp $
*
*/
#include <sys/types.h>
@ -145,7 +145,6 @@ char **argv;
modem = OpenModem(mode);
if (modem < 0) {
printf("failed to open modem.\n");
modem = 0;
break;
}
if (DialModem()) {
@ -440,7 +439,7 @@ struct cmdtab const ShowCommands[] = {
"Show Redial timeout value", StrNull},
#ifdef MSEXT
{ "msext", NULL, ShowMSExt, LOCAL_AUTH,
"Show MS PPP extention values", StrNull},
"Show MS PPP extentions", StrNull},
#endif /* MSEXT */
{ "version", NULL, ShowVersion, LOCAL_NO_AUTH | LOCAL_AUTH,
"Show version string", StrNull},
@ -575,7 +574,6 @@ TerminalCommand()
modem = OpenModem(mode);
if (modem < 0) {
printf("failed to open modem.\n");
modem = 0;
return(1);
}
printf("Enter to terminal mode.\n");

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
* $Id: main.c,v 1.36 1997/03/09 20:03:39 ache Exp $
*
* TODO:
* o Add commands for traffic summary, version display, etc.
@ -167,8 +167,10 @@ int excode;
OsInterfaceDown(1);
LogPrintf(LOG_PHASE_BIT, "PPP Terminated.\n");
LogClose();
if (server > 0)
if (server >= 0) {
close(server);
server = -1;
}
TtyOldMode();
exit(excode);
@ -292,7 +294,7 @@ char **argv;
argc--; argv++;
mode = MODE_INTER; /* default operation is interactive mode */
netfd = -1;
netfd = server = modem = tun_in = -1;
ProcessArgs(argc, argv);
Greetings();
GetUid();
@ -328,7 +330,7 @@ char **argv;
mode &= ~MODE_INTER;
if (mode & MODE_INTER) {
printf("Interactive mode\n");
netfd = 0;
netfd = STDIN_FILENO;
} else if (mode & MODE_AUTO) {
printf("Automatic Dialer mode\n");
if (dstsystem == NULL) {
@ -398,7 +400,6 @@ char **argv;
perror("pipe");
Cleanup(EX_SOCK);
}
server = -1;
}
else {
/*
@ -456,7 +457,7 @@ char **argv;
close(fd);
}
}
if (server > 0)
if (server >= 0)
LogPrintf(LOG_PHASE_BIT, "Listening at %d.\n", port);
#ifdef DOTTYINIT
if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */
@ -477,7 +478,6 @@ char **argv;
}
}
} else {
server = -1;
TtyInit();
TtyCommandMode(1);
}
@ -691,7 +691,7 @@ static void
DoLoop()
{
fd_set rfds, wfds, efds;
int pri, i, n, wfd;
int pri, i, n, wfd, nfds;
struct sockaddr_in hisaddr;
struct timeval timeout, *tp;
int ssize = sizeof(hisaddr);
@ -710,7 +710,7 @@ DoLoop()
fflush(stderr);
PacketMode();
} else if (mode & MODE_DEDICATED) {
if (!modem)
if (modem < 0)
modem = OpenModem(mode);
}
@ -722,6 +722,7 @@ DoLoop()
dial_up = FALSE; /* XXXX */
tries = 0;
for (;;) {
nfds = 0;
FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
/*
@ -741,7 +742,6 @@ DoLoop()
#endif
modem = OpenModem(mode);
if (modem < 0) {
modem = 0; /* Set intial value for next OpenModem */
StartRedialTimer();
} else {
tries++;
@ -771,14 +771,20 @@ DoLoop()
qlen = ModemQlen();
}
if (modem) {
if (modem >= 0) {
if (modem + 1 > nfds)
nfds = modem + 1;
FD_SET(modem, &rfds);
FD_SET(modem, &efds);
if (qlen > 0) {
FD_SET(modem, &wfds);
}
}
if (server > 0) FD_SET(server, &rfds);
if (server >= 0) {
if (server + 1 > nfds)
nfds = server + 1;
FD_SET(server, &rfds);
}
/* *** IMPORTANT ***
*
@ -793,10 +799,15 @@ DoLoop()
#endif
/* If there are aren't many packets queued, look for some more. */
if (qlen < 20)
if (qlen < 20 && tun_in >= 0) {
if (tun_in + 1 > nfds)
nfds = tun_in + 1;
FD_SET(tun_in, &rfds);
}
if (netfd > -1) {
if (netfd >= 0) {
if (netfd + 1 > nfds)
nfds = netfd + 1;
FD_SET(netfd, &rfds);
FD_SET(netfd, &efds);
}
@ -807,7 +818,7 @@ DoLoop()
* In AUTO mode, select will block until we find packet from tun
*/
tp = (RedialTimer.state == TIMER_RUNNING)? &timeout : NULL;
i = select(tun_in+10, &rfds, &wfds, &efds, tp);
i = select(nfds, &rfds, &wfds, &efds, tp);
#else
/*
* When SIGALRM timer is running, a select function will be
@ -816,7 +827,7 @@ DoLoop()
* trying to dial, poll with a 0 value timer.
*/
tp = (dial_up && RedialTimer.state != TIMER_RUNNING) ? &timeout : NULL;
i = select(tun_in+10, &rfds, &wfds, &efds, tp);
i = select(nfds, &rfds, &wfds, &efds, tp);
#endif
if ( i == 0 ) {
@ -831,17 +842,17 @@ DoLoop()
break;
}
if ((netfd > 0 && FD_ISSET(netfd, &efds)) || FD_ISSET(modem, &efds)) {
if ((netfd >= 0 && FD_ISSET(netfd, &efds)) || (modem >= 0 && FD_ISSET(modem, &efds))) {
logprintf("Exception detected.\n");
break;
}
if (server > 0 && FD_ISSET(server, &rfds)) {
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) {
if (netfd >= 0) {
write(wfd, "already in use.\n", 16);
close(wfd);
continue;
@ -867,12 +878,12 @@ DoLoop()
Prompt(0);
}
if ((mode & MODE_INTER) && FD_ISSET(netfd, &rfds) &&
if ((mode & MODE_INTER) && (netfd >= 0 && FD_ISSET(netfd, &rfds)) &&
((mode & MODE_AUTO) || pgroup == tcgetpgrp(0))) {
/* something to read from tty */
ReadTty();
}
if (modem) {
if (modem >= 0) {
if (FD_ISSET(modem, &wfds)) { /* ready to write into modem */
ModemStartOutput(modem);
}
@ -916,7 +927,7 @@ DoLoop()
}
}
if (FD_ISSET(tun_in, &rfds)) { /* something to read from tun */
if (tun_in >= 0 && FD_ISSET(tun_in, &rfds)) { /* something to read from tun */
n = read(tun_in, rbuff, sizeof(rbuff));
if (n < 0) {
perror("read from tun");

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
* $Id: modem.c,v 1.29 1997/02/22 16:10:36 peter Exp $
*
* TODO:
*/
@ -382,7 +382,7 @@ int mode;
if (mode & MODE_DIRECT) {
if (isatty(0))
modem = open(ctermid(NULL), O_RDWR|O_NONBLOCK);
} else if (modem == 0) {
} else if (modem < 0) {
if (strncmp(VarDevice, "/dev", 4) == 0) {
strncpy(uucplock, rindex(VarDevice, '/')+1,sizeof(uucplock)-1);
uucplock[sizeof(uucplock)-1] = '\0';
@ -506,7 +506,7 @@ int modem;
if (!isatty(modem) || DEV_IS_SYNC)
return(0);
if (!(mode & MODE_DIRECT) && modem && !Online) {
if (!(mode & MODE_DIRECT) && modem >= 0 && !Online) {
#ifdef DEBUG
logprintf("mode = %d, modem = %d, mbits = %x\n", mode, modem, mbits);
#endif
@ -557,11 +557,11 @@ int flag;
if (!isatty(modem)) {
mbits &= ~TIOCM_DTR;
close(modem);
modem = 0; /* Mark as modem has closed */
modem = -1; /* Mark as modem has closed */
return;
}
if (modem && Online) {
if (modem >= 0 && Online) {
mbits &= ~TIOCM_DTR;
#ifdef __bsdi__ /* not a POSIX way */
ioctl(modem, TIOCMSET, &mbits);
@ -579,22 +579,22 @@ int flag;
* If we are working as dedicated mode, never close it
* until we are directed to quit program.
*/
if (modem && (flag || !(mode & MODE_DEDICATED))) {
if (modem >= 0 && (flag || !(mode & MODE_DEDICATED))) {
ModemTimeout(); /* XXX */
StopTimer(&ModemTimer); /* XXX */
/* ModemTimeout() may call DownConection() to close the modem
* resulting in modem == 0.
*/
if (modem)
if (modem >= 0)
{
tcflush(modem, TCIOFLUSH);
UnrawModem(modem);
close(modem);
}
modem = -1; /* Mark as modem has closed */
(void) uu_unlock(uucplock);
modem = 0; /* Mark as modem has closed */
} else if (modem) {
} else if (modem >= 0) {
mbits |= TIOCM_DTR;
#ifndef notyet
ioctl(modem, TIOCMSET, &mbits);
@ -612,7 +612,7 @@ CloseModem()
if (modem >= 3)
{
close(modem);
modem = 0;
modem = -1;
}
(void) uu_unlock(uucplock);
}