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

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