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>
51 * UDP bases client side rpc operations
53 static enum clnt_stat
clntudp_call();
54 static void clntudp_abort();
55 static void clntudp_geterr();
56 static bool_t
clntudp_freeres();
57 static bool_t
clntudp_control();
58 static void clntudp_destroy();
60 static struct clnt_ops udp_ops
= {
70 * Private data kept per client handle
75 struct sockaddr_in cu_raddr
;
77 struct timeval cu_wait
;
78 struct timeval cu_total
;
79 struct rpc_err cu_error
;
89 * Create a UDP based client handle.
90 * If *sockp<0, *sockp is set to a newly created UPD socket.
91 * If raddr->sin_port is 0 a binder on the remote machine
92 * is consulted for the correct port number.
93 * NB: It is the clients responsibility to close *sockp.
94 * NB: The rpch->cl_auth is initialized to null authentication.
95 * Caller may wish to set this something more useful.
97 * wait is the amount of time used between retransmitting a call if
98 * no response has been heard; retransmition occurs until the actual
101 * sendsz and recvsz are the maximum allowable packet sizes that can be
105 clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
106 struct sockaddr_in
*raddr
;
115 register struct cu_data
*cu
;
117 struct rpc_msg call_msg
;
119 cl
= (CLIENT
*)mem_alloc(sizeof(CLIENT
));
121 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
122 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
123 rpc_createerr
.cf_error
.re_errno
= errno
;
126 sendsz
= ((sendsz
+ 3) / 4) * 4;
127 recvsz
= ((recvsz
+ 3) / 4) * 4;
128 cu
= (struct cu_data
*)mem_alloc(sizeof(*cu
) + sendsz
+ recvsz
);
130 (void) fprintf(stderr
, "clntudp_create: out of memory\n");
131 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
132 rpc_createerr
.cf_error
.re_errno
= errno
;
135 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
137 (void)gettimeofday(&now
, (struct timezone
*)0);
138 if (raddr
->sin_port
== 0) {
141 pmap_getport(raddr
, program
, version
, IPPROTO_UDP
)) == 0) {
144 raddr
->sin_port
= htons(port
);
146 cl
->cl_ops
= &udp_ops
;
147 cl
->cl_private
= (caddr_t
)cu
;
148 cu
->cu_raddr
= *raddr
;
149 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
151 cu
->cu_total
.tv_sec
= -1;
152 cu
->cu_total
.tv_usec
= -1;
153 cu
->cu_sendsz
= sendsz
;
154 cu
->cu_recvsz
= recvsz
;
155 call_msg
.rm_xid
= getpid() ^ now
.tv_sec
^ now
.tv_usec
;
156 call_msg
.rm_direction
= CALL
;
157 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
158 call_msg
.rm_call
.cb_prog
= program
;
159 call_msg
.rm_call
.cb_vers
= version
;
160 xdrmem_create(&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
162 if (! xdr_callhdr(&(cu
->cu_outxdrs
), &call_msg
)) {
165 cu
->cu_xdrpos
= XDR_GETPOS(&(cu
->cu_outxdrs
));
169 *sockp
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
171 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
172 rpc_createerr
.cf_error
.re_errno
= errno
;
175 /* attempt to bind to prov port */
176 (void)bindresvport(*sockp
, (struct sockaddr_in
*)0);
177 /* the sockets rpc controls are non-blocking */
178 (void)ioctl(*sockp
, FIONBIO
, (char *) &dontblock
);
179 cu
->cu_closeit
= TRUE
;
181 cu
->cu_closeit
= FALSE
;
183 cu
->cu_sock
= *sockp
;
184 cl
->cl_auth
= authnone_create();
188 mem_free((caddr_t
)cu
, sizeof(*cu
) + sendsz
+ recvsz
);
190 mem_free((caddr_t
)cl
, sizeof(CLIENT
));
191 return ((CLIENT
*)NULL
);
195 clntudp_create(raddr
, program
, version
, wait
, sockp
)
196 struct sockaddr_in
*raddr
;
203 return(clntudp_bufcreate(raddr
, program
, version
, wait
, sockp
,
204 UDPMSGSIZE
, UDPMSGSIZE
));
207 static enum clnt_stat
208 clntudp_call(cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
209 register CLIENT
*cl
; /* client handle */
210 u_long proc
; /* procedure number */
211 xdrproc_t xargs
; /* xdr routine for args */
212 caddr_t argsp
; /* pointer to args */
213 xdrproc_t xresults
; /* xdr routine for results */
214 caddr_t resultsp
; /* pointer to results */
215 struct timeval utimeout
; /* seconds to wait before giving up */
217 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
228 #endif /* def FD_SETSIZE */
229 struct sockaddr_in from
;
230 struct rpc_msg reply_msg
;
232 struct timeval time_waited
;
234 int nrefreshes
= 2; /* number of times to refresh cred */
235 struct timeval timeout
;
237 if (cu
->cu_total
.tv_usec
== -1) {
238 timeout
= utimeout
; /* use supplied timeout */
240 timeout
= cu
->cu_total
; /* use default timeout */
243 time_waited
.tv_sec
= 0;
244 time_waited
.tv_usec
= 0;
246 xdrs
= &(cu
->cu_outxdrs
);
247 xdrs
->x_op
= XDR_ENCODE
;
248 XDR_SETPOS(xdrs
, cu
->cu_xdrpos
);
250 * the transaction is the first thing in the out buffer
252 (*(u_short
*)(cu
->cu_outbuf
))++;
253 if ((! XDR_PUTLONG(xdrs
, (long *)&proc
)) ||
254 (! AUTH_MARSHALL(cl
->cl_auth
, xdrs
)) ||
255 (! (*xargs
)(xdrs
, argsp
)))
256 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
257 outlen
= (int)XDR_GETPOS(xdrs
);
260 if (sendto(cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
261 (struct sockaddr
*)&(cu
->cu_raddr
), cu
->cu_rlen
)
263 cu
->cu_error
.re_errno
= errno
;
264 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
268 * Hack to provide rpc-based message passing
270 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0) {
271 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
274 * sub-optimal code appears here because we have
275 * some clock time to spare while the packets are in flight.
276 * (We assume that this is actually only executed once.)
278 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
279 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
280 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
283 FD_SET(cu
->cu_sock
, &mask
);
285 mask
= 1 << cu
->cu_sock
;
286 #endif /* def FD_SETSIZE */
289 switch (select(_rpc_dtablesize(), &readfds
, (int *)NULL
,
290 (int *)NULL
, &(cu
->cu_wait
))) {
293 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
294 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
295 while (time_waited
.tv_usec
>= 1000000) {
296 time_waited
.tv_sec
++;
297 time_waited
.tv_usec
-= 1000000;
299 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
300 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
301 (time_waited
.tv_usec
< timeout
.tv_usec
)))
303 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
306 * buggy in other cases because time_waited is not being
312 cu
->cu_error
.re_errno
= errno
;
313 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
316 fromlen
= sizeof(struct sockaddr
);
317 inlen
= recvfrom(cu
->cu_sock
, cu
->cu_inbuf
,
318 (int) cu
->cu_recvsz
, 0,
319 (struct sockaddr
*)&from
, &fromlen
);
320 } while (inlen
< 0 && errno
== EINTR
);
322 if (errno
== EWOULDBLOCK
)
324 cu
->cu_error
.re_errno
= errno
;
325 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
327 if (inlen
< sizeof(u_long
))
329 /* see if reply transaction id matches sent id */
330 if (*((u_long
*)(cu
->cu_inbuf
)) != *((u_long
*)(cu
->cu_outbuf
)))
332 /* we now assume we have the proper reply */
337 * now decode and validate the response
339 xdrmem_create(&reply_xdrs
, cu
->cu_inbuf
, (u_int
)inlen
, XDR_DECODE
);
340 ok
= xdr_replymsg(&reply_xdrs
, &reply_msg
);
341 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
343 _seterr_reply(&reply_msg
, &(cu
->cu_error
));
344 if (cu
->cu_error
.re_status
== RPC_SUCCESS
) {
345 if (! AUTH_VALIDATE(cl
->cl_auth
,
346 &reply_msg
.acpted_rply
.ar_verf
)) {
347 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
348 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
350 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
) {
351 xdrs
->x_op
= XDR_FREE
;
352 (void)xdr_opaque_auth(xdrs
,
353 &(reply_msg
.acpted_rply
.ar_verf
));
355 } /* end successful completion */
357 /* maybe our credentials need to be refreshed ... */
358 if (nrefreshes
> 0 && AUTH_REFRESH(cl
->cl_auth
)) {
362 } /* end of unsuccessful completion */
363 } /* end of valid reply message */
365 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
367 return (cu
->cu_error
.re_status
);
371 clntudp_geterr(cl
, errp
)
373 struct rpc_err
*errp
;
375 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
377 *errp
= cu
->cu_error
;
382 clntudp_freeres(cl
, xdr_res
, res_ptr
)
387 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
388 register XDR
*xdrs
= &(cu
->cu_outxdrs
);
390 xdrs
->x_op
= XDR_FREE
;
391 return ((*xdr_res
)(xdrs
, res_ptr
));
401 clntudp_control(cl
, request
, info
)
406 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
410 cu
->cu_total
= *(struct timeval
*)info
;
413 *(struct timeval
*)info
= cu
->cu_total
;
415 case CLSET_RETRY_TIMEOUT
:
416 cu
->cu_wait
= *(struct timeval
*)info
;
418 case CLGET_RETRY_TIMEOUT
:
419 *(struct timeval
*)info
= cu
->cu_wait
;
421 case CLGET_SERVER_ADDR
:
422 *(struct sockaddr_in
*)info
= cu
->cu_raddr
;
434 register struct cu_data
*cu
= (struct cu_data
*)cl
->cl_private
;
436 if (cu
->cu_closeit
) {
437 (void)close(cu
->cu_sock
);
439 XDR_DESTROY(&(cu
->cu_outxdrs
));
440 mem_free((caddr_t
)cu
, (sizeof(*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
441 mem_free((caddr_t
)cl
, sizeof(CLIENT
));