Update.
[glibc.git] / sunrpc / clnt_udp.c
blobe19e7e820b47ccc288aa0c57ec6869451183091f
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 <rpc/rpc.h>
43 #include <rpc/xdr.h>
44 #include <rpc/clnt.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <netdb.h>
48 #include <errno.h>
49 #include <rpc/pmap_clnt.h>
51 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
54 * UDP bases client side rpc operations
56 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
57 xdrproc_t, caddr_t, struct timeval);
58 static void clntudp_abort (void);
59 static void clntudp_geterr (CLIENT *, struct rpc_err *);
60 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
61 static bool_t clntudp_control (CLIENT *, int, char *);
62 static void clntudp_destroy (CLIENT *);
64 static struct clnt_ops udp_ops =
66 clntudp_call,
67 clntudp_abort,
68 clntudp_geterr,
69 clntudp_freeres,
70 clntudp_destroy,
71 clntudp_control
75 * Private data kept per client handle
77 struct cu_data
79 int cu_sock;
80 bool_t cu_closeit;
81 struct sockaddr_in cu_raddr;
82 int cu_rlen;
83 struct timeval cu_wait;
84 struct timeval cu_total;
85 struct rpc_err cu_error;
86 XDR cu_outxdrs;
87 u_int cu_xdrpos;
88 u_int cu_sendsz;
89 char *cu_outbuf;
90 u_int cu_recvsz;
91 char cu_inbuf[1];
95 * Create a UDP based client handle.
96 * If *sockp<0, *sockp is set to a newly created UPD socket.
97 * If raddr->sin_port is 0 a binder on the remote machine
98 * is consulted for the correct port number.
99 * NB: It is the clients responsibility to close *sockp.
100 * NB: The rpch->cl_auth is initialized to null authentication.
101 * Caller may wish to set this something more useful.
103 * wait is the amount of time used between retransmitting a call if
104 * no response has been heard; retransmission occurs until the actual
105 * rpc call times out.
107 * sendsz and recvsz are the maximum allowable packet sizes that can be
108 * sent and received.
110 CLIENT *
111 clntudp_bufcreate (raddr, program, version, wait, sockp, sendsz, recvsz)
112 struct sockaddr_in *raddr;
113 u_long program;
114 u_long version;
115 struct timeval wait;
116 int *sockp;
117 u_int sendsz;
118 u_int recvsz;
120 CLIENT *cl;
121 struct cu_data *cu = NULL;
122 struct timeval now;
123 struct rpc_msg call_msg;
125 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
126 if (cl == NULL)
128 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
129 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130 rpc_createerr.cf_error.re_errno = errno;
131 goto fooy;
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 (cu == NULL)
138 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
139 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
140 rpc_createerr.cf_error.re_errno = errno;
141 goto fooy;
143 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
145 (void) gettimeofday (&now, (struct timezone *) 0);
146 if (raddr->sin_port == 0)
148 u_short port;
149 if ((port =
150 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
152 goto fooy;
154 raddr->sin_port = htons (port);
156 cl->cl_ops = &udp_ops;
157 cl->cl_private = (caddr_t) cu;
158 cu->cu_raddr = *raddr;
159 cu->cu_rlen = sizeof (cu->cu_raddr);
160 cu->cu_wait = wait;
161 cu->cu_total.tv_sec = -1;
162 cu->cu_total.tv_usec = -1;
163 cu->cu_sendsz = sendsz;
164 cu->cu_recvsz = recvsz;
165 call_msg.rm_xid = getpid () ^ now.tv_sec ^ now.tv_usec;
166 call_msg.rm_direction = CALL;
167 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
168 call_msg.rm_call.cb_prog = program;
169 call_msg.rm_call.cb_vers = version;
170 xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf,
171 sendsz, XDR_ENCODE);
172 if (!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 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
185 rpc_createerr.cf_error.re_errno = errno;
186 goto fooy;
188 /* attempt to bind to prov port */
189 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
190 /* the sockets rpc controls are non-blocking */
191 (void) ioctl (*sockp, FIONBIO, (char *) &dontblock);
192 cu->cu_closeit = TRUE;
194 else
196 cu->cu_closeit = FALSE;
198 cu->cu_sock = *sockp;
199 cl->cl_auth = authnone_create ();
200 return cl;
201 fooy:
202 if (cu)
203 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
204 if (cl)
205 mem_free ((caddr_t) cl, sizeof (CLIENT));
206 return (CLIENT *) NULL;
209 CLIENT *
210 clntudp_create (raddr, program, version, wait, sockp)
211 struct sockaddr_in *raddr;
212 u_long program;
213 u_long version;
214 struct timeval wait;
215 int *sockp;
218 return clntudp_bufcreate (raddr, program, version, wait, sockp,
219 UDPMSGSIZE, UDPMSGSIZE);
222 static enum clnt_stat
223 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
224 CLIENT *cl; /* client handle */
225 u_long proc; /* procedure number */
226 xdrproc_t xargs; /* xdr routine for args */
227 caddr_t argsp; /* pointer to args */
228 xdrproc_t xresults; /* xdr routine for results */
229 caddr_t resultsp; /* pointer to results */
230 struct timeval utimeout; /* seconds to wait before giving up */
232 struct cu_data *cu = (struct cu_data *) cl->cl_private;
233 XDR *xdrs;
234 int outlen;
235 int inlen;
236 size_t fromlen;
237 #ifdef FD_SETSIZE
238 fd_set readfds;
239 fd_set mask;
240 #else
241 int readfds;
242 int mask;
243 #endif /* def FD_SETSIZE */
244 struct sockaddr_in from;
245 struct rpc_msg reply_msg;
246 XDR reply_xdrs;
247 struct timeval time_waited;
248 bool_t ok;
249 int nrefreshes = 2; /* number of times to refresh cred */
250 struct timeval timeout;
252 if (cu->cu_total.tv_usec == -1)
254 timeout = utimeout; /* use supplied timeout */
256 else
258 timeout = cu->cu_total; /* use default timeout */
261 time_waited.tv_sec = 0;
262 time_waited.tv_usec = 0;
263 call_again:
264 xdrs = &(cu->cu_outxdrs);
265 xdrs->x_op = XDR_ENCODE;
266 XDR_SETPOS (xdrs, cu->cu_xdrpos);
268 * the transaction is the first thing in the out buffer
270 (*(u_short *) (cu->cu_outbuf))++;
271 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
272 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
273 (!(*xargs) (xdrs, argsp)))
274 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
275 outlen = (int) XDR_GETPOS (xdrs);
277 send_again:
278 if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
279 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
280 != outlen)
282 cu->cu_error.re_errno = errno;
283 return (cu->cu_error.re_status = RPC_CANTSEND);
287 * Hack to provide rpc-based message passing
289 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
291 return (cu->cu_error.re_status = RPC_TIMEDOUT);
294 * sub-optimal code appears here because we have
295 * some clock time to spare while the packets are in flight.
296 * (We assume that this is actually only executed once.)
298 reply_msg.acpted_rply.ar_verf = _null_auth;
299 reply_msg.acpted_rply.ar_results.where = resultsp;
300 reply_msg.acpted_rply.ar_results.proc = xresults;
301 #ifdef FD_SETSIZE
302 FD_ZERO (&mask);
303 FD_SET (cu->cu_sock, &mask);
304 #else
305 mask = 1 << cu->cu_sock;
306 #endif /* def FD_SETSIZE */
307 for (;;)
309 struct timeval timeout = cu->cu_wait;
310 readfds = mask;
311 switch (select (_rpc_dtablesize (), &readfds, (fd_set*) NULL,
312 (fd_set*) NULL, &timeout))
315 case 0:
316 time_waited.tv_sec += cu->cu_wait.tv_sec;
317 time_waited.tv_usec += cu->cu_wait.tv_usec;
318 while (time_waited.tv_usec >= 1000000)
320 time_waited.tv_sec++;
321 time_waited.tv_usec -= 1000000;
323 if ((time_waited.tv_sec < timeout.tv_sec) ||
324 ((time_waited.tv_sec == timeout.tv_sec) &&
325 (time_waited.tv_usec < timeout.tv_usec)))
326 goto send_again;
327 return (cu->cu_error.re_status = RPC_TIMEDOUT);
330 * buggy in other cases because time_waited is not being
331 * updated.
333 case -1:
334 if (errno == EINTR)
335 continue;
336 cu->cu_error.re_errno = errno;
337 return (cu->cu_error.re_status = RPC_CANTRECV);
341 fromlen = sizeof (struct sockaddr);
342 inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
343 (int) cu->cu_recvsz, 0,
344 (struct sockaddr *) &from, &fromlen);
346 while (inlen < 0 && errno == EINTR);
347 if (inlen < 0)
349 if (errno == EWOULDBLOCK)
350 continue;
351 cu->cu_error.re_errno = errno;
352 return (cu->cu_error.re_status = RPC_CANTRECV);
354 if (inlen < 4)
355 continue;
356 /* see if reply transaction id matches sent id */
357 if (*((u_int32_t *) (cu->cu_inbuf)) != *((u_int32_t *) (cu->cu_outbuf)))
358 continue;
359 /* we now assume we have the proper reply */
360 break;
364 * now decode and validate the response
366 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
367 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
368 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
369 if (ok)
371 _seterr_reply (&reply_msg, &(cu->cu_error));
372 if (cu->cu_error.re_status == RPC_SUCCESS)
374 if (!AUTH_VALIDATE (cl->cl_auth,
375 &reply_msg.acpted_rply.ar_verf))
377 cu->cu_error.re_status = RPC_AUTHERROR;
378 cu->cu_error.re_why = AUTH_INVALIDRESP;
380 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
382 xdrs->x_op = XDR_FREE;
383 (void) xdr_opaque_auth (xdrs,
384 &(reply_msg.acpted_rply.ar_verf));
386 } /* end successful completion */
387 else
389 /* maybe our credentials need to be refreshed ... */
390 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
392 nrefreshes--;
393 goto call_again;
395 } /* end of unsuccessful completion */
396 } /* end of valid reply message */
397 else
399 cu->cu_error.re_status = RPC_CANTDECODERES;
401 return cu->cu_error.re_status;
404 static void
405 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
407 struct cu_data *cu = (struct cu_data *) cl->cl_private;
409 *errp = cu->cu_error;
413 static bool_t
414 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
416 struct cu_data *cu = (struct cu_data *) cl->cl_private;
417 XDR *xdrs = &(cu->cu_outxdrs);
419 xdrs->x_op = XDR_FREE;
420 return (*xdr_res) (xdrs, res_ptr);
423 static void
424 clntudp_abort (void)
428 static bool_t
429 clntudp_control (CLIENT *cl, int request, char *info)
431 struct cu_data *cu = (struct cu_data *) cl->cl_private;
433 switch (request)
435 case CLSET_TIMEOUT:
436 cu->cu_total = *(struct timeval *) info;
437 break;
438 case CLGET_TIMEOUT:
439 *(struct timeval *) info = cu->cu_total;
440 break;
441 case CLSET_RETRY_TIMEOUT:
442 cu->cu_wait = *(struct timeval *) info;
443 break;
444 case CLGET_RETRY_TIMEOUT:
445 *(struct timeval *) info = cu->cu_wait;
446 break;
447 case CLGET_SERVER_ADDR:
448 *(struct sockaddr_in *) info = cu->cu_raddr;
449 break;
450 default:
451 return FALSE;
453 return TRUE;
456 static void
457 clntudp_destroy (CLIENT *cl)
459 struct cu_data *cu = (struct cu_data *) cl->cl_private;
461 if (cu->cu_closeit)
463 (void) close (cu->cu_sock);
465 XDR_DESTROY (&(cu->cu_outxdrs));
466 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
467 mem_free ((caddr_t) cl, sizeof (CLIENT));