#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
#else
__RCSID("$NetBSD: svc_generic.c,v 1.18 2024/01/23 17:24:38 christos Exp $");
#endif
#endif
#include "namespace.h"
#include "reentrant.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <rpc/rpc.h>
#include <rpc/nettype.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "svc_fdset.h"
#include "rpc_internal.h"
#ifdef __weak_alias
__weak_alias(svc_create,_svc_create)
__weak_alias(svc_tp_create,_svc_tp_create)
__weak_alias(svc_tli_create,_svc_tli_create)
#endif
extern int __svc_vc_setflag(SVCXPRT *, int);
int
svc_create(
void (*dispatch)(struct svc_req *, SVCXPRT *),
rpcprog_t prognum,
rpcvers_t versnum,
const char *nettype)
{
struct xlist {
SVCXPRT *xprt;
struct xlist *next;
} *l;
static struct xlist *xprtlist;
int num = 0;
SVCXPRT *xprt;
struct netconfig *nconf;
void *handle;
if ((handle = __rpc_setconf(nettype)) == NULL) {
warnx("%s: unknown protocol %s", __func__, nettype);
return (0);
}
while ((nconf = __rpc_getconf(handle)) != NULL) {
mutex_lock(&xprtlist_lock);
for (l = xprtlist; l; l = l->next) {
if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
(void) rpcb_unset(prognum, versnum, nconf);
if (svc_reg(l->xprt, prognum, versnum,
dispatch, nconf) == FALSE)
warnx("%s: could not register prog %u "
"vers %u on %s", __func__,
(unsigned)prognum,
(unsigned)versnum, nconf->nc_netid);
else
num++;
break;
}
}
if (l == NULL) {
xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
if (xprt) {
l = malloc(sizeof(*l));
if (l == NULL) {
warn("%s: out of memory", __func__);
mutex_unlock(&xprtlist_lock);
return (0);
}
l->xprt = xprt;
l->next = xprtlist;
xprtlist = l;
num++;
}
}
mutex_unlock(&xprtlist_lock);
}
__rpc_endconf(handle);
return (num);
}
SVCXPRT *
svc_tp_create(
void (*dispatch)(struct svc_req *, SVCXPRT *),
rpcprog_t prognum,
rpcvers_t versnum,
const struct netconfig *nconf)
{
SVCXPRT *xprt;
if (nconf == NULL) {
warnx("%s: invalid netconfig structure for prog %u vers %u",
__func__, (unsigned)prognum, (unsigned)versnum);
return (NULL);
}
xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
if (xprt == NULL) {
return (NULL);
}
(void) rpcb_unset(prognum, versnum, __UNCONST(nconf));
if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
warnx("%s: Could not register prog %u vers %u on %s",
__func__, (unsigned)prognum, (unsigned)versnum,
nconf->nc_netid);
SVC_DESTROY(xprt);
return (NULL);
}
return (xprt);
}
SVCXPRT *
svc_tli_create(
int fd,
const struct netconfig *nconf,
const struct t_bind *bindaddr,
u_int sendsz,
u_int recvsz)
{
SVCXPRT *xprt = NULL;
bool_t madefd = FALSE;
struct __rpc_sockinfo si;
struct sockaddr_storage ss;
socklen_t slen;
if (fd == RPC_ANYFD) {
if (nconf == NULL) {
warnx("%s: invalid netconfig", __func__);
return (NULL);
}
fd = __rpc_nconf2fd(nconf);
if (fd == -1) {
warnx("%s: could not open connection for %s", __func__,
nconf->nc_netid);
return (NULL);
}
__rpc_nconf2sockinfo(nconf, &si);
madefd = TRUE;
} else {
if (!__rpc_fd2sockinfo(fd, &si)) {
warnx("%s: could not get transport information",
__func__);
return (NULL);
}
}
if (madefd || !__rpc_sockisbound(fd)) {
if (bindaddr == NULL) {
if (bindresvport(fd, NULL) < 0) {
memset(&ss, 0, sizeof ss);
ss.ss_family = si.si_af;
ss.ss_len = si.si_alen;
if (bind(fd, (struct sockaddr *)(void *)&ss,
(socklen_t)si.si_alen) < 0) {
warn( "%s: could not bind to anonymous "
"port", __func__);
goto freedata;
}
}
if (si.si_socktype != SOCK_DGRAM &&
listen(fd, SOMAXCONN) == -1) {
warnx("%s: could not listen at anonymous port",
__func__);
goto freedata;
}
} else {
if (bind(fd,
(struct sockaddr *)bindaddr->addr.buf,
(socklen_t)si.si_alen) < 0) {
warnx("%s: could not bind to requested address",
__func__);
goto freedata;
}
if (si.si_socktype != SOCK_DGRAM &&
listen(fd, (int)bindaddr->qlen) == -1) {
warnx("%s: could not listen at requested "
"address", __func__);
goto freedata;
}
}
}
switch (si.si_socktype) {
case SOCK_STREAM:
slen = sizeof ss;
if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
== 0) {
xprt = svc_fd_create(fd, sendsz, recvsz);
} else
xprt = svc_vc_create(fd, sendsz, recvsz);
if (!nconf || !xprt)
break;
#if 0
if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
strcmp(nconf->nc_protofmly, "inet6") == 0)
(void) __svc_vc_setflag(xprt, TRUE);
#endif
break;
case SOCK_DGRAM:
xprt = svc_dg_create(fd, sendsz, recvsz);
break;
default:
warnx("%s: bad service type %u", __func__, si.si_socktype);
goto freedata;
}
if (xprt == NULL)
goto freedata;
xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
if (nconf) {
xprt->xp_netid = strdup(nconf->nc_netid);
xprt->xp_tp = strdup(nconf->nc_device);
if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) {
svc_destroy(xprt);
return NULL;
}
}
return (xprt);
freedata:
if (madefd)
(void) close(fd);
return (NULL);
}