#ifdef	NUT
/*
 * @(#) access.c 1.4 89/08/12
 * From -
 */
#endif	/*NUT*/
#ifndef      lint
static char sccsid[] = "@(#)M% 22.1 89/08/10";
#endif
/*
 * contents of this file taken from clients/xhost/xhost.c
 */
/***********************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

/*
 * Copyright (c) 1988 by Sun Microsystems, Inc.
 */

/*-
	Sun operating system interface routines

 */

/*

Copyright 1985, 1986, 1987 by the Massachusetts Institute of Technology

Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
M.I.T. makes no representations about the suitability of
this software for any purpose.  It is provided "as is"
without express or implied warranty.

*/

#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>

#undef NULL
#include <stdio.h>
#include <X11/X.h>

extern unsigned long inet_addr();

typedef struct {
    int af, xf;
} FamilyMap;

static FamilyMap familyMap[] = {
#ifdef AF_DECnet
    {AF_DECnet, FamilyDECnet},
#endif
#ifdef AF_CHAOS
    {AF_CHAOS, FamilyChaos},
#endif
#ifdef AF_INET
    {AF_INET, FamilyInternet}
#endif
};

#define FAMILIES ((sizeof familyMap)/(sizeof familyMap[0]))
static int XFamily(af)
    int af;
{
    int i;
    for (i=0; i<FAMILIES; i++)
	if (familyMap[i].af == af)
	    return familyMap[i].xf;
    return -1;
}

/*
 * get_hostname - Given an internet address, return a name (CHARON.MIT.EDU)
 * or a string representing the address (18.58.0.13) if the name cannot
 * be found.
 */

char * get_hostname(family, length, pAddr)
    int                 family;
    int                 length;        /* of bytes in pAddr */
    unsigned char       *pAddr;
{


  struct hostent *hp = NULL;
  char *inet_ntoa();
#ifdef DNETCONN
  struct nodeent *np;
  static char nodeaddr[16];
#endif /* DNETCONN */

  if (family == FamilyInternet) {
    if ((hp = gethostbyaddr ((char *) pAddr, length, AF_INET)))
      return (hp->h_name);
    else return (inet_ntoa(*((struct in_addr *)pAddr)));
  }
#ifdef DNETCONN
  if (family == FamilyDECnet) {
    if (np = getnodebyaddr(pAddr, length, AF_DECnet)) {
      sprintf(nodeaddr, "%s::", np->n_name);
    } else {
      sprintf(nodeaddr, "%s::", dnet_htoa(pAddr));
    }
    return(nodeaddr);
  }
#endif

  return (NULL);
}

/*
 * get_hostaddress - return an internet address given
 * either a name (CHARON.MIT.EDU) or a string with the raw
 * address (18.58.0.13)
 */
int
get_hostaddress(name, family, length, pAddr )
    char		*name;
    int                 *family;
    int                 *length;        /* of bytes in *pAddr */
    unsigned char       **pAddr;

{
  struct hostent *hp;
  static struct in_addr addr;	/* so we can point at it */
#ifdef DNETCONN
  struct dn_naddr *dnaddrp;
  struct nodeent *np;
  char *cp;
  static struct dn_naddr dnaddr;
#endif /* DNETCONN */

#ifdef DNETCONN
  if ((cp = index (name, ':')) && (*(cp + 1) == ':')) {
    *cp = '\0';
    *family = FamilyDECnet;
    if (dnaddrp = dnet_addr(name)) {
      dnaddr = *dnaddrp;
    } else {
      if ((np = getnodebyname (name)) == NULL)
	return (0);
      dnaddr.a_len = np->n_length;
      bcopy (np->n_addr, dnaddr.a_addr, np->n_length);
    }
    *length = dnaddr.a_len;
    *pAddr = (unsigned char *)&dnaddr.a_addr;
    return(1);	/* Found it */
  }
#endif /* DNETCONN */
  /*
   * First see if inet_addr() can grok the name; if so, then use it.
   */
  if ((addr.s_addr = inet_addr(name)) != -1) {
    *family = FamilyInternet;
    *length = sizeof(addr.s_addr);
    *pAddr = (unsigned char *)&addr.s_addr;
    return(1);	/* Found it */
  } 
  /*
   * Is it in the namespace?
   */
  else if (((hp = gethostbyname(name)) == (struct hostent *)NULL)
       || hp->h_addrtype != AF_INET) {
    return (0);		/* Sorry, you lose */
  } else {
    *family = XFamily(hp->h_addrtype);
    *length = hp->h_length;
    *pAddr = (unsigned char *)hp->h_addr;
    return (1);	/* Found it. */
  }
}
