2 static char sfs_cudp_id[] = "@(#)sfs_cudp.c 2.1 97/10/23";
5 /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
7 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
9 * Standard Performance Evaluation Corporation (SPEC)
10 * 6585 Merchant Place, Suite 100
13 * This product contains benchmarks acquired from several sources who
14 * understand and agree with SPEC's goal of creating fair and objective
15 * benchmarks to measure computer performance.
17 * This copyright notice is placed here only to protect SPEC in the
18 * event the source is misused in any manner that is contrary to the
19 * spirit, the goals and the intent of SPEC.
21 * The source code is provided to the user or company under the license
22 * agreement for the SPEC Benchmark Suite for this product.
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
54 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
56 * Copyright (C) 1984, Sun Microsystems, Inc.
65 #endif /* ndef FreeBSD */
68 #include "rpc/osdep.h"
70 #include "rpc/pmap_clnt.h"
72 #include "rfs_c_def.h" /* Just for the define of RFS */
74 * UDP bases client side rpc operations
76 static enum clnt_stat sfs_cudp_call(CLIENT *, uint32_t, xdrproc_t,
77 void *, xdrproc_t, void *,
79 static void sfs_cudp_abort(CLIENT *h);
80 static void sfs_cudp_geterr(CLIENT *, struct rpc_err *);
81 static bool_t sfs_cudp_freeres(CLIENT *, xdrproc_t, void *);
82 static bool_t sfs_cudp_control(CLIENT *, uint_t, void *);
83 static void sfs_cudp_destroy(CLIENT *);
84 static bool_t sfs_cudp_getreply(CLIENT *, xdrproc_t, void *,
85 int, uint32_t *, uint32_t *, struct timeval *);
86 static int sfs_cudp_poll(CLIENT *, uint32_t);
88 static struct clnt_ops sfs_cudp_ops = {
100 * Private data kept per client handle
105 struct sockaddr_in cu_raddr;
107 struct rpc_err cu_error;
117 * Create a UDP based client handle.
118 * If *sockp<0, *sockp is set to a newly created UPD socket.
119 * If raddr->sin_port is 0 a binder on the remote machine
120 * is consulted for the correct port number.
121 * NB: It is the clients responsibility to close *sockp.
122 * NB: The rpch->cl_auth is initialized to null authentication.
123 * Caller may wish to set this something more useful.
125 * wait is the amount of time used between retransmitting a call if
126 * no response has been heard; retransmition occurs until the actual
127 * rpc call times out.
129 * sendsz and recvsz are the maximum allowable packet sizes that can be
135 struct sockaddr_in *raddr,
146 struct rpc_msg call_msg;
152 #if defined(UNIXWARE) || defined(AIX)
158 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
160 (void) fprintf(stderr, "sfs_cudp_create: out of memory\n");
161 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
162 rpc_createerr.cf_error.re_errno = errno;
165 sendsz = ((sendsz + 3) / 4) * 4;
166 recvsz = ((recvsz + 3) / 4) * 4;
167 cu = (struct cu_data *)mem_alloc(sizeof(struct cu_data) +
170 (void) fprintf(stderr, "sfs_cudp_create: out of memory\n");
171 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
172 rpc_createerr.cf_error.re_errno = errno;
175 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
177 (void)gettimeofday(&now, (struct timezone *)0);
178 if (raddr->sin_port == 0) {
181 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
184 raddr->sin_port = htons(port);
186 cl->cl_ops = &sfs_cudp_ops;
187 cl->cl_private = (void *)cu;
188 cu->cu_raddr = *raddr;
189 cu->cu_rlen = sizeof (cu->cu_raddr);
190 cu->cu_sendsz = sendsz;
191 cu->cu_recvsz = recvsz;
192 call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
193 call_msg.rm_direction = CALL;
194 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
195 call_msg.rm_call.cb_prog = program;
196 call_msg.rm_call.cb_vers = version;
197 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
199 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
202 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
204 #if defined(O_NONBLOCK)
206 #elif defined(FIONBIO)
210 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
212 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
213 rpc_createerr.cf_error.re_errno = errno;
216 /* attempt to bind to prov port */
217 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
218 /* the sockets rpc controls are non-blocking */
219 #if defined(O_NONBLOCK)
220 flags = fcntl(*sockp, F_GETFL, 0) | O_NONBLOCK;
221 (void)fcntl(*sockp, F_SETFL, flags);
222 #elif defined(FIONBIO)
223 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
225 cu->cu_closeit = TRUE;
227 cu->cu_closeit = FALSE;
229 cu->cu_sock = *sockp;
231 * Need to try to size the socket buffers based on the number of
232 * outstanding requests desired. NFS reads and writes can do as
233 * much as 8K per request which can quickly run us out of space
234 * on the socket buffer queue. Use the maximum number of bio style
235 * requests * NFS_MAXDATA plus a pad as a starting point for desired
236 * socket buffer size and then back off by NFS_MAXDATA until the buffer
237 * sizes are successfully set. Note, the algorithm never sets the
238 * buffer size to less than the OS default.
241 for (i = 0; i < 2; i++) {
242 optlen = sizeof(min_buf_sz);
243 #if defined(UNIXWARE)
244 if (getsockopt(cu->cu_sock, SOL_SOCKET, type,
245 (void *)&min_buf_sz, &optlen) < 0) {
246 /* guess the default */
247 min_buf_sz = 18 * 1024;
250 if (getsockopt(cu->cu_sock, SOL_SOCKET, type,
251 (char *)&min_buf_sz, &optlen) < 0) {
252 /* guess the default */
253 min_buf_sz = 18 * 1024;
257 new_buf_sz = 512 * 1024;
258 if (new_buf_sz > min_buf_sz) {
260 error = setsockopt(cu->cu_sock, SOL_SOCKET,
261 type, (char *)&new_buf_sz,
263 new_buf_sz -= (8 * 1024);
264 } while (error != 0 && new_buf_sz > min_buf_sz);
270 cl->cl_auth = authnone_create();
274 mem_free((void *)cu, sizeof(struct cu_data) + sendsz + recvsz);
276 mem_free((void *)cl, sizeof(CLIENT));
277 return ((CLIENT *)NULL);
282 struct sockaddr_in *raddr,
289 return(sfs_cudp_bufcreate(raddr, program, version, wait, sockp,
290 UDPMSGSIZE, UDPMSGSIZE));
294 enum clnt_stat get_areply_udp (
297 struct timeval *timeout)
299 return get_areply (cl, xid, timeout);
303 static enum clnt_stat
307 struct timeval *timeout)
309 struct cu_data *cu = (struct cu_data *)cl->cl_private;
316 struct sockaddr_in from;
323 #endif /* def FD_SETSIZE */
327 FD_SET(cu->cu_sock, &mask);
329 mask = 1 << cu->cu_sock;
330 #endif /* def FD_SETSIZE */
334 switch (select(_rpc_dtablesize(), &readfds, NULL,
338 return (cu->cu_error.re_status = RPC_TIMEDOUT);
343 cu->cu_error.re_errno = errno;
344 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;
356 return (cu->cu_error.re_status = RPC_CANTRECV);
359 if (inlen < sizeof(uint32_t))
362 *xid = ntohl(*((uint32_t *)(cu->cu_inbuf)));
363 return (RPC_SUCCESS);
370 xdrproc_t xdr_results,
373 struct cu_data *cu = (struct cu_data *)cl->cl_private;
374 XDR *xdrs = &(cu->cu_outxdrs);
375 struct rpc_msg reply_msg;
380 * now decode and validate the response
382 xdrmem_create(&reply_xdrs, cu->cu_inbuf, cu->cu_recvsz, XDR_DECODE);
384 reply_msg.acpted_rply.ar_verf = _null_auth;
385 reply_msg.acpted_rply.ar_results.where = results_ptr;
386 reply_msg.acpted_rply.ar_results.proc = xdr_results;
388 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
389 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
391 return (cu->cu_error.re_status = RPC_CANTDECODERES);
394 _seterr_reply(&reply_msg, &(cu->cu_error));
396 if (cu->cu_error.re_status == RPC_SUCCESS) {
397 if (! AUTH_VALIDATE(cl->cl_auth,
398 &reply_msg.acpted_rply.ar_verf)) {
399 cu->cu_error.re_status = RPC_AUTHERROR;
400 cu->cu_error.re_why = AUTH_INVALIDRESP;
402 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
403 xdrs->x_op = XDR_FREE;
404 (void)xdr_opaque_auth(xdrs,
405 &(reply_msg.acpted_rply.ar_verf));
409 return (cu->cu_error.re_status);
413 * Non-standard changes. Make a call an at-most-once with a per call
414 * timer. Ignore the timeout set at creation. Never retransmit.
416 static enum clnt_stat
418 CLIENT *cl, /* client handle */
419 uint32_t proc, /* procedure number */
420 xdrproc_t xargs, /* xdr routine for args */
421 void * argsp, /* pointer to args */
422 xdrproc_t xresults, /* xdr routine for results */
423 void * resultsp, /* pointer to results */
424 struct timeval timeout) /* seconds to wait before giving up */
426 struct cu_data *cu = (struct cu_data *)cl->cl_private;
427 XDR *xdrs = &(cu->cu_outxdrs);
429 uint32_t x_id, r_xid;
431 xdrs->x_op = XDR_ENCODE;
432 XDR_SETPOS(xdrs, cu->cu_xdrpos);
435 * the transaction is the first thing in the out buffer
437 (*(uint32_t *)(cu->cu_outbuf))++;
438 x_id = ntohl(*(uint32_t *)(cu->cu_outbuf));
440 if ((! XDR_PUTLONG(xdrs, (int32_t *)&proc)) ||
441 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
442 (! (*xargs)(xdrs, argsp)))
443 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
445 outlen = (int)XDR_GETPOS(xdrs);
447 if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
448 (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen) != outlen) {
449 cu->cu_error.re_errno = errno;
450 return (cu->cu_error.re_status = RPC_CANTSEND);
454 * Hack to provide rpc-based message passing
456 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
458 * Double hack, send back xid in results_prt if non-NULL
460 if (resultsp != NULL)
461 *(uint32_t *)resultsp = x_id;
463 return (cu->cu_error.re_status = RPC_TIMEDOUT);
470 if ((res = get_areply(cl, &r_xid, &timeout)) != RPC_SUCCESS)
480 return (proc_header(cl, xresults, resultsp));
484 sfs_cudp_geterr(CLIENT *cl, struct rpc_err *errp)
486 struct cu_data *cu = (struct cu_data *)cl->cl_private;
488 *errp = cu->cu_error;
498 struct cu_data *cu = (struct cu_data *)cl->cl_private;
499 XDR *xdrs = &(cu->cu_outxdrs);
501 xdrs->x_op = XDR_FREE;
502 return ((*xdr_res)(xdrs, res_ptr));
507 sfs_cudp_abort(CLIENT *h)
517 struct cu_data *cu = (struct cu_data *)cl->cl_private;
520 case CLGET_SERVER_ADDR:
521 *(struct sockaddr_in *)info = cu->cu_raddr;
530 sfs_cudp_destroy(CLIENT *cl)
532 struct cu_data *cu = (struct cu_data *)cl->cl_private;
534 if (cu->cu_closeit) {
535 (void)close(cu->cu_sock);
537 XDR_DESTROY(&(cu->cu_outxdrs));
538 mem_free((void *)cu, (sizeof(struct cu_data) + cu->cu_sendsz + cu->cu_recvsz));
539 mem_free((void *)cl, sizeof(CLIENT));
553 struct cu_data *cu = (struct cu_data *)cl->cl_private;
557 cu->cu_error.re_status = RPC_SUCCESS;
559 if ((res = get_areply(cl, xid, tv)) != RPC_SUCCESS)
563 * Check to make sure xid matchs one that we are interested in
565 for (i = 0; i < cnt; i++) {
571 return (RPC_CANTDECODERES);
576 return (proc_header(cl, xproc, xres));
584 struct cu_data *cu = (struct cu_data *)cl->cl_private;
591 FD_SET(cu->cu_sock, &mask);
593 int mask = 1 << (cu->cu_sock);
595 #endif /* def FD_SETSIZE */
599 tv.tv_sec = usecs / 1000000;
600 tv.tv_usec = usecs % 1000000;
603 return (select(_rpc_dtablesize(), &readfds, NULL, NULL, &tv));