Convert to tirpc interface, and to be af-independent.

This commit is contained in:
fvdl 2000-06-09 14:00:52 +00:00
parent bb4112fd6f
commit 7704425071
6 changed files with 165 additions and 116 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: lock_proc.c,v 1.4 2000/06/07 14:34:40 bouyer Exp $ */
/* $NetBSD: lock_proc.c,v 1.5 2000/06/09 14:00:52 fvdl Exp $ */
/*
* Copyright (c) 1995
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: lock_proc.c,v 1.4 2000/06/07 14:34:40 bouyer Exp $");
__RCSID("$NetBSD: lock_proc.c,v 1.5 2000/06/09 14:00:52 fvdl Exp $");
#endif
#include <sys/param.h>
@ -48,6 +48,7 @@ __RCSID("$NetBSD: lock_proc.c,v 1.4 2000/06/07 14:34:40 bouyer Exp $");
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <netconfig.h>
#include <rpc/rpc.h>
#include <rpcsvc/sm_inter.h>
@ -61,6 +62,7 @@ __RCSID("$NetBSD: lock_proc.c,v 1.4 2000/06/07 14:34:40 bouyer Exp $");
#define CLIENT_CACHE_LIFETIME 120 /* In seconds */
static void log_from_addr __P((char *, struct svc_req *));
static int addrcmp __P((struct sockaddr *, struct sockaddr *));
/* log_from_addr ----------------------------------------------------------- */
/*
@ -74,17 +76,13 @@ log_from_addr(fun_name, req)
char *fun_name;
struct svc_req *req;
{
struct sockaddr_in *addr;
struct hostent *host;
char hostname_buf[40];
struct sockaddr *addr;
char hostname_buf[NI_MAXHOST];
addr = svc_getcaller(req->rq_xprt);
host = gethostbyaddr((char *)&(addr->sin_addr), addr->sin_len, AF_INET);
if (host) {
strncpy(hostname_buf, host->h_name, sizeof(hostname_buf));
hostname_buf[sizeof(hostname_buf) - 1] = '\0';
} else /* No hostname available - print raw address */
strcpy(hostname_buf, inet_ntoa(addr->sin_addr));
addr = svc_getrpccaller(req->rq_xprt)->buf;
if (getnameinfo(addr , addr->sa_len, hostname_buf, sizeof hostname_buf,
NULL, 0, 0) != 0)
return;
syslog(LOG_DEBUG, "%s from %s", fun_name, hostname_buf);
}
@ -120,17 +118,49 @@ log_from_addr(fun_name, req)
*/
static CLIENT *clnt_cache_ptr[CLIENT_CACHE_SIZE];
static long clnt_cache_time[CLIENT_CACHE_SIZE]; /* time entry created */
static struct in_addr clnt_cache_addr[CLIENT_CACHE_SIZE];
static struct sockaddr_storage clnt_cache_addr[CLIENT_CACHE_SIZE];
static int clnt_cache_next_to_use = 0;
static int
addrcmp(sa1, sa2)
struct sockaddr *sa1;
struct sockaddr *sa2;
{
int len;
void *p1, *p2;
if (sa1->sa_family != sa2->sa_family)
return -1;
switch (sa1->sa_family) {
case AF_INET:
p1 = &((struct sockaddr_in *)sa1)->sin_addr;
p2 = &((struct sockaddr_in *)sa2)->sin_addr;
len = 4;
break;
case AF_INET6:
p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
len = 16;
break;
default:
return -1;
}
return memcmp(p1, p2, len);
}
CLIENT *
get_client(host_addr, vers)
struct sockaddr_in *host_addr;
u_long vers;
struct sockaddr *host_addr;
rpcvers_t vers;
{
CLIENT *client;
struct timeval retry_time, time_now;
int i, sock_no;
int i;
char *netid;
struct netconfig *nconf;
char host[NI_MAXHOST];
gettimeofday(&time_now, NULL);
@ -150,8 +180,8 @@ get_client(host_addr, vers)
clnt_cache_ptr[i] = NULL;
client = NULL;
}
if (client && !memcmp(&clnt_cache_addr[i],
&host_addr->sin_addr, sizeof(struct in_addr))) {
if (client && !addrcmp((struct sockaddr *)&clnt_cache_addr[i],
host_addr)) {
/* Found it! */
if (debug_level > 3)
syslog(LOG_DEBUG, "Found CLIENT* in cache");
@ -165,28 +195,47 @@ get_client(host_addr, vers)
clnt_cache_ptr[clnt_cache_next_to_use] = NULL;
}
/* Create the new client handle */
sock_no = RPC_ANYSOCK;
retry_time.tv_sec = 5;
retry_time.tv_usec = 0;
host_addr->sin_port = 0; /* Force consultation with portmapper */
/*
* Need a host string for clnt_tp_create. Use NI_NUMERICHOST
* to avoid DNS lookups.
*/
if (getnameinfo(host_addr, host_addr->sa_len, host, sizeof host,
NULL, 0, NI_NUMERICHOST) != 0) {
syslog(LOG_ERR, "unable to get name string for caller");
return NULL;
}
#if 1
client = clntudp_create(host_addr, NLM_PROG, vers,
retry_time, &sock_no);
if (host_addr->sa_family == AF_INET6)
netid = "udp6";
else
netid = "udp";
#else
client = clnttcp_create(host_addr, NLM_PROG, vers,
&sock_no, 0, 0);
if (host_addr->sa_family == AF_INET6)
netid = "tcp6";
else
netid = "tcp";
#endif
nconf = getnetconfigent(netid);
if (nconf == NULL) {
syslog(LOG_ERR, "could not get netconfig info for '%s': "
"no /etc/netconfig file?", netid);
return NULL;
}
client = clnt_tp_create(host, NLM_PROG, vers, nconf);
freenetconfigent(nconf);
if (!client) {
syslog(LOG_ERR, clnt_spcreateerror("clntudp_create"));
syslog(LOG_ERR, "Unable to return result to %s",
inet_ntoa(host_addr->sin_addr));
syslog(LOG_ERR, "Unable to return result to %s", host);
return NULL;
}
/* Success - update the cache entry */
clnt_cache_ptr[clnt_cache_next_to_use] = client;
clnt_cache_addr[clnt_cache_next_to_use] = host_addr->sin_addr;
memcpy(&clnt_cache_addr[clnt_cache_next_to_use], host_addr,
host_addr->sa_len);
clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec;
if (++clnt_cache_next_to_use > CLIENT_CACHE_SIZE)
clnt_cache_next_to_use = 0;
@ -201,8 +250,7 @@ get_client(host_addr, vers)
clnt_control(client, CLSET_TIMEOUT, (char *)&retry_time);
if (debug_level > 3)
syslog(LOG_DEBUG, "Created CLIENT* for %s",
inet_ntoa(host_addr->sin_addr));
syslog(LOG_DEBUG, "Created CLIENT* for %s", host);
return client;
}
@ -226,7 +274,7 @@ transmit_result(opcode, result, addr)
struct timeval timeo;
int success;
if ((cli = get_client((struct sockaddr_in *)addr, NLM_VERS)) != NULL) {
if ((cli = get_client(addr, NLM_VERS)) != NULL) {
timeo.tv_sec = 0; /* No timeout - not expecting response */
timeo.tv_usec = 0;
@ -257,7 +305,7 @@ transmit4_result(opcode, result, addr)
struct timeval timeo;
int success;
if ((cli = get_client((struct sockaddr_in *)addr, NLM_VERS4)) != NULL) {
if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
timeo.tv_sec = 0; /* No timeout - not expecting response */
timeo.tv_usec = 0;
@ -357,7 +405,7 @@ nlm_test_msg_1_svc(arg, rqstp)
{
nlm_testres res;
static char dummy;
struct sockaddr_in *addr;
struct sockaddr *addr;
CLIENT *cli;
int success;
struct timeval timeo;
@ -386,7 +434,7 @@ nlm_test_msg_1_svc(arg, rqstp)
* nlm_test has different result type to the other operations, so
* can't use transmit_result() in this case
*/
addr = svc_getcaller(rqstp->rq_xprt);
addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
if ((cli = get_client(addr, NLM_VERS)) != NULL) {
timeo.tv_sec = 0; /* No timeout - not expecting response */
timeo.tv_usec = 0;
@ -832,7 +880,7 @@ nlm4_test_msg_4_svc(arg, rqstp)
{
nlm4_testres res;
static char dummy;
struct sockaddr_in *addr;
struct sockaddr *addr;
CLIENT *cli;
int success;
struct timeval timeo;
@ -856,7 +904,7 @@ nlm4_test_msg_4_svc(arg, rqstp)
* nlm_test has different result type to the other operations, so
* can't use transmit4_result() in this case
*/
addr = svc_getcaller(rqstp->rq_xprt);
addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
timeo.tv_sec = 0; /* No timeout - not expecting response */
timeo.tv_usec = 0;
@ -1049,7 +1097,7 @@ nlm4_granted_msg_4_svc(arg, rqstp)
res.cookie = arg->cookie;
res.stat.stat = nlm4_granted;
transmit4_result(NLM4_GRANTED_RES, &res,
(struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return (NULL);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lockd.c,v 1.5 2000/06/07 14:34:40 bouyer Exp $ */
/* $NetBSD: lockd.c,v 1.6 2000/06/09 14:00:53 fvdl Exp $ */
/*
* Copyright (c) 1995
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: lockd.c,v 1.5 2000/06/07 14:34:40 bouyer Exp $");
__RCSID("$NetBSD: lockd.c,v 1.6 2000/06/09 14:00:53 fvdl Exp $");
#endif
/*
@ -45,6 +45,9 @@ __RCSID("$NetBSD: lockd.c,v 1.5 2000/06/07 14:34:40 bouyer Exp $");
* The actual program logic is in the file lock_proc.c
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
@ -54,6 +57,7 @@ __RCSID("$NetBSD: lockd.c,v 1.5 2000/06/07 14:34:40 bouyer Exp $");
#include <string.h>
#include <unistd.h>
#include <util.h>
#include <netconfig.h>
#include <rpc/rpc.h>
#include <rpcsvc/sm_inter.h>
@ -75,15 +79,18 @@ void usage __P((void));
void sigalarm_handler __P((int));
char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
int
main(argc, argv)
int argc;
char **argv;
{
SVCXPRT *transp;
int ch;
int ch, i, maxindex, s;
struct sigaction sigchild, sigalarm;
int grace_period = 30;
struct netconfig *nconf;
while ((ch = getopt(argc, argv, "d:g:")) != (-1)) {
switch (ch) {
@ -108,55 +115,53 @@ main(argc, argv)
}
}
(void)pmap_unset(NLM_PROG, NLM_SM);
(void)pmap_unset(NLM_PROG, NLM_VERS);
(void)pmap_unset(NLM_PROG, NLM_VERSX);
(void)pmap_unset(NLM_PROG, NLM_VERS4);
(void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
(void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
(void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
(void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
transp = svcudp_create(RPC_ANYSOCK);
/*
* Check if IPv6 support is present.
*/
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0)
maxindex = 2;
else {
close(s);
maxindex = 4;
}
for (i = 0; i < maxindex; i++) {
nconf = getnetconfigent(transports[i]);
if (nconf == NULL)
errx(1, "cannot get udp netconf.");
transp = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
if (transp == NULL) {
errx(1, "cannot create udp service.");
errx(1, "cannot create %s service.", transports[i]);
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_SM,
nlm_prog_0, IPPROTO_UDP)) {
errx(1, "unable to register (NLM_PROG, NLM_SM, udp).");
if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_SM, %s)",
transports[i]);
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_VERS,
nlm_prog_1, IPPROTO_UDP)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS, udp).");
if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)",
transports[i]);
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_VERSX,
nlm_prog_3, IPPROTO_UDP)) {
errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp).");
if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)",
transports[i]);
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_VERS4,
nlm_prog_4, IPPROTO_UDP)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS4, udp).");
if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)",
transports[i]);
/* NOTREACHED */
}
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
errx(1, "cannot create tcp service.");
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_VERS,
nlm_prog_1, IPPROTO_TCP)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp).");
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_VERSX,
nlm_prog_3, IPPROTO_TCP)) {
errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp).");
/* NOTREACHED */
}
if (!svc_register(transp, NLM_PROG, NLM_VERS4,
nlm_prog_4, IPPROTO_TCP)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS4, tcp).");
/* NOTREACHED */
freenetconfigent(nconf);
}
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: lockd_lock.c,v 1.1 2000/06/07 14:34:40 bouyer Exp $ */
/* $NetBSD: lockd_lock.c,v 1.2 2000/06/09 14:00:53 fvdl Exp $ */
/*
* Copyright (c) 2000 Manuel Bouyer.
@ -166,7 +166,7 @@ getlock(lckarg, rqstp, flags)
lckarg->alock.fh.n_len, (int)sizeof(fhandle_t));
}
memcpy(&newfl->filehandle, lckarg->alock.fh.n_bytes, sizeof(fhandle_t));
newfl->addr = (struct sockaddr *)svc_getcaller(rqstp->rq_xprt);
newfl->addr = (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf;
newfl->client.exclusive = lckarg->exclusive;
newfl->client.svid = lckarg->alock.svid;
newfl->client.oh.n_bytes = malloc(lckarg->alock.oh.n_len);
@ -524,7 +524,7 @@ send_granted(fl, opcode)
static struct nlm_res retval;
static struct nlm4_res retval4;
cli = get_client((struct sockaddr_in *)fl->addr,
cli = get_client(fl->addr,
(fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS);
if (cli == NULL) {
syslog(LOG_NOTICE, "failed to get CLIENT for %s",

View File

@ -1,4 +1,4 @@
/* $NetBSD: lockd_lock.h,v 1.1 2000/06/07 14:34:40 bouyer Exp $ */
/* $NetBSD: lockd_lock.h,v 1.2 2000/06/09 14:00:54 fvdl Exp $ */
/* Headers and function declarations for file-locking utilities */
@ -17,4 +17,4 @@ void notify __P((char *, int));
/* callbacks from lock_proc.c */
void transmit_result __P((int, nlm_res *, struct sockaddr *));
void transmit4_result __P((int, nlm4_res *, struct sockaddr *));
CLIENT *get_client __P((struct sockaddr_in *, u_long));
CLIENT *get_client __P((struct sockaddr *, rpcvers_t));

