xref: /freebsd/lib/libc/rpc/svc_dg.c (revision 2e322d379631c3bd6107e295f3e5d814c7845fc7)
18360efbdSAlfred Perlstein /*	$NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $	*/
28360efbdSAlfred Perlstein 
3*2e322d37SHiroki Sato /*-
4*2e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
5*2e322d37SHiroki Sato  * All rights reserved.
68360efbdSAlfred Perlstein  *
7*2e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
8*2e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
9*2e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
10*2e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
11*2e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
12*2e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
13*2e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
14*2e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15*2e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
16*2e322d37SHiroki Sato  *   from this software without specific prior written permission.
178360efbdSAlfred Perlstein  *
18*2e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*2e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*2e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*2e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*2e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*2e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*2e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*2e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*2e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
298360efbdSAlfred Perlstein  */
308360efbdSAlfred Perlstein 
318360efbdSAlfred Perlstein /*
328360efbdSAlfred Perlstein  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
338360efbdSAlfred Perlstein  */
348360efbdSAlfred Perlstein 
35a986ef57SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
36a986ef57SDavid E. O'Brien #ident	"@(#)svc_dg.c	1.17	94/04/24 SMI"
37a986ef57SDavid E. O'Brien #endif
38d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
39d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
408360efbdSAlfred Perlstein 
418360efbdSAlfred Perlstein /*
428360efbdSAlfred Perlstein  * svc_dg.c, Server side for connectionless RPC.
438360efbdSAlfred Perlstein  *
448360efbdSAlfred Perlstein  * Does some caching in the hopes of achieving execute-at-most-once semantics.
458360efbdSAlfred Perlstein  */
468360efbdSAlfred Perlstein 
478360efbdSAlfred Perlstein #include "namespace.h"
489f5afc13SIan Dowse #include "reentrant.h"
498360efbdSAlfred Perlstein #include <sys/types.h>
508360efbdSAlfred Perlstein #include <sys/socket.h>
518360efbdSAlfred Perlstein #include <rpc/rpc.h>
528360efbdSAlfred Perlstein #include <rpc/svc_dg.h>
538f55a568SDoug Rabson #include <assert.h>
548360efbdSAlfred Perlstein #include <errno.h>
558360efbdSAlfred Perlstein #include <unistd.h>
568360efbdSAlfred Perlstein #include <stdio.h>
578360efbdSAlfred Perlstein #include <stdlib.h>
588360efbdSAlfred Perlstein #include <string.h>
598360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
608360efbdSAlfred Perlstein #include <netconfig.h>
618360efbdSAlfred Perlstein #include <netdir.h>
628360efbdSAlfred Perlstein #endif
638360efbdSAlfred Perlstein #include <err.h>
648360efbdSAlfred Perlstein #include "un-namespace.h"
658360efbdSAlfred Perlstein 
668360efbdSAlfred Perlstein #include "rpc_com.h"
67235baf26SDaniel Eischen #include "mt_misc.h"
688360efbdSAlfred Perlstein 
698360efbdSAlfred Perlstein #define	su_data(xprt)	((struct svc_dg_data *)(xprt->xp_p2))
708360efbdSAlfred Perlstein #define	rpc_buffer(xprt) ((xprt)->xp_p1)
718360efbdSAlfred Perlstein 
728360efbdSAlfred Perlstein #ifndef MAX
738360efbdSAlfred Perlstein #define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
748360efbdSAlfred Perlstein #endif
758360efbdSAlfred Perlstein 
76c05ac53bSDavid E. O'Brien static void svc_dg_ops(SVCXPRT *);
77c05ac53bSDavid E. O'Brien static enum xprt_stat svc_dg_stat(SVCXPRT *);
78c05ac53bSDavid E. O'Brien static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *);
79c05ac53bSDavid E. O'Brien static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *);
80f249dbccSDag-Erling Smørgrav static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *);
81f249dbccSDag-Erling Smørgrav static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *);
82c05ac53bSDavid E. O'Brien static void svc_dg_destroy(SVCXPRT *);
83c05ac53bSDavid E. O'Brien static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
84c05ac53bSDavid E. O'Brien static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *);
85c05ac53bSDavid E. O'Brien static void cache_set(SVCXPRT *, size_t);
86c05ac53bSDavid E. O'Brien int svc_dg_enablecache(SVCXPRT *, u_int);
878360efbdSAlfred Perlstein 
888360efbdSAlfred Perlstein /*
898360efbdSAlfred Perlstein  * Usage:
908360efbdSAlfred Perlstein  *	xprt = svc_dg_create(sock, sendsize, recvsize);
918360efbdSAlfred Perlstein  * Does other connectionless specific initializations.
928360efbdSAlfred Perlstein  * Once *xprt is initialized, it is registered.
938360efbdSAlfred Perlstein  * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
948360efbdSAlfred Perlstein  * system defaults are chosen.
958360efbdSAlfred Perlstein  * The routines returns NULL if a problem occurred.
968360efbdSAlfred Perlstein  */
978360efbdSAlfred Perlstein static const char svc_dg_str[] = "svc_dg_create: %s";
988360efbdSAlfred Perlstein static const char svc_dg_err1[] = "could not get transport information";
998360efbdSAlfred Perlstein static const char svc_dg_err2[] = "transport does not support data transfer";
10035941cc8SZachary Loafman static const char svc_dg_err3[] = "getsockname failed";
101e7733ffaSAlexander Kabaev static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR";
1028360efbdSAlfred Perlstein static const char __no_mem_str[] = "out of memory";
1038360efbdSAlfred Perlstein 
1048360efbdSAlfred Perlstein SVCXPRT *
1058360efbdSAlfred Perlstein svc_dg_create(fd, sendsize, recvsize)
1068360efbdSAlfred Perlstein 	int fd;
1078360efbdSAlfred Perlstein 	u_int sendsize;
1088360efbdSAlfred Perlstein 	u_int recvsize;
1098360efbdSAlfred Perlstein {
1108360efbdSAlfred Perlstein 	SVCXPRT *xprt;
1118360efbdSAlfred Perlstein 	struct svc_dg_data *su = NULL;
1128360efbdSAlfred Perlstein 	struct __rpc_sockinfo si;
1138360efbdSAlfred Perlstein 	struct sockaddr_storage ss;
1148360efbdSAlfred Perlstein 	socklen_t slen;
1158360efbdSAlfred Perlstein 
1168360efbdSAlfred Perlstein 	if (!__rpc_fd2sockinfo(fd, &si)) {
1178360efbdSAlfred Perlstein 		warnx(svc_dg_str, svc_dg_err1);
1188360efbdSAlfred Perlstein 		return (NULL);
1198360efbdSAlfred Perlstein 	}
1208360efbdSAlfred Perlstein 	/*
1218360efbdSAlfred Perlstein 	 * Find the receive and the send size
1228360efbdSAlfred Perlstein 	 */
1238360efbdSAlfred Perlstein 	sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
1248360efbdSAlfred Perlstein 	recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
1258360efbdSAlfred Perlstein 	if ((sendsize == 0) || (recvsize == 0)) {
1268360efbdSAlfred Perlstein 		warnx(svc_dg_str, svc_dg_err2);
1278360efbdSAlfred Perlstein 		return (NULL);
1288360efbdSAlfred Perlstein 	}
1298360efbdSAlfred Perlstein 
1308f55a568SDoug Rabson 	xprt = svc_xprt_alloc();
1318360efbdSAlfred Perlstein 	if (xprt == NULL)
1328360efbdSAlfred Perlstein 		goto freedata;
1338360efbdSAlfred Perlstein 
1348360efbdSAlfred Perlstein 	su = mem_alloc(sizeof (*su));
1358360efbdSAlfred Perlstein 	if (su == NULL)
1368360efbdSAlfred Perlstein 		goto freedata;
1378360efbdSAlfred Perlstein 	su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
1388360efbdSAlfred Perlstein 	if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
1398360efbdSAlfred Perlstein 		goto freedata;
1408360efbdSAlfred Perlstein 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
1418360efbdSAlfred Perlstein 		XDR_DECODE);
1428360efbdSAlfred Perlstein 	su->su_cache = NULL;
1438360efbdSAlfred Perlstein 	xprt->xp_fd = fd;
144f249dbccSDag-Erling Smørgrav 	xprt->xp_p2 = su;
1458360efbdSAlfred Perlstein 	xprt->xp_verf.oa_base = su->su_verfbody;
1468360efbdSAlfred Perlstein 	svc_dg_ops(xprt);
1478360efbdSAlfred Perlstein 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
1488360efbdSAlfred Perlstein 
1498360efbdSAlfred Perlstein 	slen = sizeof ss;
15035941cc8SZachary Loafman 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
15135941cc8SZachary Loafman 		warnx(svc_dg_str, svc_dg_err3);
15235941cc8SZachary Loafman 		goto freedata_nowarn;
15335941cc8SZachary Loafman 	}
1548360efbdSAlfred Perlstein 	xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
1558360efbdSAlfred Perlstein 	xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
1568360efbdSAlfred Perlstein 	xprt->xp_ltaddr.len = slen;
1578360efbdSAlfred Perlstein 	memcpy(xprt->xp_ltaddr.buf, &ss, slen);
1588360efbdSAlfred Perlstein 
159e7733ffaSAlexander Kabaev 	if (ss.ss_family == AF_INET) {
160e7733ffaSAlexander Kabaev 		struct sockaddr_in *sin;
161e7733ffaSAlexander Kabaev 		static const int true_value = 1;
162e7733ffaSAlexander Kabaev 
163e7733ffaSAlexander Kabaev 		sin = (struct sockaddr_in *)(void *)&ss;
164e7733ffaSAlexander Kabaev 		if (sin->sin_addr.s_addr == INADDR_ANY) {
165e7733ffaSAlexander Kabaev 		    su->su_srcaddr.buf = mem_alloc(sizeof (ss));
166e7733ffaSAlexander Kabaev 		    su->su_srcaddr.maxlen = sizeof (ss);
167e7733ffaSAlexander Kabaev 
168e7733ffaSAlexander Kabaev 		    if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR,
169e7733ffaSAlexander Kabaev 				    &true_value, sizeof(true_value))) {
170e7733ffaSAlexander Kabaev 			    warnx(svc_dg_str,  svc_dg_err4);
171e7733ffaSAlexander Kabaev 			    goto freedata_nowarn;
172e7733ffaSAlexander Kabaev 		    }
173e7733ffaSAlexander Kabaev 		}
174e7733ffaSAlexander Kabaev 	}
175e7733ffaSAlexander Kabaev 
1768360efbdSAlfred Perlstein 	xprt_register(xprt);
1778360efbdSAlfred Perlstein 	return (xprt);
1788360efbdSAlfred Perlstein freedata:
1798360efbdSAlfred Perlstein 	(void) warnx(svc_dg_str, __no_mem_str);
18035941cc8SZachary Loafman freedata_nowarn:
1818360efbdSAlfred Perlstein 	if (xprt) {
1828360efbdSAlfred Perlstein 		if (su)
1838360efbdSAlfred Perlstein 			(void) mem_free(su, sizeof (*su));
1848f55a568SDoug Rabson 		svc_xprt_free(xprt);
1858360efbdSAlfred Perlstein 	}
1868360efbdSAlfred Perlstein 	return (NULL);
1878360efbdSAlfred Perlstein }
1888360efbdSAlfred Perlstein 
1898360efbdSAlfred Perlstein /*ARGSUSED*/
1908360efbdSAlfred Perlstein static enum xprt_stat
1918360efbdSAlfred Perlstein svc_dg_stat(xprt)
1928360efbdSAlfred Perlstein 	SVCXPRT *xprt;
1938360efbdSAlfred Perlstein {
1948360efbdSAlfred Perlstein 	return (XPRT_IDLE);
1958360efbdSAlfred Perlstein }
1968360efbdSAlfred Perlstein 
19735941cc8SZachary Loafman static int
19835941cc8SZachary Loafman svc_dg_recvfrom(int fd, char *buf, int buflen,
19935941cc8SZachary Loafman     struct sockaddr *raddr, socklen_t *raddrlen,
20035941cc8SZachary Loafman     struct sockaddr *laddr, socklen_t *laddrlen)
20135941cc8SZachary Loafman {
20235941cc8SZachary Loafman 	struct msghdr msg;
20335941cc8SZachary Loafman 	struct iovec msg_iov[1];
20435941cc8SZachary Loafman 	struct sockaddr_in *lin = (struct sockaddr_in *)laddr;
20535941cc8SZachary Loafman 	int rlen;
20635941cc8SZachary Loafman 	bool_t have_lin = FALSE;
20735941cc8SZachary Loafman 	char tmp[CMSG_LEN(sizeof(*lin))];
20835941cc8SZachary Loafman 	struct cmsghdr *cmsg;
20935941cc8SZachary Loafman 
21035941cc8SZachary Loafman 	memset((char *)&msg, 0, sizeof(msg));
21135941cc8SZachary Loafman 	msg_iov[0].iov_base = buf;
21235941cc8SZachary Loafman 	msg_iov[0].iov_len = buflen;
21335941cc8SZachary Loafman 	msg.msg_iov = msg_iov;
21435941cc8SZachary Loafman 	msg.msg_iovlen = 1;
21535941cc8SZachary Loafman 	msg.msg_namelen = *raddrlen;
21635941cc8SZachary Loafman 	msg.msg_name = (char *)raddr;
217e7733ffaSAlexander Kabaev 	if (laddr != NULL) {
21835941cc8SZachary Loafman 	    msg.msg_control = (caddr_t)tmp;
21935941cc8SZachary Loafman 	    msg.msg_controllen = CMSG_LEN(sizeof(*lin));
220e7733ffaSAlexander Kabaev 	}
22135941cc8SZachary Loafman 	rlen = _recvmsg(fd, &msg, 0);
22235941cc8SZachary Loafman 	if (rlen >= 0)
22335941cc8SZachary Loafman 		*raddrlen = msg.msg_namelen;
22435941cc8SZachary Loafman 
225e7733ffaSAlexander Kabaev 	if (rlen == -1 || laddr == NULL ||
22635941cc8SZachary Loafman 	    msg.msg_controllen < sizeof(struct cmsghdr) ||
22735941cc8SZachary Loafman 	    msg.msg_flags & MSG_CTRUNC)
22835941cc8SZachary Loafman 		return rlen;
22935941cc8SZachary Loafman 
23035941cc8SZachary Loafman 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
23135941cc8SZachary Loafman 	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
23235941cc8SZachary Loafman 		if (cmsg->cmsg_level == IPPROTO_IP &&
23335941cc8SZachary Loafman 		    cmsg->cmsg_type == IP_RECVDSTADDR) {
23435941cc8SZachary Loafman 			have_lin = TRUE;
23535941cc8SZachary Loafman 			memcpy(&lin->sin_addr,
236e7733ffaSAlexander Kabaev 			    (struct in_addr *)CMSG_DATA(cmsg),
237e7733ffaSAlexander Kabaev 			    sizeof(struct in_addr));
23835941cc8SZachary Loafman 			break;
23935941cc8SZachary Loafman 		}
24035941cc8SZachary Loafman 	}
24135941cc8SZachary Loafman 
24235941cc8SZachary Loafman 	lin->sin_family = AF_INET;
24335941cc8SZachary Loafman 	lin->sin_port = 0;
24435941cc8SZachary Loafman 	*laddrlen = sizeof(struct sockaddr_in);
24535941cc8SZachary Loafman 
246e7733ffaSAlexander Kabaev 	if (!have_lin)
247e7733ffaSAlexander Kabaev 		lin->sin_addr.s_addr = INADDR_ANY;
248e7733ffaSAlexander Kabaev 
24935941cc8SZachary Loafman 	return rlen;
25035941cc8SZachary Loafman }
25135941cc8SZachary Loafman 
2528360efbdSAlfred Perlstein static bool_t
2538360efbdSAlfred Perlstein svc_dg_recv(xprt, msg)
2548360efbdSAlfred Perlstein 	SVCXPRT *xprt;
2558360efbdSAlfred Perlstein 	struct rpc_msg *msg;
2568360efbdSAlfred Perlstein {
2578360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
2588360efbdSAlfred Perlstein 	XDR *xdrs = &(su->su_xdrs);
2598360efbdSAlfred Perlstein 	char *reply;
2608360efbdSAlfred Perlstein 	struct sockaddr_storage ss;
2618360efbdSAlfred Perlstein 	socklen_t alen;
2628360efbdSAlfred Perlstein 	size_t replylen;
2632e26eba4SMartin Blapp 	ssize_t rlen;
2648360efbdSAlfred Perlstein 
2658360efbdSAlfred Perlstein again:
2668360efbdSAlfred Perlstein 	alen = sizeof (struct sockaddr_storage);
26735941cc8SZachary Loafman 	rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz,
26835941cc8SZachary Loafman 	    (struct sockaddr *)(void *)&ss, &alen,
269e7733ffaSAlexander Kabaev 	    (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len);
2708360efbdSAlfred Perlstein 	if (rlen == -1 && errno == EINTR)
2718360efbdSAlfred Perlstein 		goto again;
2722e26eba4SMartin Blapp 	if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
2738360efbdSAlfred Perlstein 		return (FALSE);
2748360efbdSAlfred Perlstein 	if (xprt->xp_rtaddr.len < alen) {
2758360efbdSAlfred Perlstein 		if (xprt->xp_rtaddr.len != 0)
2768360efbdSAlfred Perlstein 			mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
2778360efbdSAlfred Perlstein 		xprt->xp_rtaddr.buf = mem_alloc(alen);
2788360efbdSAlfred Perlstein 		xprt->xp_rtaddr.len = alen;
2798360efbdSAlfred Perlstein 	}
2808360efbdSAlfred Perlstein 	memcpy(xprt->xp_rtaddr.buf, &ss, alen);
2818360efbdSAlfred Perlstein #ifdef PORTMAP
2828360efbdSAlfred Perlstein 	if (ss.ss_family == AF_INET) {
2838360efbdSAlfred Perlstein 		xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
2848360efbdSAlfred Perlstein 		xprt->xp_addrlen = sizeof (struct sockaddr_in);
2858360efbdSAlfred Perlstein 	}
2868360efbdSAlfred Perlstein #endif				/* PORTMAP */
2878360efbdSAlfred Perlstein 	xdrs->x_op = XDR_DECODE;
2888360efbdSAlfred Perlstein 	XDR_SETPOS(xdrs, 0);
2898360efbdSAlfred Perlstein 	if (! xdr_callmsg(xdrs, msg)) {
2908360efbdSAlfred Perlstein 		return (FALSE);
2918360efbdSAlfred Perlstein 	}
2928360efbdSAlfred Perlstein 	su->su_xid = msg->rm_xid;
2938360efbdSAlfred Perlstein 	if (su->su_cache != NULL) {
2948360efbdSAlfred Perlstein 		if (cache_get(xprt, msg, &reply, &replylen)) {
2958360efbdSAlfred Perlstein 			(void)_sendto(xprt->xp_fd, reply, replylen, 0,
2968360efbdSAlfred Perlstein 			    (struct sockaddr *)(void *)&ss, alen);
2978360efbdSAlfred Perlstein 			return (FALSE);
2988360efbdSAlfred Perlstein 		}
2998360efbdSAlfred Perlstein 	}
3008360efbdSAlfred Perlstein 	return (TRUE);
3018360efbdSAlfred Perlstein }
3028360efbdSAlfred Perlstein 
30335941cc8SZachary Loafman static int
30435941cc8SZachary Loafman svc_dg_sendto(int fd, char *buf, int buflen,
30535941cc8SZachary Loafman     const struct sockaddr *raddr, socklen_t raddrlen,
30635941cc8SZachary Loafman     const struct sockaddr *laddr, socklen_t laddrlen)
30735941cc8SZachary Loafman {
30835941cc8SZachary Loafman 	struct msghdr msg;
30935941cc8SZachary Loafman 	struct iovec msg_iov[1];
31035941cc8SZachary Loafman 	struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr;
31135941cc8SZachary Loafman 	struct in_addr *lin = &laddr_in->sin_addr;
31235941cc8SZachary Loafman 	char tmp[CMSG_SPACE(sizeof(*lin))];
31335941cc8SZachary Loafman 	struct cmsghdr *cmsg;
31435941cc8SZachary Loafman 
31535941cc8SZachary Loafman 	memset((char *)&msg, 0, sizeof(msg));
31635941cc8SZachary Loafman 	msg_iov[0].iov_base = buf;
31735941cc8SZachary Loafman 	msg_iov[0].iov_len = buflen;
31835941cc8SZachary Loafman 	msg.msg_iov = msg_iov;
31935941cc8SZachary Loafman 	msg.msg_iovlen = 1;
32035941cc8SZachary Loafman 	msg.msg_namelen = raddrlen;
32135941cc8SZachary Loafman 	msg.msg_name = (char *)raddr;
32235941cc8SZachary Loafman 
323e7733ffaSAlexander Kabaev 	if (laddr != NULL && laddr->sa_family == AF_INET &&
324e7733ffaSAlexander Kabaev 	    lin->s_addr != INADDR_ANY) {
32535941cc8SZachary Loafman 		msg.msg_control = (caddr_t)tmp;
32635941cc8SZachary Loafman 		msg.msg_controllen = CMSG_LEN(sizeof(*lin));
32735941cc8SZachary Loafman 		cmsg = CMSG_FIRSTHDR(&msg);
32835941cc8SZachary Loafman 		cmsg->cmsg_len = CMSG_LEN(sizeof(*lin));
32935941cc8SZachary Loafman 		cmsg->cmsg_level = IPPROTO_IP;
33035941cc8SZachary Loafman 		cmsg->cmsg_type = IP_SENDSRCADDR;
33135941cc8SZachary Loafman 		memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin));
33235941cc8SZachary Loafman 	}
33335941cc8SZachary Loafman 
33435941cc8SZachary Loafman 	return _sendmsg(fd, &msg, 0);
33535941cc8SZachary Loafman }
33635941cc8SZachary Loafman 
3378360efbdSAlfred Perlstein static bool_t
3388360efbdSAlfred Perlstein svc_dg_reply(xprt, msg)
3398360efbdSAlfred Perlstein 	SVCXPRT *xprt;
3408360efbdSAlfred Perlstein 	struct rpc_msg *msg;
3418360efbdSAlfred Perlstein {
3428360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
3438360efbdSAlfred Perlstein 	XDR *xdrs = &(su->su_xdrs);
3448f55a568SDoug Rabson 	bool_t stat = TRUE;
3458360efbdSAlfred Perlstein 	size_t slen;
3468f55a568SDoug Rabson 	xdrproc_t xdr_proc;
3478f55a568SDoug Rabson 	caddr_t xdr_where;
3488360efbdSAlfred Perlstein 
3498360efbdSAlfred Perlstein 	xdrs->x_op = XDR_ENCODE;
3508360efbdSAlfred Perlstein 	XDR_SETPOS(xdrs, 0);
3518360efbdSAlfred Perlstein 	msg->rm_xid = su->su_xid;
3528f55a568SDoug Rabson 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
3538f55a568SDoug Rabson 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
3548f55a568SDoug Rabson 		xdr_proc = msg->acpted_rply.ar_results.proc;
3558f55a568SDoug Rabson 		xdr_where = msg->acpted_rply.ar_results.where;
3568f55a568SDoug Rabson 		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
3578f55a568SDoug Rabson 		msg->acpted_rply.ar_results.where = NULL;
3588f55a568SDoug Rabson 
3598f55a568SDoug Rabson 		if (!xdr_replymsg(xdrs, msg) ||
3608f55a568SDoug Rabson 		    !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where))
3618f55a568SDoug Rabson 			stat = FALSE;
3628f55a568SDoug Rabson 	} else {
3638f55a568SDoug Rabson 		stat = xdr_replymsg(xdrs, msg);
3648f55a568SDoug Rabson 	}
3658f55a568SDoug Rabson 	if (stat) {
3668360efbdSAlfred Perlstein 		slen = XDR_GETPOS(xdrs);
36735941cc8SZachary Loafman 		if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen,
3688360efbdSAlfred Perlstein 		    (struct sockaddr *)xprt->xp_rtaddr.buf,
36935941cc8SZachary Loafman 		    (socklen_t)xprt->xp_rtaddr.len,
370e7733ffaSAlexander Kabaev 		    (struct sockaddr *)su->su_srcaddr.buf,
371e7733ffaSAlexander Kabaev 		    (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) {
3728360efbdSAlfred Perlstein 			stat = TRUE;
3738360efbdSAlfred Perlstein 			if (su->su_cache)
3748360efbdSAlfred Perlstein 				cache_set(xprt, slen);
3758360efbdSAlfred Perlstein 		}
3768360efbdSAlfred Perlstein 	}
3778360efbdSAlfred Perlstein 	return (stat);
3788360efbdSAlfred Perlstein }
3798360efbdSAlfred Perlstein 
3808360efbdSAlfred Perlstein static bool_t
3818360efbdSAlfred Perlstein svc_dg_getargs(xprt, xdr_args, args_ptr)
3828360efbdSAlfred Perlstein 	SVCXPRT *xprt;
3838360efbdSAlfred Perlstein 	xdrproc_t xdr_args;
384f249dbccSDag-Erling Smørgrav 	void *args_ptr;
3858360efbdSAlfred Perlstein {
3868f55a568SDoug Rabson 	struct svc_dg_data *su;
3878f55a568SDoug Rabson 
3888f55a568SDoug Rabson 	assert(xprt != NULL);
3898f55a568SDoug Rabson 	su = su_data(xprt);
3908f55a568SDoug Rabson 	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
3918f55a568SDoug Rabson 		&su->su_xdrs, xdr_args, args_ptr));
3928360efbdSAlfred Perlstein }
3938360efbdSAlfred Perlstein 
3948360efbdSAlfred Perlstein static bool_t
3958360efbdSAlfred Perlstein svc_dg_freeargs(xprt, xdr_args, args_ptr)
3968360efbdSAlfred Perlstein 	SVCXPRT *xprt;
3978360efbdSAlfred Perlstein 	xdrproc_t xdr_args;
398f249dbccSDag-Erling Smørgrav 	void *args_ptr;
3998360efbdSAlfred Perlstein {
4008360efbdSAlfred Perlstein 	XDR *xdrs = &(su_data(xprt)->su_xdrs);
4018360efbdSAlfred Perlstein 
4028360efbdSAlfred Perlstein 	xdrs->x_op = XDR_FREE;
4038360efbdSAlfred Perlstein 	return (*xdr_args)(xdrs, args_ptr);
4048360efbdSAlfred Perlstein }
4058360efbdSAlfred Perlstein 
4068360efbdSAlfred Perlstein static void
4078360efbdSAlfred Perlstein svc_dg_destroy(xprt)
4088360efbdSAlfred Perlstein 	SVCXPRT *xprt;
4098360efbdSAlfred Perlstein {
4108360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
4118360efbdSAlfred Perlstein 
4128360efbdSAlfred Perlstein 	xprt_unregister(xprt);
4138360efbdSAlfred Perlstein 	if (xprt->xp_fd != -1)
4148360efbdSAlfred Perlstein 		(void)_close(xprt->xp_fd);
4158360efbdSAlfred Perlstein 	XDR_DESTROY(&(su->su_xdrs));
4168360efbdSAlfred Perlstein 	(void) mem_free(rpc_buffer(xprt), su->su_iosz);
417e7733ffaSAlexander Kabaev 	if (su->su_srcaddr.buf)
418e7733ffaSAlexander Kabaev 		(void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen);
4198360efbdSAlfred Perlstein 	(void) mem_free(su, sizeof (*su));
4208360efbdSAlfred Perlstein 	if (xprt->xp_rtaddr.buf)
4218360efbdSAlfred Perlstein 		(void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
4228360efbdSAlfred Perlstein 	if (xprt->xp_ltaddr.buf)
4238360efbdSAlfred Perlstein 		(void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
4248360efbdSAlfred Perlstein 	if (xprt->xp_tp)
4258360efbdSAlfred Perlstein 		(void) free(xprt->xp_tp);
4268f55a568SDoug Rabson 	svc_xprt_free(xprt);
4278360efbdSAlfred Perlstein }
4288360efbdSAlfred Perlstein 
4298360efbdSAlfred Perlstein static bool_t
4308360efbdSAlfred Perlstein /*ARGSUSED*/
4318360efbdSAlfred Perlstein svc_dg_control(xprt, rq, in)
4328360efbdSAlfred Perlstein 	SVCXPRT *xprt;
4338360efbdSAlfred Perlstein 	const u_int	rq;
4348360efbdSAlfred Perlstein 	void		*in;
4358360efbdSAlfred Perlstein {
4368360efbdSAlfred Perlstein 	return (FALSE);
4378360efbdSAlfred Perlstein }
4388360efbdSAlfred Perlstein 
4398360efbdSAlfred Perlstein static void
4408360efbdSAlfred Perlstein svc_dg_ops(xprt)
4418360efbdSAlfred Perlstein 	SVCXPRT *xprt;
4428360efbdSAlfred Perlstein {
4438360efbdSAlfred Perlstein 	static struct xp_ops ops;
4448360efbdSAlfred Perlstein 	static struct xp_ops2 ops2;
4458360efbdSAlfred Perlstein 
4468360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY ops_lock: ops */
4478360efbdSAlfred Perlstein 
4488360efbdSAlfred Perlstein 	mutex_lock(&ops_lock);
4498360efbdSAlfred Perlstein 	if (ops.xp_recv == NULL) {
4508360efbdSAlfred Perlstein 		ops.xp_recv = svc_dg_recv;
4518360efbdSAlfred Perlstein 		ops.xp_stat = svc_dg_stat;
4528360efbdSAlfred Perlstein 		ops.xp_getargs = svc_dg_getargs;
4538360efbdSAlfred Perlstein 		ops.xp_reply = svc_dg_reply;
4548360efbdSAlfred Perlstein 		ops.xp_freeargs = svc_dg_freeargs;
4558360efbdSAlfred Perlstein 		ops.xp_destroy = svc_dg_destroy;
4568360efbdSAlfred Perlstein 		ops2.xp_control = svc_dg_control;
4578360efbdSAlfred Perlstein 	}
4588360efbdSAlfred Perlstein 	xprt->xp_ops = &ops;
4598360efbdSAlfred Perlstein 	xprt->xp_ops2 = &ops2;
4608360efbdSAlfred Perlstein 	mutex_unlock(&ops_lock);
4618360efbdSAlfred Perlstein }
4628360efbdSAlfred Perlstein 
4638360efbdSAlfred Perlstein /*  The CACHING COMPONENT */
4648360efbdSAlfred Perlstein 
4658360efbdSAlfred Perlstein /*
4668360efbdSAlfred Perlstein  * Could have been a separate file, but some part of it depends upon the
4678360efbdSAlfred Perlstein  * private structure of the client handle.
4688360efbdSAlfred Perlstein  *
4698360efbdSAlfred Perlstein  * Fifo cache for cl server
4708360efbdSAlfred Perlstein  * Copies pointers to reply buffers into fifo cache
4718360efbdSAlfred Perlstein  * Buffers are sent again if retransmissions are detected.
4728360efbdSAlfred Perlstein  */
4738360efbdSAlfred Perlstein 
4748360efbdSAlfred Perlstein #define	SPARSENESS 4	/* 75% sparse */
4758360efbdSAlfred Perlstein 
4768360efbdSAlfred Perlstein #define	ALLOC(type, size)	\
4778360efbdSAlfred Perlstein 	(type *) mem_alloc((sizeof (type) * (size)))
4788360efbdSAlfred Perlstein 
4798360efbdSAlfred Perlstein #define	MEMZERO(addr, type, size)	 \
4808360efbdSAlfred Perlstein 	(void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
4818360efbdSAlfred Perlstein 
4828360efbdSAlfred Perlstein #define	FREE(addr, type, size)	\
4838360efbdSAlfred Perlstein 	mem_free((addr), (sizeof (type) * (size)))
4848360efbdSAlfred Perlstein 
4858360efbdSAlfred Perlstein /*
4868360efbdSAlfred Perlstein  * An entry in the cache
4878360efbdSAlfred Perlstein  */
4888360efbdSAlfred Perlstein typedef struct cache_node *cache_ptr;
4898360efbdSAlfred Perlstein struct cache_node {
4908360efbdSAlfred Perlstein 	/*
4918360efbdSAlfred Perlstein 	 * Index into cache is xid, proc, vers, prog and address
4928360efbdSAlfred Perlstein 	 */
4938360efbdSAlfred Perlstein 	u_int32_t cache_xid;
4948360efbdSAlfred Perlstein 	rpcproc_t cache_proc;
4958360efbdSAlfred Perlstein 	rpcvers_t cache_vers;
4968360efbdSAlfred Perlstein 	rpcprog_t cache_prog;
4978360efbdSAlfred Perlstein 	struct netbuf cache_addr;
4988360efbdSAlfred Perlstein 	/*
4998360efbdSAlfred Perlstein 	 * The cached reply and length
5008360efbdSAlfred Perlstein 	 */
5018360efbdSAlfred Perlstein 	char *cache_reply;
5028360efbdSAlfred Perlstein 	size_t cache_replylen;
5038360efbdSAlfred Perlstein 	/*
5048360efbdSAlfred Perlstein 	 * Next node on the list, if there is a collision
5058360efbdSAlfred Perlstein 	 */
5068360efbdSAlfred Perlstein 	cache_ptr cache_next;
5078360efbdSAlfred Perlstein };
5088360efbdSAlfred Perlstein 
5098360efbdSAlfred Perlstein /*
5108360efbdSAlfred Perlstein  * The entire cache
5118360efbdSAlfred Perlstein  */
5128360efbdSAlfred Perlstein struct cl_cache {
5138360efbdSAlfred Perlstein 	u_int uc_size;		/* size of cache */
5148360efbdSAlfred Perlstein 	cache_ptr *uc_entries;	/* hash table of entries in cache */
5158360efbdSAlfred Perlstein 	cache_ptr *uc_fifo;	/* fifo list of entries in cache */
5168360efbdSAlfred Perlstein 	u_int uc_nextvictim;	/* points to next victim in fifo list */
5178360efbdSAlfred Perlstein 	rpcprog_t uc_prog;	/* saved program number */
5188360efbdSAlfred Perlstein 	rpcvers_t uc_vers;	/* saved version number */
5198360efbdSAlfred Perlstein 	rpcproc_t uc_proc;	/* saved procedure number */
5208360efbdSAlfred Perlstein };
5218360efbdSAlfred Perlstein 
5228360efbdSAlfred Perlstein 
5238360efbdSAlfred Perlstein /*
5248360efbdSAlfred Perlstein  * the hashing function
5258360efbdSAlfred Perlstein  */
5268360efbdSAlfred Perlstein #define	CACHE_LOC(transp, xid)	\
5278360efbdSAlfred Perlstein 	(xid % (SPARSENESS * ((struct cl_cache *) \
5288360efbdSAlfred Perlstein 		su_data(transp)->su_cache)->uc_size))
5298360efbdSAlfred Perlstein 
5308360efbdSAlfred Perlstein /*
5318360efbdSAlfred Perlstein  * Enable use of the cache. Returns 1 on success, 0 on failure.
5328360efbdSAlfred Perlstein  * Note: there is no disable.
5338360efbdSAlfred Perlstein  */
5348360efbdSAlfred Perlstein static const char cache_enable_str[] = "svc_enablecache: %s %s";
5358360efbdSAlfred Perlstein static const char alloc_err[] = "could not allocate cache ";
5368360efbdSAlfred Perlstein static const char enable_err[] = "cache already enabled";
5378360efbdSAlfred Perlstein 
5388360efbdSAlfred Perlstein int
5398360efbdSAlfred Perlstein svc_dg_enablecache(transp, size)
5408360efbdSAlfred Perlstein 	SVCXPRT *transp;
5418360efbdSAlfred Perlstein 	u_int size;
5428360efbdSAlfred Perlstein {
5438360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(transp);
5448360efbdSAlfred Perlstein 	struct cl_cache *uc;
5458360efbdSAlfred Perlstein 
5468360efbdSAlfred Perlstein 	mutex_lock(&dupreq_lock);
5478360efbdSAlfred Perlstein 	if (su->su_cache != NULL) {
5488360efbdSAlfred Perlstein 		(void) warnx(cache_enable_str, enable_err, " ");
5498360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5508360efbdSAlfred Perlstein 		return (0);
5518360efbdSAlfred Perlstein 	}
5528360efbdSAlfred Perlstein 	uc = ALLOC(struct cl_cache, 1);
5538360efbdSAlfred Perlstein 	if (uc == NULL) {
5548360efbdSAlfred Perlstein 		warnx(cache_enable_str, alloc_err, " ");
5558360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5568360efbdSAlfred Perlstein 		return (0);
5578360efbdSAlfred Perlstein 	}
5588360efbdSAlfred Perlstein 	uc->uc_size = size;
5598360efbdSAlfred Perlstein 	uc->uc_nextvictim = 0;
5608360efbdSAlfred Perlstein 	uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
5618360efbdSAlfred Perlstein 	if (uc->uc_entries == NULL) {
5628360efbdSAlfred Perlstein 		warnx(cache_enable_str, alloc_err, "data");
5638360efbdSAlfred Perlstein 		FREE(uc, struct cl_cache, 1);
5648360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5658360efbdSAlfred Perlstein 		return (0);
5668360efbdSAlfred Perlstein 	}
5678360efbdSAlfred Perlstein 	MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
5688360efbdSAlfred Perlstein 	uc->uc_fifo = ALLOC(cache_ptr, size);
5698360efbdSAlfred Perlstein 	if (uc->uc_fifo == NULL) {
5708360efbdSAlfred Perlstein 		warnx(cache_enable_str, alloc_err, "fifo");
5718360efbdSAlfred Perlstein 		FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
5728360efbdSAlfred Perlstein 		FREE(uc, struct cl_cache, 1);
5738360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5748360efbdSAlfred Perlstein 		return (0);
5758360efbdSAlfred Perlstein 	}
5768360efbdSAlfred Perlstein 	MEMZERO(uc->uc_fifo, cache_ptr, size);
5778360efbdSAlfred Perlstein 	su->su_cache = (char *)(void *)uc;
5788360efbdSAlfred Perlstein 	mutex_unlock(&dupreq_lock);
5798360efbdSAlfred Perlstein 	return (1);
5808360efbdSAlfred Perlstein }
5818360efbdSAlfred Perlstein 
5828360efbdSAlfred Perlstein /*
5838360efbdSAlfred Perlstein  * Set an entry in the cache.  It assumes that the uc entry is set from
5848360efbdSAlfred Perlstein  * the earlier call to cache_get() for the same procedure.  This will always
5858360efbdSAlfred Perlstein  * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
5868360efbdSAlfred Perlstein  * by svc_dg_reply().  All this hoopla because the right RPC parameters are
5878360efbdSAlfred Perlstein  * not available at svc_dg_reply time.
5888360efbdSAlfred Perlstein  */
5898360efbdSAlfred Perlstein 
5908360efbdSAlfred Perlstein static const char cache_set_str[] = "cache_set: %s";
5918360efbdSAlfred Perlstein static const char cache_set_err1[] = "victim not found";
5928360efbdSAlfred Perlstein static const char cache_set_err2[] = "victim alloc failed";
5938360efbdSAlfred Perlstein static const char cache_set_err3[] = "could not allocate new rpc buffer";
5948360efbdSAlfred Perlstein 
5958360efbdSAlfred Perlstein static void
5968360efbdSAlfred Perlstein cache_set(xprt, replylen)
5978360efbdSAlfred Perlstein 	SVCXPRT *xprt;
5988360efbdSAlfred Perlstein 	size_t replylen;
5998360efbdSAlfred Perlstein {
6008360efbdSAlfred Perlstein 	cache_ptr victim;
6018360efbdSAlfred Perlstein 	cache_ptr *vicp;
6028360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
6038360efbdSAlfred Perlstein 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
6048360efbdSAlfred Perlstein 	u_int loc;
6058360efbdSAlfred Perlstein 	char *newbuf;
6068360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
6078360efbdSAlfred Perlstein 	struct netconfig *nconf;
6088360efbdSAlfred Perlstein 	char *uaddr;
6098360efbdSAlfred Perlstein #endif
6108360efbdSAlfred Perlstein 
6118360efbdSAlfred Perlstein 	mutex_lock(&dupreq_lock);
6128360efbdSAlfred Perlstein 	/*
6138360efbdSAlfred Perlstein 	 * Find space for the new entry, either by
6148360efbdSAlfred Perlstein 	 * reusing an old entry, or by mallocing a new one
6158360efbdSAlfred Perlstein 	 */
6168360efbdSAlfred Perlstein 	victim = uc->uc_fifo[uc->uc_nextvictim];
6178360efbdSAlfred Perlstein 	if (victim != NULL) {
6188360efbdSAlfred Perlstein 		loc = CACHE_LOC(xprt, victim->cache_xid);
6198360efbdSAlfred Perlstein 		for (vicp = &uc->uc_entries[loc];
6208360efbdSAlfred Perlstein 			*vicp != NULL && *vicp != victim;
6218360efbdSAlfred Perlstein 			vicp = &(*vicp)->cache_next)
6228360efbdSAlfred Perlstein 			;
6238360efbdSAlfred Perlstein 		if (*vicp == NULL) {
6248360efbdSAlfred Perlstein 			warnx(cache_set_str, cache_set_err1);
6258360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
6268360efbdSAlfred Perlstein 			return;
6278360efbdSAlfred Perlstein 		}
6288360efbdSAlfred Perlstein 		*vicp = victim->cache_next;	/* remove from cache */
6298360efbdSAlfred Perlstein 		newbuf = victim->cache_reply;
6308360efbdSAlfred Perlstein 	} else {
6318360efbdSAlfred Perlstein 		victim = ALLOC(struct cache_node, 1);
6328360efbdSAlfred Perlstein 		if (victim == NULL) {
6338360efbdSAlfred Perlstein 			warnx(cache_set_str, cache_set_err2);
6348360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
6358360efbdSAlfred Perlstein 			return;
6368360efbdSAlfred Perlstein 		}
6378360efbdSAlfred Perlstein 		newbuf = mem_alloc(su->su_iosz);
6388360efbdSAlfred Perlstein 		if (newbuf == NULL) {
6398360efbdSAlfred Perlstein 			warnx(cache_set_str, cache_set_err3);
6408360efbdSAlfred Perlstein 			FREE(victim, struct cache_node, 1);
6418360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
6428360efbdSAlfred Perlstein 			return;
6438360efbdSAlfred Perlstein 		}
6448360efbdSAlfred Perlstein 	}
6458360efbdSAlfred Perlstein 
6468360efbdSAlfred Perlstein 	/*
6478360efbdSAlfred Perlstein 	 * Store it away
6488360efbdSAlfred Perlstein 	 */
6498360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
6508360efbdSAlfred Perlstein 	if (nconf = getnetconfigent(xprt->xp_netid)) {
6518360efbdSAlfred Perlstein 		uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
6528360efbdSAlfred Perlstein 		freenetconfigent(nconf);
6538360efbdSAlfred Perlstein 		printf(
6548360efbdSAlfred Perlstein 	"cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
6558360efbdSAlfred Perlstein 			su->su_xid, uc->uc_prog, uc->uc_vers,
6568360efbdSAlfred Perlstein 			uc->uc_proc, uaddr);
6578360efbdSAlfred Perlstein 		free(uaddr);
6588360efbdSAlfred Perlstein 	}
6598360efbdSAlfred Perlstein #endif
6608360efbdSAlfred Perlstein 	victim->cache_replylen = replylen;
6618360efbdSAlfred Perlstein 	victim->cache_reply = rpc_buffer(xprt);
6628360efbdSAlfred Perlstein 	rpc_buffer(xprt) = newbuf;
6638360efbdSAlfred Perlstein 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
6648360efbdSAlfred Perlstein 			su->su_iosz, XDR_ENCODE);
6658360efbdSAlfred Perlstein 	victim->cache_xid = su->su_xid;
6668360efbdSAlfred Perlstein 	victim->cache_proc = uc->uc_proc;
6678360efbdSAlfred Perlstein 	victim->cache_vers = uc->uc_vers;
6688360efbdSAlfred Perlstein 	victim->cache_prog = uc->uc_prog;
6698360efbdSAlfred Perlstein 	victim->cache_addr = xprt->xp_rtaddr;
6708360efbdSAlfred Perlstein 	victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
6718360efbdSAlfred Perlstein 	(void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
6728360efbdSAlfred Perlstein 	    (size_t)xprt->xp_rtaddr.len);
6738360efbdSAlfred Perlstein 	loc = CACHE_LOC(xprt, victim->cache_xid);
6748360efbdSAlfred Perlstein 	victim->cache_next = uc->uc_entries[loc];
6758360efbdSAlfred Perlstein 	uc->uc_entries[loc] = victim;
6768360efbdSAlfred Perlstein 	uc->uc_fifo[uc->uc_nextvictim++] = victim;
6778360efbdSAlfred Perlstein 	uc->uc_nextvictim %= uc->uc_size;
6788360efbdSAlfred Perlstein 	mutex_unlock(&dupreq_lock);
6798360efbdSAlfred Perlstein }
6808360efbdSAlfred Perlstein 
6818360efbdSAlfred Perlstein /*
6828360efbdSAlfred Perlstein  * Try to get an entry from the cache
6838360efbdSAlfred Perlstein  * return 1 if found, 0 if not found and set the stage for cache_set()
6848360efbdSAlfred Perlstein  */
6858360efbdSAlfred Perlstein static int
6868360efbdSAlfred Perlstein cache_get(xprt, msg, replyp, replylenp)
6878360efbdSAlfred Perlstein 	SVCXPRT *xprt;
6888360efbdSAlfred Perlstein 	struct rpc_msg *msg;
6898360efbdSAlfred Perlstein 	char **replyp;
6908360efbdSAlfred Perlstein 	size_t *replylenp;
6918360efbdSAlfred Perlstein {
6928360efbdSAlfred Perlstein 	u_int loc;
6938360efbdSAlfred Perlstein 	cache_ptr ent;
6948360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
6958360efbdSAlfred Perlstein 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
6968360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
6978360efbdSAlfred Perlstein 	struct netconfig *nconf;
6988360efbdSAlfred Perlstein 	char *uaddr;
6998360efbdSAlfred Perlstein #endif
7008360efbdSAlfred Perlstein 
7018360efbdSAlfred Perlstein 	mutex_lock(&dupreq_lock);
7028360efbdSAlfred Perlstein 	loc = CACHE_LOC(xprt, su->su_xid);
7038360efbdSAlfred Perlstein 	for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
7048360efbdSAlfred Perlstein 		if (ent->cache_xid == su->su_xid &&
7058360efbdSAlfred Perlstein 			ent->cache_proc == msg->rm_call.cb_proc &&
7068360efbdSAlfred Perlstein 			ent->cache_vers == msg->rm_call.cb_vers &&
7078360efbdSAlfred Perlstein 			ent->cache_prog == msg->rm_call.cb_prog &&
7088360efbdSAlfred Perlstein 			ent->cache_addr.len == xprt->xp_rtaddr.len &&
7098360efbdSAlfred Perlstein 			(memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
7108360efbdSAlfred Perlstein 				xprt->xp_rtaddr.len) == 0)) {
7118360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
7128360efbdSAlfred Perlstein 			if (nconf = getnetconfigent(xprt->xp_netid)) {
7138360efbdSAlfred Perlstein 				uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
7148360efbdSAlfred Perlstein 				freenetconfigent(nconf);
7158360efbdSAlfred Perlstein 				printf(
7168360efbdSAlfred Perlstein 	"cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
7178360efbdSAlfred Perlstein 					su->su_xid, msg->rm_call.cb_prog,
7188360efbdSAlfred Perlstein 					msg->rm_call.cb_vers,
7198360efbdSAlfred Perlstein 					msg->rm_call.cb_proc, uaddr);
7208360efbdSAlfred Perlstein 				free(uaddr);
7218360efbdSAlfred Perlstein 			}
7228360efbdSAlfred Perlstein #endif
7238360efbdSAlfred Perlstein 			*replyp = ent->cache_reply;
7248360efbdSAlfred Perlstein 			*replylenp = ent->cache_replylen;
7258360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
7268360efbdSAlfred Perlstein 			return (1);
7278360efbdSAlfred Perlstein 		}
7288360efbdSAlfred Perlstein 	}
7298360efbdSAlfred Perlstein 	/*
7308360efbdSAlfred Perlstein 	 * Failed to find entry
7318360efbdSAlfred Perlstein 	 * Remember a few things so we can do a set later
7328360efbdSAlfred Perlstein 	 */
7338360efbdSAlfred Perlstein 	uc->uc_proc = msg->rm_call.cb_proc;
7348360efbdSAlfred Perlstein 	uc->uc_vers = msg->rm_call.cb_vers;
7358360efbdSAlfred Perlstein 	uc->uc_prog = msg->rm_call.cb_prog;
7368360efbdSAlfred Perlstein 	mutex_unlock(&dupreq_lock);
7378360efbdSAlfred Perlstein 	return (0);
7388360efbdSAlfred Perlstein }
739