clnt_tcp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * TCP based RPC supports 'batched calls'.
  39. * A sequence of calls may be batched-up in a send buffer. The rpc call
  40. * return immediately to the client even though the call was not necessarily
  41. * sent. The batching occurs if the results' xdr routine is NULL (0) AND
  42. * the rpc timeout value is zero (see clnt.h, rpc).
  43. *
  44. * Clients should NOT casually batch calls that in fact return results; that is,
  45. * the server side should be aware that a call is batched and not produce any
  46. * return message. Batched calls that produce many result messages can
  47. * deadlock (netlock) the client and the server....
  48. *
  49. * Now go hang yourself.
  50. */
  51. #include <stdio.h>
  52. #include <rpc/rpc.h>
  53. #include <sys/socket.h>
  54. #include <netdb.h>
  55. #include <errno.h>
  56. #include <rpc/pmap_clnt.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 *
  101. clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
  102. struct sockaddr_in *raddr;
  103. u_long prog;
  104. u_long vers;
  105. register int *sockp;
  106. u_int sendsz;
  107. u_int recvsz;
  108. {
  109. CLIENT *h;
  110. register struct ct_data *ct;
  111. struct timeval now;
  112. struct rpc_msg call_msg;
  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, timeout)
  206. register CLIENT *h;
  207. u_long proc;
  208. xdrproc_t xdr_args;
  209. caddr_t args_ptr;
  210. xdrproc_t xdr_results;
  211. caddr_t results_ptr;
  212. struct timeval timeout;
  213. {
  214. register struct ct_data *ct = (struct ct_data *) h->cl_private;
  215. register XDR *xdrs = &(ct->ct_xdrs);
  216. struct rpc_msg reply_msg;
  217. u_long x_id;
  218. u_long *msg_x_id = (u_long *)(ct->ct_mcall); /* yuk */
  219. register bool_t shipnow;
  220. int refreshes = 2;
  221. if (!ct->ct_waitset) {
  222. ct->ct_wait = timeout;
  223. }
  224. shipnow =
  225. (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
  226. && timeout.tv_usec == 0) ? FALSE : TRUE;
  227. call_again:
  228. xdrs->x_op = XDR_ENCODE;
  229. ct->ct_error.re_status = RPC_SUCCESS;
  230. x_id = ntohl(--(*msg_x_id));
  231. if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  232. (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  233. (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
  234. (! (*xdr_args)(xdrs, args_ptr))) {
  235. if (ct->ct_error.re_status == RPC_SUCCESS)
  236. ct->ct_error.re_status = RPC_CANTENCODEARGS;
  237. (void)xdrrec_endofrecord(xdrs, TRUE);
  238. return (ct->ct_error.re_status);
  239. }
  240. if (! xdrrec_endofrecord(xdrs, shipnow))
  241. return (ct->ct_error.re_status = RPC_CANTSEND);
  242. if (! shipnow)
  243. return (RPC_SUCCESS);
  244. /*
  245. * Hack to provide rpc-based message passing
  246. */
  247. if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  248. return(ct->ct_error.re_status = RPC_TIMEDOUT);
  249. }
  250. /*
  251. * Keep receiving until we get a valid transaction id
  252. */
  253. xdrs->x_op = XDR_DECODE;
  254. while (TRUE) {
  255. reply_msg.acpted_rply.ar_verf = _null_auth;
  256. reply_msg.acpted_rply.ar_results.where = NULL;
  257. reply_msg.acpted_rply.ar_results.proc = xdr_void;
  258. if (! xdrrec_skiprecord(xdrs))
  259. return (ct->ct_error.re_status);
  260. /* now decode and validate the response header */
  261. if (! xdr_replymsg(xdrs, &reply_msg)) {
  262. if (ct->ct_error.re_status == RPC_SUCCESS)
  263. continue;
  264. return (ct->ct_error.re_status);
  265. }
  266. if (reply_msg.rm_xid == x_id)
  267. break;
  268. }
  269. /*
  270. * process header
  271. */
  272. _seterr_reply(&reply_msg, &(ct->ct_error));
  273. if (ct->ct_error.re_status == RPC_SUCCESS) {
  274. if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
  275. ct->ct_error.re_status = RPC_AUTHERROR;
  276. ct->ct_error.re_why = AUTH_INVALIDRESP;
  277. } else if (! (*xdr_results)(xdrs, results_ptr)) {
  278. if (ct->ct_error.re_status == RPC_SUCCESS)
  279. ct->ct_error.re_status = RPC_CANTDECODERES;
  280. }
  281. /* free verifier ... */
  282. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  283. xdrs->x_op = XDR_FREE;
  284. (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  285. }
  286. } /* end successful completion */
  287. else {
  288. /* maybe our credentials need to be refreshed ... */
  289. if (refreshes-- && AUTH_REFRESH(h->cl_auth))
  290. goto call_again;
  291. } /* end of unsuccessful completion */
  292. return (ct->ct_error.re_status);
  293. }
  294. static void
  295. clnttcp_geterr(h, errp)
  296. CLIENT *h;
  297. struct rpc_err *errp;
  298. {
  299. register struct ct_data *ct =
  300. (struct ct_data *) h->cl_private;
  301. *errp = ct->ct_error;
  302. }
  303. static bool_t
  304. clnttcp_freeres(cl, xdr_res, res_ptr)
  305. CLIENT *cl;
  306. xdrproc_t xdr_res;
  307. caddr_t res_ptr;
  308. {
  309. register struct ct_data *ct = (struct ct_data *)cl->cl_private;
  310. register XDR *xdrs = &(ct->ct_xdrs);
  311. xdrs->x_op = XDR_FREE;
  312. return ((*xdr_res)(xdrs, res_ptr));
  313. }
  314. static void
  315. clnttcp_abort()
  316. {
  317. }
  318. static bool_t
  319. clnttcp_control(cl, request, info)
  320. CLIENT *cl;
  321. int request;
  322. char *info;
  323. {
  324. register struct ct_data *ct = (struct ct_data *)cl->cl_private;
  325. switch (request) {
  326. case CLSET_TIMEOUT:
  327. ct->ct_wait = *(struct timeval *)info;
  328. ct->ct_waitset = TRUE;
  329. break;
  330. case CLGET_TIMEOUT:
  331. *(struct timeval *)info = ct->ct_wait;
  332. break;
  333. case CLGET_SERVER_ADDR:
  334. *(struct sockaddr_in *)info = ct->ct_addr;
  335. break;
  336. default:
  337. return (FALSE);
  338. }
  339. return (TRUE);
  340. }
  341. static void
  342. clnttcp_destroy(h)
  343. CLIENT *h;
  344. {
  345. register struct ct_data *ct =
  346. (struct ct_data *) h->cl_private;
  347. if (ct->ct_closeit) {
  348. (void)close(ct->ct_sock);
  349. }
  350. XDR_DESTROY(&(ct->ct_xdrs));
  351. mem_free((caddr_t)ct, sizeof(struct ct_data));
  352. mem_free((caddr_t)h, sizeof(CLIENT));
  353. }
  354. /*
  355. * Interface between xdr serializer and tcp connection.
  356. * Behaves like the system calls, read & write, but keeps some error state
  357. * around for the rpc level.
  358. */
  359. static int
  360. readtcp(ct, buf, len)
  361. register struct ct_data *ct;
  362. caddr_t buf;
  363. register int len;
  364. {
  365. #ifdef FD_SETSIZE
  366. fd_set mask;
  367. fd_set readfds;
  368. if (len == 0)
  369. return (0);
  370. FD_ZERO(&mask);
  371. FD_SET(ct->ct_sock, &mask);
  372. #else
  373. register int mask = 1 << (ct->ct_sock);
  374. int readfds;
  375. if (len == 0)
  376. return (0);
  377. #endif /* def FD_SETSIZE */
  378. while (TRUE) {
  379. readfds = mask;
  380. switch (select(_rpc_dtablesize(), &readfds, (int*)NULL, (int*)NULL,
  381. &(ct->ct_wait))) {
  382. case 0:
  383. ct->ct_error.re_status = RPC_TIMEDOUT;
  384. return (-1);
  385. case -1:
  386. if (errno == EINTR)
  387. continue;
  388. ct->ct_error.re_status = RPC_CANTRECV;
  389. ct->ct_error.re_errno = errno;
  390. return (-1);
  391. }
  392. break;
  393. }
  394. switch (len = read(ct->ct_sock, buf, len)) {
  395. case 0:
  396. /* premature eof */
  397. ct->ct_error.re_errno = ECONNRESET;
  398. ct->ct_error.re_status = RPC_CANTRECV;
  399. len = -1; /* it's really an error */
  400. break;
  401. case -1:
  402. ct->ct_error.re_errno = errno;
  403. ct->ct_error.re_status = RPC_CANTRECV;
  404. break;
  405. }
  406. return (len);
  407. }
  408. static int
  409. writetcp(ct, buf, len)
  410. struct ct_data *ct;
  411. caddr_t buf;
  412. int len;
  413. {
  414. register int i, cnt;
  415. for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  416. if ((i = write(ct->ct_sock, buf, cnt)) == -1) {
  417. ct->ct_error.re_errno = errno;
  418. ct->ct_error.re_status = RPC_CANTSEND;
  419. return (-1);
  420. }
  421. }
  422. return (len);
  423. }