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