clnt_tcp.c 14 KB

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