svc_udp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /* @(#)svc_udp.c 2.2 88/07/29 4.0 RPCSRC */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. #if 0
  31. static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * svc_udp.c,
  35. * Server side for UDP/IP based RPC. (Does some caching in the hopes of
  36. * achieving execute-at-most-once semantics.)
  37. *
  38. * Copyright (C) 1984, Sun Microsystems, Inc.
  39. */
  40. #define __FORCE_GLIBC
  41. #include <features.h>
  42. #include <stdio.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <rpc/rpc.h>
  46. #include <sys/socket.h>
  47. #include <errno.h>
  48. #ifdef IP_PKTINFO
  49. #include <sys/uio.h>
  50. #endif
  51. #ifdef USE_IN_LIBIO
  52. # include <wchar.h>
  53. # include <libio/iolibio.h>
  54. # define fputs(s, f) _IO_fputs (s, f)
  55. /* libc_hidden_proto(fwprintf) */
  56. #endif
  57. /* Experimentally off - libc_hidden_proto(memcmp) */
  58. /* Experimentally off - libc_hidden_proto(memcpy) */
  59. /* Experimentally off - libc_hidden_proto(memset) */
  60. /* libc_hidden_proto(perror) */
  61. /* libc_hidden_proto(socket) */
  62. /* libc_hidden_proto(close) */
  63. /* libc_hidden_proto(xprt_register) */
  64. /* libc_hidden_proto(xprt_unregister) */
  65. /* libc_hidden_proto(xdrmem_create) */
  66. /* libc_hidden_proto(xdr_callmsg) */
  67. /* libc_hidden_proto(xdr_replymsg) */
  68. /* libc_hidden_proto(getsockname) */
  69. /* libc_hidden_proto(setsockopt) */
  70. /* libc_hidden_proto(bind) */
  71. /* libc_hidden_proto(bindresvport) */
  72. /* libc_hidden_proto(recvfrom) */
  73. /* libc_hidden_proto(sendto) */
  74. /* libc_hidden_proto(recvmsg) */
  75. /* libc_hidden_proto(sendmsg) */
  76. /* libc_hidden_proto(fputs) */
  77. /* libc_hidden_proto(fprintf) */
  78. #define rpc_buffer(xprt) ((xprt)->xp_p1)
  79. #ifndef MAX
  80. #define MAX(a, b) ((a > b) ? a : b)
  81. #endif
  82. static bool_t svcudp_recv (SVCXPRT *, struct rpc_msg *);
  83. static bool_t svcudp_reply (SVCXPRT *, struct rpc_msg *);
  84. static enum xprt_stat svcudp_stat (SVCXPRT *);
  85. static bool_t svcudp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
  86. static bool_t svcudp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
  87. static void svcudp_destroy (SVCXPRT *);
  88. static const struct xp_ops svcudp_op =
  89. {
  90. svcudp_recv,
  91. svcudp_stat,
  92. svcudp_getargs,
  93. svcudp_reply,
  94. svcudp_freeargs,
  95. svcudp_destroy
  96. };
  97. static int cache_get (SVCXPRT *, struct rpc_msg *, char **replyp,
  98. u_long *replylenp);
  99. static void cache_set (SVCXPRT *xprt, u_long replylen);
  100. /*
  101. * kept in xprt->xp_p2
  102. */
  103. struct svcudp_data
  104. {
  105. u_int su_iosz; /* byte size of send.recv buffer */
  106. u_long su_xid; /* transaction id */
  107. XDR su_xdrs; /* XDR handle */
  108. char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
  109. char *su_cache; /* cached data, NULL if no cache */
  110. };
  111. #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
  112. /*
  113. * Usage:
  114. * xprt = svcudp_create(sock);
  115. *
  116. * If sock<0 then a socket is created, else sock is used.
  117. * If the socket, sock is not bound to a port then svcudp_create
  118. * binds it to an arbitrary port. In any (successful) case,
  119. * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  120. * associated port number.
  121. * Once *xprt is initialized, it is registered as a transporter;
  122. * see (svc.h, xprt_register).
  123. * The routines returns NULL if a problem occurred.
  124. */
  125. /* libc_hidden_proto(svcudp_bufcreate) */
  126. SVCXPRT *
  127. svcudp_bufcreate (int sock, u_int sendsz, u_int recvsz)
  128. {
  129. bool_t madesock = FALSE;
  130. SVCXPRT *xprt;
  131. struct svcudp_data *su;
  132. struct sockaddr_in addr;
  133. socklen_t len = sizeof (struct sockaddr_in);
  134. int pad;
  135. void *buf;
  136. if (sock == RPC_ANYSOCK)
  137. {
  138. if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  139. {
  140. perror (_("svcudp_create: socket creation problem"));
  141. return (SVCXPRT *) NULL;
  142. }
  143. madesock = TRUE;
  144. }
  145. memset ((char *) &addr, 0, sizeof (addr));
  146. addr.sin_family = AF_INET;
  147. if (bindresvport (sock, &addr))
  148. {
  149. addr.sin_port = 0;
  150. (void) bind (sock, (struct sockaddr *) &addr, len);
  151. }
  152. if (getsockname (sock, (struct sockaddr *) &addr, &len) != 0)
  153. {
  154. perror (_("svcudp_create - cannot getsockname"));
  155. if (madesock)
  156. (void) close (sock);
  157. return (SVCXPRT *) NULL;
  158. }
  159. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  160. su = (struct svcudp_data *) mem_alloc (sizeof (*su));
  161. buf = mem_alloc (((MAX (sendsz, recvsz) + 3) / 4) * 4);
  162. if (xprt == NULL || su == NULL || buf == NULL)
  163. {
  164. #ifdef USE_IN_LIBIO
  165. if (_IO_fwide (stderr, 0) > 0)
  166. (void) fwprintf (stderr, L"%s", _("svcudp_create: out of memory\n"));
  167. else
  168. #endif
  169. (void) fputs (_("svcudp_create: out of memory\n"), stderr);
  170. mem_free (xprt, sizeof (SVCXPRT));
  171. mem_free (su, sizeof (*su));
  172. mem_free (buf, ((MAX (sendsz, recvsz) + 3) / 4) * 4);
  173. return NULL;
  174. }
  175. su->su_iosz = ((MAX (sendsz, recvsz) + 3) / 4) * 4;
  176. rpc_buffer (xprt) = buf;
  177. xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_DECODE);
  178. su->su_cache = NULL;
  179. xprt->xp_p2 = (caddr_t) su;
  180. xprt->xp_verf.oa_base = su->su_verfbody;
  181. xprt->xp_ops = &svcudp_op;
  182. xprt->xp_port = ntohs (addr.sin_port);
  183. xprt->xp_sock = sock;
  184. #ifdef IP_PKTINFO
  185. if ((sizeof (struct iovec) + sizeof (struct msghdr)
  186. + sizeof(struct cmsghdr) + sizeof (struct in_pktinfo))
  187. > sizeof (xprt->xp_pad))
  188. {
  189. # ifdef USE_IN_LIBIO
  190. if (_IO_fwide (stderr, 0) > 0)
  191. (void) fwprintf (stderr, L"%s",
  192. _("svcudp_create: xp_pad is too small for IP_PKTINFO\n"));
  193. else
  194. # endif
  195. (void) fputs (_("svcudp_create: xp_pad is too small for IP_PKTINFO\n"),
  196. stderr);
  197. return NULL;
  198. }
  199. pad = 1;
  200. if (setsockopt (sock, SOL_IP, IP_PKTINFO, (void *) &pad,
  201. sizeof (pad)) == 0)
  202. /* Set the padding to all 1s. */
  203. pad = 0xff;
  204. else
  205. #endif
  206. /* Clear the padding. */
  207. pad = 0;
  208. memset (&xprt->xp_pad [0], pad, sizeof (xprt->xp_pad));
  209. xprt_register (xprt);
  210. return xprt;
  211. }
  212. libc_hidden_def(svcudp_bufcreate)
  213. /* libc_hidden_proto(svcudp_create) */
  214. SVCXPRT *
  215. svcudp_create (int sock)
  216. {
  217. return svcudp_bufcreate (sock, UDPMSGSIZE, UDPMSGSIZE);
  218. }
  219. libc_hidden_def(svcudp_create)
  220. static enum xprt_stat
  221. svcudp_stat (xprt)
  222. SVCXPRT *xprt attribute_unused;
  223. {
  224. return XPRT_IDLE;
  225. }
  226. static bool_t
  227. svcudp_recv (xprt, msg)
  228. SVCXPRT *xprt;
  229. struct rpc_msg *msg;
  230. {
  231. struct svcudp_data *su = su_data (xprt);
  232. XDR *xdrs = &(su->su_xdrs);
  233. int rlen;
  234. char *reply;
  235. u_long replylen;
  236. socklen_t len;
  237. /* It is very tricky when you have IP aliases. We want to make sure
  238. that we are sending the packet from the IP address where the
  239. incoming packet is addressed to. H.J. */
  240. #ifdef IP_PKTINFO
  241. struct iovec *iovp;
  242. struct msghdr *mesgp;
  243. #endif
  244. again:
  245. /* FIXME -- should xp_addrlen be a size_t? */
  246. len = (socklen_t) sizeof(struct sockaddr_in);
  247. #ifdef IP_PKTINFO
  248. iovp = (struct iovec *) &xprt->xp_pad [0];
  249. mesgp = (struct msghdr *) &xprt->xp_pad [sizeof (struct iovec)];
  250. if (mesgp->msg_iovlen)
  251. {
  252. iovp->iov_base = rpc_buffer (xprt);
  253. iovp->iov_len = su->su_iosz;
  254. mesgp->msg_iov = iovp;
  255. mesgp->msg_iovlen = 1;
  256. mesgp->msg_name = &(xprt->xp_raddr);
  257. mesgp->msg_namelen = len;
  258. mesgp->msg_control = &xprt->xp_pad [sizeof (struct iovec)
  259. + sizeof (struct msghdr)];
  260. mesgp->msg_controllen = sizeof(xprt->xp_pad)
  261. - sizeof (struct iovec) - sizeof (struct msghdr);
  262. rlen = recvmsg (xprt->xp_sock, mesgp, 0);
  263. if (rlen >= 0)
  264. len = mesgp->msg_namelen;
  265. }
  266. else
  267. #endif
  268. rlen = recvfrom (xprt->xp_sock, rpc_buffer (xprt),
  269. (int) su->su_iosz, 0,
  270. (struct sockaddr *) &(xprt->xp_raddr), &len);
  271. xprt->xp_addrlen = len;
  272. if (rlen == -1 && errno == EINTR)
  273. goto again;
  274. if (rlen < 16) /* < 4 32-bit ints? */
  275. return FALSE;
  276. xdrs->x_op = XDR_DECODE;
  277. XDR_SETPOS (xdrs, 0);
  278. if (!xdr_callmsg (xdrs, msg))
  279. return FALSE;
  280. su->su_xid = msg->rm_xid;
  281. if (su->su_cache != NULL)
  282. {
  283. if (cache_get (xprt, msg, &reply, &replylen))
  284. {
  285. #ifdef IP_PKTINFO
  286. if (mesgp->msg_iovlen)
  287. {
  288. iovp->iov_base = reply;
  289. iovp->iov_len = replylen;
  290. (void) sendmsg (xprt->xp_sock, mesgp, 0);
  291. }
  292. else
  293. #endif
  294. (void) sendto (xprt->xp_sock, reply, (int) replylen, 0,
  295. (struct sockaddr *) &xprt->xp_raddr, len);
  296. return TRUE;
  297. }
  298. }
  299. return TRUE;
  300. }
  301. static bool_t
  302. svcudp_reply (xprt, msg)
  303. SVCXPRT *xprt;
  304. struct rpc_msg *msg;
  305. {
  306. struct svcudp_data *su = su_data (xprt);
  307. XDR *xdrs = &(su->su_xdrs);
  308. int slen, sent;
  309. bool_t stat = FALSE;
  310. #ifdef IP_PKTINFO
  311. struct iovec *iovp;
  312. struct msghdr *mesgp;
  313. #endif
  314. xdrs->x_op = XDR_ENCODE;
  315. XDR_SETPOS (xdrs, 0);
  316. msg->rm_xid = su->su_xid;
  317. if (xdr_replymsg (xdrs, msg))
  318. {
  319. slen = (int) XDR_GETPOS (xdrs);
  320. #ifdef IP_PKTINFO
  321. mesgp = (struct msghdr *) &xprt->xp_pad [sizeof (struct iovec)];
  322. if (mesgp->msg_iovlen)
  323. {
  324. iovp = (struct iovec *) &xprt->xp_pad [0];
  325. iovp->iov_base = rpc_buffer (xprt);
  326. iovp->iov_len = slen;
  327. sent = sendmsg (xprt->xp_sock, mesgp, 0);
  328. }
  329. else
  330. #endif
  331. sent = sendto (xprt->xp_sock, rpc_buffer (xprt), slen, 0,
  332. (struct sockaddr *) &(xprt->xp_raddr),
  333. xprt->xp_addrlen);
  334. if (sent == slen)
  335. {
  336. stat = TRUE;
  337. if (su->su_cache && slen >= 0)
  338. {
  339. cache_set (xprt, (u_long) slen);
  340. }
  341. }
  342. }
  343. return stat;
  344. }
  345. static bool_t
  346. svcudp_getargs (xprt, xdr_args, args_ptr)
  347. SVCXPRT *xprt;
  348. xdrproc_t xdr_args;
  349. caddr_t args_ptr;
  350. {
  351. return (*xdr_args) (&(su_data (xprt)->su_xdrs), args_ptr);
  352. }
  353. static bool_t
  354. svcudp_freeargs (xprt, xdr_args, args_ptr)
  355. SVCXPRT *xprt;
  356. xdrproc_t xdr_args;
  357. caddr_t args_ptr;
  358. {
  359. XDR *xdrs = &(su_data (xprt)->su_xdrs);
  360. xdrs->x_op = XDR_FREE;
  361. return (*xdr_args) (xdrs, args_ptr);
  362. }
  363. static void
  364. svcudp_destroy (xprt)
  365. SVCXPRT *xprt;
  366. {
  367. struct svcudp_data *su = su_data (xprt);
  368. xprt_unregister (xprt);
  369. (void) close (xprt->xp_sock);
  370. XDR_DESTROY (&(su->su_xdrs));
  371. mem_free (rpc_buffer (xprt), su->su_iosz);
  372. mem_free ((caddr_t) su, sizeof (struct svcudp_data));
  373. mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
  374. }
  375. /***********this could be a separate file*********************/
  376. /*
  377. * Fifo cache for udp server
  378. * Copies pointers to reply buffers into fifo cache
  379. * Buffers are sent again if retransmissions are detected.
  380. */
  381. #define SPARSENESS 4 /* 75% sparse */
  382. #ifdef USE_IN_LIBIO
  383. # define CACHE_PERROR(msg) \
  384. if (_IO_fwide (stderr, 0) > 0) \
  385. (void) __fwprintf(stderr, L"%s\n", msg); \
  386. else \
  387. (void) fprintf(stderr, "%s\n", msg)
  388. #else
  389. # define CACHE_PERROR(msg) \
  390. (void) fprintf(stderr,"%s\n", msg)
  391. #endif
  392. #define ALLOC(type, size) \
  393. (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
  394. #define BZERO(addr, type, size) \
  395. memset((char *) addr, 0, sizeof(type) * (int) (size))
  396. /*
  397. * An entry in the cache
  398. */
  399. typedef struct cache_node *cache_ptr;
  400. struct cache_node
  401. {
  402. /*
  403. * Index into cache is xid, proc, vers, prog and address
  404. */
  405. u_long cache_xid;
  406. u_long cache_proc;
  407. u_long cache_vers;
  408. u_long cache_prog;
  409. struct sockaddr_in cache_addr;
  410. /*
  411. * The cached reply and length
  412. */
  413. char *cache_reply;
  414. u_long cache_replylen;
  415. /*
  416. * Next node on the list, if there is a collision
  417. */
  418. cache_ptr cache_next;
  419. };
  420. /*
  421. * The entire cache
  422. */
  423. struct udp_cache
  424. {
  425. u_long uc_size; /* size of cache */
  426. cache_ptr *uc_entries; /* hash table of entries in cache */
  427. cache_ptr *uc_fifo; /* fifo list of entries in cache */
  428. u_long uc_nextvictim; /* points to next victim in fifo list */
  429. u_long uc_prog; /* saved program number */
  430. u_long uc_vers; /* saved version number */
  431. u_long uc_proc; /* saved procedure number */
  432. struct sockaddr_in uc_addr; /* saved caller's address */
  433. };
  434. /*
  435. * the hashing function
  436. */
  437. #define CACHE_LOC(transp, xid) \
  438. (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
  439. /*
  440. * Enable use of the cache.
  441. * Note: there is no disable.
  442. */
  443. int svcudp_enablecache (SVCXPRT *transp, u_long size);
  444. int
  445. svcudp_enablecache (SVCXPRT *transp, u_long size)
  446. {
  447. struct svcudp_data *su = su_data (transp);
  448. struct udp_cache *uc;
  449. if (su->su_cache != NULL)
  450. {
  451. CACHE_PERROR (_("enablecache: cache already enabled"));
  452. return 0;
  453. }
  454. uc = ALLOC (struct udp_cache, 1);
  455. if (uc == NULL)
  456. {
  457. CACHE_PERROR (_("enablecache: could not allocate cache"));
  458. return 0;
  459. }
  460. uc->uc_size = size;
  461. uc->uc_nextvictim = 0;
  462. uc->uc_entries = ALLOC (cache_ptr, size * SPARSENESS);
  463. if (uc->uc_entries == NULL)
  464. {
  465. CACHE_PERROR (_("enablecache: could not allocate cache data"));
  466. return 0;
  467. }
  468. BZERO (uc->uc_entries, cache_ptr, size * SPARSENESS);
  469. uc->uc_fifo = ALLOC (cache_ptr, size);
  470. if (uc->uc_fifo == NULL)
  471. {
  472. CACHE_PERROR (_("enablecache: could not allocate cache fifo"));
  473. return 0;
  474. }
  475. BZERO (uc->uc_fifo, cache_ptr, size);
  476. su->su_cache = (char *) uc;
  477. return 1;
  478. }
  479. /*
  480. * Set an entry in the cache
  481. */
  482. static void
  483. cache_set (SVCXPRT *xprt, u_long replylen)
  484. {
  485. cache_ptr victim;
  486. cache_ptr *vicp;
  487. struct svcudp_data *su = su_data (xprt);
  488. struct udp_cache *uc = (struct udp_cache *) su->su_cache;
  489. u_int loc;
  490. char *newbuf;
  491. /*
  492. * Find space for the new entry, either by
  493. * reusing an old entry, or by mallocing a new one
  494. */
  495. victim = uc->uc_fifo[uc->uc_nextvictim];
  496. if (victim != NULL)
  497. {
  498. loc = CACHE_LOC (xprt, victim->cache_xid);
  499. for (vicp = &uc->uc_entries[loc];
  500. *vicp != NULL && *vicp != victim;
  501. vicp = &(*vicp)->cache_next)
  502. ;
  503. if (*vicp == NULL)
  504. {
  505. CACHE_PERROR (_("cache_set: victim not found"));
  506. return;
  507. }
  508. *vicp = victim->cache_next; /* remote from cache */
  509. newbuf = victim->cache_reply;
  510. }
  511. else
  512. {
  513. victim = ALLOC (struct cache_node, 1);
  514. if (victim == NULL)
  515. {
  516. CACHE_PERROR (_("cache_set: victim alloc failed"));
  517. return;
  518. }
  519. newbuf = mem_alloc (su->su_iosz);
  520. if (newbuf == NULL)
  521. {
  522. CACHE_PERROR (_("cache_set: could not allocate new rpc_buffer"));
  523. return;
  524. }
  525. }
  526. /*
  527. * Store it away
  528. */
  529. victim->cache_replylen = replylen;
  530. victim->cache_reply = rpc_buffer (xprt);
  531. rpc_buffer (xprt) = newbuf;
  532. xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_ENCODE);
  533. victim->cache_xid = su->su_xid;
  534. victim->cache_proc = uc->uc_proc;
  535. victim->cache_vers = uc->uc_vers;
  536. victim->cache_prog = uc->uc_prog;
  537. victim->cache_addr = uc->uc_addr;
  538. loc = CACHE_LOC (xprt, victim->cache_xid);
  539. victim->cache_next = uc->uc_entries[loc];
  540. uc->uc_entries[loc] = victim;
  541. uc->uc_fifo[uc->uc_nextvictim++] = victim;
  542. uc->uc_nextvictim %= uc->uc_size;
  543. }
  544. /*
  545. * Try to get an entry from the cache
  546. * return 1 if found, 0 if not found
  547. */
  548. static int
  549. cache_get (xprt, msg, replyp, replylenp)
  550. SVCXPRT *xprt;
  551. struct rpc_msg *msg;
  552. char **replyp;
  553. u_long *replylenp;
  554. {
  555. u_int loc;
  556. cache_ptr ent;
  557. struct svcudp_data *su = su_data (xprt);
  558. struct udp_cache *uc = (struct udp_cache *) su->su_cache;
  559. #define EQADDR(a1, a2) (memcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
  560. loc = CACHE_LOC (xprt, su->su_xid);
  561. for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next)
  562. {
  563. if (ent->cache_xid == su->su_xid &&
  564. ent->cache_proc == uc->uc_proc &&
  565. ent->cache_vers == uc->uc_vers &&
  566. ent->cache_prog == uc->uc_prog &&
  567. EQADDR (ent->cache_addr, uc->uc_addr))
  568. {
  569. *replyp = ent->cache_reply;
  570. *replylenp = ent->cache_replylen;
  571. return 1;
  572. }
  573. }
  574. /*
  575. * Failed to find entry
  576. * Remember a few things so we can do a set later
  577. */
  578. uc->uc_proc = msg->rm_call.cb_proc;
  579. uc->uc_vers = msg->rm_call.cb_vers;
  580. uc->uc_prog = msg->rm_call.cb_prog;
  581. memcpy (&uc->uc_addr, &xprt->xp_raddr, sizeof (uc->uc_addr));
  582. return 0;
  583. }