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"; 637f3dea24SPeter Wemm static char rcsid[] = "$FreeBSD$"; 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 <string.h> 774faad310SPeter Wemm #include <unistd.h> 781363f04cSPaul Traina #include <syslog.h> 79248aee62SJacques Vidrine #include <stdarg.h> 80248aee62SJacques Vidrine #include <nsswitch.h> 811363f04cSPaul Traina 824faad310SPeter Wemm #include "res_config.h" 831363f04cSPaul Traina 844faad310SPeter Wemm extern int h_errno; 854faad310SPeter Wemm 864faad310SPeter Wemm #define BYADDR 0 874faad310SPeter Wemm #define BYNAME 1 881363f04cSPaul Traina #define MAXALIASES 35 891363f04cSPaul Traina 901363f04cSPaul Traina #if PACKETSZ > 1024 911363f04cSPaul Traina #define MAXPACKET PACKETSZ 921363f04cSPaul Traina #else 931363f04cSPaul Traina #define MAXPACKET 1024 941363f04cSPaul Traina #endif 951363f04cSPaul Traina 961363f04cSPaul Traina typedef union { 971363f04cSPaul Traina HEADER hdr; 981363f04cSPaul Traina u_char buf[MAXPACKET]; 991363f04cSPaul Traina } querybuf; 1001363f04cSPaul Traina 1011363f04cSPaul Traina typedef union { 1024faad310SPeter Wemm long al; 1031363f04cSPaul Traina char ac; 1041363f04cSPaul Traina } align; 1051363f04cSPaul Traina 1061363f04cSPaul Traina static struct netent * 1071363f04cSPaul Traina getnetanswer(answer, anslen, net_i) 1081363f04cSPaul Traina querybuf *answer; 1091363f04cSPaul Traina int anslen; 1101363f04cSPaul Traina int net_i; 1111363f04cSPaul Traina { 1121363f04cSPaul Traina 1138fb3f3f6SDavid E. O'Brien HEADER *hp; 1148fb3f3f6SDavid E. O'Brien u_char *cp; 1158fb3f3f6SDavid E. O'Brien int n; 1161363f04cSPaul Traina u_char *eom; 1174faad310SPeter Wemm int type, class, buflen, ancount, qdcount, haveanswer, i, nchar; 1183c8e19e1SWarner Losh char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; 1193c8e19e1SWarner Losh char *in, *st, *pauxt, *bp, **ap; 1203c8e19e1SWarner Losh char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; 1211363f04cSPaul Traina static struct netent net_entry; 1226c5aff80SPeter Wemm static char *net_aliases[MAXALIASES], netbuf[PACKETSZ]; 1231363f04cSPaul Traina 1241363f04cSPaul Traina /* 1251363f04cSPaul Traina * find first satisfactory answer 1261363f04cSPaul Traina * 1271363f04cSPaul Traina * answer --> +------------+ ( MESSAGE ) 1281363f04cSPaul Traina * | Header | 1291363f04cSPaul Traina * +------------+ 1301363f04cSPaul Traina * | Question | the question for the name server 1311363f04cSPaul Traina * +------------+ 1321363f04cSPaul Traina * | Answer | RRs answering the question 1331363f04cSPaul Traina * +------------+ 1341363f04cSPaul Traina * | Authority | RRs pointing toward an authority 1351363f04cSPaul Traina * | Additional | RRs holding additional information 1361363f04cSPaul Traina * +------------+ 1371363f04cSPaul Traina */ 1381363f04cSPaul Traina eom = answer->buf + anslen; 1391363f04cSPaul Traina hp = &answer->hdr; 1401363f04cSPaul Traina ancount = ntohs(hp->ancount); /* #/records in the answer section */ 1411363f04cSPaul Traina qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 1421363f04cSPaul Traina bp = netbuf; 1431363f04cSPaul Traina buflen = sizeof(netbuf); 1441363f04cSPaul Traina cp = answer->buf + HFIXEDSZ; 1451363f04cSPaul Traina if (!qdcount) { 1461363f04cSPaul Traina if (hp->aa) 1471363f04cSPaul Traina h_errno = HOST_NOT_FOUND; 1481363f04cSPaul Traina else 1491363f04cSPaul Traina h_errno = TRY_AGAIN; 1504faad310SPeter Wemm return (NULL); 1511363f04cSPaul Traina } 1524faad310SPeter Wemm while (qdcount-- > 0) 1531363f04cSPaul Traina cp += __dn_skipname(cp, eom) + QFIXEDSZ; 1541363f04cSPaul Traina ap = net_aliases; 1551363f04cSPaul Traina *ap = NULL; 1561363f04cSPaul Traina net_entry.n_aliases = net_aliases; 1571363f04cSPaul Traina haveanswer = 0; 1581363f04cSPaul Traina while (--ancount >= 0 && cp < eom) { 1591363f04cSPaul Traina n = dn_expand(answer->buf, eom, cp, bp, buflen); 1605ce1c533SPeter Wemm if ((n < 0) || !res_dnok(bp)) 1611363f04cSPaul Traina break; 1621363f04cSPaul Traina cp += n; 1631363f04cSPaul Traina ans[0] = '\0'; 1643c8e19e1SWarner Losh (void)strncpy(&ans[0], bp, sizeof(ans) - 1); 1653c8e19e1SWarner Losh ans[sizeof(ans) - 1] = '\0'; 1661363f04cSPaul Traina GETSHORT(type, cp); 1671363f04cSPaul Traina GETSHORT(class, cp); 1681363f04cSPaul Traina cp += INT32SZ; /* TTL */ 1691363f04cSPaul Traina GETSHORT(n, cp); 1701363f04cSPaul Traina if (class == C_IN && type == T_PTR) { 1711363f04cSPaul Traina n = dn_expand(answer->buf, eom, cp, bp, buflen); 1725ce1c533SPeter Wemm if ((n < 0) || !res_hnok(bp)) { 1731363f04cSPaul Traina cp += n; 1741363f04cSPaul Traina return (NULL); 1751363f04cSPaul Traina } 1761363f04cSPaul Traina cp += n; 1771363f04cSPaul Traina *ap++ = bp; 1784faad310SPeter Wemm bp += strlen(bp) + 1; 1794faad310SPeter Wemm net_entry.n_addrtype = 1804faad310SPeter Wemm (class == C_IN) ? AF_INET : AF_UNSPEC; 1811363f04cSPaul Traina haveanswer++; 1821363f04cSPaul Traina } 1831363f04cSPaul Traina } 1841363f04cSPaul Traina if (haveanswer) { 1851363f04cSPaul Traina *ap = NULL; 1861363f04cSPaul Traina switch (net_i) { 1871363f04cSPaul Traina case BYADDR: 1881363f04cSPaul Traina net_entry.n_name = *net_entry.n_aliases; 1891363f04cSPaul Traina net_entry.n_net = 0L; 1901363f04cSPaul Traina break; 1911363f04cSPaul Traina case BYNAME: 1921363f04cSPaul Traina in = *net_entry.n_aliases; 1931363f04cSPaul Traina net_entry.n_name = &ans[0]; 1941363f04cSPaul Traina aux2[0] = '\0'; 1951363f04cSPaul Traina for (i = 0; i < 4; i++) { 1961363f04cSPaul Traina for (st = in, nchar = 0; 1971363f04cSPaul Traina *st != '.'; 1981363f04cSPaul Traina st++, nchar++) 1991363f04cSPaul Traina ; 2001363f04cSPaul Traina if (nchar != 1 || *in != '0' || flag) { 2011363f04cSPaul Traina flag = 1; 2021363f04cSPaul Traina (void)strncpy(paux1, 2031363f04cSPaul Traina (i==0) ? in : in-1, 2041363f04cSPaul Traina (i==0) ?nchar : nchar+1); 2051363f04cSPaul Traina paux1[(i==0) ? nchar : nchar+1] = '\0'; 2061363f04cSPaul Traina pauxt = paux2; 2071363f04cSPaul Traina paux2 = strcat(paux1, paux2); 2081363f04cSPaul Traina paux1 = pauxt; 2091363f04cSPaul Traina } 2101363f04cSPaul Traina in = ++st; 2111363f04cSPaul Traina } 2121363f04cSPaul Traina net_entry.n_net = inet_network(paux2); 2134faad310SPeter Wemm break; 2141363f04cSPaul Traina } 2151363f04cSPaul Traina net_entry.n_aliases++; 2161363f04cSPaul Traina return (&net_entry); 2171363f04cSPaul Traina } 2184faad310SPeter Wemm h_errno = TRY_AGAIN; 2194faad310SPeter Wemm return (NULL); 2201363f04cSPaul Traina } 2211363f04cSPaul Traina 222248aee62SJacques Vidrine int 223248aee62SJacques Vidrine _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap) 2241363f04cSPaul Traina { 225248aee62SJacques Vidrine unsigned long net; 226248aee62SJacques Vidrine int net_type; 2271363f04cSPaul Traina unsigned int netbr[4]; 2281363f04cSPaul Traina int nn, anslen; 2291363f04cSPaul Traina querybuf buf; 2301363f04cSPaul Traina char qbuf[MAXDNAME]; 2311363f04cSPaul Traina unsigned long net2; 2321363f04cSPaul Traina struct netent *net_entry; 2331363f04cSPaul Traina 234248aee62SJacques Vidrine net = va_arg(ap, unsigned long); 235248aee62SJacques Vidrine net_type = va_arg(ap, int); 236248aee62SJacques Vidrine 237248aee62SJacques Vidrine *(struct netent **)rval = NULL; 238248aee62SJacques Vidrine 2391363f04cSPaul Traina if (net_type != AF_INET) 240248aee62SJacques Vidrine return NS_UNAVAIL; 2411363f04cSPaul Traina 2424faad310SPeter Wemm for (nn = 4, net2 = net; net2; net2 >>= 8) 2431363f04cSPaul Traina netbr[--nn] = net2 & 0xff; 2441363f04cSPaul Traina switch (nn) { 2451363f04cSPaul Traina case 3: /* Class A */ 2464faad310SPeter Wemm sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 2471363f04cSPaul Traina break; 2481363f04cSPaul Traina case 2: /* Class B */ 2494faad310SPeter Wemm sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 2501363f04cSPaul Traina break; 2511363f04cSPaul Traina case 1: /* Class C */ 2524faad310SPeter Wemm sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2534faad310SPeter Wemm netbr[1]); 2541363f04cSPaul Traina break; 2551363f04cSPaul Traina case 0: /* Class D - E */ 2564faad310SPeter Wemm sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2574faad310SPeter Wemm netbr[1], netbr[0]); 2581363f04cSPaul Traina break; 2591363f04cSPaul Traina } 2604faad310SPeter Wemm anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 2611363f04cSPaul Traina if (anslen < 0) { 2624faad310SPeter Wemm #ifdef DEBUG 2631363f04cSPaul Traina if (_res.options & RES_DEBUG) 2641363f04cSPaul Traina printf("res_query failed\n"); 2654faad310SPeter Wemm #endif 266248aee62SJacques Vidrine return NS_UNAVAIL; 2671363f04cSPaul Traina } 2681363f04cSPaul Traina net_entry = getnetanswer(&buf, anslen, BYADDR); 2691363f04cSPaul Traina if (net_entry) { 2701363f04cSPaul Traina unsigned u_net = net; /* maybe net should be unsigned ? */ 2711363f04cSPaul Traina 2721363f04cSPaul Traina /* Strip trailing zeros */ 2734faad310SPeter Wemm while ((u_net & 0xff) == 0 && u_net != 0) 2741363f04cSPaul Traina u_net >>= 8; 2751363f04cSPaul Traina net_entry->n_net = u_net; 276248aee62SJacques Vidrine *(struct netent **)rval = net_entry; 277248aee62SJacques Vidrine return NS_SUCCESS; 2781363f04cSPaul Traina } 279248aee62SJacques Vidrine return NS_NOTFOUND; 2804faad310SPeter Wemm } 2811363f04cSPaul Traina 282248aee62SJacques Vidrine int 283248aee62SJacques Vidrine _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 2841363f04cSPaul Traina { 285248aee62SJacques Vidrine const char *net; 2861363f04cSPaul Traina int anslen; 2871363f04cSPaul Traina querybuf buf; 2881363f04cSPaul Traina char qbuf[MAXDNAME]; 2891363f04cSPaul Traina 290248aee62SJacques Vidrine net = va_arg(ap, const char *); 291248aee62SJacques Vidrine 292248aee62SJacques Vidrine *(struct netent**)rval = NULL; 293248aee62SJacques Vidrine 2944faad310SPeter Wemm if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 2954faad310SPeter Wemm h_errno = NETDB_INTERNAL; 296248aee62SJacques Vidrine return NS_UNAVAIL; 2974faad310SPeter Wemm } 2983c8e19e1SWarner Losh strncpy(qbuf, net, sizeof(qbuf) - 1); 2993c8e19e1SWarner Losh qbuf[sizeof(qbuf) - 1] = '\0'; 3004faad310SPeter Wemm anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 3011363f04cSPaul Traina if (anslen < 0) { 3024faad310SPeter Wemm #ifdef DEBUG 3031363f04cSPaul Traina if (_res.options & RES_DEBUG) 3041363f04cSPaul Traina printf("res_query failed\n"); 3054faad310SPeter Wemm #endif 306248aee62SJacques Vidrine return NS_UNAVAIL; 3071363f04cSPaul Traina } 308248aee62SJacques Vidrine *(struct netent**)rval = getnetanswer(&buf, anslen, BYNAME); 309248aee62SJacques Vidrine return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 3101363f04cSPaul Traina } 3111363f04cSPaul Traina 3121363f04cSPaul Traina void 3131363f04cSPaul Traina _setnetdnsent(stayopen) 3141363f04cSPaul Traina int stayopen; 3151363f04cSPaul Traina { 3161363f04cSPaul Traina if (stayopen) 3171363f04cSPaul Traina _res.options |= RES_STAYOPEN | RES_USEVC; 3181363f04cSPaul Traina } 3191363f04cSPaul Traina 3201363f04cSPaul Traina void 3211363f04cSPaul Traina _endnetdnsent() 3221363f04cSPaul Traina { 3231363f04cSPaul Traina _res.options &= ~(RES_STAYOPEN | RES_USEVC); 324ca785773SPeter Wemm res_close(); 3251363f04cSPaul Traina } 326