1998-01-29 00:44:16 +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.
|
|
|
|
*
|
1998-02-27 21:46:00 +00:00
|
|
|
* $Id: modem.c,v 1.77.2.25 1998/02/23 00:38:36 brian Exp $
|
1998-01-29 00:44:16 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
1998-01-29 00:49:32 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <util.h>
|
|
|
|
#else
|
|
|
|
#include <libutil.h>
|
|
|
|
#endif
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "id.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "ip.h"
|
|
|
|
#include "modem.h"
|
|
|
|
#include "loadalias.h"
|
|
|
|
#include "vars.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "throughput.h"
|
1998-02-02 19:33:40 +00:00
|
|
|
#include "async.h"
|
1998-02-06 02:23:48 +00:00
|
|
|
#include "bundle.h"
|
1998-01-30 19:46:07 +00:00
|
|
|
#include "link.h"
|
1998-02-09 19:21:11 +00:00
|
|
|
#include "descriptor.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
#include "physical.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "prompt.h"
|
1998-02-13 05:10:26 +00:00
|
|
|
#include "chat.h"
|
1998-02-23 00:38:44 +00:00
|
|
|
#include "ccp.h"
|
1998-02-16 19:10:03 +00:00
|
|
|
#include "datalink.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1998-02-23 00:38:44 +00:00
|
|
|
static void modem_StartOutput(struct link *, struct bundle *);
|
1998-02-06 02:22:28 +00:00
|
|
|
static int modem_IsActive(struct link *);
|
|
|
|
static void modem_Hangup(struct link *, int);
|
1998-02-06 02:22:52 +00:00
|
|
|
static void modem_Destroy(struct link *);
|
1998-02-10 03:22:05 +00:00
|
|
|
static void modem_DescriptorRead(struct descriptor *, struct bundle *,
|
|
|
|
const fd_set *);
|
1998-02-13 05:10:26 +00:00
|
|
|
static int modem_UpdateSet(struct descriptor *, fd_set *, fd_set *, fd_set *,
|
|
|
|
int *);
|
1998-01-30 19:46:07 +00:00
|
|
|
|
1998-02-06 02:22:52 +00:00
|
|
|
struct physical *
|
1998-02-23 00:38:44 +00:00
|
|
|
modem_Create(const char *name)
|
1998-02-06 02:22:52 +00:00
|
|
|
{
|
|
|
|
struct physical *p;
|
|
|
|
|
|
|
|
p = (struct physical *)malloc(sizeof(struct physical));
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
1998-02-21 01:45:26 +00:00
|
|
|
|
1998-02-06 02:22:52 +00:00
|
|
|
p->link.type = PHYSICAL_LINK;
|
|
|
|
p->link.name = strdup(name);
|
|
|
|
p->link.len = sizeof *p;
|
1998-02-21 01:45:26 +00:00
|
|
|
memset(&p->link.throughput, '\0', sizeof p->link.throughput);
|
|
|
|
memset(&p->link.Timer, '\0', sizeof p->link.Timer);
|
|
|
|
memset(p->link.Queue, '\0', sizeof p->link.Queue);
|
|
|
|
memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
|
|
|
|
memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
|
1998-02-06 02:22:52 +00:00
|
|
|
p->link.StartOutput = modem_StartOutput;
|
|
|
|
p->link.IsActive = modem_IsActive;
|
|
|
|
p->link.Close = modem_Hangup;
|
|
|
|
p->link.Destroy = modem_Destroy;
|
1998-02-21 01:45:26 +00:00
|
|
|
|
1998-02-09 19:21:11 +00:00
|
|
|
p->desc.type = PHYSICAL_DESCRIPTOR;
|
1998-02-13 05:10:26 +00:00
|
|
|
p->desc.UpdateSet = modem_UpdateSet;
|
1998-02-09 19:21:11 +00:00
|
|
|
p->desc.IsSet = Physical_IsSet;
|
|
|
|
p->desc.Read = modem_DescriptorRead;
|
|
|
|
p->desc.Write = Physical_DescriptorWrite;
|
|
|
|
|
1998-02-21 01:45:26 +00:00
|
|
|
hdlc_Init(&p->hdlc);
|
|
|
|
async_Init(&p->async);
|
|
|
|
|
|
|
|
p->fd = -1;
|
|
|
|
p->mbits = 0;
|
|
|
|
p->abort = 0;
|
|
|
|
p->dev_is_modem = 0;
|
|
|
|
p->out = NULL;
|
|
|
|
p->connect_count = 0;
|
|
|
|
|
|
|
|
p->cfg.is_direct = 0; /* not yet used */
|
|
|
|
p->cfg.is_dedicated = 0; /* not yet used */
|
|
|
|
p->cfg.rts_cts = MODEM_CTSRTS;
|
|
|
|
p->cfg.speed = MODEM_SPEED;
|
|
|
|
p->cfg.parity = CS8;
|
|
|
|
|
1998-02-06 02:22:52 +00:00
|
|
|
return p;
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
/* XXX-ML this should probably change when we add support for other
|
|
|
|
types of devices */
|
1998-02-02 19:32:16 +00:00
|
|
|
#define Online(modem) ((modem)->mbits & TIOCM_CD)
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-06 02:22:28 +00:00
|
|
|
static void modem_LogicalClose(struct physical *);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
static struct speeds {
|
|
|
|
int nspeed;
|
|
|
|
speed_t speed;
|
1998-02-06 02:22:28 +00:00
|
|
|
} speeds[] = {
|
1998-01-29 00:44:16 +00:00
|
|
|
#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
|
|
|
|
#ifndef _POSIX_SOURCE
|
|
|
|
#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 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
SpeedToInt(speed_t speed)
|
|
|
|
{
|
|
|
|
struct speeds *sp;
|
|
|
|
|
|
|
|
for (sp = speeds; sp->nspeed; sp++) {
|
|
|
|
if (sp->speed == speed) {
|
|
|
|
return (sp->nspeed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
speed_t
|
|
|
|
IntToSpeed(int nspeed)
|
|
|
|
{
|
|
|
|
struct speeds *sp;
|
|
|
|
|
|
|
|
for (sp = speeds; sp->nspeed; sp++) {
|
|
|
|
if (sp->nspeed == nspeed) {
|
|
|
|
return (sp->speed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return B0;
|
|
|
|
}
|
|
|
|
|
1998-02-02 19:32:16 +00:00
|
|
|
struct timeoutArg {
|
|
|
|
struct bundle *bundle;
|
|
|
|
struct physical *modem;
|
|
|
|
};
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
/*
|
1998-02-06 02:22:28 +00:00
|
|
|
* modem_Timeout() watches DCD signal and notifies if it's status is changed.
|
1998-01-29 00:44:16 +00:00
|
|
|
*
|
|
|
|
*/
|
1998-02-02 19:32:16 +00:00
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Timeout(void *data)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-02 19:32:16 +00:00
|
|
|
struct timeoutArg *to = data;
|
|
|
|
int ombits = to->modem->mbits;
|
1998-01-29 00:44:16 +00:00
|
|
|
int change;
|
|
|
|
|
1998-02-02 19:32:16 +00:00
|
|
|
StopTimer(&to->modem->link.Timer);
|
|
|
|
StartTimer(&to->modem->link.Timer);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-06 02:24:29 +00:00
|
|
|
if (to->modem->abort) {
|
|
|
|
/* Something went horribly wrong */
|
|
|
|
to->modem->abort = 0;
|
1998-02-16 00:01:12 +00:00
|
|
|
link_Close(&to->modem->link, to->bundle, 0, 0);
|
1998-02-06 02:24:29 +00:00
|
|
|
} else if (to->modem->dev_is_modem) {
|
1998-02-02 19:32:16 +00:00
|
|
|
if (to->modem->fd >= 0) {
|
|
|
|
if (ioctl(to->modem->fd, TIOCMGET, &to->modem->mbits) < 0) {
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogPHASE, "ioctl error (%s)!\n", strerror(errno));
|
1998-02-16 00:01:12 +00:00
|
|
|
link_Close(&to->modem->link, to->bundle, 0, 0);
|
1998-01-29 00:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
1998-02-02 19:32:16 +00:00
|
|
|
to->modem->mbits = 0;
|
|
|
|
change = ombits ^ to->modem->mbits;
|
1998-01-29 00:44:16 +00:00
|
|
|
if (change & TIOCM_CD) {
|
1998-02-02 19:32:16 +00:00
|
|
|
if (to->modem->mbits & TIOCM_CD) {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Timeout: offline -> online\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
/*
|
|
|
|
* In dedicated mode, start packet mode immediate after we detected
|
|
|
|
* carrier.
|
|
|
|
*/
|
|
|
|
} else {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Timeout: online -> offline\n");
|
1998-02-16 00:01:12 +00:00
|
|
|
link_Close(&to->modem->link, to->bundle, 0, 0);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Timeout: Still %sline\n",
|
1998-02-02 19:32:16 +00:00
|
|
|
Online(to->modem) ? "on" : "off");
|
|
|
|
} else if (!Online(to->modem)) {
|
1998-02-06 02:22:28 +00:00
|
|
|
/* mbits was set to zero in modem_Open() */
|
1998-02-02 19:32:16 +00:00
|
|
|
to->modem->mbits = TIOCM_CD;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_StartTimer(struct bundle *bundle, struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-01-29 00:49:32 +00:00
|
|
|
struct pppTimer *ModemTimer;
|
1998-02-02 19:32:16 +00:00
|
|
|
static struct timeoutArg to;
|
1998-01-29 00:49:32 +00:00
|
|
|
|
1998-02-02 19:32:16 +00:00
|
|
|
to.modem = modem;
|
|
|
|
to.bundle = bundle;
|
1998-01-30 19:46:07 +00:00
|
|
|
ModemTimer = &modem->link.Timer;
|
1998-01-29 00:49:32 +00:00
|
|
|
|
|
|
|
StopTimer(ModemTimer);
|
|
|
|
ModemTimer->state = TIMER_STOPPED;
|
|
|
|
ModemTimer->load = SECTICKS;
|
1998-02-06 02:22:28 +00:00
|
|
|
ModemTimer->func = modem_Timeout;
|
1998-02-02 19:32:16 +00:00
|
|
|
ModemTimer->arg = &to;
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "ModemTimer using modem_Timeout() - %p\n", modem_Timeout);
|
1998-01-29 00:49:32 +00:00
|
|
|
StartTimer(ModemTimer);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct parity {
|
|
|
|
const char *name;
|
|
|
|
const char *name1;
|
|
|
|
int set;
|
|
|
|
} validparity[] = {
|
|
|
|
{ "even", "P_EVEN", CS7 | PARENB },
|
|
|
|
{ "odd", "P_ODD", CS7 | PARENB | PARODD },
|
|
|
|
{ "none", "P_ZERO", CS8 },
|
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
GetParityValue(const char *str)
|
|
|
|
{
|
|
|
|
struct parity *pp;
|
|
|
|
|
|
|
|
for (pp = validparity; pp->name; pp++) {
|
|
|
|
if (strcasecmp(pp->name, str) == 0 ||
|
|
|
|
strcasecmp(pp->name1, str) == 0) {
|
1998-01-29 00:49:32 +00:00
|
|
|
return pp->set;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_SetParity(struct physical *modem, const char *str)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = GetParityValue(str);
|
|
|
|
if (val > 0) {
|
1998-02-17 19:29:03 +00:00
|
|
|
modem->cfg.parity = val;
|
1998-01-29 00:49:32 +00:00
|
|
|
tcgetattr(modem->fd, &rstio);
|
1998-01-29 00:44:16 +00:00
|
|
|
rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
|
|
|
|
rstio.c_cflag |= val;
|
1998-01-29 00:49:32 +00:00
|
|
|
tcsetattr(modem->fd, TCSADRAIN, &rstio);
|
1998-01-29 00:44:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogWARN, "modem_SetParity: %s: Invalid parity\n", str);
|
1998-01-29 00:44:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
OpenConnection(char *host, char *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) {
|
|
|
|
memcpy(&dest.sin_addr.s_addr, hp->h_addr_list[0], 4);
|
|
|
|
} else {
|
|
|
|
LogPrintf(LogWARN, "OpenConnection: 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 {
|
|
|
|
LogPrintf(LogWARN, "OpenConnection: unknown service: %s\n", port);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogPrintf(LogPHASE, "Connecting to %s:%s\n", host, port);
|
|
|
|
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
return (sock);
|
|
|
|
}
|
|
|
|
if (connect(sock, (struct sockaddr *)&dest, sizeof dest) < 0) {
|
|
|
|
LogPrintf(LogWARN, "OpenConnection: connection failed.\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
LogPrintf(LogDEBUG, "OpenConnection: modem fd is %d.\n", sock);
|
|
|
|
return (sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-02-07 20:50:08 +00:00
|
|
|
modem_lock(struct physical *modem, int tunno)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
FILE *lockfile;
|
1998-01-29 00:49:32 +00:00
|
|
|
char fn[MAXPATHLEN];
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
if (*VarDevice != '/')
|
|
|
|
return 0;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_direct &&
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DIRECT) &&
|
|
|
|
#endif
|
|
|
|
(res = ID0uu_lock(VarBaseDevice)) != UU_LOCK_OK) {
|
1998-01-29 00:44:16 +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);
|
|
|
|
lockfile = ID0fopen(fn, "w");
|
|
|
|
if (lockfile != NULL) {
|
|
|
|
fprintf(lockfile, "tun%d\n", tunno);
|
|
|
|
fclose(lockfile);
|
|
|
|
}
|
|
|
|
#ifndef RELEASE_CRUNCH
|
|
|
|
else
|
|
|
|
LogPrintf(LogALERT, "Warning: Can't create %s: %s\n", fn, strerror(errno));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Unlock(struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-01-29 00:49:32 +00:00
|
|
|
char fn[MAXPATHLEN];
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
if (*VarDevice != '/')
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, VarBaseDevice);
|
|
|
|
#ifndef RELEASE_CRUNCH
|
|
|
|
if (ID0unlink(fn) == -1)
|
|
|
|
LogPrintf(LogALERT, "Warning: Can't remove %s: %s\n", fn, strerror(errno));
|
|
|
|
#else
|
|
|
|
ID0unlink(fn);
|
|
|
|
#endif
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_direct &&
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DIRECT) &&
|
|
|
|
#endif
|
|
|
|
ID0uu_unlock(VarBaseDevice) == -1)
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogALERT, "Warning: Can't uu_unlock %s\n", fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Found(struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-01-30 19:46:07 +00:00
|
|
|
throughput_start(&modem->link.throughput);
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->connect_count++;
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogPHASE, "Connected!\n");
|
|
|
|
}
|
|
|
|
|
1998-02-17 19:28:01 +00:00
|
|
|
void
|
|
|
|
modem_SetDeviceName(struct physical *modem, const char *name)
|
|
|
|
{
|
|
|
|
if (name == NULL)
|
|
|
|
name = "";
|
|
|
|
|
|
|
|
if (link_IsActive(&modem->link))
|
|
|
|
LogPrintf(LogWARN,
|
|
|
|
"Cannot change device to \"%s\" when \"%s\" is open\n",
|
|
|
|
name, Physical_GetDevice(modem));
|
|
|
|
else
|
|
|
|
Physical_SetDevice(modem, name);
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
int
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Open(struct physical *modem, struct bundle *bundle)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
int oldflag;
|
|
|
|
char *host, *port;
|
|
|
|
char *cp;
|
|
|
|
char tmpDeviceList[sizeof VarDeviceList];
|
|
|
|
char *tmpDevice;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->fd >= 0)
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Open: Modem is already open!\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
/* We're going back into "term" mode */
|
1998-01-29 00:49:32 +00:00
|
|
|
else if (
|
|
|
|
#ifdef notyet
|
|
|
|
modem->is_direct
|
|
|
|
#else
|
|
|
|
mode & MODE_DIRECT
|
|
|
|
#endif
|
|
|
|
) {
|
1998-01-29 00:44:16 +00:00
|
|
|
if (isatty(STDIN_FILENO)) {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Open(direct): Modem is a tty\n");
|
1998-02-17 19:28:01 +00:00
|
|
|
modem_SetDeviceName(modem, ttyname(STDIN_FILENO));
|
1998-02-07 20:50:08 +00:00
|
|
|
if (modem_lock(modem, bundle->unit) == -1) {
|
1998-01-29 00:44:16 +00:00
|
|
|
close(STDIN_FILENO);
|
|
|
|
return -1;
|
|
|
|
}
|
1998-02-07 22:22:45 +00:00
|
|
|
modem->fd = STDIN_FILENO;
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Found(modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
} else {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Open(direct): Modem is not a tty\n");
|
1998-02-17 19:28:01 +00:00
|
|
|
modem_SetDeviceName(modem, NULL);
|
1998-02-06 02:22:28 +00:00
|
|
|
/* We don't call modem_Timeout() with this type of connection */
|
|
|
|
modem_Found(modem);
|
1998-01-29 00:49:32 +00:00
|
|
|
return modem->fd = STDIN_FILENO;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strncpy(tmpDeviceList, VarDeviceList, sizeof tmpDeviceList - 1);
|
|
|
|
tmpDeviceList[sizeof tmpDeviceList - 1] = '\0';
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
for(tmpDevice=strtok(tmpDeviceList, ","); tmpDevice && (modem->fd < 0);
|
1998-01-29 00:44:16 +00:00
|
|
|
tmpDevice=strtok(NULL,",")) {
|
|
|
|
strncpy(VarDevice, tmpDevice, sizeof VarDevice - 1);
|
|
|
|
VarDevice[sizeof VarDevice - 1]= '\0';
|
|
|
|
VarBaseDevice = strrchr(VarDevice, '/');
|
|
|
|
VarBaseDevice = VarBaseDevice ? VarBaseDevice + 1 : "";
|
|
|
|
|
|
|
|
if (strncmp(VarDevice, "/dev/", 5) == 0) {
|
1998-02-07 20:50:08 +00:00
|
|
|
if (modem_lock(modem, bundle->unit) == -1) {
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->fd = -1;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
else {
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->fd = ID0open(VarDevice, O_RDWR | O_NONBLOCK);
|
|
|
|
if (modem->fd < 0) {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogERROR, "modem_Open failed: %s: %s\n", VarDevice,
|
1998-01-29 00:44:16 +00:00
|
|
|
strerror(errno));
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Unlock(modem);
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->fd = -1;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
else {
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Found(modem);
|
|
|
|
LogPrintf(LogDEBUG, "modem_Open: Modem is %s\n", VarDevice);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* PPP over TCP */
|
|
|
|
cp = strchr(VarDevice, ':');
|
|
|
|
if (cp) {
|
|
|
|
*cp = '\0';
|
|
|
|
host = VarDevice;
|
|
|
|
port = cp + 1;
|
|
|
|
if (*host && *port) {
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->fd = OpenConnection(host, port);
|
1998-01-29 00:44:16 +00:00
|
|
|
*cp = ':'; /* Don't destroy VarDevice */
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->fd < 0)
|
1998-01-29 00:44:16 +00:00
|
|
|
return (-1);
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Found(modem);
|
|
|
|
LogPrintf(LogDEBUG, "modem_Open: Modem is socket %s\n", VarDevice);
|
1998-01-29 00:44:16 +00:00
|
|
|
} else {
|
|
|
|
*cp = ':'; /* Don't destroy VarDevice */
|
|
|
|
LogPrintf(LogERROR, "Invalid host:port: \"%s\"\n", VarDevice);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogPrintf(LogERROR,
|
|
|
|
"Device (%s) must be in /dev or be a host:port pair\n",
|
|
|
|
VarDevice);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->fd < 0)
|
|
|
|
return modem->fd;
|
1998-01-29 00:44:16 +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.
|
|
|
|
*/
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->mbits = 0;
|
|
|
|
modem->dev_is_modem = isatty(modem->fd) || Physical_IsSync(modem);
|
|
|
|
if (Physical_IsSync(modem))
|
1998-01-29 00:44:16 +00:00
|
|
|
nointr_sleep(1);
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->dev_is_modem && !Physical_IsSync(modem)) {
|
|
|
|
tcgetattr(modem->fd, &rstio);
|
|
|
|
modem->ios = rstio;
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Open: modem = %d\n", modem->fd);
|
|
|
|
LogPrintf(LogDEBUG, "modem_Open: modem (get): iflag = %x, oflag = %x,"
|
1998-01-29 00:44:16 +00:00
|
|
|
" cflag = %x\n", rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
|
|
|
cfmakeraw(&rstio);
|
1998-02-17 19:29:03 +00:00
|
|
|
if (modem->cfg.rts_cts)
|
1998-01-29 00:44:16 +00:00
|
|
|
rstio.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
|
|
|
|
else {
|
|
|
|
rstio.c_cflag |= CLOCAL;
|
|
|
|
rstio.c_iflag |= IXOFF;
|
|
|
|
}
|
|
|
|
rstio.c_iflag |= IXON;
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_dedicated
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DEDICATED)
|
|
|
|
#endif
|
|
|
|
)
|
1998-01-29 00:44:16 +00:00
|
|
|
rstio.c_cflag |= HUPCL;
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_direct
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DIRECT)
|
|
|
|
#endif
|
|
|
|
) {
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are working as direct mode, don't change tty speed.
|
|
|
|
*/
|
|
|
|
rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
|
1998-02-17 19:29:03 +00:00
|
|
|
rstio.c_cflag |= modem->cfg.parity;
|
|
|
|
if (cfsetspeed(&rstio, IntToSpeed(modem->cfg.speed)) == -1) {
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogWARN, "Unable to set modem speed (modem %d to %d)\n",
|
1998-02-17 19:29:03 +00:00
|
|
|
modem->fd, modem->cfg.speed);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
1998-01-29 00:49:32 +00:00
|
|
|
tcsetattr(modem->fd, TCSADRAIN, &rstio);
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem (put): iflag = %x, oflag = %x, cflag = %x\n",
|
|
|
|
rstio.c_iflag, rstio.c_oflag, rstio.c_cflag);
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_direct
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DIRECT)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
if (ioctl(modem->fd, TIOCMGET, &modem->mbits)) {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogERROR, "modem_Open: Cannot get modem status: %s\n",
|
1998-01-29 00:44:16 +00:00
|
|
|
strerror(errno));
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_LogicalClose(modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Open: modem control = %o\n", modem->mbits);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
oldflag = fcntl(modem->fd, F_GETFL, 0);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (oldflag < 0) {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogERROR, "modem_Open: Cannot get modem flags: %s\n",
|
1998-01-29 00:44:16 +00:00
|
|
|
strerror(errno));
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_LogicalClose(modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
1998-01-29 00:49:32 +00:00
|
|
|
(void) fcntl(modem->fd, F_SETFL, oldflag & ~O_NONBLOCK);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_StartTimer(bundle, modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
return (modem->fd);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Speed(struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
struct termios rstio;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
tcgetattr(modem->fd, &rstio);
|
1998-01-29 00:44:16 +00:00
|
|
|
return (SpeedToInt(cfgetispeed(&rstio)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put modem tty line into raw mode which is necessary in packet mode operation
|
|
|
|
*/
|
|
|
|
int
|
1998-02-16 00:01:12 +00:00
|
|
|
modem_Raw(struct physical *modem, struct bundle *bundle)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-16 00:01:12 +00:00
|
|
|
struct timeoutArg to;
|
1998-01-29 00:44:16 +00:00
|
|
|
struct termios rstio;
|
|
|
|
int oldflag;
|
|
|
|
|
1998-02-17 19:28:01 +00:00
|
|
|
LogPrintf(LogDEBUG, "Entering modem_Raw\n");
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (!isatty(modem->fd) || Physical_IsSync(modem))
|
1998-02-17 19:28:01 +00:00
|
|
|
return 0;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_direct &&
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DIRECT) &&
|
|
|
|
#endif
|
1998-02-02 19:32:16 +00:00
|
|
|
modem->fd >= 0 && !Online(modem)) {
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_Raw: modem = %d, mbits = %x\n",
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->fd, modem->mbits);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-01-29 00:49:32 +00:00
|
|
|
tcgetattr(modem->fd, &rstio);
|
1998-01-29 00:44:16 +00:00
|
|
|
cfmakeraw(&rstio);
|
1998-02-17 19:29:03 +00:00
|
|
|
if (modem->cfg.rts_cts)
|
1998-01-29 00:44:16 +00:00
|
|
|
rstio.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
|
|
|
|
else
|
|
|
|
rstio.c_cflag |= CLOCAL;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (
|
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_dedicated
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DEDICATED)
|
|
|
|
#endif
|
|
|
|
)
|
1998-01-29 00:44:16 +00:00
|
|
|
rstio.c_cflag |= HUPCL;
|
1998-01-29 00:49:32 +00:00
|
|
|
tcsetattr(modem->fd, TCSADRAIN, &rstio);
|
|
|
|
oldflag = fcntl(modem->fd, F_GETFL, 0);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (oldflag < 0)
|
|
|
|
return (-1);
|
1998-01-29 00:49:32 +00:00
|
|
|
(void) fcntl(modem->fd, F_SETFL, oldflag | O_NONBLOCK);
|
1998-02-16 00:01:12 +00:00
|
|
|
|
|
|
|
to.modem = modem;
|
|
|
|
to.bundle = bundle;
|
|
|
|
modem_Timeout(&to);
|
|
|
|
|
1998-02-27 21:46:00 +00:00
|
|
|
return 0;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Unraw(struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int oldflag;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (isatty(modem->fd) && !Physical_IsSync(modem)) {
|
|
|
|
tcsetattr(modem->fd, TCSAFLUSH, &modem->ios);
|
|
|
|
oldflag = fcntl(modem->fd, F_GETFL, 0);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (oldflag < 0)
|
|
|
|
return;
|
1998-01-29 00:49:32 +00:00
|
|
|
(void) fcntl(modem->fd, F_SETFL, oldflag & ~O_NONBLOCK);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_PhysicalClose(struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_PhysicalClose\n");
|
1998-01-29 00:49:32 +00:00
|
|
|
close(modem->fd);
|
|
|
|
modem->fd = -1;
|
1998-01-30 19:46:07 +00:00
|
|
|
throughput_log(&modem->link.throughput, LogPHASE, "Modem");
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
static int force_hack;
|
|
|
|
|
1998-01-30 19:46:07 +00:00
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Hangup(struct link *l, int dedicated_force)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-16 00:01:12 +00:00
|
|
|
/* We're about to close (pre hangup script) */
|
1998-01-30 19:46:07 +00:00
|
|
|
struct physical *modem = (struct physical *)l;
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
force_hack = dedicated_force;
|
1998-02-16 00:01:12 +00:00
|
|
|
if (modem->fd >= 0) {
|
1998-02-13 05:10:26 +00:00
|
|
|
StopTimer(&modem->link.Timer);
|
|
|
|
throughput_stop(&modem->link.throughput);
|
|
|
|
|
|
|
|
if (prompt_IsTermMode(&prompt))
|
|
|
|
prompt_TtyCommandMode(&prompt);
|
1998-02-16 00:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
modem_Offline(struct physical *modem)
|
|
|
|
{
|
|
|
|
if (modem->fd >= 0) {
|
|
|
|
struct termios tio;
|
1998-02-13 05:10:26 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
modem->mbits &= ~TIOCM_DTR;
|
|
|
|
if (isatty(modem->fd) && Online(modem)) {
|
|
|
|
tcgetattr(modem->fd, &tio);
|
|
|
|
if (cfsetspeed(&tio, B0) == -1)
|
|
|
|
LogPrintf(LogWARN, "Unable to set modem to speed 0\n");
|
|
|
|
else
|
|
|
|
tcsetattr(modem->fd, TCSANOW, &tio);
|
|
|
|
/* nointr_sleep(1); */
|
|
|
|
}
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
modem_Close(struct physical *modem)
|
|
|
|
{
|
1998-02-16 00:01:12 +00:00
|
|
|
LogPrintf(LogDEBUG, "Close modem (%s)\n",
|
1998-01-30 19:46:07 +00:00
|
|
|
modem->fd >= 0 ? "open" : "closed");
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->fd < 0)
|
1998-01-29 00:44:16 +00:00
|
|
|
return;
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
modem_Offline(modem);
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (!isatty(modem->fd)) {
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_PhysicalClose(modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->fd >= 0) {
|
1998-02-13 05:10:26 +00:00
|
|
|
if (force_hack ||
|
1998-01-29 00:49:32 +00:00
|
|
|
#ifdef notyet
|
|
|
|
!modem->is_dedicated
|
|
|
|
#else
|
|
|
|
!(mode & MODE_DEDICATED)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
tcflush(modem->fd, TCIOFLUSH);
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Unraw(modem);
|
|
|
|
modem_LogicalClose(modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we are working as dedicated mode, never close it until we are
|
|
|
|
* directed to quit program.
|
|
|
|
*/
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->mbits |= TIOCM_DTR;
|
|
|
|
ioctl(modem->fd, TIOCMSET, &modem->mbits);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-06 02:22:52 +00:00
|
|
|
static void
|
|
|
|
modem_Destroy(struct link *l)
|
|
|
|
{
|
|
|
|
struct physical *p;
|
|
|
|
|
|
|
|
p = link2physical(l);
|
|
|
|
if (p->fd != -1)
|
1998-02-16 00:01:12 +00:00
|
|
|
modem_Close(p);
|
1998-02-06 02:22:52 +00:00
|
|
|
free(l->name);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
static void
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_LogicalClose(struct physical *modem)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_LogicalClose\n");
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->fd >= 0) {
|
1998-01-29 00:44:16 +00:00
|
|
|
if (Utmp) {
|
1998-02-19 02:08:53 +00:00
|
|
|
ID0logout(VarBaseDevice);
|
1998-01-29 00:44:16 +00:00
|
|
|
Utmp = 0;
|
|
|
|
}
|
1998-02-19 02:08:53 +00:00
|
|
|
modem_PhysicalClose(modem);
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_Unlock(modem);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-30 19:46:07 +00:00
|
|
|
static void
|
1998-02-23 00:38:44 +00:00
|
|
|
modem_StartOutput(struct link *l, struct bundle *bundle)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-01-30 19:46:07 +00:00
|
|
|
struct physical *modem = (struct physical *)l;
|
1998-01-29 00:44:16 +00:00
|
|
|
int nb, nw;
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->out == NULL) {
|
1998-01-30 19:46:07 +00:00
|
|
|
if (link_QueueLen(l) == 0)
|
1998-02-23 00:38:44 +00:00
|
|
|
IpStartOutput(l, bundle);
|
1998-01-30 19:46:07 +00:00
|
|
|
|
|
|
|
modem->out = link_Dequeue(l);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-01-30 19:46:07 +00:00
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (modem->out) {
|
|
|
|
nb = modem->out->cnt;
|
|
|
|
nw = write(modem->fd, MBUF_CTOP(modem->out), nb);
|
1998-02-06 02:23:48 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_StartOutput: wrote: %d(%d) to %d\n",
|
|
|
|
nw, nb, modem->fd);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (nw > 0) {
|
1998-02-06 02:23:48 +00:00
|
|
|
LogDumpBuff(LogDEBUG, "modem_StartOutput: modem write",
|
|
|
|
MBUF_CTOP(modem->out), nw);
|
1998-01-29 00:49:32 +00:00
|
|
|
modem->out->cnt -= nw;
|
|
|
|
modem->out->offset += nw;
|
|
|
|
if (modem->out->cnt == 0) {
|
|
|
|
modem->out = mbfree(modem->out);
|
1998-02-06 02:22:28 +00:00
|
|
|
LogPrintf(LogDEBUG, "modem_StartOutput: mbfree\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
} else if (nw < 0) {
|
|
|
|
if (errno != EAGAIN) {
|
1998-01-29 00:49:32 +00:00
|
|
|
LogPrintf(LogERROR, "modem write (%d): %s\n", modem->fd,
|
|
|
|
strerror(errno));
|
1998-02-06 02:24:29 +00:00
|
|
|
modem->abort = 1;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-30 19:46:07 +00:00
|
|
|
static int
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_IsActive(struct link *l)
|
1998-01-30 19:46:07 +00:00
|
|
|
{
|
|
|
|
return ((struct physical *)l)->fd >= 0;
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
int
|
1998-02-06 02:22:28 +00:00
|
|
|
modem_ShowStatus(struct cmdargs const *arg)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
const char *dev;
|
|
|
|
#ifdef TIOCOUTQ
|
|
|
|
int nb;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
dev = *VarDevice ? VarDevice : "network";
|
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "device: %s speed: ", dev);
|
1998-02-17 19:28:01 +00:00
|
|
|
if (Physical_IsSync(arg->cx->physical))
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "sync\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
else
|
1998-02-17 19:29:03 +00:00
|
|
|
prompt_Printf(&prompt, "%d\n", arg->cx->physical->cfg.speed);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-17 19:29:03 +00:00
|
|
|
switch (arg->cx->physical->cfg.parity & CSIZE) {
|
1998-01-29 00:44:16 +00:00
|
|
|
case CS7:
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "cs7, ");
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
case CS8:
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "cs8, ");
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-02-17 19:29:03 +00:00
|
|
|
if (arg->cx->physical->cfg.parity & PARENB) {
|
|
|
|
if (arg->cx->physical->cfg.parity & PARODD)
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "odd parity, ");
|
1998-01-29 00:44:16 +00:00
|
|
|
else
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "even parity, ");
|
1998-01-29 00:44:16 +00:00
|
|
|
} else
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "no parity, ");
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-17 19:28:01 +00:00
|
|
|
prompt_Printf(&prompt, "CTS/RTS %s.\n",
|
1998-02-17 19:29:03 +00:00
|
|
|
(arg->cx->physical->cfg.rts_cts ? "on" : "off"));
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
if (LogIsKept(LogDEBUG))
|
1998-02-17 19:28:01 +00:00
|
|
|
prompt_Printf(&prompt, "fd = %d, modem control = %o\n",
|
|
|
|
arg->cx->physical->fd, arg->cx->physical->mbits);
|
|
|
|
prompt_Printf(&prompt, "connect count: %d\n",
|
|
|
|
arg->cx->physical->connect_count);
|
1998-01-29 00:44:16 +00:00
|
|
|
#ifdef TIOCOUTQ
|
1998-02-17 19:28:01 +00:00
|
|
|
if (arg->cx->physical->fd >= 0)
|
|
|
|
if (ioctl(arg->cx->physical->fd, TIOCOUTQ, &nb) >= 0)
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "outq: %d\n", nb);
|
1998-01-29 00:44:16 +00:00
|
|
|
else
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "outq: ioctl probe failed: %s\n", strerror(errno));
|
1998-01-29 00:44:16 +00:00
|
|
|
#endif
|
1998-02-17 19:28:01 +00:00
|
|
|
prompt_Printf(&prompt, "outqlen: %d\n",
|
|
|
|
link_QueueLen(&arg->cx->physical->link));
|
1998-02-17 19:28:49 +00:00
|
|
|
prompt_Printf(&prompt, "DialScript = %s\n", arg->cx->cfg.script.dial);
|
|
|
|
prompt_Printf(&prompt, "LoginScript = %s\n", arg->cx->cfg.script.login);
|
|
|
|
prompt_Printf(&prompt, "PhoneNumber(s) = %s\n", arg->cx->cfg.script.hangup);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Printf(&prompt, "\n");
|
1998-02-17 19:28:01 +00:00
|
|
|
throughput_disp(&arg->cx->physical->link.throughput);
|
1998-01-30 19:46:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
|
|
|
|
/* Dummy linker functions, to keep this quiet. Might end up a full
|
|
|
|
regression test later, right now it is just to be able to track
|
|
|
|
external symbols. */
|
|
|
|
#ifdef TESTMAIN
|
|
|
|
int main(void) {}
|
|
|
|
|
|
|
|
void LogPrintf(int i, const char *a, ...) {}
|
|
|
|
int LogIsKept(int garble) { return 0; }
|
|
|
|
int Physical_IsSync(struct physical *phys) {return 0;}
|
|
|
|
int DoChat(struct physical *a, char *b) {return 0;}
|
|
|
|
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1998-02-09 19:21:11 +00:00
|
|
|
static void
|
1998-02-10 03:22:05 +00:00
|
|
|
modem_DescriptorRead(struct descriptor *d, struct bundle *bundle,
|
|
|
|
const fd_set *fdset)
|
1998-02-09 19:21:11 +00:00
|
|
|
{
|
|
|
|
struct physical *p = descriptor2physical(d);
|
|
|
|
u_char rbuff[MAX_MRU], *cp;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p);
|
|
|
|
|
|
|
|
/* something to read from modem */
|
|
|
|
if (LcpInfo.fsm.state <= ST_CLOSED)
|
|
|
|
nointr_usleep(10000);
|
|
|
|
|
|
|
|
n = Physical_Read(p, rbuff, sizeof rbuff);
|
1998-02-16 00:01:12 +00:00
|
|
|
if ((mode & MODE_DIRECT) && n <= 0)
|
|
|
|
link_Close(&p->link, bundle, 0, 1);
|
|
|
|
else
|
1998-02-09 19:21:11 +00:00
|
|
|
LogDumpBuff(LogASYNC, "ReadFromModem", rbuff, n);
|
|
|
|
|
|
|
|
if (LcpInfo.fsm.state <= ST_CLOSED) {
|
|
|
|
/* In dedicated mode, we just discard input until LCP is started */
|
|
|
|
if (!(mode & MODE_DEDICATED)) {
|
|
|
|
cp = HdlcDetect(p, rbuff, n);
|
|
|
|
if (cp) {
|
|
|
|
/* LCP packet is detected. Turn ourselves into packet mode */
|
|
|
|
if (cp != rbuff) {
|
|
|
|
/* XXX missing return value checks */
|
|
|
|
Physical_Write(p, rbuff, cp - rbuff);
|
|
|
|
Physical_Write(p, "\r\n", 2);
|
|
|
|
}
|
1998-02-17 19:28:35 +00:00
|
|
|
datalink_Up(bundle2datalink(bundle, p->link.name), 0, 1);
|
1998-02-10 03:23:50 +00:00
|
|
|
} else
|
|
|
|
prompt_Printf(&prompt, "%.*s", n, rbuff);
|
1998-02-09 19:21:11 +00:00
|
|
|
}
|
|
|
|
} else if (n > 0)
|
|
|
|
async_Input(bundle, rbuff, n, p);
|
|
|
|
}
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
modem_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
|
|
|
|
{
|
|
|
|
return Physical_UpdateSet(d, r, w, e, n, 0);
|
|
|
|
}
|