Postfix3.3.1
dns.h
[詳解]
1 #ifndef _DNS_H_INCLUDED_
2 #define _DNS_H_INCLUDED_
3 
4 /*++
5 /* NAME
6 /* dns 3h
7 /* SUMMARY
8 /* domain name service lookup
9 /* SYNOPSIS
10 /* #include <dns.h>
11 /* DESCRIPTION
12 /* .nf
13 
14  /*
15  * System library.
16  */
17 #include <netinet/in.h>
18 #include <arpa/nameser.h>
19 #ifdef RESOLVE_H_NEEDS_STDIO_H
20 #include <stdio.h>
21 #endif
22 #ifdef RESOLVE_H_NEEDS_NAMESER8_COMPAT_H
23 #include <nameser8_compat.h>
24 #endif
25 #ifdef RESOLVE_H_NEEDS_ARPA_NAMESER_COMPAT_H
26 #include <arpa/nameser_compat.h>
27 #endif
28 #include <resolv.h>
29 
30  /*
31  * Name server compatibility. These undocumented macros appear in the file
32  * <arpa/nameser.h>, but since they are undocumented we should not count on
33  * their presence, and so they are included here just in case.
34  */
35 #ifndef GETSHORT
36 
37 #define GETSHORT(s, cp) { \
38  unsigned char *t_cp = (u_char *)(cp); \
39  (s) = ((unsigned)t_cp[0] << 8) \
40  | ((unsigned)t_cp[1]) \
41  ; \
42  (cp) += 2; \
43 }
44 
45 #define GETLONG(l, cp) { \
46  unsigned char *t_cp = (u_char *)(cp); \
47  (l) = ((unsigned)t_cp[0] << 24) \
48  | ((unsigned)t_cp[1] << 16) \
49  | ((unsigned)t_cp[2] << 8) \
50  | ((unsigned)t_cp[3]) \
51  ; \
52  (cp) += 4; \
53 }
54 
55 #endif
56 
57 /*
58  * Disable DNSSEC at compile-time even if RES_USE_DNSSEC is available
59  */
60 #ifdef NO_DNSSEC
61 #undef RES_USE_DNSSEC
62 #endif
63 
64  /*
65  * Compatibility with systems that lack RES_USE_DNSSEC and RES_USE_EDNS0
66  */
67 #ifndef RES_USE_DNSSEC
68 #define RES_USE_DNSSEC 0
69 #endif
70 #ifndef RES_USE_EDNS0
71 #define RES_USE_EDNS0 0
72 #endif
73 
74  /*-
75  * TLSA: https://tools.ietf.org/html/rfc6698#section-7.1
76  * RRSIG: http://tools.ietf.org/html/rfc4034#section-3
77  *
78  * We don't request RRSIG, but we get it "for free" when we send the DO-bit.
79  */
80 #ifndef T_TLSA
81 #define T_TLSA 52
82 #endif
83 #ifndef T_RRSIG
84 #define T_RRSIG 46 /* Avoid unknown RR in logs */
85 #endif
86 #ifndef T_DNAME
87 #define T_DNAME 39 /* [RFC6672] */
88 #endif
89 
90  /*
91  * https://tools.ietf.org/html/rfc6698#section-7.2
92  */
93 #define DNS_TLSA_USAGE_CA_CONSTRAINT 0
94 #define DNS_TLSA_USAGE_SERVICE_CERTIFICATE_CONSTRAINT 1
95 #define DNS_TLSA_USAGE_TRUST_ANCHOR_ASSERTION 2
96 #define DNS_TLSA_USAGE_DOMAIN_ISSUED_CERTIFICATE 3
97 
98  /*
99  * https://tools.ietf.org/html/rfc6698#section-7.3
100  */
101 #define DNS_TLSA_SELECTOR_FULL_CERTIFICATE 0
102 #define DNS_TLSA_SELECTOR_SUBJECTPUBLICKEYINFO 1
103 
104  /*
105  * https://tools.ietf.org/html/rfc6698#section-7.4
106  */
107 #define DNS_TLSA_MATCHING_TYPE_NO_HASH_USED 0
108 #define DNS_TLSA_MATCHING_TYPE_SHA256 1
109 #define DNS_TLSA_MATCHING_TYPE_SHA512 2
110 
111  /*
112  * SunOS 4 needs this.
113  */
114 #ifndef T_TXT
115 #define T_TXT 16
116 #endif
117 
118  /*
119  * Utility library.
120  */
121 #include <vstring.h>
122 #include <sock_addr.h>
123 #include <myaddrinfo.h>
124 
125  /*
126  * Structure for fixed resource record data.
127  */
128 typedef struct DNS_FIXED {
129  unsigned short type; /* T_A, T_CNAME, etc. */
130  unsigned short class; /* C_IN, etc. */
131  unsigned int ttl; /* always */
132  unsigned length; /* record length */
133 } DNS_FIXED;
134 
135  /*
136  * Structure of a DNS resource record after expansion. The components are
137  * named after the things one can expect to find in a DNS resource record.
138  */
139 typedef struct DNS_RR {
140  char *qname; /* query name, mystrdup()ed */
141  char *rname; /* reply name, mystrdup()ed */
142  unsigned short type; /* T_A, T_CNAME, etc. */
143  unsigned short class; /* C_IN, etc. */
144  unsigned int ttl; /* always */
145  unsigned int dnssec_valid; /* DNSSEC validated */
146  unsigned short pref; /* T_MX only */
147  struct DNS_RR *next; /* linkage */
148  size_t data_len; /* actual data size */
149  char data[1]; /* actually a bunch of data */
150 } DNS_RR;
151 
152  /*
153  * dns_strerror.c
154  */
155 extern const char *dns_strerror(unsigned);
156 
157  /*
158  * dns_strtype.c
159  */
160 extern const char *dns_strtype(unsigned);
161 extern unsigned dns_type(const char *);
162 
163  /*
164  * dns_strrecord.c
165  */
166 extern char *dns_strrecord(VSTRING *, DNS_RR *);
167 
168  /*
169  * dns_rr.c
170  */
171 extern DNS_RR *dns_rr_create(const char *, const char *,
172  ushort, ushort,
173  unsigned, unsigned,
174  const char *, size_t);
175 extern void dns_rr_free(DNS_RR *);
176 extern DNS_RR *dns_rr_copy(DNS_RR *);
177 extern DNS_RR *dns_rr_append(DNS_RR *, DNS_RR *);
178 extern DNS_RR *dns_rr_sort(DNS_RR *, int (*) (DNS_RR *, DNS_RR *));
179 extern int dns_rr_compare_pref_ipv6(DNS_RR *, DNS_RR *);
180 extern int dns_rr_compare_pref_ipv4(DNS_RR *, DNS_RR *);
181 extern int dns_rr_compare_pref_any(DNS_RR *, DNS_RR *);
182 extern int dns_rr_compare_pref(DNS_RR *, DNS_RR *);
183 extern DNS_RR *dns_rr_shuffle(DNS_RR *);
184 extern DNS_RR *dns_rr_remove(DNS_RR *, DNS_RR *);
185 
186  /*
187  * dns_rr_to_pa.c
188  */
189 extern const char *dns_rr_to_pa(DNS_RR *, MAI_HOSTADDR_STR *);
190 
191  /*
192  * dns_sa_to_rr.c
193  */
194 extern DNS_RR *dns_sa_to_rr(const char *, unsigned, struct sockaddr *);
195 
196  /*
197  * dns_rr_to_sa.c
198  */
199 extern int dns_rr_to_sa(DNS_RR *, unsigned, struct sockaddr *, SOCKADDR_SIZE *);
200 
201  /*
202  * dns_rr_eq_sa.c
203  */
204 extern int dns_rr_eq_sa(DNS_RR *, struct sockaddr *);
205 
206 #ifdef HAS_IPV6
207 #define DNS_RR_EQ_SA(rr, sa) \
208  ((SOCK_ADDR_IN_FAMILY(sa) == AF_INET && (rr)->type == T_A \
209  && SOCK_ADDR_IN_ADDR(sa).s_addr == IN_ADDR((rr)->data).s_addr) \
210  || (SOCK_ADDR_IN_FAMILY(sa) == AF_INET6 && (rr)->type == T_AAAA \
211  && memcmp((char *) &(SOCK_ADDR_IN6_ADDR(sa)), \
212  (rr)->data, (rr)->data_len) == 0))
213 #else
214 #define DNS_RR_EQ_SA(rr, sa) \
215  (SOCK_ADDR_IN_FAMILY(sa) == AF_INET && (rr)->type == T_A \
216  && SOCK_ADDR_IN_ADDR(sa).s_addr == IN_ADDR((rr)->data).s_addr)
217 #endif
218 
219  /*
220  * dns_lookup.c
221  */
222 extern int dns_lookup_x(const char *, unsigned, unsigned, DNS_RR **,
223  VSTRING *, VSTRING *, int *, unsigned);
224 extern int dns_lookup_rl(const char *, unsigned, DNS_RR **, VSTRING *,
225  VSTRING *, int *, int,...);
226 extern int dns_lookup_rv(const char *, unsigned, DNS_RR **, VSTRING *,
227  VSTRING *, int *, int, unsigned *);
228 
229 #define dns_lookup(name, type, rflags, list, fqdn, why) \
230  dns_lookup_x((name), (type), (rflags), (list), (fqdn), (why), (int *) 0, \
231  (unsigned) 0)
232 #define dns_lookup_r(name, type, rflags, list, fqdn, why, rcode) \
233  dns_lookup_x((name), (type), (rflags), (list), (fqdn), (why), (rcode), \
234  (unsigned) 0)
235 #define dns_lookup_l(name, rflags, list, fqdn, why, lflags, ...) \
236  dns_lookup_rl((name), (rflags), (list), (fqdn), (why), (int *) 0, \
237  (lflags), __VA_ARGS__)
238 #define dns_lookup_v(name, rflags, list, fqdn, why, lflags, ltype) \
239  dns_lookup_rv((name), (rflags), (list), (fqdn), (why), (int *) 0, \
240  (lflags), (ltype))
241 
242  /*
243  * Request flags.
244  */
245 #define DNS_REQ_FLAG_STOP_OK (1<<0)
246 #define DNS_REQ_FLAG_STOP_INVAL (1<<1)
247 #define DNS_REQ_FLAG_STOP_NULLMX (1<<2)
248 #define DNS_REQ_FLAG_STOP_MX_POLICY (1<<3)
249 #define DNS_REQ_FLAG_NCACHE_TTL (1<<4)
250 #define DNS_REQ_FLAG_NONE (0)
251 
252  /*
253  * Status codes. Failures must have negative codes so they will not collide
254  * with valid counts of answer records etc.
255  *
256  * When a function queries multiple record types for one name, it issues one
257  * query for each query record type. Each query returns a (status, rcode,
258  * text). Only one of these (status, rcode, text) will be returned to the
259  * caller. The selection is based on the status code precedence.
260  *
261  * - Return DNS_OK (and the corresponding rcode) as long as any query returned
262  * DNS_OK. If this is changed, then code needs to be added to prevent memory
263  * leaks.
264  *
265  * - Return DNS_RETRY (and the corresponding rcode and text) instead of any
266  * hard negative result.
267  *
268  * - Return DNS_NOTFOUND (and the corresponding rcode and text) only when all
269  * queries returned DNS_NOTFOUND.
270  *
271  * DNS_POLICY ranks higher than DNS_RETRY because there was a DNS_OK result,
272  * but the reply filter dropped it. This is a very soft error.
273  *
274  * Below is the precedence order. The order between DNS_RETRY and DNS_NOTFOUND
275  * is arbitrary.
276  */
277 #define DNS_RECURSE (-7) /* internal only: recursion needed */
278 #define DNS_NOTFOUND (-6) /* query ok, data not found */
279 #define DNS_NULLMX (-5) /* query ok, service unavailable */
280 #define DNS_FAIL (-4) /* query failed, don't retry */
281 #define DNS_INVAL (-3) /* query ok, malformed reply */
282 #define DNS_RETRY (-2) /* query failed, try again */
283 #define DNS_POLICY (-1) /* query ok, all records dropped */
284 #define DNS_OK 0 /* query succeeded */
285 
286  /*
287  * How long can a DNS name or single text value be?
288  */
289 #define DNS_NAME_LEN 1024
290 
291  /*
292  * dns_rr_filter.c.
293  */
294 extern void dns_rr_filter_compile(const char *, const char *);
295 
296 #ifdef LIBDNS_INTERNAL
297 #include <maps.h>
298 extern MAPS *dns_rr_filter_maps;
299 extern int dns_rr_filter_execute(DNS_RR **);
300 
301 #endif
302 
303  /*
304  * dns_str_resflags.c
305  */
306 const char *dns_str_resflags(unsigned long);
307 
308 /* LICENSE
309 /* .ad
310 /* .fi
311 /* The Secure Mailer license must be distributed with this software.
312 /* AUTHOR(S)
313 /* Wietse Venema
314 /* IBM T.J. Watson Research
315 /* P.O. Box 704
316 /* Yorktown Heights, NY 10598, USA
317 /*
318 /* Wietse Venema
319 /* Google, Inc.
320 /* 111 8th Avenue
321 /* New York, NY 10011, USA
322 /*--*/
323 
324 #endif
unsigned short pref
Definition: dns.h:146
const char * dns_strtype(unsigned)
Definition: dns_strtype.c:187
const char * dns_str_resflags(unsigned long)
char * qname
Definition: dns.h:140
int dns_rr_compare_pref_ipv4(DNS_RR *, DNS_RR *)
Definition: dns_rr.c:197
unsigned int ttl
Definition: dns.h:131
struct DNS_RR DNS_RR
int dns_rr_compare_pref(DNS_RR *, DNS_RR *)
Definition: dns_rr.c:223
int dns_lookup_rl(const char *, unsigned, DNS_RR **, VSTRING *, VSTRING *, int *, int,...)
Definition: dns_lookup.c:1054
unsigned length
Definition: dns.h:132
unsigned short type
Definition: dns.h:129
char data[1]
Definition: dns.h:149
Definition: maps.h:22
const char * dns_rr_to_pa(DNS_RR *, MAI_HOSTADDR_STR *)
Definition: dns_rr_to_pa.c:53
#define SOCKADDR_SIZE
Definition: sys_defs.h:1411
DNS_RR * dns_rr_create(const char *, const char *, ushort, ushort, unsigned, unsigned, const char *, size_t)
const char * dns_strerror(unsigned)
Definition: dns_strerror.c:57
int dns_lookup_x(const char *, unsigned, unsigned, DNS_RR **, VSTRING *, VSTRING *, int *, unsigned)
Definition: dns_lookup.c:914
DNS_RR * dns_rr_remove(DNS_RR *, DNS_RR *)
Definition: dns_rr.c:334
MAPS * dns_rr_filter_maps
Definition: dns_rr_filter.c:77
int dns_rr_to_sa(DNS_RR *, unsigned, struct sockaddr *, SOCKADDR_SIZE *)
Definition: dns_rr_to_sa.c:57
Definition: dns.h:128
DNS_RR * dns_rr_shuffle(DNS_RR *)
Definition: dns_rr.c:288
DNS_RR * dns_rr_copy(DNS_RR *)
Definition: dns_rr.c:150
size_t data_len
Definition: dns.h:148
DNS_RR * dns_sa_to_rr(const char *, unsigned, struct sockaddr *)
Definition: dns_sa_to_rr.c:53
struct DNS_RR * next
Definition: dns.h:147
struct DNS_FIXED DNS_FIXED
int dns_rr_compare_pref_any(DNS_RR *, DNS_RR *)
Definition: dns_rr.c:214
DNS_RR * dns_rr_append(DNS_RR *, DNS_RR *)
Definition: dns_rr.c:168
int dns_rr_eq_sa(DNS_RR *, struct sockaddr *)
Definition: dns_rr_eq_sa.c:57
unsigned dns_type(const char *)
Definition: dns_strtype.c:203
char * rname
Definition: dns.h:141
unsigned int dnssec_valid
Definition: dns.h:145
DNS_RR * dns_rr_sort(DNS_RR *, int(*)(DNS_RR *, DNS_RR *))
Definition: dns_rr.c:242
int dns_lookup_rv(const char *, unsigned, DNS_RR **, VSTRING *, VSTRING *, int *, int, unsigned *)
Definition: dns_lookup.c:1130
int dns_rr_filter_execute(DNS_RR **rrlist)
char * dns_strrecord(VSTRING *, DNS_RR *)
Definition: dns_strrecord.c:50
unsigned int ttl
Definition: dns.h:144
unsigned short type
Definition: dns.h:142
int dns_rr_compare_pref_ipv6(DNS_RR *, DNS_RR *)
Definition: dns_rr.c:180
void dns_rr_free(DNS_RR *)
Definition: dns_rr.c:137
Definition: dns.h:139
void dns_rr_filter_compile(const char *, const char *)
Definition: dns_rr_filter.c:85