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