ReactOS 0.4.15-dev-7788-g1ad9096
svc_simple.c
Go to the documentation of this file.
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
58static void universal(struct svc_req *, SVCXPRT *);
59
60static struct proglst {
61 char *(*p_progname)(char *);
66 char *p_netid;
67 char *p_xdrbuf;
70 struct proglst *p_nxt;
72
73static const char rpc_reg_err[] = "%s: %s";
74static const char rpc_reg_msg[] = "rpc_reg: ";
75static const char __reg_err1[] = "can't find appropriate transport";
76static const char __reg_err2[] = "can't get protocol info";
77static const char __reg_err3[] = "unsupported transport size";
78static 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
91int
92rpc_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 */
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 }
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
234static void
235universal(rqstp, transp)
236 struct svc_req *rqstp;
237 SVCXPRT *transp;
238{
240 rpcvers_t vers;
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;
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);
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 */
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);
291 return;
292 }
293 /* free the decoded arguments */
294 (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
296 return;
297 }
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}
bool_t xdr_void(void)
Definition: xdr.c:92
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
static const char * progname
Definition: cjpeg.c:137
#define NULLPROC
Definition: clnt.h:294
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define svc_getargs(xprt, xargs, argsp)
Definition: svc.h:171
#define RPC_ANYFD
Definition: svc.h:328
#define svc_freeargs(xprt, xargs, argsp)
Definition: svc.h:181
#define SVC_DESTROY(xprt)
Definition: svc.h:184
UINT32 u_int
Definition: types.h:82
#define NULL
Definition: types.h:112
u_int32_t rpcprog_t
Definition: types.h:104
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
u_int32_t rpcvers_t
Definition: types.h:105
u_int32_t rpcproc_t
Definition: types.h:106
char * prog
Definition: isohybrid.c:47
mutex_t proglst_lock
Definition: mt_misc.c:77
static HANDLE proc()
Definition: pdb.c:34
#define mutex_lock(m)
Definition: reentrant.h:128
#define mutex_unlock(m)
Definition: reentrant.h:129
void * __rpc_setconf(char *nettype) const
Definition: rpc_generic.c:305
u_int __rpc_get_t_size(int af, int proto, int size)
Definition: rpc_generic.c:139
struct netconfig * __rpc_getconf(void *vhandle)
Definition: rpc_generic.c:348
int __rpc_fd2sockinfo(SOCKET fd, struct __rpc_sockinfo *sip)
Definition: rpc_generic.c:481
void __rpc_endconf(void *vhandle)
Definition: rpc_generic.c:425
bool_t rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
Definition: rpcb_clnt.c:581
_Check_return_ _CRTIMP char *__cdecl strdup(_In_opt_z_ const char *_Src)
#define memset(x, y, z)
Definition: compat.h:39
ADDRESS_FAMILY si_af
Definition: types.h:165
int si_proto
Definition: types.h:166
char * xp_netid
Definition: svc.h:118
Definition: module.h:456
char * nc_netid
Definition: netconfig.h:16
xdrproc_t p_outproc
Definition: svc_simple.c:69
rpcprog_t p_prognum
Definition: svc_simple.c:62
rpcvers_t p_versnum
Definition: svc_simple.c:63
char * p_xdrbuf
Definition: svc_simple.c:67
int p_recvsz
Definition: svc_simple.c:68
SVCXPRT * p_transp
Definition: svc_simple.c:65
rpcproc_t p_procnum
Definition: svc_simple.c:64
struct proglst * p_nxt
Definition: svc_simple.c:70
char * p_netid
Definition: svc_simple.c:66
char *(* p_progname)(char *)
Definition: svc_simple.c:61
xdrproc_t p_inproc
Definition: svc_simple.c:69
Definition: svc.h:132
u_int32_t rq_prog
Definition: svc.h:134
u_int32_t rq_proc
Definition: svc.h:136
u_int32_t rq_vers
Definition: svc.h:135
bool_t svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results, void *xdr_location)
Definition: svc.c:399
bool_t svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers, void *dispatch, const struct netconfig *nconf)
Definition: svc.c:178
void svcerr_decode(SVCXPRT *xprt)
Definition: svc.c:439
SVCXPRT * svc_tli_create(SOCKET fd, const struct netconfig *nconf, const struct t_bind *bindaddr, u_int sendsz, u_int recvsz)
Definition: svc_generic.c:182
static const char __reg_err3[]
Definition: svc_simple.c:77
static const char __reg_err1[]
Definition: svc_simple.c:75
static void universal(struct svc_req *, SVCXPRT *)
Definition: svc_simple.c:235
int rpc_reg(rpcprog_t prognum, rpcvers_t versnum, rpcproc_t procnum, char **progname, xdrproc_t inproc, xdrproc_t outproc, char *nettype)
Definition: svc_simple.c:92
static const char __reg_err2[]
Definition: svc_simple.c:76
static const char __no_mem_str[]
Definition: svc_simple.c:78
static const char rpc_reg_msg[]
Definition: svc_simple.c:74
static const char rpc_reg_err[]
Definition: svc_simple.c:73
bool_t(* xdrproc_t)(XDR *,...)
Definition: xdr.h:144