* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
* unrestricted use provided that this legend is included on all tape
* media and as a part of the software program in whole or part. Users
* may copy or modify Sun RPC without charge, but are not authorized
* to license or distribute it to anyone else except as part of a product or
* program developed by the user.
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
* Sun RPC is provided with no support and without any obligation on the
* part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
* Mountain View, California 94043
static char sccsid
[] = "@(#)clnt_tcp.c 1.5 85/03/17 Copyr 1984 Sun Micro";
* clnt_tcp.c, Implements a TCP/IP based, client side RPC.
* Copyright (C) 1984, Sun Microsystems, Inc.
* TCP based RPC supports 'batched calls'.
* A sequence of calls may be batched-up in a send buffer. The rpc call
* return immediately to the client even though the call was not necessarily
* sent. The batching occurs iff the results' xdr routine is NULL (0) AND
* the rpc timeout value is zero (see clnt.h, rpc).
* Clients should NOT casually batch calls that in fact return results; that is,
* the server side should be aware that a call is batched and not produce any
* return message. Batched calls that produce many result messages can
* deadlock (netlock) the client and the server....
#define MCALL_MSG_SIZE 24
static enum clnt_stat
clnttcp_call();
static void clnttcp_abort();
static void clnttcp_geterr();
static bool_t
clnttcp_freeres();
static void clnttcp_destroy();
static struct clnt_ops tcp_ops
= {
char ct_mcall
[MCALL_MSG_SIZE
]; /* marshalled callmsg */
u_int ct_mpos
; /* pos after marshal */
* Create a client handle for a tcp/ip connection.
* If *sockp<0, *sockp is set to a newly created TCP socket and it is
* connected to raddr. If *sockp non-negative then
* raddr is ignored. The rpc/tcp package does buffering
* similar to stdio, so the client must pick send and receive buffer sizes,];
* If raddr->sin_port is 0, then a binder on the remote machine is
* consulted for the right port number.
* NB: *sockp is copied into a private area.
* NB: It is the clients responsibility to close *sockp.
* NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
clnttcp_create(raddr
, prog
, vers
, sockp
, sendsz
, recvsz
)
struct sockaddr_in
*raddr
;
register struct ct_data
*ct
;
h
= (CLIENT
*)mem_alloc(sizeof(*h
));
fprintf(stderr
, "clnttcp_create: out of memory\n");
rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
rpc_createerr
.cf_error
.re_errno
= errno
;
ct
= (struct ct_data
*)mem_alloc(sizeof(*ct
));
fprintf(stderr
, "clnttcp_create: out of memory\n");
rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
rpc_createerr
.cf_error
.re_errno
= errno
;
* If no port number given ask the pmap for one
if (raddr
->sin_port
== 0) {
if ((port
= pmap_getport(raddr
, prog
, vers
, IPPROTO_TCP
)) == 0) {
mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
mem_free((caddr_t
)h
, sizeof(CLIENT
));
raddr
->sin_port
= htons(port
);
* If no socket given, open one
if (((*sockp
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
|| (connect(*sockp
, (struct sockaddr
*)raddr
,
rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
rpc_createerr
.cf_error
.re_errno
= errno
;
* Set up private data struct
* Initialize call message
(void)gettimeofday(&now
, (struct timezone
*)0);
call_msg
.rm_xid
= getpid() ^ (int)random() ^ now
.tv_sec
^ now
.tv_usec
;
call_msg
.rm_direction
= CALL
;
call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
call_msg
.rm_call
.cb_prog
= prog
;
call_msg
.rm_call
.cb_vers
= vers
;
* pre-serialize the staic part of the call msg and stash it away
xdrmem_create(&(ct
->ct_xdrs
), ct
->ct_mcall
, MCALL_MSG_SIZE
,
if (! xdr_callhdr(&(ct
->ct_xdrs
), &call_msg
)) {
ct
->ct_mpos
= XDR_GETPOS(&(ct
->ct_xdrs
));
XDR_DESTROY(&(ct
->ct_xdrs
));
* Create a client handle which uses xdrrec for serialization
* and authnone for authentication.
xdrrec_create(&(ct
->ct_xdrs
), sendsz
, recvsz
,
(caddr_t
)ct
, readtcp
, writetcp
);
h
->cl_private
= (caddr_t
) ct
;
h
->cl_auth
= authnone_create();
* Something goofed, free stuff and barf
mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
mem_free((caddr_t
)h
, sizeof(CLIENT
));
clnttcp_call(h
, proc
, xdr_args
, args_ptr
, xdr_results
, results_ptr
, timeout
)
register struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
register XDR
*xdrs
= &(ct
->ct_xdrs
);
struct rpc_msg reply_msg
;
u_long
*msg_x_id
= (u_long
*)(ct
->ct_mcall
); /* yuk */
(xdr_results
== (xdrproc_t
)0 && timeout
.tv_sec
== 0
&& timeout
.tv_usec
== 0) ? FALSE
: TRUE
;
ct
->ct_error
.re_status
= RPC_SUCCESS
;
x_id
= ntohl(--(*msg_x_id
));
if ((! XDR_PUTBYTES(xdrs
, ct
->ct_mcall
, ct
->ct_mpos
)) ||
(! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
(! AUTH_MARSHALL(h
->cl_auth
, xdrs
)) ||
(! (*xdr_args
)(xdrs
, args_ptr
))) {
if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
ct
->ct_error
.re_status
= RPC_CANTENCODEARGS
;
(void)xdrrec_endofrecord(xdrs
, TRUE
);
return (ct
->ct_error
.re_status
);
if (! xdrrec_endofrecord(xdrs
, shipnow
))
return (ct
->ct_error
.re_status
= RPC_CANTSEND
);
* Keep receiving until we get a valid transaction id
reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
reply_msg
.acpted_rply
.ar_results
.where
= NULL
;
reply_msg
.acpted_rply
.ar_results
.proc
= xdr_void
;
if (! xdrrec_skiprecord(xdrs
))
return (ct
->ct_error
.re_status
);
/* now decode and validate the response header */
if (! xdr_replymsg(xdrs
, &reply_msg
)) {
if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
return (ct
->ct_error
.re_status
);
if (reply_msg
.rm_xid
== x_id
)
_seterr_reply(&reply_msg
, &(ct
->ct_error
));
if (ct
->ct_error
.re_status
== RPC_SUCCESS
) {
if (! AUTH_VALIDATE(h
->cl_auth
, &reply_msg
.acpted_rply
.ar_verf
)) {
ct
->ct_error
.re_status
= RPC_AUTHERROR
;
ct
->ct_error
.re_why
= AUTH_INVALIDRESP
;
} else if (! (*xdr_results
)(xdrs
, results_ptr
)) {
if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
ct
->ct_error
.re_status
= RPC_CANTDECODERES
;
if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
(void)xdr_opaque_auth(xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
} /* end successful completion */
/* maybe our credentials need to be refreshed ... */
if (AUTH_REFRESH(h
->cl_auth
))
} /* end of unsuccessful completion */
return (ct
->ct_error
.re_status
);
register struct ct_data
*ct
=
(struct ct_data
*) h
->cl_private
;
clnttcp_freeres(cl
, xdr_res
, res_ptr
)
register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
register XDR
*xdrs
= &(ct
->ct_xdrs
);
return ((*xdr_res
)(xdrs
, res_ptr
));
register struct ct_data
*ct
=
(struct ct_data
*) h
->cl_private
;
XDR_DESTROY(&(ct
->ct_xdrs
));
mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
mem_free((caddr_t
)h
, sizeof(CLIENT
));
* Interface between xdr serializer and tcp connection.
* Behaves like the system calls, read & write, but keeps some error state
* around for the rpc level.
register struct ct_data
*ct
;
register int mask
= 1 << (ct
->ct_sock
);
switch (select(32, &readfds
, (int*)NULL
, (int*)NULL
,
ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
ct
->ct_error
.re_status
= RPC_CANTRECV
;
ct
->ct_error
.re_errno
= errno
;
if ((len
= read(ct
->ct_sock
, buf
, len
)) == -1) {
ct
->ct_error
.re_errno
= errno
;
ct
->ct_error
.re_status
= RPC_CANTRECV
;
for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
) {
if ((i
= write(ct
->ct_sock
, buf
, cnt
)) == -1) {
ct
->ct_error
.re_errno
= errno
;
ct
->ct_error
.re_status
= RPC_CANTSEND
;