2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/
33 static char *rcsid = "$FreeBSD: src/lib/libc/rpc/clnt_tcp.c,v 1.14 2000/01/27 23:06:36 jasone Exp $";
37 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
39 * Copyright (C) 1984, Sun Microsystems, Inc.
41 * TCP based RPC supports 'batched calls'.
42 * A sequence of calls may be batched-up in a send buffer. The rpc call
43 * return immediately to the client even though the call was not necessarily
44 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
45 * the rpc timeout value is zero (see clnt.h, rpc).
47 * Clients should NOT casually batch calls that in fact return results; that is,
48 * the server side should be aware that a call is batched and not produce any
49 * return message. Batched calls that produce many result messages can
50 * deadlock (netlock) the client and the server....
52 * Now go hang yourself.
60 #include <sys/socket.h>
63 #include <rpc/pmap_clnt.h>
65 #define MCALL_MSG_SIZE 24
68 static int writetcp();
70 static enum clnt_stat clnttcp_call();
71 static void clnttcp_abort();
72 static void clnttcp_geterr();
73 static bool_t clnttcp_freeres();
74 static bool_t clnttcp_control();
75 static void clnttcp_destroy();
77 static struct clnt_ops tcp_ops = {
89 struct timeval ct_wait;
90 bool_t ct_waitset; /* wait set by clnt_control? */
91 struct sockaddr_in ct_addr;
92 struct rpc_err ct_error;
93 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
94 u_int ct_mpos; /* pos after marshal */
99 * Create a client handle for a tcp/ip connection.
100 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
101 * connected to raddr. If *sockp non-negative then
102 * raddr is ignored. The rpc/tcp package does buffering
103 * similar to stdio, so the client must pick send and receive buffer sizes,];
104 * 0 => use the default.
105 * If raddr->sin_port is 0, then a binder on the remote machine is
106 * consulted for the right port number.
107 * NB: *sockp is copied into a private area.
108 * NB: It is the clients responsibility to close *sockp.
109 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
110 * something more useful.
113 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
114 struct sockaddr_in *raddr;
122 register struct ct_data *ct = NULL;
124 struct rpc_msg call_msg;
125 static u_int32_t disrupt;
128 disrupt = (u_int32_t)(long)raddr;
130 h = (CLIENT *)mem_alloc(sizeof(*h));
132 (void)fprintf(stderr, "clnttcp_create: out of memory\n");
133 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
134 rpc_createerr.cf_error.re_errno = errno;
137 ct = (struct ct_data *)mem_alloc(sizeof(*ct));
139 (void)fprintf(stderr, "clnttcp_create: out of memory\n");
140 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
141 rpc_createerr.cf_error.re_errno = errno;
146 * If no port number given ask the pmap for one
148 if (raddr->sin_port == 0) {
150 if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
151 mem_free((caddr_t)ct, sizeof(struct ct_data));
152 mem_free((caddr_t)h, sizeof(CLIENT));
153 return ((CLIENT *)NULL);
155 raddr->sin_port = htons(port);
159 * If no socket given, open one
162 *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
163 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
165 || (connect(*sockp, (struct sockaddr *)raddr,
166 sizeof(*raddr)) < 0)) {
167 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
168 rpc_createerr.cf_error.re_errno = errno;
170 (void)_close(*sockp);
173 ct->ct_closeit = TRUE;
175 ct->ct_closeit = FALSE;
179 * Set up private data struct
181 ct->ct_sock = *sockp;
182 ct->ct_wait.tv_usec = 0;
183 ct->ct_waitset = FALSE;
184 ct->ct_addr = *raddr;
187 * Initialize call message
189 (void)gettimeofday(&now, (struct timezone *)0);
190 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
191 call_msg.rm_direction = CALL;
192 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
193 call_msg.rm_call.cb_prog = prog;
194 call_msg.rm_call.cb_vers = vers;
197 * pre-serialize the static part of the call msg and stash it away
199 xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
201 if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
202 if (ct->ct_closeit) {
203 (void)_close(*sockp);
207 ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
208 XDR_DESTROY(&(ct->ct_xdrs));
211 * Create a client handle which uses xdrrec for serialization
212 * and authnone for authentication.
214 xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
215 (caddr_t)ct, readtcp, writetcp);
216 h->cl_ops = &tcp_ops;
217 h->cl_private = (caddr_t) ct;
218 h->cl_auth = authnone_create();
223 * Something goofed, free stuff and barf
226 mem_free((caddr_t)ct, sizeof(struct ct_data));
228 mem_free((caddr_t)h, sizeof(CLIENT));
229 return ((CLIENT *)NULL);
232 static enum clnt_stat
233 clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
238 xdrproc_t xdr_results;
240 struct timeval timeout;
242 register struct ct_data *ct = (struct ct_data *) h->cl_private;
243 register XDR *xdrs = &(ct->ct_xdrs);
244 struct rpc_msg reply_msg;
246 u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */
247 register bool_t shipnow;
250 if (!ct->ct_waitset) {
251 ct->ct_wait = timeout;
255 (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
256 && timeout.tv_usec == 0) ? FALSE : TRUE;
259 xdrs->x_op = XDR_ENCODE;
260 ct->ct_error.re_status = RPC_SUCCESS;
261 x_id = ntohl(--(*msg_x_id));
262 if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
263 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
264 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
265 (! (*xdr_args)(xdrs, args_ptr))) {
266 if (ct->ct_error.re_status == RPC_SUCCESS)
267 ct->ct_error.re_status = RPC_CANTENCODEARGS;
268 (void)xdrrec_endofrecord(xdrs, TRUE);
269 return (ct->ct_error.re_status);
271 if (! xdrrec_endofrecord(xdrs, shipnow))
272 return (ct->ct_error.re_status = RPC_CANTSEND);
274 return (RPC_SUCCESS);
276 * Hack to provide rpc-based message passing
278 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
279 return(ct->ct_error.re_status = RPC_TIMEDOUT);
284 * Keep receiving until we get a valid transaction id
286 xdrs->x_op = XDR_DECODE;
288 reply_msg.acpted_rply.ar_verf = _null_auth;
289 reply_msg.acpted_rply.ar_results.where = NULL;
290 reply_msg.acpted_rply.ar_results.proc = xdr_void;
291 if (! xdrrec_skiprecord(xdrs))
292 return (ct->ct_error.re_status);
293 /* now decode and validate the response header */
294 if (! xdr_replymsg(xdrs, &reply_msg)) {
295 if (ct->ct_error.re_status == RPC_SUCCESS)
297 return (ct->ct_error.re_status);
299 if (reply_msg.rm_xid == x_id)
306 _seterr_reply(&reply_msg, &(ct->ct_error));
307 if (ct->ct_error.re_status == RPC_SUCCESS) {
308 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
309 ct->ct_error.re_status = RPC_AUTHERROR;
310 ct->ct_error.re_why = AUTH_INVALIDRESP;
311 } else if (! (*xdr_results)(xdrs, results_ptr)) {
312 if (ct->ct_error.re_status == RPC_SUCCESS)
313 ct->ct_error.re_status = RPC_CANTDECODERES;
315 /* free verifier ... */
316 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
317 xdrs->x_op = XDR_FREE;
318 (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
320 } /* end successful completion */
322 /* maybe our credentials need to be refreshed ... */
323 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
325 } /* end of unsuccessful completion */
326 return (ct->ct_error.re_status);
330 clnttcp_geterr(h, errp)
332 struct rpc_err *errp;
334 register struct ct_data *ct =
335 (struct ct_data *) h->cl_private;
337 *errp = ct->ct_error;
341 clnttcp_freeres(cl, xdr_res, res_ptr)
346 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
347 register XDR *xdrs = &(ct->ct_xdrs);
349 xdrs->x_op = XDR_FREE;
350 return ((*xdr_res)(xdrs, res_ptr));
360 clnttcp_control(cl, request, info)
365 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
366 register struct timeval *tv;
371 ct->ct_closeit = TRUE;
373 case CLSET_FD_NCLOSE:
374 ct->ct_closeit = FALSE;
379 tv = (struct timeval *)info;
380 ct->ct_wait.tv_sec = tv->tv_sec;
381 ct->ct_wait.tv_usec = tv->tv_usec;
382 ct->ct_waitset = TRUE;
387 *(struct timeval *)info = ct->ct_wait;
389 case CLGET_SERVER_ADDR:
392 *(struct sockaddr_in *)info = ct->ct_addr;
397 *(int *)info = ct->ct_sock;
401 * use the knowledge that xid is the
402 * first element in the call structure *.
403 * This will get the xid of the PREVIOUS call
407 *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall);
410 /* This will set the xid of the NEXT call */
413 *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1);
414 /* decrement by 1 as clnttcp_call() increments once */
417 * This RELIES on the information that, in the call body,
418 * the version number field is the fifth field from the
419 * begining of the RPC header. MUST be changed if the
420 * call_struct is changed
424 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
425 4 * BYTES_PER_XDR_UNIT));
430 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
431 = htonl(*(u_long *)info);
435 * This RELIES on the information that, in the call body,
436 * the program number field is the field from the
437 * begining of the RPC header. MUST be changed if the
438 * call_struct is changed
442 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
443 3 * BYTES_PER_XDR_UNIT));
448 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
449 = htonl(*(u_long *)info);
451 case CLGET_LOCAL_ADDR:
452 len = sizeof(struct sockaddr);
453 if (getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0)
456 case CLGET_RETRY_TIMEOUT:
457 case CLSET_RETRY_TIMEOUT:
460 case CLSET_PUSH_TIMOD:
461 case CLSET_POP_TIMOD:
473 register struct ct_data *ct =
474 (struct ct_data *) h->cl_private;
476 if (ct->ct_closeit) {
477 (void)_close(ct->ct_sock);
479 XDR_DESTROY(&(ct->ct_xdrs));
480 mem_free((caddr_t)ct, sizeof(struct ct_data));
481 mem_free((caddr_t)h, sizeof(CLIENT));
485 * Interface between xdr serializer and tcp connection.
486 * Behaves like the system calls, read & write, but keeps some error state
487 * around for the rpc level.
490 readtcp(ct, buf, len)
491 register struct ct_data *ct;
495 fd_set *fds, readfds;
496 struct timeval start, after, duration, delta, tmp, tv;
502 if (ct->ct_sock + 1 > FD_SETSIZE) {
503 int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask);
504 fds = (fd_set *)malloc(bytes);
507 memset(fds, 0, bytes);
513 gettimeofday(&start, NULL);
516 /* XXX we know the other bits are still clear */
517 FD_SET(ct->ct_sock, fds);
518 tv = delta; /* in case select writes back */
519 r = select(ct->ct_sock+1, fds, NULL, NULL, &tv);
522 gettimeofday(&after, NULL);
523 timersub(&start, &after, &duration);
524 timersub(&ct->ct_wait, &duration, &tmp);
526 if (delta.tv_sec < 0 || !timerisset(&delta))
533 ct->ct_error.re_status = RPC_TIMEDOUT;
541 ct->ct_error.re_status = RPC_CANTRECV;
542 ct->ct_error.re_errno = save_errno;
547 switch (len = _read(ct->ct_sock, buf, len)) {
551 ct->ct_error.re_errno = ECONNRESET;
552 ct->ct_error.re_status = RPC_CANTRECV;
553 len = -1; /* it's really an error */
557 ct->ct_error.re_errno = errno;
558 ct->ct_error.re_status = RPC_CANTRECV;
565 writetcp(ct, buf, len)
572 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
573 if ((i = _write(ct->ct_sock, buf, cnt)) == -1) {
574 ct->ct_error.re_errno = errno;
575 ct->ct_error.re_status = RPC_CANTSEND;