clnt_tcp.c 15 KB

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