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