getnetnamadr.c revision 1.19 1 /* $NetBSD: getnetnamadr.c,v 1.19 2000/01/22 22:19:14 mycroft 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.19 2000/01/22 22:19:14 mycroft 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 <assert.h>
63 #include <ctype.h>
64 #include <errno.h>
65 #include <netdb.h>
66 #include <nsswitch.h>
67 #include <resolv.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 #ifdef __STDC__
73 #include <stdarg.h>
74 #else
75 #include <varargs.h>
76 #endif
77
78 #ifdef YP
79 #include <rpc/rpc.h>
80 #include <rpcsvc/yp_prot.h>
81 #include <rpcsvc/ypclnt.h>
82 #endif
83
84 #ifdef __weak_alias
85 __weak_alias(getnetbyaddr,_getnetbyaddr)
86 __weak_alias(getnetbyname,_getnetbyname)
87 #endif
88
89 extern int h_errno;
90 extern int _net_stayopen;
91
92 #define BYADDR 0
93 #define BYNAME 1
94 #define MAXALIASES 35
95
96 #if PACKETSZ > 1024
97 #define MAXPACKET PACKETSZ
98 #else
99 #define MAXPACKET 1024
100 #endif
101
102 typedef union {
103 HEADER hdr;
104 u_char buf[MAXPACKET];
105 } querybuf;
106
107 typedef union {
108 long al;
109 char ac;
110 } align;
111
112 #ifdef YP
113 static char *__ypdomain;
114 static char *__ypcurrent;
115 static int __ypcurrentlen;
116 #endif
117
118 static struct netent net_entry;
119 static char *net_aliases[MAXALIASES];
120
121 static struct netent *getnetanswer __P((querybuf *, int, int));
122 int _files_getnetbyaddr __P((void *, void *, va_list));
123 int _files_getnetbyname __P((void *, void *, va_list));
124 int _dns_getnetbyaddr __P((void *, void *, va_list));
125 int _dns_getnetbyname __P((void *, void *, va_list));
126 #ifdef YP
127 int _yp_getnetbyaddr __P((void *, void *, va_list));
128 int _yp_getnetbyname __P((void *, void *, va_list));
129 struct netent *_ypnetent __P((char *));
130 #endif
131
132 static struct netent *
133 getnetanswer(answer, anslen, net_i)
134 querybuf *answer;
135 int anslen;
136 int net_i;
137 {
138 HEADER *hp;
139 u_char *cp;
140 int n;
141 u_char *eom;
142 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
143 char aux1[30], aux2[30], ans[30], *in, *st, *pauxt, *bp, **ap,
144 *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
145 static char netbuf[PACKETSZ];
146
147 _DIAGASSERT(answer != NULL);
148
149 /*
150 * find first satisfactory answer
151 *
152 * answer --> +------------+ ( MESSAGE )
153 * | Header |
154 * +------------+
155 * | Question | the question for the name server
156 * +------------+
157 * | Answer | RRs answering the question
158 * +------------+
159 * | Authority | RRs pointing toward an authority
160 * | Additional | RRs holding additional information
161 * +------------+
162 */
163 eom = answer->buf + anslen;
164 hp = &answer->hdr;
165 ancount = ntohs(hp->ancount); /* #/records in the answer section */
166 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
167 bp = netbuf;
168 buflen = sizeof(netbuf);
169 cp = answer->buf + HFIXEDSZ;
170 if (!qdcount) {
171 if (hp->aa)
172 h_errno = HOST_NOT_FOUND;
173 else
174 h_errno = TRY_AGAIN;
175 return (NULL);
176 }
177 while (qdcount-- > 0)
178 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
179 ap = net_aliases;
180 *ap = NULL;
181 net_entry.n_aliases = net_aliases;
182 haveanswer = 0;
183 while (--ancount >= 0 && cp < eom) {
184 n = dn_expand(answer->buf, eom, cp, bp, buflen);
185 if ((n < 0) || !res_dnok(bp))
186 break;
187 cp += n;
188 ans[0] = '\0';
189 (void)strcpy(&ans[0], bp);
190 GETSHORT(type, cp);
191 GETSHORT(class, cp);
192 cp += INT32SZ; /* TTL */
193 GETSHORT(n, cp);
194 if (class == C_IN && type == T_PTR) {
195 n = dn_expand(answer->buf, eom, cp, bp, buflen);
196 if ((n < 0) || !res_hnok(bp)) {
197 cp += n;
198 return (NULL);
199 }
200 cp += n;
201 *ap++ = bp;
202 bp += strlen(bp) + 1;
203 net_entry.n_addrtype =
204 (class == C_IN) ? AF_INET : AF_UNSPEC;
205 haveanswer++;
206 }
207 }
208 if (haveanswer) {
209 *ap = NULL;
210 switch (net_i) {
211 case BYADDR:
212 net_entry.n_name = *net_entry.n_aliases;
213 net_entry.n_net = 0L;
214 break;
215 case BYNAME:
216 in = *net_entry.n_aliases;
217 net_entry.n_name = &ans[0];
218 aux2[0] = '\0';
219 for (i = 0; i < 4; i++) {
220 for (st = in, nchar = 0;
221 *st != '.';
222 st++, nchar++)
223 ;
224 if (nchar != 1 || *in != '0' || flag) {
225 flag = 1;
226 (void)strncpy(paux1,
227 (i==0) ? in : in-1,
228 (size_t)((i==0) ? nchar : nchar+1));
229 paux1[(i==0) ? nchar : nchar+1] = '\0';
230 pauxt = paux2;
231 paux2 = strcat(paux1, paux2);
232 paux1 = pauxt;
233 }
234 in = ++st;
235 }
236 net_entry.n_net = inet_network(paux2);
237 break;
238 }
239 net_entry.n_aliases++;
240 return (&net_entry);
241 }
242 h_errno = TRY_AGAIN;
243 return (NULL);
244 }
245
246 /*ARGSUSED*/
247 int
248 _files_getnetbyaddr(rv, cb_data, ap)
249 void *rv;
250 void *cb_data;
251 va_list ap;
252 {
253 struct netent *p;
254 unsigned long net;
255 int type;
256
257 _DIAGASSERT(rv != NULL);
258
259 net = va_arg(ap, unsigned long);
260 type = va_arg(ap, int);
261
262 setnetent(_net_stayopen);
263 while ((p = getnetent()) != NULL)
264 if (p->n_addrtype == type && p->n_net == net)
265 break;
266 if (!_net_stayopen)
267 endnetent();
268 *((struct netent **)rv) = p;
269 if (p==NULL) {
270 h_errno = HOST_NOT_FOUND;
271 return NS_NOTFOUND;
272 }
273 return NS_SUCCESS;
274 }
275
276 /*ARGSUSED*/
277 int
278 _dns_getnetbyaddr(rv, cb_data, ap)
279 void *rv;
280 void *cb_data;
281 va_list ap;
282 {
283 unsigned int netbr[4];
284 int nn, anslen;
285 querybuf buf;
286 char qbuf[MAXDNAME];
287 unsigned long net2;
288 struct netent *np;
289 unsigned long net;
290 int type;
291
292 _DIAGASSERT(rv != NULL);
293
294 net = va_arg(ap, unsigned long);
295 type = va_arg(ap, int);
296
297 if (type != AF_INET)
298 return NS_UNAVAIL;
299
300 for (nn = 4, net2 = net; net2; net2 >>= 8)
301 netbr[--nn] = (unsigned int)(net2 & 0xff);
302 switch (nn) {
303 default:
304 return NS_UNAVAIL;
305 case 3: /* Class A */
306 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
307 break;
308 case 2: /* Class B */
309 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
310 break;
311 case 1: /* Class C */
312 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
313 netbr[1]);
314 break;
315 case 0: /* Class D - E */
316 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
317 netbr[1], netbr[0]);
318 break;
319 }
320 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
321 sizeof(buf));
322 if (anslen < 0) {
323 #ifdef DEBUG
324 if (_res.options & RES_DEBUG)
325 printf("res_query failed\n");
326 #endif
327 return NS_NOTFOUND;
328 }
329 np = getnetanswer(&buf, anslen, BYADDR);
330 if (np) {
331 /* maybe net should be unsigned? */
332 unsigned long u_net = net;
333
334 /* Strip trailing zeros */
335 while ((u_net & 0xff) == 0 && u_net != 0)
336 u_net >>= 8;
337 np->n_net = u_net;
338 }
339 *((struct netent **)rv) = np;
340 if (np == NULL) {
341 h_errno = HOST_NOT_FOUND;
342 return NS_NOTFOUND;
343 }
344 return NS_SUCCESS;
345 }
346
347
348 struct netent *
349 getnetbyaddr(net, net_type)
350 u_long net;
351 int net_type;
352 {
353 struct netent *np;
354 static const ns_dtab dtab[] = {
355 NS_FILES_CB(_files_getnetbyaddr, NULL)
356 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */
357 NS_NIS_CB(_yp_getnetbyaddr, NULL)
358 { 0 }
359 };
360
361 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
362 h_errno = NETDB_INTERNAL;
363 return (NULL);
364 }
365
366 np = NULL;
367 h_errno = NETDB_INTERNAL;
368 if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyaddr", __nsdefaultsrc,
369 net, net_type) != NS_SUCCESS)
370 return (NULL);
371 h_errno = NETDB_SUCCESS;
372 return (np);
373 }
374
375 /*ARGSUSED*/
376 int
377 _files_getnetbyname(rv, cb_data, ap)
378 void *rv;
379 void *cb_data;
380 va_list ap;
381 {
382 struct netent *p;
383 char **cp;
384 const char *name;
385
386 _DIAGASSERT(rv != NULL);
387
388 name = va_arg(ap, const char *);
389 setnetent(_net_stayopen);
390 while ((p = getnetent()) != NULL) {
391 if (strcasecmp(p->n_name, name) == 0)
392 break;
393 for (cp = p->n_aliases; *cp != 0; cp++)
394 if (strcasecmp(*cp, name) == 0)
395 goto found;
396 }
397 found:
398 if (!_net_stayopen)
399 endnetent();
400 *((struct netent **)rv) = p;
401 if (p==NULL) {
402 h_errno = HOST_NOT_FOUND;
403 return NS_NOTFOUND;
404 }
405 return NS_SUCCESS;
406 }
407
408 /*ARGSUSED*/
409 int
410 _dns_getnetbyname(rv, cb_data, ap)
411 void *rv;
412 void *cb_data;
413 va_list ap;
414 {
415 int anslen;
416 querybuf buf;
417 char qbuf[MAXDNAME];
418 struct netent *np;
419 const char *net;
420
421 _DIAGASSERT(rv != NULL);
422
423 net = va_arg(ap, const char *);
424 strcpy(&qbuf[0], net);
425 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
426 sizeof(buf));
427 if (anslen < 0) {
428 #ifdef DEBUG
429 if (_res.options & RES_DEBUG)
430 printf("res_query failed\n");
431 #endif
432 return NS_NOTFOUND;
433 }
434 np = getnetanswer(&buf, anslen, BYNAME);
435
436 *((struct netent **)rv) = np;
437 if (np == NULL) {
438 h_errno = HOST_NOT_FOUND;
439 return NS_NOTFOUND;
440 }
441 return NS_SUCCESS;
442 }
443
444 struct netent *
445 getnetbyname(net)
446 const char *net;
447 {
448 struct netent *np;
449 static const ns_dtab dtab[] = {
450 NS_FILES_CB(_files_getnetbyname, NULL)
451 { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */
452 NS_NIS_CB(_yp_getnetbyname, NULL)
453 { 0 }
454 };
455
456 _DIAGASSERT(net != NULL);
457
458 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
459 h_errno = NETDB_INTERNAL;
460 return (NULL);
461 }
462
463 np = NULL;
464 h_errno = NETDB_INTERNAL;
465 if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyname", __nsdefaultsrc,
466 net) != NS_SUCCESS)
467 return (NULL);
468 h_errno = NETDB_SUCCESS;
469 return (np);
470 }
471
472 #ifdef YP
473 /*ARGSUSED*/
474 int
475 _yp_getnetbyaddr(rv, cb_data, ap)
476 void *rv;
477 void *cb_data;
478 va_list ap;
479 {
480 struct netent *np;
481 char qbuf[MAXDNAME];
482 unsigned int netbr[4];
483 unsigned long net, net2;
484 int type, r;
485
486 _DIAGASSERT(rv != NULL);
487
488 net = va_arg(ap, unsigned long);
489 type = va_arg(ap, int);
490
491 if (type != AF_INET)
492 return NS_UNAVAIL;
493
494 if (!__ypdomain) {
495 if (_yp_check(&__ypdomain) == 0)
496 return NS_UNAVAIL;
497 }
498 np = NULL;
499 if (__ypcurrent)
500 free(__ypcurrent);
501 __ypcurrent = NULL;
502 for (r = 4, net2 = net; net2; net2 >>= 8)
503 netbr[--r] = (unsigned int)(net2 & 0xff);
504 switch (r) {
505 default:
506 return NS_UNAVAIL;
507 case 3: /* Class A */
508 sprintf(qbuf, "%u", netbr[0]);
509 break;
510 case 2: /* Class B */
511 sprintf(qbuf, "%u.%u", netbr[0], netbr[1]);
512 break;
513 case 1: /* Class C */
514 sprintf(qbuf, "%u.%u.%u", netbr[0], netbr[1], netbr[2]);
515 break;
516 case 0: /* Class D - E */
517 sprintf(qbuf, "%u.%u.%u.%u", netbr[0], netbr[1], netbr[2],
518 netbr[3]);
519 break;
520 }
521 r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
522 &__ypcurrent, &__ypcurrentlen);
523 if (r == 0)
524 np = _ypnetent(__ypcurrent);
525
526 *((struct netent **)rv) = np;
527 if (np == NULL) {
528 h_errno = HOST_NOT_FOUND;
529 return NS_NOTFOUND;
530 }
531 return NS_SUCCESS;
532
533 }
534
535 int
536 /*ARGSUSED*/
537 _yp_getnetbyname(rv, cb_data, ap)
538 void *rv;
539 void *cb_data;
540 va_list ap;
541 {
542 struct netent *np;
543 const char *name;
544 int r;
545
546 _DIAGASSERT(rv != NULL);
547
548 name = va_arg(ap, const char *);
549
550 if (!__ypdomain) {
551 if (_yp_check(&__ypdomain) == 0)
552 return NS_UNAVAIL;
553 }
554 np = NULL;
555 if (__ypcurrent)
556 free(__ypcurrent);
557 __ypcurrent = NULL;
558 r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
559 &__ypcurrent, &__ypcurrentlen);
560 if (r == 0)
561 np = _ypnetent(__ypcurrent);
562
563 *((struct netent **)rv) = np;
564 if (np == NULL) {
565 h_errno = HOST_NOT_FOUND;
566 return NS_NOTFOUND;
567 }
568 return NS_SUCCESS;
569 }
570
571 struct netent *
572 _ypnetent(line)
573 char *line;
574 {
575 char *cp, *p, **q;
576
577 _DIAGASSERT(line != NULL);
578
579 net_entry.n_name = line;
580 cp = strpbrk(line, " \t");
581 if (cp == NULL)
582 return (NULL);
583 *cp++ = '\0';
584 while (*cp == ' ' || *cp == '\t')
585 cp++;
586 p = strpbrk(cp, " \t");
587 if (p != NULL)
588 *p++ = '\0';
589 net_entry.n_net = inet_network(cp);
590 net_entry.n_addrtype = AF_INET;
591 q = net_entry.n_aliases = net_aliases;
592 if (p != NULL) {
593 cp = p;
594 while (cp && *cp) {
595 if (*cp == ' ' || *cp == '\t') {
596 cp++;
597 continue;
598 }
599 if (q < &net_aliases[MAXALIASES - 1])
600 *q++ = cp;
601 cp = strpbrk(cp, " \t");
602 if (cp != NULL)
603 *cp++ = '\0';
604 }
605 }
606 *q = NULL;
607
608 return (&net_entry);
609 }
610 #endif
611