svc_udp.c 12 KB

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