]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/clnt_tcp.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";*/
56 /*static char *sccsid = "from: @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/
57 static char *rcsid
= "$Id: clnt_tcp.c,v 1.4 2002/03/15 22:07:48 majka Exp $";
61 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
63 * Copyright (C) 1984, Sun Microsystems, Inc.
65 * TCP based RPC supports 'batched calls'.
66 * A sequence of calls may be batched-up in a send buffer. The rpc call
67 * return immediately to the client even though the call was not necessarily
68 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
69 * the rpc timeout value is zero (see clnt.h, rpc).
71 * Clients should NOT casually batch calls that in fact return results; that is,
72 * the server side should be aware that a call is batched and not produce any
73 * return message. Batched calls that produce many result messages can
74 * deadlock (netlock) the client and the server....
76 * Now go hang yourself.
84 #include <sys/socket.h>
85 #include <sys/fcntl.h>
88 #include <rpc/pmap_clnt.h>
90 #define MCALL_MSG_SIZE 24
94 extern int bindresvport();
95 extern bool_t
xdr_opaque_auth();
98 static int writetcp();
100 static enum clnt_stat
clnttcp_call();
101 static void clnttcp_abort();
102 static void clnttcp_geterr();
103 static bool_t
clnttcp_freeres();
104 static bool_t
clnttcp_control();
105 static void clnttcp_destroy();
107 static struct clnt_ops tcp_ops
= {
119 struct timeval ct_wait
;
120 bool_t ct_waitset
; /* wait set by clnt_control? */
121 struct sockaddr_in ct_addr
;
122 struct rpc_err ct_error
;
123 char ct_mcall
[MCALL_MSG_SIZE
]; /* marshalled callmsg */
124 u_int ct_mpos
; /* pos after marshal */
129 * Create a client handle for a tcp/ip connection.
130 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
131 * connected to raddr. If *sockp non-negative then
132 * raddr is ignored. The rpc/tcp package does buffering
133 * similar to stdio, so the client must pick send and receive buffer sizes,];
134 * 0 => use the default.
135 * If raddr->sin_port is 0, then a binder on the remote machine is
136 * consulted for the right port number.
137 * NB: *sockp is copied into a private area.
138 * NB: It is the clients responsibility to close *sockp.
139 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
140 * something more useful.
143 clnttcp_create(raddr
, prog
, vers
, sockp
, sendsz
, recvsz
)
144 struct sockaddr_in
*raddr
;
152 register struct ct_data
*ct
= NULL
;
154 struct rpc_msg call_msg
;
157 h
= (CLIENT
*)mem_alloc(sizeof(*h
));
159 (void)fprintf(stderr
, "clnttcp_create: out of memory\n");
160 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
161 rpc_createerr
.cf_error
.re_errno
= errno
;
164 ct
= (struct ct_data
*)mem_alloc(sizeof(*ct
));
166 (void)fprintf(stderr
, "clnttcp_create: out of memory\n");
167 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
168 rpc_createerr
.cf_error
.re_errno
= errno
;
173 * If no port number given ask the pmap for one
175 if (raddr
->sin_port
== 0) {
177 if ((port
= pmap_getport(raddr
, prog
, vers
, IPPROTO_TCP
)) == 0) {
178 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
179 mem_free((caddr_t
)h
, sizeof(CLIENT
));
180 return ((CLIENT
*)NULL
);
182 raddr
->sin_port
= htons(port
);
186 * If no socket given, open one
189 *sockp
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
190 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
192 || (connect(*sockp
, (struct sockaddr
*)raddr
,
193 sizeof(*raddr
)) < 0)) {
194 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
195 rpc_createerr
.cf_error
.re_errno
= errno
;
199 ct
->ct_closeit
= TRUE
;
201 ct
->ct_closeit
= FALSE
;
205 * Set up private data struct
207 ct
->ct_sock
= *sockp
;
208 ct
->ct_wait
.tv_usec
= 0;
209 ct
->ct_waitset
= FALSE
;
210 ct
->ct_addr
= *raddr
;
213 * Initialize call message
215 rfd
= open("/dev/random", O_RDONLY
, 0);
216 if ((rfd
< 0) || (read(rfd
, &call_msg
.rm_xid
, sizeof(call_msg
.rm_xid
)) != sizeof(call_msg
.rm_xid
)))
218 gettimeofday(&now
, (struct timezone
*)0);
219 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
221 if (rfd
> 0) close(rfd
);
223 call_msg
.rm_direction
= CALL
;
224 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
225 call_msg
.rm_call
.cb_prog
= prog
;
226 call_msg
.rm_call
.cb_vers
= vers
;
229 * pre-serialize the staic part of the call msg and stash it away
231 xdrmem_create(&(ct
->ct_xdrs
), ct
->ct_mcall
, MCALL_MSG_SIZE
,
233 if (! xdr_callhdr(&(ct
->ct_xdrs
), &call_msg
)) {
234 if (ct
->ct_closeit
) {
239 ct
->ct_mpos
= XDR_GETPOS(&(ct
->ct_xdrs
));
240 XDR_DESTROY(&(ct
->ct_xdrs
));
243 * Create a client handle which uses xdrrec for serialization
244 * and authnone for authentication.
246 xdrrec_create(&(ct
->ct_xdrs
), sendsz
, recvsz
,
247 (caddr_t
)ct
, readtcp
, writetcp
);
248 h
->cl_ops
= &tcp_ops
;
249 h
->cl_private
= (caddr_t
) ct
;
250 h
->cl_auth
= authnone_create();
255 * Something goofed, free stuff and barf
257 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
258 mem_free((caddr_t
)h
, sizeof(CLIENT
));
259 return ((CLIENT
*)NULL
);
262 static enum clnt_stat
263 clnttcp_call(h
, proc
, xdr_args
, args_ptr
, xdr_results
, results_ptr
, timeout
)
268 xdrproc_t xdr_results
;
270 struct timeval timeout
;
272 register struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
273 register XDR
*xdrs
= &(ct
->ct_xdrs
);
274 struct rpc_msg reply_msg
;
276 u_long
*msg_x_id
= (u_long
*)(ct
->ct_mcall
); /* yuk */
277 register bool_t shipnow
;
280 if (!ct
->ct_waitset
) {
281 ct
->ct_wait
= timeout
;
285 (xdr_results
== (xdrproc_t
)0 && timeout
.tv_sec
== 0
286 && timeout
.tv_usec
== 0) ? FALSE
: TRUE
;
289 xdrs
->x_op
= XDR_ENCODE
;
290 ct
->ct_error
.re_status
= RPC_SUCCESS
;
291 x_id
= ntohl(--(*msg_x_id
));
292 if ((! XDR_PUTBYTES(xdrs
, ct
->ct_mcall
, ct
->ct_mpos
)) ||
293 (! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
294 (! AUTH_MARSHALL(h
->cl_auth
, xdrs
)) ||
295 (! (*xdr_args
)(xdrs
, args_ptr
))) {
296 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
297 ct
->ct_error
.re_status
= RPC_CANTENCODEARGS
;
298 (void)xdrrec_endofrecord(xdrs
, TRUE
);
299 return (ct
->ct_error
.re_status
);
301 if (! xdrrec_endofrecord(xdrs
, shipnow
))
302 return (ct
->ct_error
.re_status
= RPC_CANTSEND
);
304 return (RPC_SUCCESS
);
306 * Hack to provide rpc-based message passing
308 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
309 return(ct
->ct_error
.re_status
= RPC_TIMEDOUT
);
314 * Keep receiving until we get a valid transaction id
316 xdrs
->x_op
= XDR_DECODE
;
318 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
319 reply_msg
.acpted_rply
.ar_results
.where
= NULL
;
320 reply_msg
.acpted_rply
.ar_results
.proc
= xdr_void
;
321 if (! xdrrec_skiprecord(xdrs
))
322 return (ct
->ct_error
.re_status
);
323 /* now decode and validate the response header */
324 if (! xdr_replymsg(xdrs
, &reply_msg
)) {
325 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
327 return (ct
->ct_error
.re_status
);
329 if (reply_msg
.rm_xid
== x_id
)
336 _seterr_reply(&reply_msg
, &(ct
->ct_error
));
337 if (ct
->ct_error
.re_status
== RPC_SUCCESS
) {
338 if (! AUTH_VALIDATE(h
->cl_auth
, &reply_msg
.acpted_rply
.ar_verf
)) {
339 ct
->ct_error
.re_status
= RPC_AUTHERROR
;
340 ct
->ct_error
.re_why
= AUTH_INVALIDRESP
;
341 } else if (! (*xdr_results
)(xdrs
, results_ptr
)) {
342 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
343 ct
->ct_error
.re_status
= RPC_CANTDECODERES
;
345 /* free verifier ... */
346 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
347 xdrs
->x_op
= XDR_FREE
;
348 (void)xdr_opaque_auth(xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
350 } /* end successful completion */
352 /* maybe our credentials need to be refreshed ... */
353 if (refreshes
-- && AUTH_REFRESH(h
->cl_auth
))
355 } /* end of unsuccessful completion */
356 return (ct
->ct_error
.re_status
);
360 clnttcp_geterr(h
, errp
)
362 struct rpc_err
*errp
;
364 register struct ct_data
*ct
=
365 (struct ct_data
*) h
->cl_private
;
367 *errp
= ct
->ct_error
;
371 clnttcp_freeres(cl
, xdr_res
, res_ptr
)
376 register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
377 register XDR
*xdrs
= &(ct
->ct_xdrs
);
379 xdrs
->x_op
= XDR_FREE
;
380 return ((*xdr_res
)(xdrs
, res_ptr
));
389 clnttcp_control(cl
, request
, info
)
394 register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
398 ct
->ct_wait
= *(struct timeval
*)info
;
399 ct
->ct_waitset
= TRUE
;
402 *(struct timeval
*)info
= ct
->ct_wait
;
404 case CLGET_SERVER_ADDR
:
405 *(struct sockaddr_in
*)info
= ct
->ct_addr
;
418 register struct ct_data
*ct
=
419 (struct ct_data
*) h
->cl_private
;
421 if (ct
->ct_closeit
) {
422 (void)close(ct
->ct_sock
);
424 XDR_DESTROY(&(ct
->ct_xdrs
));
425 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
426 mem_free((caddr_t
)h
, sizeof(CLIENT
));
430 * Interface between xdr serializer and tcp connection.
431 * Behaves like the system calls, read & write, but keeps some error state
432 * around for the rpc level.
435 readtcp(ct
, buf
, len
)
436 register struct ct_data
*ct
;
446 FD_SET(ct
->ct_sock
, &mask
);
449 switch (select(ct
->ct_sock
+1, &readfds
, NULL
, NULL
,
452 ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
458 ct
->ct_error
.re_status
= RPC_CANTRECV
;
459 ct
->ct_error
.re_errno
= errno
;
464 switch (len
= read(ct
->ct_sock
, buf
, len
)) {
468 ct
->ct_error
.re_errno
= ECONNRESET
;
469 ct
->ct_error
.re_status
= RPC_CANTRECV
;
470 len
= -1; /* it's really an error */
474 ct
->ct_error
.re_errno
= errno
;
475 ct
->ct_error
.re_status
= RPC_CANTRECV
;
482 writetcp(ct
, buf
, len
)
489 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
) {
490 if ((i
= write(ct
->ct_sock
, buf
, cnt
)) == -1) {
491 ct
->ct_error
.re_errno
= errno
;
492 ct
->ct_error
.re_status
= RPC_CANTSEND
;