clnt_tcp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* @(#)clnt_tcp.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_tcp.c, Implements a TCP/IP based, client side RPC.
  34. *
  35. * Copyright (C) 1984, Sun Microsystems, Inc.
  36. *
  37. * TCP based RPC supports 'batched calls'.
  38. * A sequence of calls may be batched-up in a send buffer. The rpc call
  39. * return immediately to the client even though the call was not necessarily
  40. * sent. The batching occurs if the results' xdr routine is NULL (0) AND
  41. * the rpc timeout value is zero (see clnt.h, rpc).
  42. *
  43. * Clients should NOT casually batch calls that in fact return results; that is,
  44. * the server side should be aware that a call is batched and not produce any
  45. * return message. Batched calls that produce many result messages can
  46. * deadlock (netlock) the client and the server....
  47. *
  48. * Now go hang yourself.
  49. */
  50. #include <stdio.h>
  51. #include <rpc/rpc.h>
  52. #include <sys/socket.h>
  53. #include <netdb.h>
  54. #include <errno.h>
  55. #include <rpc/pmap_clnt.h>
  56. #include <unistd.h>
  57. #define MCALL_MSG_SIZE 24
  58. extern int errno;
  59. static int readtcp();
  60. static int writetcp();
  61. static enum clnt_stat clnttcp_call();
  62. static void clnttcp_abort();
  63. static void clnttcp_geterr();
  64. static bool_t clnttcp_freeres();
  65. static bool_t clnttcp_control();
  66. static void clnttcp_destroy();
  67. static struct clnt_ops tcp_ops = {
  68. clnttcp_call,
  69. clnttcp_abort,
  70. clnttcp_geterr,
  71. clnttcp_freeres,
  72. clnttcp_destroy,
  73. clnttcp_control
  74. };
  75. struct ct_data {
  76. int ct_sock;
  77. bool_t ct_closeit;
  78. struct timeval ct_wait;
  79. bool_t ct_waitset; /* wait set by clnt_control? */
  80. struct sockaddr_in ct_addr;
  81. struct rpc_err ct_error;
  82. char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
  83. u_int ct_mpos; /* pos after marshal */
  84. XDR ct_xdrs;
  85. };
  86. /*
  87. * Create a client handle for a tcp/ip connection.
  88. * If *sockp<0, *sockp is set to a newly created TCP socket and it is
  89. * connected to raddr. If *sockp non-negative then
  90. * raddr is ignored. The rpc/tcp package does buffering
  91. * similar to stdio, so the client must pick send and receive buffer sizes,];
  92. * 0 => use the default.
  93. * If raddr->sin_port is 0, then a binder on the remote machine is
  94. * consulted for the right port number.
  95. * NB: *sockp is copied into a private area.
  96. * NB: It is the clients responsibility to close *sockp.
  97. * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
  98. * something more useful.
  99. */
  100. CLIENT *clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
  101. struct sockaddr_in *raddr;
  102. u_long prog;
  103. u_long vers;
  104. register int *sockp;
  105. u_int sendsz;
  106. u_int recvsz;
  107. {
  108. CLIENT *h;
  109. register struct ct_data *ct;
  110. struct timeval now;
  111. struct rpc_msg call_msg;
  112. ct = NULL; /* in case of fooy */
  113. h = (CLIENT *) mem_alloc(sizeof(*h));
  114. if (h == NULL) {
  115. (void) fprintf(stderr, "clnttcp_create: out of memory\n");
  116. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  117. rpc_createerr.cf_error.re_errno = errno;
  118. goto fooy;
  119. }
  120. ct = (struct ct_data *) mem_alloc(sizeof(*ct));
  121. if (ct == NULL) {
  122. (void) fprintf(stderr, "clnttcp_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. /*
  128. * If no port number given ask the pmap for one
  129. */
  130. if (raddr->sin_port == 0) {
  131. u_short port;
  132. if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
  133. mem_free((caddr_t) ct, sizeof(struct ct_data));
  134. mem_free((caddr_t) h, sizeof(CLIENT));
  135. return ((CLIENT *) NULL);
  136. }
  137. raddr->sin_port = htons(port);
  138. }
  139. /*
  140. * If no socket given, open one
  141. */
  142. if (*sockp < 0) {
  143. *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  144. (void) bindresvport(*sockp, (struct sockaddr_in *) 0);
  145. if ((*sockp < 0)
  146. || (connect(*sockp, (struct sockaddr *) raddr,
  147. sizeof(*raddr)) < 0)) {
  148. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  149. rpc_createerr.cf_error.re_errno = errno;
  150. (void) close(*sockp);
  151. goto fooy;
  152. }
  153. ct->ct_closeit = TRUE;
  154. } else {
  155. ct->ct_closeit = FALSE;
  156. }
  157. /*
  158. * Set up private data struct
  159. */
  160. ct->ct_sock = *sockp;
  161. ct->ct_wait.tv_usec = 0;
  162. ct->ct_waitset = FALSE;
  163. ct->ct_addr = *raddr;
  164. /*
  165. * Initialize call message
  166. */
  167. (void) gettimeofday(&now, (struct timezone *) 0);
  168. call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
  169. call_msg.rm_direction = CALL;
  170. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  171. call_msg.rm_call.cb_prog = prog;
  172. call_msg.rm_call.cb_vers = vers;
  173. /*
  174. * pre-serialize the staic part of the call msg and stash it away
  175. */
  176. xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
  177. XDR_ENCODE);
  178. if (!xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
  179. if (ct->ct_closeit) {
  180. (void) close(*sockp);
  181. }
  182. goto fooy;
  183. }
  184. ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
  185. XDR_DESTROY(&(ct->ct_xdrs));
  186. /*
  187. * Create a client handle which uses xdrrec for serialization
  188. * and authnone for authentication.
  189. */
  190. xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
  191. (caddr_t) ct, readtcp, writetcp);
  192. h->cl_ops = &tcp_ops;
  193. h->cl_private = (caddr_t) ct;
  194. h->cl_auth = authnone_create();
  195. return (h);
  196. fooy:
  197. /*
  198. * Something goofed, free stuff and barf
  199. */
  200. mem_free((caddr_t) ct, sizeof(struct ct_data));
  201. mem_free((caddr_t) h, sizeof(CLIENT));
  202. return ((CLIENT *) NULL);
  203. }
  204. static enum clnt_stat
  205. clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr,
  206. timeout)
  207. register CLIENT *h;
  208. u_long proc;
  209. xdrproc_t xdr_args;
  210. caddr_t args_ptr;
  211. xdrproc_t xdr_results;
  212. caddr_t results_ptr;
  213. struct timeval timeout;
  214. {
  215. register struct ct_data *ct = (struct ct_data *) h->cl_private;
  216. register XDR *xdrs = &(ct->ct_xdrs);
  217. struct rpc_msg reply_msg;
  218. u_long x_id;
  219. u_long *msg_x_id = (u_long *) (ct->ct_mcall); /* yuk */
  220. register bool_t shipnow;
  221. int refreshes = 2;
  222. if (!ct->ct_waitset) {
  223. ct->ct_wait = timeout;
  224. }
  225. shipnow =
  226. (xdr_results == (xdrproc_t) 0 && timeout.tv_sec == 0
  227. && timeout.tv_usec == 0) ? FALSE : TRUE;
  228. call_again:
  229. xdrs->x_op = XDR_ENCODE;
  230. ct->ct_error.re_status = RPC_SUCCESS;
  231. x_id = ntohl(--(*msg_x_id));
  232. if ((!XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  233. (!XDR_PUTLONG(xdrs, (long *) &proc)) ||
  234. (!AUTH_MARSHALL(h->cl_auth, xdrs)) ||
  235. (!(*xdr_args) (xdrs, args_ptr))) {
  236. if (ct->ct_error.re_status == RPC_SUCCESS)
  237. ct->ct_error.re_status = RPC_CANTENCODEARGS;
  238. (void) xdrrec_endofrecord(xdrs, TRUE);
  239. return (ct->ct_error.re_status);
  240. }
  241. if (!xdrrec_endofrecord(xdrs, shipnow))
  242. return (ct->ct_error.re_status = RPC_CANTSEND);
  243. if (!shipnow)
  244. return (RPC_SUCCESS);
  245. /*
  246. * Hack to provide rpc-based message passing
  247. */
  248. if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  249. return (ct->ct_error.re_status = RPC_TIMEDOUT);
  250. }
  251. /*
  252. * Keep receiving until we get a valid transaction id
  253. */
  254. xdrs->x_op = XDR_DECODE;
  255. while (TRUE) {
  256. reply_msg.acpted_rply.ar_verf = _null_auth;
  257. reply_msg.acpted_rply.ar_results.where = NULL;
  258. reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
  259. if (!xdrrec_skiprecord(xdrs))
  260. return (ct->ct_error.re_status);
  261. /* now decode and validate the response header */
  262. if (!xdr_replymsg(xdrs, &reply_msg)) {
  263. if (ct->ct_error.re_status == RPC_SUCCESS)
  264. continue;
  265. return (ct->ct_error.re_status);
  266. }
  267. if (reply_msg.rm_xid == x_id)
  268. break;
  269. }
  270. /*
  271. * process header
  272. */
  273. _seterr_reply(&reply_msg, &(ct->ct_error));
  274. if (ct->ct_error.re_status == RPC_SUCCESS) {
  275. if (!AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
  276. ct->ct_error.re_status = RPC_AUTHERROR;
  277. ct->ct_error.re_why = AUTH_INVALIDRESP;
  278. } else if (!(*xdr_results) (xdrs, results_ptr)) {
  279. if (ct->ct_error.re_status == RPC_SUCCESS)
  280. ct->ct_error.re_status = RPC_CANTDECODERES;
  281. }
  282. /* free verifier ... */
  283. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  284. xdrs->x_op = XDR_FREE;
  285. (void) xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  286. }
  287. } /* end successful completion */
  288. else {
  289. /* maybe our credentials need to be refreshed ... */
  290. if (refreshes-- && AUTH_REFRESH(h->cl_auth))
  291. goto call_again;
  292. } /* end of unsuccessful completion */
  293. return (ct->ct_error.re_status);
  294. }
  295. static void clnttcp_geterr(h, errp)
  296. CLIENT *h;
  297. struct rpc_err *errp;
  298. {
  299. register struct ct_data *ct = (struct ct_data *) h->cl_private;
  300. *errp = ct->ct_error;
  301. }
  302. static bool_t clnttcp_freeres(cl, xdr_res, res_ptr)
  303. CLIENT *cl;
  304. xdrproc_t xdr_res;
  305. caddr_t res_ptr;
  306. {
  307. register struct ct_data *ct = (struct ct_data *) cl->cl_private;
  308. register XDR *xdrs = &(ct->ct_xdrs);
  309. xdrs->x_op = XDR_FREE;
  310. return ((*xdr_res) (xdrs, res_ptr));
  311. }
  312. static void clnttcp_abort()
  313. {
  314. }
  315. static bool_t clnttcp_control(cl, request, info)
  316. CLIENT *cl;
  317. int request;
  318. char *info;
  319. {
  320. register struct ct_data *ct = (struct ct_data *) cl->cl_private;
  321. switch (request) {
  322. case CLSET_TIMEOUT:
  323. ct->ct_wait = *(struct timeval *) info;
  324. ct->ct_waitset = TRUE;
  325. break;
  326. case CLGET_TIMEOUT:
  327. *(struct timeval *) info = ct->ct_wait;
  328. break;
  329. case CLGET_SERVER_ADDR:
  330. *(struct sockaddr_in *) info = ct->ct_addr;
  331. break;
  332. default:
  333. return (FALSE);
  334. }
  335. return (TRUE);
  336. }
  337. static void clnttcp_destroy(h)
  338. CLIENT *h;
  339. {
  340. register struct ct_data *ct = (struct ct_data *) h->cl_private;
  341. if (ct->ct_closeit) {
  342. (void) close(ct->ct_sock);
  343. }
  344. XDR_DESTROY(&(ct->ct_xdrs));
  345. mem_free((caddr_t) ct, sizeof(struct ct_data));
  346. mem_free((caddr_t) h, sizeof(CLIENT));
  347. }
  348. /*
  349. * Interface between xdr serializer and tcp connection.
  350. * Behaves like the system calls, read & write, but keeps some error state
  351. * around for the rpc level.
  352. */
  353. static int readtcp(ct, buf, len)
  354. register struct ct_data *ct;
  355. caddr_t buf;
  356. register int len;
  357. {
  358. #ifdef FD_SETSIZE
  359. fd_set mask;
  360. fd_set readfds;
  361. if (len == 0)
  362. return (0);
  363. FD_ZERO(&mask);
  364. FD_SET(ct->ct_sock, &mask);
  365. #else
  366. register int mask = 1 << (ct->ct_sock);
  367. int readfds;
  368. if (len == 0)
  369. return (0);
  370. #endif /* def FD_SETSIZE */
  371. while (TRUE) {
  372. readfds = mask;
  373. switch (select
  374. (_rpc_dtablesize(), &readfds, NULL, NULL,
  375. &(ct->ct_wait))) {
  376. case 0:
  377. ct->ct_error.re_status = RPC_TIMEDOUT;
  378. return (-1);
  379. case -1:
  380. if (errno == EINTR)
  381. continue;
  382. ct->ct_error.re_status = RPC_CANTRECV;
  383. ct->ct_error.re_errno = errno;
  384. return (-1);
  385. }
  386. break;
  387. }
  388. switch (len = read(ct->ct_sock, buf, len)) {
  389. case 0:
  390. /* premature eof */
  391. ct->ct_error.re_errno = ECONNRESET;
  392. ct->ct_error.re_status = RPC_CANTRECV;
  393. len = -1; /* it's really an error */
  394. break;
  395. case -1:
  396. ct->ct_error.re_errno = errno;
  397. ct->ct_error.re_status = RPC_CANTRECV;
  398. break;
  399. }
  400. return (len);
  401. }
  402. static int writetcp(ct, buf, len)
  403. struct ct_data *ct;
  404. caddr_t buf;
  405. int len;
  406. {
  407. register int i, cnt;
  408. for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  409. if ((i = write(ct->ct_sock, buf, cnt)) == -1) {
  410. ct->ct_error.re_errno = errno;
  411. ct->ct_error.re_status = RPC_CANTSEND;
  412. return (-1);
  413. }
  414. }
  415. return (len);
  416. }