clnt_udp.c 12 KB

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