xref: /dragonfly/lib/libc/rpc/svc_simple.c (revision 82730a9c)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro
30  * @(#)svc_simple.c	2.2 88/08/01 4.0 RPCSRC
31  * $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $
32  * $FreeBSD: src/lib/libc/rpc/svc_simple.c,v 1.16 2006/09/09 22:32:07 mbr Exp $
33  * $DragonFly: src/lib/libc/rpc/svc_simple.c,v 1.3 2005/11/13 12:27:04 swildner Exp $
34  */
35 /*
36  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37  */
38 
39 /*
40  * svc_simple.c
41  * Simplified front end to rpc.
42  */
43 
44 /*
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.
49  */
50 
51 #include "namespace.h"
52 #include "reentrant.h"
53 #include <sys/types.h>
54 #include <rpc/rpc.h>
55 #include <rpc/nettype.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <err.h>
60 #include "un-namespace.h"
61 
62 #include "rpc_com.h"
63 #include "mt_misc.h"
64 
65 static void universal(struct svc_req *, SVCXPRT *);
66 
67 static struct proglst {
68 	char *(*p_progname)(char *);
69 	rpcprog_t p_prognum;
70 	rpcvers_t p_versnum;
71 	rpcproc_t p_procnum;
72 	SVCXPRT *p_transp;
73 	char *p_netid;
74 	char *p_xdrbuf;
75 	int p_recvsz;
76 	xdrproc_t p_inproc, p_outproc;
77 	struct proglst *p_nxt;
78 } *proglst;
79 
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";
86 
87 /*
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.
96  */
97 
98 int
99 rpc_reg(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, xdrproc_t outproc,	/* in/out XDR procedures */
104 	char *nettype)				/* nettype */
105 {
106 	struct netconfig *nconf;
107 	int done = FALSE;
108 	void *handle;
109 
110 
111 	if (procnum == NULLPROC) {
112 		warnx("%s can't reassign procedure number %u", rpc_reg_msg,
113 			NULLPROC);
114 		return (-1);
115 	}
116 
117 	if (nettype == NULL)
118 		nettype = "netpath";		/* The default behavior */
119 	if ((handle = __rpc_setconf(nettype)) == NULL) {
120 		warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
121 		return (-1);
122 	}
123 /* VARIABLES PROTECTED BY proglst_lock: proglst */
124 	mutex_lock(&proglst_lock);
125 	while ((nconf = __rpc_getconf(handle)) != NULL) {
126 		struct proglst *pl;
127 		SVCXPRT *svcxprt;
128 		int madenow;
129 		u_int recvsz;
130 		char *xdrbuf;
131 		char *netid;
132 
133 		madenow = FALSE;
134 		svcxprt = NULL;
135 		recvsz = 0;
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;
142 				netid = pl->p_netid;
143 				break;
144 			}
145 		}
146 
147 		if (svcxprt == NULL) {
148 			struct __rpc_sockinfo si;
149 
150 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
151 			if (svcxprt == NULL)
152 				continue;
153 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
154 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
155 				SVC_DESTROY(svcxprt);
156 				continue;
157 			}
158 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
159 			if (recvsz == 0) {
160 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
161 				SVC_DESTROY(svcxprt);
162 				continue;
163 			}
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);
167 				if (xdrbuf != NULL)
168 					free(xdrbuf);
169 				if (netid != NULL)
170 					free(netid);
171 				SVC_DESTROY(svcxprt);
172 				break;
173 			}
174 			madenow = TRUE;
175 		}
176 		/*
177 		 * Check if this (program, version, netid) had already been
178 		 * registered.  The check may save a few RPC calls to rpcbind
179 		 */
180 		for (pl = proglst; pl; pl = pl->p_nxt)
181 			if ((pl->p_prognum == prognum) &&
182 				(pl->p_versnum == versnum) &&
183 				(strcmp(pl->p_netid, netid) == 0))
184 				break;
185 		if (pl == NULL) { /* Not yet */
186 			rpcb_unset(prognum, versnum, nconf);
187 		} else {
188 			/* so that svc_reg does not call rpcb_set() */
189 			nconf = NULL;
190 		}
191 
192 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
193 			warnx("%s couldn't register prog %u vers %u for %s",
194 				rpc_reg_msg, (unsigned)prognum,
195 				(unsigned)versnum, netid);
196 			if (madenow) {
197 				SVC_DESTROY(svcxprt);
198 				free(xdrbuf);
199 				free(netid);
200 			}
201 			continue;
202 		}
203 
204 		pl = malloc(sizeof (struct proglst));
205 		if (pl == NULL) {
206 			warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
207 			if (madenow) {
208 				SVC_DESTROY(svcxprt);
209 				free(xdrbuf);
210 				free(netid);
211 			}
212 			break;
213 		}
214 		pl->p_progname = progname;
215 		pl->p_prognum = prognum;
216 		pl->p_versnum = versnum;
217 		pl->p_procnum = procnum;
218 		pl->p_inproc = inproc;
219 		pl->p_outproc = outproc;
220 		pl->p_transp = svcxprt;
221 		pl->p_xdrbuf = xdrbuf;
222 		pl->p_recvsz = recvsz;
223 		pl->p_netid = netid;
224 		pl->p_nxt = proglst;
225 		proglst = pl;
226 		done = TRUE;
227 	}
228 	__rpc_endconf(handle);
229 	mutex_unlock(&proglst_lock);
230 
231 	if (done == FALSE) {
232 		warnx("%s can't find suitable transport for %s",
233 			rpc_reg_msg, nettype);
234 		return (-1);
235 	}
236 	return (0);
237 }
238 
239 /*
240  * The universal handler for the services registered using registerrpc.
241  * It handles both the connectionless and the connection oriented cases.
242  */
243 
244 static void
245 universal(struct svc_req *rqstp, SVCXPRT *transp)
246 {
247 	rpcprog_t prog;
248 	rpcvers_t vers;
249 	rpcproc_t proc;
250 	char *outdata;
251 	char *xdrbuf;
252 	struct proglst *pl;
253 
254 	/*
255 	 * enforce "procnum 0 is echo" convention
256 	 */
257 	if (rqstp->rq_proc == NULLPROC) {
258 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
259 		    FALSE) {
260 			warnx("svc_sendreply failed");
261 		}
262 		return;
263 	}
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 			memset(xdrbuf, 0, sizeof (pl->p_recvsz));
276 			/*
277 			 * Assuming that sizeof (xdrbuf) would be enough
278 			 * for the arguments; if not then the program
279 			 * may bomb. BEWARE!
280 			 */
281 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
282 				svcerr_decode(transp);
283 				mutex_unlock(&proglst_lock);
284 				return;
285 			}
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);
291 				return;
292 			}
293 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
294 				warnx(
295 			"rpc: rpc_reg trouble replying to prog %u vers %u",
296 				(unsigned)prog, (unsigned)vers);
297 				mutex_unlock(&proglst_lock);
298 				return;
299 			}
300 			/* free the decoded arguments */
301 			svc_freeargs(transp, pl->p_inproc, xdrbuf);
302 			mutex_unlock(&proglst_lock);
303 			return;
304 		}
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);
309 	return;
310 }
311