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);
91 struct timeval *timeout);
93 static struct clnt_ops sfs_cudp_ops = {
105 * Private data kept per client handle
110 struct sockaddr_in cu_raddr;
112 struct rpc_err cu_error;
122 * Create a UDP based client handle.
123 * If *sockp<0, *sockp is set to a newly created UPD socket.
124 * If raddr->sin_port is 0 a binder on the remote machine
125 * is consulted for the correct port number.
126 * NB: It is the clients responsibility to close *sockp.
127 * NB: The rpch->cl_auth is initialized to null authentication.
128 * Caller may wish to set this something more useful.
130 * wait is the amount of time used between retransmitting a call if
131 * no response has been heard; retransmition occurs until the actual
132 * rpc call times out.
134 * sendsz and recvsz are the maximum allowable packet sizes that can be
140 struct sockaddr_in *raddr,
151 struct rpc_msg call_msg;
157 #if defined(UNIXWARE) || defined(AIX)
163 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
165 (void) fprintf(stderr, "sfs_cudp_create: out of memory\n");
166 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
167 rpc_createerr.cf_error.re_errno = errno;
170 sendsz = ((sendsz + 3) / 4) * 4;
171 recvsz = ((recvsz + 3) / 4) * 4;
172 cu = (struct cu_data *)mem_alloc(sizeof(struct cu_data) +
175 (void) fprintf(stderr, "sfs_cudp_create: out of memory\n");
176 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
177 rpc_createerr.cf_error.re_errno = errno;
180 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
182 (void)gettimeofday(&now, (struct timezone *)0);
183 if (raddr->sin_port == 0) {
186 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
189 raddr->sin_port = htons(port);
191 cl->cl_ops = &sfs_cudp_ops;
192 cl->cl_private = (void *)cu;
193 cu->cu_raddr = *raddr;
194 cu->cu_rlen = sizeof (cu->cu_raddr);
195 cu->cu_sendsz = sendsz;
196 cu->cu_recvsz = recvsz;
197 call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
198 call_msg.rm_direction = CALL;
199 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
200 call_msg.rm_call.cb_prog = program;
201 call_msg.rm_call.cb_vers = version;
202 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
204 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
207 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
209 #if defined(O_NONBLOCK)
211 #elif defined(FIONBIO)
215 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
217 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
218 rpc_createerr.cf_error.re_errno = errno;
221 /* attempt to bind to prov port */
222 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
223 /* the sockets rpc controls are non-blocking */
224 #if defined(O_NONBLOCK)
225 flags = fcntl(*sockp, F_GETFL, 0) | O_NONBLOCK;
226 (void)fcntl(*sockp, F_SETFL, flags);
227 #elif defined(FIONBIO)
228 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
230 cu->cu_closeit = TRUE;
232 cu->cu_closeit = FALSE;
234 cu->cu_sock = *sockp;
236 * Need to try to size the socket buffers based on the number of
237 * outstanding requests desired. NFS reads and writes can do as
238 * much as 8K per request which can quickly run us out of space
239 * on the socket buffer queue. Use the maximum number of bio style
240 * requests * NFS_MAXDATA plus a pad as a starting point for desired
241 * socket buffer size and then back off by NFS_MAXDATA until the buffer
242 * sizes are successfully set. Note, the algorithm never sets the
243 * buffer size to less than the OS default.
246 for (i = 0; i < 2; i++) {
247 optlen = sizeof(min_buf_sz);
248 #if defined(UNIXWARE)
249 if (getsockopt(cu->cu_sock, SOL_SOCKET, type,
250 (void *)&min_buf_sz, &optlen) < 0) {
251 /* guess the default */
252 min_buf_sz = 18 * 1024;
255 if (getsockopt(cu->cu_sock, SOL_SOCKET, type,
256 (char *)&min_buf_sz, &optlen) < 0) {
257 /* guess the default */
258 min_buf_sz = 18 * 1024;
262 new_buf_sz = 512 * 1024;
263 if (new_buf_sz > min_buf_sz) {
265 error = setsockopt(cu->cu_sock, SOL_SOCKET,
266 type, (char *)&new_buf_sz,
268 new_buf_sz -= (8 * 1024);
269 } while (error != 0 && new_buf_sz > min_buf_sz);
275 cl->cl_auth = authnone_create();
279 mem_free((void *)cu, sizeof(struct cu_data) + sendsz + recvsz);
281 mem_free((void *)cl, sizeof(CLIENT));
282 return ((CLIENT *)NULL);
287 struct sockaddr_in *raddr,
294 return(sfs_cudp_bufcreate(raddr, program, version, wait, sockp,
295 UDPMSGSIZE, UDPMSGSIZE));
299 enum clnt_stat get_areply_udp (
302 struct timeval *timeout)
304 return get_areply (cl, xid, timeout);
308 static enum clnt_stat
312 struct timeval *timeout)
314 struct cu_data *cu = (struct cu_data *)cl->cl_private;
321 struct sockaddr_in from;
328 #endif /* def FD_SETSIZE */
332 FD_SET(cu->cu_sock, &mask);
334 mask = 1 << cu->cu_sock;
335 #endif /* def FD_SETSIZE */
339 switch (select(_rpc_dtablesize(), &readfds, NULL,
343 return (cu->cu_error.re_status = RPC_TIMEDOUT);
348 cu->cu_error.re_errno = errno;
349 return (cu->cu_error.re_status = RPC_CANTRECV);
352 fromlen = sizeof(struct sockaddr);
353 inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
354 (int) cu->cu_recvsz, 0,
355 (struct sockaddr *)&from, &fromlen);
356 } while (inlen < 0 && errno == EINTR);
358 if (errno == EWOULDBLOCK)
360 cu->cu_error.re_errno = errno;
361 return (cu->cu_error.re_status = RPC_CANTRECV);
364 if (inlen < sizeof(uint32_t))
367 *xid = ntohl(*((uint32_t *)(cu->cu_inbuf)));
368 return (RPC_SUCCESS);
375 xdrproc_t xdr_results,
378 struct cu_data *cu = (struct cu_data *)cl->cl_private;
379 XDR *xdrs = &(cu->cu_outxdrs);
380 struct rpc_msg reply_msg;
385 * now decode and validate the response
387 xdrmem_create(&reply_xdrs, cu->cu_inbuf, cu->cu_recvsz, XDR_DECODE);
389 reply_msg.acpted_rply.ar_verf = _null_auth;
390 reply_msg.acpted_rply.ar_results.where = results_ptr;
391 reply_msg.acpted_rply.ar_results.proc = xdr_results;
393 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
394 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
396 return (cu->cu_error.re_status = RPC_CANTDECODERES);
399 _seterr_reply(&reply_msg, &(cu->cu_error));
401 if (cu->cu_error.re_status == RPC_SUCCESS) {
402 if (! AUTH_VALIDATE(cl->cl_auth,
403 &reply_msg.acpted_rply.ar_verf)) {
404 cu->cu_error.re_status = RPC_AUTHERROR;
405 cu->cu_error.re_why = AUTH_INVALIDRESP;
407 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
408 xdrs->x_op = XDR_FREE;
409 (void)xdr_opaque_auth(xdrs,
410 &(reply_msg.acpted_rply.ar_verf));
414 return (cu->cu_error.re_status);
418 * Non-standard changes. Make a call an at-most-once with a per call
419 * timer. Ignore the timeout set at creation. Never retransmit.
421 static enum clnt_stat
423 CLIENT *cl, /* client handle */
424 uint32_t proc, /* procedure number */
425 xdrproc_t xargs, /* xdr routine for args */
426 void * argsp, /* pointer to args */
427 xdrproc_t xresults, /* xdr routine for results */
428 void * resultsp, /* pointer to results */
429 struct timeval timeout) /* seconds to wait before giving up */
431 struct cu_data *cu = (struct cu_data *)cl->cl_private;
432 XDR *xdrs = &(cu->cu_outxdrs);
434 uint32_t x_id, r_xid;
436 xdrs->x_op = XDR_ENCODE;
437 XDR_SETPOS(xdrs, cu->cu_xdrpos);
440 * the transaction is the first thing in the out buffer
442 (*(uint32_t *)(cu->cu_outbuf))++;
443 x_id = ntohl(*(uint32_t *)(cu->cu_outbuf));
445 if ((! XDR_PUTLONG(xdrs, (int32_t *)&proc)) ||
446 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
447 (! (*xargs)(xdrs, argsp)))
448 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
450 outlen = (int)XDR_GETPOS(xdrs);
452 if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
453 (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen) != outlen) {
454 cu->cu_error.re_errno = errno;
455 return (cu->cu_error.re_status = RPC_CANTSEND);
459 * Hack to provide rpc-based message passing
461 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
463 * Double hack, send back xid in results_prt if non-NULL
465 if (resultsp != NULL)
466 *(uint32_t *)resultsp = x_id;
468 return (cu->cu_error.re_status = RPC_TIMEDOUT);
475 if ((res = get_areply(cl, &r_xid, &timeout)) != RPC_SUCCESS)
485 return (proc_header(cl, xresults, resultsp));
489 sfs_cudp_geterr(CLIENT *cl, struct rpc_err *errp)
491 struct cu_data *cu = (struct cu_data *)cl->cl_private;
493 *errp = cu->cu_error;
503 struct cu_data *cu = (struct cu_data *)cl->cl_private;
504 XDR *xdrs = &(cu->cu_outxdrs);
506 xdrs->x_op = XDR_FREE;
507 return ((*xdr_res)(xdrs, res_ptr));
512 sfs_cudp_abort(CLIENT *h)
522 struct cu_data *cu = (struct cu_data *)cl->cl_private;
525 case CLGET_SERVER_ADDR:
526 *(struct sockaddr_in *)info = cu->cu_raddr;
535 sfs_cudp_destroy(CLIENT *cl)
537 struct cu_data *cu = (struct cu_data *)cl->cl_private;
539 if (cu->cu_closeit) {
540 (void)close(cu->cu_sock);
542 XDR_DESTROY(&(cu->cu_outxdrs));
543 mem_free((void *)cu, (sizeof(struct cu_data) + cu->cu_sendsz + cu->cu_recvsz));
544 mem_free((void *)cl, sizeof(CLIENT));
558 struct cu_data *cu = (struct cu_data *)cl->cl_private;
562 cu->cu_error.re_status = RPC_SUCCESS;
564 if ((res = get_areply(cl, xid, tv)) != RPC_SUCCESS)
568 * Check to make sure xid matchs one that we are interested in
570 for (i = 0; i < cnt; i++) {
576 return (RPC_CANTDECODERES);
581 return (proc_header(cl, xproc, xres));
589 struct cu_data *cu = (struct cu_data *)cl->cl_private;
596 FD_SET(cu->cu_sock, &mask);
598 int mask = 1 << (cu->cu_sock);
600 #endif /* def FD_SETSIZE */
604 tv.tv_sec = usecs / 1000000;
605 tv.tv_usec = usecs % 1000000;
608 return (select(_rpc_dtablesize(), &readfds, NULL, NULL, &tv));