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: releng/11.0/lib/libc/rpc/svc_generic.c 300386 2016-05-22 02:02:18Z ngie $"); 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); 125300386Sngie num = 0; 126300386Sngie goto done; 12774462Salfred } 12874462Salfred l->xprt = xprt; 12974462Salfred l->next = xprtlist; 13074462Salfred xprtlist = l; 13174462Salfred num++; 13274462Salfred } 13374462Salfred } 13474462Salfred mutex_unlock(&xprtlist_lock); 13574462Salfred } 136300386Sngiedone: 13774462Salfred __rpc_endconf(handle); 13874462Salfred /* 13974462Salfred * In case of num == 0; the error messages are generated by the 14074462Salfred * underlying layers; and hence not needed here. 14174462Salfred */ 14274462Salfred return (num); 14374462Salfred} 14474462Salfred 14574462Salfred/* 14674462Salfred * The high level interface to svc_tli_create(). 14774462Salfred * It tries to create a server for "nconf" and registers the service 14874462Salfred * with the rpcbind. It calls svc_tli_create(); 149288113Srodrigc * 150288113Srodrigc * prognum - Program number 151288113Srodrigc * versnum - Version number 152288113Srodrigc * ncofn - Netconfig structure for the network 15374462Salfred */ 15474462SalfredSVCXPRT * 155288113Srodrigcsvc_tp_create(void (*dispatch)(struct svc_req *, SVCXPRT *), 156288113Srodrigc rpcprog_t prognum, rpcvers_t versnum, const struct netconfig *nconf) 15774462Salfred{ 15874462Salfred SVCXPRT *xprt; 15974462Salfred 16074462Salfred if (nconf == NULL) { 16174462Salfred warnx( 16274462Salfred "svc_tp_create: invalid netconfig structure for prog %u vers %u", 16374462Salfred (unsigned)prognum, (unsigned)versnum); 16474462Salfred return (NULL); 16574462Salfred } 16674462Salfred xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); 16774462Salfred if (xprt == NULL) { 16874462Salfred return (NULL); 16974462Salfred } 17074462Salfred /*LINTED const castaway*/ 17174462Salfred (void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf); 17274462Salfred if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) { 17374462Salfred warnx( 17474462Salfred "svc_tp_create: Could not register prog %u vers %u on %s", 17574462Salfred (unsigned)prognum, (unsigned)versnum, 17674462Salfred nconf->nc_netid); 17774462Salfred SVC_DESTROY(xprt); 17874462Salfred return (NULL); 17974462Salfred } 18074462Salfred return (xprt); 18174462Salfred} 18274462Salfred 18374462Salfred/* 18474462Salfred * If fd is RPC_ANYFD, then it opens a fd for the given transport 18574462Salfred * provider (nconf cannot be NULL then). If the t_state is T_UNBND and 18674462Salfred * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For 18774462Salfred * NULL bindadr and Connection oriented transports, the value of qlen 18874462Salfred * is set to 8. 18974462Salfred * 19074462Salfred * If sendsz or recvsz are zero, their default values are chosen. 191288113Srodrigc * 192288113Srodrigc * fd - Connection end point 193288113Srodrigc * nconf - Netconfig struct for nettoken 194288113Srodrigc * bindaddr - Local bind address 195288113Srodrigc * sendsz - Max sendsize 196288113Srodrigc * recvxz - Max recvsize 19774462Salfred */ 19874462SalfredSVCXPRT * 199288113Srodrigcsvc_tli_create(int fd, const struct netconfig *nconf, 200288113Srodrigc const struct t_bind *bindaddr, u_int sendsz, u_int recvsz) 20174462Salfred{ 20274462Salfred SVCXPRT *xprt = NULL; /* service handle */ 20374462Salfred bool_t madefd = FALSE; /* whether fd opened here */ 20474462Salfred struct __rpc_sockinfo si; 20574462Salfred struct sockaddr_storage ss; 20674462Salfred socklen_t slen; 20774462Salfred 20874462Salfred if (fd == RPC_ANYFD) { 20974462Salfred if (nconf == NULL) { 21074462Salfred warnx("svc_tli_create: invalid netconfig"); 21174462Salfred return (NULL); 21274462Salfred } 21374462Salfred fd = __rpc_nconf2fd(nconf); 21474462Salfred if (fd == -1) { 21574462Salfred warnx( 21674462Salfred "svc_tli_create: could not open connection for %s", 21774462Salfred nconf->nc_netid); 21874462Salfred return (NULL); 21974462Salfred } 22074462Salfred __rpc_nconf2sockinfo(nconf, &si); 22174462Salfred madefd = TRUE; 22274462Salfred } else { 22374462Salfred /* 22474462Salfred * It is an open descriptor. Get the transport info. 22574462Salfred */ 22674462Salfred if (!__rpc_fd2sockinfo(fd, &si)) { 22774462Salfred warnx( 22874462Salfred "svc_tli_create: could not get transport information"); 22974462Salfred return (NULL); 23074462Salfred } 23174462Salfred } 23274462Salfred 23374462Salfred /* 23474462Salfred * If the fd is unbound, try to bind it. 23574462Salfred */ 23674462Salfred if (madefd || !__rpc_sockisbound(fd)) { 23774462Salfred if (bindaddr == NULL) { 23874462Salfred if (bindresvport(fd, NULL) < 0) { 23974462Salfred memset(&ss, 0, sizeof ss); 24074462Salfred ss.ss_family = si.si_af; 24174462Salfred ss.ss_len = si.si_alen; 24274462Salfred if (_bind(fd, (struct sockaddr *)(void *)&ss, 24374462Salfred (socklen_t)si.si_alen) < 0) { 24474462Salfred warnx( 24574462Salfred "svc_tli_create: could not bind to anonymous port"); 24674462Salfred goto freedata; 24774462Salfred } 24874462Salfred } 24974462Salfred _listen(fd, SOMAXCONN); 25074462Salfred } else { 25174462Salfred if (_bind(fd, 252115133Smbr (struct sockaddr *)bindaddr->addr.buf, 25374462Salfred (socklen_t)si.si_alen) < 0) { 25474462Salfred warnx( 25574462Salfred "svc_tli_create: could not bind to requested address"); 25674462Salfred goto freedata; 25774462Salfred } 25874462Salfred _listen(fd, (int)bindaddr->qlen); 25974462Salfred } 26074462Salfred 26174462Salfred } 26274462Salfred /* 26374462Salfred * call transport specific function. 26474462Salfred */ 26574462Salfred switch (si.si_socktype) { 26674462Salfred case SOCK_STREAM: 26774462Salfred slen = sizeof ss; 26874462Salfred if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) 26974462Salfred == 0) { 27074462Salfred /* accepted socket */ 27174462Salfred xprt = svc_fd_create(fd, sendsz, recvsz); 27274462Salfred } else 27374462Salfred xprt = svc_vc_create(fd, sendsz, recvsz); 27474462Salfred if (!nconf || !xprt) 27574462Salfred break; 27674462Salfred#if 0 27774462Salfred /* XXX fvdl */ 27874462Salfred if (strcmp(nconf->nc_protofmly, "inet") == 0 || 27974462Salfred strcmp(nconf->nc_protofmly, "inet6") == 0) 28074462Salfred (void) __svc_vc_setflag(xprt, TRUE); 28174462Salfred#endif 28274462Salfred break; 28374462Salfred case SOCK_DGRAM: 28474462Salfred xprt = svc_dg_create(fd, sendsz, recvsz); 28574462Salfred break; 28674462Salfred default: 28774462Salfred warnx("svc_tli_create: bad service type"); 28874462Salfred goto freedata; 28974462Salfred } 29074462Salfred 29174462Salfred if (xprt == NULL) 29274462Salfred /* 29374462Salfred * The error messages here are spitted out by the lower layers: 29474462Salfred * svc_vc_create(), svc_fd_create() and svc_dg_create(). 29574462Salfred */ 29674462Salfred goto freedata; 29774462Salfred 29874462Salfred /* Fill in type of service */ 29974462Salfred xprt->xp_type = __rpc_socktype2seman(si.si_socktype); 30074462Salfred 30174462Salfred if (nconf) { 30274462Salfred xprt->xp_netid = strdup(nconf->nc_netid); 30374462Salfred xprt->xp_tp = strdup(nconf->nc_device); 30474462Salfred } 30574462Salfred return (xprt); 30674462Salfred 30774462Salfredfreedata: 30874462Salfred if (madefd) 30974462Salfred (void)_close(fd); 31074462Salfred if (xprt) { 31174462Salfred if (!madefd) /* so that svc_destroy doesnt close fd */ 31274462Salfred xprt->xp_fd = RPC_ANYFD; 31374462Salfred SVC_DESTROY(xprt); 31474462Salfred } 31574462Salfred return (NULL); 31674462Salfred} 317