clnt_udp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 *clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz,
  98. 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, sendsz, XDR_ENCODE);
  152. if (!xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
  153. goto fooy;
  154. }
  155. cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  156. if (*sockp < 0) {
  157. int dontblock = 1;
  158. *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  159. if (*sockp < 0) {
  160. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  161. rpc_createerr.cf_error.re_errno = errno;
  162. goto fooy;
  163. }
  164. /* attempt to bind to prov port */
  165. (void) bindresvport(*sockp, (struct sockaddr_in *) 0);
  166. /* the sockets rpc controls are non-blocking */
  167. (void) ioctl(*sockp, FIONBIO, (char *) &dontblock);
  168. cu->cu_closeit = TRUE;
  169. } else {
  170. cu->cu_closeit = FALSE;
  171. }
  172. cu->cu_sock = *sockp;
  173. cl->cl_auth = authnone_create();
  174. return (cl);
  175. fooy:
  176. if (cu)
  177. mem_free((caddr_t) cu, sizeof(*cu) + sendsz + recvsz);
  178. if (cl)
  179. mem_free((caddr_t) cl, sizeof(CLIENT));
  180. return ((CLIENT *) NULL);
  181. }
  182. CLIENT *clntudp_create(raddr, program, version, wait, sockp)
  183. struct sockaddr_in *raddr;
  184. u_long program;
  185. u_long version;
  186. struct timeval wait;
  187. register int *sockp;
  188. {
  189. return (clntudp_bufcreate(raddr, program, version, wait, sockp,
  190. UDPMSGSIZE, UDPMSGSIZE));
  191. }
  192. static enum clnt_stat
  193. clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
  194. register CLIENT *cl; /* client handle */
  195. u_long proc; /* procedure number */
  196. xdrproc_t xargs; /* xdr routine for args */
  197. caddr_t argsp; /* pointer to args */
  198. xdrproc_t xresults; /* xdr routine for results */
  199. caddr_t resultsp; /* pointer to results */
  200. struct timeval utimeout; /* seconds to wait before giving up */
  201. {
  202. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  203. register XDR *xdrs;
  204. register int outlen;
  205. register int inlen;
  206. int fromlen;
  207. #ifdef FD_SETSIZE
  208. fd_set readfds;
  209. fd_set mask;
  210. #else
  211. int readfds;
  212. register int mask;
  213. #endif /* def FD_SETSIZE */
  214. struct sockaddr_in from;
  215. struct rpc_msg reply_msg;
  216. XDR reply_xdrs;
  217. struct timeval time_waited;
  218. bool_t ok;
  219. int nrefreshes = 2; /* number of times to refresh cred */
  220. struct timeval timeout;
  221. if (cu->cu_total.tv_usec == -1) {
  222. timeout = utimeout; /* use supplied timeout */
  223. } else {
  224. timeout = cu->cu_total; /* use default timeout */
  225. }
  226. time_waited.tv_sec = 0;
  227. time_waited.tv_usec = 0;
  228. call_again:
  229. xdrs = &(cu->cu_outxdrs);
  230. xdrs->x_op = XDR_ENCODE;
  231. XDR_SETPOS(xdrs, cu->cu_xdrpos);
  232. /*
  233. * the transaction is the first thing in the out buffer
  234. */
  235. (*(u_short *) (cu->cu_outbuf))++;
  236. if ((!XDR_PUTLONG(xdrs, (long *) &proc)) ||
  237. (!AUTH_MARSHALL(cl->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp)))
  238. return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
  239. outlen = (int) XDR_GETPOS(xdrs);
  240. send_again:
  241. if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  242. (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
  243. != outlen) {
  244. cu->cu_error.re_errno = errno;
  245. return (cu->cu_error.re_status = RPC_CANTSEND);
  246. }
  247. /*
  248. * Hack to provide rpc-based message passing
  249. */
  250. if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  251. return (cu->cu_error.re_status = RPC_TIMEDOUT);
  252. }
  253. /*
  254. * sub-optimal code appears here because we have
  255. * some clock time to spare while the packets are in flight.
  256. * (We assume that this is actually only executed once.)
  257. */
  258. reply_msg.acpted_rply.ar_verf = _null_auth;
  259. reply_msg.acpted_rply.ar_results.where = resultsp;
  260. reply_msg.acpted_rply.ar_results.proc = xresults;
  261. #ifdef FD_SETSIZE
  262. FD_ZERO(&mask);
  263. FD_SET(cu->cu_sock, &mask);
  264. #else
  265. mask = 1 << cu->cu_sock;
  266. #endif /* def FD_SETSIZE */
  267. for (;;) {
  268. readfds = mask;
  269. switch (select(_rpc_dtablesize(), &readfds, (int *) NULL,
  270. (int *) NULL, &(cu->cu_wait))) {
  271. case 0:
  272. time_waited.tv_sec += cu->cu_wait.tv_sec;
  273. time_waited.tv_usec += cu->cu_wait.tv_usec;
  274. while (time_waited.tv_usec >= 1000000) {
  275. time_waited.tv_sec++;
  276. time_waited.tv_usec -= 1000000;
  277. }
  278. if ((time_waited.tv_sec < timeout.tv_sec) ||
  279. ((time_waited.tv_sec == timeout.tv_sec) &&
  280. (time_waited.tv_usec < timeout.tv_usec)))
  281. goto send_again;
  282. return (cu->cu_error.re_status = RPC_TIMEDOUT);
  283. /*
  284. * buggy in other cases because time_waited is not being
  285. * updated.
  286. */
  287. case -1:
  288. if (errno == EINTR)
  289. continue;
  290. cu->cu_error.re_errno = errno;
  291. return (cu->cu_error.re_status = RPC_CANTRECV);
  292. }
  293. do {
  294. fromlen = sizeof(struct sockaddr);
  295. inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
  296. (int) cu->cu_recvsz, 0,
  297. (struct sockaddr *) &from, &fromlen);
  298. } while (inlen < 0 && errno == EINTR);
  299. if (inlen < 0) {
  300. if (errno == EWOULDBLOCK)
  301. continue;
  302. cu->cu_error.re_errno = errno;
  303. return (cu->cu_error.re_status = RPC_CANTRECV);
  304. }
  305. if (inlen < sizeof(u_long))
  306. continue;
  307. /* see if reply transaction id matches sent id */
  308. if (*((u_long *) (cu->cu_inbuf)) != *((u_long *) (cu->cu_outbuf)))
  309. continue;
  310. /* we now assume we have the proper reply */
  311. break;
  312. }
  313. /*
  314. * now decode and validate the response
  315. */
  316. xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
  317. ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  318. /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
  319. if (ok) {
  320. _seterr_reply(&reply_msg, &(cu->cu_error));
  321. if (cu->cu_error.re_status == RPC_SUCCESS) {
  322. if (!AUTH_VALIDATE(cl->cl_auth,
  323. &reply_msg.acpted_rply.ar_verf)) {
  324. cu->cu_error.re_status = RPC_AUTHERROR;
  325. cu->cu_error.re_why = AUTH_INVALIDRESP;
  326. }
  327. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  328. xdrs->x_op = XDR_FREE;
  329. (void) xdr_opaque_auth(xdrs,
  330. &(reply_msg.acpted_rply.ar_verf));
  331. }
  332. } /* end successful completion */
  333. else {
  334. /* maybe our credentials need to be refreshed ... */
  335. if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
  336. nrefreshes--;
  337. goto call_again;
  338. }
  339. } /* end of unsuccessful completion */
  340. } /* end of valid reply message */
  341. else {
  342. cu->cu_error.re_status = RPC_CANTDECODERES;
  343. }
  344. return (cu->cu_error.re_status);
  345. }
  346. static void clntudp_geterr(cl, errp)
  347. CLIENT *cl;
  348. struct rpc_err *errp;
  349. {
  350. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  351. *errp = cu->cu_error;
  352. }
  353. static bool_t clntudp_freeres(cl, xdr_res, res_ptr)
  354. CLIENT *cl;
  355. xdrproc_t xdr_res;
  356. caddr_t res_ptr;
  357. {
  358. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  359. register XDR *xdrs = &(cu->cu_outxdrs);
  360. xdrs->x_op = XDR_FREE;
  361. return ((*xdr_res) (xdrs, res_ptr));
  362. }
  363. static void clntudp_abort( /*h */ )
  364. /*CLIENT *h; */
  365. {
  366. }
  367. static bool_t clntudp_control(cl, request, info)
  368. CLIENT *cl;
  369. int request;
  370. char *info;
  371. {
  372. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  373. switch (request) {
  374. case CLSET_TIMEOUT:
  375. cu->cu_total = *(struct timeval *) info;
  376. break;
  377. case CLGET_TIMEOUT:
  378. *(struct timeval *) info = cu->cu_total;
  379. break;
  380. case CLSET_RETRY_TIMEOUT:
  381. cu->cu_wait = *(struct timeval *) info;
  382. break;
  383. case CLGET_RETRY_TIMEOUT:
  384. *(struct timeval *) info = cu->cu_wait;
  385. break;
  386. case CLGET_SERVER_ADDR:
  387. *(struct sockaddr_in *) info = cu->cu_raddr;
  388. break;
  389. default:
  390. return (FALSE);
  391. }
  392. return (TRUE);
  393. }
  394. static void clntudp_destroy(cl)
  395. CLIENT *cl;
  396. {
  397. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  398. if (cu->cu_closeit) {
  399. (void) close(cu->cu_sock);
  400. }
  401. XDR_DESTROY(&(cu->cu_outxdrs));
  402. mem_free((caddr_t) cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
  403. mem_free((caddr_t) cl, sizeof(CLIENT));
  404. }