]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/svc_simple.c
Fix netname2host
[thirdparty/glibc.git] / sunrpc / svc_simple.c
CommitLineData
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>
50304ef0 46
f1e4a4a4 47struct proglst_
e7fd8a39
UD
48 {
49 char *(*p_progname) (char *);
50 int p_prognum;
51 int p_procnum;
52 xdrproc_t p_inproc, p_outproc;
f1e4a4a4
UD
53 struct proglst_ *p_nxt;
54 };
55#ifdef _RPC_THREAD_SAFE_
1bc1a2b9 56#define proglst RPC_THREAD_VARIABLE(svcsimple_proglst_s)
f1e4a4a4
UD
57#else
58static struct proglst_ *proglst;
59#endif
60
e7fd8a39 61
f1e4a4a4
UD
62static void universal (struct svc_req *rqstp, SVCXPRT *transp_s);
63#ifdef _RPC_THREAD_SAFE_
1bc1a2b9 64#define transp RPC_THREAD_VARIABLE(svcsimple_transp_s)
f1e4a4a4 65#else
28f540f4 66static SVCXPRT *transp;
f1e4a4a4 67#endif
28f540f4 68
e7fd8a39 69int
7b57bfe5
UD
70__registerrpc (u_long prognum, u_long versnum, u_long procnum,
71 char *(*progname) (char *), xdrproc_t inproc, xdrproc_t outproc)
28f540f4 72{
f1e4a4a4 73 struct proglst_ *pl;
51028f34 74 char *buf;
cbd3dceb 75
e7fd8a39
UD
76 if (procnum == NULLPROC)
77 {
51028f34 78
322861e8
UD
79 if (__asprintf (&buf, _("can't reassign procedure number %ld\n"),
80 NULLPROC) < 0)
81 buf = NULL;
51028f34 82 goto err_out;
e7fd8a39
UD
83 }
84 if (transp == 0)
85 {
7b57bfe5 86 transp = svcudp_create (RPC_ANYSOCK);
e7fd8a39
UD
87 if (transp == NULL)
88 {
51028f34
UD
89 buf = strdup (_("couldn't create an rpc server\n"));
90 goto err_out;
28f540f4 91 }
e7fd8a39
UD
92 }
93 (void) pmap_unset ((u_long) prognum, (u_long) versnum);
e2ec9b4d
RM
94 if (!svc_register (transp, (u_long) prognum, (u_long) versnum,
95 universal, IPPROTO_UDP))
e7fd8a39 96 {
322861e8
UD
97 if (__asprintf (&buf, _("couldn't register prog %ld vers %ld\n"),
98 prognum, versnum) < 0)
99 buf = NULL;
51028f34 100 goto err_out;
e7fd8a39 101 }
f1e4a4a4 102 pl = (struct proglst_ *) malloc (sizeof (struct proglst_));
e7fd8a39
UD
103 if (pl == NULL)
104 {
51028f34
UD
105 buf = strdup (_("registerrpc: out of memory\n"));
106 goto err_out;
e7fd8a39
UD
107 }
108 pl->p_progname = progname;
109 pl->p_prognum = prognum;
110 pl->p_procnum = procnum;
111 pl->p_inproc = inproc;
112 pl->p_outproc = outproc;
113 pl->p_nxt = proglst;
114 proglst = pl;
115 return 0;
51028f34
UD
116
117 err_out:
322861e8
UD
118 if (buf == NULL)
119 return -1;
8a259a23 120 (void) __fxprintf (NULL, "%s", buf);
51028f34
UD
121 free (buf);
122 return -1;
28f540f4 123}
7b57bfe5 124compat_symbol (libc, __registerrpc, registerrpc, GLIBC_2_0);
28f540f4
RM
125
126static void
f1e4a4a4 127universal (struct svc_req *rqstp, SVCXPRT *transp_l)
28f540f4 128{
e7fd8a39
UD
129 int prog, proc;
130 char *outdata;
131 char xdrbuf[UDPMSGSIZE];
f1e4a4a4 132 struct proglst_ *pl;
51028f34 133 char *buf = NULL;
28f540f4 134
e7fd8a39
UD
135 /*
136 * enforce "procnum 0 is echo" convention
137 */
138 if (rqstp->rq_proc == NULLPROC)
139 {
7b57bfe5
UD
140 if (svc_sendreply (transp_l, (xdrproc_t)xdr_void,
141 (char *) NULL) == FALSE)
e7fd8a39 142 {
6293b803 143 __write (STDERR_FILENO, "xxx\n", 4);
e7fd8a39 144 exit (1);
28f540f4 145 }
e7fd8a39
UD
146 return;
147 }
148 prog = rqstp->rq_prog;
149 proc = rqstp->rq_proc;
150 for (pl = proglst; pl != NULL; pl = pl->p_nxt)
151 if (pl->p_prognum == prog && pl->p_procnum == proc)
152 {
153 /* decode arguments into a CLEAN buffer */
50304ef0 154 __bzero (xdrbuf, sizeof (xdrbuf)); /* required ! */
f1e4a4a4 155 if (!svc_getargs (transp_l, pl->p_inproc, xdrbuf))
e7fd8a39 156 {
7b57bfe5 157 svcerr_decode (transp_l);
e7fd8a39
UD
158 return;
159 }
160 outdata = (*(pl->p_progname)) (xdrbuf);
7b57bfe5 161 if (outdata == NULL && pl->p_outproc != (xdrproc_t)xdr_void)
e7fd8a39
UD
162 /* there was an error */
163 return;
7b57bfe5 164 if (!svc_sendreply (transp_l, pl->p_outproc, outdata))
e7fd8a39 165 {
322861e8
UD
166 if (__asprintf (&buf, _("trouble replying to prog %d\n"),
167 pl->p_prognum) < 0)
168 buf = NULL;
169 goto err_out2;
e7fd8a39
UD
170 }
171 /* free the decoded arguments */
f1e4a4a4 172 (void) svc_freeargs (transp_l, pl->p_inproc, xdrbuf);
e7fd8a39
UD
173 return;
174 }
322861e8
UD
175 if (__asprintf (&buf, _("never registered prog %d\n"), prog) < 0)
176 buf = NULL;
177 err_out2:
178 if (buf == NULL)
179 exit (1);
8a259a23 180 __fxprintf (NULL, "%s", buf);
51028f34 181 free (buf);
e7fd8a39 182 exit (1);
28f540f4 183}