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