2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid
= "$Id: clnt_tcp.c,v 1.2 1999/10/14 21:56:53 wsanchez Exp $";
60 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
62 * Copyright (C) 1984, Sun Microsystems, Inc.
64 * TCP based RPC supports 'batched calls'.
65 * A sequence of calls may be batched-up in a send buffer. The rpc call
66 * return immediately to the client even though the call was not necessarily
67 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
68 * the rpc timeout value is zero (see clnt.h, rpc).
70 * Clients should NOT casually batch calls that in fact return results; that is,
71 * the server side should be aware that a call is batched and not produce any
72 * return message. Batched calls that produce many result messages can
73 * deadlock (netlock) the client and the server....
75 * Now go hang yourself.
80 #include <sys/socket.h>
83 #include <rpc/pmap_clnt.h>
85 #define MCALL_MSG_SIZE 24
90 static int writetcp();
92 static enum clnt_stat
clnttcp_call();
93 static void clnttcp_abort();
94 static void clnttcp_geterr();
95 static bool_t
clnttcp_freeres();
96 static bool_t
clnttcp_control();
97 static void clnttcp_destroy();
99 static struct clnt_ops tcp_ops
= {
111 struct timeval ct_wait
;
112 bool_t ct_waitset
; /* wait set by clnt_control? */
113 struct sockaddr_in ct_addr
;
114 struct rpc_err ct_error
;
115 char ct_mcall
[MCALL_MSG_SIZE
]; /* marshalled callmsg */
116 u_int ct_mpos
; /* pos after marshal */
121 * Create a client handle for a tcp/ip connection.
122 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
123 * connected to raddr. If *sockp non-negative then
124 * raddr is ignored. The rpc/tcp package does buffering
125 * similar to stdio, so the client must pick send and receive buffer sizes,];
126 * 0 => use the default.
127 * If raddr->sin_port is 0, then a binder on the remote machine is
128 * consulted for the right port number.
129 * NB: *sockp is copied into a private area.
130 * NB: It is the clients responsibility to close *sockp.
131 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
132 * something more useful.
135 clnttcp_create(raddr
, prog
, vers
, sockp
, sendsz
, recvsz
)
136 struct sockaddr_in
*raddr
;
144 register struct ct_data
*ct
;
146 struct rpc_msg call_msg
;
148 h
= (CLIENT
*)mem_alloc(sizeof(*h
));
150 (void)fprintf(stderr
, "clnttcp_create: out of memory\n");
151 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
152 rpc_createerr
.cf_error
.re_errno
= errno
;
155 ct
= (struct ct_data
*)mem_alloc(sizeof(*ct
));
157 (void)fprintf(stderr
, "clnttcp_create: out of memory\n");
158 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
159 rpc_createerr
.cf_error
.re_errno
= errno
;
164 * If no port number given ask the pmap for one
166 if (raddr
->sin_port
== 0) {
168 if ((port
= pmap_getport(raddr
, prog
, vers
, IPPROTO_TCP
)) == 0) {
169 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
170 mem_free((caddr_t
)h
, sizeof(CLIENT
));
171 return ((CLIENT
*)NULL
);
173 raddr
->sin_port
= htons(port
);
177 * If no socket given, open one
180 *sockp
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
181 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
183 || (connect(*sockp
, (struct sockaddr
*)raddr
,
184 sizeof(*raddr
)) < 0)) {
185 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
186 rpc_createerr
.cf_error
.re_errno
= errno
;
190 ct
->ct_closeit
= TRUE
;
192 ct
->ct_closeit
= FALSE
;
196 * Set up private data struct
198 ct
->ct_sock
= *sockp
;
199 ct
->ct_wait
.tv_usec
= 0;
200 ct
->ct_waitset
= FALSE
;
201 ct
->ct_addr
= *raddr
;
204 * Initialize call message
206 (void)gettimeofday(&now
, (struct timezone
*)0);
207 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
208 call_msg
.rm_direction
= CALL
;
209 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
210 call_msg
.rm_call
.cb_prog
= prog
;
211 call_msg
.rm_call
.cb_vers
= vers
;
214 * pre-serialize the staic part of the call msg and stash it away
216 xdrmem_create(&(ct
->ct_xdrs
), ct
->ct_mcall
, MCALL_MSG_SIZE
,
218 if (! xdr_callhdr(&(ct
->ct_xdrs
), &call_msg
)) {
219 if (ct
->ct_closeit
) {
224 ct
->ct_mpos
= XDR_GETPOS(&(ct
->ct_xdrs
));
225 XDR_DESTROY(&(ct
->ct_xdrs
));
228 * Create a client handle which uses xdrrec for serialization
229 * and authnone for authentication.
231 xdrrec_create(&(ct
->ct_xdrs
), sendsz
, recvsz
,
232 (caddr_t
)ct
, readtcp
, writetcp
);
233 h
->cl_ops
= &tcp_ops
;
234 h
->cl_private
= (caddr_t
) ct
;
235 h
->cl_auth
= authnone_create();
240 * Something goofed, free stuff and barf
242 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
243 mem_free((caddr_t
)h
, sizeof(CLIENT
));
244 return ((CLIENT
*)NULL
);
247 static enum clnt_stat
248 clnttcp_call(h
, proc
, xdr_args
, args_ptr
, xdr_results
, results_ptr
, timeout
)
253 xdrproc_t xdr_results
;
255 struct timeval timeout
;
257 register struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
258 register XDR
*xdrs
= &(ct
->ct_xdrs
);
259 struct rpc_msg reply_msg
;
261 u_long
*msg_x_id
= (u_long
*)(ct
->ct_mcall
); /* yuk */
262 register bool_t shipnow
;
265 if (!ct
->ct_waitset
) {
266 ct
->ct_wait
= timeout
;
270 (xdr_results
== (xdrproc_t
)0 && timeout
.tv_sec
== 0
271 && timeout
.tv_usec
== 0) ? FALSE
: TRUE
;
274 xdrs
->x_op
= XDR_ENCODE
;
275 ct
->ct_error
.re_status
= RPC_SUCCESS
;
276 x_id
= ntohl(--(*msg_x_id
));
277 if ((! XDR_PUTBYTES(xdrs
, ct
->ct_mcall
, ct
->ct_mpos
)) ||
278 (! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
279 (! AUTH_MARSHALL(h
->cl_auth
, xdrs
)) ||
280 (! (*xdr_args
)(xdrs
, args_ptr
))) {
281 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
282 ct
->ct_error
.re_status
= RPC_CANTENCODEARGS
;
283 (void)xdrrec_endofrecord(xdrs
, TRUE
);
284 return (ct
->ct_error
.re_status
);
286 if (! xdrrec_endofrecord(xdrs
, shipnow
))
287 return (ct
->ct_error
.re_status
= RPC_CANTSEND
);
289 return (RPC_SUCCESS
);
291 * Hack to provide rpc-based message passing
293 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
294 return(ct
->ct_error
.re_status
= RPC_TIMEDOUT
);
299 * Keep receiving until we get a valid transaction id
301 xdrs
->x_op
= XDR_DECODE
;
303 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
304 reply_msg
.acpted_rply
.ar_results
.where
= NULL
;
305 reply_msg
.acpted_rply
.ar_results
.proc
= xdr_void
;
306 if (! xdrrec_skiprecord(xdrs
))
307 return (ct
->ct_error
.re_status
);
308 /* now decode and validate the response header */
309 if (! xdr_replymsg(xdrs
, &reply_msg
)) {
310 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
312 return (ct
->ct_error
.re_status
);
314 if (reply_msg
.rm_xid
== x_id
)
321 _seterr_reply(&reply_msg
, &(ct
->ct_error
));
322 if (ct
->ct_error
.re_status
== RPC_SUCCESS
) {
323 if (! AUTH_VALIDATE(h
->cl_auth
, &reply_msg
.acpted_rply
.ar_verf
)) {
324 ct
->ct_error
.re_status
= RPC_AUTHERROR
;
325 ct
->ct_error
.re_why
= AUTH_INVALIDRESP
;
326 } else if (! (*xdr_results
)(xdrs
, results_ptr
)) {
327 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
328 ct
->ct_error
.re_status
= RPC_CANTDECODERES
;
330 /* free verifier ... */
331 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
332 xdrs
->x_op
= XDR_FREE
;
333 (void)xdr_opaque_auth(xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
335 } /* end successful completion */
337 /* maybe our credentials need to be refreshed ... */
338 if (refreshes
-- && AUTH_REFRESH(h
->cl_auth
))
340 } /* end of unsuccessful completion */
341 return (ct
->ct_error
.re_status
);
345 clnttcp_geterr(h
, errp
)
347 struct rpc_err
*errp
;
349 register struct ct_data
*ct
=
350 (struct ct_data
*) h
->cl_private
;
352 *errp
= ct
->ct_error
;
356 clnttcp_freeres(cl
, xdr_res
, res_ptr
)
361 register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
362 register XDR
*xdrs
= &(ct
->ct_xdrs
);
364 xdrs
->x_op
= XDR_FREE
;
365 return ((*xdr_res
)(xdrs
, res_ptr
));
374 clnttcp_control(cl
, request
, info
)
379 register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
383 ct
->ct_wait
= *(struct timeval
*)info
;
384 ct
->ct_waitset
= TRUE
;
387 *(struct timeval
*)info
= ct
->ct_wait
;
389 case CLGET_SERVER_ADDR
:
390 *(struct sockaddr_in
*)info
= ct
->ct_addr
;
403 register struct ct_data
*ct
=
404 (struct ct_data
*) h
->cl_private
;
406 if (ct
->ct_closeit
) {
407 (void)close(ct
->ct_sock
);
409 XDR_DESTROY(&(ct
->ct_xdrs
));
410 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
411 mem_free((caddr_t
)h
, sizeof(CLIENT
));
415 * Interface between xdr serializer and tcp connection.
416 * Behaves like the system calls, read & write, but keeps some error state
417 * around for the rpc level.
420 readtcp(ct
, buf
, len
)
421 register struct ct_data
*ct
;
431 FD_SET(ct
->ct_sock
, &mask
);
434 switch (select(ct
->ct_sock
+1, &readfds
, (int*)NULL
, (int*)NULL
,
437 ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
443 ct
->ct_error
.re_status
= RPC_CANTRECV
;
444 ct
->ct_error
.re_errno
= errno
;
449 switch (len
= read(ct
->ct_sock
, buf
, len
)) {
453 ct
->ct_error
.re_errno
= ECONNRESET
;
454 ct
->ct_error
.re_status
= RPC_CANTRECV
;
455 len
= -1; /* it's really an error */
459 ct
->ct_error
.re_errno
= errno
;
460 ct
->ct_error
.re_status
= RPC_CANTRECV
;
467 writetcp(ct
, buf
, len
)
474 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
) {
475 if ((i
= write(ct
->ct_sock
, buf
, cnt
)) == -1) {
476 ct
->ct_error
.re_errno
= errno
;
477 ct
->ct_error
.re_status
= RPC_CANTSEND
;