1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * - Redistributions of source code must retain the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer. |
---|
13 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
14 | * this list of conditions and the following disclaimer in the documentation |
---|
15 | * and/or other materials provided with the distribution. |
---|
16 | * - Neither the name of Sun Microsystems, 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 "AS IS" |
---|
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
31 | */ |
---|
32 | |
---|
33 | /* |
---|
34 | * Copyright (c) 1986-1991 by Sun Microsystems Inc. |
---|
35 | */ |
---|
36 | |
---|
37 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
38 | #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" |
---|
39 | static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro"; |
---|
40 | #endif |
---|
41 | #include <sys/cdefs.h> |
---|
42 | __FBSDID("$FreeBSD$"); |
---|
43 | |
---|
44 | /* |
---|
45 | * svc_generic.c, Server side for RPC. |
---|
46 | * |
---|
47 | */ |
---|
48 | |
---|
49 | #include "namespace.h" |
---|
50 | #include "reentrant.h" |
---|
51 | #include <sys/types.h> |
---|
52 | #include <sys/socket.h> |
---|
53 | #include <netinet/in.h> |
---|
54 | #include <rpc/rpc.h> |
---|
55 | #include <rpc/nettype.h> |
---|
56 | #include <stdio.h> |
---|
57 | #include <errno.h> |
---|
58 | #include <stdlib.h> |
---|
59 | #include <string.h> |
---|
60 | #include <unistd.h> |
---|
61 | #include <err.h> |
---|
62 | #include "un-namespace.h" |
---|
63 | |
---|
64 | #include "rpc_com.h" |
---|
65 | #include "mt_misc.h" |
---|
66 | |
---|
67 | extern int __svc_vc_setflag(SVCXPRT *, int); |
---|
68 | |
---|
69 | /* |
---|
70 | * The highest level interface for server creation. |
---|
71 | * It tries for all the nettokens in that particular class of token |
---|
72 | * and returns the number of handles it can create and/or find. |
---|
73 | * |
---|
74 | * It creates a link list of all the handles it could create. |
---|
75 | * If svc_create() is called multiple times, it uses the handle |
---|
76 | * created earlier instead of creating a new handle every time. |
---|
77 | */ |
---|
78 | int |
---|
79 | svc_create(dispatch, prognum, versnum, nettype) |
---|
80 | void (*dispatch)(struct svc_req *, SVCXPRT *); |
---|
81 | rpcprog_t prognum; /* Program number */ |
---|
82 | rpcvers_t versnum; /* Version number */ |
---|
83 | const char *nettype; /* Networktype token */ |
---|
84 | { |
---|
85 | struct xlist { |
---|
86 | SVCXPRT *xprt; /* Server handle */ |
---|
87 | struct xlist *next; /* Next item */ |
---|
88 | } *l; |
---|
89 | static struct xlist *xprtlist; /* A link list of all the handles */ |
---|
90 | int num = 0; |
---|
91 | SVCXPRT *xprt; |
---|
92 | struct netconfig *nconf; |
---|
93 | void *handle; |
---|
94 | |
---|
95 | /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */ |
---|
96 | |
---|
97 | if ((handle = __rpc_setconf(nettype)) == NULL) { |
---|
98 | warnx("svc_create: unknown protocol"); |
---|
99 | return (0); |
---|
100 | } |
---|
101 | while ((nconf = __rpc_getconf(handle)) != NULL) { |
---|
102 | mutex_lock(&xprtlist_lock); |
---|
103 | for (l = xprtlist; l; l = l->next) { |
---|
104 | if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) { |
---|
105 | /* Found an old one, use it */ |
---|
106 | (void) rpcb_unset(prognum, versnum, nconf); |
---|
107 | if (svc_reg(l->xprt, prognum, versnum, |
---|
108 | dispatch, nconf) == FALSE) |
---|
109 | warnx( |
---|
110 | "svc_create: could not register prog %u vers %u on %s", |
---|
111 | (unsigned)prognum, (unsigned)versnum, |
---|
112 | nconf->nc_netid); |
---|
113 | else |
---|
114 | num++; |
---|
115 | break; |
---|
116 | } |
---|
117 | } |
---|
118 | if (l == NULL) { |
---|
119 | /* It was not found. Now create a new one */ |
---|
120 | xprt = svc_tp_create(dispatch, prognum, versnum, nconf); |
---|
121 | if (xprt) { |
---|
122 | l = (struct xlist *)malloc(sizeof (*l)); |
---|
123 | if (l == NULL) { |
---|
124 | warnx("svc_create: no memory"); |
---|
125 | mutex_unlock(&xprtlist_lock); |
---|
126 | return (0); |
---|
127 | } |
---|
128 | l->xprt = xprt; |
---|
129 | l->next = xprtlist; |
---|
130 | xprtlist = l; |
---|
131 | num++; |
---|
132 | } |
---|
133 | } |
---|
134 | mutex_unlock(&xprtlist_lock); |
---|
135 | } |
---|
136 | __rpc_endconf(handle); |
---|
137 | /* |
---|
138 | * In case of num == 0; the error messages are generated by the |
---|
139 | * underlying layers; and hence not needed here. |
---|
140 | */ |
---|
141 | return (num); |
---|
142 | } |
---|
143 | |
---|
144 | /* |
---|
145 | * The high level interface to svc_tli_create(). |
---|
146 | * It tries to create a server for "nconf" and registers the service |
---|
147 | * with the rpcbind. It calls svc_tli_create(); |
---|
148 | */ |
---|
149 | SVCXPRT * |
---|
150 | svc_tp_create(dispatch, prognum, versnum, nconf) |
---|
151 | void (*dispatch)(struct svc_req *, SVCXPRT *); |
---|
152 | rpcprog_t prognum; /* Program number */ |
---|
153 | rpcvers_t versnum; /* Version number */ |
---|
154 | const struct netconfig *nconf; /* Netconfig structure for the network */ |
---|
155 | { |
---|
156 | SVCXPRT *xprt; |
---|
157 | |
---|
158 | if (nconf == NULL) { |
---|
159 | warnx( |
---|
160 | "svc_tp_create: invalid netconfig structure for prog %u vers %u", |
---|
161 | (unsigned)prognum, (unsigned)versnum); |
---|
162 | return (NULL); |
---|
163 | } |
---|
164 | xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); |
---|
165 | if (xprt == NULL) { |
---|
166 | return (NULL); |
---|
167 | } |
---|
168 | /*LINTED const castaway*/ |
---|
169 | (void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf); |
---|
170 | if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) { |
---|
171 | warnx( |
---|
172 | "svc_tp_create: Could not register prog %u vers %u on %s", |
---|
173 | (unsigned)prognum, (unsigned)versnum, |
---|
174 | nconf->nc_netid); |
---|
175 | SVC_DESTROY(xprt); |
---|
176 | return (NULL); |
---|
177 | } |
---|
178 | return (xprt); |
---|
179 | } |
---|
180 | |
---|
181 | /* |
---|
182 | * If fd is RPC_ANYFD, then it opens a fd for the given transport |
---|
183 | * provider (nconf cannot be NULL then). If the t_state is T_UNBND and |
---|
184 | * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For |
---|
185 | * NULL bindadr and Connection oriented transports, the value of qlen |
---|
186 | * is set to 8. |
---|
187 | * |
---|
188 | * If sendsz or recvsz are zero, their default values are chosen. |
---|
189 | */ |
---|
190 | SVCXPRT * |
---|
191 | svc_tli_create(fd, nconf, bindaddr, sendsz, recvsz) |
---|
192 | int fd; /* Connection end point */ |
---|
193 | const struct netconfig *nconf; /* Netconfig struct for nettoken */ |
---|
194 | const struct t_bind *bindaddr; /* Local bind address */ |
---|
195 | u_int sendsz; /* Max sendsize */ |
---|
196 | u_int recvsz; /* Max recvsize */ |
---|
197 | { |
---|
198 | SVCXPRT *xprt = NULL; /* service handle */ |
---|
199 | bool_t madefd = FALSE; /* whether fd opened here */ |
---|
200 | struct __rpc_sockinfo si; |
---|
201 | struct sockaddr_storage ss; |
---|
202 | socklen_t slen; |
---|
203 | |
---|
204 | if (fd == RPC_ANYFD) { |
---|
205 | if (nconf == NULL) { |
---|
206 | warnx("svc_tli_create: invalid netconfig"); |
---|
207 | return (NULL); |
---|
208 | } |
---|
209 | fd = __rpc_nconf2fd(nconf); |
---|
210 | if (fd == -1) { |
---|
211 | warnx( |
---|
212 | "svc_tli_create: could not open connection for %s", |
---|
213 | nconf->nc_netid); |
---|
214 | return (NULL); |
---|
215 | } |
---|
216 | __rpc_nconf2sockinfo(nconf, &si); |
---|
217 | madefd = TRUE; |
---|
218 | } else { |
---|
219 | /* |
---|
220 | * It is an open descriptor. Get the transport info. |
---|
221 | */ |
---|
222 | if (!__rpc_fd2sockinfo(fd, &si)) { |
---|
223 | warnx( |
---|
224 | "svc_tli_create: could not get transport information"); |
---|
225 | return (NULL); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /* |
---|
230 | * If the fd is unbound, try to bind it. |
---|
231 | */ |
---|
232 | if (madefd || !__rpc_sockisbound(fd)) { |
---|
233 | if (bindaddr == NULL) { |
---|
234 | if (bindresvport(fd, NULL) < 0) { |
---|
235 | memset(&ss, 0, sizeof ss); |
---|
236 | ss.ss_family = si.si_af; |
---|
237 | ss.ss_len = si.si_alen; |
---|
238 | if (_bind(fd, (struct sockaddr *)(void *)&ss, |
---|
239 | (socklen_t)si.si_alen) < 0) { |
---|
240 | warnx( |
---|
241 | "svc_tli_create: could not bind to anonymous port"); |
---|
242 | goto freedata; |
---|
243 | } |
---|
244 | } |
---|
245 | _listen(fd, SOMAXCONN); |
---|
246 | } else { |
---|
247 | if (_bind(fd, |
---|
248 | (struct sockaddr *)bindaddr->addr.buf, |
---|
249 | (socklen_t)si.si_alen) < 0) { |
---|
250 | warnx( |
---|
251 | "svc_tli_create: could not bind to requested address"); |
---|
252 | goto freedata; |
---|
253 | } |
---|
254 | _listen(fd, (int)bindaddr->qlen); |
---|
255 | } |
---|
256 | |
---|
257 | } |
---|
258 | /* |
---|
259 | * call transport specific function. |
---|
260 | */ |
---|
261 | switch (si.si_socktype) { |
---|
262 | case SOCK_STREAM: |
---|
263 | slen = sizeof ss; |
---|
264 | if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) |
---|
265 | == 0) { |
---|
266 | /* accepted socket */ |
---|
267 | xprt = svc_fd_create(fd, sendsz, recvsz); |
---|
268 | } else |
---|
269 | xprt = svc_vc_create(fd, sendsz, recvsz); |
---|
270 | if (!nconf || !xprt) |
---|
271 | break; |
---|
272 | #if 0 |
---|
273 | /* XXX fvdl */ |
---|
274 | if (strcmp(nconf->nc_protofmly, "inet") == 0 || |
---|
275 | strcmp(nconf->nc_protofmly, "inet6") == 0) |
---|
276 | (void) __svc_vc_setflag(xprt, TRUE); |
---|
277 | #endif |
---|
278 | break; |
---|
279 | case SOCK_DGRAM: |
---|
280 | xprt = svc_dg_create(fd, sendsz, recvsz); |
---|
281 | break; |
---|
282 | default: |
---|
283 | warnx("svc_tli_create: bad service type"); |
---|
284 | goto freedata; |
---|
285 | } |
---|
286 | |
---|
287 | if (xprt == NULL) |
---|
288 | /* |
---|
289 | * The error messages here are spitted out by the lower layers: |
---|
290 | * svc_vc_create(), svc_fd_create() and svc_dg_create(). |
---|
291 | */ |
---|
292 | goto freedata; |
---|
293 | |
---|
294 | /* Fill in type of service */ |
---|
295 | xprt->xp_type = __rpc_socktype2seman(si.si_socktype); |
---|
296 | |
---|
297 | if (nconf) { |
---|
298 | xprt->xp_netid = strdup(nconf->nc_netid); |
---|
299 | xprt->xp_tp = strdup(nconf->nc_device); |
---|
300 | } |
---|
301 | return (xprt); |
---|
302 | |
---|
303 | freedata: |
---|
304 | if (madefd) |
---|
305 | (void)_close(fd); |
---|
306 | if (xprt) { |
---|
307 | if (!madefd) /* so that svc_destroy doesnt close fd */ |
---|
308 | xprt->xp_fd = RPC_ANYFD; |
---|
309 | SVC_DESTROY(xprt); |
---|
310 | } |
---|
311 | return (NULL); |
---|
312 | } |
---|