svc_udp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #define __FORCE_GLIBC
  31. #include <features.h>
  32. /*
  33. * svc_udp.c,
  34. * Server side for UDP/IP based RPC. (Does some caching in the hopes of
  35. * achieving execute-at-most-once semantics.)
  36. *
  37. * Copyright (C) 1984, Sun Microsystems, Inc.
  38. */
  39. #include <stdio.h>
  40. #include <rpc/rpc.h>
  41. #include <sys/socket.h>
  42. #include <errno.h>
  43. #include <unistd.h>
  44. #define rpc_buffer(xprt) ((xprt)->xp_p1)
  45. #ifndef MAX
  46. #define MAX(a, b) ((a > b) ? a : b)
  47. #endif
  48. static bool_t svcudp_recv();
  49. static bool_t svcudp_reply();
  50. static enum xprt_stat svcudp_stat();
  51. static bool_t svcudp_getargs();
  52. static bool_t svcudp_freeargs();
  53. static void svcudp_destroy();
  54. static void cache_set(SVCXPRT *xprt, u_long replylen);
  55. static int cache_get(SVCXPRT *xprt, struct rpc_msg *msg,
  56. char **replyp, u_long *replylenp);
  57. static struct xp_ops svcudp_op = {
  58. svcudp_recv,
  59. svcudp_stat,
  60. svcudp_getargs,
  61. svcudp_reply,
  62. svcudp_freeargs,
  63. svcudp_destroy
  64. };
  65. /*
  66. * kept in xprt->xp_p2
  67. */
  68. struct svcudp_data {
  69. u_int su_iosz; /* byte size of send.recv buffer */
  70. u_long su_xid; /* transaction id */
  71. XDR su_xdrs; /* XDR handle */
  72. char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
  73. char *su_cache; /* cached data, NULL if no cache */
  74. };
  75. #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
  76. /*
  77. * Usage:
  78. * xprt = svcudp_create(sock);
  79. *
  80. * If sock<0 then a socket is created, else sock is used.
  81. * If the socket, sock is not bound to a port then svcudp_create
  82. * binds it to an arbitrary port. In any (successful) case,
  83. * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  84. * associated port number.
  85. * Once *xprt is initialized, it is registered as a transporter;
  86. * see (svc.h, xprt_register).
  87. * The routines returns NULL if a problem occurred.
  88. */
  89. SVCXPRT *svcudp_bufcreate(sock, sendsz, recvsz)
  90. register int sock;
  91. u_int sendsz, recvsz;
  92. {
  93. bool_t madesock = FALSE;
  94. register SVCXPRT *xprt;
  95. register struct svcudp_data *su;
  96. struct sockaddr_in addr;
  97. int len = sizeof(struct sockaddr_in);
  98. if (sock == RPC_ANYSOCK) {
  99. if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  100. perror("svcudp_create: socket creation problem");
  101. return ((SVCXPRT *) NULL);
  102. }
  103. madesock = TRUE;
  104. }
  105. bzero((char *) &addr, sizeof(addr));
  106. addr.sin_family = AF_INET;
  107. if (bindresvport(sock, &addr)) {
  108. addr.sin_port = 0;
  109. (void) bind(sock, (struct sockaddr *) &addr, len);
  110. }
  111. if (getsockname(sock, (struct sockaddr *) &addr, &len) != 0) {
  112. perror("svcudp_create - cannot getsockname");
  113. if (madesock)
  114. (void) close(sock);
  115. return ((SVCXPRT *) NULL);
  116. }
  117. xprt = (SVCXPRT *) mem_alloc(sizeof(SVCXPRT));
  118. if (xprt == NULL) {
  119. (void) fprintf(stderr, "svcudp_create: out of memory\n");
  120. return (NULL);
  121. }
  122. su = (struct svcudp_data *) mem_alloc(sizeof(*su));
  123. if (su == NULL) {
  124. (void) fprintf(stderr, "svcudp_create: out of memory\n");
  125. return (NULL);
  126. }
  127. su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
  128. if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
  129. (void) fprintf(stderr, "svcudp_create: out of memory\n");
  130. return (NULL);
  131. }
  132. xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
  133. XDR_DECODE);
  134. su->su_cache = NULL;
  135. xprt->xp_p2 = (caddr_t) su;
  136. xprt->xp_verf.oa_base = su->su_verfbody;
  137. xprt->xp_ops = &svcudp_op;
  138. xprt->xp_port = ntohs(addr.sin_port);
  139. xprt->xp_sock = sock;
  140. xprt_register(xprt);
  141. return (xprt);
  142. }
  143. SVCXPRT *svcudp_create(sock)
  144. int sock;
  145. {
  146. return (svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
  147. }
  148. static enum xprt_stat svcudp_stat(xprt)
  149. SVCXPRT *xprt;
  150. {
  151. return (XPRT_IDLE);
  152. }
  153. static bool_t svcudp_recv(xprt, msg)
  154. register SVCXPRT *xprt;
  155. struct rpc_msg *msg;
  156. {
  157. register struct svcudp_data *su = su_data(xprt);
  158. register XDR *xdrs = &(su->su_xdrs);
  159. register int rlen;
  160. char *reply;
  161. u_long replylen;
  162. again:
  163. xprt->xp_addrlen = sizeof(struct sockaddr_in);
  164. rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
  165. 0, (struct sockaddr *) &(xprt->xp_raddr),
  166. &(xprt->xp_addrlen));
  167. if (rlen == -1 && errno == EINTR)
  168. goto again;
  169. if (rlen < 4 * sizeof(u_long))
  170. return (FALSE);
  171. xdrs->x_op = XDR_DECODE;
  172. XDR_SETPOS(xdrs, 0);
  173. if (!xdr_callmsg(xdrs, msg))
  174. return (FALSE);
  175. su->su_xid = msg->rm_xid;
  176. if (su->su_cache != NULL) {
  177. if (cache_get(xprt, msg, &reply, &replylen)) {
  178. (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
  179. (struct sockaddr *) &xprt->xp_raddr,
  180. xprt->xp_addrlen);
  181. return (TRUE);
  182. }
  183. }
  184. return (TRUE);
  185. }
  186. static bool_t svcudp_reply(xprt, msg)
  187. register SVCXPRT *xprt;
  188. struct rpc_msg *msg;
  189. {
  190. register struct svcudp_data *su = su_data(xprt);
  191. register XDR *xdrs = &(su->su_xdrs);
  192. register int slen;
  193. register bool_t stat = FALSE;
  194. xdrs->x_op = XDR_ENCODE;
  195. XDR_SETPOS(xdrs, 0);
  196. msg->rm_xid = su->su_xid;
  197. if (xdr_replymsg(xdrs, msg)) {
  198. slen = (int) XDR_GETPOS(xdrs);
  199. if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
  200. (struct sockaddr *) &(xprt->xp_raddr), xprt->xp_addrlen)
  201. == slen) {
  202. stat = TRUE;
  203. if (su->su_cache && slen >= 0) {
  204. cache_set(xprt, (u_long) slen);
  205. }
  206. }
  207. }
  208. return (stat);
  209. }
  210. static bool_t svcudp_getargs(xprt, xdr_args, args_ptr)
  211. SVCXPRT *xprt;
  212. xdrproc_t xdr_args;
  213. caddr_t args_ptr;
  214. {
  215. return ((*xdr_args) (&(su_data(xprt)->su_xdrs), args_ptr));
  216. }
  217. static bool_t svcudp_freeargs(xprt, xdr_args, args_ptr)
  218. SVCXPRT *xprt;
  219. xdrproc_t xdr_args;
  220. caddr_t args_ptr;
  221. {
  222. register XDR *xdrs = &(su_data(xprt)->su_xdrs);
  223. xdrs->x_op = XDR_FREE;
  224. return ((*xdr_args) (xdrs, args_ptr));
  225. }
  226. static void svcudp_destroy(xprt)
  227. register SVCXPRT *xprt;
  228. {
  229. register struct svcudp_data *su = su_data(xprt);
  230. xprt_unregister(xprt);
  231. (void) close(xprt->xp_sock);
  232. XDR_DESTROY(&(su->su_xdrs));
  233. mem_free(rpc_buffer(xprt), su->su_iosz);
  234. mem_free((caddr_t) su, sizeof(struct svcudp_data));
  235. mem_free((caddr_t) xprt, sizeof(SVCXPRT));
  236. }
  237. /***********this could be a separate file*********************/
  238. /*
  239. * Fifo cache for udp server
  240. * Copies pointers to reply buffers into fifo cache
  241. * Buffers are sent again if retransmissions are detected.
  242. */
  243. #define SPARSENESS 4 /* 75% sparse */
  244. #define CACHE_PERROR(msg) \
  245. (void) fprintf(stderr,"%s\n", msg)
  246. #define ALLOC(type, size) \
  247. (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
  248. #define BZERO(addr, type, size) \
  249. bzero((char *) addr, sizeof(type) * (int) (size))
  250. /*
  251. * An entry in the cache
  252. */
  253. typedef struct cache_node *cache_ptr;
  254. struct cache_node {
  255. /*
  256. * Index into cache is xid, proc, vers, prog and address
  257. */
  258. u_long cache_xid;
  259. u_long cache_proc;
  260. u_long cache_vers;
  261. u_long cache_prog;
  262. struct sockaddr_in cache_addr;
  263. /*
  264. * The cached reply and length
  265. */
  266. char *cache_reply;
  267. u_long cache_replylen;
  268. /*
  269. * Next node on the list, if there is a collision
  270. */
  271. cache_ptr cache_next;
  272. };
  273. /*
  274. * The entire cache
  275. */
  276. struct udp_cache {
  277. u_long uc_size; /* size of cache */
  278. cache_ptr *uc_entries; /* hash table of entries in cache */
  279. cache_ptr *uc_fifo; /* fifo list of entries in cache */
  280. u_long uc_nextvictim; /* points to next victim in fifo list */
  281. u_long uc_prog; /* saved program number */
  282. u_long uc_vers; /* saved version number */
  283. u_long uc_proc; /* saved procedure number */
  284. struct sockaddr_in uc_addr; /* saved caller's address */
  285. };
  286. /*
  287. * the hashing function
  288. */
  289. #define CACHE_LOC(transp, xid) \
  290. (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
  291. /*
  292. * Enable use of the cache.
  293. * Note: there is no disable.
  294. */
  295. int svcudp_enablecache(transp, size)
  296. SVCXPRT *transp;
  297. u_long size;
  298. {
  299. struct svcudp_data *su = su_data(transp);
  300. struct udp_cache *uc;
  301. if (su->su_cache != NULL) {
  302. CACHE_PERROR("enablecache: cache already enabled");
  303. return (0);
  304. }
  305. uc = ALLOC(struct udp_cache, 1);
  306. if (uc == NULL) {
  307. CACHE_PERROR("enablecache: could not allocate cache");
  308. return (0);
  309. }
  310. uc->uc_size = size;
  311. uc->uc_nextvictim = 0;
  312. uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
  313. if (uc->uc_entries == NULL) {
  314. CACHE_PERROR("enablecache: could not allocate cache data");
  315. return (0);
  316. }
  317. BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
  318. uc->uc_fifo = ALLOC(cache_ptr, size);
  319. if (uc->uc_fifo == NULL) {
  320. CACHE_PERROR("enablecache: could not allocate cache fifo");
  321. return (0);
  322. }
  323. BZERO(uc->uc_fifo, cache_ptr, size);
  324. su->su_cache = (char *) uc;
  325. return (1);
  326. }
  327. /*
  328. * Set an entry in the cache
  329. */
  330. static void cache_set(xprt, replylen)
  331. SVCXPRT *xprt;
  332. u_long replylen;
  333. {
  334. register cache_ptr victim;
  335. register cache_ptr *vicp;
  336. register struct svcudp_data *su = su_data(xprt);
  337. struct udp_cache *uc = (struct udp_cache *) su->su_cache;
  338. u_int loc;
  339. char *newbuf;
  340. /*
  341. * Find space for the new entry, either by
  342. * reusing an old entry, or by mallocing a new one
  343. */
  344. victim = uc->uc_fifo[uc->uc_nextvictim];
  345. if (victim != NULL) {
  346. loc = CACHE_LOC(xprt, victim->cache_xid);
  347. for (vicp = &uc->uc_entries[loc];
  348. *vicp != NULL && *vicp != victim;
  349. vicp = &(*vicp)->cache_next);
  350. if (*vicp == NULL) {
  351. CACHE_PERROR("cache_set: victim not found");
  352. return;
  353. }
  354. *vicp = victim->cache_next; /* remote from cache */
  355. newbuf = victim->cache_reply;
  356. } else {
  357. victim = ALLOC(struct cache_node, 1);
  358. if (victim == NULL) {
  359. CACHE_PERROR("cache_set: victim alloc failed");
  360. return;
  361. }
  362. newbuf = mem_alloc(su->su_iosz);
  363. if (newbuf == NULL) {
  364. CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
  365. return;
  366. }
  367. }
  368. /*
  369. * Store it away
  370. */
  371. victim->cache_replylen = replylen;
  372. victim->cache_reply = rpc_buffer(xprt);
  373. rpc_buffer(xprt) = newbuf;
  374. xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
  375. XDR_ENCODE);
  376. victim->cache_xid = su->su_xid;
  377. victim->cache_proc = uc->uc_proc;
  378. victim->cache_vers = uc->uc_vers;
  379. victim->cache_prog = uc->uc_prog;
  380. victim->cache_addr = uc->uc_addr;
  381. loc = CACHE_LOC(xprt, victim->cache_xid);
  382. victim->cache_next = uc->uc_entries[loc];
  383. uc->uc_entries[loc] = victim;
  384. uc->uc_fifo[uc->uc_nextvictim++] = victim;
  385. uc->uc_nextvictim %= uc->uc_size;
  386. }
  387. /*
  388. * Try to get an entry from the cache
  389. * return 1 if found, 0 if not found
  390. */
  391. static int cache_get(xprt, msg, replyp, replylenp)
  392. SVCXPRT *xprt;
  393. struct rpc_msg *msg;
  394. char **replyp;
  395. u_long *replylenp;
  396. {
  397. u_int loc;
  398. register cache_ptr ent;
  399. register struct svcudp_data *su = su_data(xprt);
  400. register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
  401. # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
  402. loc = CACHE_LOC(xprt, su->su_xid);
  403. for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
  404. if (ent->cache_xid == su->su_xid &&
  405. ent->cache_proc == uc->uc_proc &&
  406. ent->cache_vers == uc->uc_vers &&
  407. ent->cache_prog == uc->uc_prog &&
  408. EQADDR(ent->cache_addr, uc->uc_addr)) {
  409. *replyp = ent->cache_reply;
  410. *replylenp = ent->cache_replylen;
  411. return (1);
  412. }
  413. }
  414. /*
  415. * Failed to find entry
  416. * Remember a few things so we can do a set later
  417. */
  418. uc->uc_proc = msg->rm_call.cb_proc;
  419. uc->uc_vers = msg->rm_call.cb_vers;
  420. uc->uc_prog = msg->rm_call.cb_prog;
  421. uc->uc_addr = xprt->xp_raddr;
  422. return (0);
  423. }