2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: gethost.c,v 1.29.206.1 2004/03/06 08:15:30 marka Exp $ */
25 #include <lwres/net.h>
26 #include <lwres/netdb.h>
30 #define LWRES_ALIGNBYTES (sizeof(char *) - 1)
31 #define LWRES_ALIGN(p) \
32 (((unsigned long)(p) + LWRES_ALIGNBYTES) &~ LWRES_ALIGNBYTES)
34 static struct hostent *he = NULL;
35 static int copytobuf(struct hostent *, struct hostent *, char *, int);
38 lwres_gethostbyname(const char *name) {
41 lwres_freehostent(he);
43 he = lwres_getipnodebyname(name, AF_INET, 0, &lwres_h_errno);
48 lwres_gethostbyname2(const char *name, int af) {
50 lwres_freehostent(he);
52 he = lwres_getipnodebyname(name, af, 0, &lwres_h_errno);
57 lwres_gethostbyaddr(const char *addr, int len, int type) {
60 lwres_freehostent(he);
62 he = lwres_getipnodebyaddr(addr, len, type, &lwres_h_errno);
67 lwres_gethostent(void) {
69 lwres_freehostent(he);
75 lwres_sethostent(int stayopen) {
83 lwres_endhostent(void) {
90 lwres_gethostbyname_r(const char *name, struct hostent *resbuf,
91 char *buf, int buflen, int *error)
96 he = lwres_getipnodebyname(name, AF_INET, 0, error);
99 res = copytobuf(he, resbuf, buf, buflen);
100 lwres_freehostent(he);
109 lwres_gethostbyaddr_r(const char *addr, int len, int type,
110 struct hostent *resbuf, char *buf, int buflen,
116 he = lwres_getipnodebyaddr(addr, len, type, error);
119 res = copytobuf(he, resbuf, buf, buflen);
120 lwres_freehostent(he);
129 lwres_gethostent_r(struct hostent *resbuf, char *buf, int buflen, int *error) {
138 lwres_sethostent_r(int stayopen) {
146 lwres_endhostent_r(void) {
153 copytobuf(struct hostent *he, struct hostent *hptr, char *buf, int buflen) {
160 * Find out the amount of space required to store the answer.
162 nptr = 2; /* NULL ptrs */
163 len = (char *)LWRES_ALIGN(buf) - buf;
164 for (i = 0; he->h_addr_list[i]; i++, nptr++) {
167 for (i = 0; he->h_aliases[i]; i++, nptr++) {
168 len += strlen(he->h_aliases[i]) + 1;
170 len += strlen(he->h_name) + 1;
171 len += nptr * sizeof(char*);
178 * Copy address size and type.
180 hptr->h_addrtype = he->h_addrtype;
181 n = hptr->h_length = he->h_length;
183 ptr = (char **)LWRES_ALIGN(buf);
184 cp = (char *)LWRES_ALIGN(buf) + nptr * sizeof(char *);
189 hptr->h_addr_list = ptr;
190 for (i = 0; he->h_addr_list[i]; i++, ptr++) {
191 memcpy(cp, he->h_addr_list[i], n);
192 hptr->h_addr_list[i] = cp;
195 hptr->h_addr_list[i] = NULL;
199 * Copy official name.
201 n = strlen(he->h_name) + 1;
202 strcpy(cp, he->h_name);
209 hptr->h_aliases = ptr;
210 for (i = 0; he->h_aliases[i]; i++) {
211 n = strlen(he->h_aliases[i]) + 1;
212 strcpy(cp, he->h_aliases[i]);
213 hptr->h_aliases[i] = cp;
216 hptr->h_aliases[i] = NULL;