/*
 * 5799-WZQ (C) COPYRIGHT = NONE
 * LICENSED MATERIALS - PROPERTY OF IBM
 */
/* $Header:getnetent.c 12.0$ */
/* $ACIS:getnetent.c 12.0$ */
/* $Source: /ibm/acis/usr/src/lib/libc/net/yp/RCS/getnetent.c,v $ */

#ifndef lint
static char *rcsid = "$Header:getnetent.c 12.0$";
#endif

#include <sys/nfs_defines.h>

/* @(#)getnetent.c	1.5 87/09/11 3.2/4.3NFSSRC */
/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getnetent.c	5.3 (Berkeley) 5/19/86";
#endif LIBC_SCCS and not lint

#ifdef VFS

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "../../yp/ypclnt.h"
#include <netinet/in.h>
#include <arpa/inet.h>

/*
 * Internet version.
 */
#define	MAXALIASES	35
#define	MAXADDRSIZE	14

static char domain[256];
static char *current = NULL;	/* current entry, analogous to netf */
static int currentlen;
static struct netent *interpret();
struct netent *getnetent();
char *inet_ntoa();
static char *any();
static char NETDB[] = "/etc/networks";
static FILE *netf = NULL;
static int usingyellow;		/* are yellow pages up? */
static char *nettoa();
static char line[BUFSIZ+1];
static struct netent net;
static int _net_stayopen;

struct netent *
getnetbyaddr(net, type)
{
	register struct netent *p;
	int reason;
	char *adrstr, *val;
	int vallen;

	setnetent(_net_stayopen);
	if (!usingyellow) {
		while (p = getnetent()) {
			if (p->n_addrtype == type && p->n_net == net)
				break;
		}
	}
	else {
		adrstr = nettoa(net);
		if (reason = yp_match(domain, "networks.byaddr",
		    adrstr, strlen(adrstr), &val, &vallen)) {
#ifdef DEBUG
			fprintf(stderr, "reason yp_first failed is %d\n",
			    reason);
#endif
			p = NULL;
		    }
		else {
			p = interpret(val, vallen);
			free(val);
		}
	}
	if (!_net_stayopen)
		endnetent();
	return (p);
}

struct netent *
getnetbyname(name)
	register char *name;
{
	register struct netent *p;
	register char **cp;
	int reason;
	char *val;
	int vallen;

	setnetent(_net_stayopen);
	if (!usingyellow) {
		while (p = getnetent()) {
			if (strcmp(p->n_name, name) == 0)
				break;
			for (cp = p->n_aliases; *cp != 0; cp++)
				if (strcmp(*cp, name) == 0)
					goto found;
		}
	}
	else {
		if (reason = yp_match(domain, "networks.byname",
		    name, strlen(name), &val, &vallen)) {
#ifdef DEBUG
			fprintf(stderr, "reason yp_first failed is %d\n",
			    reason);
#endif
			p = NULL;
		    }
		else {
			p = interpret(val, vallen);
			free(val);
		}
	}
found:
	if (!_net_stayopen)
		endnetent();
	return (p);
}

setnetent(f)
	int f;
{
	if (netf == NULL)
		netf = fopen(NETDB, "r" );
	else
		rewind(netf);
	if (current)
		free(current);
	current = NULL;
	_net_stayopen |= f;
	yellowup(1);	/* recompute whether yellow pages are up */
}

endnetent()
{
	if (current) {
		free(current);
		current = NULL;
	}
	if (netf) {
		fclose(netf);
		netf = NULL;
	}
	_net_stayopen = 0;
}

