(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sunrpc / clnt_udp.c
blobf3787dd1a5f8718e8b075f8978e28b3c9ea6ae6b
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
58 #ifdef IP_RECVERR
59 #include <errqueue.h>
60 #include <sys/uio.h>
61 #endif
63 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
64 extern u_long _create_xid (void);
67 * UDP bases client side rpc operations
69 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
70 xdrproc_t, caddr_t, struct timeval);
71 static void clntudp_abort (void);
72 static void clntudp_geterr (CLIENT *, struct rpc_err *);
73 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
74 static bool_t clntudp_control (CLIENT *, int, char *);
75 static void clntudp_destroy (CLIENT *);
77 static struct clnt_ops udp_ops =
79 clntudp_call,
80 clntudp_abort,
81 clntudp_geterr,
82 clntudp_freeres,
83 clntudp_destroy,
84 clntudp_control
88 * Private data kept per client handle
90 struct cu_data
92 int cu_sock;
93 bool_t cu_closeit;
94 struct sockaddr_in cu_raddr;
95 int cu_rlen;
96 struct timeval cu_wait;
97 struct timeval cu_total;
98 struct rpc_err cu_error;
99 XDR cu_outxdrs;
100 u_int cu_xdrpos;
101 u_int cu_sendsz;
102 char *cu_outbuf;
103 u_int cu_recvsz;
104 char cu_inbuf[1];
108 * Create a UDP based client handle.
109 * If *sockp<0, *sockp is set to a newly created UPD socket.
110 * If raddr->sin_port is 0 a binder on the remote machine
111 * is consulted for the correct port number.
112 * NB: It is the clients responsibility to close *sockp.
113 * NB: The rpch->cl_auth is initialized to null authentication.
114 * Caller may wish to set this something more useful.
116 * wait is the amount of time used between retransmitting a call if
117 * no response has been heard; retransmission occurs until the actual
118 * rpc call times out.
120 * sendsz and recvsz are the maximum allowable packet sizes that can be
121 * sent and received.
123 CLIENT *
124 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
125 struct timeval wait, int *sockp, u_int sendsz,
126 u_int recvsz)
128 CLIENT *cl;
129 struct cu_data *cu = NULL;
130 struct rpc_msg call_msg;
132 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
133 sendsz = ((sendsz + 3) / 4) * 4;
134 recvsz = ((recvsz + 3) / 4) * 4;
135 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
136 if (cl == NULL || cu == NULL)
138 struct rpc_createerr *ce = &get_rpc_createerr ();
139 #ifdef USE_IN_LIBIO
140 if (_IO_fwide (stderr, 0) > 0)
141 (void) __fwprintf (stderr, L"%s",
142 _("clntudp_create: out of memory\n"));
143 else
144 #endif
145 (void) fputs (_("clntudp_create: out of memory\n"), stderr);
146 ce->cf_stat = RPC_SYSTEMERROR;
147 ce->cf_error.re_errno = ENOMEM;
148 goto fooy;
150 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
152 if (raddr->sin_port == 0)
154 u_short port;
155 if ((port =
156 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
158 goto fooy;
160 raddr->sin_port = htons (port);
162 cl->cl_ops = &udp_ops;
163 cl->cl_private = (caddr_t) cu;
164 cu->cu_raddr = *raddr;
165 cu->cu_rlen = sizeof (cu->cu_raddr);
166 cu->cu_wait = wait;
167 cu->cu_total.tv_sec = -1;
168 cu->cu_total.tv_usec = -1;
169 cu->cu_sendsz = sendsz;
170 cu->cu_recvsz = recvsz;
171 call_msg.rm_xid = _create_xid ();
172 call_msg.rm_direction = CALL;
173 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
174 call_msg.rm_call.cb_prog = program;
175 call_msg.rm_call.cb_vers = version;
176 INTUSE(xdrmem_create) (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
177 if (!INTUSE(xdr_callhdr) (&(cu->cu_outxdrs), &call_msg))
179 goto fooy;
181 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
182 if (*sockp < 0)
184 int dontblock = 1;
186 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
187 if (*sockp < 0)
189 struct rpc_createerr *ce = &get_rpc_createerr ();
190 ce->cf_stat = RPC_SYSTEMERROR;
191 ce->cf_error.re_errno = errno;
192 goto fooy;
194 /* attempt to bind to prov port */
195 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
196 /* the sockets rpc controls are non-blocking */
197 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
198 #ifdef IP_RECVERR
200 int on = 1;
201 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
203 #endif
204 cu->cu_closeit = TRUE;
206 else
208 cu->cu_closeit = FALSE;
210 cu->cu_sock = *sockp;
211 cl->cl_auth = INTUSE(authnone_create) ();
212 return cl;
213 fooy:
214 if (cu)
215 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
216 if (cl)
217 mem_free ((caddr_t) cl, sizeof (CLIENT));
218 return (CLIENT *) NULL;
220 INTDEF (clntudp_bufcreate)
222 CLIENT *
223 clntudp_create (raddr, program, version, wait, sockp)
224 struct sockaddr_in *raddr;
225 u_long program;
226 u_long version;
227 struct timeval wait;
228 int *sockp;
230 return INTUSE(clntudp_bufcreate) (raddr, program, version, wait, sockp,
231 UDPMSGSIZE, UDPMSGSIZE);
233 INTDEF (clntudp_create)
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, 0,
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 && (*((u_int32_t *) (cu->cu_inbuf))
440 != *((u_int32_t *) (cu->cu_outbuf))))
441 continue;
442 /* we now assume we have the proper reply */
443 break;
447 * now decode and validate the response
449 INTUSE(xdrmem_create) (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
450 ok = INTUSE(xdr_replymsg) (&reply_xdrs, &reply_msg);
451 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
452 if (ok)
454 _seterr_reply (&reply_msg, &(cu->cu_error));
455 if (cu->cu_error.re_status == RPC_SUCCESS)
457 if (!AUTH_VALIDATE (cl->cl_auth,
458 &reply_msg.acpted_rply.ar_verf))
460 cu->cu_error.re_status = RPC_AUTHERROR;
461 cu->cu_error.re_why = AUTH_INVALIDRESP;
463 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
465 xdrs->x_op = XDR_FREE;
466 (void) INTUSE(xdr_opaque_auth) (xdrs,
467 &(reply_msg.acpted_rply.ar_verf));
469 } /* end successful completion */
470 else
472 /* maybe our credentials need to be refreshed ... */
473 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
475 nrefreshes--;
476 goto call_again;
478 } /* end of unsuccessful completion */
479 } /* end of valid reply message */
480 else
482 cu->cu_error.re_status = RPC_CANTDECODERES;
484 return cu->cu_error.re_status;
487 static void
488 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
490 struct cu_data *cu = (struct cu_data *) cl->cl_private;
492 *errp = cu->cu_error;
496 static bool_t
497 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
499 struct cu_data *cu = (struct cu_data *) cl->cl_private;
500 XDR *xdrs = &(cu->cu_outxdrs);
502 xdrs->x_op = XDR_FREE;
503 return (*xdr_res) (xdrs, res_ptr);
506 static void
507 clntudp_abort (void)
511 static bool_t
512 clntudp_control (CLIENT *cl, int request, char *info)
514 struct cu_data *cu = (struct cu_data *) cl->cl_private;
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 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
549 break;
550 case CLSET_XID:
551 /* This will set the xid of the NEXT call */
552 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
553 /* decrement by 1 as clntudp_call() increments once */
554 case CLGET_VERS:
556 * This RELIES on the information that, in the call body,
557 * the version number field is the fifth field from the
558 * begining of the RPC header. MUST be changed if the
559 * call_struct is changed
561 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
562 4 * BYTES_PER_XDR_UNIT));
563 break;
564 case CLSET_VERS:
565 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
566 = htonl(*(u_long *)info);
567 break;
568 case CLGET_PROG:
570 * This RELIES on the information that, in the call body,
571 * the program number field is the field from the
572 * begining of the RPC header. MUST be changed if the
573 * call_struct is changed
575 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
576 3 * BYTES_PER_XDR_UNIT));
577 break;
578 case CLSET_PROG:
579 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
580 = htonl(*(u_long *)info);
581 break;
582 /* The following are only possible with TI-RPC */
583 case CLGET_SVC_ADDR:
584 case CLSET_SVC_ADDR:
585 case CLSET_PUSH_TIMOD:
586 case CLSET_POP_TIMOD:
587 default:
588 return FALSE;
590 return TRUE;
593 static void
594 clntudp_destroy (CLIENT *cl)
596 struct cu_data *cu = (struct cu_data *) cl->cl_private;
598 if (cu->cu_closeit)
600 (void) __close (cu->cu_sock);
602 XDR_DESTROY (&(cu->cu_outxdrs));
603 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
604 mem_free ((caddr_t) cl, sizeof (CLIENT));