]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/gethostbyht.c
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Portions copyright (c) 1999, 2000
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley, Intel Corporation, and its contributors.
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
44 * Permission to use, copy, modify, and distribute this software for any
45 * purpose with or without fee is hereby granted, provided that the above
46 * copyright notice and this permission notice appear in all copies, and that
47 * the name of Digital Equipment Corporation not be used in advertising or
48 * publicity pertaining to distribution of the document or software without
49 * specific, written prior permission.
51 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
52 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
54 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
63 #if defined(LIBC_SCCS) && !defined(lint)
64 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
65 static char rcsid
[] = "$Id: gethostbyht.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";
66 #endif /* LIBC_SCCS and not lint */
68 #include <sys/param.h>
69 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
77 #include <arpa/nameser.h> /* XXX */
78 #include <resolv.h> /* XXX */
79 #include "Socklib_internals.h"
83 static struct hostent host
;
84 static char *host_aliases
[MAXALIASES
];
85 static char hostbuf
[BUFSIZ
+1];
86 static FILE *hostf
= NULL
;
87 static u_char host_addr
[16]; /* IPv4 or IPv6 */
88 static char *h_addr_ptrs
[2];
89 static int stayopen
= 0;
95 hostf
= fopen(_PATH_HOSTS
, "r" );
104 if (hostf
&& !stayopen
) {
105 (void) fclose(hostf
);
114 register char *cp
, **q
;
117 if (!hostf
&& ( NULL
== (hostf
= fopen(_PATH_HOSTS
, "r" )))) {
118 h_errno
= NETDB_INTERNAL
;
122 if ( NULL
== (p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
123 h_errno
= HOST_NOT_FOUND
;
128 if ( NULL
== (cp
= strpbrk(p
, "#\n")))
131 if ( NULL
== (cp
= strpbrk(p
, " \t")))
134 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
137 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
138 if (_res
.options
& RES_USE_INET6
) {
139 _map_v4v6_address((char*)host_addr
, (char*)host_addr
);
149 h_addr_ptrs
[0] = (char *)host_addr
;
150 h_addr_ptrs
[1] = NULL
;
151 host
.h_addr_list
= h_addr_ptrs
;
153 host
.h_addrtype
= af
;
154 while (*cp
== ' ' || *cp
== '\t')
157 q
= host
.h_aliases
= host_aliases
;
158 if ((cp
= strpbrk(cp
, " \t\r")) != NULL
)
161 if (*cp
== ' ' || *cp
== '\t') {
165 if (q
< &host_aliases
[MAXALIASES
- 1])
167 if ((cp
= strpbrk(cp
, " \t\r")) != NULL
)
171 h_errno
= NETDB_SUCCESS
;
176 _gethostbyhtname(const char *name
, int af
)
178 register struct hostent
*p
;
182 while ((p
= gethostent()) != NULL
) {
183 if (p
->h_addrtype
!= af
)
185 if (strcasecmp(p
->h_name
, name
) == 0)
187 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
188 if (strcasecmp(*cp
, name
) == 0)
197 _gethostbyhtaddr(const char *addr
, int len
, int af
)
199 register struct hostent
*p
;
202 while ((p
= gethostent()) != NULL
)
203 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))