svc_udp.c 15 KB

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