Convert 69 more function definitions to prototype style (line wrap cases).
[glibc.git] / sunrpc / clnt_udp.c
blob5653324bf1319f90cb2369448cac573a12252cb5
1 /*
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
8 * met:
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.
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <libintl.h>
37 #include <rpc/rpc.h>
38 #include <rpc/xdr.h>
39 #include <rpc/clnt.h>
40 #include <sys/poll.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <netdb.h>
44 #include <errno.h>
45 #include <stdint.h>
46 #include <rpc/pmap_clnt.h>
47 #include <net/if.h>
48 #include <ifaddrs.h>
49 #include <wchar.h>
50 #include <fcntl.h>
52 #ifdef IP_RECVERR
53 #include <errqueue.h>
54 #include <sys/uio.h>
55 #endif
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 =
74 clntudp_call,
75 clntudp_abort,
76 clntudp_geterr,
77 clntudp_freeres,
78 clntudp_destroy,
79 clntudp_control
83 * Private data kept per client handle
85 struct cu_data
87 int cu_sock;
88 bool_t cu_closeit;
89 struct sockaddr_in cu_raddr;
90 int cu_rlen;
91 struct timeval cu_wait;
92 struct timeval cu_total;
93 struct rpc_err cu_error;
94 XDR cu_outxdrs;
95 u_int cu_xdrpos;
96 u_int cu_sendsz;
97 char *cu_outbuf;
98 u_int cu_recvsz;
99 char cu_inbuf[1];
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
116 * sent and received.
118 CLIENT *
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)
123 CLIENT *cl;
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;
138 goto fooy;
140 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
142 if (raddr->sin_port == 0)
144 u_short port;
145 if ((port =
146 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
148 goto fooy;
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);
156 cu->cu_wait = wait;
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))
169 goto fooy;
171 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
172 if (*sockp < 0)
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;
180 goto fooy;
182 /* attempt to bind to prov port */
183 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
184 #ifdef IP_RECVERR
186 int on = 1;
187 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
189 #endif
190 cu->cu_closeit = TRUE;
192 else
194 cu->cu_closeit = FALSE;
196 cu->cu_sock = *sockp;
197 cl->cl_auth = authnone_create ();
198 return cl;
199 fooy:
200 if (cu)
201 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
202 if (cl)
203 mem_free ((caddr_t) cl, sizeof (CLIENT));
204 return (CLIENT *) NULL;
206 #ifdef EXPORT_RPC_SYMBOLS
207 libc_hidden_def (__libc_clntudp_bufcreate)
208 #else
209 libc_hidden_nolink_sunrpc (__libc_clntudp_bufcreate, GLIBC_PRIVATE)
210 #endif
212 CLIENT *
213 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
214 struct timeval wait, int *sockp, u_int sendsz,
215 u_int recvsz)
217 return __libc_clntudp_bufcreate (raddr, program, version, wait,
218 sockp, sendsz, recvsz, 0);
220 libc_hidden_nolink_sunrpc (clntudp_bufcreate, GLIBC_2_0)
222 CLIENT *
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)
231 #else
232 libc_hidden_nolink_sunrpc (clntudp_create, GLIBC_2_0)
233 #endif
235 static int
236 is_network_up (int sock)
238 struct ifaddrs *ifa;
240 if (getifaddrs (&ifa) != 0)
241 return 0;
243 struct ifaddrs *run = ifa;
244 while (run != NULL)
246 if ((run->ifa_flags & IFF_UP) != 0
247 && run->ifa_addr != NULL
248 && run->ifa_addr->sa_family == AF_INET)
249 break;
251 run = run->ifa_next;
254 freeifaddrs (ifa);
256 return run != NULL;
259 static enum clnt_stat
260 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
261 CLIENT *cl; /* client handle */
262 u_long proc; /* procedure number */
263 xdrproc_t xargs; /* xdr routine for args */
264 caddr_t argsp; /* pointer to args */
265 xdrproc_t xresults; /* xdr routine for results */
266 caddr_t resultsp; /* pointer to results */
267 struct timeval utimeout; /* seconds to wait before giving up */
269 struct cu_data *cu = (struct cu_data *) cl->cl_private;
270 XDR *xdrs;
271 int outlen = 0;
272 int inlen;
273 socklen_t fromlen;
274 struct pollfd fd;
275 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
276 (cu->cu_wait.tv_usec / 1000);
277 struct sockaddr_in from;
278 struct rpc_msg reply_msg;
279 XDR reply_xdrs;
280 struct timeval time_waited;
281 bool_t ok;
282 int nrefreshes = 2; /* number of times to refresh cred */
283 struct timeval timeout;
284 int anyup; /* any network interface up */
286 if (cu->cu_total.tv_usec == -1)
288 timeout = utimeout; /* use supplied timeout */
290 else
292 timeout = cu->cu_total; /* use default timeout */
295 time_waited.tv_sec = 0;
296 time_waited.tv_usec = 0;
297 call_again:
298 xdrs = &(cu->cu_outxdrs);
299 if (xargs == NULL)
300 goto get_reply;
301 xdrs->x_op = XDR_ENCODE;
302 XDR_SETPOS (xdrs, cu->cu_xdrpos);
304 * the transaction is the first thing in the out buffer
306 (*(uint32_t *) (cu->cu_outbuf))++;
307 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
308 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
309 (!(*xargs) (xdrs, argsp)))
310 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
311 outlen = (int) XDR_GETPOS (xdrs);
313 send_again:
314 if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
315 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
316 != outlen)
318 cu->cu_error.re_errno = errno;
319 return (cu->cu_error.re_status = RPC_CANTSEND);
323 * Hack to provide rpc-based message passing
325 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
327 return (cu->cu_error.re_status = RPC_TIMEDOUT);
329 get_reply:
331 * sub-optimal code appears here because we have
332 * some clock time to spare while the packets are in flight.
333 * (We assume that this is actually only executed once.)
335 reply_msg.acpted_rply.ar_verf = _null_auth;
336 reply_msg.acpted_rply.ar_results.where = resultsp;
337 reply_msg.acpted_rply.ar_results.proc = xresults;
338 fd.fd = cu->cu_sock;
339 fd.events = POLLIN;
340 anyup = 0;
341 for (;;)
343 switch (__poll (&fd, 1, milliseconds))
346 case 0:
347 if (anyup == 0)
349 anyup = is_network_up (cu->cu_sock);
350 if (!anyup)
351 return (cu->cu_error.re_status = RPC_CANTRECV);
354 time_waited.tv_sec += cu->cu_wait.tv_sec;
355 time_waited.tv_usec += cu->cu_wait.tv_usec;
356 while (time_waited.tv_usec >= 1000000)
358 time_waited.tv_sec++;
359 time_waited.tv_usec -= 1000000;
361 if ((time_waited.tv_sec < timeout.tv_sec) ||
362 ((time_waited.tv_sec == timeout.tv_sec) &&
363 (time_waited.tv_usec < timeout.tv_usec)))
364 goto send_again;
365 return (cu->cu_error.re_status = RPC_TIMEDOUT);
368 * buggy in other cases because time_waited is not being
369 * updated.
371 case -1:
372 if (errno == EINTR)
373 continue;
374 cu->cu_error.re_errno = errno;
375 return (cu->cu_error.re_status = RPC_CANTRECV);
377 #ifdef IP_RECVERR
378 if (fd.revents & POLLERR)
380 struct msghdr msg;
381 struct cmsghdr *cmsg;
382 struct sock_extended_err *e;
383 struct sockaddr_in err_addr;
384 struct iovec iov;
385 char *cbuf = (char *) alloca (outlen + 256);
386 int ret;
388 iov.iov_base = cbuf + 256;
389 iov.iov_len = outlen;
390 msg.msg_name = (void *) &err_addr;
391 msg.msg_namelen = sizeof (err_addr);
392 msg.msg_iov = &iov;
393 msg.msg_iovlen = 1;
394 msg.msg_flags = 0;
395 msg.msg_control = cbuf;
396 msg.msg_controllen = 256;
397 ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
398 if (ret >= 0
399 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
400 && (msg.msg_flags & MSG_ERRQUEUE)
401 && ((msg.msg_namelen == 0
402 && ret >= 12)
403 || (msg.msg_namelen == sizeof (err_addr)
404 && err_addr.sin_family == AF_INET
405 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
406 sizeof (err_addr.sin_addr)) == 0
407 && err_addr.sin_port == cu->cu_raddr.sin_port)))
408 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
409 cmsg = CMSG_NXTHDR (&msg, cmsg))
410 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
412 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
413 cu->cu_error.re_errno = e->ee_errno;
414 return (cu->cu_error.re_status = RPC_CANTRECV);
417 #endif
420 fromlen = sizeof (struct sockaddr);
421 inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf,
422 (int) cu->cu_recvsz, MSG_DONTWAIT,
423 (struct sockaddr *) &from, &fromlen);
425 while (inlen < 0 && errno == EINTR);
426 if (inlen < 0)
428 if (errno == EWOULDBLOCK)
429 continue;
430 cu->cu_error.re_errno = errno;
431 return (cu->cu_error.re_status = RPC_CANTRECV);
433 if (inlen < 4)
434 continue;
436 /* see if reply transaction id matches sent id.
437 Don't do this if we only wait for a replay */
438 if (xargs != NULL
439 && memcmp (cu->cu_inbuf, cu->cu_outbuf, sizeof (u_int32_t)) != 0)
440 continue;
441 /* we now assume we have the proper reply */
442 break;
446 * now decode and validate the response
448 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
449 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
450 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
451 if (ok)
453 _seterr_reply (&reply_msg, &(cu->cu_error));
454 if (cu->cu_error.re_status == RPC_SUCCESS)
456 if (!AUTH_VALIDATE (cl->cl_auth,
457 &reply_msg.acpted_rply.ar_verf))
459 cu->cu_error.re_status = RPC_AUTHERROR;
460 cu->cu_error.re_why = AUTH_INVALIDRESP;
462 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
464 xdrs->x_op = XDR_FREE;
465 (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
467 } /* end successful completion */
468 else
470 /* maybe our credentials need to be refreshed ... */
471 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
473 nrefreshes--;
474 goto call_again;
476 } /* end of unsuccessful completion */
477 } /* end of valid reply message */
478 else
480 cu->cu_error.re_status = RPC_CANTDECODERES;
482 return cu->cu_error.re_status;
485 static void
486 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
488 struct cu_data *cu = (struct cu_data *) cl->cl_private;
490 *errp = cu->cu_error;
494 static bool_t
495 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
497 struct cu_data *cu = (struct cu_data *) cl->cl_private;
498 XDR *xdrs = &(cu->cu_outxdrs);
500 xdrs->x_op = XDR_FREE;
501 return (*xdr_res) (xdrs, res_ptr);
504 static void
505 clntudp_abort (void)
509 static bool_t
510 clntudp_control (CLIENT *cl, int request, char *info)
512 struct cu_data *cu = (struct cu_data *) cl->cl_private;
513 u_long ul;
514 u_int32_t ui32;
516 switch (request)
518 case CLSET_FD_CLOSE:
519 cu->cu_closeit = TRUE;
520 break;
521 case CLSET_FD_NCLOSE:
522 cu->cu_closeit = FALSE;
523 break;
524 case CLSET_TIMEOUT:
525 cu->cu_total = *(struct timeval *) info;
526 break;
527 case CLGET_TIMEOUT:
528 *(struct timeval *) info = cu->cu_total;
529 break;
530 case CLSET_RETRY_TIMEOUT:
531 cu->cu_wait = *(struct timeval *) info;
532 break;
533 case CLGET_RETRY_TIMEOUT:
534 *(struct timeval *) info = cu->cu_wait;
535 break;
536 case CLGET_SERVER_ADDR:
537 *(struct sockaddr_in *) info = cu->cu_raddr;
538 break;
539 case CLGET_FD:
540 *(int *)info = cu->cu_sock;
541 break;
542 case CLGET_XID:
544 * use the knowledge that xid is the
545 * first element in the call structure *.
546 * This will get the xid of the PREVIOUS call
548 memcpy (&ui32, cu->cu_outbuf, sizeof (ui32));
549 ul = ntohl (ui32);
550 memcpy (info, &ul, sizeof (ul));
551 break;
552 case CLSET_XID:
553 /* This will set the xid of the NEXT call */
554 memcpy (&ul, info, sizeof (ul));
555 ui32 = htonl (ul - 1);
556 memcpy (cu->cu_outbuf, &ui32, sizeof (ui32));
557 /* decrement by 1 as clntudp_call() increments once */
558 break;
559 case CLGET_VERS:
561 * This RELIES on the information that, in the call body,
562 * the version number field is the fifth field from the
563 * beginning of the RPC header. MUST be changed if the
564 * call_struct is changed
566 memcpy (&ui32, cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT, sizeof (ui32));
567 ul = ntohl (ui32);
568 memcpy (info, &ul, sizeof (ul));
569 break;
570 case CLSET_VERS:
571 memcpy (&ul, info, sizeof (ul));
572 ui32 = htonl (ul);
573 memcpy (cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
574 break;
575 case CLGET_PROG:
577 * This RELIES on the information that, in the call body,
578 * the program number field is the field from the
579 * beginning of the RPC header. MUST be changed if the
580 * call_struct is changed
582 memcpy (&ui32, cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT, sizeof (ui32));
583 ul = ntohl (ui32);
584 memcpy (info, &ul, sizeof (ul));
585 break;
586 case CLSET_PROG:
587 memcpy (&ul, info, sizeof (ul));
588 ui32 = htonl (ul);
589 memcpy (cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
590 break;
591 /* The following are only possible with TI-RPC */
592 case CLGET_SVC_ADDR:
593 case CLSET_SVC_ADDR:
594 case CLSET_PUSH_TIMOD:
595 case CLSET_POP_TIMOD:
596 default:
597 return FALSE;
599 return TRUE;
602 static void
603 clntudp_destroy (CLIENT *cl)
605 struct cu_data *cu = (struct cu_data *) cl->cl_private;
607 if (cu->cu_closeit)
609 (void) __close (cu->cu_sock);
611 XDR_DESTROY (&(cu->cu_outxdrs));
612 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
613 mem_free ((caddr_t) cl, sizeof (CLIENT));