xref: /freebsd/lib/libc/rpc/svc_dg.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
18360efbdSAlfred Perlstein /*	$NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $	*/
28360efbdSAlfred Perlstein 
32e322d37SHiroki Sato /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
62e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato  * All rights reserved.
88360efbdSAlfred Perlstein  *
92e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
132e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
162e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato  *   from this software without specific prior written permission.
198360efbdSAlfred Perlstein  *
202e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
318360efbdSAlfred Perlstein  */
328360efbdSAlfred Perlstein 
338360efbdSAlfred Perlstein /*
348360efbdSAlfred Perlstein  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
358360efbdSAlfred Perlstein  */
368360efbdSAlfred Perlstein 
378360efbdSAlfred Perlstein /*
388360efbdSAlfred Perlstein  * svc_dg.c, Server side for connectionless RPC.
398360efbdSAlfred Perlstein  *
408360efbdSAlfred Perlstein  * Does some caching in the hopes of achieving execute-at-most-once semantics.
418360efbdSAlfred Perlstein  */
428360efbdSAlfred Perlstein 
438360efbdSAlfred Perlstein #include "namespace.h"
449f5afc13SIan Dowse #include "reentrant.h"
458360efbdSAlfred Perlstein #include <sys/types.h>
468360efbdSAlfred Perlstein #include <sys/socket.h>
478360efbdSAlfred Perlstein #include <rpc/rpc.h>
488360efbdSAlfred Perlstein #include <rpc/svc_dg.h>
498f55a568SDoug Rabson #include <assert.h>
508360efbdSAlfred Perlstein #include <errno.h>
518360efbdSAlfred Perlstein #include <unistd.h>
528360efbdSAlfred Perlstein #include <stdio.h>
538360efbdSAlfred Perlstein #include <stdlib.h>
548360efbdSAlfred Perlstein #include <string.h>
558360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
568360efbdSAlfred Perlstein #include <netconfig.h>
578360efbdSAlfred Perlstein #include <netdir.h>
588360efbdSAlfred Perlstein #endif
598360efbdSAlfred Perlstein #include <err.h>
608360efbdSAlfred Perlstein #include "un-namespace.h"
618360efbdSAlfred Perlstein 
628360efbdSAlfred Perlstein #include "rpc_com.h"
63235baf26SDaniel Eischen #include "mt_misc.h"
648360efbdSAlfred Perlstein 
65*2614eccfSPedro F. Giffuni #define	su_data(xprt)	((struct svc_dg_data *)((xprt)->xp_p2))
668360efbdSAlfred Perlstein #define	rpc_buffer(xprt) ((xprt)->xp_p1)
678360efbdSAlfred Perlstein 
688360efbdSAlfred Perlstein #ifndef MAX
698360efbdSAlfred Perlstein #define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
708360efbdSAlfred Perlstein #endif
718360efbdSAlfred Perlstein 
72c05ac53bSDavid E. O'Brien static void svc_dg_ops(SVCXPRT *);
73c05ac53bSDavid E. O'Brien static enum xprt_stat svc_dg_stat(SVCXPRT *);
74c05ac53bSDavid E. O'Brien static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *);
75c05ac53bSDavid E. O'Brien static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *);
76f249dbccSDag-Erling Smørgrav static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *);
77f249dbccSDag-Erling Smørgrav static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *);
78c05ac53bSDavid E. O'Brien static void svc_dg_destroy(SVCXPRT *);
79c05ac53bSDavid E. O'Brien static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
80c05ac53bSDavid E. O'Brien static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *);
81c05ac53bSDavid E. O'Brien static void cache_set(SVCXPRT *, size_t);
82c05ac53bSDavid E. O'Brien int svc_dg_enablecache(SVCXPRT *, u_int);
838360efbdSAlfred Perlstein 
848360efbdSAlfred Perlstein /*
858360efbdSAlfred Perlstein  * Usage:
868360efbdSAlfred Perlstein  *	xprt = svc_dg_create(sock, sendsize, recvsize);
878360efbdSAlfred Perlstein  * Does other connectionless specific initializations.
888360efbdSAlfred Perlstein  * Once *xprt is initialized, it is registered.
898360efbdSAlfred Perlstein  * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
908360efbdSAlfred Perlstein  * system defaults are chosen.
918360efbdSAlfred Perlstein  * The routines returns NULL if a problem occurred.
928360efbdSAlfred Perlstein  */
938360efbdSAlfred Perlstein static const char svc_dg_str[] = "svc_dg_create: %s";
948360efbdSAlfred Perlstein static const char svc_dg_err1[] = "could not get transport information";
958360efbdSAlfred Perlstein static const char svc_dg_err2[] = "transport does not support data transfer";
9635941cc8SZachary Loafman static const char svc_dg_err3[] = "getsockname failed";
97e7733ffaSAlexander Kabaev static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR";
988360efbdSAlfred Perlstein static const char __no_mem_str[] = "out of memory";
998360efbdSAlfred Perlstein 
1008360efbdSAlfred Perlstein SVCXPRT *
svc_dg_create(int fd,u_int sendsize,u_int recvsize)10168895e38SCraig Rodrigues svc_dg_create(int fd, u_int sendsize, u_int recvsize)
1028360efbdSAlfred Perlstein {
1038360efbdSAlfred Perlstein 	SVCXPRT *xprt;
1048360efbdSAlfred Perlstein 	struct svc_dg_data *su = NULL;
1058360efbdSAlfred Perlstein 	struct __rpc_sockinfo si;
1068360efbdSAlfred Perlstein 	struct sockaddr_storage ss;
1078360efbdSAlfred Perlstein 	socklen_t slen;
1088360efbdSAlfred Perlstein 
1098360efbdSAlfred Perlstein 	if (!__rpc_fd2sockinfo(fd, &si)) {
1108360efbdSAlfred Perlstein 		warnx(svc_dg_str, svc_dg_err1);
1118360efbdSAlfred Perlstein 		return (NULL);
1128360efbdSAlfred Perlstein 	}
1138360efbdSAlfred Perlstein 	/*
1148360efbdSAlfred Perlstein 	 * Find the receive and the send size
1158360efbdSAlfred Perlstein 	 */
1168360efbdSAlfred Perlstein 	sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
1178360efbdSAlfred Perlstein 	recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
1188360efbdSAlfred Perlstein 	if ((sendsize == 0) || (recvsize == 0)) {
1198360efbdSAlfred Perlstein 		warnx(svc_dg_str, svc_dg_err2);
1208360efbdSAlfred Perlstein 		return (NULL);
1218360efbdSAlfred Perlstein 	}
1228360efbdSAlfred Perlstein 
1238f55a568SDoug Rabson 	xprt = svc_xprt_alloc();
1248360efbdSAlfred Perlstein 	if (xprt == NULL)
1258360efbdSAlfred Perlstein 		goto freedata;
1268360efbdSAlfred Perlstein 
1278360efbdSAlfred Perlstein 	su = mem_alloc(sizeof (*su));
1288360efbdSAlfred Perlstein 	if (su == NULL)
1298360efbdSAlfred Perlstein 		goto freedata;
1308360efbdSAlfred Perlstein 	su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
1318360efbdSAlfred Perlstein 	if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
1328360efbdSAlfred Perlstein 		goto freedata;
1338360efbdSAlfred Perlstein 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
1348360efbdSAlfred Perlstein 		XDR_DECODE);
1358360efbdSAlfred Perlstein 	su->su_cache = NULL;
1368360efbdSAlfred Perlstein 	xprt->xp_fd = fd;
137f249dbccSDag-Erling Smørgrav 	xprt->xp_p2 = su;
1388360efbdSAlfred Perlstein 	xprt->xp_verf.oa_base = su->su_verfbody;
1398360efbdSAlfred Perlstein 	svc_dg_ops(xprt);
1408360efbdSAlfred Perlstein 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
1418360efbdSAlfred Perlstein 
1428360efbdSAlfred Perlstein 	slen = sizeof ss;
14335941cc8SZachary Loafman 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
14435941cc8SZachary Loafman 		warnx(svc_dg_str, svc_dg_err3);
14535941cc8SZachary Loafman 		goto freedata_nowarn;
14635941cc8SZachary Loafman 	}
1478360efbdSAlfred Perlstein 	xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
1488360efbdSAlfred Perlstein 	xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
1498360efbdSAlfred Perlstein 	xprt->xp_ltaddr.len = slen;
1508360efbdSAlfred Perlstein 	memcpy(xprt->xp_ltaddr.buf, &ss, slen);
1518360efbdSAlfred Perlstein 
152e7733ffaSAlexander Kabaev 	if (ss.ss_family == AF_INET) {
153e7733ffaSAlexander Kabaev 		struct sockaddr_in *sin;
154e7733ffaSAlexander Kabaev 		static const int true_value = 1;
155e7733ffaSAlexander Kabaev 
156e7733ffaSAlexander Kabaev 		sin = (struct sockaddr_in *)(void *)&ss;
157e7733ffaSAlexander Kabaev 		if (sin->sin_addr.s_addr == INADDR_ANY) {
158e7733ffaSAlexander Kabaev 		    su->su_srcaddr.buf = mem_alloc(sizeof (ss));
159e7733ffaSAlexander Kabaev 		    su->su_srcaddr.maxlen = sizeof (ss);
160e7733ffaSAlexander Kabaev 
161e7733ffaSAlexander Kabaev 		    if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR,
162e7733ffaSAlexander Kabaev 				    &true_value, sizeof(true_value))) {
163e7733ffaSAlexander Kabaev 			    warnx(svc_dg_str,  svc_dg_err4);
164e7733ffaSAlexander Kabaev 			    goto freedata_nowarn;
165e7733ffaSAlexander Kabaev 		    }
166e7733ffaSAlexander Kabaev 		}
167e7733ffaSAlexander Kabaev 	}
168e7733ffaSAlexander Kabaev 
1698360efbdSAlfred Perlstein 	xprt_register(xprt);
1708360efbdSAlfred Perlstein 	return (xprt);
1718360efbdSAlfred Perlstein freedata:
1728360efbdSAlfred Perlstein 	(void) warnx(svc_dg_str, __no_mem_str);
17335941cc8SZachary Loafman freedata_nowarn:
1748360efbdSAlfred Perlstein 	if (xprt) {
1758360efbdSAlfred Perlstein 		if (su)
1768360efbdSAlfred Perlstein 			(void) mem_free(su, sizeof (*su));
1778f55a568SDoug Rabson 		svc_xprt_free(xprt);
1788360efbdSAlfred Perlstein 	}
1798360efbdSAlfred Perlstein 	return (NULL);
1808360efbdSAlfred Perlstein }
1818360efbdSAlfred Perlstein 
1828360efbdSAlfred Perlstein /*ARGSUSED*/
1838360efbdSAlfred Perlstein static enum xprt_stat
svc_dg_stat(SVCXPRT * xprt)18468895e38SCraig Rodrigues svc_dg_stat(SVCXPRT *xprt)
1858360efbdSAlfred Perlstein {
1868360efbdSAlfred Perlstein 	return (XPRT_IDLE);
1878360efbdSAlfred Perlstein }
1888360efbdSAlfred Perlstein 
18935941cc8SZachary Loafman static int
svc_dg_recvfrom(int fd,char * buf,int buflen,struct sockaddr * raddr,socklen_t * raddrlen,struct sockaddr * laddr,socklen_t * laddrlen)19035941cc8SZachary Loafman svc_dg_recvfrom(int fd, char *buf, int buflen,
19135941cc8SZachary Loafman     struct sockaddr *raddr, socklen_t *raddrlen,
19235941cc8SZachary Loafman     struct sockaddr *laddr, socklen_t *laddrlen)
19335941cc8SZachary Loafman {
19435941cc8SZachary Loafman 	struct msghdr msg;
19535941cc8SZachary Loafman 	struct iovec msg_iov[1];
19635941cc8SZachary Loafman 	struct sockaddr_in *lin = (struct sockaddr_in *)laddr;
19735941cc8SZachary Loafman 	int rlen;
19835941cc8SZachary Loafman 	bool_t have_lin = FALSE;
19935941cc8SZachary Loafman 	char tmp[CMSG_LEN(sizeof(*lin))];
20035941cc8SZachary Loafman 	struct cmsghdr *cmsg;
20135941cc8SZachary Loafman 
20235941cc8SZachary Loafman 	memset((char *)&msg, 0, sizeof(msg));
20335941cc8SZachary Loafman 	msg_iov[0].iov_base = buf;
20435941cc8SZachary Loafman 	msg_iov[0].iov_len = buflen;
20535941cc8SZachary Loafman 	msg.msg_iov = msg_iov;
20635941cc8SZachary Loafman 	msg.msg_iovlen = 1;
20735941cc8SZachary Loafman 	msg.msg_namelen = *raddrlen;
20835941cc8SZachary Loafman 	msg.msg_name = (char *)raddr;
209e7733ffaSAlexander Kabaev 	if (laddr != NULL) {
21035941cc8SZachary Loafman 	    msg.msg_control = (caddr_t)tmp;
21135941cc8SZachary Loafman 	    msg.msg_controllen = CMSG_LEN(sizeof(*lin));
212e7733ffaSAlexander Kabaev 	}
21335941cc8SZachary Loafman 	rlen = _recvmsg(fd, &msg, 0);
21435941cc8SZachary Loafman 	if (rlen >= 0)
21535941cc8SZachary Loafman 		*raddrlen = msg.msg_namelen;
21635941cc8SZachary Loafman 
217e7733ffaSAlexander Kabaev 	if (rlen == -1 || laddr == NULL ||
21835941cc8SZachary Loafman 	    msg.msg_controllen < sizeof(struct cmsghdr) ||
21935941cc8SZachary Loafman 	    msg.msg_flags & MSG_CTRUNC)
22035941cc8SZachary Loafman 		return rlen;
22135941cc8SZachary Loafman 
22235941cc8SZachary Loafman 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
22335941cc8SZachary Loafman 	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
22435941cc8SZachary Loafman 		if (cmsg->cmsg_level == IPPROTO_IP &&
22535941cc8SZachary Loafman 		    cmsg->cmsg_type == IP_RECVDSTADDR) {
22635941cc8SZachary Loafman 			have_lin = TRUE;
22735941cc8SZachary Loafman 			memcpy(&lin->sin_addr,
228e7733ffaSAlexander Kabaev 			    (struct in_addr *)CMSG_DATA(cmsg),
229e7733ffaSAlexander Kabaev 			    sizeof(struct in_addr));
23035941cc8SZachary Loafman 			break;
23135941cc8SZachary Loafman 		}
23235941cc8SZachary Loafman 	}
23335941cc8SZachary Loafman 
23435941cc8SZachary Loafman 	lin->sin_family = AF_INET;
23535941cc8SZachary Loafman 	lin->sin_port = 0;
23635941cc8SZachary Loafman 	*laddrlen = sizeof(struct sockaddr_in);
23735941cc8SZachary Loafman 
238e7733ffaSAlexander Kabaev 	if (!have_lin)
239e7733ffaSAlexander Kabaev 		lin->sin_addr.s_addr = INADDR_ANY;
240e7733ffaSAlexander Kabaev 
24135941cc8SZachary Loafman 	return rlen;
24235941cc8SZachary Loafman }
24335941cc8SZachary Loafman 
2448360efbdSAlfred Perlstein static bool_t
svc_dg_recv(SVCXPRT * xprt,struct rpc_msg * msg)24568895e38SCraig Rodrigues svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg)
2468360efbdSAlfred Perlstein {
2478360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
2488360efbdSAlfred Perlstein 	XDR *xdrs = &(su->su_xdrs);
2498360efbdSAlfred Perlstein 	char *reply;
2508360efbdSAlfred Perlstein 	struct sockaddr_storage ss;
2518360efbdSAlfred Perlstein 	socklen_t alen;
2528360efbdSAlfred Perlstein 	size_t replylen;
2532e26eba4SMartin Blapp 	ssize_t rlen;
2548360efbdSAlfred Perlstein 
2558360efbdSAlfred Perlstein again:
2568360efbdSAlfred Perlstein 	alen = sizeof (struct sockaddr_storage);
25735941cc8SZachary Loafman 	rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz,
25835941cc8SZachary Loafman 	    (struct sockaddr *)(void *)&ss, &alen,
259e7733ffaSAlexander Kabaev 	    (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len);
2608360efbdSAlfred Perlstein 	if (rlen == -1 && errno == EINTR)
2618360efbdSAlfred Perlstein 		goto again;
2622e26eba4SMartin Blapp 	if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
2638360efbdSAlfred Perlstein 		return (FALSE);
2648360efbdSAlfred Perlstein 	if (xprt->xp_rtaddr.len < alen) {
2658360efbdSAlfred Perlstein 		if (xprt->xp_rtaddr.len != 0)
2668360efbdSAlfred Perlstein 			mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
2678360efbdSAlfred Perlstein 		xprt->xp_rtaddr.buf = mem_alloc(alen);
2688360efbdSAlfred Perlstein 		xprt->xp_rtaddr.len = alen;
2698360efbdSAlfred Perlstein 	}
2708360efbdSAlfred Perlstein 	memcpy(xprt->xp_rtaddr.buf, &ss, alen);
2718360efbdSAlfred Perlstein #ifdef PORTMAP
2728360efbdSAlfred Perlstein 	if (ss.ss_family == AF_INET) {
2738360efbdSAlfred Perlstein 		xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
2748360efbdSAlfred Perlstein 		xprt->xp_addrlen = sizeof (struct sockaddr_in);
2758360efbdSAlfred Perlstein 	}
2768360efbdSAlfred Perlstein #endif				/* PORTMAP */
2778360efbdSAlfred Perlstein 	xdrs->x_op = XDR_DECODE;
2788360efbdSAlfred Perlstein 	XDR_SETPOS(xdrs, 0);
2798360efbdSAlfred Perlstein 	if (! xdr_callmsg(xdrs, msg)) {
2808360efbdSAlfred Perlstein 		return (FALSE);
2818360efbdSAlfred Perlstein 	}
2828360efbdSAlfred Perlstein 	su->su_xid = msg->rm_xid;
2838360efbdSAlfred Perlstein 	if (su->su_cache != NULL) {
2848360efbdSAlfred Perlstein 		if (cache_get(xprt, msg, &reply, &replylen)) {
2858360efbdSAlfred Perlstein 			(void)_sendto(xprt->xp_fd, reply, replylen, 0,
2868360efbdSAlfred Perlstein 			    (struct sockaddr *)(void *)&ss, alen);
2878360efbdSAlfred Perlstein 			return (FALSE);
2888360efbdSAlfred Perlstein 		}
2898360efbdSAlfred Perlstein 	}
2908360efbdSAlfred Perlstein 	return (TRUE);
2918360efbdSAlfred Perlstein }
2928360efbdSAlfred Perlstein 
29335941cc8SZachary Loafman static int
svc_dg_sendto(int fd,char * buf,int buflen,const struct sockaddr * raddr,socklen_t raddrlen,const struct sockaddr * laddr,socklen_t laddrlen)29435941cc8SZachary Loafman svc_dg_sendto(int fd, char *buf, int buflen,
29535941cc8SZachary Loafman     const struct sockaddr *raddr, socklen_t raddrlen,
29635941cc8SZachary Loafman     const struct sockaddr *laddr, socklen_t laddrlen)
29735941cc8SZachary Loafman {
29835941cc8SZachary Loafman 	struct msghdr msg;
29935941cc8SZachary Loafman 	struct iovec msg_iov[1];
30035941cc8SZachary Loafman 	struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr;
30135941cc8SZachary Loafman 	struct in_addr *lin = &laddr_in->sin_addr;
30235941cc8SZachary Loafman 	char tmp[CMSG_SPACE(sizeof(*lin))];
30335941cc8SZachary Loafman 	struct cmsghdr *cmsg;
30435941cc8SZachary Loafman 
30535941cc8SZachary Loafman 	memset((char *)&msg, 0, sizeof(msg));
30635941cc8SZachary Loafman 	msg_iov[0].iov_base = buf;
30735941cc8SZachary Loafman 	msg_iov[0].iov_len = buflen;
30835941cc8SZachary Loafman 	msg.msg_iov = msg_iov;
30935941cc8SZachary Loafman 	msg.msg_iovlen = 1;
31035941cc8SZachary Loafman 	msg.msg_namelen = raddrlen;
31135941cc8SZachary Loafman 	msg.msg_name = (char *)raddr;
31235941cc8SZachary Loafman 
313e7733ffaSAlexander Kabaev 	if (laddr != NULL && laddr->sa_family == AF_INET &&
314e7733ffaSAlexander Kabaev 	    lin->s_addr != INADDR_ANY) {
31535941cc8SZachary Loafman 		msg.msg_control = (caddr_t)tmp;
31635941cc8SZachary Loafman 		msg.msg_controllen = CMSG_LEN(sizeof(*lin));
31735941cc8SZachary Loafman 		cmsg = CMSG_FIRSTHDR(&msg);
31835941cc8SZachary Loafman 		cmsg->cmsg_len = CMSG_LEN(sizeof(*lin));
31935941cc8SZachary Loafman 		cmsg->cmsg_level = IPPROTO_IP;
32035941cc8SZachary Loafman 		cmsg->cmsg_type = IP_SENDSRCADDR;
32135941cc8SZachary Loafman 		memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin));
32235941cc8SZachary Loafman 	}
32335941cc8SZachary Loafman 
32435941cc8SZachary Loafman 	return _sendmsg(fd, &msg, 0);
32535941cc8SZachary Loafman }
32635941cc8SZachary Loafman 
3278360efbdSAlfred Perlstein static bool_t
svc_dg_reply(SVCXPRT * xprt,struct rpc_msg * msg)32868895e38SCraig Rodrigues svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg)
3298360efbdSAlfred Perlstein {
3308360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
3318360efbdSAlfred Perlstein 	XDR *xdrs = &(su->su_xdrs);
3328f55a568SDoug Rabson 	bool_t stat = TRUE;
3338360efbdSAlfred Perlstein 	size_t slen;
3348f55a568SDoug Rabson 	xdrproc_t xdr_proc;
3358f55a568SDoug Rabson 	caddr_t xdr_where;
3368360efbdSAlfred Perlstein 
3378360efbdSAlfred Perlstein 	xdrs->x_op = XDR_ENCODE;
3388360efbdSAlfred Perlstein 	XDR_SETPOS(xdrs, 0);
3398360efbdSAlfred Perlstein 	msg->rm_xid = su->su_xid;
3408f55a568SDoug Rabson 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
3418f55a568SDoug Rabson 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
3428f55a568SDoug Rabson 		xdr_proc = msg->acpted_rply.ar_results.proc;
3438f55a568SDoug Rabson 		xdr_where = msg->acpted_rply.ar_results.where;
3448f55a568SDoug Rabson 		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
3458f55a568SDoug Rabson 		msg->acpted_rply.ar_results.where = NULL;
3468f55a568SDoug Rabson 
3478f55a568SDoug Rabson 		if (!xdr_replymsg(xdrs, msg) ||
3488f55a568SDoug Rabson 		    !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where))
3498f55a568SDoug Rabson 			stat = FALSE;
3508f55a568SDoug Rabson 	} else {
3518f55a568SDoug Rabson 		stat = xdr_replymsg(xdrs, msg);
3528f55a568SDoug Rabson 	}
3538f55a568SDoug Rabson 	if (stat) {
3548360efbdSAlfred Perlstein 		slen = XDR_GETPOS(xdrs);
35535941cc8SZachary Loafman 		if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen,
3568360efbdSAlfred Perlstein 		    (struct sockaddr *)xprt->xp_rtaddr.buf,
35735941cc8SZachary Loafman 		    (socklen_t)xprt->xp_rtaddr.len,
358e7733ffaSAlexander Kabaev 		    (struct sockaddr *)su->su_srcaddr.buf,
359e7733ffaSAlexander Kabaev 		    (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) {
3608360efbdSAlfred Perlstein 			stat = TRUE;
3618360efbdSAlfred Perlstein 			if (su->su_cache)
3628360efbdSAlfred Perlstein 				cache_set(xprt, slen);
3638360efbdSAlfred Perlstein 		}
3648360efbdSAlfred Perlstein 	}
3658360efbdSAlfred Perlstein 	return (stat);
3668360efbdSAlfred Perlstein }
3678360efbdSAlfred Perlstein 
3688360efbdSAlfred Perlstein static bool_t
svc_dg_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)36968895e38SCraig Rodrigues svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
3708360efbdSAlfred Perlstein {
3718f55a568SDoug Rabson 	struct svc_dg_data *su;
3728f55a568SDoug Rabson 
3738f55a568SDoug Rabson 	assert(xprt != NULL);
3748f55a568SDoug Rabson 	su = su_data(xprt);
3758f55a568SDoug Rabson 	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
3768f55a568SDoug Rabson 		&su->su_xdrs, xdr_args, args_ptr));
3778360efbdSAlfred Perlstein }
3788360efbdSAlfred Perlstein 
3798360efbdSAlfred Perlstein static bool_t
svc_dg_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)38068895e38SCraig Rodrigues svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
3818360efbdSAlfred Perlstein {
3828360efbdSAlfred Perlstein 	XDR *xdrs = &(su_data(xprt)->su_xdrs);
3838360efbdSAlfred Perlstein 
3848360efbdSAlfred Perlstein 	xdrs->x_op = XDR_FREE;
3858360efbdSAlfred Perlstein 	return (*xdr_args)(xdrs, args_ptr);
3868360efbdSAlfred Perlstein }
3878360efbdSAlfred Perlstein 
3888360efbdSAlfred Perlstein static void
svc_dg_destroy(SVCXPRT * xprt)38968895e38SCraig Rodrigues svc_dg_destroy(SVCXPRT *xprt)
3908360efbdSAlfred Perlstein {
3918360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
3928360efbdSAlfred Perlstein 
3938360efbdSAlfred Perlstein 	xprt_unregister(xprt);
3948360efbdSAlfred Perlstein 	if (xprt->xp_fd != -1)
3958360efbdSAlfred Perlstein 		(void)_close(xprt->xp_fd);
3968360efbdSAlfred Perlstein 	XDR_DESTROY(&(su->su_xdrs));
3978360efbdSAlfred Perlstein 	(void) mem_free(rpc_buffer(xprt), su->su_iosz);
398e7733ffaSAlexander Kabaev 	if (su->su_srcaddr.buf)
399e7733ffaSAlexander Kabaev 		(void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen);
4008360efbdSAlfred Perlstein 	(void) mem_free(su, sizeof (*su));
4018360efbdSAlfred Perlstein 	if (xprt->xp_rtaddr.buf)
4028360efbdSAlfred Perlstein 		(void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
4038360efbdSAlfred Perlstein 	if (xprt->xp_ltaddr.buf)
4048360efbdSAlfred Perlstein 		(void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
405c7f7fdd2SEnji Cooper 	free(xprt->xp_tp);
4068f55a568SDoug Rabson 	svc_xprt_free(xprt);
4078360efbdSAlfred Perlstein }
4088360efbdSAlfred Perlstein 
4098360efbdSAlfred Perlstein static bool_t
4108360efbdSAlfred Perlstein /*ARGSUSED*/
svc_dg_control(SVCXPRT * xprt,const u_int rq,void * in)41168895e38SCraig Rodrigues svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in)
4128360efbdSAlfred Perlstein {
4138360efbdSAlfred Perlstein 	return (FALSE);
4148360efbdSAlfred Perlstein }
4158360efbdSAlfred Perlstein 
4168360efbdSAlfred Perlstein static void
svc_dg_ops(SVCXPRT * xprt)41768895e38SCraig Rodrigues svc_dg_ops(SVCXPRT *xprt)
4188360efbdSAlfred Perlstein {
4198360efbdSAlfred Perlstein 	static struct xp_ops ops;
4208360efbdSAlfred Perlstein 	static struct xp_ops2 ops2;
4218360efbdSAlfred Perlstein 
4228360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY ops_lock: ops */
4238360efbdSAlfred Perlstein 
4248360efbdSAlfred Perlstein 	mutex_lock(&ops_lock);
4258360efbdSAlfred Perlstein 	if (ops.xp_recv == NULL) {
4268360efbdSAlfred Perlstein 		ops.xp_recv = svc_dg_recv;
4278360efbdSAlfred Perlstein 		ops.xp_stat = svc_dg_stat;
4288360efbdSAlfred Perlstein 		ops.xp_getargs = svc_dg_getargs;
4298360efbdSAlfred Perlstein 		ops.xp_reply = svc_dg_reply;
4308360efbdSAlfred Perlstein 		ops.xp_freeargs = svc_dg_freeargs;
4318360efbdSAlfred Perlstein 		ops.xp_destroy = svc_dg_destroy;
4328360efbdSAlfred Perlstein 		ops2.xp_control = svc_dg_control;
4338360efbdSAlfred Perlstein 	}
4348360efbdSAlfred Perlstein 	xprt->xp_ops = &ops;
4358360efbdSAlfred Perlstein 	xprt->xp_ops2 = &ops2;
4368360efbdSAlfred Perlstein 	mutex_unlock(&ops_lock);
4378360efbdSAlfred Perlstein }
4388360efbdSAlfred Perlstein 
4398360efbdSAlfred Perlstein /*  The CACHING COMPONENT */
4408360efbdSAlfred Perlstein 
4418360efbdSAlfred Perlstein /*
4428360efbdSAlfred Perlstein  * Could have been a separate file, but some part of it depends upon the
4438360efbdSAlfred Perlstein  * private structure of the client handle.
4448360efbdSAlfred Perlstein  *
4458360efbdSAlfred Perlstein  * Fifo cache for cl server
4468360efbdSAlfred Perlstein  * Copies pointers to reply buffers into fifo cache
4478360efbdSAlfred Perlstein  * Buffers are sent again if retransmissions are detected.
4488360efbdSAlfred Perlstein  */
4498360efbdSAlfred Perlstein 
4508360efbdSAlfred Perlstein #define	SPARSENESS 4	/* 75% sparse */
4518360efbdSAlfred Perlstein 
4528360efbdSAlfred Perlstein #define	ALLOC(type, size)	\
4538360efbdSAlfred Perlstein 	(type *) mem_alloc((sizeof (type) * (size)))
4548360efbdSAlfred Perlstein 
4558360efbdSAlfred Perlstein #define	MEMZERO(addr, type, size)	 \
4568360efbdSAlfred Perlstein 	(void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
4578360efbdSAlfred Perlstein 
4588360efbdSAlfred Perlstein #define	FREE(addr, type, size)	\
4598360efbdSAlfred Perlstein 	mem_free((addr), (sizeof (type) * (size)))
4608360efbdSAlfred Perlstein 
4618360efbdSAlfred Perlstein /*
4628360efbdSAlfred Perlstein  * An entry in the cache
4638360efbdSAlfred Perlstein  */
4648360efbdSAlfred Perlstein typedef struct cache_node *cache_ptr;
4658360efbdSAlfred Perlstein struct cache_node {
4668360efbdSAlfred Perlstein 	/*
4678360efbdSAlfred Perlstein 	 * Index into cache is xid, proc, vers, prog and address
4688360efbdSAlfred Perlstein 	 */
4698360efbdSAlfred Perlstein 	u_int32_t cache_xid;
4708360efbdSAlfred Perlstein 	rpcproc_t cache_proc;
4718360efbdSAlfred Perlstein 	rpcvers_t cache_vers;
4728360efbdSAlfred Perlstein 	rpcprog_t cache_prog;
4738360efbdSAlfred Perlstein 	struct netbuf cache_addr;
4748360efbdSAlfred Perlstein 	/*
4758360efbdSAlfred Perlstein 	 * The cached reply and length
4768360efbdSAlfred Perlstein 	 */
4778360efbdSAlfred Perlstein 	char *cache_reply;
4788360efbdSAlfred Perlstein 	size_t cache_replylen;
4798360efbdSAlfred Perlstein 	/*
4808360efbdSAlfred Perlstein 	 * Next node on the list, if there is a collision
4818360efbdSAlfred Perlstein 	 */
4828360efbdSAlfred Perlstein 	cache_ptr cache_next;
4838360efbdSAlfred Perlstein };
4848360efbdSAlfred Perlstein 
4858360efbdSAlfred Perlstein /*
4868360efbdSAlfred Perlstein  * The entire cache
4878360efbdSAlfred Perlstein  */
4888360efbdSAlfred Perlstein struct cl_cache {
4898360efbdSAlfred Perlstein 	u_int uc_size;		/* size of cache */
4908360efbdSAlfred Perlstein 	cache_ptr *uc_entries;	/* hash table of entries in cache */
4918360efbdSAlfred Perlstein 	cache_ptr *uc_fifo;	/* fifo list of entries in cache */
4928360efbdSAlfred Perlstein 	u_int uc_nextvictim;	/* points to next victim in fifo list */
4938360efbdSAlfred Perlstein 	rpcprog_t uc_prog;	/* saved program number */
4948360efbdSAlfred Perlstein 	rpcvers_t uc_vers;	/* saved version number */
4958360efbdSAlfred Perlstein 	rpcproc_t uc_proc;	/* saved procedure number */
4968360efbdSAlfred Perlstein };
4978360efbdSAlfred Perlstein 
4988360efbdSAlfred Perlstein 
4998360efbdSAlfred Perlstein /*
5008360efbdSAlfred Perlstein  * the hashing function
5018360efbdSAlfred Perlstein  */
5028360efbdSAlfred Perlstein #define	CACHE_LOC(transp, xid)	\
5038360efbdSAlfred Perlstein 	(xid % (SPARSENESS * ((struct cl_cache *) \
5048360efbdSAlfred Perlstein 		su_data(transp)->su_cache)->uc_size))
5058360efbdSAlfred Perlstein 
5068360efbdSAlfred Perlstein /*
5078360efbdSAlfred Perlstein  * Enable use of the cache. Returns 1 on success, 0 on failure.
5088360efbdSAlfred Perlstein  * Note: there is no disable.
5098360efbdSAlfred Perlstein  */
5108360efbdSAlfred Perlstein static const char cache_enable_str[] = "svc_enablecache: %s %s";
5118360efbdSAlfred Perlstein static const char alloc_err[] = "could not allocate cache ";
5128360efbdSAlfred Perlstein static const char enable_err[] = "cache already enabled";
5138360efbdSAlfred Perlstein 
5148360efbdSAlfred Perlstein int
svc_dg_enablecache(SVCXPRT * transp,u_int size)51568895e38SCraig Rodrigues svc_dg_enablecache(SVCXPRT *transp, u_int size)
5168360efbdSAlfred Perlstein {
5178360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(transp);
5188360efbdSAlfred Perlstein 	struct cl_cache *uc;
5198360efbdSAlfred Perlstein 
5208360efbdSAlfred Perlstein 	mutex_lock(&dupreq_lock);
5218360efbdSAlfred Perlstein 	if (su->su_cache != NULL) {
5228360efbdSAlfred Perlstein 		(void) warnx(cache_enable_str, enable_err, " ");
5238360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5248360efbdSAlfred Perlstein 		return (0);
5258360efbdSAlfred Perlstein 	}
5268360efbdSAlfred Perlstein 	uc = ALLOC(struct cl_cache, 1);
5278360efbdSAlfred Perlstein 	if (uc == NULL) {
5288360efbdSAlfred Perlstein 		warnx(cache_enable_str, alloc_err, " ");
5298360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5308360efbdSAlfred Perlstein 		return (0);
5318360efbdSAlfred Perlstein 	}
5328360efbdSAlfred Perlstein 	uc->uc_size = size;
5338360efbdSAlfred Perlstein 	uc->uc_nextvictim = 0;
5348360efbdSAlfred Perlstein 	uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
5358360efbdSAlfred Perlstein 	if (uc->uc_entries == NULL) {
5368360efbdSAlfred Perlstein 		warnx(cache_enable_str, alloc_err, "data");
5378360efbdSAlfred Perlstein 		FREE(uc, struct cl_cache, 1);
5388360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5398360efbdSAlfred Perlstein 		return (0);
5408360efbdSAlfred Perlstein 	}
5418360efbdSAlfred Perlstein 	MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
5428360efbdSAlfred Perlstein 	uc->uc_fifo = ALLOC(cache_ptr, size);
5438360efbdSAlfred Perlstein 	if (uc->uc_fifo == NULL) {
5448360efbdSAlfred Perlstein 		warnx(cache_enable_str, alloc_err, "fifo");
5458360efbdSAlfred Perlstein 		FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
5468360efbdSAlfred Perlstein 		FREE(uc, struct cl_cache, 1);
5478360efbdSAlfred Perlstein 		mutex_unlock(&dupreq_lock);
5488360efbdSAlfred Perlstein 		return (0);
5498360efbdSAlfred Perlstein 	}
5508360efbdSAlfred Perlstein 	MEMZERO(uc->uc_fifo, cache_ptr, size);
5518360efbdSAlfred Perlstein 	su->su_cache = (char *)(void *)uc;
5528360efbdSAlfred Perlstein 	mutex_unlock(&dupreq_lock);
5538360efbdSAlfred Perlstein 	return (1);
5548360efbdSAlfred Perlstein }
5558360efbdSAlfred Perlstein 
5568360efbdSAlfred Perlstein /*
5578360efbdSAlfred Perlstein  * Set an entry in the cache.  It assumes that the uc entry is set from
5588360efbdSAlfred Perlstein  * the earlier call to cache_get() for the same procedure.  This will always
5598360efbdSAlfred Perlstein  * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
5608360efbdSAlfred Perlstein  * by svc_dg_reply().  All this hoopla because the right RPC parameters are
5618360efbdSAlfred Perlstein  * not available at svc_dg_reply time.
5628360efbdSAlfred Perlstein  */
5638360efbdSAlfred Perlstein 
5648360efbdSAlfred Perlstein static const char cache_set_str[] = "cache_set: %s";
5658360efbdSAlfred Perlstein static const char cache_set_err1[] = "victim not found";
5668360efbdSAlfred Perlstein static const char cache_set_err2[] = "victim alloc failed";
5678360efbdSAlfred Perlstein static const char cache_set_err3[] = "could not allocate new rpc buffer";
5688360efbdSAlfred Perlstein 
5698360efbdSAlfred Perlstein static void
cache_set(SVCXPRT * xprt,size_t replylen)57068895e38SCraig Rodrigues cache_set(SVCXPRT *xprt, size_t replylen)
5718360efbdSAlfred Perlstein {
5728360efbdSAlfred Perlstein 	cache_ptr victim;
5738360efbdSAlfred Perlstein 	cache_ptr *vicp;
5748360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
5758360efbdSAlfred Perlstein 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
5768360efbdSAlfred Perlstein 	u_int loc;
5778360efbdSAlfred Perlstein 	char *newbuf;
5788360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
5798360efbdSAlfred Perlstein 	struct netconfig *nconf;
5808360efbdSAlfred Perlstein 	char *uaddr;
5818360efbdSAlfred Perlstein #endif
5828360efbdSAlfred Perlstein 
5838360efbdSAlfred Perlstein 	mutex_lock(&dupreq_lock);
5848360efbdSAlfred Perlstein 	/*
5858360efbdSAlfred Perlstein 	 * Find space for the new entry, either by
5868360efbdSAlfred Perlstein 	 * reusing an old entry, or by mallocing a new one
5878360efbdSAlfred Perlstein 	 */
5888360efbdSAlfred Perlstein 	victim = uc->uc_fifo[uc->uc_nextvictim];
5898360efbdSAlfred Perlstein 	if (victim != NULL) {
5908360efbdSAlfred Perlstein 		loc = CACHE_LOC(xprt, victim->cache_xid);
5918360efbdSAlfred Perlstein 		for (vicp = &uc->uc_entries[loc];
5928360efbdSAlfred Perlstein 			*vicp != NULL && *vicp != victim;
5938360efbdSAlfred Perlstein 			vicp = &(*vicp)->cache_next)
5948360efbdSAlfred Perlstein 			;
5958360efbdSAlfred Perlstein 		if (*vicp == NULL) {
5968360efbdSAlfred Perlstein 			warnx(cache_set_str, cache_set_err1);
5978360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
5988360efbdSAlfred Perlstein 			return;
5998360efbdSAlfred Perlstein 		}
6008360efbdSAlfred Perlstein 		*vicp = victim->cache_next;	/* remove from cache */
6018360efbdSAlfred Perlstein 		newbuf = victim->cache_reply;
6028360efbdSAlfred Perlstein 	} else {
6038360efbdSAlfred Perlstein 		victim = ALLOC(struct cache_node, 1);
6048360efbdSAlfred Perlstein 		if (victim == NULL) {
6058360efbdSAlfred Perlstein 			warnx(cache_set_str, cache_set_err2);
6068360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
6078360efbdSAlfred Perlstein 			return;
6088360efbdSAlfred Perlstein 		}
6098360efbdSAlfred Perlstein 		newbuf = mem_alloc(su->su_iosz);
6108360efbdSAlfred Perlstein 		if (newbuf == NULL) {
6118360efbdSAlfred Perlstein 			warnx(cache_set_str, cache_set_err3);
6128360efbdSAlfred Perlstein 			FREE(victim, struct cache_node, 1);
6138360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
6148360efbdSAlfred Perlstein 			return;
6158360efbdSAlfred Perlstein 		}
6168360efbdSAlfred Perlstein 	}
6178360efbdSAlfred Perlstein 
6188360efbdSAlfred Perlstein 	/*
6198360efbdSAlfred Perlstein 	 * Store it away
6208360efbdSAlfred Perlstein 	 */
6218360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
6228360efbdSAlfred Perlstein 	if (nconf = getnetconfigent(xprt->xp_netid)) {
6238360efbdSAlfred Perlstein 		uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
6248360efbdSAlfred Perlstein 		freenetconfigent(nconf);
6258360efbdSAlfred Perlstein 		printf(
6268360efbdSAlfred Perlstein 	"cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
6278360efbdSAlfred Perlstein 			su->su_xid, uc->uc_prog, uc->uc_vers,
6288360efbdSAlfred Perlstein 			uc->uc_proc, uaddr);
6298360efbdSAlfred Perlstein 		free(uaddr);
6308360efbdSAlfred Perlstein 	}
6318360efbdSAlfred Perlstein #endif
6328360efbdSAlfred Perlstein 	victim->cache_replylen = replylen;
6338360efbdSAlfred Perlstein 	victim->cache_reply = rpc_buffer(xprt);
6348360efbdSAlfred Perlstein 	rpc_buffer(xprt) = newbuf;
6358360efbdSAlfred Perlstein 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
6368360efbdSAlfred Perlstein 			su->su_iosz, XDR_ENCODE);
6378360efbdSAlfred Perlstein 	victim->cache_xid = su->su_xid;
6388360efbdSAlfred Perlstein 	victim->cache_proc = uc->uc_proc;
6398360efbdSAlfred Perlstein 	victim->cache_vers = uc->uc_vers;
6408360efbdSAlfred Perlstein 	victim->cache_prog = uc->uc_prog;
6418360efbdSAlfred Perlstein 	victim->cache_addr = xprt->xp_rtaddr;
6428360efbdSAlfred Perlstein 	victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
6438360efbdSAlfred Perlstein 	(void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
6448360efbdSAlfred Perlstein 	    (size_t)xprt->xp_rtaddr.len);
6458360efbdSAlfred Perlstein 	loc = CACHE_LOC(xprt, victim->cache_xid);
6468360efbdSAlfred Perlstein 	victim->cache_next = uc->uc_entries[loc];
6478360efbdSAlfred Perlstein 	uc->uc_entries[loc] = victim;
6488360efbdSAlfred Perlstein 	uc->uc_fifo[uc->uc_nextvictim++] = victim;
6498360efbdSAlfred Perlstein 	uc->uc_nextvictim %= uc->uc_size;
6508360efbdSAlfred Perlstein 	mutex_unlock(&dupreq_lock);
6518360efbdSAlfred Perlstein }
6528360efbdSAlfred Perlstein 
6538360efbdSAlfred Perlstein /*
6548360efbdSAlfred Perlstein  * Try to get an entry from the cache
6558360efbdSAlfred Perlstein  * return 1 if found, 0 if not found and set the stage for cache_set()
6568360efbdSAlfred Perlstein  */
6578360efbdSAlfred Perlstein static int
cache_get(SVCXPRT * xprt,struct rpc_msg * msg,char ** replyp,size_t * replylenp)65868895e38SCraig Rodrigues cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp, size_t *replylenp)
6598360efbdSAlfred Perlstein {
6608360efbdSAlfred Perlstein 	u_int loc;
6618360efbdSAlfred Perlstein 	cache_ptr ent;
6628360efbdSAlfred Perlstein 	struct svc_dg_data *su = su_data(xprt);
6638360efbdSAlfred Perlstein 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
6648360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
6658360efbdSAlfred Perlstein 	struct netconfig *nconf;
6668360efbdSAlfred Perlstein 	char *uaddr;
6678360efbdSAlfred Perlstein #endif
6688360efbdSAlfred Perlstein 
6698360efbdSAlfred Perlstein 	mutex_lock(&dupreq_lock);
6708360efbdSAlfred Perlstein 	loc = CACHE_LOC(xprt, su->su_xid);
6718360efbdSAlfred Perlstein 	for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
6728360efbdSAlfred Perlstein 		if (ent->cache_xid == su->su_xid &&
6738360efbdSAlfred Perlstein 			ent->cache_proc == msg->rm_call.cb_proc &&
6748360efbdSAlfred Perlstein 			ent->cache_vers == msg->rm_call.cb_vers &&
6758360efbdSAlfred Perlstein 			ent->cache_prog == msg->rm_call.cb_prog &&
6768360efbdSAlfred Perlstein 			ent->cache_addr.len == xprt->xp_rtaddr.len &&
6778360efbdSAlfred Perlstein 			(memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
6788360efbdSAlfred Perlstein 				xprt->xp_rtaddr.len) == 0)) {
6798360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG
6808360efbdSAlfred Perlstein 			if (nconf = getnetconfigent(xprt->xp_netid)) {
6818360efbdSAlfred Perlstein 				uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
6828360efbdSAlfred Perlstein 				freenetconfigent(nconf);
6838360efbdSAlfred Perlstein 				printf(
6848360efbdSAlfred Perlstein 	"cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
6858360efbdSAlfred Perlstein 					su->su_xid, msg->rm_call.cb_prog,
6868360efbdSAlfred Perlstein 					msg->rm_call.cb_vers,
6878360efbdSAlfred Perlstein 					msg->rm_call.cb_proc, uaddr);
6888360efbdSAlfred Perlstein 				free(uaddr);
6898360efbdSAlfred Perlstein 			}
6908360efbdSAlfred Perlstein #endif
6918360efbdSAlfred Perlstein 			*replyp = ent->cache_reply;
6928360efbdSAlfred Perlstein 			*replylenp = ent->cache_replylen;
6938360efbdSAlfred Perlstein 			mutex_unlock(&dupreq_lock);
6948360efbdSAlfred Perlstein 			return (1);
6958360efbdSAlfred Perlstein 		}
6968360efbdSAlfred Perlstein 	}
6978360efbdSAlfred Perlstein 	/*
6988360efbdSAlfred Perlstein 	 * Failed to find entry
6998360efbdSAlfred Perlstein 	 * Remember a few things so we can do a set later
7008360efbdSAlfred Perlstein 	 */
7018360efbdSAlfred Perlstein 	uc->uc_proc = msg->rm_call.cb_proc;
7028360efbdSAlfred Perlstein 	uc->uc_vers = msg->rm_call.cb_vers;
7038360efbdSAlfred Perlstein 	uc->uc_prog = msg->rm_call.cb_prog;
7048360efbdSAlfred Perlstein 	mutex_unlock(&dupreq_lock);
7058360efbdSAlfred Perlstein 	return (0);
7068360efbdSAlfred Perlstein }
707