slattach.c revision 1.4
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.1Scgdstatic char sccsid[] = "@(#)slattach.c	4.6 (Berkeley) 6/1/90";
611.4Scgdstatic char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/sbin/slattach/slattach.c,v 1.4 1993/04/08 04:15:43 cgd 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.4Scgdusage: %s [-a ][-c ][-n ][-s <speed> ]<device>\n\
801.4Scgd	-a -- autoenable VJ compression\n\
811.4Scgd	-c -- enable VJ compression\n\
821.4Scgd	-n -- throw out ICMP packets\n\
831.4Scgd	-h -- turn on cts/rts style flow control\n\
841.4Scgd	-s -- baud rate (default 9600)\n";
851.1Scgd
861.4Scgdint main(int argc, char **argv)
871.1Scgd{
881.4Scgd	struct termios tty;
891.4Scgd	int option;
901.4Scgd	int fd;
911.4Scgd	char devname[32];
921.4Scgd	char *dev = (char *)0;
931.4Scgd	int slipdisc = SLIPDISC;
941.4Scgd	int speed = DEFAULT_BAUD;
951.4Scgd	int slflags = 0;
961.4Scgd	int flow_control = 0;	/* extra flags to enable hardware flow cont. */
971.4Scgd
981.4Scgd	extern char *optarg;
991.4Scgd	extern int optind;
1001.4Scgd
1011.4Scgd	while ((option = getopt(argc, argv, "achns:")) != EOF) {
1021.4Scgd		switch (option) {
1031.4Scgd		case 'a':
1041.4Scgd			slflags |= SC_AUTOCOMP;
1051.4Scgd			slflags &= ~SC_COMPRESS;
1061.4Scgd			break;
1071.4Scgd		case 'c':
1081.4Scgd			slflags |= SC_COMPRESS;
1091.4Scgd			slflags &= ~SC_AUTOCOMP;
1101.4Scgd			break;
1111.4Scgd		case 'h':
1121.4Scgd			flow_control |= CRTSCTS;
1131.4Scgd			break;
1141.4Scgd		case 'n':
1151.4Scgd			slflags |= SC_NOICMP;
1161.4Scgd			break;
1171.4Scgd		case 's':
1181.4Scgd			speed = atoi(optarg);
1191.4Scgd			break;
1201.4Scgd		case '?':
1211.4Scgd		default:
1221.4Scgd			fprintf(stderr, usage_str, argv[0]);
1231.4Scgd			exit(1);
1241.4Scgd		}
1251.4Scgd	}
1261.4Scgd
1271.4Scgd	if (optind == argc - 1)
1281.4Scgd		dev = argv[optind];
1291.4Scgd
1301.4Scgd
1311.4Scgd	if (dev == (char *)0) {
1321.4Scgd		fprintf(stderr, usage_str, argv[0]);
1331.4Scgd		exit(2);
1341.1Scgd	}
1351.4Scgd
1361.4Scgd	if ((speed = findspeed(speed)) == 0) {
1371.4Scgd		fprintf(stderr, "unknown speed");
1381.1Scgd		exit(1);
1391.1Scgd	}
1401.4Scgd
1411.1Scgd	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
1421.4Scgd		strcpy(devname, _PATH_DEV);
1431.4Scgd		strcat(devname, "/");
1441.4Scgd		strncat(devname, dev, 10);
1451.1Scgd		dev = devname;
1461.1Scgd	}
1471.4Scgd
1481.1Scgd	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
1491.1Scgd		perror(dev);
1501.1Scgd		exit(1);
1511.1Scgd	}
1521.4Scgd
1531.4Scgd	tty.c_iflag = 0;
1541.4Scgd	tty.c_oflag = 0;
1551.4Scgd	tty.c_cflag = CREAD | CS8 | flow_control;
1561.4Scgd	tty.c_lflag = 0;
1571.4Scgd	tty.c_cc[VMIN] = 1; /* wait for one char */
1581.4Scgd	tty.c_cc[VTIME] = 0; /* wait forever for a char */
1591.4Scgd	if (ioctl(fd, TIOCSETA, &tty) < 0) {
1601.4Scgd		perror("ioctl(TIOCSETA)");
1611.4Scgd		close(fd);
1621.4Scgd		exit(1);
1631.4Scgd	}
1641.4Scgd
1651.4Scgd	cfsetispeed(&tty, speed);
1661.4Scgd	cfsetospeed(&tty, speed);
1671.4Scgd	if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
1681.4Scgd		perror("tcsetattr");
1691.4Scgd		close(fd);
1701.1Scgd		exit(1);
1711.1Scgd	}
1721.4Scgd
1731.1Scgd	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
1741.1Scgd		perror("ioctl(TIOCSETD)");
1751.4Scgd		close(fd);
1761.4Scgd		exit(1);
1771.4Scgd	}
1781.4Scgd
1791.4Scgd	if (ioctl(fd, SLIOCSFLAGS, &slflags) < 0) {
1801.4Scgd		perror("ioctl(SLIOCSFLAGS)");
1811.4Scgd		close(fd);
1821.1Scgd		exit(1);
1831.1Scgd	}
1841.1Scgd
1851.1Scgd	if (fork() > 0)
1861.1Scgd		exit(0);
1871.4Scgd
1881.1Scgd	for (;;)
1891.1Scgd		sigpause(0L);
1901.1Scgd}
1911.1Scgd
1921.1Scgdstruct sg_spds {
1931.1Scgd	int sp_val, sp_name;
1941.1Scgd}       spds[] = {
1951.1Scgd#ifdef B50
1961.1Scgd	{ 50, B50 },
1971.1Scgd#endif
1981.1Scgd#ifdef B75
1991.1Scgd	{ 75, B75 },
2001.1Scgd#endif
2011.1Scgd#ifdef B110
2021.1Scgd	{ 110, B110 },
2031.1Scgd#endif
2041.1Scgd#ifdef B150
2051.1Scgd	{ 150, B150 },
2061.1Scgd#endif
2071.1Scgd#ifdef B200
2081.1Scgd	{ 200, B200 },
2091.1Scgd#endif
2101.1Scgd#ifdef B300
2111.1Scgd	{ 300, B300 },
2121.1Scgd#endif
2131.1Scgd#ifdef B600
2141.1Scgd	{ 600, B600 },
2151.1Scgd#endif
2161.1Scgd#ifdef B1200
2171.1Scgd	{ 1200, B1200 },
2181.1Scgd#endif
2191.1Scgd#ifdef B1800
2201.1Scgd	{ 1800, B1800 },
2211.1Scgd#endif
2221.1Scgd#ifdef B2000
2231.1Scgd	{ 2000, B2000 },
2241.1Scgd#endif
2251.1Scgd#ifdef B2400
2261.1Scgd	{ 2400, B2400 },
2271.1Scgd#endif
2281.1Scgd#ifdef B3600
2291.1Scgd	{ 3600, B3600 },
2301.1Scgd#endif
2311.1Scgd#ifdef B4800
2321.1Scgd	{ 4800, B4800 },
2331.1Scgd#endif
2341.1Scgd#ifdef B7200
2351.1Scgd	{ 7200, B7200 },
2361.1Scgd#endif
2371.1Scgd#ifdef B9600
2381.1Scgd	{ 9600, B9600 },
2391.1Scgd#endif
2401.1Scgd#ifdef EXTA
2411.1Scgd	{ 19200, EXTA },
2421.1Scgd#endif
2431.1Scgd#ifdef EXTB
2441.1Scgd	{ 38400, EXTB },
2451.1Scgd#endif
2461.1Scgd	{ 0, 0 }
2471.1Scgd};
2481.1Scgd
2491.4Scgdint findspeed(int speed)
2501.1Scgd{
2511.4Scgd	struct sg_spds *sp = spds;
2521.1Scgd
2531.4Scgd	while ((sp->sp_val != 0) && (sp->sp_val != speed))
2541.1Scgd		sp++;
2551.4Scgd
2561.1Scgd	return (sp->sp_name);
2571.1Scgd}
258