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"; 631363f04cSPaul Traina #endif /* LIBC_SCCS and not lint */ 64333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 65333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 661363f04cSPaul Traina 671363f04cSPaul Traina #include <sys/param.h> 681363f04cSPaul Traina #include <sys/socket.h> 691363f04cSPaul Traina #include <netinet/in.h> 701363f04cSPaul Traina #include <arpa/inet.h> 711363f04cSPaul Traina #include <arpa/nameser.h> 724faad310SPeter Wemm 734faad310SPeter Wemm #include <stdio.h> 741363f04cSPaul Traina #include <netdb.h> 751363f04cSPaul Traina #include <resolv.h> 761363f04cSPaul Traina #include <ctype.h> 771363f04cSPaul Traina #include <string.h> 784faad310SPeter Wemm #include <unistd.h> 791363f04cSPaul Traina #include <syslog.h> 80248aee62SJacques Vidrine #include <stdarg.h> 81248aee62SJacques Vidrine #include <nsswitch.h> 821363f04cSPaul Traina 834faad310SPeter Wemm #include "res_config.h" 841363f04cSPaul Traina 854faad310SPeter Wemm extern int h_errno; 864faad310SPeter Wemm 874faad310SPeter Wemm #define BYADDR 0 884faad310SPeter Wemm #define BYNAME 1 891363f04cSPaul Traina #define MAXALIASES 35 901363f04cSPaul Traina 911363f04cSPaul Traina #if PACKETSZ > 1024 921363f04cSPaul Traina #define MAXPACKET PACKETSZ 931363f04cSPaul Traina #else 941363f04cSPaul Traina #define MAXPACKET 1024 951363f04cSPaul Traina #endif 961363f04cSPaul Traina 971363f04cSPaul Traina typedef union { 981363f04cSPaul Traina HEADER hdr; 991363f04cSPaul Traina u_char buf[MAXPACKET]; 1001363f04cSPaul Traina } querybuf; 1011363f04cSPaul Traina 1021363f04cSPaul Traina typedef union { 1034faad310SPeter Wemm long al; 1041363f04cSPaul Traina char ac; 1051363f04cSPaul Traina } align; 1061363f04cSPaul Traina 1071363f04cSPaul Traina static struct netent * 1081363f04cSPaul Traina getnetanswer(answer, anslen, net_i) 1091363f04cSPaul Traina querybuf *answer; 1101363f04cSPaul Traina int anslen; 1111363f04cSPaul Traina int net_i; 1121363f04cSPaul Traina { 1131363f04cSPaul Traina 1148fb3f3f6SDavid E. O'Brien HEADER *hp; 1158fb3f3f6SDavid E. O'Brien u_char *cp; 1168fb3f3f6SDavid E. O'Brien int n; 1171363f04cSPaul Traina u_char *eom; 1184faad310SPeter Wemm int type, class, buflen, ancount, qdcount, haveanswer, i, nchar; 1193c8e19e1SWarner Losh char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; 1203c8e19e1SWarner Losh char *in, *st, *pauxt, *bp, **ap; 1213c8e19e1SWarner Losh char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; 1221363f04cSPaul Traina static struct netent net_entry; 1236c5aff80SPeter Wemm static char *net_aliases[MAXALIASES], netbuf[PACKETSZ]; 1241363f04cSPaul Traina 1251363f04cSPaul Traina /* 1261363f04cSPaul Traina * find first satisfactory answer 1271363f04cSPaul Traina * 1281363f04cSPaul Traina * answer --> +------------+ ( MESSAGE ) 1291363f04cSPaul Traina * | Header | 1301363f04cSPaul Traina * +------------+ 1311363f04cSPaul Traina * | Question | the question for the name server 1321363f04cSPaul Traina * +------------+ 1331363f04cSPaul Traina * | Answer | RRs answering the question 1341363f04cSPaul Traina * +------------+ 1351363f04cSPaul Traina * | Authority | RRs pointing toward an authority 1361363f04cSPaul Traina * | Additional | RRs holding additional information 1371363f04cSPaul Traina * +------------+ 1381363f04cSPaul Traina */ 1391363f04cSPaul Traina eom = answer->buf + anslen; 1401363f04cSPaul Traina hp = &answer->hdr; 1411363f04cSPaul Traina ancount = ntohs(hp->ancount); /* #/records in the answer section */ 1421363f04cSPaul Traina qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 1431363f04cSPaul Traina bp = netbuf; 1441363f04cSPaul Traina buflen = sizeof(netbuf); 1451363f04cSPaul Traina cp = answer->buf + HFIXEDSZ; 1461363f04cSPaul Traina if (!qdcount) { 1471363f04cSPaul Traina if (hp->aa) 1481363f04cSPaul Traina h_errno = HOST_NOT_FOUND; 1491363f04cSPaul Traina else 1501363f04cSPaul Traina h_errno = TRY_AGAIN; 1514faad310SPeter Wemm return (NULL); 1521363f04cSPaul Traina } 1534faad310SPeter Wemm while (qdcount-- > 0) 1541363f04cSPaul Traina cp += __dn_skipname(cp, eom) + QFIXEDSZ; 1551363f04cSPaul Traina ap = net_aliases; 1561363f04cSPaul Traina *ap = NULL; 1571363f04cSPaul Traina net_entry.n_aliases = net_aliases; 1581363f04cSPaul Traina haveanswer = 0; 1591363f04cSPaul Traina while (--ancount >= 0 && cp < eom) { 1601363f04cSPaul Traina n = dn_expand(answer->buf, eom, cp, bp, buflen); 1615ce1c533SPeter Wemm if ((n < 0) || !res_dnok(bp)) 1621363f04cSPaul Traina break; 1631363f04cSPaul Traina cp += n; 1641363f04cSPaul Traina ans[0] = '\0'; 1653c8e19e1SWarner Losh (void)strncpy(&ans[0], bp, sizeof(ans) - 1); 1663c8e19e1SWarner Losh ans[sizeof(ans) - 1] = '\0'; 1671363f04cSPaul Traina GETSHORT(type, cp); 1681363f04cSPaul Traina GETSHORT(class, cp); 1691363f04cSPaul Traina cp += INT32SZ; /* TTL */ 1701363f04cSPaul Traina GETSHORT(n, cp); 1711363f04cSPaul Traina if (class == C_IN && type == T_PTR) { 1721363f04cSPaul Traina n = dn_expand(answer->buf, eom, cp, bp, buflen); 1735ce1c533SPeter Wemm if ((n < 0) || !res_hnok(bp)) { 1741363f04cSPaul Traina cp += n; 1751363f04cSPaul Traina return (NULL); 1761363f04cSPaul Traina } 1771363f04cSPaul Traina cp += n; 1781363f04cSPaul Traina *ap++ = bp; 1794cbd2472SWarner Losh n = strlen(bp) + 1; 1804cbd2472SWarner Losh bp += n; 1814cbd2472SWarner Losh buflen -= n; 1824faad310SPeter Wemm net_entry.n_addrtype = 1834faad310SPeter Wemm (class == C_IN) ? AF_INET : AF_UNSPEC; 1841363f04cSPaul Traina haveanswer++; 1851363f04cSPaul Traina } 1861363f04cSPaul Traina } 1871363f04cSPaul Traina if (haveanswer) { 1881363f04cSPaul Traina *ap = NULL; 1891363f04cSPaul Traina switch (net_i) { 1901363f04cSPaul Traina case BYADDR: 1911363f04cSPaul Traina net_entry.n_name = *net_entry.n_aliases; 1921363f04cSPaul Traina net_entry.n_net = 0L; 1931363f04cSPaul Traina break; 1941363f04cSPaul Traina case BYNAME: 1951363f04cSPaul Traina in = *net_entry.n_aliases; 1961363f04cSPaul Traina net_entry.n_name = &ans[0]; 1971363f04cSPaul Traina aux2[0] = '\0'; 1981363f04cSPaul Traina for (i = 0; i < 4; i++) { 1991363f04cSPaul Traina for (st = in, nchar = 0; 2001363f04cSPaul Traina *st != '.'; 2011363f04cSPaul Traina st++, nchar++) 2021363f04cSPaul Traina ; 2031363f04cSPaul Traina if (nchar != 1 || *in != '0' || flag) { 2041363f04cSPaul Traina flag = 1; 2051363f04cSPaul Traina (void)strncpy(paux1, 2061363f04cSPaul Traina (i==0) ? in : in-1, 2071363f04cSPaul Traina (i==0) ?nchar : nchar+1); 2081363f04cSPaul Traina paux1[(i==0) ? nchar : nchar+1] = '\0'; 2091363f04cSPaul Traina pauxt = paux2; 2101363f04cSPaul Traina paux2 = strcat(paux1, paux2); 2111363f04cSPaul Traina paux1 = pauxt; 2121363f04cSPaul Traina } 2131363f04cSPaul Traina in = ++st; 2141363f04cSPaul Traina } 2151363f04cSPaul Traina net_entry.n_net = inet_network(paux2); 2164faad310SPeter Wemm break; 2171363f04cSPaul Traina } 2181363f04cSPaul Traina net_entry.n_aliases++; 2191363f04cSPaul Traina return (&net_entry); 2201363f04cSPaul Traina } 2214faad310SPeter Wemm h_errno = TRY_AGAIN; 2224faad310SPeter Wemm return (NULL); 2231363f04cSPaul Traina } 2241363f04cSPaul Traina 225248aee62SJacques Vidrine int 226248aee62SJacques Vidrine _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap) 2271363f04cSPaul Traina { 228248aee62SJacques Vidrine unsigned long net; 229248aee62SJacques Vidrine int net_type; 2301363f04cSPaul Traina unsigned int netbr[4]; 2311363f04cSPaul Traina int nn, anslen; 2321363f04cSPaul Traina querybuf buf; 2331363f04cSPaul Traina char qbuf[MAXDNAME]; 2341363f04cSPaul Traina unsigned long net2; 2351363f04cSPaul Traina struct netent *net_entry; 2361363f04cSPaul Traina 237248aee62SJacques Vidrine net = va_arg(ap, unsigned long); 238248aee62SJacques Vidrine net_type = va_arg(ap, int); 239248aee62SJacques Vidrine 240248aee62SJacques Vidrine *(struct netent **)rval = NULL; 241248aee62SJacques Vidrine 2421363f04cSPaul Traina if (net_type != AF_INET) 243248aee62SJacques Vidrine return NS_UNAVAIL; 2441363f04cSPaul Traina 2454faad310SPeter Wemm for (nn = 4, net2 = net; net2; net2 >>= 8) 2461363f04cSPaul Traina netbr[--nn] = net2 & 0xff; 2471363f04cSPaul Traina switch (nn) { 2481363f04cSPaul Traina case 3: /* Class A */ 2494faad310SPeter Wemm sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 2501363f04cSPaul Traina break; 2511363f04cSPaul Traina case 2: /* Class B */ 2524faad310SPeter Wemm sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 2531363f04cSPaul Traina break; 2541363f04cSPaul Traina case 1: /* Class C */ 2554faad310SPeter Wemm sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2564faad310SPeter Wemm netbr[1]); 2571363f04cSPaul Traina break; 2581363f04cSPaul Traina case 0: /* Class D - E */ 2594faad310SPeter Wemm sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2604faad310SPeter Wemm netbr[1], netbr[0]); 2611363f04cSPaul Traina break; 2621363f04cSPaul Traina } 2634faad310SPeter Wemm anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 2641363f04cSPaul Traina if (anslen < 0) { 2654faad310SPeter Wemm #ifdef DEBUG 2661363f04cSPaul Traina if (_res.options & RES_DEBUG) 2671363f04cSPaul Traina printf("res_query failed\n"); 2684faad310SPeter Wemm #endif 269248aee62SJacques Vidrine return NS_UNAVAIL; 2701363f04cSPaul Traina } 2711363f04cSPaul Traina net_entry = getnetanswer(&buf, anslen, BYADDR); 2721363f04cSPaul Traina if (net_entry) { 2731363f04cSPaul Traina unsigned u_net = net; /* maybe net should be unsigned ? */ 2741363f04cSPaul Traina 2751363f04cSPaul Traina /* Strip trailing zeros */ 2764faad310SPeter Wemm while ((u_net & 0xff) == 0 && u_net != 0) 2771363f04cSPaul Traina u_net >>= 8; 2781363f04cSPaul Traina net_entry->n_net = u_net; 279248aee62SJacques Vidrine *(struct netent **)rval = net_entry; 280248aee62SJacques Vidrine return NS_SUCCESS; 2811363f04cSPaul Traina } 282248aee62SJacques Vidrine return NS_NOTFOUND; 2834faad310SPeter Wemm } 2841363f04cSPaul Traina 285248aee62SJacques Vidrine int 286248aee62SJacques Vidrine _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 2871363f04cSPaul Traina { 288248aee62SJacques Vidrine const char *net; 2891363f04cSPaul Traina int anslen; 2901363f04cSPaul Traina querybuf buf; 2911363f04cSPaul Traina char qbuf[MAXDNAME]; 2921363f04cSPaul Traina 293248aee62SJacques Vidrine net = va_arg(ap, const char *); 294248aee62SJacques Vidrine 295248aee62SJacques Vidrine *(struct netent**)rval = NULL; 296248aee62SJacques Vidrine 2974faad310SPeter Wemm if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 2984faad310SPeter Wemm h_errno = NETDB_INTERNAL; 299248aee62SJacques Vidrine return NS_UNAVAIL; 3004faad310SPeter Wemm } 3013c8e19e1SWarner Losh strncpy(qbuf, net, sizeof(qbuf) - 1); 3023c8e19e1SWarner Losh qbuf[sizeof(qbuf) - 1] = '\0'; 3034faad310SPeter Wemm anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 3041363f04cSPaul Traina if (anslen < 0) { 3054faad310SPeter Wemm #ifdef DEBUG 3061363f04cSPaul Traina if (_res.options & RES_DEBUG) 3071363f04cSPaul Traina printf("res_query failed\n"); 3084faad310SPeter Wemm #endif 309248aee62SJacques Vidrine return NS_UNAVAIL; 3101363f04cSPaul Traina } 311248aee62SJacques Vidrine *(struct netent**)rval = getnetanswer(&buf, anslen, BYNAME); 312248aee62SJacques Vidrine return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 3131363f04cSPaul Traina } 3141363f04cSPaul Traina 3151363f04cSPaul Traina void 3161363f04cSPaul Traina _setnetdnsent(stayopen) 3171363f04cSPaul Traina int stayopen; 3181363f04cSPaul Traina { 3191363f04cSPaul Traina if (stayopen) 3201363f04cSPaul Traina _res.options |= RES_STAYOPEN | RES_USEVC; 3211363f04cSPaul Traina } 3221363f04cSPaul Traina 3231363f04cSPaul Traina void 3241363f04cSPaul Traina _endnetdnsent() 3251363f04cSPaul Traina { 3261363f04cSPaul Traina _res.options &= ~(RES_STAYOPEN | RES_USEVC); 327ca785773SPeter Wemm res_close(); 3281363f04cSPaul Traina } 329