1 /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
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.
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";
35 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
51 #include <rpc/pmap_clnt.h>
62 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
63 extern u_long _create_xid (void);
66 * UDP bases client side rpc operations
68 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
69 xdrproc_t, caddr_t, struct timeval);
70 static void clntudp_abort (void);
71 static void clntudp_geterr (CLIENT *, struct rpc_err *);
72 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
73 static bool_t clntudp_control (CLIENT *, int, char *);
74 static void clntudp_destroy (CLIENT *);
76 static struct clnt_ops udp_ops =
87 * Private data kept per client handle
93 struct sockaddr_in cu_raddr;
95 struct timeval cu_wait;
96 struct timeval cu_total;
97 struct rpc_err cu_error;
107 * Create a UDP based client handle.
108 * If *sockp<0, *sockp is set to a newly created UPD socket.
109 * If raddr->sin_port is 0 a binder on the remote machine
110 * is consulted for the correct port number.
111 * NB: It is the clients responsibility to close *sockp.
112 * NB: The rpch->cl_auth is initialized to null authentication.
113 * Caller may wish to set this something more useful.
115 * wait is the amount of time used between retransmitting a call if
116 * no response has been heard; retransmission occurs until the actual
117 * rpc call times out.
119 * sendsz and recvsz are the maximum allowable packet sizes that can be
123 clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
124 struct timeval wait, int *sockp, u_int sendsz,
128 struct cu_data *cu = NULL;
129 struct rpc_msg call_msg;
131 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
132 sendsz = ((sendsz + 3) / 4) * 4;
133 recvsz = ((recvsz + 3) / 4) * 4;
134 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
135 if (cl == NULL || cu == NULL)
137 struct rpc_createerr *ce = &get_rpc_createerr ();
139 if (_IO_fwide (stderr, 0) > 0)
140 (void) __fwprintf (stderr, L"%s",
141 _("clntudp_create: out of memory\n"));
144 (void) fputs (_("clntudp_create: out of memory\n"), stderr);
145 ce->cf_stat = RPC_SYSTEMERROR;
146 ce->cf_error.re_errno = ENOMEM;
149 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
151 if (raddr->sin_port == 0)
155 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
159 raddr->sin_port = htons (port);
161 cl->cl_ops = &udp_ops;
162 cl->cl_private = (caddr_t) cu;
163 cu->cu_raddr = *raddr;
164 cu->cu_rlen = sizeof (cu->cu_raddr);
166 cu->cu_total.tv_sec = -1;
167 cu->cu_total.tv_usec = -1;
168 cu->cu_sendsz = sendsz;
169 cu->cu_recvsz = recvsz;
170 call_msg.rm_xid = _create_xid ();
171 call_msg.rm_direction = CALL;
172 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
173 call_msg.rm_call.cb_prog = program;
174 call_msg.rm_call.cb_vers = version;
175 INTUSE(xdrmem_create) (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
176 if (!INTUSE(xdr_callhdr) (&(cu->cu_outxdrs), &call_msg))
180 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
185 *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
188 struct rpc_createerr *ce = &get_rpc_createerr ();
189 ce->cf_stat = RPC_SYSTEMERROR;
190 ce->cf_error.re_errno = errno;
193 /* attempt to bind to prov port */
194 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
195 /* the sockets rpc controls are non-blocking */
196 (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock);
200 setsockopt(*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
203 cu->cu_closeit = TRUE;
207 cu->cu_closeit = FALSE;
209 cu->cu_sock = *sockp;
210 cl->cl_auth = authnone_create ();
214 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
216 mem_free ((caddr_t) cl, sizeof (CLIENT));
217 return (CLIENT *) NULL;
221 clntudp_create (raddr, program, version, wait, sockp)
222 struct sockaddr_in *raddr;
229 return clntudp_bufcreate (raddr, program, version, wait, sockp,
230 UDPMSGSIZE, UDPMSGSIZE);
234 is_network_up (int sock)
237 char buf[UDPMSGSIZE];
238 struct ifreq ifreq, *ifr;
241 ifc.ifc_len = sizeof (buf);
243 if (__ioctl(sock, SIOCGIFCONF, (char *) &ifc) == 0)
246 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
249 if (__ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
252 if ((ifreq.ifr_flags & IFF_UP)
253 && ifr->ifr_addr.sa_family == AF_INET)
260 static enum clnt_stat
261 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
262 CLIENT *cl; /* client handle */
263 u_long proc; /* procedure number */
264 xdrproc_t xargs; /* xdr routine for args */
265 caddr_t argsp; /* pointer to args */
266 xdrproc_t xresults; /* xdr routine for results */
267 caddr_t resultsp; /* pointer to results */
268 struct timeval utimeout; /* seconds to wait before giving up */
270 struct cu_data *cu = (struct cu_data *) cl->cl_private;
276 int milliseconds = (cu->cu_wait.tv_sec * 1000) +
277 (cu->cu_wait.tv_usec / 1000);
278 struct sockaddr_in from;
279 struct rpc_msg reply_msg;
281 struct timeval time_waited;
283 int nrefreshes = 2; /* number of times to refresh cred */
284 struct timeval timeout;
285 int anyup; /* any network interface up */
287 if (cu->cu_total.tv_usec == -1)
289 timeout = utimeout; /* use supplied timeout */
293 timeout = cu->cu_total; /* use default timeout */
296 time_waited.tv_sec = 0;
297 time_waited.tv_usec = 0;
299 xdrs = &(cu->cu_outxdrs);
302 xdrs->x_op = XDR_ENCODE;
303 XDR_SETPOS (xdrs, cu->cu_xdrpos);
305 * the transaction is the first thing in the out buffer
307 (*(uint32_t *) (cu->cu_outbuf))++;
308 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
309 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
310 (!(*xargs) (xdrs, argsp)))
311 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
312 outlen = (int) XDR_GETPOS (xdrs);
315 if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
316 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
319 cu->cu_error.re_errno = errno;
320 return (cu->cu_error.re_status = RPC_CANTSEND);
324 * Hack to provide rpc-based message passing
326 if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
328 return (cu->cu_error.re_status = RPC_TIMEDOUT);
332 * sub-optimal code appears here because we have
333 * some clock time to spare while the packets are in flight.
334 * (We assume that this is actually only executed once.)
336 reply_msg.acpted_rply.ar_verf = _null_auth;
337 reply_msg.acpted_rply.ar_results.where = resultsp;
338 reply_msg.acpted_rply.ar_results.proc = xresults;
344 switch (__poll (&fd, 1, milliseconds))
350 anyup = is_network_up (cu->cu_sock);
352 return (cu->cu_error.re_status = RPC_CANTRECV);
355 time_waited.tv_sec += cu->cu_wait.tv_sec;
356 time_waited.tv_usec += cu->cu_wait.tv_usec;
357 while (time_waited.tv_usec >= 1000000)
359 time_waited.tv_sec++;
360 time_waited.tv_usec -= 1000000;
362 if ((time_waited.tv_sec < timeout.tv_sec) ||
363 ((time_waited.tv_sec == timeout.tv_sec) &&
364 (time_waited.tv_usec < timeout.tv_usec)))
366 return (cu->cu_error.re_status = RPC_TIMEDOUT);
369 * buggy in other cases because time_waited is not being
375 cu->cu_error.re_errno = errno;
376 return (cu->cu_error.re_status = RPC_CANTRECV);
379 if (fd.revents & POLLERR)
382 struct cmsghdr *cmsg;
383 struct sock_extended_err *e;
384 struct sockaddr_in err_addr;
386 char *cbuf = (char *) alloca (outlen + 256);
389 iov.iov_base = cbuf + 256;
390 iov.iov_len = outlen;
391 msg.msg_name = (void *) &err_addr;
392 msg.msg_namelen = sizeof (err_addr);
396 msg.msg_control = cbuf;
397 msg.msg_controllen = 256;
398 ret = recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
400 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
401 && (msg.msg_flags & MSG_ERRQUEUE)
402 && ((msg.msg_namelen == 0
404 || (msg.msg_namelen == sizeof (err_addr)
405 && err_addr.sin_family == AF_INET
406 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
407 sizeof (err_addr.sin_addr)) == 0
408 && err_addr.sin_port == cu->cu_raddr.sin_port)))
409 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
410 cmsg = CMSG_NXTHDR (&msg, cmsg))
411 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
413 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
414 cu->cu_error.re_errno = e->ee_errno;
415 return (cu->cu_error.re_status = RPC_CANTRECV);
421 fromlen = sizeof (struct sockaddr);
422 inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
423 (int) cu->cu_recvsz, 0,
424 (struct sockaddr *) &from, &fromlen);
426 while (inlen < 0 && errno == EINTR);
429 if (errno == EWOULDBLOCK)
431 cu->cu_error.re_errno = errno;
432 return (cu->cu_error.re_status = RPC_CANTRECV);
437 /* see if reply transaction id matches sent id.
438 Don't do this if we only wait for a replay */
440 && (*((u_int32_t *) (cu->cu_inbuf))
441 != *((u_int32_t *) (cu->cu_outbuf))))
443 /* we now assume we have the proper reply */
448 * now decode and validate the response
450 INTUSE(xdrmem_create) (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
451 ok = INTUSE(xdr_replymsg) (&reply_xdrs, &reply_msg);
452 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
455 _seterr_reply (&reply_msg, &(cu->cu_error));
456 if (cu->cu_error.re_status == RPC_SUCCESS)
458 if (!AUTH_VALIDATE (cl->cl_auth,
459 &reply_msg.acpted_rply.ar_verf))
461 cu->cu_error.re_status = RPC_AUTHERROR;
462 cu->cu_error.re_why = AUTH_INVALIDRESP;
464 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
466 xdrs->x_op = XDR_FREE;
467 (void) INTUSE(xdr_opaque_auth) (xdrs,
468 &(reply_msg.acpted_rply.ar_verf));
470 } /* end successful completion */
473 /* maybe our credentials need to be refreshed ... */
474 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
479 } /* end of unsuccessful completion */
480 } /* end of valid reply message */
483 cu->cu_error.re_status = RPC_CANTDECODERES;
485 return cu->cu_error.re_status;
489 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
491 struct cu_data *cu = (struct cu_data *) cl->cl_private;
493 *errp = cu->cu_error;
498 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
500 struct cu_data *cu = (struct cu_data *) cl->cl_private;
501 XDR *xdrs = &(cu->cu_outxdrs);
503 xdrs->x_op = XDR_FREE;
504 return (*xdr_res) (xdrs, res_ptr);
513 clntudp_control (CLIENT *cl, int request, char *info)
515 struct cu_data *cu = (struct cu_data *) cl->cl_private;
520 cu->cu_closeit = TRUE;
522 case CLSET_FD_NCLOSE:
523 cu->cu_closeit = FALSE;
526 cu->cu_total = *(struct timeval *) info;
529 *(struct timeval *) info = cu->cu_total;
531 case CLSET_RETRY_TIMEOUT:
532 cu->cu_wait = *(struct timeval *) info;
534 case CLGET_RETRY_TIMEOUT:
535 *(struct timeval *) info = cu->cu_wait;
537 case CLGET_SERVER_ADDR:
538 *(struct sockaddr_in *) info = cu->cu_raddr;
541 *(int *)info = cu->cu_sock;
545 * use the knowledge that xid is the
546 * first element in the call structure *.
547 * This will get the xid of the PREVIOUS call
549 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
552 /* This will set the xid of the NEXT call */
553 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
554 /* decrement by 1 as clntudp_call() increments once */
557 * This RELIES on the information that, in the call body,
558 * the version number field is the fifth field from the
559 * begining of the RPC header. MUST be changed if the
560 * call_struct is changed
562 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
563 4 * BYTES_PER_XDR_UNIT));
566 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
567 = htonl(*(u_long *)info);
571 * This RELIES on the information that, in the call body,
572 * the program number field is the field from the
573 * begining of the RPC header. MUST be changed if the
574 * call_struct is changed
576 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
577 3 * BYTES_PER_XDR_UNIT));
580 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
581 = htonl(*(u_long *)info);
583 /* The following are only possible with TI-RPC */
586 case CLSET_PUSH_TIMOD:
587 case CLSET_POP_TIMOD:
595 clntudp_destroy (CLIENT *cl)
597 struct cu_data *cu = (struct cu_data *) cl->cl_private;
601 (void) __close (cu->cu_sock);
603 XDR_DESTROY (&(cu->cu_outxdrs));
604 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
605 mem_free ((caddr_t) cl, sizeof (CLIENT));