slattach.c revision 1.4
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 60static char sccsid[] = "@(#)slattach.c 4.6 (Berkeley) 6/1/90"; 61static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/sbin/slattach/slattach.c,v 1.4 1993/04/08 04:15:43 cgd 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 ][-n ][-s <speed> ]<device>\n\ 80 -a -- autoenable VJ compression\n\ 81 -c -- enable VJ compression\n\ 82 -n -- throw out ICMP packets\n\ 83 -h -- turn on cts/rts style flow control\n\ 84 -s -- baud rate (default 9600)\n"; 85 86int main(int argc, char **argv) 87{ 88 struct termios tty; 89 int option; 90 int fd; 91 char devname[32]; 92 char *dev = (char *)0; 93 int slipdisc = SLIPDISC; 94 int speed = DEFAULT_BAUD; 95 int slflags = 0; 96 int flow_control = 0; /* extra flags to enable hardware flow cont. */ 97 98 extern char *optarg; 99 extern int optind; 100 101 while ((option = getopt(argc, argv, "achns:")) != EOF) { 102 switch (option) { 103 case 'a': 104 slflags |= SC_AUTOCOMP; 105 slflags &= ~SC_COMPRESS; 106 break; 107 case 'c': 108 slflags |= SC_COMPRESS; 109 slflags &= ~SC_AUTOCOMP; 110 break; 111 case 'h': 112 flow_control |= CRTSCTS; 113 break; 114 case 'n': 115 slflags |= SC_NOICMP; 116 break; 117 case 's': 118 speed = atoi(optarg); 119 break; 120 case '?': 121 default: 122 fprintf(stderr, usage_str, argv[0]); 123 exit(1); 124 } 125 } 126 127 if (optind == argc - 1) 128 dev = argv[optind]; 129 130 131 if (dev == (char *)0) { 132 fprintf(stderr, usage_str, argv[0]); 133 exit(2); 134 } 135 136 if ((speed = findspeed(speed)) == 0) { 137 fprintf(stderr, "unknown speed"); 138 exit(1); 139 } 140 141 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { 142 strcpy(devname, _PATH_DEV); 143 strcat(devname, "/"); 144 strncat(devname, dev, 10); 145 dev = devname; 146 } 147 148 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) { 149 perror(dev); 150 exit(1); 151 } 152 153 tty.c_iflag = 0; 154 tty.c_oflag = 0; 155 tty.c_cflag = CREAD | CS8 | flow_control; 156 tty.c_lflag = 0; 157 tty.c_cc[VMIN] = 1; /* wait for one char */ 158 tty.c_cc[VTIME] = 0; /* wait forever for a char */ 159 if (ioctl(fd, TIOCSETA, &tty) < 0) { 160 perror("ioctl(TIOCSETA)"); 161 close(fd); 162 exit(1); 163 } 164 165 cfsetispeed(&tty, speed); 166 cfsetospeed(&tty, speed); 167 if (tcsetattr(fd, TCSADRAIN, &tty) < 0) { 168 perror("tcsetattr"); 169 close(fd); 170 exit(1); 171 } 172 173 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) { 174 perror("ioctl(TIOCSETD)"); 175 close(fd); 176 exit(1); 177 } 178 179 if (ioctl(fd, SLIOCSFLAGS, &slflags) < 0) { 180 perror("ioctl(SLIOCSFLAGS)"); 181 close(fd); 182 exit(1); 183 } 184 185 if (fork() > 0) 186 exit(0); 187 188 for (;;) 189 sigpause(0L); 190} 191 192struct sg_spds { 193 int sp_val, sp_name; 194} spds[] = { 195#ifdef B50 196 { 50, B50 }, 197#endif 198#ifdef B75 199 { 75, B75 }, 200#endif 201#ifdef B110 202 { 110, B110 }, 203#endif 204#ifdef B150 205 { 150, B150 }, 206#endif 207#ifdef B200 208 { 200, B200 }, 209#endif 210#ifdef B300 211 { 300, B300 }, 212#endif 213#ifdef B600 214 { 600, B600 }, 215#endif 216#ifdef B1200 217 { 1200, B1200 }, 218#endif 219#ifdef B1800 220 { 1800, B1800 }, 221#endif 222#ifdef B2000 223 { 2000, B2000 }, 224#endif 225#ifdef B2400 226 { 2400, B2400 }, 227#endif 228#ifdef B3600 229 { 3600, B3600 }, 230#endif 231#ifdef B4800 232 { 4800, B4800 }, 233#endif 234#ifdef B7200 235 { 7200, B7200 }, 236#endif 237#ifdef B9600 238 { 9600, B9600 }, 239#endif 240#ifdef EXTA 241 { 19200, EXTA }, 242#endif 243#ifdef EXTB 244 { 38400, EXTB }, 245#endif 246 { 0, 0 } 247}; 248 249int findspeed(int speed) 250{ 251 struct sg_spds *sp = spds; 252 253 while ((sp->sp_val != 0) && (sp->sp_val != speed)) 254 sp++; 255 256 return (sp->sp_name); 257} 258