]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/clnt_udp.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_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid
= "$Id: clnt_udp.c,v 1.4 2002/03/15 22:07:49 majka Exp $";
60 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
62 * Copyright (C) 1984, Sun Microsystems, Inc.
70 #include <sys/socket.h>
71 #include <sys/fcntl.h>
72 #include <sys/ioctl.h>
75 #include <rpc/pmap_clnt.h>
77 extern int bindresvport();
78 extern bool_t
xdr_opaque_auth();
83 * UDP bases client side rpc operations
85 static enum clnt_stat
clntudp_call();
86 static void clntudp_abort();
87 static void clntudp_geterr();
88 static bool_t
clntudp_freeres();
89 static bool_t
clntudp_control();
90 static void clntudp_destroy();
92 static struct clnt_ops udp_ops
= {
102 * Private data kept per client handle
107 struct sockaddr_in cu_raddr
;
109 struct timeval cu_wait
;
110 struct timeval cu_total
;
111 struct rpc_err cu_error
;
121 * Create a UDP based client handle.
122 * If *sockp<0, *sockp is set to a newly created UPD socket.
123 * If raddr->sin_port is 0 a binder on the remote machine
124 * is consulted for the correct port number.
125 * NB: It is the clients responsibility to close *sockp.
126 * NB: The rpch->cl_auth is initialized to null authentication.
127 * Caller may wish to set this something more useful.
129 * wait is the amount of time used between retransmitting a call if
130 * no response has been heard; retransmition occurs until the actual
131 * rpc call times out.
133 * sendsz and recvsz are the maximum allowable packet sizes that can be
137 clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
138 struct sockaddr_in
*raddr
;
147 register struct cu_data
*cu
= NULL
;
149 struct rpc_msg call_msg
;
152 cl
= (CLIENT
*)mem_alloc(sizeof(CLIENT
));
154 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
155 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
156 rpc_createerr
.cf_error
.re_errno
= errno
;
159 sendsz
= ((sendsz
+ 3) / 4) * 4;
160 recvsz
= ((recvsz
+ 3) / 4) * 4;
161 cu
= (struct cu_data
*)mem_alloc(sizeof(*cu
) + sendsz
+ recvsz
);
163 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
164 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
165 rpc_createerr
.cf_error
.re_errno
= errno
;
168 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
170 if (raddr
->sin_port
== 0) {
173 pmap_getport(raddr
, program
, version
, IPPROTO_UDP
)) == 0) {
176 raddr
->sin_port
= htons(port
);
178 cl
->cl_ops
= &udp_ops
;
179 cl
->cl_private
= (caddr_t
)cu
;
180 cu
->cu_raddr
= *raddr
;
181 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
183 cu
->cu_total
.tv_sec
= -1;
184 cu
->cu_total
.tv_usec
= -1;
185 cu
->cu_sendsz
= sendsz
;
186 cu
->cu_recvsz
= recvsz
;
188 rfd
= open("/dev/random", O_RDONLY
, 0);
189 if ((rfd
< 0) || (read(rfd
, &call_msg
.rm_xid
, sizeof(call_msg
.rm_xid
)) != sizeof(call_msg
.rm_xid
)))
191 gettimeofday(&now
, (struct timezone
*)0);
192 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
194 if (rfd
> 0) close(rfd
);
196 call_msg
.rm_direction
= CALL
;
197 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
198 call_msg
.rm_call
.cb_prog
= program
;
199 call_msg
.rm_call
.cb_vers
= version
;
200 xdrmem_create(&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
202 if (! xdr_callhdr(&(cu
->cu_outxdrs
), &call_msg
)) {
205 cu
->cu_xdrpos
= XDR_GETPOS(&(cu
->cu_outxdrs
));
209 *sockp
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
211 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
212 rpc_createerr
.cf_error
.re_errno
= errno
;
215 /* attempt to bind to prov port */
216 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
217 /* the sockets rpc controls are non-blocking */
218 (void)ioctl(*sockp
, FIONBIO
, (char *) &dontblock
);
219 cu
->cu_closeit
= TRUE
;
221 cu
->cu_closeit
= FALSE
;
223 cu
->cu_sock
= *sockp
;
224 cl
->cl_auth
= authnone_create();
228 mem_free((caddr_t
)cu
, sizeof(*cu
) + sendsz
+ recvsz
);
230 mem_free((caddr_t
)cl
, sizeof(CLIENT
));
231 return ((CLIENT
*)NULL
);
235 clntudp_create(raddr
, program
, version
, wait
, sockp
)
236 struct sockaddr_in
*raddr
;
243 return(clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
,
244 UDPMSGSIZE
, UDPMSGSIZE
));
247 static enum clnt_stat
248 clntudp_call(cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
249 register CLIENT
*cl
; /* client handle */
250 u_long proc
; /* procedure number */
251 xdrproc_t xargs
; /* xdr routine for args */
252 caddr_t argsp
; /* pointer to args */
253 xdrproc_t xresults
; /* xdr routine for results */
254 caddr_t resultsp
; /* pointer to results */
255 struct timeval utimeout
; /* seconds to wait before giving up */
257 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
264 struct sockaddr_in from
;
265 struct rpc_msg reply_msg
;
267 struct timeval time_waited
;
269 int nrefreshes
= 2; /* number of times to refresh cred */
270 struct timeval timeout
;
272 if (cu
->cu_total
.tv_usec
== -1) {
273 timeout
= utimeout
; /* use supplied timeout */
275 timeout
= cu
->cu_total
; /* use default timeout */
278 time_waited
.tv_sec
= 0;
279 time_waited
.tv_usec
= 0;
281 xdrs
= &(cu
->cu_outxdrs
);
282 xdrs
->x_op
= XDR_ENCODE
;
283 XDR_SETPOS(xdrs
, cu
->cu_xdrpos
);
285 * the transaction is the first thing in the out buffer
287 (*(u_short
*)(cu
->cu_outbuf
))++;
288 if ((! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
289 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
290 (! (*xargs
)(xdrs
, argsp
)))
291 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
292 outlen
= (int)XDR_GETPOS(xdrs
);
295 if (sendto(cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
296 (struct sockaddr
*)&(cu
->cu_raddr
), cu
->cu_rlen
)
298 cu
->cu_error
.re_errno
= errno
;
299 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
303 * Hack to provide rpc-based message passing
305 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
306 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
309 * sub-optimal code appears here because we have
310 * some clock time to spare while the packets are in flight.
311 * (We assume that this is actually only executed once.)
313 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
314 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
315 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
317 FD_SET(cu
->cu_sock
, &mask
);
320 switch (select(cu
->cu_sock
+1, &readfds
, NULL
,
321 NULL
, &(cu
->cu_wait
))) {
324 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
325 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
326 while (time_waited
.tv_usec
>= 1000000) {
327 time_waited
.tv_sec
++;
328 time_waited
.tv_usec
-= 1000000;
330 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
331 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
332 (time_waited
.tv_usec
< timeout
.tv_usec
)))
334 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
337 * buggy in other cases because time_waited is not being
343 cu
->cu_error
.re_errno
= errno
;
344 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
347 fromlen
= sizeof(struct sockaddr
);
348 inlen
= recvfrom(cu
->cu_sock
, cu
->cu_inbuf
,
349 (int) cu
->cu_recvsz
, 0,
350 (struct sockaddr
*)&from
, &fromlen
);
351 } while (inlen
< 0 && errno
== EINTR
);
353 if (errno
== EWOULDBLOCK
)
355 cu
->cu_error
.re_errno
= errno
;
356 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
358 if (inlen
< sizeof(u_long
))
360 /* see if reply transaction id matches sent id */
361 if (*((u_long
*)(cu
->cu_inbuf
)) != *((u_long
*)(cu
->cu_outbuf
)))
363 /* we now assume we have the proper reply */
368 * now decode and validate the response
370 xdrmem_create(&reply_xdrs
, cu
->cu_inbuf
, (u_int
)inlen
, XDR_DECODE
);
371 ok
= xdr_replymsg(&reply_xdrs
, &reply_msg
);
372 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
374 _seterr_reply(&reply_msg
, &(cu
->cu_error
));
375 if (cu
->cu_error
.re_status
== RPC_SUCCESS
) {
376 if (! AUTH_VALIDATE(cl
->cl_auth
,
377 &reply_msg
.acpted_rply
.ar_verf
)) {
378 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
379 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
381 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
382 xdrs
->x_op
= XDR_FREE
;
383 (void)xdr_opaque_auth(xdrs
,
384 &(reply_msg
.acpted_rply
.ar_verf
));
386 } /* end successful completion */
388 /* maybe our credentials need to be refreshed ... */
389 if (nrefreshes
> 0 && AUTH_REFRESH(cl
->cl_auth
)) {
393 } /* end of unsuccessful completion */
394 } /* end of valid reply message */
396 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
398 return (cu
->cu_error
.re_status
);
402 clntudp_geterr(cl
, errp
)
404 struct rpc_err
*errp
;
406 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
408 *errp
= cu
->cu_error
;
413 clntudp_freeres(cl
, xdr_res
, res_ptr
)
418 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
419 register XDR
*xdrs
= &(cu
->cu_outxdrs
);
421 xdrs
->x_op
= XDR_FREE
;
422 return ((*xdr_res
)(xdrs
, res_ptr
));
432 clntudp_control(cl
, request
, info
)
437 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
441 cu
->cu_total
= *(struct timeval
*)info
;
444 *(struct timeval
*)info
= cu
->cu_total
;
446 case CLSET_RETRY_TIMEOUT
:
447 cu
->cu_wait
= *(struct timeval
*)info
;
449 case CLGET_RETRY_TIMEOUT
:
450 *(struct timeval
*)info
= cu
->cu_wait
;
452 case CLGET_SERVER_ADDR
:
453 *(struct sockaddr_in
*)info
= cu
->cu_raddr
;
465 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
467 if (cu
->cu_closeit
) {
468 (void)close(cu
->cu_sock
);
470 XDR_DESTROY(&(cu
->cu_outxdrs
));
471 mem_free((caddr_t
)cu
, (sizeof(*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
472 mem_free((caddr_t
)cl
, sizeof(CLIENT
));