xref: /onnv-gate/usr/src/lib/libnsl/rpc/clnt_raw.c (revision 132:e3f7eaf7dde4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
21*132Srobinson  */
22*132Srobinson 
23*132Srobinson /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * clnt_raw.c
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * Memory based rpc for simple testing and timing.
410Sstevel@tonic-gate  * Interface to create an rpc client and server in the same process.
420Sstevel@tonic-gate  * This lets us similate rpc and get round trip overhead, without
430Sstevel@tonic-gate  * any interference from the kernel.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate #include "mt.h"
460Sstevel@tonic-gate #include "rpc_mt.h"
47*132Srobinson #include <stdlib.h>
480Sstevel@tonic-gate #include <rpc/rpc.h>
490Sstevel@tonic-gate #include <rpc/raw.h>
500Sstevel@tonic-gate #include <syslog.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate extern mutex_t	clntraw_lock;
530Sstevel@tonic-gate #define	MCALL_MSG_SIZE 24
540Sstevel@tonic-gate #ifndef UDPMSGSIZE
550Sstevel@tonic-gate #define	UDPMSGSIZE 8800
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * This is the "network" we will be moving stuff over.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate static struct clnt_raw_private {
620Sstevel@tonic-gate 	CLIENT	client_object;
630Sstevel@tonic-gate 	XDR	xdr_stream;
640Sstevel@tonic-gate 	char	*raw_buf;	/* should be shared with server handle */
650Sstevel@tonic-gate 	char	mashl_callmsg[MCALL_MSG_SIZE];
660Sstevel@tonic-gate 	uint_t	mcnt;
670Sstevel@tonic-gate } *clnt_raw_private;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static struct clnt_ops *clnt_raw_ops();
700Sstevel@tonic-gate 
710Sstevel@tonic-gate extern void svc_getreq_common(int);
720Sstevel@tonic-gate extern bool_t xdr_opaque_auth();
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Create a client handle for memory based rpc.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate CLIENT *
clnt_raw_create(const rpcprog_t prog,const rpcvers_t vers)78*132Srobinson clnt_raw_create(const rpcprog_t prog, const rpcvers_t vers)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	struct clnt_raw_private *clp;
810Sstevel@tonic-gate 	struct rpc_msg call_msg;
820Sstevel@tonic-gate 	XDR *xdrs;
830Sstevel@tonic-gate 	CLIENT *client;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* VARIABLES PROTECTED BY clntraw_lock: clp */
860Sstevel@tonic-gate 
87*132Srobinson 	(void) mutex_lock(&clntraw_lock);
880Sstevel@tonic-gate 	clp = clnt_raw_private;
890Sstevel@tonic-gate 	if (clp == NULL) {
90*132Srobinson 		clp = calloc(1, sizeof (*clp));
910Sstevel@tonic-gate 		if (clp == NULL) {
92*132Srobinson 			(void) mutex_unlock(&clntraw_lock);
93*132Srobinson 			return (NULL);
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 		if (_rawcombuf == NULL) {
96*132Srobinson 			_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
970Sstevel@tonic-gate 			if (_rawcombuf == NULL) {
980Sstevel@tonic-gate 				syslog(LOG_ERR, "clnt_raw_create: "
990Sstevel@tonic-gate 					"out of memory.");
1000Sstevel@tonic-gate 				if (clp)
1010Sstevel@tonic-gate 					free(clp);
102*132Srobinson 				(void) mutex_unlock(&clntraw_lock);
103*132Srobinson 				return (NULL);
1040Sstevel@tonic-gate 			}
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 		clp->raw_buf = _rawcombuf; /* Share it with the server */
1070Sstevel@tonic-gate 		clnt_raw_private = clp;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	xdrs = &clp->xdr_stream;
1100Sstevel@tonic-gate 	client = &clp->client_object;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * pre-serialize the static part of the call msg and stash it away
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
1160Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
1170Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = prog;
1180Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = vers;
1190Sstevel@tonic-gate 	xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
120*132Srobinson 	if (!xdr_callhdr(xdrs, &call_msg))
1210Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
1220Sstevel@tonic-gate 			(const char *) "clnt_raw_create :  \
1230Sstevel@tonic-gate 			Fatal header serialization error.");
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	clp->mcnt = XDR_GETPOS(xdrs);
1260Sstevel@tonic-gate 	XDR_DESTROY(xdrs);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * Set xdrmem for client/server shared buffer
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	xdrmem_create(xdrs, clp->raw_buf, UDPMSGSIZE, XDR_FREE);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * create client handle
1350Sstevel@tonic-gate 	 */
1360Sstevel@tonic-gate 	client->cl_ops = clnt_raw_ops();
1370Sstevel@tonic-gate 	client->cl_auth = authnone_create();
138*132Srobinson 	(void) mutex_unlock(&clntraw_lock);
1390Sstevel@tonic-gate 	return (client);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*ARGSUSED*/
1430Sstevel@tonic-gate static enum clnt_stat
clnt_raw_call(CLIENT * h,rpcproc_t proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xresults,caddr_t resultsp,struct timeval timeout)1440Sstevel@tonic-gate clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, caddr_t argsp,
1450Sstevel@tonic-gate 	xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	struct clnt_raw_private *clp;
1480Sstevel@tonic-gate 	XDR *xdrs;
1490Sstevel@tonic-gate 	struct rpc_msg msg;
1500Sstevel@tonic-gate 	enum clnt_stat status;
1510Sstevel@tonic-gate 	struct rpc_err error;
1520Sstevel@tonic-gate 
153*132Srobinson 	(void) mutex_lock(&clntraw_lock);
1540Sstevel@tonic-gate 	clp = clnt_raw_private;
1550Sstevel@tonic-gate 	xdrs = &clp->xdr_stream;
1560Sstevel@tonic-gate 	if (clp == NULL) {
157*132Srobinson 		(void) mutex_unlock(&clntraw_lock);
1580Sstevel@tonic-gate 		return (RPC_FAILED);
1590Sstevel@tonic-gate 	}
160*132Srobinson 	(void) mutex_unlock(&clntraw_lock);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate call_again:
1630Sstevel@tonic-gate 	/*
1640Sstevel@tonic-gate 	 * send request
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
1670Sstevel@tonic-gate 	XDR_SETPOS(xdrs, 0);
1680Sstevel@tonic-gate /* LINTED pointer alignment */
1690Sstevel@tonic-gate 	((struct rpc_msg *)clp->mashl_callmsg)->rm_xid++;
170*132Srobinson 	if ((!XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
171*132Srobinson 	    (!XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
172*132Srobinson 	    (!AUTH_MARSHALL(h->cl_auth, xdrs)) ||
173*132Srobinson 	    (!(*xargs)(xdrs, argsp)))
1740Sstevel@tonic-gate 		return (RPC_CANTENCODEARGS);
1750Sstevel@tonic-gate 	(void) XDR_GETPOS(xdrs);  /* called just to cause overhead */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * We have to call server input routine here because this is
1790Sstevel@tonic-gate 	 * all going on in one process.
1800Sstevel@tonic-gate 	 * By convention using FD_SETSIZE as the psuedo file descriptor.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	svc_getreq_common(FD_SETSIZE);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * get results
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	xdrs->x_op = XDR_DECODE;
1880Sstevel@tonic-gate 	XDR_SETPOS(xdrs, 0);
1890Sstevel@tonic-gate 	msg.acpted_rply.ar_verf = _null_auth;
1900Sstevel@tonic-gate 	msg.acpted_rply.ar_results.where = resultsp;
1910Sstevel@tonic-gate 	msg.acpted_rply.ar_results.proc = xresults;
192*132Srobinson 	if (!xdr_replymsg(xdrs, &msg))
1930Sstevel@tonic-gate 		return (RPC_CANTDECODERES);
1940Sstevel@tonic-gate 	if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
1950Sstevel@tonic-gate 		    (msg.acpted_rply.ar_stat == SUCCESS))
1960Sstevel@tonic-gate 		status = RPC_SUCCESS;
1970Sstevel@tonic-gate 	else {
1980Sstevel@tonic-gate 		__seterr_reply(&msg, &error);
1990Sstevel@tonic-gate 		status = error.re_status;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (status == RPC_SUCCESS) {
203*132Srobinson 		if (!AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
2040Sstevel@tonic-gate 			status = RPC_AUTHERROR;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		/* end successful completion */
2070Sstevel@tonic-gate 	} else {
2080Sstevel@tonic-gate 		if (AUTH_REFRESH(h->cl_auth, &msg))
2090Sstevel@tonic-gate 			goto call_again;
2100Sstevel@tonic-gate 		/* end of unsuccessful completion */
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (status == RPC_SUCCESS) {
214*132Srobinson 		if (!AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
2150Sstevel@tonic-gate 			status = RPC_AUTHERROR;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
2180Sstevel@tonic-gate 			xdrs->x_op = XDR_FREE;
2190Sstevel@tonic-gate 			(void) xdr_opaque_auth(xdrs,
2200Sstevel@tonic-gate 					&(msg.acpted_rply.ar_verf));
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	return (status);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*ARGSUSED*/
2270Sstevel@tonic-gate static enum clnt_stat
clnt_raw_send(CLIENT * h,rpcproc_t proc,xdrproc_t xargs,caddr_t argsp)2280Sstevel@tonic-gate clnt_raw_send(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, caddr_t argsp)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	struct clnt_raw_private *clp;
2310Sstevel@tonic-gate 	XDR *xdrs;
2320Sstevel@tonic-gate 
233*132Srobinson 	(void) mutex_lock(&clntraw_lock);
2340Sstevel@tonic-gate 	clp = clnt_raw_private;
2350Sstevel@tonic-gate 	xdrs = &clp->xdr_stream;
2360Sstevel@tonic-gate 	if (clp == NULL) {
237*132Srobinson 		(void) mutex_unlock(&clntraw_lock);
2380Sstevel@tonic-gate 		return (RPC_FAILED);
2390Sstevel@tonic-gate 	}
240*132Srobinson 	(void) mutex_unlock(&clntraw_lock);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * send request
2440Sstevel@tonic-gate 	 */
2450Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
2460Sstevel@tonic-gate 	XDR_SETPOS(xdrs, 0);
2470Sstevel@tonic-gate /* LINTED pointer alignment */
2480Sstevel@tonic-gate 	((struct rpc_msg *)clp->mashl_callmsg)->rm_xid++;
249*132Srobinson 	if ((!XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
250*132Srobinson 	    (!XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
251*132Srobinson 	    (!AUTH_MARSHALL(h->cl_auth, xdrs)) ||
252*132Srobinson 	    (!(*xargs)(xdrs, argsp)))
2530Sstevel@tonic-gate 		return (RPC_CANTENCODEARGS);
2540Sstevel@tonic-gate 	(void) XDR_GETPOS(xdrs);  /* called just to cause overhead */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * We have to call server input routine here because this is
2580Sstevel@tonic-gate 	 * all going on in one process.
2590Sstevel@tonic-gate 	 * By convention using FD_SETSIZE as the psuedo file descriptor.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	svc_getreq_common(FD_SETSIZE);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	return (RPC_SUCCESS);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*ARGSUSED*/
2670Sstevel@tonic-gate static void
clnt_raw_geterr(CLIENT * cl,struct rpc_err * errp)2680Sstevel@tonic-gate clnt_raw_geterr(CLIENT *cl, struct rpc_err *errp)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*ARGSUSED*/
2730Sstevel@tonic-gate static bool_t
clnt_raw_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)2740Sstevel@tonic-gate clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	struct clnt_raw_private *clp;
2770Sstevel@tonic-gate 	XDR *xdrs;
2780Sstevel@tonic-gate 
279*132Srobinson 	(void) mutex_lock(&clntraw_lock);
2800Sstevel@tonic-gate 	clp = clnt_raw_private;
2810Sstevel@tonic-gate 	xdrs = &clp->xdr_stream;
2820Sstevel@tonic-gate 	if (clp == NULL) {
283*132Srobinson 		(void) mutex_unlock(&clntraw_lock);
2840Sstevel@tonic-gate 		return (FALSE);
2850Sstevel@tonic-gate 	}
286*132Srobinson 	(void) mutex_unlock(&clntraw_lock);
2870Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
288*132Srobinson 	return ((*xdr_res)(xdrs, res_ptr));
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*ARGSUSED*/
2920Sstevel@tonic-gate static void
clnt_raw_abort(CLIENT * cl,struct rpc_err * errp)2930Sstevel@tonic-gate clnt_raw_abort(CLIENT *cl, struct rpc_err *errp)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*ARGSUSED*/
2980Sstevel@tonic-gate static bool_t
clnt_raw_control(CLIENT * cl,int request,char * info)2990Sstevel@tonic-gate clnt_raw_control(CLIENT *cl, int request, char *info)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	return (FALSE);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*ARGSUSED*/
3050Sstevel@tonic-gate static void
clnt_raw_destroy(CLIENT * cl)3060Sstevel@tonic-gate clnt_raw_destroy(CLIENT *cl)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static struct clnt_ops *
clnt_raw_ops(void)3110Sstevel@tonic-gate clnt_raw_ops(void)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	static struct clnt_ops ops;
3140Sstevel@tonic-gate 	extern mutex_t	ops_lock;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/* VARIABLES PROTECTED BY ops_lock: ops */
3170Sstevel@tonic-gate 
318*132Srobinson 	(void) mutex_lock(&ops_lock);
3190Sstevel@tonic-gate 	if (ops.cl_call == NULL) {
3200Sstevel@tonic-gate 		ops.cl_call = clnt_raw_call;
3210Sstevel@tonic-gate 		ops.cl_send = clnt_raw_send;
3220Sstevel@tonic-gate 		ops.cl_abort = clnt_raw_abort;
3230Sstevel@tonic-gate 		ops.cl_geterr = clnt_raw_geterr;
3240Sstevel@tonic-gate 		ops.cl_freeres = clnt_raw_freeres;
3250Sstevel@tonic-gate 		ops.cl_destroy = clnt_raw_destroy;
3260Sstevel@tonic-gate 		ops.cl_control = clnt_raw_control;
3270Sstevel@tonic-gate 	}
328*132Srobinson 	(void) mutex_unlock(&ops_lock);
3290Sstevel@tonic-gate 	return (&ops);
3300Sstevel@tonic-gate }
331