]> git.ipfire.org Git - thirdparty/glibc.git/blob - sunrpc/clnt_udp.c
Update.
[thirdparty/glibc.git] / sunrpc / clnt_udp.c
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.
9 *
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.
13 *
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.
17 *
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.
21 *
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.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
36 *
37 * Copyright (C) 1984, Sun Microsystems, Inc.
38 */
39
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>
50
51 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
52
53 /*
54 * UDP bases client side rpc operations
55 */
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 *);
63
64 static struct clnt_ops udp_ops =
65 {
66 clntudp_call,
67 clntudp_abort,
68 clntudp_geterr,
69 clntudp_freeres,
70 clntudp_destroy,
71 clntudp_control
72 };
73
74 /*
75 * Private data kept per client handle
76 */
77 struct cu_data
78 {
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];
92 };
93
94 /*
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.
102 *
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.
106 *
107 * sendsz and recvsz are the maximum allowable packet sizes that can be
108 * sent and received.
109 */
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;
119 {
120 CLIENT *cl;
121 struct cu_data *cu = NULL;
122 struct timeval now;
123 struct rpc_msg call_msg;
124
125 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
126 if (cl == NULL)
127 {
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;
132 }
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)
137 {
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;
142 }
143 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
144
145 (void) gettimeofday (&now, (struct timezone *) 0);
146 if (raddr->sin_port == 0)
147 {
148 u_short port;
149 if ((port =
150 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
151 {
152 goto fooy;
153 }
154 raddr->sin_port = htons (port);
155 }
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))
173 {
174 goto fooy;
175 }
176 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
177 if (*sockp < 0)
178 {
179 int dontblock = 1;
180
181 *sockp = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
182 if (*sockp < 0)
183 {
184 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
185 rpc_createerr.cf_error.re_errno = errno;
186 goto fooy;
187 }
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;
193 }
194 else
195 {
196 cu->cu_closeit = FALSE;
197 }
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;
207 }
208
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;
216 {
217
218 return clntudp_bufcreate (raddr, program, version, wait, sockp,
219 UDPMSGSIZE, UDPMSGSIZE);
220 }
221
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 */
231 {
232 struct cu_data *cu = (struct cu_data *) cl->cl_private;
233 XDR *xdrs;
234 int outlen = 0;
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;
251
252 if (cu->cu_total.tv_usec == -1)
253 {
254 timeout = utimeout; /* use supplied timeout */
255 }
256 else
257 {
258 timeout = cu->cu_total; /* use default timeout */
259 }
260
261 time_waited.tv_sec = 0;
262 time_waited.tv_usec = 0;
263 call_again:
264 xdrs = &(cu->cu_outxdrs);
265 if (xargs == NULL)
266 goto get_reply;
267 xdrs->x_op = XDR_ENCODE;
268 XDR_SETPOS (xdrs, cu->cu_xdrpos);
269 /*
270 * the transaction is the first thing in the out buffer
271 */
272 (*(u_short *) (cu->cu_outbuf))++;
273 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
274 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
275 (!(*xargs) (xdrs, argsp)))
276 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
277 outlen = (int) XDR_GETPOS (xdrs);
278
279 send_again:
280 if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
281 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
282 != outlen)
283 {
284 cu->cu_error.re_errno = errno;
285 return (cu->cu_error.re_status = RPC_CANTSEND);
286 }
287
288 /*
289 * Hack to provide rpc-based message passing
290 */
291 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
292 {
293 return (cu->cu_error.re_status = RPC_TIMEDOUT);
294 }
295 get_reply:
296 /*
297 * sub-optimal code appears here because we have
298 * some clock time to spare while the packets are in flight.
299 * (We assume that this is actually only executed once.)
300 */
301 reply_msg.acpted_rply.ar_verf = _null_auth;
302 reply_msg.acpted_rply.ar_results.where = resultsp;
303 reply_msg.acpted_rply.ar_results.proc = xresults;
304 #ifdef FD_SETSIZE
305 FD_ZERO (&mask);
306 FD_SET (cu->cu_sock, &mask);
307 #else
308 mask = 1 << cu->cu_sock;
309 #endif /* def FD_SETSIZE */
310 for (;;)
311 {
312 struct timeval timeout = cu->cu_wait;
313 readfds = mask;
314 switch (select (_rpc_dtablesize (), &readfds, (fd_set*) NULL,
315 (fd_set*) NULL, &timeout))
316 {
317
318 case 0:
319 time_waited.tv_sec += cu->cu_wait.tv_sec;
320 time_waited.tv_usec += cu->cu_wait.tv_usec;
321 while (time_waited.tv_usec >= 1000000)
322 {
323 time_waited.tv_sec++;
324 time_waited.tv_usec -= 1000000;
325 }
326 if ((time_waited.tv_sec < timeout.tv_sec) ||
327 ((time_waited.tv_sec == timeout.tv_sec) &&
328 (time_waited.tv_usec < timeout.tv_usec)))
329 goto send_again;
330 return (cu->cu_error.re_status = RPC_TIMEDOUT);
331
332 /*
333 * buggy in other cases because time_waited is not being
334 * updated.
335 */
336 case -1:
337 if (errno == EINTR)
338 continue;
339 cu->cu_error.re_errno = errno;
340 return (cu->cu_error.re_status = RPC_CANTRECV);
341 }
342 do
343 {
344 fromlen = sizeof (struct sockaddr);
345 inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
346 (int) cu->cu_recvsz, 0,
347 (struct sockaddr *) &from, &fromlen);
348 }
349 while (inlen < 0 && errno == EINTR);
350 if (inlen < 0)
351 {
352 if (errno == EWOULDBLOCK)
353 continue;
354 cu->cu_error.re_errno = errno;
355 return (cu->cu_error.re_status = RPC_CANTRECV);
356 }
357 if (inlen < 4)
358 continue;
359 /* see if reply transaction id matches sent id */
360 if (*((u_int32_t *) (cu->cu_inbuf)) != *((u_int32_t *) (cu->cu_outbuf)))
361 continue;
362 /* we now assume we have the proper reply */
363 break;
364 }
365
366 /*
367 * now decode and validate the response
368 */
369 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
370 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
371 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
372 if (ok)
373 {
374 _seterr_reply (&reply_msg, &(cu->cu_error));
375 if (cu->cu_error.re_status == RPC_SUCCESS)
376 {
377 if (!AUTH_VALIDATE (cl->cl_auth,
378 &reply_msg.acpted_rply.ar_verf))
379 {
380 cu->cu_error.re_status = RPC_AUTHERROR;
381 cu->cu_error.re_why = AUTH_INVALIDRESP;
382 }
383 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
384 {
385 xdrs->x_op = XDR_FREE;
386 (void) xdr_opaque_auth (xdrs,
387 &(reply_msg.acpted_rply.ar_verf));
388 }
389 } /* end successful completion */
390 else
391 {
392 /* maybe our credentials need to be refreshed ... */
393 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
394 {
395 nrefreshes--;
396 goto call_again;
397 }
398 } /* end of unsuccessful completion */
399 } /* end of valid reply message */
400 else
401 {
402 cu->cu_error.re_status = RPC_CANTDECODERES;
403 }
404 return cu->cu_error.re_status;
405 }
406
407 static void
408 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
409 {
410 struct cu_data *cu = (struct cu_data *) cl->cl_private;
411
412 *errp = cu->cu_error;
413 }
414
415
416 static bool_t
417 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
418 {
419 struct cu_data *cu = (struct cu_data *) cl->cl_private;
420 XDR *xdrs = &(cu->cu_outxdrs);
421
422 xdrs->x_op = XDR_FREE;
423 return (*xdr_res) (xdrs, res_ptr);
424 }
425
426 static void
427 clntudp_abort (void)
428 {
429 }
430
431 static bool_t
432 clntudp_control (CLIENT *cl, int request, char *info)
433 {
434 struct cu_data *cu = (struct cu_data *) cl->cl_private;
435
436 switch (request)
437 {
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 default:
454 return FALSE;
455 }
456 return TRUE;
457 }
458
459 static void
460 clntudp_destroy (CLIENT *cl)
461 {
462 struct cu_data *cu = (struct cu_data *) cl->cl_private;
463
464 if (cu->cu_closeit)
465 {
466 (void) close (cu->cu_sock);
467 }
468 XDR_DESTROY (&(cu->cu_outxdrs));
469 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
470 mem_free ((caddr_t) cl, sizeof (CLIENT));
471 }