]>
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 * 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.4 2002/03/15 22:07:48 majka 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 * returns 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.
83 #include <sys/socket.h>
84 #include <sys/fcntl.h>
87 #include <rpc/pmap_clnt.h>
89 #define MCALL_MSG_SIZE 24
93 extern int bindresvport();
94 extern bool_t
xdr_opaque_auth();
96 extern u_short
pmap_getport_timeout(struct sockaddr_in
*address
, uint32_t program
, uint32_t version
, uint32_t protocol
, struct timeval
*timeout
, struct timeval
*totaltimeout
);
99 static int writetcp();
101 static enum clnt_stat
clnttcp_call();
102 static void clnttcp_abort();
103 static void clnttcp_geterr();
104 static bool_t
clnttcp_freeres();
105 static bool_t
clnttcp_control();
106 static void clnttcp_destroy();
108 static struct clnt_ops tcp_ops
= {
120 struct timeval ct_timeout
;
121 bool_t ct_timeout_set
; /* timeout set by clnt_control? */
122 struct sockaddr_in ct_addr
;
123 struct rpc_err ct_error
;
124 char ct_mcall
[MCALL_MSG_SIZE
]; /* marshalled callmsg */
125 u_int ct_mpos
; /* pos after marshal */
130 * Create a client handle for a tcp/ip connection.
131 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
132 * connected to raddr. If *sockp non-negative then
133 * raddr is ignored. The rpc/tcp package does buffering
134 * similar to stdio, so the client must pick send and receive buffer sizes,];
135 * 0 => use the default.
136 * If raddr->sin_port is 0, then a binder on the remote machine is
137 * consulted for the right port number.
138 * NB: *sockp is copied into a private area.
139 * NB: It is the clients responsibility to close *sockp.
140 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
141 * something more useful.
144 clnttcp_create_timeout(struct sockaddr_in
*raddr
, uint32_t prog
, uint32_t vers
, int *sockp
, uint32_t sendsz
, uint32_t recvsz
, struct timeval
*retry_timeout
, struct timeval
*total_timeout
)
147 register struct ct_data
*ct
= NULL
;
149 struct rpc_msg call_msg
;
153 h
= (CLIENT
*)mem_alloc(sizeof(*h
));
156 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
157 rpc_createerr
.cf_error
.re_errno
= errno
;
161 ct
= (struct ct_data
*)mem_alloc(sizeof(*ct
));
164 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
165 rpc_createerr
.cf_error
.re_errno
= errno
;
170 * If no port number given ask the pmap for one
172 if (raddr
->sin_port
== 0)
174 port
= pmap_getport_timeout(raddr
, prog
, vers
, IPPROTO_TCP
, retry_timeout
, total_timeout
);
177 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
178 mem_free((caddr_t
)h
, sizeof(CLIENT
));
182 raddr
->sin_port
= htons(port
);
186 * If no socket given, open one
190 *sockp
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
191 bindresvport(*sockp
, (struct sockaddr_in
*)0);
192 if ((*sockp
< 0) || (connect(*sockp
, (struct sockaddr
*)raddr
, sizeof(*raddr
)) < 0))
194 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
195 rpc_createerr
.cf_error
.re_errno
= errno
;
200 ct
->ct_closeit
= TRUE
;
204 ct
->ct_closeit
= FALSE
;
208 * Set up private data struct
210 ct
->ct_sock
= *sockp
;
211 ct
->ct_timeout
.tv_sec
= 60;
212 ct
->ct_timeout
.tv_usec
= 0;
213 ct
->ct_timeout_set
= FALSE
;
214 if (total_timeout
!= NULL
)
216 ct
->ct_timeout
= *total_timeout
;
217 ct
->ct_timeout_set
= TRUE
;
219 ct
->ct_addr
= *raddr
;
222 * Initialize call message
224 rfd
= open("/dev/random", O_RDONLY
, 0);
225 if ((rfd
< 0) || (read(rfd
, &call_msg
.rm_xid
, sizeof(call_msg
.rm_xid
)) != sizeof(call_msg
.rm_xid
)))
227 gettimeofday(&now
, (struct timezone
*)0);
228 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
231 if (rfd
> 0) close(rfd
);
233 call_msg
.rm_direction
= CALL
;
234 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
235 call_msg
.rm_call
.cb_prog
= prog
;
236 call_msg
.rm_call
.cb_vers
= vers
;
239 * pre-serialize the staic part of the call msg and stash it away
241 xdrmem_create(&(ct
->ct_xdrs
), ct
->ct_mcall
, MCALL_MSG_SIZE
, XDR_ENCODE
);
242 if (! xdr_callhdr(&(ct
->ct_xdrs
), &call_msg
))
244 if (ct
->ct_closeit
) close(*sockp
);
248 ct
->ct_mpos
= XDR_GETPOS(&(ct
->ct_xdrs
));
249 XDR_DESTROY(&(ct
->ct_xdrs
));
252 * Create a client handle which uses xdrrec for serialization
253 * and authnone for authentication.
255 xdrrec_create(&(ct
->ct_xdrs
), sendsz
, recvsz
, (caddr_t
)ct
, readtcp
, writetcp
);
256 h
->cl_ops
= &tcp_ops
;
257 h
->cl_private
= (caddr_t
)ct
;
258 h
->cl_auth
= authnone_create();
262 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
263 mem_free((caddr_t
)h
, sizeof(CLIENT
));
268 clnttcp_create(raddr
, prog
, vers
, sockp
, sendsz
, recvsz
)
270 struct sockaddr_in
*raddr
;
277 struct sockaddr_in
*raddr
;
285 return clnttcp_create_timeout(raddr
, (uint32_t)prog
, (uint32_t)vers
, sockp
, (uint32_t)sendsz
, (uint32_t)recvsz
, NULL
, NULL
);
288 static enum clnt_stat
289 clnttcp_call(h
, proc
, xdr_args
, args_ptr
, xdr_results
, results_ptr
, timeout
)
298 xdrproc_t xdr_results
;
300 struct timeval timeout
;
302 register struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
303 register XDR
*xdrs
= &(ct
->ct_xdrs
);
304 struct rpc_msg reply_msg
;
307 uint32_t *msg_x_id
= (uint32_t *)(ct
->ct_mcall
); /* yuk */
310 u_long
*msg_x_id
= (u_long
*)(ct
->ct_mcall
); /* yuk */
312 register bool_t shipnow
;
315 if (!ct
->ct_timeout_set
) {
316 ct
->ct_timeout
= timeout
;
320 (xdr_results
== (xdrproc_t
)NULLPROC
|| (timeout
.tv_sec
== 0
321 && timeout
.tv_usec
== 0)) ? FALSE
: TRUE
;
324 xdrs
->x_op
= XDR_ENCODE
;
325 ct
->ct_error
.re_status
= RPC_SUCCESS
;
326 x_id
= ntohl(--(*msg_x_id
));
328 if ((! XDR_PUTBYTES(xdrs
, ct
->ct_mcall
, ct
->ct_mpos
)) ||
329 (! XDR_PUTLONG(xdrs
, (int *)&proc
)) ||
330 (! AUTH_MARSHALL(h
->cl_auth
, xdrs
)) ||
331 (! (*xdr_args
)(xdrs
, args_ptr
, 0)))
333 if ((! XDR_PUTBYTES(xdrs
, ct
->ct_mcall
, ct
->ct_mpos
)) ||
334 (! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
335 (! AUTH_MARSHALL(h
->cl_auth
, xdrs
)) ||
336 (! (*xdr_args
)(xdrs
, args_ptr
, 0)))
339 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
340 ct
->ct_error
.re_status
= RPC_CANTENCODEARGS
;
341 (void)xdrrec_endofrecord(xdrs
, TRUE
);
342 return (ct
->ct_error
.re_status
);
344 if (! xdrrec_endofrecord(xdrs
, shipnow
))
345 return (ct
->ct_error
.re_status
= RPC_CANTSEND
);
347 return (RPC_SUCCESS
);
349 * Hack to provide rpc-based message passing
351 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
352 return(ct
->ct_error
.re_status
= RPC_TIMEDOUT
);
357 * Keep receiving until we get a valid transaction id
359 xdrs
->x_op
= XDR_DECODE
;
361 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
362 reply_msg
.acpted_rply
.ar_results
.where
= NULL
;
363 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
)xdr_void
;
364 if (! xdrrec_skiprecord(xdrs
))
365 return (ct
->ct_error
.re_status
);
366 /* now decode and validate the response header */
367 if (! xdr_replymsg(xdrs
, &reply_msg
)) {
368 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
370 return (ct
->ct_error
.re_status
);
372 if (reply_msg
.rm_xid
== x_id
)
379 _seterr_reply(&reply_msg
, &(ct
->ct_error
));
380 if (ct
->ct_error
.re_status
== RPC_SUCCESS
) {
381 if (! AUTH_VALIDATE(h
->cl_auth
, &reply_msg
.acpted_rply
.ar_verf
)) {
382 ct
->ct_error
.re_status
= RPC_AUTHERROR
;
383 ct
->ct_error
.re_why
= AUTH_INVALIDRESP
;
384 } else if (! (*xdr_results
)(xdrs
, results_ptr
, 0)) {
385 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
386 ct
->ct_error
.re_status
= RPC_CANTDECODERES
;
388 /* free verifier ... */
389 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
390 xdrs
->x_op
= XDR_FREE
;
391 (void)xdr_opaque_auth(xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
393 } /* end successful completion */
395 /* maybe our credentials need to be refreshed ... */
396 if (refreshes
-- && AUTH_REFRESH(h
->cl_auth
))
398 } /* end of unsuccessful completion */
399 return (ct
->ct_error
.re_status
);
403 clnttcp_geterr(h
, errp
)
405 struct rpc_err
*errp
;
407 register struct ct_data
*ct
=
408 (struct ct_data
*) h
->cl_private
;
410 *errp
= ct
->ct_error
;
414 clnttcp_freeres(cl
, xdr_res
, res_ptr
)
419 register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
420 register XDR
*xdrs
= &(ct
->ct_xdrs
);
422 xdrs
->x_op
= XDR_FREE
;
423 return ((*xdr_res
)(xdrs
, res_ptr
, 0));
432 clnttcp_control(cl
, request
, info
)
437 register struct ct_data
*ct
= (struct ct_data
*)cl
->cl_private
;
441 ct
->ct_timeout
= *(struct timeval
*)info
;
442 ct
->ct_timeout_set
= TRUE
;
445 *(struct timeval
*)info
= ct
->ct_timeout
;
447 case CLGET_SERVER_ADDR
:
448 *(struct sockaddr_in
*)info
= ct
->ct_addr
;
461 register struct ct_data
*ct
=
462 (struct ct_data
*) h
->cl_private
;
464 if (ct
->ct_closeit
) {
465 (void)close(ct
->ct_sock
);
467 XDR_DESTROY(&(ct
->ct_xdrs
));
468 mem_free((caddr_t
)ct
, sizeof(struct ct_data
));
469 mem_free((caddr_t
)h
, sizeof(CLIENT
));
473 * Interface between xdr serializer and tcp connection.
474 * Behaves like the system calls, read & write, but keeps some error state
475 * around for the rpc level.
478 readtcp(ct
, buf
, len
)
479 register struct ct_data
*ct
;
489 FD_SET(ct
->ct_sock
, &mask
);
492 switch (select(ct
->ct_sock
+1, &readfds
, NULL
, NULL
, &(ct
->ct_timeout
))) {
494 ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
500 ct
->ct_error
.re_status
= RPC_CANTRECV
;
501 ct
->ct_error
.re_errno
= errno
;
506 switch (len
= read(ct
->ct_sock
, buf
, len
)) {
510 ct
->ct_error
.re_errno
= ECONNRESET
;
511 ct
->ct_error
.re_status
= RPC_CANTRECV
;
512 len
= -1; /* it's really an error */
516 ct
->ct_error
.re_errno
= errno
;
517 ct
->ct_error
.re_status
= RPC_CANTRECV
;
524 writetcp(ct
, buf
, len
)
531 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
) {
532 if ((i
= write(ct
->ct_sock
, buf
, cnt
)) == -1) {
533 ct
->ct_error
.re_errno
= errno
;
534 ct
->ct_error
.re_status
= RPC_CANTSEND
;