getnetnamadr.c revision 1.9 1 /* $NetBSD: getnetnamadr.c,v 1.9 1999/01/16 11:43:08 lukem 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.9 1999/01/16 11:43:08 lukem 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 <ctype.h>
63 #include <errno.h>
64 #include <netdb.h>
65 #include <nsswitch.h>
66 #include <resolv.h>
67 #include <stdio.h>
68 #include <string.h>
69
70 #ifdef __weak_alias
71 __weak_alias(getnetbyaddr,_getnetbyaddr);
72 __weak_alias(getnetbyname,_getnetbyname);
73 #endif
74
75 extern int h_errno;
76 extern int _net_stayopen;
77
78 #if defined(mips) && defined(SYSTYPE_BSD43)
79 extern int errno;
80 #endif
81
82 int _getnetbyaddr __P((void *, void *, va_list));
83 int _getnetbyname __P((void *, void *, va_list));
84 int _dns_getnetbyaddr __P((void *, void *, va_list));
85 int _dns_getnetbyname __P((void *, void *, va_list));
86
87 #define BYADDR 0
88 #define BYNAME 1
89 #define MAXALIASES 35
90
91 #if PACKETSZ > 1024
92 #define MAXPACKET PACKETSZ
93 #else
94 #define MAXPACKET 1024
95 #endif
96
97 typedef union {
98 HEADER hdr;
99 u_char buf[MAXPACKET];
100 } querybuf;
101
102 typedef union {
103 long al;
104 char ac;
105 } align;
106
107 static struct netent *getnetanswer __P((querybuf *, int, int));
108 static struct netent *
109 getnetanswer(answer, anslen, net_i)
110 querybuf *answer;
111 int anslen;
112 int net_i;
113 {
114
115 HEADER *hp;
116 u_char *cp;
117 int n;
118 u_char *eom;
119 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
120 char aux1[30], aux2[30], ans[30], *in, *st, *pauxt, *bp, **ap,
121 *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
122 static struct netent net_entry;
123 static char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
124
125 /*
126 * find first satisfactory answer
127 *
128 * answer --> +------------+ ( MESSAGE )
129 * | Header |
130 * +------------+
131 * | Question | the question for the name server
132 * +------------+
133 * | Answer | RRs answering the question
134 * +------------+
135 * | Authority | RRs pointing toward an authority
136 * | Additional | RRs holding additional information
137 * +------------+
138 */
139 eom = answer->buf + anslen;
140 hp = &answer->hdr;
141 ancount = ntohs(hp->ancount); /* #/records in the answer section */
142 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
143 bp = netbuf;
144 buflen = sizeof(netbuf);
145 cp = answer->buf + HFIXEDSZ;
146 if (!qdcount) {
147 if (hp->aa)
148 h_errno = HOST_NOT_FOUND;
149 else
150 h_errno = TRY_AGAIN;
151 return (NULL);
152 }
153 while (qdcount-- > 0)
154 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
155 ap = net_aliases;
156 *ap = NULL;
157 net_entry.n_aliases = net_aliases;
158 haveanswer = 0;
159 while (--ancount >= 0 && cp < eom) {
160 n = dn_expand(answer->buf, eom, cp, bp, buflen);
161 if ((n < 0) || !res_dnok(bp))
162 break;
163 cp += n;
164 ans[0] = '\0';
165 (void)strcpy(&ans[0], bp);
166 GETSHORT(type, cp);
167 GETSHORT(class, cp);
168 cp += INT32SZ; /* TTL */
169 GETSHORT(n, cp);
170 if (class == C_IN && type == T_PTR) {
171 n = dn_expand(answer->buf, eom, cp, bp, buflen);
172 if ((n < 0) || !res_hnok(bp)) {
173 cp += n;
174 return (NULL);
175 }
176 cp += n;
177 *ap++ = bp;
178 bp += strlen(bp) + 1;
179 net_entry.n_addrtype =
180 (class == C_IN) ? AF_INET : AF_UNSPEC;
181 haveanswer++;
182 }
183 }
184 if (haveanswer) {
185 *ap = NULL;
186 switch (net_i) {
187 case BYADDR:
188 net_entry.n_name = *net_entry.n_aliases;
189 net_entry.n_net = 0L;
190 break;
191 case BYNAME:
192 in = *net_entry.n_aliases;
193 net_entry.n_name = &ans[0];
194 aux2[0] = '\0';
195 for (i = 0; i < 4; i++) {
196 for (st = in, nchar = 0;
197 *st != '.';
198 st++, nchar++)
199 ;
200 if (nchar != 1 || *in != '0' || flag) {
201 flag = 1;
202 (void)strncpy(paux1,
203 (i==0) ? in : in-1,
204 (size_t)((i==0) ? nchar : nchar+1));
205 paux1[(i==0) ? nchar : nchar+1] = '\0';
206 pauxt = paux2;
207 paux2 = strcat(paux1, paux2);
208 paux1 = pauxt;
209 }
210 in = ++st;
211 }
212 net_entry.n_net = inet_network(paux2);
213 break;
214 }
215 net_entry.n_aliases++;
216 return (&net_entry);
217 }
218 h_errno = TRY_AGAIN;
219 return (NULL);
220 }
221
222 int
223 _getnetbyaddr(rv, cb_data, ap)
224 void *rv;
225 void *cb_data;
226 va_list ap;
227 {
228 struct netent *p;
229 unsigned long net;
230 int type;
231
232 net = va_arg(ap, unsigned long);
233 type = va_arg(ap, int);
234
235 setnetent(_net_stayopen);
236 while ((p = getnetent()) != NULL)
237 if (p->n_addrtype == type && p->n_net == net)
238 break;
239 if (!_net_stayopen)
240 endnetent();
241 *((struct netent **)rv) = p;
242 if (p==NULL) {
243 h_errno = HOST_NOT_FOUND;
244 return NS_NOTFOUND;
245 }
246 return NS_SUCCESS;
247 }
248
249 int
250 _dns_getnetbyaddr(rv, cb_data, ap)
251 void *rv;
252 void *cb_data;
253 va_list ap;
254 {
255 unsigned int netbr[4];
256 int nn, anslen;
257 querybuf buf;
258 char qbuf[MAXDNAME];
259 unsigned long net2;
260 struct netent *net_entry;
261 unsigned long net;
262 int type;
263
264 net = va_arg(ap, unsigned long);
265 type = va_arg(ap, int);
266
267 if (type != AF_INET)
268 return NS_UNAVAIL;
269
270 for (nn = 4, net2 = net; net2; net2 >>= 8)
271 netbr[--nn] = (unsigned int)(net2 & 0xff);
272 switch (nn) {
273 default:
274 return NS_UNAVAIL;
275 case 3: /* Class A */
276 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
277 break;
278 case 2: /* Class B */
279 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
280 break;
281 case 1: /* Class C */
282 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
283 netbr[1]);
284 break;
285 case 0: /* Class D - E */
286 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
287 netbr[1], netbr[0]);
288 break;
289 }
290 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
291 sizeof(buf));
292 if (anslen < 0) {
293 #ifdef DEBUG
294 if (_res.options & RES_DEBUG)
295 printf("res_query failed\n");
296 #endif
297 return NS_NOTFOUND;
298 }
299 net_entry = getnetanswer(&buf, anslen, BYADDR);
300 if (net_entry) {
301 /* maybe net should be unsigned? */
302 unsigned long u_net = net;
303
304 /* Strip trailing zeros */
305 while ((u_net & 0xff) == 0 && u_net != 0)
306 u_net >>= 8;
307 net_entry->n_net = u_net;
308 }
309 *((struct netent **)rv) = net_entry;
310 if (net_entry == NULL) {
311 h_errno = HOST_NOT_FOUND;
312 return NS_NOTFOUND;
313 }
314 return NS_SUCCESS;
315 }
316
317 struct netent *
318 getnetbyaddr(net, net_type)
319 u_long net;
320 int net_type;
321 {
322 struct netent *net_entry;
323 static ns_dtab dtab[] = {
324 NS_FILES_CB(_getnetbyaddr, NULL),
325 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */
326 { NULL, NULL, NULL }
327 };
328
329 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
330 h_errno = NETDB_INTERNAL;
331 return (NULL);
332 }
333
334 net_entry = NULL;
335 if (nsdispatch(&net_entry, dtab, NSDB_NETWORKS, net, net_type) !=
336 NS_SUCCESS) {
337 h_errno = NETDB_INTERNAL;
338 return (NULL);
339 }
340 return (net_entry);
341 }
342
343 int
344 _getnetbyname(rv, cb_data, ap)
345 void *rv;
346 void *cb_data;
347 va_list ap;
348 {
349 struct netent *p;
350 char **cp;
351 const char *name;
352
353 name = va_arg(ap, const char *);
354 setnetent(_net_stayopen);
355 while ((p = getnetent()) != NULL) {
356 if (strcasecmp(p->n_name, name) == 0)
357 break;
358 for (cp = p->n_aliases; *cp != 0; cp++)
359 if (strcasecmp(*cp, name) == 0)
360 goto found;
361 }
362 found:
363 if (!_net_stayopen)
364 endnetent();
365 *((struct netent **)rv) = p;
366 if (p==NULL) {
367 h_errno = HOST_NOT_FOUND;
368 return NS_NOTFOUND;
369 }
370 return NS_SUCCESS;
371 }
372
373 int
374 _dns_getnetbyname(rv, cb_data, ap)
375 void *rv;
376 void *cb_data;
377 va_list ap;
378 {
379 int anslen;
380 querybuf buf;
381 char qbuf[MAXDNAME];
382 struct netent *net_entry;
383 const char *net;
384
385 net = va_arg(ap, const char *);
386 strcpy(&qbuf[0], net);
387 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
388 sizeof(buf));
389 if (anslen < 0) {
390 #ifdef DEBUG
391 if (_res.options & RES_DEBUG)
392 printf("res_query failed\n");
393 #endif
394 return NS_NOTFOUND;
395 }
396 net_entry = getnetanswer(&buf, anslen, BYNAME);
397
398 *((struct netent **)rv) = net_entry;
399 if (net_entry == NULL) {
400 h_errno = HOST_NOT_FOUND;
401 return NS_NOTFOUND;
402 }
403 return NS_SUCCESS;
404 }
405
406 struct netent *
407 getnetbyname(net)
408 const char *net;
409 {
410 struct netent *net_entry;
411 static ns_dtab dtab[] = {
412 NS_FILES_CB(_getnetbyname, NULL),
413 { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */
414 { NULL, NULL, NULL }
415 };
416
417 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
418 h_errno = NETDB_INTERNAL;
419 return (NULL);
420 }
421
422 net_entry = NULL;
423 if (nsdispatch(&net_entry, dtab, NSDB_NETWORKS, net) != NS_SUCCESS) {
424 h_errno = NETDB_INTERNAL;
425 return (NULL);
426 }
427 return (net_entry);
428 }
429