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.
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
50 #include <rpc/pmap_clnt.h>
52 extern bool_t
xdr_opaque_auth (XDR
*, struct opaque_auth
*);
55 * UDP bases client side rpc operations
57 static enum clnt_stat
clntudp_call (CLIENT
*, u_long
, xdrproc_t
, caddr_t
,
58 xdrproc_t
, caddr_t
, struct timeval
);
59 static void clntudp_abort (void);
60 static void clntudp_geterr (CLIENT
*, struct rpc_err
*);
61 static bool_t
clntudp_freeres (CLIENT
*, xdrproc_t
, caddr_t
);
62 static bool_t
clntudp_control (CLIENT
*, int, char *);
63 static void clntudp_destroy (CLIENT
*);
65 static struct clnt_ops udp_ops
=
76 * Private data kept per client handle
82 struct sockaddr_in cu_raddr
;
84 struct timeval cu_wait
;
85 struct timeval cu_total
;
86 struct rpc_err cu_error
;
96 * Create a UDP based client handle.
97 * If *sockp<0, *sockp is set to a newly created UPD socket.
98 * If raddr->sin_port is 0 a binder on the remote machine
99 * is consulted for the correct port number.
100 * NB: It is the clients responsibility to close *sockp.
101 * NB: The rpch->cl_auth is initialized to null authentication.
102 * Caller may wish to set this something more useful.
104 * wait is the amount of time used between retransmitting a call if
105 * no response has been heard; retransmission occurs until the actual
106 * rpc call times out.
108 * sendsz and recvsz are the maximum allowable packet sizes that can be
112 clntudp_bufcreate (raddr
, program
, version
, wait
, sockp
, sendsz
, recvsz
)
113 struct sockaddr_in
*raddr
;
122 struct cu_data
*cu
= NULL
;
124 struct rpc_msg call_msg
;
126 cl
= (CLIENT
*) mem_alloc (sizeof (CLIENT
));
129 (void) fprintf (stderr
, _("clntudp_create: out of memory\n"));
130 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
131 rpc_createerr
.cf_error
.re_errno
= errno
;
134 sendsz
= ((sendsz
+ 3) / 4) * 4;
135 recvsz
= ((recvsz
+ 3) / 4) * 4;
136 cu
= (struct cu_data
*) mem_alloc (sizeof (*cu
) + sendsz
+ recvsz
);
139 (void) fprintf (stderr
, _("clntudp_create: out of memory\n"));
140 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
141 rpc_createerr
.cf_error
.re_errno
= errno
;
144 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
146 (void) __gettimeofday (&now
, (struct timezone
*) 0);
147 if (raddr
->sin_port
== 0)
151 pmap_getport (raddr
, program
, version
, IPPROTO_UDP
)) == 0)
155 raddr
->sin_port
= htons (port
);
157 cl
->cl_ops
= &udp_ops
;
158 cl
->cl_private
= (caddr_t
) cu
;
159 cu
->cu_raddr
= *raddr
;
160 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
162 cu
->cu_total
.tv_sec
= -1;
163 cu
->cu_total
.tv_usec
= -1;
164 cu
->cu_sendsz
= sendsz
;
165 cu
->cu_recvsz
= recvsz
;
166 call_msg
.rm_xid
= __getpid () ^ now
.tv_sec
^ now
.tv_usec
;
167 call_msg
.rm_direction
= CALL
;
168 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
169 call_msg
.rm_call
.cb_prog
= program
;
170 call_msg
.rm_call
.cb_vers
= version
;
171 xdrmem_create (&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
173 if (!xdr_callhdr (&(cu
->cu_outxdrs
), &call_msg
))
177 cu
->cu_xdrpos
= XDR_GETPOS (&(cu
->cu_outxdrs
));
182 *sockp
= __socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
185 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
186 rpc_createerr
.cf_error
.re_errno
= errno
;
189 /* attempt to bind to prov port */
190 (void) bindresvport (*sockp
, (struct sockaddr_in
*) 0);
191 /* the sockets rpc controls are non-blocking */
192 (void) __ioctl (*sockp
, FIONBIO
, (char *) &dontblock
);
193 cu
->cu_closeit
= TRUE
;
197 cu
->cu_closeit
= FALSE
;
199 cu
->cu_sock
= *sockp
;
200 cl
->cl_auth
= authnone_create ();
204 mem_free ((caddr_t
) cu
, sizeof (*cu
) + sendsz
+ recvsz
);
206 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));
207 return (CLIENT
*) NULL
;
211 clntudp_create (raddr
, program
, version
, wait
, sockp
)
212 struct sockaddr_in
*raddr
;
219 return clntudp_bufcreate (raddr
, program
, version
, wait
, sockp
,
220 UDPMSGSIZE
, UDPMSGSIZE
);
223 static enum clnt_stat
224 clntudp_call (cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
225 CLIENT
*cl
; /* client handle */
226 u_long proc
; /* procedure number */
227 xdrproc_t xargs
; /* xdr routine for args */
228 caddr_t argsp
; /* pointer to args */
229 xdrproc_t xresults
; /* xdr routine for results */
230 caddr_t resultsp
; /* pointer to results */
231 struct timeval utimeout
; /* seconds to wait before giving up */
233 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
239 int milliseconds
= (cu
->cu_wait
.tv_sec
* 1000) +
240 (cu
->cu_wait
.tv_usec
/ 1000);
241 struct sockaddr_in from
;
242 struct rpc_msg reply_msg
;
244 struct timeval time_waited
;
246 int nrefreshes
= 2; /* number of times to refresh cred */
247 struct timeval timeout
;
249 if (cu
->cu_total
.tv_usec
== -1)
251 timeout
= utimeout
; /* use supplied timeout */
255 timeout
= cu
->cu_total
; /* use default timeout */
258 time_waited
.tv_sec
= 0;
259 time_waited
.tv_usec
= 0;
261 xdrs
= &(cu
->cu_outxdrs
);
264 xdrs
->x_op
= XDR_ENCODE
;
265 XDR_SETPOS (xdrs
, cu
->cu_xdrpos
);
267 * the transaction is the first thing in the out buffer
269 (*(u_short
*) (cu
->cu_outbuf
))++;
270 if ((!XDR_PUTLONG (xdrs
, (long *) &proc
)) ||
271 (!AUTH_MARSHALL (cl
->cl_auth
, xdrs
)) ||
272 (!(*xargs
) (xdrs
, argsp
)))
273 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
274 outlen
= (int) XDR_GETPOS (xdrs
);
277 if (sendto (cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
278 (struct sockaddr
*) &(cu
->cu_raddr
), cu
->cu_rlen
)
281 cu
->cu_error
.re_errno
= errno
;
282 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
286 * Hack to provide rpc-based message passing
288 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
290 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
;
305 switch (__poll(&fd
, 1, milliseconds
))
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)
313 time_waited
.tv_sec
++;
314 time_waited
.tv_usec
-= 1000000;
316 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
317 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
318 (time_waited
.tv_usec
< timeout
.tv_usec
)))
320 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
323 * buggy in other cases because time_waited is not being
329 cu
->cu_error
.re_errno
= errno
;
330 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
334 fromlen
= sizeof (struct sockaddr
);
335 inlen
= recvfrom (cu
->cu_sock
, cu
->cu_inbuf
,
336 (int) cu
->cu_recvsz
, 0,
337 (struct sockaddr
*) &from
, &fromlen
);
339 while (inlen
< 0 && errno
== EINTR
);
342 if (errno
== EWOULDBLOCK
)
344 cu
->cu_error
.re_errno
= errno
;
345 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
350 /* see if reply transaction id matches sent id.
351 Don't do this if we only wait for a replay */
353 && (*((u_int32_t
*) (cu
->cu_inbuf
))
354 != *((u_int32_t
*) (cu
->cu_outbuf
))))
356 /* we now assume we have the proper reply */
361 * now decode and validate the response
363 xdrmem_create (&reply_xdrs
, cu
->cu_inbuf
, (u_int
) inlen
, XDR_DECODE
);
364 ok
= xdr_replymsg (&reply_xdrs
, &reply_msg
);
365 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
368 _seterr_reply (&reply_msg
, &(cu
->cu_error
));
369 if (cu
->cu_error
.re_status
== RPC_SUCCESS
)
371 if (!AUTH_VALIDATE (cl
->cl_auth
,
372 &reply_msg
.acpted_rply
.ar_verf
))
374 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
375 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
377 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
379 xdrs
->x_op
= XDR_FREE
;
380 (void) xdr_opaque_auth (xdrs
,
381 &(reply_msg
.acpted_rply
.ar_verf
));
383 } /* end successful completion */
386 /* maybe our credentials need to be refreshed ... */
387 if (nrefreshes
> 0 && AUTH_REFRESH (cl
->cl_auth
))
392 } /* end of unsuccessful completion */
393 } /* end of valid reply message */
396 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
398 return cu
->cu_error
.re_status
;
402 clntudp_geterr (CLIENT
*cl
, struct rpc_err
*errp
)
404 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
406 *errp
= cu
->cu_error
;
411 clntudp_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
413 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
414 XDR
*xdrs
= &(cu
->cu_outxdrs
);
416 xdrs
->x_op
= XDR_FREE
;
417 return (*xdr_res
) (xdrs
, res_ptr
);
426 clntudp_control (CLIENT
*cl
, int request
, char *info
)
428 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
433 cu
->cu_closeit
= TRUE
;
435 case CLSET_FD_NCLOSE
:
436 cu
->cu_closeit
= FALSE
;
439 cu
->cu_total
= *(struct timeval
*) info
;
442 *(struct timeval
*) info
= cu
->cu_total
;
444 case CLSET_RETRY_TIMEOUT
:
445 cu
->cu_wait
= *(struct timeval
*) info
;
447 case CLGET_RETRY_TIMEOUT
:
448 *(struct timeval
*) info
= cu
->cu_wait
;
450 case CLGET_SERVER_ADDR
:
451 *(struct sockaddr_in
*) info
= cu
->cu_raddr
;
454 *(int *)info
= cu
->cu_sock
;
458 * use the knowledge that xid is the
459 * first element in the call structure *.
460 * This will get the xid of the PREVIOUS call
462 *(u_long
*)info
= ntohl(*(u_long
*)cu
->cu_outbuf
);
465 /* This will set the xid of the NEXT call */
466 *(u_long
*)cu
->cu_outbuf
= htonl(*(u_long
*)info
- 1);
467 /* decrement by 1 as clntudp_call() increments once */
470 * This RELIES on the information that, in the call body,
471 * the version number field is the fifth field from the
472 * begining of the RPC header. MUST be changed if the
473 * call_struct is changed
475 *(u_long
*)info
= ntohl(*(u_long
*)(cu
->cu_outbuf
+
476 4 * BYTES_PER_XDR_UNIT
));
479 *(u_long
*)(cu
->cu_outbuf
+ 4 * BYTES_PER_XDR_UNIT
)
480 = htonl(*(u_long
*)info
);
484 * This RELIES on the information that, in the call body,
485 * the program number field is the field from the
486 * begining of the RPC header. MUST be changed if the
487 * call_struct is changed
489 *(u_long
*)info
= ntohl(*(u_long
*)(cu
->cu_outbuf
+
490 3 * BYTES_PER_XDR_UNIT
));
493 *(u_long
*)(cu
->cu_outbuf
+ 3 * BYTES_PER_XDR_UNIT
)
494 = htonl(*(u_long
*)info
);
496 /* The following are only possible with TI-RPC */
499 case CLSET_PUSH_TIMOD
:
500 case CLSET_POP_TIMOD
:
508 clntudp_destroy (CLIENT
*cl
)
510 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
514 (void) __close (cu
->cu_sock
);
516 XDR_DESTROY (&(cu
->cu_outxdrs
));
517 mem_free ((caddr_t
) cu
, (sizeof (*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
518 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));