xref: /reactos/dll/3rdparty/libtirpc/src/svc_simple.c (revision 50cf16b3)
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
30  */
31 
32 //#include <sys/cdefs.h>
33 
34 /*
35  * svc_simple.c
36  * Simplified front end to rpc.
37  */
38 
39 /*
40  * This interface creates a virtual listener for all the services
41  * started thru rpc_reg(). It listens on the same endpoint for
42  * all the services and then executes the corresponding service
43  * for the given prognum and procnum.
44  */
45 #include <wintirpc.h>
46 //#include <pthread.h>
47 #include <reentrant.h>
48 #include <sys/types.h>
49 #include <rpc/rpc.h>
50 #include <rpc/nettype.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 //#include <err.h>
55 
56 #include "rpc_com.h"
57 
58 static void universal(struct svc_req *, SVCXPRT *);
59 
60 static struct proglst {
61 	char *(*p_progname)(char *);
62 	rpcprog_t p_prognum;
63 	rpcvers_t p_versnum;
64 	rpcproc_t p_procnum;
65 	SVCXPRT *p_transp;
66 	char *p_netid;
67 	char *p_xdrbuf;
68 	int p_recvsz;
69 	xdrproc_t p_inproc, p_outproc;
70 	struct proglst *p_nxt;
71 } *proglst;
72 
73 static const char rpc_reg_err[] = "%s: %s";
74 static const char rpc_reg_msg[] = "rpc_reg: ";
75 static const char __reg_err1[] = "can't find appropriate transport";
76 static const char __reg_err2[] = "can't get protocol info";
77 static const char __reg_err3[] = "unsupported transport size";
78 static const char __no_mem_str[] = "out of memory";
79 
80 /*
81  * For simplified, easy to use kind of rpc interfaces.
82  * nettype indicates the type of transport on which the service will be
83  * listening. Used for conservation of the system resource. Only one
84  * handle is created for all the services (actually one of each netid)
85  * and same xdrbuf is used for same netid. The size of the arguments
86  * is also limited by the recvsize for that transport, even if it is
87  * a COTS transport. This may be wrong, but for cases like these, they
88  * should not use the simplified interfaces like this.
89  */
90 
91 int
92 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
93 	rpcprog_t prognum;			/* program number */
94 	rpcvers_t versnum;			/* version number */
95 	rpcproc_t procnum;			/* procedure number */
96 	char *(*progname)(char *); /* Server routine */
97 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
98 	char *nettype;			/* nettype */
99 {
100 	struct netconfig *nconf;
101 	int done = FALSE;
102 	void *handle;
103 	extern mutex_t proglst_lock;
104 
105 	if (procnum == NULLPROC) {
106 		// XXXwarnx("%s can't reassign procedure number %u", rpc_reg_msg,
107 //			NULLPROC);
108 		return (-1);
109 	}
110 
111 	if (nettype == NULL)
112 		nettype = "netpath";		/* The default behavior */
113 	if ((handle = __rpc_setconf(nettype)) == NULL) {
114 		// XXX warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
115 		return (-1);
116 	}
117 /* VARIABLES PROTECTED BY proglst_lock: proglst */
118 	mutex_lock(&proglst_lock);
119 	while ((nconf = __rpc_getconf(handle)) != NULL) {
120 		struct proglst *pl;
121 		SVCXPRT *svcxprt;
122 		int madenow;
123 		u_int recvsz;
124 		char *xdrbuf;
125 		char *netid;
126 
127 		madenow = FALSE;
128 		svcxprt = NULL;
129 		recvsz = 0;
130 		xdrbuf = netid = NULL;
131 		for (pl = proglst; pl; pl = pl->p_nxt) {
132 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
133 				svcxprt = pl->p_transp;
134 				xdrbuf = pl->p_xdrbuf;
135 				recvsz = pl->p_recvsz;
136 				netid = pl->p_netid;
137 				break;
138 			}
139 		}
140 
141 		if (svcxprt == NULL) {
142 			struct __rpc_sockinfo si;
143 
144 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
145 			if (svcxprt == NULL)
146 				continue;
147 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
148 				// XXX warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
149 				SVC_DESTROY(svcxprt);
150 				continue;
151 			}
152 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
153 			if (recvsz == 0) {
154 				// XXX warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
155 				SVC_DESTROY(svcxprt);
156 				continue;
157 			}
158 			if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
159 				((netid = strdup(nconf->nc_netid)) == NULL)) {
160 				// XXX warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
161 				SVC_DESTROY(svcxprt);
162 				break;
163 			}
164 			madenow = TRUE;
165 		}
166 		/*
167 		 * Check if this (program, version, netid) had already been
168 		 * registered.  The check may save a few RPC calls to rpcbind
169 		 */
170 		for (pl = proglst; pl; pl = pl->p_nxt)
171 			if ((pl->p_prognum == prognum) &&
172 				(pl->p_versnum == versnum) &&
173 				(strcmp(pl->p_netid, netid) == 0))
174 				break;
175 		if (pl == NULL) { /* Not yet */
176 			(void) rpcb_unset(prognum, versnum, nconf);
177 		} else {
178 			/* so that svc_reg does not call rpcb_set() */
179 			nconf = NULL;
180 		}
181 
182 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
183 			// XXX warnx("%s couldn't register prog %u vers %u for %s",
184 //				rpc_reg_msg, (unsigned)prognum,
185 //				(unsigned)versnum, netid);
186 			if (madenow) {
187 				SVC_DESTROY(svcxprt);
188 				free(xdrbuf);
189 				free(netid);
190 			}
191 			continue;
192 		}
193 
194 		pl = malloc(sizeof (struct proglst));
195 		if (pl == NULL) {
196 			// XXX warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
197 			if (madenow) {
198 				SVC_DESTROY(svcxprt);
199 				free(xdrbuf);
200 				free(netid);
201 			}
202 			break;
203 		}
204 		pl->p_progname = progname;
205 		pl->p_prognum = prognum;
206 		pl->p_versnum = versnum;
207 		pl->p_procnum = procnum;
208 		pl->p_inproc = inproc;
209 		pl->p_outproc = outproc;
210 		pl->p_transp = svcxprt;
211 		pl->p_xdrbuf = xdrbuf;
212 		pl->p_recvsz = recvsz;
213 		pl->p_netid = netid;
214 		pl->p_nxt = proglst;
215 		proglst = pl;
216 		done = TRUE;
217 	}
218 	__rpc_endconf(handle);
219 	mutex_unlock(&proglst_lock);
220 
221 	if (done == FALSE) {
222 		// XXX warnx("%s cant find suitable transport for %s",
223 //			rpc_reg_msg, nettype);
224 		return (-1);
225 	}
226 	return (0);
227 }
228 
229 /*
230  * The universal handler for the services registered using registerrpc.
231  * It handles both the connectionless and the connection oriented cases.
232  */
233 
234 static void
235 universal(rqstp, transp)
236 	struct svc_req *rqstp;
237 	SVCXPRT *transp;
238 {
239 	rpcprog_t prog;
240 	rpcvers_t vers;
241 	rpcproc_t proc;
242 	char *outdata;
243 	char *xdrbuf;
244 	struct proglst *pl;
245 	extern mutex_t proglst_lock;
246 
247 	/*
248 	 * enforce "procnum 0 is echo" convention
249 	 */
250 	if (rqstp->rq_proc == NULLPROC) {
251 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
252 		    FALSE) {
253 			// XXX warnx("svc_sendreply failed");
254 		}
255 		return;
256 	}
257 	prog = rqstp->rq_prog;
258 	vers = rqstp->rq_vers;
259 	proc = rqstp->rq_proc;
260 	mutex_lock(&proglst_lock);
261 	for (pl = proglst; pl; pl = pl->p_nxt)
262 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
263 			pl->p_versnum == vers &&
264 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
265 			/* decode arguments into a CLEAN buffer */
266 			xdrbuf = pl->p_xdrbuf;
267 			/* Zero the arguments: reqd ! */
268 			(void) memset(xdrbuf, 0, sizeof (pl->p_recvsz));
269 			/*
270 			 * Assuming that sizeof (xdrbuf) would be enough
271 			 * for the arguments; if not then the program
272 			 * may bomb. BEWARE!
273 			 */
274 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
275 				svcerr_decode(transp);
276 				mutex_unlock(&proglst_lock);
277 				return;
278 			}
279 			outdata = (*(pl->p_progname))(xdrbuf);
280 			if (outdata == NULL &&
281 				pl->p_outproc != (xdrproc_t) xdr_void){
282 				/* there was an error */
283 				mutex_unlock(&proglst_lock);
284 				return;
285 			}
286 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
287 				// XXX warnx(
288 //			"rpc: rpc_reg trouble replying to prog %u vers %u",
289 //				(unsigned)prog, (unsigned)vers);
290 				mutex_unlock(&proglst_lock);
291 				return;
292 			}
293 			/* free the decoded arguments */
294 			(void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
295 			mutex_unlock(&proglst_lock);
296 			return;
297 		}
298 	mutex_unlock(&proglst_lock);
299 	/* This should never happen */
300 	// XXX warnx("rpc: rpc_reg: never registered prog %u vers %u",
301 //		(unsigned)prog, (unsigned)vers);
302 	return;
303 }
304