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