gethost.c revision dafcb997e390efa4423883dafd100c975c4095d6
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (C) 2000, 2001 Internet Software Consortium.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Permission to use, copy, modify, and distribute this software for any
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * purpose with or without fee is hereby granted, provided that the above
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * copyright notice and this permission notice appear in all copies.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * PERFORMANCE OF THIS SOFTWARE.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/* $Id: gethost.c,v 1.30 2004/03/05 05:12:45 marka Exp $ */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <config.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <errno.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <string.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <lwres/net.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <lwres/netdb.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include "assert_p.h"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define LWRES_ALIGNBYTES (sizeof(char *) - 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define LWRES_ALIGN(p) \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (((unsigned long)(p) + LWRES_ALIGNBYTES) &~ LWRES_ALIGNBYTES)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic struct hostent *he = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int copytobuf(struct hostent *, struct hostent *, char *, int);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostbyname(const char *name) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (he != NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews lwres_freehostent(he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews he = lwres_getipnodebyname(name, AF_INET, 0, &lwres_h_errno);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostbyname2(const char *name, int af) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (he != NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews lwres_freehostent(he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews he = lwres_getipnodebyname(name, af, 0, &lwres_h_errno);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostbyaddr(const char *addr, int len, int type) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (he != NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews lwres_freehostent(he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews he = lwres_getipnodebyaddr(addr, len, type, &lwres_h_errno);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostent(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (he != NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews lwres_freehostent(he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_sethostent(int stayopen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Empty.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNUSED(stayopen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_endhostent(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Empty.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostbyname_r(const char *name, struct hostent *resbuf,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *buf, int buflen, int *error)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews{
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews struct hostent *he;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int res;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews he = lwres_getipnodebyname(name, AF_INET, 0, error);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (he == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews res = copytobuf(he, resbuf, buf, buflen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews lwres_freehostent(he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (res != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews errno = ERANGE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (resbuf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostbyaddr_r(const char *addr, int len, int type,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews struct hostent *resbuf, char *buf, int buflen,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int *error)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews{
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews struct hostent *he;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int res;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews he = lwres_getipnodebyaddr(addr, len, type, error);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (he == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews res = copytobuf(he, resbuf, buf, buflen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews lwres_freehostent(he);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (res != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews errno = ERANGE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (resbuf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct hostent *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_gethostent_r(struct hostent *resbuf, char *buf, int buflen, int *error) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNUSED(resbuf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNUSED(buf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNUSED(buflen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *error = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_sethostent_r(int stayopen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Empty.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNUSED(stayopen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewslwres_endhostent_r(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Empty.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewscopytobuf(struct hostent *he, struct hostent *hptr, char *buf, int buflen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *cp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char **ptr;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int i, n;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int nptr, len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Find out the amount of space required to store the answer.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews nptr = 2; /* NULL ptrs */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len = (char *)LWRES_ALIGN(buf) - buf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (i = 0; he->h_addr_list[i]; i++, nptr++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len += he->h_length;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (i = 0; he->h_aliases[i]; i++, nptr++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len += strlen(he->h_aliases[i]) + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len += strlen(he->h_name) + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len += nptr * sizeof(char*);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (len > buflen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (-1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copy address size and type.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_addrtype = he->h_addrtype;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews n = hptr->h_length = he->h_length;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ptr = (char **)LWRES_ALIGN(buf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cp = (char *)LWRES_ALIGN(buf) + nptr * sizeof(char *);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copy address list.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_addr_list = ptr;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (i = 0; he->h_addr_list[i]; i++, ptr++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews memcpy(cp, he->h_addr_list[i], n);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_addr_list[i] = cp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cp += n;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_addr_list[i] = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ptr++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copy official name.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews n = strlen(he->h_name) + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews strcpy(cp, he->h_name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_name = cp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cp += n;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copy aliases.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_aliases = ptr;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (i = 0; he->h_aliases[i]; i++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews n = strlen(he->h_aliases[i]) + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews strcpy(cp, he->h_aliases[i]);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_aliases[i] = cp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cp += n;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews hptr->h_aliases[i] = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews