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 35/* 36 * Status returned in a dns_reply_t 37 */ 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 48 49/* 50 * dns_print_reply mask 51 */ 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 66 67/* 68 * DNS query / reply header 69 */ 70typedef struct { 71 uint16_t xid; 72 uint16_t flags; 73 uint16_t qdcount; 74 uint16_t ancount; 75 uint16_t nscount; 76 uint16_t arcount; 77} dns_header_t; 78 79/* 80 * DNS query 81 */ 82typedef struct 83{ 84 char *name; 85 uint16_t dnstype; 86 uint16_t dnsclass; 87} dns_question_t; 88 89/* 90 * Resource Record types 91 * dns_parse_packet() creates resourse records of these types. 92 */ 93typedef struct 94{ 95 uint16_t length; 96 char *data; 97} dns_raw_resource_record_t; 98 99typedef struct 100{ 101 struct in_addr addr; 102} dns_address_record_t; 103 104typedef struct 105{ 106 struct in6_addr addr; 107} dns_in6_address_record_t; 108 109typedef struct 110{ 111 char *name; 112} dns_domain_name_record_t; 113 114typedef struct 115{ 116 char *mname; 117 char *rname; 118 uint32_t serial; 119 uint32_t refresh; 120 uint32_t retry; 121 uint32_t expire; 122 uint32_t minimum; 123} dns_SOA_record_t; 124 125typedef struct 126{ 127 char *cpu; 128 char *os; 129} dns_HINFO_record_t; 130 131typedef struct 132{ 133 char *rmailbx; 134 char *emailbx; 135} dns_MINFO_record_t; 136 137typedef struct 138{ 139 uint16_t preference; 140 char *name; 141} dns_MX_record_t; 142 143typedef struct 144{ 145 uint32_t string_count; 146 char **strings; 147} dns_TXT_record_t; 148 149typedef struct 150{ 151 struct in_addr addr; 152 uint8_t protocol; 153 uint32_t maplength; 154 uint8_t *map; 155} dns_WKS_record_t; 156 157typedef struct 158{ 159 char *mailbox; 160 char *txtdname; 161} dns_RP_record_t; 162 163typedef struct 164{ 165 uint32_t subtype; 166 char *hostname; 167} dns_AFSDB_record_t; 168 169typedef struct 170{ 171 char *psdn_address; 172} dns_X25_record_t; 173 174typedef struct 175{ 176 char *isdn_address; 177 char *subaddress; 178} dns_ISDN_record_t; 179 180typedef struct 181{ 182 uint16_t preference; 183 char * intermediate; 184} dns_RT_record_t; 185 186typedef struct 187{ 188 uint8_t version; 189 uint8_t size; 190 uint8_t horizontal_precision; 191 uint8_t vertical_precision; 192 uint32_t latitude; 193 uint32_t longitude; 194 uint32_t altitude; 195} dns_LOC_record_t; 196 197typedef struct 198{ 199 uint16_t priority; 200 uint16_t weight; 201 uint16_t port; 202 char *target; 203} dns_SRV_record_t; 204 205/* 206 * DNS Resource Record 207 * 208 * Data contained in unsupported or obsolete Resource Record types 209 * may be accessed via DNSNULL as a dns_raw_resource_record_t. 210 */ 211typedef struct 212{ 213 char *name; 214 uint16_t dnstype; 215 uint16_t dnsclass; 216 uint32_t ttl; 217 union 218 { 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; 233 dns_MX_record_t *MX; 234 dns_TXT_record_t *TXT; 235 dns_RP_record_t *RP; 236 dns_AFSDB_record_t *AFSDB; 237 dns_X25_record_t *X25; 238 dns_ISDN_record_t *ISDN; 239 dns_RT_record_t *RT; 240 dns_in6_address_record_t *AAAA; 241 dns_LOC_record_t *LOC; 242 dns_SRV_record_t *SRV; 243 } data; 244} dns_resource_record_t; 245 246/* 247 * A parsed DNS record. Returned by dns_parse_packet() and dns_lookup(). 248 * The contents may be printed using dns_print_reply(). 249 */ 250typedef struct 251{ 252 uint32_t status; 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; 259} dns_reply_t; 260 261 262__BEGIN_DECLS 263 264/* 265 * High-level lookup performs a search (using dns_search), parses the 266 * reply and returns a dns_reply_t structure. 267 * 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. 272 * 273 * Note that in a multithreaded application, each thread using this API must 274 * open a separate handle. 275 */ 276extern dns_reply_t *dns_lookup(dns_handle_t dns, const char *name, uint32_t dnsclass, uint32_t dnstype); 277 278/* 279 * Get / Set the size of the internal receive buffer used by dns_lookup() 280 */ 281extern uint32_t dns_get_buffer_size(dns_handle_t d); 282extern void dns_set_buffer_size(dns_handle_t d, uint32_t len); 283 284/* 285 * Parse a reply packet into a reply structure. 286 */ 287extern dns_reply_t *dns_parse_packet(const char *buf, uint32_t len); 288 289/* 290 * Free a reply structure. 291 */ 292extern void dns_free_reply(dns_reply_t *r); 293 294/* 295 * Parse a query packet into a question structure. 296 */ 297extern dns_question_t *dns_parse_question(const char *buf, uint32_t len); 298 299/* 300 * Free a question structure. 301 */ 302extern void dns_free_question(dns_question_t *q); 303 304/* 305 * Parse a resource record into a structure. 306 */ 307extern dns_resource_record_t *dns_parse_resource_record(const char *buf, uint32_t len); 308 309/* 310 * Free a resource record structure. 311 */ 312extern void dns_free_resource_record(dns_resource_record_t *rr); 313 314/* 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. 318 */ 319extern const char *dns_class_string(uint16_t dnsclass); 320extern int32_t dns_class_number(const char *c, uint16_t *n); 321 322/* 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. 326 */ 327extern const char *dns_type_string(uint16_t dnstype); 328extern int32_t dns_type_number(const char *t, uint16_t *n); 329 330/* 331 * Print a dns handle. 332 */ 333extern void dns_print_handle(dns_handle_t d, FILE *f); 334 335/* 336 * Print the contents of a question structure. 337 */ 338extern void dns_print_question(const dns_question_t *q, FILE *f); 339 340/* 341 * Print the contents of a resource record structure. 342 */ 343extern void dns_print_resource_record(const dns_resource_record_t *r, FILE *f); 344 345/* 346 * Print the contents of a reply structure. 347 */ 348extern void dns_print_reply(const dns_reply_t *r, FILE *f, uint16_t mask); 349 350__END_DECLS 351 352#endif /* __DNS_UTIL_H__ */ 353