]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 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 | /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro\r | |
63 | * Dep. Matematica Universidade de Coimbra, Portugal, Europe\r | |
64 | *\r | |
65 | * Permission to use, copy, modify, and distribute this software for any\r | |
66 | * purpose with or without fee is hereby granted, provided that the above\r | |
67 | * copyright notice and this permission notice appear in all copies.\r | |
68 | */\r | |
69 | \r | |
70 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
71 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";\r | |
72 | static char rcsid[] = "$Id: getnetbydns.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r | |
73 | #endif /* LIBC_SCCS and not lint */\r | |
74 | \r | |
75 | #include <sys/param.h>\r | |
76 | #include <sys/socket.h>\r | |
77 | #include <netinet/in.h>\r | |
78 | #include <arpa/inet.h>\r | |
79 | #include <arpa/nameser.h>\r | |
80 | \r | |
81 | #include <stdio.h>\r | |
82 | #include <netdb.h>\r | |
83 | #include <resolv.h>\r | |
84 | #include <ctype.h>\r | |
85 | #include <errno.h>\r | |
86 | #include <string.h>\r | |
87 | #include <unistd.h>\r | |
88 | #ifdef _ORG_FREEBSD_\r | |
89 | #include <syslog.h>\r | |
90 | #endif\r | |
91 | \r | |
92 | #include "res_config.h"\r | |
93 | #include "Socklib_internals.h"\r | |
94 | \r | |
95 | extern int h_errno;\r | |
96 | \r | |
97 | #define BYADDR 0\r | |
98 | #define BYNAME 1\r | |
99 | #define MAXALIASES 35\r | |
100 | \r | |
101 | #if PACKETSZ > 1024\r | |
102 | #define MAXPACKET PACKETSZ\r | |
103 | #else\r | |
104 | #define MAXPACKET 1024\r | |
105 | #endif\r | |
106 | \r | |
107 | typedef union {\r | |
108 | HEADER hdr;\r | |
109 | u_char buf[MAXPACKET];\r | |
110 | } querybuf;\r | |
111 | \r | |
112 | typedef union {\r | |
113 | long al;\r | |
114 | char ac;\r | |
115 | } align;\r | |
116 | \r | |
117 | static struct netent *\r | |
118 | getnetanswer(querybuf *answer, int anslen, int net_i)\r | |
119 | {\r | |
120 | \r | |
121 | register HEADER *hp;\r | |
122 | register u_char *cp;\r | |
123 | register int n;\r | |
124 | u_char *eom;\r | |
125 | int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;\r | |
126 | char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];\r | |
127 | char *in, *st, *pauxt, *bp, **ap;\r | |
128 | char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;\r | |
129 | static struct netent net_entry;\r | |
130 | static char *net_aliases[MAXALIASES], netbuf[PACKETSZ];\r | |
131 | \r | |
132 | /*\r | |
133 | * find first satisfactory answer\r | |
134 | *\r | |
135 | * answer --> +------------+ ( MESSAGE )\r | |
136 | * | Header |\r | |
137 | * +------------+\r | |
138 | * | Question | the question for the name server\r | |
139 | * +------------+\r | |
140 | * | Answer | RRs answering the question\r | |
141 | * +------------+\r | |
142 | * | Authority | RRs pointing toward an authority\r | |
143 | * | Additional | RRs holding additional information\r | |
144 | * +------------+\r | |
145 | */\r | |
146 | eom = answer->buf + anslen;\r | |
147 | hp = &answer->hdr;\r | |
148 | ancount = ntohs(hp->ancount); /* #/records in the answer section */\r | |
149 | qdcount = ntohs(hp->qdcount); /* #/entries in the question section */\r | |
150 | bp = netbuf;\r | |
151 | buflen = sizeof(netbuf);\r | |
152 | cp = answer->buf + HFIXEDSZ;\r | |
153 | if (!qdcount) {\r | |
154 | if (hp->aa)\r | |
155 | h_errno = HOST_NOT_FOUND;\r | |
156 | else\r | |
157 | h_errno = TRY_AGAIN;\r | |
158 | return (NULL);\r | |
159 | }\r | |
160 | while (qdcount-- > 0)\r | |
161 | cp += __dn_skipname(cp, eom) + QFIXEDSZ;\r | |
162 | ap = net_aliases;\r | |
163 | *ap = NULL;\r | |
164 | net_entry.n_aliases = net_aliases;\r | |
165 | haveanswer = 0;\r | |
166 | while (--ancount >= 0 && cp < eom) {\r | |
167 | n = dn_expand(answer->buf, eom, cp, bp, buflen);\r | |
168 | if ((n < 0) || !res_dnok(bp))\r | |
169 | break;\r | |
170 | cp += n;\r | |
171 | ans[0] = '\0';\r | |
172 | (void)strncpy(&ans[0], bp, sizeof(ans) - 1);\r | |
173 | ans[sizeof(ans) - 1] = '\0';\r | |
174 | GETSHORT(type, cp);\r | |
175 | GETSHORT(class, cp);\r | |
176 | cp += INT32SZ; /* TTL */\r | |
177 | GETSHORT(n, cp);\r | |
178 | if (class == C_IN && type == T_PTR) {\r | |
179 | n = dn_expand(answer->buf, eom, cp, bp, buflen);\r | |
180 | if ((n < 0) || !res_hnok(bp)) {\r | |
181 | cp += n;\r | |
182 | return (NULL);\r | |
183 | }\r | |
184 | cp += n; \r | |
185 | *ap++ = bp;\r | |
186 | bp += strlen(bp) + 1;\r | |
187 | net_entry.n_addrtype =\r | |
188 | (class == C_IN) ? AF_INET : AF_UNSPEC;\r | |
189 | haveanswer++;\r | |
190 | }\r | |
191 | }\r | |
192 | if (haveanswer) {\r | |
193 | *ap = NULL;\r | |
194 | switch (net_i) {\r | |
195 | case BYADDR:\r | |
196 | net_entry.n_name = *net_entry.n_aliases;\r | |
197 | net_entry.n_net = 0L;\r | |
198 | break;\r | |
199 | case BYNAME:\r | |
200 | in = *net_entry.n_aliases;\r | |
201 | net_entry.n_name = &ans[0];\r | |
202 | aux2[0] = '\0';\r | |
203 | for (i = 0; i < 4; i++) {\r | |
204 | for (st = in, nchar = 0;\r | |
205 | *st != '.';\r | |
206 | st++, nchar++)\r | |
207 | ;\r | |
208 | if (nchar != 1 || *in != '0' || flag) {\r | |
209 | flag = 1;\r | |
210 | (void)strncpy(paux1,\r | |
211 | (i==0) ? in : in-1,\r | |
212 | (i==0) ?nchar : nchar+1);\r | |
213 | paux1[(i==0) ? nchar : nchar+1] = '\0';\r | |
214 | pauxt = paux2;\r | |
215 | paux2 = strcat(paux1, paux2);\r | |
216 | paux1 = pauxt;\r | |
217 | }\r | |
218 | in = ++st;\r | |
219 | } \r | |
220 | net_entry.n_net = inet_network(paux2);\r | |
221 | break;\r | |
222 | }\r | |
223 | net_entry.n_aliases++;\r | |
224 | return (&net_entry);\r | |
225 | }\r | |
226 | h_errno = TRY_AGAIN;\r | |
227 | return (NULL);\r | |
228 | }\r | |
229 | \r | |
230 | struct netent *\r | |
231 | _getnetbydnsaddr(register unsigned long net, register int net_type)\r | |
232 | {\r | |
233 | unsigned int netbr[4];\r | |
234 | int nn, anslen;\r | |
235 | querybuf buf;\r | |
236 | char qbuf[MAXDNAME];\r | |
237 | unsigned long net2;\r | |
238 | struct netent *net_entry;\r | |
239 | \r | |
240 | if (net_type != AF_INET)\r | |
241 | return (NULL);\r | |
242 | \r | |
243 | for (nn = 4, net2 = net; net2; net2 >>= 8)\r | |
244 | netbr[--nn] = net2 & 0xff;\r | |
245 | switch (nn) {\r | |
246 | case 3: /* Class A */\r | |
247 | sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);\r | |
248 | break;\r | |
249 | case 2: /* Class B */\r | |
250 | sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);\r | |
251 | break;\r | |
252 | case 1: /* Class C */\r | |
253 | sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],\r | |
254 | netbr[1]);\r | |
255 | break;\r | |
256 | case 0: /* Class D - E */\r | |
257 | sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],\r | |
258 | netbr[1], netbr[0]);\r | |
259 | break;\r | |
260 | }\r | |
261 | anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));\r | |
262 | if (anslen < 0) {\r | |
263 | #ifdef DEBUG\r | |
264 | if (_res.options & RES_DEBUG)\r | |
265 | printf("res_query failed\n");\r | |
266 | #endif\r | |
267 | return (NULL);\r | |
268 | }\r | |
269 | net_entry = getnetanswer(&buf, anslen, BYADDR);\r | |
270 | if (net_entry) {\r | |
271 | unsigned u_net = net; /* maybe net should be unsigned ? */\r | |
272 | \r | |
273 | /* Strip trailing zeros */\r | |
274 | while ((u_net & 0xff) == 0 && u_net != 0)\r | |
275 | u_net >>= 8;\r | |
276 | net_entry->n_net = u_net;\r | |
277 | return (net_entry);\r | |
278 | }\r | |
279 | return (NULL);\r | |
280 | }\r | |
281 | \r | |
282 | struct netent *\r | |
283 | _getnetbydnsname(register const char *net)\r | |
284 | {\r | |
285 | int anslen;\r | |
286 | querybuf buf;\r | |
287 | char qbuf[MAXDNAME];\r | |
288 | \r | |
289 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r | |
290 | h_errno = NETDB_INTERNAL;\r | |
291 | return (NULL);\r | |
292 | }\r | |
293 | strncpy(qbuf, net, sizeof(qbuf) - 1);\r | |
294 | qbuf[sizeof(qbuf) - 1] = '\0';\r | |
295 | anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));\r | |
296 | if (anslen < 0) {\r | |
297 | #ifdef DEBUG\r | |
298 | if (_res.options & RES_DEBUG)\r | |
299 | printf("res_query failed\n");\r | |
300 | #endif\r | |
301 | return (NULL);\r | |
302 | }\r | |
303 | return getnetanswer(&buf, anslen, BYNAME);\r | |
304 | }\r | |
305 | \r | |
306 | void\r | |
307 | _setnetdnsent(int stayopen)\r | |
308 | {\r | |
309 | if (stayopen)\r | |
310 | _res.options |= RES_STAYOPEN | RES_USEVC;\r | |
311 | }\r | |
312 | \r | |
313 | void\r | |
314 | _endnetdnsent()\r | |
315 | {\r | |
316 | _res.options &= ~(RES_STAYOPEN | RES_USEVC);\r | |
317 | res_close();\r | |
318 | }\r |