xref: /openbsd/lib/libc/rpc/svc_simple.c (revision 137e52c2)
1*137e52c2Sderaadt /*	$OpenBSD: svc_simple.c,v 1.13 2015/09/01 19:54:01 deraadt Exp $ */
2cb7760d1Smillert 
3df930be7Sderaadt /*
4cb7760d1Smillert  * Copyright (c) 2010, Oracle America, Inc.
5df930be7Sderaadt  *
6cb7760d1Smillert  * Redistribution and use in source and binary forms, with or without
7cb7760d1Smillert  * modification, are permitted provided that the following conditions are
8cb7760d1Smillert  * met:
9df930be7Sderaadt  *
10cb7760d1Smillert  *     * Redistributions of source code must retain the above copyright
11cb7760d1Smillert  *       notice, this list of conditions and the following disclaimer.
12cb7760d1Smillert  *     * Redistributions in binary form must reproduce the above
13cb7760d1Smillert  *       copyright notice, this list of conditions and the following
14cb7760d1Smillert  *       disclaimer in the documentation and/or other materials
15cb7760d1Smillert  *       provided with the distribution.
16cb7760d1Smillert  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17cb7760d1Smillert  *       contributors may be used to endorse or promote products derived
18cb7760d1Smillert  *       from this software without specific prior written permission.
19df930be7Sderaadt  *
20cb7760d1Smillert  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21cb7760d1Smillert  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cb7760d1Smillert  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cb7760d1Smillert  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24cb7760d1Smillert  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25cb7760d1Smillert  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26cb7760d1Smillert  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27cb7760d1Smillert  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cb7760d1Smillert  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29cb7760d1Smillert  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30cb7760d1Smillert  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31cb7760d1Smillert  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt /*
35df930be7Sderaadt  * svc_simple.c
36df930be7Sderaadt  * Simplified front end to rpc.
37df930be7Sderaadt  */
38df930be7Sderaadt 
39df930be7Sderaadt #include <stdio.h>
40df930be7Sderaadt #include <stdlib.h>
41df930be7Sderaadt #include <string.h>
42df930be7Sderaadt #include <rpc/rpc.h>
43a7c2f5b9Smillert #include <rpc/pmap_clnt.h>
44df930be7Sderaadt #include <sys/socket.h>
45df930be7Sderaadt #include <netdb.h>
46df930be7Sderaadt 
47df930be7Sderaadt static struct proglst {
48df930be7Sderaadt 	char *(*p_progname)();
49df930be7Sderaadt 	int  p_prognum;
50df930be7Sderaadt 	int  p_procnum;
51df930be7Sderaadt 	xdrproc_t p_inproc, p_outproc;
52df930be7Sderaadt 	struct proglst *p_nxt;
53df930be7Sderaadt } *proglst;
54a9defc8dSotto static void universal(struct svc_req *, SVCXPRT *);
55df930be7Sderaadt static SVCXPRT *transp;
56df930be7Sderaadt 
57a7c2f5b9Smillert int
registerrpc(int prognum,int versnum,int procnum,char * (* progname)(),xdrproc_t inproc,xdrproc_t outproc)58384ec30aSotto registerrpc(int prognum, int versnum, int procnum, char *(*progname)(),
59384ec30aSotto     xdrproc_t inproc, xdrproc_t outproc)
60df930be7Sderaadt {
61a9defc8dSotto 	struct proglst *pl;
62df930be7Sderaadt 
63*137e52c2Sderaadt 	if (procnum == NULLPROC)
64df930be7Sderaadt 		return (-1);
65f590e579Sderaadt 	if (transp == NULL) {
66df930be7Sderaadt 		transp = svcudp_create(RPC_ANYSOCK);
67*137e52c2Sderaadt 		if (transp == NULL)
68df930be7Sderaadt 			return (-1);
69df930be7Sderaadt 	}
70df930be7Sderaadt 	(void) pmap_unset((u_long)prognum, (u_long)versnum);
71df930be7Sderaadt 	if (!svc_register(transp, (u_long)prognum, (u_long)versnum,
72*137e52c2Sderaadt 	    universal, IPPROTO_UDP))
73df930be7Sderaadt 		return (-1);
74fa713987Sderaadt 	pl = malloc(sizeof(struct proglst));
75*137e52c2Sderaadt 	if (pl == NULL)
76df930be7Sderaadt 		return (-1);
77df930be7Sderaadt 	pl->p_progname = progname;
78df930be7Sderaadt 	pl->p_prognum = prognum;
79df930be7Sderaadt 	pl->p_procnum = procnum;
80df930be7Sderaadt 	pl->p_inproc = inproc;
81df930be7Sderaadt 	pl->p_outproc = outproc;
82df930be7Sderaadt 	pl->p_nxt = proglst;
83df930be7Sderaadt 	proglst = pl;
84df930be7Sderaadt 	return (0);
85df930be7Sderaadt }
86df930be7Sderaadt 
87df930be7Sderaadt static void
universal(struct svc_req * rqstp,SVCXPRT * transp)88384ec30aSotto universal(struct svc_req *rqstp, SVCXPRT *transp)
89df930be7Sderaadt {
90df930be7Sderaadt 	int prog, proc;
91df930be7Sderaadt 	char *outdata;
92df930be7Sderaadt 	char xdrbuf[UDPMSGSIZE];
93df930be7Sderaadt 	struct proglst *pl;
94df930be7Sderaadt 
95df930be7Sderaadt 	/*
96df930be7Sderaadt 	 * enforce "procnum 0 is echo" convention
97df930be7Sderaadt 	 */
98df930be7Sderaadt 	if (rqstp->rq_proc == NULLPROC) {
99*137e52c2Sderaadt 		if (svc_sendreply(transp, xdr_void, NULL) == FALSE)
100df930be7Sderaadt 			exit(1);
101df930be7Sderaadt 		return;
102df930be7Sderaadt 	}
103df930be7Sderaadt 	prog = rqstp->rq_prog;
104df930be7Sderaadt 	proc = rqstp->rq_proc;
105df930be7Sderaadt 	for (pl = proglst; pl != NULL; pl = pl->p_nxt)
106df930be7Sderaadt 		if (pl->p_prognum == prog && pl->p_procnum == proc) {
107df930be7Sderaadt 			/* decode arguments into a CLEAN buffer */
108df930be7Sderaadt 			memset(xdrbuf, 0, sizeof(xdrbuf)); /* required ! */
109df930be7Sderaadt 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
110df930be7Sderaadt 				svcerr_decode(transp);
111df930be7Sderaadt 				return;
112df930be7Sderaadt 			}
113df930be7Sderaadt 			outdata = (*(pl->p_progname))(xdrbuf);
114df930be7Sderaadt 			if (outdata == NULL &&
115df930be7Sderaadt 			    pl->p_outproc != xdr_void)
116df930be7Sderaadt 				/* there was an error */
117df930be7Sderaadt 				return;
118*137e52c2Sderaadt 			if (!svc_sendreply(transp, pl->p_outproc, outdata))
119df930be7Sderaadt 				exit(1);
120df930be7Sderaadt 			/* free the decoded arguments */
121df930be7Sderaadt 			(void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
122df930be7Sderaadt 			return;
123df930be7Sderaadt 		}
124df930be7Sderaadt 	exit(1);
125df930be7Sderaadt }
126df930be7Sderaadt 
127