1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * svc_tcp.c, Server side for TCP/IP based RPC.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * Actually implements two flavors of transporter -
44*7c478bd9Sstevel@tonic-gate  * a tcp rendezvouser (a listner and connection establisher)
45*7c478bd9Sstevel@tonic-gate  * and a record/tcp stream.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
51*7c478bd9Sstevel@tonic-gate #include <errno.h>
52*7c478bd9Sstevel@tonic-gate #include <syslog.h>
53*7c478bd9Sstevel@tonic-gate #include <malloc.h>
54*7c478bd9Sstevel@tonic-gate #include <stdio.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate extern bool_t abort();
57*7c478bd9Sstevel@tonic-gate extern int errno;
58*7c478bd9Sstevel@tonic-gate extern SVCXPRT *svc_xprt_alloc();
59*7c478bd9Sstevel@tonic-gate extern void svc_xprt_free();
60*7c478bd9Sstevel@tonic-gate extern int _socket(int, int, int);
61*7c478bd9Sstevel@tonic-gate extern int _bind(int, const struct sockaddr *, int);
62*7c478bd9Sstevel@tonic-gate extern int _getsockname(int, struct sockaddr *, int *);
63*7c478bd9Sstevel@tonic-gate extern int _listen(int, int);
64*7c478bd9Sstevel@tonic-gate extern int _accept(int, struct sockaddr *, int *);
65*7c478bd9Sstevel@tonic-gate extern int bindresvport(int, struct sockaddr_in *);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate static struct xp_ops *svctcp_ops();
68*7c478bd9Sstevel@tonic-gate static struct xp_ops *svctcp_rendezvous_ops();
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static int readtcp(), writetcp();
71*7c478bd9Sstevel@tonic-gate static SVCXPRT *makefd_xprt();
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate struct tcp_rendezvous { /* kept in xprt->xp_p1 */
74*7c478bd9Sstevel@tonic-gate 	u_int sendsize;
75*7c478bd9Sstevel@tonic-gate 	u_int recvsize;
76*7c478bd9Sstevel@tonic-gate };
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate struct tcp_conn {  /* kept in xprt->xp_p1 */
79*7c478bd9Sstevel@tonic-gate 	enum xprt_stat strm_stat;
80*7c478bd9Sstevel@tonic-gate 	uint32_t x_id;
81*7c478bd9Sstevel@tonic-gate 	XDR xdrs;
82*7c478bd9Sstevel@tonic-gate 	char verf_body[MAX_AUTH_BYTES];
83*7c478bd9Sstevel@tonic-gate };
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * Usage:
87*7c478bd9Sstevel@tonic-gate  *	xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  * Creates, registers, and returns a (rpc) tcp based transporter.
90*7c478bd9Sstevel@tonic-gate  * Once *xprt is initialized, it is registered as a transporter
91*7c478bd9Sstevel@tonic-gate  * see (svc.h, xprt_register).  This routine returns
92*7c478bd9Sstevel@tonic-gate  * a NULL if a problem occurred.
93*7c478bd9Sstevel@tonic-gate  *
94*7c478bd9Sstevel@tonic-gate  * If sock<0 then a socket is created, else sock is used.
95*7c478bd9Sstevel@tonic-gate  * If the socket, sock is not bound to a port then svctcp_create
96*7c478bd9Sstevel@tonic-gate  * binds it to an arbitrary port.  The routine then starts a tcp
97*7c478bd9Sstevel@tonic-gate  * listener on the socket's associated port.  In any (successful) case,
98*7c478bd9Sstevel@tonic-gate  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
99*7c478bd9Sstevel@tonic-gate  * associated port number.
100*7c478bd9Sstevel@tonic-gate  *
101*7c478bd9Sstevel@tonic-gate  * Since tcp streams do buffered io similar to stdio, the caller can specify
102*7c478bd9Sstevel@tonic-gate  * how big the send and receive buffers are via the second and third parms;
103*7c478bd9Sstevel@tonic-gate  * 0 => use the system default.
104*7c478bd9Sstevel@tonic-gate  */
105*7c478bd9Sstevel@tonic-gate SVCXPRT *
svctcp_create(sock,sendsize,recvsize)106*7c478bd9Sstevel@tonic-gate svctcp_create(sock, sendsize, recvsize)
107*7c478bd9Sstevel@tonic-gate 	register int sock;
108*7c478bd9Sstevel@tonic-gate 	u_int sendsize;
109*7c478bd9Sstevel@tonic-gate 	u_int recvsize;
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	bool_t madesock = FALSE;
112*7c478bd9Sstevel@tonic-gate 	register SVCXPRT *xprt;
113*7c478bd9Sstevel@tonic-gate 	register struct tcp_rendezvous *r;
114*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr;
115*7c478bd9Sstevel@tonic-gate 	int len = sizeof (struct sockaddr_in);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if (sock == RPC_ANYSOCK) {
118*7c478bd9Sstevel@tonic-gate 		if ((sock = _socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
119*7c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR, "svctcp_create - tcp",
120*7c478bd9Sstevel@tonic-gate 				" socket creation problem: %m");
121*7c478bd9Sstevel@tonic-gate 			return ((SVCXPRT *)NULL);
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 		madesock = TRUE;
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	memset((char *)&addr, 0, sizeof (addr));
126*7c478bd9Sstevel@tonic-gate 	addr.sin_family = AF_INET;
127*7c478bd9Sstevel@tonic-gate 	if (bindresvport(sock, &addr)) {
128*7c478bd9Sstevel@tonic-gate 		addr.sin_port = 0;
129*7c478bd9Sstevel@tonic-gate 		(void) _bind(sock, (struct sockaddr *)&addr, len);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 	if ((_getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
132*7c478bd9Sstevel@tonic-gate 	    (_listen(sock, 2) != 0)) {
133*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svctcp_create - cannot",
134*7c478bd9Sstevel@tonic-gate 			" getsockname or listen: %m");
135*7c478bd9Sstevel@tonic-gate 		if (madesock)
136*7c478bd9Sstevel@tonic-gate 			(void) close(sock);
137*7c478bd9Sstevel@tonic-gate 		return ((SVCXPRT *)NULL);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	r = (struct tcp_rendezvous *)mem_alloc(sizeof (*r));
140*7c478bd9Sstevel@tonic-gate 	if (r == NULL) {
141*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svctcp_create: out of memory");
142*7c478bd9Sstevel@tonic-gate 		if (madesock)
143*7c478bd9Sstevel@tonic-gate 			(void) close(sock);
144*7c478bd9Sstevel@tonic-gate 		return (NULL);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 	r->sendsize = sendsize;
147*7c478bd9Sstevel@tonic-gate 	r->recvsize = recvsize;
148*7c478bd9Sstevel@tonic-gate 	xprt = svc_xprt_alloc();
149*7c478bd9Sstevel@tonic-gate 	if (xprt == NULL) {
150*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svctcp_create: out of memory");
151*7c478bd9Sstevel@tonic-gate 		mem_free((char *) r, sizeof (*r));
152*7c478bd9Sstevel@tonic-gate 		if (madesock)
153*7c478bd9Sstevel@tonic-gate 			(void) close(sock);
154*7c478bd9Sstevel@tonic-gate 		return (NULL);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	xprt->xp_p2 = NULL;
157*7c478bd9Sstevel@tonic-gate 	xprt->xp_netid = NULL;
158*7c478bd9Sstevel@tonic-gate 	xprt->xp_p1 = (caddr_t)r;
159*7c478bd9Sstevel@tonic-gate 	xprt->xp_verf = _null_auth;
160*7c478bd9Sstevel@tonic-gate 	xprt->xp_ops = svctcp_rendezvous_ops();
161*7c478bd9Sstevel@tonic-gate 	xprt->xp_port = ntohs(addr.sin_port);
162*7c478bd9Sstevel@tonic-gate 	xprt->xp_sock = sock;
163*7c478bd9Sstevel@tonic-gate 	xprt->xp_rtaddr.buf = xprt->xp_raddr;
164*7c478bd9Sstevel@tonic-gate 	xprt_register(xprt);
165*7c478bd9Sstevel@tonic-gate 	return (xprt);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * Like svtcp_create(), except the routine takes any *open* UNIX file
170*7c478bd9Sstevel@tonic-gate  * descriptor as its first input.
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate SVCXPRT *
svcfd_create(fd,sendsize,recvsize)173*7c478bd9Sstevel@tonic-gate svcfd_create(fd, sendsize, recvsize)
174*7c478bd9Sstevel@tonic-gate 	int fd;
175*7c478bd9Sstevel@tonic-gate 	u_int sendsize;
176*7c478bd9Sstevel@tonic-gate 	u_int recvsize;
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	return (makefd_xprt(fd, sendsize, recvsize));
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static SVCXPRT *
makefd_xprt(fd,sendsize,recvsize)183*7c478bd9Sstevel@tonic-gate makefd_xprt(fd, sendsize, recvsize)
184*7c478bd9Sstevel@tonic-gate 	int fd;
185*7c478bd9Sstevel@tonic-gate 	u_int sendsize;
186*7c478bd9Sstevel@tonic-gate 	u_int recvsize;
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	register SVCXPRT *xprt;
189*7c478bd9Sstevel@tonic-gate 	register struct tcp_conn *cd;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	xprt = svc_xprt_alloc();
192*7c478bd9Sstevel@tonic-gate 	if (xprt == (SVCXPRT *)NULL) {
193*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svc_tcp: makefd_xprt: out of memory");
194*7c478bd9Sstevel@tonic-gate 		goto done;
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 	cd = (struct tcp_conn *)mem_alloc(sizeof (struct tcp_conn));
197*7c478bd9Sstevel@tonic-gate 	if (cd == (struct tcp_conn *)NULL) {
198*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "svc_tcp: makefd_xprt: out of memory");
199*7c478bd9Sstevel@tonic-gate 		svc_xprt_free(xprt);
200*7c478bd9Sstevel@tonic-gate 		xprt = (SVCXPRT *)NULL;
201*7c478bd9Sstevel@tonic-gate 		goto done;
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 	cd->strm_stat = XPRT_IDLE;
204*7c478bd9Sstevel@tonic-gate 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
205*7c478bd9Sstevel@tonic-gate 	    (caddr_t)xprt, readtcp, writetcp);
206*7c478bd9Sstevel@tonic-gate 	xprt->xp_p2 = NULL;
207*7c478bd9Sstevel@tonic-gate 	xprt->xp_netid = NULL;
208*7c478bd9Sstevel@tonic-gate 	xprt->xp_p1 = (caddr_t)cd;
209*7c478bd9Sstevel@tonic-gate 	xprt->xp_verf.oa_base = cd->verf_body;
210*7c478bd9Sstevel@tonic-gate 	xprt->xp_addrlen = 0;
211*7c478bd9Sstevel@tonic-gate 	xprt->xp_ops = svctcp_ops();  /* truely deals with calls */
212*7c478bd9Sstevel@tonic-gate 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
213*7c478bd9Sstevel@tonic-gate 	xprt->xp_sock = fd;
214*7c478bd9Sstevel@tonic-gate 	/* to handle svc_getcaller() properly */
215*7c478bd9Sstevel@tonic-gate 	xprt->xp_rtaddr.buf = xprt->xp_raddr;
216*7c478bd9Sstevel@tonic-gate 	xprt_register(xprt);
217*7c478bd9Sstevel@tonic-gate 	done:
218*7c478bd9Sstevel@tonic-gate 	return (xprt);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate static bool_t
rendezvous_request(xprt,rpc_msg)222*7c478bd9Sstevel@tonic-gate rendezvous_request(xprt, rpc_msg)
223*7c478bd9Sstevel@tonic-gate 	register SVCXPRT *xprt;
224*7c478bd9Sstevel@tonic-gate 	struct rpc_msg	*rpc_msg;
225*7c478bd9Sstevel@tonic-gate {
226*7c478bd9Sstevel@tonic-gate 	int sock;
227*7c478bd9Sstevel@tonic-gate 	struct tcp_rendezvous *r;
228*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr;
229*7c478bd9Sstevel@tonic-gate 	int len;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	r = (struct tcp_rendezvous *)xprt->xp_p1;
232*7c478bd9Sstevel@tonic-gate 	again:
233*7c478bd9Sstevel@tonic-gate 	len = sizeof (struct sockaddr_in);
234*7c478bd9Sstevel@tonic-gate 	if ((sock = _accept(xprt->xp_sock, (struct sockaddr *)&addr,
235*7c478bd9Sstevel@tonic-gate 	    &len)) < 0) {
236*7c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
237*7c478bd9Sstevel@tonic-gate 			goto again;
238*7c478bd9Sstevel@tonic-gate 		return (FALSE);
239*7c478bd9Sstevel@tonic-gate 	}
240*7c478bd9Sstevel@tonic-gate 	/*
241*7c478bd9Sstevel@tonic-gate 	 * make a new transporter (re-uses xprt)
242*7c478bd9Sstevel@tonic-gate 	 */
243*7c478bd9Sstevel@tonic-gate 	xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	memcpy((char *)&xprt->xp_raddr, (char *)&addr, len);
246*7c478bd9Sstevel@tonic-gate 	xprt->xp_addrlen = len;
247*7c478bd9Sstevel@tonic-gate 	return (FALSE); /* there is never an rpc msg to be processed */
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate static enum xprt_stat
rendezvous_stat(xprt)251*7c478bd9Sstevel@tonic-gate rendezvous_stat(xprt)
252*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	return (XPRT_IDLE);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate static void
svctcp_destroy(xprt)259*7c478bd9Sstevel@tonic-gate svctcp_destroy(xprt)
260*7c478bd9Sstevel@tonic-gate 	register SVCXPRT *xprt;
261*7c478bd9Sstevel@tonic-gate {
262*7c478bd9Sstevel@tonic-gate 	register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	xprt_unregister(xprt);
265*7c478bd9Sstevel@tonic-gate 	(void) close(xprt->xp_sock);
266*7c478bd9Sstevel@tonic-gate 	if (xprt->xp_port != 0) {
267*7c478bd9Sstevel@tonic-gate 		/* a rendezvouser socket */
268*7c478bd9Sstevel@tonic-gate 		xprt->xp_port = 0;
269*7c478bd9Sstevel@tonic-gate 	} else {
270*7c478bd9Sstevel@tonic-gate 		/* an actual connection socket */
271*7c478bd9Sstevel@tonic-gate 		XDR_DESTROY(&(cd->xdrs));
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 	mem_free((caddr_t)cd, sizeof (struct tcp_conn));
274*7c478bd9Sstevel@tonic-gate 	svc_xprt_free(xprt);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate /*
278*7c478bd9Sstevel@tonic-gate  * All read operations timeout after 35 seconds.
279*7c478bd9Sstevel@tonic-gate  * A timeout is fatal for the connection.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate static struct timeval wait_per_try = { 35, 0 };
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate /*
284*7c478bd9Sstevel@tonic-gate  * reads data from the tcp conection.
285*7c478bd9Sstevel@tonic-gate  * any error is fatal and the connection is closed.
286*7c478bd9Sstevel@tonic-gate  * (And a read of zero bytes is a half closed stream => error.)
287*7c478bd9Sstevel@tonic-gate  */
288*7c478bd9Sstevel@tonic-gate static int
readtcp(xprt,buf,len)289*7c478bd9Sstevel@tonic-gate readtcp(xprt, buf, len)
290*7c478bd9Sstevel@tonic-gate 	register SVCXPRT *xprt;
291*7c478bd9Sstevel@tonic-gate 	caddr_t buf;
292*7c478bd9Sstevel@tonic-gate 	register int len;
293*7c478bd9Sstevel@tonic-gate {
294*7c478bd9Sstevel@tonic-gate 	register int sock = xprt->xp_sock;
295*7c478bd9Sstevel@tonic-gate 	fd_set mask;
296*7c478bd9Sstevel@tonic-gate 	fd_set readfds;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	FD_ZERO(&mask);
299*7c478bd9Sstevel@tonic-gate 	FD_SET(sock, &mask);
300*7c478bd9Sstevel@tonic-gate 	do {
301*7c478bd9Sstevel@tonic-gate 		readfds = mask;
302*7c478bd9Sstevel@tonic-gate 		if (select(__rpc_dtbsize(), &readfds, NULL, NULL,
303*7c478bd9Sstevel@tonic-gate 			&wait_per_try) <= 0) {
304*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR) {
305*7c478bd9Sstevel@tonic-gate 				continue;
306*7c478bd9Sstevel@tonic-gate 			}
307*7c478bd9Sstevel@tonic-gate 			goto fatal_err;
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 	} while (!FD_ISSET(sock, &readfds));
310*7c478bd9Sstevel@tonic-gate 	if ((len = read(sock, buf, len)) > 0) {
311*7c478bd9Sstevel@tonic-gate 		return (len);
312*7c478bd9Sstevel@tonic-gate 	}
313*7c478bd9Sstevel@tonic-gate fatal_err:
314*7c478bd9Sstevel@tonic-gate 	((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
315*7c478bd9Sstevel@tonic-gate 	return (-1);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate /*
319*7c478bd9Sstevel@tonic-gate  * writes data to the tcp connection.
320*7c478bd9Sstevel@tonic-gate  * Any error is fatal and the connection is closed.
321*7c478bd9Sstevel@tonic-gate  */
322*7c478bd9Sstevel@tonic-gate static int
writetcp(xprt,buf,len)323*7c478bd9Sstevel@tonic-gate writetcp(xprt, buf, len)
324*7c478bd9Sstevel@tonic-gate 	register SVCXPRT *xprt;
325*7c478bd9Sstevel@tonic-gate 	caddr_t buf;
326*7c478bd9Sstevel@tonic-gate 	int len;
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate 	register int i, cnt;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
331*7c478bd9Sstevel@tonic-gate 		if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
332*7c478bd9Sstevel@tonic-gate 			((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
333*7c478bd9Sstevel@tonic-gate 			    XPRT_DIED;
334*7c478bd9Sstevel@tonic-gate 			return (-1);
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate 	return (len);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate static enum xprt_stat
svctcp_stat(xprt)341*7c478bd9Sstevel@tonic-gate svctcp_stat(xprt)
342*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate 	register struct tcp_conn *cd =
345*7c478bd9Sstevel@tonic-gate 	    (struct tcp_conn *)(xprt->xp_p1);
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if (cd->strm_stat == XPRT_DIED)
348*7c478bd9Sstevel@tonic-gate 		return (XPRT_DIED);
349*7c478bd9Sstevel@tonic-gate 	if (! xdrrec_eof(&(cd->xdrs)))
350*7c478bd9Sstevel@tonic-gate 		return (XPRT_MOREREQS);
351*7c478bd9Sstevel@tonic-gate 	return (XPRT_IDLE);
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate static bool_t
svctcp_recv(xprt,msg)355*7c478bd9Sstevel@tonic-gate svctcp_recv(xprt, msg)
356*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
357*7c478bd9Sstevel@tonic-gate 	register struct rpc_msg *msg;
358*7c478bd9Sstevel@tonic-gate {
359*7c478bd9Sstevel@tonic-gate 	register struct tcp_conn *cd =
360*7c478bd9Sstevel@tonic-gate 	    (struct tcp_conn *)(xprt->xp_p1);
361*7c478bd9Sstevel@tonic-gate 	register XDR *xdrs = &(cd->xdrs);
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_DECODE;
364*7c478bd9Sstevel@tonic-gate 	(void) xdrrec_skiprecord(xdrs);
365*7c478bd9Sstevel@tonic-gate 	if (xdr_callmsg(xdrs, msg)) {
366*7c478bd9Sstevel@tonic-gate 		cd->x_id = msg->rm_xid;
367*7c478bd9Sstevel@tonic-gate 		return (TRUE);
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 	return (FALSE);
370*7c478bd9Sstevel@tonic-gate }
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate static bool_t
svctcp_getargs(xprt,xdr_args,args_ptr)373*7c478bd9Sstevel@tonic-gate svctcp_getargs(xprt, xdr_args, args_ptr)
374*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
375*7c478bd9Sstevel@tonic-gate 	xdrproc_t xdr_args;
376*7c478bd9Sstevel@tonic-gate 	caddr_t args_ptr;
377*7c478bd9Sstevel@tonic-gate {
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs),
380*7c478bd9Sstevel@tonic-gate 		args_ptr));
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate static bool_t
svctcp_freeargs(xprt,xdr_args,args_ptr)384*7c478bd9Sstevel@tonic-gate svctcp_freeargs(xprt, xdr_args, args_ptr)
385*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
386*7c478bd9Sstevel@tonic-gate 	xdrproc_t xdr_args;
387*7c478bd9Sstevel@tonic-gate 	caddr_t args_ptr;
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	register XDR *xdrs =
390*7c478bd9Sstevel@tonic-gate 	    &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
393*7c478bd9Sstevel@tonic-gate 	return ((*xdr_args)(xdrs, args_ptr));
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate static bool_t
svctcp_reply(xprt,msg)397*7c478bd9Sstevel@tonic-gate svctcp_reply(xprt, msg)
398*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
399*7c478bd9Sstevel@tonic-gate 	register struct rpc_msg *msg;
400*7c478bd9Sstevel@tonic-gate {
401*7c478bd9Sstevel@tonic-gate 	register struct tcp_conn *cd =
402*7c478bd9Sstevel@tonic-gate 	    (struct tcp_conn *)(xprt->xp_p1);
403*7c478bd9Sstevel@tonic-gate 	register XDR *xdrs = &(cd->xdrs);
404*7c478bd9Sstevel@tonic-gate 	register bool_t stat;
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
407*7c478bd9Sstevel@tonic-gate 	msg->rm_xid = cd->x_id;
408*7c478bd9Sstevel@tonic-gate 	stat = xdr_replymsg(xdrs, msg);
409*7c478bd9Sstevel@tonic-gate 	(void) xdrrec_endofrecord(xdrs, TRUE);
410*7c478bd9Sstevel@tonic-gate 	return (stat);
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate static struct xp_ops *
svctcp_ops()415*7c478bd9Sstevel@tonic-gate svctcp_ops()
416*7c478bd9Sstevel@tonic-gate {
417*7c478bd9Sstevel@tonic-gate 	static struct xp_ops ops;
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	if (ops.xp_recv == NULL) {
420*7c478bd9Sstevel@tonic-gate 		ops.xp_recv = svctcp_recv;
421*7c478bd9Sstevel@tonic-gate 		ops.xp_stat = svctcp_stat;
422*7c478bd9Sstevel@tonic-gate 		ops.xp_getargs = svctcp_getargs;
423*7c478bd9Sstevel@tonic-gate 		ops.xp_reply = svctcp_reply;
424*7c478bd9Sstevel@tonic-gate 		ops.xp_freeargs = svctcp_freeargs;
425*7c478bd9Sstevel@tonic-gate 		ops.xp_destroy = svctcp_destroy;
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 	return (&ops);
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate static struct xp_ops *
svctcp_rendezvous_ops()432*7c478bd9Sstevel@tonic-gate svctcp_rendezvous_ops()
433*7c478bd9Sstevel@tonic-gate {
434*7c478bd9Sstevel@tonic-gate 	static struct xp_ops ops;
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	if (ops.xp_recv == NULL) {
437*7c478bd9Sstevel@tonic-gate 		ops.xp_recv = rendezvous_request;
438*7c478bd9Sstevel@tonic-gate 		ops.xp_stat = rendezvous_stat;
439*7c478bd9Sstevel@tonic-gate 		ops.xp_getargs = abort;
440*7c478bd9Sstevel@tonic-gate 		ops.xp_reply = abort;
441*7c478bd9Sstevel@tonic-gate 		ops.xp_freeargs = abort,
442*7c478bd9Sstevel@tonic-gate 		ops.xp_destroy = svctcp_destroy;
443*7c478bd9Sstevel@tonic-gate 	}
444*7c478bd9Sstevel@tonic-gate 	return (&ops);
445*7c478bd9Sstevel@tonic-gate }
446