clnt_tcp.c 14 KB

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