BSD 4_3 release
[unix-history] / usr / contrib / sunrpc / svc_simple.c
CommitLineData
95f51977
C
1/*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 *
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
16 *
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
20 *
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
24 *
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
28 */
29#ifndef lint
30static char sccsid[] = "@(#)svc_simple.c 1.3 85/03/14 Copyr 1984 Sun Micro";
31#endif
32
33/*
34 * svc_simple.c
35 * Simplified front end to rpc.
36 *
37 * Copyright (C) 1984, Sun Microsystems, Inc.
38 */
39
40#include <stdio.h>
41#include <rpc/rpc.h>
42#include <sys/socket.h>
43#include <sys/time.h>
44#include <netdb.h>
45
46static struct proglst {
47 char *(*p_progname)();
48 int p_prognum;
49 int p_procnum;
50 xdrproc_t p_inproc, p_outproc;
51 struct proglst *p_nxt;
52} *proglst;
53int universal();
54
55registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
56 char *(*progname)();
57 xdrproc_t inproc, outproc;
58{
59 static SVCXPRT *transp;
60 static madetransp = 0;
61 struct proglst *pl;
62
63 if (procnum == NULLPROC) {
64 fprintf(stderr,
65 "can't reassign procedure number %d\n", NULLPROC);
66 return (-1);
67 }
68 if (!madetransp) {
69 madetransp = 1;
70 transp = svcudp_create(RPC_ANYSOCK);
71 if (transp == NULL) {
72 fprintf(stderr, "couldn't create an rpc server\n");
73 return (-1);
74 }
75 }
76 pmap_unset(prognum, versnum);
77 if (!svc_register(transp, prognum, versnum, universal, IPPROTO_UDP)) {
78 fprintf(stderr, "couldn't register prog %d vers %d\n",
79 prognum, versnum);
80 return (-1);
81 }
82 pl = (struct proglst *)malloc(sizeof(struct proglst));
83 if (pl == NULL) {
84 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
98universal(rqstp, transp)
99 struct svc_req *rqstp;
100 SVCXPRT *transp;
101{
102 int prog, proc, i;
103 char *outdata;
104 char xdrbuf[UDPMSGSIZE];
105 struct proglst *pl;
106
107 /*
108 * enforce "procnum 0 is echo" convention
109 */
110 if (rqstp->rq_proc == NULLPROC) {
111 if (svc_sendreply(transp, xdr_void, 0) == FALSE) {
112 fprintf(stderr, "xxx\n");
113 exit(1);
114 }
115 return;
116 }
117 prog = rqstp->rq_prog;
118 proc = rqstp->rq_proc;
119 for (pl = proglst; pl != NULL; pl = pl->p_nxt)
120 if (pl->p_prognum == prog && pl->p_procnum == proc) {
121 /* decode arguments into a CLEAN buffer */
122 bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
123 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
124 svcerr_decode(transp);
125 return;
126 }
127 outdata = (*(pl->p_progname))(xdrbuf);
128 if (outdata == NULL && pl->p_outproc != xdr_void)
129 /* there was an error */
130 return;
131 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
132 fprintf(stderr,
133 "trouble replying to prog %d\n",
134 pl->p_prognum);
135 exit(1);
136 /* free the decoded arguments */
137 (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
138 }
139 return;
140 }
141 fprintf(stderr, "never registered prog %d\n", prog);
142 exit(1);
143}
144