133736Sbostic /* 233736Sbostic * Copyright (c) 1988 Regents of the University of California. 333736Sbostic * All rights reserved. 433736Sbostic * 542627Sbostic * %sccs.include.redist.c% 633736Sbostic */ 733736Sbostic 833736Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*47045Sbostic static char sccsid[] = "@(#)res_query.c 5.11 (Berkeley) 03/06/91"; 1033736Sbostic #endif /* LIBC_SCCS and not lint */ 1133736Sbostic 1233736Sbostic #include <sys/param.h> 1333736Sbostic #include <netinet/in.h> 1446604Sbostic #include <arpa/inet.h> 1546604Sbostic #include <arpa/nameser.h> 1633736Sbostic #include <netdb.h> 1746604Sbostic #include <resolv.h> 1833736Sbostic #include <stdio.h> 1946606Sbostic #include <ctype.h> 2033736Sbostic #include <errno.h> 2146604Sbostic #include <stdlib.h> 2242026Sbostic #include <string.h> 2333736Sbostic 2433736Sbostic #if PACKETSZ > 1024 2533736Sbostic #define MAXPACKET PACKETSZ 2633736Sbostic #else 2733736Sbostic #define MAXPACKET 1024 2833736Sbostic #endif 2933736Sbostic 3033736Sbostic int h_errno; 3133736Sbostic 3233736Sbostic /* 3333736Sbostic * Formulate a normal query, send, and await answer. 3433736Sbostic * Returned answer is placed in supplied buffer "answer". 3533736Sbostic * Perform preliminary check of answer, returning success only 3633736Sbostic * if no error is indicated and the answer count is nonzero. 3733736Sbostic * Return the size of the response on success, -1 on error. 3833736Sbostic * Error number is left in h_errno. 3933826Skarels * Caller must parse answer and determine whether it answers the question. 4033736Sbostic */ 4133736Sbostic res_query(name, class, type, answer, anslen) 4233736Sbostic char *name; /* domain name */ 4333736Sbostic int class, type; /* class and type of query */ 4433736Sbostic u_char *answer; /* buffer to put answer */ 4533736Sbostic int anslen; /* size of answer buffer */ 4633736Sbostic { 4733736Sbostic char buf[MAXPACKET]; 4833736Sbostic HEADER *hp; 4933736Sbostic int n; 5033736Sbostic 5133736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 5233736Sbostic return (-1); 5333736Sbostic #ifdef DEBUG 5433736Sbostic if (_res.options & RES_DEBUG) 5533736Sbostic printf("res_query(%s, %d, %d)\n", name, class, type); 5633736Sbostic #endif 5733736Sbostic n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, 5833736Sbostic buf, sizeof(buf)); 5933736Sbostic 6033736Sbostic if (n <= 0) { 6133736Sbostic #ifdef DEBUG 6233736Sbostic if (_res.options & RES_DEBUG) 6333736Sbostic printf("res_query: mkquery failed\n"); 6433736Sbostic #endif 6533736Sbostic h_errno = NO_RECOVERY; 6633736Sbostic return (n); 6733736Sbostic } 6846604Sbostic n = res_send(buf, n, (char *)answer, anslen); 6933736Sbostic if (n < 0) { 7033736Sbostic #ifdef DEBUG 7133736Sbostic if (_res.options & RES_DEBUG) 7233736Sbostic printf("res_query: send error\n"); 7333736Sbostic #endif 7433736Sbostic h_errno = TRY_AGAIN; 7533736Sbostic return(n); 7633736Sbostic } 7733736Sbostic 7833736Sbostic hp = (HEADER *) answer; 7933736Sbostic if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 8033736Sbostic #ifdef DEBUG 8133736Sbostic if (_res.options & RES_DEBUG) 8233736Sbostic printf("rcode = %d, ancount=%d\n", hp->rcode, 8333736Sbostic ntohs(hp->ancount)); 8433736Sbostic #endif 8533736Sbostic switch (hp->rcode) { 8633736Sbostic case NXDOMAIN: 8733826Skarels h_errno = HOST_NOT_FOUND; 8833736Sbostic break; 8933736Sbostic case SERVFAIL: 9033736Sbostic h_errno = TRY_AGAIN; 9133736Sbostic break; 9233736Sbostic case NOERROR: 9333977Skarels h_errno = NO_DATA; 9433736Sbostic break; 9533736Sbostic case FORMERR: 9633736Sbostic case NOTIMP: 9733736Sbostic case REFUSED: 9833826Skarels default: 9933736Sbostic h_errno = NO_RECOVERY; 10033826Skarels break; 10133736Sbostic } 10233736Sbostic return (-1); 10333736Sbostic } 10433736Sbostic return(n); 10533736Sbostic } 10633736Sbostic 10733736Sbostic /* 10833736Sbostic * Formulate a normal query, send, and retrieve answer in supplied buffer. 10933736Sbostic * Return the size of the response on success, -1 on error. 11033736Sbostic * If enabled, implement search rules until answer or unrecoverable failure 11133736Sbostic * is detected. Error number is left in h_errno. 11233736Sbostic * Only useful for queries in the same name hierarchy as the local host 11333736Sbostic * (not, for example, for host address-to-name lookups in domain in-addr.arpa). 11433736Sbostic */ 11533736Sbostic res_search(name, class, type, answer, anslen) 11633736Sbostic char *name; /* domain name */ 11733736Sbostic int class, type; /* class and type of query */ 11833736Sbostic u_char *answer; /* buffer to put answer */ 11933736Sbostic int anslen; /* size of answer */ 12033736Sbostic { 12133736Sbostic register char *cp, **domain; 12233977Skarels int n, ret, got_nodata = 0; 123*47045Sbostic char *__hostalias(); 12433736Sbostic 12533736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 12633736Sbostic return (-1); 12733736Sbostic 12833736Sbostic errno = 0; 12933977Skarels h_errno = HOST_NOT_FOUND; /* default, if we never query */ 13033736Sbostic for (cp = name, n = 0; *cp; cp++) 13133736Sbostic if (*cp == '.') 13233736Sbostic n++; 133*47045Sbostic if (n == 0 && (cp = __hostalias(name))) 13433736Sbostic return (res_query(cp, class, type, answer, anslen)); 13533736Sbostic 13635655Skarels /* 13735655Skarels * We do at least one level of search if 13835655Skarels * - there is no dot and RES_DEFNAME is set, or 13935655Skarels * - there is at least one dot, there is no trailing dot, 14035655Skarels * and RES_DNSRCH is set. 14135655Skarels */ 14235655Skarels if ((n == 0 && _res.options & RES_DEFNAMES) || 14335655Skarels (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) 14435655Skarels for (domain = _res.dnsrch; *domain; domain++) { 14533736Sbostic ret = res_querydomain(name, *domain, class, type, 14633736Sbostic answer, anslen); 14733736Sbostic if (ret > 0) 14833736Sbostic return (ret); 14933736Sbostic /* 15033736Sbostic * If no server present, give up. 15133736Sbostic * If name isn't found in this domain, 15233736Sbostic * keep trying higher domains in the search list 15333736Sbostic * (if that's enabled). 15433977Skarels * On a NO_DATA error, keep trying, otherwise 15533736Sbostic * a wildcard entry of another type could keep us 15633736Sbostic * from finding this entry higher in the domain. 15735655Skarels * If we get some other error (negative answer or 15835655Skarels * server failure), then stop searching up, 15933736Sbostic * but try the input name below in case it's fully-qualified. 16033736Sbostic */ 16133736Sbostic if (errno == ECONNREFUSED) { 16233736Sbostic h_errno = TRY_AGAIN; 16333736Sbostic return (-1); 16433736Sbostic } 16533977Skarels if (h_errno == NO_DATA) 16633977Skarels got_nodata++; 16733977Skarels if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) || 16833736Sbostic (_res.options & RES_DNSRCH) == 0) 16933736Sbostic break; 17033736Sbostic } 17133736Sbostic /* 17233736Sbostic * If the search/default failed, try the name as fully-qualified, 17333736Sbostic * but only if it contained at least one dot (even trailing). 17435655Skarels * This is purely a heuristic; we assume that any reasonable query 17535655Skarels * about a top-level domain (for servers, SOA, etc) will not use 17635655Skarels * res_search. 17733736Sbostic */ 17835655Skarels if (n && (ret = res_querydomain(name, (char *)NULL, class, type, 17935655Skarels answer, anslen)) > 0) 18035655Skarels return (ret); 18133977Skarels if (got_nodata) 18233977Skarels h_errno = NO_DATA; 18333736Sbostic return (-1); 18433736Sbostic } 18533736Sbostic 18633736Sbostic /* 18733736Sbostic * Perform a call on res_query on the concatenation of name and domain, 18833736Sbostic * removing a trailing dot from name if domain is NULL. 18933736Sbostic */ 19033736Sbostic res_querydomain(name, domain, class, type, answer, anslen) 19133736Sbostic char *name, *domain; 19233736Sbostic int class, type; /* class and type of query */ 19333736Sbostic u_char *answer; /* buffer to put answer */ 19433736Sbostic int anslen; /* size of answer */ 19533736Sbostic { 19633736Sbostic char nbuf[2*MAXDNAME+2]; 19733736Sbostic char *longname = nbuf; 19833736Sbostic int n; 19933736Sbostic 20033977Skarels #ifdef DEBUG 20133977Skarels if (_res.options & RES_DEBUG) 20233977Skarels printf("res_querydomain(%s, %s, %d, %d)\n", 20333977Skarels name, domain, class, type); 20433977Skarels #endif 20533736Sbostic if (domain == NULL) { 20633736Sbostic /* 20733736Sbostic * Check for trailing '.'; 20833736Sbostic * copy without '.' if present. 20933736Sbostic */ 21033736Sbostic n = strlen(name) - 1; 21133736Sbostic if (name[n] == '.' && n < sizeof(nbuf) - 1) { 21233736Sbostic bcopy(name, nbuf, n); 21333736Sbostic nbuf[n] = '\0'; 21433736Sbostic } else 21533736Sbostic longname = name; 21633736Sbostic } else 21733736Sbostic (void)sprintf(nbuf, "%.*s.%.*s", 21833736Sbostic MAXDNAME, name, MAXDNAME, domain); 21933736Sbostic 22033736Sbostic return (res_query(longname, class, type, answer, anslen)); 22133736Sbostic } 22233736Sbostic 223*47045Sbostic char * 224*47045Sbostic __hostalias(name) 225*47045Sbostic register const char *name; 22633736Sbostic { 22733736Sbostic register char *C1, *C2; 22833736Sbostic FILE *fp; 22933736Sbostic char *file, *getenv(), *strcpy(), *strncpy(); 23033736Sbostic char buf[BUFSIZ]; 23133736Sbostic static char abuf[MAXDNAME]; 23233736Sbostic 23333736Sbostic file = getenv("HOSTALIASES"); 23433736Sbostic if (file == NULL || (fp = fopen(file, "r")) == NULL) 23533736Sbostic return (NULL); 23633736Sbostic buf[sizeof(buf) - 1] = '\0'; 23733736Sbostic while (fgets(buf, sizeof(buf), fp)) { 23833736Sbostic for (C1 = buf; *C1 && !isspace(*C1); ++C1); 23933736Sbostic if (!*C1) 24033736Sbostic break; 24133736Sbostic *C1 = '\0'; 24233736Sbostic if (!strcasecmp(buf, name)) { 24333736Sbostic while (isspace(*++C1)); 24433736Sbostic if (!*C1) 24533736Sbostic break; 24633736Sbostic for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2); 24733736Sbostic abuf[sizeof(abuf) - 1] = *C2 = '\0'; 24833736Sbostic (void)strncpy(abuf, C1, sizeof(abuf) - 1); 24933736Sbostic fclose(fp); 25033736Sbostic return (abuf); 25133736Sbostic } 25233736Sbostic } 25333736Sbostic fclose(fp); 25433736Sbostic return (NULL); 25533736Sbostic } 256