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> 74a52b6b83SHajimu UMEMOTO #include <stdlib.h> 751363f04cSPaul Traina #include <netdb.h> 761363f04cSPaul Traina #include <resolv.h> 771363f04cSPaul Traina #include <ctype.h> 781363f04cSPaul Traina #include <string.h> 794faad310SPeter Wemm #include <unistd.h> 801363f04cSPaul Traina #include <syslog.h> 81248aee62SJacques Vidrine #include <stdarg.h> 82248aee62SJacques Vidrine #include <nsswitch.h> 831363f04cSPaul Traina 844faad310SPeter Wemm #include "res_config.h" 851363f04cSPaul Traina 864faad310SPeter Wemm extern int h_errno; 874faad310SPeter Wemm 884faad310SPeter Wemm #define BYADDR 0 894faad310SPeter Wemm #define BYNAME 1 901363f04cSPaul Traina #define MAXALIASES 35 911363f04cSPaul Traina 92a52b6b83SHajimu UMEMOTO #define MAXPACKET (64*1024) 931363f04cSPaul Traina 941363f04cSPaul Traina typedef union { 951363f04cSPaul Traina HEADER hdr; 961363f04cSPaul Traina u_char buf[MAXPACKET]; 971363f04cSPaul Traina } querybuf; 981363f04cSPaul Traina 991363f04cSPaul Traina typedef union { 1004faad310SPeter Wemm long al; 1011363f04cSPaul Traina char ac; 1021363f04cSPaul Traina } align; 1031363f04cSPaul Traina 1041363f04cSPaul Traina static struct netent * 1051363f04cSPaul Traina getnetanswer(answer, anslen, net_i) 1061363f04cSPaul Traina querybuf *answer; 1071363f04cSPaul Traina int anslen; 1081363f04cSPaul Traina int net_i; 1091363f04cSPaul Traina { 1101363f04cSPaul Traina 1118fb3f3f6SDavid E. O'Brien HEADER *hp; 1128fb3f3f6SDavid E. O'Brien u_char *cp; 1138fb3f3f6SDavid E. O'Brien int n; 1141363f04cSPaul Traina u_char *eom; 115d6af58f5SWarner Losh int type, class, ancount, qdcount, haveanswer, i, nchar; 1163c8e19e1SWarner Losh char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; 117d6af58f5SWarner Losh char *in, *st, *pauxt, *bp, *ep, **ap; 1183c8e19e1SWarner Losh char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; 1191363f04cSPaul Traina static struct netent net_entry; 1206c5aff80SPeter Wemm static char *net_aliases[MAXALIASES], netbuf[PACKETSZ]; 1211363f04cSPaul Traina 1221363f04cSPaul Traina /* 1231363f04cSPaul Traina * find first satisfactory answer 1241363f04cSPaul Traina * 1251363f04cSPaul Traina * answer --> +------------+ ( MESSAGE ) 1261363f04cSPaul Traina * | Header | 1271363f04cSPaul Traina * +------------+ 1281363f04cSPaul Traina * | Question | the question for the name server 1291363f04cSPaul Traina * +------------+ 1301363f04cSPaul Traina * | Answer | RRs answering the question 1311363f04cSPaul Traina * +------------+ 1321363f04cSPaul Traina * | Authority | RRs pointing toward an authority 1331363f04cSPaul Traina * | Additional | RRs holding additional information 1341363f04cSPaul Traina * +------------+ 1351363f04cSPaul Traina */ 1361363f04cSPaul Traina eom = answer->buf + anslen; 1371363f04cSPaul Traina hp = &answer->hdr; 1381363f04cSPaul Traina ancount = ntohs(hp->ancount); /* #/records in the answer section */ 1391363f04cSPaul Traina qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 1401363f04cSPaul Traina bp = netbuf; 1419a370b24SJacques Vidrine ep = netbuf + sizeof(netbuf); 1421363f04cSPaul Traina cp = answer->buf + HFIXEDSZ; 1431363f04cSPaul Traina if (!qdcount) { 1441363f04cSPaul Traina if (hp->aa) 1451363f04cSPaul Traina h_errno = HOST_NOT_FOUND; 1461363f04cSPaul Traina else 1471363f04cSPaul Traina h_errno = TRY_AGAIN; 1484faad310SPeter Wemm return (NULL); 1491363f04cSPaul Traina } 1504faad310SPeter Wemm while (qdcount-- > 0) 1511363f04cSPaul Traina cp += __dn_skipname(cp, eom) + QFIXEDSZ; 1521363f04cSPaul Traina ap = net_aliases; 1531363f04cSPaul Traina *ap = NULL; 1541363f04cSPaul Traina net_entry.n_aliases = net_aliases; 1551363f04cSPaul Traina haveanswer = 0; 1561363f04cSPaul Traina while (--ancount >= 0 && cp < eom) { 157d6af58f5SWarner Losh n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1585ce1c533SPeter Wemm if ((n < 0) || !res_dnok(bp)) 1591363f04cSPaul Traina break; 1601363f04cSPaul Traina cp += n; 1611363f04cSPaul Traina ans[0] = '\0'; 1623c8e19e1SWarner Losh (void)strncpy(&ans[0], bp, sizeof(ans) - 1); 1633c8e19e1SWarner Losh ans[sizeof(ans) - 1] = '\0'; 1641363f04cSPaul Traina GETSHORT(type, cp); 1651363f04cSPaul Traina GETSHORT(class, cp); 1661363f04cSPaul Traina cp += INT32SZ; /* TTL */ 1671363f04cSPaul Traina GETSHORT(n, cp); 1681363f04cSPaul Traina if (class == C_IN && type == T_PTR) { 169d6af58f5SWarner Losh n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1705ce1c533SPeter Wemm if ((n < 0) || !res_hnok(bp)) { 1711363f04cSPaul Traina cp += n; 1721363f04cSPaul Traina return (NULL); 1731363f04cSPaul Traina } 1741363f04cSPaul Traina cp += n; 1751363f04cSPaul Traina *ap++ = bp; 1764cbd2472SWarner Losh n = strlen(bp) + 1; 1774cbd2472SWarner Losh bp += n; 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 221248aee62SJacques Vidrine int 222248aee62SJacques Vidrine _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap) 2231363f04cSPaul Traina { 224248aee62SJacques Vidrine unsigned long net; 225248aee62SJacques Vidrine int net_type; 2261363f04cSPaul Traina unsigned int netbr[4]; 2271363f04cSPaul Traina int nn, anslen; 228a52b6b83SHajimu UMEMOTO querybuf *buf; 2291363f04cSPaul Traina char qbuf[MAXDNAME]; 2301363f04cSPaul Traina unsigned long net2; 2311363f04cSPaul Traina struct netent *net_entry; 2321363f04cSPaul Traina 233248aee62SJacques Vidrine net = va_arg(ap, unsigned long); 234248aee62SJacques Vidrine net_type = va_arg(ap, int); 235248aee62SJacques Vidrine 236248aee62SJacques Vidrine *(struct netent **)rval = NULL; 237248aee62SJacques Vidrine 2381363f04cSPaul Traina if (net_type != AF_INET) 239248aee62SJacques Vidrine return NS_UNAVAIL; 2401363f04cSPaul Traina 2414faad310SPeter Wemm for (nn = 4, net2 = net; net2; net2 >>= 8) 2421363f04cSPaul Traina netbr[--nn] = net2 & 0xff; 2431363f04cSPaul Traina switch (nn) { 2441363f04cSPaul Traina case 3: /* Class A */ 2454faad310SPeter Wemm sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 2461363f04cSPaul Traina break; 2471363f04cSPaul Traina case 2: /* Class B */ 2484faad310SPeter Wemm sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 2491363f04cSPaul Traina break; 2501363f04cSPaul Traina case 1: /* Class C */ 2514faad310SPeter Wemm sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2524faad310SPeter Wemm netbr[1]); 2531363f04cSPaul Traina break; 2541363f04cSPaul Traina case 0: /* Class D - E */ 2554faad310SPeter Wemm sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 2564faad310SPeter Wemm netbr[1], netbr[0]); 2571363f04cSPaul Traina break; 2581363f04cSPaul Traina } 259a52b6b83SHajimu UMEMOTO if ((buf = malloc(sizeof(*buf))) == NULL) { 260a52b6b83SHajimu UMEMOTO h_errno = NETDB_INTERNAL; 261a52b6b83SHajimu UMEMOTO return NS_NOTFOUND; 262a52b6b83SHajimu UMEMOTO } 263a52b6b83SHajimu UMEMOTO anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf)); 2641363f04cSPaul Traina if (anslen < 0) { 265a52b6b83SHajimu UMEMOTO free(buf); 2664faad310SPeter Wemm #ifdef DEBUG 2671363f04cSPaul Traina if (_res.options & RES_DEBUG) 26854384cf3SJacques Vidrine printf("res_search failed\n"); 26954384cf3SJacques Vidrine #endif 27054384cf3SJacques Vidrine return NS_UNAVAIL; 271a52b6b83SHajimu UMEMOTO } else if (anslen > sizeof(*buf)) { 272a52b6b83SHajimu UMEMOTO free(buf); 27354384cf3SJacques Vidrine #ifdef DEBUG 27454384cf3SJacques Vidrine if (_res.options & RES_DEBUG) 2757922d4cfSJacques Vidrine printf("res_search static buffer too small\n"); 2764faad310SPeter Wemm #endif 277248aee62SJacques Vidrine return NS_UNAVAIL; 2781363f04cSPaul Traina } 279a52b6b83SHajimu UMEMOTO net_entry = getnetanswer(buf, anslen, BYADDR); 280a52b6b83SHajimu UMEMOTO free(buf); 2811363f04cSPaul Traina if (net_entry) { 2821363f04cSPaul Traina unsigned u_net = net; /* maybe net should be unsigned ? */ 2831363f04cSPaul Traina 2841363f04cSPaul Traina /* Strip trailing zeros */ 2854faad310SPeter Wemm while ((u_net & 0xff) == 0 && u_net != 0) 2861363f04cSPaul Traina u_net >>= 8; 2871363f04cSPaul Traina net_entry->n_net = u_net; 288248aee62SJacques Vidrine *(struct netent **)rval = net_entry; 289248aee62SJacques Vidrine return NS_SUCCESS; 2901363f04cSPaul Traina } 291248aee62SJacques Vidrine return NS_NOTFOUND; 2924faad310SPeter Wemm } 2931363f04cSPaul Traina 294248aee62SJacques Vidrine int 295248aee62SJacques Vidrine _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 2961363f04cSPaul Traina { 297248aee62SJacques Vidrine const char *net; 2981363f04cSPaul Traina int anslen; 299a52b6b83SHajimu UMEMOTO querybuf *buf; 3001363f04cSPaul Traina char qbuf[MAXDNAME]; 3011363f04cSPaul Traina 302248aee62SJacques Vidrine net = va_arg(ap, const char *); 303248aee62SJacques Vidrine 304248aee62SJacques Vidrine *(struct netent**)rval = NULL; 305248aee62SJacques Vidrine 3064faad310SPeter Wemm if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 3074faad310SPeter Wemm h_errno = NETDB_INTERNAL; 308248aee62SJacques Vidrine return NS_UNAVAIL; 3094faad310SPeter Wemm } 310a52b6b83SHajimu UMEMOTO if ((buf = malloc(sizeof(*buf))) == NULL) { 311a52b6b83SHajimu UMEMOTO h_errno = NETDB_INTERNAL; 312a52b6b83SHajimu UMEMOTO return NS_NOTFOUND; 313a52b6b83SHajimu UMEMOTO } 3143c8e19e1SWarner Losh strncpy(qbuf, net, sizeof(qbuf) - 1); 3153c8e19e1SWarner Losh qbuf[sizeof(qbuf) - 1] = '\0'; 316a52b6b83SHajimu UMEMOTO anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf)); 3171363f04cSPaul Traina if (anslen < 0) { 318a52b6b83SHajimu UMEMOTO free(buf); 3194faad310SPeter Wemm #ifdef DEBUG 3201363f04cSPaul Traina if (_res.options & RES_DEBUG) 32154384cf3SJacques Vidrine printf("res_search failed\n"); 32254384cf3SJacques Vidrine #endif 32354384cf3SJacques Vidrine return NS_UNAVAIL; 324a52b6b83SHajimu UMEMOTO } else if (anslen > sizeof(*buf)) { 325a52b6b83SHajimu UMEMOTO free(buf); 32654384cf3SJacques Vidrine #ifdef DEBUG 32754384cf3SJacques Vidrine if (_res.options & RES_DEBUG) 3287922d4cfSJacques Vidrine printf("res_search static buffer too small\n"); 3294faad310SPeter Wemm #endif 330248aee62SJacques Vidrine return NS_UNAVAIL; 3311363f04cSPaul Traina } 332a52b6b83SHajimu UMEMOTO *(struct netent**)rval = getnetanswer(buf, anslen, BYNAME); 333a52b6b83SHajimu UMEMOTO free(buf); 334248aee62SJacques Vidrine return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 3351363f04cSPaul Traina } 3361363f04cSPaul Traina 3371363f04cSPaul Traina void 3381363f04cSPaul Traina _setnetdnsent(stayopen) 3391363f04cSPaul Traina int stayopen; 3401363f04cSPaul Traina { 3411363f04cSPaul Traina if (stayopen) 3421363f04cSPaul Traina _res.options |= RES_STAYOPEN | RES_USEVC; 3431363f04cSPaul Traina } 3441363f04cSPaul Traina 3451363f04cSPaul Traina void 3461363f04cSPaul Traina _endnetdnsent() 3471363f04cSPaul Traina { 3481363f04cSPaul Traina _res.options &= ~(RES_STAYOPEN | RES_USEVC); 349ca785773SPeter Wemm res_close(); 3501363f04cSPaul Traina } 351