1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Modem handling module
|
|
|
|
*
|
|
|
|
* 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-05-29 02:29:13 +00:00
|
|
|
* $Id: modem.c,v 1.40 1997/05/26 00:44:06 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
|
|
|
#include "fsm.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <sys/tty.h>
|
1995-02-27 10:57:54 +00:00
|
|
|
#include <errno.h>
|
1996-01-10 21:28:04 +00:00
|
|
|
#include <time.h>
|
1997-03-31 22:51:00 +00:00
|
|
|
#include <libutil.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
1996-01-30 11:08:50 +00:00
|
|
|
#include "ip.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "modem.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
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
extern int DoChat();
|
|
|
|
|
|
|
|
static int mbits; /* Current DCD status */
|
|
|
|
static int connect_count;
|
|
|
|
static struct pppTimer ModemTimer;
|
|
|
|
|
1996-03-08 09:03:09 +00:00
|
|
|
extern void PacketMode(), TtyTermMode(), TtyCommandMode();
|
|
|
|
extern int TermMode;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
#define Online (mbits & TIOCM_CD)
|
|
|
|
|
|
|
|
static struct mbuf *modemout;
|
1996-01-30 11:08:50 +00:00
|
|
|
static struct mqueue OutputQueues[PRI_LINK+1];
|
1995-01-31 06:29:58 +00:00
|
|
|
static int dev_is_modem;
|
|
|
|
|
1996-01-30 11:08:50 +00:00
|
|
|
#undef QDEBUG
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
|
|
|
Enqueue(queue, bp)
|
|
|
|
struct mqueue *queue;
|
|
|
|
struct mbuf *bp;
|
|
|
|
{
|
|
|
|
if (queue->last) {
|
|
|
|
queue->last->pnext = bp;
|
|
|
|
queue->last = bp;
|
|
|
|
} else
|
|
|
|
queue->last = queue->top = bp;
|
|
|
|
queue->qlen++;
|
|
|
|
#ifdef QDEBUG
|
|
|
|
logprintf("Enqueue: len = %d\n", queue->qlen);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
|
|
|
Dequeue(queue)
|
|
|
|
struct mqueue *queue;
|
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
#ifdef QDEBUG
|
|
|
|
logprintf("Dequeue: len = %d\n", queue->qlen);
|
|
|
|
#endif
|
1996-01-10 21:28:04 +00:00
|
|
|
bp = queue->top;
|
|
|
|
if (bp) {
|
1995-01-31 06:29:58 +00:00
|
|
|
queue->top = queue->top->pnext;
|
|
|
|
queue->qlen--;
|
|
|
|
if (queue->top == NULL) {
|
|
|
|
queue->last = queue->top;
|
|
|
|
#ifdef QDEBUG
|
|
|
|
if (queue->qlen)
|
|
|
|
logprintf("!!! not zero (%d)!!!\n", queue->qlen);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(bp);
|
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
static struct speeds{
|
|
|
|
int nspeed;
|
|
|
|
speed_t speed;
|
|
|
|
} speeds[] = {
|
|
|
|
#ifdef B50
|
|
|
|
{ 50, B50, },
|
|
|
|
#endif
|
|
|
|
#ifdef B75
|
|
|
|
{ 75, B75, },
|
|
|
|
#endif
|
|
|
|
#ifdef B110
|
|
|
|
{ 110, B110, },
|
|
|
|
#endif
|
|
|
|
#ifdef B134
|
|
|
|
{ 134, B134, },
|
|
|
|
#endif
|
|
|
|
#ifdef B150
|
|
|
|
{ 150, B150, },
|
|
|
|
#endif
|
|
|
|
#ifdef B200
|
|
|
|
{ 200, B200, },
|
|
|
|
#endif
|
|
|
|
#ifdef B300
|
|
|
|
{ 300, B300, },
|
|
|
|
#endif
|
|
|
|
#ifdef B600
|
|
|
|
{ 600, B600, },
|
|
|
|
#endif
|
|
|
|
#ifdef B1200
|
|
|
|
{ 1200, B1200, },
|
|
|
|
#endif
|
|
|
|
#ifdef B1800
|
|
|
|
{ 1800, B1800, },
|
|
|
|
#endif
|
|
|
|
#ifdef B2400
|
|
|
|
{ 2400, B2400, },
|
|
|
|
#endif
|
|
|
|
#ifdef B4800
|
|
|
|
{ 4800, B4800, },
|
|
|
|
#endif
|
|
|
|
#ifdef B9600
|
|
|
|
{ 9600, B9600, },
|
|
|
|
#endif
|
|
|
|
#ifdef B19200
|
|
|
|
{ 19200, B19200, },
|
|
|
|
#endif
|
|
|
|
#ifdef B38400
|
|
|
|
{ 38400, B38400, },
|
|
|
|
#endif
|
1995-02-27 10:57:54 +00:00
|
|
|
#ifndef _POSIX_SOURCE
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef B7200
|
|
|
|
{ 7200, B7200, },
|
|
|
|
#endif
|
|
|
|
#ifdef B14400
|
|
|
|
{ 14400, B14400, },
|
|
|
|
#endif
|
|
|
|
#ifdef B28800
|
|
|
|
{ 28800, B28800, },
|
|
|
|
#endif
|
|
|
|
#ifdef B57600
|
|
|
|
{ 57600, B57600, },
|
|
|
|
#endif
|
|
|
|
#ifdef B76800
|
|
|
|
{ 76800, B76800, },
|
|
|
|
#endif
|
|
|
|
#ifdef B115200
|
|
|
|
{ 115200, B115200, },
|
|
|
|
#endif
|
|
|
|
#ifdef B230400
|
|
|
|
{ 230400, B230400, },
|
|
|
|
#endif
|
|
|
|
#ifdef EXTA
|
|
|
|
{ 19200, EXTA, },
|
|
|
|
#endif
|
|
|
|
#ifdef EXTB
|
|
|
|
{ 38400, EXTB, },
|
|
|
|
#endif
|
|
|
|
#endif /*_POSIX_SOURCE */
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int SpeedToInt(speed)
|
|
|
|
speed_t speed;
|
|
|
|
{
|
|
|
|
struct speeds *sp;
|
|
|
|
|
|
|
|
for (sp = speeds; sp->nspeed; sp++) {
|
|
|
|
if (sp->speed == speed) {
|
|
|
|
return(sp->nspeed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
speed_t IntToSpeed(nspeed)
|
|
|
|
int nspeed;
|
|
|
|
{
|
|
|
|
struct speeds *sp;
|
|
|
|
|
|
|
|
for (sp = speeds; sp->nspeed; sp++) {
|
|
|
|
if (sp->nspeed == nspeed) {
|
|
|
|
return(sp->speed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return B0;
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
static time_t uptime;
|
|
|
|
|
|
|
|
void
|
|
|
|
DownConnection()
|
|
|
|
{
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Disconnected!\n");
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Connect time: %d secs\n", time(NULL) - uptime);
|
1996-03-13 12:02:16 +00:00
|
|
|
if (!TermMode) {
|
|
|
|
CloseModem();
|
|
|
|
LcpDown();
|
1996-03-08 09:03:09 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ModemTimeout() watches DCD signal and notifies if it's status is changed.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ModemTimeout()
|
|
|
|
{
|
|
|
|
int ombits = mbits;
|
|
|
|
int change;
|
|
|
|
|
|
|
|
StopTimer(&ModemTimer);
|
|
|
|
StartTimer(&ModemTimer);
|
|
|
|
|
|
|
|
if (dev_is_modem) {
|
1997-05-19 02:00:16 +00:00
|
|
|
if (modem >= 0) {
|
|
|
|
if (ioctl(modem, TIOCMGET, &mbits) < 0) {
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "ioctl error (%s)!\n", strerror(errno));
|
1997-05-14 01:14:32 +00:00
|
|
|
DownConnection();
|
1997-05-19 02:00:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
mbits = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
change = ombits ^ mbits;
|
|
|
|
if (change & TIOCM_CD) {
|
|
|
|
if (Online) {
|
|
|
|
time(&uptime);
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "*Connected!\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
connect_count++;
|
|
|
|
/*
|
|
|
|
* In dedicated mode, start packet mode immediate
|
|
|
|
* after we detected carrier.
|
|
|
|
*/
|
|
|
|
if (mode & MODE_DEDICATED)
|
|
|
|
PacketMode();
|
|
|
|
} else {
|
1997-05-24 17:32:42 +00:00
|
|
|
reconnect(RECON_TRUE);
|
1995-01-31 06:29:58 +00:00
|
|
|
DownConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!Online) {
|
|
|
|
time(&uptime);
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Connected!\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
mbits = TIOCM_CD;
|
|
|
|
connect_count++;
|
1995-02-26 12:18:08 +00:00
|
|
|
} else if (uptime == 0) {
|
|
|
|
time(&uptime);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StartModemTimer()
|
|
|
|
{
|
|
|
|
StopTimer(&ModemTimer);
|
|
|
|
ModemTimer.state = TIMER_STOPPED;
|
|
|
|
ModemTimer.load = SECTICKS;
|
|
|
|
ModemTimer.func = ModemTimeout;
|
|
|
|
StartTimer(&ModemTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct parity {
|
|
|
|
char *name;
|
|
|
|
char *name1;
|
|
|
|
int set;
|
|
|
|
} validparity[] = {
|
|
|
|
{ "even", "P_EVEN", CS7 | PARENB }, { "odd", "P_ODD", CS7 | PARENB | PARODD },
|
|
|
|
{ "none", "P_ZERO", CS8 }, { NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
GetParityValue(str)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
struct parity *pp;
|
|
|
|
|
|
|
|
for (pp = validparity; pp->name; pp++) {
|
|
|
|
if (strcasecmp(pp->name, str) == 0 ||
|
|
|
|
strcasecmp(pp->name1, str) == 0) {
|
|
|
|
VarParity = pp->set;
|
|
|
|
return(pp->set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ChangeParity(str)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = GetParityValue(str);
|
|
|
|
if (val > 0) {
|
|
|
|
VarParity = val;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(modem, &rstio);
|
1995-01-31 06:29:58 +00:00
|
|
|
rstio.c_cflag &= ~(CSIZE|PARODD|PARENB);
|
|
|
|
rstio.c_cflag |= val;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSADRAIN, &rstio);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
return(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
OpenConnection(host, port)
|
|
|
|
char *host, *port;
|
|
|
|
{
|
|
|
|
struct sockaddr_in dest;
|
|
|
|
int sock;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct servent *sp;
|
|
|
|
|
|
|
|
dest.sin_family = AF_INET;
|
|
|
|
dest.sin_addr.s_addr = inet_addr(host);
|
|
|
|
if (dest.sin_addr.s_addr == INADDR_NONE) {
|
|
|
|
hp = gethostbyname(host);
|
|
|
|
if (hp) {
|
|
|
|
bcopy(hp->h_addr_list[0], &dest.sin_addr.s_addr, 4);
|
|
|
|
} else {
|
|
|
|
printf("unknown host: %s\n", host);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dest.sin_port = htons(atoi(port));
|
|
|
|
if (dest.sin_port == 0) {
|
|
|
|
sp = getservbyname(port, "tcp");
|
|
|
|
if (sp) {
|
|
|
|
dest.sin_port = sp->s_port;
|
|
|
|
} else {
|
|
|
|
printf("unknown service: %s\n", port);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Connected to %s:%s\n", host, port);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
return(sock);
|
|
|
|
}
|
|
|
|
if (connect(sock, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
|
|
|
|
printf("connection failed.\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(sock);
|
|
|
|
}
|
|
|
|
|
1996-03-29 15:24:04 +00:00
|
|
|
static struct termios modemios;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
|
|
|
OpenModem(mode)
|
|
|
|
int mode;
|
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int oldflag;
|
|
|
|
char *host, *cp, *port;
|
1997-03-31 22:51:00 +00:00
|
|
|
int res;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
mbits = 0;
|
|
|
|
if (mode & MODE_DIRECT) {
|
1997-03-10 06:54:58 +00:00
|
|
|
if (isatty(0)) {
|
1995-02-26 12:18:08 +00:00
|
|
|
modem = open(ctermid(NULL), O_RDWR|O_NONBLOCK);
|
1997-03-10 06:54:58 +00:00
|
|
|
if (modem < 0) {
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Open Failed %s\n", ctermid(NULL));
|
|
|
|
return(modem);
|
|
|
|
}
|
|
|
|
} else if (modem < 0)
|
|
|
|
return(modem);
|
1997-03-10 06:21:02 +00:00
|
|
|
} else if (modem < 0) {
|
1997-05-10 03:39:57 +00:00
|
|
|
if (strncmp(VarDevice, "/dev/", 5) == 0) {
|
|
|
|
if ((res = uu_lock(VarBaseDevice)) != UU_LOCK_OK) {
|
1997-03-31 22:51:00 +00:00
|
|
|
if (res == UU_LOCK_INUSE)
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Modem %s is in use\n", VarDevice);
|
|
|
|
else
|
|
|
|
LogPrintf(LOG_PHASE_BIT, "Modem %s is in use: uu_lock: %s\n",
|
|
|
|
VarDevice, uu_lockerr(res));
|
1995-01-31 06:29:58 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
modem = open(VarDevice, O_RDWR|O_NONBLOCK);
|
|
|
|
if (modem < 0) {
|
1996-05-11 20:48:42 +00:00
|
|
|
LogPrintf(LOG_PHASE_BIT, "Open Failed %s\n", VarDevice);
|
1997-05-10 03:39:57 +00:00
|
|
|
(void) uu_unlock(VarBaseDevice);
|
1995-01-31 06:29:58 +00:00
|
|
|
return(modem);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* XXX: PPP over TCP */
|
|
|
|
cp = index(VarDevice, ':');
|
|
|
|
if (cp) {
|
|
|
|
*cp = 0;
|
|
|
|
host = VarDevice;
|
|
|
|
port = cp + 1;
|
|
|
|
if (*host && *port) {
|
|
|
|
modem = OpenConnection(host, port);
|
|
|
|
*cp = ':'; /* Don't destroy VarDevice */
|
|
|
|
if (modem < 0) return(-1);
|
|
|
|
} else {
|
|
|
|
*cp = ':'; /* Don't destroy VarDevice */
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-04-16 13:38:39 +00:00
|
|
|
/* This code gets around the problem of closing descriptor 0
|
|
|
|
* when it should not have been closed and closing descriptor 1
|
|
|
|
* when the telnet connection dies. Since this program always
|
|
|
|
* opens a descriptor for the modem in auto and direct mode,
|
|
|
|
* having to dup the descriptor here is a fatal error.
|
|
|
|
*
|
|
|
|
* With the other changes I have made this should no longer happen.
|
|
|
|
* JC
|
|
|
|
*/
|
1995-01-31 06:29:58 +00:00
|
|
|
while (modem < 3)
|
1995-04-16 13:38:39 +00:00
|
|
|
{
|
|
|
|
logprintf("Duping modem fd %d\n", modem);
|
1995-01-31 06:29:58 +00:00
|
|
|
modem = dup(modem);
|
1995-04-16 13:38:39 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are working on tty device, change it's mode into
|
|
|
|
* the one desired for further operation. In this implementation,
|
|
|
|
* we assume that modem is configuted to use CTS/RTS flow control.
|
|
|
|
*/
|
1995-02-26 12:18:08 +00:00
|
|
|
dev_is_modem = isatty(modem) || DEV_IS_SYNC;
|
|
|
|
if (DEV_IS_SYNC)
|
|
|
|
sleep(1);
|
|
|
|
if (dev_is_modem && !DEV_IS_SYNC) {
|
|
|
|
tcgetattr(modem, &rstio);
|
1996-03-29 15:24:04 +00:00
|
|
|
modemios = rstio;
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("## modem = %d\n", modem);
|
|
|
|
logprintf("modem (get): iflag = %x, oflag = %x, cflag = %x\n",
|
|
|
|
rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
|
|
|
#endif
|
1996-03-27 21:40:55 +00:00
|
|
|
cfmakeraw(&rstio);
|
1996-12-22 17:09:17 +00:00
|
|
|
if (VarCtsRts)
|
|
|
|
rstio.c_cflag |= CLOCAL | CCTS_OFLOW|CRTS_IFLOW;
|
|
|
|
else {
|
|
|
|
rstio.c_cflag |= CLOCAL;
|
|
|
|
rstio.c_iflag |= IXOFF;
|
|
|
|
}
|
1996-03-27 22:58:21 +00:00
|
|
|
rstio.c_iflag |= IXON;
|
1996-03-09 08:18:41 +00:00
|
|
|
if (!(mode & MODE_DEDICATED))
|
|
|
|
rstio.c_cflag |= HUPCL;
|
1995-01-31 06:29:58 +00:00
|
|
|
if ((mode & MODE_DIRECT) == 0) {
|
|
|
|
/*
|
|
|
|
* If we are working as direct mode, don't change tty speed.
|
|
|
|
*/
|
1996-03-28 13:38:59 +00:00
|
|
|
rstio.c_cflag &= ~(CSIZE|PARODD|PARENB);
|
1995-01-31 06:29:58 +00:00
|
|
|
rstio.c_cflag |= VarParity;
|
1997-05-10 01:22:19 +00:00
|
|
|
if (cfsetspeed(&rstio, IntToSpeed(VarSpeed)) == -1) {
|
|
|
|
logprintf("Unable to set modem speed (modem %d to %d)\n",
|
|
|
|
modem, VarSpeed);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSADRAIN, &rstio);
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("modem (put): iflag = %x, oflag = %x, cflag = %x\n",
|
|
|
|
rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
|
|
|
#endif
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (!(mode & MODE_DIRECT))
|
1997-05-10 01:22:19 +00:00
|
|
|
if (ioctl(modem, TIOCMGET, &mbits))
|
|
|
|
return(-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "modem control = %o\n", mbits);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
oldflag = fcntl(modem, F_GETFL, 0);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (oldflag < 0)
|
|
|
|
return(-1);
|
|
|
|
(void)fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
StartModemTimer();
|
|
|
|
|
|
|
|
return(modem);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ModemSpeed()
|
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(modem, &rstio);
|
|
|
|
return(SpeedToInt(cfgetispeed(&rstio)));
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put modem tty line into raw mode which is necessary in packet mode operation
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
RawModem(modem)
|
|
|
|
int modem;
|
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int oldflag;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (!isatty(modem) || DEV_IS_SYNC)
|
1995-01-31 06:29:58 +00:00
|
|
|
return(0);
|
1997-03-10 06:21:02 +00:00
|
|
|
if (!(mode & MODE_DIRECT) && modem >= 0 && !Online) {
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("mode = %d, modem = %d, mbits = %x\n", mode, modem, mbits);
|
|
|
|
#endif
|
|
|
|
#ifdef notdef
|
|
|
|
return(-1);
|
|
|
|
#endif
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(modem, &rstio);
|
1996-03-27 21:40:55 +00:00
|
|
|
cfmakeraw(&rstio);
|
1996-12-22 17:09:17 +00:00
|
|
|
if (VarCtsRts)
|
|
|
|
rstio.c_cflag |= CLOCAL | CCTS_OFLOW|CRTS_IFLOW;
|
|
|
|
else
|
|
|
|
rstio.c_cflag |= CLOCAL;
|
|
|
|
|
1996-03-09 08:18:41 +00:00
|
|
|
if (!(mode & MODE_DEDICATED))
|
|
|
|
rstio.c_cflag |= HUPCL;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSADRAIN, &rstio);
|
1995-01-31 06:29:58 +00:00
|
|
|
oldflag = fcntl(modem, F_GETFL, 0);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (oldflag < 0)
|
|
|
|
return(-1);
|
|
|
|
(void)fcntl(modem, F_SETFL, oldflag | O_NONBLOCK);
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
oldflag = fcntl(modem, F_GETFL, 0);
|
|
|
|
logprintf("modem (put2): iflag = %x, oflag = %x, cflag = %x\n",
|
|
|
|
rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
|
|
|
logprintf("flag = %x\n", oldflag);
|
|
|
|
#endif
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnrawModem(modem)
|
|
|
|
int modem;
|
|
|
|
{
|
|
|
|
int oldflag;
|
|
|
|
|
1996-03-29 15:24:04 +00:00
|
|
|
if (isatty(modem) && !DEV_IS_SYNC) {
|
|
|
|
tcsetattr(modem, TCSAFLUSH, &modemios);
|
1995-01-31 06:29:58 +00:00
|
|
|
oldflag = fcntl(modem, F_GETFL, 0);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (oldflag < 0)
|
|
|
|
return;
|
|
|
|
(void)fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HangupModem(flag)
|
|
|
|
int flag;
|
|
|
|
{
|
1995-02-26 12:18:08 +00:00
|
|
|
struct termios tio;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (!isatty(modem)) {
|
|
|
|
mbits &= ~TIOCM_DTR;
|
|
|
|
close(modem);
|
1997-03-10 06:21:02 +00:00
|
|
|
modem = -1; /* Mark as modem has closed */
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-03-10 06:21:02 +00:00
|
|
|
if (modem >= 0 && Online) {
|
1995-01-31 06:29:58 +00:00
|
|
|
mbits &= ~TIOCM_DTR;
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef __bsdi__ /* not a POSIX way */
|
1995-01-31 06:29:58 +00:00
|
|
|
ioctl(modem, TIOCMSET, &mbits);
|
1995-02-26 12:18:08 +00:00
|
|
|
#else
|
|
|
|
tcgetattr(modem, &tio);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (cfsetspeed(&tio, B0) == -1) {
|
|
|
|
logprintf("Unable to set modem to speed 0\n");
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSANOW, &tio);
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
sleep(1);
|
|
|
|
#ifdef notdef
|
|
|
|
mbits &= ~TIOCM_CD;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we are working as dedicated mode, never close it
|
|
|
|
* until we are directed to quit program.
|
|
|
|
*/
|
1997-03-10 06:21:02 +00:00
|
|
|
if (modem >= 0 && (flag || !(mode & MODE_DEDICATED))) {
|
1995-01-31 06:29:58 +00:00
|
|
|
ModemTimeout(); /* XXX */
|
|
|
|
StopTimer(&ModemTimer); /* XXX */
|
1995-04-16 13:38:39 +00:00
|
|
|
|
|
|
|
/* ModemTimeout() may call DownConection() to close the modem
|
|
|
|
* resulting in modem == 0.
|
|
|
|
*/
|
1997-03-10 06:21:02 +00:00
|
|
|
if (modem >= 0)
|
1995-04-16 13:38:39 +00:00
|
|
|
{
|
1996-03-27 22:28:19 +00:00
|
|
|
tcflush(modem, TCIOFLUSH);
|
1995-04-16 13:38:39 +00:00
|
|
|
UnrawModem(modem);
|
|
|
|
close(modem);
|
|
|
|
}
|
1997-03-10 06:21:02 +00:00
|
|
|
modem = -1; /* Mark as modem has closed */
|
1997-05-10 03:39:57 +00:00
|
|
|
(void) uu_unlock(VarBaseDevice);
|
1997-03-10 06:21:02 +00:00
|
|
|
} else if (modem >= 0) {
|
1995-01-31 06:29:58 +00:00
|
|
|
mbits |= TIOCM_DTR;
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef notyet
|
1995-01-31 06:29:58 +00:00
|
|
|
ioctl(modem, TIOCMSET, &mbits);
|
1995-02-26 12:18:08 +00:00
|
|
|
#else
|
|
|
|
tcgetattr(modem, &ts);
|
|
|
|
cfsetspeed(&ts, IntToSpeed(VarSpeed));
|
|
|
|
tcsetattr(modem, TCSADRAIN, &ts);
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-01-10 21:28:04 +00:00
|
|
|
void
|
1995-01-31 06:29:58 +00:00
|
|
|
CloseModem()
|
|
|
|
{
|
1997-03-10 06:54:58 +00:00
|
|
|
if (modem >= 0)
|
1995-04-16 13:38:39 +00:00
|
|
|
{
|
|
|
|
close(modem);
|
1997-03-10 06:21:02 +00:00
|
|
|
modem = -1;
|
1995-04-16 13:38:39 +00:00
|
|
|
}
|
1997-05-10 03:39:57 +00:00
|
|
|
(void) uu_unlock(VarBaseDevice);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write to modem. Actualy, requested packets are queued, and goes out
|
|
|
|
* to the line when ModemStartOutput() is called.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
WriteModem(pri, ptr, count)
|
|
|
|
int pri;
|
|
|
|
char *ptr;
|
|
|
|
int count;
|
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
bp = mballoc(count, MB_MODEM);
|
|
|
|
bcopy(ptr, MBUF_CTOP(bp), count);
|
1996-01-30 11:08:50 +00:00
|
|
|
|
|
|
|
/* Should be NORMAL and LINK only.
|
|
|
|
* All IP frames get here marked NORMAL.
|
|
|
|
*/
|
1995-01-31 06:29:58 +00:00
|
|
|
Enqueue(&OutputQueues[pri], bp);
|
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
void
|
|
|
|
ModemOutput(pri, bp)
|
|
|
|
int pri;
|
|
|
|
struct mbuf *bp;
|
|
|
|
{
|
|
|
|
struct mbuf *wp;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = plength(bp);
|
|
|
|
wp = mballoc(len, MB_MODEM);
|
|
|
|
mbread(bp, MBUF_CTOP(wp), len);
|
|
|
|
Enqueue(&OutputQueues[pri], wp);
|
|
|
|
ModemStartOutput(modem);
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
|
|
|
ModemQlen()
|
|
|
|
{
|
1995-07-08 17:46:56 +00:00
|
|
|
struct mqueue *queue;
|
1995-01-31 06:29:58 +00:00
|
|
|
int len = 0;
|
|
|
|
int i;
|
|
|
|
|
1996-01-30 11:08:50 +00:00
|
|
|
for ( i = PRI_NORMAL; i <= PRI_LINK; i ++ ) {
|
1995-07-08 17:46:56 +00:00
|
|
|
queue = &OutputQueues[i];
|
|
|
|
len += queue->qlen;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
return(len);
|
1995-07-08 17:46:56 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModemStartOutput(fd)
|
|
|
|
int fd;
|
|
|
|
{
|
|
|
|
struct mqueue *queue;
|
1997-05-10 01:22:19 +00:00
|
|
|
int nb, nw;
|
|
|
|
#ifdef QDEBUG
|
|
|
|
int i;
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1996-01-30 11:08:50 +00:00
|
|
|
if (modemout == NULL && ModemQlen() == 0)
|
|
|
|
IpStartOutput();
|
1995-01-31 06:29:58 +00:00
|
|
|
if (modemout == NULL) {
|
1997-05-10 01:22:19 +00:00
|
|
|
#ifdef QDEBUG
|
1996-01-30 11:08:50 +00:00
|
|
|
i = PRI_LINK;
|
1997-05-10 01:22:19 +00:00
|
|
|
#endif
|
1996-01-30 11:08:50 +00:00
|
|
|
for (queue = &OutputQueues[PRI_LINK]; queue >= OutputQueues; queue--) {
|
1995-01-31 06:29:58 +00:00
|
|
|
if (queue->top) {
|
|
|
|
modemout = Dequeue(queue);
|
|
|
|
#ifdef QDEBUG
|
1996-01-30 11:08:50 +00:00
|
|
|
if (i > PRI_NORMAL) {
|
1995-01-31 06:29:58 +00:00
|
|
|
struct mqueue *q;
|
|
|
|
|
|
|
|
q = &OutputQueues[0];
|
|
|
|
logprintf("output from queue %d, normal has %d\n", i, q->qlen);
|
|
|
|
}
|
|
|
|
logprintf("Dequeue(%d): ", i);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
1997-05-10 01:22:19 +00:00
|
|
|
#ifdef QDEBUG
|
1996-01-30 11:08:50 +00:00
|
|
|
i--;
|
1997-05-10 01:22:19 +00:00
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (modemout) {
|
|
|
|
nb = modemout->cnt;
|
1995-02-26 12:18:08 +00:00
|
|
|
if (nb > 1600) nb = 1600;
|
1996-01-30 11:08:50 +00:00
|
|
|
if (fd == 0) fd = 1; /* XXX WTFO! This is bogus */
|
1995-01-31 06:29:58 +00:00
|
|
|
nw = write(fd, MBUF_CTOP(modemout), nb);
|
|
|
|
#ifdef QDEBUG
|
|
|
|
logprintf("wrote: %d(%d)\n", nw, nb);
|
|
|
|
LogDumpBuff(LOG_HDLC, "modem write", MBUF_CTOP(modemout), nb);
|
|
|
|
#endif
|
|
|
|
if (nw > 0) {
|
|
|
|
modemout->cnt -= nw;
|
|
|
|
modemout->offset += nw;
|
|
|
|
if (modemout->cnt == 0) {
|
|
|
|
modemout = mbfree(modemout);
|
|
|
|
#ifdef QDEBUG
|
|
|
|
logprintf(" mbfree\n");
|
|
|
|
#endif
|
|
|
|
}
|
1995-02-27 10:57:54 +00:00
|
|
|
} else if (nw < 0) {
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
perror("modem write");
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
DialModem()
|
|
|
|
{
|
|
|
|
char ScriptBuffer[200];
|
1997-05-29 02:29:13 +00:00
|
|
|
int excode;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-01-10 07:53:28 +00:00
|
|
|
strncpy(ScriptBuffer, VarDialScript,sizeof(ScriptBuffer)-1);
|
|
|
|
ScriptBuffer[sizeof(ScriptBuffer)-1] = '\0';
|
1995-01-31 06:29:58 +00:00
|
|
|
if (DoChat(ScriptBuffer) > 0) {
|
1996-03-08 09:03:09 +00:00
|
|
|
if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER)
|
|
|
|
fprintf(stderr, "dial OK!\n");
|
1997-01-10 07:53:28 +00:00
|
|
|
strncpy(ScriptBuffer, VarLoginScript,sizeof(ScriptBuffer)-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (DoChat(ScriptBuffer) > 0) {
|
1996-03-08 09:03:09 +00:00
|
|
|
if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER)
|
|
|
|
fprintf(stderr, "login OK!\n");
|
1997-05-29 02:29:13 +00:00
|
|
|
return EX_DONE;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-05-29 02:29:13 +00:00
|
|
|
if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER)
|
|
|
|
fprintf(stderr, "login failed.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
ModemTimeout(); /* Dummy call to check modem status */
|
1997-05-29 02:29:13 +00:00
|
|
|
excode = EX_NOLOGIN;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1996-03-08 09:03:09 +00:00
|
|
|
else {
|
1997-05-29 02:29:13 +00:00
|
|
|
if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER)
|
1996-03-08 09:03:09 +00:00
|
|
|
fprintf(stderr, "dial failed.\n");
|
1997-05-29 02:29:13 +00:00
|
|
|
excode = EX_NODIAL;
|
1996-03-08 09:03:09 +00:00
|
|
|
}
|
1995-06-30 19:53:04 +00:00
|
|
|
HangupModem(0);
|
1997-05-10 01:22:19 +00:00
|
|
|
return(excode);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ShowModemStatus()
|
|
|
|
{
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef TIOCOUTQ
|
1995-01-31 06:29:58 +00:00
|
|
|
int nb;
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("device: %s speed: ", VarDevice);
|
|
|
|
if (DEV_IS_SYNC)
|
|
|
|
printf("sync\n");
|
|
|
|
else
|
|
|
|
printf("%d\n", VarSpeed);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (VarParity & CSIZE) {
|
|
|
|
case CS7:
|
|
|
|
printf("cs7, ");
|
|
|
|
break;
|
|
|
|
case CS8:
|
|
|
|
printf("cs8, ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (VarParity & PARENB) {
|
|
|
|
if (VarParity & PARODD)
|
1996-12-22 17:09:17 +00:00
|
|
|
printf("odd parity, ");
|
1995-01-31 06:29:58 +00:00
|
|
|
else
|
1996-12-22 17:09:17 +00:00
|
|
|
printf("even parity, ");
|
1995-01-31 06:29:58 +00:00
|
|
|
} else
|
1996-12-22 17:09:17 +00:00
|
|
|
printf("no parity, ");
|
|
|
|
|
|
|
|
printf("CTS/RTS %s.\n", (VarCtsRts? "on" : "off"));
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("fd = %d, modem control = %o\n", modem, mbits);
|
|
|
|
#endif
|
|
|
|
printf("connect count: %d\n", connect_count);
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef TIOCOUTQ
|
1997-05-10 01:22:19 +00:00
|
|
|
if (ioctl(modem, TIOCOUTQ, &nb) > 0)
|
|
|
|
printf("outq: %d\n", nb);
|
|
|
|
else
|
|
|
|
printf("outq: ioctl probe failed.\n");
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1995-09-02 17:20:54 +00:00
|
|
|
printf("outqlen: %d\n", ModemQlen());
|
1995-01-31 06:29:58 +00:00
|
|
|
printf("DialScript = %s\n", VarDialScript);
|
|
|
|
printf("LoginScript = %s\n", VarLoginScript);
|
1996-03-08 09:03:09 +00:00
|
|
|
printf("PhoneNumber(s) = %s\n", VarPhoneList);
|
1995-01-31 06:29:58 +00:00
|
|
|
return(1);
|
|
|
|
}
|