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