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