11363f04cSPaul Traina /*- 21363f04cSPaul Traina * Copyright (c) 1985, 1988, 1993 31363f04cSPaul Traina * The Regents of the University of California. All rights reserved. 41363f04cSPaul Traina * 51363f04cSPaul Traina * Redistribution and use in source and binary forms, with or without 61363f04cSPaul Traina * modification, are permitted provided that the following conditions 71363f04cSPaul Traina * are met: 81363f04cSPaul Traina * 1. Redistributions of source code must retain the above copyright 91363f04cSPaul Traina * notice, this list of conditions and the following disclaimer. 101363f04cSPaul Traina * 2. Redistributions in binary form must reproduce the above copyright 111363f04cSPaul Traina * notice, this list of conditions and the following disclaimer in the 121363f04cSPaul Traina * documentation and/or other materials provided with the distribution. 131363f04cSPaul Traina * 3. All advertising materials mentioning features or use of this software 141363f04cSPaul Traina * must display the following acknowledgement: 151363f04cSPaul Traina * This product includes software developed by the University of 161363f04cSPaul Traina * California, Berkeley and its contributors. 171363f04cSPaul Traina * 4. Neither the name of the University nor the names of its contributors 181363f04cSPaul Traina * may be used to endorse or promote products derived from this software 191363f04cSPaul Traina * without specific prior written permission. 201363f04cSPaul Traina * 211363f04cSPaul Traina * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221363f04cSPaul Traina * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231363f04cSPaul Traina * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241363f04cSPaul Traina * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251363f04cSPaul Traina * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261363f04cSPaul Traina * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271363f04cSPaul Traina * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281363f04cSPaul Traina * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291363f04cSPaul Traina * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301363f04cSPaul Traina * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311363f04cSPaul Traina * SUCH DAMAGE. 321363f04cSPaul Traina * - 331363f04cSPaul Traina * Portions Copyright (c) 1993 by Digital Equipment Corporation. 341363f04cSPaul Traina * 351363f04cSPaul Traina * Permission to use, copy, modify, and distribute this software for any 361363f04cSPaul Traina * purpose with or without fee is hereby granted, provided that the above 371363f04cSPaul Traina * copyright notice and this permission notice appear in all copies, and that 381363f04cSPaul Traina * the name of Digital Equipment Corporation not be used in advertising or 391363f04cSPaul Traina * publicity pertaining to distribution of the document or software without 401363f04cSPaul Traina * specific, written prior permission. 411363f04cSPaul Traina * 421363f04cSPaul Traina * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 431363f04cSPaul Traina * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 441363f04cSPaul Traina * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 451363f04cSPaul Traina * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 461363f04cSPaul Traina * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 471363f04cSPaul Traina * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 481363f04cSPaul Traina * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 491363f04cSPaul Traina * SOFTWARE. 501363f04cSPaul Traina * - 511363f04cSPaul Traina * --Copyright-- 521363f04cSPaul Traina */ 534faad310SPeter Wemm /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro 544faad310SPeter Wemm * Dep. Matematica Universidade de Coimbra, Portugal, Europe 554faad310SPeter Wemm * 564faad310SPeter Wemm * Permission to use, copy, modify, and distribute this software for any 574faad310SPeter Wemm * purpose with or without fee is hereby granted, provided that the above 584faad310SPeter Wemm * copyright notice and this permission notice appear in all copies. 594faad310SPeter Wemm */ 601363f04cSPaul Traina 611363f04cSPaul Traina #if defined(LIBC_SCCS) && !defined(lint) 621363f04cSPaul Traina static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; 633c8e19e1SWarner Losh static char rcsid[] = "$Id: getnetbydns.c,v 1.11 1997/06/27 08:22:01 peter Exp $"; 641363f04cSPaul Traina #endif /* LIBC_SCCS and not lint */ 651363f04cSPaul Traina 661363f04cSPaul Traina #include <sys/param.h> 671363f04cSPaul Traina #include <sys/socket.h> 681363f04cSPaul Traina #include <netinet/in.h> 691363f04cSPaul Traina #include <arpa/inet.h> 701363f04cSPaul Traina #include <arpa/nameser.h> 714faad310SPeter Wemm 724faad310SPeter Wemm #include <stdio.h> 731363f04cSPaul Traina #include <netdb.h> 741363f04cSPaul Traina #include <resolv.h> 751363f04cSPaul Traina #include <ctype.h> 761363f04cSPaul Traina #include <errno.h> 771363f04cSPaul Traina #include <string.h> 784faad310SPeter Wemm #include <unistd.h> 791363f04cSPaul Traina #include <syslog.h> 801363f04cSPaul Traina 814faad310SPeter Wemm #include "res_config.h" 821363f04cSPaul Traina 834faad310SPeter Wemm extern int h_errno; 844faad310SPeter Wemm 854faad310SPeter Wemm #define BYADDR 0 864faad310SPeter Wemm #define BYNAME 1 871363f04cSPaul Traina #define MAXALIASES 35 881363f04cSPaul Traina 891363f04cSPaul Traina #if PACKETSZ > 1024 901363f04cSPaul Traina #define MAXPACKET PACKETSZ 911363f04cSPaul Traina #else 921363f04cSPaul Traina #define MAXPACKET 1024 931363f04cSPaul Traina #endif 941363f04cSPaul Traina 951363f04cSPaul Traina typedef union { 961363f04cSPaul Traina HEADER hdr; 971363f04cSPaul Traina u_char buf[MAXPACKET]; 981363f04cSPaul Traina } querybuf; 991363f04cSPaul Traina 1001363f04cSPaul Traina typedef union { 1014faad310SPeter Wemm long al; 1021363f04cSPaul Traina char ac; 1031363f04cSPaul Traina } align; 1041363f04cSPaul Traina 1051363f04cSPaul Traina static struct netent * 1061363f04cSPaul Traina getnetanswer(answer, anslen, net_i) 1071363f04cSPaul Traina querybuf *answer; 1081363f04cSPaul Traina int anslen; 1091363f04cSPaul Traina int net_i; 1101363f04cSPaul Traina { 1111363f04cSPaul Traina 1121363f04cSPaul Traina register HEADER *hp; 1131363f04cSPaul Traina register u_char *cp; 1141363f04cSPaul Traina register int n; 1151363f04cSPaul Traina u_char *eom; 1164faad310SPeter Wemm int type, class, buflen, ancount, qdcount, haveanswer, i, nchar; 1173c8e19e1SWarner Losh char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; 1183c8e19e1SWarner Losh char *in, *st, *pauxt, *bp, **ap; 1193c8e19e1SWarner Losh char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; 1201363f04cSPaul Traina static struct netent net_entry; 1216c5aff80SPeter Wemm static char *net_aliases[MAXALIASES], netbuf[PACKETSZ]; 1221363f04cSPaul Traina 1231363f04cSPaul Traina /* 1241363f04cSPaul Traina * find first satisfactory answer 1251363f04cSPaul Traina * 1261363f04cSPaul Traina * answer --> +------------+ ( MESSAGE ) 1271363f04cSPaul Traina * | Header | 1281363f04cSPaul Traina * +------------+ 1291363f04cSPaul Traina * | Question | the question for the name server 1301363f04cSPaul Traina * +------------+ 1311363f04cSPaul Traina * | Answer | RRs answering the question 1321363f04cSPaul Traina * +------------+ 1331363f04cSPaul Traina * | Authority | RRs pointing toward an authority 1341363f04cSPaul Traina * | Additional | RRs holding additional information 1351363f04cSPaul Traina * +------------+ 1361363f04cSPaul Traina */ 1371363f04cSPaul Traina eom = answer->buf + anslen; 1381363f04cSPaul Traina hp = &answer->hdr; 1391363f04cSPaul Traina ancount = ntohs(hp->ancount); /* #/records in the answer section */ 1401363f04cSPaul Traina qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 1411363f04cSPaul Traina bp = netbuf; 1421363f04cSPaul Traina buflen = sizeof(netbuf); 1431363f04cSPaul Traina cp = answer->buf + HFIXEDSZ; 1441363f04cSPaul Traina if (!qdcount) { 1451363f04cSPaul Traina if (hp->aa) 1461363f04cSPaul Traina h_errno = HOST_NOT_FOUND; 1471363f04cSPaul Traina else 1481363f04cSPaul Traina h_errno = TRY_AGAIN; 1494faad310SPeter Wemm return (NULL); 1501363f04cSPaul Traina } 1514faad310SPeter Wemm while (qdcount-- > 0) 1521363f04cSPaul Traina cp += __dn_skipname(cp, eom) + QFIXEDSZ; 1531363f04cSPaul Traina ap = net_aliases; 1541363f04cSPaul Traina *ap = NULL; 1551363f04cSPaul Traina net_entry.n_aliases = net_aliases; 1561363f04cSPaul Traina haveanswer = 0; 1571363f04cSPaul Traina while (--ancount >= 0 && cp < eom) { 1581363f04cSPaul Traina n = dn_expand(answer->buf, eom, cp, bp, buflen); 1595ce1c533SPeter Wemm if ((n < 0) || !res_dnok(bp)) 1601363f04cSPaul Traina break; 1611363f04cSPaul Traina cp += n; 1621363f04cSPaul Traina ans[0] = '\0'; 1633c8e19e1SWarner Losh (void)strncpy(&ans[0], bp, sizeof(ans) - 1); 1643c8e19e1SWarner Losh ans[sizeof(ans) - 1] = '\0'; 1651363f04cSPaul Traina GETSHORT(type, cp); 1661363f04cSPaul Traina GETSHORT(class, cp); 1671363f04cSPaul Traina cp += INT32SZ; /* TTL */ 1681363f04cSPaul Traina GETSHORT(n, cp); 1691363f04cSPaul Traina if (class == C_IN && type == T_PTR) { 1701363f04cSPaul Traina n = dn_expand(answer->buf, eom, cp, bp, buflen); 1715ce1c533SPeter Wemm if ((n < 0) || !res_hnok(bp)) { 1721363f04cSPaul Traina cp += n; 1731363f04cSPaul Traina return (NULL); 1741363f04cSPaul Traina } 1751363f04cSPaul Traina cp += n; 1761363f04cSPaul Traina *ap++ = bp; 1774faad310SPeter Wemm bp += strlen(bp) + 1; 1784faad310SPeter Wemm net_entry.n_addrtype = 1794faad310SPeter Wemm (class == C_IN) ? AF_INET : AF_UNSPEC; 1801363f04cSPaul Traina haveanswer++; 1811363f04cSPaul Traina } 1821363f04cSPaul Traina } 1831363f04cSPaul Traina if (haveanswer) { 1841363f04cSPaul Traina *ap = NULL; 1851363f04cSPaul Traina switch (net_i) { 1861363f04cSPaul Traina case BYADDR: 1871363f04cSPaul Traina net_entry.n_name = *net_entry.n_aliases; 1881363f04cSPaul Traina net_entry.n_net = 0L; 1891363f04cSPaul Traina break; 1901363f04cSPaul Traina case BYNAME: 1911363f04cSPaul Traina in = *net_entry.n_aliases; 1921363f04cSPaul Traina net_entry.n_name = &ans[0]; 1931363f04cSPaul Traina aux2[0] = '\0'; 1941363f04cSPaul Traina for (i = 0; i < 4; i++) { 1951363f04cSPaul Traina for (st = in, nchar = 0; 1961363f04cSPaul Traina *st != '.'; 1971363f04cSPaul Traina st++, nchar++) 1981363f04cSPaul Traina ; 1991363f04cSPaul Traina if (nchar != 1 || *in != '0' || flag) { 2001363f04cSPaul Traina flag = 1; 2011363f04cSPaul Traina (void)strncpy(paux1, 2021363f04cSPaul Traina (i==0) ? in : in-1, 2031363f04cSPaul Traina (i==0) ?nchar : nchar+1); 2041363f04cSPaul Traina paux1[(i==0) ? nchar : nchar+1] = '\0'; 2051363f04cSPaul Traina pauxt = paux2; 2061363f04cSPaul Traina paux2 = strcat(paux1, paux2); 2071363f04cSPaul Traina paux1 = pauxt; 2081363f04cSPaul Traina } 2091363f04cSPaul Traina in = ++st; 2101363f04cSPaul Traina } 2111363f04cSPaul Traina net_entry.n_net = inet_network(paux2); 2124faad310SPeter Wemm break; 2131363f04cSPaul Traina } 2141363f04cSPaul Traina net_entry.n_aliases++; 2151363f04cSPaul Traina return (&net_entry); 2161363f04cSPaul Traina } 2174faad310SPeter Wemm h_errno = TRY_AGAIN; 2184faad310SPeter Wemm return (NULL); 2191363f04cSPaul Traina } 2201363f04cSPaul Traina 2211363f04cSPaul Traina struct netent * 2221363f04cSPaul Traina _getnetbydnsaddr(net, net_type) 2235ce1c533SPeter Wemm register unsigned long net; 2241363f04cSPaul Traina register int net_type; 2251363f04cSPaul Traina { 2261363f04cSPaul Traina unsigned int netbr[4]; 2271363f04cSPaul Traina int nn, anslen; 2281363f04cSPaul Traina querybuf buf; 2291363f04cSPaul Traina char qbuf[MAXDNAME]; 2301363f04cSPaul Traina unsigned long net2; 2311363f04cSPaul Traina struct netent *net_entry; 2321363f04cSPaul Traina 2331363f04cSPaul Traina if (net_type != AF_INET) 2341363f04cSPaul Traina return (NULL); 2351363f04cSPaul Traina 2364faad310SPeter Wemm for (nn = 4, net2 = net; net2; net2 >>= 8) 2371363f04cSPaul Traina netbr[--nn] = net2 & 0xff; 2381363f04cSPaul Traina switch (nn) { 2391363f04cSPaul Traina case 3: /* Class A */ 2404faad310SPeter Wemm sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 2411363f04cSPaul Traina break; 2421363f04cSPaul Traina case 2: /* Class B */ 2434faad310SPeter Wemm sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 2441363f04cSPaul Traina break; 2451363f04cSPaul Traina case 1: /* Class C */ 2464faad310SPeter Wemm sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2474faad310SPeter Wemm netbr[1]); 2481363f04cSPaul Traina break; 2491363f04cSPaul Traina case 0: /* Class D - E */ 2504faad310SPeter Wemm sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2514faad310SPeter Wemm netbr[1], netbr[0]); 2521363f04cSPaul Traina break; 2531363f04cSPaul Traina } 2544faad310SPeter Wemm anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 2551363f04cSPaul Traina if (anslen < 0) { 2564faad310SPeter Wemm #ifdef DEBUG 2571363f04cSPaul Traina if (_res.options & RES_DEBUG) 2581363f04cSPaul Traina printf("res_query failed\n"); 2594faad310SPeter Wemm #endif 2601363f04cSPaul Traina return (NULL); 2611363f04cSPaul Traina } 2621363f04cSPaul Traina net_entry = getnetanswer(&buf, anslen, BYADDR); 2631363f04cSPaul Traina if (net_entry) { 2641363f04cSPaul Traina unsigned u_net = net; /* maybe net should be unsigned ? */ 2651363f04cSPaul Traina 2661363f04cSPaul Traina /* Strip trailing zeros */ 2674faad310SPeter Wemm while ((u_net & 0xff) == 0 && u_net != 0) 2681363f04cSPaul Traina u_net >>= 8; 2691363f04cSPaul Traina net_entry->n_net = u_net; 2701363f04cSPaul Traina return (net_entry); 2711363f04cSPaul Traina } 2724faad310SPeter Wemm return (NULL); 2734faad310SPeter Wemm } 2741363f04cSPaul Traina 2751363f04cSPaul Traina struct netent * 2761363f04cSPaul Traina _getnetbydnsname(net) 2771363f04cSPaul Traina register const char *net; 2781363f04cSPaul Traina { 2791363f04cSPaul Traina int anslen; 2801363f04cSPaul Traina querybuf buf; 2811363f04cSPaul Traina char qbuf[MAXDNAME]; 2821363f04cSPaul Traina 2834faad310SPeter Wemm if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 2844faad310SPeter Wemm h_errno = NETDB_INTERNAL; 2854faad310SPeter Wemm return (NULL); 2864faad310SPeter Wemm } 2873c8e19e1SWarner Losh strncpy(qbuf, net, sizeof(qbuf) - 1); 2883c8e19e1SWarner Losh qbuf[sizeof(qbuf) - 1] = '\0'; 2894faad310SPeter Wemm anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 2901363f04cSPaul Traina if (anslen < 0) { 2914faad310SPeter Wemm #ifdef DEBUG 2921363f04cSPaul Traina if (_res.options & RES_DEBUG) 2931363f04cSPaul Traina printf("res_query failed\n"); 2944faad310SPeter Wemm #endif 2954faad310SPeter Wemm return (NULL); 2961363f04cSPaul Traina } 2971363f04cSPaul Traina return getnetanswer(&buf, anslen, BYNAME); 2981363f04cSPaul Traina } 2991363f04cSPaul Traina 3001363f04cSPaul Traina void 3011363f04cSPaul Traina _setnetdnsent(stayopen) 3021363f04cSPaul Traina int stayopen; 3031363f04cSPaul Traina { 3041363f04cSPaul Traina if (stayopen) 3051363f04cSPaul Traina _res.options |= RES_STAYOPEN | RES_USEVC; 3061363f04cSPaul Traina } 3071363f04cSPaul Traina 3081363f04cSPaul Traina void 3091363f04cSPaul Traina _endnetdnsent() 3101363f04cSPaul Traina { 3111363f04cSPaul Traina _res.options &= ~(RES_STAYOPEN | RES_USEVC); 312ca785773SPeter Wemm res_close(); 3131363f04cSPaul Traina } 314