biz31.c revision 1.6 1 /* $NetBSD: biz31.c,v 1.6 1997/11/22 07:28:53 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)biz31.c 8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: biz31.c,v 1.6 1997/11/22 07:28:53 lukem Exp $");
42 #endif /* not lint */
43
44 #include "tip.h"
45
46 #define MAXRETRY 3 /* sync up retry count */
47 #define DISCONNECT_CMD "\21\25\11\24" /* disconnection string */
48
49 static void sigALRM();
50 static int timeout = 0;
51 static jmp_buf timeoutbuf;
52
53 /*
54 * Dial up on a BIZCOMP Model 1031 with either
55 * tone dialing (mod = "f")
56 * pulse dialing (mod = "w")
57 */
58 static int
59 biz_dialer(num, mod)
60 char *num, *mod;
61 {
62 int connected = 0;
63
64 if (!bizsync(FD)) {
65 logent(value(HOST), "", "biz", "out of sync");
66 printf("bizcomp out of sync\n");
67 delock(uucplock);
68 exit(0);
69 }
70 if (boolean(value(VERBOSE)))
71 printf("\nstarting call...");
72 echo("#\rk$\r$\n"); /* disable auto-answer */
73 echo("$>$.$ #\r"); /* tone/pulse dialing */
74 echo(mod);
75 echo("$\r$\n");
76 echo("$>$.$ #\re$ "); /* disconnection sequence */
77 echo(DISCONNECT_CMD);
78 echo("\r$\n$\r$\n");
79 echo("$>$.$ #\rr$ "); /* repeat dial */
80 echo(num);
81 echo("\r$\n");
82 if (boolean(value(VERBOSE)))
83 printf("ringing...");
84 /*
85 * The reply from the BIZCOMP should be:
86 * `^G NO CONNECTION\r\n^G\r\n' failure
87 * ` CONNECTION\r\n^G' success
88 */
89 connected = detect(" ");
90 #ifdef ACULOG
91 if (timeout) {
92 char line[80];
93
94 (void)snprintf(line, sizeof line, "%d second dial timeout",
95 number(value(DIALTIMEOUT)));
96 logent(value(HOST), num, "biz", line);
97 }
98 #endif
99 if (!connected)
100 flush(" NO CONNECTION\r\n\07\r\n");
101 else
102 flush("CONNECTION\r\n\07");
103 if (timeout)
104 biz31_disconnect(); /* insurance */
105 return (connected);
106 }
107
108 biz31w_dialer(num, acu)
109 char *num, *acu;
110 {
111
112 return (biz_dialer(num, "w"));
113 }
114
115 biz31f_dialer(num, acu)
116 char *num, *acu;
117 {
118
119 return (biz_dialer(num, "f"));
120 }
121
122 biz31_disconnect()
123 {
124
125 write(FD, DISCONNECT_CMD, 4);
126 sleep(2);
127 tcflush(FD, TCIOFLUSH);
128 }
129
130 biz31_abort()
131 {
132
133 write(FD, "\33", 1);
134 }
135
136 static int
137 echo(s)
138 char *s;
139 {
140 char c;
141
142 while (c = *s++) switch (c) {
143
144 case '$':
145 read(FD, &c, 1);
146 s++;
147 break;
148
149 case '#':
150 c = *s++;
151 write(FD, &c, 1);
152 break;
153
154 default:
155 write(FD, &c, 1);
156 read(FD, &c, 1);
157 }
158 }
159
160 static void
161 sigALRM()
162 {
163
164 timeout = 1;
165 longjmp(timeoutbuf, 1);
166 }
167
168 static int
169 detect(s)
170 char *s;
171 {
172 sig_t f;
173 char c;
174
175 f = signal(SIGALRM, sigALRM);
176 timeout = 0;
177 while (*s) {
178 if (setjmp(timeoutbuf)) {
179 printf("\07timeout waiting for reply\n");
180 biz31_abort();
181 break;
182 }
183 alarm(number(value(DIALTIMEOUT)));
184 read(FD, &c, 1);
185 alarm(0);
186 if (c != *s++)
187 break;
188 }
189 signal(SIGALRM, f);
190 return (timeout == 0);
191 }
192
193 static int
194 flush(s)
195 char *s;
196 {
197 sig_t f;
198 char c;
199
200 f = signal(SIGALRM, sigALRM);
201 while (*s++) {
202 if (setjmp(timeoutbuf))
203 break;
204 alarm(10);
205 read(FD, &c, 1);
206 alarm(0);
207 }
208 signal(SIGALRM, f);
209 timeout = 0; /* guard against disconnection */
210 }
211
212 /*
213 * This convoluted piece of code attempts to get
214 * the bizcomp in sync. If you don't have the capacity or nread
215 * call there are gory ways to simulate this.
216 */
217 static int
218 bizsync(fd)
219 {
220 #ifdef FIOCAPACITY
221 struct capacity b;
222 # define chars(b) ((b).cp_nbytes)
223 # define IOCTL FIOCAPACITY
224 #endif
225 #ifdef FIONREAD
226 long b;
227 # define chars(b) (b)
228 # define IOCTL FIONREAD
229 #endif
230 int already = 0;
231 char buf[10];
232
233 retry:
234 if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
235 tcflush(FD, TCIOFLUSH);
236 write(fd, "\rp>\r", 4);
237 sleep(1);
238 if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
239 if (chars(b) != 10) {
240 nono:
241 if (already > MAXRETRY)
242 return (0);
243 write(fd, DISCONNECT_CMD, 4);
244 sleep(2);
245 already++;
246 goto retry;
247 } else {
248 read(fd, buf, 10);
249 if (strncmp(buf, "p >\r\n\r\n>", 8))
250 goto nono;
251 }
252 }
253 return (1);
254 }
255