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/libcr/rpc/Attic/clnt_udp.c,v 1.3 2004/10/25 19:38:25 drhodus Exp $
36 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
50 #include <rpc/pmap_clnt.h>
53 * UDP bases client side rpc operations
55 static enum clnt_stat clntudp_call();
56 static void clntudp_abort();
57 static void clntudp_geterr();
58 static bool_t clntudp_freeres();
59 static bool_t clntudp_control();
60 static void clntudp_destroy();
62 static struct clnt_ops udp_ops = {
72 * Private data kept per client handle
77 struct sockaddr_in cu_raddr;
79 struct timeval cu_wait;
80 struct timeval cu_total;
81 struct rpc_err cu_error;
87 int cu_connect; /* Use connect(). */
88 int cu_connected; /* Have done connect(). */
93 * Create a UDP based client handle.
94 * If *sockp<0, *sockp is set to a newly created UPD socket.
95 * If raddr->sin_port is 0 a binder on the remote machine
96 * is consulted for the correct port number.
97 * NB: It is the clients responsibility to close *sockp.
98 * NB: The rpch->cl_auth is initialized to null authentication.
99 * Caller may wish to set this something more useful.
101 * wait is the amount of time used between retransmitting a call if
102 * no response has been heard; retransmition occurs until the actual
103 * rpc call times out.
105 * sendsz and recvsz are the maximum allowable packet sizes that can be
109 clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
110 struct sockaddr_in *raddr;
119 struct cu_data *cu = NULL;
121 struct rpc_msg call_msg;
122 static u_int32_t disrupt;
125 disrupt = (u_int32_t)(long)raddr;
127 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
129 (void) fprintf(stderr, "clntudp_create: out of memory\n");
130 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
131 rpc_createerr.cf_error.re_errno = errno;
134 sendsz = ((sendsz + 3) / 4) * 4;
135 recvsz = ((recvsz + 3) / 4) * 4;
136 cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
138 (void) fprintf(stderr, "clntudp_create: out of memory\n");
139 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
140 rpc_createerr.cf_error.re_errno = errno;
143 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
145 (void)gettimeofday(&now, (struct timezone *)0);
146 if (raddr->sin_port == 0) {
149 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
152 raddr->sin_port = htons(port);
154 cl->cl_ops = &udp_ops;
155 cl->cl_private = (caddr_t)cu;
156 cu->cu_raddr = *raddr;
157 cu->cu_rlen = sizeof (cu->cu_raddr);
159 cu->cu_total.tv_sec = -1;
160 cu->cu_total.tv_usec = -1;
161 cu->cu_sendsz = sendsz;
162 cu->cu_recvsz = recvsz;
163 cu->cu_connect = FALSE;
164 cu->cu_connected = FALSE;
165 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
166 call_msg.rm_direction = CALL;
167 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
168 call_msg.rm_call.cb_prog = program;
169 call_msg.rm_call.cb_vers = version;
170 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
172 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
175 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
179 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
181 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
182 rpc_createerr.cf_error.re_errno = errno;
185 /* attempt to bind to priv port */
186 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
187 /* the sockets rpc controls are non-blocking */
188 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
189 cu->cu_closeit = TRUE;
191 cu->cu_closeit = FALSE;
193 cu->cu_sock = *sockp;
194 cl->cl_auth = authnone_create();
198 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
200 mem_free((caddr_t)cl, sizeof(CLIENT));
201 return ((CLIENT *)NULL);
205 clntudp_create(raddr, program, version, wait, sockp)
206 struct sockaddr_in *raddr;
213 return(clntudp_bufcreate(raddr, program, version, wait, sockp,
214 UDPMSGSIZE, UDPMSGSIZE));
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 */
227 struct cu_data *cu = (struct cu_data *)cl->cl_private;
233 fd_set *fds, readfds;
234 struct sockaddr_in from;
235 struct rpc_msg reply_msg;
237 struct timeval time_waited, start, after, tmp1, tmp2, tv;
239 int nrefreshes = 2; /* number of times to refresh cred */
240 struct timeval timeout;
243 if (cu->cu_total.tv_usec == -1)
244 timeout = utimeout; /* use supplied timeout */
246 timeout = cu->cu_total; /* use default timeout */
248 if (cu->cu_sock + 1 > FD_SETSIZE) {
249 int bytes = howmany(cu->cu_sock + 1, NFDBITS) * sizeof(fd_mask);
250 fds = (fd_set *)malloc(bytes);
252 return (cu->cu_error.re_status = RPC_CANTSEND);
253 memset(fds, 0, bytes);
259 if (cu->cu_connect && !cu->cu_connected) {
260 if (connect(cu->cu_sock, (struct sockaddr *)&cu->cu_raddr,
262 cu->cu_error.re_errno = errno;
265 return (cu->cu_error.re_status = RPC_CANTSEND);
267 cu->cu_connected = 1;
269 if (cu->cu_connected) {
273 sa = (struct sockaddr *)&cu->cu_raddr;
276 timerclear(&time_waited);
279 xdrs = &(cu->cu_outxdrs);
280 xdrs->x_op = XDR_ENCODE;
281 XDR_SETPOS(xdrs, cu->cu_xdrpos);
283 * the transaction is the first thing in the out buffer
285 (*(u_short *)(cu->cu_outbuf))++;
286 if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
287 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
288 (! (*xargs)(xdrs, argsp))) {
291 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
293 outlen = (int)XDR_GETPOS(xdrs);
296 if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0, sa, salen)
298 cu->cu_error.re_errno = errno;
301 return (cu->cu_error.re_status = RPC_CANTSEND);
305 * Hack to provide rpc-based message passing
307 if (!timerisset(&timeout)) {
310 return (cu->cu_error.re_status = RPC_TIMEDOUT);
313 * sub-optimal code appears here because we have
314 * some clock time to spare while the packets are in flight.
315 * (We assume that this is actually only executed once.)
317 reply_msg.acpted_rply.ar_verf = _null_auth;
318 reply_msg.acpted_rply.ar_results.where = resultsp;
319 reply_msg.acpted_rply.ar_results.proc = xresults;
321 gettimeofday(&start, NULL);
323 /* XXX we know the other bits are still clear */
324 FD_SET(cu->cu_sock, fds);
326 switch (select(cu->cu_sock+1, fds, NULL, NULL, &tv)) {
329 timeradd(&time_waited, &cu->cu_wait, &tmp1);
331 if (timercmp(&time_waited, &timeout, <))
335 return (cu->cu_error.re_status = RPC_TIMEDOUT);
338 if (errno == EINTR) {
339 gettimeofday(&after, NULL);
340 timersub(&after, &start, &tmp1);
341 timeradd(&time_waited, &tmp1, &tmp2);
343 if (timercmp(&time_waited, &timeout, <))
347 return (cu->cu_error.re_status = RPC_TIMEDOUT);
349 cu->cu_error.re_errno = errno;
352 return (cu->cu_error.re_status = RPC_CANTRECV);
356 fromlen = sizeof(struct sockaddr);
357 inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
358 (int) cu->cu_recvsz, 0,
359 (struct sockaddr *)&from, &fromlen);
360 } while (inlen < 0 && errno == EINTR);
362 if (errno == EWOULDBLOCK)
364 cu->cu_error.re_errno = errno;
367 return (cu->cu_error.re_status = RPC_CANTRECV);
369 if (inlen < sizeof(u_int32_t))
371 /* see if reply transaction id matches sent id */
372 if (*((u_int32_t *)(cu->cu_inbuf)) != *((u_int32_t *)(cu->cu_outbuf)))
374 /* we now assume we have the proper reply */
379 * now decode and validate the response
381 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
382 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
383 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
385 _seterr_reply(&reply_msg, &(cu->cu_error));
386 if (cu->cu_error.re_status == RPC_SUCCESS) {
387 if (! AUTH_VALIDATE(cl->cl_auth,
388 &reply_msg.acpted_rply.ar_verf)) {
389 cu->cu_error.re_status = RPC_AUTHERROR;
390 cu->cu_error.re_why = AUTH_INVALIDRESP;
392 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
393 xdrs->x_op = XDR_FREE;
394 (void)xdr_opaque_auth(xdrs,
395 &(reply_msg.acpted_rply.ar_verf));
397 } /* end successful completion */
399 /* maybe our credentials need to be refreshed ... */
400 if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
404 } /* end of unsuccessful completion */
405 } /* end of valid reply message */
408 * It's possible for xdr_replymsg() to fail partway
409 * through its attempt to decode the result from the
410 * server. If this happens, it will leave the reply
411 * structure partially populated with dynamically
412 * allocated memory. (This can happen if someone uses
413 * clntudp_bufcreate() to create a CLIENT handle and
414 * specifies a receive buffer size that is too small.)
415 * This memory must be free()ed to avoid a leak.
417 int op = reply_xdrs.x_op;
418 reply_xdrs.x_op = XDR_FREE;
419 xdr_replymsg(&reply_xdrs, &reply_msg);
420 reply_xdrs.x_op = op;
421 cu->cu_error.re_status = RPC_CANTDECODERES;
425 return (cu->cu_error.re_status);
429 clntudp_geterr(cl, errp)
431 struct rpc_err *errp;
433 struct cu_data *cu = (struct cu_data *)cl->cl_private;
435 *errp = cu->cu_error;
440 clntudp_freeres(cl, xdr_res, res_ptr)
445 struct cu_data *cu = (struct cu_data *)cl->cl_private;
446 XDR *xdrs = &(cu->cu_outxdrs);
448 xdrs->x_op = XDR_FREE;
449 return ((*xdr_res)(xdrs, res_ptr));
460 clntudp_control(cl, request, info)
465 struct cu_data *cu = (struct cu_data *)cl->cl_private;
471 cu->cu_closeit = TRUE;
473 case CLSET_FD_NCLOSE:
474 cu->cu_closeit = FALSE;
479 tv = (struct timeval *)info;
480 cu->cu_total.tv_sec = tv->tv_sec;
481 cu->cu_total.tv_usec = tv->tv_usec;
486 *(struct timeval *)info = cu->cu_total;
488 case CLSET_RETRY_TIMEOUT:
491 tv = (struct timeval *)info;
492 cu->cu_wait.tv_sec = tv->tv_sec;
493 cu->cu_wait.tv_usec = tv->tv_usec;
495 case CLGET_RETRY_TIMEOUT:
498 *(struct timeval *)info = cu->cu_wait;
500 case CLGET_SERVER_ADDR:
503 *(struct sockaddr_in *)info = cu->cu_raddr;
508 *(int *)info = cu->cu_sock;
512 * use the knowledge that xid is the
513 * first element in the call structure *.
514 * This will get the xid of the PREVIOUS call
518 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
521 /* This will set the xid of the NEXT call */
524 *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1);
525 /* decrement by 1 as clntudp_call() increments once */
528 * This RELIES on the information that, in the call body,
529 * the version number field is the fifth 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 4 * BYTES_PER_XDR_UNIT));
541 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
542 = htonl(*(u_long *)info);
546 * This RELIES on the information that, in the call body,
547 * the program number field is the field from the
548 * begining of the RPC header. MUST be changed if the
549 * call_struct is changed
553 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
554 3 * BYTES_PER_XDR_UNIT));
559 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
560 = htonl(*(u_long *)info);
562 case CLGET_LOCAL_ADDR:
563 len = sizeof(struct sockaddr);
564 if (getsockname(cu->cu_sock, (struct sockaddr *)info, &len) <0)
568 cu->cu_connect = *(int *)(void *)info;
572 case CLSET_PUSH_TIMOD:
573 case CLSET_POP_TIMOD:
584 struct cu_data *cu = (struct cu_data *)cl->cl_private;
586 if (cu->cu_closeit) {
587 (void)_close(cu->cu_sock);
589 XDR_DESTROY(&(cu->cu_outxdrs));
590 mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
591 mem_free((caddr_t)cl, sizeof(CLIENT));