xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_snd.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
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_snd.c and t_sndv.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 <unistd.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <stropts.h>
41 #include <sys/stream.h>
42 #define	_SUN_TPI_VERSION 2
43 #include <sys/tihdr.h>
44 #include <sys/timod.h>
45 #include <xti.h>
46 #include <syslog.h>
47 #include "tx.h"
48 
49 
50 int
51 _tx_snd(int fd, char *buf, unsigned nbytes, int flags, int api_semantics)
52 {
53 	struct T_data_req datareq;
54 	struct strbuf ctlbuf, databuf;
55 	unsigned int bytes_sent, bytes_remaining, bytes_to_send;
56 	char *curptr;
57 	struct _ti_user *tiptr;
58 	int band;
59 	int retval, lookevent;
60 	int sv_errno;
61 	int doputmsg = 0;
62 	int32_t tsdu_limit;
63 
64 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
65 		return (-1);
66 	sig_mutex_lock(&tiptr->ti_lock);
67 
68 	if (tiptr->ti_servtype == T_CLTS) {
69 		t_errno = TNOTSUPPORT;
70 		sig_mutex_unlock(&tiptr->ti_lock);
71 		return (-1);
72 	}
73 
74 	if (_T_IS_XTI(api_semantics)) {
75 		/*
76 		 * User level state verification only done for XTI
77 		 * because doing for TLI may break existing applications
78 		 */
79 		if (! (tiptr->ti_state == T_DATAXFER ||
80 		    tiptr->ti_state == T_INREL)) {
81 			t_errno = TOUTSTATE;
82 			sig_mutex_unlock(&tiptr->ti_lock);
83 			return (-1);
84 		}
85 		/*
86 		 * XXX
87 		 * Is it OK to do this TBADFLAG check when XTI spec
88 		 * is being extended with new and interesting flags
89 		 * everyday ?
90 		 */
91 		if ((flags & ~(TX_ALL_VALID_FLAGS)) != 0) {
92 			t_errno = TBADFLAG;
93 			sig_mutex_unlock(&tiptr->ti_lock);
94 			return (-1);
95 		}
96 		if (flags & T_EXPEDITED)
97 			tsdu_limit = tiptr->ti_etsdusize;
98 		else {
99 			/* normal data */
100 			tsdu_limit = tiptr->ti_tsdusize;
101 		}
102 
103 		if ((tsdu_limit > 0) && /* limit meaningful and ... */
104 		    (nbytes > (uint32_t)tsdu_limit)) {
105 			t_errno = TBADDATA;
106 			sig_mutex_unlock(&tiptr->ti_lock);
107 			return (-1);
108 		}
109 
110 		/*
111 		 * Check for incoming disconnect or orderly release
112 		 * Did anyone say "performance" ? Didn't hear that.
113 		 */
114 		lookevent = _t_look_locked(fd, tiptr, 0, api_semantics);
115 		if (lookevent < 0) {
116 			sv_errno = errno;
117 			sig_mutex_unlock(&tiptr->ti_lock);
118 			errno = sv_errno;
119 			return (-1);
120 		}
121 		/*
122 		 * XNS 4 required the TLOOK error to be returned if there
123 		 * is any incoming T_ORDREL. XNS 5 does not require an
124 		 * error to be returned in such a case.
125 		 */
126 		if (lookevent == T_DISCONNECT ||
127 		    (api_semantics == TX_XTI_XNS4_API &&
128 		    lookevent == T_ORDREL)) {
129 			t_errno = TLOOK;
130 			sig_mutex_unlock(&tiptr->ti_lock);
131 			return (-1);
132 		}
133 	}
134 
135 	/* sending zero length data when not allowed */
136 	if (nbytes == 0 && !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
137 		t_errno = TBADDATA;
138 		sig_mutex_unlock(&tiptr->ti_lock);
139 		return (-1);
140 	}
141 
142 	doputmsg = (tiptr->ti_tsdusize != 0) || (flags & T_EXPEDITED);
143 
144 	if (doputmsg) {
145 		/*
146 		 * Initialize ctlbuf for use in sending/receiving control part
147 		 * of the message.
148 		 */
149 		ctlbuf.maxlen = (int)sizeof (struct T_data_req);
150 		ctlbuf.len = (int)sizeof (struct T_data_req);
151 		ctlbuf.buf = (char *)&datareq;
152 
153 		band = TI_NORMAL; /* band 0 */
154 		if (flags & T_EXPEDITED) {
155 			datareq.PRIM_type = T_EXDATA_REQ;
156 			if (! (tiptr->ti_prov_flag & EXPINLINE))
157 				band = TI_EXPEDITED; /* band > 0 */
158 		} else
159 			datareq.PRIM_type = T_DATA_REQ;
160 	}
161 
162 	bytes_remaining = nbytes;
163 	curptr = buf;
164 
165 	/*
166 	 * Calls to send data (write or putmsg) can potentially
167 	 * block, for MT case, we drop the lock and enable signals here
168 	 * and acquire it back
169 	 */
170 	sig_mutex_unlock(&tiptr->ti_lock);
171 	do {
172 		bytes_to_send = bytes_remaining;
173 		if (doputmsg) {
174 			/*
175 			 * transport provider supports TSDU concept
176 			 * (unlike TCP) or it is expedited data.
177 			 * In this case do the fragmentation
178 			 */
179 			if (bytes_to_send > (unsigned int)tiptr->ti_maxpsz) {
180 				datareq.MORE_flag = 1;
181 				bytes_to_send = (unsigned int)tiptr->ti_maxpsz;
182 			} else {
183 				if (flags&T_MORE)
184 					datareq.MORE_flag = 1;
185 				else
186 					datareq.MORE_flag = 0;
187 			}
188 			databuf.maxlen = bytes_to_send;
189 			databuf.len = bytes_to_send;
190 			databuf.buf = curptr;
191 			retval = putpmsg(fd, &ctlbuf, &databuf, band, MSG_BAND);
192 			if (retval == 0)
193 				bytes_sent = bytes_to_send;
194 		} else {
195 			/*
196 			 * transport provider does *not* support TSDU concept
197 			 * (e.g. TCP) and it is not expedited data. A
198 			 * perf. optimization is used. Note: the T_MORE
199 			 * flag is ignored here even if set by the user.
200 			 */
201 			retval = write(fd, curptr, bytes_to_send);
202 			if (retval >= 0) {
203 				/* Amount that was actually sent */
204 				bytes_sent = retval;
205 			}
206 		}
207 
208 		if (retval < 0) {
209 			if (nbytes == bytes_remaining) {
210 				/*
211 				 * Error on *first* putmsg/write attempt.
212 				 * Return appropriate error
213 				 */
214 				if (errno == EAGAIN)
215 					t_errno = TFLOW;
216 				else
217 					t_errno = TSYSERR;
218 				return (-1); /* return error */
219 			}
220 			/*
221 			 * Not the first putmsg/write
222 			 * [ partial completion of t_snd() case.
223 			 *
224 			 * Error on putmsg/write attempt but
225 			 * some data was transmitted so don't
226 			 * return error. Don't attempt to
227 			 * send more (break from loop) but
228 			 * return OK.
229 			 */
230 			break;
231 		}
232 		bytes_remaining = bytes_remaining - bytes_sent;
233 		curptr = curptr + bytes_sent;
234 	} while (bytes_remaining != 0);
235 
236 	_T_TX_NEXTSTATE(T_SND, tiptr, "t_snd: invalid state event T_SND");
237 	return (nbytes - bytes_remaining);
238 }
239