getnetnamadr.c revision 1.20.4.2 1 /* $NetBSD: getnetnamadr.c,v 1.20.4.2 2002/08/17 15:45:39 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.20.4.2 2002/08/17 15:45:39 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 <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 _net_stayopen;
90
91 #define BYADDR 0
92 #define BYNAME 1
93 #define MAXALIASES 35
94
95 #if PACKETSZ > 1024
96 #define MAXPACKET PACKETSZ
97 #else
98 #define MAXPACKET 1024
99 #endif
100
101 typedef union {
102 HEADER hdr;
103 u_char buf[MAXPACKET];
104 } querybuf;
105
106 typedef union {
107 long al;
108 char ac;
109 } align;
110
111 #ifdef YP
112 static char *__ypdomain;
113 static char *__ypcurrent;
114 static int __ypcurrentlen;
115 #endif
116
117 static struct netent net_entry;
118 static char *net_aliases[MAXALIASES];
119
120 static struct netent *getnetanswer __P((querybuf *, int, int));
121 int _files_getnetbyaddr __P((void *, void *, va_list));
122 int _files_getnetbyname __P((void *, void *, va_list));
123 int _dns_getnetbyaddr __P((void *, void *, va_list));
124 int _dns_getnetbyname __P((void *, void *, va_list));
125 #ifdef YP
126 int _yp_getnetbyaddr __P((void *, void *, va_list));
127 int _yp_getnetbyname __P((void *, void *, va_list));
128 struct netent *_ypnetent __P((char *));
129 #endif
130
131 static struct netent *
132 getnetanswer(answer, anslen, net_i)
133 querybuf *answer;
134 int anslen;
135 int net_i;
136 {
137 HEADER *hp;
138 u_char *cp;
139 int n;
140 u_char *eom;
141 int type, class, ancount, qdcount, haveanswer, i, nchar;
142 char aux1[30], aux2[30], ans[30], *in, *st, *pauxt, *bp, **ap,
143 *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0, *ep;
144 static char netbuf[PACKETSZ];
145
146 _DIAGASSERT(answer != NULL);
147
148 /*
149 * find first satisfactory answer
150 *
151 * answer --> +------------+ ( MESSAGE )
152 * | Header |
153 * +------------+
154 * | Question | the question for the name server
155 * +------------+
156 * | Answer | RRs answering the question
157 * +------------+
158 * | Authority | RRs pointing toward an authority
159 * | Additional | RRs holding additional information
160 * +------------+
161 */
162 eom = answer->buf + anslen;
163 hp = &answer->hdr;
164 ancount = ntohs(hp->ancount); /* #/records in the answer section */
165 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
166 bp = netbuf;
167 ep = netbuf + sizeof(netbuf);
168 cp = answer->buf + HFIXEDSZ;
169 if (!qdcount) {
170 if (hp->aa)
171 h_errno = HOST_NOT_FOUND;
172 else
173 h_errno = TRY_AGAIN;
174 return (NULL);
175 }
176 while (qdcount-- > 0)
177 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
178 ap = net_aliases;
179 *ap = NULL;
180 net_entry.n_aliases = net_aliases;
181 haveanswer = 0;
182 while (--ancount >= 0 && cp < eom) {
183 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
184 if ((n < 0) || !res_dnok(bp))
185 break;
186 cp += n;
187 ans[0] = '\0';
188 (void)strcpy(&ans[0], bp);
189 GETSHORT(type, cp);
190 GETSHORT(class, cp);
191 cp += INT32SZ; /* TTL */
192 GETSHORT(n, cp);
193 if (class == C_IN && type == T_PTR) {
194 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
195 if ((n < 0) || !res_hnok(bp)) {
196 cp += n;
197 return (NULL);
198 }
199 cp += n;
200 *ap++ = bp;
201 bp += strlen(bp) + 1;
202 net_entry.n_addrtype =
203 (class == C_IN) ? AF_INET : AF_UNSPEC;
204 haveanswer++;
205 }
206 }
207 if (haveanswer) {
208 *ap = NULL;
209 switch (net_i) {
210 case BYADDR:
211 net_entry.n_name = *net_entry.n_aliases;
212 net_entry.n_net = 0L;
213 break;
214 case BYNAME:
215 in = *net_entry.n_aliases;
216 net_entry.n_name = &ans[0];
217 aux2[0] = '\0';
218 for (i = 0; i < 4; i++) {
219 for (st = in, nchar = 0;
220 *st != '.';
221 st++, nchar++)
222 ;
223 if (nchar != 1 || *in != '0' || flag) {
224 flag = 1;
225 (void)strncpy(paux1,
226 (i==0) ? in : in-1,
227 (size_t)((i==0) ? nchar : nchar+1));
228 paux1[(i==0) ? nchar : nchar+1] = '\0';
229 pauxt = paux2;
230 paux2 = strcat(paux1, paux2);
231 paux1 = pauxt;
232 }
233 in = ++st;
234 }
235 net_entry.n_net = inet_network(paux2);
236 break;
237 }
238 net_entry.n_aliases++;
239 return (&net_entry);
240 }
241 h_errno = TRY_AGAIN;
242 return (NULL);
243 }
244
245 /*ARGSUSED*/
246 int
247 _files_getnetbyaddr(rv, cb_data, ap)
248 void *rv;
249 void *cb_data;
250 va_list ap;
251 {
252 struct netent *p;
253 unsigned long net;
254 int type;
255
256 _DIAGASSERT(rv != NULL);
257
258 net = va_arg(ap, unsigned long);
259 type = va_arg(ap, int);
260
261 setnetent(_net_stayopen);
262 while ((p = getnetent()) != NULL)
263 if (p->n_addrtype == type && p->n_net == net)
264 break;
265 if (!_net_stayopen)
266 endnetent();
267 *((struct netent **)rv) = p;
268 if (p==NULL) {
269 h_errno = HOST_NOT_FOUND;
270 return NS_NOTFOUND;
271 }
272 return NS_SUCCESS;
273 }
274
275 /*ARGSUSED*/
276 int
277 _dns_getnetbyaddr(rv, cb_data, ap)
278 void *rv;
279 void *cb_data;
280 va_list ap;
281 {
282 unsigned int netbr[4];
283 int nn, anslen;
284 querybuf buf;
285 char qbuf[MAXDNAME];
286 unsigned long net2;
287 struct netent *np;
288 unsigned long net;
289 int type;
290
291 _DIAGASSERT(rv != NULL);
292
293 net = va_arg(ap, unsigned long);
294 type = va_arg(ap, int);
295
296 if (type != AF_INET)
297 return NS_UNAVAIL;
298
299 for (nn = 4, net2 = net; net2; net2 >>= 8)
300 netbr[--nn] = (unsigned int)(net2 & 0xff);
301 switch (nn) {
302 default:
303 return NS_UNAVAIL;
304 case 3: /* Class A */
305 snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
306 break;
307 case 2: /* Class B */
308 snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
309 netbr[3], netbr[2]);
310 break;
311 case 1: /* Class C */
312 snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
313 netbr[3], netbr[2], netbr[1]);
314 break;
315 case 0: /* Class D - E */
316 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
317 netbr[3], netbr[2], 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 snprintf(qbuf, sizeof(qbuf), "%u", netbr[0]);
509 break;
510 case 2: /* Class B */
511 snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[0], netbr[1]);
512 break;
513 case 1: /* Class C */
514 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[0], netbr[1],
515 netbr[2]);
516 break;
517 case 0: /* Class D - E */
518 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
519 netbr[2], netbr[3]);
520 break;
521 }
522 r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
523 &__ypcurrent, &__ypcurrentlen);
524 if (r == 0)
525 np = _ypnetent(__ypcurrent);
526
527 *((struct netent **)rv) = np;
528 if (np == NULL) {
529 h_errno = HOST_NOT_FOUND;
530 return NS_NOTFOUND;
531 }
532 return NS_SUCCESS;
533
534 }
535
536 int
537 /*ARGSUSED*/
538 _yp_getnetbyname(rv, cb_data, ap)
539 void *rv;
540 void *cb_data;
541 va_list ap;
542 {
543 struct netent *np;
544 const char *name;
545 int r;
546
547 _DIAGASSERT(rv != NULL);
548
549 name = va_arg(ap, const char *);
550
551 if (!__ypdomain) {
552 if (_yp_check(&__ypdomain) == 0)
553 return NS_UNAVAIL;
554 }
555 np = NULL;
556 if (__ypcurrent)
557 free(__ypcurrent);
558 __ypcurrent = NULL;
559 r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
560 &__ypcurrent, &__ypcurrentlen);
561 if (r == 0)
562 np = _ypnetent(__ypcurrent);
563
564 *((struct netent **)rv) = np;
565 if (np == NULL) {
566 h_errno = HOST_NOT_FOUND;
567 return NS_NOTFOUND;
568 }
569 return NS_SUCCESS;
570 }
571
572 struct netent *
573 _ypnetent(line)
574 char *line;
575 {
576 char *cp, *p, **q;
577
578 _DIAGASSERT(line != NULL);
579
580 net_entry.n_name = line;
581 cp = strpbrk(line, " \t");
582 if (cp == NULL)
583 return (NULL);
584 *cp++ = '\0';
585 while (*cp == ' ' || *cp == '\t')
586 cp++;
587 p = strpbrk(cp, " \t");
588 if (p != NULL)
589 *p++ = '\0';
590 net_entry.n_net = inet_network(cp);
591 net_entry.n_addrtype = AF_INET;
592 q = net_entry.n_aliases = net_aliases;
593 if (p != NULL) {
594 cp = p;
595 while (cp && *cp) {
596 if (*cp == ' ' || *cp == '\t') {
597 cp++;
598 continue;
599 }
600 if (q < &net_aliases[MAXALIASES - 1])
601 *q++ = cp;
602 cp = strpbrk(cp, " \t");
603 if (cp != NULL)
604 *cp++ = '\0';
605 }
606 }
607 *q = NULL;
608
609 return (&net_entry);
610 }
611 #endif
612