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