clnt_tcp.c 15 KB

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