xref: /netbsd/lib/libc/rpc/svc_simple.c (revision c4a72b64)
1 /*	$NetBSD: svc_simple.c,v 1.22 2002/11/08 00:13:08 fvdl 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 "namespace.h"
50 #include "reentrant.h"
51 #include <sys/types.h>
52 #include <rpc/rpc.h>
53 #include <rpc/nettype.h>
54 #include <assert.h>
55 #include <err.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 #include "rpc_internal.h"
61 
62 #ifdef __weak_alias
63 __weak_alias(rpc_reg,_rpc_reg)
64 #endif
65 
66 static void universal __P((struct svc_req *, SVCXPRT *));
67 
68 static struct proglst {
69 	char *(*p_progname) __P((char *));
70 	rpcprog_t p_prognum;
71 	rpcvers_t p_versnum;
72 	rpcproc_t p_procnum;
73 	SVCXPRT *p_transp;
74 	char *p_netid;
75 	char *p_xdrbuf;
76 	int p_recvsz;
77 	xdrproc_t p_inproc, p_outproc;
78 	struct proglst *p_nxt;
79 } *proglst;
80 
81 static const char rpc_reg_err[] = "%s: %s";
82 static const char rpc_reg_msg[] = "rpc_reg: ";
83 static const char __reg_err1[] = "can't find appropriate transport";
84 static const char __reg_err2[] = "can't get protocol info";
85 static const char __reg_err3[] = "unsupported transport size";
86 static const char __no_mem_str[] = "out of memory";
87 
88 /*
89  * For simplified, easy to use kind of rpc interfaces.
90  * nettype indicates the type of transport on which the service will be
91  * listening. Used for conservation of the system resource. Only one
92  * handle is created for all the services (actually one of each netid)
93  * and same xdrbuf is used for same netid. The size of the arguments
94  * is also limited by the recvsize for that transport, even if it is
95  * a COTS transport. This may be wrong, but for cases like these, they
96  * should not use the simplified interfaces like this.
97  */
98 
99 int
100 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
101 	rpcprog_t prognum;			/* program number */
102 	rpcvers_t versnum;			/* version number */
103 	rpcproc_t procnum;			/* procedure number */
104 	char *(*progname) __P((char *)); /* Server routine */
105 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
106 	char *nettype;			/* nettype */
107 {
108 	struct netconfig *nconf;
109 	int done = FALSE;
110 	void *handle;
111 #ifdef __REENT
112 	extern mutex_t proglst_lock;
113 #endif
114 
115 	if (procnum == NULLPROC) {
116 		warnx("%s can't reassign procedure number %u", rpc_reg_msg,
117 			NULLPROC);
118 		return (-1);
119 	}
120 
121 	if (nettype == NULL)
122 		nettype = "netpath";		/* The default behavior */
123 	if ((handle = __rpc_setconf(nettype)) == NULL) {
124 		warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
125 		return (-1);
126 	}
127 /* VARIABLES PROTECTED BY proglst_lock: proglst */
128 	mutex_lock(&proglst_lock);
129 	while ((nconf = __rpc_getconf(handle)) != NULL) {
130 		struct proglst *pl;
131 		SVCXPRT *svcxprt;
132 		int madenow;
133 		u_int recvsz;
134 		char *xdrbuf;
135 		char *netid;
136 
137 		madenow = FALSE;
138 		svcxprt = NULL;
139 		for (pl = proglst; pl; pl = pl->p_nxt)
140 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
141 				svcxprt = pl->p_transp;
142 				xdrbuf = pl->p_xdrbuf;
143 				recvsz = pl->p_recvsz;
144 				netid = pl->p_netid;
145 				break;
146 			}
147 
148 		if (svcxprt == NULL) {
149 			struct __rpc_sockinfo si;
150 
151 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
152 			if (svcxprt == NULL)
153 				continue;
154 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
155 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
156 				SVC_DESTROY(svcxprt);
157 				continue;
158 			}
159 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
160 			if (recvsz == 0) {
161 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
162 				SVC_DESTROY(svcxprt);
163 				continue;
164 			}
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);
169 				break;
170 			}
171 			madenow = TRUE;
172 		}
173 		/*
174 		 * Check if this (program, version, netid) had already been
175 		 * registered.  The check may save a few RPC calls to rpcbind
176 		 */
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))
181 				break;
182 		if (pl == NULL) { /* Not yet */
183 			(void) rpcb_unset(prognum, versnum, nconf);
184 		} else {
185 			/* so that svc_reg does not call rpcb_set() */
186 			nconf = NULL;
187 		}
188 
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);
193 			if (madenow) {
194 				SVC_DESTROY(svcxprt);
195 				free(xdrbuf);
196 				free(netid);
197 			}
198 			continue;
199 		}
200 
201 		pl = malloc(sizeof (struct proglst));
202 		if (pl == NULL) {
203 			warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
204 			if (madenow) {
205 				SVC_DESTROY(svcxprt);
206 				free(xdrbuf);
207 				free(netid);
208 			}
209 			break;
210 		}
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;
220 		pl->p_netid = netid;
221 		pl->p_nxt = proglst;
222 		proglst = pl;
223 		done = TRUE;
224 	}
225 	__rpc_endconf(handle);
226 	mutex_unlock(&proglst_lock);
227 
228 	if (done == FALSE) {
229 		warnx("%s cant find suitable transport for %s",
230 			rpc_reg_msg, nettype);
231 		return (-1);
232 	}
233 	return (0);
234 }
235 
236 /*
237  * The universal handler for the services registered using registerrpc.
238  * It handles both the connectionless and the connection oriented cases.
239  */
240 
241 static void
242 universal(rqstp, transp)
243 	struct svc_req *rqstp;
244 	SVCXPRT *transp;
245 {
246 	rpcprog_t prog;
247 	rpcvers_t vers;
248 	rpcproc_t proc;
249 	char *outdata;
250 	char *xdrbuf;
251 	struct proglst *pl;
252 #ifdef __REENT
253 	extern mutex_t proglst_lock;
254 #endif
255 
256 	_DIAGASSERT(rqstp != NULL);
257 	_DIAGASSERT(transp != NULL);
258 
259 	/*
260 	 * enforce "procnum 0 is echo" convention
261 	 */
262 	if (rqstp->rq_proc == NULLPROC) {
263 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
264 		    FALSE) {
265 			warnx("svc_sendreply failed");
266 		}
267 		return;
268 	}
269 	prog = rqstp->rq_prog;
270 	vers = rqstp->rq_vers;
271 	proc = rqstp->rq_proc;
272 	mutex_lock(&proglst_lock);
273 	for (pl = proglst; pl; pl = pl->p_nxt)
274 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
275 			pl->p_versnum == vers &&
276 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
277 			/* decode arguments into a CLEAN buffer */
278 			xdrbuf = pl->p_xdrbuf;
279 			/* Zero the arguments: reqd ! */
280 			(void) memset(xdrbuf, 0, sizeof (pl->p_recvsz));
281 			/*
282 			 * Assuming that sizeof (xdrbuf) would be enough
283 			 * for the arguments; if not then the program
284 			 * may bomb. BEWARE!
285 			 */
286 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
287 				svcerr_decode(transp);
288 				mutex_unlock(&proglst_lock);
289 				return;
290 			}
291 			outdata = (*(pl->p_progname))(xdrbuf);
292 			if (outdata == NULL &&
293 				pl->p_outproc != (xdrproc_t) xdr_void){
294 				/* there was an error */
295 				mutex_unlock(&proglst_lock);
296 				return;
297 			}
298 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
299 				warnx(
300 			"rpc: rpc_reg trouble replying to prog %u vers %u",
301 				(unsigned)prog, (unsigned)vers);
302 				mutex_unlock(&proglst_lock);
303 				return;
304 			}
305 			/* free the decoded arguments */
306 			(void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
307 			mutex_unlock(&proglst_lock);
308 			return;
309 		}
310 	mutex_unlock(&proglst_lock);
311 	/* This should never happen */
312 	warnx("rpc: rpc_reg: never registered prog %u vers %u",
313 		(unsigned)prog, (unsigned)vers);
314 	return;
315 }
316