]>
Commit | Line | Data |
---|---|---|
cbd3dceb | 1 | /* |
a7ab6ec8 UD |
2 | * svc_simple.c |
3 | * Simplified front end to rpc. | |
ab09b221 | 4 | * |
a7ab6ec8 | 5 | * Copyright (c) 2010, Oracle America, Inc. |
cb636bb2 | 6 | * |
a7ab6ec8 UD |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are | |
9 | * met: | |
cb636bb2 | 10 | * |
a7ab6ec8 UD |
11 | * * Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * * Redistributions in binary form must reproduce the above | |
14 | * copyright notice, this list of conditions and the following | |
15 | * disclaimer in the documentation and/or other materials | |
16 | * provided with the distribution. | |
17 | * * Neither the name of the "Oracle America, Inc." nor the names of its | |
18 | * contributors may be used to endorse or promote products derived | |
19 | * from this software without specific prior written permission. | |
ab09b221 | 20 | * |
a7ab6ec8 UD |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
25 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
28 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
30 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28f540f4 RM |
33 | */ |
34 | ||
35 | #include <stdio.h> | |
e7fd8a39 | 36 | #include <string.h> |
4360eafd | 37 | #include <libintl.h> |
51028f34 | 38 | #include <unistd.h> |
28f540f4 | 39 | #include <rpc/rpc.h> |
e7fd8a39 | 40 | #include <rpc/pmap_clnt.h> |
28f540f4 RM |
41 | #include <sys/socket.h> |
42 | #include <netdb.h> | |
43 | ||
3ce1f295 UD |
44 | #include <wchar.h> |
45 | #include <libio/iolibio.h> | |
07c58f8f | 46 | #include <shlib-compat.h> |
50304ef0 | 47 | |
f1e4a4a4 | 48 | struct proglst_ |
e7fd8a39 UD |
49 | { |
50 | char *(*p_progname) (char *); | |
51 | int p_prognum; | |
52 | int p_procnum; | |
53 | xdrproc_t p_inproc, p_outproc; | |
f1e4a4a4 UD |
54 | struct proglst_ *p_nxt; |
55 | }; | |
1bc1a2b9 | 56 | #define proglst RPC_THREAD_VARIABLE(svcsimple_proglst_s) |
f1e4a4a4 | 57 | |
e7fd8a39 | 58 | |
f1e4a4a4 | 59 | static void universal (struct svc_req *rqstp, SVCXPRT *transp_s); |
1bc1a2b9 | 60 | #define transp RPC_THREAD_VARIABLE(svcsimple_transp_s) |
28f540f4 | 61 | |
e7fd8a39 | 62 | int |
7b57bfe5 UD |
63 | __registerrpc (u_long prognum, u_long versnum, u_long procnum, |
64 | char *(*progname) (char *), xdrproc_t inproc, xdrproc_t outproc) | |
28f540f4 | 65 | { |
f1e4a4a4 | 66 | struct proglst_ *pl; |
51028f34 | 67 | char *buf; |
cbd3dceb | 68 | |
e7fd8a39 UD |
69 | if (procnum == NULLPROC) |
70 | { | |
51028f34 | 71 | |
322861e8 UD |
72 | if (__asprintf (&buf, _("can't reassign procedure number %ld\n"), |
73 | NULLPROC) < 0) | |
74 | buf = NULL; | |
51028f34 | 75 | goto err_out; |
e7fd8a39 UD |
76 | } |
77 | if (transp == 0) | |
78 | { | |
7b57bfe5 | 79 | transp = svcudp_create (RPC_ANYSOCK); |
e7fd8a39 UD |
80 | if (transp == NULL) |
81 | { | |
ae65d4f3 | 82 | buf = __strdup (_("couldn't create an rpc server\n")); |
51028f34 | 83 | goto err_out; |
28f540f4 | 84 | } |
e7fd8a39 UD |
85 | } |
86 | (void) pmap_unset ((u_long) prognum, (u_long) versnum); | |
e2ec9b4d RM |
87 | if (!svc_register (transp, (u_long) prognum, (u_long) versnum, |
88 | universal, IPPROTO_UDP)) | |
e7fd8a39 | 89 | { |
322861e8 UD |
90 | if (__asprintf (&buf, _("couldn't register prog %ld vers %ld\n"), |
91 | prognum, versnum) < 0) | |
92 | buf = NULL; | |
51028f34 | 93 | goto err_out; |
e7fd8a39 | 94 | } |
f1e4a4a4 | 95 | pl = (struct proglst_ *) malloc (sizeof (struct proglst_)); |
e7fd8a39 UD |
96 | if (pl == NULL) |
97 | { | |
ae65d4f3 | 98 | buf = __strdup (_("registerrpc: out of memory\n")); |
51028f34 | 99 | goto err_out; |
e7fd8a39 UD |
100 | } |
101 | pl->p_progname = progname; | |
102 | pl->p_prognum = prognum; | |
103 | pl->p_procnum = procnum; | |
104 | pl->p_inproc = inproc; | |
105 | pl->p_outproc = outproc; | |
106 | pl->p_nxt = proglst; | |
107 | proglst = pl; | |
108 | return 0; | |
51028f34 UD |
109 | |
110 | err_out: | |
322861e8 UD |
111 | if (buf == NULL) |
112 | return -1; | |
8a259a23 | 113 | (void) __fxprintf (NULL, "%s", buf); |
51028f34 UD |
114 | free (buf); |
115 | return -1; | |
28f540f4 | 116 | } |
07c58f8f AJ |
117 | |
118 | libc_sunrpc_symbol (__registerrpc, registerrpc, GLIBC_2_0) | |
119 | ||
28f540f4 RM |
120 | |
121 | static void | |
f1e4a4a4 | 122 | universal (struct svc_req *rqstp, SVCXPRT *transp_l) |
28f540f4 | 123 | { |
e7fd8a39 UD |
124 | int prog, proc; |
125 | char *outdata; | |
126 | char xdrbuf[UDPMSGSIZE]; | |
f1e4a4a4 | 127 | struct proglst_ *pl; |
51028f34 | 128 | char *buf = NULL; |
28f540f4 | 129 | |
e7fd8a39 UD |
130 | /* |
131 | * enforce "procnum 0 is echo" convention | |
132 | */ | |
133 | if (rqstp->rq_proc == NULLPROC) | |
134 | { | |
7b57bfe5 UD |
135 | if (svc_sendreply (transp_l, (xdrproc_t)xdr_void, |
136 | (char *) NULL) == FALSE) | |
e7fd8a39 | 137 | { |
6293b803 | 138 | __write (STDERR_FILENO, "xxx\n", 4); |
e7fd8a39 | 139 | exit (1); |
28f540f4 | 140 | } |
e7fd8a39 UD |
141 | return; |
142 | } | |
143 | prog = rqstp->rq_prog; | |
144 | proc = rqstp->rq_proc; | |
145 | for (pl = proglst; pl != NULL; pl = pl->p_nxt) | |
146 | if (pl->p_prognum == prog && pl->p_procnum == proc) | |
147 | { | |
148 | /* decode arguments into a CLEAN buffer */ | |
d99431e5 | 149 | memset (xdrbuf, 0, sizeof (xdrbuf)); /* required ! */ |
f1e4a4a4 | 150 | if (!svc_getargs (transp_l, pl->p_inproc, xdrbuf)) |
e7fd8a39 | 151 | { |
7b57bfe5 | 152 | svcerr_decode (transp_l); |
e7fd8a39 UD |
153 | return; |
154 | } | |
155 | outdata = (*(pl->p_progname)) (xdrbuf); | |
7b57bfe5 | 156 | if (outdata == NULL && pl->p_outproc != (xdrproc_t)xdr_void) |
e7fd8a39 UD |
157 | /* there was an error */ |
158 | return; | |
7b57bfe5 | 159 | if (!svc_sendreply (transp_l, pl->p_outproc, outdata)) |
e7fd8a39 | 160 | { |
322861e8 UD |
161 | if (__asprintf (&buf, _("trouble replying to prog %d\n"), |
162 | pl->p_prognum) < 0) | |
163 | buf = NULL; | |
164 | goto err_out2; | |
e7fd8a39 UD |
165 | } |
166 | /* free the decoded arguments */ | |
f1e4a4a4 | 167 | (void) svc_freeargs (transp_l, pl->p_inproc, xdrbuf); |
e7fd8a39 UD |
168 | return; |
169 | } | |
322861e8 UD |
170 | if (__asprintf (&buf, _("never registered prog %d\n"), prog) < 0) |
171 | buf = NULL; | |
172 | err_out2: | |
173 | if (buf == NULL) | |
174 | exit (1); | |
8a259a23 | 175 | __fxprintf (NULL, "%s", buf); |
51028f34 | 176 | free (buf); |
e7fd8a39 | 177 | exit (1); |
28f540f4 | 178 | } |