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