svc_udp.c 12 KB

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