clnt_tcp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 0
  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 <netdb.h>
  52. #include <errno.h>
  53. #include <stdio.h>
  54. #include <unistd.h>
  55. #include "rpc_private.h"
  56. #include <sys/poll.h>
  57. #include <sys/socket.h>
  58. #include <rpc/pmap_clnt.h>
  59. #define MCALL_MSG_SIZE 24
  60. struct ct_data
  61. {
  62. int ct_sock;
  63. bool_t ct_closeit;
  64. struct timeval ct_wait;
  65. bool_t ct_waitset; /* wait set by clnt_control? */
  66. struct sockaddr_in ct_addr;
  67. struct rpc_err ct_error;
  68. char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
  69. u_int ct_mpos; /* pos after marshal */
  70. XDR ct_xdrs;
  71. };
  72. static int readtcp (char *, char *, int);
  73. static int writetcp (char *, char *, int);
  74. static enum clnt_stat clnttcp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
  75. xdrproc_t, caddr_t, struct timeval);
  76. static void clnttcp_abort (void);
  77. static void clnttcp_geterr (CLIENT *, struct rpc_err *);
  78. static bool_t clnttcp_freeres (CLIENT *, xdrproc_t, caddr_t);
  79. static bool_t clnttcp_control (CLIENT *, int, char *);
  80. static void clnttcp_destroy (CLIENT *);
  81. static const struct clnt_ops tcp_ops =
  82. {
  83. clnttcp_call,
  84. clnttcp_abort,
  85. clnttcp_geterr,
  86. clnttcp_freeres,
  87. clnttcp_destroy,
  88. clnttcp_control
  89. };
  90. /*
  91. * Create a client handle for a tcp/ip connection.
  92. * If *sockp<0, *sockp is set to a newly created TCP socket and it is
  93. * connected to raddr. If *sockp non-negative then
  94. * raddr is ignored. The rpc/tcp package does buffering
  95. * similar to stdio, so the client must pick send and receive buffer sizes,];
  96. * 0 => use the default.
  97. * If raddr->sin_port is 0, then a binder on the remote machine is
  98. * consulted for the right port number.
  99. * NB: *sockp is copied into a private area.
  100. * NB: It is the clients responsibility to close *sockp.
  101. * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
  102. * something more useful.
  103. */
  104. CLIENT *
  105. clnttcp_create (struct sockaddr_in *raddr, u_long prog, u_long vers,
  106. int *sockp, u_int sendsz, u_int recvsz)
  107. {
  108. CLIENT *h;
  109. struct ct_data *ct;
  110. struct rpc_msg call_msg;
  111. h = (CLIENT *) mem_alloc (sizeof (*h));
  112. ct = (struct ct_data *) mem_alloc (sizeof (*ct));
  113. if (h == NULL || ct == NULL)
  114. {
  115. struct rpc_createerr *ce = &get_rpc_createerr ();
  116. (void) fputs ("clnttcp_create: out of memory\n", stderr);
  117. ce->cf_stat = RPC_SYSTEMERROR;
  118. ce->cf_error.re_errno = ENOMEM;
  119. goto fooy;
  120. }
  121. /*
  122. * If no port number given ask the pmap for one
  123. */
  124. if (raddr->sin_port == 0)
  125. {
  126. u_short port;
  127. if ((port = pmap_getport (raddr, prog, vers, IPPROTO_TCP)) == 0)
  128. {
  129. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  130. mem_free ((caddr_t) h, sizeof (CLIENT));
  131. return ((CLIENT *) NULL);
  132. }
  133. raddr->sin_port = htons (port);
  134. }
  135. /*
  136. * If no socket given, open one
  137. */
  138. if (*sockp < 0)
  139. {
  140. *sockp = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  141. (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
  142. if ((*sockp < 0)
  143. || (connect (*sockp, (struct sockaddr *) raddr,
  144. sizeof (*raddr)) < 0))
  145. {
  146. struct rpc_createerr *ce = &get_rpc_createerr ();
  147. ce->cf_stat = RPC_SYSTEMERROR;
  148. ce->cf_error.re_errno = errno;
  149. if (*sockp >= 0)
  150. (void) close (*sockp);
  151. goto fooy;
  152. }
  153. ct->ct_closeit = TRUE;
  154. }
  155. else
  156. {
  157. ct->ct_closeit = FALSE;
  158. }
  159. /*
  160. * Set up private data struct
  161. */
  162. ct->ct_sock = *sockp;
  163. ct->ct_wait.tv_usec = 0;
  164. ct->ct_waitset = FALSE;
  165. ct->ct_addr = *raddr;
  166. /*
  167. * Initialize call message
  168. */
  169. call_msg.rm_xid = _create_xid ();
  170. call_msg.rm_direction = CALL;
  171. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  172. call_msg.rm_call.cb_prog = prog;
  173. call_msg.rm_call.cb_vers = vers;
  174. /*
  175. * pre-serialize the static part of the call msg and stash it away
  176. */
  177. xdrmem_create (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
  178. XDR_ENCODE);
  179. if (!xdr_callhdr (&(ct->ct_xdrs), &call_msg))
  180. {
  181. if (ct->ct_closeit)
  182. {
  183. (void) close (*sockp);
  184. }
  185. goto fooy;
  186. }
  187. ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs));
  188. XDR_DESTROY (&(ct->ct_xdrs));
  189. /*
  190. * Create a client handle which uses xdrrec for serialization
  191. * and authnone for authentication.
  192. */
  193. xdrrec_create (&(ct->ct_xdrs), sendsz, recvsz,
  194. (caddr_t) ct, readtcp, writetcp);
  195. h->cl_ops = &tcp_ops;
  196. h->cl_private = (caddr_t) ct;
  197. h->cl_auth = authnone_create ();
  198. return h;
  199. fooy:
  200. /*
  201. * Something goofed, free stuff and barf
  202. */
  203. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  204. mem_free ((caddr_t) h, sizeof (CLIENT));
  205. return ((CLIENT *) NULL);
  206. }
  207. libc_hidden_def(clnttcp_create)
  208. static enum clnt_stat
  209. clnttcp_call (CLIENT *h, u_long proc, xdrproc_t xdr_args, caddr_t args_ptr,
  210. xdrproc_t xdr_results, caddr_t results_ptr,
  211. struct timeval timeout)
  212. {
  213. struct ct_data *ct = (struct ct_data *) h->cl_private;
  214. XDR *xdrs = &(ct->ct_xdrs);
  215. struct rpc_msg reply_msg;
  216. u_long x_id;
  217. u_int32_t *msg_x_id = (u_int32_t *) (ct->ct_mcall); /* yuk */
  218. bool_t shipnow;
  219. int refreshes = 2;
  220. if (!ct->ct_waitset)
  221. {
  222. ct->ct_wait = timeout;
  223. }
  224. shipnow =
  225. (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
  226. && ct->ct_wait.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. {
  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 (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
  249. {
  250. return ct->ct_error.re_status = RPC_TIMEDOUT;
  251. }
  252. /*
  253. * Keep receiving until we get a valid transaction id
  254. */
  255. xdrs->x_op = XDR_DECODE;
  256. while (TRUE)
  257. {
  258. reply_msg.acpted_rply.ar_verf = _null_auth;
  259. reply_msg.acpted_rply.ar_results.where = NULL;
  260. reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  261. if (!xdrrec_skiprecord (xdrs))
  262. return (ct->ct_error.re_status);
  263. /* now decode and validate the response header */
  264. if (!xdr_replymsg (xdrs, &reply_msg))
  265. {
  266. if (ct->ct_error.re_status == RPC_SUCCESS)
  267. continue;
  268. return ct->ct_error.re_status;
  269. }
  270. if ((u_int32_t) reply_msg.rm_xid == (u_int32_t) x_id)
  271. break;
  272. }
  273. /*
  274. * process header
  275. */
  276. _seterr_reply (&reply_msg, &(ct->ct_error));
  277. if (ct->ct_error.re_status == RPC_SUCCESS)
  278. {
  279. if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
  280. {
  281. ct->ct_error.re_status = RPC_AUTHERROR;
  282. ct->ct_error.re_why = AUTH_INVALIDRESP;
  283. }
  284. else if (!(*xdr_results) (xdrs, results_ptr))
  285. {
  286. if (ct->ct_error.re_status == RPC_SUCCESS)
  287. ct->ct_error.re_status = RPC_CANTDECODERES;
  288. }
  289. /* free verifier ... */
  290. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  291. {
  292. xdrs->x_op = XDR_FREE;
  293. (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
  294. }
  295. } /* end successful completion */
  296. else
  297. {
  298. /* maybe our credentials need to be refreshed ... */
  299. if (refreshes-- && AUTH_REFRESH (h->cl_auth))
  300. goto call_again;
  301. } /* end of unsuccessful completion */
  302. return ct->ct_error.re_status;
  303. }
  304. static void
  305. clnttcp_geterr (CLIENT *h, struct rpc_err *errp)
  306. {
  307. struct ct_data *ct =
  308. (struct ct_data *) h->cl_private;
  309. *errp = ct->ct_error;
  310. }
  311. static bool_t
  312. clnttcp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
  313. {
  314. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  315. XDR *xdrs = &(ct->ct_xdrs);
  316. xdrs->x_op = XDR_FREE;
  317. return (*xdr_res) (xdrs, res_ptr);
  318. }
  319. static void
  320. clnttcp_abort (void)
  321. {
  322. }
  323. static bool_t
  324. clnttcp_control (CLIENT *cl, int request, char *info)
  325. {
  326. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  327. switch (request)
  328. {
  329. case CLSET_FD_CLOSE:
  330. ct->ct_closeit = TRUE;
  331. break;
  332. case CLSET_FD_NCLOSE:
  333. ct->ct_closeit = FALSE;
  334. break;
  335. case CLSET_TIMEOUT:
  336. ct->ct_wait = *(struct timeval *) info;
  337. ct->ct_waitset = TRUE;
  338. break;
  339. case CLGET_TIMEOUT:
  340. *(struct timeval *) info = ct->ct_wait;
  341. break;
  342. case CLGET_SERVER_ADDR:
  343. *(struct sockaddr_in *) info = ct->ct_addr;
  344. break;
  345. case CLGET_FD:
  346. *(int *)info = ct->ct_sock;
  347. break;
  348. case CLGET_XID:
  349. /*
  350. * use the knowledge that xid is the
  351. * first element in the call structure *.
  352. * This will get the xid of the PREVIOUS call
  353. */
  354. *(u_long *)info = ntohl (*(u_long *)ct->ct_mcall);
  355. break;
  356. case CLSET_XID:
  357. /* This will set the xid of the NEXT call */
  358. *(u_long *)ct->ct_mcall = htonl (*(u_long *)info - 1);
  359. /* decrement by 1 as clnttcp_call() increments once */
  360. break;
  361. case CLGET_VERS:
  362. /*
  363. * This RELIES on the information that, in the call body,
  364. * the version number field is the fifth field from the
  365. * begining of the RPC header. MUST be changed if the
  366. * call_struct is changed
  367. */
  368. *(u_long *)info = ntohl (*(u_long *)(ct->ct_mcall +
  369. 4 * BYTES_PER_XDR_UNIT));
  370. break;
  371. case CLSET_VERS:
  372. *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
  373. = htonl (*(u_long *)info);
  374. break;
  375. case CLGET_PROG:
  376. /*
  377. * This RELIES on the information that, in the call body,
  378. * the program number field is the field from the
  379. * begining of the RPC header. MUST be changed if the
  380. * call_struct is changed
  381. */
  382. *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
  383. 3 * BYTES_PER_XDR_UNIT));
  384. break;
  385. case CLSET_PROG:
  386. *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
  387. = htonl(*(u_long *)info);
  388. break;
  389. /* The following are only possible with TI-RPC */
  390. case CLGET_RETRY_TIMEOUT:
  391. case CLSET_RETRY_TIMEOUT:
  392. case CLGET_SVC_ADDR:
  393. case CLSET_SVC_ADDR:
  394. case CLSET_PUSH_TIMOD:
  395. case CLSET_POP_TIMOD:
  396. default:
  397. return FALSE;
  398. }
  399. return TRUE;
  400. }
  401. static void
  402. clnttcp_destroy (CLIENT *h)
  403. {
  404. struct ct_data *ct =
  405. (struct ct_data *) h->cl_private;
  406. if (ct->ct_closeit)
  407. {
  408. (void) close (ct->ct_sock);
  409. }
  410. XDR_DESTROY (&(ct->ct_xdrs));
  411. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  412. mem_free ((caddr_t) h, sizeof (CLIENT));
  413. }
  414. /*
  415. * Interface between xdr serializer and tcp connection.
  416. * Behaves like the system calls, read & write, but keeps some error state
  417. * around for the rpc level.
  418. */
  419. static int
  420. readtcp (char *ctptr, char *buf, int len)
  421. {
  422. struct ct_data *ct = (struct ct_data *)ctptr;
  423. struct pollfd fd;
  424. int milliseconds = (ct->ct_wait.tv_sec * 1000) +
  425. (ct->ct_wait.tv_usec / 1000);
  426. if (len == 0)
  427. return 0;
  428. fd.fd = ct->ct_sock;
  429. fd.events = POLLIN;
  430. while (TRUE)
  431. {
  432. switch (poll(&fd, 1, milliseconds))
  433. {
  434. case 0:
  435. ct->ct_error.re_status = RPC_TIMEDOUT;
  436. return -1;
  437. case -1:
  438. if (errno == EINTR)
  439. continue;
  440. ct->ct_error.re_status = RPC_CANTRECV;
  441. ct->ct_error.re_errno = errno;
  442. return -1;
  443. }
  444. break;
  445. }
  446. switch (len = read (ct->ct_sock, buf, len))
  447. {
  448. case 0:
  449. /* premature eof */
  450. ct->ct_error.re_errno = ECONNRESET;
  451. ct->ct_error.re_status = RPC_CANTRECV;
  452. len = -1; /* it's really an error */
  453. break;
  454. case -1:
  455. ct->ct_error.re_errno = errno;
  456. ct->ct_error.re_status = RPC_CANTRECV;
  457. break;
  458. }
  459. return len;
  460. }
  461. static int
  462. writetcp (char *ctptr, char *buf, int len)
  463. {
  464. int i, cnt;
  465. struct ct_data *ct = (struct ct_data*)ctptr;
  466. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  467. {
  468. if ((i = write (ct->ct_sock, buf, cnt)) == -1)
  469. {
  470. ct->ct_error.re_errno = errno;
  471. ct->ct_error.re_status = RPC_CANTSEND;
  472. return -1;
  473. }
  474. }
  475. return len;
  476. }