2 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
46 #include <rpc/pmap_clnt.h>
57 #include <kernel-features.h>
59 extern u_long
_create_xid (void);
62 * UDP bases client side rpc operations
64 static enum clnt_stat
clntudp_call (CLIENT
*, u_long
, xdrproc_t
, caddr_t
,
65 xdrproc_t
, caddr_t
, struct timeval
);
66 static void clntudp_abort (void);
67 static void clntudp_geterr (CLIENT
*, struct rpc_err
*);
68 static bool_t
clntudp_freeres (CLIENT
*, xdrproc_t
, caddr_t
);
69 static bool_t
clntudp_control (CLIENT
*, int, char *);
70 static void clntudp_destroy (CLIENT
*);
72 static const struct clnt_ops udp_ops
=
83 * Private data kept per client handle
89 struct sockaddr_in cu_raddr
;
91 struct timeval cu_wait
;
92 struct timeval cu_total
;
93 struct rpc_err cu_error
;
103 * Create a UDP based client handle.
104 * If *sockp<0, *sockp is set to a newly created UPD socket.
105 * If raddr->sin_port is 0 a binder on the remote machine
106 * is consulted for the correct port number.
107 * NB: It is the clients responsibility to close *sockp.
108 * NB: The rpch->cl_auth is initialized to null authentication.
109 * Caller may wish to set this something more useful.
111 * wait is the amount of time used between retransmitting a call if
112 * no response has been heard; retransmission occurs until the actual
113 * rpc call times out.
115 * sendsz and recvsz are the maximum allowable packet sizes that can be
119 __libc_clntudp_bufcreate (struct sockaddr_in
*raddr
, u_long program
,
120 u_long version
, struct timeval wait
, int *sockp
,
121 u_int sendsz
, u_int recvsz
, int flags
)
124 struct cu_data
*cu
= NULL
;
125 struct rpc_msg call_msg
;
127 cl
= (CLIENT
*) mem_alloc (sizeof (CLIENT
));
128 sendsz
= ((sendsz
+ 3) / 4) * 4;
129 recvsz
= ((recvsz
+ 3) / 4) * 4;
130 cu
= (struct cu_data
*) mem_alloc (sizeof (*cu
) + sendsz
+ recvsz
);
131 if (cl
== NULL
|| cu
== NULL
)
133 struct rpc_createerr
*ce
= &get_rpc_createerr ();
134 (void) __fxprintf (NULL
, "%s: %s",
135 "clntudp_create", _("out of memory\n"));
136 ce
->cf_stat
= RPC_SYSTEMERROR
;
137 ce
->cf_error
.re_errno
= ENOMEM
;
140 cu
->cu_outbuf
= &cu
->cu_inbuf
[recvsz
];
142 if (raddr
->sin_port
== 0)
146 pmap_getport (raddr
, program
, version
, IPPROTO_UDP
)) == 0)
150 raddr
->sin_port
= htons (port
);
152 cl
->cl_ops
= (struct clnt_ops
*) &udp_ops
;
153 cl
->cl_private
= (caddr_t
) cu
;
154 cu
->cu_raddr
= *raddr
;
155 cu
->cu_rlen
= sizeof (cu
->cu_raddr
);
157 cu
->cu_total
.tv_sec
= -1;
158 cu
->cu_total
.tv_usec
= -1;
159 cu
->cu_sendsz
= sendsz
;
160 cu
->cu_recvsz
= recvsz
;
161 call_msg
.rm_xid
= _create_xid ();
162 call_msg
.rm_direction
= CALL
;
163 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
164 call_msg
.rm_call
.cb_prog
= program
;
165 call_msg
.rm_call
.cb_vers
= version
;
166 xdrmem_create (&(cu
->cu_outxdrs
), cu
->cu_outbuf
, sendsz
, XDR_ENCODE
);
167 if (!xdr_callhdr (&(cu
->cu_outxdrs
), &call_msg
))
171 cu
->cu_xdrpos
= XDR_GETPOS (&(cu
->cu_outxdrs
));
174 *sockp
= __socket (AF_INET
, SOCK_DGRAM
|SOCK_NONBLOCK
|flags
, IPPROTO_UDP
);
175 if (__glibc_unlikely (*sockp
< 0))
177 struct rpc_createerr
*ce
= &get_rpc_createerr ();
178 ce
->cf_stat
= RPC_SYSTEMERROR
;
179 ce
->cf_error
.re_errno
= errno
;
182 /* attempt to bind to prov port */
183 (void) bindresvport (*sockp
, (struct sockaddr_in
*) 0);
187 __setsockopt (*sockp
, SOL_IP
, IP_RECVERR
, &on
, sizeof(on
));
190 cu
->cu_closeit
= TRUE
;
194 cu
->cu_closeit
= FALSE
;
196 cu
->cu_sock
= *sockp
;
197 cl
->cl_auth
= authnone_create ();
201 mem_free ((caddr_t
) cu
, sizeof (*cu
) + sendsz
+ recvsz
);
203 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));
204 return (CLIENT
*) NULL
;
206 #ifdef EXPORT_RPC_SYMBOLS
207 libc_hidden_def (__libc_clntudp_bufcreate
)
209 libc_hidden_nolink_sunrpc (__libc_clntudp_bufcreate
, GLIBC_PRIVATE
)
213 clntudp_bufcreate (struct sockaddr_in
*raddr
, u_long program
, u_long version
,
214 struct timeval wait
, int *sockp
, u_int sendsz
,
217 return __libc_clntudp_bufcreate (raddr
, program
, version
, wait
,
218 sockp
, sendsz
, recvsz
, 0);
220 libc_hidden_nolink_sunrpc (clntudp_bufcreate
, GLIBC_2_0
)
223 clntudp_create (struct sockaddr_in
*raddr
, u_long program
, u_long version
,
224 struct timeval wait
, int *sockp
)
226 return __libc_clntudp_bufcreate (raddr
, program
, version
, wait
,
227 sockp
, UDPMSGSIZE
, UDPMSGSIZE
, 0);
229 #ifdef EXPORT_RPC_SYMBOLS
230 libc_hidden_def (clntudp_create
)
232 libc_hidden_nolink_sunrpc (clntudp_create
, GLIBC_2_0
)
236 is_network_up (int sock
)
240 if (getifaddrs (&ifa
) != 0)
243 struct ifaddrs
*run
= ifa
;
246 if ((run
->ifa_flags
& IFF_UP
) != 0
247 && run
->ifa_addr
!= NULL
248 && run
->ifa_addr
->sa_family
== AF_INET
)
259 static enum clnt_stat
260 clntudp_call (/* client handle */
262 /* procedure number */
264 /* xdr routine for args */
266 /* pointer to args */
268 /* xdr routine for results */
270 /* pointer to results */
272 /* seconds to wait before giving up */
273 struct timeval utimeout
)
275 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
281 int milliseconds
= (cu
->cu_wait
.tv_sec
* 1000) +
282 (cu
->cu_wait
.tv_usec
/ 1000);
283 struct sockaddr_in from
;
284 struct rpc_msg reply_msg
;
286 struct timeval time_waited
;
288 int nrefreshes
= 2; /* number of times to refresh cred */
289 struct timeval timeout
;
290 int anyup
; /* any network interface up */
292 if (cu
->cu_total
.tv_usec
== -1)
294 timeout
= utimeout
; /* use supplied timeout */
298 timeout
= cu
->cu_total
; /* use default timeout */
301 time_waited
.tv_sec
= 0;
302 time_waited
.tv_usec
= 0;
304 xdrs
= &(cu
->cu_outxdrs
);
307 xdrs
->x_op
= XDR_ENCODE
;
308 XDR_SETPOS (xdrs
, cu
->cu_xdrpos
);
310 * the transaction is the first thing in the out buffer
312 (*(uint32_t *) (cu
->cu_outbuf
))++;
313 if ((!XDR_PUTLONG (xdrs
, (long *) &proc
)) ||
314 (!AUTH_MARSHALL (cl
->cl_auth
, xdrs
)) ||
315 (!(*xargs
) (xdrs
, argsp
)))
316 return (cu
->cu_error
.re_status
= RPC_CANTENCODEARGS
);
317 outlen
= (int) XDR_GETPOS (xdrs
);
320 if (__sendto (cu
->cu_sock
, cu
->cu_outbuf
, outlen
, 0,
321 (struct sockaddr
*) &(cu
->cu_raddr
), cu
->cu_rlen
)
324 cu
->cu_error
.re_errno
= errno
;
325 return (cu
->cu_error
.re_status
= RPC_CANTSEND
);
329 * Hack to provide rpc-based message passing
331 if (timeout
.tv_sec
== 0 && timeout
.tv_usec
== 0)
333 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
337 * sub-optimal code appears here because we have
338 * some clock time to spare while the packets are in flight.
339 * (We assume that this is actually only executed once.)
341 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
342 reply_msg
.acpted_rply
.ar_results
.where
= resultsp
;
343 reply_msg
.acpted_rply
.ar_results
.proc
= xresults
;
349 switch (__poll (&fd
, 1, milliseconds
))
355 anyup
= is_network_up (cu
->cu_sock
);
357 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
360 time_waited
.tv_sec
+= cu
->cu_wait
.tv_sec
;
361 time_waited
.tv_usec
+= cu
->cu_wait
.tv_usec
;
362 while (time_waited
.tv_usec
>= 1000000)
364 time_waited
.tv_sec
++;
365 time_waited
.tv_usec
-= 1000000;
367 if ((time_waited
.tv_sec
< timeout
.tv_sec
) ||
368 ((time_waited
.tv_sec
== timeout
.tv_sec
) &&
369 (time_waited
.tv_usec
< timeout
.tv_usec
)))
371 return (cu
->cu_error
.re_status
= RPC_TIMEDOUT
);
374 * buggy in other cases because time_waited is not being
380 cu
->cu_error
.re_errno
= errno
;
381 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
384 if (fd
.revents
& POLLERR
)
387 struct cmsghdr
*cmsg
;
388 struct sock_extended_err
*e
;
389 struct sockaddr_in err_addr
;
391 char *cbuf
= (char *) alloca (outlen
+ 256);
394 iov
.iov_base
= cbuf
+ 256;
395 iov
.iov_len
= outlen
;
396 msg
.msg_name
= (void *) &err_addr
;
397 msg
.msg_namelen
= sizeof (err_addr
);
401 msg
.msg_control
= cbuf
;
402 msg
.msg_controllen
= 256;
403 ret
= __recvmsg (cu
->cu_sock
, &msg
, MSG_ERRQUEUE
);
405 && memcmp (cbuf
+ 256, cu
->cu_outbuf
, ret
) == 0
406 && (msg
.msg_flags
& MSG_ERRQUEUE
)
407 && ((msg
.msg_namelen
== 0
409 || (msg
.msg_namelen
== sizeof (err_addr
)
410 && err_addr
.sin_family
== AF_INET
411 && memcmp (&err_addr
.sin_addr
, &cu
->cu_raddr
.sin_addr
,
412 sizeof (err_addr
.sin_addr
)) == 0
413 && err_addr
.sin_port
== cu
->cu_raddr
.sin_port
)))
414 for (cmsg
= CMSG_FIRSTHDR (&msg
); cmsg
;
415 cmsg
= CMSG_NXTHDR (&msg
, cmsg
))
416 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_RECVERR
)
418 e
= (struct sock_extended_err
*) CMSG_DATA(cmsg
);
419 cu
->cu_error
.re_errno
= e
->ee_errno
;
420 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
426 fromlen
= sizeof (struct sockaddr
);
427 inlen
= __recvfrom (cu
->cu_sock
, cu
->cu_inbuf
,
428 (int) cu
->cu_recvsz
, MSG_DONTWAIT
,
429 (struct sockaddr
*) &from
, &fromlen
);
431 while (inlen
< 0 && errno
== EINTR
);
434 if (errno
== EWOULDBLOCK
)
436 cu
->cu_error
.re_errno
= errno
;
437 return (cu
->cu_error
.re_status
= RPC_CANTRECV
);
442 /* see if reply transaction id matches sent id.
443 Don't do this if we only wait for a replay */
445 && memcmp (cu
->cu_inbuf
, cu
->cu_outbuf
, sizeof (u_int32_t
)) != 0)
447 /* we now assume we have the proper reply */
452 * now decode and validate the response
454 xdrmem_create (&reply_xdrs
, cu
->cu_inbuf
, (u_int
) inlen
, XDR_DECODE
);
455 ok
= xdr_replymsg (&reply_xdrs
, &reply_msg
);
456 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
459 _seterr_reply (&reply_msg
, &(cu
->cu_error
));
460 if (cu
->cu_error
.re_status
== RPC_SUCCESS
)
462 if (!AUTH_VALIDATE (cl
->cl_auth
,
463 &reply_msg
.acpted_rply
.ar_verf
))
465 cu
->cu_error
.re_status
= RPC_AUTHERROR
;
466 cu
->cu_error
.re_why
= AUTH_INVALIDRESP
;
468 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
470 xdrs
->x_op
= XDR_FREE
;
471 (void) xdr_opaque_auth (xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
473 } /* end successful completion */
476 /* maybe our credentials need to be refreshed ... */
477 if (nrefreshes
> 0 && AUTH_REFRESH (cl
->cl_auth
))
482 } /* end of unsuccessful completion */
483 } /* end of valid reply message */
486 cu
->cu_error
.re_status
= RPC_CANTDECODERES
;
488 return cu
->cu_error
.re_status
;
492 clntudp_geterr (CLIENT
*cl
, struct rpc_err
*errp
)
494 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
496 *errp
= cu
->cu_error
;
501 clntudp_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
503 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
504 XDR
*xdrs
= &(cu
->cu_outxdrs
);
506 xdrs
->x_op
= XDR_FREE
;
507 return (*xdr_res
) (xdrs
, res_ptr
);
516 clntudp_control (CLIENT
*cl
, int request
, char *info
)
518 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
525 cu
->cu_closeit
= TRUE
;
527 case CLSET_FD_NCLOSE
:
528 cu
->cu_closeit
= FALSE
;
531 cu
->cu_total
= *(struct timeval
*) info
;
534 *(struct timeval
*) info
= cu
->cu_total
;
536 case CLSET_RETRY_TIMEOUT
:
537 cu
->cu_wait
= *(struct timeval
*) info
;
539 case CLGET_RETRY_TIMEOUT
:
540 *(struct timeval
*) info
= cu
->cu_wait
;
542 case CLGET_SERVER_ADDR
:
543 *(struct sockaddr_in
*) info
= cu
->cu_raddr
;
546 *(int *)info
= cu
->cu_sock
;
550 * use the knowledge that xid is the
551 * first element in the call structure *.
552 * This will get the xid of the PREVIOUS call
554 memcpy (&ui32
, cu
->cu_outbuf
, sizeof (ui32
));
556 memcpy (info
, &ul
, sizeof (ul
));
559 /* This will set the xid of the NEXT call */
560 memcpy (&ul
, info
, sizeof (ul
));
561 ui32
= htonl (ul
- 1);
562 memcpy (cu
->cu_outbuf
, &ui32
, sizeof (ui32
));
563 /* decrement by 1 as clntudp_call() increments once */
567 * This RELIES on the information that, in the call body,
568 * the version number field is the fifth field from the
569 * beginning of the RPC header. MUST be changed if the
570 * call_struct is changed
572 memcpy (&ui32
, cu
->cu_outbuf
+ 4 * BYTES_PER_XDR_UNIT
, sizeof (ui32
));
574 memcpy (info
, &ul
, sizeof (ul
));
577 memcpy (&ul
, info
, sizeof (ul
));
579 memcpy (cu
->cu_outbuf
+ 4 * BYTES_PER_XDR_UNIT
, &ui32
, sizeof (ui32
));
583 * This RELIES on the information that, in the call body,
584 * the program number field is the field from the
585 * beginning of the RPC header. MUST be changed if the
586 * call_struct is changed
588 memcpy (&ui32
, cu
->cu_outbuf
+ 3 * BYTES_PER_XDR_UNIT
, sizeof (ui32
));
590 memcpy (info
, &ul
, sizeof (ul
));
593 memcpy (&ul
, info
, sizeof (ul
));
595 memcpy (cu
->cu_outbuf
+ 3 * BYTES_PER_XDR_UNIT
, &ui32
, sizeof (ui32
));
597 /* The following are only possible with TI-RPC */
600 case CLSET_PUSH_TIMOD
:
601 case CLSET_POP_TIMOD
:
609 clntudp_destroy (CLIENT
*cl
)
611 struct cu_data
*cu
= (struct cu_data
*) cl
->cl_private
;
615 (void) __close (cu
->cu_sock
);
617 XDR_DESTROY (&(cu
->cu_outxdrs
));
618 mem_free ((caddr_t
) cu
, (sizeof (*cu
) + cu
->cu_sendsz
+ cu
->cu_recvsz
));
619 mem_free ((caddr_t
) cl
, sizeof (CLIENT
));