]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/rpc/svc_dg.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / rpc / svc_dg.c
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 svc_dg_err4[] = "cannot set IP_RECVDSTADDR";
103 static const char __no_mem_str[] = "out of memory";
104
105 SVCXPRT *
106 svc_dg_create(fd, sendsize, recvsize)
107         int fd;
108         u_int sendsize;
109         u_int recvsize;
110 {
111         SVCXPRT *xprt;
112         struct svc_dg_data *su = NULL;
113         struct __rpc_sockinfo si;
114         struct sockaddr_storage ss;
115         socklen_t slen;
116
117         if (!__rpc_fd2sockinfo(fd, &si)) {
118                 warnx(svc_dg_str, svc_dg_err1);
119                 return (NULL);
120         }
121         /*
122          * Find the receive and the send size
123          */
124         sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
125         recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
126         if ((sendsize == 0) || (recvsize == 0)) {
127                 warnx(svc_dg_str, svc_dg_err2);
128                 return (NULL);
129         }
130
131         xprt = svc_xprt_alloc();
132         if (xprt == NULL)
133                 goto freedata;
134
135         su = mem_alloc(sizeof (*su));
136         if (su == NULL)
137                 goto freedata;
138         su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
139         if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
140                 goto freedata;
141         xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
142                 XDR_DECODE);
143         su->su_cache = NULL;
144         xprt->xp_fd = fd;
145         xprt->xp_p2 = su;
146         xprt->xp_verf.oa_base = su->su_verfbody;
147         svc_dg_ops(xprt);
148         xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
149
150         slen = sizeof ss;
151         if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
152                 warnx(svc_dg_str, svc_dg_err3);
153                 goto freedata_nowarn;
154         }
155         xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
156         xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
157         xprt->xp_ltaddr.len = slen;
158         memcpy(xprt->xp_ltaddr.buf, &ss, slen);
159
160         if (ss.ss_family == AF_INET) {
161                 struct sockaddr_in *sin;
162                 static const int true_value = 1;
163
164                 sin = (struct sockaddr_in *)(void *)&ss;
165                 if (sin->sin_addr.s_addr == INADDR_ANY) {
166                     su->su_srcaddr.buf = mem_alloc(sizeof (ss));
167                     su->su_srcaddr.maxlen = sizeof (ss);
168
169                     if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR,
170                                     &true_value, sizeof(true_value))) {
171                             warnx(svc_dg_str,  svc_dg_err4);
172                             goto freedata_nowarn;
173                     }
174                 }
175         }
176
177         xprt_register(xprt);
178         return (xprt);
179 freedata:
180         (void) warnx(svc_dg_str, __no_mem_str);
181 freedata_nowarn:
182         if (xprt) {
183                 if (su)
184                         (void) mem_free(su, sizeof (*su));
185                 svc_xprt_free(xprt);
186         }
187         return (NULL);
188 }
189
190 /*ARGSUSED*/
191 static enum xprt_stat
192 svc_dg_stat(xprt)
193         SVCXPRT *xprt;
194 {
195         return (XPRT_IDLE);
196 }
197
198 static int
199 svc_dg_recvfrom(int fd, char *buf, int buflen,
200     struct sockaddr *raddr, socklen_t *raddrlen,
201     struct sockaddr *laddr, socklen_t *laddrlen)
202 {
203         struct msghdr msg;
204         struct iovec msg_iov[1];
205         struct sockaddr_in *lin = (struct sockaddr_in *)laddr;
206         int rlen;
207         bool_t have_lin = FALSE;
208         char tmp[CMSG_LEN(sizeof(*lin))];
209         struct cmsghdr *cmsg;
210
211         memset((char *)&msg, 0, sizeof(msg));
212         msg_iov[0].iov_base = buf;
213         msg_iov[0].iov_len = buflen;
214         msg.msg_iov = msg_iov;
215         msg.msg_iovlen = 1;
216         msg.msg_namelen = *raddrlen;
217         msg.msg_name = (char *)raddr;
218         if (laddr != NULL) {
219             msg.msg_control = (caddr_t)tmp;
220             msg.msg_controllen = CMSG_LEN(sizeof(*lin));
221         }
222         rlen = _recvmsg(fd, &msg, 0);
223         if (rlen >= 0)
224                 *raddrlen = msg.msg_namelen;
225
226         if (rlen == -1 || laddr == NULL ||
227             msg.msg_controllen < sizeof(struct cmsghdr) ||
228             msg.msg_flags & MSG_CTRUNC)
229                 return rlen;
230
231         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
232              cmsg = CMSG_NXTHDR(&msg, cmsg)) {
233                 if (cmsg->cmsg_level == IPPROTO_IP &&
234                     cmsg->cmsg_type == IP_RECVDSTADDR) {
235                         have_lin = TRUE;
236                         memcpy(&lin->sin_addr,
237                             (struct in_addr *)CMSG_DATA(cmsg),
238                             sizeof(struct in_addr));
239                         break;
240                 }
241         }
242
243         lin->sin_family = AF_INET;
244         lin->sin_port = 0;
245         *laddrlen = sizeof(struct sockaddr_in);
246
247         if (!have_lin)
248                 lin->sin_addr.s_addr = INADDR_ANY;
249
250         return rlen;
251 }
252
253 static bool_t
254 svc_dg_recv(xprt, msg)
255         SVCXPRT *xprt;
256         struct rpc_msg *msg;
257 {
258         struct svc_dg_data *su = su_data(xprt);
259         XDR *xdrs = &(su->su_xdrs);
260         char *reply;
261         struct sockaddr_storage ss;
262         socklen_t alen;
263         size_t replylen;
264         ssize_t rlen;
265
266 again:
267         alen = sizeof (struct sockaddr_storage);
268         rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz,
269             (struct sockaddr *)(void *)&ss, &alen,
270             (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len);
271         if (rlen == -1 && errno == EINTR)
272                 goto again;
273         if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
274                 return (FALSE);
275         if (xprt->xp_rtaddr.len < alen) {
276                 if (xprt->xp_rtaddr.len != 0)
277                         mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
278                 xprt->xp_rtaddr.buf = mem_alloc(alen);
279                 xprt->xp_rtaddr.len = alen;
280         }
281         memcpy(xprt->xp_rtaddr.buf, &ss, alen);
282 #ifdef PORTMAP
283         if (ss.ss_family == AF_INET) {
284                 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
285                 xprt->xp_addrlen = sizeof (struct sockaddr_in);
286         }
287 #endif                          /* PORTMAP */
288         xdrs->x_op = XDR_DECODE;
289         XDR_SETPOS(xdrs, 0);
290         if (! xdr_callmsg(xdrs, msg)) {
291                 return (FALSE);
292         }
293         su->su_xid = msg->rm_xid;
294         if (su->su_cache != NULL) {
295                 if (cache_get(xprt, msg, &reply, &replylen)) {
296                         (void)_sendto(xprt->xp_fd, reply, replylen, 0,
297                             (struct sockaddr *)(void *)&ss, alen);
298                         return (FALSE);
299                 }
300         }
301         return (TRUE);
302 }
303
304 static int
305 svc_dg_sendto(int fd, char *buf, int buflen,
306     const struct sockaddr *raddr, socklen_t raddrlen,
307     const struct sockaddr *laddr, socklen_t laddrlen)
308 {
309         struct msghdr msg;
310         struct iovec msg_iov[1];
311         struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr;
312         struct in_addr *lin = &laddr_in->sin_addr;
313         char tmp[CMSG_SPACE(sizeof(*lin))];
314         struct cmsghdr *cmsg;
315
316         memset((char *)&msg, 0, sizeof(msg));
317         msg_iov[0].iov_base = buf;
318         msg_iov[0].iov_len = buflen;
319         msg.msg_iov = msg_iov;
320         msg.msg_iovlen = 1;
321         msg.msg_namelen = raddrlen;
322         msg.msg_name = (char *)raddr;
323
324         if (laddr != NULL && laddr->sa_family == AF_INET &&
325             lin->s_addr != INADDR_ANY) {
326                 msg.msg_control = (caddr_t)tmp;
327                 msg.msg_controllen = CMSG_LEN(sizeof(*lin));
328                 cmsg = CMSG_FIRSTHDR(&msg);
329                 cmsg->cmsg_len = CMSG_LEN(sizeof(*lin));
330                 cmsg->cmsg_level = IPPROTO_IP;
331                 cmsg->cmsg_type = IP_SENDSRCADDR;
332                 memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin));
333         }
334
335         return _sendmsg(fd, &msg, 0);
336 }
337
338 static bool_t
339 svc_dg_reply(xprt, msg)
340         SVCXPRT *xprt;
341         struct rpc_msg *msg;
342 {
343         struct svc_dg_data *su = su_data(xprt);
344         XDR *xdrs = &(su->su_xdrs);
345         bool_t stat = TRUE;
346         size_t slen;
347         xdrproc_t xdr_proc;
348         caddr_t xdr_where;
349
350         xdrs->x_op = XDR_ENCODE;
351         XDR_SETPOS(xdrs, 0);
352         msg->rm_xid = su->su_xid;
353         if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
354             msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
355                 xdr_proc = msg->acpted_rply.ar_results.proc;
356                 xdr_where = msg->acpted_rply.ar_results.where;
357                 msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
358                 msg->acpted_rply.ar_results.where = NULL;
359
360                 if (!xdr_replymsg(xdrs, msg) ||
361                     !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where))
362                         stat = FALSE;
363         } else {
364                 stat = xdr_replymsg(xdrs, msg);
365         }
366         if (stat) {
367                 slen = XDR_GETPOS(xdrs);
368                 if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen,
369                     (struct sockaddr *)xprt->xp_rtaddr.buf,
370                     (socklen_t)xprt->xp_rtaddr.len,
371                     (struct sockaddr *)su->su_srcaddr.buf,
372                     (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) {
373                         stat = TRUE;
374                         if (su->su_cache)
375                                 cache_set(xprt, slen);
376                 }
377         }
378         return (stat);
379 }
380
381 static bool_t
382 svc_dg_getargs(xprt, xdr_args, args_ptr)
383         SVCXPRT *xprt;
384         xdrproc_t xdr_args;
385         void *args_ptr;
386 {
387         struct svc_dg_data *su;
388
389         assert(xprt != NULL);
390         su = su_data(xprt);
391         return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
392                 &su->su_xdrs, xdr_args, args_ptr));
393 }
394
395 static bool_t
396 svc_dg_freeargs(xprt, xdr_args, args_ptr)
397         SVCXPRT *xprt;
398         xdrproc_t xdr_args;
399         void *args_ptr;
400 {
401         XDR *xdrs = &(su_data(xprt)->su_xdrs);
402
403         xdrs->x_op = XDR_FREE;
404         return (*xdr_args)(xdrs, args_ptr);
405 }
406
407 static void
408 svc_dg_destroy(xprt)
409         SVCXPRT *xprt;
410 {
411         struct svc_dg_data *su = su_data(xprt);
412
413         xprt_unregister(xprt);
414         if (xprt->xp_fd != -1)
415                 (void)_close(xprt->xp_fd);
416         XDR_DESTROY(&(su->su_xdrs));
417         (void) mem_free(rpc_buffer(xprt), su->su_iosz);
418         if (su->su_srcaddr.buf)
419                 (void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen);
420         (void) mem_free(su, sizeof (*su));
421         if (xprt->xp_rtaddr.buf)
422                 (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
423         if (xprt->xp_ltaddr.buf)
424                 (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
425         if (xprt->xp_tp)
426                 (void) free(xprt->xp_tp);
427         svc_xprt_free(xprt);
428 }
429
430 static bool_t
431 /*ARGSUSED*/
432 svc_dg_control(xprt, rq, in)
433         SVCXPRT *xprt;
434         const u_int     rq;
435         void            *in;
436 {
437         return (FALSE);
438 }
439
440 static void
441 svc_dg_ops(xprt)
442         SVCXPRT *xprt;
443 {
444         static struct xp_ops ops;
445         static struct xp_ops2 ops2;
446
447 /* VARIABLES PROTECTED BY ops_lock: ops */
448
449         mutex_lock(&ops_lock);
450         if (ops.xp_recv == NULL) {
451                 ops.xp_recv = svc_dg_recv;
452                 ops.xp_stat = svc_dg_stat;
453                 ops.xp_getargs = svc_dg_getargs;
454                 ops.xp_reply = svc_dg_reply;
455                 ops.xp_freeargs = svc_dg_freeargs;
456                 ops.xp_destroy = svc_dg_destroy;
457                 ops2.xp_control = svc_dg_control;
458         }
459         xprt->xp_ops = &ops;
460         xprt->xp_ops2 = &ops2;
461         mutex_unlock(&ops_lock);
462 }
463
464 /*  The CACHING COMPONENT */
465
466 /*
467  * Could have been a separate file, but some part of it depends upon the
468  * private structure of the client handle.
469  *
470  * Fifo cache for cl server
471  * Copies pointers to reply buffers into fifo cache
472  * Buffers are sent again if retransmissions are detected.
473  */
474
475 #define SPARSENESS 4    /* 75% sparse */
476
477 #define ALLOC(type, size)       \
478         (type *) mem_alloc((sizeof (type) * (size)))
479
480 #define MEMZERO(addr, type, size)        \
481         (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
482
483 #define FREE(addr, type, size)  \
484         mem_free((addr), (sizeof (type) * (size)))
485
486 /*
487  * An entry in the cache
488  */
489 typedef struct cache_node *cache_ptr;
490 struct cache_node {
491         /*
492          * Index into cache is xid, proc, vers, prog and address
493          */
494         u_int32_t cache_xid;
495         rpcproc_t cache_proc;
496         rpcvers_t cache_vers;
497         rpcprog_t cache_prog;
498         struct netbuf cache_addr;
499         /*
500          * The cached reply and length
501          */
502         char *cache_reply;
503         size_t cache_replylen;
504         /*
505          * Next node on the list, if there is a collision
506          */
507         cache_ptr cache_next;
508 };
509
510 /*
511  * The entire cache
512  */
513 struct cl_cache {
514         u_int uc_size;          /* size of cache */
515         cache_ptr *uc_entries;  /* hash table of entries in cache */
516         cache_ptr *uc_fifo;     /* fifo list of entries in cache */
517         u_int uc_nextvictim;    /* points to next victim in fifo list */
518         rpcprog_t uc_prog;      /* saved program number */
519         rpcvers_t uc_vers;      /* saved version number */
520         rpcproc_t uc_proc;      /* saved procedure number */
521 };
522
523
524 /*
525  * the hashing function
526  */
527 #define CACHE_LOC(transp, xid)  \
528         (xid % (SPARSENESS * ((struct cl_cache *) \
529                 su_data(transp)->su_cache)->uc_size))
530
531 /*
532  * Enable use of the cache. Returns 1 on success, 0 on failure.
533  * Note: there is no disable.
534  */
535 static const char cache_enable_str[] = "svc_enablecache: %s %s";
536 static const char alloc_err[] = "could not allocate cache ";
537 static const char enable_err[] = "cache already enabled";
538
539 int
540 svc_dg_enablecache(transp, size)
541         SVCXPRT *transp;
542         u_int size;
543 {
544         struct svc_dg_data *su = su_data(transp);
545         struct cl_cache *uc;
546
547         mutex_lock(&dupreq_lock);
548         if (su->su_cache != NULL) {
549                 (void) warnx(cache_enable_str, enable_err, " ");
550                 mutex_unlock(&dupreq_lock);
551                 return (0);
552         }
553         uc = ALLOC(struct cl_cache, 1);
554         if (uc == NULL) {
555                 warnx(cache_enable_str, alloc_err, " ");
556                 mutex_unlock(&dupreq_lock);
557                 return (0);
558         }
559         uc->uc_size = size;
560         uc->uc_nextvictim = 0;
561         uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
562         if (uc->uc_entries == NULL) {
563                 warnx(cache_enable_str, alloc_err, "data");
564                 FREE(uc, struct cl_cache, 1);
565                 mutex_unlock(&dupreq_lock);
566                 return (0);
567         }
568         MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
569         uc->uc_fifo = ALLOC(cache_ptr, size);
570         if (uc->uc_fifo == NULL) {
571                 warnx(cache_enable_str, alloc_err, "fifo");
572                 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
573                 FREE(uc, struct cl_cache, 1);
574                 mutex_unlock(&dupreq_lock);
575                 return (0);
576         }
577         MEMZERO(uc->uc_fifo, cache_ptr, size);
578         su->su_cache = (char *)(void *)uc;
579         mutex_unlock(&dupreq_lock);
580         return (1);
581 }
582
583 /*
584  * Set an entry in the cache.  It assumes that the uc entry is set from
585  * the earlier call to cache_get() for the same procedure.  This will always
586  * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
587  * by svc_dg_reply().  All this hoopla because the right RPC parameters are
588  * not available at svc_dg_reply time.
589  */
590
591 static const char cache_set_str[] = "cache_set: %s";
592 static const char cache_set_err1[] = "victim not found";
593 static const char cache_set_err2[] = "victim alloc failed";
594 static const char cache_set_err3[] = "could not allocate new rpc buffer";
595
596 static void
597 cache_set(xprt, replylen)
598         SVCXPRT *xprt;
599         size_t replylen;
600 {
601         cache_ptr victim;
602         cache_ptr *vicp;
603         struct svc_dg_data *su = su_data(xprt);
604         struct cl_cache *uc = (struct cl_cache *) su->su_cache;
605         u_int loc;
606         char *newbuf;
607 #ifdef RPC_CACHE_DEBUG
608         struct netconfig *nconf;
609         char *uaddr;
610 #endif
611
612         mutex_lock(&dupreq_lock);
613         /*
614          * Find space for the new entry, either by
615          * reusing an old entry, or by mallocing a new one
616          */
617         victim = uc->uc_fifo[uc->uc_nextvictim];
618         if (victim != NULL) {
619                 loc = CACHE_LOC(xprt, victim->cache_xid);
620                 for (vicp = &uc->uc_entries[loc];
621                         *vicp != NULL && *vicp != victim;
622                         vicp = &(*vicp)->cache_next)
623                         ;
624                 if (*vicp == NULL) {
625                         warnx(cache_set_str, cache_set_err1);
626                         mutex_unlock(&dupreq_lock);
627                         return;
628                 }
629                 *vicp = victim->cache_next;     /* remove from cache */
630                 newbuf = victim->cache_reply;
631         } else {
632                 victim = ALLOC(struct cache_node, 1);
633                 if (victim == NULL) {
634                         warnx(cache_set_str, cache_set_err2);
635                         mutex_unlock(&dupreq_lock);
636                         return;
637                 }
638                 newbuf = mem_alloc(su->su_iosz);
639                 if (newbuf == NULL) {
640                         warnx(cache_set_str, cache_set_err3);
641                         FREE(victim, struct cache_node, 1);
642                         mutex_unlock(&dupreq_lock);
643                         return;
644                 }
645         }
646
647         /*
648          * Store it away
649          */
650 #ifdef RPC_CACHE_DEBUG
651         if (nconf = getnetconfigent(xprt->xp_netid)) {
652                 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
653                 freenetconfigent(nconf);
654                 printf(
655         "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
656                         su->su_xid, uc->uc_prog, uc->uc_vers,
657                         uc->uc_proc, uaddr);
658                 free(uaddr);
659         }
660 #endif
661         victim->cache_replylen = replylen;
662         victim->cache_reply = rpc_buffer(xprt);
663         rpc_buffer(xprt) = newbuf;
664         xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
665                         su->su_iosz, XDR_ENCODE);
666         victim->cache_xid = su->su_xid;
667         victim->cache_proc = uc->uc_proc;
668         victim->cache_vers = uc->uc_vers;
669         victim->cache_prog = uc->uc_prog;
670         victim->cache_addr = xprt->xp_rtaddr;
671         victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
672         (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
673             (size_t)xprt->xp_rtaddr.len);
674         loc = CACHE_LOC(xprt, victim->cache_xid);
675         victim->cache_next = uc->uc_entries[loc];
676         uc->uc_entries[loc] = victim;
677         uc->uc_fifo[uc->uc_nextvictim++] = victim;
678         uc->uc_nextvictim %= uc->uc_size;
679         mutex_unlock(&dupreq_lock);
680 }
681
682 /*
683  * Try to get an entry from the cache
684  * return 1 if found, 0 if not found and set the stage for cache_set()
685  */
686 static int
687 cache_get(xprt, msg, replyp, replylenp)
688         SVCXPRT *xprt;
689         struct rpc_msg *msg;
690         char **replyp;
691         size_t *replylenp;
692 {
693         u_int loc;
694         cache_ptr ent;
695         struct svc_dg_data *su = su_data(xprt);
696         struct cl_cache *uc = (struct cl_cache *) su->su_cache;
697 #ifdef RPC_CACHE_DEBUG
698         struct netconfig *nconf;
699         char *uaddr;
700 #endif
701
702         mutex_lock(&dupreq_lock);
703         loc = CACHE_LOC(xprt, su->su_xid);
704         for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
705                 if (ent->cache_xid == su->su_xid &&
706                         ent->cache_proc == msg->rm_call.cb_proc &&
707                         ent->cache_vers == msg->rm_call.cb_vers &&
708                         ent->cache_prog == msg->rm_call.cb_prog &&
709                         ent->cache_addr.len == xprt->xp_rtaddr.len &&
710                         (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
711                                 xprt->xp_rtaddr.len) == 0)) {
712 #ifdef RPC_CACHE_DEBUG
713                         if (nconf = getnetconfigent(xprt->xp_netid)) {
714                                 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
715                                 freenetconfigent(nconf);
716                                 printf(
717         "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
718                                         su->su_xid, msg->rm_call.cb_prog,
719                                         msg->rm_call.cb_vers,
720                                         msg->rm_call.cb_proc, uaddr);
721                                 free(uaddr);
722                         }
723 #endif
724                         *replyp = ent->cache_reply;
725                         *replylenp = ent->cache_replylen;
726                         mutex_unlock(&dupreq_lock);
727                         return (1);
728                 }
729         }
730         /*
731          * Failed to find entry
732          * Remember a few things so we can do a set later
733          */
734         uc->uc_proc = msg->rm_call.cb_proc;
735         uc->uc_vers = msg->rm_call.cb_vers;
736         uc->uc_prog = msg->rm_call.cb_prog;
737         mutex_unlock(&dupreq_lock);
738         return (0);
739 }