xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_rcvconnect.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include "mt.h"
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <stropts.h>
35 #include <xti.h>
36 #include <sys/timod.h>
37 #include "tx.h"
38 
39 
40 /*
41  * t_rcvconnect() is documented to be only called with non-blocking
42  * endpoints for asynchronous connection establishment. However, the
43  * direction taken by XTI is to allow it to be called if t_connect()
44  * fails with TSYSERR/EINTR and state is T_OUTCON (i.e. T_CONN_REQ was
45  * sent down). This implies that an interrupted synchronous connection
46  * establishment which was interrupted after connection request was transmitted
47  * can now be completed by calling t_rcvconnect()
48  */
49 int
50 _tx_rcvconnect(int fd, struct t_call *call, int api_semantics)
51 {
52 	struct _ti_user *tiptr;
53 	int retval, sv_errno;
54 	struct strbuf ctlbuf;
55 	int didalloc;
56 
57 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
58 		return (-1);
59 
60 	sig_mutex_lock(&tiptr->ti_lock);
61 
62 	if (_T_IS_XTI(api_semantics)) {
63 		/*
64 		 * User level state verification only done for XTI
65 		 * because doing for TLI may break existing applications
66 		 */
67 		if (tiptr->ti_state != T_OUTCON) {
68 			t_errno = TOUTSTATE;
69 			sig_mutex_unlock(&tiptr->ti_lock);
70 			return (-1);
71 		}
72 	}
73 
74 	/*
75 	 * Acquire ctlbuf for use in sending/receiving control part
76 	 * of the message.
77 	 */
78 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
79 		sv_errno = errno;
80 		sig_mutex_unlock(&tiptr->ti_lock);
81 		errno = sv_errno;
82 		return (-1);
83 	}
84 
85 	retval = _t_rcv_conn_con(tiptr, call, &ctlbuf, api_semantics);
86 	if (retval == 0 || t_errno == TBUFOVFLW) {
87 		_T_TX_NEXTSTATE(T_RCVCONNECT, tiptr,
88 			"t_rcvconnect: Invalid state on event T_RCVCONNECT");
89 		/*
90 		 * Update attributes which may have been negotiated during
91 		 * connection establishment for protocols where we suspect
92 		 * such negotiation is likely (e.g. OSI). We do not do it for
93 		 * all endpoints for performance reasons. Also, this code is
94 		 * deliberately done after user level state changes so even
95 		 * the (unlikely) failure case reflects a connected endpoint.
96 		 */
97 		if (tiptr->ti_tsdusize != 0)
98 			if (_t_do_postconn_sync(fd, tiptr) < 0)
99 				retval = -1;
100 	}
101 	sv_errno = errno;
102 	if (didalloc)
103 		free(ctlbuf.buf);
104 	else
105 		tiptr->ti_ctlbuf = ctlbuf.buf;
106 	sig_mutex_unlock(&tiptr->ti_lock);
107 	errno = sv_errno;
108 	return (retval);
109 }
110