source: trunk/athena/bin/hostinfo/res_query.c @ 12030

Revision 12030, 7.6 KB checked in by ghudson, 26 years ago (diff)
From svalente: rename hostalias() to avoid system header conflicts.
Line 
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley.  The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#if defined(LIBC_SCCS) && !defined(lint)
19static char sccsid[] = "@(#)res_query.c 5.5 (Berkeley) 9/21/88";
20#endif /* LIBC_SCCS and not lint */
21
22#ifdef _IBMR2
23#define BIT_ZERO_ON_LEFT
24#endif
25
26#ifdef _AUX_SOURCE
27#include <sys/types.h>
28#endif
29
30#include <sys/param.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <ctype.h>
34#include <netdb.h>
35#include <stdio.h>
36#include <errno.h>
37#include <string.h>
38#include <arpa/inet.h>
39#include <arpa/nameser.h>
40#include <resolv.h>
41
42#if !defined NO_DATA
43#define NO_DATA NO_ADDRESS
44#endif
45
46#if PACKETSZ > 1024
47#define MAXPACKET       PACKETSZ
48#else
49#define MAXPACKET       1024
50#endif
51
52extern int server_specified;
53extern struct in_addr server_addr;
54extern int errno;
55int h_errno;
56
57static char *local_hostalias (const char *);
58
59/*
60 * Formulate a normal query, send, and await answer.
61 * Returned answer is placed in supplied buffer "answer".
62 * Perform preliminary check of answer, returning success only
63 * if no error is indicated and the answer count is nonzero.
64 * Return the size of the response on success, -1 on error.
65 * Error number is left in h_errno.
66 * Caller must parse answer and determine whether it answers the question.
67 */
68res_query(name, class, type, answer, anslen)
69        const char *name;       /* domain name */
70        int class, type;        /* class and type of query */
71        u_char *answer;         /* buffer to put answer */
72        int anslen;             /* size of answer buffer */
73{
74        char buf[MAXPACKET];
75        HEADER *hp;
76        int n;
77
78        if ((_res.options & RES_INIT) == 0 && res_init() == -1)
79                return (-1);
80
81        if(server_specified)
82          _res.nsaddr.sin_addr = server_addr;
83         
84#ifdef DEBUG
85        if (_res.options & RES_DEBUG)
86                printf("res_query(%s, %d, %d)\n", name, class, type);
87#endif
88        n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
89            buf, sizeof(buf));
90
91        if (n <= 0) {
92#ifdef DEBUG
93                if (_res.options & RES_DEBUG)
94                        printf("res_query: mkquery failed\n");
95#endif
96                h_errno = NO_RECOVERY;
97                return (n);
98        }
99        n = res_send(buf, n, answer, anslen);
100        if (n < 0) {
101#ifdef DEBUG
102                if (_res.options & RES_DEBUG)
103                        printf("res_query: send error\n");
104#endif
105                h_errno = TRY_AGAIN;
106                return(n);
107        }
108
109        hp = (HEADER *) answer;
110        if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
111#ifdef DEBUG
112                if (_res.options & RES_DEBUG)
113                        printf("rcode = %d, ancount=%d\n", hp->rcode,
114                            ntohs(hp->ancount));
115#endif
116                switch (hp->rcode) {
117                        case NXDOMAIN:
118                                h_errno = HOST_NOT_FOUND;
119                                break;
120                        case SERVFAIL:
121                                h_errno = TRY_AGAIN;
122                                break;
123                        case NOERROR:
124                                h_errno = NO_DATA;
125                                break;
126                        case FORMERR:
127                        case NOTIMP:
128                        case REFUSED:
129                        default:
130                                h_errno = NO_RECOVERY;
131                                break;
132                }
133                return (-1);
134        }
135        return(n);
136}
137
138/*
139 * Formulate a normal query, send, and retrieve answer in supplied buffer.
140 * Return the size of the response on success, -1 on error.
141 * If enabled, implement search rules until answer or unrecoverable failure
142 * is detected.  Error number is left in h_errno.
143 * Only useful for queries in the same name hierarchy as the local host
144 * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
145 */
146res_search(name, class, type, answer, anslen)
147        const char *name;       /* domain name */
148        int class, type;        /* class and type of query */
149        u_char *answer;         /* buffer to put answer */
150        int anslen;             /* size of answer */
151{
152        register char *cp, **domain;
153        int n, ret, got_nodata = 0;
154
155        if ((_res.options & RES_INIT) == 0 && res_init() == -1)
156                return (-1);
157
158        errno = 0;
159        h_errno = HOST_NOT_FOUND;               /* default, if we never query */
160        for (cp = name, n = 0; *cp; cp++)
161                if (*cp == '.')
162                        n++;
163        if (n == 0 && (cp = local_hostalias(name)))
164                return (res_query(cp, class, type, answer, anslen));
165
166        /*
167         * We do at least one level of search if
168         *      - there is no dot and RES_DEFNAME is set, or
169         *      - there is at least one dot, there is no trailing dot,
170         *        and RES_DNSRCH is set.
171         */
172        if ((n == 0 && _res.options & RES_DEFNAMES) ||
173           (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
174             for (domain = _res.dnsrch; *domain; domain++) {
175                ret = res_querydomain(name, *domain, class, type,
176                    answer, anslen);
177                if (ret > 0)
178                        return (ret);
179                /*
180                 * If no server present, give up.
181                 * If name isn't found in this domain,
182                 * keep trying higher domains in the search list
183                 * (if that's enabled).
184                 * On a NO_DATA error, keep trying, otherwise
185                 * a wildcard entry of another type could keep us
186                 * from finding this entry higher in the domain.
187                 * If we get some other error (negative answer or
188                 * server failure), then stop searching up,
189                 * but try the input name below in case it's fully-qualified.
190                 */
191                if (errno == ECONNREFUSED) {
192                        h_errno = TRY_AGAIN;
193                        return (-1);
194                }
195                if (h_errno == NO_DATA)
196                        got_nodata++;
197                if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
198                    (_res.options & RES_DNSRCH) == 0)
199                        break;
200        }
201        /*
202         * If the search/default failed, try the name as fully-qualified,
203         * but only if it contained at least one dot (even trailing).
204         * This is purely a heuristic; we assume that any reasonable query
205         * about a top-level domain (for servers, SOA, etc) will not use
206         * res_search.
207         */
208        if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
209            answer, anslen)) > 0)
210                return (ret);
211        if (got_nodata)
212                h_errno = NO_DATA;
213        return (-1);
214}
215
216/*
217 * Perform a call on res_query on the concatenation of name and domain,
218 * removing a trailing dot from name if domain is NULL.
219 */
220res_querydomain(name, domain, class, type, answer, anslen)
221        const char *name, *domain;
222        int class, type;        /* class and type of query */
223        u_char *answer;         /* buffer to put answer */
224        int anslen;             /* size of answer */
225{
226        char nbuf[2*MAXDNAME+2];
227        char *longname = nbuf;
228        int n;
229
230#ifdef DEBUG
231        if (_res.options & RES_DEBUG)
232                printf("res_querydomain(%s, %s, %d, %d)\n",
233                    name, domain, class, type);
234#endif
235        if (domain == NULL) {
236                /*
237                 * Check for trailing '.';
238                 * copy without '.' if present.
239                 */
240                n = strlen(name) - 1;
241                if (name[n] == '.' && n < sizeof(nbuf) - 1) {
242                        memcpy(nbuf, name, n);
243                        nbuf[n] = '\0';
244                } else
245                        longname = name;
246        } else
247                (void)sprintf(nbuf, "%.*s.%.*s",
248                    MAXDNAME, name, MAXDNAME, domain);
249
250        return (res_query(longname, class, type, answer, anslen));
251}
252
253static char *
254local_hostalias(name)
255        const char *name;
256{
257        register char *C1, *C2;
258        FILE *fp;
259#ifndef _IBMR2
260        char *file, *getenv(), *strcpy(), *strncpy();
261#else
262        char *file;
263#endif
264        char buf[BUFSIZ];
265        static char abuf[MAXDNAME];
266
267        file = getenv("HOSTALIASES");
268        if (file == NULL || (fp = fopen(file, "r")) == NULL)
269                return (NULL);
270        buf[sizeof(buf) - 1] = '\0';
271        while (fgets(buf, sizeof(buf), fp)) {
272                for (C1 = buf; *C1 && !isspace(*C1); ++C1);
273                if (!*C1)
274                        break;
275                *C1 = '\0';
276                if (!strcasecmp(buf, name)) {
277                        while (isspace(*++C1));
278                        if (!*C1)
279                                break;
280                        for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
281                        abuf[sizeof(abuf) - 1] = *C2 = '\0';
282                        (void)strncpy(abuf, C1, sizeof(abuf) - 1);
283                        fclose(fp);
284                        return (abuf);
285                }
286        }
287        fclose(fp);
288        return (NULL);
289}
Note: See TracBrowser for help on using the repository browser.