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
29 * @(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro
30 * @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/clnt_tcp.c,v 1.14 2000/01/27 23:06:36 jasone Exp $
32 * $DragonFly: src/lib/libcr/rpc/Attic/clnt_tcp.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
36 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * TCP based RPC supports 'batched calls'.
41 * A sequence of calls may be batched-up in a send buffer. The rpc call
42 * return immediately to the client even though the call was not necessarily
43 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
44 * the rpc timeout value is zero (see clnt.h, rpc).
46 * Clients should NOT casually batch calls that in fact return results; that is,
47 * the server side should be aware that a call is batched and not produce any
48 * return message. Batched calls that produce many result messages can
49 * deadlock (netlock) the client and the server....
51 * Now go hang yourself.
59 #include <sys/socket.h>
62 #include <rpc/pmap_clnt.h>
64 #define MCALL_MSG_SIZE 24
67 static int writetcp();
69 static enum clnt_stat clnttcp_call();
70 static void clnttcp_abort();
71 static void clnttcp_geterr();
72 static bool_t clnttcp_freeres();
73 static bool_t clnttcp_control();
74 static void clnttcp_destroy();
76 static struct clnt_ops tcp_ops = {
88 struct timeval ct_wait;
89 bool_t ct_waitset; /* wait set by clnt_control? */
90 struct sockaddr_in ct_addr;
91 struct rpc_err ct_error;
92 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
93 u_int ct_mpos; /* pos after marshal */
98 * Create a client handle for a tcp/ip connection.
99 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
100 * connected to raddr. If *sockp non-negative then
101 * raddr is ignored. The rpc/tcp package does buffering
102 * similar to stdio, so the client must pick send and receive buffer sizes,];
103 * 0 => use the default.
104 * If raddr->sin_port is 0, then a binder on the remote machine is
105 * consulted for the right port number.
106 * NB: *sockp is copied into a private area.
107 * NB: It is the clients responsibility to close *sockp.
108 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
109 * something more useful.
112 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
113 struct sockaddr_in *raddr;
121 register struct ct_data *ct = NULL;
123 struct rpc_msg call_msg;
124 static u_int32_t disrupt;
127 disrupt = (u_int32_t)(long)raddr;
129 h = (CLIENT *)mem_alloc(sizeof(*h));
131 (void)fprintf(stderr, "clnttcp_create: out of memory\n");
132 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
133 rpc_createerr.cf_error.re_errno = errno;
136 ct = (struct ct_data *)mem_alloc(sizeof(*ct));
138 (void)fprintf(stderr, "clnttcp_create: out of memory\n");
139 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
140 rpc_createerr.cf_error.re_errno = errno;
145 * If no port number given ask the pmap for one
147 if (raddr->sin_port == 0) {
149 if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
150 mem_free((caddr_t)ct, sizeof(struct ct_data));
151 mem_free((caddr_t)h, sizeof(CLIENT));
152 return ((CLIENT *)NULL);
154 raddr->sin_port = htons(port);
158 * If no socket given, open one
161 *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
162 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
164 || (connect(*sockp, (struct sockaddr *)raddr,
165 sizeof(*raddr)) < 0)) {
166 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
167 rpc_createerr.cf_error.re_errno = errno;
169 (void)_close(*sockp);
172 ct->ct_closeit = TRUE;
174 ct->ct_closeit = FALSE;
178 * Set up private data struct
180 ct->ct_sock = *sockp;
181 ct->ct_wait.tv_usec = 0;
182 ct->ct_waitset = FALSE;
183 ct->ct_addr = *raddr;
186 * Initialize call message
188 (void)gettimeofday(&now, (struct timezone *)0);
189 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
190 call_msg.rm_direction = CALL;
191 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
192 call_msg.rm_call.cb_prog = prog;
193 call_msg.rm_call.cb_vers = vers;
196 * pre-serialize the static part of the call msg and stash it away
198 xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
200 if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
201 if (ct->ct_closeit) {
202 (void)_close(*sockp);
206 ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
207 XDR_DESTROY(&(ct->ct_xdrs));
210 * Create a client handle which uses xdrrec for serialization
211 * and authnone for authentication.
213 xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
214 (caddr_t)ct, readtcp, writetcp);
215 h->cl_ops = &tcp_ops;
216 h->cl_private = (caddr_t) ct;
217 h->cl_auth = authnone_create();
222 * Something goofed, free stuff and barf
225 mem_free((caddr_t)ct, sizeof(struct ct_data));
227 mem_free((caddr_t)h, sizeof(CLIENT));
228 return ((CLIENT *)NULL);
231 static enum clnt_stat
232 clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
237 xdrproc_t xdr_results;
239 struct timeval timeout;
241 register struct ct_data *ct = (struct ct_data *) h->cl_private;
242 register XDR *xdrs = &(ct->ct_xdrs);
243 struct rpc_msg reply_msg;
245 u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */
246 register bool_t shipnow;
249 if (!ct->ct_waitset) {
250 ct->ct_wait = timeout;
254 (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
255 && timeout.tv_usec == 0) ? FALSE : TRUE;
258 xdrs->x_op = XDR_ENCODE;
259 ct->ct_error.re_status = RPC_SUCCESS;
260 x_id = ntohl(--(*msg_x_id));
261 if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
262 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
263 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
264 (! (*xdr_args)(xdrs, args_ptr))) {
265 if (ct->ct_error.re_status == RPC_SUCCESS)
266 ct->ct_error.re_status = RPC_CANTENCODEARGS;
267 (void)xdrrec_endofrecord(xdrs, TRUE);
268 return (ct->ct_error.re_status);
270 if (! xdrrec_endofrecord(xdrs, shipnow))
271 return (ct->ct_error.re_status = RPC_CANTSEND);
273 return (RPC_SUCCESS);
275 * Hack to provide rpc-based message passing
277 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
278 return(ct->ct_error.re_status = RPC_TIMEDOUT);
283 * Keep receiving until we get a valid transaction id
285 xdrs->x_op = XDR_DECODE;
287 reply_msg.acpted_rply.ar_verf = _null_auth;
288 reply_msg.acpted_rply.ar_results.where = NULL;
289 reply_msg.acpted_rply.ar_results.proc = xdr_void;
290 if (! xdrrec_skiprecord(xdrs))
291 return (ct->ct_error.re_status);
292 /* now decode and validate the response header */
293 if (! xdr_replymsg(xdrs, &reply_msg)) {
294 if (ct->ct_error.re_status == RPC_SUCCESS)
296 return (ct->ct_error.re_status);
298 if (reply_msg.rm_xid == x_id)
305 _seterr_reply(&reply_msg, &(ct->ct_error));
306 if (ct->ct_error.re_status == RPC_SUCCESS) {
307 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
308 ct->ct_error.re_status = RPC_AUTHERROR;
309 ct->ct_error.re_why = AUTH_INVALIDRESP;
310 } else if (! (*xdr_results)(xdrs, results_ptr)) {
311 if (ct->ct_error.re_status == RPC_SUCCESS)
312 ct->ct_error.re_status = RPC_CANTDECODERES;
314 /* free verifier ... */
315 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
316 xdrs->x_op = XDR_FREE;
317 (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
319 } /* end successful completion */
321 /* maybe our credentials need to be refreshed ... */
322 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
324 } /* end of unsuccessful completion */
325 return (ct->ct_error.re_status);
329 clnttcp_geterr(h, errp)
331 struct rpc_err *errp;
333 register struct ct_data *ct =
334 (struct ct_data *) h->cl_private;
336 *errp = ct->ct_error;
340 clnttcp_freeres(cl, xdr_res, res_ptr)
345 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
346 register XDR *xdrs = &(ct->ct_xdrs);
348 xdrs->x_op = XDR_FREE;
349 return ((*xdr_res)(xdrs, res_ptr));
359 clnttcp_control(cl, request, info)
364 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
365 register struct timeval *tv;
370 ct->ct_closeit = TRUE;
372 case CLSET_FD_NCLOSE:
373 ct->ct_closeit = FALSE;
378 tv = (struct timeval *)info;
379 ct->ct_wait.tv_sec = tv->tv_sec;
380 ct->ct_wait.tv_usec = tv->tv_usec;
381 ct->ct_waitset = TRUE;
386 *(struct timeval *)info = ct->ct_wait;
388 case CLGET_SERVER_ADDR:
391 *(struct sockaddr_in *)info = ct->ct_addr;
396 *(int *)info = ct->ct_sock;
400 * use the knowledge that xid is the
401 * first element in the call structure *.
402 * This will get the xid of the PREVIOUS call
406 *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall);
409 /* This will set the xid of the NEXT call */
412 *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1);
413 /* decrement by 1 as clnttcp_call() increments once */
416 * This RELIES on the information that, in the call body,
417 * the version number field is the fifth field from the
418 * begining of the RPC header. MUST be changed if the
419 * call_struct is changed
423 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
424 4 * BYTES_PER_XDR_UNIT));
429 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
430 = htonl(*(u_long *)info);
434 * This RELIES on the information that, in the call body,
435 * the program number field is the field from the
436 * begining of the RPC header. MUST be changed if the
437 * call_struct is changed
441 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
442 3 * BYTES_PER_XDR_UNIT));
447 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
448 = htonl(*(u_long *)info);
450 case CLGET_LOCAL_ADDR:
451 len = sizeof(struct sockaddr);
452 if (getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0)
455 case CLGET_RETRY_TIMEOUT:
456 case CLSET_RETRY_TIMEOUT:
459 case CLSET_PUSH_TIMOD:
460 case CLSET_POP_TIMOD:
472 register struct ct_data *ct =
473 (struct ct_data *) h->cl_private;
475 if (ct->ct_closeit) {
476 (void)_close(ct->ct_sock);
478 XDR_DESTROY(&(ct->ct_xdrs));
479 mem_free((caddr_t)ct, sizeof(struct ct_data));
480 mem_free((caddr_t)h, sizeof(CLIENT));
484 * Interface between xdr serializer and tcp connection.
485 * Behaves like the system calls, read & write, but keeps some error state
486 * around for the rpc level.
489 readtcp(ct, buf, len)
490 register struct ct_data *ct;
494 fd_set *fds, readfds;
495 struct timeval start, after, duration, delta, tmp, tv;
501 if (ct->ct_sock + 1 > FD_SETSIZE) {
502 int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask);
503 fds = (fd_set *)malloc(bytes);
506 memset(fds, 0, bytes);
512 gettimeofday(&start, NULL);
515 /* XXX we know the other bits are still clear */
516 FD_SET(ct->ct_sock, fds);
517 tv = delta; /* in case select writes back */
518 r = select(ct->ct_sock+1, fds, NULL, NULL, &tv);
521 gettimeofday(&after, NULL);
522 timersub(&start, &after, &duration);
523 timersub(&ct->ct_wait, &duration, &tmp);
525 if (delta.tv_sec < 0 || !timerisset(&delta))
532 ct->ct_error.re_status = RPC_TIMEDOUT;
540 ct->ct_error.re_status = RPC_CANTRECV;
541 ct->ct_error.re_errno = save_errno;
546 switch (len = _read(ct->ct_sock, buf, len)) {
550 ct->ct_error.re_errno = ECONNRESET;
551 ct->ct_error.re_status = RPC_CANTRECV;
552 len = -1; /* it's really an error */
556 ct->ct_error.re_errno = errno;
557 ct->ct_error.re_status = RPC_CANTRECV;
564 writetcp(ct, buf, len)
571 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
572 if ((i = _write(ct->ct_sock, buf, cnt)) == -1) {
573 ct->ct_error.re_errno = errno;
574 ct->ct_error.re_status = RPC_CANTSEND;