Imported Upstream version 0.2.2
[platform/upstream/libtirpc.git] / src / svc_simple.c
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 <pthread.h>
46 #include <reentrant.h>
47 #include <sys/types.h>
48 #include <rpc/rpc.h>
49 #include <rpc/nettype.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <err.h>
54
55 #include "rpc_com.h"
56
57 static void universal(struct svc_req *, SVCXPRT *);
58
59 static struct proglst {
60         char *(*p_progname)(char *);
61         rpcprog_t p_prognum;
62         rpcvers_t p_versnum;
63         rpcproc_t p_procnum;
64         SVCXPRT *p_transp;
65         char *p_netid;
66         char *p_xdrbuf;
67         int p_recvsz;
68         xdrproc_t p_inproc, p_outproc;
69         struct proglst *p_nxt;
70 } *proglst;
71
72 static const char rpc_reg_err[] = "%s: %s";
73 static const char rpc_reg_msg[] = "rpc_reg: ";
74 static const char __reg_err1[] = "can't find appropriate transport";
75 static const char __reg_err2[] = "can't get protocol info";
76 static const char __reg_err3[] = "unsupported transport size";
77 static const char __no_mem_str[] = "out of memory";
78
79 /*
80  * For simplified, easy to use kind of rpc interfaces.
81  * nettype indicates the type of transport on which the service will be
82  * listening. Used for conservation of the system resource. Only one
83  * handle is created for all the services (actually one of each netid)
84  * and same xdrbuf is used for same netid. The size of the arguments
85  * is also limited by the recvsize for that transport, even if it is
86  * a COTS transport. This may be wrong, but for cases like these, they
87  * should not use the simplified interfaces like this.
88  */
89
90 int
91 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
92         rpcprog_t prognum;                      /* program number */
93         rpcvers_t versnum;                      /* version number */
94         rpcproc_t procnum;                      /* procedure number */
95         char *(*progname)(char *); /* Server routine */
96         xdrproc_t inproc, outproc;      /* in/out XDR procedures */
97         char *nettype;                  /* nettype */
98 {
99         struct netconfig *nconf;
100         int done = FALSE;
101         void *handle;
102         extern mutex_t proglst_lock;
103
104         if (procnum == NULLPROC) {
105                 warnx("%s can't reassign procedure number %u", rpc_reg_msg,
106                         NULLPROC);
107                 return (-1);
108         }
109
110         if (nettype == NULL)
111                 nettype = "netpath";            /* The default behavior */
112         if ((handle = __rpc_setconf(nettype)) == NULL) {
113                 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
114                 return (-1);
115         }
116 /* VARIABLES PROTECTED BY proglst_lock: proglst */
117         mutex_lock(&proglst_lock);
118         while ((nconf = __rpc_getconf(handle)) != NULL) {
119                 struct proglst *pl;
120                 SVCXPRT *svcxprt;
121                 int madenow;
122                 u_int recvsz;
123                 char *xdrbuf;
124                 char *netid;
125
126                 madenow = FALSE;
127                 svcxprt = NULL;
128                 recvsz = 0;
129                 xdrbuf = netid = NULL;
130                 for (pl = proglst; pl; pl = pl->p_nxt) {
131                         if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
132                                 svcxprt = pl->p_transp;
133                                 xdrbuf = pl->p_xdrbuf;
134                                 recvsz = pl->p_recvsz;
135                                 netid = pl->p_netid;
136                                 break;
137                         }
138                 }
139
140                 if (svcxprt == NULL) {
141                         struct __rpc_sockinfo si;
142
143                         svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
144                         if (svcxprt == NULL)
145                                 continue;
146                         if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
147                                 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
148                                 SVC_DESTROY(svcxprt);
149                                 continue;
150                         }
151                         recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
152                         if (recvsz == 0) {
153                                 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
154                                 SVC_DESTROY(svcxprt);
155                                 continue;
156                         }
157                         if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
158                                 ((netid = strdup(nconf->nc_netid)) == NULL)) {
159                                 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
160                                 SVC_DESTROY(svcxprt);
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 }