133736Sbostic /* 233736Sbostic * Copyright (c) 1988 Regents of the University of California. 333736Sbostic * All rights reserved. 433736Sbostic * 533736Sbostic * Redistribution and use in source and binary forms are permitted 633736Sbostic * provided that this notice is preserved and that due credit is given 733736Sbostic * to the University of California at Berkeley. The name of the University 833736Sbostic * may not be used to endorse or promote products derived from this 933736Sbostic * software without specific prior written permission. This software 1033736Sbostic * is provided ``as is'' without express or implied warranty. 1133736Sbostic */ 1233736Sbostic 1333736Sbostic #if defined(LIBC_SCCS) && !defined(lint) 14*33977Skarels static char sccsid[] = "@(#)res_query.c 5.3 (Berkeley) 04/05/88"; 1533736Sbostic #endif /* LIBC_SCCS and not lint */ 1633736Sbostic 1733736Sbostic #include <sys/param.h> 1833736Sbostic #include <sys/socket.h> 1933736Sbostic #include <netinet/in.h> 2033736Sbostic #include <ctype.h> 2133736Sbostic #include <netdb.h> 2233736Sbostic #include <stdio.h> 2333736Sbostic #include <errno.h> 2433736Sbostic #include <strings.h> 2533736Sbostic #include <arpa/inet.h> 2633736Sbostic #include <arpa/nameser.h> 2733736Sbostic #include <resolv.h> 2833736Sbostic 2933736Sbostic #if PACKETSZ > 1024 3033736Sbostic #define MAXPACKET PACKETSZ 3133736Sbostic #else 3233736Sbostic #define MAXPACKET 1024 3333736Sbostic #endif 3433736Sbostic 3533736Sbostic extern int errno; 3633736Sbostic int h_errno; 3733736Sbostic 3833736Sbostic /* 3933736Sbostic * Formulate a normal query, send, and await answer. 4033736Sbostic * Returned answer is placed in supplied buffer "answer". 4133736Sbostic * Perform preliminary check of answer, returning success only 4233736Sbostic * if no error is indicated and the answer count is nonzero. 4333736Sbostic * Return the size of the response on success, -1 on error. 4433736Sbostic * Error number is left in h_errno. 4533826Skarels * Caller must parse answer and determine whether it answers the question. 4633736Sbostic */ 4733736Sbostic res_query(name, class, type, answer, anslen) 4833736Sbostic char *name; /* domain name */ 4933736Sbostic int class, type; /* class and type of query */ 5033736Sbostic u_char *answer; /* buffer to put answer */ 5133736Sbostic int anslen; /* size of answer buffer */ 5233736Sbostic { 5333736Sbostic char buf[MAXPACKET]; 5433736Sbostic HEADER *hp; 5533736Sbostic int n; 5633736Sbostic 5733736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 5833736Sbostic return (-1); 5933736Sbostic #ifdef DEBUG 6033736Sbostic if (_res.options & RES_DEBUG) 6133736Sbostic printf("res_query(%s, %d, %d)\n", name, class, type); 6233736Sbostic #endif 6333736Sbostic n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, 6433736Sbostic buf, sizeof(buf)); 6533736Sbostic 6633736Sbostic if (n <= 0) { 6733736Sbostic #ifdef DEBUG 6833736Sbostic if (_res.options & RES_DEBUG) 6933736Sbostic printf("res_query: mkquery failed\n"); 7033736Sbostic #endif 7133736Sbostic h_errno = NO_RECOVERY; 7233736Sbostic return (n); 7333736Sbostic } 7433736Sbostic n = res_send(buf, n, answer, anslen); 7533736Sbostic if (n < 0) { 7633736Sbostic #ifdef DEBUG 7733736Sbostic if (_res.options & RES_DEBUG) 7833736Sbostic printf("res_query: send error\n"); 7933736Sbostic #endif 8033736Sbostic h_errno = TRY_AGAIN; 8133736Sbostic return(n); 8233736Sbostic } 8333736Sbostic 8433736Sbostic hp = (HEADER *) answer; 8533736Sbostic if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 8633736Sbostic #ifdef DEBUG 8733736Sbostic if (_res.options & RES_DEBUG) 8833736Sbostic printf("rcode = %d, ancount=%d\n", hp->rcode, 8933736Sbostic ntohs(hp->ancount)); 9033736Sbostic #endif 9133736Sbostic switch (hp->rcode) { 9233736Sbostic case NXDOMAIN: 9333826Skarels h_errno = HOST_NOT_FOUND; 9433736Sbostic break; 9533736Sbostic case SERVFAIL: 9633736Sbostic h_errno = TRY_AGAIN; 9733736Sbostic break; 9833736Sbostic case NOERROR: 99*33977Skarels h_errno = NO_DATA; 10033736Sbostic break; 10133736Sbostic case FORMERR: 10233736Sbostic case NOTIMP: 10333736Sbostic case REFUSED: 10433826Skarels default: 10533736Sbostic h_errno = NO_RECOVERY; 10633826Skarels break; 10733736Sbostic } 10833736Sbostic return (-1); 10933736Sbostic } 11033736Sbostic return(n); 11133736Sbostic } 11233736Sbostic 11333736Sbostic /* 11433736Sbostic * Formulate a normal query, send, and retrieve answer in supplied buffer. 11533736Sbostic * Return the size of the response on success, -1 on error. 11633736Sbostic * If enabled, implement search rules until answer or unrecoverable failure 11733736Sbostic * is detected. Error number is left in h_errno. 11833736Sbostic * Only useful for queries in the same name hierarchy as the local host 11933736Sbostic * (not, for example, for host address-to-name lookups in domain in-addr.arpa). 12033736Sbostic */ 12133736Sbostic res_search(name, class, type, answer, anslen) 12233736Sbostic char *name; /* domain name */ 12333736Sbostic int class, type; /* class and type of query */ 12433736Sbostic u_char *answer; /* buffer to put answer */ 12533736Sbostic int anslen; /* size of answer */ 12633736Sbostic { 12733736Sbostic register char *cp, **domain; 128*33977Skarels int n, ret, got_nodata = 0; 12933736Sbostic char *hostalias(); 13033736Sbostic 13133736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 13233736Sbostic return (-1); 13333736Sbostic 13433736Sbostic errno = 0; 135*33977Skarels h_errno = HOST_NOT_FOUND; /* default, if we never query */ 13633736Sbostic for (cp = name, n = 0; *cp; cp++) 13733736Sbostic if (*cp == '.') 13833736Sbostic n++; 13933736Sbostic if (n == 0 && (cp = hostalias(name))) 14033736Sbostic return (res_query(cp, class, type, answer, anslen)); 14133736Sbostic 14233736Sbostic if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES)) 14333736Sbostic 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). 153*33977Skarels * 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. 15633736Sbostic * If we get some other error (non-authoritative negative 15733736Sbostic * answer or 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 } 164*33977Skarels if (h_errno == NO_DATA) 165*33977Skarels got_nodata++; 166*33977Skarels if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) || 16733736Sbostic (_res.options & RES_DNSRCH) == 0) 16833736Sbostic break; 16933736Sbostic h_errno = 0; 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). 17433736Sbostic */ 17533736Sbostic if (n) 17633736Sbostic return (res_querydomain(name, (char *)NULL, class, type, 17733736Sbostic answer, anslen)); 178*33977Skarels if (got_nodata) 179*33977Skarels h_errno = NO_DATA; 18033736Sbostic return (-1); 18133736Sbostic } 18233736Sbostic 18333736Sbostic /* 18433736Sbostic * Perform a call on res_query on the concatenation of name and domain, 18533736Sbostic * removing a trailing dot from name if domain is NULL. 18633736Sbostic */ 18733736Sbostic res_querydomain(name, domain, class, type, answer, anslen) 18833736Sbostic char *name, *domain; 18933736Sbostic int class, type; /* class and type of query */ 19033736Sbostic u_char *answer; /* buffer to put answer */ 19133736Sbostic int anslen; /* size of answer */ 19233736Sbostic { 19333736Sbostic char nbuf[2*MAXDNAME+2]; 19433736Sbostic char *longname = nbuf; 19533736Sbostic int n; 19633736Sbostic 197*33977Skarels #ifdef DEBUG 198*33977Skarels if (_res.options & RES_DEBUG) 199*33977Skarels printf("res_querydomain(%s, %s, %d, %d)\n", 200*33977Skarels name, domain, class, type); 201*33977Skarels #endif 20233736Sbostic if (domain == NULL) { 20333736Sbostic /* 20433736Sbostic * Check for trailing '.'; 20533736Sbostic * copy without '.' if present. 20633736Sbostic */ 20733736Sbostic n = strlen(name) - 1; 20833736Sbostic if (name[n] == '.' && n < sizeof(nbuf) - 1) { 20933736Sbostic bcopy(name, nbuf, n); 21033736Sbostic nbuf[n] = '\0'; 21133736Sbostic } else 21233736Sbostic longname = name; 21333736Sbostic } else 21433736Sbostic (void)sprintf(nbuf, "%.*s.%.*s", 21533736Sbostic MAXDNAME, name, MAXDNAME, domain); 21633736Sbostic 21733736Sbostic return (res_query(longname, class, type, answer, anslen)); 21833736Sbostic } 21933736Sbostic 22033736Sbostic char * 22133736Sbostic hostalias(name) 22233736Sbostic register char *name; 22333736Sbostic { 22433736Sbostic register char *C1, *C2; 22533736Sbostic FILE *fp; 22633736Sbostic char *file, *getenv(), *strcpy(), *strncpy(); 22733736Sbostic char buf[BUFSIZ]; 22833736Sbostic static char abuf[MAXDNAME]; 22933736Sbostic 23033736Sbostic file = getenv("HOSTALIASES"); 23133736Sbostic if (file == NULL || (fp = fopen(file, "r")) == NULL) 23233736Sbostic return (NULL); 23333736Sbostic buf[sizeof(buf) - 1] = '\0'; 23433736Sbostic while (fgets(buf, sizeof(buf), fp)) { 23533736Sbostic for (C1 = buf; *C1 && !isspace(*C1); ++C1); 23633736Sbostic if (!*C1) 23733736Sbostic break; 23833736Sbostic *C1 = '\0'; 23933736Sbostic if (!strcasecmp(buf, name)) { 24033736Sbostic while (isspace(*++C1)); 24133736Sbostic if (!*C1) 24233736Sbostic break; 24333736Sbostic for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2); 24433736Sbostic abuf[sizeof(abuf) - 1] = *C2 = '\0'; 24533736Sbostic (void)strncpy(abuf, C1, sizeof(abuf) - 1); 24633736Sbostic fclose(fp); 24733736Sbostic return (abuf); 24833736Sbostic } 24933736Sbostic } 25033736Sbostic fclose(fp); 25133736Sbostic return (NULL); 25233736Sbostic } 253