svc_simple.c revision 1.30.8.2 1 1.30.8.2 christos /* $NetBSD: svc_simple.c,v 1.30.8.2 2008/04/25 17:44:45 christos Exp $ */
2 1.30.8.2 christos
3 1.30.8.2 christos /*
4 1.30.8.2 christos * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.30.8.2 christos * unrestricted use provided that this legend is included on all tape
6 1.30.8.2 christos * media and as a part of the software program in whole or part. Users
7 1.30.8.2 christos * may copy or modify Sun RPC without charge, but are not authorized
8 1.30.8.2 christos * to license or distribute it to anyone else except as part of a product or
9 1.30.8.2 christos * program developed by the user.
10 1.30.8.2 christos *
11 1.30.8.2 christos * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.30.8.2 christos * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.30.8.2 christos * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.30.8.2 christos *
15 1.30.8.2 christos * Sun RPC is provided with no support and without any obligation on the
16 1.30.8.2 christos * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.30.8.2 christos * modification or enhancement.
18 1.30.8.2 christos *
19 1.30.8.2 christos * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.30.8.2 christos * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.30.8.2 christos * OR ANY PART THEREOF.
22 1.30.8.2 christos *
23 1.30.8.2 christos * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.30.8.2 christos * or profits or other special, indirect and consequential damages, even if
25 1.30.8.2 christos * Sun has been advised of the possibility of such damages.
26 1.30.8.2 christos *
27 1.30.8.2 christos * Sun Microsystems, Inc.
28 1.30.8.2 christos * 2550 Garcia Avenue
29 1.30.8.2 christos * Mountain View, California 94043
30 1.30.8.2 christos */
31 1.30.8.2 christos /*
32 1.30.8.2 christos * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 1.30.8.2 christos */
34 1.30.8.2 christos
35 1.30.8.2 christos /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
36 1.30.8.2 christos
37 1.30.8.2 christos /*
38 1.30.8.2 christos * svc_simple.c
39 1.30.8.2 christos * Simplified front end to rpc.
40 1.30.8.2 christos */
41 1.30.8.2 christos
42 1.30.8.2 christos /*
43 1.30.8.2 christos * This interface creates a virtual listener for all the services
44 1.30.8.2 christos * started thru rpc_reg(). It listens on the same endpoint for
45 1.30.8.2 christos * all the services and then executes the corresponding service
46 1.30.8.2 christos * for the given prognum and procnum.
47 1.30.8.2 christos */
48 1.30.8.2 christos
49 1.30.8.2 christos #include <sys/cdefs.h>
50 1.30.8.2 christos #if defined(LIBC_SCCS) && !defined(lint)
51 1.30.8.2 christos __RCSID("$NetBSD: svc_simple.c,v 1.30.8.2 2008/04/25 17:44:45 christos Exp $");
52 1.30.8.2 christos #endif
53 1.30.8.2 christos
54 1.30.8.2 christos #include "namespace.h"
55 1.30.8.2 christos #include "reentrant.h"
56 1.30.8.2 christos #include <sys/types.h>
57 1.30.8.2 christos #include <rpc/rpc.h>
58 1.30.8.2 christos #include <rpc/nettype.h>
59 1.30.8.2 christos #include <assert.h>
60 1.30.8.2 christos #include <err.h>
61 1.30.8.2 christos #include <stdio.h>
62 1.30.8.2 christos #include <stdlib.h>
63 1.30.8.2 christos #include <string.h>
64 1.30.8.2 christos
65 1.30.8.2 christos #include "rpc_internal.h"
66 1.30.8.2 christos
67 1.30.8.2 christos #ifdef __weak_alias
68 1.30.8.2 christos __weak_alias(rpc_reg,_rpc_reg)
69 1.30.8.2 christos #endif
70 1.30.8.2 christos
71 1.30.8.2 christos static void universal __P((struct svc_req *, SVCXPRT *));
72 1.30.8.2 christos
73 1.30.8.2 christos static struct proglst {
74 1.30.8.2 christos char *(*p_progname) __P((char *));
75 1.30.8.2 christos rpcprog_t p_prognum;
76 1.30.8.2 christos rpcvers_t p_versnum;
77 1.30.8.2 christos rpcproc_t p_procnum;
78 1.30.8.2 christos SVCXPRT *p_transp;
79 1.30.8.2 christos char *p_netid;
80 1.30.8.2 christos char *p_xdrbuf;
81 1.30.8.2 christos int p_recvsz;
82 1.30.8.2 christos xdrproc_t p_inproc, p_outproc;
83 1.30.8.2 christos struct proglst *p_nxt;
84 1.30.8.2 christos } *proglst;
85 1.30.8.2 christos
86 1.30.8.2 christos static const char rpc_reg_err[] = "%s: %s";
87 1.30.8.2 christos static const char rpc_reg_msg[] = "rpc_reg: ";
88 1.30.8.2 christos static const char __reg_err1[] = "can't find appropriate transport";
89 1.30.8.2 christos static const char __reg_err2[] = "can't get protocol info";
90 1.30.8.2 christos static const char __reg_err3[] = "unsupported transport size";
91 1.30.8.2 christos static const char __no_mem_str[] = "out of memory";
92 1.30.8.2 christos
93 1.30.8.2 christos /*
94 1.30.8.2 christos * For simplified, easy to use kind of rpc interfaces.
95 1.30.8.2 christos * nettype indicates the type of transport on which the service will be
96 1.30.8.2 christos * listening. Used for conservation of the system resource. Only one
97 1.30.8.2 christos * handle is created for all the services (actually one of each netid)
98 1.30.8.2 christos * and same xdrbuf is used for same netid. The size of the arguments
99 1.30.8.2 christos * is also limited by the recvsize for that transport, even if it is
100 1.30.8.2 christos * a COTS transport. This may be wrong, but for cases like these, they
101 1.30.8.2 christos * should not use the simplified interfaces like this.
102 1.30.8.2 christos */
103 1.30.8.2 christos
104 1.30.8.2 christos int
105 1.30.8.2 christos rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
106 1.30.8.2 christos rpcprog_t prognum; /* program number */
107 1.30.8.2 christos rpcvers_t versnum; /* version number */
108 1.30.8.2 christos rpcproc_t procnum; /* procedure number */
109 1.30.8.2 christos char *(*progname) __P((char *)); /* Server routine */
110 1.30.8.2 christos xdrproc_t inproc, outproc; /* in/out XDR procedures */
111 1.30.8.2 christos char *nettype; /* nettype */
112 1.30.8.2 christos {
113 1.30.8.2 christos struct netconfig *nconf;
114 1.30.8.2 christos int done = FALSE;
115 1.30.8.2 christos void *handle;
116 1.30.8.2 christos #ifdef _REENTRANT
117 1.30.8.2 christos extern mutex_t proglst_lock;
118 1.30.8.2 christos #endif
119 1.30.8.2 christos
120 1.30.8.2 christos if (procnum == NULLPROC) {
121 1.30.8.2 christos warnx("%s can't reassign procedure number %u", rpc_reg_msg,
122 1.30.8.2 christos NULLPROC);
123 1.30.8.2 christos return (-1);
124 1.30.8.2 christos }
125 1.30.8.2 christos
126 1.30.8.2 christos if (nettype == NULL)
127 1.30.8.2 christos nettype = __UNCONST("netpath"); /* The default behavior */
128 1.30.8.2 christos if ((handle = __rpc_setconf(nettype)) == NULL) {
129 1.30.8.2 christos warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
130 1.30.8.2 christos return (-1);
131 1.30.8.2 christos }
132 1.30.8.2 christos /* VARIABLES PROTECTED BY proglst_lock: proglst */
133 1.30.8.2 christos mutex_lock(&proglst_lock);
134 1.30.8.2 christos while ((nconf = __rpc_getconf(handle)) != NULL) {
135 1.30.8.2 christos struct proglst *pl;
136 1.30.8.2 christos SVCXPRT *svcxprt;
137 1.30.8.2 christos int madenow;
138 1.30.8.2 christos u_int recvsz;
139 1.30.8.2 christos char *xdrbuf;
140 1.30.8.2 christos char *netid;
141 1.30.8.2 christos
142 1.30.8.2 christos madenow = FALSE;
143 1.30.8.2 christos svcxprt = NULL;
144 1.30.8.2 christos recvsz = 0;
145 1.30.8.2 christos xdrbuf = NULL;
146 1.30.8.2 christos netid = NULL;
147 1.30.8.2 christos for (pl = proglst; pl; pl = pl->p_nxt)
148 1.30.8.2 christos if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
149 1.30.8.2 christos svcxprt = pl->p_transp;
150 1.30.8.2 christos xdrbuf = pl->p_xdrbuf;
151 1.30.8.2 christos recvsz = pl->p_recvsz;
152 1.30.8.2 christos netid = pl->p_netid;
153 1.30.8.2 christos break;
154 1.30.8.2 christos }
155 1.30.8.2 christos
156 1.30.8.2 christos if (svcxprt == NULL) {
157 1.30.8.2 christos struct __rpc_sockinfo si;
158 1.30.8.2 christos
159 1.30.8.2 christos svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
160 1.30.8.2 christos if (svcxprt == NULL)
161 1.30.8.2 christos continue;
162 1.30.8.2 christos if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
163 1.30.8.2 christos warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
164 1.30.8.2 christos SVC_DESTROY(svcxprt);
165 1.30.8.2 christos continue;
166 1.30.8.2 christos }
167 1.30.8.2 christos recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
168 1.30.8.2 christos if (recvsz == 0) {
169 1.30.8.2 christos warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
170 1.30.8.2 christos SVC_DESTROY(svcxprt);
171 1.30.8.2 christos continue;
172 1.30.8.2 christos }
173 1.30.8.2 christos if (((xdrbuf = malloc((size_t)recvsz)) == NULL) ||
174 1.30.8.2 christos ((netid = strdup(nconf->nc_netid)) == NULL)) {
175 1.30.8.2 christos warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
176 1.30.8.2 christos if (xdrbuf != NULL)
177 1.30.8.2 christos free(xdrbuf);
178 1.30.8.2 christos if (netid != NULL)
179 1.30.8.2 christos free(netid);
180 1.30.8.2 christos SVC_DESTROY(svcxprt);
181 1.30.8.2 christos break;
182 1.30.8.2 christos }
183 1.30.8.2 christos madenow = TRUE;
184 1.30.8.2 christos }
185 1.30.8.2 christos /*
186 1.30.8.2 christos * Check if this (program, version, netid) had already been
187 1.30.8.2 christos * registered. The check may save a few RPC calls to rpcbind
188 1.30.8.2 christos */
189 1.30.8.2 christos for (pl = proglst; pl; pl = pl->p_nxt)
190 1.30.8.2 christos if ((pl->p_prognum == prognum) &&
191 1.30.8.2 christos (pl->p_versnum == versnum) &&
192 1.30.8.2 christos (strcmp(pl->p_netid, netid) == 0))
193 1.30.8.2 christos break;
194 1.30.8.2 christos if (pl == NULL) { /* Not yet */
195 1.30.8.2 christos (void) rpcb_unset(prognum, versnum, nconf);
196 1.30.8.2 christos } else {
197 1.30.8.2 christos /* so that svc_reg does not call rpcb_set() */
198 1.30.8.2 christos nconf = NULL;
199 1.30.8.2 christos }
200 1.30.8.2 christos
201 1.30.8.2 christos if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
202 1.30.8.2 christos warnx("%s couldn't register prog %u vers %u for %s",
203 1.30.8.2 christos rpc_reg_msg, (unsigned)prognum,
204 1.30.8.2 christos (unsigned)versnum, netid);
205 1.30.8.2 christos if (madenow) {
206 1.30.8.2 christos SVC_DESTROY(svcxprt);
207 1.30.8.2 christos free(xdrbuf);
208 1.30.8.2 christos free(netid);
209 1.30.8.2 christos }
210 1.30.8.2 christos continue;
211 1.30.8.2 christos }
212 1.30.8.2 christos
213 1.30.8.2 christos pl = malloc(sizeof(*pl));
214 1.30.8.2 christos if (pl == NULL) {
215 1.30.8.2 christos warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
216 1.30.8.2 christos if (madenow) {
217 1.30.8.2 christos SVC_DESTROY(svcxprt);
218 1.30.8.2 christos free(xdrbuf);
219 1.30.8.2 christos free(netid);
220 1.30.8.2 christos }
221 1.30.8.2 christos break;
222 1.30.8.2 christos }
223 1.30.8.2 christos pl->p_progname = progname;
224 1.30.8.2 christos pl->p_prognum = prognum;
225 1.30.8.2 christos pl->p_versnum = versnum;
226 1.30.8.2 christos pl->p_procnum = procnum;
227 1.30.8.2 christos pl->p_inproc = inproc;
228 1.30.8.2 christos pl->p_outproc = outproc;
229 1.30.8.2 christos pl->p_transp = svcxprt;
230 1.30.8.2 christos pl->p_xdrbuf = xdrbuf;
231 1.30.8.2 christos pl->p_recvsz = recvsz;
232 1.30.8.2 christos pl->p_netid = netid;
233 1.30.8.2 christos pl->p_nxt = proglst;
234 1.30.8.2 christos proglst = pl;
235 1.30.8.2 christos done = TRUE;
236 1.30.8.2 christos }
237 1.30.8.2 christos __rpc_endconf(handle);
238 1.30.8.2 christos mutex_unlock(&proglst_lock);
239 1.30.8.2 christos
240 1.30.8.2 christos if (done == FALSE) {
241 1.30.8.2 christos warnx("%s cant find suitable transport for %s",
242 1.30.8.2 christos rpc_reg_msg, nettype);
243 1.30.8.2 christos return (-1);
244 1.30.8.2 christos }
245 1.30.8.2 christos return (0);
246 1.30.8.2 christos }
247 1.30.8.2 christos
248 1.30.8.2 christos /*
249 1.30.8.2 christos * The universal handler for the services registered using registerrpc.
250 1.30.8.2 christos * It handles both the connectionless and the connection oriented cases.
251 1.30.8.2 christos */
252 1.30.8.2 christos
253 1.30.8.2 christos static void
254 1.30.8.2 christos universal(rqstp, transp)
255 1.30.8.2 christos struct svc_req *rqstp;
256 1.30.8.2 christos SVCXPRT *transp;
257 1.30.8.2 christos {
258 1.30.8.2 christos rpcprog_t prog;
259 1.30.8.2 christos rpcvers_t vers;
260 1.30.8.2 christos rpcproc_t proc;
261 1.30.8.2 christos char *outdata;
262 1.30.8.2 christos char *xdrbuf;
263 1.30.8.2 christos struct proglst *pl;
264 1.30.8.2 christos #ifdef _REENTRANT
265 1.30.8.2 christos extern mutex_t proglst_lock;
266 1.30.8.2 christos #endif
267 1.30.8.2 christos
268 1.30.8.2 christos _DIAGASSERT(rqstp != NULL);
269 1.30.8.2 christos _DIAGASSERT(transp != NULL);
270 1.30.8.2 christos
271 1.30.8.2 christos /*
272 1.30.8.2 christos * enforce "procnum 0 is echo" convention
273 1.30.8.2 christos */
274 1.30.8.2 christos if (rqstp->rq_proc == NULLPROC) {
275 1.30.8.2 christos if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
276 1.30.8.2 christos FALSE) {
277 1.30.8.2 christos warnx("svc_sendreply failed");
278 1.30.8.2 christos }
279 1.30.8.2 christos return;
280 1.30.8.2 christos }
281 1.30.8.2 christos prog = rqstp->rq_prog;
282 1.30.8.2 christos vers = rqstp->rq_vers;
283 1.30.8.2 christos proc = rqstp->rq_proc;
284 1.30.8.2 christos mutex_lock(&proglst_lock);
285 1.30.8.2 christos for (pl = proglst; pl; pl = pl->p_nxt)
286 1.30.8.2 christos if (pl->p_prognum == prog && pl->p_procnum == proc &&
287 1.30.8.2 christos pl->p_versnum == vers &&
288 1.30.8.2 christos (strcmp(pl->p_netid, transp->xp_netid) == 0)) {
289 1.30.8.2 christos /* decode arguments into a CLEAN buffer */
290 1.30.8.2 christos xdrbuf = pl->p_xdrbuf;
291 1.30.8.2 christos /* Zero the arguments: reqd ! */
292 1.30.8.2 christos (void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
293 1.30.8.2 christos /*
294 1.30.8.2 christos * Assuming that sizeof (xdrbuf) would be enough
295 1.30.8.2 christos * for the arguments; if not then the program
296 1.30.8.2 christos * may bomb. BEWARE!
297 1.30.8.2 christos */
298 1.30.8.2 christos if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
299 1.30.8.2 christos svcerr_decode(transp);
300 1.30.8.2 christos mutex_unlock(&proglst_lock);
301 1.30.8.2 christos return;
302 1.30.8.2 christos }
303 1.30.8.2 christos outdata = (*(pl->p_progname))(xdrbuf);
304 1.30.8.2 christos if (outdata == NULL &&
305 1.30.8.2 christos pl->p_outproc != (xdrproc_t) xdr_void){
306 1.30.8.2 christos /* there was an error */
307 1.30.8.2 christos mutex_unlock(&proglst_lock);
308 1.30.8.2 christos return;
309 1.30.8.2 christos }
310 1.30.8.2 christos if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
311 1.30.8.2 christos warnx(
312 1.30.8.2 christos "rpc: rpc_reg trouble replying to prog %u vers %u",
313 1.30.8.2 christos (unsigned)prog, (unsigned)vers);
314 1.30.8.2 christos mutex_unlock(&proglst_lock);
315 1.30.8.2 christos return;
316 1.30.8.2 christos }
317 1.30.8.2 christos /* free the decoded arguments */
318 1.30.8.2 christos (void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
319 1.30.8.2 christos mutex_unlock(&proglst_lock);
320 1.30.8.2 christos return;
321 1.30.8.2 christos }
322 1.30.8.2 christos mutex_unlock(&proglst_lock);
323 1.30.8.2 christos /* This should never happen */
324 1.30.8.2 christos warnx("rpc: rpc_reg: never registered prog %u vers %u",
325 1.30.8.2 christos (unsigned)prog, (unsigned)vers);
326 1.30.8.2 christos return;
327 1.30.8.2 christos }
328