Update.
[glibc.git] / sunrpc / clnt_udp.c
blob0733206c9b62b837736fcce0e20a219a0467494f
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 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
55 extern u_long _create_xid (void);
58 * UDP bases client side rpc operations
60 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
61 xdrproc_t, caddr_t, struct timeval);
62 static void clntudp_abort (void);
63 static void clntudp_geterr (CLIENT *, struct rpc_err *);
64 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
65 static bool_t clntudp_control (CLIENT *, int, char *);
66 static void clntudp_destroy (CLIENT *);
68 static struct clnt_ops udp_ops =
70 clntudp_call,
71 clntudp_abort,
72 clntudp_geterr,
73 clntudp_freeres,
74 clntudp_destroy,
75 clntudp_control
79 * Private data kept per client handle
81 struct cu_data
83 int cu_sock;
84 bool_t cu_closeit;
85 struct sockaddr_in cu_raddr;
86 int cu_rlen;
87 struct timeval cu_wait;
88 struct timeval cu_total;
89 struct rpc_err cu_error;
90 XDR cu_outxdrs;
91 u_int cu_xdrpos;
92 u_int cu_sendsz;
93 char *cu_outbuf;
94 u_int cu_recvsz;
95 char cu_inbuf[1];
99 * Create a UDP based client handle.
100 * If *sockp<0, *sockp is set to a newly created UPD socket.
101 * If raddr->sin_port is 0 a binder on the remote machine
102 * is consulted for the correct port number.
103 * NB: It is the clients responsibility to close *sockp.
104 * NB: The rpch->cl_auth is initialized to null authentication.
105 * Caller may wish to set this something more useful.
107 * wait is the amount of time used between retransmitting a call if
108 * no response has been heard; retransmission occurs until the actual
109 * rpc call times out.
111 * sendsz and recvsz are the maximum allowable packet sizes that can be
112 * sent and received.
114 CLIENT *
115 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
116 struct timeval wait, int *sockp, u_int sendsz,
117 u_int recvsz)
119 CLIENT *cl;
120 struct cu_data *cu = NULL;
121 struct rpc_msg call_msg;
123 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
124 if (cl == NULL)
126 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
127 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
128 rpc_createerr.cf_error.re_errno = errno;
129 goto fooy;
131 sendsz = ((sendsz + 3) / 4) * 4;
132 recvsz = ((recvsz + 3) / 4) * 4;
133 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
134 if (cu == NULL)
136 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
137 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
138 rpc_createerr.cf_error.re_errno = errno;
139 goto fooy;
141 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
143 if (raddr->sin_port == 0)
145 u_short port;
146 if ((port =
147 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
149 goto fooy;
151 raddr->sin_port = htons (port);
153 cl->cl_ops = &udp_ops;
154 cl->cl_private = (caddr_t) cu;
155 cu->cu_raddr = *raddr;
156 cu->cu_rlen = sizeof (cu->cu_raddr);
157 cu->cu_wait = wait;
158 cu->cu_total.tv_sec = -1;
159 cu->cu_total.tv_usec = -1;
160 cu->cu_sendsz = sendsz;
161 cu->cu_recvsz = recvsz;
162 call_msg.rm_xid = _create_xid ();
163 call_msg.rm_direction = CALL;
164 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
165 call_msg.rm_call.cb_prog = program;
166 call_msg.rm_call.cb_vers = version;
167 xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf,
168 sendsz, XDR_ENCODE);
169 if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
171 goto fooy;
173 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
174 if (*sockp < 0)
176 int dontblock = 1;
178 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
179 if (*sockp < 0)
181 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
182 rpc_createerr.cf_error.re_errno = errno;
183 goto fooy;
185 /* attempt to bind to prov port */
186 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
187 /* the sockets rpc controls are non-blocking */
188 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
189 cu->cu_closeit = TRUE;
191 else
193 cu->cu_closeit = FALSE;
195 cu->cu_sock = *sockp;
196 cl->cl_auth = authnone_create ();
197 return cl;
198 fooy:
199 if (cu)
200 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
201 if (cl)
202 mem_free ((caddr_t) cl, sizeof (CLIENT));
203 return (CLIENT *) NULL;
206 CLIENT *
207 clntudp_create (raddr, program, version, wait, sockp)
208 struct sockaddr_in *raddr;
209 u_long program;
210 u_long version;
211 struct timeval wait;
212 int *sockp;
215 return clntudp_bufcreate (raddr, program, version, wait, sockp,
216 UDPMSGSIZE, UDPMSGSIZE);
219 static int
220 is_network_up (int sock)
222 struct ifconf ifc;
223 char buf[UDPMSGSIZE];
224 struct ifreq ifreq, *ifr;
225 int n;
227 ifc.ifc_len = sizeof (buf);
228 ifc.ifc_buf = buf;
229 if (__ioctl(sock, SIOCGIFCONF, (char *) &ifc) == 0)
231 ifr = ifc.ifc_req;
232 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
234 ifreq = *ifr;
235 if (__ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
236 break;
238 if ((ifreq.ifr_flags & IFF_UP)
239 && ifr->ifr_addr.sa_family == AF_INET)
240 return 1;
243 return 0;
246 static enum clnt_stat
247 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
248 CLIENT *cl; /* client handle */
249 u_long proc; /* procedure number */
250 xdrproc_t xargs; /* xdr routine for args */
251 caddr_t argsp; /* pointer to args */
252 xdrproc_t xresults; /* xdr routine for results */
253 caddr_t resultsp; /* pointer to results */
254 struct timeval utimeout; /* seconds to wait before giving up */
256 struct cu_data *cu = (struct cu_data *) cl->cl_private;
257 XDR *xdrs;
258 int outlen = 0;
259 int inlen;
260 socklen_t fromlen;
261 struct pollfd fd;
262 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
263 (cu->cu_wait.tv_usec / 1000);
264 struct sockaddr_in from;
265 struct rpc_msg reply_msg;
266 XDR reply_xdrs;
267 struct timeval time_waited;
268 bool_t ok;
269 int nrefreshes = 2; /* number of times to refresh cred */
270 struct timeval timeout;
271 int anyup; /* any network interface up */
273 if (cu->cu_total.tv_usec == -1)
275 timeout = utimeout; /* use supplied timeout */
277 else
279 timeout = cu->cu_total; /* use default timeout */
282 time_waited.tv_sec = 0;
283 time_waited.tv_usec = 0;
284 call_again:
285 xdrs = &(cu->cu_outxdrs);
286 if (xargs == NULL)
287 goto get_reply;
288 xdrs->x_op = XDR_ENCODE;
289 XDR_SETPOS (xdrs, cu->cu_xdrpos);
291 * the transaction is the first thing in the out buffer
293 (*(uint32_t *) (cu->cu_outbuf))++;
294 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
295 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
296 (!(*xargs) (xdrs, argsp)))
297 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
298 outlen = (int) XDR_GETPOS (xdrs);
300 send_again:
301 if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
302 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
303 != outlen)
305 cu->cu_error.re_errno = errno;
306 return (cu->cu_error.re_status = RPC_CANTSEND);
310 * Hack to provide rpc-based message passing
312 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
314 return (cu->cu_error.re_status = RPC_TIMEDOUT);
316 get_reply:
318 * sub-optimal code appears here because we have
319 * some clock time to spare while the packets are in flight.
320 * (We assume that this is actually only executed once.)
322 reply_msg.acpted_rply.ar_verf = _null_auth;
323 reply_msg.acpted_rply.ar_results.where = resultsp;
324 reply_msg.acpted_rply.ar_results.proc = xresults;
325 fd.fd = cu->cu_sock;
326 fd.events = POLLIN;
327 anyup = 0;
328 for (;;)
330 switch (__poll (&fd, 1, milliseconds))
333 case 0:
334 if (anyup == 0)
336 anyup = is_network_up (cu->cu_sock);
337 if (!anyup)
338 return (cu->cu_error.re_status = RPC_CANTRECV);
341 time_waited.tv_sec += cu->cu_wait.tv_sec;
342 time_waited.tv_usec += cu->cu_wait.tv_usec;
343 while (time_waited.tv_usec >= 1000000)
345 time_waited.tv_sec++;
346 time_waited.tv_usec -= 1000000;
348 if ((time_waited.tv_sec < timeout.tv_sec) ||
349 ((time_waited.tv_sec == timeout.tv_sec) &&
350 (time_waited.tv_usec < timeout.tv_usec)))
351 goto send_again;
352 return (cu->cu_error.re_status = RPC_TIMEDOUT);
355 * buggy in other cases because time_waited is not being
356 * updated.
358 case -1:
359 if (errno == EINTR)
360 continue;
361 cu->cu_error.re_errno = errno;
362 return (cu->cu_error.re_status = RPC_CANTRECV);
366 fromlen = sizeof (struct sockaddr);
367 inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
368 (int) cu->cu_recvsz, 0,
369 (struct sockaddr *) &from, &fromlen);
371 while (inlen < 0 && errno == EINTR);
372 if (inlen < 0)
374 if (errno == EWOULDBLOCK)
375 continue;
376 cu->cu_error.re_errno = errno;
377 return (cu->cu_error.re_status = RPC_CANTRECV);
379 if (inlen < 4)
380 continue;
382 /* see if reply transaction id matches sent id.
383 Don't do this if we only wait for a replay */
384 if (xargs != NULL
385 && (*((u_int32_t *) (cu->cu_inbuf))
386 != *((u_int32_t *) (cu->cu_outbuf))))
387 continue;
388 /* we now assume we have the proper reply */
389 break;
393 * now decode and validate the response
395 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
396 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
397 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
398 if (ok)
400 _seterr_reply (&reply_msg, &(cu->cu_error));
401 if (cu->cu_error.re_status == RPC_SUCCESS)
403 if (!AUTH_VALIDATE (cl->cl_auth,
404 &reply_msg.acpted_rply.ar_verf))
406 cu->cu_error.re_status = RPC_AUTHERROR;
407 cu->cu_error.re_why = AUTH_INVALIDRESP;
409 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
411 xdrs->x_op = XDR_FREE;
412 (void) xdr_opaque_auth (xdrs,
413 &(reply_msg.acpted_rply.ar_verf));
415 } /* end successful completion */
416 else
418 /* maybe our credentials need to be refreshed ... */
419 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
421 nrefreshes--;
422 goto call_again;
424 } /* end of unsuccessful completion */
425 } /* end of valid reply message */
426 else
428 cu->cu_error.re_status = RPC_CANTDECODERES;
430 return cu->cu_error.re_status;
433 static void
434 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
436 struct cu_data *cu = (struct cu_data *) cl->cl_private;
438 *errp = cu->cu_error;
442 static bool_t
443 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
445 struct cu_data *cu = (struct cu_data *) cl->cl_private;
446 XDR *xdrs = &(cu->cu_outxdrs);
448 xdrs->x_op = XDR_FREE;
449 return (*xdr_res) (xdrs, res_ptr);
452 static void
453 clntudp_abort (void)
457 static bool_t
458 clntudp_control (CLIENT *cl, int request, char *info)
460 struct cu_data *cu = (struct cu_data *) cl->cl_private;
462 switch (request)
464 case CLSET_FD_CLOSE:
465 cu->cu_closeit = TRUE;
466 break;
467 case CLSET_FD_NCLOSE:
468 cu->cu_closeit = FALSE;
469 break;
470 case CLSET_TIMEOUT:
471 cu->cu_total = *(struct timeval *) info;
472 break;
473 case CLGET_TIMEOUT:
474 *(struct timeval *) info = cu->cu_total;
475 break;
476 case CLSET_RETRY_TIMEOUT:
477 cu->cu_wait = *(struct timeval *) info;
478 break;
479 case CLGET_RETRY_TIMEOUT:
480 *(struct timeval *) info = cu->cu_wait;
481 break;
482 case CLGET_SERVER_ADDR:
483 *(struct sockaddr_in *) info = cu->cu_raddr;
484 break;
485 case CLGET_FD:
486 *(int *)info = cu->cu_sock;
487 break;
488 case CLGET_XID:
490 * use the knowledge that xid is the
491 * first element in the call structure *.
492 * This will get the xid of the PREVIOUS call
494 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
495 break;
496 case CLSET_XID:
497 /* This will set the xid of the NEXT call */
498 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
499 /* decrement by 1 as clntudp_call() increments once */
500 case CLGET_VERS:
502 * This RELIES on the information that, in the call body,
503 * the version number field is the fifth field from the
504 * begining of the RPC header. MUST be changed if the
505 * call_struct is changed
507 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
508 4 * BYTES_PER_XDR_UNIT));
509 break;
510 case CLSET_VERS:
511 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
512 = htonl(*(u_long *)info);
513 break;
514 case CLGET_PROG:
516 * This RELIES on the information that, in the call body,
517 * the program number field is the field from the
518 * begining of the RPC header. MUST be changed if the
519 * call_struct is changed
521 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
522 3 * BYTES_PER_XDR_UNIT));
523 break;
524 case CLSET_PROG:
525 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
526 = htonl(*(u_long *)info);
527 break;
528 /* The following are only possible with TI-RPC */
529 case CLGET_SVC_ADDR:
530 case CLSET_SVC_ADDR:
531 case CLSET_PUSH_TIMOD:
532 case CLSET_POP_TIMOD:
533 default:
534 return FALSE;
536 return TRUE;
539 static void
540 clntudp_destroy (CLIENT *cl)
542 struct cu_data *cu = (struct cu_data *) cl->cl_private;
544 if (cu->cu_closeit)
546 (void) __close (cu->cu_sock);
548 XDR_DESTROY (&(cu->cu_outxdrs));
549 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
550 mem_free ((caddr_t) cl, sizeof (CLIENT));