174462Salfred/*	$NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $	*/
274462Salfred
3261057Smav/*-
4261057Smav * Copyright (c) 2009, Sun Microsystems, Inc.
5261057Smav * All rights reserved.
6261057Smav *
7261057Smav * Redistribution and use in source and binary forms, with or without
8261057Smav * modification, are permitted provided that the following conditions are met:
9261057Smav * - Redistributions of source code must retain the above copyright notice,
10261057Smav *   this list of conditions and the following disclaimer.
11261057Smav * - Redistributions in binary form must reproduce the above copyright notice,
12261057Smav *   this list of conditions and the following disclaimer in the documentation
13261057Smav *   and/or other materials provided with the distribution.
14261057Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its
15261057Smav *   contributors may be used to endorse or promote products derived
16261057Smav *   from this software without specific prior written permission.
1774462Salfred *
18261057Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19261057Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20261057Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21261057Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22261057Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23261057Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24261057Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25261057Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26261057Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27261057Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28261057Smav * POSSIBILITY OF SUCH DAMAGE.
2974462Salfred */
3074462Salfred
3174462Salfred/*
3274462Salfred * Copyright (c) 1986-1991 by Sun Microsystems Inc.
3374462Salfred */
3474462Salfred
35136581Sobrien#if defined(LIBC_SCCS) && !defined(lint)
36136581Sobrien#ident	"@(#)svc_generic.c	1.19	94/04/24 SMI"
3774462Salfredstatic char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
3874462Salfred#endif
3992990Sobrien#include <sys/cdefs.h>
4092990Sobrien__FBSDID("$FreeBSD$");
4174462Salfred
4274462Salfred/*
4374462Salfred * svc_generic.c, Server side for RPC.
4474462Salfred *
4574462Salfred */
4674462Salfred
4775094Siedowse#include "namespace.h"
4874462Salfred#include "reentrant.h"
4974462Salfred#include <sys/types.h>
5074462Salfred#include <sys/socket.h>
5174462Salfred#include <netinet/in.h>
5274462Salfred#include <rpc/rpc.h>
5374462Salfred#include <rpc/nettype.h>
5474462Salfred#include <stdio.h>
5574462Salfred#include <errno.h>
5674462Salfred#include <stdlib.h>
5774462Salfred#include <string.h>
5874462Salfred#include <unistd.h>
5974462Salfred#include <err.h>
6074462Salfred#include "un-namespace.h"
6174462Salfred
6274462Salfred#include "rpc_com.h"
63156090Sdeischen#include "mt_misc.h"
6474462Salfred
6592905Sobrienextern int __svc_vc_setflag(SVCXPRT *, int);
6674462Salfred
6774462Salfred/*
6874462Salfred * The highest level interface for server creation.
6974462Salfred * It tries for all the nettokens in that particular class of token
7074462Salfred * and returns the number of handles it can create and/or find.
7174462Salfred *
7274462Salfred * It creates a link list of all the handles it could create.
7374462Salfred * If svc_create() is called multiple times, it uses the handle
7474462Salfred * created earlier instead of creating a new handle every time.
7574462Salfred */
7674462Salfredint
7774462Salfredsvc_create(dispatch, prognum, versnum, nettype)
7892905Sobrien	void (*dispatch)(struct svc_req *, SVCXPRT *);
7974462Salfred	rpcprog_t prognum;		/* Program number */
8074462Salfred	rpcvers_t versnum;		/* Version number */
8174462Salfred	const char *nettype;		/* Networktype token */
8274462Salfred{
8374462Salfred	struct xlist {
8474462Salfred		SVCXPRT *xprt;		/* Server handle */
8574462Salfred		struct xlist *next;	/* Next item */
8674462Salfred	} *l;
8774462Salfred	static struct xlist *xprtlist;	/* A link list of all the handles */
8874462Salfred	int num = 0;
8974462Salfred	SVCXPRT *xprt;
9074462Salfred	struct netconfig *nconf;
9174462Salfred	void *handle;
9274462Salfred
9374462Salfred/* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
9474462Salfred
9574462Salfred	if ((handle = __rpc_setconf(nettype)) == NULL) {
9674462Salfred		warnx("svc_create: unknown protocol");
9774462Salfred		return (0);
9874462Salfred	}
9974462Salfred	while ((nconf = __rpc_getconf(handle)) != NULL) {
10074462Salfred		mutex_lock(&xprtlist_lock);
10174462Salfred		for (l = xprtlist; l; l = l->next) {
10274462Salfred			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
10374462Salfred				/* Found an old one, use it */
10474462Salfred				(void) rpcb_unset(prognum, versnum, nconf);
10574462Salfred				if (svc_reg(l->xprt, prognum, versnum,
10674462Salfred					dispatch, nconf) == FALSE)
10774462Salfred					warnx(
10874462Salfred		"svc_create: could not register prog %u vers %u on %s",
10974462Salfred					(unsigned)prognum, (unsigned)versnum,
11074462Salfred					 nconf->nc_netid);
11174462Salfred				else
11274462Salfred					num++;
11374462Salfred				break;
11474462Salfred			}
11574462Salfred		}
11674462Salfred		if (l == NULL) {
11774462Salfred			/* It was not found. Now create a new one */
11874462Salfred			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
11974462Salfred			if (xprt) {
12074462Salfred				l = (struct xlist *)malloc(sizeof (*l));
12174462Salfred				if (l == NULL) {
12274462Salfred					warnx("svc_create: no memory");
12374462Salfred					mutex_unlock(&xprtlist_lock);
12474462Salfred					return (0);
12574462Salfred				}
12674462Salfred				l->xprt = xprt;
12774462Salfred				l->next = xprtlist;
12874462Salfred				xprtlist = l;
12974462Salfred				num++;
13074462Salfred			}
13174462Salfred		}
13274462Salfred		mutex_unlock(&xprtlist_lock);
13374462Salfred	}
13474462Salfred	__rpc_endconf(handle);
13574462Salfred	/*
13674462Salfred	 * In case of num == 0; the error messages are generated by the
13774462Salfred	 * underlying layers; and hence not needed here.
13874462Salfred	 */
13974462Salfred	return (num);
14074462Salfred}
14174462Salfred
14274462Salfred/*
14374462Salfred * The high level interface to svc_tli_create().
14474462Salfred * It tries to create a server for "nconf" and registers the service
14574462Salfred * with the rpcbind. It calls svc_tli_create();
14674462Salfred */
14774462SalfredSVCXPRT *
14874462Salfredsvc_tp_create(dispatch, prognum, versnum, nconf)
14992905Sobrien	void (*dispatch)(struct svc_req *, SVCXPRT *);
15074462Salfred	rpcprog_t prognum;		/* Program number */
15174462Salfred	rpcvers_t versnum;		/* Version number */
15274462Salfred	const struct netconfig *nconf; /* Netconfig structure for the network */
15374462Salfred{
15474462Salfred	SVCXPRT *xprt;
15574462Salfred
15674462Salfred	if (nconf == NULL) {
15774462Salfred		warnx(
15874462Salfred	"svc_tp_create: invalid netconfig structure for prog %u vers %u",
15974462Salfred				(unsigned)prognum, (unsigned)versnum);
16074462Salfred		return (NULL);
16174462Salfred	}
16274462Salfred	xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
16374462Salfred	if (xprt == NULL) {
16474462Salfred		return (NULL);
16574462Salfred	}
16674462Salfred	/*LINTED const castaway*/
16774462Salfred	(void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
16874462Salfred	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
16974462Salfred		warnx(
17074462Salfred		"svc_tp_create: Could not register prog %u vers %u on %s",
17174462Salfred				(unsigned)prognum, (unsigned)versnum,
17274462Salfred				nconf->nc_netid);
17374462Salfred		SVC_DESTROY(xprt);
17474462Salfred		return (NULL);
17574462Salfred	}
17674462Salfred	return (xprt);
17774462Salfred}
17874462Salfred
17974462Salfred/*
18074462Salfred * If fd is RPC_ANYFD, then it opens a fd for the given transport
18174462Salfred * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
18274462Salfred * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
18374462Salfred * NULL bindadr and Connection oriented transports, the value of qlen
18474462Salfred * is set to 8.
18574462Salfred *
18674462Salfred * If sendsz or recvsz are zero, their default values are chosen.
18774462Salfred */
18874462SalfredSVCXPRT *
18974462Salfredsvc_tli_create(fd, nconf, bindaddr, sendsz, recvsz)
19074462Salfred	int fd;				/* Connection end point */
19174462Salfred	const struct netconfig *nconf;	/* Netconfig struct for nettoken */
19274462Salfred	const struct t_bind *bindaddr;	/* Local bind address */
19374462Salfred	u_int sendsz;			/* Max sendsize */
19474462Salfred	u_int recvsz;			/* Max recvsize */
19574462Salfred{
19674462Salfred	SVCXPRT *xprt = NULL;		/* service handle */
19774462Salfred	bool_t madefd = FALSE;		/* whether fd opened here  */
19874462Salfred	struct __rpc_sockinfo si;
19974462Salfred	struct sockaddr_storage ss;
20074462Salfred	socklen_t slen;
20174462Salfred
20274462Salfred	if (fd == RPC_ANYFD) {
20374462Salfred		if (nconf == NULL) {
20474462Salfred			warnx("svc_tli_create: invalid netconfig");
20574462Salfred			return (NULL);
20674462Salfred		}
20774462Salfred		fd = __rpc_nconf2fd(nconf);
20874462Salfred		if (fd == -1) {
20974462Salfred			warnx(
21074462Salfred			    "svc_tli_create: could not open connection for %s",
21174462Salfred					nconf->nc_netid);
21274462Salfred			return (NULL);
21374462Salfred		}
21474462Salfred		__rpc_nconf2sockinfo(nconf, &si);
21574462Salfred		madefd = TRUE;
21674462Salfred	} else {
21774462Salfred		/*
21874462Salfred		 * It is an open descriptor. Get the transport info.
21974462Salfred		 */
22074462Salfred		if (!__rpc_fd2sockinfo(fd, &si)) {
22174462Salfred			warnx(
22274462Salfred		"svc_tli_create: could not get transport information");
22374462Salfred			return (NULL);
22474462Salfred		}
22574462Salfred	}
22674462Salfred
22774462Salfred	/*
22874462Salfred	 * If the fd is unbound, try to bind it.
22974462Salfred	 */
23074462Salfred	if (madefd || !__rpc_sockisbound(fd)) {
23174462Salfred		if (bindaddr == NULL) {
23274462Salfred			if (bindresvport(fd, NULL) < 0) {
23374462Salfred				memset(&ss, 0, sizeof ss);
23474462Salfred				ss.ss_family = si.si_af;
23574462Salfred				ss.ss_len = si.si_alen;
23674462Salfred				if (_bind(fd, (struct sockaddr *)(void *)&ss,
23774462Salfred				    (socklen_t)si.si_alen) < 0) {
23874462Salfred					warnx(
23974462Salfred			"svc_tli_create: could not bind to anonymous port");
24074462Salfred					goto freedata;
24174462Salfred				}
24274462Salfred			}
24374462Salfred			_listen(fd, SOMAXCONN);
24474462Salfred		} else {
24574462Salfred			if (_bind(fd,
246115133Smbr			    (struct sockaddr *)bindaddr->addr.buf,
24774462Salfred			    (socklen_t)si.si_alen) < 0) {
24874462Salfred				warnx(
24974462Salfred		"svc_tli_create: could not bind to requested address");
25074462Salfred				goto freedata;
25174462Salfred			}
25274462Salfred			_listen(fd, (int)bindaddr->qlen);
25374462Salfred		}
25474462Salfred
25574462Salfred	}
25674462Salfred	/*
25774462Salfred	 * call transport specific function.
25874462Salfred	 */
25974462Salfred	switch (si.si_socktype) {
26074462Salfred		case SOCK_STREAM:
26174462Salfred			slen = sizeof ss;
26274462Salfred			if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
26374462Salfred			    == 0) {
26474462Salfred				/* accepted socket */
26574462Salfred				xprt = svc_fd_create(fd, sendsz, recvsz);
26674462Salfred			} else
26774462Salfred				xprt = svc_vc_create(fd, sendsz, recvsz);
26874462Salfred			if (!nconf || !xprt)
26974462Salfred				break;
27074462Salfred#if 0
27174462Salfred			/* XXX fvdl */
27274462Salfred			if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
27374462Salfred			    strcmp(nconf->nc_protofmly, "inet6") == 0)
27474462Salfred				(void) __svc_vc_setflag(xprt, TRUE);
27574462Salfred#endif
27674462Salfred			break;
27774462Salfred		case SOCK_DGRAM:
27874462Salfred			xprt = svc_dg_create(fd, sendsz, recvsz);
27974462Salfred			break;
28074462Salfred		default:
28174462Salfred			warnx("svc_tli_create: bad service type");
28274462Salfred			goto freedata;
28374462Salfred	}
28474462Salfred
28574462Salfred	if (xprt == NULL)
28674462Salfred		/*
28774462Salfred		 * The error messages here are spitted out by the lower layers:
28874462Salfred		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
28974462Salfred		 */
29074462Salfred		goto freedata;
29174462Salfred
29274462Salfred	/* Fill in type of service */
29374462Salfred	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
29474462Salfred
29574462Salfred	if (nconf) {
29674462Salfred		xprt->xp_netid = strdup(nconf->nc_netid);
29774462Salfred		xprt->xp_tp = strdup(nconf->nc_device);
29874462Salfred	}
29974462Salfred	return (xprt);
30074462Salfred
30174462Salfredfreedata:
30274462Salfred	if (madefd)
30374462Salfred		(void)_close(fd);
30474462Salfred	if (xprt) {
30574462Salfred		if (!madefd) /* so that svc_destroy doesnt close fd */
30674462Salfred			xprt->xp_fd = RPC_ANYFD;
30774462Salfred		SVC_DESTROY(xprt);
30874462Salfred	}
30974462Salfred	return (NULL);
31074462Salfred}
311