2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC";*/
33 static char *rcsid = "$FreeBSD: src/lib/libc/rpc/clnt_udp.c,v 1.15.2.1 2001/06/28 21:44:24 iedowse Exp $";
37 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
39 * Copyright (C) 1984, Sun Microsystems, Inc.
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
51 #include <rpc/pmap_clnt.h>
54 * UDP bases client side rpc operations
56 static enum clnt_stat clntudp_call();
57 static void clntudp_abort();
58 static void clntudp_geterr();
59 static bool_t clntudp_freeres();
60 static bool_t clntudp_control();
61 static void clntudp_destroy();
63 static struct clnt_ops udp_ops = {
73 * Private data kept per client handle
78 struct sockaddr_in cu_raddr;
80 struct timeval cu_wait;
81 struct timeval cu_total;
82 struct rpc_err cu_error;
88 int cu_connect; /* Use connect(). */
89 int cu_connected; /* Have done connect(). */
94 * Create a UDP based client handle.
95 * If *sockp<0, *sockp is set to a newly created UPD socket.
96 * If raddr->sin_port is 0 a binder on the remote machine
97 * is consulted for the correct port number.
98 * NB: It is the clients responsibility to close *sockp.
99 * NB: The rpch->cl_auth is initialized to null authentication.
100 * Caller may wish to set this something more useful.
102 * wait is the amount of time used between retransmitting a call if
103 * no response has been heard; retransmition occurs until the actual
104 * rpc call times out.
106 * sendsz and recvsz are the maximum allowable packet sizes that can be
110 clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
111 struct sockaddr_in *raddr;
120 register struct cu_data *cu = NULL;
122 struct rpc_msg call_msg;
123 static u_int32_t disrupt;
126 disrupt = (u_int32_t)(long)raddr;
128 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
130 (void) fprintf(stderr, "clntudp_create: out of memory\n");
131 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
132 rpc_createerr.cf_error.re_errno = errno;
135 sendsz = ((sendsz + 3) / 4) * 4;
136 recvsz = ((recvsz + 3) / 4) * 4;
137 cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
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;
144 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
146 (void)gettimeofday(&now, (struct timezone *)0);
147 if (raddr->sin_port == 0) {
150 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
153 raddr->sin_port = htons(port);
155 cl->cl_ops = &udp_ops;
156 cl->cl_private = (caddr_t)cu;
157 cu->cu_raddr = *raddr;
158 cu->cu_rlen = sizeof (cu->cu_raddr);
160 cu->cu_total.tv_sec = -1;
161 cu->cu_total.tv_usec = -1;
162 cu->cu_sendsz = sendsz;
163 cu->cu_recvsz = recvsz;
164 cu->cu_connect = FALSE;
165 cu->cu_connected = FALSE;
166 call_msg.rm_xid = (++disrupt) ^ 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,
173 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
176 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
180 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
182 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
183 rpc_createerr.cf_error.re_errno = errno;
186 /* attempt to bind to priv port */
187 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
188 /* the sockets rpc controls are non-blocking */
189 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
190 cu->cu_closeit = TRUE;
192 cu->cu_closeit = FALSE;
194 cu->cu_sock = *sockp;
195 cl->cl_auth = authnone_create();
199 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
201 mem_free((caddr_t)cl, sizeof(CLIENT));
202 return ((CLIENT *)NULL);
206 clntudp_create(raddr, program, version, wait, sockp)
207 struct sockaddr_in *raddr;
214 return(clntudp_bufcreate(raddr, program, version, wait, sockp,
215 UDPMSGSIZE, UDPMSGSIZE));
218 static enum clnt_stat
219 clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
220 register CLIENT *cl; /* client handle */
221 u_long proc; /* procedure number */
222 xdrproc_t xargs; /* xdr routine for args */
223 caddr_t argsp; /* pointer to args */
224 xdrproc_t xresults; /* xdr routine for results */
225 caddr_t resultsp; /* pointer to results */
226 struct timeval utimeout; /* seconds to wait before giving up */
228 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
234 fd_set *fds, readfds;
235 struct sockaddr_in from;
236 struct rpc_msg reply_msg;
238 struct timeval time_waited, start, after, tmp1, tmp2, tv;
240 int nrefreshes = 2; /* number of times to refresh cred */
241 struct timeval timeout;
244 if (cu->cu_total.tv_usec == -1)
245 timeout = utimeout; /* use supplied timeout */
247 timeout = cu->cu_total; /* use default timeout */
249 if (cu->cu_sock + 1 > FD_SETSIZE) {
250 int bytes = howmany(cu->cu_sock + 1, NFDBITS) * sizeof(fd_mask);
251 fds = (fd_set *)malloc(bytes);
253 return (cu->cu_error.re_status = RPC_CANTSEND);
254 memset(fds, 0, bytes);
260 if (cu->cu_connect && !cu->cu_connected) {
261 if (connect(cu->cu_sock, (struct sockaddr *)&cu->cu_raddr,
263 cu->cu_error.re_errno = errno;
266 return (cu->cu_error.re_status = RPC_CANTSEND);
268 cu->cu_connected = 1;
270 if (cu->cu_connected) {
274 sa = (struct sockaddr *)&cu->cu_raddr;
277 timerclear(&time_waited);
280 xdrs = &(cu->cu_outxdrs);
281 xdrs->x_op = XDR_ENCODE;
282 XDR_SETPOS(xdrs, cu->cu_xdrpos);
284 * the transaction is the first thing in the out buffer
286 (*(u_short *)(cu->cu_outbuf))++;
287 if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
288 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
289 (! (*xargs)(xdrs, argsp))) {
292 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
294 outlen = (int)XDR_GETPOS(xdrs);
297 if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0, sa, salen)
299 cu->cu_error.re_errno = errno;
302 return (cu->cu_error.re_status = RPC_CANTSEND);
306 * Hack to provide rpc-based message passing
308 if (!timerisset(&timeout)) {
311 return (cu->cu_error.re_status = RPC_TIMEDOUT);
314 * sub-optimal code appears here because we have
315 * some clock time to spare while the packets are in flight.
316 * (We assume that this is actually only executed once.)
318 reply_msg.acpted_rply.ar_verf = _null_auth;
319 reply_msg.acpted_rply.ar_results.where = resultsp;
320 reply_msg.acpted_rply.ar_results.proc = xresults;
322 gettimeofday(&start, NULL);
324 /* XXX we know the other bits are still clear */
325 FD_SET(cu->cu_sock, fds);
327 switch (select(cu->cu_sock+1, fds, NULL, NULL, &tv)) {
330 timeradd(&time_waited, &cu->cu_wait, &tmp1);
332 if (timercmp(&time_waited, &timeout, <))
336 return (cu->cu_error.re_status = RPC_TIMEDOUT);
339 if (errno == EINTR) {
340 gettimeofday(&after, NULL);
341 timersub(&after, &start, &tmp1);
342 timeradd(&time_waited, &tmp1, &tmp2);
344 if (timercmp(&time_waited, &timeout, <))
348 return (cu->cu_error.re_status = RPC_TIMEDOUT);
350 cu->cu_error.re_errno = errno;
353 return (cu->cu_error.re_status = RPC_CANTRECV);
357 fromlen = sizeof(struct sockaddr);
358 inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
359 (int) cu->cu_recvsz, 0,
360 (struct sockaddr *)&from, &fromlen);
361 } while (inlen < 0 && errno == EINTR);
363 if (errno == EWOULDBLOCK)
365 cu->cu_error.re_errno = errno;
368 return (cu->cu_error.re_status = RPC_CANTRECV);
370 if (inlen < sizeof(u_int32_t))
372 /* see if reply transaction id matches sent id */
373 if (*((u_int32_t *)(cu->cu_inbuf)) != *((u_int32_t *)(cu->cu_outbuf)))
375 /* we now assume we have the proper reply */
380 * now decode and validate the response
382 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
383 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
384 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
386 _seterr_reply(&reply_msg, &(cu->cu_error));
387 if (cu->cu_error.re_status == RPC_SUCCESS) {
388 if (! AUTH_VALIDATE(cl->cl_auth,
389 &reply_msg.acpted_rply.ar_verf)) {
390 cu->cu_error.re_status = RPC_AUTHERROR;
391 cu->cu_error.re_why = AUTH_INVALIDRESP;
393 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
394 xdrs->x_op = XDR_FREE;
395 (void)xdr_opaque_auth(xdrs,
396 &(reply_msg.acpted_rply.ar_verf));
398 } /* end successful completion */
400 /* maybe our credentials need to be refreshed ... */
401 if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
405 } /* end of unsuccessful completion */
406 } /* end of valid reply message */
409 * It's possible for xdr_replymsg() to fail partway
410 * through its attempt to decode the result from the
411 * server. If this happens, it will leave the reply
412 * structure partially populated with dynamically
413 * allocated memory. (This can happen if someone uses
414 * clntudp_bufcreate() to create a CLIENT handle and
415 * specifies a receive buffer size that is too small.)
416 * This memory must be free()ed to avoid a leak.
418 int op = reply_xdrs.x_op;
419 reply_xdrs.x_op = XDR_FREE;
420 xdr_replymsg(&reply_xdrs, &reply_msg);
421 reply_xdrs.x_op = op;
422 cu->cu_error.re_status = RPC_CANTDECODERES;
426 return (cu->cu_error.re_status);
430 clntudp_geterr(cl, errp)
432 struct rpc_err *errp;
434 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
436 *errp = cu->cu_error;
441 clntudp_freeres(cl, xdr_res, res_ptr)
446 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
447 register XDR *xdrs = &(cu->cu_outxdrs);
449 xdrs->x_op = XDR_FREE;
450 return ((*xdr_res)(xdrs, res_ptr));
461 clntudp_control(cl, request, info)
466 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
467 register struct timeval *tv;
472 cu->cu_closeit = TRUE;
474 case CLSET_FD_NCLOSE:
475 cu->cu_closeit = FALSE;
480 tv = (struct timeval *)info;
481 cu->cu_total.tv_sec = tv->tv_sec;
482 cu->cu_total.tv_usec = tv->tv_usec;
487 *(struct timeval *)info = cu->cu_total;
489 case CLSET_RETRY_TIMEOUT:
492 tv = (struct timeval *)info;
493 cu->cu_wait.tv_sec = tv->tv_sec;
494 cu->cu_wait.tv_usec = tv->tv_usec;
496 case CLGET_RETRY_TIMEOUT:
499 *(struct timeval *)info = cu->cu_wait;
501 case CLGET_SERVER_ADDR:
504 *(struct sockaddr_in *)info = cu->cu_raddr;
509 *(int *)info = cu->cu_sock;
513 * use the knowledge that xid is the
514 * first element in the call structure *.
515 * This will get the xid of the PREVIOUS call
519 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
522 /* This will set the xid of the NEXT call */
525 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
526 /* decrement by 1 as clntudp_call() increments once */
529 * This RELIES on the information that, in the call body,
530 * the version number field is the fifth field from the
531 * begining of the RPC header. MUST be changed if the
532 * call_struct is changed
536 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
537 4 * BYTES_PER_XDR_UNIT));
542 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
543 = htonl(*(u_long *)info);
547 * This RELIES on the information that, in the call body,
548 * the program number field is the field from the
549 * begining of the RPC header. MUST be changed if the
550 * call_struct is changed
554 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
555 3 * BYTES_PER_XDR_UNIT));
560 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
561 = htonl(*(u_long *)info);
563 case CLGET_LOCAL_ADDR:
564 len = sizeof(struct sockaddr);
565 if (getsockname(cu->cu_sock, (struct sockaddr *)info, &len) <0)
569 cu->cu_connect = *(int *)(void *)info;
573 case CLSET_PUSH_TIMOD:
574 case CLSET_POP_TIMOD:
585 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
587 if (cu->cu_closeit) {
588 (void)_close(cu->cu_sock);
590 XDR_DESTROY(&(cu->cu_outxdrs));
591 mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
592 mem_free((caddr_t)cl, sizeof(CLIENT));