xref: /onnv-gate/usr/src/lib/libnsl/dial/interface.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
33132Srobinson /*
34132Srobinson  * interface( label )
35132Srobinson  *	provide alternate definitions for the I/O functions through global
36132Srobinson  *	interfaces.
37132Srobinson  */
38*1219Sraf #include "mt.h"
39*1219Sraf #include "uucp.h"
40*1219Sraf #include <unistd.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #ifdef TLI
43*1219Sraf #include <tiuser.h>
440Sstevel@tonic-gate #endif /*  TLI  */
450Sstevel@tonic-gate 
46132Srobinson static void	sethup(int);
47132Srobinson static int	restline(void);
48132Srobinson static int	usetup(int, int *, int *);
49132Srobinson static int	uteardown(int, int, int);
500Sstevel@tonic-gate 
51132Srobinson static ssize_t	(*Read)() = read,
520Sstevel@tonic-gate 	(*Write)() = write;
53132Srobinson static int	(*Ioctl)(int, int, ...) = ioctl,
54132Srobinson 	(*Setup)() = usetup;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #ifdef TLI
57132Srobinson static void tfaillog(int fd, const char *s);
58132Srobinson static void show_tlook(int);
59132Srobinson static ssize_t	tread(int, char *, unsigned);
60132Srobinson static ssize_t	twrite(int, char *, unsigned);
61132Srobinson static int tioctl(int, int, ...);
62132Srobinson static int tsetup(int, int *, int *); /* TLI setup without streams module */
63132Srobinson static int tssetup(int, int *, int *); /* TLI setup with streams module */
64132Srobinson static int tteardown(int, int, int); /* TLI teardown, works with either setup */
650Sstevel@tonic-gate #endif /*  TLI  */
660Sstevel@tonic-gate 
67132Srobinson /*
68132Srobinson  *	The IN_label in Interface[] imply different caller routines:
69132Srobinson  *	e.g. tlicall().
70132Srobinson  *	If so, the names here and the names in callers.c must match.
71132Srobinson  */
72132Srobinson static struct Interface {
73132Srobinson 	const	char	*IN_label;	/* interface name */
740Sstevel@tonic-gate 	ssize_t	(*IN_read)();		/* read function */
750Sstevel@tonic-gate 	ssize_t	(*IN_write)();		/* write function */
76132Srobinson 	int	(*IN_ioctl)(int, int, ...);
77132Srobinson 	int	(*IN_setup)();		/* setup function, called before */
78132Srobinson 					/* first i/o operation */
79132Srobinson 	int	(*IN_teardown)();	/* teardown function, called after */
80132Srobinson 					/* last i/o operation */
810Sstevel@tonic-gate } Interface[] = {
820Sstevel@tonic-gate 			/* vanilla UNIX */
830Sstevel@tonic-gate 		{ "UNIX", read, write, ioctl, usetup, uteardown },
840Sstevel@tonic-gate #ifdef TLI
850Sstevel@tonic-gate 			/* AT&T Transport Interface Library WITHOUT streams */
860Sstevel@tonic-gate 		{ "TLI", tread, twrite, tioctl, tsetup, tteardown },
870Sstevel@tonic-gate #ifdef TLIS
880Sstevel@tonic-gate 			/* AT&T Transport Interface Library WITH streams */
890Sstevel@tonic-gate 		{ "TLIS", read, write, tioctl, tssetup, uteardown },
900Sstevel@tonic-gate #endif /*  TLIS  */
910Sstevel@tonic-gate #endif /*  TLI  */
920Sstevel@tonic-gate 		{ 0, 0, 0, 0, 0, 0 }
930Sstevel@tonic-gate 	};
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
96132Srobinson static int
interface(const char * label)97132Srobinson interface(const char *label)
980Sstevel@tonic-gate {
99132Srobinson 	int	i;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	for (i = 0;  Interface[i].IN_label;  ++i) {
102132Srobinson 		if (strcmp(Interface[i].IN_label, label) == 0) {
1030Sstevel@tonic-gate 			Read = Interface[i].IN_read;
1040Sstevel@tonic-gate 			Write = Interface[i].IN_write;
1050Sstevel@tonic-gate 			Ioctl = Interface[i].IN_ioctl;
1060Sstevel@tonic-gate 			Setup = Interface[i].IN_setup;
1070Sstevel@tonic-gate 			DEBUG(5, "set interface %s\n", label);
1080Sstevel@tonic-gate 			return (0);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	return (FAIL);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  *	usetup - vanilla unix setup routine
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate static int
usetup(int role,int * fdreadp,int * fdwritep)1180Sstevel@tonic-gate usetup(int role, int *fdreadp, int *fdwritep)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	if (role == SLAVE) {
1210Sstevel@tonic-gate 		*fdreadp = 0;
1220Sstevel@tonic-gate 		*fdwritep = 1;
1230Sstevel@tonic-gate 		/* 2 has been re-opened to RMTDEBUG in main() */
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 	return (SUCCESS);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  *	uteardown - vanilla unix teardown routine
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate static int
uteardown(int role,int fdread,int fdwrite)1320Sstevel@tonic-gate uteardown(int role, int fdread, int fdwrite)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	char *ttyn;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (role == SLAVE) {
137132Srobinson 		(void) restline();
1380Sstevel@tonic-gate 		sethup(0);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	if (fdread != -1) {
1410Sstevel@tonic-gate 		ttyn = ttyname(fdread);
1420Sstevel@tonic-gate 		if (ttyn != NULL)
143132Srobinson 			/* can fail, but who cares? */
144132Srobinson 			(void) chmod(ttyn, Dev_mode);
1450Sstevel@tonic-gate 		(void) close(fdread);
1460Sstevel@tonic-gate 		(void) close(fdwrite);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	return (SUCCESS);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #ifdef TLI
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  *	tread - tli read routine
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate static ssize_t
tread(int fd,char * buf,unsigned nbytes)156132Srobinson tread(int fd, char *buf, unsigned nbytes)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	int		rcvflags;
1590Sstevel@tonic-gate 
160132Srobinson 	return ((ssize_t)t_rcv(fd, buf, nbytes, &rcvflags));
161132Srobinson }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  *	twrite - tli write routine
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate #define	N_CHECK	100
1670Sstevel@tonic-gate static ssize_t
twrite(int fd,char * buf,unsigned nbytes)168132Srobinson twrite(int fd, char *buf, unsigned nbytes)
1690Sstevel@tonic-gate {
170132Srobinson 	int			i, ret;
1710Sstevel@tonic-gate 	static int		n_writ, got_info;
1720Sstevel@tonic-gate 	static struct t_info	info;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (got_info == 0) {
1750Sstevel@tonic-gate 		if (t_getinfo(fd, &info) != 0) {
1760Sstevel@tonic-gate 			tfaillog(fd, "twrite: t_getinfo\n");
1770Sstevel@tonic-gate 			return (FAIL);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		got_info = 1;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* on every N_CHECKth call, check that are still in DATAXFER state */
1830Sstevel@tonic-gate 	if (++n_writ == N_CHECK) {
1840Sstevel@tonic-gate 		n_writ = 0;
185132Srobinson 		if (t_getstate(fd) != T_DATAXFER)
1860Sstevel@tonic-gate 			return (FAIL);
187132Srobinson 	}
1880Sstevel@tonic-gate 
189132Srobinson 	if (info.tsdu <= 0 || nbytes <= info.tsdu)
190132Srobinson 		return ((ssize_t)t_snd(fd, buf, nbytes, NULL));
1910Sstevel@tonic-gate 	/* if get here, then there is a limit on transmit size	*/
1920Sstevel@tonic-gate 	/* (info.tsdu > 0) and buf exceeds it			*/
1930Sstevel@tonic-gate 	i = ret = 0;
1940Sstevel@tonic-gate 	while (nbytes >= info.tsdu) {
195132Srobinson 		if ((ret = t_snd(fd,  &buf[i], info.tsdu, NULL)) != info.tsdu)
1960Sstevel@tonic-gate 			return ((ssize_t)(ret >= 0 ? (i + ret) : ret));
1970Sstevel@tonic-gate 		i += info.tsdu;
1980Sstevel@tonic-gate 		nbytes -= info.tsdu;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	if (nbytes != 0) {
201132Srobinson 		if ((ret = t_snd(fd,  &buf[i], nbytes, NULL)) != nbytes)
2020Sstevel@tonic-gate 			return ((ssize_t)(ret >= 0 ? (i + ret) : ret));
2030Sstevel@tonic-gate 		i += nbytes;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 	return ((ssize_t)i);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  *	tioctl - stub for tli ioctl routine
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate /* ARGSUSED */
2120Sstevel@tonic-gate static int
tioctl(int fd,int request,...)2130Sstevel@tonic-gate tioctl(int fd, int request, ...)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	return (SUCCESS);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  *	tsetup - tli setup routine
2200Sstevel@tonic-gate  *	note blatant assumption that *fdreadp == *fdwritep == 0
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate static int
tsetup(int role,int * fdreadp,int * fdwritep)2230Sstevel@tonic-gate tsetup(int role, int *fdreadp, int *fdwritep)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	if (role == SLAVE) {
2260Sstevel@tonic-gate 		*fdreadp = 0;
2270Sstevel@tonic-gate 		*fdwritep = 1;
2280Sstevel@tonic-gate 		/* 2 has been re-opened to RMTDEBUG in main() */
2290Sstevel@tonic-gate 		errno = t_errno = 0;
2300Sstevel@tonic-gate 		if (t_sync(*fdreadp) == -1 || t_sync(*fdwritep) == -1) {
2310Sstevel@tonic-gate 			tfaillog(*fdreadp, "tsetup: t_sync\n");
2320Sstevel@tonic-gate 			return (FAIL);
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	return (SUCCESS);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  *	tteardown - tli shutdown routine
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate /* ARGSUSED */
2420Sstevel@tonic-gate static int
tteardown(int role,int fdread,int fdwrite)2430Sstevel@tonic-gate tteardown(int role, int fdread, int fdwrite)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	(void) t_unbind(fdread);
2460Sstevel@tonic-gate 	(void) t_close(fdread);
2470Sstevel@tonic-gate 	return (SUCCESS);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate #ifdef TLIS
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  *	tssetup - tli, with streams module, setup routine
2530Sstevel@tonic-gate  *	note blatant assumption that *fdreadp == *fdwritep
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate static int
tssetup(int role,int * fdreadp,int * fdwritep)256132Srobinson tssetup(int role, int *fdreadp, int *fdwritep)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	if (role == SLAVE) {
2590Sstevel@tonic-gate 		*fdreadp = 0;
2600Sstevel@tonic-gate 		*fdwritep = 1;
2610Sstevel@tonic-gate 		/* 2 has been re-opened to RMTDEBUG in main() */
2620Sstevel@tonic-gate 		DEBUG(5, "tssetup: SLAVE mode: leaving ok\n%s", "");
2630Sstevel@tonic-gate 		return (SUCCESS);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	DEBUG(4, "tssetup: MASTER mode: leaving ok\n%s", "");
2670Sstevel@tonic-gate 	return (SUCCESS);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  *	Report why a TLI call failed.
2720Sstevel@tonic-gate  */
273132Srobinson static void
tfaillog(int fd,const char * s)274132Srobinson tfaillog(int fd, const char *s)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	char	fmt[ BUFSIZ ];
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (0 < t_errno && t_errno < t_nerr) {
279132Srobinson 		(void) snprintf(fmt, sizeof (fmt), "%s: %%s\n", s);
2800Sstevel@tonic-gate 		DEBUG(5, fmt, t_errlist[t_errno]);
2810Sstevel@tonic-gate 		logent(s, t_errlist[t_errno]);
2820Sstevel@tonic-gate 		if (t_errno == TSYSERR) {
283132Srobinson 			(void) strcpy(fmt, "tlicall: system error: %s\n");
2840Sstevel@tonic-gate 			DEBUG(5, fmt, strerror(errno));
2850Sstevel@tonic-gate 		} else if (t_errno == TLOOK) {
2860Sstevel@tonic-gate 			show_tlook(fd);
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 	} else {
289132Srobinson 		(void) snprintf(fmt, sizeof (fmt),
290132Srobinson 					"unknown tli error %d", t_errno);
2910Sstevel@tonic-gate 		logent(s, fmt);
292132Srobinson 		(void) snprintf(fmt, sizeof (fmt),
293132Srobinson 					"%s: unknown tli error %d", s, t_errno);
2940Sstevel@tonic-gate 		DEBUG(5, fmt, 0);
295132Srobinson 		(void) snprintf(fmt, sizeof (fmt), "%s: %%s\n", s);
2960Sstevel@tonic-gate 		DEBUG(5, fmt, strerror(errno));
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
300132Srobinson static void
show_tlook(int fd)301132Srobinson show_tlook(int fd)
3020Sstevel@tonic-gate {
303132Srobinson 	int reason;
304132Srobinson 	const char *msg;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * Find out the current state of the interface.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate 	errno = t_errno = 0;
310132Srobinson 	switch (reason = t_getstate(fd)) {
3110Sstevel@tonic-gate 	case T_UNBND:		msg = (const char *)"T_UNBIND";	break;
3120Sstevel@tonic-gate 	case T_IDLE:		msg = (const char *)"T_IDLE";	break;
3130Sstevel@tonic-gate 	case T_OUTCON:		msg = (const char *)"T_OUTCON";	break;
3140Sstevel@tonic-gate 	case T_INCON:		msg = (const char *)"T_INCON";	break;
3150Sstevel@tonic-gate 	case T_DATAXFER:	msg = (const char *)"T_DATAXFER"; break;
3160Sstevel@tonic-gate 	case T_OUTREL:		msg = (const char *)"T_OUTREL";	break;
3170Sstevel@tonic-gate 	case T_INREL:		msg = (const char *)"T_INREL";	break;
3180Sstevel@tonic-gate 	default:		msg = NULL;		break;
3190Sstevel@tonic-gate 	}
320132Srobinson 	if (msg == NULL)
3210Sstevel@tonic-gate 		return;
3220Sstevel@tonic-gate 	DEBUG(5, "state is %s", msg);
323132Srobinson 	switch (reason = t_look(fd)) {
3240Sstevel@tonic-gate 	case -1:		msg = (const char *)""; break;
3250Sstevel@tonic-gate 	case 0:			msg = (const char *)"NO ERROR"; break;
3260Sstevel@tonic-gate 	case T_LISTEN:		msg = (const char *)"T_LISTEN"; break;
3270Sstevel@tonic-gate 	case T_CONNECT:		msg = (const char *)"T_CONNECT"; break;
3280Sstevel@tonic-gate 	case T_DATA:		msg = (const char *)"T_DATA";	 break;
3290Sstevel@tonic-gate 	case T_EXDATA:		msg = (const char *)"T_EXDATA"; break;
3300Sstevel@tonic-gate 	case T_DISCONNECT:	msg = (const char *)"T_DISCONNECT"; break;
3310Sstevel@tonic-gate 	case T_ORDREL:		msg = (const char *)"T_ORDREL"; break;
3320Sstevel@tonic-gate 	case T_ERROR:		msg = (const char *)"T_ERROR"; break;
3330Sstevel@tonic-gate 	case T_UDERR:		msg = (const char *)"T_UDERR"; break;
3340Sstevel@tonic-gate 	default:		msg = (const char *)"UNKNOWN ERROR"; break;
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 	DEBUG(4, " reason is %s\n", msg);
3370Sstevel@tonic-gate 
338132Srobinson 	if (reason == T_DISCONNECT) {
3390Sstevel@tonic-gate 		struct t_discon	*dropped;
340132Srobinson 		if (((dropped =
341132Srobinson 			/* LINTED pointer cast */
342132Srobinson 			(struct t_discon *)t_alloc(fd, T_DIS, T_ALL)) == 0) ||
343132Srobinson 			(t_rcvdis(fd, dropped) == -1)) {
3440Sstevel@tonic-gate 			if (dropped)
345132Srobinson 				(void) t_free((char *)dropped, T_DIS);
3460Sstevel@tonic-gate 			return;
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 		DEBUG(5, "disconnect reason #%d\n", dropped->reason);
349132Srobinson 		(void) t_free((char *)dropped, T_DIS);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate #endif /*  TLIS  */
3530Sstevel@tonic-gate #endif /*  TLI  */
354