xref: /freebsd/crypto/krb5/src/lib/rpc/svc_tcp.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* @(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  *     * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  *     * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert  *       the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert  *       distribution.
17*7f2fe78bSCy Schubert  *
18*7f2fe78bSCy Schubert  *     * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert  *       its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert  *       derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert  *
22*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert  */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert  * svc_tcp.c, Server side for TCP/IP based RPC.
40*7f2fe78bSCy Schubert  *
41*7f2fe78bSCy Schubert  * Actually implements two flavors of transporter -
42*7f2fe78bSCy Schubert  * a tcp rendezvouser (a listener and connection establisher)
43*7f2fe78bSCy Schubert  * and a record/tcp stream.
44*7f2fe78bSCy Schubert  */
45*7f2fe78bSCy Schubert 
46*7f2fe78bSCy Schubert #include "k5-platform.h"
47*7f2fe78bSCy Schubert #include <unistd.h>
48*7f2fe78bSCy Schubert #include <gssrpc/rpc.h>
49*7f2fe78bSCy Schubert #include <sys/socket.h>
50*7f2fe78bSCy Schubert #include <port-sockets.h>
51*7f2fe78bSCy Schubert #include <socket-utils.h>
52*7f2fe78bSCy Schubert /*extern bool_t abort();
53*7f2fe78bSCy Schubert extern errno;
54*7f2fe78bSCy Schubert */
55*7f2fe78bSCy Schubert 
56*7f2fe78bSCy Schubert #ifndef FD_SETSIZE
57*7f2fe78bSCy Schubert #ifdef NBBY
58*7f2fe78bSCy Schubert #define NOFILE (sizeof(int) * NBBY)
59*7f2fe78bSCy Schubert #else
60*7f2fe78bSCy Schubert #define NOFILE (sizeof(int) * 8)
61*7f2fe78bSCy Schubert #endif
62*7f2fe78bSCy Schubert #endif
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert /*
65*7f2fe78bSCy Schubert  * Ops vector for TCP/IP based rpc service handle
66*7f2fe78bSCy Schubert  */
67*7f2fe78bSCy Schubert static bool_t		svctcp_recv(SVCXPRT *, struct rpc_msg *);
68*7f2fe78bSCy Schubert static enum xprt_stat	svctcp_stat(SVCXPRT *);
69*7f2fe78bSCy Schubert static bool_t		svctcp_getargs(SVCXPRT *, xdrproc_t, void *);
70*7f2fe78bSCy Schubert static bool_t		svctcp_reply(SVCXPRT *, struct rpc_msg *);
71*7f2fe78bSCy Schubert static bool_t		svctcp_freeargs(SVCXPRT *, xdrproc_t, void *);
72*7f2fe78bSCy Schubert static void		svctcp_destroy(SVCXPRT *);
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert static struct xp_ops svctcp_op = {
75*7f2fe78bSCy Schubert 	svctcp_recv,
76*7f2fe78bSCy Schubert 	svctcp_stat,
77*7f2fe78bSCy Schubert 	svctcp_getargs,
78*7f2fe78bSCy Schubert 	svctcp_reply,
79*7f2fe78bSCy Schubert 	svctcp_freeargs,
80*7f2fe78bSCy Schubert 	svctcp_destroy
81*7f2fe78bSCy Schubert };
82*7f2fe78bSCy Schubert 
83*7f2fe78bSCy Schubert /*
84*7f2fe78bSCy Schubert  * Ops vector for TCP/IP rendezvous handler
85*7f2fe78bSCy Schubert  */
86*7f2fe78bSCy Schubert static bool_t		rendezvous_request(SVCXPRT *, struct rpc_msg *);
87*7f2fe78bSCy Schubert static bool_t		abortx(void);
88*7f2fe78bSCy Schubert static bool_t		abortx_getargs(SVCXPRT *, xdrproc_t, void *);
89*7f2fe78bSCy Schubert static bool_t		abortx_reply(SVCXPRT *, struct rpc_msg *);
90*7f2fe78bSCy Schubert static bool_t		abortx_freeargs(SVCXPRT *, xdrproc_t, void *);
91*7f2fe78bSCy Schubert static enum xprt_stat	rendezvous_stat(SVCXPRT *);
92*7f2fe78bSCy Schubert 
93*7f2fe78bSCy Schubert static struct xp_ops svctcp_rendezvous_op = {
94*7f2fe78bSCy Schubert 	rendezvous_request,
95*7f2fe78bSCy Schubert 	rendezvous_stat,
96*7f2fe78bSCy Schubert 	abortx_getargs,
97*7f2fe78bSCy Schubert 	abortx_reply,
98*7f2fe78bSCy Schubert 	abortx_freeargs,
99*7f2fe78bSCy Schubert 	svctcp_destroy
100*7f2fe78bSCy Schubert };
101*7f2fe78bSCy Schubert 
102*7f2fe78bSCy Schubert static int readtcp(char *, caddr_t, int), writetcp(char *, caddr_t, int);
103*7f2fe78bSCy Schubert static SVCXPRT *makefd_xprt(int, u_int, u_int);
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert struct tcp_rendezvous { /* kept in xprt->xp_p1 */
106*7f2fe78bSCy Schubert 	u_int sendsize;
107*7f2fe78bSCy Schubert 	u_int recvsize;
108*7f2fe78bSCy Schubert };
109*7f2fe78bSCy Schubert 
110*7f2fe78bSCy Schubert struct tcp_conn {  /* kept in xprt->xp_p1 */
111*7f2fe78bSCy Schubert 	enum xprt_stat strm_stat;
112*7f2fe78bSCy Schubert 	uint32_t x_id;
113*7f2fe78bSCy Schubert 	XDR xdrs;
114*7f2fe78bSCy Schubert 	char verf_body[MAX_AUTH_BYTES];
115*7f2fe78bSCy Schubert };
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert /*
118*7f2fe78bSCy Schubert  * Usage:
119*7f2fe78bSCy Schubert  *	xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
120*7f2fe78bSCy Schubert  *
121*7f2fe78bSCy Schubert  * Creates, registers, and returns a (rpc) tcp based transporter.
122*7f2fe78bSCy Schubert  * Once *xprt is initialized, it is registered as a transporter
123*7f2fe78bSCy Schubert  * see (svc.h, xprt_register).  This routine returns
124*7f2fe78bSCy Schubert  * a NULL if a problem occurred.
125*7f2fe78bSCy Schubert  *
126*7f2fe78bSCy Schubert  * If sock<0 then a socket is created, else sock is used.
127*7f2fe78bSCy Schubert  * If the socket, sock is not bound to a port then svctcp_create
128*7f2fe78bSCy Schubert  * binds it to an arbitrary port.  The routine then starts a tcp
129*7f2fe78bSCy Schubert  * listener on the socket's associated port.  In any (successful) case,
130*7f2fe78bSCy Schubert  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
131*7f2fe78bSCy Schubert  * associated port number.
132*7f2fe78bSCy Schubert  *
133*7f2fe78bSCy Schubert  * Since tcp streams do buffered io similar to stdio, the caller can specify
134*7f2fe78bSCy Schubert  * how big the send and receive buffers are via the second and third parms;
135*7f2fe78bSCy Schubert  * 0 => use the system default.
136*7f2fe78bSCy Schubert  */
137*7f2fe78bSCy Schubert SVCXPRT *
svctcp_create(SOCKET sock,u_int sendsize,u_int recvsize)138*7f2fe78bSCy Schubert svctcp_create(
139*7f2fe78bSCy Schubert         SOCKET sock,
140*7f2fe78bSCy Schubert 	u_int sendsize,
141*7f2fe78bSCy Schubert 	u_int recvsize)
142*7f2fe78bSCy Schubert {
143*7f2fe78bSCy Schubert 	bool_t madesock = FALSE;
144*7f2fe78bSCy Schubert 	SVCXPRT *xprt;
145*7f2fe78bSCy Schubert 	struct tcp_rendezvous *r;
146*7f2fe78bSCy Schubert 	struct sockaddr_storage ss;
147*7f2fe78bSCy Schubert 	struct sockaddr *sa = (struct sockaddr *)&ss;
148*7f2fe78bSCy Schubert 	socklen_t len;
149*7f2fe78bSCy Schubert 
150*7f2fe78bSCy Schubert 	if (sock == RPC_ANYSOCK) {
151*7f2fe78bSCy Schubert 		if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
152*7f2fe78bSCy Schubert 			perror("svctcp_.c - udp socket creation problem");
153*7f2fe78bSCy Schubert 			return ((SVCXPRT *)NULL);
154*7f2fe78bSCy Schubert 		}
155*7f2fe78bSCy Schubert 		set_cloexec_fd(sock);
156*7f2fe78bSCy Schubert 		madesock = TRUE;
157*7f2fe78bSCy Schubert 		memset(&ss, 0, sizeof(ss));
158*7f2fe78bSCy Schubert 		sa->sa_family = AF_INET;
159*7f2fe78bSCy Schubert 	} else {
160*7f2fe78bSCy Schubert 		len = sizeof(struct sockaddr_storage);
161*7f2fe78bSCy Schubert 		if (getsockname(sock, sa, &len) != 0) {
162*7f2fe78bSCy Schubert 			perror("svc_tcp.c - cannot getsockname");
163*7f2fe78bSCy Schubert 			return ((SVCXPRT *)NULL);
164*7f2fe78bSCy Schubert 		}
165*7f2fe78bSCy Schubert 	}
166*7f2fe78bSCy Schubert 
167*7f2fe78bSCy Schubert 	if (bindresvport_sa(sock, sa)) {
168*7f2fe78bSCy Schubert 		sa_setport(sa, 0);
169*7f2fe78bSCy Schubert 		(void)bind(sock, sa, sa_socklen(sa));
170*7f2fe78bSCy Schubert 	}
171*7f2fe78bSCy Schubert 	len = sizeof(struct sockaddr_storage);
172*7f2fe78bSCy Schubert 	if (getsockname(sock, sa, &len) != 0) {
173*7f2fe78bSCy Schubert 		perror("svc_tcp.c - cannot getsockname");
174*7f2fe78bSCy Schubert                 if (madesock)
175*7f2fe78bSCy Schubert                         (void)closesocket(sock);
176*7f2fe78bSCy Schubert 		return ((SVCXPRT *)NULL);
177*7f2fe78bSCy Schubert 	}
178*7f2fe78bSCy Schubert 	if (listen(sock, 2) != 0) {
179*7f2fe78bSCy Schubert 		perror("svctcp_.c - cannot listen");
180*7f2fe78bSCy Schubert                 if (madesock)
181*7f2fe78bSCy Schubert                         (void)closesocket(sock);
182*7f2fe78bSCy Schubert 		return ((SVCXPRT *)NULL);
183*7f2fe78bSCy Schubert 	}
184*7f2fe78bSCy Schubert 	r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
185*7f2fe78bSCy Schubert 	if (r == NULL) {
186*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "svctcp_create: out of memory\n");
187*7f2fe78bSCy Schubert 		return (NULL);
188*7f2fe78bSCy Schubert 	}
189*7f2fe78bSCy Schubert 	r->sendsize = sendsize;
190*7f2fe78bSCy Schubert 	r->recvsize = recvsize;
191*7f2fe78bSCy Schubert 	xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
192*7f2fe78bSCy Schubert 	if (xprt == NULL) {
193*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "svctcp_create: out of memory\n");
194*7f2fe78bSCy Schubert 		return (NULL);
195*7f2fe78bSCy Schubert 	}
196*7f2fe78bSCy Schubert 	xprt->xp_p2 = NULL;
197*7f2fe78bSCy Schubert 	xprt->xp_p1 = (caddr_t)r;
198*7f2fe78bSCy Schubert 	xprt->xp_auth = NULL;
199*7f2fe78bSCy Schubert 	xprt->xp_verf = gssrpc__null_auth;
200*7f2fe78bSCy Schubert 	xprt->xp_ops = &svctcp_rendezvous_op;
201*7f2fe78bSCy Schubert 	xprt->xp_port = sa_getport(sa);
202*7f2fe78bSCy Schubert 	xprt->xp_sock = sock;
203*7f2fe78bSCy Schubert 	xprt->xp_laddrlen = 0;
204*7f2fe78bSCy Schubert 	xprt_register(xprt);
205*7f2fe78bSCy Schubert 	return (xprt);
206*7f2fe78bSCy Schubert }
207*7f2fe78bSCy Schubert 
208*7f2fe78bSCy Schubert /*
209*7f2fe78bSCy Schubert  * Like svtcp_create(), except the routine takes any *open* UNIX file
210*7f2fe78bSCy Schubert  * descriptor as its first input.
211*7f2fe78bSCy Schubert  */
212*7f2fe78bSCy Schubert SVCXPRT *
svcfd_create(int fd,u_int sendsize,u_int recvsize)213*7f2fe78bSCy Schubert svcfd_create(
214*7f2fe78bSCy Schubert 	int fd,
215*7f2fe78bSCy Schubert 	u_int sendsize,
216*7f2fe78bSCy Schubert 	u_int recvsize)
217*7f2fe78bSCy Schubert {
218*7f2fe78bSCy Schubert 
219*7f2fe78bSCy Schubert 	return (makefd_xprt(fd, sendsize, recvsize));
220*7f2fe78bSCy Schubert }
221*7f2fe78bSCy Schubert 
222*7f2fe78bSCy Schubert static SVCXPRT *
makefd_xprt(int fd,u_int sendsize,u_int recvsize)223*7f2fe78bSCy Schubert makefd_xprt(
224*7f2fe78bSCy Schubert 	int fd,
225*7f2fe78bSCy Schubert 	u_int sendsize,
226*7f2fe78bSCy Schubert 	u_int recvsize)
227*7f2fe78bSCy Schubert {
228*7f2fe78bSCy Schubert 	SVCXPRT *xprt;
229*7f2fe78bSCy Schubert 	struct tcp_conn *cd;
230*7f2fe78bSCy Schubert 
231*7f2fe78bSCy Schubert #ifdef FD_SETSIZE
232*7f2fe78bSCy Schubert 	if (fd >= FD_SETSIZE) {
233*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "svc_tcp: makefd_xprt: fd too high\n");
234*7f2fe78bSCy Schubert 		xprt = NULL;
235*7f2fe78bSCy Schubert 		goto done;
236*7f2fe78bSCy Schubert 	}
237*7f2fe78bSCy Schubert #else
238*7f2fe78bSCy Schubert 	if (fd >= NOFILE) {
239*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "svc_tcp: makefd_xprt: fd too high\n");
240*7f2fe78bSCy Schubert 		xprt = NULL;
241*7f2fe78bSCy Schubert 		goto done;
242*7f2fe78bSCy Schubert 	}
243*7f2fe78bSCy Schubert #endif
244*7f2fe78bSCy Schubert 	xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
245*7f2fe78bSCy Schubert 	if (xprt == (SVCXPRT *)NULL) {
246*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
247*7f2fe78bSCy Schubert 		goto done;
248*7f2fe78bSCy Schubert 	}
249*7f2fe78bSCy Schubert 	cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
250*7f2fe78bSCy Schubert 	if (cd == (struct tcp_conn *)NULL) {
251*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
252*7f2fe78bSCy Schubert 		mem_free((char *) xprt, sizeof(SVCXPRT));
253*7f2fe78bSCy Schubert 		xprt = (SVCXPRT *)NULL;
254*7f2fe78bSCy Schubert 		goto done;
255*7f2fe78bSCy Schubert 	}
256*7f2fe78bSCy Schubert 	cd->strm_stat = XPRT_IDLE;
257*7f2fe78bSCy Schubert 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
258*7f2fe78bSCy Schubert 	    (caddr_t)xprt, readtcp, writetcp);
259*7f2fe78bSCy Schubert 	xprt->xp_p2 = NULL;
260*7f2fe78bSCy Schubert 	xprt->xp_p1 = (caddr_t)cd;
261*7f2fe78bSCy Schubert 	xprt->xp_auth = NULL;
262*7f2fe78bSCy Schubert 	xprt->xp_verf.oa_base = cd->verf_body;
263*7f2fe78bSCy Schubert 	xprt->xp_addrlen = 0;
264*7f2fe78bSCy Schubert 	xprt->xp_laddrlen = 0;
265*7f2fe78bSCy Schubert 	xprt->xp_ops = &svctcp_op;  /* truly deals with calls */
266*7f2fe78bSCy Schubert 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
267*7f2fe78bSCy Schubert 	xprt->xp_sock = fd;
268*7f2fe78bSCy Schubert 	xprt_register(xprt);
269*7f2fe78bSCy Schubert     done:
270*7f2fe78bSCy Schubert 	return (xprt);
271*7f2fe78bSCy Schubert }
272*7f2fe78bSCy Schubert 
273*7f2fe78bSCy Schubert static bool_t
rendezvous_request(SVCXPRT * xprt,struct rpc_msg * msg)274*7f2fe78bSCy Schubert rendezvous_request(
275*7f2fe78bSCy Schubert 	SVCXPRT *xprt,
276*7f2fe78bSCy Schubert 	struct rpc_msg *msg)
277*7f2fe78bSCy Schubert {
278*7f2fe78bSCy Schubert         SOCKET sock;
279*7f2fe78bSCy Schubert 	struct tcp_rendezvous *r;
280*7f2fe78bSCy Schubert 	struct sockaddr_in addr, laddr;
281*7f2fe78bSCy Schubert 	socklen_t len, llen;
282*7f2fe78bSCy Schubert 
283*7f2fe78bSCy Schubert 	r = (struct tcp_rendezvous *)xprt->xp_p1;
284*7f2fe78bSCy Schubert     again:
285*7f2fe78bSCy Schubert 	len = llen = sizeof(struct sockaddr_in);
286*7f2fe78bSCy Schubert 	if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
287*7f2fe78bSCy Schubert 	    &len)) < 0) {
288*7f2fe78bSCy Schubert 		if (errno == EINTR)
289*7f2fe78bSCy Schubert 			goto again;
290*7f2fe78bSCy Schubert 	       return (FALSE);
291*7f2fe78bSCy Schubert 	}
292*7f2fe78bSCy Schubert 	set_cloexec_fd(sock);
293*7f2fe78bSCy Schubert 	if (getsockname(sock, (struct sockaddr *) &laddr, &llen) < 0)
294*7f2fe78bSCy Schubert 	     return (FALSE);
295*7f2fe78bSCy Schubert 
296*7f2fe78bSCy Schubert 	/*
297*7f2fe78bSCy Schubert 	 * make a new transporter (re-uses xprt)
298*7f2fe78bSCy Schubert 	 */
299*7f2fe78bSCy Schubert 	xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
300*7f2fe78bSCy Schubert 	if (xprt == NULL) {
301*7f2fe78bSCy Schubert                 (void)closesocket(sock);
302*7f2fe78bSCy Schubert 		return (FALSE);
303*7f2fe78bSCy Schubert 	}
304*7f2fe78bSCy Schubert 	xprt->xp_raddr = addr;
305*7f2fe78bSCy Schubert 	xprt->xp_addrlen = len;
306*7f2fe78bSCy Schubert 	xprt->xp_laddr = laddr;
307*7f2fe78bSCy Schubert 	xprt->xp_laddrlen = llen;
308*7f2fe78bSCy Schubert 	return (FALSE); /* there is never an rpc msg to be processed */
309*7f2fe78bSCy Schubert }
310*7f2fe78bSCy Schubert 
311*7f2fe78bSCy Schubert static enum xprt_stat
rendezvous_stat(SVCXPRT * xprt)312*7f2fe78bSCy Schubert rendezvous_stat(SVCXPRT *xprt)
313*7f2fe78bSCy Schubert {
314*7f2fe78bSCy Schubert 
315*7f2fe78bSCy Schubert 	return (XPRT_IDLE);
316*7f2fe78bSCy Schubert }
317*7f2fe78bSCy Schubert 
318*7f2fe78bSCy Schubert static void
svctcp_destroy(SVCXPRT * xprt)319*7f2fe78bSCy Schubert svctcp_destroy(SVCXPRT *xprt)
320*7f2fe78bSCy Schubert {
321*7f2fe78bSCy Schubert 	struct tcp_conn *cd = xprt->xp_p1;
322*7f2fe78bSCy Schubert 
323*7f2fe78bSCy Schubert 	xprt_unregister(xprt);
324*7f2fe78bSCy Schubert         (void)closesocket(xprt->xp_sock);
325*7f2fe78bSCy Schubert 	if (xprt->xp_port != 0) {
326*7f2fe78bSCy Schubert 		/* a rendezvouser socket */
327*7f2fe78bSCy Schubert 		xprt->xp_port = 0;
328*7f2fe78bSCy Schubert 	} else {
329*7f2fe78bSCy Schubert 		/* an actual connection socket */
330*7f2fe78bSCy Schubert 		XDR_DESTROY(&(cd->xdrs));
331*7f2fe78bSCy Schubert 	}
332*7f2fe78bSCy Schubert 	if (xprt->xp_auth != NULL) {
333*7f2fe78bSCy Schubert 		SVCAUTH_DESTROY(xprt->xp_auth);
334*7f2fe78bSCy Schubert 		xprt->xp_auth = NULL;
335*7f2fe78bSCy Schubert 	}
336*7f2fe78bSCy Schubert 	mem_free((caddr_t)cd, sizeof(struct tcp_conn));
337*7f2fe78bSCy Schubert 	mem_free((caddr_t)xprt, sizeof(SVCXPRT));
338*7f2fe78bSCy Schubert }
339*7f2fe78bSCy Schubert 
340*7f2fe78bSCy Schubert /*
341*7f2fe78bSCy Schubert  * All read operations timeout after 35 seconds.
342*7f2fe78bSCy Schubert  * A timeout is fatal for the connection.
343*7f2fe78bSCy Schubert  */
344*7f2fe78bSCy Schubert static struct timeval wait_per_try = { 35, 0 };
345*7f2fe78bSCy Schubert 
346*7f2fe78bSCy Schubert /*
347*7f2fe78bSCy Schubert  * reads data from the tcp connection.
348*7f2fe78bSCy Schubert  * any error is fatal and the connection is closed.
349*7f2fe78bSCy Schubert  * (And a read of zero bytes is a half closed stream => error.)
350*7f2fe78bSCy Schubert  */
351*7f2fe78bSCy Schubert static int
readtcp(char * xprtptr,caddr_t buf,int len)352*7f2fe78bSCy Schubert readtcp(
353*7f2fe78bSCy Schubert         char *xprtptr,
354*7f2fe78bSCy Schubert 	caddr_t buf,
355*7f2fe78bSCy Schubert 	int len)
356*7f2fe78bSCy Schubert {
357*7f2fe78bSCy Schubert 	SVCXPRT *xprt = (void *)xprtptr;
358*7f2fe78bSCy Schubert 	int sock = xprt->xp_sock;
359*7f2fe78bSCy Schubert 	struct timeval tout;
360*7f2fe78bSCy Schubert #ifdef FD_SETSIZE
361*7f2fe78bSCy Schubert 	fd_set mask;
362*7f2fe78bSCy Schubert 	fd_set readfds;
363*7f2fe78bSCy Schubert 
364*7f2fe78bSCy Schubert 	FD_ZERO(&mask);
365*7f2fe78bSCy Schubert 	FD_SET(sock, &mask);
366*7f2fe78bSCy Schubert #else
367*7f2fe78bSCy Schubert 	int mask = 1 << sock;
368*7f2fe78bSCy Schubert 	int readfds;
369*7f2fe78bSCy Schubert #endif /* def FD_SETSIZE */
370*7f2fe78bSCy Schubert #ifdef FD_SETSIZE
371*7f2fe78bSCy Schubert #define loopcond (!FD_ISSET(sock, &readfds))
372*7f2fe78bSCy Schubert #else
373*7f2fe78bSCy Schubert #define loopcond (readfds != mask)
374*7f2fe78bSCy Schubert #endif
375*7f2fe78bSCy Schubert 	do {
376*7f2fe78bSCy Schubert 		readfds = mask;
377*7f2fe78bSCy Schubert 		tout = wait_per_try;
378*7f2fe78bSCy Schubert 		if (select(sock + 1, &readfds, (fd_set*)NULL,
379*7f2fe78bSCy Schubert 			   (fd_set*)NULL, &tout) <= 0) {
380*7f2fe78bSCy Schubert 			if (errno == EINTR) {
381*7f2fe78bSCy Schubert 				continue;
382*7f2fe78bSCy Schubert 			}
383*7f2fe78bSCy Schubert 			goto fatal_err;
384*7f2fe78bSCy Schubert 		}
385*7f2fe78bSCy Schubert 	} while (loopcond);
386*7f2fe78bSCy Schubert 	if ((len = read(sock, buf, (size_t) len)) > 0) {
387*7f2fe78bSCy Schubert 		return (len);
388*7f2fe78bSCy Schubert 	}
389*7f2fe78bSCy Schubert fatal_err:
390*7f2fe78bSCy Schubert 	((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
391*7f2fe78bSCy Schubert 	return (-1);
392*7f2fe78bSCy Schubert }
393*7f2fe78bSCy Schubert 
394*7f2fe78bSCy Schubert /*
395*7f2fe78bSCy Schubert  * writes data to the tcp connection.
396*7f2fe78bSCy Schubert  * Any error is fatal and the connection is closed.
397*7f2fe78bSCy Schubert  */
398*7f2fe78bSCy Schubert static int
writetcp(char * xprtptr,caddr_t buf,int len)399*7f2fe78bSCy Schubert writetcp(
400*7f2fe78bSCy Schubert 	char *xprtptr,
401*7f2fe78bSCy Schubert 	caddr_t buf,
402*7f2fe78bSCy Schubert 	int len)
403*7f2fe78bSCy Schubert {
404*7f2fe78bSCy Schubert 	SVCXPRT *xprt = (void *)xprtptr;
405*7f2fe78bSCy Schubert 	int i, cnt;
406*7f2fe78bSCy Schubert 
407*7f2fe78bSCy Schubert 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
408*7f2fe78bSCy Schubert 		if ((i = write(xprt->xp_sock, buf, (size_t) cnt)) < 0) {
409*7f2fe78bSCy Schubert 			((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
410*7f2fe78bSCy Schubert 			    XPRT_DIED;
411*7f2fe78bSCy Schubert 			return (-1);
412*7f2fe78bSCy Schubert 		}
413*7f2fe78bSCy Schubert 	}
414*7f2fe78bSCy Schubert 	return (len);
415*7f2fe78bSCy Schubert }
416*7f2fe78bSCy Schubert 
417*7f2fe78bSCy Schubert static enum xprt_stat
svctcp_stat(SVCXPRT * xprt)418*7f2fe78bSCy Schubert svctcp_stat(SVCXPRT *xprt)
419*7f2fe78bSCy Schubert {
420*7f2fe78bSCy Schubert 	struct tcp_conn *cd = xprt->xp_p1;
421*7f2fe78bSCy Schubert 
422*7f2fe78bSCy Schubert 	if (cd->strm_stat == XPRT_DIED)
423*7f2fe78bSCy Schubert 		return (XPRT_DIED);
424*7f2fe78bSCy Schubert 	if (! xdrrec_eof(&(cd->xdrs)))
425*7f2fe78bSCy Schubert 		return (XPRT_MOREREQS);
426*7f2fe78bSCy Schubert 	return (XPRT_IDLE);
427*7f2fe78bSCy Schubert }
428*7f2fe78bSCy Schubert 
429*7f2fe78bSCy Schubert static bool_t
svctcp_recv(SVCXPRT * xprt,struct rpc_msg * msg)430*7f2fe78bSCy Schubert svctcp_recv(
431*7f2fe78bSCy Schubert 	SVCXPRT *xprt,
432*7f2fe78bSCy Schubert 	struct rpc_msg *msg)
433*7f2fe78bSCy Schubert {
434*7f2fe78bSCy Schubert 	struct tcp_conn *cd = xprt->xp_p1;
435*7f2fe78bSCy Schubert 	XDR *xdrs = &cd->xdrs;
436*7f2fe78bSCy Schubert 
437*7f2fe78bSCy Schubert 	xdrs->x_op = XDR_DECODE;
438*7f2fe78bSCy Schubert 	(void)xdrrec_skiprecord(xdrs);
439*7f2fe78bSCy Schubert 	if (xdr_callmsg(xdrs, msg)) {
440*7f2fe78bSCy Schubert 		cd->x_id = msg->rm_xid;
441*7f2fe78bSCy Schubert 		return (TRUE);
442*7f2fe78bSCy Schubert 	}
443*7f2fe78bSCy Schubert 	return (FALSE);
444*7f2fe78bSCy Schubert }
445*7f2fe78bSCy Schubert 
446*7f2fe78bSCy Schubert static bool_t
svctcp_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)447*7f2fe78bSCy Schubert svctcp_getargs(
448*7f2fe78bSCy Schubert 	SVCXPRT *xprt,
449*7f2fe78bSCy Schubert 	xdrproc_t xdr_args,
450*7f2fe78bSCy Schubert 	void *args_ptr)
451*7f2fe78bSCy Schubert {
452*7f2fe78bSCy Schubert 	if (! SVCAUTH_UNWRAP(xprt->xp_auth,
453*7f2fe78bSCy Schubert 			     &(((struct tcp_conn *)(xprt->xp_p1))->xdrs),
454*7f2fe78bSCy Schubert 			     xdr_args, args_ptr)) {
455*7f2fe78bSCy Schubert 		(void)svctcp_freeargs(xprt, xdr_args, args_ptr);
456*7f2fe78bSCy Schubert 		return FALSE;
457*7f2fe78bSCy Schubert 	}
458*7f2fe78bSCy Schubert 	return TRUE;
459*7f2fe78bSCy Schubert }
460*7f2fe78bSCy Schubert 
461*7f2fe78bSCy Schubert static bool_t
svctcp_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)462*7f2fe78bSCy Schubert svctcp_freeargs(
463*7f2fe78bSCy Schubert 	SVCXPRT *xprt,
464*7f2fe78bSCy Schubert 	xdrproc_t xdr_args,
465*7f2fe78bSCy Schubert 	void * args_ptr)
466*7f2fe78bSCy Schubert {
467*7f2fe78bSCy Schubert 	XDR *xdrs = &((struct tcp_conn *)(xprt->xp_p1))->xdrs;
468*7f2fe78bSCy Schubert 
469*7f2fe78bSCy Schubert 	xdrs->x_op = XDR_FREE;
470*7f2fe78bSCy Schubert 	return ((*xdr_args)(xdrs, args_ptr));
471*7f2fe78bSCy Schubert }
472*7f2fe78bSCy Schubert 
svctcp_reply(SVCXPRT * xprt,struct rpc_msg * msg)473*7f2fe78bSCy Schubert static bool_t svctcp_reply(
474*7f2fe78bSCy Schubert 	SVCXPRT *xprt,
475*7f2fe78bSCy Schubert 	struct rpc_msg *msg)
476*7f2fe78bSCy Schubert {
477*7f2fe78bSCy Schubert 	struct tcp_conn *cd = xprt->xp_p1;
478*7f2fe78bSCy Schubert 	XDR *xdrs = &cd->xdrs;
479*7f2fe78bSCy Schubert 	bool_t stat;
480*7f2fe78bSCy Schubert 
481*7f2fe78bSCy Schubert 	xdrproc_t xdr_results = NULL;
482*7f2fe78bSCy Schubert 	caddr_t xdr_location = 0;
483*7f2fe78bSCy Schubert 	bool_t has_args;
484*7f2fe78bSCy Schubert 
485*7f2fe78bSCy Schubert 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
486*7f2fe78bSCy Schubert 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
487*7f2fe78bSCy Schubert 		has_args = TRUE;
488*7f2fe78bSCy Schubert 		xdr_results = msg->acpted_rply.ar_results.proc;
489*7f2fe78bSCy Schubert 		xdr_location = msg->acpted_rply.ar_results.where;
490*7f2fe78bSCy Schubert 
491*7f2fe78bSCy Schubert 		msg->acpted_rply.ar_results.proc = xdr_void;
492*7f2fe78bSCy Schubert 		msg->acpted_rply.ar_results.where = NULL;
493*7f2fe78bSCy Schubert 	} else
494*7f2fe78bSCy Schubert 		has_args = FALSE;
495*7f2fe78bSCy Schubert 
496*7f2fe78bSCy Schubert 	xdrs->x_op = XDR_ENCODE;
497*7f2fe78bSCy Schubert 	msg->rm_xid = cd->x_id;
498*7f2fe78bSCy Schubert 	stat = FALSE;
499*7f2fe78bSCy Schubert 	if (xdr_replymsg(xdrs, msg) &&
500*7f2fe78bSCy Schubert 	    (!has_args ||
501*7f2fe78bSCy Schubert 	     (SVCAUTH_WRAP(xprt->xp_auth, xdrs, xdr_results, xdr_location)))) {
502*7f2fe78bSCy Schubert 		stat = TRUE;
503*7f2fe78bSCy Schubert 	}
504*7f2fe78bSCy Schubert 	(void)xdrrec_endofrecord(xdrs, TRUE);
505*7f2fe78bSCy Schubert 	return (stat);
506*7f2fe78bSCy Schubert }
507*7f2fe78bSCy Schubert 
abortx(void)508*7f2fe78bSCy Schubert static bool_t abortx(void)
509*7f2fe78bSCy Schubert {
510*7f2fe78bSCy Schubert 	abort();
511*7f2fe78bSCy Schubert 	return 1;
512*7f2fe78bSCy Schubert }
513*7f2fe78bSCy Schubert 
abortx_getargs(SVCXPRT * xprt,xdrproc_t proc,void * info)514*7f2fe78bSCy Schubert static bool_t abortx_getargs(
515*7f2fe78bSCy Schubert 	SVCXPRT *xprt,
516*7f2fe78bSCy Schubert 	xdrproc_t proc,
517*7f2fe78bSCy Schubert 	void *info)
518*7f2fe78bSCy Schubert {
519*7f2fe78bSCy Schubert 	return abortx();
520*7f2fe78bSCy Schubert }
521*7f2fe78bSCy Schubert 
abortx_reply(SVCXPRT * xprt,struct rpc_msg * msg)522*7f2fe78bSCy Schubert static bool_t abortx_reply(SVCXPRT *xprt, struct rpc_msg *msg)
523*7f2fe78bSCy Schubert {
524*7f2fe78bSCy Schubert 	return abortx();
525*7f2fe78bSCy Schubert }
526*7f2fe78bSCy Schubert 
abortx_freeargs(SVCXPRT * xprt,xdrproc_t proc,void * info)527*7f2fe78bSCy Schubert static bool_t abortx_freeargs(
528*7f2fe78bSCy Schubert 	SVCXPRT *xprt, xdrproc_t proc,
529*7f2fe78bSCy Schubert 	void * info)
530*7f2fe78bSCy Schubert {
531*7f2fe78bSCy Schubert 	return abortx();
532*7f2fe78bSCy Schubert }
533