1998-01-29 00:49:32 +00:00
|
|
|
/*
|
|
|
|
* Written by Eivind Eklund <eivind@yes.no>
|
|
|
|
* for Yes Interactive
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998, Yes Interactive. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in any form is permitted. Redistribution in
|
|
|
|
* source form should include the above copyright and this set of
|
|
|
|
* conditions, because large sections american law seems to have been
|
|
|
|
* created by a bunch of jerks on drugs that are now illegal, forcing
|
|
|
|
* me to include this copyright-stuff instead of placing this in the
|
|
|
|
* public domain. The name of of 'Yes Interactive' or 'Eivind Eklund'
|
|
|
|
* 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-06-27 14:18:15 +00:00
|
|
|
* $Id: physical.c,v 1.3 1998/05/23 22:24:47 brian Exp $
|
1998-01-29 00:49:32 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-04-30 23:53:56 +00:00
|
|
|
#include <stdlib.h>
|
1998-01-29 00:49:32 +00:00
|
|
|
#include <string.h>
|
1998-03-10 03:06:07 +00:00
|
|
|
#include <time.h>
|
1998-01-29 00:49:32 +00:00
|
|
|
#include <unistd.h>
|
1998-03-10 03:06:07 +00:00
|
|
|
#include <utmp.h>
|
1998-04-30 23:53:56 +00:00
|
|
|
#include <sys/tty.h>
|
1998-01-29 00:49:32 +00:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "timer.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-02-18 19:35:59 +00:00
|
|
|
#include "hdlc.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
#include "throughput.h"
|
1998-02-02 19:33:40 +00:00
|
|
|
#include "fsm.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "async.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "ccp.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-09 19:21:11 +00:00
|
|
|
#include "log.h"
|
1998-03-10 03:06:07 +00:00
|
|
|
#include "id.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
|
|
|
|
/* External calls - should possibly be moved inline */
|
|
|
|
extern int IntToSpeed(int);
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_GetFD(struct physical *phys) {
|
1998-01-29 00:49:32 +00:00
|
|
|
return phys->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_IsATTY(struct physical *phys) {
|
1998-01-29 00:49:32 +00:00
|
|
|
return isatty(phys->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_IsSync(struct physical *phys) {
|
1998-02-17 19:29:03 +00:00
|
|
|
return phys->cfg.speed == 0;
|
1998-01-29 00:49:32 +00:00
|
|
|
}
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
const char *physical_GetDevice(struct physical *phys)
|
1998-03-06 00:34:47 +00:00
|
|
|
{
|
|
|
|
return phys->name.full;
|
1998-01-29 00:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
|
1998-04-10 23:51:33 +00:00
|
|
|
{
|
|
|
|
int f, pos;
|
|
|
|
|
|
|
|
p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
|
|
|
|
for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
|
|
|
|
if (pos)
|
|
|
|
p->cfg.devlist[pos++] = ' ';
|
|
|
|
strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
|
|
|
|
pos += strlen(p->cfg.devlist + pos);
|
|
|
|
}
|
1998-01-29 00:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_SetSpeed(struct physical *phys, int speed) {
|
1998-01-29 00:49:32 +00:00
|
|
|
if (IntToSpeed(speed) != B0) {
|
1998-02-17 19:29:03 +00:00
|
|
|
phys->cfg.speed = speed;
|
1998-01-29 00:49:32 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
1998-02-17 19:29:03 +00:00
|
|
|
return 0;
|
1998-01-29 00:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_SetSync(struct physical *phys) {
|
1998-02-17 19:29:03 +00:00
|
|
|
phys->cfg.speed = 0;
|
1998-01-29 00:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_SetRtsCts(struct physical *phys, int enable) {
|
1998-05-01 19:22:23 +00:00
|
|
|
phys->cfg.rts_cts = enable ? 1 : 0;
|
1998-01-29 00:49:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encapsulation for a read on the FD. Avoids some exposure, and
|
|
|
|
concentrates control. */
|
|
|
|
ssize_t
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_Read(struct physical *phys, void *buf, size_t nbytes) {
|
1998-01-29 00:49:32 +00:00
|
|
|
return read(phys->fd, buf, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_Write(struct physical *phys, const void *buf, size_t nbytes) {
|
1998-01-29 00:49:32 +00:00
|
|
|
return write(phys->fd, buf, nbytes);
|
|
|
|
}
|
1998-02-06 02:22:28 +00:00
|
|
|
|
1998-02-09 19:21:11 +00:00
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
|
1998-02-13 05:10:26 +00:00
|
|
|
int *n, int force)
|
1998-02-09 19:21:11 +00:00
|
|
|
{
|
|
|
|
struct physical *p = descriptor2physical(d);
|
|
|
|
int sets;
|
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
sets = 0;
|
1998-02-09 19:21:11 +00:00
|
|
|
if (p->fd >= 0) {
|
1998-02-13 05:10:26 +00:00
|
|
|
if (r) {
|
|
|
|
FD_SET(p->fd, r);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
|
1998-02-13 05:10:26 +00:00
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (e) {
|
|
|
|
FD_SET(p->fd, e);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
|
1998-02-13 05:10:26 +00:00
|
|
|
sets++;
|
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
if (w && (force || link_QueueLen(&p->link) || p->out)) {
|
1998-02-09 19:21:11 +00:00
|
|
|
FD_SET(p->fd, w);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
|
1998-02-13 05:10:26 +00:00
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (sets && *n < p->fd + 1)
|
|
|
|
*n = p->fd + 1;
|
|
|
|
}
|
1998-02-09 19:21:11 +00:00
|
|
|
|
|
|
|
return sets;
|
|
|
|
}
|
|
|
|
|
1998-05-15 18:21:45 +00:00
|
|
|
int
|
|
|
|
physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
|
|
|
|
{
|
|
|
|
int sets;
|
|
|
|
|
|
|
|
sets = 0;
|
|
|
|
if (p->fd >= 0) {
|
|
|
|
if (r && FD_ISSET(p->fd, r)) {
|
|
|
|
FD_CLR(p->fd, r);
|
|
|
|
log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
|
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (e && FD_ISSET(p->fd, e)) {
|
|
|
|
FD_CLR(p->fd, e);
|
|
|
|
log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
|
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (w && FD_ISSET(p->fd, w)) {
|
|
|
|
FD_CLR(p->fd, w);
|
|
|
|
log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
|
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sets;
|
|
|
|
}
|
|
|
|
|
1998-02-09 19:21:11 +00:00
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_IsSet(struct descriptor *d, const fd_set *fdset)
|
1998-02-09 19:21:11 +00:00
|
|
|
{
|
|
|
|
struct physical *p = descriptor2physical(d);
|
|
|
|
return p->fd >= 0 && FD_ISSET(p->fd, fdset);
|
|
|
|
}
|
|
|
|
|
1998-03-10 03:06:07 +00:00
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_Login(struct physical *phys, const char *name)
|
1998-03-10 03:06:07 +00:00
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
if (phys->type == PHYS_DIRECT && physical_IsATTY(phys)) {
|
1998-03-10 03:06:07 +00:00
|
|
|
if (phys->Utmp)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Oops, already logged in on %s\n", phys->name.base);
|
1998-03-10 03:06:07 +00:00
|
|
|
else {
|
|
|
|
struct utmp ut;
|
1998-05-21 01:12:20 +00:00
|
|
|
const char *connstr;
|
1998-03-10 03:06:07 +00:00
|
|
|
|
|
|
|
memset(&ut, 0, sizeof ut);
|
|
|
|
time(&ut.ut_time);
|
|
|
|
strncpy(ut.ut_name, name, sizeof ut.ut_name);
|
1998-05-21 01:12:20 +00:00
|
|
|
strncpy(ut.ut_line, phys->name.base, sizeof ut.ut_line);
|
|
|
|
if ((connstr = getenv("CONNECT")))
|
|
|
|
/* mgetty sets this to the connection speed */
|
|
|
|
strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
|
1998-03-10 03:06:07 +00:00
|
|
|
ID0login(&ut);
|
|
|
|
phys->Utmp = 1;
|
|
|
|
}
|
1998-04-25 10:49:52 +00:00
|
|
|
}
|
1998-03-10 03:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
physical_Logout(struct physical *phys)
|
1998-03-10 03:06:07 +00:00
|
|
|
{
|
|
|
|
if (phys->Utmp) {
|
|
|
|
ID0logout(phys->name.base);
|
|
|
|
phys->Utmp = 0;
|
|
|
|
}
|
|
|
|
}
|
1998-05-15 23:58:30 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
physical_SetMode(struct physical *p, int mode)
|
|
|
|
{
|
|
|
|
if (p->type & (PHYS_DIRECT|PHYS_DEDICATED)
|
|
|
|
|| mode & (PHYS_DIRECT|PHYS_DEDICATED)) {
|
|
|
|
log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
|
|
|
|
mode2Nam(p->type), mode2Nam(mode));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p->type = mode;
|
|
|
|
return 1;
|
|
|
|
}
|