]>
Commit | Line | Data |
---|---|---|
1 | /*-\r | |
2 | * Copyright (c) 1985, 1988, 1993\r | |
3 | * The Regents of the University of California. All rights reserved.\r | |
4 | *\r | |
5 | * Portions copyright (c) 1999, 2000\r | |
6 | * Intel Corporation.\r | |
7 | * All rights reserved.\r | |
8 | * \r | |
9 | * Redistribution and use in source and binary forms, with or without\r | |
10 | * modification, are permitted provided that the following conditions\r | |
11 | * are met:\r | |
12 | * \r | |
13 | * 1. Redistributions of source code must retain the above copyright\r | |
14 | * notice, this list of conditions and the following disclaimer.\r | |
15 | * \r | |
16 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
17 | * notice, this list of conditions and the following disclaimer in the\r | |
18 | * documentation and/or other materials provided with the distribution.\r | |
19 | * \r | |
20 | * 3. All advertising materials mentioning features or use of this software\r | |
21 | * must display the following acknowledgement:\r | |
22 | * \r | |
23 | * This product includes software developed by the University of\r | |
24 | * California, Berkeley, Intel Corporation, and its contributors.\r | |
25 | * \r | |
26 | * 4. Neither the name of University, Intel Corporation, or their respective\r | |
27 | * contributors may be used to endorse or promote products derived from\r | |
28 | * this software without specific prior written permission.\r | |
29 | * \r | |
30 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r | |
31 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r | |
32 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r | |
33 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r | |
34 | * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r | |
35 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r | |
36 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r | |
37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r | |
38 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r | |
39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r | |
40 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r | |
41 | *\r | |
42 | * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r | |
43 | *\r | |
44 | * Permission to use, copy, modify, and distribute this software for any\r | |
45 | * purpose with or without fee is hereby granted, provided that the above\r | |
46 | * copyright notice and this permission notice appear in all copies, and that\r | |
47 | * the name of Digital Equipment Corporation not be used in advertising or\r | |
48 | * publicity pertaining to distribution of the document or software without\r | |
49 | * specific, written prior permission.\r | |
50 | *\r | |
51 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r | |
52 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r | |
53 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r | |
54 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r | |
55 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r | |
56 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r | |
57 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r | |
58 | * SOFTWARE.\r | |
59 | * -\r | |
60 | * --Copyright--\r | |
61 | */\r | |
62 | \r | |
63 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
64 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";\r | |
65 | static char rcsid[] = "$Id: gethostbyht.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r | |
66 | #endif /* LIBC_SCCS and not lint */\r | |
67 | \r | |
68 | #include <sys/param.h>\r | |
69 | #include <sys/socket.h>\r | |
70 | #include <netinet/in.h>\r | |
71 | #include <arpa/inet.h>\r | |
72 | #include <netdb.h>\r | |
73 | #include <stdio.h>\r | |
74 | #include <ctype.h>\r | |
75 | #include <errno.h>\r | |
76 | #include <string.h>\r | |
77 | #include <arpa/nameser.h> /* XXX */\r | |
78 | #include <resolv.h> /* XXX */\r | |
79 | #include "Socklib_internals.h"\r | |
80 | \r | |
81 | #define MAXALIASES 35\r | |
82 | \r | |
83 | static struct hostent host;\r | |
84 | static char *host_aliases[MAXALIASES];\r | |
85 | static char hostbuf[BUFSIZ+1];\r | |
86 | static FILE *hostf = NULL;\r | |
87 | static u_char host_addr[16]; /* IPv4 or IPv6 */\r | |
88 | static char *h_addr_ptrs[2];\r | |
89 | static int stayopen = 0;\r | |
90 | \r | |
91 | void\r | |
92 | _sethosthtent(int f)\r | |
93 | {\r | |
94 | if (!hostf)\r | |
95 | hostf = fopen(_PATH_HOSTS, "r" );\r | |
96 | else\r | |
97 | rewind(hostf);\r | |
98 | stayopen = f;\r | |
99 | }\r | |
100 | \r | |
101 | void\r | |
102 | _endhosthtent()\r | |
103 | {\r | |
104 | if (hostf && !stayopen) {\r | |
105 | (void) fclose(hostf);\r | |
106 | hostf = NULL;\r | |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | struct hostent *\r | |
111 | gethostent()\r | |
112 | {\r | |
113 | char *p;\r | |
114 | register char *cp, **q;\r | |
115 | int af, len;\r | |
116 | \r | |
117 | if (!hostf && ( NULL == (hostf = fopen(_PATH_HOSTS, "r" )))) {\r | |
118 | h_errno = NETDB_INTERNAL;\r | |
119 | return (NULL);\r | |
120 | }\r | |
121 | again:\r | |
122 | if ( NULL == (p = fgets(hostbuf, sizeof hostbuf, hostf))) {\r | |
123 | h_errno = HOST_NOT_FOUND;\r | |
124 | return (NULL);\r | |
125 | }\r | |
126 | if (*p == '#')\r | |
127 | goto again;\r | |
128 | if ( NULL == (cp = strpbrk(p, "#\n")))\r | |
129 | goto again;\r | |
130 | *cp = '\0';\r | |
131 | if ( NULL == (cp = strpbrk(p, " \t")))\r | |
132 | goto again;\r | |
133 | *cp++ = '\0';\r | |
134 | if (inet_pton(AF_INET6, p, host_addr) > 0) {\r | |
135 | af = AF_INET6;\r | |
136 | len = IN6ADDRSZ;\r | |
137 | } else if (inet_pton(AF_INET, p, host_addr) > 0) {\r | |
138 | if (_res.options & RES_USE_INET6) {\r | |
139 | _map_v4v6_address((char*)host_addr, (char*)host_addr);\r | |
140 | af = AF_INET6;\r | |
141 | len = IN6ADDRSZ;\r | |
142 | } else {\r | |
143 | af = AF_INET;\r | |
144 | len = INADDRSZ;\r | |
145 | }\r | |
146 | } else {\r | |
147 | goto again;\r | |
148 | }\r | |
149 | h_addr_ptrs[0] = (char *)host_addr;\r | |
150 | h_addr_ptrs[1] = NULL;\r | |
151 | host.h_addr_list = h_addr_ptrs;\r | |
152 | host.h_length = len;\r | |
153 | host.h_addrtype = af;\r | |
154 | while (*cp == ' ' || *cp == '\t')\r | |
155 | cp++;\r | |
156 | host.h_name = cp;\r | |
157 | q = host.h_aliases = host_aliases;\r | |
158 | if ((cp = strpbrk(cp, " \t\r")) != NULL)\r | |
159 | *cp++ = '\0';\r | |
160 | while (cp && *cp) {\r | |
161 | if (*cp == ' ' || *cp == '\t') {\r | |
162 | cp++;\r | |
163 | continue;\r | |
164 | }\r | |
165 | if (q < &host_aliases[MAXALIASES - 1])\r | |
166 | *q++ = cp;\r | |
167 | if ((cp = strpbrk(cp, " \t\r")) != NULL)\r | |
168 | *cp++ = '\0';\r | |
169 | }\r | |
170 | *q = NULL;\r | |
171 | h_errno = NETDB_SUCCESS;\r | |
172 | return (&host);\r | |
173 | }\r | |
174 | \r | |
175 | struct hostent *\r | |
176 | _gethostbyhtname(const char *name, int af)\r | |
177 | {\r | |
178 | register struct hostent *p;\r | |
179 | register char **cp;\r | |
180 | \r | |
181 | sethostent(0);\r | |
182 | while ((p = gethostent()) != NULL) {\r | |
183 | if (p->h_addrtype != af)\r | |
184 | continue;\r | |
185 | if (strcasecmp(p->h_name, name) == 0)\r | |
186 | break;\r | |
187 | for (cp = p->h_aliases; *cp != 0; cp++)\r | |
188 | if (strcasecmp(*cp, name) == 0)\r | |
189 | goto found;\r | |
190 | }\r | |
191 | found:\r | |
192 | endhostent();\r | |
193 | return (p);\r | |
194 | }\r | |
195 | \r | |
196 | struct hostent *\r | |
197 | _gethostbyhtaddr(const char *addr, int len, int af)\r | |
198 | {\r | |
199 | register struct hostent *p;\r | |
200 | \r | |
201 | sethostent(0);\r | |
202 | while ((p = gethostent()) != NULL)\r | |
203 | if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))\r | |
204 | break;\r | |
205 | endhostent();\r | |
206 | return (p);\r | |
207 | }\r |