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.
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
46 #include <rpc/pmap_clnt.h>
53 * UDP bases client side rpc operations
55 static enum clnt_stat
clntudp_call();
56 static void clntudp_abort();
57 static void clntudp_geterr();
58 static bool_t
clntudp_freeres();
59 static bool_t
clntudp_control();
60 static void clntudp_destroy();
62 static struct clnt_ops udp_ops
= {
72 * Private data kept per client handle
77 struct sockaddr_in cu_raddr
;
79 struct timeval cu_wait
;
80 struct timeval cu_total
;
81 struct rpc_err cu_error
;
91 * Create a UDP based client handle.
92 * If *sockp<0, *sockp is set to a newly created UPD socket.
93 * If raddr->sin_port is 0 a binder on the remote machine
94 * is consulted for the correct port number.
95 * NB: It is the clients responsibility to close *sockp.
96 * NB: The rpch->cl_auth is initialized to null authentication.
97 * Caller may wish to set this something more useful.
99 * wait is the amount of time used between retransmitting a call if
100 * no response has been heard; retransmission occurs until the actual
101 * rpc call times out.
103 * sendsz and recvsz are the maximum allowable packet sizes that can be
107 clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
108 struct sockaddr_in
*raddr
;
117 register struct cu_data
*cu
;
119 struct rpc_msg call_msg
;
121 cl
= (CLIENT
*)mem_alloc(sizeof(CLIENT
));
123 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
124 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
125 rpc_createerr
.cf_error
.re_errno
= errno
;
128 sendsz
= ((sendsz
+ 3) / 4) * 4;
129 recvsz
= ((recvsz
+ 3) / 4) * 4;
130 cu
= (struct cu_data
*)mem_alloc(sizeof(*cu
) + sendsz
+ recvsz
);
132 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
133 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
134 rpc_createerr
.cf_error
.re_errno
= errno
;
137 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
139 (void)gettimeofday(&now
, (struct timezone
*)0);
140 if (raddr
->sin_port
== 0) {
143 pmap_getport(raddr
, program
, version
, IPPROTO_UDP
)) == 0) {
146 raddr
->sin_port
= htons(port
);
148 cl
->cl_ops
= &udp_ops
;
149 cl
->cl_private
= (caddr_t
)cu
;
150 cu
->cu_raddr
= *raddr
;
151 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
153 cu
->cu_total
.tv_sec
= -1;
154 cu
->cu_total
.tv_usec
= -1;
155 cu
->cu_sendsz
= sendsz
;
156 cu
->cu_recvsz
= recvsz
;
157 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
158 call_msg
.rm_direction
= CALL
;
159 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
160 call_msg
.rm_call
.cb_prog
= program
;
161 call_msg
.rm_call
.cb_vers
= version
;
162 xdrmem_create(&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
164 if (! xdr_callhdr(&(cu
->cu_outxdrs
), &call_msg
)) {
167 cu
->cu_xdrpos
= XDR_GETPOS(&(cu
->cu_outxdrs
));
171 *sockp
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
173 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
174 rpc_createerr
.cf_error
.re_errno
= errno
;
177 /* attempt to bind to prov port */
178 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
179 /* the sockets rpc controls are non-blocking */
180 (void)ioctl(*sockp
, FIONBIO
, (char *) &dontblock
);
181 cu
->cu_closeit
= TRUE
;
183 cu
->cu_closeit
= FALSE
;
185 cu
->cu_sock
= *sockp
;
186 cl
->cl_auth
= authnone_create();
190 mem_free((caddr_t
)cu
, sizeof(*cu
) + sendsz
+ recvsz
);
192 mem_free((caddr_t
)cl
, sizeof(CLIENT
));
193 return ((CLIENT
*)NULL
);
197 clntudp_create(raddr
, program
, version
, wait
, sockp
)
198 struct sockaddr_in
*raddr
;
205 return(clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
,
206 UDPMSGSIZE
, UDPMSGSIZE
));
209 static enum clnt_stat
210 clntudp_call(cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
211 register CLIENT
*cl
; /* client handle */
212 u_long proc
; /* procedure number */
213 xdrproc_t xargs
; /* xdr routine for args */
214 caddr_t argsp
; /* pointer to args */
215 xdrproc_t xresults
; /* xdr routine for results */
216 caddr_t resultsp
; /* pointer to results */
217 struct timeval utimeout
; /* seconds to wait before giving up */
219 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
230 #endif /* def FD_SETSIZE */
231 struct sockaddr_in from
;
232 struct rpc_msg reply_msg
;
234 struct timeval time_waited
;
236 int nrefreshes
= 2; /* number of times to refresh cred */
237 struct timeval timeout
;
239 if (cu
->cu_total
.tv_usec
== -1) {
240 timeout
= utimeout
; /* use supplied timeout */
242 timeout
= cu
->cu_total
; /* use default timeout */
245 time_waited
.tv_sec
= 0;
246 time_waited
.tv_usec
= 0;
248 xdrs
= &(cu
->cu_outxdrs
);
249 xdrs
->x_op
= XDR_ENCODE
;
250 XDR_SETPOS(xdrs
, cu
->cu_xdrpos
);
252 * the transaction is the first thing in the out buffer
254 (*(u_short
*)(cu
->cu_outbuf
))++;
255 if ((! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
256 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
257 (! (*xargs
)(xdrs
, argsp
)))
258 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
259 outlen
= (int)XDR_GETPOS(xdrs
);
262 if (sendto(cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
263 (struct sockaddr
*)&(cu
->cu_raddr
), cu
->cu_rlen
)
265 cu
->cu_error
.re_errno
= errno
;
266 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
270 * Hack to provide rpc-based message passing
272 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
273 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
276 * sub-optimal code appears here because we have
277 * some clock time to spare while the packets are in flight.
278 * (We assume that this is actually only executed once.)
280 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
281 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
282 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
285 FD_SET(cu
->cu_sock
, &mask
);
287 mask
= 1 << cu
->cu_sock
;
288 #endif /* def FD_SETSIZE */
290 struct timeval cu_wait
= cu
->cu_wait
;
292 switch (select(_rpc_dtablesize(), &readfds
, (int *)NULL
,
293 (int *)NULL
, &cu_wait
)) {
296 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
297 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
298 while (time_waited
.tv_usec
>= 1000000) {
299 time_waited
.tv_sec
++;
300 time_waited
.tv_usec
-= 1000000;
302 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
303 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
304 (time_waited
.tv_usec
< timeout
.tv_usec
)))
306 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
309 * buggy in other cases because time_waited is not being
315 cu
->cu_error
.re_errno
= errno
;
316 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
319 fromlen
= sizeof(struct sockaddr
);
320 inlen
= recvfrom(cu
->cu_sock
, cu
->cu_inbuf
,
321 (int) cu
->cu_recvsz
, 0,
322 (struct sockaddr
*)&from
, &fromlen
);
323 } while (inlen
< 0 && errno
== EINTR
);
325 if (errno
== EWOULDBLOCK
)
327 cu
->cu_error
.re_errno
= errno
;
328 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
332 /* see if reply transaction id matches sent id */
333 if (*((u_int32_t
*)(cu
->cu_inbuf
)) != *((u_int32_t
*)(cu
->cu_outbuf
)))
335 /* we now assume we have the proper reply */
340 * now decode and validate the response
342 xdrmem_create(&reply_xdrs
, cu
->cu_inbuf
, (u_int
)inlen
, XDR_DECODE
);
343 ok
= xdr_replymsg(&reply_xdrs
, &reply_msg
);
344 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
346 _seterr_reply(&reply_msg
, &(cu
->cu_error
));
347 if (cu
->cu_error
.re_status
== RPC_SUCCESS
) {
348 if (! AUTH_VALIDATE(cl
->cl_auth
,
349 &reply_msg
.acpted_rply
.ar_verf
)) {
350 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
351 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
353 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
354 xdrs
->x_op
= XDR_FREE
;
355 (void)xdr_opaque_auth(xdrs
,
356 &(reply_msg
.acpted_rply
.ar_verf
));
358 } /* end successful completion */
360 /* maybe our credentials need to be refreshed ... */
361 if (nrefreshes
> 0 && AUTH_REFRESH(cl
->cl_auth
)) {
365 } /* end of unsuccessful completion */
366 } /* end of valid reply message */
368 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
370 return (cu
->cu_error
.re_status
);
374 clntudp_geterr(cl
, errp
)
376 struct rpc_err
*errp
;
378 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
380 *errp
= cu
->cu_error
;
385 clntudp_freeres(cl
, xdr_res
, res_ptr
)
390 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
391 register XDR
*xdrs
= &(cu
->cu_outxdrs
);
393 xdrs
->x_op
= XDR_FREE
;
394 return ((*xdr_res
)(xdrs
, res_ptr
));
404 clntudp_control(cl
, request
, info
)
409 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
413 cu
->cu_total
= *(struct timeval
*)info
;
416 *(struct timeval
*)info
= cu
->cu_total
;
418 case CLSET_RETRY_TIMEOUT
:
419 cu
->cu_wait
= *(struct timeval
*)info
;
421 case CLGET_RETRY_TIMEOUT
:
422 *(struct timeval
*)info
= cu
->cu_wait
;
424 case CLGET_SERVER_ADDR
:
425 *(struct sockaddr_in
*)info
= cu
->cu_raddr
;
437 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
439 if (cu
->cu_closeit
) {
440 (void)close(cu
->cu_sock
);
442 XDR_DESTROY(&(cu
->cu_outxdrs
));
443 mem_free((caddr_t
)cu
, (sizeof(*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
444 mem_free((caddr_t
)cl
, sizeof(CLIENT
));