slattach.c revision 1.8
1/* 2 * Copyright (c) 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Adams. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37/* 38** Hacks to support "-a|c|n" flags on the command line which enalbe VJ 39** header compresion and disable ICMP. I use getopt to deal witht that 40** stuff because I'm a lazy sob, I can't spell, and that's OK. 41** 42** If this is good all rights go to B & L Jolitz, otherwise send your 43** comments to Reagan (/dev/null). 44** 45** nerd@percival.rain.com (Michael Galassi) 92.09.03 46** 47** Hacked to change from sgtty to POSIX termio style serial line control 48** and added flag to enable cts/rts style flow control. 49** 50** blymn@awadi.com.au (Brett Lymn) 93.04.04 51*/ 52 53#ifndef lint 54char copyright[] = 55"@(#) Copyright (c) 1988 Regents of the University of California.\n\ 56 All rights reserved.\n"; 57#endif /* not lint */ 58 59#ifndef lint 60/*static char sccsid[] = "from: @(#)slattach.c 4.6 (Berkeley) 6/1/90";*/ 61static char rcsid[] = "$Id: slattach.c,v 1.8 1993/12/02 05:44:30 mycroft Exp $"; 62#endif /* not lint */ 63 64#include <sys/param.h> 65#include <sys/ioctl.h> 66#include <termios.h> 67#include <sys/socket.h> 68#include <netinet/in.h> 69#include <net/if.h> 70#include <net/if_slvar.h> 71#include <netdb.h> 72#include <fcntl.h> 73#include <stdio.h> 74#include <paths.h> 75 76#define DEFAULT_BAUD 9600 77 78static char usage_str[] = "\ 79usage: %s [-a] [-c] [-h] [-m] [-n] [-s <speed>] <device>\n\ 80 -a -- autoenable VJ compression\n\ 81 -c -- enable VJ compression\n\ 82 -h -- turn on CTS/RTS style flow control\n\ 83 -m -- maintain DTR after last close (no HUPCL)\n\ 84 -n -- throw out ICMP packets\n\ 85 -s -- baud rate (default 9600)\n"; 86 87int main(int argc, char **argv) 88{ 89 struct termios tty; 90 int option; 91 int fd; 92 char devname[32]; 93 char *dev = (char *)0; 94 int slipdisc = SLIPDISC; 95 int speed = DEFAULT_BAUD; 96 int slflags = 0; 97 int cflags = HUPCL; 98 99 extern char *optarg; 100 extern int optind; 101 102 while ((option = getopt(argc, argv, "achmns:")) != EOF) { 103 switch (option) { 104 case 'a': 105 slflags |= SC_AUTOCOMP; 106 slflags &= ~SC_COMPRESS; 107 break; 108 case 'c': 109 slflags |= SC_COMPRESS; 110 slflags &= ~SC_AUTOCOMP; 111 break; 112 case 'h': 113 cflags |= CRTSCTS; 114 break; 115 case 'm': 116 cflags &= ~HUPCL; 117 break; 118 case 'n': 119 slflags |= SC_NOICMP; 120 break; 121 case 's': 122 speed = atoi(optarg); 123 break; 124 case '?': 125 default: 126 fprintf(stderr, usage_str, argv[0]); 127 exit(1); 128 } 129 } 130 131 if (optind == argc - 1) 132 dev = argv[optind]; 133 134 135 if (dev == (char *)0) { 136 fprintf(stderr, usage_str, argv[0]); 137 exit(2); 138 } 139 140 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { 141 strcpy(devname, _PATH_DEV); 142 strcat(devname, "/"); 143 strncat(devname, dev, 10); 144 dev = devname; 145 } 146 147 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) { 148 perror(dev); 149 exit(1); 150 } 151 152 if (tcgetattr(fd, &tty) < 0) { 153 perror("tcgetattr"); 154 close(fd); 155 exit(1); 156 } 157 tty.c_iflag = 0; 158 tty.c_oflag = 0; 159 tty.c_cflag = CREAD | CS8 | cflags; 160 tty.c_lflag = 0; 161 tty.c_cc[VMIN] = 1; /* wait for one char */ 162 tty.c_cc[VTIME] = 0; /* wait forever for a char */ 163 cfsetispeed(&tty, speed); 164 cfsetospeed(&tty, speed); 165 if (tcsetattr(fd, TCSADRAIN, &tty) < 0) { 166 perror("tcsetattr"); 167 close(fd); 168 exit(1); 169 } 170 171 if (ioctl(fd, TIOCSDTR) < 0) { 172 perror("ioctl(TIOCSDTR)"); 173 close(fd); 174 exit(1); 175 } 176 177 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) { 178 perror("ioctl(TIOCSETD)"); 179 close(fd); 180 exit(1); 181 } 182 183 if (ioctl(fd, SLIOCSFLAGS, &slflags) < 0) { 184 perror("ioctl(SLIOCSFLAGS)"); 185 close(fd); 186 exit(1); 187 } 188 189 if (fork() > 0) 190 exit(0); 191 192 for (;;) 193 sigpause(0L); 194} 195