xref: /titanic_51/usr/src/lib/libnsl/nsl/t_connect.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 1993-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.7 */
32 
33 #include "mt.h"
34 #include <rpc/trace.h>
35 #include <stropts.h>
36 #include <stdlib.h>
37 #include <sys/timod.h>
38 #define	_SUN_TPI_VERSION 2
39 #include <sys/tihdr.h>
40 #include <xti.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <errno.h>
44 #include <syslog.h>
45 #include "tx.h"
46 
47 /*
48  * If a system call fails with EINTR after T_CONN_REQ is sent out,
49  * we change state for caller to continue with t_rcvconnect(). This
50  * semantics is not documented for TLI but is the direction taken with
51  * XTI so we adopt it. With this the call establishment is completed
52  * by calling t_rcvconnect() even for synchronous endpoints.
53  */
54 int
55 _tx_connect(
56 	int fd,
57 	const struct t_call *sndcall,
58 	struct t_call *rcvcall,
59 	int api_semantics
60 )
61 {
62 	int fctlflg;
63 	struct _ti_user *tiptr;
64 	sigset_t mask;
65 	struct strbuf ctlbuf;
66 	int sv_errno;
67 	int didalloc;
68 
69 	trace2(TR_t_connect, 0, fd);
70 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) {
71 		sv_errno = errno;
72 		trace2(TR_t_connect, 1, fd);
73 		errno = sv_errno;
74 		return (-1);
75 	}
76 
77 	sig_mutex_lock(&tiptr->ti_lock);
78 	if (_T_IS_XTI(api_semantics)) {
79 		/*
80 		 * User level state verification only done for XTI
81 		 * because doing for TLI may break existing applications
82 		 */
83 		if (tiptr->ti_state != T_IDLE) {
84 			t_errno = TOUTSTATE;
85 			sig_mutex_unlock(&tiptr->ti_lock);
86 			trace2(TR_t_connect, 1, fd);
87 			return (-1);
88 		}
89 	}
90 
91 	/*
92 	 * Acquire ctlbuf for use in sending/receiving control part
93 	 * of the message.
94 	 */
95 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
96 		sv_errno = errno;
97 		sig_mutex_unlock(&tiptr->ti_lock);
98 		trace2(TR_t_connect, 1, fd);
99 		errno = sv_errno;
100 		return (-1);
101 	}
102 	/*
103 	 * Block all signals until T_CONN_REQ sent and
104 	 * acked with T_OK_ACK/ERROR_ACK
105 	 */
106 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
107 	if (_t_snd_conn_req(tiptr, sndcall, &ctlbuf) < 0) {
108 		sv_errno = errno;
109 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
110 		errno = sv_errno;
111 		/*
112 		 * At the TPI level, the error returned in a T_ERROR_ACK
113 		 * received in response to a T_CONN_REQ for an attempt to
114 		 * establish a duplicate conection has changed to a
115 		 * new t_errno code introduced with XTI (ADDRBUSY).
116 		 * We need to adjust TLI error code to be same as before.
117 		 */
118 		if (_T_IS_TLI(api_semantics) && t_errno == TADDRBUSY)
119 			/* TLI only */
120 			t_errno = TBADADDR;
121 
122 		goto err_out;
123 	}
124 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
125 
126 	if ((fctlflg = _fcntl(fd, F_GETFL, 0)) < 0) {
127 		t_errno = TSYSERR;
128 		goto err_out;
129 	}
130 
131 	if (fctlflg & (O_NDELAY | O_NONBLOCK)) {
132 		_T_TX_NEXTSTATE(T_CONNECT2, tiptr,
133 				"t_connect: invalid state event T_CONNECT2");
134 		t_errno = TNODATA;
135 		goto err_out;
136 	}
137 
138 	/*
139 	 * Note: The following call to _t_rcv_conn_con blocks awaiting
140 	 * T_CONN_CON from remote client. Therefore it drops the
141 	 * tiptr->lock during the call (and reacquires it)
142 	 */
143 	if (_t_rcv_conn_con(tiptr, rcvcall, &ctlbuf, api_semantics) < 0) {
144 		if ((t_errno == TSYSERR && errno == EINTR) ||
145 		    t_errno == TLOOK) {
146 			_T_TX_NEXTSTATE(T_CONNECT2, tiptr,
147 				"t_connect: invalid state event T_CONNECT2");
148 		} else if (t_errno == TBUFOVFLW) {
149 			_T_TX_NEXTSTATE(T_CONNECT1, tiptr,
150 				"t_connect: invalid state event T_CONNECT1");
151 		}
152 		goto err_out;
153 	}
154 	_T_TX_NEXTSTATE(T_CONNECT1, tiptr,
155 				"t_connect: invalid state event T_CONNECT1");
156 	/*
157 	 * Update attributes which may have been negotiated during
158 	 * connection establishment for protocols where we suspect
159 	 * such negotiation is likely (e.g. OSI). We do not do it for
160 	 * all endpoints for performance reasons. Also, this code is
161 	 * deliberately done after user level state changes so even
162 	 * the (unlikely) failure case reflects a connected endpoint.
163 	 */
164 	if (tiptr->ti_tsdusize != 0) {
165 		if (_t_do_postconn_sync(fd, tiptr) < 0)
166 		    goto err_out;
167 	}
168 
169 
170 	if (didalloc)
171 		free(ctlbuf.buf);
172 	else
173 		tiptr->ti_ctlbuf = ctlbuf.buf;
174 	sig_mutex_unlock(&tiptr->ti_lock);
175 	trace2(TR_t_connect, 1, fd);
176 	return (0);
177 
178 err_out:
179 	sv_errno = errno;
180 	if (didalloc)
181 		free(ctlbuf.buf);
182 	else
183 		tiptr->ti_ctlbuf = ctlbuf.buf;
184 	sig_mutex_unlock(&tiptr->ti_lock);
185 
186 	trace2(TR_t_connect, 1, fd);
187 	errno = sv_errno;
188 	return (-1);
189 }
190