Updated to fedora-glibc-20080728T2320
[glibc.git] / sunrpc / clnt_udp.c
blob548c987e6977f2c38c957a674e4c47c2b86e3bda
1 /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
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.
27 * 2550 Garcia Avenue
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";
32 #endif
35 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <libintl.h>
43 #include <rpc/rpc.h>
44 #include <rpc/xdr.h>
45 #include <rpc/clnt.h>
46 #include <sys/poll.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <netdb.h>
50 #include <errno.h>
51 #include <rpc/pmap_clnt.h>
52 #include <net/if.h>
53 #include <ifaddrs.h>
54 #ifdef USE_IN_LIBIO
55 # include <wchar.h>
56 #endif
57 #include <fcntl.h>
59 #ifdef IP_RECVERR
60 #include <errqueue.h>
61 #include <sys/uio.h>
62 #endif
64 #include <kernel-features.h>
66 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
67 extern u_long _create_xid (void);
70 * UDP bases client side rpc operations
72 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
73 xdrproc_t, caddr_t, struct timeval);
74 static void clntudp_abort (void);
75 static void clntudp_geterr (CLIENT *, struct rpc_err *);
76 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
77 static bool_t clntudp_control (CLIENT *, int, char *);
78 static void clntudp_destroy (CLIENT *);
80 static const struct clnt_ops udp_ops =
82 clntudp_call,
83 clntudp_abort,
84 clntudp_geterr,
85 clntudp_freeres,
86 clntudp_destroy,
87 clntudp_control
91 * Private data kept per client handle
93 struct cu_data
95 int cu_sock;
96 bool_t cu_closeit;
97 struct sockaddr_in cu_raddr;
98 int cu_rlen;
99 struct timeval cu_wait;
100 struct timeval cu_total;
101 struct rpc_err cu_error;
102 XDR cu_outxdrs;
103 u_int cu_xdrpos;
104 u_int cu_sendsz;
105 char *cu_outbuf;
106 u_int cu_recvsz;
107 char cu_inbuf[1];
111 * Create a UDP based client handle.
112 * If *sockp<0, *sockp is set to a newly created UPD socket.
113 * If raddr->sin_port is 0 a binder on the remote machine
114 * is consulted for the correct port number.
115 * NB: It is the clients responsibility to close *sockp.
116 * NB: The rpch->cl_auth is initialized to null authentication.
117 * Caller may wish to set this something more useful.
119 * wait is the amount of time used between retransmitting a call if
120 * no response has been heard; retransmission occurs until the actual
121 * rpc call times out.
123 * sendsz and recvsz are the maximum allowable packet sizes that can be
124 * sent and received.
126 CLIENT *
127 __libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program,
128 u_long version, struct timeval wait, int *sockp,
129 u_int sendsz, u_int recvsz, int flags)
131 CLIENT *cl;
132 struct cu_data *cu = NULL;
133 struct rpc_msg call_msg;
135 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
136 sendsz = ((sendsz + 3) / 4) * 4;
137 recvsz = ((recvsz + 3) / 4) * 4;
138 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
139 if (cl == NULL || cu == NULL)
141 struct rpc_createerr *ce = &get_rpc_createerr ();
142 (void) __fxprintf (NULL, "%s: %s",
143 "clntudp_create", _("out of memory\n"));
144 ce->cf_stat = RPC_SYSTEMERROR;
145 ce->cf_error.re_errno = ENOMEM;
146 goto fooy;
148 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
150 if (raddr->sin_port == 0)
152 u_short port;
153 if ((port =
154 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
156 goto fooy;
158 raddr->sin_port = htons (port);
160 cl->cl_ops = (struct clnt_ops *) &udp_ops;
161 cl->cl_private = (caddr_t) cu;
162 cu->cu_raddr = *raddr;
163 cu->cu_rlen = sizeof (cu->cu_raddr);
164 cu->cu_wait = wait;
165 cu->cu_total.tv_sec = -1;
166 cu->cu_total.tv_usec = -1;
167 cu->cu_sendsz = sendsz;
168 cu->cu_recvsz = recvsz;
169 call_msg.rm_xid = _create_xid ();
170 call_msg.rm_direction = CALL;
171 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
172 call_msg.rm_call.cb_prog = program;
173 call_msg.rm_call.cb_vers = version;
174 INTUSE(xdrmem_create) (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
175 if (!INTUSE(xdr_callhdr) (&(cu->cu_outxdrs), &call_msg))
177 goto fooy;
179 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
180 if (*sockp < 0)
182 int dontblock = 1;
184 #ifdef SOCK_NONBLOCK
185 # ifndef __ASSUME_SOCK_CLOEXEC
186 if (__have_sock_cloexec >= 0)
187 # endif
189 *sockp = __socket (AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|flags,
190 IPPROTO_UDP);
191 # ifndef __ASSUME_SOCK_CLOEXEC
192 if (__have_sock_cloexec == 0)
193 __have_sock_cloexec = *sockp >= 0 || errno != EINVAL ? 1 : -1;
194 # endif
196 #endif
197 #ifndef __ASSUME_SOCK_CLOEXEC
198 # ifdef SOCK_CLOEXEC
199 if (__have_sock_cloexec < 0)
200 # endif
202 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
203 # ifdef SOCK_CLOEXEC
204 if (flags & SOCK_CLOEXEC)
205 __fcntl (*sockp, F_SETFD, FD_CLOEXEC);
206 # endif
208 #endif
209 if (__builtin_expect (*sockp < 0, 0))
211 struct rpc_createerr *ce = &get_rpc_createerr ();
212 ce->cf_stat = RPC_SYSTEMERROR;
213 ce->cf_error.re_errno = errno;
214 goto fooy;
216 /* attempt to bind to prov port */
217 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
218 #ifndef __ASSUME_SOCK_CLOEXEC
219 # ifdef SOCK_CLOEXEC
220 if (__have_sock_cloexec < 0)
221 # endif
222 /* the sockets rpc controls are non-blocking */
223 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
224 #endif
225 #ifdef IP_RECVERR
227 int on = 1;
228 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
230 #endif
231 cu->cu_closeit = TRUE;
233 else
235 cu->cu_closeit = FALSE;
237 cu->cu_sock = *sockp;
238 cl->cl_auth = INTUSE(authnone_create) ();
239 return cl;
240 fooy:
241 if (cu)
242 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
243 if (cl)
244 mem_free ((caddr_t) cl, sizeof (CLIENT));
245 return (CLIENT *) NULL;
247 INTDEF (__libc_clntudp_bufcreate)
249 CLIENT *
250 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
251 struct timeval wait, int *sockp, u_int sendsz,
252 u_int recvsz)
254 return INTUSE(__libc_clntudp_bufcreate) (raddr, program, version, wait,
255 sockp, sendsz, recvsz, 0);
257 INTDEF (clntudp_bufcreate)
259 CLIENT *
260 clntudp_create (raddr, program, version, wait, sockp)
261 struct sockaddr_in *raddr;
262 u_long program;
263 u_long version;
264 struct timeval wait;
265 int *sockp;
267 return INTUSE(__libc_clntudp_bufcreate) (raddr, program, version, wait,
268 sockp, UDPMSGSIZE, UDPMSGSIZE, 0);
270 INTDEF (clntudp_create)
272 static int
273 is_network_up (int sock)
275 struct ifaddrs *ifa;
277 if (getifaddrs (&ifa) != 0)
278 return 0;
280 struct ifaddrs *run = ifa;
281 while (run != NULL)
283 if ((run->ifa_flags & IFF_UP) != 0
284 && run->ifa_addr != NULL
285 && run->ifa_addr->sa_family == AF_INET)
286 break;
288 run = run->ifa_next;
291 freeifaddrs (ifa);
293 return run != NULL;
296 static enum clnt_stat
297 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
298 CLIENT *cl; /* client handle */
299 u_long proc; /* procedure number */
300 xdrproc_t xargs; /* xdr routine for args */
301 caddr_t argsp; /* pointer to args */
302 xdrproc_t xresults; /* xdr routine for results */
303 caddr_t resultsp; /* pointer to results */
304 struct timeval utimeout; /* seconds to wait before giving up */
306 struct cu_data *cu = (struct cu_data *) cl->cl_private;
307 XDR *xdrs;
308 int outlen = 0;
309 int inlen;
310 socklen_t fromlen;
311 struct pollfd fd;
312 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
313 (cu->cu_wait.tv_usec / 1000);
314 struct sockaddr_in from;
315 struct rpc_msg reply_msg;
316 XDR reply_xdrs;
317 struct timeval time_waited;
318 bool_t ok;
319 int nrefreshes = 2; /* number of times to refresh cred */
320 struct timeval timeout;
321 int anyup; /* any network interface up */
323 if (cu->cu_total.tv_usec == -1)
325 timeout = utimeout; /* use supplied timeout */
327 else
329 timeout = cu->cu_total; /* use default timeout */
332 time_waited.tv_sec = 0;
333 time_waited.tv_usec = 0;
334 call_again:
335 xdrs = &(cu->cu_outxdrs);
336 if (xargs == NULL)
337 goto get_reply;
338 xdrs->x_op = XDR_ENCODE;
339 XDR_SETPOS (xdrs, cu->cu_xdrpos);
341 * the transaction is the first thing in the out buffer
343 (*(uint32_t *) (cu->cu_outbuf))++;
344 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
345 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
346 (!(*xargs) (xdrs, argsp)))
347 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
348 outlen = (int) XDR_GETPOS (xdrs);
350 send_again:
351 if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
352 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
353 != outlen)
355 cu->cu_error.re_errno = errno;
356 return (cu->cu_error.re_status = RPC_CANTSEND);
360 * Hack to provide rpc-based message passing
362 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
364 return (cu->cu_error.re_status = RPC_TIMEDOUT);
366 get_reply:
368 * sub-optimal code appears here because we have
369 * some clock time to spare while the packets are in flight.
370 * (We assume that this is actually only executed once.)
372 reply_msg.acpted_rply.ar_verf = _null_auth;
373 reply_msg.acpted_rply.ar_results.where = resultsp;
374 reply_msg.acpted_rply.ar_results.proc = xresults;
375 fd.fd = cu->cu_sock;
376 fd.events = POLLIN;
377 anyup = 0;
378 for (;;)
380 switch (__poll (&fd, 1, milliseconds))
383 case 0:
384 if (anyup == 0)
386 anyup = is_network_up (cu->cu_sock);
387 if (!anyup)
388 return (cu->cu_error.re_status = RPC_CANTRECV);
391 time_waited.tv_sec += cu->cu_wait.tv_sec;
392 time_waited.tv_usec += cu->cu_wait.tv_usec;
393 while (time_waited.tv_usec >= 1000000)
395 time_waited.tv_sec++;
396 time_waited.tv_usec -= 1000000;
398 if ((time_waited.tv_sec < timeout.tv_sec) ||
399 ((time_waited.tv_sec == timeout.tv_sec) &&
400 (time_waited.tv_usec < timeout.tv_usec)))
401 goto send_again;
402 return (cu->cu_error.re_status = RPC_TIMEDOUT);
405 * buggy in other cases because time_waited is not being
406 * updated.
408 case -1:
409 if (errno == EINTR)
410 continue;
411 cu->cu_error.re_errno = errno;
412 return (cu->cu_error.re_status = RPC_CANTRECV);
414 #ifdef IP_RECVERR
415 if (fd.revents & POLLERR)
417 struct msghdr msg;
418 struct cmsghdr *cmsg;
419 struct sock_extended_err *e;
420 struct sockaddr_in err_addr;
421 struct iovec iov;
422 char *cbuf = (char *) alloca (outlen + 256);
423 int ret;
425 iov.iov_base = cbuf + 256;
426 iov.iov_len = outlen;
427 msg.msg_name = (void *) &err_addr;
428 msg.msg_namelen = sizeof (err_addr);
429 msg.msg_iov = &iov;
430 msg.msg_iovlen = 1;
431 msg.msg_flags = 0;
432 msg.msg_control = cbuf;
433 msg.msg_controllen = 256;
434 ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
435 if (ret >= 0
436 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
437 && (msg.msg_flags & MSG_ERRQUEUE)
438 && ((msg.msg_namelen == 0
439 && ret >= 12)
440 || (msg.msg_namelen == sizeof (err_addr)
441 && err_addr.sin_family == AF_INET
442 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
443 sizeof (err_addr.sin_addr)) == 0
444 && err_addr.sin_port == cu->cu_raddr.sin_port)))
445 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
446 cmsg = CMSG_NXTHDR (&msg, cmsg))
447 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
449 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
450 cu->cu_error.re_errno = e->ee_errno;
451 return (cu->cu_error.re_status = RPC_CANTRECV);
454 #endif
457 fromlen = sizeof (struct sockaddr);
458 inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf,
459 (int) cu->cu_recvsz, MSG_DONTWAIT,
460 (struct sockaddr *) &from, &fromlen);
462 while (inlen < 0 && errno == EINTR);
463 if (inlen < 0)
465 if (errno == EWOULDBLOCK)
466 continue;
467 cu->cu_error.re_errno = errno;
468 return (cu->cu_error.re_status = RPC_CANTRECV);
470 if (inlen < 4)
471 continue;
473 /* see if reply transaction id matches sent id.
474 Don't do this if we only wait for a replay */
475 if (xargs != NULL
476 && (*((u_int32_t *) (cu->cu_inbuf))
477 != *((u_int32_t *) (cu->cu_outbuf))))
478 continue;
479 /* we now assume we have the proper reply */
480 break;
484 * now decode and validate the response
486 INTUSE(xdrmem_create) (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
487 ok = INTUSE(xdr_replymsg) (&reply_xdrs, &reply_msg);
488 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
489 if (ok)
491 _seterr_reply (&reply_msg, &(cu->cu_error));
492 if (cu->cu_error.re_status == RPC_SUCCESS)
494 if (!AUTH_VALIDATE (cl->cl_auth,
495 &reply_msg.acpted_rply.ar_verf))
497 cu->cu_error.re_status = RPC_AUTHERROR;
498 cu->cu_error.re_why = AUTH_INVALIDRESP;
500 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
502 xdrs->x_op = XDR_FREE;
503 (void) INTUSE(xdr_opaque_auth) (xdrs,
504 &(reply_msg.acpted_rply.ar_verf));
506 } /* end successful completion */
507 else
509 /* maybe our credentials need to be refreshed ... */
510 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
512 nrefreshes--;
513 goto call_again;
515 } /* end of unsuccessful completion */
516 } /* end of valid reply message */
517 else
519 cu->cu_error.re_status = RPC_CANTDECODERES;
521 return cu->cu_error.re_status;
524 static void
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;
533 static bool_t
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);
543 static void
544 clntudp_abort (void)
548 static bool_t
549 clntudp_control (CLIENT *cl, int request, char *info)
551 struct cu_data *cu = (struct cu_data *) cl->cl_private;
553 switch (request)
555 case CLSET_FD_CLOSE:
556 cu->cu_closeit = TRUE;
557 break;
558 case CLSET_FD_NCLOSE:
559 cu->cu_closeit = FALSE;
560 break;
561 case CLSET_TIMEOUT:
562 cu->cu_total = *(struct timeval *) info;
563 break;
564 case CLGET_TIMEOUT:
565 *(struct timeval *) info = cu->cu_total;
566 break;
567 case CLSET_RETRY_TIMEOUT:
568 cu->cu_wait = *(struct timeval *) info;
569 break;
570 case CLGET_RETRY_TIMEOUT:
571 *(struct timeval *) info = cu->cu_wait;
572 break;
573 case CLGET_SERVER_ADDR:
574 *(struct sockaddr_in *) info = cu->cu_raddr;
575 break;
576 case CLGET_FD:
577 *(int *)info = cu->cu_sock;
578 break;
579 case CLGET_XID:
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);
586 break;
587 case CLSET_XID:
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 */
591 case CLGET_VERS:
593 * This RELIES on the information that, in the call body,
594 * the version number field is the fifth field from the
595 * begining of the RPC header. MUST be changed if the
596 * call_struct is changed
598 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
599 4 * BYTES_PER_XDR_UNIT));
600 break;
601 case CLSET_VERS:
602 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
603 = htonl(*(u_long *)info);
604 break;
605 case CLGET_PROG:
607 * This RELIES on the information that, in the call body,
608 * the program number field is the field from the
609 * begining of the RPC header. MUST be changed if the
610 * call_struct is changed
612 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
613 3 * BYTES_PER_XDR_UNIT));
614 break;
615 case CLSET_PROG:
616 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
617 = htonl(*(u_long *)info);
618 break;
619 /* The following are only possible with TI-RPC */
620 case CLGET_SVC_ADDR:
621 case CLSET_SVC_ADDR:
622 case CLSET_PUSH_TIMOD:
623 case CLSET_POP_TIMOD:
624 default:
625 return FALSE;
627 return TRUE;
630 static void
631 clntudp_destroy (CLIENT *cl)
633 struct cu_data *cu = (struct cu_data *) cl->cl_private;
635 if (cu->cu_closeit)
637 (void) __close (cu->cu_sock);
639 XDR_DESTROY (&(cu->cu_outxdrs));
640 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
641 mem_free ((caddr_t) cl, sizeof (CLIENT));