* intl/dcigettext.c (_nl_find_msg): Reread nconversions after
[glibc.git] / sunrpc / clnt_udp.c
blob634313211d25aedeba59bfa25074541048ba4086
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 const 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 (void) __fxprintf (NULL, "%s: %s",
140 "clntudp_create", _("out of memory\n"));
141 ce->cf_stat = RPC_SYSTEMERROR;
142 ce->cf_error.re_errno = ENOMEM;
143 goto fooy;
145 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
147 if (raddr->sin_port == 0)
149 u_short port;
150 if ((port =
151 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
153 goto fooy;
155 raddr->sin_port = htons (port);
157 cl->cl_ops = (struct clnt_ops *) &udp_ops;
158 cl->cl_private = (caddr_t) cu;
159 cu->cu_raddr = *raddr;
160 cu->cu_rlen = sizeof (cu->cu_raddr);
161 cu->cu_wait = wait;
162 cu->cu_total.tv_sec = -1;
163 cu->cu_total.tv_usec = -1;
164 cu->cu_sendsz = sendsz;
165 cu->cu_recvsz = recvsz;
166 call_msg.rm_xid = _create_xid ();
167 call_msg.rm_direction = CALL;
168 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
169 call_msg.rm_call.cb_prog = program;
170 call_msg.rm_call.cb_vers = version;
171 INTUSE(xdrmem_create) (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
172 if (!INTUSE(xdr_callhdr) (&(cu->cu_outxdrs), &call_msg))
174 goto fooy;
176 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
177 if (*sockp < 0)
179 int dontblock = 1;
181 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
182 if (*sockp < 0)
184 struct rpc_createerr *ce = &get_rpc_createerr ();
185 ce->cf_stat = RPC_SYSTEMERROR;
186 ce->cf_error.re_errno = errno;
187 goto fooy;
189 /* attempt to bind to prov port */
190 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
191 /* the sockets rpc controls are non-blocking */
192 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
193 #ifdef IP_RECVERR
195 int on = 1;
196 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
198 #endif
199 cu->cu_closeit = TRUE;
201 else
203 cu->cu_closeit = FALSE;
205 cu->cu_sock = *sockp;
206 cl->cl_auth = INTUSE(authnone_create) ();
207 return cl;
208 fooy:
209 if (cu)
210 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
211 if (cl)
212 mem_free ((caddr_t) cl, sizeof (CLIENT));
213 return (CLIENT *) NULL;
215 INTDEF (clntudp_bufcreate)
217 CLIENT *
218 clntudp_create (raddr, program, version, wait, sockp)
219 struct sockaddr_in *raddr;
220 u_long program;
221 u_long version;
222 struct timeval wait;
223 int *sockp;
225 return INTUSE(clntudp_bufcreate) (raddr, program, version, wait, sockp,
226 UDPMSGSIZE, UDPMSGSIZE);
228 INTDEF (clntudp_create)
230 static int
231 is_network_up (int sock)
233 struct ifaddrs *ifa;
235 if (getifaddrs (&ifa) != 0)
236 return 0;
238 struct ifaddrs *run = ifa;
239 while (run != NULL)
241 if ((run->ifa_flags & IFF_UP) != 0
242 && run->ifa_addr != NULL
243 && run->ifa_addr->sa_family == AF_INET)
244 break;
246 run = run->ifa_next;
249 freeifaddrs (ifa);
251 return run != NULL;
254 static enum clnt_stat
255 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
256 CLIENT *cl; /* client handle */
257 u_long proc; /* procedure number */
258 xdrproc_t xargs; /* xdr routine for args */
259 caddr_t argsp; /* pointer to args */
260 xdrproc_t xresults; /* xdr routine for results */
261 caddr_t resultsp; /* pointer to results */
262 struct timeval utimeout; /* seconds to wait before giving up */
264 struct cu_data *cu = (struct cu_data *) cl->cl_private;
265 XDR *xdrs;
266 int outlen = 0;
267 int inlen;
268 socklen_t fromlen;
269 struct pollfd fd;
270 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
271 (cu->cu_wait.tv_usec / 1000);
272 struct sockaddr_in from;
273 struct rpc_msg reply_msg;
274 XDR reply_xdrs;
275 struct timeval time_waited;
276 bool_t ok;
277 int nrefreshes = 2; /* number of times to refresh cred */
278 struct timeval timeout;
279 int anyup; /* any network interface up */
281 if (cu->cu_total.tv_usec == -1)
283 timeout = utimeout; /* use supplied timeout */
285 else
287 timeout = cu->cu_total; /* use default timeout */
290 time_waited.tv_sec = 0;
291 time_waited.tv_usec = 0;
292 call_again:
293 xdrs = &(cu->cu_outxdrs);
294 if (xargs == NULL)
295 goto get_reply;
296 xdrs->x_op = XDR_ENCODE;
297 XDR_SETPOS (xdrs, cu->cu_xdrpos);
299 * the transaction is the first thing in the out buffer
301 (*(uint32_t *) (cu->cu_outbuf))++;
302 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
303 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
304 (!(*xargs) (xdrs, argsp)))
305 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
306 outlen = (int) XDR_GETPOS (xdrs);
308 send_again:
309 if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
310 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
311 != outlen)
313 cu->cu_error.re_errno = errno;
314 return (cu->cu_error.re_status = RPC_CANTSEND);
318 * Hack to provide rpc-based message passing
320 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
322 return (cu->cu_error.re_status = RPC_TIMEDOUT);
324 get_reply:
326 * sub-optimal code appears here because we have
327 * some clock time to spare while the packets are in flight.
328 * (We assume that this is actually only executed once.)
330 reply_msg.acpted_rply.ar_verf = _null_auth;
331 reply_msg.acpted_rply.ar_results.where = resultsp;
332 reply_msg.acpted_rply.ar_results.proc = xresults;
333 fd.fd = cu->cu_sock;
334 fd.events = POLLIN;
335 anyup = 0;
336 for (;;)
338 switch (__poll (&fd, 1, milliseconds))
341 case 0:
342 if (anyup == 0)
344 anyup = is_network_up (cu->cu_sock);
345 if (!anyup)
346 return (cu->cu_error.re_status = RPC_CANTRECV);
349 time_waited.tv_sec += cu->cu_wait.tv_sec;
350 time_waited.tv_usec += cu->cu_wait.tv_usec;
351 while (time_waited.tv_usec >= 1000000)
353 time_waited.tv_sec++;
354 time_waited.tv_usec -= 1000000;
356 if ((time_waited.tv_sec < timeout.tv_sec) ||
357 ((time_waited.tv_sec == timeout.tv_sec) &&
358 (time_waited.tv_usec < timeout.tv_usec)))
359 goto send_again;
360 return (cu->cu_error.re_status = RPC_TIMEDOUT);
363 * buggy in other cases because time_waited is not being
364 * updated.
366 case -1:
367 if (errno == EINTR)
368 continue;
369 cu->cu_error.re_errno = errno;
370 return (cu->cu_error.re_status = RPC_CANTRECV);
372 #ifdef IP_RECVERR
373 if (fd.revents & POLLERR)
375 struct msghdr msg;
376 struct cmsghdr *cmsg;
377 struct sock_extended_err *e;
378 struct sockaddr_in err_addr;
379 struct iovec iov;
380 char *cbuf = (char *) alloca (outlen + 256);
381 int ret;
383 iov.iov_base = cbuf + 256;
384 iov.iov_len = outlen;
385 msg.msg_name = (void *) &err_addr;
386 msg.msg_namelen = sizeof (err_addr);
387 msg.msg_iov = &iov;
388 msg.msg_iovlen = 1;
389 msg.msg_flags = 0;
390 msg.msg_control = cbuf;
391 msg.msg_controllen = 256;
392 ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
393 if (ret >= 0
394 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
395 && (msg.msg_flags & MSG_ERRQUEUE)
396 && ((msg.msg_namelen == 0
397 && ret >= 12)
398 || (msg.msg_namelen == sizeof (err_addr)
399 && err_addr.sin_family == AF_INET
400 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
401 sizeof (err_addr.sin_addr)) == 0
402 && err_addr.sin_port == cu->cu_raddr.sin_port)))
403 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
404 cmsg = CMSG_NXTHDR (&msg, cmsg))
405 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
407 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
408 cu->cu_error.re_errno = e->ee_errno;
409 return (cu->cu_error.re_status = RPC_CANTRECV);
412 #endif
415 fromlen = sizeof (struct sockaddr);
416 inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf,
417 (int) cu->cu_recvsz, MSG_DONTWAIT,
418 (struct sockaddr *) &from, &fromlen);
420 while (inlen < 0 && errno == EINTR);
421 if (inlen < 0)
423 if (errno == EWOULDBLOCK)
424 continue;
425 cu->cu_error.re_errno = errno;
426 return (cu->cu_error.re_status = RPC_CANTRECV);
428 if (inlen < 4)
429 continue;
431 /* see if reply transaction id matches sent id.
432 Don't do this if we only wait for a replay */
433 if (xargs != NULL
434 && (*((u_int32_t *) (cu->cu_inbuf))
435 != *((u_int32_t *) (cu->cu_outbuf))))
436 continue;
437 /* we now assume we have the proper reply */
438 break;
442 * now decode and validate the response
444 INTUSE(xdrmem_create) (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
445 ok = INTUSE(xdr_replymsg) (&reply_xdrs, &reply_msg);
446 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
447 if (ok)
449 _seterr_reply (&reply_msg, &(cu->cu_error));
450 if (cu->cu_error.re_status == RPC_SUCCESS)
452 if (!AUTH_VALIDATE (cl->cl_auth,
453 &reply_msg.acpted_rply.ar_verf))
455 cu->cu_error.re_status = RPC_AUTHERROR;
456 cu->cu_error.re_why = AUTH_INVALIDRESP;
458 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
460 xdrs->x_op = XDR_FREE;
461 (void) INTUSE(xdr_opaque_auth) (xdrs,
462 &(reply_msg.acpted_rply.ar_verf));
464 } /* end successful completion */
465 else
467 /* maybe our credentials need to be refreshed ... */
468 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
470 nrefreshes--;
471 goto call_again;
473 } /* end of unsuccessful completion */
474 } /* end of valid reply message */
475 else
477 cu->cu_error.re_status = RPC_CANTDECODERES;
479 return cu->cu_error.re_status;
482 static void
483 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
485 struct cu_data *cu = (struct cu_data *) cl->cl_private;
487 *errp = cu->cu_error;
491 static bool_t
492 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
494 struct cu_data *cu = (struct cu_data *) cl->cl_private;
495 XDR *xdrs = &(cu->cu_outxdrs);
497 xdrs->x_op = XDR_FREE;
498 return (*xdr_res) (xdrs, res_ptr);
501 static void
502 clntudp_abort (void)
506 static bool_t
507 clntudp_control (CLIENT *cl, int request, char *info)
509 struct cu_data *cu = (struct cu_data *) cl->cl_private;
511 switch (request)
513 case CLSET_FD_CLOSE:
514 cu->cu_closeit = TRUE;
515 break;
516 case CLSET_FD_NCLOSE:
517 cu->cu_closeit = FALSE;
518 break;
519 case CLSET_TIMEOUT:
520 cu->cu_total = *(struct timeval *) info;
521 break;
522 case CLGET_TIMEOUT:
523 *(struct timeval *) info = cu->cu_total;
524 break;
525 case CLSET_RETRY_TIMEOUT:
526 cu->cu_wait = *(struct timeval *) info;
527 break;
528 case CLGET_RETRY_TIMEOUT:
529 *(struct timeval *) info = cu->cu_wait;
530 break;
531 case CLGET_SERVER_ADDR:
532 *(struct sockaddr_in *) info = cu->cu_raddr;
533 break;
534 case CLGET_FD:
535 *(int *)info = cu->cu_sock;
536 break;
537 case CLGET_XID:
539 * use the knowledge that xid is the
540 * first element in the call structure *.
541 * This will get the xid of the PREVIOUS call
543 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
544 break;
545 case CLSET_XID:
546 /* This will set the xid of the NEXT call */
547 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
548 /* decrement by 1 as clntudp_call() increments once */
549 case CLGET_VERS:
551 * This RELIES on the information that, in the call body,
552 * the version number field is the fifth field from the
553 * begining of the RPC header. MUST be changed if the
554 * call_struct is changed
556 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
557 4 * BYTES_PER_XDR_UNIT));
558 break;
559 case CLSET_VERS:
560 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
561 = htonl(*(u_long *)info);
562 break;
563 case CLGET_PROG:
565 * This RELIES on the information that, in the call body,
566 * the program number field is the field from the
567 * begining of the RPC header. MUST be changed if the
568 * call_struct is changed
570 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
571 3 * BYTES_PER_XDR_UNIT));
572 break;
573 case CLSET_PROG:
574 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
575 = htonl(*(u_long *)info);
576 break;
577 /* The following are only possible with TI-RPC */
578 case CLGET_SVC_ADDR:
579 case CLSET_SVC_ADDR:
580 case CLSET_PUSH_TIMOD:
581 case CLSET_POP_TIMOD:
582 default:
583 return FALSE;
585 return TRUE;
588 static void
589 clntudp_destroy (CLIENT *cl)
591 struct cu_data *cu = (struct cu_data *) cl->cl_private;
593 if (cu->cu_closeit)
595 (void) __close (cu->cu_sock);
597 XDR_DESTROY (&(cu->cu_outxdrs));
598 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
599 mem_free ((caddr_t) cl, sizeof (CLIENT));