Home | History | Annotate | Line # | Download | only in rpc
svc_simple.c revision 1.31
      1 /*	$NetBSD: svc_simple.c,v 1.31 2012/03/20 17:14:50 matt 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.31 2012/03/20 17:14:50 matt 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(struct svc_req *, SVCXPRT *);
     72 
     73 static struct proglst {
     74 	char *(*p_progname)(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(
    106 	rpcprog_t prognum,		/* program number */
    107 	rpcvers_t versnum,		/* version number */
    108 	rpcproc_t procnum,		/* procedure number */
    109 	char *(*progname)(char *),	/* Server routine */
    110 	xdrproc_t inproc,		/* in XDR procedure */
    111 	xdrproc_t outproc,		/* out XDR procedure */
    112 	char *nettype)			/* nettype */
    113 {
    114 	struct netconfig *nconf;
    115 	int done = FALSE;
    116 	void *handle;
    117 #ifdef _REENTRANT
    118 	extern mutex_t proglst_lock;
    119 #endif
    120 
    121 	if (procnum == NULLPROC) {
    122 		warnx("%s can't reassign procedure number %u", rpc_reg_msg,
    123 			NULLPROC);
    124 		return (-1);
    125 	}
    126 
    127 	if (nettype == NULL)
    128 		nettype = __UNCONST("netpath");	/* The default behavior */
    129 	if ((handle = __rpc_setconf(nettype)) == NULL) {
    130 		warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
    131 		return (-1);
    132 	}
    133 /* VARIABLES PROTECTED BY proglst_lock: proglst */
    134 	mutex_lock(&proglst_lock);
    135 	while ((nconf = __rpc_getconf(handle)) != NULL) {
    136 		struct proglst *pl;
    137 		SVCXPRT *svcxprt;
    138 		int madenow;
    139 		u_int recvsz;
    140 		char *xdrbuf;
    141 		char *netid;
    142 
    143 		madenow = FALSE;
    144 		svcxprt = NULL;
    145 		recvsz = 0;
    146 		xdrbuf = NULL;
    147 		netid = NULL;
    148 		for (pl = proglst; pl; pl = pl->p_nxt)
    149 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
    150 				svcxprt = pl->p_transp;
    151 				xdrbuf = pl->p_xdrbuf;
    152 				recvsz = pl->p_recvsz;
    153 				netid = pl->p_netid;
    154 				break;
    155 			}
    156 
    157 		if (svcxprt == NULL) {
    158 			struct __rpc_sockinfo si;
    159 
    160 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
    161 			if (svcxprt == NULL)
    162 				continue;
    163 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
    164 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
    165 				SVC_DESTROY(svcxprt);
    166 				continue;
    167 			}
    168 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
    169 			if (recvsz == 0) {
    170 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
    171 				SVC_DESTROY(svcxprt);
    172 				continue;
    173 			}
    174 			if (((xdrbuf = malloc((size_t)recvsz)) == NULL) ||
    175 				((netid = strdup(nconf->nc_netid)) == NULL)) {
    176 				warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
    177 				if (xdrbuf != NULL)
    178 					free(xdrbuf);
    179 				if (netid != NULL)
    180 					free(netid);
    181 				SVC_DESTROY(svcxprt);
    182 				break;
    183 			}
    184 			madenow = TRUE;
    185 		}
    186 		/*
    187 		 * Check if this (program, version, netid) had already been
    188 		 * registered.  The check may save a few RPC calls to rpcbind
    189 		 */
    190 		for (pl = proglst; pl; pl = pl->p_nxt)
    191 			if ((pl->p_prognum == prognum) &&
    192 				(pl->p_versnum == versnum) &&
    193 				(strcmp(pl->p_netid, netid) == 0))
    194 				break;
    195 		if (pl == NULL) { /* Not yet */
    196 			(void) rpcb_unset(prognum, versnum, nconf);
    197 		} else {
    198 			/* so that svc_reg does not call rpcb_set() */
    199 			nconf = NULL;
    200 		}
    201 
    202 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
    203 			warnx("%s couldn't register prog %u vers %u for %s",
    204 				rpc_reg_msg, (unsigned)prognum,
    205 				(unsigned)versnum, netid);
    206 			if (madenow) {
    207 				SVC_DESTROY(svcxprt);
    208 				free(xdrbuf);
    209 				free(netid);
    210 			}
    211 			continue;
    212 		}
    213 
    214 		pl = malloc(sizeof(*pl));
    215 		if (pl == NULL) {
    216 			warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
    217 			if (madenow) {
    218 				SVC_DESTROY(svcxprt);
    219 				free(xdrbuf);
    220 				free(netid);
    221 			}
    222 			break;
    223 		}
    224 		pl->p_progname = progname;
    225 		pl->p_prognum = prognum;
    226 		pl->p_versnum = versnum;
    227 		pl->p_procnum = procnum;
    228 		pl->p_inproc = inproc;
    229 		pl->p_outproc = outproc;
    230 		pl->p_transp = svcxprt;
    231 		pl->p_xdrbuf = xdrbuf;
    232 		pl->p_recvsz = recvsz;
    233 		pl->p_netid = netid;
    234 		pl->p_nxt = proglst;
    235 		proglst = pl;
    236 		done = TRUE;
    237 	}
    238 	__rpc_endconf(handle);
    239 	mutex_unlock(&proglst_lock);
    240 
    241 	if (done == FALSE) {
    242 		warnx("%s cant find suitable transport for %s",
    243 			rpc_reg_msg, nettype);
    244 		return (-1);
    245 	}
    246 	return (0);
    247 }
    248 
    249 /*
    250  * The universal handler for the services registered using registerrpc.
    251  * It handles both the connectionless and the connection oriented cases.
    252  */
    253 
    254 static void
    255 universal(struct svc_req *rqstp, SVCXPRT *transp)
    256 {
    257 	rpcprog_t prog;
    258 	rpcvers_t vers;
    259 	rpcproc_t proc;
    260 	char *outdata;
    261 	char *xdrbuf;
    262 	struct proglst *pl;
    263 #ifdef _REENTRANT
    264 	extern mutex_t proglst_lock;
    265 #endif
    266 
    267 	_DIAGASSERT(rqstp != NULL);
    268 	_DIAGASSERT(transp != NULL);
    269 
    270 	/*
    271 	 * enforce "procnum 0 is echo" convention
    272 	 */
    273 	if (rqstp->rq_proc == NULLPROC) {
    274 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
    275 		    FALSE) {
    276 			warnx("svc_sendreply failed");
    277 		}
    278 		return;
    279 	}
    280 	prog = rqstp->rq_prog;
    281 	vers = rqstp->rq_vers;
    282 	proc = rqstp->rq_proc;
    283 	mutex_lock(&proglst_lock);
    284 	for (pl = proglst; pl; pl = pl->p_nxt)
    285 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
    286 			pl->p_versnum == vers &&
    287 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
    288 			/* decode arguments into a CLEAN buffer */
    289 			xdrbuf = pl->p_xdrbuf;
    290 			/* Zero the arguments: reqd ! */
    291 			(void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
    292 			/*
    293 			 * Assuming that sizeof (xdrbuf) would be enough
    294 			 * for the arguments; if not then the program
    295 			 * may bomb. BEWARE!
    296 			 */
    297 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
    298 				svcerr_decode(transp);
    299 				mutex_unlock(&proglst_lock);
    300 				return;
    301 			}
    302 			outdata = (*(pl->p_progname))(xdrbuf);
    303 			if (outdata == NULL &&
    304 				pl->p_outproc != (xdrproc_t) xdr_void){
    305 				/* there was an error */
    306 				mutex_unlock(&proglst_lock);
    307 				return;
    308 			}
    309 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
    310 				warnx(
    311 			"rpc: rpc_reg trouble replying to prog %u vers %u",
    312 				(unsigned)prog, (unsigned)vers);
    313 				mutex_unlock(&proglst_lock);
    314 				return;
    315 			}
    316 			/* free the decoded arguments */
    317 			(void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
    318 			mutex_unlock(&proglst_lock);
    319 			return;
    320 		}
    321 	mutex_unlock(&proglst_lock);
    322 	/* This should never happen */
    323 	warnx("rpc: rpc_reg: never registered prog %u vers %u",
    324 		(unsigned)prog, (unsigned)vers);
    325 	return;
    326 }
    327