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
29 * @(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro
30 * @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/clnt_udp.c,v 1.15.2.1 2001/06/28 21:44:24 iedowse Exp $
32 * $DragonFly: src/lib/libc/rpc/clnt_udp.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
36 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
41 #include "namespace.h"
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
51 #include <rpc/pmap_clnt.h>
52 #include "un-namespace.h"
55 * UDP bases client side rpc operations
57 static enum clnt_stat clntudp_call();
58 static void clntudp_abort();
59 static void clntudp_geterr();
60 static bool_t clntudp_freeres();
61 static bool_t clntudp_control();
62 static void clntudp_destroy();
64 static struct clnt_ops udp_ops = {
74 * Private data kept per client handle
79 struct sockaddr_in cu_raddr;
81 struct timeval cu_wait;
82 struct timeval cu_total;
83 struct rpc_err cu_error;
89 int cu_connect; /* Use connect(). */
90 int cu_connected; /* Have done connect(). */
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.
103 * wait is the amount of time used between retransmitting a call if
104 * no response has been heard; retransmition occurs until the actual
105 * rpc call times out.
107 * sendsz and recvsz are the maximum allowable packet sizes that can be
111 clntudp_bufcreate(struct sockaddr_in *raddr, u_long program, u_long version,
112 struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
115 struct cu_data *cu = NULL;
117 struct rpc_msg call_msg;
118 static u_int32_t disrupt;
121 disrupt = (u_int32_t)(long)raddr;
123 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
125 fprintf(stderr, "clntudp_create: out of memory\n");
126 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
127 rpc_createerr.cf_error.re_errno = errno;
130 sendsz = ((sendsz + 3) / 4) * 4;
131 recvsz = ((recvsz + 3) / 4) * 4;
132 cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
134 fprintf(stderr, "clntudp_create: out of memory\n");
135 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
136 rpc_createerr.cf_error.re_errno = errno;
139 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
141 gettimeofday(&now, (struct timezone *)0);
142 if (raddr->sin_port == 0) {
145 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
148 raddr->sin_port = htons(port);
150 cl->cl_ops = &udp_ops;
151 cl->cl_private = (caddr_t)cu;
152 cu->cu_raddr = *raddr;
153 cu->cu_rlen = sizeof (cu->cu_raddr);
155 cu->cu_total.tv_sec = -1;
156 cu->cu_total.tv_usec = -1;
157 cu->cu_sendsz = sendsz;
158 cu->cu_recvsz = recvsz;
159 cu->cu_connect = FALSE;
160 cu->cu_connected = FALSE;
161 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
162 call_msg.rm_direction = CALL;
163 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
164 call_msg.rm_call.cb_prog = program;
165 call_msg.rm_call.cb_vers = version;
166 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
168 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
171 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
175 *sockp = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
177 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
178 rpc_createerr.cf_error.re_errno = errno;
181 /* attempt to bind to priv port */
182 bindresvport(*sockp, (struct sockaddr_in *)0);
183 /* the sockets rpc controls are non-blocking */
184 _ioctl(*sockp, FIONBIO, (char *) &dontblock);
185 cu->cu_closeit = TRUE;
187 cu->cu_closeit = FALSE;
189 cu->cu_sock = *sockp;
190 cl->cl_auth = authnone_create();
194 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
196 mem_free((caddr_t)cl, sizeof(CLIENT));
197 return ((CLIENT *)NULL);
201 clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
202 struct timeval wait, int *sockp)
205 return(clntudp_bufcreate(raddr, program, version, wait, sockp,
206 UDPMSGSIZE, UDPMSGSIZE));
209 static enum clnt_stat
210 clntudp_call(CLIENT *cl, /* client handle */
211 u_long proc, /* procedure number */
212 xdrproc_t xargs, /* xdr routine for args */
213 caddr_t argsp, /* pointer to args */
214 xdrproc_t xresults, /* xdr routine for results */
215 caddr_t resultsp, /* pointer to results */
216 struct timeval utimeout) /* seconds to wait before giving up */
218 struct cu_data *cu = (struct cu_data *)cl->cl_private;
224 fd_set *fds, readfds;
225 struct sockaddr_in from;
226 struct rpc_msg reply_msg;
228 struct timeval time_waited, start, after, tmp1, tmp2, tv;
230 int nrefreshes = 2; /* number of times to refresh cred */
231 struct timeval timeout;
234 if (cu->cu_total.tv_usec == -1)
235 timeout = utimeout; /* use supplied timeout */
237 timeout = cu->cu_total; /* use default timeout */
239 if (cu->cu_sock + 1 > FD_SETSIZE) {
240 int bytes = howmany(cu->cu_sock + 1, NFDBITS) * sizeof(fd_mask);
241 fds = (fd_set *)malloc(bytes);
243 return (cu->cu_error.re_status = RPC_CANTSEND);
244 memset(fds, 0, bytes);
250 if (cu->cu_connect && !cu->cu_connected) {
251 if (connect(cu->cu_sock, (struct sockaddr *)&cu->cu_raddr,
253 cu->cu_error.re_errno = errno;
256 return (cu->cu_error.re_status = RPC_CANTSEND);
258 cu->cu_connected = 1;
260 if (cu->cu_connected) {
264 sa = (struct sockaddr *)&cu->cu_raddr;
267 timerclear(&time_waited);
270 xdrs = &(cu->cu_outxdrs);
271 xdrs->x_op = XDR_ENCODE;
272 XDR_SETPOS(xdrs, cu->cu_xdrpos);
274 * the transaction is the first thing in the out buffer
276 (*(u_short *)(cu->cu_outbuf))++;
277 if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
278 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
279 (! (*xargs)(xdrs, argsp))) {
282 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
284 outlen = (int)XDR_GETPOS(xdrs);
287 if (_sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0, sa, salen)
289 cu->cu_error.re_errno = errno;
292 return (cu->cu_error.re_status = RPC_CANTSEND);
296 * Hack to provide rpc-based message passing
298 if (!timerisset(&timeout)) {
301 return (cu->cu_error.re_status = RPC_TIMEDOUT);
304 * sub-optimal code appears here because we have
305 * some clock time to spare while the packets are in flight.
306 * (We assume that this is actually only executed once.)
308 reply_msg.acpted_rply.ar_verf = _null_auth;
309 reply_msg.acpted_rply.ar_results.where = resultsp;
310 reply_msg.acpted_rply.ar_results.proc = xresults;
312 gettimeofday(&start, NULL);
314 /* XXX we know the other bits are still clear */
315 FD_SET(cu->cu_sock, fds);
317 switch (_select(cu->cu_sock+1, fds, NULL, NULL, &tv)) {
320 timeradd(&time_waited, &cu->cu_wait, &tmp1);
322 if (timercmp(&time_waited, &timeout, <))
326 return (cu->cu_error.re_status = RPC_TIMEDOUT);
329 if (errno == EINTR) {
330 gettimeofday(&after, NULL);
331 timersub(&after, &start, &tmp1);
332 timeradd(&time_waited, &tmp1, &tmp2);
334 if (timercmp(&time_waited, &timeout, <))
338 return (cu->cu_error.re_status = RPC_TIMEDOUT);
340 cu->cu_error.re_errno = errno;
343 return (cu->cu_error.re_status = RPC_CANTRECV);
347 fromlen = sizeof(struct sockaddr);
348 inlen = _recvfrom(cu->cu_sock, cu->cu_inbuf,
349 (int) cu->cu_recvsz, 0,
350 (struct sockaddr *)&from, &fromlen);
351 } while (inlen < 0 && errno == EINTR);
353 if (errno == EWOULDBLOCK)
355 cu->cu_error.re_errno = errno;
358 return (cu->cu_error.re_status = RPC_CANTRECV);
360 if (inlen < sizeof(u_int32_t))
362 /* see if reply transaction id matches sent id */
363 if (*((u_int32_t *)(cu->cu_inbuf)) != *((u_int32_t *)(cu->cu_outbuf)))
365 /* we now assume we have the proper reply */
370 * now decode and validate the response
372 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
373 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
374 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
376 _seterr_reply(&reply_msg, &(cu->cu_error));
377 if (cu->cu_error.re_status == RPC_SUCCESS) {
378 if (! AUTH_VALIDATE(cl->cl_auth,
379 &reply_msg.acpted_rply.ar_verf)) {
380 cu->cu_error.re_status = RPC_AUTHERROR;
381 cu->cu_error.re_why = AUTH_INVALIDRESP;
383 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
384 xdrs->x_op = XDR_FREE;
385 xdr_opaque_auth(xdrs,
386 &(reply_msg.acpted_rply.ar_verf));
388 } /* end successful completion */
390 /* maybe our credentials need to be refreshed ... */
391 if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
395 } /* end of unsuccessful completion */
396 } /* end of valid reply message */
399 * It's possible for xdr_replymsg() to fail partway
400 * through its attempt to decode the result from the
401 * server. If this happens, it will leave the reply
402 * structure partially populated with dynamically
403 * allocated memory. (This can happen if someone uses
404 * clntudp_bufcreate() to create a CLIENT handle and
405 * specifies a receive buffer size that is too small.)
406 * This memory must be free()ed to avoid a leak.
408 int op = reply_xdrs.x_op;
409 reply_xdrs.x_op = XDR_FREE;
410 xdr_replymsg(&reply_xdrs, &reply_msg);
411 reply_xdrs.x_op = op;
412 cu->cu_error.re_status = RPC_CANTDECODERES;
416 return (cu->cu_error.re_status);
420 clntudp_geterr(CLIENT *cl, struct rpc_err *errp)
422 struct cu_data *cu = (struct cu_data *)cl->cl_private;
424 *errp = cu->cu_error;
429 clntudp_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
431 struct cu_data *cu = (struct cu_data *)cl->cl_private;
432 XDR *xdrs = &(cu->cu_outxdrs);
434 xdrs->x_op = XDR_FREE;
435 return ((*xdr_res)(xdrs, res_ptr));
439 clntudp_abort(void) /* CLIENT *h */
445 clntudp_control(CLIENT *cl, int request, char *info)
447 struct cu_data *cu = (struct cu_data *)cl->cl_private;
453 cu->cu_closeit = TRUE;
455 case CLSET_FD_NCLOSE:
456 cu->cu_closeit = FALSE;
461 tv = (struct timeval *)info;
462 cu->cu_total.tv_sec = tv->tv_sec;
463 cu->cu_total.tv_usec = tv->tv_usec;
468 *(struct timeval *)info = cu->cu_total;
470 case CLSET_RETRY_TIMEOUT:
473 tv = (struct timeval *)info;
474 cu->cu_wait.tv_sec = tv->tv_sec;
475 cu->cu_wait.tv_usec = tv->tv_usec;
477 case CLGET_RETRY_TIMEOUT:
480 *(struct timeval *)info = cu->cu_wait;
482 case CLGET_SERVER_ADDR:
485 *(struct sockaddr_in *)info = cu->cu_raddr;
490 *(int *)info = cu->cu_sock;
494 * use the knowledge that xid is the
495 * first element in the call structure *.
496 * This will get the xid of the PREVIOUS call
500 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
503 /* This will set the xid of the NEXT call */
506 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
507 /* decrement by 1 as clntudp_call() increments once */
510 * This RELIES on the information that, in the call body,
511 * the version number field is the fifth field from the
512 * begining of the RPC header. MUST be changed if the
513 * call_struct is changed
517 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
518 4 * BYTES_PER_XDR_UNIT));
523 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
524 = htonl(*(u_long *)info);
528 * This RELIES on the information that, in the call body,
529 * the program number field is the field from the
530 * begining of the RPC header. MUST be changed if the
531 * call_struct is changed
535 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
536 3 * BYTES_PER_XDR_UNIT));
541 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
542 = htonl(*(u_long *)info);
544 case CLGET_LOCAL_ADDR:
545 len = sizeof(struct sockaddr);
546 if (_getsockname(cu->cu_sock, (struct sockaddr *)info, &len) <0)
550 cu->cu_connect = *(int *)(void *)info;
554 case CLSET_PUSH_TIMOD:
555 case CLSET_POP_TIMOD:
563 clntudp_destroy(CLIENT *cl)
565 struct cu_data *cu = (struct cu_data *)cl->cl_private;
567 if (cu->cu_closeit) {
570 XDR_DESTROY(&(cu->cu_outxdrs));
571 mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
572 mem_free((caddr_t)cl, sizeof(CLIENT));