]> git.saurik.com Git - apple/libresolv.git/blob - dns_util.h
libresolv-65.200.2.tar.gz
[apple/libresolv.git] / dns_util.h
1 /*
2 * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef __DNS_UTIL_H__
25 #define __DNS_UTIL_H__
26
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <dns.h>
34 #include <stdio.h>
35
36 /*
37 * Status returned in a dns_reply_t
38 */
39 #define DNS_STATUS_OK 0
40 #define DNS_STATUS_BAD_HANDLE 1
41 #define DNS_STATUS_MALFORMED_QUERY 2
42 #define DNS_STATUS_TIMEOUT 3
43 #define DNS_STATUS_SEND_FAILED 4
44 #define DNS_STATUS_RECEIVE_FAILED 5
45 #define DNS_STATUS_CONNECTION_FAILED 6
46 #define DNS_STATUS_WRONG_SERVER 7
47 #define DNS_STATUS_WRONG_XID 8
48 #define DNS_STATUS_WRONG_QUESTION 9
49
50 /*
51 * dns_print_reply mask
52 */
53 #define DNS_PRINT_XID 0x0001
54 #define DNS_PRINT_QR 0x0002
55 #define DNS_PRINT_OPCODE 0x0004
56 #define DNS_PRINT_AA 0x0008
57 #define DNS_PRINT_TC 0x0010
58 #define DNS_PRINT_RD 0x0020
59 #define DNS_PRINT_RA 0x0040
60 #define DNS_PRINT_PR 0x0080
61 #define DNS_PRINT_RCODE 0x0100
62 #define DNS_PRINT_QUESTION 0x0200
63 #define DNS_PRINT_ANSWER 0x0400
64 #define DNS_PRINT_AUTHORITY 0x0800
65 #define DNS_PRINT_ADDITIONAL 0x1000
66 #define DNS_PRINT_SERVER 0x2000
67
68 /*
69 * DNS query / reply header
70 */
71 typedef struct {
72 uint16_t xid;
73 uint16_t flags;
74 uint16_t qdcount;
75 uint16_t ancount;
76 uint16_t nscount;
77 uint16_t arcount;
78 } dns_header_t;
79
80 /*
81 * DNS query
82 */
83 typedef struct
84 {
85 char *name;
86 uint16_t dnstype;
87 uint16_t dnsclass;
88 } dns_question_t;
89
90 /*
91 * Resource Record types
92 * dns_parse_packet() creates resourse records of these types.
93 */
94 typedef struct
95 {
96 uint16_t length;
97 char *data;
98 } dns_raw_resource_record_t;
99
100 typedef struct
101 {
102 struct in_addr addr;
103 } dns_address_record_t;
104
105 typedef struct
106 {
107 struct in6_addr addr;
108 } dns_in6_address_record_t;
109
110 typedef struct
111 {
112 char *name;
113 } dns_domain_name_record_t;
114
115 typedef struct
116 {
117 char *mname;
118 char *rname;
119 uint32_t serial;
120 uint32_t refresh;
121 uint32_t retry;
122 uint32_t expire;
123 uint32_t minimum;
124 } dns_SOA_record_t;
125
126 typedef struct
127 {
128 char *cpu;
129 char *os;
130 } dns_HINFO_record_t;
131
132 typedef struct
133 {
134 char *rmailbx;
135 char *emailbx;
136 } dns_MINFO_record_t;
137
138 typedef struct
139 {
140 uint16_t preference;
141 char *name;
142 } dns_MX_record_t;
143
144 typedef struct
145 {
146 uint32_t string_count;
147 char **strings;
148 } dns_TXT_record_t;
149
150 typedef struct
151 {
152 struct in_addr addr;
153 uint8_t protocol;
154 uint32_t maplength;
155 uint8_t *map;
156 } dns_WKS_record_t;
157
158 typedef struct
159 {
160 char *mailbox;
161 char *txtdname;
162 } dns_RP_record_t;
163
164 typedef struct
165 {
166 uint32_t subtype;
167 char *hostname;
168 } dns_AFSDB_record_t;
169
170 typedef struct
171 {
172 char *psdn_address;
173 } dns_X25_record_t;
174
175 typedef struct
176 {
177 char *isdn_address;
178 char *subaddress;
179 } dns_ISDN_record_t;
180
181 typedef struct
182 {
183 uint16_t preference;
184 char * intermediate;
185 } dns_RT_record_t;
186
187 typedef struct
188 {
189 uint8_t version;
190 uint8_t size;
191 uint8_t horizontal_precision;
192 uint8_t vertical_precision;
193 uint32_t latitude;
194 uint32_t longitude;
195 uint32_t altitude;
196 } dns_LOC_record_t;
197
198 typedef struct
199 {
200 uint16_t priority;
201 uint16_t weight;
202 uint16_t port;
203 char *target;
204 } dns_SRV_record_t;
205
206 /*
207 * DNS Resource Record
208 *
209 * Data contained in unsupported or obsolete Resource Record types
210 * may be accessed via DNSNULL as a dns_raw_resource_record_t.
211 */
212 typedef struct
213 {
214 char *name;
215 uint16_t dnstype;
216 uint16_t dnsclass;
217 uint32_t ttl;
218 union
219 {
220 dns_address_record_t *A;
221 dns_domain_name_record_t *NS;
222 dns_domain_name_record_t *MD; /* Obsolete */
223 dns_domain_name_record_t *MF; /* Obsolete */
224 dns_domain_name_record_t *CNAME;
225 dns_SOA_record_t *SOA;
226 dns_domain_name_record_t *MB;
227 dns_domain_name_record_t *MG;
228 dns_domain_name_record_t *MR;
229 dns_raw_resource_record_t *DNSNULL;
230 dns_WKS_record_t *WKS;
231 dns_domain_name_record_t *PTR;
232 dns_HINFO_record_t *HINFO;
233 dns_MINFO_record_t *MINFO;
234 dns_MX_record_t *MX;
235 dns_TXT_record_t *TXT;
236 dns_RP_record_t *RP;
237 dns_AFSDB_record_t *AFSDB;
238 dns_X25_record_t *X25;
239 dns_ISDN_record_t *ISDN;
240 dns_RT_record_t *RT;
241 dns_in6_address_record_t *AAAA;
242 dns_LOC_record_t *LOC;
243 dns_SRV_record_t *SRV;
244 } data;
245 } dns_resource_record_t;
246
247 /*
248 * A parsed DNS record. Returned by dns_parse_packet() and dns_lookup().
249 * The contents may be printed using dns_print_reply().
250 */
251 typedef struct
252 {
253 uint32_t status;
254 struct sockaddr *server;
255 dns_header_t *header;
256 dns_question_t **question;
257 dns_resource_record_t **answer;
258 dns_resource_record_t **authority;
259 dns_resource_record_t **additional;
260 } dns_reply_t;
261
262
263 __BEGIN_DECLS
264
265 /*
266 * High-level lookup performs a search (using dns_search), parses the
267 * reply and returns a dns_reply_t structure.
268 *
269 * The DNS handle contains an internal buffer used for fetching replies.
270 * The buffer is reused for each query, and is released with the DNS client
271 * handle when dns_free() is called. The default buffer size is 1024 bytes.
272 * The size may be changed with dns_set_buffer_size.
273 *
274 * Note that in a multithreaded application, each thread using this API must
275 * open a separate handle.
276 */
277 extern dns_reply_t *dns_lookup(dns_handle_t dns, const char *name, uint32_t dnsclass, uint32_t dnstype);
278
279 /*
280 * Get / Set the size of the internal receive buffer used by dns_lookup()
281 */
282 extern uint32_t dns_get_buffer_size(dns_handle_t d);
283 extern void dns_set_buffer_size(dns_handle_t d, uint32_t len);
284
285 /*
286 * Parse a reply packet into a reply structure.
287 */
288 extern dns_reply_t *dns_parse_packet(const char *buf, uint32_t len);
289
290 /*
291 * Free a reply structure.
292 */
293 extern void dns_free_reply(dns_reply_t *r);
294
295 /*
296 * Parse a query packet into a question structure.
297 */
298 extern dns_question_t *dns_parse_question(const char *buf, uint32_t len);
299
300 /*
301 * Free a question structure.
302 */
303 extern void dns_free_question(dns_question_t *q);
304
305 /*
306 * Parse a resource record into a structure.
307 */
308 extern dns_resource_record_t *dns_parse_resource_record(const char *buf, uint32_t len);
309
310 /*
311 * Free a resource record structure.
312 */
313 extern void dns_free_resource_record(dns_resource_record_t *rr);
314
315 /*
316 * String / number representation of a DNS class
317 * dns_class_number returns 0 if the string is recognized,
318 * non-zero if the class string is unknown.
319 */
320 extern const char *dns_class_string(uint16_t dnsclass);
321 extern int32_t dns_class_number(const char *c, uint16_t *n);
322
323 /*
324 * String / number representation of a DNS type
325 * dns_type_number returns 0 if the string is recognized,
326 * non-zero if the class string is unknown.
327 */
328 extern const char *dns_type_string(uint16_t dnstype);
329 extern int32_t dns_type_number(const char *t, uint16_t *n);
330
331 /*
332 * Print a dns handle.
333 */
334 extern void dns_print_handle(dns_handle_t d, FILE *f);
335
336 /*
337 * Print the contents of a question structure.
338 */
339 extern void dns_print_question(const dns_question_t *q, FILE *f);
340
341 /*
342 * Print the contents of a resource record structure.
343 */
344 extern void dns_print_resource_record(const dns_resource_record_t *r, FILE *f);
345
346 /*
347 * Print the contents of a reply structure.
348 */
349 extern void dns_print_reply(const dns_reply_t *r, FILE *f, uint16_t mask);
350
351 __END_DECLS
352
353 #endif /* __DNS_UTIL_H__ */