1 /* $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 * Simplified front end to rpc.
45 * This interface creates a virtual listener for all the services
46 * started thru rpc_reg(). It listens on the same endpoint for
47 * all the services and then executes the corresponding service
48 * for the given prognum and procnum.
51 #include "namespace.h"
52 #include "reentrant.h"
53 #include <sys/types.h>
55 #include <rpc/nettype.h>
60 #include "un-namespace.h"
65 static void universal(struct svc_req *, SVCXPRT *);
67 static struct proglst {
68 char *(*p_progname)(char *);
76 xdrproc_t p_inproc, p_outproc;
77 struct proglst *p_nxt;
80 static const char rpc_reg_err[] = "%s: %s";
81 static const char rpc_reg_msg[] = "rpc_reg: ";
82 static const char __reg_err1[] = "can't find appropriate transport";
83 static const char __reg_err2[] = "can't get protocol info";
84 static const char __reg_err3[] = "unsupported transport size";
85 static const char __no_mem_str[] = "out of memory";
88 * For simplified, easy to use kind of rpc interfaces.
89 * nettype indicates the type of transport on which the service will be
90 * listening. Used for conservation of the system resource. Only one
91 * handle is created for all the services (actually one of each netid)
92 * and same xdrbuf is used for same netid. The size of the arguments
93 * is also limited by the recvsize for that transport, even if it is
94 * a COTS transport. This may be wrong, but for cases like these, they
95 * should not use the simplified interfaces like this.
99 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
100 rpcprog_t prognum; /* program number */
101 rpcvers_t versnum; /* version number */
102 rpcproc_t procnum; /* procedure number */
103 char *(*progname)(char *); /* Server routine */
104 xdrproc_t inproc, outproc; /* in/out XDR procedures */
105 char *nettype; /* nettype */
107 struct netconfig *nconf;
112 if (procnum == NULLPROC) {
113 warnx("%s can't reassign procedure number %u", rpc_reg_msg,
119 nettype = "netpath"; /* The default behavior */
120 if ((handle = __rpc_setconf(nettype)) == NULL) {
121 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
124 /* VARIABLES PROTECTED BY proglst_lock: proglst */
125 mutex_lock(&proglst_lock);
126 while ((nconf = __rpc_getconf(handle)) != NULL) {
137 xdrbuf = netid = NULL;
138 for (pl = proglst; pl; pl = pl->p_nxt) {
139 if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
140 svcxprt = pl->p_transp;
141 xdrbuf = pl->p_xdrbuf;
142 recvsz = pl->p_recvsz;
148 if (svcxprt == NULL) {
149 struct __rpc_sockinfo si;
151 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
154 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
155 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
156 SVC_DESTROY(svcxprt);
159 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
161 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
162 SVC_DESTROY(svcxprt);
165 if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
166 ((netid = strdup(nconf->nc_netid)) == NULL)) {
167 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
168 SVC_DESTROY(svcxprt);
174 * Check if this (program, version, netid) had already been
175 * registered. The check may save a few RPC calls to rpcbind
177 for (pl = proglst; pl; pl = pl->p_nxt)
178 if ((pl->p_prognum == prognum) &&
179 (pl->p_versnum == versnum) &&
180 (strcmp(pl->p_netid, netid) == 0))
182 if (pl == NULL) { /* Not yet */
183 (void) rpcb_unset(prognum, versnum, nconf);
185 /* so that svc_reg does not call rpcb_set() */
189 if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
190 warnx("%s couldn't register prog %u vers %u for %s",
191 rpc_reg_msg, (unsigned)prognum,
192 (unsigned)versnum, netid);
194 SVC_DESTROY(svcxprt);
201 pl = malloc(sizeof (struct proglst));
203 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
205 SVC_DESTROY(svcxprt);
211 pl->p_progname = progname;
212 pl->p_prognum = prognum;
213 pl->p_versnum = versnum;
214 pl->p_procnum = procnum;
215 pl->p_inproc = inproc;
216 pl->p_outproc = outproc;
217 pl->p_transp = svcxprt;
218 pl->p_xdrbuf = xdrbuf;
219 pl->p_recvsz = recvsz;
225 __rpc_endconf(handle);
226 mutex_unlock(&proglst_lock);
229 warnx("%s cant find suitable transport for %s",
230 rpc_reg_msg, nettype);
237 * The universal handler for the services registered using registerrpc.
238 * It handles both the connectionless and the connection oriented cases.
242 universal(rqstp, transp)
243 struct svc_req *rqstp;
254 * enforce "procnum 0 is echo" convention
256 if (rqstp->rq_proc == NULLPROC) {
257 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
259 warnx("svc_sendreply failed");
263 prog = rqstp->rq_prog;
264 vers = rqstp->rq_vers;
265 proc = rqstp->rq_proc;
266 mutex_lock(&proglst_lock);
267 for (pl = proglst; pl; pl = pl->p_nxt)
268 if (pl->p_prognum == prog && pl->p_procnum == proc &&
269 pl->p_versnum == vers &&
270 (strcmp(pl->p_netid, transp->xp_netid) == 0)) {
271 /* decode arguments into a CLEAN buffer */
272 xdrbuf = pl->p_xdrbuf;
273 /* Zero the arguments: reqd ! */
274 (void) memset(xdrbuf, 0, sizeof (pl->p_recvsz));
276 * Assuming that sizeof (xdrbuf) would be enough
277 * for the arguments; if not then the program
280 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
281 svcerr_decode(transp);
282 mutex_unlock(&proglst_lock);
285 outdata = (*(pl->p_progname))(xdrbuf);
286 if (outdata == NULL &&
287 pl->p_outproc != (xdrproc_t) xdr_void){
288 /* there was an error */
289 mutex_unlock(&proglst_lock);
292 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
294 "rpc: rpc_reg trouble replying to prog %u vers %u",
295 (unsigned)prog, (unsigned)vers);
296 mutex_unlock(&proglst_lock);
299 /* free the decoded arguments */
300 (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
301 mutex_unlock(&proglst_lock);
304 mutex_unlock(&proglst_lock);
305 /* This should never happen */
306 warnx("rpc: rpc_reg: never registered prog %u vers %u",
307 (unsigned)prog, (unsigned)vers);