1 /* $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 * Simplified front end to rpc.
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.
50 #include "namespace.h"
51 #include "reentrant.h"
52 #include <sys/types.h>
54 #include <rpc/nettype.h>
59 #include "un-namespace.h"
64 static void universal(struct svc_req *, SVCXPRT *);
66 static struct proglst {
67 char *(*p_progname)(char *);
75 xdrproc_t p_inproc, p_outproc;
76 struct proglst *p_nxt;
79 static const char rpc_reg_err[] = "%s: %s";
80 static const char rpc_reg_msg[] = "rpc_reg: ";
81 static const char __reg_err1[] = "can't find appropriate transport";
82 static const char __reg_err2[] = "can't get protocol info";
83 static const char __reg_err3[] = "unsupported transport size";
84 static const char __no_mem_str[] = "out of memory";
87 * For simplified, easy to use kind of rpc interfaces.
88 * nettype indicates the type of transport on which the service will be
89 * listening. Used for conservation of the system resource. Only one
90 * handle is created for all the services (actually one of each netid)
91 * and same xdrbuf is used for same netid. The size of the arguments
92 * is also limited by the recvsize for that transport, even if it is
93 * a COTS transport. This may be wrong, but for cases like these, they
94 * should not use the simplified interfaces like this.
98 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
99 rpcprog_t prognum; /* program number */
100 rpcvers_t versnum; /* version number */
101 rpcproc_t procnum; /* procedure number */
102 char *(*progname)(char *); /* Server routine */
103 xdrproc_t inproc, outproc; /* in/out XDR procedures */
104 char *nettype; /* nettype */
106 struct netconfig *nconf;
111 if (procnum == NULLPROC) {
112 warnx("%s can't reassign procedure number %u", rpc_reg_msg,
118 nettype = "netpath"; /* The default behavior */
119 if ((handle = __rpc_setconf(nettype)) == NULL) {
120 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
123 /* VARIABLES PROTECTED BY proglst_lock: proglst */
124 mutex_lock(&proglst_lock);
125 while ((nconf = __rpc_getconf(handle)) != NULL) {
136 xdrbuf = netid = NULL;
137 for (pl = proglst; pl; pl = pl->p_nxt) {
138 if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
139 svcxprt = pl->p_transp;
140 xdrbuf = pl->p_xdrbuf;
141 recvsz = pl->p_recvsz;
147 if (svcxprt == NULL) {
148 struct __rpc_sockinfo si;
150 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
153 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
154 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
155 SVC_DESTROY(svcxprt);
158 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
160 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
161 SVC_DESTROY(svcxprt);
164 if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
165 ((netid = strdup(nconf->nc_netid)) == NULL)) {
166 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
169 SVC_DESTROY(svcxprt);
175 * Check if this (program, version, netid) had already been
176 * registered. The check may save a few RPC calls to rpcbind
178 for (pl = proglst; pl; pl = pl->p_nxt)
179 if ((pl->p_prognum == prognum) &&
180 (pl->p_versnum == versnum) &&
181 (strcmp(pl->p_netid, netid) == 0))
183 if (pl == NULL) { /* Not yet */
184 (void) rpcb_unset(prognum, versnum, nconf);
186 /* so that svc_reg does not call rpcb_set() */
190 if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
191 warnx("%s couldn't register prog %u vers %u for %s",
192 rpc_reg_msg, (unsigned)prognum,
193 (unsigned)versnum, netid);
195 SVC_DESTROY(svcxprt);
202 pl = malloc(sizeof (struct proglst));
204 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
206 SVC_DESTROY(svcxprt);
212 pl->p_progname = progname;
213 pl->p_prognum = prognum;
214 pl->p_versnum = versnum;
215 pl->p_procnum = procnum;
216 pl->p_inproc = inproc;
217 pl->p_outproc = outproc;
218 pl->p_transp = svcxprt;
219 pl->p_xdrbuf = xdrbuf;
220 pl->p_recvsz = recvsz;
226 __rpc_endconf(handle);
227 mutex_unlock(&proglst_lock);
230 warnx("%s cant find suitable transport for %s",
231 rpc_reg_msg, nettype);
238 * The universal handler for the services registered using registerrpc.
239 * It handles both the connectionless and the connection oriented cases.
243 universal(rqstp, transp)
244 struct svc_req *rqstp;
255 * enforce "procnum 0 is echo" convention
257 if (rqstp->rq_proc == NULLPROC) {
258 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
260 warnx("svc_sendreply failed");
264 prog = rqstp->rq_prog;
265 vers = rqstp->rq_vers;
266 proc = rqstp->rq_proc;
267 mutex_lock(&proglst_lock);
268 for (pl = proglst; pl; pl = pl->p_nxt)
269 if (pl->p_prognum == prog && pl->p_procnum == proc &&
270 pl->p_versnum == vers &&
271 (strcmp(pl->p_netid, transp->xp_netid) == 0)) {
272 /* decode arguments into a CLEAN buffer */
273 xdrbuf = pl->p_xdrbuf;
274 /* Zero the arguments: reqd ! */
275 (void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
277 * Assuming that sizeof (xdrbuf) would be enough
278 * for the arguments; if not then the program
281 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
282 svcerr_decode(transp);
283 mutex_unlock(&proglst_lock);
286 outdata = (*(pl->p_progname))(xdrbuf);
287 if (outdata == NULL &&
288 pl->p_outproc != (xdrproc_t) xdr_void){
289 /* there was an error */
290 mutex_unlock(&proglst_lock);
293 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
295 "rpc: rpc_reg trouble replying to prog %u vers %u",
296 (unsigned)prog, (unsigned)vers);
297 mutex_unlock(&proglst_lock);
300 /* free the decoded arguments */
301 (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
302 mutex_unlock(&proglst_lock);
305 mutex_unlock(&proglst_lock);
306 /* This should never happen */
307 warnx("rpc: rpc_reg: never registered prog %u vers %u",
308 (unsigned)prog, (unsigned)vers);