1 /* $OpenBSD: v831.c,v 1.10 2005/02/17 12:45:42 aaron Exp $ */
2 /* $NetBSD: v831.c,v 1.5 1996/12/29 10:42:01 cgd Exp $ */
3
4 /*
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)v831.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 static const char rcsid[] = "$OpenBSD: v831.c,v 1.10 2005/02/17 12:45:42 aaron Exp $";
38 #endif /* not lint */
39
40 /*
41 * Routines for dialing up on Vadic 831
42 */
43 #include "tip.h"
44 #include <termios.h>
45
46 void v831_abort();
47 static void alarmtr();
48 static int dialit();
49 static char *sanitize();
50
51 static jmp_buf jmpbuf;
52 static pid_t child = -1;
53
54 int
v831_dialer(num,acu)55 v831_dialer(num, acu)
56 char *num, *acu;
57 {
58 int status;
59 int timelim;
60 pid_t pid;
61
62 if (boolean(value(VERBOSE)))
63 printf("\nstarting call...");
64 #ifdef DEBUG
65 printf ("(acu=%s)\n", acu);
66 #endif
67 if ((AC = open(acu, O_RDWR)) < 0) {
68 if (errno == EBUSY)
69 printf("line busy...");
70 else
71 printf("acu open error...");
72 return (0);
73 }
74 if (setjmp(jmpbuf)) {
75 kill(child, SIGKILL);
76 close(AC);
77 return (0);
78 }
79 signal(SIGALRM, alarmtr);
80 timelim = 5 * strlen(num);
81 alarm(timelim < 30 ? 30 : timelim);
82 if ((child = fork()) == 0) {
83 /*
84 * ignore this stuff for aborts
85 */
86 signal(SIGALRM, SIG_IGN);
87 signal(SIGINT, SIG_IGN);
88 signal(SIGQUIT, SIG_IGN);
89 sleep(2);
90 exit(dialit(num, acu) != 'A');
91 }
92 /*
93 * open line - will return on carrier
94 */
95 if ((FD = open(DV, O_RDWR)) < 0) {
96 #ifdef DEBUG
97 printf("(after open, errno=%d)\n", errno);
98 #endif
99 if (errno == EIO)
100 printf("lost carrier...");
101 else
102 printf("dialup line open failed...");
103 alarm(0);
104 kill(child, SIGKILL);
105 close(AC);
106 return (0);
107 }
108 alarm(0);
109 signal(SIGALRM, SIG_DFL);
110 while ((pid = wait(&status)) != child && pid != -1)
111 ;
112 if (status) {
113 close(AC);
114 return (0);
115 }
116 return (1);
117 }
118
119 static void
alarmtr()120 alarmtr()
121 {
122 alarm(0);
123 longjmp(jmpbuf, 1);
124 }
125
126 /*
127 * Insurance, for some reason we don't seem to be
128 * hanging up...
129 */
130 void
v831_disconnect()131 v831_disconnect()
132 {
133 struct termios cntrl;
134
135 sleep(2);
136 #ifdef DEBUG
137 printf("[disconnect: FD=%d]\n", FD);
138 #endif
139 if (FD > 0) {
140 ioctl(FD, TIOCCDTR, 0);
141 tcgetattr(FD, &cntrl);
142 cfsetospeed(&cntrl, 0);
143 cfsetispeed(&cntrl, 0);
144 tcsetattr(FD, TCSAFLUSH, &cntrl);
145 ioctl(FD, TIOCNXCL, NULL);
146 }
147 close(FD);
148 }
149
150 void
v831_abort()151 v831_abort()
152 {
153
154 #ifdef DEBUG
155 printf("[abort: AC=%d]\n", AC);
156 #endif
157 sleep(2);
158 if (child > 0)
159 kill(child, SIGKILL);
160 if (FD > 0)
161 ioctl(FD, TIOCNXCL, NULL);
162 close(AC);
163 if (FD > 0)
164 ioctl(FD, TIOCCDTR, 0);
165 close(FD);
166 }
167
168 /*
169 * Sigh, this probably must be changed at each site.
170 */
171 struct vaconfig {
172 char *vc_name;
173 char vc_rack;
174 char vc_modem;
175 } vaconfig[] = {
176 { "/dev/cua0",'4','0' },
177 { "/dev/cua1",'4','1' },
178 { 0 }
179 };
180
181 #define pc(x) (c = x, write(AC,&c,1))
182 #define ABORT 01
183 #define SI 017
184 #define STX 02
185 #define ETX 03
186
187 static int
dialit(phonenum,acu)188 dialit(phonenum, acu)
189 char *phonenum;
190 char *acu;
191 {
192 struct vaconfig *vp;
193 struct termios cntrl;
194 char c;
195 int i;
196
197 phonenum = sanitize(phonenum);
198 #ifdef DEBUG
199 printf ("(dial phonenum=%s)\n", phonenum);
200 #endif
201 if (*phonenum == '<' && phonenum[1] == 0)
202 return ('Z');
203 for (vp = vaconfig; vp->vc_name; vp++)
204 if (strcmp(vp->vc_name, acu) == 0)
205 break;
206 if (vp->vc_name == 0) {
207 printf("Unable to locate dialer (%s)\n", acu);
208 return ('K');
209 }
210 tcgetattr(AC, &cntrl);
211 cfsetospeed(&cntrl, B2400);
212 cfsetispeed(&cntrl, B2400);
213 cntrl.c_cflag |= PARODD | PARENB;
214 cntrl.c_lflag &= ~(ISIG | ICANON);
215 tcsetattr(AC, TCSANOW, &cntrl);
216 tcflush(AC, TCIOFLUSH);
217 pc(STX);
218 pc(vp->vc_rack);
219 pc(vp->vc_modem);
220 while (*phonenum && *phonenum != '<')
221 pc(*phonenum++);
222 pc(SI);
223 pc(ETX);
224 sleep(1);
225 i = read(AC, &c, 1);
226 #ifdef DEBUG
227 printf("read %d chars, char=%c, errno %d\n", i, c, errno);
228 #endif
229 if (i != 1)
230 c = 'M';
231 if (c == 'B' || c == 'G') {
232 char cc, oc = c;
233
234 pc(ABORT);
235 read(AC, &cc, 1);
236 #ifdef DEBUG
237 printf("abort response=%c\n", cc);
238 #endif
239 c = oc;
240 v831_disconnect();
241 }
242 close(AC);
243 #ifdef DEBUG
244 printf("dialit: returns %c\n", c);
245 #endif
246 return (c);
247 }
248
249 static char *
sanitize(s)250 sanitize(s)
251 char *s;
252 {
253 static char buf[128];
254 char *cp;
255
256 for (cp = buf; *s; s++) {
257 if (!isdigit(*s) && *s == '<' && *s != '_')
258 continue;
259 if (*s == '_')
260 *s = '=';
261 *cp++ = *s;
262 }
263 *cp++ = 0;
264 return (buf);
265 }
266