Update.
[glibc.git] / sunrpc / clnt_udp.c
blob0842e5ac1a4e9f49cc7c9e7fad2aa3810c6c4181
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/poll.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <netdb.h>
49 #include <errno.h>
50 #include <rpc/pmap_clnt.h>
52 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
55 * UDP bases client side rpc operations
57 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
58 xdrproc_t, caddr_t, struct timeval);
59 static void clntudp_abort (void);
60 static void clntudp_geterr (CLIENT *, struct rpc_err *);
61 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
62 static bool_t clntudp_control (CLIENT *, int, char *);
63 static void clntudp_destroy (CLIENT *);
65 static struct clnt_ops udp_ops =
67 clntudp_call,
68 clntudp_abort,
69 clntudp_geterr,
70 clntudp_freeres,
71 clntudp_destroy,
72 clntudp_control
76 * Private data kept per client handle
78 struct cu_data
80 int cu_sock;
81 bool_t cu_closeit;
82 struct sockaddr_in cu_raddr;
83 int cu_rlen;
84 struct timeval cu_wait;
85 struct timeval cu_total;
86 struct rpc_err cu_error;
87 XDR cu_outxdrs;
88 u_int cu_xdrpos;
89 u_int cu_sendsz;
90 char *cu_outbuf;
91 u_int cu_recvsz;
92 char cu_inbuf[1];
96 * Create a UDP based client handle.
97 * If *sockp<0, *sockp is set to a newly created UPD socket.
98 * If raddr->sin_port is 0 a binder on the remote machine
99 * is consulted for the correct port number.
100 * NB: It is the clients responsibility to close *sockp.
101 * NB: The rpch->cl_auth is initialized to null authentication.
102 * Caller may wish to set this something more useful.
104 * wait is the amount of time used between retransmitting a call if
105 * no response has been heard; retransmission occurs until the actual
106 * rpc call times out.
108 * sendsz and recvsz are the maximum allowable packet sizes that can be
109 * sent and received.
111 CLIENT *
112 clntudp_bufcreate (raddr, program, version, wait, sockp, sendsz, recvsz)
113 struct sockaddr_in *raddr;
114 u_long program;
115 u_long version;
116 struct timeval wait;
117 int *sockp;
118 u_int sendsz;
119 u_int recvsz;
121 CLIENT *cl;
122 struct cu_data *cu = NULL;
123 struct timeval now;
124 struct rpc_msg call_msg;
126 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
127 if (cl == NULL)
129 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
130 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
131 rpc_createerr.cf_error.re_errno = errno;
132 goto fooy;
134 sendsz = ((sendsz + 3) / 4) * 4;
135 recvsz = ((recvsz + 3) / 4) * 4;
136 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
137 if (cu == NULL)
139 (void) fprintf (stderr, _("clntudp_create: out of memory\n"));
140 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
141 rpc_createerr.cf_error.re_errno = errno;
142 goto fooy;
144 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
146 (void) __gettimeofday (&now, (struct timezone *) 0);
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 = &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 = __getpid () ^ now.tv_sec ^ now.tv_usec;
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 xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf,
172 sendsz, XDR_ENCODE);
173 if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
175 goto fooy;
177 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
178 if (*sockp < 0)
180 int dontblock = 1;
182 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
183 if (*sockp < 0)
185 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
186 rpc_createerr.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 cu->cu_closeit = TRUE;
195 else
197 cu->cu_closeit = FALSE;
199 cu->cu_sock = *sockp;
200 cl->cl_auth = authnone_create ();
201 return cl;
202 fooy:
203 if (cu)
204 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
205 if (cl)
206 mem_free ((caddr_t) cl, sizeof (CLIENT));
207 return (CLIENT *) NULL;
210 CLIENT *
211 clntudp_create (raddr, program, version, wait, sockp)
212 struct sockaddr_in *raddr;
213 u_long program;
214 u_long version;
215 struct timeval wait;
216 int *sockp;
219 return clntudp_bufcreate (raddr, program, version, wait, sockp,
220 UDPMSGSIZE, UDPMSGSIZE);
223 static enum clnt_stat
224 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
225 CLIENT *cl; /* client handle */
226 u_long proc; /* procedure number */
227 xdrproc_t xargs; /* xdr routine for args */
228 caddr_t argsp; /* pointer to args */
229 xdrproc_t xresults; /* xdr routine for results */
230 caddr_t resultsp; /* pointer to results */
231 struct timeval utimeout; /* seconds to wait before giving up */
233 struct cu_data *cu = (struct cu_data *) cl->cl_private;
234 XDR *xdrs;
235 int outlen = 0;
236 int inlen;
237 socklen_t fromlen;
238 struct pollfd fd;
239 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
240 (cu->cu_wait.tv_usec / 1000);
241 struct sockaddr_in from;
242 struct rpc_msg reply_msg;
243 XDR reply_xdrs;
244 struct timeval time_waited;
245 bool_t ok;
246 int nrefreshes = 2; /* number of times to refresh cred */
247 struct timeval timeout;
249 if (cu->cu_total.tv_usec == -1)
251 timeout = utimeout; /* use supplied timeout */
253 else
255 timeout = cu->cu_total; /* use default timeout */
258 time_waited.tv_sec = 0;
259 time_waited.tv_usec = 0;
260 call_again:
261 xdrs = &(cu->cu_outxdrs);
262 if (xargs == NULL)
263 goto get_reply;
264 xdrs->x_op = XDR_ENCODE;
265 XDR_SETPOS (xdrs, cu->cu_xdrpos);
267 * the transaction is the first thing in the out buffer
269 (*(u_short *) (cu->cu_outbuf))++;
270 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
271 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
272 (!(*xargs) (xdrs, argsp)))
273 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
274 outlen = (int) XDR_GETPOS (xdrs);
276 send_again:
277 if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
278 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
279 != outlen)
281 cu->cu_error.re_errno = errno;
282 return (cu->cu_error.re_status = RPC_CANTSEND);
286 * Hack to provide rpc-based message passing
288 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
290 return (cu->cu_error.re_status = RPC_TIMEDOUT);
292 get_reply:
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 fd.fd = cu->cu_sock;
302 fd.events = POLLIN;
303 for (;;)
305 switch (__poll(&fd, 1, milliseconds))
308 case 0:
309 time_waited.tv_sec += cu->cu_wait.tv_sec;
310 time_waited.tv_usec += cu->cu_wait.tv_usec;
311 while (time_waited.tv_usec >= 1000000)
313 time_waited.tv_sec++;
314 time_waited.tv_usec -= 1000000;
316 if ((time_waited.tv_sec < timeout.tv_sec) ||
317 ((time_waited.tv_sec == timeout.tv_sec) &&
318 (time_waited.tv_usec < timeout.tv_usec)))
319 goto send_again;
320 return (cu->cu_error.re_status = RPC_TIMEDOUT);
323 * buggy in other cases because time_waited is not being
324 * updated.
326 case -1:
327 if (errno == EINTR)
328 continue;
329 cu->cu_error.re_errno = errno;
330 return (cu->cu_error.re_status = RPC_CANTRECV);
334 fromlen = sizeof (struct sockaddr);
335 inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
336 (int) cu->cu_recvsz, 0,
337 (struct sockaddr *) &from, &fromlen);
339 while (inlen < 0 && errno == EINTR);
340 if (inlen < 0)
342 if (errno == EWOULDBLOCK)
343 continue;
344 cu->cu_error.re_errno = errno;
345 return (cu->cu_error.re_status = RPC_CANTRECV);
347 if (inlen < 4)
348 continue;
350 /* see if reply transaction id matches sent id.
351 Don't do this if we only wait for a replay */
352 if (xargs != NULL
353 && (*((u_int32_t *) (cu->cu_inbuf))
354 != *((u_int32_t *) (cu->cu_outbuf))))
355 continue;
356 /* we now assume we have the proper reply */
357 break;
361 * now decode and validate the response
363 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
364 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
365 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
366 if (ok)
368 _seterr_reply (&reply_msg, &(cu->cu_error));
369 if (cu->cu_error.re_status == RPC_SUCCESS)
371 if (!AUTH_VALIDATE (cl->cl_auth,
372 &reply_msg.acpted_rply.ar_verf))
374 cu->cu_error.re_status = RPC_AUTHERROR;
375 cu->cu_error.re_why = AUTH_INVALIDRESP;
377 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
379 xdrs->x_op = XDR_FREE;
380 (void) xdr_opaque_auth (xdrs,
381 &(reply_msg.acpted_rply.ar_verf));
383 } /* end successful completion */
384 else
386 /* maybe our credentials need to be refreshed ... */
387 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
389 nrefreshes--;
390 goto call_again;
392 } /* end of unsuccessful completion */
393 } /* end of valid reply message */
394 else
396 cu->cu_error.re_status = RPC_CANTDECODERES;
398 return cu->cu_error.re_status;
401 static void
402 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
404 struct cu_data *cu = (struct cu_data *) cl->cl_private;
406 *errp = cu->cu_error;
410 static bool_t
411 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
413 struct cu_data *cu = (struct cu_data *) cl->cl_private;
414 XDR *xdrs = &(cu->cu_outxdrs);
416 xdrs->x_op = XDR_FREE;
417 return (*xdr_res) (xdrs, res_ptr);
420 static void
421 clntudp_abort (void)
425 static bool_t
426 clntudp_control (CLIENT *cl, int request, char *info)
428 struct cu_data *cu = (struct cu_data *) cl->cl_private;
430 switch (request)
432 case CLSET_FD_CLOSE:
433 cu->cu_closeit = TRUE;
434 break;
435 case CLSET_FD_NCLOSE:
436 cu->cu_closeit = FALSE;
437 break;
438 case CLSET_TIMEOUT:
439 cu->cu_total = *(struct timeval *) info;
440 break;
441 case CLGET_TIMEOUT:
442 *(struct timeval *) info = cu->cu_total;
443 break;
444 case CLSET_RETRY_TIMEOUT:
445 cu->cu_wait = *(struct timeval *) info;
446 break;
447 case CLGET_RETRY_TIMEOUT:
448 *(struct timeval *) info = cu->cu_wait;
449 break;
450 case CLGET_SERVER_ADDR:
451 *(struct sockaddr_in *) info = cu->cu_raddr;
452 break;
453 case CLGET_FD:
454 *(int *)info = cu->cu_sock;
455 break;
456 case CLGET_XID:
458 * use the knowledge that xid is the
459 * first element in the call structure *.
460 * This will get the xid of the PREVIOUS call
462 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
463 break;
464 case CLSET_XID:
465 /* This will set the xid of the NEXT call */
466 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
467 /* decrement by 1 as clntudp_call() increments once */
468 case CLGET_VERS:
470 * This RELIES on the information that, in the call body,
471 * the version number field is the fifth field from the
472 * begining of the RPC header. MUST be changed if the
473 * call_struct is changed
475 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
476 4 * BYTES_PER_XDR_UNIT));
477 break;
478 case CLSET_VERS:
479 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
480 = htonl(*(u_long *)info);
481 break;
482 case CLGET_PROG:
484 * This RELIES on the information that, in the call body,
485 * the program number field is the field from the
486 * begining of the RPC header. MUST be changed if the
487 * call_struct is changed
489 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
490 3 * BYTES_PER_XDR_UNIT));
491 break;
492 case CLSET_PROG:
493 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
494 = htonl(*(u_long *)info);
495 break;
496 /* The following are only possible with TI-RPC */
497 case CLGET_SVC_ADDR:
498 case CLSET_SVC_ADDR:
499 case CLSET_PUSH_TIMOD:
500 case CLSET_POP_TIMOD:
501 default:
502 return FALSE;
504 return TRUE;
507 static void
508 clntudp_destroy (CLIENT *cl)
510 struct cu_data *cu = (struct cu_data *) cl->cl_private;
512 if (cu->cu_closeit)
514 (void) __close (cu->cu_sock);
516 XDR_DESTROY (&(cu->cu_outxdrs));
517 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
518 mem_free ((caddr_t) cl, sizeof (CLIENT));