]>
sourceware.org Git - glibc.git/blob - sunrpc/clnt_udp.c
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
);
265 xdrs
->x_op
= XDR_ENCODE
;
266 XDR_SETPOS (xdrs
, cu
->cu_xdrpos
);
268 * the transaction is the first thing in the out buffer
270 (*(u_short
*) (cu
->cu_outbuf
))++;
271 if ((!XDR_PUTLONG (xdrs
, (long *) &proc
)) ||
272 (!AUTH_MARSHALL (cl
->cl_auth
, xdrs
)) ||
273 (!(*xargs
) (xdrs
, argsp
)))
274 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
275 outlen
= (int) XDR_GETPOS (xdrs
);
278 if (sendto (cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
279 (struct sockaddr
*) &(cu
->cu_raddr
), cu
->cu_rlen
)
282 cu
->cu_error
.re_errno
= errno
;
283 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
287 * Hack to provide rpc-based message passing
289 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
291 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
;
303 FD_SET (cu
->cu_sock
, &mask
);
305 mask
= 1 << cu
->cu_sock
;
306 #endif /* def FD_SETSIZE */
309 struct timeval timeout
= cu
->cu_wait
;
311 switch (select (_rpc_dtablesize (), &readfds
, (fd_set
*) NULL
,
312 (fd_set
*) NULL
, &timeout
))
316 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
317 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
318 while (time_waited
.tv_usec
>= 1000000)
320 time_waited
.tv_sec
++;
321 time_waited
.tv_usec
-= 1000000;
323 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
324 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
325 (time_waited
.tv_usec
< timeout
.tv_usec
)))
327 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
330 * buggy in other cases because time_waited is not being
336 cu
->cu_error
.re_errno
= errno
;
337 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
341 fromlen
= sizeof (struct sockaddr
);
342 inlen
= recvfrom (cu
->cu_sock
, cu
->cu_inbuf
,
343 (int) cu
->cu_recvsz
, 0,
344 (struct sockaddr
*) &from
, &fromlen
);
346 while (inlen
< 0 && errno
== EINTR
);
349 if (errno
== EWOULDBLOCK
)
351 cu
->cu_error
.re_errno
= errno
;
352 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
356 /* see if reply transaction id matches sent id */
357 if (*((u_int32_t
*) (cu
->cu_inbuf
)) != *((u_int32_t
*) (cu
->cu_outbuf
)))
359 /* we now assume we have the proper reply */
364 * now decode and validate the response
366 xdrmem_create (&reply_xdrs
, cu
->cu_inbuf
, (u_int
) inlen
, XDR_DECODE
);
367 ok
= xdr_replymsg (&reply_xdrs
, &reply_msg
);
368 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
371 _seterr_reply (&reply_msg
, &(cu
->cu_error
));
372 if (cu
->cu_error
.re_status
== RPC_SUCCESS
)
374 if (!AUTH_VALIDATE (cl
->cl_auth
,
375 &reply_msg
.acpted_rply
.ar_verf
))
377 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
378 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
380 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
382 xdrs
->x_op
= XDR_FREE
;
383 (void) xdr_opaque_auth (xdrs
,
384 &(reply_msg
.acpted_rply
.ar_verf
));
386 } /* end successful completion */
389 /* maybe our credentials need to be refreshed ... */
390 if (nrefreshes
> 0 && AUTH_REFRESH (cl
->cl_auth
))
395 } /* end of unsuccessful completion */
396 } /* end of valid reply message */
399 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
401 return cu
->cu_error
.re_status
;
405 clntudp_geterr (CLIENT
*cl
, struct rpc_err
*errp
)
407 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
409 *errp
= cu
->cu_error
;
414 clntudp_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
416 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
417 XDR
*xdrs
= &(cu
->cu_outxdrs
);
419 xdrs
->x_op
= XDR_FREE
;
420 return (*xdr_res
) (xdrs
, res_ptr
);
429 clntudp_control (CLIENT
*cl
, int request
, char *info
)
431 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
436 cu
->cu_total
= *(struct timeval
*) info
;
439 *(struct timeval
*) info
= cu
->cu_total
;
441 case CLSET_RETRY_TIMEOUT
:
442 cu
->cu_wait
= *(struct timeval
*) info
;
444 case CLGET_RETRY_TIMEOUT
:
445 *(struct timeval
*) info
= cu
->cu_wait
;
447 case CLGET_SERVER_ADDR
:
448 *(struct sockaddr_in
*) info
= cu
->cu_raddr
;
457 clntudp_destroy (CLIENT
*cl
)
459 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
463 (void) close (cu
->cu_sock
);
465 XDR_DESTROY (&(cu
->cu_outxdrs
));
466 mem_free ((caddr_t
) cu
, (sizeof (*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
467 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));
This page took 0.060407 seconds and 5 git commands to generate.