clnt_tcp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 *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. h = (CLIENT *) mem_alloc(sizeof(*h));
  113. if (h == NULL) {
  114. (void) fprintf(stderr, "clnttcp_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. ct = (struct ct_data *) mem_alloc(sizeof(*ct));
  120. if (ct == NULL) {
  121. (void) fprintf(stderr, "clnttcp_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. /*
  127. * If no port number given ask the pmap for one
  128. */
  129. if (raddr->sin_port == 0) {
  130. u_short port;
  131. if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
  132. mem_free((caddr_t) ct, sizeof(struct ct_data));
  133. mem_free((caddr_t) h, sizeof(CLIENT));
  134. return ((CLIENT *) NULL);
  135. }
  136. raddr->sin_port = htons(port);
  137. }
  138. /*
  139. * If no socket given, open one
  140. */
  141. if (*sockp < 0) {
  142. *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  143. (void) bindresvport(*sockp, (struct sockaddr_in *) 0);
  144. if ((*sockp < 0)
  145. || (connect(*sockp, (struct sockaddr *) raddr,
  146. sizeof(*raddr)) < 0)) {
  147. rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  148. rpc_createerr.cf_error.re_errno = errno;
  149. (void) close(*sockp);
  150. goto fooy;
  151. }
  152. ct->ct_closeit = TRUE;
  153. } else {
  154. ct->ct_closeit = FALSE;
  155. }
  156. /*
  157. * Set up private data struct
  158. */
  159. ct->ct_sock = *sockp;
  160. ct->ct_wait.tv_usec = 0;
  161. ct->ct_waitset = FALSE;
  162. ct->ct_addr = *raddr;
  163. /*
  164. * Initialize call message
  165. */
  166. (void) gettimeofday(&now, (struct timezone *) 0);
  167. call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
  168. call_msg.rm_direction = CALL;
  169. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  170. call_msg.rm_call.cb_prog = prog;
  171. call_msg.rm_call.cb_vers = vers;
  172. /*
  173. * pre-serialize the staic part of the call msg and stash it away
  174. */
  175. xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
  176. XDR_ENCODE);
  177. if (!xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
  178. if (ct->ct_closeit) {
  179. (void) close(*sockp);
  180. }
  181. goto fooy;
  182. }
  183. ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
  184. XDR_DESTROY(&(ct->ct_xdrs));
  185. /*
  186. * Create a client handle which uses xdrrec for serialization
  187. * and authnone for authentication.
  188. */
  189. xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
  190. (caddr_t) ct, readtcp, writetcp);
  191. h->cl_ops = &tcp_ops;
  192. h->cl_private = (caddr_t) ct;
  193. h->cl_auth = authnone_create();
  194. return (h);
  195. fooy:
  196. /*
  197. * Something goofed, free stuff and barf
  198. */
  199. mem_free((caddr_t) ct, sizeof(struct ct_data));
  200. mem_free((caddr_t) h, sizeof(CLIENT));
  201. return ((CLIENT *) NULL);
  202. }
  203. static enum clnt_stat
  204. clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr,
  205. 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 clnttcp_geterr(h, errp)
  295. CLIENT *h;
  296. struct rpc_err *errp;
  297. {
  298. register struct ct_data *ct = (struct ct_data *) h->cl_private;
  299. *errp = ct->ct_error;
  300. }
  301. static bool_t clnttcp_freeres(cl, xdr_res, res_ptr)
  302. CLIENT *cl;
  303. xdrproc_t xdr_res;
  304. caddr_t res_ptr;
  305. {
  306. register struct ct_data *ct = (struct ct_data *) cl->cl_private;
  307. register XDR *xdrs = &(ct->ct_xdrs);
  308. xdrs->x_op = XDR_FREE;
  309. return ((*xdr_res) (xdrs, res_ptr));
  310. }
  311. static void clnttcp_abort()
  312. {
  313. }
  314. static bool_t clnttcp_control(cl, request, info)
  315. CLIENT *cl;
  316. int request;
  317. char *info;
  318. {
  319. register struct ct_data *ct = (struct ct_data *) cl->cl_private;
  320. switch (request) {
  321. case CLSET_TIMEOUT:
  322. ct->ct_wait = *(struct timeval *) info;
  323. ct->ct_waitset = TRUE;
  324. break;
  325. case CLGET_TIMEOUT:
  326. *(struct timeval *) info = ct->ct_wait;
  327. break;
  328. case CLGET_SERVER_ADDR:
  329. *(struct sockaddr_in *) info = ct->ct_addr;
  330. break;
  331. default:
  332. return (FALSE);
  333. }
  334. return (TRUE);
  335. }
  336. static void clnttcp_destroy(h)
  337. CLIENT *h;
  338. {
  339. register struct ct_data *ct = (struct ct_data *) h->cl_private;
  340. if (ct->ct_closeit) {
  341. (void) close(ct->ct_sock);
  342. }
  343. XDR_DESTROY(&(ct->ct_xdrs));
  344. mem_free((caddr_t) ct, sizeof(struct ct_data));
  345. mem_free((caddr_t) h, sizeof(CLIENT));
  346. }
  347. /*
  348. * Interface between xdr serializer and tcp connection.
  349. * Behaves like the system calls, read & write, but keeps some error state
  350. * around for the rpc level.
  351. */
  352. static int readtcp(ct, buf, len)
  353. register struct ct_data *ct;
  354. caddr_t buf;
  355. register int len;
  356. {
  357. #ifdef FD_SETSIZE
  358. fd_set mask;
  359. fd_set readfds;
  360. if (len == 0)
  361. return (0);
  362. FD_ZERO(&mask);
  363. FD_SET(ct->ct_sock, &mask);
  364. #else
  365. register int mask = 1 << (ct->ct_sock);
  366. int readfds;
  367. if (len == 0)
  368. return (0);
  369. #endif /* def FD_SETSIZE */
  370. while (TRUE) {
  371. readfds = mask;
  372. switch (select
  373. (_rpc_dtablesize(), &readfds, (int *) NULL, (int *) NULL,
  374. &(ct->ct_wait))) {
  375. case 0:
  376. ct->ct_error.re_status = RPC_TIMEDOUT;
  377. return (-1);
  378. case -1:
  379. if (errno == EINTR)
  380. continue;
  381. ct->ct_error.re_status = RPC_CANTRECV;
  382. ct->ct_error.re_errno = errno;
  383. return (-1);
  384. }
  385. break;
  386. }
  387. switch (len = read(ct->ct_sock, buf, len)) {
  388. case 0:
  389. /* premature eof */
  390. ct->ct_error.re_errno = ECONNRESET;
  391. ct->ct_error.re_status = RPC_CANTRECV;
  392. len = -1; /* it's really an error */
  393. break;
  394. case -1:
  395. ct->ct_error.re_errno = errno;
  396. ct->ct_error.re_status = RPC_CANTRECV;
  397. break;
  398. }
  399. return (len);
  400. }
  401. static int writetcp(ct, buf, len)
  402. struct ct_data *ct;
  403. caddr_t buf;
  404. int len;
  405. {
  406. register int i, cnt;
  407. for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  408. if ((i = write(ct->ct_sock, buf, cnt)) == -1) {
  409. ct->ct_error.re_errno = errno;
  410. ct->ct_error.re_status = RPC_CANTSEND;
  411. return (-1);
  412. }
  413. }
  414. return (len);
  415. }