17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*70d17f24Sas158974 * Common Development and Distribution License (the "License").
6*70d17f24Sas158974 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*70d17f24Sas158974 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include "uucp.h"
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #ifdef E_PROTOCOL
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #ifndef MIN
377c478bd9Sstevel@tonic-gate #define MIN(a,b) (((a)<(b))?(a):(b))
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #if defined(BSD4_2) || defined (ATTSVR4)
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #endif /* BSD4_2 || ATTSVR4 */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #define EBUFSIZ 1024
457c478bd9Sstevel@tonic-gate #define EMESGLEN 20
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define TBUFSIZE 1024
487c478bd9Sstevel@tonic-gate #define TPACKSIZE 512
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate extern long lseek(); /* Find offset into the file. */
517c478bd9Sstevel@tonic-gate static jmp_buf Failbuf;
527c478bd9Sstevel@tonic-gate extern int erdblk();
537c478bd9Sstevel@tonic-gate extern unsigned msgtime;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static char Erdstash[EBUFSIZ];
567c478bd9Sstevel@tonic-gate static int Erdlen;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * error-free channel protocol
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate /* ARGSUSED */
627c478bd9Sstevel@tonic-gate static void
ealarm(sig)637c478bd9Sstevel@tonic-gate ealarm(sig)
647c478bd9Sstevel@tonic-gate int sig;
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate longjmp(Failbuf, 1);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate static void (*esig)();
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * turn on protocol timer
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate int
eturnon()747c478bd9Sstevel@tonic-gate eturnon()
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate esig=signal(SIGALRM, ealarm);
777c478bd9Sstevel@tonic-gate return(0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate int
eturnoff()817c478bd9Sstevel@tonic-gate eturnoff()
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate signal(SIGALRM, esig);
847c478bd9Sstevel@tonic-gate return(0);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * write message across link
897c478bd9Sstevel@tonic-gate * type -> message type
907c478bd9Sstevel@tonic-gate * str -> message body (ascii string)
917c478bd9Sstevel@tonic-gate * fn -> link file descriptor
927c478bd9Sstevel@tonic-gate * return
937c478bd9Sstevel@tonic-gate * FAIL -> write failed
947c478bd9Sstevel@tonic-gate * SUCCESS -> write succeeded
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate int
ewrmsg(type,str,fn)977c478bd9Sstevel@tonic-gate ewrmsg(type, str, fn)
98462be471Sceastha char *str;
997c478bd9Sstevel@tonic-gate int fn;
1007c478bd9Sstevel@tonic-gate char type;
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate return(etwrmsg(type, str, fn, 0));
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * read message from link
1077c478bd9Sstevel@tonic-gate * str -> message buffer
1087c478bd9Sstevel@tonic-gate * fn -> file descriptor
1097c478bd9Sstevel@tonic-gate * return
1107c478bd9Sstevel@tonic-gate * FAIL -> read timed out
1117c478bd9Sstevel@tonic-gate * SUCCESS -> ok message in str
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate int
erdmsg(str,fn)1147c478bd9Sstevel@tonic-gate erdmsg(str, fn)
115462be471Sceastha char *str;
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate return(etrdmsg(str, fn, 0));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * read data from file fp1 and write
1227c478bd9Sstevel@tonic-gate * on link
1237c478bd9Sstevel@tonic-gate * fp1 -> file descriptor
1247c478bd9Sstevel@tonic-gate * fn -> link descriptor
1257c478bd9Sstevel@tonic-gate * returns:
1267c478bd9Sstevel@tonic-gate * FAIL ->failure in link
1277c478bd9Sstevel@tonic-gate * SUCCESS -> ok
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate int
ewrdata(fp1,fn)1307c478bd9Sstevel@tonic-gate ewrdata(fp1, fn)
131462be471Sceastha FILE *fp1;
1327c478bd9Sstevel@tonic-gate int fn;
1337c478bd9Sstevel@tonic-gate {
134462be471Sceastha int ret;
1357c478bd9Sstevel@tonic-gate int fd1;
1367c478bd9Sstevel@tonic-gate int len;
1377c478bd9Sstevel@tonic-gate unsigned long bytes;
1387c478bd9Sstevel@tonic-gate char bufr[EBUFSIZ];
1397c478bd9Sstevel@tonic-gate struct stat statbuf;
1407c478bd9Sstevel@tonic-gate off_t msglen;
1417c478bd9Sstevel@tonic-gate char cmsglen[EMESGLEN];
1427c478bd9Sstevel@tonic-gate off_t startPoint; /* Offset from begining of the file in
1437c478bd9Sstevel@tonic-gate * case we are restarting from a check
1447c478bd9Sstevel@tonic-gate * point.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if (setjmp(Failbuf)) {
1487c478bd9Sstevel@tonic-gate DEBUG(7, "ewrdata failed\n%s", "");
1497c478bd9Sstevel@tonic-gate return(FAIL);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate bytes = 0L;
1527c478bd9Sstevel@tonic-gate fd1 = fileno(fp1);
1537c478bd9Sstevel@tonic-gate fstat(fd1, &statbuf);
1547c478bd9Sstevel@tonic-gate startPoint = lseek(fd1, 0L, 1);
1557c478bd9Sstevel@tonic-gate if (startPoint < 0)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate DEBUG(7, "ewrdata lseek failed. Errno=%d\n", errno);
1587c478bd9Sstevel@tonic-gate return(FAIL);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate msglen = statbuf.st_size - startPoint;
1617c478bd9Sstevel@tonic-gate if (msglen < 0)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate DEBUG(7, "ewrdata: startPoint past end of file.\n%s", "");
1647c478bd9Sstevel@tonic-gate return(FAIL);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate sprintf(cmsglen, "%ld", (long) msglen);
1677c478bd9Sstevel@tonic-gate DEBUG(9, "ewrdata writing %d ...", sizeof(cmsglen));
1687c478bd9Sstevel@tonic-gate alarm(msgtime);
1697c478bd9Sstevel@tonic-gate ret = (*Write)(fn, cmsglen, sizeof(cmsglen));
1707c478bd9Sstevel@tonic-gate alarm(0);
1717c478bd9Sstevel@tonic-gate DEBUG(9, "ret %d\n", ret);
1727c478bd9Sstevel@tonic-gate if (ret != sizeof(cmsglen))
1737c478bd9Sstevel@tonic-gate return(FAIL);
1747c478bd9Sstevel@tonic-gate DEBUG(7, "ewrdata planning to send %ld bytes to remote.\n", msglen);
1757c478bd9Sstevel@tonic-gate while ((len = read( fd1, bufr, EBUFSIZ )) > 0) {
1767c478bd9Sstevel@tonic-gate DEBUG(9, "ewrdata writing %d ...", len);
1777c478bd9Sstevel@tonic-gate alarm(msgtime);
1787c478bd9Sstevel@tonic-gate bytes += len;
1797c478bd9Sstevel@tonic-gate putfilesize(bytes);
1807c478bd9Sstevel@tonic-gate ret = (*Write)(fn, bufr, (unsigned) len);
1817c478bd9Sstevel@tonic-gate alarm(0);
1827c478bd9Sstevel@tonic-gate DEBUG(9, "ewrdata ret %d\n", ret);
1837c478bd9Sstevel@tonic-gate if (ret != len)
1847c478bd9Sstevel@tonic-gate return(FAIL);
1857c478bd9Sstevel@tonic-gate if ((msglen -= len) <= 0)
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate if (len < 0 || (len == 0 && msglen != 0)) return(FAIL);
1897c478bd9Sstevel@tonic-gate return(SUCCESS);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * read data from link and
1947c478bd9Sstevel@tonic-gate * write into file
1957c478bd9Sstevel@tonic-gate * fp2 -> file descriptor
1967c478bd9Sstevel@tonic-gate * fn -> link descriptor
1977c478bd9Sstevel@tonic-gate * returns:
1987c478bd9Sstevel@tonic-gate * SUCCESS -> ok
1997c478bd9Sstevel@tonic-gate * FAIL -> failure on link
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate int
erddata(fn,fp2)2027c478bd9Sstevel@tonic-gate erddata(fn, fp2)
203462be471Sceastha FILE *fp2;
2047c478bd9Sstevel@tonic-gate {
205462be471Sceastha int ret;
2067c478bd9Sstevel@tonic-gate int fd2;
2077c478bd9Sstevel@tonic-gate char bufr[EBUFSIZ];
2087c478bd9Sstevel@tonic-gate int len;
2097c478bd9Sstevel@tonic-gate long msglen, bytes;
2107c478bd9Sstevel@tonic-gate char cmsglen[EMESGLEN], *cptr, *erdptr = Erdstash;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate DEBUG(9, "erddata wants %d\n", sizeof(cmsglen));
2137c478bd9Sstevel@tonic-gate if (Erdlen > 0) {
2147c478bd9Sstevel@tonic-gate DEBUG(9, "%d bytes stashed\n", Erdlen);
2157c478bd9Sstevel@tonic-gate if (Erdlen >= sizeof(cmsglen)) {
2167c478bd9Sstevel@tonic-gate memcpy(cmsglen, erdptr, sizeof(cmsglen));
2177c478bd9Sstevel@tonic-gate Erdlen -= sizeof(cmsglen);
2187c478bd9Sstevel@tonic-gate erdptr += sizeof(cmsglen);
2197c478bd9Sstevel@tonic-gate ret = len = 0;
2207c478bd9Sstevel@tonic-gate } else {
2217c478bd9Sstevel@tonic-gate memcpy(cmsglen, Erdstash, Erdlen);
2227c478bd9Sstevel@tonic-gate cptr = cmsglen + Erdlen;
2237c478bd9Sstevel@tonic-gate len = sizeof(cmsglen) - Erdlen;
2247c478bd9Sstevel@tonic-gate ret = erdblk(cptr, len, fn);
2257c478bd9Sstevel@tonic-gate Erdlen = 0;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate } else {
2287c478bd9Sstevel@tonic-gate len = sizeof(cmsglen);
2297c478bd9Sstevel@tonic-gate ret = erdblk(cmsglen, sizeof(cmsglen), fn);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate if (ret != len)
2327c478bd9Sstevel@tonic-gate return(FAIL);
2337c478bd9Sstevel@tonic-gate ret = SUCCESS;
2347c478bd9Sstevel@tonic-gate sscanf(cmsglen, "%ld", &msglen);
2357c478bd9Sstevel@tonic-gate if ( ((msglen-1)/512 +1) > Ulimit )
2367c478bd9Sstevel@tonic-gate ret = EFBIG;
2377c478bd9Sstevel@tonic-gate DEBUG(7, "erddata file is %ld bytes\n", msglen);
2387c478bd9Sstevel@tonic-gate fd2 = fileno( fp2 );
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if (Erdlen > 0) {
2417c478bd9Sstevel@tonic-gate DEBUG(9, "%d bytes stashed\n", Erdlen);
2427c478bd9Sstevel@tonic-gate if (write(fileno(fp2), erdptr, Erdlen) != Erdlen)
2437c478bd9Sstevel@tonic-gate return(FAIL);
2447c478bd9Sstevel@tonic-gate msglen -= Erdlen;
2457c478bd9Sstevel@tonic-gate Erdlen = 0;
2467c478bd9Sstevel@tonic-gate DEBUG(7, "erddata remainder is %ld bytes\n", msglen);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate for (;;) {
2507c478bd9Sstevel@tonic-gate len = erdblk(bufr, (int) MIN(msglen, EBUFSIZ), fn);
2517c478bd9Sstevel@tonic-gate DEBUG(9, "erdblk ret %d\n", len);
2527c478bd9Sstevel@tonic-gate if (len < 0) {
2537c478bd9Sstevel@tonic-gate DEBUG(7, "erdblk failed\n%s", "");
2547c478bd9Sstevel@tonic-gate return(FAIL);
2557c478bd9Sstevel@tonic-gate }
256*70d17f24Sas158974
257*70d17f24Sas158974 /*
258*70d17f24Sas158974 * handle the case for remote socket close.
259*70d17f24Sas158974 */
260*70d17f24Sas158974 if (len == 0) {
261*70d17f24Sas158974 ret = errno;
262*70d17f24Sas158974 DEBUG(7, "erddata: remote socket closed, errno %d\n",
263*70d17f24Sas158974 ret);
264*70d17f24Sas158974 break;
265*70d17f24Sas158974 }
2667c478bd9Sstevel@tonic-gate bytes += len;
2677c478bd9Sstevel@tonic-gate putfilesize(bytes);
2687c478bd9Sstevel@tonic-gate if ((msglen -= len) < 0) {
2697c478bd9Sstevel@tonic-gate DEBUG(7, "erdblk read too much\n%s", "");
2707c478bd9Sstevel@tonic-gate return(FAIL);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate /* this write is to file -- use write(2), not (*Write) */
2737c478bd9Sstevel@tonic-gate if ( ret == SUCCESS && write( fd2, bufr, len ) != len ) {
2747c478bd9Sstevel@tonic-gate ret = errno;
2757c478bd9Sstevel@tonic-gate DEBUG(7, "erddata: write to file failed, errno %d\n", ret);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate if (msglen == 0)
2787c478bd9Sstevel@tonic-gate break;
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate return(ret);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate * read block from link
2857c478bd9Sstevel@tonic-gate * reads are timed
2867c478bd9Sstevel@tonic-gate * blk -> address of buffer
2877c478bd9Sstevel@tonic-gate * len -> size to read
2887c478bd9Sstevel@tonic-gate * fn -> link descriptor
2897c478bd9Sstevel@tonic-gate * returns:
2907c478bd9Sstevel@tonic-gate * FAIL -> link error timeout on link
2917c478bd9Sstevel@tonic-gate * i -> # of bytes read (must not be 0)
2927c478bd9Sstevel@tonic-gate */
2937c478bd9Sstevel@tonic-gate int
erdblk(blk,len,fn)2947c478bd9Sstevel@tonic-gate erdblk(blk, len, fn)
295462be471Sceastha char *blk;
2967c478bd9Sstevel@tonic-gate {
297462be471Sceastha int i, ret;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate if(setjmp(Failbuf)) {
3007c478bd9Sstevel@tonic-gate DEBUG(7, "timeout (%d sec)\n", msgtime);
3017c478bd9Sstevel@tonic-gate return(FAIL);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate alarm(msgtime);
3057c478bd9Sstevel@tonic-gate for (i = 0; i < len; i += ret) {
3067c478bd9Sstevel@tonic-gate DEBUG(9, "erdblk ask %d ", len - i);
3077c478bd9Sstevel@tonic-gate if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
3087c478bd9Sstevel@tonic-gate alarm(0);
3097c478bd9Sstevel@tonic-gate DEBUG(7, "erdblk read failed\n%s", "");
3107c478bd9Sstevel@tonic-gate return(FAIL);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate DEBUG(9, "erdblk got %d\n", ret);
3137c478bd9Sstevel@tonic-gate if (ret == 0)
3147c478bd9Sstevel@tonic-gate break;
3157c478bd9Sstevel@tonic-gate blk += ret;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate alarm(0);
3187c478bd9Sstevel@tonic-gate return(i);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate struct tbuf {
3227c478bd9Sstevel@tonic-gate long t_nbytes;
3237c478bd9Sstevel@tonic-gate char t_data[TBUFSIZE];
3247c478bd9Sstevel@tonic-gate };
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * read message from link
3287c478bd9Sstevel@tonic-gate * str -> message buffer
3297c478bd9Sstevel@tonic-gate * fn -> file descriptor
3307c478bd9Sstevel@tonic-gate * return
3317c478bd9Sstevel@tonic-gate * FAIL -> read timed out
3327c478bd9Sstevel@tonic-gate * SUCCESS -> ok message in str
3337c478bd9Sstevel@tonic-gate */
334462be471Sceastha int
trdmsg(str,fn)3357c478bd9Sstevel@tonic-gate trdmsg(str, fn)
3367c478bd9Sstevel@tonic-gate char *str;
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate return(etrdmsg(str, fn, TPACKSIZE));
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * write message across link
3437c478bd9Sstevel@tonic-gate * type -> message type
3447c478bd9Sstevel@tonic-gate * str -> message body (ascii string)
3457c478bd9Sstevel@tonic-gate * fn -> link file descriptor
3467c478bd9Sstevel@tonic-gate * return
3477c478bd9Sstevel@tonic-gate * FAIL -> write failed
3487c478bd9Sstevel@tonic-gate * SUCCESS -> write succeeded
3497c478bd9Sstevel@tonic-gate */
350462be471Sceastha int
twrmsg(type,str,fn)3517c478bd9Sstevel@tonic-gate twrmsg(type, str, fn)
3527c478bd9Sstevel@tonic-gate char type;
3537c478bd9Sstevel@tonic-gate char *str;
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate return(etwrmsg(type, str, fn, TPACKSIZE));
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate * read data from file fp1 and write on link
3607c478bd9Sstevel@tonic-gate * fp1 -> file descriptor
3617c478bd9Sstevel@tonic-gate * fn -> link descriptor
3627c478bd9Sstevel@tonic-gate * returns:
3637c478bd9Sstevel@tonic-gate * FAIL ->failure in link
3647c478bd9Sstevel@tonic-gate * SUCCESS -> ok
3657c478bd9Sstevel@tonic-gate */
366462be471Sceastha int
twrdata(fp1,fn)3677c478bd9Sstevel@tonic-gate twrdata(fp1, fn)
368462be471Sceastha FILE *fp1;
3697c478bd9Sstevel@tonic-gate int fn;
3707c478bd9Sstevel@tonic-gate {
371462be471Sceastha int ret;
3727c478bd9Sstevel@tonic-gate int len;
3737c478bd9Sstevel@tonic-gate unsigned long bytes;
3747c478bd9Sstevel@tonic-gate struct tbuf bufr;
3757c478bd9Sstevel@tonic-gate struct stat statbuf;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate if (setjmp(Failbuf)) {
3787c478bd9Sstevel@tonic-gate DEBUG(7, "twrdata failed\n", 0);
3797c478bd9Sstevel@tonic-gate return(FAIL);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate fstat(fileno(fp1), &statbuf);
3827c478bd9Sstevel@tonic-gate bytes = 0L;
3837c478bd9Sstevel@tonic-gate while ((len = read(fileno(fp1), bufr.t_data, TBUFSIZE)) > 0) {
3847c478bd9Sstevel@tonic-gate bufr.t_nbytes = htonl((long)len);
3857c478bd9Sstevel@tonic-gate DEBUG(7, "twrdata writing %d ...", len);
3867c478bd9Sstevel@tonic-gate bytes += len;
3877c478bd9Sstevel@tonic-gate putfilesize(bytes);
3887c478bd9Sstevel@tonic-gate len += sizeof(long);
3897c478bd9Sstevel@tonic-gate alarm(msgtime);
3907c478bd9Sstevel@tonic-gate ret = (*Write)(fn, (char *)&bufr, (unsigned) len);
3917c478bd9Sstevel@tonic-gate alarm(0);
3927c478bd9Sstevel@tonic-gate DEBUG(7, "ret %d\n", ret);
3937c478bd9Sstevel@tonic-gate if (ret != len)
3947c478bd9Sstevel@tonic-gate return(FAIL);
3957c478bd9Sstevel@tonic-gate if (len != TBUFSIZE+sizeof(long))
3967c478bd9Sstevel@tonic-gate break;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate bufr.t_nbytes = 0;
3997c478bd9Sstevel@tonic-gate alarm(msgtime);
4007c478bd9Sstevel@tonic-gate ret = write(fn, (char *)&bufr, sizeof(long));
4017c478bd9Sstevel@tonic-gate alarm(0);
4027c478bd9Sstevel@tonic-gate if (ret != sizeof(long))
4037c478bd9Sstevel@tonic-gate return FAIL;
4047c478bd9Sstevel@tonic-gate return(SUCCESS);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * read data from link and write into file
4097c478bd9Sstevel@tonic-gate * fp2 -> file descriptor
4107c478bd9Sstevel@tonic-gate * fn -> link descriptor
4117c478bd9Sstevel@tonic-gate * returns:
4127c478bd9Sstevel@tonic-gate * SUCCESS -> ok
4137c478bd9Sstevel@tonic-gate * FAIL -> failure on link
4147c478bd9Sstevel@tonic-gate */
415462be471Sceastha int
trddata(fn,fp2)4167c478bd9Sstevel@tonic-gate trddata(fn, fp2)
417462be471Sceastha FILE *fp2;
4187c478bd9Sstevel@tonic-gate {
419462be471Sceastha int len, nread;
4207c478bd9Sstevel@tonic-gate long Nbytes;
4217c478bd9Sstevel@tonic-gate unsigned long bytes = 0L;
4227c478bd9Sstevel@tonic-gate char bufr[TBUFSIZE];
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate for (;;) {
4257c478bd9Sstevel@tonic-gate len = erdblk((char *)&Nbytes, sizeof(Nbytes), fn);
4267c478bd9Sstevel@tonic-gate DEBUG(7, "trddata ret %d\n", len);
4277c478bd9Sstevel@tonic-gate if (len != sizeof(Nbytes))
4287c478bd9Sstevel@tonic-gate return(FAIL);
4297c478bd9Sstevel@tonic-gate Nbytes = ntohl(Nbytes);
4307c478bd9Sstevel@tonic-gate DEBUG(7,"trddata expecting %ld bytes\n", Nbytes);
4317c478bd9Sstevel@tonic-gate nread = Nbytes;
4327c478bd9Sstevel@tonic-gate if (nread == 0)
4337c478bd9Sstevel@tonic-gate break;
4347c478bd9Sstevel@tonic-gate len = erdblk(bufr, nread, fn);
4357c478bd9Sstevel@tonic-gate if (len != Nbytes)
4367c478bd9Sstevel@tonic-gate return(FAIL);
4377c478bd9Sstevel@tonic-gate bytes += len;
4387c478bd9Sstevel@tonic-gate putfilesize(bytes);
4397c478bd9Sstevel@tonic-gate if (write(fileno(fp2), bufr, len) != len)
4407c478bd9Sstevel@tonic-gate return(FAIL);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate return(SUCCESS);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate * read message from link
4477c478bd9Sstevel@tonic-gate * str -> message buffer
4487c478bd9Sstevel@tonic-gate * fn -> file descriptor
4497c478bd9Sstevel@tonic-gate * i -> if non-zero, amount to read; o.w., read up to '\0'
4507c478bd9Sstevel@tonic-gate * return
4517c478bd9Sstevel@tonic-gate * FAIL -> read timed out
4527c478bd9Sstevel@tonic-gate * SUCCESS -> ok message in str
4537c478bd9Sstevel@tonic-gate *
4547c478bd9Sstevel@tonic-gate * 'e' is fatally flawed -- in a byte stream world, rdmsg can pick up
4557c478bd9Sstevel@tonic-gate * the cmsglen on a R request. if this happens, we stash the excess
4567c478bd9Sstevel@tonic-gate * where rddata can pick it up.
4577c478bd9Sstevel@tonic-gate */
4587c478bd9Sstevel@tonic-gate
459462be471Sceastha int
etrdmsg(str,fn,i)4607c478bd9Sstevel@tonic-gate etrdmsg(str, fn, i)
461462be471Sceastha char *str;
462462be471Sceastha int i;
4637c478bd9Sstevel@tonic-gate {
464462be471Sceastha int len;
4657c478bd9Sstevel@tonic-gate int nullterm = 0;
4667c478bd9Sstevel@tonic-gate char *null, *argstr;
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate if (i == 0) {
4707c478bd9Sstevel@tonic-gate DEBUG(9, "etrdmsg looking for null terminator\n", 0);
4717c478bd9Sstevel@tonic-gate nullterm++;
4727c478bd9Sstevel@tonic-gate i = EBUFSIZ;
4737c478bd9Sstevel@tonic-gate argstr = str;
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate if(setjmp(Failbuf)) {
4777c478bd9Sstevel@tonic-gate DEBUG(7, "timeout (%d sec)\n", msgtime);
4787c478bd9Sstevel@tonic-gate return(FAIL);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate alarm(msgtime);
4827c478bd9Sstevel@tonic-gate for (;;) {
4837c478bd9Sstevel@tonic-gate DEBUG(9, "etrdmsg want %d ...", i);
4847c478bd9Sstevel@tonic-gate len = (*Read)(fn, str, i);
4857c478bd9Sstevel@tonic-gate DEBUG(9, "got %d\n", len);
4867c478bd9Sstevel@tonic-gate if (len == 0)
4877c478bd9Sstevel@tonic-gate continue; /* timeout will get this */
4887c478bd9Sstevel@tonic-gate if (len < 0) {
4897c478bd9Sstevel@tonic-gate alarm(0);
4907c478bd9Sstevel@tonic-gate return(FAIL);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate str += len;
4937c478bd9Sstevel@tonic-gate i -= len;
4947c478bd9Sstevel@tonic-gate if (nullterm) {
4957c478bd9Sstevel@tonic-gate /* no way can a msg be as long as EBUFSIZ-1 ... */
4967c478bd9Sstevel@tonic-gate *str = 0;
4977c478bd9Sstevel@tonic-gate null = strchr(argstr, '\0');
4987c478bd9Sstevel@tonic-gate if (null != str) {
4997c478bd9Sstevel@tonic-gate null++; /* start of stash */
5007c478bd9Sstevel@tonic-gate memcpy(Erdstash + Erdlen, null, str - null);
5017c478bd9Sstevel@tonic-gate Erdlen += str - null;
5027c478bd9Sstevel@tonic-gate break;
5037c478bd9Sstevel@tonic-gate } else
5047c478bd9Sstevel@tonic-gate argstr = str;
5057c478bd9Sstevel@tonic-gate } else {
5067c478bd9Sstevel@tonic-gate if (i == 0)
5077c478bd9Sstevel@tonic-gate break;
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate alarm(0);
5117c478bd9Sstevel@tonic-gate return(SUCCESS);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate * write message across link
5167c478bd9Sstevel@tonic-gate * type -> message type
5177c478bd9Sstevel@tonic-gate * str -> message body (ascii string)
5187c478bd9Sstevel@tonic-gate * fn -> link file descriptor
5197c478bd9Sstevel@tonic-gate * len -> if non-zero, amount to write;
5207c478bd9Sstevel@tonic-gate o.w., write up to '\0' (inclusive)
5217c478bd9Sstevel@tonic-gate * return
5227c478bd9Sstevel@tonic-gate * FAIL -> write failed
5237c478bd9Sstevel@tonic-gate * SUCCESS -> write succeeded
5247c478bd9Sstevel@tonic-gate */
525462be471Sceastha int
etwrmsg(type,str,fn,len)5267c478bd9Sstevel@tonic-gate etwrmsg(type, str, fn, len)
5277c478bd9Sstevel@tonic-gate char type;
528462be471Sceastha char *str;
5297c478bd9Sstevel@tonic-gate int fn, len;
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate char bufr[EBUFSIZ], *endstr;
5327c478bd9Sstevel@tonic-gate int ret;
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate bufr[0] = type;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /* point endstr to last character to be sent */
5377c478bd9Sstevel@tonic-gate if ((endstr = strchr(str, '\n')) != 0)
5387c478bd9Sstevel@tonic-gate *endstr = 0;
5397c478bd9Sstevel@tonic-gate else
5407c478bd9Sstevel@tonic-gate endstr = str + strlen(str);
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate memcpy(bufr+1, str, (endstr - str) + 1); /* include '\0' */
5437c478bd9Sstevel@tonic-gate if (len == 0)
5447c478bd9Sstevel@tonic-gate len = (endstr - str) + 2; /* include bufr[0] and '\0' */
5457c478bd9Sstevel@tonic-gate else
5467c478bd9Sstevel@tonic-gate bufr[len-1] = 0; /* 't' needs this terminator */
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate if (setjmp(Failbuf)) {
5507c478bd9Sstevel@tonic-gate DEBUG(7, "etwrmsg write failed\n", 0);
5517c478bd9Sstevel@tonic-gate return(FAIL);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate DEBUG(9, "etwrmsg want %d ... ", len);
5547c478bd9Sstevel@tonic-gate alarm(msgtime);
5557c478bd9Sstevel@tonic-gate ret = (*Write)(fn, bufr, (unsigned) len);
5567c478bd9Sstevel@tonic-gate alarm(0);
5577c478bd9Sstevel@tonic-gate DEBUG(9, "sent %d\n", ret);
5587c478bd9Sstevel@tonic-gate if (ret != len)
5597c478bd9Sstevel@tonic-gate return(FAIL);
5607c478bd9Sstevel@tonic-gate return(SUCCESS);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate #endif /* E_PROTOCOL */
563