1 /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid
[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
35 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
49 #include <rpc/pmap_clnt.h>
51 extern bool_t
xdr_opaque_auth (XDR
*, struct opaque_auth
*);
54 * UDP bases client side rpc operations
56 static enum clnt_stat
clntudp_call (CLIENT
*, u_long
, xdrproc_t
, caddr_t
,
57 xdrproc_t
, caddr_t
, struct timeval
);
58 static void clntudp_abort (void);
59 static void clntudp_geterr (CLIENT
*, struct rpc_err
*);
60 static bool_t
clntudp_freeres (CLIENT
*, xdrproc_t
, caddr_t
);
61 static bool_t
clntudp_control (CLIENT
*, int, char *);
62 static void clntudp_destroy (CLIENT
*);
64 static struct clnt_ops udp_ops
=
75 * Private data kept per client handle
81 struct sockaddr_in cu_raddr
;
83 struct timeval cu_wait
;
84 struct timeval cu_total
;
85 struct rpc_err cu_error
;
95 * Create a UDP based client handle.
96 * If *sockp<0, *sockp is set to a newly created UPD socket.
97 * If raddr->sin_port is 0 a binder on the remote machine
98 * is consulted for the correct port number.
99 * NB: It is the clients responsibility to close *sockp.
100 * NB: The rpch->cl_auth is initialized to null authentication.
101 * Caller may wish to set this something more useful.
103 * wait is the amount of time used between retransmitting a call if
104 * no response has been heard; retransmission occurs until the actual
105 * rpc call times out.
107 * sendsz and recvsz are the maximum allowable packet sizes that can be
111 clntudp_bufcreate (raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
112 struct sockaddr_in
*raddr
;
121 struct cu_data
*cu
= NULL
;
123 struct rpc_msg call_msg
;
125 cl
= (CLIENT
*) mem_alloc (sizeof (CLIENT
));
128 (void) fprintf (stderr
, _("clntudp_create: out of memory\n"));
129 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
130 rpc_createerr
.cf_error
.re_errno
= errno
;
133 sendsz
= ((sendsz
+ 3) / 4) * 4;
134 recvsz
= ((recvsz
+ 3) / 4) * 4;
135 cu
= (struct cu_data
*) mem_alloc (sizeof (*cu
) + sendsz
+ recvsz
);
138 (void) fprintf (stderr
, _("clntudp_create: out of memory\n"));
139 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
140 rpc_createerr
.cf_error
.re_errno
= errno
;
143 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
145 (void) gettimeofday (&now
, (struct timezone
*) 0);
146 if (raddr
->sin_port
== 0)
150 pmap_getport (raddr
, program
, version
, IPPROTO_UDP
)) == 0)
154 raddr
->sin_port
= htons (port
);
156 cl
->cl_ops
= &udp_ops
;
157 cl
->cl_private
= (caddr_t
) cu
;
158 cu
->cu_raddr
= *raddr
;
159 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
161 cu
->cu_total
.tv_sec
= -1;
162 cu
->cu_total
.tv_usec
= -1;
163 cu
->cu_sendsz
= sendsz
;
164 cu
->cu_recvsz
= recvsz
;
165 call_msg
.rm_xid
= getpid () ^ now
.tv_sec
^ now
.tv_usec
;
166 call_msg
.rm_direction
= CALL
;
167 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
168 call_msg
.rm_call
.cb_prog
= program
;
169 call_msg
.rm_call
.cb_vers
= version
;
170 xdrmem_create (&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
172 if (!xdr_callhdr (&(cu
->cu_outxdrs
), &call_msg
))
176 cu
->cu_xdrpos
= XDR_GETPOS (&(cu
->cu_outxdrs
));
181 *sockp
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
184 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
185 rpc_createerr
.cf_error
.re_errno
= errno
;
188 /* attempt to bind to prov port */
189 (void) bindresvport (*sockp
, (struct sockaddr_in
*) 0);
190 /* the sockets rpc controls are non-blocking */
191 (void) ioctl (*sockp
, FIONBIO
, (char *) &dontblock
);
192 cu
->cu_closeit
= TRUE
;
196 cu
->cu_closeit
= FALSE
;
198 cu
->cu_sock
= *sockp
;
199 cl
->cl_auth
= authnone_create ();
203 mem_free ((caddr_t
) cu
, sizeof (*cu
) + sendsz
+ recvsz
);
205 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));
206 return (CLIENT
*) NULL
;
210 clntudp_create (raddr
, program
, version
, wait
, sockp
)
211 struct sockaddr_in
*raddr
;
218 return clntudp_bufcreate (raddr
, program
, version
, wait
, sockp
,
219 UDPMSGSIZE
, UDPMSGSIZE
);
222 static enum clnt_stat
223 clntudp_call (cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
224 CLIENT
*cl
; /* client handle */
225 u_long proc
; /* procedure number */
226 xdrproc_t xargs
; /* xdr routine for args */
227 caddr_t argsp
; /* pointer to args */
228 xdrproc_t xresults
; /* xdr routine for results */
229 caddr_t resultsp
; /* pointer to results */
230 struct timeval utimeout
; /* seconds to wait before giving up */
232 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
243 #endif /* def FD_SETSIZE */
244 struct sockaddr_in from
;
245 struct rpc_msg reply_msg
;
247 struct timeval time_waited
;
249 int nrefreshes
= 2; /* number of times to refresh cred */
250 struct timeval timeout
;
252 if (cu
->cu_total
.tv_usec
== -1)
254 timeout
= utimeout
; /* use supplied timeout */
258 timeout
= cu
->cu_total
; /* use default timeout */
261 time_waited
.tv_sec
= 0;
262 time_waited
.tv_usec
= 0;
264 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
)
284 cu
->cu_error
.re_errno
= errno
;
285 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
289 * Hack to provide rpc-based message passing
291 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
293 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
297 * sub-optimal code appears here because we have
298 * some clock time to spare while the packets are in flight.
299 * (We assume that this is actually only executed once.)
301 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
302 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
303 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
306 FD_SET (cu
->cu_sock
, &mask
);
308 mask
= 1 << cu
->cu_sock
;
309 #endif /* def FD_SETSIZE */
312 struct timeval cu_wait
= cu
->cu_wait
;
314 switch (select (_rpc_dtablesize (), &readfds
, (fd_set
*) NULL
,
315 (fd_set
*) NULL
, &cu_wait
))
319 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
320 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
321 while (time_waited
.tv_usec
>= 1000000)
323 time_waited
.tv_sec
++;
324 time_waited
.tv_usec
-= 1000000;
326 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
327 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
328 (time_waited
.tv_usec
< timeout
.tv_usec
)))
330 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
333 * buggy in other cases because time_waited is not being
339 cu
->cu_error
.re_errno
= errno
;
340 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
344 fromlen
= sizeof (struct sockaddr
);
345 inlen
= recvfrom (cu
->cu_sock
, cu
->cu_inbuf
,
346 (int) cu
->cu_recvsz
, 0,
347 (struct sockaddr
*) &from
, &fromlen
);
349 while (inlen
< 0 && errno
== EINTR
);
352 if (errno
== EWOULDBLOCK
)
354 cu
->cu_error
.re_errno
= errno
;
355 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
360 /* see if reply transaction id matches sent id.
361 Don't do this if we only wait for a replay */
363 && (*((u_int32_t
*) (cu
->cu_inbuf
))
364 != *((u_int32_t
*) (cu
->cu_outbuf
))))
366 /* we now assume we have the proper reply */
371 * now decode and validate the response
373 xdrmem_create (&reply_xdrs
, cu
->cu_inbuf
, (u_int
) inlen
, XDR_DECODE
);
374 ok
= xdr_replymsg (&reply_xdrs
, &reply_msg
);
375 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
378 _seterr_reply (&reply_msg
, &(cu
->cu_error
));
379 if (cu
->cu_error
.re_status
== RPC_SUCCESS
)
381 if (!AUTH_VALIDATE (cl
->cl_auth
,
382 &reply_msg
.acpted_rply
.ar_verf
))
384 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
385 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
387 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
389 xdrs
->x_op
= XDR_FREE
;
390 (void) xdr_opaque_auth (xdrs
,
391 &(reply_msg
.acpted_rply
.ar_verf
));
393 } /* end successful completion */
396 /* maybe our credentials need to be refreshed ... */
397 if (nrefreshes
> 0 && AUTH_REFRESH (cl
->cl_auth
))
402 } /* end of unsuccessful completion */
403 } /* end of valid reply message */
406 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
408 return cu
->cu_error
.re_status
;
412 clntudp_geterr (CLIENT
*cl
, struct rpc_err
*errp
)
414 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
416 *errp
= cu
->cu_error
;
421 clntudp_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
423 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
424 XDR
*xdrs
= &(cu
->cu_outxdrs
);
426 xdrs
->x_op
= XDR_FREE
;
427 return (*xdr_res
) (xdrs
, res_ptr
);
436 clntudp_control (CLIENT
*cl
, int request
, char *info
)
438 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
443 cu
->cu_total
= *(struct timeval
*) info
;
446 *(struct timeval
*) info
= cu
->cu_total
;
448 case CLSET_RETRY_TIMEOUT
:
449 cu
->cu_wait
= *(struct timeval
*) info
;
451 case CLGET_RETRY_TIMEOUT
:
452 *(struct timeval
*) info
= cu
->cu_wait
;
454 case CLGET_SERVER_ADDR
:
455 *(struct sockaddr_in
*) info
= cu
->cu_raddr
;
464 clntudp_destroy (CLIENT
*cl
)
466 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
470 (void) close (cu
->cu_sock
);
472 XDR_DESTROY (&(cu
->cu_outxdrs
));
473 mem_free ((caddr_t
) cu
, (sizeof (*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
474 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));