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