clnt_udp.c 12 KB

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