#include <stdio.h>
#include <rpc/rpc.h>
#include <errno.h>
#ifdef SYSLOG
#include <sys/syslog.h>
#else
#define LOG_ERR 3
#endif
#include <rpc/nettype.h>
#include <netconfig.h>
#include <netdir.h>
extern char *strdup();
int
svc_create_local_service(dispatch, prognum, versnum, nettype, servname)
void (*dispatch) ();
ulong_t prognum;
ulong_t versnum;
char *nettype;
char *servname;
{
struct xlist {
SVCXPRT *xprt;
struct xlist *next;
} *l;
static struct xlist *xprtlist;
int num = 0;
SVCXPRT *xprt;
struct netconfig *nconf;
struct t_bind *bind_addr;
void *handle;
int fd;
struct nd_hostserv ns;
struct nd_addrlist *nas;
if ((handle = __rpc_setconf(nettype)) == NULL) {
(void) syslog(LOG_ERR,
"svc_create: could not read netconfig database");
return (0);
}
while (nconf = __rpc_getconf(handle)) {
if (strcmp(nconf->nc_protofmly, NC_LOOPBACK))
continue;
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)
(void) syslog(LOG_ERR,
"svc_create: could not register prog %d vers %d on %s",
prognum, versnum, nconf->nc_netid);
else
num++;
break;
}
}
if (l)
continue;
if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
syslog(LOG_ERR,
"svc_create: %s: cannot open connection: %s",
nconf->nc_netid, t_errlist[t_errno]);
continue;
}
if (__rpc_negotiate_uid(fd) != 0) {
syslog(LOG_ERR,
"Couldn't negotiate for uid with loopback transport %s",
nconf->nc_netid);
t_close(fd);
continue;
}
bind_addr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
if ((bind_addr == NULL)) {
t_close(fd);
(void) syslog(LOG_ERR, "svc_create: t_alloc failed\n");
continue;
}
ns.h_host = HOST_SELF;
ns.h_serv = servname;
if (!netdir_getbyname(nconf, &ns, &nas)) {
bind_addr->addr.len = nas->n_addrs->len;
memcpy(bind_addr->addr.buf, nas->n_addrs->buf,
(int)nas->n_addrs->len);
bind_addr->qlen = 8;
netdir_free((char *)nas, ND_ADDRLIST);
} else {
syslog(LOG_ERR,
"svc_create: no well known address for %s on transport %s",
servname, nconf->nc_netid);
(void) t_free((char *)bind_addr, T_BIND);
bind_addr = NULL;
}
xprt = svc_tli_create(fd, nconf, bind_addr, 0, 0);
if (bind_addr)
(void) t_free((char *)bind_addr, T_BIND);
if (xprt) {
(void) rpcb_unset(prognum, versnum, nconf);
if (svc_reg(xprt, prognum, versnum,
dispatch, nconf) == FALSE) {
(void) syslog(LOG_ERR,
"svc_create: could not register prog %d vers %d on %s",
prognum, versnum, nconf->nc_netid);
SVC_DESTROY(xprt);
continue;
}
l = (struct xlist *)malloc(sizeof (struct xlist));
if (l == (struct xlist *)NULL) {
(void) syslog(LOG_ERR,
"svc_create: no memory");
SVC_DESTROY(xprt);
return (num);
}
l->xprt = xprt;
l->next = xprtlist;
xprtlist = l;
num++;
}
}
__rpc_endconf(handle);
return (num);
}