1 /*
2  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Niels Provos.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _DNS_H_
32 #define _DNS_H_
33 
34 #define DNS_MAX_CHILDREN	3
35 
36 #define DNS_POSITIVE	0x0001
37 #define DNS_TEMPORARY	0x0002		/* temporary error */
38 #define DNS_PENDING	0x8000
39 
40 #define DNS_MAX_RETRY	3		/* try resolution three times */
41 #define DNS_RETRY_TIME	(1*60)		/* retry DNS in that time */
42 
43 struct dns_entry {
44 	TAILQ_ENTRY(dns_entry) next;		/* LRU list */
45 	TAILQ_ENTRY(dns_entry) wait_next;	/* Wait list for dns child */
46 	SPLAY_ENTRY(dns_entry) splay_next;	/* Lookup splay */
47 
48 	struct timeval creat;		/* Creation time */
49 	struct timeval access;		/* Last access */
50 
51 	int retries;			/* resolution retries */
52 
53 	u_int16_t flags;
54 	short ref;			/* reference counter for downloads */
55 
56 	char *name;
57 	struct addrinfo *ai;
58 
59 	/* Callbacks when DNS is done */
60 	void (*cb)(struct addrinfo *, struct dns_entry *, void *);
61 	void *cbarg;
62 
63 	/* Storage for documents yet to fetch */
64 	int depth;
65 	struct uri_list uriqueue;
66 	struct uri_list mediaqueue;
67 };
68 
69 TAILQ_HEAD(dns_list, dns_entry);
70 
71 void dns_init(void);
72 void dns_end(void);
73 void dns_print_stats(void);
74 
75 struct addrinfo;
76 int dns_ready(struct dns_entry *, struct timeval *, struct timeval *, int);
77 int dns_resolve_cb(char *, u_short,
78     void (*)(struct addrinfo *, struct dns_entry *, void *), void *);
79 void dns_ref(struct dns_entry *);
80 void dns_unref(struct dns_entry *);
81 struct dns_entry *dns_find(char *);
82 
83 void dns_setdepth(struct dns_entry *, int);
84 
85 #define DNS_UNUSED(x)	((x)->ref || TAILQ_FIRST(&(x)->mediaqueue) != NULL || \
86 			   TAILQ_FIRST(&(x)->uriqueue) != NULL)
87 
88 /* Negative cache hit */
89 #define DNS_NEGATIVE(x)	((x) == 0)
90 #endif /* _DNS_H */
91