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