1 /* $NetBSD: svc_generic.c,v 1.15 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, 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.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
38 /* #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" */
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid
[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
45 __RCSID("$NetBSD: svc_generic.c,v 1.15 2013/03/11 20:19:29 tron Exp $");
50 * svc_generic.c, Server side for RPC.
54 #include "namespace.h"
55 #include "reentrant.h"
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include <rpc/nettype.h>
68 #include "svc_fdset.h"
69 #include "rpc_internal.h"
72 __weak_alias(svc_create
,_svc_create
)
73 __weak_alias(svc_tp_create
,_svc_tp_create
)
74 __weak_alias(svc_tli_create
,_svc_tli_create
)
77 extern int __svc_vc_setflag(SVCXPRT
*, int);
80 * The highest level interface for server creation.
81 * It tries for all the nettokens in that particular class of token
82 * and returns the number of handles it can create and/or find.
84 * It creates a link list of all the handles it could create.
85 * If svc_create() is called multiple times, it uses the handle
86 * created earlier instead of creating a new handle every time.
90 void (*dispatch
)(struct svc_req
*, SVCXPRT
*),
91 rpcprog_t prognum
, /* Program number */
92 rpcvers_t versnum
, /* Version number */
93 const char *nettype
) /* Networktype token */
96 SVCXPRT
*xprt
; /* Server handle */
97 struct xlist
*next
; /* Next item */
99 static struct xlist
*xprtlist
; /* A link list of all the handles */
102 struct netconfig
*nconf
;
105 extern mutex_t xprtlist_lock
;
108 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
110 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
111 warnx("%s: unknown protocol %s", __func__
, nettype
);
114 while ((nconf
= __rpc_getconf(handle
)) != NULL
) {
115 mutex_lock(&xprtlist_lock
);
116 for (l
= xprtlist
; l
; l
= l
->next
) {
117 if (strcmp(l
->xprt
->xp_netid
, nconf
->nc_netid
) == 0) {
118 /* Found an old one, use it */
119 (void) rpcb_unset(prognum
, versnum
, nconf
);
120 if (svc_reg(l
->xprt
, prognum
, versnum
,
121 dispatch
, nconf
) == FALSE
)
122 warnx("%s: could not register prog %u "
123 "vers %u on %s", __func__
,
125 (unsigned)versnum
, nconf
->nc_netid
);
132 /* It was not found. Now create a new one */
133 xprt
= svc_tp_create(dispatch
, prognum
, versnum
, nconf
);
135 l
= malloc(sizeof(*l
));
137 warn("%s: out of memory", __func__
);
138 mutex_unlock(&xprtlist_lock
);
147 mutex_unlock(&xprtlist_lock
);
149 __rpc_endconf(handle
);
151 * In case of num == 0; the error messages are generated by the
152 * underlying layers; and hence not needed here.
158 * The high level interface to svc_tli_create().
159 * It tries to create a server for "nconf" and registers the service
160 * with the rpcbind. It calls svc_tli_create();
164 void (*dispatch
)(struct svc_req
*, SVCXPRT
*),
165 rpcprog_t prognum
, /* Program number */
166 rpcvers_t versnum
, /* Version number */
167 const struct netconfig
*nconf
) /* Netconfig structure for the network */
172 warnx("%s: invalid netconfig structure for prog %u vers %u",
173 __func__
, (unsigned)prognum
, (unsigned)versnum
);
176 xprt
= svc_tli_create(RPC_ANYFD
, nconf
, NULL
, 0, 0);
180 (void) rpcb_unset(prognum
, versnum
, __UNCONST(nconf
));
181 if (svc_reg(xprt
, prognum
, versnum
, dispatch
, nconf
) == FALSE
) {
182 warnx("%s: Could not register prog %u vers %u on %s",
183 __func__
, (unsigned)prognum
, (unsigned)versnum
,
192 * If fd is RPC_ANYFD, then it opens a fd for the given transport
193 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
194 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
195 * NULL bindadr and Connection oriented transports, the value of qlen
198 * If sendsz or recvsz are zero, their default values are chosen.
202 int fd
, /* Connection end point */
203 const struct netconfig
*nconf
, /* Netconfig struct for nettoken */
204 const struct t_bind
*bindaddr
, /* Local bind address */
205 u_int sendsz
, /* Max sendsize */
206 u_int recvsz
) /* Max recvsize */
208 SVCXPRT
*xprt
= NULL
; /* service handle */
209 bool_t madefd
= FALSE
; /* whether fd opened here */
210 struct __rpc_sockinfo si
;
211 struct sockaddr_storage ss
;
214 if (fd
== RPC_ANYFD
) {
216 warnx("%s: invalid netconfig", __func__
);
219 fd
= __rpc_nconf2fd(nconf
);
221 warnx("%s: could not open connection for %s", __func__
,
225 __rpc_nconf2sockinfo(nconf
, &si
);
229 * It is an open descriptor. Get the transport info.
231 if (!__rpc_fd2sockinfo(fd
, &si
)) {
232 warnx("%s: could not get transport information",
239 * If the fd is unbound, try to bind it.
241 if (madefd
|| !__rpc_sockisbound(fd
)) {
242 if (bindaddr
== NULL
) {
243 if (bindresvport(fd
, NULL
) < 0) {
244 memset(&ss
, 0, sizeof ss
);
245 ss
.ss_family
= si
.si_af
;
246 ss
.ss_len
= si
.si_alen
;
247 if (bind(fd
, (struct sockaddr
*)(void *)&ss
,
248 (socklen_t
)si
.si_alen
) < 0) {
249 warn( "%s: could not bind to anonymous "
254 listen(fd
, SOMAXCONN
);
257 (struct sockaddr
*)bindaddr
->addr
.buf
,
258 (socklen_t
)si
.si_alen
) < 0) {
259 warnx("%s: could not bind to requested address",
263 listen(fd
, (int)bindaddr
->qlen
);
268 * call transport specific function.
270 switch (si
.si_socktype
) {
273 if (getpeername(fd
, (struct sockaddr
*)(void *)&ss
, &slen
)
275 /* accepted socket */
276 xprt
= svc_fd_create(fd
, sendsz
, recvsz
);
278 xprt
= svc_vc_create(fd
, sendsz
, recvsz
);
283 if (strcmp(nconf
->nc_protofmly
, "inet") == 0 ||
284 strcmp(nconf
->nc_protofmly
, "inet6") == 0)
285 (void) __svc_vc_setflag(xprt
, TRUE
);
289 xprt
= svc_dg_create(fd
, sendsz
, recvsz
);
292 warnx("%s: bad service type %u", __func__
, si
.si_socktype
);
298 * The error messages here are spitted out by the lower layers:
299 * svc_vc_create(), svc_fd_create() and svc_dg_create().
303 /* Fill in type of service */
304 xprt
->xp_type
= __rpc_socktype2seman(si
.si_socktype
);
307 xprt
->xp_netid
= strdup(nconf
->nc_netid
);
308 xprt
->xp_tp
= strdup(nconf
->nc_device
);
309 if (xprt
->xp_netid
== NULL
|| xprt
->xp_tp
== NULL
) {