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*46604Sbostic static char sccsid[] = "@(#)res_query.c 5.9 (Berkeley) 02/24/91"; 1033736Sbostic #endif /* LIBC_SCCS and not lint */ 1133736Sbostic 1233736Sbostic #include <sys/param.h> 1333736Sbostic #include <netinet/in.h> 14*46604Sbostic #include <arpa/inet.h> 15*46604Sbostic #include <arpa/nameser.h> 1633736Sbostic #include <netdb.h> 17*46604Sbostic #include <resolv.h> 1833736Sbostic #include <stdio.h> 1933736Sbostic #include <errno.h> 20*46604Sbostic #include <stdlib.h> 2142026Sbostic #include <string.h> 2233736Sbostic 2333736Sbostic #if PACKETSZ > 1024 2433736Sbostic #define MAXPACKET PACKETSZ 2533736Sbostic #else 2633736Sbostic #define MAXPACKET 1024 2733736Sbostic #endif 2833736Sbostic 2933736Sbostic int h_errno; 3033736Sbostic 3133736Sbostic /* 3233736Sbostic * Formulate a normal query, send, and await answer. 3333736Sbostic * Returned answer is placed in supplied buffer "answer". 3433736Sbostic * Perform preliminary check of answer, returning success only 3533736Sbostic * if no error is indicated and the answer count is nonzero. 3633736Sbostic * Return the size of the response on success, -1 on error. 3733736Sbostic * Error number is left in h_errno. 3833826Skarels * Caller must parse answer and determine whether it answers the question. 3933736Sbostic */ 4033736Sbostic res_query(name, class, type, answer, anslen) 4133736Sbostic char *name; /* domain name */ 4233736Sbostic int class, type; /* class and type of query */ 4333736Sbostic u_char *answer; /* buffer to put answer */ 4433736Sbostic int anslen; /* size of answer buffer */ 4533736Sbostic { 4633736Sbostic char buf[MAXPACKET]; 4733736Sbostic HEADER *hp; 4833736Sbostic int n; 4933736Sbostic 5033736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 5133736Sbostic return (-1); 5233736Sbostic #ifdef DEBUG 5333736Sbostic if (_res.options & RES_DEBUG) 5433736Sbostic printf("res_query(%s, %d, %d)\n", name, class, type); 5533736Sbostic #endif 5633736Sbostic n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, 5733736Sbostic buf, sizeof(buf)); 5833736Sbostic 5933736Sbostic if (n <= 0) { 6033736Sbostic #ifdef DEBUG 6133736Sbostic if (_res.options & RES_DEBUG) 6233736Sbostic printf("res_query: mkquery failed\n"); 6333736Sbostic #endif 6433736Sbostic h_errno = NO_RECOVERY; 6533736Sbostic return (n); 6633736Sbostic } 67*46604Sbostic n = res_send(buf, n, (char *)answer, anslen); 6833736Sbostic if (n < 0) { 6933736Sbostic #ifdef DEBUG 7033736Sbostic if (_res.options & RES_DEBUG) 7133736Sbostic printf("res_query: send error\n"); 7233736Sbostic #endif 7333736Sbostic h_errno = TRY_AGAIN; 7433736Sbostic return(n); 7533736Sbostic } 7633736Sbostic 7733736Sbostic hp = (HEADER *) answer; 7833736Sbostic if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 7933736Sbostic #ifdef DEBUG 8033736Sbostic if (_res.options & RES_DEBUG) 8133736Sbostic printf("rcode = %d, ancount=%d\n", hp->rcode, 8233736Sbostic ntohs(hp->ancount)); 8333736Sbostic #endif 8433736Sbostic switch (hp->rcode) { 8533736Sbostic case NXDOMAIN: 8633826Skarels h_errno = HOST_NOT_FOUND; 8733736Sbostic break; 8833736Sbostic case SERVFAIL: 8933736Sbostic h_errno = TRY_AGAIN; 9033736Sbostic break; 9133736Sbostic case NOERROR: 9233977Skarels h_errno = NO_DATA; 9333736Sbostic break; 9433736Sbostic case FORMERR: 9533736Sbostic case NOTIMP: 9633736Sbostic case REFUSED: 9733826Skarels default: 9833736Sbostic h_errno = NO_RECOVERY; 9933826Skarels break; 10033736Sbostic } 10133736Sbostic return (-1); 10233736Sbostic } 10333736Sbostic return(n); 10433736Sbostic } 10533736Sbostic 10633736Sbostic /* 10733736Sbostic * Formulate a normal query, send, and retrieve answer in supplied buffer. 10833736Sbostic * Return the size of the response on success, -1 on error. 10933736Sbostic * If enabled, implement search rules until answer or unrecoverable failure 11033736Sbostic * is detected. Error number is left in h_errno. 11133736Sbostic * Only useful for queries in the same name hierarchy as the local host 11233736Sbostic * (not, for example, for host address-to-name lookups in domain in-addr.arpa). 11333736Sbostic */ 11433736Sbostic res_search(name, class, type, answer, anslen) 11533736Sbostic char *name; /* domain name */ 11633736Sbostic int class, type; /* class and type of query */ 11733736Sbostic u_char *answer; /* buffer to put answer */ 11833736Sbostic int anslen; /* size of answer */ 11933736Sbostic { 12033736Sbostic register char *cp, **domain; 12133977Skarels int n, ret, got_nodata = 0; 12246496Sbostic static char *hostalias(); 12333736Sbostic 12433736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 12533736Sbostic return (-1); 12633736Sbostic 12733736Sbostic errno = 0; 12833977Skarels h_errno = HOST_NOT_FOUND; /* default, if we never query */ 12933736Sbostic for (cp = name, n = 0; *cp; cp++) 13033736Sbostic if (*cp == '.') 13133736Sbostic n++; 13233736Sbostic if (n == 0 && (cp = hostalias(name))) 13333736Sbostic return (res_query(cp, class, type, answer, anslen)); 13433736Sbostic 13535655Skarels /* 13635655Skarels * We do at least one level of search if 13735655Skarels * - there is no dot and RES_DEFNAME is set, or 13835655Skarels * - there is at least one dot, there is no trailing dot, 13935655Skarels * and RES_DNSRCH is set. 14035655Skarels */ 14135655Skarels if ((n == 0 && _res.options & RES_DEFNAMES) || 14235655Skarels (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) 14335655Skarels for (domain = _res.dnsrch; *domain; domain++) { 14433736Sbostic ret = res_querydomain(name, *domain, class, type, 14533736Sbostic answer, anslen); 14633736Sbostic if (ret > 0) 14733736Sbostic return (ret); 14833736Sbostic /* 14933736Sbostic * If no server present, give up. 15033736Sbostic * If name isn't found in this domain, 15133736Sbostic * keep trying higher domains in the search list 15233736Sbostic * (if that's enabled). 15333977Skarels * On a NO_DATA error, keep trying, otherwise 15433736Sbostic * a wildcard entry of another type could keep us 15533736Sbostic * from finding this entry higher in the domain. 15635655Skarels * If we get some other error (negative answer or 15735655Skarels * server failure), then stop searching up, 15833736Sbostic * but try the input name below in case it's fully-qualified. 15933736Sbostic */ 16033736Sbostic if (errno == ECONNREFUSED) { 16133736Sbostic h_errno = TRY_AGAIN; 16233736Sbostic return (-1); 16333736Sbostic } 16433977Skarels if (h_errno == NO_DATA) 16533977Skarels got_nodata++; 16633977Skarels if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) || 16733736Sbostic (_res.options & RES_DNSRCH) == 0) 16833736Sbostic break; 16933736Sbostic } 17033736Sbostic /* 17133736Sbostic * If the search/default failed, try the name as fully-qualified, 17233736Sbostic * but only if it contained at least one dot (even trailing). 17335655Skarels * This is purely a heuristic; we assume that any reasonable query 17435655Skarels * about a top-level domain (for servers, SOA, etc) will not use 17535655Skarels * res_search. 17633736Sbostic */ 17735655Skarels if (n && (ret = res_querydomain(name, (char *)NULL, class, type, 17835655Skarels answer, anslen)) > 0) 17935655Skarels return (ret); 18033977Skarels if (got_nodata) 18133977Skarels h_errno = NO_DATA; 18233736Sbostic return (-1); 18333736Sbostic } 18433736Sbostic 18533736Sbostic /* 18633736Sbostic * Perform a call on res_query on the concatenation of name and domain, 18733736Sbostic * removing a trailing dot from name if domain is NULL. 18833736Sbostic */ 18933736Sbostic res_querydomain(name, domain, class, type, answer, anslen) 19033736Sbostic char *name, *domain; 19133736Sbostic int class, type; /* class and type of query */ 19233736Sbostic u_char *answer; /* buffer to put answer */ 19333736Sbostic int anslen; /* size of answer */ 19433736Sbostic { 19533736Sbostic char nbuf[2*MAXDNAME+2]; 19633736Sbostic char *longname = nbuf; 19733736Sbostic int n; 19833736Sbostic 19933977Skarels #ifdef DEBUG 20033977Skarels if (_res.options & RES_DEBUG) 20133977Skarels printf("res_querydomain(%s, %s, %d, %d)\n", 20233977Skarels name, domain, class, type); 20333977Skarels #endif 20433736Sbostic if (domain == NULL) { 20533736Sbostic /* 20633736Sbostic * Check for trailing '.'; 20733736Sbostic * copy without '.' if present. 20833736Sbostic */ 20933736Sbostic n = strlen(name) - 1; 21033736Sbostic if (name[n] == '.' && n < sizeof(nbuf) - 1) { 21133736Sbostic bcopy(name, nbuf, n); 21233736Sbostic nbuf[n] = '\0'; 21333736Sbostic } else 21433736Sbostic longname = name; 21533736Sbostic } else 21633736Sbostic (void)sprintf(nbuf, "%.*s.%.*s", 21733736Sbostic MAXDNAME, name, MAXDNAME, domain); 21833736Sbostic 21933736Sbostic return (res_query(longname, class, type, answer, anslen)); 22033736Sbostic } 22133736Sbostic 22246496Sbostic static char * 22333736Sbostic hostalias(name) 22433736Sbostic register char *name; 22533736Sbostic { 22633736Sbostic register char *C1, *C2; 22733736Sbostic FILE *fp; 22833736Sbostic char *file, *getenv(), *strcpy(), *strncpy(); 22933736Sbostic char buf[BUFSIZ]; 23033736Sbostic static char abuf[MAXDNAME]; 23133736Sbostic 23233736Sbostic file = getenv("HOSTALIASES"); 23333736Sbostic if (file == NULL || (fp = fopen(file, "r")) == NULL) 23433736Sbostic return (NULL); 23533736Sbostic buf[sizeof(buf) - 1] = '\0'; 23633736Sbostic while (fgets(buf, sizeof(buf), fp)) { 23733736Sbostic for (C1 = buf; *C1 && !isspace(*C1); ++C1); 23833736Sbostic if (!*C1) 23933736Sbostic break; 24033736Sbostic *C1 = '\0'; 24133736Sbostic if (!strcasecmp(buf, name)) { 24233736Sbostic while (isspace(*++C1)); 24333736Sbostic if (!*C1) 24433736Sbostic break; 24533736Sbostic for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2); 24633736Sbostic abuf[sizeof(abuf) - 1] = *C2 = '\0'; 24733736Sbostic (void)strncpy(abuf, C1, sizeof(abuf) - 1); 24833736Sbostic fclose(fp); 24933736Sbostic return (abuf); 25033736Sbostic } 25133736Sbostic } 25233736Sbostic fclose(fp); 25333736Sbostic return (NULL); 25433736Sbostic } 255