svc_generic.c revision 288113
174462Salfred/*	$NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $	*/
274462Salfred
3258578Shrs/*-
4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc.
5258578Shrs * All rights reserved.
6258578Shrs *
7258578Shrs * Redistribution and use in source and binary forms, with or without
8258578Shrs * modification, are permitted provided that the following conditions are met:
9258578Shrs * - Redistributions of source code must retain the above copyright notice,
10258578Shrs *   this list of conditions and the following disclaimer.
11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice,
12258578Shrs *   this list of conditions and the following disclaimer in the documentation
13258578Shrs *   and/or other materials provided with the distribution.
14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
15258578Shrs *   contributors may be used to endorse or promote products derived
16258578Shrs *   from this software without specific prior written permission.
1774462Salfred *
18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28258578Shrs * 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: head/lib/libc/rpc/svc_generic.c 288113 2015-09-22 15:40:07Z rodrigc $");
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.
75288113Srodrigc *
76288113Srodrigc * prognum - Program number
77288113Srodrigc * versnum - Version number
78288113Srodrigc * nettype - Networktype token
7974462Salfred */
8074462Salfredint
81288113Srodrigcsvc_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
82288113Srodrigc    rpcprog_t prognum, rpcvers_t versnum, const char *nettype)
8374462Salfred{
8474462Salfred	struct xlist {
8574462Salfred		SVCXPRT *xprt;		/* Server handle */
8674462Salfred		struct xlist *next;	/* Next item */
8774462Salfred	} *l;
8874462Salfred	static struct xlist *xprtlist;	/* A link list of all the handles */
8974462Salfred	int num = 0;
9074462Salfred	SVCXPRT *xprt;
9174462Salfred	struct netconfig *nconf;
9274462Salfred	void *handle;
9374462Salfred
9474462Salfred/* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
9574462Salfred
9674462Salfred	if ((handle = __rpc_setconf(nettype)) == NULL) {
9774462Salfred		warnx("svc_create: unknown protocol");
9874462Salfred		return (0);
9974462Salfred	}
10074462Salfred	while ((nconf = __rpc_getconf(handle)) != NULL) {
10174462Salfred		mutex_lock(&xprtlist_lock);
10274462Salfred		for (l = xprtlist; l; l = l->next) {
10374462Salfred			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
10474462Salfred				/* Found an old one, use it */
10574462Salfred				(void) rpcb_unset(prognum, versnum, nconf);
10674462Salfred				if (svc_reg(l->xprt, prognum, versnum,
10774462Salfred					dispatch, nconf) == FALSE)
10874462Salfred					warnx(
10974462Salfred		"svc_create: could not register prog %u vers %u on %s",
11074462Salfred					(unsigned)prognum, (unsigned)versnum,
11174462Salfred					 nconf->nc_netid);
11274462Salfred				else
11374462Salfred					num++;
11474462Salfred				break;
11574462Salfred			}
11674462Salfred		}
11774462Salfred		if (l == NULL) {
11874462Salfred			/* It was not found. Now create a new one */
11974462Salfred			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
12074462Salfred			if (xprt) {
12174462Salfred				l = (struct xlist *)malloc(sizeof (*l));
12274462Salfred				if (l == NULL) {
12374462Salfred					warnx("svc_create: no memory");
12474462Salfred					mutex_unlock(&xprtlist_lock);
12574462Salfred					return (0);
12674462Salfred				}
12774462Salfred				l->xprt = xprt;
12874462Salfred				l->next = xprtlist;
12974462Salfred				xprtlist = l;
13074462Salfred				num++;
13174462Salfred			}
13274462Salfred		}
13374462Salfred		mutex_unlock(&xprtlist_lock);
13474462Salfred	}
13574462Salfred	__rpc_endconf(handle);
13674462Salfred	/*
13774462Salfred	 * In case of num == 0; the error messages are generated by the
13874462Salfred	 * underlying layers; and hence not needed here.
13974462Salfred	 */
14074462Salfred	return (num);
14174462Salfred}
14274462Salfred
14374462Salfred/*
14474462Salfred * The high level interface to svc_tli_create().
14574462Salfred * It tries to create a server for "nconf" and registers the service
14674462Salfred * with the rpcbind. It calls svc_tli_create();
147288113Srodrigc *
148288113Srodrigc * prognum - Program number
149288113Srodrigc * versnum - Version number
150288113Srodrigc * ncofn   - Netconfig structure for the network
15174462Salfred */
15274462SalfredSVCXPRT *
153288113Srodrigcsvc_tp_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
154288113Srodrigc    rpcprog_t prognum, rpcvers_t versnum, const struct netconfig *nconf)
15574462Salfred{
15674462Salfred	SVCXPRT *xprt;
15774462Salfred
15874462Salfred	if (nconf == NULL) {
15974462Salfred		warnx(
16074462Salfred	"svc_tp_create: invalid netconfig structure for prog %u vers %u",
16174462Salfred				(unsigned)prognum, (unsigned)versnum);
16274462Salfred		return (NULL);
16374462Salfred	}
16474462Salfred	xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
16574462Salfred	if (xprt == NULL) {
16674462Salfred		return (NULL);
16774462Salfred	}
16874462Salfred	/*LINTED const castaway*/
16974462Salfred	(void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
17074462Salfred	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
17174462Salfred		warnx(
17274462Salfred		"svc_tp_create: Could not register prog %u vers %u on %s",
17374462Salfred				(unsigned)prognum, (unsigned)versnum,
17474462Salfred				nconf->nc_netid);
17574462Salfred		SVC_DESTROY(xprt);
17674462Salfred		return (NULL);
17774462Salfred	}
17874462Salfred	return (xprt);
17974462Salfred}
18074462Salfred
18174462Salfred/*
18274462Salfred * If fd is RPC_ANYFD, then it opens a fd for the given transport
18374462Salfred * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
18474462Salfred * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
18574462Salfred * NULL bindadr and Connection oriented transports, the value of qlen
18674462Salfred * is set to 8.
18774462Salfred *
18874462Salfred * If sendsz or recvsz are zero, their default values are chosen.
189288113Srodrigc *
190288113Srodrigc * fd       - Connection end point
191288113Srodrigc * nconf    - Netconfig struct for nettoken
192288113Srodrigc * bindaddr - Local bind address
193288113Srodrigc * sendsz   - Max sendsize
194288113Srodrigc * recvxz   - Max recvsize
19574462Salfred */
19674462SalfredSVCXPRT *
197288113Srodrigcsvc_tli_create(int fd, const struct netconfig *nconf,
198288113Srodrigc    const struct t_bind *bindaddr, u_int sendsz, u_int recvsz)
19974462Salfred{
20074462Salfred	SVCXPRT *xprt = NULL;		/* service handle */
20174462Salfred	bool_t madefd = FALSE;		/* whether fd opened here  */
20274462Salfred	struct __rpc_sockinfo si;
20374462Salfred	struct sockaddr_storage ss;
20474462Salfred	socklen_t slen;
20574462Salfred
20674462Salfred	if (fd == RPC_ANYFD) {
20774462Salfred		if (nconf == NULL) {
20874462Salfred			warnx("svc_tli_create: invalid netconfig");
20974462Salfred			return (NULL);
21074462Salfred		}
21174462Salfred		fd = __rpc_nconf2fd(nconf);
21274462Salfred		if (fd == -1) {
21374462Salfred			warnx(
21474462Salfred			    "svc_tli_create: could not open connection for %s",
21574462Salfred					nconf->nc_netid);
21674462Salfred			return (NULL);
21774462Salfred		}
21874462Salfred		__rpc_nconf2sockinfo(nconf, &si);
21974462Salfred		madefd = TRUE;
22074462Salfred	} else {
22174462Salfred		/*
22274462Salfred		 * It is an open descriptor. Get the transport info.
22374462Salfred		 */
22474462Salfred		if (!__rpc_fd2sockinfo(fd, &si)) {
22574462Salfred			warnx(
22674462Salfred		"svc_tli_create: could not get transport information");
22774462Salfred			return (NULL);
22874462Salfred		}
22974462Salfred	}
23074462Salfred
23174462Salfred	/*
23274462Salfred	 * If the fd is unbound, try to bind it.
23374462Salfred	 */
23474462Salfred	if (madefd || !__rpc_sockisbound(fd)) {
23574462Salfred		if (bindaddr == NULL) {
23674462Salfred			if (bindresvport(fd, NULL) < 0) {
23774462Salfred				memset(&ss, 0, sizeof ss);
23874462Salfred				ss.ss_family = si.si_af;
23974462Salfred				ss.ss_len = si.si_alen;
24074462Salfred				if (_bind(fd, (struct sockaddr *)(void *)&ss,
24174462Salfred				    (socklen_t)si.si_alen) < 0) {
24274462Salfred					warnx(
24374462Salfred			"svc_tli_create: could not bind to anonymous port");
24474462Salfred					goto freedata;
24574462Salfred				}
24674462Salfred			}
24774462Salfred			_listen(fd, SOMAXCONN);
24874462Salfred		} else {
24974462Salfred			if (_bind(fd,
250115133Smbr			    (struct sockaddr *)bindaddr->addr.buf,
25174462Salfred			    (socklen_t)si.si_alen) < 0) {
25274462Salfred				warnx(
25374462Salfred		"svc_tli_create: could not bind to requested address");
25474462Salfred				goto freedata;
25574462Salfred			}
25674462Salfred			_listen(fd, (int)bindaddr->qlen);
25774462Salfred		}
25874462Salfred
25974462Salfred	}
26074462Salfred	/*
26174462Salfred	 * call transport specific function.
26274462Salfred	 */
26374462Salfred	switch (si.si_socktype) {
26474462Salfred		case SOCK_STREAM:
26574462Salfred			slen = sizeof ss;
26674462Salfred			if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
26774462Salfred			    == 0) {
26874462Salfred				/* accepted socket */
26974462Salfred				xprt = svc_fd_create(fd, sendsz, recvsz);
27074462Salfred			} else
27174462Salfred				xprt = svc_vc_create(fd, sendsz, recvsz);
27274462Salfred			if (!nconf || !xprt)
27374462Salfred				break;
27474462Salfred#if 0
27574462Salfred			/* XXX fvdl */
27674462Salfred			if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
27774462Salfred			    strcmp(nconf->nc_protofmly, "inet6") == 0)
27874462Salfred				(void) __svc_vc_setflag(xprt, TRUE);
27974462Salfred#endif
28074462Salfred			break;
28174462Salfred		case SOCK_DGRAM:
28274462Salfred			xprt = svc_dg_create(fd, sendsz, recvsz);
28374462Salfred			break;
28474462Salfred		default:
28574462Salfred			warnx("svc_tli_create: bad service type");
28674462Salfred			goto freedata;
28774462Salfred	}
28874462Salfred
28974462Salfred	if (xprt == NULL)
29074462Salfred		/*
29174462Salfred		 * The error messages here are spitted out by the lower layers:
29274462Salfred		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
29374462Salfred		 */
29474462Salfred		goto freedata;
29574462Salfred
29674462Salfred	/* Fill in type of service */
29774462Salfred	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
29874462Salfred
29974462Salfred	if (nconf) {
30074462Salfred		xprt->xp_netid = strdup(nconf->nc_netid);
30174462Salfred		xprt->xp_tp = strdup(nconf->nc_device);
30274462Salfred	}
30374462Salfred	return (xprt);
30474462Salfred
30574462Salfredfreedata:
30674462Salfred	if (madefd)
30774462Salfred		(void)_close(fd);
30874462Salfred	if (xprt) {
30974462Salfred		if (!madefd) /* so that svc_destroy doesnt close fd */
31074462Salfred			xprt->xp_fd = RPC_ANYFD;
31174462Salfred		SVC_DESTROY(xprt);
31274462Salfred	}
31374462Salfred	return (NULL);
31474462Salfred}
315