]>
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.2 1999/10/14 21:56:53 wsanchez Exp $";
60 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
62 * Copyright (C) 1984, Sun Microsystems, Inc.
67 #include <sys/socket.h>
68 #include <sys/ioctl.h>
71 #include <rpc/pmap_clnt.h>
76 * UDP bases client side rpc operations
78 static enum clnt_stat
clntudp_call();
79 static void clntudp_abort();
80 static void clntudp_geterr();
81 static bool_t
clntudp_freeres();
82 static bool_t
clntudp_control();
83 static void clntudp_destroy();
85 static struct clnt_ops udp_ops
= {
95 * Private data kept per client handle
100 struct sockaddr_in cu_raddr
;
102 struct timeval cu_wait
;
103 struct timeval cu_total
;
104 struct rpc_err cu_error
;
114 * Create a UDP based client handle.
115 * If *sockp<0, *sockp is set to a newly created UPD socket.
116 * If raddr->sin_port is 0 a binder on the remote machine
117 * is consulted for the correct port number.
118 * NB: It is the clients responsibility to close *sockp.
119 * NB: The rpch->cl_auth is initialized to null authentication.
120 * Caller may wish to set this something more useful.
122 * wait is the amount of time used between retransmitting a call if
123 * no response has been heard; retransmition occurs until the actual
124 * rpc call times out.
126 * sendsz and recvsz are the maximum allowable packet sizes that can be
130 clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
131 struct sockaddr_in
*raddr
;
140 register struct cu_data
*cu
;
142 struct rpc_msg call_msg
;
144 cl
= (CLIENT
*)mem_alloc(sizeof(CLIENT
));
146 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
147 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
148 rpc_createerr
.cf_error
.re_errno
= errno
;
151 sendsz
= ((sendsz
+ 3) / 4) * 4;
152 recvsz
= ((recvsz
+ 3) / 4) * 4;
153 cu
= (struct cu_data
*)mem_alloc(sizeof(*cu
) + sendsz
+ recvsz
);
155 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
156 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
157 rpc_createerr
.cf_error
.re_errno
= errno
;
160 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
162 (void)gettimeofday(&now
, (struct timezone
*)0);
163 if (raddr
->sin_port
== 0) {
166 pmap_getport(raddr
, program
, version
, IPPROTO_UDP
)) == 0) {
169 raddr
->sin_port
= htons(port
);
171 cl
->cl_ops
= &udp_ops
;
172 cl
->cl_private
= (caddr_t
)cu
;
173 cu
->cu_raddr
= *raddr
;
174 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
176 cu
->cu_total
.tv_sec
= -1;
177 cu
->cu_total
.tv_usec
= -1;
178 cu
->cu_sendsz
= sendsz
;
179 cu
->cu_recvsz
= recvsz
;
180 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
181 call_msg
.rm_direction
= CALL
;
182 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
183 call_msg
.rm_call
.cb_prog
= program
;
184 call_msg
.rm_call
.cb_vers
= version
;
185 xdrmem_create(&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
187 if (! xdr_callhdr(&(cu
->cu_outxdrs
), &call_msg
)) {
190 cu
->cu_xdrpos
= XDR_GETPOS(&(cu
->cu_outxdrs
));
194 *sockp
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
196 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
197 rpc_createerr
.cf_error
.re_errno
= errno
;
200 /* attempt to bind to prov port */
201 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
202 /* the sockets rpc controls are non-blocking */
203 (void)ioctl(*sockp
, FIONBIO
, (char *) &dontblock
);
204 cu
->cu_closeit
= TRUE
;
206 cu
->cu_closeit
= FALSE
;
208 cu
->cu_sock
= *sockp
;
209 cl
->cl_auth
= authnone_create();
213 mem_free((caddr_t
)cu
, sizeof(*cu
) + sendsz
+ recvsz
);
215 mem_free((caddr_t
)cl
, sizeof(CLIENT
));
216 return ((CLIENT
*)NULL
);
220 clntudp_create(raddr
, program
, version
, wait
, sockp
)
221 struct sockaddr_in
*raddr
;
228 return(clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
,
229 UDPMSGSIZE
, UDPMSGSIZE
));
232 static enum clnt_stat
233 clntudp_call(cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
234 register CLIENT
*cl
; /* client handle */
235 u_long proc
; /* procedure number */
236 xdrproc_t xargs
; /* xdr routine for args */
237 caddr_t argsp
; /* pointer to args */
238 xdrproc_t xresults
; /* xdr routine for results */
239 caddr_t resultsp
; /* pointer to results */
240 struct timeval utimeout
; /* seconds to wait before giving up */
242 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
249 struct sockaddr_in from
;
250 struct rpc_msg reply_msg
;
252 struct timeval time_waited
;
254 int nrefreshes
= 2; /* number of times to refresh cred */
255 struct timeval timeout
;
257 if (cu
->cu_total
.tv_usec
== -1) {
258 timeout
= utimeout
; /* use supplied timeout */
260 timeout
= cu
->cu_total
; /* use default timeout */
263 time_waited
.tv_sec
= 0;
264 time_waited
.tv_usec
= 0;
266 xdrs
= &(cu
->cu_outxdrs
);
267 xdrs
->x_op
= XDR_ENCODE
;
268 XDR_SETPOS(xdrs
, cu
->cu_xdrpos
);
270 * the transaction is the first thing in the out buffer
272 (*(u_short
*)(cu
->cu_outbuf
))++;
273 if ((! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
274 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
275 (! (*xargs
)(xdrs
, argsp
)))
276 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
277 outlen
= (int)XDR_GETPOS(xdrs
);
280 if (sendto(cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
281 (struct sockaddr
*)&(cu
->cu_raddr
), cu
->cu_rlen
)
283 cu
->cu_error
.re_errno
= errno
;
284 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
288 * Hack to provide rpc-based message passing
290 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
291 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
294 * sub-optimal code appears here because we have
295 * some clock time to spare while the packets are in flight.
296 * (We assume that this is actually only executed once.)
298 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
299 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
300 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
302 FD_SET(cu
->cu_sock
, &mask
);
305 switch (select(cu
->cu_sock
+1, &readfds
, (int *)NULL
,
306 (int *)NULL
, &(cu
->cu_wait
))) {
309 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
310 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
311 while (time_waited
.tv_usec
>= 1000000) {
312 time_waited
.tv_sec
++;
313 time_waited
.tv_usec
-= 1000000;
315 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
316 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
317 (time_waited
.tv_usec
< timeout
.tv_usec
)))
319 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
322 * buggy in other cases because time_waited is not being
328 cu
->cu_error
.re_errno
= errno
;
329 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
332 fromlen
= sizeof(struct sockaddr
);
333 inlen
= recvfrom(cu
->cu_sock
, cu
->cu_inbuf
,
334 (int) cu
->cu_recvsz
, 0,
335 (struct sockaddr
*)&from
, &fromlen
);
336 } while (inlen
< 0 && errno
== EINTR
);
338 if (errno
== EWOULDBLOCK
)
340 cu
->cu_error
.re_errno
= errno
;
341 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
343 if (inlen
< sizeof(u_long
))
345 /* see if reply transaction id matches sent id */
346 if (*((u_long
*)(cu
->cu_inbuf
)) != *((u_long
*)(cu
->cu_outbuf
)))
348 /* we now assume we have the proper reply */
353 * now decode and validate the response
355 xdrmem_create(&reply_xdrs
, cu
->cu_inbuf
, (u_int
)inlen
, XDR_DECODE
);
356 ok
= xdr_replymsg(&reply_xdrs
, &reply_msg
);
357 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
359 _seterr_reply(&reply_msg
, &(cu
->cu_error
));
360 if (cu
->cu_error
.re_status
== RPC_SUCCESS
) {
361 if (! AUTH_VALIDATE(cl
->cl_auth
,
362 &reply_msg
.acpted_rply
.ar_verf
)) {
363 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
364 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
366 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
367 xdrs
->x_op
= XDR_FREE
;
368 (void)xdr_opaque_auth(xdrs
,
369 &(reply_msg
.acpted_rply
.ar_verf
));
371 } /* end successful completion */
373 /* maybe our credentials need to be refreshed ... */
374 if (nrefreshes
> 0 && AUTH_REFRESH(cl
->cl_auth
)) {
378 } /* end of unsuccessful completion */
379 } /* end of valid reply message */
381 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
383 return (cu
->cu_error
.re_status
);
387 clntudp_geterr(cl
, errp
)
389 struct rpc_err
*errp
;
391 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
393 *errp
= cu
->cu_error
;
398 clntudp_freeres(cl
, xdr_res
, res_ptr
)
403 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
404 register XDR
*xdrs
= &(cu
->cu_outxdrs
);
406 xdrs
->x_op
= XDR_FREE
;
407 return ((*xdr_res
)(xdrs
, res_ptr
));
417 clntudp_control(cl
, request
, info
)
422 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
426 cu
->cu_total
= *(struct timeval
*)info
;
429 *(struct timeval
*)info
= cu
->cu_total
;
431 case CLSET_RETRY_TIMEOUT
:
432 cu
->cu_wait
= *(struct timeval
*)info
;
434 case CLGET_RETRY_TIMEOUT
:
435 *(struct timeval
*)info
= cu
->cu_wait
;
437 case CLGET_SERVER_ADDR
:
438 *(struct sockaddr_in
*)info
= cu
->cu_raddr
;
450 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
452 if (cu
->cu_closeit
) {
453 (void)close(cu
->cu_sock
);
455 XDR_DESTROY(&(cu
->cu_outxdrs
));
456 mem_free((caddr_t
)cu
, (sizeof(*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
457 mem_free((caddr_t
)cl
, sizeof(CLIENT
));