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.
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
45 #include <sys/filio.h>
49 #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;
84 unsigned int cu_xdrpos;
85 unsigned int cu_sendsz;
87 unsigned int cu_recvsz;
92 * Create a UDP based client handle.
93 * If *sockp<0, *sockp is set to a newly created UPD socket.
94 * If raddr->sin_port is 0 a binder on the remote machine
95 * is consulted for the correct port number.
96 * NB: It is the clients responsibility to close *sockp.
97 * NB: The rpch->cl_auth is initialized to null authentication.
98 * Caller may wish to set this something more useful.
100 * wait is the amount of time used between retransmitting a call if
101 * no response has been heard; retransmition occurs until the actual
102 * rpc call times out.
104 * sendsz and recvsz are the maximum allowable packet sizes that can be
108 clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
109 struct sockaddr_in *raddr;
118 register struct cu_data *cu;
120 struct rpc_msg call_msg;
122 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
124 (void) fprintf(stderr, "clntudp_create: out of memory\n");
125 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
126 rpc_createerr.cf_error.re_errno = errno;
129 sendsz = ((sendsz + 3) / 4) * 4;
130 recvsz = ((recvsz + 3) / 4) * 4;
131 cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
133 (void) fprintf(stderr, "clntudp_create: out of memory\n");
134 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
135 rpc_createerr.cf_error.re_errno = errno;
138 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
140 (void)gettimeofday(&now, (struct timezone *)0);
141 if (raddr->sin_port == 0) {
144 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
147 raddr->sin_port = htons(port);
149 cl->cl_ops = &udp_ops;
150 cl->cl_private = (caddr_t)cu;
151 cu->cu_raddr = *raddr;
152 cu->cu_rlen = sizeof (cu->cu_raddr);
154 cu->cu_total.tv_sec = -1;
155 cu->cu_total.tv_usec = -1;
156 cu->cu_sendsz = sendsz;
157 cu->cu_recvsz = recvsz;
158 call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
159 call_msg.rm_direction = CALL;
160 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
161 call_msg.rm_call.cb_prog = program;
162 call_msg.rm_call.cb_vers = version;
163 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
165 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
168 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
172 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
174 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
175 rpc_createerr.cf_error.re_errno = errno;
178 /* attempt to bind to prov port */
179 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
180 /* the sockets rpc controls are non-blocking */
181 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
182 cu->cu_closeit = TRUE;
184 cu->cu_closeit = FALSE;
186 cu->cu_sock = *sockp;
187 cl->cl_auth = authnone_create();
191 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
193 mem_free((caddr_t)cl, sizeof(CLIENT));
194 return ((CLIENT *)NULL);
198 clntudp_create(raddr, program, version, wait, sockp)
199 struct sockaddr_in *raddr;
206 return(clntudp_bufcreate(raddr, program, version, wait, sockp,
207 UDPMSGSIZE, UDPMSGSIZE));
210 static enum clnt_stat
211 clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
212 register CLIENT *cl; /* client handle */
213 rpc_u_int32 proc; /* procedure number */
214 xdrproc_t xargs; /* xdr routine for args */
215 caddr_t argsp; /* pointer to args */
216 xdrproc_t xresults; /* xdr routine for results */
217 caddr_t resultsp; /* pointer to results */
218 struct timeval utimeout; /* seconds to wait before giving up */
220 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
231 #endif /* def FD_SETSIZE */
232 struct sockaddr_in from;
233 struct rpc_msg reply_msg;
235 struct timeval time_waited;
237 int nrefreshes = 2; /* number of times to refresh cred */
238 struct timeval timeout;
241 if (cu->cu_total.tv_usec == -1) {
242 timeout = utimeout; /* use supplied timeout */
244 timeout = cu->cu_total; /* use default timeout */
247 time_waited.tv_sec = 0;
248 time_waited.tv_usec = 0;
250 xdrs = &(cu->cu_outxdrs);
251 xdrs->x_op = XDR_ENCODE;
252 XDR_SETPOS(xdrs, cu->cu_xdrpos);
254 * the transaction is the first thing in the out buffer
256 (*(unsigned short *)(cu->cu_outbuf))++;
257 if ((! XDR_PUTLONG(xdrs, &procl)) ||
258 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
259 (! AUTH_WRAP(cl->cl_auth, xdrs, xargs, argsp)))
260 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
261 outlen = (int)XDR_GETPOS(xdrs);
264 if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
265 (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
267 cu->cu_error.re_errno = errno;
268 return (cu->cu_error.re_status = RPC_CANTSEND);
272 * Hack to provide rpc-based message passing
274 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
275 return (cu->cu_error.re_status = RPC_TIMEDOUT);
278 * sub-optimal code appears here because we have
279 * some clock time to spare while the packets are in flight.
280 * (We assume that this is actually only executed once.)
282 reply_msg.acpted_rply.ar_verf = _null_auth;
283 reply_msg.acpted_rply.ar_results.where = NULL;
284 reply_msg.acpted_rply.ar_results.proc = xdr_void;
287 FD_SET(cu->cu_sock, &mask);
289 mask = 1 << cu->cu_sock;
290 #endif /* def FD_SETSIZE */
293 switch (select(_rpc_dtablesize(), &readfds, (fd_set *)NULL,
294 (fd_set *)NULL, &(cu->cu_wait))) {
297 time_waited.tv_sec += cu->cu_wait.tv_sec;
298 time_waited.tv_usec += cu->cu_wait.tv_usec;
299 while (time_waited.tv_usec >= 1000000) {
300 time_waited.tv_sec++;
301 time_waited.tv_usec -= 1000000;
303 if ((time_waited.tv_sec < timeout.tv_sec) ||
304 ((time_waited.tv_sec == timeout.tv_sec) &&
305 (time_waited.tv_usec < timeout.tv_usec)))
307 return (cu->cu_error.re_status = RPC_TIMEDOUT);
310 * buggy in other cases because time_waited is not being
316 cu->cu_error.re_errno = errno;
317 return (cu->cu_error.re_status = RPC_CANTRECV);
320 fromlen = sizeof(struct sockaddr);
321 inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
322 (int) cu->cu_recvsz, 0,
323 (struct sockaddr *)&from, &fromlen);
324 } while (inlen < 0 && errno == EINTR);
326 if (errno == EWOULDBLOCK)
328 cu->cu_error.re_errno = errno;
329 return (cu->cu_error.re_status = RPC_CANTRECV);
331 if (inlen < sizeof(rpc_u_int32))
333 /* see if reply transaction id matches sent id */
334 if (*((rpc_u_int32 *)(cu->cu_inbuf)) != *((rpc_u_int32 *)(cu->cu_outbuf)))
336 /* we now assume we have the proper reply */
341 * now decode and validate the response
343 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (unsigned int)inlen, XDR_DECODE);
344 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
345 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
347 sunrpc_seterr_reply(&reply_msg, &(cu->cu_error));
348 if (cu->cu_error.re_status == RPC_SUCCESS) {
349 if (! AUTH_VALIDATE(cl->cl_auth,
350 &reply_msg.acpted_rply.ar_verf)) {
351 cu->cu_error.re_status = RPC_AUTHERROR;
352 cu->cu_error.re_why = AUTH_INVALIDRESP;
353 } else if (! AUTH_UNWRAP(cl->cl_auth, &reply_xdrs,
354 xresults, resultsp)) {
355 if (cu->cu_error.re_status == RPC_SUCCESS)
356 cu->cu_error.re_status = RPC_CANTDECODERES;
358 } /* end successful completion */
360 /* maybe our credentials need to be refreshed ... */
361 if (nrefreshes > 0 &&
362 AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
366 } /* end of unsuccessful completion */
368 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
369 (reply_msg.acpted_rply.ar_verf.oa_base != NULL)) {
370 xdrs->x_op = XDR_FREE;
371 (void)xdr_opaque_auth(xdrs,
372 &(reply_msg.acpted_rply.ar_verf));
374 } /* end of valid reply message */
376 cu->cu_error.re_status = RPC_CANTDECODERES;
378 return (cu->cu_error.re_status);
382 clntudp_geterr(cl, errp)
384 struct rpc_err *errp;
386 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
388 *errp = cu->cu_error;
393 clntudp_freeres(cl, xdr_res, res_ptr)
398 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
399 register XDR *xdrs = &(cu->cu_outxdrs);
401 xdrs->x_op = XDR_FREE;
402 return ((*xdr_res)(xdrs, res_ptr));
412 clntudp_control(cl, request, info)
417 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
422 cu->cu_total = *(struct timeval *)info;
425 *(struct timeval *)info = cu->cu_total;
427 case CLSET_RETRY_TIMEOUT:
428 cu->cu_wait = *(struct timeval *)info;
430 case CLGET_RETRY_TIMEOUT:
431 *(struct timeval *)info = cu->cu_wait;
433 case CLGET_SERVER_ADDR:
434 *(struct sockaddr_in *)info = cu->cu_raddr;
436 case CLGET_LOCAL_ADDR:
437 len = sizeof(struct sockaddr);
438 if (getsockname(cu->cu_sock, (struct sockaddr*)info, &len) < 0)
452 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
454 if (cu->cu_closeit) {
455 (void)close(cu->cu_sock);
457 XDR_DESTROY(&(cu->cu_outxdrs));
458 mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
459 mem_free((caddr_t)cl, sizeof(CLIENT));