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