Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/svc_simple.c

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