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 #ifndef lint 37 #endif /* not lint */ 38 39 #include "tip.h" 40 41 #define MAXRETRY 3 /* sync up retry count */ 42 #define DISCONNECT_CMD "\21\25\11\24" /* disconnection string */ 43 44 static int biz_dialer(char *, char *); 45 static int bizsync(int); 46 static int echo(char *); 47 static void sigALRM(int); 48 static int detect(char *); 49 static int flush(char *); 50 static int bizsync(int); 51 52 static int timeout = 0; 53 static jmp_buf timeoutbuf; 54 55 /* 56 * Dial up on a BIZCOMP Model 1031 with either 57 * tone dialing (mod = "f") 58 * pulse dialing (mod = "w") 59 */ 60 static int 61 biz_dialer(char *num, char *mod) 62 { 63 int connected = 0; 64 65 if (!bizsync(FD)) { 66 logent(value(HOST), "", "biz", "out of sync"); 67 printf("bizcomp out of sync\n"); 68 delock(uucplock); 69 exit(0); 70 } 71 if (boolean(value(VERBOSE))) 72 printf("\nstarting call..."); 73 echo("#\rk$\r$\n"); /* disable auto-answer */ 74 echo("$>$.$ #\r"); /* tone/pulse dialing */ 75 echo(mod); 76 echo("$\r$\n"); 77 echo("$>$.$ #\re$ "); /* disconnection sequence */ 78 echo(DISCONNECT_CMD); 79 echo("\r$\n$\r$\n"); 80 echo("$>$.$ #\rr$ "); /* repeat dial */ 81 echo(num); 82 echo("\r$\n"); 83 if (boolean(value(VERBOSE))) 84 printf("ringing..."); 85 /* 86 * The reply from the BIZCOMP should be: 87 * `^G NO CONNECTION\r\n^G\r\n' failure 88 * ` CONNECTION\r\n^G' success 89 */ 90 connected = detect(" "); 91 #ifdef ACULOG 92 if (timeout) { 93 char line[80]; 94 95 (void)snprintf(line, sizeof line, "%ld second dial timeout", 96 number(value(DIALTIMEOUT))); 97 logent(value(HOST), num, "biz", line); 98 } 99 #endif 100 if (!connected) 101 flush(" NO CONNECTION\r\n\07\r\n"); 102 else 103 flush("CONNECTION\r\n\07"); 104 if (timeout) 105 biz31_disconnect(); /* insurance */ 106 return (connected); 107 } 108 109 int 110 biz31w_dialer(char *num, char *acu) 111 { 112 return (biz_dialer(num, "w")); 113 } 114 115 int 116 biz31f_dialer(char *num, char *acu) 117 { 118 return (biz_dialer(num, "f")); 119 } 120 121 void 122 biz31_disconnect(void) 123 { 124 write(FD, DISCONNECT_CMD, sizeof(DISCONNECT_CMD)-1); 125 sleep(2); 126 tcflush(FD, TCIOFLUSH); 127 } 128 129 void 130 biz31_abort(void) 131 { 132 write(FD, "\33", 1); 133 } 134 135 static int 136 echo(char *s) 137 { 138 char c; 139 140 while (c = *s++) switch (c) { 141 142 case '$': 143 read(FD, &c, 1); 144 s++; 145 break; 146 147 case '#': 148 c = *s++; 149 write(FD, &c, 1); 150 break; 151 152 default: 153 write(FD, &c, 1); 154 read(FD, &c, 1); 155 } 156 } 157 158 /*ARGSUSED*/ 159 static void 160 sigALRM(int signo) 161 { 162 timeout = 1; 163 longjmp(timeoutbuf, 1); 164 } 165 166 static int 167 detect(char *s) 168 { 169 sig_t f; 170 char c; 171 172 f = signal(SIGALRM, sigALRM); 173 timeout = 0; 174 while (*s) { 175 if (setjmp(timeoutbuf)) { 176 printf("\07timeout waiting for reply\n"); 177 biz31_abort(); 178 break; 179 } 180 alarm(number(value(DIALTIMEOUT))); 181 read(FD, &c, 1); 182 alarm(0); 183 if (c != *s++) 184 break; 185 } 186 signal(SIGALRM, f); 187 return (timeout == 0); 188 } 189 190 static int 191 flush(char *s) 192 { 193 sig_t f; 194 char c; 195 196 f = signal(SIGALRM, sigALRM); 197 while (*s++) { 198 if (setjmp(timeoutbuf)) 199 break; 200 alarm(10); 201 read(FD, &c, 1); 202 alarm(0); 203 } 204 signal(SIGALRM, f); 205 timeout = 0; /* guard against disconnection */ 206 } 207 208 /* 209 * This convoluted piece of code attempts to get 210 * the bizcomp in sync. If you don't have the capacity or nread 211 * call there are gory ways to simulate this. 212 */ 213 static int 214 bizsync(int fd) 215 { 216 #ifdef FIOCAPACITY 217 struct capacity b; 218 # define chars(b) ((b).cp_nbytes) 219 # define IOCTL FIOCAPACITY 220 #endif 221 #ifdef FIONREAD 222 long b; 223 # define chars(b) (b) 224 # define IOCTL FIONREAD 225 #endif 226 int already = 0; 227 char buf[10]; 228 229 retry: 230 if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0) 231 tcflush(FD, TCIOFLUSH); 232 write(fd, "\rp>\r", 4); 233 sleep(1); 234 if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) { 235 if (chars(b) != 10) { 236 nono: 237 if (already > MAXRETRY) 238 return (0); 239 write(fd, DISCONNECT_CMD, 4); 240 sleep(2); 241 already++; 242 goto retry; 243 } else { 244 read(fd, buf, 10); 245 if (strncmp(buf, "p >\r\n\r\n>", 8)) 246 goto nono; 247 } 248 } 249 return (1); 250 } 251