xref: /illumos-gate/usr/src/cmd/bnu/interface.c (revision 3b296559)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
2309b0d01cSGary Mills  * Copyright 2014 Gary Mills
24ace1a5f1Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
3109b0d01cSGary Mills /*
3209b0d01cSGary Mills  *	interface(label)
3309b0d01cSGary Mills  *	provide alternate definitions for the I/O functions through global
3409b0d01cSGary Mills  *	interfaces.
3509b0d01cSGary Mills  */
367c478bd9Sstevel@tonic-gate #include	"uucp.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifdef TLI
397c478bd9Sstevel@tonic-gate #include	<tiuser.h>
407c478bd9Sstevel@tonic-gate char *t_alloc();
417c478bd9Sstevel@tonic-gate int t_bind(), t_close(), t_connect(), t_free(), t_look(), t_open(), t_rcvdis();
427c478bd9Sstevel@tonic-gate int t_getinfo(), t_getstate(), t_look(), t_rcv(), t_snd(), t_sync(), t_unbind();
437c478bd9Sstevel@tonic-gate #endif /*  TLI  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef DATAKIT
467c478bd9Sstevel@tonic-gate #include	"dk.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static int	dksetup();
497c478bd9Sstevel@tonic-gate static int	dkteardown();
507c478bd9Sstevel@tonic-gate #endif	/* DATAKIT */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate EXTERN void	sethup();
537c478bd9Sstevel@tonic-gate EXTERN int	restline();
547c478bd9Sstevel@tonic-gate #if defined(__STDC__)
557c478bd9Sstevel@tonic-gate extern int	ioctl(int, int, ...);
567c478bd9Sstevel@tonic-gate #else
577c478bd9Sstevel@tonic-gate extern int	ioctl();
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate static int	usetup(), uteardown();
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate GLOBAL ssize_t	(*Read)() = read,
627c478bd9Sstevel@tonic-gate 	(*Write)() = write;
637c478bd9Sstevel@tonic-gate #if defined(__STDC__)
6409b0d01cSGary Mills GLOBAL int	(*Ioctl)(int, int, ...) = ioctl,
657c478bd9Sstevel@tonic-gate #else
667c478bd9Sstevel@tonic-gate GLOBAL int	(*Ioctl)() = ioctl,
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 	(*Setup)() = usetup,
697c478bd9Sstevel@tonic-gate 	(*Teardown)() = uteardown;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef TLI
727c478bd9Sstevel@tonic-gate EXTERN void tfaillog(), show_tlook();
737c478bd9Sstevel@tonic-gate static ssize_t	tread(), twrite();	/* TLI i/o */
747c478bd9Sstevel@tonic-gate #ifdef __STDC__
757c478bd9Sstevel@tonic-gate static int	tioctl(int, int, ...),
767c478bd9Sstevel@tonic-gate #else
777c478bd9Sstevel@tonic-gate static int	tioctl(),		/* TLI i/o control */
787c478bd9Sstevel@tonic-gate #endif
7909b0d01cSGary Mills 		tsetup(),	/* TLI setup without streams module */
8009b0d01cSGary Mills 		tssetup(),	/* TLI setup with streams module */
8109b0d01cSGary Mills 		tteardown();	/* TLI teardown, works with either setup */
827c478bd9Sstevel@tonic-gate #endif /*  TLI  */
8309b0d01cSGary Mills /*
8409b0d01cSGary Mills  *	The IN_label in Interface[] imply different caller routines:
8509b0d01cSGary Mills  *	e.g. tlicall().
8609b0d01cSGary Mills  *	If so, the names here and the names in callers.c must match.
8709b0d01cSGary Mills  */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static
9009b0d01cSGary Mills struct Interface {
917c478bd9Sstevel@tonic-gate 	char	*IN_label;		/* interface name */
927c478bd9Sstevel@tonic-gate 	ssize_t	(*IN_read)();		/* read function */
937c478bd9Sstevel@tonic-gate 	ssize_t	(*IN_write)();		/* write function */
947c478bd9Sstevel@tonic-gate #ifdef __STDC__
9509b0d01cSGary Mills 	int	(*IN_ioctl)(int, int, ...);
967c478bd9Sstevel@tonic-gate #else
977c478bd9Sstevel@tonic-gate 	int	(*IN_ioctl)();		/* ioctl function */
987c478bd9Sstevel@tonic-gate #endif
9909b0d01cSGary Mills 	int	(*IN_setup)();		/* setup function, called before */
10009b0d01cSGary Mills 					/* first i/o operation */
10109b0d01cSGary Mills 	int	(*IN_teardown)();	/* teardown function, called after */
10209b0d01cSGary Mills 					/* last i/o operation */
1037c478bd9Sstevel@tonic-gate } Interface[] = {
1047c478bd9Sstevel@tonic-gate 			/* vanilla UNIX */
1057c478bd9Sstevel@tonic-gate 		{ "UNIX", read, write, ioctl, usetup, uteardown },
1067c478bd9Sstevel@tonic-gate #ifdef TCP
1077c478bd9Sstevel@tonic-gate 			/* TCP over sockets or UNET */
1087c478bd9Sstevel@tonic-gate 		{ "TCP", read, write, ioctl, usetup, uteardown },
1097c478bd9Sstevel@tonic-gate #endif /* TCP */
1107c478bd9Sstevel@tonic-gate #ifdef SYTEK
1117c478bd9Sstevel@tonic-gate 			/* Sytek network */
1127c478bd9Sstevel@tonic-gate 		{ "Sytek", read, write, ioctl, usetup, uteardown },
1137c478bd9Sstevel@tonic-gate #endif /* Sytek network */
1147c478bd9Sstevel@tonic-gate #ifdef DIAL801
1157c478bd9Sstevel@tonic-gate 			/* 801 auto dialers */
1167c478bd9Sstevel@tonic-gate 		{ "801", read, write, ioctl, usetup, uteardown },
1177c478bd9Sstevel@tonic-gate #endif /* DIAL801 */
1187c478bd9Sstevel@tonic-gate #ifdef DIAL801
1197c478bd9Sstevel@tonic-gate 			/* 212 auto dialers */
1207c478bd9Sstevel@tonic-gate 		{ "212", read, write, ioctl, usetup, uteardown },
1217c478bd9Sstevel@tonic-gate #endif /* DIAL801 */
1227c478bd9Sstevel@tonic-gate #ifdef TLI
1237c478bd9Sstevel@tonic-gate 			/* AT&T Transport Interface Library WITHOUT streams */
1247c478bd9Sstevel@tonic-gate 		{ "TLI", tread, twrite, tioctl, tsetup, tteardown },
1257c478bd9Sstevel@tonic-gate #ifdef TLIS
1267c478bd9Sstevel@tonic-gate 			/* AT&T Transport Interface Library WITH streams */
1277c478bd9Sstevel@tonic-gate 		{ "TLIS", read, write, tioctl, tssetup, uteardown },
1287c478bd9Sstevel@tonic-gate #endif /*  TLIS  */
1297c478bd9Sstevel@tonic-gate #endif /*  TLI  */
1307c478bd9Sstevel@tonic-gate #ifdef DATAKIT
1317c478bd9Sstevel@tonic-gate 		{ "DK", read, write, ioctl, dksetup, dkteardown },
1327c478bd9Sstevel@tonic-gate #endif /* DATAKIT */
1337c478bd9Sstevel@tonic-gate #ifdef UNET
1347c478bd9Sstevel@tonic-gate 		{ "Unetserver", read, write, ioctl, usetup, uteardown },
13509b0d01cSGary Mills #endif
1367c478bd9Sstevel@tonic-gate 		{ 0, 0, 0, 0, 0, 0 }
1377c478bd9Sstevel@tonic-gate 	};
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate GLOBAL int
interface(label)1417c478bd9Sstevel@tonic-gate interface(label)
1427c478bd9Sstevel@tonic-gate char	*label;
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	register int	i;
1457c478bd9Sstevel@tonic-gate 
14609b0d01cSGary Mills 	for (i = 0;  Interface[i].IN_label;  ++i) {
14709b0d01cSGary Mills 		if (0 == strcmp(Interface[i].IN_label, label)) {
1487c478bd9Sstevel@tonic-gate 			Read = Interface[i].IN_read;
1497c478bd9Sstevel@tonic-gate 			Write = Interface[i].IN_write;
1507c478bd9Sstevel@tonic-gate 			Ioctl = Interface[i].IN_ioctl;
1517c478bd9Sstevel@tonic-gate 			Setup = Interface[i].IN_setup;
1527c478bd9Sstevel@tonic-gate 			Teardown = Interface[i].IN_teardown;
1537c478bd9Sstevel@tonic-gate 			DEBUG(5, "set interface %s\n", label);
15409b0d01cSGary Mills 			return (0);
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
15709b0d01cSGary Mills 	return (FAIL);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  *	usetup - vanilla unix setup routine
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate static int
usetup(int role,int * fdreadp,int * fdwritep)164*3b296559SToomas Soome usetup(int role, int *fdreadp, int *fdwritep)
1657c478bd9Sstevel@tonic-gate {
16609b0d01cSGary Mills 	if (role == SLAVE)
1677c478bd9Sstevel@tonic-gate 	{
1687c478bd9Sstevel@tonic-gate 		*fdreadp = 0;
1697c478bd9Sstevel@tonic-gate 		*fdwritep = 1;
1707c478bd9Sstevel@tonic-gate 		/* 2 has been re-opened to RMTDEBUG in main() */
1717c478bd9Sstevel@tonic-gate 	}
17209b0d01cSGary Mills 	return (SUCCESS);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  *	uteardown - vanilla unix teardown routine
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static int
uteardown(int role,int fdread,int fdwrite)179*3b296559SToomas Soome uteardown(int role, int fdread, int fdwrite)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int ret;
1827c478bd9Sstevel@tonic-gate 	char *ttyn;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (role == SLAVE) {
1857c478bd9Sstevel@tonic-gate 		ret = restline();
1867c478bd9Sstevel@tonic-gate 		DEBUG(4, "ret restline - %d\n", ret);
1877c478bd9Sstevel@tonic-gate 		if (fdread != -1)
1887c478bd9Sstevel@tonic-gate 			sethup(fdread);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	if (fdread != -1) {
1917c478bd9Sstevel@tonic-gate 		ttyn = ttyname(fdread);
1927c478bd9Sstevel@tonic-gate 		if (ttyn != NULL && Dev_mode != 0)
1937c478bd9Sstevel@tonic-gate 			chmod(ttyn, Dev_mode);	/* can fail, but who cares? */
1947c478bd9Sstevel@tonic-gate 		(void) close(fdread);
1957c478bd9Sstevel@tonic-gate 		(void) close(fdwrite);
1967c478bd9Sstevel@tonic-gate 	}
19709b0d01cSGary Mills 	return (SUCCESS);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #ifdef DATAKIT
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  *	dksetup - DATAKIT setup routine
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * Put line in block mode.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate static int
dksetup(role,fdreadp,fdwritep)20809b0d01cSGary Mills dksetup(role, fdreadp, fdwritep)
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate int	role;
21109b0d01cSGary Mills int 	*fdreadp;
21209b0d01cSGary Mills int 	*fdwritep;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	static short dkrmode[3] = { DKR_BLOCK | DKR_TIME, 0, 0 };
2167c478bd9Sstevel@tonic-gate 	int	ret;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	(void) usetup(role, fdreadp, fdwritep);
21909b0d01cSGary Mills 	if ((ret = (*Ioctl)(*fdreadp, DIOCRMODE, dkrmode)) < 0) {
2207c478bd9Sstevel@tonic-gate 		DEBUG(4, "dksetup: failed to set block mode. ret=%d,\n", ret);
2217c478bd9Sstevel@tonic-gate 		DEBUG(4, "read fd=%d, ", *fdreadp);
2227c478bd9Sstevel@tonic-gate 		DEBUG(4, "errno=%d\n", errno);
22309b0d01cSGary Mills 		return (FAIL);
2247c478bd9Sstevel@tonic-gate 	}
22509b0d01cSGary Mills 	return (SUCCESS);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  *	dkteardown  -  DATAKIT teardown routine
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate static int
dkteardown(role,fdread,fdwrite)23209b0d01cSGary Mills dkteardown(role, fdread, fdwrite)
2337c478bd9Sstevel@tonic-gate int	role, fdread, fdwrite;
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	char	*ttyn;
2367c478bd9Sstevel@tonic-gate 
23709b0d01cSGary Mills 	if (role == MASTER) {
2387c478bd9Sstevel@tonic-gate 		ttyn = ttyname(fdread);
23909b0d01cSGary Mills 		if (ttyn != NULL && Dev_mode != 0)
2407c478bd9Sstevel@tonic-gate 			chmod(ttyn, Dev_mode);	/* can fail, but who cares? */
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*	must flush fd's for datakit	*/
2447c478bd9Sstevel@tonic-gate 	/*	else close can hang		*/
24509b0d01cSGary Mills 	if (ioctl(fdread, DIOCFLUSH, NULL) != 0)
24609b0d01cSGary Mills 		DEBUG(4, "dkteardown: DIOCFLUSH of input fd %d failed",
24709b0d01cSGary Mills 		    fdread);
24809b0d01cSGary Mills 	if (ioctl(fdwrite, DIOCFLUSH, NULL) != 0)
24909b0d01cSGary Mills 		DEBUG(4, "dkteardown: DIOCFLUSH of output fd %d failed",
25009b0d01cSGary Mills 		    fdwrite);
25109b0d01cSGary Mills 
25209b0d01cSGary Mills 	(void) close(fdread);
25309b0d01cSGary Mills 	(void) close(fdwrite);
25409b0d01cSGary Mills 	return (SUCCESS);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate #endif /* DATAKIT */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate #ifdef TLI
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  *	tread - tli read routine
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate static ssize_t
tread(fd,buf,nbytes)2647c478bd9Sstevel@tonic-gate tread(fd, buf, nbytes)
2657c478bd9Sstevel@tonic-gate int		fd;
2667c478bd9Sstevel@tonic-gate char		*buf;
2677c478bd9Sstevel@tonic-gate unsigned	nbytes;
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	int		rcvflags;
2707c478bd9Sstevel@tonic-gate 
27109b0d01cSGary Mills 	return ((ssize_t)t_rcv(fd, buf, nbytes, &rcvflags));
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  *	twrite - tli write routine
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate #define	N_CHECK	100
2787c478bd9Sstevel@tonic-gate static ssize_t
twrite(fd,buf,nbytes)2797c478bd9Sstevel@tonic-gate twrite(fd, buf, nbytes)
2807c478bd9Sstevel@tonic-gate int		fd;
2817c478bd9Sstevel@tonic-gate char		*buf;
2827c478bd9Sstevel@tonic-gate unsigned	nbytes;
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	register int		i, ret;
2857c478bd9Sstevel@tonic-gate 	static int		n_writ, got_info;
2867c478bd9Sstevel@tonic-gate 	static struct t_info	info;
2877c478bd9Sstevel@tonic-gate 
28809b0d01cSGary Mills 	if (got_info == 0) {
28909b0d01cSGary Mills 		if (t_getinfo(fd, &info) != 0) {
2907c478bd9Sstevel@tonic-gate 			tfaillog(fd, "twrite: t_getinfo\n");
29109b0d01cSGary Mills 			return (FAIL);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		got_info = 1;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* on every N_CHECKth call, check that are still in DATAXFER state */
29709b0d01cSGary Mills 	if (++n_writ == N_CHECK) {
2987c478bd9Sstevel@tonic-gate 		n_writ = 0;
29909b0d01cSGary Mills 		if (t_getstate(fd) != T_DATAXFER)
30009b0d01cSGary Mills 			return (FAIL);
30109b0d01cSGary Mills 	}
3027c478bd9Sstevel@tonic-gate 
30309b0d01cSGary Mills 	if (info.tsdu <= 0 || nbytes <= info.tsdu)
30409f57adeSToomas Soome 		return (t_snd(fd, buf, nbytes, 0));
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/* if get here, then there is a limit on transmit size	*/
3077c478bd9Sstevel@tonic-gate 	/* (info.tsdu > 0) and buf exceeds it			*/
3087c478bd9Sstevel@tonic-gate 	i = ret = 0;
30909b0d01cSGary Mills 	while (nbytes >= info.tsdu) {
31009f57adeSToomas Soome 		if ((ret = t_snd(fd,  &buf[i], info.tsdu, 0)) != info.tsdu)
31109b0d01cSGary Mills 			return ((ret >= 0 ? (i + ret) : ret));
3127c478bd9Sstevel@tonic-gate 		i += info.tsdu;
3137c478bd9Sstevel@tonic-gate 		nbytes -= info.tsdu;
3147c478bd9Sstevel@tonic-gate 	}
31509b0d01cSGary Mills 	if (nbytes != 0) {
31609f57adeSToomas Soome 		if ((ret = t_snd(fd,  &buf[i], nbytes, 0)) != nbytes)
31709b0d01cSGary Mills 			return ((ssize_t)(ret >= 0 ? (i + ret) : ret));
3187c478bd9Sstevel@tonic-gate 		i += nbytes;
3197c478bd9Sstevel@tonic-gate 	}
32009b0d01cSGary Mills 	return ((ssize_t)i);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  *	tioctl - stub for tli ioctl routine
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate static int
tioctl(int fd __unused,int request __unused,...)328*3b296559SToomas Soome tioctl(int fd __unused, int request __unused, ...)
3297c478bd9Sstevel@tonic-gate {
33009b0d01cSGary Mills 	return (SUCCESS);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  *	tsetup - tli setup routine
3357c478bd9Sstevel@tonic-gate  *	note blatant assumption that *fdreadp == *fdwritep == 0
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate static int
tsetup(int role,int * fdreadp,int * fdwritep)338*3b296559SToomas Soome tsetup(int role, int *fdreadp, int *fdwritep)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 
34109b0d01cSGary Mills 	if (role == SLAVE) {
3427c478bd9Sstevel@tonic-gate 		*fdreadp = 0;
3437c478bd9Sstevel@tonic-gate 		*fdwritep = 1;
3447c478bd9Sstevel@tonic-gate 		/* 2 has been re-opened to RMTDEBUG in main() */
3457c478bd9Sstevel@tonic-gate 		errno = t_errno = 0;
34609b0d01cSGary Mills 		if (t_sync(*fdreadp) == -1 || t_sync(*fdwritep) == -1) {
3477c478bd9Sstevel@tonic-gate 			tfaillog(*fdreadp, "tsetup: t_sync\n");
34809b0d01cSGary Mills 			return (FAIL);
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 	}
35109b0d01cSGary Mills 	return (SUCCESS);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  *	tteardown - tli shutdown routine
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate static int
tteardown(int role __unused,int fdread,int fdwrite __unused)358*3b296559SToomas Soome tteardown(int role __unused, int fdread, int fdwrite __unused)
3597c478bd9Sstevel@tonic-gate {
36009b0d01cSGary Mills 	(void) t_unbind(fdread);
36109b0d01cSGary Mills 	(void) t_close(fdread);
36209b0d01cSGary Mills 	return (SUCCESS);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate #ifdef TLIS
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  *	tssetup - tli, with streams module, setup routine
3687c478bd9Sstevel@tonic-gate  *	note blatant assumption that *fdreadp == *fdwritep
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate static int
tssetup(role,fdreadp,fdwritep)37109b0d01cSGary Mills tssetup(role, fdreadp, fdwritep)
3727c478bd9Sstevel@tonic-gate int	role;
3737c478bd9Sstevel@tonic-gate int	*fdreadp;
3747c478bd9Sstevel@tonic-gate int	*fdwritep;
3757c478bd9Sstevel@tonic-gate {
37609b0d01cSGary Mills 	if (role == SLAVE) {
3777c478bd9Sstevel@tonic-gate 		*fdreadp = 0;
3787c478bd9Sstevel@tonic-gate 		*fdwritep = 1;
3797c478bd9Sstevel@tonic-gate 		/* 2 has been re-opened to RMTDEBUG in main() */
3807c478bd9Sstevel@tonic-gate 		DEBUG(5, "tssetup: SLAVE mode: leaving ok\n%s", "");
38109b0d01cSGary Mills 		return (SUCCESS);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	DEBUG(4, "tssetup: MASTER mode: leaving ok\n%s", "");
38509b0d01cSGary Mills 	return (SUCCESS);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  *	Report why a TLI call failed.
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate GLOBAL void
tfaillog(fd,s)3927c478bd9Sstevel@tonic-gate tfaillog(fd, s)
3937c478bd9Sstevel@tonic-gate int	fd;
3947c478bd9Sstevel@tonic-gate char	*s;
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	char	fmt[ BUFSIZ ];
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (0 < t_errno && t_errno < t_nerr) {
39909b0d01cSGary Mills 		sprintf(fmt, "%s: %%s\n", s);
4007c478bd9Sstevel@tonic-gate 		DEBUG(5, fmt, t_errlist[t_errno]);
4017c478bd9Sstevel@tonic-gate 		logent(s, t_errlist[t_errno]);
40209b0d01cSGary Mills 		if (t_errno == TSYSERR) {
4037c478bd9Sstevel@tonic-gate 			strcpy(fmt, "tlicall: system error: %s\n");
404ace1a5f1Sdp 			DEBUG(5, fmt, strerror(errno));
40509b0d01cSGary Mills 		} else if (t_errno == TLOOK) {
4067c478bd9Sstevel@tonic-gate 			show_tlook(fd);
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 	} else {
4097c478bd9Sstevel@tonic-gate 		sprintf(fmt, "unknown tli error %d", t_errno);
4107c478bd9Sstevel@tonic-gate 		logent(s, fmt);
4117c478bd9Sstevel@tonic-gate 		sprintf(fmt, "%s: unknown tli error %d", s, t_errno);
4127c478bd9Sstevel@tonic-gate 		DEBUG(5, fmt, 0);
4137c478bd9Sstevel@tonic-gate 		sprintf(fmt, "%s: %%s\n", s);
414ace1a5f1Sdp 		DEBUG(5, fmt, strerror(errno));
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate GLOBAL void
show_tlook(fd)4197c478bd9Sstevel@tonic-gate show_tlook(fd)
4207c478bd9Sstevel@tonic-gate int fd;
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	register int reason;
4237c478bd9Sstevel@tonic-gate 	register char *msg;
4247c478bd9Sstevel@tonic-gate 	extern int	t_errno;
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * Find out the current state of the interface.
4277c478bd9Sstevel@tonic-gate  */
4287c478bd9Sstevel@tonic-gate 	errno = t_errno = 0;
42909b0d01cSGary Mills 	switch (reason = t_getstate(fd)) {
4307c478bd9Sstevel@tonic-gate 	case T_UNBND:		msg = "T_UNBIND";	break;
4317c478bd9Sstevel@tonic-gate 	case T_IDLE:		msg = "T_IDLE";		break;
4327c478bd9Sstevel@tonic-gate 	case T_OUTCON:		msg = "T_OUTCON";	break;
4337c478bd9Sstevel@tonic-gate 	case T_INCON:		msg = "T_INCON";	break;
4347c478bd9Sstevel@tonic-gate 	case T_DATAXFER:	msg = "T_DATAXFER";	break;
4357c478bd9Sstevel@tonic-gate 	case T_OUTREL:		msg = "T_OUTREL";	break;
4367c478bd9Sstevel@tonic-gate 	case T_INREL:		msg = "T_INREL";	break;
4377c478bd9Sstevel@tonic-gate 	default:		msg = NULL;		break;
4387c478bd9Sstevel@tonic-gate 	}
43909b0d01cSGary Mills 	if (msg == NULL)
4407c478bd9Sstevel@tonic-gate 		return;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	DEBUG(5, "state is %s", msg);
44309b0d01cSGary Mills 	switch (reason = t_look(fd)) {
4447c478bd9Sstevel@tonic-gate 	case -1:		msg = ""; break;
4457c478bd9Sstevel@tonic-gate 	case 0:			msg = "NO ERROR"; break;
4467c478bd9Sstevel@tonic-gate 	case T_LISTEN:		msg = "T_LISTEN"; break;
4477c478bd9Sstevel@tonic-gate 	case T_CONNECT:		msg = "T_CONNECT"; break;
4487c478bd9Sstevel@tonic-gate 	case T_DATA:		msg = "T_DATA";	 break;
4497c478bd9Sstevel@tonic-gate 	case T_EXDATA:		msg = "T_EXDATA"; break;
4507c478bd9Sstevel@tonic-gate 	case T_DISCONNECT:	msg = "T_DISCONNECT"; break;
4517c478bd9Sstevel@tonic-gate 	case T_ORDREL:		msg = "T_ORDREL"; break;
4527c478bd9Sstevel@tonic-gate 	case T_UDERR:		msg = "T_UDERR"; break;
4537c478bd9Sstevel@tonic-gate 	default:		msg = "UNKNOWN ERROR"; break;
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	DEBUG(4, " reason is %s\n", msg);
4567c478bd9Sstevel@tonic-gate 
45709b0d01cSGary Mills 	if (reason == T_DISCONNECT)
4587c478bd9Sstevel@tonic-gate 	{
4597c478bd9Sstevel@tonic-gate 		struct t_discon	*dropped;
46009b0d01cSGary Mills 		if (((dropped =
46109b0d01cSGary Mills 		    (struct t_discon *)t_alloc(fd, T_DIS, T_ALL)) == 0) ||
46209b0d01cSGary Mills 		    (t_rcvdis(fd, dropped) == -1)) {
4637c478bd9Sstevel@tonic-gate 			if (dropped)
4647c478bd9Sstevel@tonic-gate 				t_free((char *)dropped, T_DIS);
4657c478bd9Sstevel@tonic-gate 			return;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		DEBUG(5, "disconnect reason #%d\n", dropped->reason);
4687c478bd9Sstevel@tonic-gate 		t_free((char *)dropped, T_DIS);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate #endif /*  TLIS  */
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate #endif /*  TLI  */
475