2 * Copyright (c) 1999-2007 Apple 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();
80 __private_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
);
85 * UDP bases client side rpc operations
87 static enum clnt_stat
clntudp_call();
88 static void clntudp_abort();
89 static void clntudp_geterr();
90 static bool_t
clntudp_freeres();
91 static bool_t
clntudp_control();
92 static void clntudp_destroy();
94 static struct clnt_ops udp_ops
= {
104 * Private data kept per client handle
109 struct sockaddr_in cu_raddr
;
111 struct timeval cu_retry_timeout
;
112 struct timeval cu_total_timeout
;
113 struct rpc_err cu_error
;
123 * Create a UDP based client handle.
124 * If *sockp<0, *sockp is set to a newly created UPD socket.
125 * If raddr->sin_port is 0 a binder on the remote machine
126 * is consulted for the correct port number.
127 * NB: It is the clients responsibility to close *sockp.
128 * NB: The rpch->cl_auth is initialized to null authentication.
129 * Caller may wish to set this something more useful.
131 * wait is the amount of time used between retransmitting a call if
132 * no response has been heard; retransmition occurs until the actual
133 * rpc call times out.
135 * sendsz and recvsz are the maximum allowable packet sizes that can be
138 __private_extern__ CLIENT
*
139 clntudp_bufcreate_timeout(struct sockaddr_in
*raddr
, uint32_t program
, uint32_t version
, int *sockp
, uint32_t sendsz
, uint32_t recvsz
, struct timeval
*retry_timeout
, struct timeval
*total_timeout
)
142 struct cu_data
*cu
= NULL
;
144 struct rpc_msg call_msg
;
150 cl
= (CLIENT
*)mem_alloc(sizeof(CLIENT
));
153 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
154 rpc_createerr
.cf_error
.re_errno
= errno
;
158 sendsz
= ((sendsz
+ 3) / 4) * 4;
159 recvsz
= ((recvsz
+ 3) / 4) * 4;
160 cu
= (struct cu_data
*)mem_alloc(sizeof(*cu
) + sendsz
+ recvsz
);
163 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
164 rpc_createerr
.cf_error
.re_errno
= errno
;
168 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
170 if (raddr
->sin_port
== 0)
172 port
= pmap_getport_timeout(raddr
, program
, version
, IPPROTO_UDP
, retry_timeout
, total_timeout
);
173 if (port
== 0) goto fooy
;
175 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
);
182 cu
->cu_sendsz
= sendsz
;
183 cu
->cu_recvsz
= recvsz
;
185 cu
->cu_retry_timeout
.tv_sec
= 1;
186 cu
->cu_retry_timeout
.tv_usec
= 0;
187 if (retry_timeout
!= NULL
) cu
->cu_retry_timeout
= *retry_timeout
;
189 cu
->cu_total_timeout
.tv_sec
= -1;
190 cu
->cu_total_timeout
.tv_usec
= -1;
191 if (total_timeout
!= NULL
) cu
->cu_total_timeout
= *retry_timeout
;
193 rfd
= open("/dev/random", O_RDONLY
, 0);
194 if ((rfd
< 0) || (read(rfd
, &call_msg
.rm_xid
, sizeof(call_msg
.rm_xid
)) != sizeof(call_msg
.rm_xid
)))
196 gettimeofday(&now
, (struct timezone
*)0);
197 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
200 if (rfd
> 0) close(rfd
);
202 call_msg
.rm_direction
= CALL
;
203 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
204 call_msg
.rm_call
.cb_prog
= program
;
205 call_msg
.rm_call
.cb_vers
= version
;
206 xdrmem_create(&(cu
->cu_outxdrs
), cu
->cu_outbuf
, sendsz
, XDR_ENCODE
);
207 if (! xdr_callhdr(&(cu
->cu_outxdrs
), &call_msg
)) goto fooy
;
209 cu
->cu_xdrpos
= XDR_GETPOS(&(cu
->cu_outxdrs
));
214 *sockp
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
217 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
218 rpc_createerr
.cf_error
.re_errno
= errno
;
222 /* attempt to bind to prov port */
223 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
225 /* the socket's rpc controls are non-blocking */
226 (void)ioctl(*sockp
, FIONBIO
, (char *) &dontblock
);
227 cu
->cu_closeit
= TRUE
;
229 /* set receive size */
232 if (getsockopt(*sockp
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rsize
, &len
) != 0)
239 len
= sizeof(recvsz
);
240 if ((recvsz
> rsize
) && (setsockopt(*sockp
, SOL_SOCKET
, SO_RCVBUF
, (char *)&recvsz
, len
)))
249 cu
->cu_closeit
= FALSE
;
252 cu
->cu_sock
= *sockp
;
253 cl
->cl_auth
= authnone_create();
257 if (cu
) mem_free((caddr_t
)cu
, sizeof(*cu
) + sendsz
+ recvsz
);
258 if (cl
) mem_free((caddr_t
)cl
, sizeof(CLIENT
));
263 clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
265 struct sockaddr_in
*raddr
;
273 struct sockaddr_in
*raddr
;
282 return clntudp_bufcreate_timeout(raddr
, (uint32_t)program
, (uint32_t)version
, sockp
, (uint32_t)sendsz
, (uint32_t)recvsz
, &wait
, NULL
);
286 clntudp_create(raddr
, program
, version
, wait
, sockp
)
288 struct sockaddr_in
*raddr
;
294 struct sockaddr_in
*raddr
;
302 return clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
, UDPMSGSIZE
, UDPMSGSIZE
);
305 static enum clnt_stat
306 clntudp_call(cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
307 register CLIENT
*cl
; /* client handle */
308 u_long proc
; /* procedure number */
309 xdrproc_t xargs
; /* xdr routine for args */
310 caddr_t argsp
; /* pointer to args */
311 xdrproc_t xresults
; /* xdr routine for results */
312 caddr_t resultsp
; /* pointer to results */
313 struct timeval utimeout
; /* seconds to wait before giving up */
315 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
322 struct sockaddr_in from
;
323 struct rpc_msg reply_msg
;
325 struct timeval time_waited
;
327 int nrefreshes
= 2; /* number of times to refresh cred */
328 struct timeval timeout
;
330 if (cu
->cu_total_timeout
.tv_sec
== -1)
332 /* use supplied total timeout */
337 /* use client's total timeout */
338 timeout
= cu
->cu_total_timeout
;
341 time_waited
.tv_sec
= 0;
342 time_waited
.tv_usec
= 0;
345 xdrs
= &(cu
->cu_outxdrs
);
346 xdrs
->x_op
= XDR_ENCODE
;
347 XDR_SETPOS(xdrs
, cu
->cu_xdrpos
);
350 * the transaction is the first thing in the out buffer
352 (*(u_short
*)(cu
->cu_outbuf
))++;
354 if ((! XDR_PUTLONG(xdrs
, (int *)&proc
)) ||
355 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
356 (! (*xargs
)(xdrs
, argsp
)))
357 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
359 if ((! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
360 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
361 (! (*xargs
)(xdrs
, argsp
)))
362 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
364 outlen
= (int)XDR_GETPOS(xdrs
);
367 if (sendto(cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0, (struct sockaddr
*)&(cu
->cu_raddr
), cu
->cu_rlen
) != outlen
)
369 cu
->cu_error
.re_errno
= errno
;
370 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
374 * Hack to provide rpc-based message passing
376 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
378 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
382 * sub-optimal code appears here because we have
383 * some clock time to spare while the packets are in flight.
384 * (We assume that this is actually only executed once.)
386 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
387 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
388 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
390 FD_SET(cu
->cu_sock
, &mask
);
394 switch (select(cu
->cu_sock
+1, &readfds
, NULL
, NULL
, &(cu
->cu_retry_timeout
))) {
396 time_waited
.tv_sec
+= cu
->cu_retry_timeout
.tv_sec
;
397 time_waited
.tv_usec
+= cu
->cu_retry_timeout
.tv_usec
;
398 while (time_waited
.tv_usec
>= 1000000) {
399 time_waited
.tv_sec
++;
400 time_waited
.tv_usec
-= 1000000;
402 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
403 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
404 (time_waited
.tv_usec
< timeout
.tv_usec
)))
406 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
409 * buggy in other cases because time_waited is not being
415 cu
->cu_error
.re_errno
= errno
;
416 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
419 fromlen
= sizeof(struct sockaddr
);
420 inlen
= recvfrom(cu
->cu_sock
, cu
->cu_inbuf
,
421 (int) cu
->cu_recvsz
, 0,
422 (struct sockaddr
*)&from
, &fromlen
);
423 } while (inlen
< 0 && errno
== EINTR
);
425 if (errno
== EWOULDBLOCK
)
427 cu
->cu_error
.re_errno
= errno
;
428 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
430 if (inlen
< sizeof(u_long
))
432 /* see if reply transaction id matches sent id */
433 if (*((u_long
*)(cu
->cu_inbuf
)) != *((u_long
*)(cu
->cu_outbuf
)))
435 /* we now assume we have the proper reply */
440 * now decode and validate the response
442 xdrmem_create(&reply_xdrs
, cu
->cu_inbuf
, (u_int
)inlen
, XDR_DECODE
);
443 ok
= xdr_replymsg(&reply_xdrs
, &reply_msg
);
444 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
446 _seterr_reply(&reply_msg
, &(cu
->cu_error
));
447 if (cu
->cu_error
.re_status
== RPC_SUCCESS
) {
448 if (! AUTH_VALIDATE(cl
->cl_auth
,
449 &reply_msg
.acpted_rply
.ar_verf
)) {
450 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
451 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
453 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
454 xdrs
->x_op
= XDR_FREE
;
455 (void)xdr_opaque_auth(xdrs
,
456 &(reply_msg
.acpted_rply
.ar_verf
));
458 } /* end successful completion */
460 /* maybe our credentials need to be refreshed ... */
461 if (nrefreshes
> 0 && AUTH_REFRESH(cl
->cl_auth
)) {
465 } /* end of unsuccessful completion */
466 } /* end of valid reply message */
468 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
470 return (cu
->cu_error
.re_status
);
474 clntudp_geterr(cl
, errp
)
476 struct rpc_err
*errp
;
478 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
480 *errp
= cu
->cu_error
;
485 clntudp_freeres(cl
, xdr_res
, res_ptr
)
490 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
491 register XDR
*xdrs
= &(cu
->cu_outxdrs
);
493 xdrs
->x_op
= XDR_FREE
;
494 return ((*xdr_res
)(xdrs
, res_ptr
));
504 clntudp_control(cl
, request
, info
)
509 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
513 cu
->cu_total_timeout
= *(struct timeval
*)info
;
516 *(struct timeval
*)info
= cu
->cu_total_timeout
;
518 case CLSET_RETRY_TIMEOUT
:
519 cu
->cu_retry_timeout
= *(struct timeval
*)info
;
521 case CLGET_RETRY_TIMEOUT
:
522 *(struct timeval
*)info
= cu
->cu_retry_timeout
;
524 case CLGET_SERVER_ADDR
:
525 *(struct sockaddr_in
*)info
= cu
->cu_raddr
;
537 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
539 if (cu
->cu_closeit
) {
540 (void)close(cu
->cu_sock
);
542 XDR_DESTROY(&(cu
->cu_outxdrs
));
543 mem_free((caddr_t
)cu
, (sizeof(*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
544 mem_free((caddr_t
)cl
, sizeof(CLIENT
));