xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_rcvrel.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 /*
32  * t_rcvrel.c and t_rcvreldata.c are very similar and contain common code.
33  * Any changes to either of them should be reviewed to see whether they
34  * are applicable to the other file.
35  */
36 #include "mt.h"
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <stropts.h>
40 #include <sys/stream.h>
41 #define	_SUN_TPI_VERSION 2
42 #include <sys/tihdr.h>
43 #include <sys/timod.h>
44 #include <xti.h>
45 #include <signal.h>
46 #include <syslog.h>
47 #include "tx.h"
48 
49 int
50 _tx_rcvrel(int fd, int api_semantics)
51 {
52 	struct strbuf ctlbuf;
53 	struct strbuf databuf;
54 	int retval;
55 	union T_primitives *pptr;
56 	struct _ti_user *tiptr;
57 	int sv_errno;
58 	int didalloc, didralloc;
59 
60 	int flg = 0;
61 
62 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
63 		return (-1);
64 	sig_mutex_lock(&tiptr->ti_lock);
65 
66 	if (tiptr->ti_servtype != T_COTS_ORD) {
67 		t_errno = TNOTSUPPORT;
68 		sig_mutex_unlock(&tiptr->ti_lock);
69 		return (-1);
70 	}
71 
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_DATAXFER ||
78 		    tiptr->ti_state == T_OUTREL)) {
79 			t_errno = TOUTSTATE;
80 			sig_mutex_unlock(&tiptr->ti_lock);
81 			return (-1);
82 		}
83 	}
84 
85 	if ((retval = _t_look_locked(fd, tiptr, 0, api_semantics)) < 0) {
86 		sv_errno = errno;
87 		sig_mutex_unlock(&tiptr->ti_lock);
88 		errno = sv_errno;
89 		return (-1);
90 	}
91 
92 	if (retval == T_DISCONNECT) {
93 		/*
94 		 * This ensures preference to T_DISCON_IND which is
95 		 * the design model for TPI
96 		 */
97 		t_errno = TLOOK;
98 		sig_mutex_unlock(&tiptr->ti_lock);
99 		return (-1);
100 	}
101 
102 	if ((tiptr->ti_lookcnt > 0) &&
103 	    /* LINTED pointer cast */
104 	    (*((t_scalar_t *)tiptr->ti_lookbufs.tl_lookcbuf) == T_ORDREL_IND)) {
105 		/*
106 		 * Current look buffer event is T_ORDREL_IND.
107 		 * Remove it from look buffer event list.
108 		 */
109 		_t_free_looklist_head(tiptr);
110 		_T_TX_NEXTSTATE(T_RCVREL, tiptr,
111 					"t_rcv: invalid state event T_RCVREL");
112 		sig_mutex_unlock(&tiptr->ti_lock);
113 		return (0);
114 	} else {
115 		if (retval != T_ORDREL) {
116 			t_errno = TNOREL;
117 			sig_mutex_unlock(&tiptr->ti_lock);
118 			return (-1);
119 		}
120 	}
121 
122 	/*
123 	 * get ordrel off read queue.
124 	 * use ctl and rcv buffers
125 	 *
126 	 * Acquire ctlbuf for use in sending/receiving control part
127 	 * of the message.
128 	 */
129 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
130 		sv_errno = errno;
131 		sig_mutex_unlock(&tiptr->ti_lock);
132 		errno = sv_errno;
133 		return (-1);
134 	}
135 
136 	/*
137 	 * Acquire databuf for use in sending/receiving data part
138 	 */
139 	if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0) {
140 		sv_errno = errno;
141 		if (didalloc)
142 			free(ctlbuf.buf);
143 		else
144 			tiptr->ti_ctlbuf = ctlbuf.buf;
145 		sig_mutex_unlock(&tiptr->ti_lock);
146 		errno = sv_errno;
147 		return (-1);
148 	}
149 
150 	/*
151 	 * Since we have verified above that an orderly release event
152 	 * is pending on this endpoint, we assume that this getmsg()
153 	 * cannot block forever.
154 	 */
155 	do {
156 		retval = getmsg(fd, &ctlbuf, &databuf, &flg);
157 	} while (retval < 0 && errno == EINTR);
158 
159 	if (retval < 0) {
160 		t_errno = TSYSERR;
161 		goto err_out;
162 	}
163 
164 	/*
165 	 * did I get entire message?
166 	 */
167 	if (retval > 0) {
168 		t_errno = TSYSERR;
169 		errno = EIO;
170 		goto err_out;
171 	}
172 	/* LINTED pointer cast */
173 	pptr = (union T_primitives *)ctlbuf.buf;
174 
175 	if (ctlbuf.len < (int)sizeof (struct T_ordrel_ind)) {
176 		t_errno = TSYSERR;
177 		errno = EPROTO;
178 		goto err_out;
179 	}
180 	if (pptr->type != T_ORDREL_IND) {
181 		if (pptr->type == T_DISCON_IND) {
182 			/*
183 			 * T_DISCON_IND gets priority following
184 			 * TPI design philosphy.
185 			 *
186 			 * Add it to the events in the "look buffer"
187 			 * list of events. This routine may defer signals.
188 			 */
189 			if (_t_register_lookevent(tiptr, databuf.buf,
190 						databuf.len, ctlbuf.buf,
191 						ctlbuf.len) < 0) {
192 				t_errno = TSYSERR;
193 				errno = ENOMEM;
194 				goto err_out;
195 			}
196 			t_errno = TLOOK;
197 			goto err_out;
198 		} else {
199 			t_errno = TSYSERR;
200 			errno = EPROTO;
201 			goto err_out;
202 		}
203 	}
204 
205 	_T_TX_NEXTSTATE(T_RCVREL, tiptr,
206 			"t_rcvrel: invalid state event T_RCVREL");
207 
208 	if (didalloc)
209 		free(ctlbuf.buf);
210 	else
211 		tiptr->ti_ctlbuf = ctlbuf.buf;
212 	if (didralloc)
213 		free(databuf.buf);
214 	else
215 		tiptr->ti_rcvbuf = databuf.buf;
216 	sig_mutex_unlock(&tiptr->ti_lock);
217 	return (0);
218 
219 err_out:
220 	sv_errno = errno;
221 
222 	if (didalloc)
223 		free(ctlbuf.buf);
224 	else
225 		tiptr->ti_ctlbuf = ctlbuf.buf;
226 	if (didralloc)
227 		free(databuf.buf);
228 	else
229 		tiptr->ti_rcvbuf = databuf.buf;
230 	sig_mutex_unlock(&tiptr->ti_lock);
231 	errno = sv_errno;
232 	return (-1);
233 }
234