slattach.c revision 1.8
11.1Scgd/*
21.1Scgd * Copyright (c) 1988 Regents of the University of California.
31.1Scgd * All rights reserved.
41.1Scgd *
51.1Scgd * This code is derived from software contributed to Berkeley by
61.1Scgd * Rick Adams.
71.1Scgd *
81.1Scgd * Redistribution and use in source and binary forms, with or without
91.1Scgd * modification, are permitted provided that the following conditions
101.1Scgd * are met:
111.1Scgd * 1. Redistributions of source code must retain the above copyright
121.1Scgd *    notice, this list of conditions and the following disclaimer.
131.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer in the
151.1Scgd *    documentation and/or other materials provided with the distribution.
161.1Scgd * 3. All advertising materials mentioning features or use of this software
171.1Scgd *    must display the following acknowledgement:
181.1Scgd *	This product includes software developed by the University of
191.1Scgd *	California, Berkeley and its contributors.
201.1Scgd * 4. Neither the name of the University nor the names of its contributors
211.1Scgd *    may be used to endorse or promote products derived from this software
221.1Scgd *    without specific prior written permission.
231.1Scgd *
241.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341.1Scgd * SUCH DAMAGE.
351.1Scgd */
361.1Scgd
371.4Scgd/*
381.4Scgd** Hacks to support "-a|c|n" flags on the command line which enalbe VJ
391.4Scgd** header compresion and disable ICMP.  I use getopt to deal witht that
401.4Scgd** stuff because I'm a lazy sob, I can't spell, and that's OK.
411.4Scgd**
421.4Scgd** If this is good all rights go to B & L Jolitz, otherwise send your
431.4Scgd** comments to Reagan (/dev/null).
441.4Scgd**
451.4Scgd** nerd@percival.rain.com (Michael Galassi) 92.09.03
461.4Scgd**
471.4Scgd** Hacked to change from sgtty to POSIX termio style serial line control
481.4Scgd** and added flag to enable cts/rts style flow control.
491.4Scgd**
501.4Scgd** blymn@awadi.com.au (Brett Lymn) 93.04.04
511.4Scgd*/
521.4Scgd
531.1Scgd#ifndef lint
541.1Scgdchar copyright[] =
551.1Scgd"@(#) Copyright (c) 1988 Regents of the University of California.\n\
561.1Scgd All rights reserved.\n";
571.1Scgd#endif /* not lint */
581.1Scgd
591.1Scgd#ifndef lint
601.7Smycroft/*static char sccsid[] = "from: @(#)slattach.c	4.6 (Berkeley) 6/1/90";*/
611.8Smycroftstatic char rcsid[] = "$Id: slattach.c,v 1.8 1993/12/02 05:44:30 mycroft Exp $";
621.1Scgd#endif /* not lint */
631.1Scgd
641.1Scgd#include <sys/param.h>
651.4Scgd#include <sys/ioctl.h>
661.4Scgd#include <termios.h>
671.1Scgd#include <sys/socket.h>
681.1Scgd#include <netinet/in.h>
691.1Scgd#include <net/if.h>
701.4Scgd#include <net/if_slvar.h>
711.1Scgd#include <netdb.h>
721.1Scgd#include <fcntl.h>
731.1Scgd#include <stdio.h>
741.1Scgd#include <paths.h>
751.1Scgd
761.1Scgd#define DEFAULT_BAUD	9600
771.1Scgd
781.4Scgdstatic char usage_str[] = "\
791.8Smycroftusage: %s [-a] [-c] [-h] [-m] [-n] [-s <speed>] <device>\n\
801.4Scgd	-a -- autoenable VJ compression\n\
811.4Scgd	-c -- enable VJ compression\n\
821.8Smycroft	-h -- turn on CTS/RTS style flow control\n\
831.8Smycroft	-m -- maintain DTR after last close (no HUPCL)\n\
841.4Scgd	-n -- throw out ICMP packets\n\
851.4Scgd	-s -- baud rate (default 9600)\n";
861.1Scgd
871.4Scgdint main(int argc, char **argv)
881.1Scgd{
891.4Scgd	struct termios tty;
901.4Scgd	int option;
911.4Scgd	int fd;
921.4Scgd	char devname[32];
931.4Scgd	char *dev = (char *)0;
941.4Scgd	int slipdisc = SLIPDISC;
951.4Scgd	int speed = DEFAULT_BAUD;
961.4Scgd	int slflags = 0;
971.8Smycroft	int cflags = HUPCL;
981.4Scgd
991.4Scgd	extern char *optarg;
1001.4Scgd	extern int optind;
1011.4Scgd
1021.8Smycroft	while ((option = getopt(argc, argv, "achmns:")) != EOF) {
1031.4Scgd		switch (option) {
1041.4Scgd		case 'a':
1051.4Scgd			slflags |= SC_AUTOCOMP;
1061.4Scgd			slflags &= ~SC_COMPRESS;
1071.4Scgd			break;
1081.4Scgd		case 'c':
1091.4Scgd			slflags |= SC_COMPRESS;
1101.4Scgd			slflags &= ~SC_AUTOCOMP;
1111.4Scgd			break;
1121.4Scgd		case 'h':
1131.8Smycroft			cflags |= CRTSCTS;
1141.8Smycroft			break;
1151.8Smycroft		case 'm':
1161.8Smycroft			cflags &= ~HUPCL;
1171.4Scgd			break;
1181.4Scgd		case 'n':
1191.4Scgd			slflags |= SC_NOICMP;
1201.4Scgd			break;
1211.4Scgd		case 's':
1221.4Scgd			speed = atoi(optarg);
1231.4Scgd			break;
1241.4Scgd		case '?':
1251.4Scgd		default:
1261.4Scgd			fprintf(stderr, usage_str, argv[0]);
1271.4Scgd			exit(1);
1281.4Scgd		}
1291.4Scgd	}
1301.4Scgd
1311.4Scgd	if (optind == argc - 1)
1321.4Scgd		dev = argv[optind];
1331.4Scgd
1341.4Scgd
1351.4Scgd	if (dev == (char *)0) {
1361.4Scgd		fprintf(stderr, usage_str, argv[0]);
1371.4Scgd		exit(2);
1381.1Scgd	}
1391.4Scgd
1401.1Scgd	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
1411.4Scgd		strcpy(devname, _PATH_DEV);
1421.4Scgd		strcat(devname, "/");
1431.4Scgd		strncat(devname, dev, 10);
1441.1Scgd		dev = devname;
1451.1Scgd	}
1461.4Scgd
1471.1Scgd	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
1481.1Scgd		perror(dev);
1491.1Scgd		exit(1);
1501.1Scgd	}
1511.4Scgd
1521.8Smycroft	if (tcgetattr(fd, &tty) < 0) {
1531.8Smycroft		perror("tcgetattr");
1541.8Smycroft		close(fd);
1551.8Smycroft		exit(1);
1561.8Smycroft	}
1571.4Scgd	tty.c_iflag = 0;
1581.4Scgd	tty.c_oflag = 0;
1591.8Smycroft	tty.c_cflag = CREAD | CS8 | cflags;
1601.4Scgd	tty.c_lflag = 0;
1611.4Scgd	tty.c_cc[VMIN] = 1; /* wait for one char */
1621.4Scgd	tty.c_cc[VTIME] = 0; /* wait forever for a char */
1631.8Smycroft	cfsetispeed(&tty, speed);
1641.8Smycroft	cfsetospeed(&tty, speed);
1651.8Smycroft	if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
1661.8Smycroft		perror("tcsetattr");
1671.4Scgd		close(fd);
1681.4Scgd		exit(1);
1691.4Scgd	}
1701.5Scgd
1711.5Scgd	if (ioctl(fd, TIOCSDTR) < 0) {
1721.5Scgd                perror("ioctl(TIOCSDTR)");
1731.5Scgd                close(fd);
1741.5Scgd                exit(1);
1751.5Scgd        }
1761.4Scgd
1771.1Scgd	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
1781.1Scgd		perror("ioctl(TIOCSETD)");
1791.4Scgd		close(fd);
1801.4Scgd		exit(1);
1811.4Scgd	}
1821.4Scgd
1831.4Scgd	if (ioctl(fd, SLIOCSFLAGS, &slflags) < 0) {
1841.4Scgd		perror("ioctl(SLIOCSFLAGS)");
1851.4Scgd		close(fd);
1861.1Scgd		exit(1);
1871.1Scgd	}
1881.1Scgd
1891.1Scgd	if (fork() > 0)
1901.1Scgd		exit(0);
1911.4Scgd
1921.1Scgd	for (;;)
1931.1Scgd		sigpause(0L);
1941.1Scgd}
195