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