clnt_tcp.c 15 KB

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