xref: /netbsd-src/sbin/slattach/slattach.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
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
54 char 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[] = "@(#)slattach.c	4.6 (Berkeley) 6/1/90";
61 static char rcsid[] = "$Header: /cvsroot/src/sbin/slattach/slattach.c,v 1.6 1993/06/29 19:16:38 brezak 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 
78 static char usage_str[] = "\
79 usage: %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 
86 int 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 	if (ioctl(fd, TIOCSDTR) < 0) {
166                 perror("ioctl(TIOCSDTR)");
167                 close(fd);
168                 exit(1);
169         }
170 
171 	cfsetispeed(&tty, speed);
172 	cfsetospeed(&tty, speed);
173 	if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
174 		perror("tcsetattr");
175 		close(fd);
176 		exit(1);
177 	}
178 
179 	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
180 		perror("ioctl(TIOCSETD)");
181 		close(fd);
182 		exit(1);
183 	}
184 
185 	if (ioctl(fd, SLIOCSFLAGS, &slflags) < 0) {
186 		perror("ioctl(SLIOCSFLAGS)");
187 		close(fd);
188 		exit(1);
189 	}
190 
191 	if (fork() > 0)
192 		exit(0);
193 
194 	for (;;)
195 		sigpause(0L);
196 }
197 
198 struct sg_spds {
199 	int sp_val, sp_name;
200 }       spds[] = {
201 #ifdef B50
202 	{ 50, B50 },
203 #endif
204 #ifdef B75
205 	{ 75, B75 },
206 #endif
207 #ifdef B110
208 	{ 110, B110 },
209 #endif
210 #ifdef B150
211 	{ 150, B150 },
212 #endif
213 #ifdef B200
214 	{ 200, B200 },
215 #endif
216 #ifdef B300
217 	{ 300, B300 },
218 #endif
219 #ifdef B600
220 	{ 600, B600 },
221 #endif
222 #ifdef B1200
223 	{ 1200, B1200 },
224 #endif
225 #ifdef B1800
226 	{ 1800, B1800 },
227 #endif
228 #ifdef B2000
229 	{ 2000, B2000 },
230 #endif
231 #ifdef B2400
232 	{ 2400, B2400 },
233 #endif
234 #ifdef B3600
235 	{ 3600, B3600 },
236 #endif
237 #ifdef B4800
238 	{ 4800, B4800 },
239 #endif
240 #ifdef B7200
241 	{ 7200, B7200 },
242 #endif
243 #ifdef B9600
244 	{ 9600, B9600 },
245 #endif
246 #ifdef B19200
247 	{ 19200, B19200 },
248 #endif
249 #ifdef B38400
250 	{ 38400, B38400 },
251 #endif
252 #ifdef B57600
253 	{ 57600, B57600 },
254 #endif
255 #ifdef B115200
256 	{ 115200, B115200 },
257 #endif
258 	{ 0, 0 }
259 };
260 
261 int findspeed(int speed)
262 {
263 	struct sg_spds *sp = spds;
264 
265 	while ((sp->sp_val != 0) && (sp->sp_val != speed))
266 		sp++;
267 
268 	return (sp->sp_name);
269 }
270