|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* svc_simple.c
|
|
Packit |
6c4009 |
* Simplified front end to rpc.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Copyright (c) 2010, Oracle America, Inc.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
6c4009 |
* modification, are permitted provided that the following conditions are
|
|
Packit |
6c4009 |
* met:
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* * Redistributions of source code must retain the above copyright
|
|
Packit |
6c4009 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
6c4009 |
* * Redistributions in binary form must reproduce the above
|
|
Packit |
6c4009 |
* copyright notice, this list of conditions and the following
|
|
Packit |
6c4009 |
* disclaimer in the documentation and/or other materials
|
|
Packit |
6c4009 |
* provided with the distribution.
|
|
Packit |
6c4009 |
* * Neither the name of the "Oracle America, Inc." nor the names of its
|
|
Packit |
6c4009 |
* contributors may be used to endorse or promote products derived
|
|
Packit |
6c4009 |
* from this software without specific prior written permission.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
6c4009 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
6c4009 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
Packit |
6c4009 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
Packit |
6c4009 |
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
Packit |
6c4009 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
6c4009 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
Packit |
6c4009 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
6c4009 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
Packit |
6c4009 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
Packit |
6c4009 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
6c4009 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <libintl.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <rpc/rpc.h>
|
|
Packit |
6c4009 |
#include <rpc/pmap_clnt.h>
|
|
Packit |
6c4009 |
#include <sys/socket.h>
|
|
Packit |
6c4009 |
#include <netdb.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <wchar.h>
|
|
Packit |
6c4009 |
#include <libio/iolibio.h>
|
|
Packit |
6c4009 |
#include <shlib-compat.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct proglst_
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *(*p_progname) (char *);
|
|
Packit |
6c4009 |
int p_prognum;
|
|
Packit |
6c4009 |
int p_procnum;
|
|
Packit |
6c4009 |
xdrproc_t p_inproc, p_outproc;
|
|
Packit |
6c4009 |
struct proglst_ *p_nxt;
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
#define proglst RPC_THREAD_VARIABLE(svcsimple_proglst_s)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void universal (struct svc_req *rqstp, SVCXPRT *transp_s);
|
|
Packit |
6c4009 |
#define transp RPC_THREAD_VARIABLE(svcsimple_transp_s)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__registerrpc (u_long prognum, u_long versnum, u_long procnum,
|
|
Packit |
6c4009 |
char *(*progname) (char *), xdrproc_t inproc, xdrproc_t outproc)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct proglst_ *pl;
|
|
Packit |
6c4009 |
char *buf;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (procnum == NULLPROC)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__asprintf (&buf, _("can't reassign procedure number %ld\n"),
|
|
Packit |
6c4009 |
NULLPROC) < 0)
|
|
Packit |
6c4009 |
buf = NULL;
|
|
Packit |
6c4009 |
goto err_out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (transp == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
transp = svcudp_create (RPC_ANYSOCK);
|
|
Packit |
6c4009 |
if (transp == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
buf = __strdup (_("couldn't create an rpc server\n"));
|
|
Packit |
6c4009 |
goto err_out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
(void) pmap_unset ((u_long) prognum, (u_long) versnum);
|
|
Packit |
6c4009 |
if (!svc_register (transp, (u_long) prognum, (u_long) versnum,
|
|
Packit |
6c4009 |
universal, IPPROTO_UDP))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__asprintf (&buf, _("couldn't register prog %ld vers %ld\n"),
|
|
Packit |
6c4009 |
prognum, versnum) < 0)
|
|
Packit |
6c4009 |
buf = NULL;
|
|
Packit |
6c4009 |
goto err_out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
pl = (struct proglst_ *) malloc (sizeof (struct proglst_));
|
|
Packit |
6c4009 |
if (pl == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
buf = __strdup (_("registerrpc: out of memory\n"));
|
|
Packit |
6c4009 |
goto err_out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
pl->p_progname = progname;
|
|
Packit |
6c4009 |
pl->p_prognum = prognum;
|
|
Packit |
6c4009 |
pl->p_procnum = procnum;
|
|
Packit |
6c4009 |
pl->p_inproc = inproc;
|
|
Packit |
6c4009 |
pl->p_outproc = outproc;
|
|
Packit |
6c4009 |
pl->p_nxt = proglst;
|
|
Packit |
6c4009 |
proglst = pl;
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
err_out:
|
|
Packit |
6c4009 |
if (buf == NULL)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
(void) __fxprintf (NULL, "%s", buf);
|
|
Packit |
6c4009 |
free (buf);
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
libc_sunrpc_symbol (__registerrpc, registerrpc, GLIBC_2_0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
universal (struct svc_req *rqstp, SVCXPRT *transp_l)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int prog, proc;
|
|
Packit |
6c4009 |
char *outdata;
|
|
Packit |
6c4009 |
char xdrbuf[UDPMSGSIZE];
|
|
Packit |
6c4009 |
struct proglst_ *pl;
|
|
Packit |
6c4009 |
char *buf = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* enforce "procnum 0 is echo" convention
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
if (rqstp->rq_proc == NULLPROC)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (svc_sendreply (transp_l, (xdrproc_t)xdr_void,
|
|
Packit |
6c4009 |
(char *) NULL) == FALSE)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__write (STDERR_FILENO, "xxx\n", 4);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
prog = rqstp->rq_prog;
|
|
Packit |
6c4009 |
proc = rqstp->rq_proc;
|
|
Packit |
6c4009 |
for (pl = proglst; pl != NULL; pl = pl->p_nxt)
|
|
Packit |
6c4009 |
if (pl->p_prognum == prog && pl->p_procnum == proc)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* decode arguments into a CLEAN buffer */
|
|
Packit |
6c4009 |
memset (xdrbuf, 0, sizeof (xdrbuf)); /* required ! */
|
|
Packit |
6c4009 |
if (!svc_getargs (transp_l, pl->p_inproc, xdrbuf))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
svcerr_decode (transp_l);
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
outdata = (*(pl->p_progname)) (xdrbuf);
|
|
Packit |
6c4009 |
if (outdata == NULL && pl->p_outproc != (xdrproc_t)xdr_void)
|
|
Packit |
6c4009 |
/* there was an error */
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
if (!svc_sendreply (transp_l, pl->p_outproc, outdata))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__asprintf (&buf, _("trouble replying to prog %d\n"),
|
|
Packit |
6c4009 |
pl->p_prognum) < 0)
|
|
Packit |
6c4009 |
buf = NULL;
|
|
Packit |
6c4009 |
goto err_out2;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
/* free the decoded arguments */
|
|
Packit |
6c4009 |
(void) svc_freeargs (transp_l, pl->p_inproc, xdrbuf);
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (__asprintf (&buf, _("never registered prog %d\n"), prog) < 0)
|
|
Packit |
6c4009 |
buf = NULL;
|
|
Packit |
6c4009 |
err_out2:
|
|
Packit |
6c4009 |
if (buf == NULL)
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
__fxprintf (NULL, "%s", buf);
|
|
Packit |
6c4009 |
free (buf);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|