*/
#ifndef lint
-static char rcsid[] = "$Id: sys-NeXT.c,v 1.1 1995/08/10 06:48:34 paulus Exp $";
+static char rcsid[] = "$Id: sys-NeXT.c,v 1.4 1996/01/01 23:03:23 paulus Exp $";
#endif
#include <stdio.h>
#include "pppd.h"
static int initdisc = -1; /* Initial TTY discipline */
+static int initfdflags = -1; /* Initial file descriptor flags for fd */
extern int errno;
static char *lock_file;
-int sockfd; /* socket for doing interface ioctls */
+static int sockfd; /* socket for doing interface ioctls */
#if defined(i386) && defined(HAS_BROKEN_IOCTL)
#define ioctl myioctl
#endif
+static int if_is_up; /* the interface is currently up */
+static u_int32_t default_route_gateway; /* gateway addr for default route */
+static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
+
+/* Prototypes for procedures local to this file. */
+static int translate_speed __P((int));
+static int baud_rate_of __P((int));
+static int dodefaultroute __P((u_int32_t, int));
+static int get_ether_addr __P((u_int32_t, struct sockaddr *));
+static int ether_by_host __P((char *, struct ether_addr *));
+
+
/*
* sys_init - System-dependent initialization.
*/
}
}
+/*
+ * sys_cleanup - restore any system state we modified before exiting:
+ * mark the interface down, delete default route and/or proxy arp entry.
+ * This should call die() because it's called from die().
+ */
+void
+sys_cleanup()
+{
+ struct ifreq ifr;
+
+ if (if_is_up) {
+ strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
+ && ((ifr.ifr_flags & IFF_UP) != 0)) {
+ ifr.ifr_flags &= ~IFF_UP;
+ ioctl(sockfd, SIOCSIFFLAGS, &ifr);
+ }
+ }
+
+ if (default_route_gateway)
+ cifdefaultroute(0, default_route_gateway);
+ if (proxy_arp_addr)
+ cifproxyarp(0, proxy_arp_addr);
+}
+
/*
* note_debug_level - note a change in the debug level.
*/
* establish_ppp - Turn the serial port into a ppp interface.
*/
void
-establish_ppp()
+establish_ppp(fd)
+ int fd;
{
int pppdisc = PPPDISC;
int x;
syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
}
}
+
+ /*
+ * Set device for non-blocking reads.
+ */
+ if ((initfdflags = fcntl(fd, F_GETFL)) == -1
+ || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
+ syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
+ }
}
* This shouldn't call die() because it's called from die().
*/
void
-disestablish_ppp()
+disestablish_ppp(fd)
+ int fd;
{
int x;
char *s;
+ /* Reset non-blocking mode on the file descriptor. */
+ if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
+ syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
+ initfdflags = -1;
+
if (initdisc >= 0) {
/*
* Check whether the link seems not to be 8-bit clean.
#ifdef B57600
{ 57600, B57600 },
#endif
+/*
+#ifndef B115200
+#warning Defining B115200
+#define B115200 20
+#endif
+*/
#ifdef B115200
{ 115200, B115200 },
#endif
/*
* Translate from a speed_t to bits/second.
*/
-int
+static int
baud_rate_of(speed)
int speed;
{
* at the requested speed, etc. If `local' is true, set CLOCAL
* regardless of whether the modem option was specified.
*/
+void
set_up_tty(fd, local)
int fd, local;
{
tios.c_cflag |= CS8 | CREAD | HUPCL;
if (local || !modem)
tios.c_cflag |= CLOCAL;
+
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
tios.c_cc[VMIN] = 1;
tios.c_cc[VTIME] = 0;
- if (crtscts == 2) {
- tios.c_iflag |= IXOFF;
+ if (crtscts == -2) {
+ tios.c_iflag |= IXON | IXOFF;
tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
}
* restore_tty - restore the terminal to the saved settings.
*/
void
-restore_tty()
+restore_tty(fd)
+ int fd;
{
if (restore_term) {
if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
* NXFax's capability to determine port usage.
*
*/
+void
setdtr(fd, on)
int fd, on;
{
u_char *p;
int len;
{
- if (unit != 0)
- MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
if (debug)
log_packet(p, len, "sent ");
- if (write(fd, p, len) < 0) {
- syslog(LOG_ERR, "write: %m");
- die(1);
+ if (write(ttyfd, p, len) < 0) {
+ if (errno == EWOULDBLOCK || errno == ENOBUFS
+ || errno == ENXIO || errno == EIO) {
+ syslog(LOG_WARNING, "write: warning: %m");
+ } else {
+ syslog(LOG_ERR, "write: %m");
+ die(1);
+ }
}
}
/*
- * wait_input - wait until there is data available on fd,
+ * wait_input - wait until there is data available on ttyfd,
* for the length of time specified by *timo (indefinite
* if timo is NULL).
*/
+void
wait_input(timo)
struct timeval *timo;
{
int n;
FD_ZERO(&ready);
- FD_SET(fd, &ready);
- n = select(fd+1, &ready, NULL, &ready, timo);
+ FD_SET(ttyfd, &ready);
+ n = select(ttyfd+1, &ready, NULL, &ready, timo);
if (n < 0 && errno != EINTR) {
syslog(LOG_ERR, "select: %m");
die(1);
{
int len;
- if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
+ if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
if (errno == EWOULDBLOCK || errno == EINTR) {
- MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
+ MAINDEBUG((LOG_DEBUG, "read: %m"));
return -1;
}
- syslog(LOG_ERR, "read(fd): %m");
+ syslog(LOG_ERR, "read: %m");
die(1);
}
return len;
quit();
}
- if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
+ if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
quit();
}
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
quit();
}
x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
- if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
quit();
}
int unit;
ext_accm accm;
{
- if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
+ if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
syslog(LOG_WARNING, "ioctl(PPPIOCSXASYNCMAP): %m");
}
{
int x;
- if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
+ if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
quit();
}
- if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
+ if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
quit();
}
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
quit();
}
x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
- if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
quit();
}
* ccp_test - ask kernel whether a given compression method
* is acceptable for use.
*/
+int
ccp_test(unit, opt_ptr, opt_len, for_transmit)
int unit, opt_len, for_transmit;
u_char *opt_ptr;
data.ptr = opt_ptr;
data.length = opt_len;
data.transmit = for_transmit;
- return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
+ if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
+ return 1;
+ return (errno == ENOBUFS)? 0: -1;
}
/*
{
int x;
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
return;
}
x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
- if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
+ if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
}
{
int x;
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
return 0;
}
{
u_int x;
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPIOCGFLAGS): %m");
return 0;
}
x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
- if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
return 0;
}
- if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
+ if (ioctl(ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
return 0;
}
syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
return 0;
}
+ if_is_up = 1;
npi.protocol = PPP_IP;
npi.mode = NPMODE_PASS;
- if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
+ if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
if (errno != ENOTTY) {
syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
return 0;
}
/* for backwards compatibility */
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
return 0;
}
x |= SC_ENABLE_IP;
- if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
return 0;
}
rv = 1;
npi.protocol = PPP_IP;
npi.mode = NPMODE_ERROR;
- if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
+ if (ioctl(ttyfd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
if (errno != ENOTTY) {
syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
rv = 0;
} else {
/* backwards compatibility */
- if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
rv = 0;
} else {
x &= ~SC_ENABLE_IP;
- if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
+ if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
rv = 0;
}
if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
rv = 0;
- }
+ } else
+ if_is_up = 0;
}
return rv;
}
(cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
return 0;
}
+ default_route_gateway = (cmd == 's')? g: 0;
return 1;
}
return 0;
}
+ proxy_arp_addr = hisaddr;
return 1;
}
syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
return 0;
}
+ proxy_arp_addr = 0;
return 1;
}
return 1;
}
-int
+static int
ether_by_host(hostname, etherptr)
char *hostname;
struct ether_addr *etherptr;