svc_udp.c 15 KB

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