1 /* $NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident "@(#)svc_generic.c 1.19 94/04/24 SMI"
37 static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * svc_generic.c, Server side for RPC.
47 #include "opt_inet6.h"
49 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/protosw.h>
55 #include <sys/queue.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/systm.h>
60 #include <sys/ucred.h>
65 #include <rpc/rpcb_clnt.h>
66 #include <rpc/nettype.h>
68 #include <rpc/rpc_com.h>
70 extern int __svc_vc_setflag(SVCXPRT *, int);
73 * The highest level interface for server creation.
74 * It tries for all the nettokens in that particular class of token
75 * and returns the number of handles it can create and/or find.
77 * It creates a link list of all the handles it could create.
78 * If svc_create() is called multiple times, it uses the handle
79 * created earlier instead of creating a new handle every time.
84 void (*dispatch)(struct svc_req *, SVCXPRT *),
85 rpcprog_t prognum, /* Program number */
86 rpcvers_t versnum, /* Version number */
87 const char *nettype) /* Networktype token */
92 struct netconfig *nconf;
95 if ((handle = __rpc_setconf(nettype)) == NULL) {
96 printf("svc_create: unknown protocol");
99 while ((nconf = __rpc_getconf(handle)) != NULL) {
100 for (g = 0; g < SVC_MAXGROUPS; g++) {
101 grp = &pool->sp_groups[g];
102 mtx_lock(&grp->sg_lock);
103 TAILQ_FOREACH(xprt, &grp->sg_xlist, xp_link) {
104 if (strcmp(xprt->xp_netid, nconf->nc_netid))
106 /* Found an old one, use it */
107 mtx_unlock(&grp->sg_lock);
108 (void) rpcb_unset(prognum, versnum, nconf);
109 if (svc_reg(xprt, prognum, versnum,
110 dispatch, nconf) == FALSE) {
112 "svc_create: could not register prog %u vers %u on %s\n",
113 (unsigned)prognum, (unsigned)versnum,
115 mtx_lock(&grp->sg_lock);
118 mtx_lock(&grp->sg_lock);
122 mtx_unlock(&grp->sg_lock);
125 /* It was not found. Now create a new one */
126 xprt = svc_tp_create(pool, dispatch, prognum, versnum,
134 __rpc_endconf(handle);
136 * In case of num == 0; the error messages are generated by the
137 * underlying layers; and hence not needed here.
143 * The high level interface to svc_tli_create().
144 * It tries to create a server for "nconf" and registers the service
145 * with the rpcbind. It calls svc_tli_create();
150 void (*dispatch)(struct svc_req *, SVCXPRT *),
151 rpcprog_t prognum, /* Program number */
152 rpcvers_t versnum, /* Version number */
153 const char *uaddr, /* Address (or null for default) */
154 const struct netconfig *nconf) /* Netconfig structure for the network */
156 struct netconfig nconfcopy;
157 struct netbuf *taddr;
163 "svc_tp_create: invalid netconfig structure for prog %u vers %u\n",
164 (unsigned)prognum, (unsigned)versnum);
168 taddr = uaddr2taddr(nconf, uaddr);
172 xprt = svc_tli_create(pool, NULL, nconf, &bind, 0, 0);
173 free(bind.addr.buf, M_RPC);
175 xprt = svc_tli_create(pool, NULL, nconf, NULL, 0, 0);
180 /*LINTED const castaway*/
182 (void) rpcb_unset(prognum, versnum, &nconfcopy);
183 if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
185 "svc_tp_create: Could not register prog %u vers %u on %s\n",
186 (unsigned)prognum, (unsigned)versnum,
188 xprt_unregister(xprt);
196 * If so is NULL, then it opens a socket for the given transport
197 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
198 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
199 * NULL bindadr and Connection oriented transports, the value of qlen
202 * If sendsz or recvsz are zero, their default values are chosen.
207 struct socket *so, /* Connection end point */
208 const struct netconfig *nconf, /* Netconfig struct for nettoken */
209 const struct t_bind *bindaddr, /* Local bind address */
210 size_t sendsz, /* Max sendsize */
211 size_t recvsz) /* Max recvsize */
213 SVCXPRT *xprt = NULL; /* service handle */
214 bool_t madeso = FALSE; /* whether so opened here */
215 struct __rpc_sockinfo si;
216 struct sockaddr_storage ss;
220 printf("svc_tli_create: invalid netconfig\n");
223 so = __rpc_nconf2socket(nconf);
226 "svc_tli_create: could not open connection for %s\n",
230 __rpc_nconf2sockinfo(nconf, &si);
234 * It is an open socket. Get the transport info.
236 if (!__rpc_socket2sockinfo(so, &si)) {
238 "svc_tli_create: could not get transport information\n");
244 * If the socket is unbound, try to bind it.
246 if (madeso || !__rpc_sockisbound(so)) {
247 if (bindaddr == NULL) {
248 if (bindresvport(so, NULL)) {
249 memset(&ss, 0, sizeof ss);
250 ss.ss_family = si.si_af;
251 ss.ss_len = si.si_alen;
252 if (sobind(so, (struct sockaddr *)&ss,
255 "svc_tli_create: could not bind to anonymous port\n");
259 solisten(so, -1, curthread);
262 (struct sockaddr *)bindaddr->addr.buf)) {
264 "svc_tli_create: could not bind to requested address\n");
267 solisten(so, (int)bindaddr->qlen, curthread);
272 * call transport specific function.
274 switch (si.si_socktype) {
278 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
280 /* accepted socket */
281 xprt = svc_fd_create(fd, sendsz, recvsz);
284 xprt = svc_vc_create(pool, so, sendsz, recvsz);
289 if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
290 strcmp(nconf->nc_protofmly, "inet6") == 0)
291 (void) __svc_vc_setflag(xprt, TRUE);
295 xprt = svc_dg_create(pool, so, sendsz, recvsz);
298 printf("svc_tli_create: bad service type");
304 * The error messages here are spitted out by the lower layers:
305 * svc_vc_create(), svc_fd_create() and svc_dg_create().
309 /* Fill in type of service */
310 xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
313 xprt->xp_netid = strdup(nconf->nc_netid, M_RPC);
321 if (!madeso) /* so that svc_destroy doesnt close fd */
322 xprt->xp_socket = NULL;
323 xprt_unregister(xprt);