Update.
[glibc.git] / sunrpc / clnt_udp.c
blobf95192305287be26e4251fae32bbb75f9fe32a8f
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>
54 #ifdef IP_RECVERR
55 #include <errqueue.h>
56 #include <sys/uio.h>
57 #endif
59 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
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 struct clnt_ops udp_ops =
75 clntudp_call,
76 clntudp_abort,
77 clntudp_geterr,
78 clntudp_freeres,
79 clntudp_destroy,
80 clntudp_control
84 * Private data kept per client handle
86 struct cu_data
88 int cu_sock;
89 bool_t cu_closeit;
90 struct sockaddr_in cu_raddr;
91 int cu_rlen;
92 struct timeval cu_wait;
93 struct timeval cu_total;
94 struct rpc_err cu_error;
95 XDR cu_outxdrs;
96 u_int cu_xdrpos;
97 u_int cu_sendsz;
98 char *cu_outbuf;
99 u_int cu_recvsz;
100 char cu_inbuf[1];
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
117 * sent and received.
119 CLIENT *
120 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
121 struct timeval wait, int *sockp, u_int sendsz,
122 u_int recvsz)
124 CLIENT *cl;
125 struct cu_data *cu = NULL;
126 struct rpc_msg call_msg;
128 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
129 if (cl == NULL)
131 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
132 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
133 rpc_createerr.cf_error.re_errno = errno;
134 goto fooy;
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 (cu == NULL)
141 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
142 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
143 rpc_createerr.cf_error.re_errno = errno;
144 goto fooy;
146 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
148 if (raddr->sin_port == 0)
150 u_short port;
151 if ((port =
152 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
154 goto fooy;
156 raddr->sin_port = htons (port);
158 cl->cl_ops = &udp_ops;
159 cl->cl_private = (caddr_t) cu;
160 cu->cu_raddr = *raddr;
161 cu->cu_rlen = sizeof (cu->cu_raddr);
162 cu->cu_wait = wait;
163 cu->cu_total.tv_sec = -1;
164 cu->cu_total.tv_usec = -1;
165 cu->cu_sendsz = sendsz;
166 cu->cu_recvsz = recvsz;
167 call_msg.rm_xid = _create_xid ();
168 call_msg.rm_direction = CALL;
169 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
170 call_msg.rm_call.cb_prog = program;
171 call_msg.rm_call.cb_vers = version;
172 xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf,
173 sendsz, XDR_ENCODE);
174 if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
176 goto fooy;
178 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
179 if (*sockp < 0)
181 int dontblock = 1;
183 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
184 if (*sockp < 0)
186 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
187 rpc_createerr.cf_error.re_errno = errno;
188 goto fooy;
190 /* attempt to bind to prov port */
191 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
192 /* the sockets rpc controls are non-blocking */
193 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
194 #ifdef IP_RECVERR
196 int on = 1;
197 setsockopt(*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
199 #endif
200 cu->cu_closeit = TRUE;
202 else
204 cu->cu_closeit = FALSE;
206 cu->cu_sock = *sockp;
207 cl->cl_auth = authnone_create ();
208 return cl;
209 fooy:
210 if (cu)
211 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
212 if (cl)
213 mem_free ((caddr_t) cl, sizeof (CLIENT));
214 return (CLIENT *) NULL;
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;
226 return clntudp_bufcreate (raddr, program, version, wait, sockp,
227 UDPMSGSIZE, UDPMSGSIZE);
230 static int
231 is_network_up (int sock)
233 struct ifconf ifc;
234 char buf[UDPMSGSIZE];
235 struct ifreq ifreq, *ifr;
236 int n;
238 ifc.ifc_len = sizeof (buf);
239 ifc.ifc_buf = buf;
240 if (__ioctl(sock, SIOCGIFCONF, (char *) &ifc) == 0)
242 ifr = ifc.ifc_req;
243 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
245 ifreq = *ifr;
246 if (__ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
247 break;
249 if ((ifreq.ifr_flags & IFF_UP)
250 && ifr->ifr_addr.sa_family == AF_INET)
251 return 1;
254 return 0;
257 static enum clnt_stat
258 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
259 CLIENT *cl; /* client handle */
260 u_long proc; /* procedure number */
261 xdrproc_t xargs; /* xdr routine for args */
262 caddr_t argsp; /* pointer to args */
263 xdrproc_t xresults; /* xdr routine for results */
264 caddr_t resultsp; /* pointer to results */
265 struct timeval utimeout; /* seconds to wait before giving up */
267 struct cu_data *cu = (struct cu_data *) cl->cl_private;
268 XDR *xdrs;
269 int outlen = 0;
270 int inlen;
271 socklen_t fromlen;
272 struct pollfd fd;
273 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
274 (cu->cu_wait.tv_usec / 1000);
275 struct sockaddr_in from;
276 struct rpc_msg reply_msg;
277 XDR reply_xdrs;
278 struct timeval time_waited;
279 bool_t ok;
280 int nrefreshes = 2; /* number of times to refresh cred */
281 struct timeval timeout;
282 int anyup; /* any network interface up */
284 if (cu->cu_total.tv_usec == -1)
286 timeout = utimeout; /* use supplied timeout */
288 else
290 timeout = cu->cu_total; /* use default timeout */
293 time_waited.tv_sec = 0;
294 time_waited.tv_usec = 0;
295 call_again:
296 xdrs = &(cu->cu_outxdrs);
297 if (xargs == NULL)
298 goto get_reply;
299 xdrs->x_op = XDR_ENCODE;
300 XDR_SETPOS (xdrs, cu->cu_xdrpos);
302 * the transaction is the first thing in the out buffer
304 (*(uint32_t *) (cu->cu_outbuf))++;
305 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
306 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
307 (!(*xargs) (xdrs, argsp)))
308 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
309 outlen = (int) XDR_GETPOS (xdrs);
311 send_again:
312 if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
313 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
314 != outlen)
316 cu->cu_error.re_errno = errno;
317 return (cu->cu_error.re_status = RPC_CANTSEND);
321 * Hack to provide rpc-based message passing
323 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
325 return (cu->cu_error.re_status = RPC_TIMEDOUT);
327 get_reply:
329 * sub-optimal code appears here because we have
330 * some clock time to spare while the packets are in flight.
331 * (We assume that this is actually only executed once.)
333 reply_msg.acpted_rply.ar_verf = _null_auth;
334 reply_msg.acpted_rply.ar_results.where = resultsp;
335 reply_msg.acpted_rply.ar_results.proc = xresults;
336 fd.fd = cu->cu_sock;
337 fd.events = POLLIN;
338 anyup = 0;
339 for (;;)
341 switch (__poll (&fd, 1, milliseconds))
344 case 0:
345 if (anyup == 0)
347 anyup = is_network_up (cu->cu_sock);
348 if (!anyup)
349 return (cu->cu_error.re_status = RPC_CANTRECV);
352 time_waited.tv_sec += cu->cu_wait.tv_sec;
353 time_waited.tv_usec += cu->cu_wait.tv_usec;
354 while (time_waited.tv_usec >= 1000000)
356 time_waited.tv_sec++;
357 time_waited.tv_usec -= 1000000;
359 if ((time_waited.tv_sec < timeout.tv_sec) ||
360 ((time_waited.tv_sec == timeout.tv_sec) &&
361 (time_waited.tv_usec < timeout.tv_usec)))
362 goto send_again;
363 return (cu->cu_error.re_status = RPC_TIMEDOUT);
366 * buggy in other cases because time_waited is not being
367 * updated.
369 case -1:
370 if (errno == EINTR)
371 continue;
372 cu->cu_error.re_errno = errno;
373 return (cu->cu_error.re_status = RPC_CANTRECV);
375 #ifdef IP_RECVERR
376 if (fd.revents & POLLERR)
378 struct msghdr msg;
379 struct cmsghdr *cmsg;
380 struct sock_extended_err *e;
381 struct sockaddr_in err_addr;
382 struct iovec iov;
383 char *cbuf = (char *) alloca (outlen + 256);
384 int ret;
386 iov.iov_base = cbuf + 256;
387 iov.iov_len = outlen;
388 msg.msg_name = (void *) &err_addr;
389 msg.msg_namelen = sizeof (err_addr);
390 msg.msg_iov = &iov;
391 msg.msg_iovlen = 1;
392 msg.msg_flags = 0;
393 msg.msg_control = cbuf;
394 msg.msg_controllen = 256;
395 ret = recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
396 if (ret >= 0
397 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
398 && (msg.msg_flags & MSG_ERRQUEUE)
399 && ((msg.msg_namelen == 0
400 && ret >= 12)
401 || (msg.msg_namelen == sizeof (err_addr)
402 && err_addr.sin_family == AF_INET
403 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
404 sizeof (err_addr.sin_addr)) == 0
405 && err_addr.sin_port == cu->cu_raddr.sin_port)))
406 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
407 cmsg = CMSG_NXTHDR (&msg, cmsg))
408 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
410 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
411 cu->cu_error.re_errno = e->ee_errno;
412 return (cu->cu_error.re_status = RPC_CANTRECV);
415 #endif
418 fromlen = sizeof (struct sockaddr);
419 inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
420 (int) cu->cu_recvsz, 0,
421 (struct sockaddr *) &from, &fromlen);
423 while (inlen < 0 && errno == EINTR);
424 if (inlen < 0)
426 if (errno == EWOULDBLOCK)
427 continue;
428 cu->cu_error.re_errno = errno;
429 return (cu->cu_error.re_status = RPC_CANTRECV);
431 if (inlen < 4)
432 continue;
434 /* see if reply transaction id matches sent id.
435 Don't do this if we only wait for a replay */
436 if (xargs != NULL
437 && (*((u_int32_t *) (cu->cu_inbuf))
438 != *((u_int32_t *) (cu->cu_outbuf))))
439 continue;
440 /* we now assume we have the proper reply */
441 break;
445 * now decode and validate the response
447 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
448 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
449 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
450 if (ok)
452 _seterr_reply (&reply_msg, &(cu->cu_error));
453 if (cu->cu_error.re_status == RPC_SUCCESS)
455 if (!AUTH_VALIDATE (cl->cl_auth,
456 &reply_msg.acpted_rply.ar_verf))
458 cu->cu_error.re_status = RPC_AUTHERROR;
459 cu->cu_error.re_why = AUTH_INVALIDRESP;
461 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
463 xdrs->x_op = XDR_FREE;
464 (void) xdr_opaque_auth (xdrs,
465 &(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;
514 switch (request)
516 case CLSET_FD_CLOSE:
517 cu->cu_closeit = TRUE;
518 break;
519 case CLSET_FD_NCLOSE:
520 cu->cu_closeit = FALSE;
521 break;
522 case CLSET_TIMEOUT:
523 cu->cu_total = *(struct timeval *) info;
524 break;
525 case CLGET_TIMEOUT:
526 *(struct timeval *) info = cu->cu_total;
527 break;
528 case CLSET_RETRY_TIMEOUT:
529 cu->cu_wait = *(struct timeval *) info;
530 break;
531 case CLGET_RETRY_TIMEOUT:
532 *(struct timeval *) info = cu->cu_wait;
533 break;
534 case CLGET_SERVER_ADDR:
535 *(struct sockaddr_in *) info = cu->cu_raddr;
536 break;
537 case CLGET_FD:
538 *(int *)info = cu->cu_sock;
539 break;
540 case CLGET_XID:
542 * use the knowledge that xid is the
543 * first element in the call structure *.
544 * This will get the xid of the PREVIOUS call
546 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
547 break;
548 case CLSET_XID:
549 /* This will set the xid of the NEXT call */
550 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
551 /* decrement by 1 as clntudp_call() increments once */
552 case CLGET_VERS:
554 * This RELIES on the information that, in the call body,
555 * the version number field is the fifth field from the
556 * begining of the RPC header. MUST be changed if the
557 * call_struct is changed
559 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
560 4 * BYTES_PER_XDR_UNIT));
561 break;
562 case CLSET_VERS:
563 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
564 = htonl(*(u_long *)info);
565 break;
566 case CLGET_PROG:
568 * This RELIES on the information that, in the call body,
569 * the program number field is the field from the
570 * begining of the RPC header. MUST be changed if the
571 * call_struct is changed
573 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
574 3 * BYTES_PER_XDR_UNIT));
575 break;
576 case CLSET_PROG:
577 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
578 = htonl(*(u_long *)info);
579 break;
580 /* The following are only possible with TI-RPC */
581 case CLGET_SVC_ADDR:
582 case CLSET_SVC_ADDR:
583 case CLSET_PUSH_TIMOD:
584 case CLSET_POP_TIMOD:
585 default:
586 return FALSE;
588 return TRUE;
591 static void
592 clntudp_destroy (CLIENT *cl)
594 struct cu_data *cu = (struct cu_data *) cl->cl_private;
596 if (cu->cu_closeit)
598 (void) __close (cu->cu_sock);
600 XDR_DESTROY (&(cu->cu_outxdrs));
601 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
602 mem_free ((caddr_t) cl, sizeof (CLIENT));