Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/svc_dg.c

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