2 * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __DNS_UTIL_H__
25 #define __DNS_UTIL_H__
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
37 * Status returned in a dns_reply_t
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
51 * dns_print_reply mask
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
69 * DNS query / reply header
91 * Resource Record types
92 * dns_parse_packet() creates resourse records of these types.
98 } dns_raw_resource_record_t
;
103 } dns_address_record_t
;
107 struct in6_addr addr
;
108 } dns_in6_address_record_t
;
113 } dns_domain_name_record_t
;
130 } dns_HINFO_record_t
;
136 } dns_MINFO_record_t
;
146 uint32_t string_count
;
168 } dns_AFSDB_record_t
;
191 uint8_t horizontal_precision
;
192 uint8_t vertical_precision
;
207 * DNS Resource Record
209 * Data contained in unsupported or obsolete Resource Record types
210 * may be accessed via DNSNULL as a dns_raw_resource_record_t.
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
;
235 dns_TXT_record_t
*TXT
;
237 dns_AFSDB_record_t
*AFSDB
;
238 dns_X25_record_t
*X25
;
239 dns_ISDN_record_t
*ISDN
;
241 dns_in6_address_record_t
*AAAA
;
242 dns_LOC_record_t
*LOC
;
243 dns_SRV_record_t
*SRV
;
245 } dns_resource_record_t
;
248 * A parsed DNS record. Returned by dns_parse_packet() and dns_lookup().
249 * The contents may be printed using dns_print_reply().
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
;
266 * High-level lookup performs a search (using dns_search), parses the
267 * reply and returns a dns_reply_t structure.
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.
274 * Note that in a multithreaded application, each thread using this API must
275 * open a separate handle.
277 extern dns_reply_t
*dns_lookup(dns_handle_t dns
, const char *name
, uint32_t dnsclass
, uint32_t dnstype
);
280 * Get / Set the size of the internal receive buffer used by dns_lookup()
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
);
286 * Parse a reply packet into a reply structure.
288 extern dns_reply_t
*dns_parse_packet(const char *buf
, uint32_t len
);
291 * Free a reply structure.
293 extern void dns_free_reply(dns_reply_t
*r
);
296 * Parse a query packet into a question structure.
298 extern dns_question_t
*dns_parse_question(const char *buf
, uint32_t len
);
301 * Free a question structure.
303 extern void dns_free_question(dns_question_t
*q
);
306 * Parse a resource record into a structure.
308 extern dns_resource_record_t
*dns_parse_resource_record(const char *buf
, uint32_t len
);
311 * Free a resource record structure.
313 extern void dns_free_resource_record(dns_resource_record_t
*rr
);
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.
320 extern const char *dns_class_string(uint16_t dnsclass
);
321 extern int32_t dns_class_number(const char *c
, uint16_t *n
);
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.
328 extern const char *dns_type_string(uint16_t dnstype
);
329 extern int32_t dns_type_number(const char *t
, uint16_t *n
);
332 * Print a dns handle.
334 extern void dns_print_handle(dns_handle_t d
, FILE *f
);
337 * Print the contents of a question structure.
339 extern void dns_print_question(const dns_question_t
*q
, FILE *f
);
342 * Print the contents of a resource record structure.
344 extern void dns_print_resource_record(const dns_resource_record_t
*r
, FILE *f
);
347 * Print the contents of a reply structure.
349 extern void dns_print_reply(const dns_reply_t
*r
, FILE *f
, uint16_t mask
);
353 #endif /* __DNS_UTIL_H__ */