* Versions: Add comment about linuxthreads' frozen ABI.
[glibc.git] / sunrpc / clnt_udp.c
blobf906173363bc974fb0a4363382397a52e92f008e
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 #ifdef USE_IN_LIBIO
54 # include <wchar.h>
55 #endif
57 #ifdef IP_RECVERR
58 #include <errqueue.h>
59 #include <sys/uio.h>
60 #endif
62 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
63 extern u_long _create_xid (void);
66 * UDP bases client side rpc operations
68 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
69 xdrproc_t, caddr_t, struct timeval);
70 static void clntudp_abort (void);
71 static void clntudp_geterr (CLIENT *, struct rpc_err *);
72 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
73 static bool_t clntudp_control (CLIENT *, int, char *);
74 static void clntudp_destroy (CLIENT *);
76 static struct clnt_ops udp_ops =
78 clntudp_call,
79 clntudp_abort,
80 clntudp_geterr,
81 clntudp_freeres,
82 clntudp_destroy,
83 clntudp_control
87 * Private data kept per client handle
89 struct cu_data
91 int cu_sock;
92 bool_t cu_closeit;
93 struct sockaddr_in cu_raddr;
94 int cu_rlen;
95 struct timeval cu_wait;
96 struct timeval cu_total;
97 struct rpc_err cu_error;
98 XDR cu_outxdrs;
99 u_int cu_xdrpos;
100 u_int cu_sendsz;
101 char *cu_outbuf;
102 u_int cu_recvsz;
103 char cu_inbuf[1];
107 * Create a UDP based client handle.
108 * If *sockp<0, *sockp is set to a newly created UPD socket.
109 * If raddr->sin_port is 0 a binder on the remote machine
110 * is consulted for the correct port number.
111 * NB: It is the clients responsibility to close *sockp.
112 * NB: The rpch->cl_auth is initialized to null authentication.
113 * Caller may wish to set this something more useful.
115 * wait is the amount of time used between retransmitting a call if
116 * no response has been heard; retransmission occurs until the actual
117 * rpc call times out.
119 * sendsz and recvsz are the maximum allowable packet sizes that can be
120 * sent and received.
122 CLIENT *
123 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
124 struct timeval wait, int *sockp, u_int sendsz,
125 u_int recvsz)
127 CLIENT *cl;
128 struct cu_data *cu = NULL;
129 struct rpc_msg call_msg;
131 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
132 sendsz = ((sendsz + 3) / 4) * 4;
133 recvsz = ((recvsz + 3) / 4) * 4;
134 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
135 if (cl == NULL || cu == NULL)
137 struct rpc_createerr *ce = &get_rpc_createerr ();
138 #ifdef USE_IN_LIBIO
139 if (_IO_fwide (stderr, 0) > 0)
140 (void) __fwprintf (stderr, L"%s",
141 _("clntudp_create: out of memory\n"));
142 else
143 #endif
144 (void) fputs (_("clntudp_create: out of memory\n"), stderr);
145 ce->cf_stat = RPC_SYSTEMERROR;
146 ce->cf_error.re_errno = ENOMEM;
147 goto fooy;
149 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
151 if (raddr->sin_port == 0)
153 u_short port;
154 if ((port =
155 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
157 goto fooy;
159 raddr->sin_port = htons (port);
161 cl->cl_ops = &udp_ops;
162 cl->cl_private = (caddr_t) cu;
163 cu->cu_raddr = *raddr;
164 cu->cu_rlen = sizeof (cu->cu_raddr);
165 cu->cu_wait = wait;
166 cu->cu_total.tv_sec = -1;
167 cu->cu_total.tv_usec = -1;
168 cu->cu_sendsz = sendsz;
169 cu->cu_recvsz = recvsz;
170 call_msg.rm_xid = _create_xid ();
171 call_msg.rm_direction = CALL;
172 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
173 call_msg.rm_call.cb_prog = program;
174 call_msg.rm_call.cb_vers = version;
175 INTUSE(xdrmem_create) (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
176 if (!INTUSE(xdr_callhdr) (&(cu->cu_outxdrs), &call_msg))
178 goto fooy;
180 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
181 if (*sockp < 0)
183 int dontblock = 1;
185 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
186 if (*sockp < 0)
188 struct rpc_createerr *ce = &get_rpc_createerr ();
189 ce->cf_stat = RPC_SYSTEMERROR;
190 ce->cf_error.re_errno = errno;
191 goto fooy;
193 /* attempt to bind to prov port */
194 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
195 /* the sockets rpc controls are non-blocking */
196 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
197 #ifdef IP_RECVERR
199 int on = 1;
200 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
202 #endif
203 cu->cu_closeit = TRUE;
205 else
207 cu->cu_closeit = FALSE;
209 cu->cu_sock = *sockp;
210 cl->cl_auth = INTUSE(authnone_create) ();
211 return cl;
212 fooy:
213 if (cu)
214 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
215 if (cl)
216 mem_free ((caddr_t) cl, sizeof (CLIENT));
217 return (CLIENT *) NULL;
219 INTDEF (clntudp_bufcreate)
221 CLIENT *
222 clntudp_create (raddr, program, version, wait, sockp)
223 struct sockaddr_in *raddr;
224 u_long program;
225 u_long version;
226 struct timeval wait;
227 int *sockp;
229 return INTUSE(clntudp_bufcreate) (raddr, program, version, wait, sockp,
230 UDPMSGSIZE, UDPMSGSIZE);
232 INTDEF (clntudp_create)
234 static int
235 is_network_up (int sock)
237 struct ifconf ifc;
238 char buf[UDPMSGSIZE];
239 struct ifreq ifreq, *ifr;
240 int n;
242 ifc.ifc_len = sizeof (buf);
243 ifc.ifc_buf = buf;
244 if (__ioctl(sock, SIOCGIFCONF, (char *) &ifc) == 0)
246 ifr = ifc.ifc_req;
247 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
249 ifreq = *ifr;
250 if (__ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
251 break;
253 if ((ifreq.ifr_flags & IFF_UP)
254 && ifr->ifr_addr.sa_family == AF_INET)
255 return 1;
258 return 0;
261 static enum clnt_stat
262 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
263 CLIENT *cl; /* client handle */
264 u_long proc; /* procedure number */
265 xdrproc_t xargs; /* xdr routine for args */
266 caddr_t argsp; /* pointer to args */
267 xdrproc_t xresults; /* xdr routine for results */
268 caddr_t resultsp; /* pointer to results */
269 struct timeval utimeout; /* seconds to wait before giving up */
271 struct cu_data *cu = (struct cu_data *) cl->cl_private;
272 XDR *xdrs;
273 int outlen = 0;
274 int inlen;
275 socklen_t fromlen;
276 struct pollfd fd;
277 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
278 (cu->cu_wait.tv_usec / 1000);
279 struct sockaddr_in from;
280 struct rpc_msg reply_msg;
281 XDR reply_xdrs;
282 struct timeval time_waited;
283 bool_t ok;
284 int nrefreshes = 2; /* number of times to refresh cred */
285 struct timeval timeout;
286 int anyup; /* any network interface up */
288 if (cu->cu_total.tv_usec == -1)
290 timeout = utimeout; /* use supplied timeout */
292 else
294 timeout = cu->cu_total; /* use default timeout */
297 time_waited.tv_sec = 0;
298 time_waited.tv_usec = 0;
299 call_again:
300 xdrs = &(cu->cu_outxdrs);
301 if (xargs == NULL)
302 goto get_reply;
303 xdrs->x_op = XDR_ENCODE;
304 XDR_SETPOS (xdrs, cu->cu_xdrpos);
306 * the transaction is the first thing in the out buffer
308 (*(uint32_t *) (cu->cu_outbuf))++;
309 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
310 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
311 (!(*xargs) (xdrs, argsp)))
312 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
313 outlen = (int) XDR_GETPOS (xdrs);
315 send_again:
316 if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
317 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
318 != outlen)
320 cu->cu_error.re_errno = errno;
321 return (cu->cu_error.re_status = RPC_CANTSEND);
325 * Hack to provide rpc-based message passing
327 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
329 return (cu->cu_error.re_status = RPC_TIMEDOUT);
331 get_reply:
333 * sub-optimal code appears here because we have
334 * some clock time to spare while the packets are in flight.
335 * (We assume that this is actually only executed once.)
337 reply_msg.acpted_rply.ar_verf = _null_auth;
338 reply_msg.acpted_rply.ar_results.where = resultsp;
339 reply_msg.acpted_rply.ar_results.proc = xresults;
340 fd.fd = cu->cu_sock;
341 fd.events = POLLIN;
342 anyup = 0;
343 for (;;)
345 switch (__poll (&fd, 1, milliseconds))
348 case 0:
349 if (anyup == 0)
351 anyup = is_network_up (cu->cu_sock);
352 if (!anyup)
353 return (cu->cu_error.re_status = RPC_CANTRECV);
356 time_waited.tv_sec += cu->cu_wait.tv_sec;
357 time_waited.tv_usec += cu->cu_wait.tv_usec;
358 while (time_waited.tv_usec >= 1000000)
360 time_waited.tv_sec++;
361 time_waited.tv_usec -= 1000000;
363 if ((time_waited.tv_sec < timeout.tv_sec) ||
364 ((time_waited.tv_sec == timeout.tv_sec) &&
365 (time_waited.tv_usec < timeout.tv_usec)))
366 goto send_again;
367 return (cu->cu_error.re_status = RPC_TIMEDOUT);
370 * buggy in other cases because time_waited is not being
371 * updated.
373 case -1:
374 if (errno == EINTR)
375 continue;
376 cu->cu_error.re_errno = errno;
377 return (cu->cu_error.re_status = RPC_CANTRECV);
379 #ifdef IP_RECVERR
380 if (fd.revents & POLLERR)
382 struct msghdr msg;
383 struct cmsghdr *cmsg;
384 struct sock_extended_err *e;
385 struct sockaddr_in err_addr;
386 struct iovec iov;
387 char *cbuf = (char *) alloca (outlen + 256);
388 int ret;
390 iov.iov_base = cbuf + 256;
391 iov.iov_len = outlen;
392 msg.msg_name = (void *) &err_addr;
393 msg.msg_namelen = sizeof (err_addr);
394 msg.msg_iov = &iov;
395 msg.msg_iovlen = 1;
396 msg.msg_flags = 0;
397 msg.msg_control = cbuf;
398 msg.msg_controllen = 256;
399 ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
400 if (ret >= 0
401 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
402 && (msg.msg_flags & MSG_ERRQUEUE)
403 && ((msg.msg_namelen == 0
404 && ret >= 12)
405 || (msg.msg_namelen == sizeof (err_addr)
406 && err_addr.sin_family == AF_INET
407 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
408 sizeof (err_addr.sin_addr)) == 0
409 && err_addr.sin_port == cu->cu_raddr.sin_port)))
410 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
411 cmsg = CMSG_NXTHDR (&msg, cmsg))
412 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
414 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
415 cu->cu_error.re_errno = e->ee_errno;
416 return (cu->cu_error.re_status = RPC_CANTRECV);
419 #endif
422 fromlen = sizeof (struct sockaddr);
423 inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf,
424 (int) cu->cu_recvsz, 0,
425 (struct sockaddr *) &from, &fromlen);
427 while (inlen < 0 && errno == EINTR);
428 if (inlen < 0)
430 if (errno == EWOULDBLOCK)
431 continue;
432 cu->cu_error.re_errno = errno;
433 return (cu->cu_error.re_status = RPC_CANTRECV);
435 if (inlen < 4)
436 continue;
438 /* see if reply transaction id matches sent id.
439 Don't do this if we only wait for a replay */
440 if (xargs != NULL
441 && (*((u_int32_t *) (cu->cu_inbuf))
442 != *((u_int32_t *) (cu->cu_outbuf))))
443 continue;
444 /* we now assume we have the proper reply */
445 break;
449 * now decode and validate the response
451 INTUSE(xdrmem_create) (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
452 ok = INTUSE(xdr_replymsg) (&reply_xdrs, &reply_msg);
453 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
454 if (ok)
456 _seterr_reply (&reply_msg, &(cu->cu_error));
457 if (cu->cu_error.re_status == RPC_SUCCESS)
459 if (!AUTH_VALIDATE (cl->cl_auth,
460 &reply_msg.acpted_rply.ar_verf))
462 cu->cu_error.re_status = RPC_AUTHERROR;
463 cu->cu_error.re_why = AUTH_INVALIDRESP;
465 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
467 xdrs->x_op = XDR_FREE;
468 (void) INTUSE(xdr_opaque_auth) (xdrs,
469 &(reply_msg.acpted_rply.ar_verf));
471 } /* end successful completion */
472 else
474 /* maybe our credentials need to be refreshed ... */
475 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
477 nrefreshes--;
478 goto call_again;
480 } /* end of unsuccessful completion */
481 } /* end of valid reply message */
482 else
484 cu->cu_error.re_status = RPC_CANTDECODERES;
486 return cu->cu_error.re_status;
489 static void
490 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
492 struct cu_data *cu = (struct cu_data *) cl->cl_private;
494 *errp = cu->cu_error;
498 static bool_t
499 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
501 struct cu_data *cu = (struct cu_data *) cl->cl_private;
502 XDR *xdrs = &(cu->cu_outxdrs);
504 xdrs->x_op = XDR_FREE;
505 return (*xdr_res) (xdrs, res_ptr);
508 static void
509 clntudp_abort (void)
513 static bool_t
514 clntudp_control (CLIENT *cl, int request, char *info)
516 struct cu_data *cu = (struct cu_data *) cl->cl_private;
518 switch (request)
520 case CLSET_FD_CLOSE:
521 cu->cu_closeit = TRUE;
522 break;
523 case CLSET_FD_NCLOSE:
524 cu->cu_closeit = FALSE;
525 break;
526 case CLSET_TIMEOUT:
527 cu->cu_total = *(struct timeval *) info;
528 break;
529 case CLGET_TIMEOUT:
530 *(struct timeval *) info = cu->cu_total;
531 break;
532 case CLSET_RETRY_TIMEOUT:
533 cu->cu_wait = *(struct timeval *) info;
534 break;
535 case CLGET_RETRY_TIMEOUT:
536 *(struct timeval *) info = cu->cu_wait;
537 break;
538 case CLGET_SERVER_ADDR:
539 *(struct sockaddr_in *) info = cu->cu_raddr;
540 break;
541 case CLGET_FD:
542 *(int *)info = cu->cu_sock;
543 break;
544 case CLGET_XID:
546 * use the knowledge that xid is the
547 * first element in the call structure *.
548 * This will get the xid of the PREVIOUS call
550 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
551 break;
552 case CLSET_XID:
553 /* This will set the xid of the NEXT call */
554 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
555 /* decrement by 1 as clntudp_call() increments once */
556 case CLGET_VERS:
558 * This RELIES on the information that, in the call body,
559 * the version number field is the fifth field from the
560 * begining of the RPC header. MUST be changed if the
561 * call_struct is changed
563 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
564 4 * BYTES_PER_XDR_UNIT));
565 break;
566 case CLSET_VERS:
567 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
568 = htonl(*(u_long *)info);
569 break;
570 case CLGET_PROG:
572 * This RELIES on the information that, in the call body,
573 * the program number field is the field from the
574 * begining of the RPC header. MUST be changed if the
575 * call_struct is changed
577 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
578 3 * BYTES_PER_XDR_UNIT));
579 break;
580 case CLSET_PROG:
581 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
582 = htonl(*(u_long *)info);
583 break;
584 /* The following are only possible with TI-RPC */
585 case CLGET_SVC_ADDR:
586 case CLSET_SVC_ADDR:
587 case CLSET_PUSH_TIMOD:
588 case CLSET_POP_TIMOD:
589 default:
590 return FALSE;
592 return TRUE;
595 static void
596 clntudp_destroy (CLIENT *cl)
598 struct cu_data *cu = (struct cu_data *) cl->cl_private;
600 if (cu->cu_closeit)
602 (void) __close (cu->cu_sock);
604 XDR_DESTROY (&(cu->cu_outxdrs));
605 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
606 mem_free ((caddr_t) cl, sizeof (CLIENT));