svc_udp.c 12 KB

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