]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 1 | /*-\r |
2 | * Copyright (c) 1994, Garrett Wollman\r | |
3 | *\r | |
4 | * Redistribution and use in source and binary forms, with or without\r | |
5 | * modification, are permitted provided that the following conditions\r | |
6 | * are met:\r | |
7 | * 1. Redistributions of source code must retain the above copyright\r | |
8 | * notice, this list of conditions and the following disclaimer.\r | |
9 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
10 | * notice, this list of conditions and the following disclaimer in the\r | |
11 | * documentation and/or other materials provided with the distribution.\r | |
12 | *\r | |
13 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND\r | |
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
23 | * SUCH DAMAGE.\r | |
24 | */\r | |
25 | \r | |
26 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
27 | static char sccsid[] = "@(#)$Id: gethostnamadr.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r | |
28 | static char rcsid[] = "$Id: gethostnamadr.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r | |
29 | #endif /* LIBC_SCCS and not lint */\r | |
30 | \r | |
31 | #include <sys/param.h>\r | |
32 | #include <sys/socket.h>\r | |
33 | #include <netinet/in.h>\r | |
34 | #include <arpa/inet.h>\r | |
35 | #include <netdb.h>\r | |
36 | #include <stdio.h>\r | |
37 | #include <ctype.h>\r | |
38 | #include <errno.h>\r | |
39 | #include <paths.h>\r | |
40 | #include <string.h>\r | |
41 | #include <arpa/nameser.h> /* XXX hack for _res */\r | |
42 | #include <resolv.h> /* XXX hack for _res */\r | |
43 | \r | |
44 | #include "Socklib_internals.h"\r | |
45 | \r | |
46 | \r | |
47 | enum service_type {\r | |
48 | SERVICE_NONE = 0,\r | |
49 | SERVICE_BIND,\r | |
50 | SERVICE_HOSTS,\r | |
51 | SERVICE_NIS };\r | |
52 | #define SERVICE_MAX SERVICE_NIS\r | |
53 | \r | |
54 | static struct {\r | |
55 | const char *name;\r | |
56 | enum service_type type;\r | |
57 | } service_names[] = {\r | |
58 | { "hosts", SERVICE_HOSTS },\r | |
59 | { _PATH_HOSTS, SERVICE_HOSTS },\r | |
60 | { "hosttable", SERVICE_HOSTS },\r | |
61 | { "htable", SERVICE_HOSTS },\r | |
62 | { "bind", SERVICE_BIND },\r | |
63 | { "dns", SERVICE_BIND },\r | |
64 | { "domain", SERVICE_BIND },\r | |
65 | { "yp", SERVICE_NIS },\r | |
66 | { "yellowpages", SERVICE_NIS },\r | |
67 | { "nis", SERVICE_NIS },\r | |
68 | { 0, SERVICE_NONE }\r | |
69 | };\r | |
70 | \r | |
71 | static enum service_type service_order[SERVICE_MAX + 1];\r | |
72 | static int service_done = 0;\r | |
73 | \r | |
74 | static enum service_type\r | |
75 | get_service_name(const char *name) {\r | |
76 | int i;\r | |
77 | for(i = 0; service_names[i].type != SERVICE_NONE; i++) {\r | |
78 | if(!strcasecmp(name, service_names[i].name)) {\r | |
79 | return service_names[i].type;\r | |
80 | }\r | |
81 | }\r | |
82 | return SERVICE_NONE;\r | |
83 | }\r | |
84 | \r | |
85 | static void\r | |
86 | init_services()\r | |
87 | {\r | |
88 | char *cp, *p, buf[BUFSIZ];\r | |
89 | register int cc = 0;\r | |
90 | FILE *fd;\r | |
91 | \r | |
92 | if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) {\r | |
93 | /* make some assumptions */\r | |
94 | service_order[0] = SERVICE_HOSTS;\r | |
95 | service_order[1] = SERVICE_BIND;\r | |
96 | service_order[2] = SERVICE_NONE;\r | |
97 | } else {\r | |
98 | while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {\r | |
99 | if(buf[0] == '#')\r | |
100 | continue;\r | |
101 | \r | |
102 | p = buf;\r | |
103 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')\r | |
104 | ;\r | |
105 | if (cp == NULL)\r | |
106 | continue;\r | |
107 | do {\r | |
108 | if (isalpha(cp[0])) {\r | |
109 | service_order[cc] = get_service_name(cp);\r | |
110 | if(service_order[cc] != SERVICE_NONE)\r | |
111 | cc++;\r | |
112 | }\r | |
113 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')\r | |
114 | ;\r | |
115 | } while(cp != NULL && cc < SERVICE_MAX);\r | |
116 | }\r | |
117 | service_order[cc] = SERVICE_NONE;\r | |
118 | fclose(fd);\r | |
119 | }\r | |
120 | service_done = 1;\r | |
121 | }\r | |
122 | \r | |
123 | struct hostent *\r | |
124 | gethostbyname(const char *name)\r | |
125 | {\r | |
126 | struct hostent *hp;\r | |
127 | \r | |
128 | if (_res.options & RES_USE_INET6) { /* XXX */\r | |
129 | hp = gethostbyname2(name, AF_INET6); /* XXX */\r | |
130 | if (hp) /* XXX */\r | |
131 | return (hp); /* XXX */\r | |
132 | } /* XXX */\r | |
133 | return (gethostbyname2(name, AF_INET));\r | |
134 | }\r | |
135 | \r | |
136 | struct hostent *\r | |
137 | gethostbyname2(const char *name, int type)\r | |
138 | {\r | |
139 | struct hostent *hp = 0;\r | |
140 | int nserv = 0;\r | |
141 | \r | |
142 | if (!service_done)\r | |
143 | init_services();\r | |
144 | \r | |
145 | while (!hp) {\r | |
146 | switch (service_order[nserv]) {\r | |
147 | case SERVICE_NONE:\r | |
148 | return NULL;\r | |
149 | case SERVICE_HOSTS:\r | |
150 | hp = _gethostbyhtname(name, type);\r | |
151 | break;\r | |
152 | case SERVICE_BIND:\r | |
153 | hp = _gethostbydnsname(name, type);\r | |
154 | break;\r | |
155 | case SERVICE_NIS:\r | |
156 | hp = _gethostbynisname(name, type);\r | |
157 | break;\r | |
158 | }\r | |
159 | nserv++;\r | |
160 | }\r | |
161 | return hp;\r | |
162 | }\r | |
163 | \r | |
164 | struct hostent *\r | |
165 | gethostbyaddr(const char *addr, socklen_t len, int type)\r | |
166 | {\r | |
167 | struct hostent *hp = 0;\r | |
168 | int nserv = 0;\r | |
169 | \r | |
170 | if (!service_done)\r | |
171 | init_services();\r | |
172 | \r | |
173 | while (!hp) {\r | |
174 | switch (service_order[nserv]) {\r | |
175 | case SERVICE_NONE:\r | |
176 | return 0;\r | |
177 | case SERVICE_HOSTS:\r | |
178 | hp = _gethostbyhtaddr(addr, len, type);\r | |
179 | break;\r | |
180 | case SERVICE_BIND:\r | |
181 | hp = _gethostbydnsaddr(addr, len, type);\r | |
182 | break;\r | |
183 | case SERVICE_NIS:\r | |
184 | hp = _gethostbynisaddr(addr, len, type);\r | |
185 | break;\r | |
186 | }\r | |
187 | nserv++;\r | |
188 | }\r | |
189 | return hp;\r | |
190 | }\r | |
191 | \r | |
192 | #ifdef _THREAD_SAFE\r | |
193 | struct hostent_data;\r | |
194 | \r | |
195 | /*\r | |
196 | * Temporary function (not thread safe)\r | |
197 | */\r | |
198 | int gethostbyaddr_r(const char *addr, int len, int type,\r | |
199 | struct hostent *result, struct hostent_data *buffer)\r | |
200 | {\r | |
201 | struct hostent *hp;\r | |
202 | int ret;\r | |
203 | if ((hp = gethostbyaddr(addr, len, type)) == NULL) {\r | |
204 | ret = -1;\r | |
205 | } else {\r | |
206 | memcpy(result, hp, sizeof(struct hostent));\r | |
207 | ret = 0;\r | |
208 | }\r | |
209 | return(ret);\r | |
210 | }\r | |
211 | #endif\r | |
212 | \r | |
213 | void\r | |
214 | sethostent(int stayopen)\r | |
215 | {\r | |
216 | _sethosthtent(stayopen);\r | |
217 | _sethostdnsent(stayopen);\r | |
218 | }\r | |
219 | \r | |
220 | void\r | |
221 | endhostent()\r | |
222 | {\r | |
223 | _endhosthtent();\r | |
224 | _endhostdnsent();\r | |
225 | }\r |