clnt_tcp.c 12 KB

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