struct netent *
getnetent()
{
	int reason;
	char *key, *val;
	int keylen, vallen;
	static char line1[BUFSIZ+1];
	struct netent *np;

	yellowup(0);
	if (!usingyellow) {
		if (netf == NULL && (netf = fopen(NETDB, "r")) == NULL)
			return (NULL);
	        if (fgets(line1, BUFSIZ, netf) == NULL)
			return (NULL);
		return interpret(line1, strlen(line1));
	}
	if (current == NULL) {
		if (reason =  yp_first(domain, "networks.byaddr",
		    &key, &keylen, &val, &vallen)) {
#ifdef DEBUG
			fprintf(stderr, "reason yp_first failed is %d\n",
			    reason);
#endif
			return NULL;
		    }
	}
	else {
		if (reason = yp_next(domain, "networks.byaddr",
		    current, currentlen, &key, &keylen, &val, &vallen)) {
#ifdef DEBUG
			fprintf(stderr, "reason yp_next failed is %d\n",
			    reason);
#endif
			return NULL;
		}
	}
	if (current)
		free(current);
	current = key;
	currentlen = keylen;
	np = interpret(val, vallen);
	free(val);
	return (np);
}

static struct netent *
interpret(val, len)
{
	static char *net_aliases[MAXALIASES];
	static struct netent net;
	static char line[BUFSIZ+1];
	char *p;
	register char *cp, **q;

	strncpy(line, val, len);
	p = line;
	line[len] = '\n';
	if (*p == '#')
		return (getnetent());
	cp = any(p, "#\n");
	if (cp == NULL)
		return (getnetent());
	*cp = '\0';
	net.n_name = p;
	cp = any(p, " \t");
	if (cp == NULL)
		return (getnetent());
	*cp++ = '\0';
	while (*cp == ' ' || *cp == '\t')
		cp++;
	p = any(cp, " \t");
	if (p != NULL)
		*p++ = '\0';
	net.n_net = inet_network(cp);
	net.n_addrtype = AF_INET;
	q = net.n_aliases = net_aliases;
	if (p != NULL) 
		cp = p;
	while (cp && *cp) {
		if (*cp == ' ' || *cp == '\t') {
			cp++;
			continue;
		}
		if (q < &net_aliases[MAXALIASES - 1])
			*q++ = cp;
		cp = any(cp, " \t");
		if (cp != NULL)
			*cp++ = '\0';
	}
	*q = NULL;
	return (&net);
}

static char *
any(cp, match)
	register char *cp;
	char *match;
{
	register char *mp, c;

	while (c = *cp) {
		for (mp = match; *mp; mp++)
			if (*mp == c)
				return (cp);
		cp++;
	}
	return ((char *)0);
}

/* 
 * check to see if yellow pages are up, and store that fact in usingyellow.
 * The check is performed once at startup and thereafter if flag is set
 */
static
yellowup(flag)
{
	static int firsttime = 1;
	char *key, *val;
	int keylen, vallen;

	if (firsttime || flag) {
		firsttime = 0;
		if (domain[0] == 0) {
			if (getdomainname(domain, sizeof(domain)) < 0) {
				domain[0] = '\0';   /* missing system call implies that */
				usingyellow = 0;    /*   we're not usingyellow */
				return;
			}
		}
		usingyellow = !yp_bind(domain);
	}	
}

static
char *
nettoa(net)
	unsigned net;
{
	static char buf[10];
	char *p, *index(), *rindex();
	struct in_addr in;
	int addr;

	in = inet_makeaddr(net, INADDR_ANY);
	addr = in.s_addr;
	strcpy(buf, inet_ntoa(in));
	if (IN_CLASSA(htonl(addr))) {
		p = index(buf, '.');
		if (p == NULL)
			return NULL;
		*p = 0;
	}
	else if (IN_CLASSB(htonl(addr))) {
		p = index(buf, '.');
		if (p == NULL)
			return NULL;
		p = index(p+1, '.');
		if (p == NULL)
			return NULL;
		*p = 0;
	}
	else if (IN_CLASSC(htonl(addr))) {
		p = rindex(buf, '.');
		if (p == NULL)
			return NULL;
		*p = 0;
	}
	return buf;
}

#endif VFS
