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>
45 #include <rpc/pmap_clnt.h>
58 #include <kernel-features.h>
60 extern u_long _create_xid (void);
63 * UDP bases client side rpc operations
65 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
66 xdrproc_t, caddr_t, struct timeval);
67 static void clntudp_abort (void);
68 static void clntudp_geterr (CLIENT *, struct rpc_err *);
69 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
70 static bool_t clntudp_control (CLIENT *, int, char *);
71 static void clntudp_destroy (CLIENT *);
73 static const struct clnt_ops udp_ops =
84 * Private data kept per client handle
90 struct sockaddr_in cu_raddr;
92 struct timeval cu_wait;
93 struct timeval cu_total;
94 struct rpc_err cu_error;
104 * Create a UDP based client handle.
105 * If *sockp<0, *sockp is set to a newly created UPD socket.
106 * If raddr->sin_port is 0 a binder on the remote machine
107 * is consulted for the correct port number.
108 * NB: It is the clients responsibility to close *sockp.
109 * NB: The rpch->cl_auth is initialized to null authentication.
110 * Caller may wish to set this something more useful.
112 * wait is the amount of time used between retransmitting a call if
113 * no response has been heard; retransmission occurs until the actual
114 * rpc call times out.
116 * sendsz and recvsz are the maximum allowable packet sizes that can be
120 __libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program,
121 u_long version, struct timeval wait, int *sockp,
122 u_int sendsz, u_int recvsz, int flags)
125 struct cu_data *cu = NULL;
126 struct rpc_msg call_msg;
128 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
129 sendsz = ((sendsz + 3) / 4) * 4;
130 recvsz = ((recvsz + 3) / 4) * 4;
131 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
132 if (cl == NULL || cu == NULL)
134 struct rpc_createerr *ce = &get_rpc_createerr ();
135 (void) __fxprintf (NULL, "%s: %s",
136 "clntudp_create", _("out of memory\n"));
137 ce->cf_stat = RPC_SYSTEMERROR;
138 ce->cf_error.re_errno = ENOMEM;
141 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
143 if (raddr->sin_port == 0)
147 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
151 raddr->sin_port = htons (port);
153 cl->cl_ops = (struct clnt_ops *) &udp_ops;
154 cl->cl_private = (caddr_t) cu;
155 cu->cu_raddr = *raddr;
156 cu->cu_rlen = sizeof (cu->cu_raddr);
158 cu->cu_total.tv_sec = -1;
159 cu->cu_total.tv_usec = -1;
160 cu->cu_sendsz = sendsz;
161 cu->cu_recvsz = recvsz;
162 call_msg.rm_xid = _create_xid ();
163 call_msg.rm_direction = CALL;
164 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
165 call_msg.rm_call.cb_prog = program;
166 call_msg.rm_call.cb_vers = version;
167 xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
168 if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
172 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
178 # ifndef __ASSUME_SOCK_CLOEXEC
179 if (__have_sock_cloexec >= 0)
182 *sockp = __socket (AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|flags,
184 # ifndef __ASSUME_SOCK_CLOEXEC
185 if (__have_sock_cloexec == 0)
186 __have_sock_cloexec = *sockp >= 0 || errno != EINVAL ? 1 : -1;
190 #ifndef __ASSUME_SOCK_CLOEXEC
192 if (__have_sock_cloexec < 0)
195 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
197 if (flags & SOCK_CLOEXEC)
198 __fcntl (*sockp, F_SETFD, FD_CLOEXEC);
202 if (__builtin_expect (*sockp < 0, 0))
204 struct rpc_createerr *ce = &get_rpc_createerr ();
205 ce->cf_stat = RPC_SYSTEMERROR;
206 ce->cf_error.re_errno = errno;
209 /* attempt to bind to prov port */
210 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
211 #ifndef __ASSUME_SOCK_CLOEXEC
213 if (__have_sock_cloexec < 0)
215 /* the sockets rpc controls are non-blocking */
216 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
221 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
224 cu->cu_closeit = TRUE;
228 cu->cu_closeit = FALSE;
230 cu->cu_sock = *sockp;
231 cl->cl_auth = authnone_create ();
235 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
237 mem_free ((caddr_t) cl, sizeof (CLIENT));
238 return (CLIENT *) NULL;
240 #ifdef EXPORT_RPC_SYMBOLS
241 libc_hidden_def (__libc_clntudp_bufcreate)
243 libc_hidden_nolink (__libc_clntudp_bufcreate, GLIBC_PRIVATE)
247 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
248 struct timeval wait, int *sockp, u_int sendsz,
251 return __libc_clntudp_bufcreate (raddr, program, version, wait,
252 sockp, sendsz, recvsz, 0);
254 libc_hidden_nolink (clntudp_bufcreate, GLIBC_2_0)
257 clntudp_create (raddr, program, version, wait, sockp)
258 struct sockaddr_in *raddr;
264 return __libc_clntudp_bufcreate (raddr, program, version, wait,
265 sockp, UDPMSGSIZE, UDPMSGSIZE, 0);
267 #ifdef EXPORT_RPC_SYMBOLS
268 libc_hidden_def (clntudp_create)
270 libc_hidden_nolink (clntudp_create, GLIBC_2_0)
274 is_network_up (int sock)
278 if (getifaddrs (&ifa) != 0)
281 struct ifaddrs *run = ifa;
284 if ((run->ifa_flags & IFF_UP) != 0
285 && run->ifa_addr != NULL
286 && run->ifa_addr->sa_family == AF_INET)
297 static enum clnt_stat
298 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
299 CLIENT *cl; /* client handle */
300 u_long proc; /* procedure number */
301 xdrproc_t xargs; /* xdr routine for args */
302 caddr_t argsp; /* pointer to args */
303 xdrproc_t xresults; /* xdr routine for results */
304 caddr_t resultsp; /* pointer to results */
305 struct timeval utimeout; /* seconds to wait before giving up */
307 struct cu_data *cu = (struct cu_data *) cl->cl_private;
313 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
314 (cu->cu_wait.tv_usec / 1000);
315 struct sockaddr_in from;
316 struct rpc_msg reply_msg;
318 struct timeval time_waited;
320 int nrefreshes = 2; /* number of times to refresh cred */
321 struct timeval timeout;
322 int anyup; /* any network interface up */
324 if (cu->cu_total.tv_usec == -1)
326 timeout = utimeout; /* use supplied timeout */
330 timeout = cu->cu_total; /* use default timeout */
333 time_waited.tv_sec = 0;
334 time_waited.tv_usec = 0;
336 xdrs = &(cu->cu_outxdrs);
339 xdrs->x_op = XDR_ENCODE;
340 XDR_SETPOS (xdrs, cu->cu_xdrpos);
342 * the transaction is the first thing in the out buffer
344 (*(uint32_t *) (cu->cu_outbuf))++;
345 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
346 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
347 (!(*xargs) (xdrs, argsp)))
348 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
349 outlen = (int) XDR_GETPOS (xdrs);
352 if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
353 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
356 cu->cu_error.re_errno = errno;
357 return (cu->cu_error.re_status = RPC_CANTSEND);
361 * Hack to provide rpc-based message passing
363 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
365 return (cu->cu_error.re_status = RPC_TIMEDOUT);
369 * sub-optimal code appears here because we have
370 * some clock time to spare while the packets are in flight.
371 * (We assume that this is actually only executed once.)
373 reply_msg.acpted_rply.ar_verf = _null_auth;
374 reply_msg.acpted_rply.ar_results.where = resultsp;
375 reply_msg.acpted_rply.ar_results.proc = xresults;
381 switch (__poll (&fd, 1, milliseconds))
387 anyup = is_network_up (cu->cu_sock);
389 return (cu->cu_error.re_status = RPC_CANTRECV);
392 time_waited.tv_sec += cu->cu_wait.tv_sec;
393 time_waited.tv_usec += cu->cu_wait.tv_usec;
394 while (time_waited.tv_usec >= 1000000)
396 time_waited.tv_sec++;
397 time_waited.tv_usec -= 1000000;
399 if ((time_waited.tv_sec < timeout.tv_sec) ||
400 ((time_waited.tv_sec == timeout.tv_sec) &&
401 (time_waited.tv_usec < timeout.tv_usec)))
403 return (cu->cu_error.re_status = RPC_TIMEDOUT);
406 * buggy in other cases because time_waited is not being
412 cu->cu_error.re_errno = errno;
413 return (cu->cu_error.re_status = RPC_CANTRECV);
416 if (fd.revents & POLLERR)
419 struct cmsghdr *cmsg;
420 struct sock_extended_err *e;
421 struct sockaddr_in err_addr;
423 char *cbuf = (char *) alloca (outlen + 256);
426 iov.iov_base = cbuf + 256;
427 iov.iov_len = outlen;
428 msg.msg_name = (void *) &err_addr;
429 msg.msg_namelen = sizeof (err_addr);
433 msg.msg_control = cbuf;
434 msg.msg_controllen = 256;
435 ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
437 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
438 && (msg.msg_flags & MSG_ERRQUEUE)
439 && ((msg.msg_namelen == 0
441 || (msg.msg_namelen == sizeof (err_addr)
442 && err_addr.sin_family == AF_INET
443 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
444 sizeof (err_addr.sin_addr)) == 0
445 && err_addr.sin_port == cu->cu_raddr.sin_port)))
446 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
447 cmsg = CMSG_NXTHDR (&msg, cmsg))
448 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
450 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
451 cu->cu_error.re_errno = e->ee_errno;
452 return (cu->cu_error.re_status = RPC_CANTRECV);
458 fromlen = sizeof (struct sockaddr);
459 inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf,
460 (int) cu->cu_recvsz, MSG_DONTWAIT,
461 (struct sockaddr *) &from, &fromlen);
463 while (inlen < 0 && errno == EINTR);
466 if (errno == EWOULDBLOCK)
468 cu->cu_error.re_errno = errno;
469 return (cu->cu_error.re_status = RPC_CANTRECV);
474 /* see if reply transaction id matches sent id.
475 Don't do this if we only wait for a replay */
477 && (*((u_int32_t *) (cu->cu_inbuf))
478 != *((u_int32_t *) (cu->cu_outbuf))))
480 /* we now assume we have the proper reply */
485 * now decode and validate the response
487 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
488 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
489 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
492 _seterr_reply (&reply_msg, &(cu->cu_error));
493 if (cu->cu_error.re_status == RPC_SUCCESS)
495 if (!AUTH_VALIDATE (cl->cl_auth,
496 &reply_msg.acpted_rply.ar_verf))
498 cu->cu_error.re_status = RPC_AUTHERROR;
499 cu->cu_error.re_why = AUTH_INVALIDRESP;
501 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
503 xdrs->x_op = XDR_FREE;
504 (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
506 } /* end successful completion */
509 /* maybe our credentials need to be refreshed ... */
510 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
515 } /* end of unsuccessful completion */
516 } /* end of valid reply message */
519 cu->cu_error.re_status = RPC_CANTDECODERES;
521 return cu->cu_error.re_status;
525 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
527 struct cu_data *cu = (struct cu_data *) cl->cl_private;
529 *errp = cu->cu_error;
534 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
536 struct cu_data *cu = (struct cu_data *) cl->cl_private;
537 XDR *xdrs = &(cu->cu_outxdrs);
539 xdrs->x_op = XDR_FREE;
540 return (*xdr_res) (xdrs, res_ptr);
549 clntudp_control (CLIENT *cl, int request, char *info)
551 struct cu_data *cu = (struct cu_data *) cl->cl_private;
556 cu->cu_closeit = TRUE;
558 case CLSET_FD_NCLOSE:
559 cu->cu_closeit = FALSE;
562 cu->cu_total = *(struct timeval *) info;
565 *(struct timeval *) info = cu->cu_total;
567 case CLSET_RETRY_TIMEOUT:
568 cu->cu_wait = *(struct timeval *) info;
570 case CLGET_RETRY_TIMEOUT:
571 *(struct timeval *) info = cu->cu_wait;
573 case CLGET_SERVER_ADDR:
574 *(struct sockaddr_in *) info = cu->cu_raddr;
577 *(int *)info = cu->cu_sock;
581 * use the knowledge that xid is the
582 * first element in the call structure *.
583 * This will get the xid of the PREVIOUS call
585 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
588 /* This will set the xid of the NEXT call */
589 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
590 /* decrement by 1 as clntudp_call() increments once */
594 * This RELIES on the information that, in the call body,
595 * the version number field is the fifth field from the
596 * begining of the RPC header. MUST be changed if the
597 * call_struct is changed
599 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
600 4 * BYTES_PER_XDR_UNIT));
603 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
604 = htonl(*(u_long *)info);
608 * This RELIES on the information that, in the call body,
609 * the program number field is the field from the
610 * begining of the RPC header. MUST be changed if the
611 * call_struct is changed
613 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
614 3 * BYTES_PER_XDR_UNIT));
617 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
618 = htonl(*(u_long *)info);
620 /* The following are only possible with TI-RPC */
623 case CLSET_PUSH_TIMOD:
624 case CLSET_POP_TIMOD:
632 clntudp_destroy (CLIENT *cl)
634 struct cu_data *cu = (struct cu_data *) cl->cl_private;
638 (void) __close (cu->cu_sock);
640 XDR_DESTROY (&(cu->cu_outxdrs));
641 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
642 mem_free ((caddr_t) cl, sizeof (CLIENT));