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-12-18 01:10:13 +00:00
|
|
|
* $Id: modem.c,v 1.67 1997/11/22 03:37:41 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/param.h>
|
1997-06-09 03:27:43 +00:00
|
|
|
#include <sys/socket.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <netinet/in.h>
|
1997-06-09 03:27:43 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-02-27 10:57:54 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <fcntl.h>
|
1997-09-22 00:46:56 +00:00
|
|
|
#include <paths.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.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"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "vars.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "chat.h"
|
1997-11-18 14:52:08 +00:00
|
|
|
#include "throughput.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <util.h>
|
|
|
|
#else
|
|
|
|
#include <libutil.h>
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static int mbits; /* Current DCD status */
|
1995-01-31 06:29:58 +00:00
|
|
|
static int connect_count;
|
|
|
|
static struct pppTimer ModemTimer;
|
|
|
|
|
|
|
|
#define Online (mbits & TIOCM_CD)
|
|
|
|
|
|
|
|
static struct mbuf *modemout;
|
1997-08-25 00:29:32 +00:00
|
|
|
static struct mqueue OutputQueues[PRI_LINK + 1];
|
1995-01-31 06:29:58 +00:00
|
|
|
static int dev_is_modem;
|
1997-11-18 14:52:08 +00:00
|
|
|
static struct pppThroughput throughput;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-10-29 01:19:51 +00:00
|
|
|
static void CloseLogicalModem(void);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
Enqueue(struct mqueue * queue, struct mbuf * bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
if (queue->last) {
|
|
|
|
queue->last->pnext = bp;
|
|
|
|
queue->last = bp;
|
|
|
|
} else
|
|
|
|
queue->last = queue->top = bp;
|
|
|
|
queue->qlen++;
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "Enqueue: len = %d\n", queue->qlen);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
1997-08-25 00:29:32 +00:00
|
|
|
Dequeue(struct mqueue * queue)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "Dequeue: len = %d\n", queue->qlen);
|
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;
|
|
|
|
if (queue->qlen)
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "Dequeue: Not zero (%d)!!!\n", queue->qlen);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static struct speeds {
|
1995-02-26 12:18:08 +00:00
|
|
|
int nspeed;
|
|
|
|
speed_t speed;
|
1997-08-25 00:29:32 +00:00
|
|
|
} speeds[] = {
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef B50
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 50, B50, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B75
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 75, B75, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B110
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 110, B110, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B134
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 134, B134, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B150
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 150, B150, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B200
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 200, B200, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B300
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 300, B300, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B600
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 600, B600, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B1200
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 1200, B1200, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B1800
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 1800, B1800, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B2400
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 2400, B2400, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B4800
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 4800, B4800, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B9600
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 9600, B9600, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B19200
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 19200, B19200, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B38400
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 38400, B38400, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1995-02-27 10:57:54 +00:00
|
|
|
#ifndef _POSIX_SOURCE
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef B7200
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 7200, B7200, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B14400
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 14400, B14400, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B28800
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 28800, B28800, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B57600
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 57600, B57600, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B76800
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 76800, B76800, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B115200
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 115200, B115200, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef B230400
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 230400, B230400, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef EXTA
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 19200, EXTA, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef EXTB
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 38400, EXTB, },
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1997-08-25 00:29:32 +00:00
|
|
|
#endif /* _POSIX_SOURCE */
|
1997-11-18 14:52:08 +00:00
|
|
|
{ 0, 0 }
|
1995-02-26 12:18:08 +00:00
|
|
|
};
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static int
|
1997-08-25 00:29:32 +00:00
|
|
|
SpeedToInt(speed_t speed)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
|
|
|
struct speeds *sp;
|
|
|
|
|
|
|
|
for (sp = speeds; sp->nspeed; sp++) {
|
|
|
|
if (sp->speed == speed) {
|
1997-08-25 00:29:32 +00:00
|
|
|
return (sp->nspeed);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
speed_t
|
|
|
|
IntToSpeed(int nspeed)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
|
|
|
struct speeds *sp;
|
|
|
|
|
|
|
|
for (sp = speeds; sp->nspeed; sp++) {
|
|
|
|
if (sp->nspeed == nspeed) {
|
1997-08-25 00:29:32 +00:00
|
|
|
return (sp->speed);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return B0;
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
|
|
|
DownConnection()
|
|
|
|
{
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogPHASE, "Disconnected!\n");
|
1997-10-29 01:19:51 +00:00
|
|
|
LcpDown();
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ModemTimeout() watches DCD signal and notifies if it's status is changed.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
1997-11-22 03:37:54 +00:00
|
|
|
ModemTimeout(void *data)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
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) {
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogPHASE, "ioctl error (%s)!\n", strerror(errno));
|
|
|
|
DownConnection();
|
|
|
|
return;
|
1997-05-19 02:00:16 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
mbits = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
change = ombits ^ mbits;
|
|
|
|
if (change & TIOCM_CD) {
|
|
|
|
if (Online) {
|
1997-10-29 01:19:51 +00:00
|
|
|
LogPrintf(LogDEBUG, "ModemTimeout: offline -> online\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
/*
|
|
|
|
* In dedicated mode, start packet mode immediate after we detected
|
|
|
|
* carrier.
|
|
|
|
*/
|
|
|
|
if (mode & MODE_DEDICATED)
|
1995-01-31 06:29:58 +00:00
|
|
|
PacketMode();
|
|
|
|
} else {
|
1997-10-29 01:19:51 +00:00
|
|
|
LogPrintf(LogDEBUG, "ModemTimeout: online -> offline\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
reconnect(RECON_TRUE);
|
1995-01-31 06:29:58 +00:00
|
|
|
DownConnection();
|
|
|
|
}
|
|
|
|
}
|
1997-10-29 01:19:51 +00:00
|
|
|
else
|
|
|
|
LogPrintf(LogDEBUG, "ModemTimeout: Still %sline\n",
|
|
|
|
Online ? "on" : "off");
|
1997-09-16 23:15:16 +00:00
|
|
|
} else if (!Online) {
|
|
|
|
/* mbits was set to zero in OpenModem() */
|
|
|
|
mbits = TIOCM_CD;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
StartModemTimer(void)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
StopTimer(&ModemTimer);
|
|
|
|
ModemTimer.state = TIMER_STOPPED;
|
|
|
|
ModemTimer.load = SECTICKS;
|
|
|
|
ModemTimer.func = ModemTimeout;
|
1997-09-16 23:15:16 +00:00
|
|
|
LogPrintf(LogDEBUG, "ModemTimer using ModemTimeout() - %p\n", ModemTimeout);
|
1995-01-31 06:29:58 +00:00
|
|
|
StartTimer(&ModemTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct parity {
|
1997-11-22 03:37:54 +00:00
|
|
|
const char *name;
|
|
|
|
const char *name1;
|
1997-08-25 00:29:32 +00:00
|
|
|
int set;
|
1997-11-22 03:37:54 +00:00
|
|
|
} validparity[] = {
|
|
|
|
{ "even", "P_EVEN", CS7 | PARENB },
|
|
|
|
{ "odd", "P_ODD", CS7 | PARENB | PARODD },
|
|
|
|
{ "none", "P_ZERO", CS8 },
|
|
|
|
{ NULL, 0 },
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static int
|
1997-11-22 03:37:54 +00:00
|
|
|
GetParityValue(const char *str)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct parity *pp;
|
|
|
|
|
|
|
|
for (pp = validparity; pp->name; pp++) {
|
|
|
|
if (strcasecmp(pp->name, str) == 0 ||
|
|
|
|
strcasecmp(pp->name1, str) == 0) {
|
1997-06-09 03:27:43 +00:00
|
|
|
return VarParity = pp->set;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-11-22 03:37:54 +00:00
|
|
|
ChangeParity(const char *str)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = GetParityValue(str);
|
|
|
|
if (val > 0) {
|
|
|
|
VarParity = val;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(modem, &rstio);
|
1997-08-25 00:29:32 +00:00
|
|
|
rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
|
1995-01-31 06:29:58 +00:00
|
|
|
rstio.c_cflag |= val;
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSADRAIN, &rstio);
|
1997-06-09 03:27:43 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "ChangeParity: %s: Invalid parity\n", str);
|
|
|
|
return -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static int
|
1997-08-25 00:29:32 +00:00
|
|
|
OpenConnection(char *host, char *port)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
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) {
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(&dest.sin_addr.s_addr, hp->h_addr_list[0], 4);
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "OpenConnection: unknown host: %s\n", host);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dest.sin_port = htons(atoi(port));
|
|
|
|
if (dest.sin_port == 0) {
|
|
|
|
sp = getservbyname(port, "tcp");
|
|
|
|
if (sp) {
|
|
|
|
dest.sin_port = sp->s_port;
|
|
|
|
} else {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "OpenConnection: unknown service: %s\n", port);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1997-10-29 01:19:51 +00:00
|
|
|
LogPrintf(LogPHASE, "Connecting to %s:%s\n", host, port);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
return (sock);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (connect(sock, (struct sockaddr *) & dest, sizeof(dest)) < 0) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "OpenConnection: connection failed.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-06-11 03:57:51 +00:00
|
|
|
LogPrintf(LogDEBUG, "OpenConnection: modem fd is %d.\n", sock);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (sock);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-09-22 00:46:56 +00:00
|
|
|
static char fn[MAXPATHLEN];
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static int
|
1997-11-22 03:37:54 +00:00
|
|
|
LockModem(void)
|
1997-09-22 00:46:56 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
FILE *lockfile;
|
|
|
|
|
|
|
|
if (*VarDevice != '/')
|
|
|
|
return 0;
|
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
if (!(mode & MODE_DIRECT) && (res = ID0uu_lock(VarBaseDevice)) != UU_LOCK_OK) {
|
1997-09-22 00:46:56 +00:00
|
|
|
if (res == UU_LOCK_INUSE)
|
|
|
|
LogPrintf(LogPHASE, "Modem %s is in use\n", VarDevice);
|
|
|
|
else
|
|
|
|
LogPrintf(LogPHASE, "Modem %s is in use: uu_lock: %s\n",
|
|
|
|
VarDevice, uu_lockerr(res));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, VarBaseDevice);
|
1997-11-09 06:22:49 +00:00
|
|
|
lockfile = ID0fopen(fn, "w");
|
|
|
|
if (lockfile != NULL) {
|
1997-09-22 00:46:56 +00:00
|
|
|
fprintf(lockfile, "tun%d\n", tunno);
|
|
|
|
fclose(lockfile);
|
|
|
|
} else
|
|
|
|
LogPrintf(LogALERT, "Warning: Can't create %s: %s\n", fn, strerror(errno));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
UnlockModem(void)
|
1997-09-22 00:46:56 +00:00
|
|
|
{
|
|
|
|
if (*VarDevice != '/')
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, VarBaseDevice);
|
1997-11-09 06:22:49 +00:00
|
|
|
if (ID0unlink(fn) == -1)
|
1997-09-22 00:46:56 +00:00
|
|
|
LogPrintf(LogALERT, "Warning: Can't remove %s: %s\n", fn, strerror(errno));
|
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
if (!(mode & MODE_DIRECT) && ID0uu_unlock(VarBaseDevice) == -1)
|
1997-09-22 00:46:56 +00:00
|
|
|
LogPrintf(LogALERT, "Warning: Can't uu_unlock %s\n", fn);
|
|
|
|
}
|
|
|
|
|
1997-10-29 01:19:51 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
HaveModem(void)
|
1997-10-29 01:19:51 +00:00
|
|
|
{
|
1997-11-18 14:52:08 +00:00
|
|
|
throughput_start(&throughput);
|
1997-10-29 01:19:51 +00:00
|
|
|
connect_count++;
|
|
|
|
LogPrintf(LogPHASE, "Connected!\n");
|
|
|
|
}
|
|
|
|
|
1996-03-29 15:24:04 +00:00
|
|
|
static struct termios modemios;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
1997-11-08 00:28:11 +00:00
|
|
|
OpenModem()
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int oldflag;
|
1997-11-22 03:37:54 +00:00
|
|
|
char *host, *port;
|
|
|
|
char *cp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-09-16 23:15:16 +00:00
|
|
|
if (modem >= 0)
|
|
|
|
LogPrintf(LogDEBUG, "OpenModem: Modem is already open!\n");
|
|
|
|
/* We're going back into "term" mode */
|
|
|
|
else if (mode & MODE_DIRECT) {
|
1997-11-22 03:37:54 +00:00
|
|
|
struct cmdargs arg;
|
|
|
|
arg.cmd = NULL;
|
|
|
|
arg.data = (const void *)VAR_DEVICE;
|
1997-09-22 00:46:56 +00:00
|
|
|
if (isatty(0)) {
|
|
|
|
LogPrintf(LogDEBUG, "OpenModem(direct): Modem is a tty\n");
|
|
|
|
cp = ttyname(0);
|
1997-11-22 03:37:54 +00:00
|
|
|
arg.argc = 1;
|
|
|
|
arg.argv = (char const *const *)&cp;
|
|
|
|
SetVariable(&arg);
|
1997-09-22 00:46:56 +00:00
|
|
|
if (LockModem() == -1) {
|
|
|
|
close(0);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-09-23 22:07:51 +00:00
|
|
|
modem = 0;
|
1997-11-18 14:52:08 +00:00
|
|
|
HaveModem();
|
1997-09-22 00:46:56 +00:00
|
|
|
} else {
|
|
|
|
LogPrintf(LogDEBUG, "OpenModem(direct): Modem is not a tty\n");
|
1997-11-22 03:37:54 +00:00
|
|
|
arg.argc = 0;
|
|
|
|
arg.argv = NULL;
|
|
|
|
SetVariable(&arg);
|
1997-10-29 01:19:51 +00:00
|
|
|
/* We don't call ModemTimeout() with this type of connection */
|
1997-11-18 14:52:08 +00:00
|
|
|
HaveModem();
|
1997-09-23 22:07:51 +00:00
|
|
|
return modem = 0;
|
1997-09-22 00:46:56 +00:00
|
|
|
}
|
1997-09-16 23:15:16 +00:00
|
|
|
} else {
|
1997-05-10 03:39:57 +00:00
|
|
|
if (strncmp(VarDevice, "/dev/", 5) == 0) {
|
1997-09-22 00:46:56 +00:00
|
|
|
if (LockModem() == -1)
|
|
|
|
return (-1);
|
1997-11-09 06:22:49 +00:00
|
|
|
modem = ID0open(VarDevice, O_RDWR | O_NONBLOCK);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (modem < 0) {
|
1997-09-16 23:15:16 +00:00
|
|
|
LogPrintf(LogERROR, "OpenModem failed: %s: %s\n", VarDevice,
|
|
|
|
strerror(errno));
|
1997-09-22 00:46:56 +00:00
|
|
|
UnlockModem();
|
1997-09-16 23:15:16 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-10-29 01:19:51 +00:00
|
|
|
HaveModem();
|
1997-09-16 23:15:16 +00:00
|
|
|
LogPrintf(LogDEBUG, "OpenModem: Modem is %s\n", VarDevice);
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1997-10-26 01:04:02 +00:00
|
|
|
/* PPP over TCP */
|
|
|
|
cp = strchr(VarDevice, ':');
|
1995-01-31 06:29:58 +00:00
|
|
|
if (cp) {
|
1997-11-22 03:37:54 +00:00
|
|
|
*cp = '\0';
|
1995-01-31 06:29:58 +00:00
|
|
|
host = VarDevice;
|
1997-08-25 00:29:32 +00:00
|
|
|
port = cp + 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
if (*host && *port) {
|
|
|
|
modem = OpenConnection(host, port);
|
1997-08-25 00:29:32 +00:00
|
|
|
*cp = ':'; /* Don't destroy VarDevice */
|
|
|
|
if (modem < 0)
|
|
|
|
return (-1);
|
1997-10-29 01:19:51 +00:00
|
|
|
HaveModem();
|
1997-09-16 23:15:16 +00:00
|
|
|
LogPrintf(LogDEBUG, "OpenModem: Modem is socket %s\n", VarDevice);
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1997-08-25 00:29:32 +00:00
|
|
|
*cp = ':'; /* Don't destroy VarDevice */
|
1997-09-16 23:15:16 +00:00
|
|
|
LogPrintf(LogERROR, "Invalid host:port: \"%s\"\n", VarDevice);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-09-16 23:15:16 +00:00
|
|
|
} else {
|
|
|
|
LogPrintf(LogERROR,
|
|
|
|
"Device (%s) must be in /dev or be a host:port pair\n",
|
|
|
|
VarDevice);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1997-09-16 23:15:16 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-08-25 00:29:32 +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-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-09-16 23:15:16 +00:00
|
|
|
mbits = 0;
|
1995-02-26 12:18:08 +00:00
|
|
|
dev_is_modem = isatty(modem) || DEV_IS_SYNC;
|
|
|
|
if (DEV_IS_SYNC)
|
1997-10-24 22:36:31 +00:00
|
|
|
nointr_sleep(1);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (dev_is_modem && !DEV_IS_SYNC) {
|
|
|
|
tcgetattr(modem, &rstio);
|
1996-03-29 15:24:04 +00:00
|
|
|
modemios = rstio;
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "OpenModem: modem = %d\n", modem);
|
|
|
|
LogPrintf(LogDEBUG, "OpenModem: modem (get): iflag = %x, oflag = %x,"
|
|
|
|
" cflag = %x\n", rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
1996-03-27 21:40:55 +00:00
|
|
|
cfmakeraw(&rstio);
|
1996-12-22 17:09:17 +00:00
|
|
|
if (VarCtsRts)
|
1997-08-25 00:29:32 +00:00
|
|
|
rstio.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
|
1996-12-22 17:09:17 +00:00
|
|
|
else {
|
1997-08-25 00:29:32 +00:00
|
|
|
rstio.c_cflag |= CLOCAL;
|
|
|
|
rstio.c_iflag |= IXOFF;
|
1996-12-22 17:09:17 +00:00
|
|
|
}
|
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) {
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* If we are working as direct mode, don't change tty speed.
|
|
|
|
*/
|
1997-08-25 00:29:32 +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) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "Unable to set modem speed (modem %d to %d)\n",
|
1997-05-10 01:22:19 +00:00
|
|
|
modem, VarSpeed);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSADRAIN, &rstio);
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem (put): iflag = %x, oflag = %x, cflag = %x\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (!(mode & MODE_DIRECT))
|
1997-09-16 23:15:16 +00:00
|
|
|
if (ioctl(modem, TIOCMGET, &mbits)) {
|
|
|
|
LogPrintf(LogERROR, "OpenModem: Cannot get modem status: %s\n",
|
|
|
|
strerror(errno));
|
1997-10-29 01:19:51 +00:00
|
|
|
CloseLogicalModem();
|
1997-09-22 00:46:56 +00:00
|
|
|
return (-1);
|
1997-09-16 23:15:16 +00:00
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "OpenModem: modem control = %o\n", mbits);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
oldflag = fcntl(modem, F_GETFL, 0);
|
1997-09-16 23:15:16 +00:00
|
|
|
if (oldflag < 0) {
|
|
|
|
LogPrintf(LogERROR, "OpenModem: Cannot get modem flags: %s\n",
|
|
|
|
strerror(errno));
|
1997-10-29 01:19:51 +00:00
|
|
|
CloseLogicalModem();
|
1997-09-22 00:46:56 +00:00
|
|
|
return (-1);
|
1997-09-16 23:15:16 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
(void) fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
StartModemTimer();
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
return (modem);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ModemSpeed()
|
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
tcgetattr(modem, &rstio);
|
1997-08-25 00:29:32 +00:00
|
|
|
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
|
1997-11-08 00:28:11 +00:00
|
|
|
RawModem()
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int oldflag;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (!isatty(modem) || DEV_IS_SYNC)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1997-03-10 06:21:02 +00:00
|
|
|
if (!(mode & MODE_DIRECT) && modem >= 0 && !Online) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "RawModem: mode = %d, modem = %d, mbits = %x\n", mode, modem, mbits);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
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)
|
1997-08-25 00:29:32 +00:00
|
|
|
rstio.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
|
1996-12-22 17:09:17 +00:00
|
|
|
else
|
1997-08-25 00:29:32 +00:00
|
|
|
rstio.c_cflag |= CLOCAL;
|
1996-12-22 17:09:17 +00:00
|
|
|
|
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)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
|
|
|
(void) fcntl(modem, F_SETFL, oldflag | O_NONBLOCK);
|
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
UnrawModem(void)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
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)
|
1997-08-25 00:29:32 +00:00
|
|
|
return;
|
|
|
|
(void) fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-18 14:52:08 +00:00
|
|
|
void
|
|
|
|
ModemAddInOctets(int n)
|
1997-10-29 01:19:51 +00:00
|
|
|
{
|
1997-11-18 14:52:08 +00:00
|
|
|
throughput_addin(&throughput, n);
|
1997-10-29 01:19:51 +00:00
|
|
|
}
|
|
|
|
|
1997-11-18 14:52:08 +00:00
|
|
|
void
|
|
|
|
ModemAddOutOctets(int n)
|
1997-10-29 01:19:51 +00:00
|
|
|
{
|
1997-11-18 14:52:08 +00:00
|
|
|
throughput_addout(&throughput, n);
|
1997-10-29 01:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
ClosePhysicalModem(void)
|
1997-10-29 01:19:51 +00:00
|
|
|
{
|
1997-11-18 14:52:08 +00:00
|
|
|
LogPrintf(LogDEBUG, "ClosePhysicalModem\n");
|
1997-10-29 01:19:51 +00:00
|
|
|
close(modem);
|
1997-11-18 14:52:08 +00:00
|
|
|
modem = -1;
|
|
|
|
throughput_log(&throughput, LogPHASE, "Modem");
|
1997-10-29 01:19:51 +00:00
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
HangupModem(int flag)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1995-02-26 12:18:08 +00:00
|
|
|
struct termios tio;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-18 14:52:08 +00:00
|
|
|
LogPrintf(LogDEBUG, "Hangup modem (%s)\n", modem >= 0 ? "open" : "closed");
|
1997-10-29 01:19:51 +00:00
|
|
|
|
|
|
|
if (modem < 0)
|
|
|
|
return;
|
|
|
|
|
1997-11-18 14:52:08 +00:00
|
|
|
StopTimer(&ModemTimer);
|
|
|
|
throughput_stop(&throughput);
|
|
|
|
|
1997-10-29 01:19:51 +00:00
|
|
|
if (TermMode) {
|
|
|
|
LogPrintf(LogDEBUG, "HangupModem: Not in 'term' mode\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (!isatty(modem)) {
|
|
|
|
mbits &= ~TIOCM_DTR;
|
1997-10-29 01:19:51 +00:00
|
|
|
ClosePhysicalModem();
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
1997-09-16 23:15:16 +00:00
|
|
|
|
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
|
|
|
tcgetattr(modem, &tio);
|
1997-05-10 01:22:19 +00:00
|
|
|
if (cfsetspeed(&tio, B0) == -1) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "Unable to set modem to speed 0\n");
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
tcsetattr(modem, TCSANOW, &tio);
|
1997-10-24 22:36:31 +00:00
|
|
|
nointr_sleep(1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1997-11-09 14:18:55 +00:00
|
|
|
if (modem >= 0) {
|
|
|
|
char ScriptBuffer[SCRIPT_LEN];
|
1997-07-14 01:41:35 +00:00
|
|
|
|
1997-12-18 01:10:13 +00:00
|
|
|
strncpy(ScriptBuffer, VarHangupScript, sizeof(ScriptBuffer));
|
|
|
|
ScriptBuffer[sizeof(ScriptBuffer) - 1] = '\0';
|
1997-11-18 14:52:08 +00:00
|
|
|
LogPrintf(LogDEBUG, "HangupModem: Script: %s\n", ScriptBuffer);
|
1997-11-09 14:18:55 +00:00
|
|
|
if (flag || !(mode & MODE_DEDICATED)) {
|
1997-07-14 01:41:35 +00:00
|
|
|
DoChat(ScriptBuffer);
|
|
|
|
tcflush(modem, TCIOFLUSH);
|
1997-11-08 00:28:11 +00:00
|
|
|
UnrawModem();
|
1997-10-29 01:19:51 +00:00
|
|
|
CloseLogicalModem();
|
1997-11-09 14:18:55 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we are working as dedicated mode, never close it until we are
|
|
|
|
* directed to quit program.
|
|
|
|
*/
|
|
|
|
mbits |= TIOCM_DTR;
|
|
|
|
ioctl(modem, TIOCMSET, &mbits);
|
|
|
|
DoChat(ScriptBuffer);
|
1995-04-16 13:38:39 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-29 01:19:51 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
CloseLogicalModem(void)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-18 14:52:08 +00:00
|
|
|
LogPrintf(LogDEBUG, "CloseLogicalModem\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
if (modem >= 0) {
|
1997-10-29 01:19:51 +00:00
|
|
|
ClosePhysicalModem();
|
1997-09-22 23:59:16 +00:00
|
|
|
if (Utmp) {
|
|
|
|
struct utmp ut;
|
|
|
|
strncpy(ut.ut_line, VarBaseDevice, sizeof(ut.ut_line)-1);
|
|
|
|
ut.ut_line[sizeof(ut.ut_line)-1] = '\0';
|
|
|
|
if (logout(ut.ut_line))
|
|
|
|
logwtmp(ut.ut_line, "", "");
|
|
|
|
else
|
1997-10-29 01:19:51 +00:00
|
|
|
LogPrintf(LogERROR, "CloseLogicalModem: No longer logged in on %s\n",
|
1997-09-22 23:59:16 +00:00
|
|
|
ut.ut_line);
|
|
|
|
Utmp = 0;
|
|
|
|
}
|
1997-09-22 00:46:56 +00:00
|
|
|
UnlockModem();
|
1995-04-16 13:38:39 +00:00
|
|
|
}
|
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
|
1997-11-22 03:37:54 +00:00
|
|
|
WriteModem(int pri, const char *ptr, int count)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
bp = mballoc(count, MB_MODEM);
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), ptr, count);
|
1996-01-30 11:08:50 +00:00
|
|
|
|
1997-08-25 00:29:32 +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
|
1997-08-25 00:29:32 +00:00
|
|
|
ModemOutput(int pri, struct mbuf * bp)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
for (i = PRI_NORMAL; i <= PRI_LINK; i++) {
|
|
|
|
queue = &OutputQueues[i];
|
|
|
|
len += queue->qlen;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (len);
|
1995-07-08 17:46:56 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
ModemStartOutput(int fd)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mqueue *queue;
|
1997-05-10 01:22:19 +00:00
|
|
|
int nb, nw;
|
|
|
|
int i;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
if (modemout == NULL && ModemQlen() == 0)
|
|
|
|
IpStartOutput();
|
1995-01-31 06:29:58 +00:00
|
|
|
if (modemout == NULL) {
|
1996-01-30 11:08:50 +00:00
|
|
|
i = PRI_LINK;
|
|
|
|
for (queue = &OutputQueues[PRI_LINK]; queue >= OutputQueues; queue--) {
|
1995-01-31 06:29:58 +00:00
|
|
|
if (queue->top) {
|
|
|
|
modemout = Dequeue(queue);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (LogIsKept(LogDEBUG)) {
|
1997-06-09 03:27:43 +00:00
|
|
|
if (i > PRI_NORMAL) {
|
|
|
|
struct mqueue *q;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
q = &OutputQueues[0];
|
|
|
|
LogPrintf(LogDEBUG, "ModemStartOutput: Output from queue %d,"
|
|
|
|
" normal has %d\n", i, q->qlen);
|
|
|
|
}
|
|
|
|
LogPrintf(LogDEBUG, "ModemStartOutput: Dequeued %d\n", i);
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
1996-01-30 11:08:50 +00:00
|
|
|
i--;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (modemout) {
|
|
|
|
nb = modemout->cnt;
|
1997-08-25 00:29:32 +00:00
|
|
|
if (nb > 1600)
|
|
|
|
nb = 1600;
|
1995-01-31 06:29:58 +00:00
|
|
|
nw = write(fd, MBUF_CTOP(modemout), nb);
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "ModemStartOutput: wrote: %d(%d)\n", nw, nb);
|
|
|
|
LogDumpBuff(LogDEBUG, "ModemStartOutput: modem write",
|
|
|
|
MBUF_CTOP(modemout), nb);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (nw > 0) {
|
|
|
|
modemout->cnt -= nw;
|
|
|
|
modemout->offset += nw;
|
|
|
|
if (modemout->cnt == 0) {
|
|
|
|
modemout = mbfree(modemout);
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "ModemStartOutput: mbfree\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-27 10:57:54 +00:00
|
|
|
} else if (nw < 0) {
|
1997-06-11 03:57:51 +00:00
|
|
|
if (errno != EAGAIN) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "modem write (%d): %s\n", modem, strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
reconnect(RECON_TRUE);
|
1997-06-11 03:57:51 +00:00
|
|
|
DownConnection();
|
|
|
|
}
|
1995-02-27 10:57:54 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
DialModem()
|
|
|
|
{
|
1997-11-09 14:18:55 +00:00
|
|
|
char ScriptBuffer[SCRIPT_LEN];
|
1997-05-29 02:29:13 +00:00
|
|
|
int excode;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
strncpy(ScriptBuffer, VarDialScript, sizeof(ScriptBuffer) - 1);
|
|
|
|
ScriptBuffer[sizeof(ScriptBuffer) - 1] = '\0';
|
1997-06-23 23:10:13 +00:00
|
|
|
if ((excode = DoChat(ScriptBuffer)) > 0) {
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm)
|
|
|
|
fprintf(VarTerm, "dial OK!\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
strncpy(ScriptBuffer, VarLoginScript, sizeof(ScriptBuffer) - 1);
|
1997-06-23 23:10:13 +00:00
|
|
|
if ((excode = DoChat(ScriptBuffer)) > 0) {
|
1997-08-23 23:14:24 +00:00
|
|
|
VarAltPhone = NULL;
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm)
|
1997-08-25 00:29:32 +00:00
|
|
|
fprintf(VarTerm, "login OK!\n");
|
1997-05-29 02:29:13 +00:00
|
|
|
return EX_DONE;
|
1997-06-23 23:10:13 +00:00
|
|
|
} else if (excode == -1)
|
|
|
|
excode = EX_SIG;
|
|
|
|
else {
|
|
|
|
LogPrintf(LogWARN, "DialModem: login failed.\n");
|
|
|
|
excode = EX_NOLOGIN;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-11-22 03:37:54 +00:00
|
|
|
ModemTimeout(NULL); /* Dummy call to check modem status */
|
1997-06-23 23:10:13 +00:00
|
|
|
} else if (excode == -1)
|
|
|
|
excode = EX_SIG;
|
1996-03-08 09:03:09 +00:00
|
|
|
else {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogWARN, "DialModem: 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-08-25 00:29:32 +00:00
|
|
|
return (excode);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-11-22 03:37:54 +00:00
|
|
|
ShowModemStatus(struct cmdargs const *arg)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-22 03:37:54 +00:00
|
|
|
const char *dev;
|
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
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
if (!VarTerm)
|
|
|
|
return 1;
|
|
|
|
|
1997-09-22 00:55:46 +00:00
|
|
|
dev = *VarDevice ? VarDevice : "network";
|
1997-09-16 23:15:16 +00:00
|
|
|
|
|
|
|
fprintf(VarTerm, "device: %s speed: ", dev);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (DEV_IS_SYNC)
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "sync\n");
|
1995-02-26 12:18:08 +00:00
|
|
|
else
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "%d\n", VarSpeed);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (VarParity & CSIZE) {
|
|
|
|
case CS7:
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "cs7, ");
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case CS8:
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "cs8, ");
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (VarParity & PARENB) {
|
|
|
|
if (VarParity & PARODD)
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "odd parity, ");
|
1995-01-31 06:29:58 +00:00
|
|
|
else
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "even parity, ");
|
1995-01-31 06:29:58 +00:00
|
|
|
} else
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "no parity, ");
|
1996-12-22 17:09:17 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
fprintf(VarTerm, "CTS/RTS %s.\n", (VarCtsRts ? "on" : "off"));
|
1996-12-22 17:09:17 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
if (LogIsKept(LogDEBUG))
|
|
|
|
fprintf(VarTerm, "fd = %d, modem control = %o\n", modem, mbits);
|
|
|
|
fprintf(VarTerm, "connect count: %d\n", connect_count);
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef TIOCOUTQ
|
1997-07-24 02:42:34 +00:00
|
|
|
if (modem >= 0)
|
1997-11-18 08:49:03 +00:00
|
|
|
if (ioctl(modem, TIOCOUTQ, &nb) >= 0)
|
1997-07-24 02:42:34 +00:00
|
|
|
fprintf(VarTerm, "outq: %d\n", nb);
|
|
|
|
else
|
|
|
|
fprintf(VarTerm, "outq: ioctl probe failed: %s\n", strerror(errno));
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "outqlen: %d\n", ModemQlen());
|
|
|
|
fprintf(VarTerm, "DialScript = %s\n", VarDialScript);
|
|
|
|
fprintf(VarTerm, "LoginScript = %s\n", VarLoginScript);
|
|
|
|
fprintf(VarTerm, "PhoneNumber(s) = %s\n", VarPhoneList);
|
|
|
|
|
1997-11-18 14:52:08 +00:00
|
|
|
fprintf(VarTerm, "\n");
|
|
|
|
throughput_disp(&throughput, VarTerm);
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|