Libinfo-330.9.tar.gz
[apple/libinfo.git] / rpc.subproj / svc_simple.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
31 *
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 *
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
39 *
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
43 *
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)svc_simple.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid = "$Id: svc_simple.c,v 1.3 2002/02/19 20:36:25 epeyton Exp $";
57 #endif
58
59 /*
60 * svc_simple.c
61 * Simplified front end to rpc.
62 *
63 * Copyright (C) 1984, Sun Microsystems, Inc.
64 */
65
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <rpc/rpc.h>
70 #include <rpc/pmap_clnt.h>
71 #include <sys/socket.h>
72 #include <netdb.h>
73
74 static struct proglst {
75 char *(*p_progname)();
76 int p_prognum;
77 int p_procnum;
78 xdrproc_t p_inproc, p_outproc;
79 struct proglst *p_nxt;
80 } *proglst;
81 static void universal();
82 static SVCXPRT *transp;
83 struct proglst *pl;
84
85 int
86 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
87 int prognum;
88 int versnum;
89 int procnum;
90 char *(*progname)();
91 xdrproc_t inproc, outproc;
92 {
93
94 if (procnum == NULLPROC) {
95 #ifdef __LP64__
96 (void) fprintf(stderr, "can't reassign procedure number %d\n", NULLPROC);
97 #else
98 (void) fprintf(stderr, "can't reassign procedure number %ld\n", NULLPROC);
99 #endif
100 return (-1);
101 }
102 if (transp == 0) {
103 transp = svcudp_create(RPC_ANYSOCK);
104 if (transp == NULL) {
105 (void) fprintf(stderr, "couldn't create an rpc server\n");
106 return (-1);
107 }
108 }
109 #ifdef __LP64__
110 (void) pmap_unset((uint32_t)prognum, (uint32_t)versnum);
111 if (!svc_register(transp, (uint32_t)prognum, (uint32_t)versnum,
112 universal, IPPROTO_UDP)) {
113 (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
114 prognum, versnum);
115 return (-1);
116 }
117 #else
118 (void) pmap_unset((u_long)prognum, (u_long)versnum);
119 if (!svc_register(transp, (u_long)prognum, (u_long)versnum,
120 universal, IPPROTO_UDP)) {
121 (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
122 prognum, versnum);
123 return (-1);
124 }
125 #endif
126 pl = (struct proglst *)malloc(sizeof(struct proglst));
127 if (pl == NULL) {
128 (void) fprintf(stderr, "registerrpc: out of memory\n");
129 return (-1);
130 }
131 pl->p_progname = progname;
132 pl->p_prognum = prognum;
133 pl->p_procnum = procnum;
134 pl->p_inproc = inproc;
135 pl->p_outproc = outproc;
136 pl->p_nxt = proglst;
137 proglst = pl;
138 return (0);
139 }
140
141 static void
142 universal(rqstp, transp)
143 struct svc_req *rqstp;
144 SVCXPRT *transp;
145 {
146 int prog, proc;
147 char *outdata;
148 char xdrbuf[UDPMSGSIZE];
149 struct proglst *pl;
150
151 /*
152 * enforce "procnum 0 is echo" convention
153 */
154 if (rqstp->rq_proc == NULLPROC) {
155 if (svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL) == FALSE) {
156 (void) fprintf(stderr, "xxx\n");
157 exit(1);
158 }
159 return;
160 }
161 prog = rqstp->rq_prog;
162 proc = rqstp->rq_proc;
163 for (pl = proglst; pl != NULL; pl = pl->p_nxt)
164 if (pl->p_prognum == prog && pl->p_procnum == proc) {
165 /* decode arguments into a CLEAN buffer */
166 bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
167 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
168 svcerr_decode(transp);
169 return;
170 }
171 outdata = (*(pl->p_progname))(xdrbuf);
172 if (outdata == NULL && pl->p_outproc != (xdrproc_t)xdr_void)
173 /* there was an error */
174 return;
175 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
176 (void) fprintf(stderr,
177 "trouble replying to prog %d\n",
178 pl->p_prognum);
179 exit(1);
180 }
181 /* free the decoded arguments */
182 (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
183 return;
184 }
185 (void) fprintf(stderr, "never registered prog %d\n", prog);
186 exit(1);
187 }
188