clnt_tcp.c 15 KB

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