1*0a6a1f1dSLionel Sambuc /* $NetBSD: svc_generic.c,v 1.17 2014/05/29 12:35:45 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
484d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
684d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
784d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
884d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
1084d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
1184d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1284d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
1384d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
1484d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
1584d9c625SLionel Sambuc * provided with the distribution.
1684d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
1784d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
1884d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
2084d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2184d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2284d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2384d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2484d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2584d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2684d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2784d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2884d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2984d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3084d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3184d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras /*
352fe8fb19SBen Gras * Copyright (c) 1986-1991 by Sun Microsystems Inc.
362fe8fb19SBen Gras */
372fe8fb19SBen Gras
382fe8fb19SBen Gras /* #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" */
392fe8fb19SBen Gras
402fe8fb19SBen Gras #include <sys/cdefs.h>
412fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
422fe8fb19SBen Gras #if 0
432fe8fb19SBen Gras static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
442fe8fb19SBen Gras #else
45*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: svc_generic.c,v 1.17 2014/05/29 12:35:45 christos Exp $");
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras #endif
482fe8fb19SBen Gras
492fe8fb19SBen Gras /*
502fe8fb19SBen Gras * svc_generic.c, Server side for RPC.
512fe8fb19SBen Gras *
522fe8fb19SBen Gras */
532fe8fb19SBen Gras
542fe8fb19SBen Gras #include "namespace.h"
552fe8fb19SBen Gras #include "reentrant.h"
562fe8fb19SBen Gras #include <sys/types.h>
572fe8fb19SBen Gras #include <sys/socket.h>
582fe8fb19SBen Gras #include <netinet/in.h>
592fe8fb19SBen Gras #include <rpc/rpc.h>
602fe8fb19SBen Gras #include <rpc/nettype.h>
612fe8fb19SBen Gras #include <stdio.h>
622fe8fb19SBen Gras #include <errno.h>
63f14fb602SLionel Sambuc #include <stdlib.h>
642fe8fb19SBen Gras #include <string.h>
652fe8fb19SBen Gras #include <unistd.h>
662fe8fb19SBen Gras #include <err.h>
672fe8fb19SBen Gras
6884d9c625SLionel Sambuc #include "svc_fdset.h"
692fe8fb19SBen Gras #include "rpc_internal.h"
702fe8fb19SBen Gras
712fe8fb19SBen Gras #ifdef __weak_alias
722fe8fb19SBen Gras __weak_alias(svc_create,_svc_create)
732fe8fb19SBen Gras __weak_alias(svc_tp_create,_svc_tp_create)
742fe8fb19SBen Gras __weak_alias(svc_tli_create,_svc_tli_create)
752fe8fb19SBen Gras #endif
762fe8fb19SBen Gras
77f14fb602SLionel Sambuc extern int __svc_vc_setflag(SVCXPRT *, int);
782fe8fb19SBen Gras
792fe8fb19SBen Gras /*
802fe8fb19SBen Gras * The highest level interface for server creation.
812fe8fb19SBen Gras * It tries for all the nettokens in that particular class of token
822fe8fb19SBen Gras * and returns the number of handles it can create and/or find.
832fe8fb19SBen Gras *
842fe8fb19SBen Gras * It creates a link list of all the handles it could create.
852fe8fb19SBen Gras * If svc_create() is called multiple times, it uses the handle
862fe8fb19SBen Gras * created earlier instead of creating a new handle every time.
872fe8fb19SBen Gras */
882fe8fb19SBen Gras int
svc_create(void (* dispatch)(struct svc_req *,SVCXPRT *),rpcprog_t prognum,rpcvers_t versnum,const char * nettype)89f14fb602SLionel Sambuc svc_create(
90f14fb602SLionel Sambuc void (*dispatch)(struct svc_req *, SVCXPRT *),
91f14fb602SLionel Sambuc rpcprog_t prognum, /* Program number */
92f14fb602SLionel Sambuc rpcvers_t versnum, /* Version number */
93f14fb602SLionel Sambuc const char *nettype) /* Networktype token */
942fe8fb19SBen Gras {
952fe8fb19SBen Gras struct xlist {
962fe8fb19SBen Gras SVCXPRT *xprt; /* Server handle */
972fe8fb19SBen Gras struct xlist *next; /* Next item */
982fe8fb19SBen Gras } *l;
992fe8fb19SBen Gras static struct xlist *xprtlist; /* A link list of all the handles */
1002fe8fb19SBen Gras int num = 0;
1012fe8fb19SBen Gras SVCXPRT *xprt;
1022fe8fb19SBen Gras struct netconfig *nconf;
1032fe8fb19SBen Gras void *handle;
1042fe8fb19SBen Gras #ifdef _REENTRANT
1052fe8fb19SBen Gras extern mutex_t xprtlist_lock;
1062fe8fb19SBen Gras #endif
1072fe8fb19SBen Gras
1082fe8fb19SBen Gras /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
1092fe8fb19SBen Gras
1102fe8fb19SBen Gras if ((handle = __rpc_setconf(nettype)) == NULL) {
11184d9c625SLionel Sambuc warnx("%s: unknown protocol %s", __func__, nettype);
1122fe8fb19SBen Gras return (0);
1132fe8fb19SBen Gras }
1142fe8fb19SBen Gras while ((nconf = __rpc_getconf(handle)) != NULL) {
1152fe8fb19SBen Gras mutex_lock(&xprtlist_lock);
1162fe8fb19SBen Gras for (l = xprtlist; l; l = l->next) {
1172fe8fb19SBen Gras if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
1182fe8fb19SBen Gras /* Found an old one, use it */
1192fe8fb19SBen Gras (void) rpcb_unset(prognum, versnum, nconf);
1202fe8fb19SBen Gras if (svc_reg(l->xprt, prognum, versnum,
1212fe8fb19SBen Gras dispatch, nconf) == FALSE)
12284d9c625SLionel Sambuc warnx("%s: could not register prog %u "
12384d9c625SLionel Sambuc "vers %u on %s", __func__,
12484d9c625SLionel Sambuc (unsigned)prognum,
12584d9c625SLionel Sambuc (unsigned)versnum, nconf->nc_netid);
1262fe8fb19SBen Gras else
1272fe8fb19SBen Gras num++;
1282fe8fb19SBen Gras break;
1292fe8fb19SBen Gras }
1302fe8fb19SBen Gras }
1312fe8fb19SBen Gras if (l == NULL) {
1322fe8fb19SBen Gras /* It was not found. Now create a new one */
1332fe8fb19SBen Gras xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
1342fe8fb19SBen Gras if (xprt) {
1352fe8fb19SBen Gras l = malloc(sizeof(*l));
1362fe8fb19SBen Gras if (l == NULL) {
13784d9c625SLionel Sambuc warn("%s: out of memory", __func__);
1382fe8fb19SBen Gras mutex_unlock(&xprtlist_lock);
1392fe8fb19SBen Gras return (0);
1402fe8fb19SBen Gras }
1412fe8fb19SBen Gras l->xprt = xprt;
1422fe8fb19SBen Gras l->next = xprtlist;
1432fe8fb19SBen Gras xprtlist = l;
1442fe8fb19SBen Gras num++;
1452fe8fb19SBen Gras }
1462fe8fb19SBen Gras }
1472fe8fb19SBen Gras mutex_unlock(&xprtlist_lock);
1482fe8fb19SBen Gras }
1492fe8fb19SBen Gras __rpc_endconf(handle);
1502fe8fb19SBen Gras /*
1512fe8fb19SBen Gras * In case of num == 0; the error messages are generated by the
1522fe8fb19SBen Gras * underlying layers; and hence not needed here.
1532fe8fb19SBen Gras */
1542fe8fb19SBen Gras return (num);
1552fe8fb19SBen Gras }
1562fe8fb19SBen Gras
1572fe8fb19SBen Gras /*
1582fe8fb19SBen Gras * The high level interface to svc_tli_create().
1592fe8fb19SBen Gras * It tries to create a server for "nconf" and registers the service
1602fe8fb19SBen Gras * with the rpcbind. It calls svc_tli_create();
1612fe8fb19SBen Gras */
1622fe8fb19SBen Gras SVCXPRT *
svc_tp_create(void (* dispatch)(struct svc_req *,SVCXPRT *),rpcprog_t prognum,rpcvers_t versnum,const struct netconfig * nconf)163f14fb602SLionel Sambuc svc_tp_create(
164f14fb602SLionel Sambuc void (*dispatch)(struct svc_req *, SVCXPRT *),
165f14fb602SLionel Sambuc rpcprog_t prognum, /* Program number */
166f14fb602SLionel Sambuc rpcvers_t versnum, /* Version number */
167f14fb602SLionel Sambuc const struct netconfig *nconf) /* Netconfig structure for the network */
1682fe8fb19SBen Gras {
1692fe8fb19SBen Gras SVCXPRT *xprt;
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras if (nconf == NULL) {
17284d9c625SLionel Sambuc warnx("%s: invalid netconfig structure for prog %u vers %u",
17384d9c625SLionel Sambuc __func__, (unsigned)prognum, (unsigned)versnum);
1742fe8fb19SBen Gras return (NULL);
1752fe8fb19SBen Gras }
1762fe8fb19SBen Gras xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
1772fe8fb19SBen Gras if (xprt == NULL) {
1782fe8fb19SBen Gras return (NULL);
1792fe8fb19SBen Gras }
1802fe8fb19SBen Gras (void) rpcb_unset(prognum, versnum, __UNCONST(nconf));
1812fe8fb19SBen Gras if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
18284d9c625SLionel Sambuc warnx("%s: Could not register prog %u vers %u on %s",
18384d9c625SLionel Sambuc __func__, (unsigned)prognum, (unsigned)versnum,
1842fe8fb19SBen Gras nconf->nc_netid);
1852fe8fb19SBen Gras SVC_DESTROY(xprt);
1862fe8fb19SBen Gras return (NULL);
1872fe8fb19SBen Gras }
1882fe8fb19SBen Gras return (xprt);
1892fe8fb19SBen Gras }
1902fe8fb19SBen Gras
1912fe8fb19SBen Gras /*
1922fe8fb19SBen Gras * If fd is RPC_ANYFD, then it opens a fd for the given transport
1932fe8fb19SBen Gras * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
1942fe8fb19SBen Gras * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
1952fe8fb19SBen Gras * NULL bindadr and Connection oriented transports, the value of qlen
1962fe8fb19SBen Gras * is set to 8.
1972fe8fb19SBen Gras *
1982fe8fb19SBen Gras * If sendsz or recvsz are zero, their default values are chosen.
1992fe8fb19SBen Gras */
2002fe8fb19SBen Gras SVCXPRT *
svc_tli_create(int fd,const struct netconfig * nconf,const struct t_bind * bindaddr,u_int sendsz,u_int recvsz)201f14fb602SLionel Sambuc svc_tli_create(
202f14fb602SLionel Sambuc int fd, /* Connection end point */
203f14fb602SLionel Sambuc const struct netconfig *nconf, /* Netconfig struct for nettoken */
204f14fb602SLionel Sambuc const struct t_bind *bindaddr, /* Local bind address */
205f14fb602SLionel Sambuc u_int sendsz, /* Max sendsize */
206f14fb602SLionel Sambuc u_int recvsz) /* Max recvsize */
2072fe8fb19SBen Gras {
2082fe8fb19SBen Gras SVCXPRT *xprt = NULL; /* service handle */
2092fe8fb19SBen Gras bool_t madefd = FALSE; /* whether fd opened here */
2102fe8fb19SBen Gras struct __rpc_sockinfo si;
2112fe8fb19SBen Gras struct sockaddr_storage ss;
2122fe8fb19SBen Gras socklen_t slen;
2132fe8fb19SBen Gras
2142fe8fb19SBen Gras if (fd == RPC_ANYFD) {
2152fe8fb19SBen Gras if (nconf == NULL) {
21684d9c625SLionel Sambuc warnx("%s: invalid netconfig", __func__);
2172fe8fb19SBen Gras return (NULL);
2182fe8fb19SBen Gras }
2192fe8fb19SBen Gras fd = __rpc_nconf2fd(nconf);
2202fe8fb19SBen Gras if (fd == -1) {
22184d9c625SLionel Sambuc warnx("%s: could not open connection for %s", __func__,
2222fe8fb19SBen Gras nconf->nc_netid);
2232fe8fb19SBen Gras return (NULL);
2242fe8fb19SBen Gras }
2252fe8fb19SBen Gras __rpc_nconf2sockinfo(nconf, &si);
2262fe8fb19SBen Gras madefd = TRUE;
2272fe8fb19SBen Gras } else {
2282fe8fb19SBen Gras /*
2292fe8fb19SBen Gras * It is an open descriptor. Get the transport info.
2302fe8fb19SBen Gras */
2312fe8fb19SBen Gras if (!__rpc_fd2sockinfo(fd, &si)) {
23284d9c625SLionel Sambuc warnx("%s: could not get transport information",
23384d9c625SLionel Sambuc __func__);
2342fe8fb19SBen Gras return (NULL);
2352fe8fb19SBen Gras }
2362fe8fb19SBen Gras }
2372fe8fb19SBen Gras
2382fe8fb19SBen Gras /*
2392fe8fb19SBen Gras * If the fd is unbound, try to bind it.
2402fe8fb19SBen Gras */
2412fe8fb19SBen Gras if (madefd || !__rpc_sockisbound(fd)) {
2422fe8fb19SBen Gras if (bindaddr == NULL) {
2432fe8fb19SBen Gras if (bindresvport(fd, NULL) < 0) {
2442fe8fb19SBen Gras memset(&ss, 0, sizeof ss);
2452fe8fb19SBen Gras ss.ss_family = si.si_af;
2462fe8fb19SBen Gras ss.ss_len = si.si_alen;
2472fe8fb19SBen Gras if (bind(fd, (struct sockaddr *)(void *)&ss,
2482fe8fb19SBen Gras (socklen_t)si.si_alen) < 0) {
24984d9c625SLionel Sambuc warn( "%s: could not bind to anonymous "
25084d9c625SLionel Sambuc "port", __func__);
2512fe8fb19SBen Gras goto freedata;
2522fe8fb19SBen Gras }
2532fe8fb19SBen Gras }
254*0a6a1f1dSLionel Sambuc if (si.si_socktype != SOCK_DGRAM &&
255*0a6a1f1dSLionel Sambuc listen(fd, SOMAXCONN) == -1) {
256*0a6a1f1dSLionel Sambuc warnx("%s: could not listen at anonymous port",
257*0a6a1f1dSLionel Sambuc __func__);
258*0a6a1f1dSLionel Sambuc goto freedata;
259*0a6a1f1dSLionel Sambuc }
2602fe8fb19SBen Gras } else {
2612fe8fb19SBen Gras if (bind(fd,
2622fe8fb19SBen Gras (struct sockaddr *)bindaddr->addr.buf,
2632fe8fb19SBen Gras (socklen_t)si.si_alen) < 0) {
26484d9c625SLionel Sambuc warnx("%s: could not bind to requested address",
26584d9c625SLionel Sambuc __func__);
2662fe8fb19SBen Gras goto freedata;
2672fe8fb19SBen Gras }
268*0a6a1f1dSLionel Sambuc if (si.si_socktype != SOCK_DGRAM &&
269*0a6a1f1dSLionel Sambuc listen(fd, (int)bindaddr->qlen) == -1) {
270*0a6a1f1dSLionel Sambuc warnx("%s: could not listen at requested "
271*0a6a1f1dSLionel Sambuc "address", __func__);
272*0a6a1f1dSLionel Sambuc goto freedata;
2732fe8fb19SBen Gras }
274*0a6a1f1dSLionel Sambuc }
2752fe8fb19SBen Gras }
2762fe8fb19SBen Gras /*
2772fe8fb19SBen Gras * call transport specific function.
2782fe8fb19SBen Gras */
2792fe8fb19SBen Gras switch (si.si_socktype) {
2802fe8fb19SBen Gras case SOCK_STREAM:
2812fe8fb19SBen Gras slen = sizeof ss;
2822fe8fb19SBen Gras if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
2832fe8fb19SBen Gras == 0) {
2842fe8fb19SBen Gras /* accepted socket */
2852fe8fb19SBen Gras xprt = svc_fd_create(fd, sendsz, recvsz);
2862fe8fb19SBen Gras } else
2872fe8fb19SBen Gras xprt = svc_vc_create(fd, sendsz, recvsz);
2882fe8fb19SBen Gras if (!nconf || !xprt)
2892fe8fb19SBen Gras break;
2902fe8fb19SBen Gras #if 0
2912fe8fb19SBen Gras /* XXX fvdl */
2922fe8fb19SBen Gras if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
2932fe8fb19SBen Gras strcmp(nconf->nc_protofmly, "inet6") == 0)
2942fe8fb19SBen Gras (void) __svc_vc_setflag(xprt, TRUE);
2952fe8fb19SBen Gras #endif
2962fe8fb19SBen Gras break;
2972fe8fb19SBen Gras case SOCK_DGRAM:
2982fe8fb19SBen Gras xprt = svc_dg_create(fd, sendsz, recvsz);
2992fe8fb19SBen Gras break;
3002fe8fb19SBen Gras default:
30184d9c625SLionel Sambuc warnx("%s: bad service type %u", __func__, si.si_socktype);
3022fe8fb19SBen Gras goto freedata;
3032fe8fb19SBen Gras }
3042fe8fb19SBen Gras
3052fe8fb19SBen Gras if (xprt == NULL)
3062fe8fb19SBen Gras /*
3072fe8fb19SBen Gras * The error messages here are spitted out by the lower layers:
3082fe8fb19SBen Gras * svc_vc_create(), svc_fd_create() and svc_dg_create().
3092fe8fb19SBen Gras */
3102fe8fb19SBen Gras goto freedata;
3112fe8fb19SBen Gras
3122fe8fb19SBen Gras /* Fill in type of service */
3132fe8fb19SBen Gras xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
3142fe8fb19SBen Gras
3152fe8fb19SBen Gras if (nconf) {
3162fe8fb19SBen Gras xprt->xp_netid = strdup(nconf->nc_netid);
3172fe8fb19SBen Gras xprt->xp_tp = strdup(nconf->nc_device);
3182fe8fb19SBen Gras if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) {
3192fe8fb19SBen Gras svc_destroy(xprt);
3202fe8fb19SBen Gras return NULL;
3212fe8fb19SBen Gras }
3222fe8fb19SBen Gras }
3232fe8fb19SBen Gras return (xprt);
3242fe8fb19SBen Gras
3252fe8fb19SBen Gras freedata:
3262fe8fb19SBen Gras if (madefd)
3272fe8fb19SBen Gras (void) close(fd);
3282fe8fb19SBen Gras return (NULL);
3292fe8fb19SBen Gras }
330