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