]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/clnt_udp.c
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 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
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
= *total_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 */
309 uint32_t proc
; /* procedure number */
311 u_long proc
; /* procedure number */
313 xdrproc_t xargs
; /* xdr routine for args */
314 caddr_t argsp
; /* pointer to args */
315 xdrproc_t xresults
; /* xdr routine for results */
316 caddr_t resultsp
; /* pointer to results */
317 struct timeval utimeout
; /* seconds to wait before giving up */
319 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
326 struct sockaddr_in from
;
327 struct rpc_msg reply_msg
;
329 struct timeval time_waited
;
331 int nrefreshes
= 2; /* number of times to refresh cred */
332 struct timeval timeout
;
334 if (cu
->cu_total_timeout
.tv_sec
== -1)
336 /* use supplied total timeout */
341 /* use client's total timeout */
342 timeout
= cu
->cu_total_timeout
;
345 time_waited
.tv_sec
= 0;
346 time_waited
.tv_usec
= 0;
349 xdrs
= &(cu
->cu_outxdrs
);
350 xdrs
->x_op
= XDR_ENCODE
;
351 XDR_SETPOS(xdrs
, cu
->cu_xdrpos
);
354 * the transaction is the first thing in the out buffer
356 (*(u_short
*)(cu
->cu_outbuf
))++;
358 if ((! XDR_PUTLONG(xdrs
, (int *)&proc
)) ||
359 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
360 (! (*xargs
)(xdrs
, argsp
, 0)))
361 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
363 if ((! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
364 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
365 (! (*xargs
)(xdrs
, argsp
, 0)))
366 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
368 outlen
= (int)XDR_GETPOS(xdrs
);
371 if (sendto(cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0, (struct sockaddr
*)&(cu
->cu_raddr
), cu
->cu_rlen
) != outlen
)
373 cu
->cu_error
.re_errno
= errno
;
374 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
378 * Hack to provide rpc-based message passing
380 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
382 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
386 * sub-optimal code appears here because we have
387 * some clock time to spare while the packets are in flight.
388 * (We assume that this is actually only executed once.)
390 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
391 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
392 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
394 FD_SET(cu
->cu_sock
, &mask
);
398 switch (select(cu
->cu_sock
+1, &readfds
, NULL
, NULL
, &(cu
->cu_retry_timeout
))) {
400 time_waited
.tv_sec
+= cu
->cu_retry_timeout
.tv_sec
;
401 time_waited
.tv_usec
+= cu
->cu_retry_timeout
.tv_usec
;
402 while (time_waited
.tv_usec
>= 1000000) {
403 time_waited
.tv_sec
++;
404 time_waited
.tv_usec
-= 1000000;
406 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
407 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
408 (time_waited
.tv_usec
< timeout
.tv_usec
)))
410 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
413 * buggy in other cases because time_waited is not being
419 cu
->cu_error
.re_errno
= errno
;
420 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
423 fromlen
= sizeof(struct sockaddr
);
424 inlen
= recvfrom(cu
->cu_sock
, cu
->cu_inbuf
,
425 (int) cu
->cu_recvsz
, 0,
426 (struct sockaddr
*)&from
, &fromlen
);
427 } while (inlen
< 0 && errno
== EINTR
);
429 if (errno
== EWOULDBLOCK
)
431 cu
->cu_error
.re_errno
= errno
;
432 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
435 if (inlen
< sizeof(uint32_t))
437 /* see if reply transaction id matches sent id */
438 if (*((uint32_t *)(cu
->cu_inbuf
)) != *((uint32_t *)(cu
->cu_outbuf
))) continue;
440 if (inlen
< sizeof(u_long
))
442 /* see if reply transaction id matches sent id */
443 if (*((u_long
*)(cu
->cu_inbuf
)) != *((u_long
*)(cu
->cu_outbuf
))) continue;
445 /* we now assume we have the proper reply */
450 * now decode and validate the response
452 xdrmem_create(&reply_xdrs
, cu
->cu_inbuf
, (u_int
)inlen
, XDR_DECODE
);
453 ok
= xdr_replymsg(&reply_xdrs
, &reply_msg
);
454 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
456 _seterr_reply(&reply_msg
, &(cu
->cu_error
));
457 if (cu
->cu_error
.re_status
== RPC_SUCCESS
) {
458 if (! AUTH_VALIDATE(cl
->cl_auth
,
459 &reply_msg
.acpted_rply
.ar_verf
)) {
460 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
461 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
463 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
464 xdrs
->x_op
= XDR_FREE
;
465 (void)xdr_opaque_auth(xdrs
,
466 &(reply_msg
.acpted_rply
.ar_verf
));
468 } /* end successful completion */
470 /* maybe our credentials need to be refreshed ... */
471 if (nrefreshes
> 0 && AUTH_REFRESH(cl
->cl_auth
)) {
475 } /* end of unsuccessful completion */
476 } /* end of valid reply message */
478 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
480 return (cu
->cu_error
.re_status
);
484 clntudp_geterr(cl
, errp
)
486 struct rpc_err
*errp
;
488 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
490 *errp
= cu
->cu_error
;
495 clntudp_freeres(cl
, xdr_res
, res_ptr
)
500 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
501 register XDR
*xdrs
= &(cu
->cu_outxdrs
);
503 xdrs
->x_op
= XDR_FREE
;
504 return ((*xdr_res
)(xdrs
, res_ptr
, 0));
514 clntudp_control(cl
, request
, info
)
519 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
523 cu
->cu_total_timeout
= *(struct timeval
*)info
;
526 *(struct timeval
*)info
= cu
->cu_total_timeout
;
528 case CLSET_RETRY_TIMEOUT
:
529 cu
->cu_retry_timeout
= *(struct timeval
*)info
;
531 case CLGET_RETRY_TIMEOUT
:
532 *(struct timeval
*)info
= cu
->cu_retry_timeout
;
534 case CLGET_SERVER_ADDR
:
535 *(struct sockaddr_in
*)info
= cu
->cu_raddr
;
547 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
549 if (cu
->cu_closeit
) {
550 (void)close(cu
->cu_sock
);
552 XDR_DESTROY(&(cu
->cu_outxdrs
));
553 mem_free((caddr_t
)cu
, (sizeof(*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
554 mem_free((caddr_t
)cl
, sizeof(CLIENT
));