47524f916c32a55d542e1632cd0e0ac4b579dc7f
[shibboleth/cpp-sp.git] / oncrpc / svc_simp.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)svc_simple.c     2.2 88/08/01 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  *
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  *
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  *
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  *
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  *
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * svc_simple.c
47  * Simplified front end to rpc.
48  *
49  * Copyright (C) 1984, Sun Microsystems, Inc.
50  */
51
52 #include <stdio.h>
53 #include <rpc/rpc.h>
54 #ifndef WIN32
55 #include <sys/socket.h>
56 #include <netdb.h>
57 #endif
58
59 static struct proglst {
60         char *(*p_progname)();
61         int  p_prognum;
62         int  p_procnum;
63         xdrproc_t p_inproc, p_outproc;
64         struct proglst *p_nxt;
65 } *proglst;
66 static void universal();
67 static SVCXPRT *transp;
68 struct proglst *pl;
69
70 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
71         char *(*progname)();
72         xdrproc_t inproc, outproc;
73 {
74
75         if (procnum == NULLPROC) {
76 #ifdef WIN32
77                 nt_rpc_report(
78                     "can't reassign procedure number 0\n");
79 #else
80                 (void) fprintf(stderr,
81                     "can't reassign procedure number %d\n", NULLPROC);
82 #endif
83                 return (-1);
84         }
85         if (transp == 0) {
86                 transp = svcudp_create(RPC_ANYSOCK);
87                 if (transp == NULL) {
88 #ifdef WIN32
89                         nt_rpc_report("couldn't create an rpc server\n");
90 #else
91                         (void) fprintf(stderr, "couldn't create an rpc server\n");
92 #endif
93                         return (-1);
94                 }
95         }
96         (void) pmap_unset((u_long)prognum, (u_long)versnum);
97         if (!svc_register(transp, (u_long)prognum, (u_long)versnum,
98             universal, IPPROTO_UDP)) {
99 #ifdef WIN32
100                 nt_rpc_report("couldn't register prog");
101 #else
102                 (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
103                     prognum, versnum);
104 #endif
105                 return (-1);
106         }
107         pl = (struct proglst *)malloc(sizeof(struct proglst));
108         if (pl == NULL) {
109 #ifdef WIN32
110                 nt_rpc_report("registerrpc: out of memory\n");
111 #else
112                 (void) fprintf(stderr, "registerrpc: out of memory\n");
113 #endif
114                 return (-1);
115         }
116         pl->p_progname = progname;
117         pl->p_prognum = prognum;
118         pl->p_procnum = procnum;
119         pl->p_inproc = inproc;
120         pl->p_outproc = outproc;
121         pl->p_nxt = proglst;
122         proglst = pl;
123         return (0);
124 }
125
126 static void
127 universal(rqstp, transp)
128         struct svc_req *rqstp;
129         SVCXPRT *transp;
130 {
131         int prog, proc;
132         char *outdata;
133         char xdrbuf[UDPMSGSIZE];
134         struct proglst *pl;
135
136         /*
137          * enforce "procnum 0 is echo" convention
138          */
139         if (rqstp->rq_proc == NULLPROC) {
140                 if (svc_sendreply(transp, xdr_void, (char *)NULL) == FALSE) {
141 #ifdef WIN32
142                         nt_rpc_report(stderr, "xxx\n");
143 #else
144                         (void) fprintf(stderr, "xxx\n");
145 #endif
146                         exit(1);
147                 }
148                 return;
149         }
150         prog = rqstp->rq_prog;
151         proc = rqstp->rq_proc;
152         for (pl = proglst; pl != NULL; pl = pl->p_nxt)
153                 if (pl->p_prognum == prog && pl->p_procnum == proc) {
154                         /* decode arguments into a CLEAN buffer */
155                         bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
156                         if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
157                                 svcerr_decode(transp);
158                                 return;
159                         }
160                         outdata = (*(pl->p_progname))(xdrbuf);
161                         if (outdata == NULL && pl->p_outproc != xdr_void)
162                                 /* there was an error */
163                                 return;
164                         if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
165 #ifdef WIN32
166                                 nt_rpc_report(
167                                     "trouble replying to prog\n"
168                                     /*, pl->p_prognum*/);
169 #else
170                                 (void) fprintf(stderr,
171                                     "trouble replying to prog %d\n",
172                                     pl->p_prognum);
173 #endif
174                                 exit(1);
175                         }
176                         /* free the decoded arguments */
177                         (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
178                         return;
179                 }
180 #ifdef WIN32
181         nt_rpc_report("never registered prog %d\n"/*, prog*/);
182 #else
183         (void) fprintf(stderr, "never registered prog %d\n", prog);
184 #endif
185         exit(1);
186 }
187