clnt_tcp.c 14 KB

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