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