xref: /titanic_53/usr/src/cmd/bnu/eio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "uucp.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef	E_PROTOCOL
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifndef MIN
38*7c478bd9Sstevel@tonic-gate #define     MIN(a,b) (((a)<(b))?(a):(b))
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #if defined(BSD4_2) || defined (ATTSVR4)
42*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
43*7c478bd9Sstevel@tonic-gate #endif /* BSD4_2 || ATTSVR4 */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	EBUFSIZ	1024
46*7c478bd9Sstevel@tonic-gate #define	EMESGLEN 20
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define TBUFSIZE 1024
49*7c478bd9Sstevel@tonic-gate #define TPACKSIZE	512
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate extern long lseek();	/* Find offset into the file. */
52*7c478bd9Sstevel@tonic-gate static jmp_buf Failbuf;
53*7c478bd9Sstevel@tonic-gate extern int erdblk();
54*7c478bd9Sstevel@tonic-gate extern unsigned msgtime;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static char Erdstash[EBUFSIZ];
57*7c478bd9Sstevel@tonic-gate static int Erdlen;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * error-free channel protocol
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
63*7c478bd9Sstevel@tonic-gate static void
64*7c478bd9Sstevel@tonic-gate ealarm(sig)
65*7c478bd9Sstevel@tonic-gate int sig;
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	longjmp(Failbuf, 1);
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate static void (*esig)();
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate  * turn on protocol timer
73*7c478bd9Sstevel@tonic-gate  */
74*7c478bd9Sstevel@tonic-gate int
75*7c478bd9Sstevel@tonic-gate eturnon()
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	esig=signal(SIGALRM, ealarm);
78*7c478bd9Sstevel@tonic-gate 	return(0);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate int
82*7c478bd9Sstevel@tonic-gate eturnoff()
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	signal(SIGALRM, esig);
85*7c478bd9Sstevel@tonic-gate 	return(0);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * write message across link
90*7c478bd9Sstevel@tonic-gate  *	type	-> message type
91*7c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
92*7c478bd9Sstevel@tonic-gate  *	fn	-> link file descriptor
93*7c478bd9Sstevel@tonic-gate  * return
94*7c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
95*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> write succeeded
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate int
98*7c478bd9Sstevel@tonic-gate ewrmsg(type, str, fn)
99*7c478bd9Sstevel@tonic-gate register char *str;
100*7c478bd9Sstevel@tonic-gate int fn;
101*7c478bd9Sstevel@tonic-gate char type;
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	return(etwrmsg(type, str, fn, 0));
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * read message from link
108*7c478bd9Sstevel@tonic-gate  *	str	-> message buffer
109*7c478bd9Sstevel@tonic-gate  *	fn	-> file descriptor
110*7c478bd9Sstevel@tonic-gate  * return
111*7c478bd9Sstevel@tonic-gate  *	FAIL	-> read timed out
112*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate int
115*7c478bd9Sstevel@tonic-gate erdmsg(str, fn)
116*7c478bd9Sstevel@tonic-gate register char *str;
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	return(etrdmsg(str, fn, 0));
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * read data from file fp1 and write
123*7c478bd9Sstevel@tonic-gate  * on link
124*7c478bd9Sstevel@tonic-gate  *	fp1	-> file descriptor
125*7c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
126*7c478bd9Sstevel@tonic-gate  * returns:
127*7c478bd9Sstevel@tonic-gate  *	FAIL	->failure in link
128*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate int
131*7c478bd9Sstevel@tonic-gate ewrdata(fp1, fn)
132*7c478bd9Sstevel@tonic-gate register FILE *fp1;
133*7c478bd9Sstevel@tonic-gate int	fn;
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	register int ret;
136*7c478bd9Sstevel@tonic-gate 	int	fd1;
137*7c478bd9Sstevel@tonic-gate 	int len;
138*7c478bd9Sstevel@tonic-gate 	unsigned long bytes;
139*7c478bd9Sstevel@tonic-gate 	char bufr[EBUFSIZ];
140*7c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
141*7c478bd9Sstevel@tonic-gate 	off_t	msglen;
142*7c478bd9Sstevel@tonic-gate 	char	cmsglen[EMESGLEN];
143*7c478bd9Sstevel@tonic-gate 	off_t	startPoint;	/* Offset from begining of the file in
144*7c478bd9Sstevel@tonic-gate 				 *   case we are restarting from a check
145*7c478bd9Sstevel@tonic-gate 				 *   point.
146*7c478bd9Sstevel@tonic-gate 				 */
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (setjmp(Failbuf)) {
149*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "ewrdata failed\n%s", "");
150*7c478bd9Sstevel@tonic-gate 		return(FAIL);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	bytes = 0L;
153*7c478bd9Sstevel@tonic-gate 	fd1 = fileno(fp1);
154*7c478bd9Sstevel@tonic-gate 	fstat(fd1, &statbuf);
155*7c478bd9Sstevel@tonic-gate 	startPoint = lseek(fd1, 0L, 1);
156*7c478bd9Sstevel@tonic-gate 	if (startPoint < 0)
157*7c478bd9Sstevel@tonic-gate 	{
158*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "ewrdata lseek failed.  Errno=%d\n", errno);
159*7c478bd9Sstevel@tonic-gate 		return(FAIL);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 	msglen = statbuf.st_size - startPoint;
162*7c478bd9Sstevel@tonic-gate 	if (msglen < 0)
163*7c478bd9Sstevel@tonic-gate 	{
164*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "ewrdata: startPoint past end of file.\n%s", "");
165*7c478bd9Sstevel@tonic-gate 		return(FAIL);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 	sprintf(cmsglen, "%ld", (long) msglen);
168*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "ewrdata writing %d ...", sizeof(cmsglen));
169*7c478bd9Sstevel@tonic-gate 	alarm(msgtime);
170*7c478bd9Sstevel@tonic-gate 	ret = (*Write)(fn, cmsglen, sizeof(cmsglen));
171*7c478bd9Sstevel@tonic-gate 	alarm(0);
172*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "ret %d\n", ret);
173*7c478bd9Sstevel@tonic-gate 	if (ret != sizeof(cmsglen))
174*7c478bd9Sstevel@tonic-gate 		return(FAIL);
175*7c478bd9Sstevel@tonic-gate 	DEBUG(7, "ewrdata planning to send %ld bytes to remote.\n", msglen);
176*7c478bd9Sstevel@tonic-gate 	while ((len = read( fd1, bufr, EBUFSIZ )) > 0) {
177*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "ewrdata writing %d ...", len);
178*7c478bd9Sstevel@tonic-gate 		alarm(msgtime);
179*7c478bd9Sstevel@tonic-gate 		bytes += len;
180*7c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
181*7c478bd9Sstevel@tonic-gate 		ret = (*Write)(fn, bufr, (unsigned) len);
182*7c478bd9Sstevel@tonic-gate 		alarm(0);
183*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "ewrdata ret %d\n", ret);
184*7c478bd9Sstevel@tonic-gate 		if (ret != len)
185*7c478bd9Sstevel@tonic-gate 			return(FAIL);
186*7c478bd9Sstevel@tonic-gate 		if ((msglen -= len) <= 0)
187*7c478bd9Sstevel@tonic-gate 			break;
188*7c478bd9Sstevel@tonic-gate 	}
189*7c478bd9Sstevel@tonic-gate 	if (len < 0 || (len == 0 && msglen != 0)) return(FAIL);
190*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate  * read data from link and
195*7c478bd9Sstevel@tonic-gate  * write into file
196*7c478bd9Sstevel@tonic-gate  *	fp2	-> file descriptor
197*7c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
198*7c478bd9Sstevel@tonic-gate  * returns:
199*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
200*7c478bd9Sstevel@tonic-gate  *	FAIL	-> failure on link
201*7c478bd9Sstevel@tonic-gate  */
202*7c478bd9Sstevel@tonic-gate int
203*7c478bd9Sstevel@tonic-gate erddata(fn, fp2)
204*7c478bd9Sstevel@tonic-gate register FILE *fp2;
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate 	register int ret;
207*7c478bd9Sstevel@tonic-gate 	int	fd2;
208*7c478bd9Sstevel@tonic-gate 	char bufr[EBUFSIZ];
209*7c478bd9Sstevel@tonic-gate 	int	len;
210*7c478bd9Sstevel@tonic-gate 	long	msglen, bytes;
211*7c478bd9Sstevel@tonic-gate 	char	cmsglen[EMESGLEN], *cptr, *erdptr = Erdstash;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "erddata wants %d\n", sizeof(cmsglen));
214*7c478bd9Sstevel@tonic-gate 	if (Erdlen > 0) {
215*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "%d bytes stashed\n", Erdlen);
216*7c478bd9Sstevel@tonic-gate 		if (Erdlen >= sizeof(cmsglen)) {
217*7c478bd9Sstevel@tonic-gate 			memcpy(cmsglen, erdptr, sizeof(cmsglen));
218*7c478bd9Sstevel@tonic-gate 			Erdlen -= sizeof(cmsglen);
219*7c478bd9Sstevel@tonic-gate 			erdptr += sizeof(cmsglen);
220*7c478bd9Sstevel@tonic-gate 			ret = len = 0;
221*7c478bd9Sstevel@tonic-gate 		} else {
222*7c478bd9Sstevel@tonic-gate 			memcpy(cmsglen, Erdstash, Erdlen);
223*7c478bd9Sstevel@tonic-gate 			cptr = cmsglen + Erdlen;
224*7c478bd9Sstevel@tonic-gate 			len = sizeof(cmsglen) - Erdlen;
225*7c478bd9Sstevel@tonic-gate 			ret = erdblk(cptr, len, fn);
226*7c478bd9Sstevel@tonic-gate 			Erdlen = 0;
227*7c478bd9Sstevel@tonic-gate 		}
228*7c478bd9Sstevel@tonic-gate 	} else {
229*7c478bd9Sstevel@tonic-gate 		len = sizeof(cmsglen);
230*7c478bd9Sstevel@tonic-gate 		ret = erdblk(cmsglen, sizeof(cmsglen), fn);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	if (ret != len)
233*7c478bd9Sstevel@tonic-gate 		return(FAIL);
234*7c478bd9Sstevel@tonic-gate 	ret = SUCCESS;
235*7c478bd9Sstevel@tonic-gate 	sscanf(cmsglen, "%ld", &msglen);
236*7c478bd9Sstevel@tonic-gate 	if ( ((msglen-1)/512 +1) > Ulimit )
237*7c478bd9Sstevel@tonic-gate 		ret = EFBIG;
238*7c478bd9Sstevel@tonic-gate 	DEBUG(7, "erddata file is %ld bytes\n", msglen);
239*7c478bd9Sstevel@tonic-gate 	fd2 = fileno( fp2 );
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	if (Erdlen > 0) {
242*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "%d bytes stashed\n", Erdlen);
243*7c478bd9Sstevel@tonic-gate 		if (write(fileno(fp2), erdptr, Erdlen) != Erdlen)
244*7c478bd9Sstevel@tonic-gate 			return(FAIL);
245*7c478bd9Sstevel@tonic-gate 		msglen -= Erdlen;
246*7c478bd9Sstevel@tonic-gate 		Erdlen = 0;
247*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "erddata remainder is %ld bytes\n", msglen);
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	for (;;) {
251*7c478bd9Sstevel@tonic-gate 		len = erdblk(bufr, (int) MIN(msglen, EBUFSIZ), fn);
252*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "erdblk ret %d\n", len);
253*7c478bd9Sstevel@tonic-gate 		if (len < 0) {
254*7c478bd9Sstevel@tonic-gate 			DEBUG(7, "erdblk failed\n%s", "");
255*7c478bd9Sstevel@tonic-gate 			return(FAIL);
256*7c478bd9Sstevel@tonic-gate 		}
257*7c478bd9Sstevel@tonic-gate 		bytes += len;
258*7c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
259*7c478bd9Sstevel@tonic-gate 		if ((msglen -= len) < 0) {
260*7c478bd9Sstevel@tonic-gate 			DEBUG(7, "erdblk read too much\n%s", "");
261*7c478bd9Sstevel@tonic-gate 			return(FAIL);
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 		/* this write is to file -- use write(2), not (*Write) */
264*7c478bd9Sstevel@tonic-gate 		if ( ret == SUCCESS && write( fd2, bufr, len ) != len ) {
265*7c478bd9Sstevel@tonic-gate 			ret = errno;
266*7c478bd9Sstevel@tonic-gate 			DEBUG(7, "erddata: write to file failed, errno %d\n", ret);
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 		if (msglen == 0)
269*7c478bd9Sstevel@tonic-gate 			break;
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate 	return(ret);
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate /*
275*7c478bd9Sstevel@tonic-gate  * read block from link
276*7c478bd9Sstevel@tonic-gate  * reads are timed
277*7c478bd9Sstevel@tonic-gate  *	blk	-> address of buffer
278*7c478bd9Sstevel@tonic-gate  *	len	-> size to read
279*7c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
280*7c478bd9Sstevel@tonic-gate  * returns:
281*7c478bd9Sstevel@tonic-gate  *	FAIL	-> link error timeout on link
282*7c478bd9Sstevel@tonic-gate  *	i	-> # of bytes read (must not be 0)
283*7c478bd9Sstevel@tonic-gate  */
284*7c478bd9Sstevel@tonic-gate int
285*7c478bd9Sstevel@tonic-gate erdblk(blk, len,  fn)
286*7c478bd9Sstevel@tonic-gate register char *blk;
287*7c478bd9Sstevel@tonic-gate {
288*7c478bd9Sstevel@tonic-gate 	register int i, ret;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	if(setjmp(Failbuf)) {
291*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "timeout (%d sec)\n", msgtime);
292*7c478bd9Sstevel@tonic-gate 		return(FAIL);
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	alarm(msgtime);
296*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += ret) {
297*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "erdblk ask %d ", len - i);
298*7c478bd9Sstevel@tonic-gate 		if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
299*7c478bd9Sstevel@tonic-gate 			alarm(0);
300*7c478bd9Sstevel@tonic-gate 			DEBUG(7, "erdblk read failed\n%s", "");
301*7c478bd9Sstevel@tonic-gate 			return(FAIL);
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "erdblk got %d\n", ret);
304*7c478bd9Sstevel@tonic-gate 		if (ret == 0)
305*7c478bd9Sstevel@tonic-gate 			break;
306*7c478bd9Sstevel@tonic-gate 		blk += ret;
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 	alarm(0);
309*7c478bd9Sstevel@tonic-gate 	return(i);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate struct tbuf {
313*7c478bd9Sstevel@tonic-gate 	long t_nbytes;
314*7c478bd9Sstevel@tonic-gate 	char t_data[TBUFSIZE];
315*7c478bd9Sstevel@tonic-gate };
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate /*
318*7c478bd9Sstevel@tonic-gate  * read message from link
319*7c478bd9Sstevel@tonic-gate  *	str	-> message buffer
320*7c478bd9Sstevel@tonic-gate  *	fn	-> file descriptor
321*7c478bd9Sstevel@tonic-gate  * return
322*7c478bd9Sstevel@tonic-gate  *	FAIL	-> read timed out
323*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
324*7c478bd9Sstevel@tonic-gate  */
325*7c478bd9Sstevel@tonic-gate trdmsg(str, fn)
326*7c478bd9Sstevel@tonic-gate char *str;
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate 	return(etrdmsg(str, fn, TPACKSIZE));
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate /*
332*7c478bd9Sstevel@tonic-gate  * write message across link
333*7c478bd9Sstevel@tonic-gate  *	type	-> message type
334*7c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
335*7c478bd9Sstevel@tonic-gate  *	fn	-> link file descriptor
336*7c478bd9Sstevel@tonic-gate  * return
337*7c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
338*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> write succeeded
339*7c478bd9Sstevel@tonic-gate  */
340*7c478bd9Sstevel@tonic-gate twrmsg(type, str, fn)
341*7c478bd9Sstevel@tonic-gate char type;
342*7c478bd9Sstevel@tonic-gate char *str;
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate 	return(etwrmsg(type, str, fn, TPACKSIZE));
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate /*
348*7c478bd9Sstevel@tonic-gate  * read data from file fp1 and write on link
349*7c478bd9Sstevel@tonic-gate  *	fp1	-> file descriptor
350*7c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
351*7c478bd9Sstevel@tonic-gate  * returns:
352*7c478bd9Sstevel@tonic-gate  *	FAIL	->failure in link
353*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
354*7c478bd9Sstevel@tonic-gate  */
355*7c478bd9Sstevel@tonic-gate twrdata(fp1, fn)
356*7c478bd9Sstevel@tonic-gate register FILE *fp1;
357*7c478bd9Sstevel@tonic-gate int	fn;
358*7c478bd9Sstevel@tonic-gate {
359*7c478bd9Sstevel@tonic-gate 	register int ret;
360*7c478bd9Sstevel@tonic-gate 	int len;
361*7c478bd9Sstevel@tonic-gate 	unsigned long bytes;
362*7c478bd9Sstevel@tonic-gate 	struct tbuf bufr;
363*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	if (setjmp(Failbuf)) {
366*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "twrdata failed\n", 0);
367*7c478bd9Sstevel@tonic-gate 		return(FAIL);
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 	fstat(fileno(fp1), &statbuf);
370*7c478bd9Sstevel@tonic-gate 	bytes = 0L;
371*7c478bd9Sstevel@tonic-gate 	while ((len = read(fileno(fp1), bufr.t_data, TBUFSIZE)) > 0) {
372*7c478bd9Sstevel@tonic-gate 		bufr.t_nbytes = htonl((long)len);
373*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "twrdata writing %d ...", len);
374*7c478bd9Sstevel@tonic-gate 		bytes += len;
375*7c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
376*7c478bd9Sstevel@tonic-gate 		len += sizeof(long);
377*7c478bd9Sstevel@tonic-gate 		alarm(msgtime);
378*7c478bd9Sstevel@tonic-gate 		ret = (*Write)(fn, (char *)&bufr, (unsigned) len);
379*7c478bd9Sstevel@tonic-gate 		alarm(0);
380*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "ret %d\n", ret);
381*7c478bd9Sstevel@tonic-gate 		if (ret != len)
382*7c478bd9Sstevel@tonic-gate 			return(FAIL);
383*7c478bd9Sstevel@tonic-gate 		if (len != TBUFSIZE+sizeof(long))
384*7c478bd9Sstevel@tonic-gate 			break;
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 	bufr.t_nbytes = 0;
387*7c478bd9Sstevel@tonic-gate 	alarm(msgtime);
388*7c478bd9Sstevel@tonic-gate 	ret = write(fn, (char *)&bufr, sizeof(long));
389*7c478bd9Sstevel@tonic-gate 	alarm(0);
390*7c478bd9Sstevel@tonic-gate 	if (ret != sizeof(long))
391*7c478bd9Sstevel@tonic-gate 		return FAIL;
392*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
393*7c478bd9Sstevel@tonic-gate }
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /*
396*7c478bd9Sstevel@tonic-gate  * read data from link and write into file
397*7c478bd9Sstevel@tonic-gate  *	fp2	-> file descriptor
398*7c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
399*7c478bd9Sstevel@tonic-gate  * returns:
400*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
401*7c478bd9Sstevel@tonic-gate  *	FAIL	-> failure on link
402*7c478bd9Sstevel@tonic-gate  */
403*7c478bd9Sstevel@tonic-gate trddata(fn, fp2)
404*7c478bd9Sstevel@tonic-gate register FILE *fp2;
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	register int len, nread;
407*7c478bd9Sstevel@tonic-gate 	long Nbytes;
408*7c478bd9Sstevel@tonic-gate 	unsigned long bytes = 0L;
409*7c478bd9Sstevel@tonic-gate 	char bufr[TBUFSIZE];
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	for (;;) {
412*7c478bd9Sstevel@tonic-gate 		len = erdblk((char *)&Nbytes, sizeof(Nbytes), fn);
413*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "trddata ret %d\n", len);
414*7c478bd9Sstevel@tonic-gate 		if (len != sizeof(Nbytes))
415*7c478bd9Sstevel@tonic-gate 			return(FAIL);
416*7c478bd9Sstevel@tonic-gate 		Nbytes = ntohl(Nbytes);
417*7c478bd9Sstevel@tonic-gate 		DEBUG(7,"trddata expecting %ld bytes\n", Nbytes);
418*7c478bd9Sstevel@tonic-gate 		nread = Nbytes;
419*7c478bd9Sstevel@tonic-gate 		if (nread == 0)
420*7c478bd9Sstevel@tonic-gate 			break;
421*7c478bd9Sstevel@tonic-gate 		len = erdblk(bufr, nread, fn);
422*7c478bd9Sstevel@tonic-gate 		if (len != Nbytes)
423*7c478bd9Sstevel@tonic-gate 			return(FAIL);
424*7c478bd9Sstevel@tonic-gate 		bytes += len;
425*7c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
426*7c478bd9Sstevel@tonic-gate 		if (write(fileno(fp2), bufr, len) != len)
427*7c478bd9Sstevel@tonic-gate 			return(FAIL);
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
430*7c478bd9Sstevel@tonic-gate }
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate /*
433*7c478bd9Sstevel@tonic-gate  * read message from link
434*7c478bd9Sstevel@tonic-gate  *	str	-> message buffer
435*7c478bd9Sstevel@tonic-gate  *	fn	-> file descriptor
436*7c478bd9Sstevel@tonic-gate  *	i	-> if non-zero, amount to read; o.w., read up to '\0'
437*7c478bd9Sstevel@tonic-gate  * return
438*7c478bd9Sstevel@tonic-gate  *	FAIL	-> read timed out
439*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
440*7c478bd9Sstevel@tonic-gate  *
441*7c478bd9Sstevel@tonic-gate  * 'e' is fatally flawed -- in a byte stream world, rdmsg can pick up
442*7c478bd9Sstevel@tonic-gate  * the cmsglen on a R request.  if this happens, we stash the excess
443*7c478bd9Sstevel@tonic-gate  * where rddata can pick it up.
444*7c478bd9Sstevel@tonic-gate  */
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate etrdmsg(str, fn, i)
447*7c478bd9Sstevel@tonic-gate register char *str;
448*7c478bd9Sstevel@tonic-gate register int i;
449*7c478bd9Sstevel@tonic-gate {
450*7c478bd9Sstevel@tonic-gate 	register int len;
451*7c478bd9Sstevel@tonic-gate 	int nullterm = 0;
452*7c478bd9Sstevel@tonic-gate 	char *null, *argstr;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 	if (i == 0) {
456*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "etrdmsg looking for null terminator\n", 0);
457*7c478bd9Sstevel@tonic-gate 		nullterm++;
458*7c478bd9Sstevel@tonic-gate 		i = EBUFSIZ;
459*7c478bd9Sstevel@tonic-gate 		argstr = str;
460*7c478bd9Sstevel@tonic-gate 	}
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	if(setjmp(Failbuf)) {
463*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "timeout (%d sec)\n", msgtime);
464*7c478bd9Sstevel@tonic-gate 		return(FAIL);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	alarm(msgtime);
468*7c478bd9Sstevel@tonic-gate 	for (;;) {
469*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "etrdmsg want %d ...", i);
470*7c478bd9Sstevel@tonic-gate 		len = (*Read)(fn, str, i);
471*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "got %d\n", len);
472*7c478bd9Sstevel@tonic-gate 		if (len == 0)
473*7c478bd9Sstevel@tonic-gate 			continue;	/* timeout will get this */
474*7c478bd9Sstevel@tonic-gate 		if (len < 0) {
475*7c478bd9Sstevel@tonic-gate 			alarm(0);
476*7c478bd9Sstevel@tonic-gate 			return(FAIL);
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 		str += len;
479*7c478bd9Sstevel@tonic-gate 		i -= len;
480*7c478bd9Sstevel@tonic-gate 		if (nullterm) {
481*7c478bd9Sstevel@tonic-gate 			/* no way can a msg be as long as EBUFSIZ-1 ... */
482*7c478bd9Sstevel@tonic-gate 			*str = 0;
483*7c478bd9Sstevel@tonic-gate 			null = strchr(argstr, '\0');
484*7c478bd9Sstevel@tonic-gate 			if (null != str) {
485*7c478bd9Sstevel@tonic-gate 				null++;	/* start of stash */
486*7c478bd9Sstevel@tonic-gate 				memcpy(Erdstash + Erdlen, null, str - null);
487*7c478bd9Sstevel@tonic-gate 				Erdlen += str - null;
488*7c478bd9Sstevel@tonic-gate 				break;
489*7c478bd9Sstevel@tonic-gate 			} else
490*7c478bd9Sstevel@tonic-gate 				argstr = str;
491*7c478bd9Sstevel@tonic-gate 		} else {
492*7c478bd9Sstevel@tonic-gate 			if (i == 0)
493*7c478bd9Sstevel@tonic-gate 				break;
494*7c478bd9Sstevel@tonic-gate 		}
495*7c478bd9Sstevel@tonic-gate 	}
496*7c478bd9Sstevel@tonic-gate 	alarm(0);
497*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
498*7c478bd9Sstevel@tonic-gate }
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate /*
501*7c478bd9Sstevel@tonic-gate  * write message across link
502*7c478bd9Sstevel@tonic-gate  *	type	-> message type
503*7c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
504*7c478bd9Sstevel@tonic-gate  *	fn	-> link file descriptor
505*7c478bd9Sstevel@tonic-gate  *	len	-> if non-zero, amount to write;
506*7c478bd9Sstevel@tonic-gate 		   o.w., write up to '\0' (inclusive)
507*7c478bd9Sstevel@tonic-gate  * return
508*7c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
509*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> write succeeded
510*7c478bd9Sstevel@tonic-gate  */
511*7c478bd9Sstevel@tonic-gate etwrmsg(type, str, fn, len)
512*7c478bd9Sstevel@tonic-gate char type;
513*7c478bd9Sstevel@tonic-gate register char *str;
514*7c478bd9Sstevel@tonic-gate int fn, len;
515*7c478bd9Sstevel@tonic-gate {
516*7c478bd9Sstevel@tonic-gate 	char bufr[EBUFSIZ], *endstr;
517*7c478bd9Sstevel@tonic-gate 	int ret;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	bufr[0] = type;
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 	/* point endstr to last character to be sent */
522*7c478bd9Sstevel@tonic-gate 	if ((endstr = strchr(str, '\n')) != 0)
523*7c478bd9Sstevel@tonic-gate 		*endstr = 0;
524*7c478bd9Sstevel@tonic-gate 	else
525*7c478bd9Sstevel@tonic-gate 		endstr = str + strlen(str);
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	memcpy(bufr+1, str, (endstr - str) + 1);	/* include '\0' */
528*7c478bd9Sstevel@tonic-gate 	if (len == 0)
529*7c478bd9Sstevel@tonic-gate 		len = (endstr - str) + 2;	/* include bufr[0] and '\0' */
530*7c478bd9Sstevel@tonic-gate 	else
531*7c478bd9Sstevel@tonic-gate 		bufr[len-1] = 0;		/* 't' needs this terminator */
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	if (setjmp(Failbuf)) {
535*7c478bd9Sstevel@tonic-gate 		DEBUG(7, "etwrmsg write failed\n", 0);
536*7c478bd9Sstevel@tonic-gate 		return(FAIL);
537*7c478bd9Sstevel@tonic-gate 	}
538*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "etwrmsg want %d ... ", len);
539*7c478bd9Sstevel@tonic-gate 	alarm(msgtime);
540*7c478bd9Sstevel@tonic-gate 	ret = (*Write)(fn, bufr, (unsigned) len);
541*7c478bd9Sstevel@tonic-gate 	alarm(0);
542*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "sent %d\n", ret);
543*7c478bd9Sstevel@tonic-gate 	if (ret != len)
544*7c478bd9Sstevel@tonic-gate 		return(FAIL);
545*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
546*7c478bd9Sstevel@tonic-gate }
547*7c478bd9Sstevel@tonic-gate #endif	/* E_PROTOCOL */
548