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
*);
53 extern u_long
_create_xid (void);
56 * UDP bases client side rpc operations
58 static enum clnt_stat
clntudp_call (CLIENT
*, u_long
, xdrproc_t
, caddr_t
,
59 xdrproc_t
, caddr_t
, struct timeval
);
60 static void clntudp_abort (void);
61 static void clntudp_geterr (CLIENT
*, struct rpc_err
*);
62 static bool_t
clntudp_freeres (CLIENT
*, xdrproc_t
, caddr_t
);
63 static bool_t
clntudp_control (CLIENT
*, int, char *);
64 static void clntudp_destroy (CLIENT
*);
66 static struct clnt_ops udp_ops
=
77 * Private data kept per client handle
83 struct sockaddr_in cu_raddr
;
85 struct timeval cu_wait
;
86 struct timeval cu_total
;
87 struct rpc_err cu_error
;
97 * Create a UDP based client handle.
98 * If *sockp<0, *sockp is set to a newly created UPD socket.
99 * If raddr->sin_port is 0 a binder on the remote machine
100 * is consulted for the correct port number.
101 * NB: It is the clients responsibility to close *sockp.
102 * NB: The rpch->cl_auth is initialized to null authentication.
103 * Caller may wish to set this something more useful.
105 * wait is the amount of time used between retransmitting a call if
106 * no response has been heard; retransmission occurs until the actual
107 * rpc call times out.
109 * sendsz and recvsz are the maximum allowable packet sizes that can be
113 clntudp_bufcreate (struct sockaddr_in
*raddr
, u_long program
, u_long version
,
114 struct timeval wait
, int *sockp
, u_int sendsz
,
118 struct cu_data
*cu
= NULL
;
119 struct rpc_msg call_msg
;
121 cl
= (CLIENT
*) mem_alloc (sizeof (CLIENT
));
124 (void) fprintf (stderr
, _("clntudp_create: out of memory\n"));
125 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
126 rpc_createerr
.cf_error
.re_errno
= errno
;
129 sendsz
= ((sendsz
+ 3) / 4) * 4;
130 recvsz
= ((recvsz
+ 3) / 4) * 4;
131 cu
= (struct cu_data
*) mem_alloc (sizeof (*cu
) + sendsz
+ recvsz
);
134 (void) fprintf (stderr
, _("clntudp_create: out of memory\n"));
135 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
136 rpc_createerr
.cf_error
.re_errno
= errno
;
139 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
141 if (raddr
->sin_port
== 0)
145 pmap_getport (raddr
, program
, version
, IPPROTO_UDP
)) == 0)
149 raddr
->sin_port
= htons (port
);
151 cl
->cl_ops
= &udp_ops
;
152 cl
->cl_private
= (caddr_t
) cu
;
153 cu
->cu_raddr
= *raddr
;
154 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
156 cu
->cu_total
.tv_sec
= -1;
157 cu
->cu_total
.tv_usec
= -1;
158 cu
->cu_sendsz
= sendsz
;
159 cu
->cu_recvsz
= recvsz
;
160 call_msg
.rm_xid
= _create_xid ();
161 call_msg
.rm_direction
= CALL
;
162 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
163 call_msg
.rm_call
.cb_prog
= program
;
164 call_msg
.rm_call
.cb_vers
= version
;
165 xdrmem_create (&(cu
->cu_outxdrs
), cu
->cu_outbuf
,
167 if (!xdr_callhdr (&(cu
->cu_outxdrs
), &call_msg
))
171 cu
->cu_xdrpos
= XDR_GETPOS (&(cu
->cu_outxdrs
));
176 *sockp
= __socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
179 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
180 rpc_createerr
.cf_error
.re_errno
= errno
;
183 /* attempt to bind to prov port */
184 (void) bindresvport (*sockp
, (struct sockaddr_in
*) 0);
185 /* the sockets rpc controls are non-blocking */
186 (void) __ioctl (*sockp
, FIONBIO
, (char *) &dontblock
);
187 cu
->cu_closeit
= TRUE
;
191 cu
->cu_closeit
= FALSE
;
193 cu
->cu_sock
= *sockp
;
194 cl
->cl_auth
= authnone_create ();
198 mem_free ((caddr_t
) cu
, sizeof (*cu
) + sendsz
+ recvsz
);
200 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));
201 return (CLIENT
*) NULL
;
205 clntudp_create (raddr
, program
, version
, wait
, sockp
)
206 struct sockaddr_in
*raddr
;
213 return clntudp_bufcreate (raddr
, program
, version
, wait
, sockp
,
214 UDPMSGSIZE
, UDPMSGSIZE
);
217 static enum clnt_stat
218 clntudp_call (cl
, proc
, xargs
, argsp
, xresults
, resultsp
, utimeout
)
219 CLIENT
*cl
; /* client handle */
220 u_long proc
; /* procedure number */
221 xdrproc_t xargs
; /* xdr routine for args */
222 caddr_t argsp
; /* pointer to args */
223 xdrproc_t xresults
; /* xdr routine for results */
224 caddr_t resultsp
; /* pointer to results */
225 struct timeval utimeout
; /* seconds to wait before giving up */
227 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
233 int milliseconds
= (cu
->cu_wait
.tv_sec
* 1000) +
234 (cu
->cu_wait
.tv_usec
/ 1000);
235 struct sockaddr_in from
;
236 struct rpc_msg reply_msg
;
238 struct timeval time_waited
;
240 int nrefreshes
= 2; /* number of times to refresh cred */
241 struct timeval timeout
;
243 if (cu
->cu_total
.tv_usec
== -1)
245 timeout
= utimeout
; /* use supplied timeout */
249 timeout
= cu
->cu_total
; /* use default timeout */
252 time_waited
.tv_sec
= 0;
253 time_waited
.tv_usec
= 0;
255 xdrs
= &(cu
->cu_outxdrs
);
258 xdrs
->x_op
= XDR_ENCODE
;
259 XDR_SETPOS (xdrs
, cu
->cu_xdrpos
);
261 * the transaction is the first thing in the out buffer
263 (*(u_short
*) (cu
->cu_outbuf
))++;
264 if ((!XDR_PUTLONG (xdrs
, (long *) &proc
)) ||
265 (!AUTH_MARSHALL (cl
->cl_auth
, xdrs
)) ||
266 (!(*xargs
) (xdrs
, argsp
)))
267 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
268 outlen
= (int) XDR_GETPOS (xdrs
);
271 if (sendto (cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
272 (struct sockaddr
*) &(cu
->cu_raddr
), cu
->cu_rlen
)
275 cu
->cu_error
.re_errno
= errno
;
276 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
280 * Hack to provide rpc-based message passing
282 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
284 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
288 * sub-optimal code appears here because we have
289 * some clock time to spare while the packets are in flight.
290 * (We assume that this is actually only executed once.)
292 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
293 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
294 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
299 switch (__poll(&fd
, 1, milliseconds
))
303 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
304 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
305 while (time_waited
.tv_usec
>= 1000000)
307 time_waited
.tv_sec
++;
308 time_waited
.tv_usec
-= 1000000;
310 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
311 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
312 (time_waited
.tv_usec
< timeout
.tv_usec
)))
314 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
317 * buggy in other cases because time_waited is not being
323 cu
->cu_error
.re_errno
= errno
;
324 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
328 fromlen
= sizeof (struct sockaddr
);
329 inlen
= recvfrom (cu
->cu_sock
, cu
->cu_inbuf
,
330 (int) cu
->cu_recvsz
, 0,
331 (struct sockaddr
*) &from
, &fromlen
);
333 while (inlen
< 0 && errno
== EINTR
);
336 if (errno
== EWOULDBLOCK
)
338 cu
->cu_error
.re_errno
= errno
;
339 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
344 /* see if reply transaction id matches sent id.
345 Don't do this if we only wait for a replay */
347 && (*((u_int32_t
*) (cu
->cu_inbuf
))
348 != *((u_int32_t
*) (cu
->cu_outbuf
))))
350 /* we now assume we have the proper reply */
355 * now decode and validate the response
357 xdrmem_create (&reply_xdrs
, cu
->cu_inbuf
, (u_int
) inlen
, XDR_DECODE
);
358 ok
= xdr_replymsg (&reply_xdrs
, &reply_msg
);
359 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
362 _seterr_reply (&reply_msg
, &(cu
->cu_error
));
363 if (cu
->cu_error
.re_status
== RPC_SUCCESS
)
365 if (!AUTH_VALIDATE (cl
->cl_auth
,
366 &reply_msg
.acpted_rply
.ar_verf
))
368 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
369 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
371 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
373 xdrs
->x_op
= XDR_FREE
;
374 (void) xdr_opaque_auth (xdrs
,
375 &(reply_msg
.acpted_rply
.ar_verf
));
377 } /* end successful completion */
380 /* maybe our credentials need to be refreshed ... */
381 if (nrefreshes
> 0 && AUTH_REFRESH (cl
->cl_auth
))
386 } /* end of unsuccessful completion */
387 } /* end of valid reply message */
390 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
392 return cu
->cu_error
.re_status
;
396 clntudp_geterr (CLIENT
*cl
, struct rpc_err
*errp
)
398 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
400 *errp
= cu
->cu_error
;
405 clntudp_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
407 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
408 XDR
*xdrs
= &(cu
->cu_outxdrs
);
410 xdrs
->x_op
= XDR_FREE
;
411 return (*xdr_res
) (xdrs
, res_ptr
);
420 clntudp_control (CLIENT
*cl
, int request
, char *info
)
422 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
427 cu
->cu_closeit
= TRUE
;
429 case CLSET_FD_NCLOSE
:
430 cu
->cu_closeit
= FALSE
;
433 cu
->cu_total
= *(struct timeval
*) info
;
436 *(struct timeval
*) info
= cu
->cu_total
;
438 case CLSET_RETRY_TIMEOUT
:
439 cu
->cu_wait
= *(struct timeval
*) info
;
441 case CLGET_RETRY_TIMEOUT
:
442 *(struct timeval
*) info
= cu
->cu_wait
;
444 case CLGET_SERVER_ADDR
:
445 *(struct sockaddr_in
*) info
= cu
->cu_raddr
;
448 *(int *)info
= cu
->cu_sock
;
452 * use the knowledge that xid is the
453 * first element in the call structure *.
454 * This will get the xid of the PREVIOUS call
456 *(u_long
*)info
= ntohl(*(u_long
*)cu
->cu_outbuf
);
459 /* This will set the xid of the NEXT call */
460 *(u_long
*)cu
->cu_outbuf
= htonl(*(u_long
*)info
- 1);
461 /* decrement by 1 as clntudp_call() increments once */
464 * This RELIES on the information that, in the call body,
465 * the version number field is the fifth field from the
466 * begining of the RPC header. MUST be changed if the
467 * call_struct is changed
469 *(u_long
*)info
= ntohl(*(u_long
*)(cu
->cu_outbuf
+
470 4 * BYTES_PER_XDR_UNIT
));
473 *(u_long
*)(cu
->cu_outbuf
+ 4 * BYTES_PER_XDR_UNIT
)
474 = htonl(*(u_long
*)info
);
478 * This RELIES on the information that, in the call body,
479 * the program number field is the field from the
480 * begining of the RPC header. MUST be changed if the
481 * call_struct is changed
483 *(u_long
*)info
= ntohl(*(u_long
*)(cu
->cu_outbuf
+
484 3 * BYTES_PER_XDR_UNIT
));
487 *(u_long
*)(cu
->cu_outbuf
+ 3 * BYTES_PER_XDR_UNIT
)
488 = htonl(*(u_long
*)info
);
490 /* The following are only possible with TI-RPC */
493 case CLSET_PUSH_TIMOD
:
494 case CLSET_POP_TIMOD
:
502 clntudp_destroy (CLIENT
*cl
)
504 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
508 (void) __close (cu
->cu_sock
);
510 XDR_DESTROY (&(cu
->cu_outxdrs
));
511 mem_free ((caddr_t
) cu
, (sizeof (*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
512 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));