clnt_tcp.c 14 KB

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