clnt_udp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* @(#)clnt_udp.c 2.2 88/08/01 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[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * clnt_udp.c, Implements a UDP/IP based, client side RPC.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. */
  38. #include <stdio.h>
  39. #include <rpc/rpc.h>
  40. #include <sys/socket.h>
  41. #include <sys/ioctl.h>
  42. #include <netdb.h>
  43. #include <errno.h>
  44. #include <rpc/pmap_clnt.h>
  45. extern int errno;
  46. /*
  47. * UDP bases client side rpc operations
  48. */
  49. static enum clnt_stat clntudp_call();
  50. static void clntudp_abort();
  51. static void clntudp_geterr();
  52. static bool_t clntudp_freeres();
  53. static bool_t clntudp_control();
  54. static void clntudp_destroy();
  55. static struct clnt_ops udp_ops = {
  56. clntudp_call,
  57. clntudp_abort,
  58. clntudp_geterr,
  59. clntudp_freeres,
  60. clntudp_destroy,
  61. clntudp_control
  62. };
  63. /*
  64. * Private data kept per client handle
  65. */
  66. struct cu_data {
  67. int cu_sock;
  68. bool_t cu_closeit;
  69. struct sockaddr_in cu_raddr;
  70. int cu_rlen;
  71. struct timeval cu_wait;
  72. struct timeval cu_total;
  73. struct rpc_err cu_error;
  74. XDR cu_outxdrs;
  75. u_int cu_xdrpos;
  76. u_int cu_sendsz;
  77. char *cu_outbuf;
  78. u_int cu_recvsz;
  79. char cu_inbuf[1];
  80. };
  81. /*
  82. * Create a UDP based client handle.
  83. * If *sockp<0, *sockp is set to a newly created UPD socket.
  84. * If raddr->sin_port is 0 a binder on the remote machine
  85. * is consulted for the correct port number.
  86. * NB: It is the clients responsibility to close *sockp.
  87. * NB: The rpch->cl_auth is initialized to null authentication.
  88. * Caller may wish to set this something more useful.
  89. *
  90. * wait is the amount of time used between retransmitting a call if
  91. * no response has been heard; retransmition occurs until the actual
  92. * rpc call times out.
  93. *
  94. * sendsz and recvsz are the maximum allowable packet sizes that can be
  95. * sent and received.
  96. */
  97. CLIENT *
  98. clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
  99. struct sockaddr_in *raddr;
  100. u_long program;
  101. u_long version;
  102. struct timeval wait;
  103. register int *sockp;
  104. u_int sendsz;
  105. u_int recvsz;
  106. {
  107. CLIENT *cl;
  108. register struct cu_data *cu;
  109. struct timeval now;
  110. struct rpc_msg call_msg;
  111. cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
  112. if (cl == NULL) {
  113. (void) fprintf(stderr, "clntudp_create: out of memory\n");
  114. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  115. rpc_createerr.cf_error.re_errno = errno;
  116. goto fooy;
  117. }
  118. sendsz = ((sendsz + 3) / 4) * 4;
  119. recvsz = ((recvsz + 3) / 4) * 4;
  120. cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
  121. if (cu == NULL) {
  122. (void) fprintf(stderr, "clntudp_create: out of memory\n");
  123. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  124. rpc_createerr.cf_error.re_errno = errno;
  125. goto fooy;
  126. }
  127. cu->cu_outbuf = &cu->cu_inbuf[recvsz];
  128. (void)gettimeofday(&now, (struct timezone *)0);
  129. if (raddr->sin_port == 0) {
  130. u_short port;
  131. if ((port =
  132. pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
  133. goto fooy;
  134. }
  135. raddr->sin_port = htons(port);
  136. }
  137. cl->cl_ops = &udp_ops;
  138. cl->cl_private = (caddr_t)cu;
  139. cu->cu_raddr = *raddr;
  140. cu->cu_rlen = sizeof (cu->cu_raddr);
  141. cu->cu_wait = wait;
  142. cu->cu_total.tv_sec = -1;
  143. cu->cu_total.tv_usec = -1;
  144. cu->cu_sendsz = sendsz;
  145. cu->cu_recvsz = recvsz;
  146. call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
  147. call_msg.rm_direction = CALL;
  148. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  149. call_msg.rm_call.cb_prog = program;
  150. call_msg.rm_call.cb_vers = version;
  151. xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
  152. sendsz, XDR_ENCODE);
  153. if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
  154. goto fooy;
  155. }
  156. cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  157. if (*sockp < 0) {
  158. int dontblock = 1;
  159. *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  160. if (*sockp < 0) {
  161. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  162. rpc_createerr.cf_error.re_errno = errno;
  163. goto fooy;
  164. }
  165. /* attempt to bind to prov port */
  166. (void)bindresvport(*sockp, (struct sockaddr_in *)0);
  167. /* the sockets rpc controls are non-blocking */
  168. (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
  169. cu->cu_closeit = TRUE;
  170. } else {
  171. cu->cu_closeit = FALSE;
  172. }
  173. cu->cu_sock = *sockp;
  174. cl->cl_auth = authnone_create();
  175. return (cl);
  176. fooy:
  177. if (cu)
  178. mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
  179. if (cl)
  180. mem_free((caddr_t)cl, sizeof(CLIENT));
  181. return ((CLIENT *)NULL);
  182. }
  183. CLIENT *
  184. clntudp_create(raddr, program, version, wait, sockp)
  185. struct sockaddr_in *raddr;
  186. u_long program;
  187. u_long version;
  188. struct timeval wait;
  189. register int *sockp;
  190. {
  191. return(clntudp_bufcreate(raddr, program, version, wait, sockp,
  192. UDPMSGSIZE, UDPMSGSIZE));
  193. }
  194. static enum clnt_stat
  195. clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
  196. register CLIENT *cl; /* client handle */
  197. u_long proc; /* procedure number */
  198. xdrproc_t xargs; /* xdr routine for args */
  199. caddr_t argsp; /* pointer to args */
  200. xdrproc_t xresults; /* xdr routine for results */
  201. caddr_t resultsp; /* pointer to results */
  202. struct timeval utimeout; /* seconds to wait before giving up */
  203. {
  204. register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  205. register XDR *xdrs;
  206. register int outlen;
  207. register int inlen;
  208. int fromlen;
  209. #ifdef FD_SETSIZE
  210. fd_set readfds;
  211. fd_set mask;
  212. #else
  213. int readfds;
  214. register int mask;
  215. #endif /* def FD_SETSIZE */
  216. struct sockaddr_in from;
  217. struct rpc_msg reply_msg;
  218. XDR reply_xdrs;
  219. struct timeval time_waited;
  220. bool_t ok;
  221. int nrefreshes = 2; /* number of times to refresh cred */
  222. struct timeval timeout;
  223. if (cu->cu_total.tv_usec == -1) {
  224. timeout = utimeout; /* use supplied timeout */
  225. } else {
  226. timeout = cu->cu_total; /* use default timeout */
  227. }
  228. time_waited.tv_sec = 0;
  229. time_waited.tv_usec = 0;
  230. call_again:
  231. xdrs = &(cu->cu_outxdrs);
  232. xdrs->x_op = XDR_ENCODE;
  233. XDR_SETPOS(xdrs, cu->cu_xdrpos);
  234. /*
  235. * the transaction is the first thing in the out buffer
  236. */
  237. (*(u_short *)(cu->cu_outbuf))++;
  238. if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  239. (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
  240. (! (*xargs)(xdrs, argsp)))
  241. return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
  242. outlen = (int)XDR_GETPOS(xdrs);
  243. send_again:
  244. if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  245. (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
  246. != outlen) {
  247. cu->cu_error.re_errno = errno;
  248. return (cu->cu_error.re_status = RPC_CANTSEND);
  249. }
  250. /*
  251. * Hack to provide rpc-based message passing
  252. */
  253. if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  254. return (cu->cu_error.re_status = RPC_TIMEDOUT);
  255. }
  256. /*
  257. * sub-optimal code appears here because we have
  258. * some clock time to spare while the packets are in flight.
  259. * (We assume that this is actually only executed once.)
  260. */
  261. reply_msg.acpted_rply.ar_verf = _null_auth;
  262. reply_msg.acpted_rply.ar_results.where = resultsp;
  263. reply_msg.acpted_rply.ar_results.proc = xresults;
  264. #ifdef FD_SETSIZE
  265. FD_ZERO(&mask);
  266. FD_SET(cu->cu_sock, &mask);
  267. #else
  268. mask = 1 << cu->cu_sock;
  269. #endif /* def FD_SETSIZE */
  270. for (;;) {
  271. readfds = mask;
  272. switch (select(_rpc_dtablesize(), &readfds, (int *)NULL,
  273. (int *)NULL, &(cu->cu_wait))) {
  274. case 0:
  275. time_waited.tv_sec += cu->cu_wait.tv_sec;
  276. time_waited.tv_usec += cu->cu_wait.tv_usec;
  277. while (time_waited.tv_usec >= 1000000) {
  278. time_waited.tv_sec++;
  279. time_waited.tv_usec -= 1000000;
  280. }
  281. if ((time_waited.tv_sec < timeout.tv_sec) ||
  282. ((time_waited.tv_sec == timeout.tv_sec) &&
  283. (time_waited.tv_usec < timeout.tv_usec)))
  284. goto send_again;
  285. return (cu->cu_error.re_status = RPC_TIMEDOUT);
  286. /*
  287. * buggy in other cases because time_waited is not being
  288. * updated.
  289. */
  290. case -1:
  291. if (errno == EINTR)
  292. continue;
  293. cu->cu_error.re_errno = errno;
  294. return (cu->cu_error.re_status = RPC_CANTRECV);
  295. }
  296. do {
  297. fromlen = sizeof(struct sockaddr);
  298. inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
  299. (int) cu->cu_recvsz, 0,
  300. (struct sockaddr *)&from, &fromlen);
  301. } while (inlen < 0 && errno == EINTR);
  302. if (inlen < 0) {
  303. if (errno == EWOULDBLOCK)
  304. continue;
  305. cu->cu_error.re_errno = errno;
  306. return (cu->cu_error.re_status = RPC_CANTRECV);
  307. }
  308. if (inlen < sizeof(u_long))
  309. continue;
  310. /* see if reply transaction id matches sent id */
  311. if (*((u_long *)(cu->cu_inbuf)) != *((u_long *)(cu->cu_outbuf)))
  312. continue;
  313. /* we now assume we have the proper reply */
  314. break;
  315. }
  316. /*
  317. * now decode and validate the response
  318. */
  319. xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
  320. ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  321. /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
  322. if (ok) {
  323. _seterr_reply(&reply_msg, &(cu->cu_error));
  324. if (cu->cu_error.re_status == RPC_SUCCESS) {
  325. if (! AUTH_VALIDATE(cl->cl_auth,
  326. &reply_msg.acpted_rply.ar_verf)) {
  327. cu->cu_error.re_status = RPC_AUTHERROR;
  328. cu->cu_error.re_why = AUTH_INVALIDRESP;
  329. }
  330. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  331. xdrs->x_op = XDR_FREE;
  332. (void)xdr_opaque_auth(xdrs,
  333. &(reply_msg.acpted_rply.ar_verf));
  334. }
  335. } /* end successful completion */
  336. else {
  337. /* maybe our credentials need to be refreshed ... */
  338. if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
  339. nrefreshes--;
  340. goto call_again;
  341. }
  342. } /* end of unsuccessful completion */
  343. } /* end of valid reply message */
  344. else {
  345. cu->cu_error.re_status = RPC_CANTDECODERES;
  346. }
  347. return (cu->cu_error.re_status);
  348. }
  349. static void
  350. clntudp_geterr(cl, errp)
  351. CLIENT *cl;
  352. struct rpc_err *errp;
  353. {
  354. register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  355. *errp = cu->cu_error;
  356. }
  357. static bool_t
  358. clntudp_freeres(cl, xdr_res, res_ptr)
  359. CLIENT *cl;
  360. xdrproc_t xdr_res;
  361. caddr_t res_ptr;
  362. {
  363. register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  364. register XDR *xdrs = &(cu->cu_outxdrs);
  365. xdrs->x_op = XDR_FREE;
  366. return ((*xdr_res)(xdrs, res_ptr));
  367. }
  368. static void
  369. clntudp_abort(/*h*/)
  370. /*CLIENT *h;*/
  371. {
  372. }
  373. static bool_t
  374. clntudp_control(cl, request, info)
  375. CLIENT *cl;
  376. int request;
  377. char *info;
  378. {
  379. register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  380. switch (request) {
  381. case CLSET_TIMEOUT:
  382. cu->cu_total = *(struct timeval *)info;
  383. break;
  384. case CLGET_TIMEOUT:
  385. *(struct timeval *)info = cu->cu_total;
  386. break;
  387. case CLSET_RETRY_TIMEOUT:
  388. cu->cu_wait = *(struct timeval *)info;
  389. break;
  390. case CLGET_RETRY_TIMEOUT:
  391. *(struct timeval *)info = cu->cu_wait;
  392. break;
  393. case CLGET_SERVER_ADDR:
  394. *(struct sockaddr_in *)info = cu->cu_raddr;
  395. break;
  396. default:
  397. return (FALSE);
  398. }
  399. return (TRUE);
  400. }
  401. static void
  402. clntudp_destroy(cl)
  403. CLIENT *cl;
  404. {
  405. register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  406. if (cu->cu_closeit) {
  407. (void)close(cu->cu_sock);
  408. }
  409. XDR_DESTROY(&(cu->cu_outxdrs));
  410. mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
  411. mem_free((caddr_t)cl, sizeof(CLIENT));
  412. }