Home | History | Annotate | Line # | Download | only in rpc
svc_simple.c revision 1.27
      1 /*	$NetBSD: svc_simple.c,v 1.27 2005/06/01 05:54:07 lukem Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 /*
     32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33  */
     34 
     35 /* #pragma ident	"@(#)svc_simple.c	1.18	94/04/24 SMI" */
     36 
     37 /*
     38  * svc_simple.c
     39  * Simplified front end to rpc.
     40  */
     41 
     42 /*
     43  * This interface creates a virtual listener for all the services
     44  * started thru rpc_reg(). It listens on the same endpoint for
     45  * all the services and then executes the corresponding service
     46  * for the given prognum and procnum.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 #if defined(LIBC_SCCS) && !defined(lint)
     51 __RCSID("$NetBSD: svc_simple.c,v 1.27 2005/06/01 05:54:07 lukem Exp $");
     52 #endif
     53 
     54 #include "namespace.h"
     55 #include "reentrant.h"
     56 #include <sys/types.h>
     57 #include <rpc/rpc.h>
     58 #include <rpc/nettype.h>
     59 #include <assert.h>
     60 #include <err.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 
     65 #include "rpc_internal.h"
     66 
     67 #ifdef __weak_alias
     68 __weak_alias(rpc_reg,_rpc_reg)
     69 #endif
     70 
     71 static void universal __P((struct svc_req *, SVCXPRT *));
     72 
     73 static struct proglst {
     74 	char *(*p_progname) __P((char *));
     75 	rpcprog_t p_prognum;
     76 	rpcvers_t p_versnum;
     77 	rpcproc_t p_procnum;
     78 	SVCXPRT *p_transp;
     79 	char *p_netid;
     80 	char *p_xdrbuf;
     81 	int p_recvsz;
     82 	xdrproc_t p_inproc, p_outproc;
     83 	struct proglst *p_nxt;
     84 } *proglst;
     85 
     86 static const char rpc_reg_err[] = "%s: %s";
     87 static const char rpc_reg_msg[] = "rpc_reg: ";
     88 static const char __reg_err1[] = "can't find appropriate transport";
     89 static const char __reg_err2[] = "can't get protocol info";
     90 static const char __reg_err3[] = "unsupported transport size";
     91 static const char __no_mem_str[] = "out of memory";
     92 
     93 /*
     94  * For simplified, easy to use kind of rpc interfaces.
     95  * nettype indicates the type of transport on which the service will be
     96  * listening. Used for conservation of the system resource. Only one
     97  * handle is created for all the services (actually one of each netid)
     98  * and same xdrbuf is used for same netid. The size of the arguments
     99  * is also limited by the recvsize for that transport, even if it is
    100  * a COTS transport. This may be wrong, but for cases like these, they
    101  * should not use the simplified interfaces like this.
    102  */
    103 
    104 int
    105 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
    106 	rpcprog_t prognum;			/* program number */
    107 	rpcvers_t versnum;			/* version number */
    108 	rpcproc_t procnum;			/* procedure number */
    109 	char *(*progname) __P((char *)); /* Server routine */
    110 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
    111 	char *nettype;			/* nettype */
    112 {
    113 	struct netconfig *nconf;
    114 	int done = FALSE;
    115 	void *handle;
    116 #ifdef _REENTRANT
    117 	extern mutex_t proglst_lock;
    118 #endif
    119 
    120 	if (procnum == NULLPROC) {
    121 		warnx("%s can't reassign procedure number %u", rpc_reg_msg,
    122 			NULLPROC);
    123 		return (-1);
    124 	}
    125 
    126 	if (nettype == NULL)
    127 		nettype = "netpath";		/* The default behavior */
    128 	if ((handle = __rpc_setconf(nettype)) == NULL) {
    129 		warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
    130 		return (-1);
    131 	}
    132 /* VARIABLES PROTECTED BY proglst_lock: proglst */
    133 	mutex_lock(&proglst_lock);
    134 	while ((nconf = __rpc_getconf(handle)) != NULL) {
    135 		struct proglst *pl;
    136 		SVCXPRT *svcxprt;
    137 		int madenow;
    138 		u_int recvsz;
    139 		char *xdrbuf;
    140 		char *netid;
    141 
    142 		madenow = FALSE;
    143 		svcxprt = NULL;
    144 		recvsz = 0;
    145 		xdrbuf = NULL;
    146 		netid = NULL;
    147 		for (pl = proglst; pl; pl = pl->p_nxt)
    148 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
    149 				svcxprt = pl->p_transp;
    150 				xdrbuf = pl->p_xdrbuf;
    151 				recvsz = pl->p_recvsz;
    152 				netid = pl->p_netid;
    153 				break;
    154 			}
    155 
    156 		if (svcxprt == NULL) {
    157 			struct __rpc_sockinfo si;
    158 
    159 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
    160 			if (svcxprt == NULL)
    161 				continue;
    162 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
    163 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
    164 				SVC_DESTROY(svcxprt);
    165 				continue;
    166 			}
    167 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
    168 			if (recvsz == 0) {
    169 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
    170 				SVC_DESTROY(svcxprt);
    171 				continue;
    172 			}
    173 			if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
    174 				((netid = strdup(nconf->nc_netid)) == NULL)) {
    175 				warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
    176 				SVC_DESTROY(svcxprt);
    177 				break;
    178 			}
    179 			madenow = TRUE;
    180 		}
    181 		/*
    182 		 * Check if this (program, version, netid) had already been
    183 		 * registered.  The check may save a few RPC calls to rpcbind
    184 		 */
    185 		for (pl = proglst; pl; pl = pl->p_nxt)
    186 			if ((pl->p_prognum == prognum) &&
    187 				(pl->p_versnum == versnum) &&
    188 				(strcmp(pl->p_netid, netid) == 0))
    189 				break;
    190 		if (pl == NULL) { /* Not yet */
    191 			(void) rpcb_unset(prognum, versnum, nconf);
    192 		} else {
    193 			/* so that svc_reg does not call rpcb_set() */
    194 			nconf = NULL;
    195 		}
    196 
    197 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
    198 			warnx("%s couldn't register prog %u vers %u for %s",
    199 				rpc_reg_msg, (unsigned)prognum,
    200 				(unsigned)versnum, netid);
    201 			if (madenow) {
    202 				SVC_DESTROY(svcxprt);
    203 				free(xdrbuf);
    204 				free(netid);
    205 			}
    206 			continue;
    207 		}
    208 
    209 		pl = malloc(sizeof (struct proglst));
    210 		if (pl == NULL) {
    211 			warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
    212 			if (madenow) {
    213 				SVC_DESTROY(svcxprt);
    214 				free(xdrbuf);
    215 				free(netid);
    216 			}
    217 			break;
    218 		}
    219 		pl->p_progname = progname;
    220 		pl->p_prognum = prognum;
    221 		pl->p_versnum = versnum;
    222 		pl->p_procnum = procnum;
    223 		pl->p_inproc = inproc;
    224 		pl->p_outproc = outproc;
    225 		pl->p_transp = svcxprt;
    226 		pl->p_xdrbuf = xdrbuf;
    227 		pl->p_recvsz = recvsz;
    228 		pl->p_netid = netid;
    229 		pl->p_nxt = proglst;
    230 		proglst = pl;
    231 		done = TRUE;
    232 	}
    233 	__rpc_endconf(handle);
    234 	mutex_unlock(&proglst_lock);
    235 
    236 	if (done == FALSE) {
    237 		warnx("%s cant find suitable transport for %s",
    238 			rpc_reg_msg, nettype);
    239 		return (-1);
    240 	}
    241 	return (0);
    242 }
    243 
    244 /*
    245  * The universal handler for the services registered using registerrpc.
    246  * It handles both the connectionless and the connection oriented cases.
    247  */
    248 
    249 static void
    250 universal(rqstp, transp)
    251 	struct svc_req *rqstp;
    252 	SVCXPRT *transp;
    253 {
    254 	rpcprog_t prog;
    255 	rpcvers_t vers;
    256 	rpcproc_t proc;
    257 	char *outdata;
    258 	char *xdrbuf;
    259 	struct proglst *pl;
    260 #ifdef _REENTRANT
    261 	extern mutex_t proglst_lock;
    262 #endif
    263 
    264 	_DIAGASSERT(rqstp != NULL);
    265 	_DIAGASSERT(transp != NULL);
    266 
    267 	/*
    268 	 * enforce "procnum 0 is echo" convention
    269 	 */
    270 	if (rqstp->rq_proc == NULLPROC) {
    271 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
    272 		    FALSE) {
    273 			warnx("svc_sendreply failed");
    274 		}
    275 		return;
    276 	}
    277 	prog = rqstp->rq_prog;
    278 	vers = rqstp->rq_vers;
    279 	proc = rqstp->rq_proc;
    280 	mutex_lock(&proglst_lock);
    281 	for (pl = proglst; pl; pl = pl->p_nxt)
    282 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
    283 			pl->p_versnum == vers &&
    284 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
    285 			/* decode arguments into a CLEAN buffer */
    286 			xdrbuf = pl->p_xdrbuf;
    287 			/* Zero the arguments: reqd ! */
    288 			(void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
    289 			/*
    290 			 * Assuming that sizeof (xdrbuf) would be enough
    291 			 * for the arguments; if not then the program
    292 			 * may bomb. BEWARE!
    293 			 */
    294 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
    295 				svcerr_decode(transp);
    296 				mutex_unlock(&proglst_lock);
    297 				return;
    298 			}
    299 			outdata = (*(pl->p_progname))(xdrbuf);
    300 			if (outdata == NULL &&
    301 				pl->p_outproc != (xdrproc_t) xdr_void){
    302 				/* there was an error */
    303 				mutex_unlock(&proglst_lock);
    304 				return;
    305 			}
    306 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
    307 				warnx(
    308 			"rpc: rpc_reg trouble replying to prog %u vers %u",
    309 				(unsigned)prog, (unsigned)vers);
    310 				mutex_unlock(&proglst_lock);
    311 				return;
    312 			}
    313 			/* free the decoded arguments */
    314 			(void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
    315 			mutex_unlock(&proglst_lock);
    316 			return;
    317 		}
    318 	mutex_unlock(&proglst_lock);
    319 	/* This should never happen */
    320 	warnx("rpc: rpc_reg: never registered prog %u vers %u",
    321 		(unsigned)prog, (unsigned)vers);
    322 	return;
    323 }
    324