getnetnamadr.c revision 1.7 1 /* $NetBSD: getnetnamadr.c,v 1.7 1998/12/05 13:17:55 pk Exp $ */
2
3 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
4 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 */
10 /*
11 * Copyright (c) 1983, 1993
12 * The Regents of the University of California. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43 #include <sys/cdefs.h>
44 #if defined(LIBC_SCCS) && !defined(lint)
45 #if 0
46 static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
47 static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
48 static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
49 #else
50 __RCSID("$NetBSD: getnetnamadr.c,v 1.7 1998/12/05 13:17:55 pk Exp $");
51 #endif
52 #endif /* LIBC_SCCS and not lint */
53
54 #include "namespace.h"
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <arpa/nameser.h>
61
62 #include <stdio.h>
63 #include <netdb.h>
64 #include <resolv.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #include <string.h>
68
69 #ifdef __weak_alias
70 __weak_alias(getnetbyaddr,_getnetbyaddr);
71 __weak_alias(getnetbyname,_getnetbyname);
72 #endif
73
74 extern int h_errno;
75
76 #if defined(mips) && defined(SYSTYPE_BSD43)
77 extern int errno;
78 #endif
79
80 /* XXX private header! */
81 struct netent *__getnetbyaddr __P((unsigned long net, int type));
82 struct netent *__getnetbyname __P((const char *name));
83
84 #define BYADDR 0
85 #define BYNAME 1
86 #define MAXALIASES 35
87
88 #if PACKETSZ > 1024
89 #define MAXPACKET PACKETSZ
90 #else
91 #define MAXPACKET 1024
92 #endif
93
94 typedef union {
95 HEADER hdr;
96 u_char buf[MAXPACKET];
97 } querybuf;
98
99 typedef union {
100 long al;
101 char ac;
102 } align;
103
104 static struct netent *getnetanswer __P((querybuf *, int, int));
105 static struct netent *
106 getnetanswer(answer, anslen, net_i)
107 querybuf *answer;
108 int anslen;
109 int net_i;
110 {
111
112 register HEADER *hp;
113 register u_char *cp;
114 register int n;
115 u_char *eom;
116 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
117 char aux1[30], aux2[30], ans[30], *in, *st, *pauxt, *bp, **ap,
118 *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
119 static struct netent net_entry;
120 static char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
121
122 /*
123 * find first satisfactory answer
124 *
125 * answer --> +------------+ ( MESSAGE )
126 * | Header |
127 * +------------+
128 * | Question | the question for the name server
129 * +------------+
130 * | Answer | RRs answering the question
131 * +------------+
132 * | Authority | RRs pointing toward an authority
133 * | Additional | RRs holding additional information
134 * +------------+
135 */
136 eom = answer->buf + anslen;
137 hp = &answer->hdr;
138 ancount = ntohs(hp->ancount); /* #/records in the answer section */
139 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
140 bp = netbuf;
141 buflen = sizeof(netbuf);
142 cp = answer->buf + HFIXEDSZ;
143 if (!qdcount) {
144 if (hp->aa)
145 h_errno = HOST_NOT_FOUND;
146 else
147 h_errno = TRY_AGAIN;
148 return (NULL);
149 }
150 while (qdcount-- > 0)
151 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
152 ap = net_aliases;
153 *ap = NULL;
154 net_entry.n_aliases = net_aliases;
155 haveanswer = 0;
156 while (--ancount >= 0 && cp < eom) {
157 n = dn_expand(answer->buf, eom, cp, bp, buflen);
158 if ((n < 0) || !res_dnok(bp))
159 break;
160 cp += n;
161 ans[0] = '\0';
162 (void)strcpy(&ans[0], bp);
163 GETSHORT(type, cp);
164 GETSHORT(class, cp);
165 cp += INT32SZ; /* TTL */
166 GETSHORT(n, cp);
167 if (class == C_IN && type == T_PTR) {
168 n = dn_expand(answer->buf, eom, cp, bp, buflen);
169 if ((n < 0) || !res_hnok(bp)) {
170 cp += n;
171 return (NULL);
172 }
173 cp += n;
174 *ap++ = bp;
175 bp += strlen(bp) + 1;
176 net_entry.n_addrtype =
177 (class == C_IN) ? AF_INET : AF_UNSPEC;
178 haveanswer++;
179 }
180 }
181 if (haveanswer) {
182 *ap = NULL;
183 switch (net_i) {
184 case BYADDR:
185 net_entry.n_name = *net_entry.n_aliases;
186 net_entry.n_net = 0L;
187 break;
188 case BYNAME:
189 in = *net_entry.n_aliases;
190 net_entry.n_name = &ans[0];
191 aux2[0] = '\0';
192 for (i = 0; i < 4; i++) {
193 for (st = in, nchar = 0;
194 *st != '.';
195 st++, nchar++)
196 ;
197 if (nchar != 1 || *in != '0' || flag) {
198 flag = 1;
199 (void)strncpy(paux1,
200 (i==0) ? in : in-1,
201 (size_t)((i==0) ? nchar : nchar+1));
202 paux1[(i==0) ? nchar : nchar+1] = '\0';
203 pauxt = paux2;
204 paux2 = strcat(paux1, paux2);
205 paux1 = pauxt;
206 }
207 in = ++st;
208 }
209 net_entry.n_net = inet_network(paux2);
210 break;
211 }
212 net_entry.n_aliases++;
213 return (&net_entry);
214 }
215 h_errno = TRY_AGAIN;
216 return (NULL);
217 }
218
219 struct netent *
220 getnetbyaddr(net, net_type)
221 register u_long net;
222 register int net_type;
223 {
224 unsigned int netbr[4];
225 int nn, anslen, i;
226 querybuf buf;
227 char qbuf[MAXDNAME];
228 unsigned long net2;
229 struct netent *net_entry;
230 char lookups[MAXDNSLUS];
231
232 if (net_type != AF_INET)
233 return (__getnetbyaddr(net, net_type));
234
235 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
236 h_errno = NETDB_INTERNAL;
237 return (NULL);
238 }
239
240 memcpy(lookups, _res.lookups, sizeof(lookups));
241 if (lookups[0] == '\0')
242 strncpy(lookups, "bf", sizeof(lookups));
243
244 net_entry = NULL;
245 for (i = 0; i < MAXDNSLUS && net_entry == NULL && lookups[i]; i++) {
246 switch (lookups[i]) {
247 case 'b':
248 for (nn = 4, net2 = net; net2; net2 >>= 8)
249 netbr[--nn] = (unsigned int)(net2 & 0xff);
250 switch (nn) {
251 default:
252 return (NULL);
253 case 3: /* Class A */
254 sprintf(qbuf, "0.0.0.%u.in-addr.arpa",
255 netbr[3]);
256 break;
257 case 2: /* Class B */
258 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa",
259 netbr[3], netbr[2]);
260 break;
261 case 1: /* Class C */
262 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa",
263 netbr[3], netbr[2], netbr[1]);
264 break;
265 case 0: /* Class D - E */
266 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
267 netbr[3], netbr[2], netbr[1], netbr[0]);
268 break;
269 }
270 anslen = res_query(qbuf, C_IN, T_PTR,
271 (u_char *)(void *)&buf, sizeof(buf));
272 if (anslen < 0) {
273 #ifdef DEBUG
274 if (_res.options & RES_DEBUG)
275 printf("res_query failed\n");
276 #endif
277 break;
278 }
279 net_entry = getnetanswer(&buf, anslen, BYADDR);
280 if (net_entry) {
281 /* maybe net should be unsigned? */
282 unsigned long u_net = net;
283
284 /* Strip trailing zeros */
285 while ((u_net & 0xff) == 0 && u_net != 0)
286 u_net >>= 8;
287 net_entry->n_net = u_net;
288 break;
289 }
290 break;
291
292 case 'f':
293 net_entry = __getnetbyaddr(net, net_type);
294 break;
295 }
296 }
297
298 return (net_entry);
299 }
300
301 struct netent *
302 getnetbyname(net)
303 register const char *net;
304 {
305 int anslen, i;
306 querybuf buf;
307 char qbuf[MAXDNAME];
308 struct netent *net_entry;
309 char lookups[MAXDNSLUS];
310
311 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
312 h_errno = NETDB_INTERNAL;
313 return (NULL);
314 }
315
316 memcpy(lookups, _res.lookups, sizeof(lookups));
317 if (lookups[0] == '\0')
318 strncpy(lookups, "bf", sizeof(lookups));
319
320 net_entry = NULL;
321 for (i = 0; i < MAXDNSLUS && net_entry == NULL && lookups[i]; i++) {
322 switch (lookups[i]) {
323 case 'b':
324 strcpy(&qbuf[0], net);
325 anslen = res_search(qbuf, C_IN, T_PTR,
326 (u_char *)(void *)&buf, sizeof(buf));
327 if (anslen < 0) {
328 #ifdef DEBUG
329 if (_res.options & RES_DEBUG)
330 printf("res_query failed\n");
331 #endif
332 break;
333 }
334 net_entry = getnetanswer(&buf, anslen, BYNAME);
335 break;
336
337 case 'f':
338 net_entry = __getnetbyname(net);
339 break;
340 }
341 }
342
343 return (net_entry);
344 }
345