xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_connect.c (revision fc910014e8a32a65612105835a10995f2c13d942)
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 
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include "mt.h"
32 #include <stropts.h>
33 #include <stdlib.h>
34 #include <sys/timod.h>
35 #define	_SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
37 #include <xti.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include <syslog.h>
42 #include "tx.h"
43 
44 /*
45  * If a system call fails with EINTR after T_CONN_REQ is sent out,
46  * we change state for caller to continue with t_rcvconnect(). This
47  * semantics is not documented for TLI but is the direction taken with
48  * XTI so we adopt it. With this the call establishment is completed
49  * by calling t_rcvconnect() even for synchronous endpoints.
50  */
51 int
52 _tx_connect(
53 	int fd,
54 	const struct t_call *sndcall,
55 	struct t_call *rcvcall,
56 	int api_semantics
57 )
58 {
59 	int fctlflg;
60 	struct _ti_user *tiptr;
61 	sigset_t mask;
62 	struct strbuf ctlbuf;
63 	int sv_errno;
64 	int didalloc;
65 
66 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
67 		return (-1);
68 
69 	sig_mutex_lock(&tiptr->ti_lock);
70 	if (_T_IS_XTI(api_semantics)) {
71 		/*
72 		 * User level state verification only done for XTI
73 		 * because doing for TLI may break existing applications
74 		 */
75 		if (tiptr->ti_state != T_IDLE) {
76 			t_errno = TOUTSTATE;
77 			sig_mutex_unlock(&tiptr->ti_lock);
78 			return (-1);
79 		}
80 	}
81 
82 	/*
83 	 * Acquire ctlbuf for use in sending/receiving control part
84 	 * of the message.
85 	 */
86 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
87 		sv_errno = errno;
88 		sig_mutex_unlock(&tiptr->ti_lock);
89 		errno = sv_errno;
90 		return (-1);
91 	}
92 	/*
93 	 * Block all signals until T_CONN_REQ sent and
94 	 * acked with T_OK_ACK/ERROR_ACK
95 	 */
96 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
97 	if (_t_snd_conn_req(tiptr, sndcall, &ctlbuf) < 0) {
98 		sv_errno = errno;
99 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
100 		errno = sv_errno;
101 		/*
102 		 * At the TPI level, the error returned in a T_ERROR_ACK
103 		 * received in response to a T_CONN_REQ for an attempt to
104 		 * establish a duplicate conection has changed to a
105 		 * new t_errno code introduced with XTI (ADDRBUSY).
106 		 * We need to adjust TLI error code to be same as before.
107 		 */
108 		if (_T_IS_TLI(api_semantics) && t_errno == TADDRBUSY)
109 			/* TLI only */
110 			t_errno = TBADADDR;
111 
112 		goto err_out;
113 	}
114 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
115 
116 	if ((fctlflg = fcntl(fd, F_GETFL, 0)) < 0) {
117 		t_errno = TSYSERR;
118 		goto err_out;
119 	}
120 
121 	if (fctlflg & (O_NDELAY | O_NONBLOCK)) {
122 		_T_TX_NEXTSTATE(T_CONNECT2, tiptr,
123 				"t_connect: invalid state event T_CONNECT2");
124 		t_errno = TNODATA;
125 		goto err_out;
126 	}
127 
128 	/*
129 	 * Note: The following call to _t_rcv_conn_con blocks awaiting
130 	 * T_CONN_CON from remote client. Therefore it drops the
131 	 * tiptr->lock during the call (and reacquires it)
132 	 */
133 	if (_t_rcv_conn_con(tiptr, rcvcall, &ctlbuf, api_semantics) < 0) {
134 		if ((t_errno == TSYSERR && errno == EINTR) ||
135 		    t_errno == TLOOK) {
136 			_T_TX_NEXTSTATE(T_CONNECT2, tiptr,
137 				"t_connect: invalid state event T_CONNECT2");
138 		} else if (t_errno == TBUFOVFLW) {
139 			_T_TX_NEXTSTATE(T_CONNECT1, tiptr,
140 				"t_connect: invalid state event T_CONNECT1");
141 		}
142 		goto err_out;
143 	}
144 	_T_TX_NEXTSTATE(T_CONNECT1, tiptr,
145 				"t_connect: invalid state event T_CONNECT1");
146 	/*
147 	 * Update attributes which may have been negotiated during
148 	 * connection establishment for protocols where we suspect
149 	 * such negotiation is likely (e.g. OSI). We do not do it for
150 	 * all endpoints for performance reasons. Also, this code is
151 	 * deliberately done after user level state changes so even
152 	 * the (unlikely) failure case reflects a connected endpoint.
153 	 */
154 	if (tiptr->ti_tsdusize != 0) {
155 		if (_t_do_postconn_sync(fd, tiptr) < 0)
156 		    goto err_out;
157 	}
158 
159 
160 	if (didalloc)
161 		free(ctlbuf.buf);
162 	else
163 		tiptr->ti_ctlbuf = ctlbuf.buf;
164 	sig_mutex_unlock(&tiptr->ti_lock);
165 	return (0);
166 
167 err_out:
168 	sv_errno = errno;
169 	if (didalloc)
170 		free(ctlbuf.buf);
171 	else
172 		tiptr->ti_ctlbuf = ctlbuf.buf;
173 	sig_mutex_unlock(&tiptr->ti_lock);
174 
175 	errno = sv_errno;
176 	return (-1);
177 }
178