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>
36 * Status returned in a dns_reply_t
38 #define DNS_STATUS_OK 0
39 #define DNS_STATUS_BAD_HANDLE 1
40 #define DNS_STATUS_MALFORMED_QUERY 2
41 #define DNS_STATUS_TIMEOUT 3
42 #define DNS_STATUS_SEND_FAILED 4
43 #define DNS_STATUS_RECEIVE_FAILED 5
44 #define DNS_STATUS_CONNECTION_FAILED 6
45 #define DNS_STATUS_WRONG_SERVER 7
46 #define DNS_STATUS_WRONG_XID 8
47 #define DNS_STATUS_WRONG_QUESTION 9
50 * dns_print_reply mask
52 #define DNS_PRINT_XID 0x0001
53 #define DNS_PRINT_QR 0x0002
54 #define DNS_PRINT_OPCODE 0x0004
55 #define DNS_PRINT_AA 0x0008
56 #define DNS_PRINT_TC 0x0010
57 #define DNS_PRINT_RD 0x0020
58 #define DNS_PRINT_RA 0x0040
59 #define DNS_PRINT_PR 0x0080
60 #define DNS_PRINT_RCODE 0x0100
61 #define DNS_PRINT_QUESTION 0x0200
62 #define DNS_PRINT_ANSWER 0x0400
63 #define DNS_PRINT_AUTHORITY 0x0800
64 #define DNS_PRINT_ADDITIONAL 0x1000
65 #define DNS_PRINT_SERVER 0x2000
68 * DNS query / reply header
90 * Resource Record types
91 * dns_parse_packet() creates resourse records of these types.
97 } dns_raw_resource_record_t
;
102 } dns_address_record_t
;
106 struct in6_addr addr
;
107 } dns_in6_address_record_t
;
112 } dns_domain_name_record_t
;
129 } dns_HINFO_record_t
;
135 } dns_MINFO_record_t
;
145 uint32_t string_count
;
167 } dns_AFSDB_record_t
;
190 uint8_t horizontal_precision
;
191 uint8_t vertical_precision
;
206 * DNS Resource Record
208 * Data contained in unsupported or obsolete Resource Record types
209 * may be accessed via DNSNULL as a dns_raw_resource_record_t.
219 dns_address_record_t
*A
;
220 dns_domain_name_record_t
*NS
;
221 dns_domain_name_record_t
*MD
; /* Obsolete */
222 dns_domain_name_record_t
*MF
; /* Obsolete */
223 dns_domain_name_record_t
*CNAME
;
224 dns_SOA_record_t
*SOA
;
225 dns_domain_name_record_t
*MB
;
226 dns_domain_name_record_t
*MG
;
227 dns_domain_name_record_t
*MR
;
228 dns_raw_resource_record_t
*DNSNULL
;
229 dns_WKS_record_t
*WKS
;
230 dns_domain_name_record_t
*PTR
;
231 dns_HINFO_record_t
*HINFO
;
232 dns_MINFO_record_t
*MINFO
;
234 dns_TXT_record_t
*TXT
;
236 dns_AFSDB_record_t
*AFSDB
;
237 dns_X25_record_t
*X25
;
238 dns_ISDN_record_t
*ISDN
;
240 dns_in6_address_record_t
*AAAA
;
241 dns_LOC_record_t
*LOC
;
242 dns_SRV_record_t
*SRV
;
244 } dns_resource_record_t
;
247 * A parsed DNS record. Returned by dns_parse_packet() and dns_lookup().
248 * The contents may be printed using dns_print_reply().
253 struct sockaddr
*server
;
254 dns_header_t
*header
;
255 dns_question_t
**question
;
256 dns_resource_record_t
**answer
;
257 dns_resource_record_t
**authority
;
258 dns_resource_record_t
**additional
;
265 * High-level lookup performs a search (using dns_search), parses the
266 * reply and returns a dns_reply_t structure.
268 * The DNS handle contains an internal buffer used for fetching replies.
269 * The buffer is reused for each query, and is released with the DNS client
270 * handle when dns_free() is called. The default buffer size is 1024 bytes.
271 * The size may be changed with dns_set_buffer_size.
273 * Note that in a multithreaded application, each thread using this API must
274 * open a separate handle.
276 extern dns_reply_t
*dns_lookup(dns_handle_t dns
, const char *name
, uint32_t dnsclass
, uint32_t dnstype
);
279 * Get / Set the size of the internal receive buffer used by dns_lookup()
281 extern uint32_t dns_get_buffer_size(dns_handle_t d
);
282 extern void dns_set_buffer_size(dns_handle_t d
, uint32_t len
);
285 * Parse a reply packet into a reply structure.
287 extern dns_reply_t
*dns_parse_packet(const char *buf
, uint32_t len
);
290 * Free a reply structure.
292 extern void dns_free_reply(dns_reply_t
*r
);
295 * Parse a query packet into a question structure.
297 extern dns_question_t
*dns_parse_question(const char *buf
, uint32_t len
);
300 * Free a question structure.
302 extern void dns_free_question(dns_question_t
*q
);
305 * Parse a resource record into a structure.
307 extern dns_resource_record_t
*dns_parse_resource_record(const char *buf
, uint32_t len
);
310 * Free a resource record structure.
312 extern void dns_free_resource_record(dns_resource_record_t
*rr
);
315 * String / number representation of a DNS class
316 * dns_class_number returns 0 if the string is recognized,
317 * non-zero if the class string is unknown.
319 extern const char *dns_class_string(uint16_t dnsclass
);
320 extern int32_t dns_class_number(const char *c
, uint16_t *n
);
323 * String / number representation of a DNS type
324 * dns_type_number returns 0 if the string is recognized,
325 * non-zero if the class string is unknown.
327 extern const char *dns_type_string(uint16_t dnstype
);
328 extern int32_t dns_type_number(const char *t
, uint16_t *n
);
331 * Print a dns handle.
333 extern void dns_print_handle(dns_handle_t d
, FILE *f
);
336 * Print the contents of a question structure.
338 extern void dns_print_question(const dns_question_t
*q
, FILE *f
);
341 * Print the contents of a resource record structure.
343 extern void dns_print_resource_record(const dns_resource_record_t
*r
, FILE *f
);
346 * Print the contents of a reply structure.
348 extern void dns_print_reply(const dns_reply_t
*r
, FILE *f
, uint16_t mask
);
352 #endif /* __DNS_UTIL_H__ */