xref: /freebsd/lib/libc/rpc/svc_dg.c (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
1 /*	$NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15  *   contributors may be used to endorse or promote products derived
16  *   from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident	"@(#)svc_dg.c	1.17	94/04/24 SMI"
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * svc_dg.c, Server side for connectionless RPC.
43  *
44  * Does some caching in the hopes of achieving execute-at-most-once semantics.
45  */
46 
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <rpc/rpc.h>
52 #include <rpc/svc_dg.h>
53 #include <assert.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #ifdef RPC_CACHE_DEBUG
60 #include <netconfig.h>
61 #include <netdir.h>
62 #endif
63 #include <err.h>
64 #include "un-namespace.h"
65 
66 #include "rpc_com.h"
67 #include "mt_misc.h"
68 
69 #define	su_data(xprt)	((struct svc_dg_data *)(xprt->xp_p2))
70 #define	rpc_buffer(xprt) ((xprt)->xp_p1)
71 
72 #ifndef MAX
73 #define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
74 #endif
75 
76 static void svc_dg_ops(SVCXPRT *);
77 static enum xprt_stat svc_dg_stat(SVCXPRT *);
78 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *);
79 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *);
80 static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *);
81 static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *);
82 static void svc_dg_destroy(SVCXPRT *);
83 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
84 static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *);
85 static void cache_set(SVCXPRT *, size_t);
86 int svc_dg_enablecache(SVCXPRT *, u_int);
87 
88 /*
89  * Usage:
90  *	xprt = svc_dg_create(sock, sendsize, recvsize);
91  * Does other connectionless specific initializations.
92  * Once *xprt is initialized, it is registered.
93  * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
94  * system defaults are chosen.
95  * The routines returns NULL if a problem occurred.
96  */
97 static const char svc_dg_str[] = "svc_dg_create: %s";
98 static const char svc_dg_err1[] = "could not get transport information";
99 static const char svc_dg_err2[] = "transport does not support data transfer";
100 static const char svc_dg_err3[] = "getsockname failed";
101 static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR";
102 static const char __no_mem_str[] = "out of memory";
103 
104 SVCXPRT *
105 svc_dg_create(int fd, u_int sendsize, u_int recvsize)
106 {
107 	SVCXPRT *xprt;
108 	struct svc_dg_data *su = NULL;
109 	struct __rpc_sockinfo si;
110 	struct sockaddr_storage ss;
111 	socklen_t slen;
112 
113 	if (!__rpc_fd2sockinfo(fd, &si)) {
114 		warnx(svc_dg_str, svc_dg_err1);
115 		return (NULL);
116 	}
117 	/*
118 	 * Find the receive and the send size
119 	 */
120 	sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
121 	recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
122 	if ((sendsize == 0) || (recvsize == 0)) {
123 		warnx(svc_dg_str, svc_dg_err2);
124 		return (NULL);
125 	}
126 
127 	xprt = svc_xprt_alloc();
128 	if (xprt == NULL)
129 		goto freedata;
130 
131 	su = mem_alloc(sizeof (*su));
132 	if (su == NULL)
133 		goto freedata;
134 	su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
135 	if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
136 		goto freedata;
137 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
138 		XDR_DECODE);
139 	su->su_cache = NULL;
140 	xprt->xp_fd = fd;
141 	xprt->xp_p2 = su;
142 	xprt->xp_verf.oa_base = su->su_verfbody;
143 	svc_dg_ops(xprt);
144 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
145 
146 	slen = sizeof ss;
147 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
148 		warnx(svc_dg_str, svc_dg_err3);
149 		goto freedata_nowarn;
150 	}
151 	xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
152 	xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
153 	xprt->xp_ltaddr.len = slen;
154 	memcpy(xprt->xp_ltaddr.buf, &ss, slen);
155 
156 	if (ss.ss_family == AF_INET) {
157 		struct sockaddr_in *sin;
158 		static const int true_value = 1;
159 
160 		sin = (struct sockaddr_in *)(void *)&ss;
161 		if (sin->sin_addr.s_addr == INADDR_ANY) {
162 		    su->su_srcaddr.buf = mem_alloc(sizeof (ss));
163 		    su->su_srcaddr.maxlen = sizeof (ss);
164 
165 		    if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR,
166 				    &true_value, sizeof(true_value))) {
167 			    warnx(svc_dg_str,  svc_dg_err4);
168 			    goto freedata_nowarn;
169 		    }
170 		}
171 	}
172 
173 	xprt_register(xprt);
174 	return (xprt);
175 freedata:
176 	(void) warnx(svc_dg_str, __no_mem_str);
177 freedata_nowarn:
178 	if (xprt) {
179 		if (su)
180 			(void) mem_free(su, sizeof (*su));
181 		svc_xprt_free(xprt);
182 	}
183 	return (NULL);
184 }
185 
186 /*ARGSUSED*/
187 static enum xprt_stat
188 svc_dg_stat(SVCXPRT *xprt)
189 {
190 	return (XPRT_IDLE);
191 }
192 
193 static int
194 svc_dg_recvfrom(int fd, char *buf, int buflen,
195     struct sockaddr *raddr, socklen_t *raddrlen,
196     struct sockaddr *laddr, socklen_t *laddrlen)
197 {
198 	struct msghdr msg;
199 	struct iovec msg_iov[1];
200 	struct sockaddr_in *lin = (struct sockaddr_in *)laddr;
201 	int rlen;
202 	bool_t have_lin = FALSE;
203 	char tmp[CMSG_LEN(sizeof(*lin))];
204 	struct cmsghdr *cmsg;
205 
206 	memset((char *)&msg, 0, sizeof(msg));
207 	msg_iov[0].iov_base = buf;
208 	msg_iov[0].iov_len = buflen;
209 	msg.msg_iov = msg_iov;
210 	msg.msg_iovlen = 1;
211 	msg.msg_namelen = *raddrlen;
212 	msg.msg_name = (char *)raddr;
213 	if (laddr != NULL) {
214 	    msg.msg_control = (caddr_t)tmp;
215 	    msg.msg_controllen = CMSG_LEN(sizeof(*lin));
216 	}
217 	rlen = _recvmsg(fd, &msg, 0);
218 	if (rlen >= 0)
219 		*raddrlen = msg.msg_namelen;
220 
221 	if (rlen == -1 || laddr == NULL ||
222 	    msg.msg_controllen < sizeof(struct cmsghdr) ||
223 	    msg.msg_flags & MSG_CTRUNC)
224 		return rlen;
225 
226 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
227 	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
228 		if (cmsg->cmsg_level == IPPROTO_IP &&
229 		    cmsg->cmsg_type == IP_RECVDSTADDR) {
230 			have_lin = TRUE;
231 			memcpy(&lin->sin_addr,
232 			    (struct in_addr *)CMSG_DATA(cmsg),
233 			    sizeof(struct in_addr));
234 			break;
235 		}
236 	}
237 
238 	lin->sin_family = AF_INET;
239 	lin->sin_port = 0;
240 	*laddrlen = sizeof(struct sockaddr_in);
241 
242 	if (!have_lin)
243 		lin->sin_addr.s_addr = INADDR_ANY;
244 
245 	return rlen;
246 }
247 
248 static bool_t
249 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg)
250 {
251 	struct svc_dg_data *su = su_data(xprt);
252 	XDR *xdrs = &(su->su_xdrs);
253 	char *reply;
254 	struct sockaddr_storage ss;
255 	socklen_t alen;
256 	size_t replylen;
257 	ssize_t rlen;
258 
259 again:
260 	alen = sizeof (struct sockaddr_storage);
261 	rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz,
262 	    (struct sockaddr *)(void *)&ss, &alen,
263 	    (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len);
264 	if (rlen == -1 && errno == EINTR)
265 		goto again;
266 	if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
267 		return (FALSE);
268 	if (xprt->xp_rtaddr.len < alen) {
269 		if (xprt->xp_rtaddr.len != 0)
270 			mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
271 		xprt->xp_rtaddr.buf = mem_alloc(alen);
272 		xprt->xp_rtaddr.len = alen;
273 	}
274 	memcpy(xprt->xp_rtaddr.buf, &ss, alen);
275 #ifdef PORTMAP
276 	if (ss.ss_family == AF_INET) {
277 		xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
278 		xprt->xp_addrlen = sizeof (struct sockaddr_in);
279 	}
280 #endif				/* PORTMAP */
281 	xdrs->x_op = XDR_DECODE;
282 	XDR_SETPOS(xdrs, 0);
283 	if (! xdr_callmsg(xdrs, msg)) {
284 		return (FALSE);
285 	}
286 	su->su_xid = msg->rm_xid;
287 	if (su->su_cache != NULL) {
288 		if (cache_get(xprt, msg, &reply, &replylen)) {
289 			(void)_sendto(xprt->xp_fd, reply, replylen, 0,
290 			    (struct sockaddr *)(void *)&ss, alen);
291 			return (FALSE);
292 		}
293 	}
294 	return (TRUE);
295 }
296 
297 static int
298 svc_dg_sendto(int fd, char *buf, int buflen,
299     const struct sockaddr *raddr, socklen_t raddrlen,
300     const struct sockaddr *laddr, socklen_t laddrlen)
301 {
302 	struct msghdr msg;
303 	struct iovec msg_iov[1];
304 	struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr;
305 	struct in_addr *lin = &laddr_in->sin_addr;
306 	char tmp[CMSG_SPACE(sizeof(*lin))];
307 	struct cmsghdr *cmsg;
308 
309 	memset((char *)&msg, 0, sizeof(msg));
310 	msg_iov[0].iov_base = buf;
311 	msg_iov[0].iov_len = buflen;
312 	msg.msg_iov = msg_iov;
313 	msg.msg_iovlen = 1;
314 	msg.msg_namelen = raddrlen;
315 	msg.msg_name = (char *)raddr;
316 
317 	if (laddr != NULL && laddr->sa_family == AF_INET &&
318 	    lin->s_addr != INADDR_ANY) {
319 		msg.msg_control = (caddr_t)tmp;
320 		msg.msg_controllen = CMSG_LEN(sizeof(*lin));
321 		cmsg = CMSG_FIRSTHDR(&msg);
322 		cmsg->cmsg_len = CMSG_LEN(sizeof(*lin));
323 		cmsg->cmsg_level = IPPROTO_IP;
324 		cmsg->cmsg_type = IP_SENDSRCADDR;
325 		memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin));
326 	}
327 
328 	return _sendmsg(fd, &msg, 0);
329 }
330 
331 static bool_t
332 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg)
333 {
334 	struct svc_dg_data *su = su_data(xprt);
335 	XDR *xdrs = &(su->su_xdrs);
336 	bool_t stat = TRUE;
337 	size_t slen;
338 	xdrproc_t xdr_proc;
339 	caddr_t xdr_where;
340 
341 	xdrs->x_op = XDR_ENCODE;
342 	XDR_SETPOS(xdrs, 0);
343 	msg->rm_xid = su->su_xid;
344 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
345 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
346 		xdr_proc = msg->acpted_rply.ar_results.proc;
347 		xdr_where = msg->acpted_rply.ar_results.where;
348 		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
349 		msg->acpted_rply.ar_results.where = NULL;
350 
351 		if (!xdr_replymsg(xdrs, msg) ||
352 		    !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where))
353 			stat = FALSE;
354 	} else {
355 		stat = xdr_replymsg(xdrs, msg);
356 	}
357 	if (stat) {
358 		slen = XDR_GETPOS(xdrs);
359 		if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen,
360 		    (struct sockaddr *)xprt->xp_rtaddr.buf,
361 		    (socklen_t)xprt->xp_rtaddr.len,
362 		    (struct sockaddr *)su->su_srcaddr.buf,
363 		    (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) {
364 			stat = TRUE;
365 			if (su->su_cache)
366 				cache_set(xprt, slen);
367 		}
368 	}
369 	return (stat);
370 }
371 
372 static bool_t
373 svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
374 {
375 	struct svc_dg_data *su;
376 
377 	assert(xprt != NULL);
378 	su = su_data(xprt);
379 	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
380 		&su->su_xdrs, xdr_args, args_ptr));
381 }
382 
383 static bool_t
384 svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
385 {
386 	XDR *xdrs = &(su_data(xprt)->su_xdrs);
387 
388 	xdrs->x_op = XDR_FREE;
389 	return (*xdr_args)(xdrs, args_ptr);
390 }
391 
392 static void
393 svc_dg_destroy(SVCXPRT *xprt)
394 {
395 	struct svc_dg_data *su = su_data(xprt);
396 
397 	xprt_unregister(xprt);
398 	if (xprt->xp_fd != -1)
399 		(void)_close(xprt->xp_fd);
400 	XDR_DESTROY(&(su->su_xdrs));
401 	(void) mem_free(rpc_buffer(xprt), su->su_iosz);
402 	if (su->su_srcaddr.buf)
403 		(void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen);
404 	(void) mem_free(su, sizeof (*su));
405 	if (xprt->xp_rtaddr.buf)
406 		(void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
407 	if (xprt->xp_ltaddr.buf)
408 		(void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
409 	free(xprt->xp_tp);
410 	svc_xprt_free(xprt);
411 }
412 
413 static bool_t
414 /*ARGSUSED*/
415 svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in)
416 {
417 	return (FALSE);
418 }
419 
420 static void
421 svc_dg_ops(SVCXPRT *xprt)
422 {
423 	static struct xp_ops ops;
424 	static struct xp_ops2 ops2;
425 
426 /* VARIABLES PROTECTED BY ops_lock: ops */
427 
428 	mutex_lock(&ops_lock);
429 	if (ops.xp_recv == NULL) {
430 		ops.xp_recv = svc_dg_recv;
431 		ops.xp_stat = svc_dg_stat;
432 		ops.xp_getargs = svc_dg_getargs;
433 		ops.xp_reply = svc_dg_reply;
434 		ops.xp_freeargs = svc_dg_freeargs;
435 		ops.xp_destroy = svc_dg_destroy;
436 		ops2.xp_control = svc_dg_control;
437 	}
438 	xprt->xp_ops = &ops;
439 	xprt->xp_ops2 = &ops2;
440 	mutex_unlock(&ops_lock);
441 }
442 
443 /*  The CACHING COMPONENT */
444 
445 /*
446  * Could have been a separate file, but some part of it depends upon the
447  * private structure of the client handle.
448  *
449  * Fifo cache for cl server
450  * Copies pointers to reply buffers into fifo cache
451  * Buffers are sent again if retransmissions are detected.
452  */
453 
454 #define	SPARSENESS 4	/* 75% sparse */
455 
456 #define	ALLOC(type, size)	\
457 	(type *) mem_alloc((sizeof (type) * (size)))
458 
459 #define	MEMZERO(addr, type, size)	 \
460 	(void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
461 
462 #define	FREE(addr, type, size)	\
463 	mem_free((addr), (sizeof (type) * (size)))
464 
465 /*
466  * An entry in the cache
467  */
468 typedef struct cache_node *cache_ptr;
469 struct cache_node {
470 	/*
471 	 * Index into cache is xid, proc, vers, prog and address
472 	 */
473 	u_int32_t cache_xid;
474 	rpcproc_t cache_proc;
475 	rpcvers_t cache_vers;
476 	rpcprog_t cache_prog;
477 	struct netbuf cache_addr;
478 	/*
479 	 * The cached reply and length
480 	 */
481 	char *cache_reply;
482 	size_t cache_replylen;
483 	/*
484 	 * Next node on the list, if there is a collision
485 	 */
486 	cache_ptr cache_next;
487 };
488 
489 /*
490  * The entire cache
491  */
492 struct cl_cache {
493 	u_int uc_size;		/* size of cache */
494 	cache_ptr *uc_entries;	/* hash table of entries in cache */
495 	cache_ptr *uc_fifo;	/* fifo list of entries in cache */
496 	u_int uc_nextvictim;	/* points to next victim in fifo list */
497 	rpcprog_t uc_prog;	/* saved program number */
498 	rpcvers_t uc_vers;	/* saved version number */
499 	rpcproc_t uc_proc;	/* saved procedure number */
500 };
501 
502 
503 /*
504  * the hashing function
505  */
506 #define	CACHE_LOC(transp, xid)	\
507 	(xid % (SPARSENESS * ((struct cl_cache *) \
508 		su_data(transp)->su_cache)->uc_size))
509 
510 /*
511  * Enable use of the cache. Returns 1 on success, 0 on failure.
512  * Note: there is no disable.
513  */
514 static const char cache_enable_str[] = "svc_enablecache: %s %s";
515 static const char alloc_err[] = "could not allocate cache ";
516 static const char enable_err[] = "cache already enabled";
517 
518 int
519 svc_dg_enablecache(SVCXPRT *transp, u_int size)
520 {
521 	struct svc_dg_data *su = su_data(transp);
522 	struct cl_cache *uc;
523 
524 	mutex_lock(&dupreq_lock);
525 	if (su->su_cache != NULL) {
526 		(void) warnx(cache_enable_str, enable_err, " ");
527 		mutex_unlock(&dupreq_lock);
528 		return (0);
529 	}
530 	uc = ALLOC(struct cl_cache, 1);
531 	if (uc == NULL) {
532 		warnx(cache_enable_str, alloc_err, " ");
533 		mutex_unlock(&dupreq_lock);
534 		return (0);
535 	}
536 	uc->uc_size = size;
537 	uc->uc_nextvictim = 0;
538 	uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
539 	if (uc->uc_entries == NULL) {
540 		warnx(cache_enable_str, alloc_err, "data");
541 		FREE(uc, struct cl_cache, 1);
542 		mutex_unlock(&dupreq_lock);
543 		return (0);
544 	}
545 	MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
546 	uc->uc_fifo = ALLOC(cache_ptr, size);
547 	if (uc->uc_fifo == NULL) {
548 		warnx(cache_enable_str, alloc_err, "fifo");
549 		FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
550 		FREE(uc, struct cl_cache, 1);
551 		mutex_unlock(&dupreq_lock);
552 		return (0);
553 	}
554 	MEMZERO(uc->uc_fifo, cache_ptr, size);
555 	su->su_cache = (char *)(void *)uc;
556 	mutex_unlock(&dupreq_lock);
557 	return (1);
558 }
559 
560 /*
561  * Set an entry in the cache.  It assumes that the uc entry is set from
562  * the earlier call to cache_get() for the same procedure.  This will always
563  * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
564  * by svc_dg_reply().  All this hoopla because the right RPC parameters are
565  * not available at svc_dg_reply time.
566  */
567 
568 static const char cache_set_str[] = "cache_set: %s";
569 static const char cache_set_err1[] = "victim not found";
570 static const char cache_set_err2[] = "victim alloc failed";
571 static const char cache_set_err3[] = "could not allocate new rpc buffer";
572 
573 static void
574 cache_set(SVCXPRT *xprt, size_t replylen)
575 {
576 	cache_ptr victim;
577 	cache_ptr *vicp;
578 	struct svc_dg_data *su = su_data(xprt);
579 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
580 	u_int loc;
581 	char *newbuf;
582 #ifdef RPC_CACHE_DEBUG
583 	struct netconfig *nconf;
584 	char *uaddr;
585 #endif
586 
587 	mutex_lock(&dupreq_lock);
588 	/*
589 	 * Find space for the new entry, either by
590 	 * reusing an old entry, or by mallocing a new one
591 	 */
592 	victim = uc->uc_fifo[uc->uc_nextvictim];
593 	if (victim != NULL) {
594 		loc = CACHE_LOC(xprt, victim->cache_xid);
595 		for (vicp = &uc->uc_entries[loc];
596 			*vicp != NULL && *vicp != victim;
597 			vicp = &(*vicp)->cache_next)
598 			;
599 		if (*vicp == NULL) {
600 			warnx(cache_set_str, cache_set_err1);
601 			mutex_unlock(&dupreq_lock);
602 			return;
603 		}
604 		*vicp = victim->cache_next;	/* remove from cache */
605 		newbuf = victim->cache_reply;
606 	} else {
607 		victim = ALLOC(struct cache_node, 1);
608 		if (victim == NULL) {
609 			warnx(cache_set_str, cache_set_err2);
610 			mutex_unlock(&dupreq_lock);
611 			return;
612 		}
613 		newbuf = mem_alloc(su->su_iosz);
614 		if (newbuf == NULL) {
615 			warnx(cache_set_str, cache_set_err3);
616 			FREE(victim, struct cache_node, 1);
617 			mutex_unlock(&dupreq_lock);
618 			return;
619 		}
620 	}
621 
622 	/*
623 	 * Store it away
624 	 */
625 #ifdef RPC_CACHE_DEBUG
626 	if (nconf = getnetconfigent(xprt->xp_netid)) {
627 		uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
628 		freenetconfigent(nconf);
629 		printf(
630 	"cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
631 			su->su_xid, uc->uc_prog, uc->uc_vers,
632 			uc->uc_proc, uaddr);
633 		free(uaddr);
634 	}
635 #endif
636 	victim->cache_replylen = replylen;
637 	victim->cache_reply = rpc_buffer(xprt);
638 	rpc_buffer(xprt) = newbuf;
639 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
640 			su->su_iosz, XDR_ENCODE);
641 	victim->cache_xid = su->su_xid;
642 	victim->cache_proc = uc->uc_proc;
643 	victim->cache_vers = uc->uc_vers;
644 	victim->cache_prog = uc->uc_prog;
645 	victim->cache_addr = xprt->xp_rtaddr;
646 	victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
647 	(void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
648 	    (size_t)xprt->xp_rtaddr.len);
649 	loc = CACHE_LOC(xprt, victim->cache_xid);
650 	victim->cache_next = uc->uc_entries[loc];
651 	uc->uc_entries[loc] = victim;
652 	uc->uc_fifo[uc->uc_nextvictim++] = victim;
653 	uc->uc_nextvictim %= uc->uc_size;
654 	mutex_unlock(&dupreq_lock);
655 }
656 
657 /*
658  * Try to get an entry from the cache
659  * return 1 if found, 0 if not found and set the stage for cache_set()
660  */
661 static int
662 cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp, size_t *replylenp)
663 {
664 	u_int loc;
665 	cache_ptr ent;
666 	struct svc_dg_data *su = su_data(xprt);
667 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
668 #ifdef RPC_CACHE_DEBUG
669 	struct netconfig *nconf;
670 	char *uaddr;
671 #endif
672 
673 	mutex_lock(&dupreq_lock);
674 	loc = CACHE_LOC(xprt, su->su_xid);
675 	for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
676 		if (ent->cache_xid == su->su_xid &&
677 			ent->cache_proc == msg->rm_call.cb_proc &&
678 			ent->cache_vers == msg->rm_call.cb_vers &&
679 			ent->cache_prog == msg->rm_call.cb_prog &&
680 			ent->cache_addr.len == xprt->xp_rtaddr.len &&
681 			(memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
682 				xprt->xp_rtaddr.len) == 0)) {
683 #ifdef RPC_CACHE_DEBUG
684 			if (nconf = getnetconfigent(xprt->xp_netid)) {
685 				uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
686 				freenetconfigent(nconf);
687 				printf(
688 	"cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
689 					su->su_xid, msg->rm_call.cb_prog,
690 					msg->rm_call.cb_vers,
691 					msg->rm_call.cb_proc, uaddr);
692 				free(uaddr);
693 			}
694 #endif
695 			*replyp = ent->cache_reply;
696 			*replylenp = ent->cache_replylen;
697 			mutex_unlock(&dupreq_lock);
698 			return (1);
699 		}
700 	}
701 	/*
702 	 * Failed to find entry
703 	 * Remember a few things so we can do a set later
704 	 */
705 	uc->uc_proc = msg->rm_call.cb_proc;
706 	uc->uc_vers = msg->rm_call.cb_vers;
707 	uc->uc_prog = msg->rm_call.cb_prog;
708 	mutex_unlock(&dupreq_lock);
709 	return (0);
710 }
711