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