View File

@ -1,4 +1,4 @@
/* $NetBSD: stat_proc.c,v 1.5 2000/06/06 18:19:56 bouyer Exp $ */
/* $NetBSD: stat_proc.c,v 1.6 2000/06/09 14:02:12 fvdl Exp $ */
/*
* Copyright (c) 1995
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: stat_proc.c,v 1.5 2000/06/06 18:19:56 bouyer Exp $");
__RCSID("$NetBSD: stat_proc.c,v 1.6 2000/06/09 14:02:12 fvdl Exp $");
#endif
#include <errno.h>
@ -63,14 +63,16 @@ sm_stat_1_svc(arg, req)
struct svc_req *req;
{
static sm_stat_res res;
struct addrinfo *ai;
NO_ALARM;
if (debug)
syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name);
if (gethostbyname(arg->mon_name))
if (getaddrinfo(arg->mon_name, NULL, NULL, &ai) == 0) {
res.res_stat = stat_succ;
else {
freeaddrinfo(ai);
} else {
syslog(LOG_ERR, "invalid hostname to sm_stat: %s",
arg->mon_name);
res.res_stat = stat_fail;
@ -95,6 +97,7 @@ sm_mon_1_svc(arg, req)
struct svc_req *req;
{
static sm_stat_res res;
struct addrinfo *ai;
HostInfo *hp, h;
MonList *lp;
@ -113,12 +116,14 @@ sm_mon_1_svc(arg, req)
* Find existing host entry, or create one if not found. If
* find_host() fails, it will have logged the error already.
*/
if (!gethostbyname(arg->mon_id.mon_name)) {
if (getaddrinfo(arg->mon_id.mon_name, NULL, NULL, &ai) != 0) {
syslog(LOG_ERR, "Invalid hostname to sm_mon: %s",
arg->mon_id.mon_name);
return &res;
}
freeaddrinfo(ai);
if ((hp = find_host(arg->mon_id.mon_name, &h)) == NULL)
memset(hp = &h, 0, sizeof(h));

View File

@ -1,4 +1,4 @@
/* $NetBSD: statd.c,v 1.16 2000/06/09 09:57:29 bouyer Exp $ */
/* $NetBSD: statd.c,v 1.17 2000/06/09 14:02:13 fvdl Exp $ */
/*
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: statd.c,v 1.16 2000/06/09 09:57:29 bouyer Exp $");
__RCSID("$NetBSD: statd.c,v 1.17 2000/06/09 14:02:13 fvdl Exp $");
#endif
/* main() function for status monitor daemon. Some of the code in this */
@ -58,6 +58,7 @@ __RCSID("$NetBSD: statd.c,v 1.16 2000/06/09 09:57:29 bouyer Exp $");
#include <unistd.h>
#include <util.h>
#include <db.h>
#include <netconfig.h>
#include <rpc/rpc.h>
@ -96,7 +97,6 @@ main(argc, argv)
int argc;
char **argv;
{
SVCXPRT *transp;
int ch;
while ((ch = getopt(argc, argv, "d")) != (-1)) {
@ -111,26 +111,17 @@ main(argc, argv)
/* NOTREACHED */
}
}
(void)pmap_unset(SM_PROG, SM_VERS);
(void)rpcb_unset(SM_PROG, SM_VERS, NULL);
transp = svcudp_create(RPC_ANYSOCK);
if (transp == NULL) {
if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "udp")) {
errx(1, "cannot create udp service.");
/* NOTREACHED */
}
if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP)) {
errx(1, "unable to register (SM_PROG, SM_VERS, udp).");
/* NOTREACHED */
}
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
errx(1, "cannot create tcp service.");
/* NOTREACHED */
}
if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) {
errx(1, "unable to register (SM_PROG, SM_VERS, tcp).");
if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "tcp")) {
errx(1, "cannot create udp service.");
/* NOTREACHED */
}
init_file("/var/db/statd.status");
/*