]> git.saurik.com Git - apple/mdnsresponder.git/blob - ServiceRegistration/dns-msg.h
mDNSResponder-1096.60.2.tar.gz
[apple/mdnsresponder.git] / ServiceRegistration / dns-msg.h
1 /* dns-msg.h
2 *
3 * Copyright (c) 2018 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Lightweight framework for generating, sending, and unpacking DNS messages.
18 * Definitions...
19 */
20
21 #ifndef __DNS_MSG_H
22 #define __DNS_MSG_H
23
24 #ifndef DNS_MAX_UDP_PAYLOAD
25 #define DNS_MAX_UDP_PAYLOAD 1410
26 #endif
27
28 #define DNS_HEADER_SIZE 12
29 #define DNS_DATA_SIZE (DNS_MAX_UDP_PAYLOAD - DNS_HEADER_SIZE)
30 #define DNS_MAX_POINTER ((2 << 14) - 1)
31 #define DNS_MAX_LABEL_SIZE 63
32 #define DNS_MAX_NAME_SIZE 255
33 #define DNS_MAX_NAME_SIZE_ESCAPED 1009
34
35 typedef struct dns_wire dns_wire_t;
36 struct dns_wire {
37 uint16_t id;
38 uint16_t bitfield;
39 uint16_t qdcount;
40 uint16_t ancount;
41 uint16_t nscount;
42 uint16_t arcount;
43 uint8_t data[DNS_DATA_SIZE];
44 };
45
46 typedef struct dns_towire_state dns_towire_state_t;
47 struct dns_towire_state {
48 dns_wire_t *NULLABLE message;
49 uint8_t *NONNULL p;
50 uint8_t *NONNULL lim;
51 uint8_t *NULLABLE p_rdlength;
52 uint8_t *NULLABLE p_opt;
53 int error;
54 };
55
56 typedef struct dns_transaction dns_transaction_t;
57 struct dns_transaction {
58 dns_transaction_t *NULLABLE next;
59 dns_towire_state_t towire;
60 dns_wire_t *NULLABLE response;
61 int response_length;
62 int sock;
63 };
64
65 typedef struct dns_name_pointer dns_name_pointer_t;
66 struct dns_name_pointer {
67 uint8_t *NONNULL message_start;
68 uint8_t *NONNULL name_start;
69 int num_labels;
70 int length;
71 };
72
73 typedef void (*dns_response_callback_t)(dns_transaction_t *NONNULL txn);
74
75 typedef struct dns_label dns_label_t;
76 typedef dns_label_t dns_name_t;
77 struct dns_label {
78 dns_label_t *NULLABLE next;
79 uint8_t len;
80 char data[DNS_MAX_LABEL_SIZE];
81 };
82
83 typedef struct dns_txt_element dns_txt_element_t;
84 struct dns_txt_element {
85 dns_txt_element_t *NULLABLE next;
86 uint8_t len;
87 char data[0];
88 };
89
90 typedef struct dns_rdata_unparsed dns_rdata_unparsed_t;
91 struct dns_rdata_unparsed {
92 uint8_t *NULLABLE data;
93 uint16_t len;
94 };
95
96 typedef struct dns_rdata_single_name dns_rdata_ptr_t;
97 typedef struct dns_rdata_single_name dns_rdata_cname_t;
98 struct dns_rdata_single_name {
99 dns_label_t *NONNULL name;
100 };
101
102 typedef struct dns_rdata_a dns_rdata_a_t;
103 struct dns_rdata_a {
104 struct in_addr *NONNULL addrs;
105 int num;
106 };
107
108 typedef struct dns_rdata_aaaa dns_rdata_aaaa_t;
109 struct dns_rdata_aaaa {
110 struct in6_addr *NONNULL addrs;
111 int num;
112 } aaaa;
113
114 typedef struct dns_rdata_srv dns_rdata_srv_t;
115 struct dns_rdata_srv {
116 dns_label_t *NONNULL name;
117 uint16_t priority;
118 uint16_t weight;
119 uint16_t port;
120 } srv;
121
122 typedef struct dns_rdata_sig dns_rdata_sig_t;
123 struct dns_rdata_sig {
124 uint16_t type;
125 uint8_t algorithm;
126 uint8_t label;
127 uint32_t rrttl;
128 uint32_t expiry;
129 uint32_t inception;
130 uint16_t key_tag;
131 dns_label_t *NONNULL signer;
132 int start;
133 int len;
134 uint8_t *NONNULL signature;
135 } sig;
136
137 typedef struct dns_rdata_key dns_rdata_key_t;
138 struct dns_rdata_key {
139 uint16_t flags;
140 uint8_t protocol;
141 uint8_t algorithm;
142 int len;
143 uint8_t *NONNULL key;
144 } key;
145
146 typedef struct dns_rr dns_rr_t;
147 struct dns_rr {
148 dns_label_t *NONNULL name;
149 uint16_t type;
150 uint16_t qclass;
151 uint32_t ttl;
152 union {
153 dns_rdata_unparsed_t unparsed;
154 dns_rdata_ptr_t ptr;
155 dns_rdata_cname_t cname;
156 dns_rdata_a_t a;
157 dns_rdata_aaaa_t aaaa;
158 dns_rdata_srv_t srv;
159 dns_txt_element_t *NONNULL txt;
160 dns_rdata_sig_t sig;
161 dns_rdata_key_t key;
162 } data;
163 };
164
165 typedef struct dns_edns0 dns_edns0_t;
166 struct dns_edns0 {
167 dns_edns0_t *NULLABLE next;
168 uint16_t length;
169 uint8_t data[0];
170 };
171
172 typedef struct dns_message dns_message_t;
173 struct dns_message {
174 dns_wire_t *NULLABLE wire;
175 int qdcount, ancount, nscount, arcount;
176 dns_rr_t *NULLABLE questions;
177 dns_rr_t *NULLABLE answers;
178 dns_rr_t *NULLABLE authority;
179 dns_rr_t *NULLABLE additional;
180 dns_edns0_t *NULLABLE edns0;
181 };
182
183 // Masks for bitfield data
184 #define dns_qr_mask 0x8000
185 #define dns_opcode_mask 0x7800
186 #define dns_flags_mask 0x07f0
187 #define dns_rcode_mask 0x000f
188
189 // Shifts for bitfield data
190 #define dns_qr_shift 15
191 #define dns_opcode_shift 11
192 #define dns_rcode_shift 0
193
194 // Booleans
195 #define dns_flags_aa 0x0400
196 #define dns_flags_tc 0x0200
197 #define dns_flags_rd 0x0100
198 #define dns_flags_ra 0x0080
199 #define dns_flags_ad 0x0020
200 #define dns_flags_cd 0x0010
201
202 // Getters
203 #define dns_qr_get(w) ((ntohs((w)->bitfield) & dns_qr_mask) >> dns_qr_shift)
204 #define dns_opcode_get(w) ((ntohs((w)->bitfield) & dns_opcode_mask) >> dns_opcode_shift)
205 #define dns_rcode_get(w) ((ntohs((w)->bitfield) & dns_rcode_mask) >> dns_rcode_shift)
206
207 // Setters
208 #define dns_qr_set(w, value) ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_qr_mask) | \
209 ((value) << dns_qr_shift))))
210 #define dns_opcode_set(w, value) ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_opcode_mask) | \
211 ((value) << dns_opcode_shift))))
212 #define dns_rcode_set(w, value) ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_rcode_mask) | \
213 ((value) << dns_rcode_shift))))
214
215 // Query/Response
216 #define dns_qr_query 0
217 #define dns_qr_response 1
218
219 // Opcodes
220 #define dns_opcode_query 0
221 #define dns_opcode_iquery 1
222 #define dns_opcode_status 2
223 #define dns_opcode_notify 4
224 #define dns_opcode_update 5
225 #define dns_opcode_dso 6
226
227 // Response Codes
228 #define dns_rcode_noerror 0 // [RFC1035] No Error
229 #define dns_rcode_formerr 1 // [RFC1035] Format Error
230 #define dns_rcode_servfail 2 // [RFC1035] Server Failure
231 #define dns_rcode_nxdomain 3 // [RFC1035] Non-Existent Domain
232 #define dns_rcode_notimp 4 // [RFC1035] Not Implemented
233 #define dns_rcode_refused 5 // [RFC1035] Query Refused
234 #define dns_rcode_yxdomain 6 // [RFC2136][RFC6672] Name Exists when it should not
235 #define dns_rcode_yxrrset 7 // [RFC2136] RR Set Exists when it should not
236 #define dns_rcode_nxrrset 8 // [RFC2136] RR Set that should exist does not
237 #define dns_rcode_notauth 9 // [RFC2136] Server Not Authoritative for zone, or [RFC2845] Not Authorized
238 #define dns_rcode_notzone 10 // [RFC2136] Name not contained in zone
239 #define dns_rcode_dsotypeni 11 // [RFCTBD draft-ietf-dnsop-session-signal] DSO-Type Not Implemented
240 #define dns_rcode_badvers 16 // [RFC6891] Bad OPT Version, or [RFC2845] TSIG Signature Failure
241 #define dns_rcode_badkey 17 // [RFC2845] Key not recognized
242 #define dns_rcode_badtime 18 // [RFC2845] Signature out of time window
243 #define dns_rcode_badmode 19 // [RFC2930] Bad TKEY Mode
244 #define dns_rcode_badname 20 // [RFC2930] Duplicate key name
245 #define dns_rcode_badalg 21 // [RFC2930] Algorithm not supported
246 #define dns_rcode_badtrunc 22 // [RFC4635] Bad Truncation
247 #define dns_rcode_badcookie 23 // [RFC7873] Bad/missing Server Cookie
248
249 #define dns_qclass_in 1 // [RFC1035] Internet (IN)
250 #define dns_qclass_chaos 3 // [D. Moon, "Chaosnet"] Chaosnet (MIT)
251 #define dns_qclass_hesiod 4 // [MIT Project Athena Technical Plan] Hesiod service
252 #define dns_qclass_none 254 // [RFC2136] NONE (delete, or not in use)
253 #define dns_qclass_any 255 // [RFC1035] ANY (wildcard)
254
255 #define dns_rrtype_a 1 // [RFC1035] a host address
256 #define dns_rrtype_ns 2 // [RFC1035] an authoritative name server
257 #define dns_rrtype_md 3 // [RFC1035] a mail destination (OBSOLETE - use MX)
258 #define dns_rrtype_mf 4 // [RFC1035] a mail forwarder (OBSOLETE - use MX)
259 #define dns_rrtype_cname 5 // [RFC1035] the canonical name for an alias
260 #define dns_rrtype_soa 6 // [RFC1035] marks the start of a zone of authority
261 #define dns_rrtype_mb 7 // [RFC1035] a mailbox domain name (EXPERIMENTAL)
262 #define dns_rrtype_mg 8 // [RFC1035] a mail group member (EXPERIMENTAL)
263 #define dns_rrtype_mr 9 // [RFC1035] a mail rename domain name (EXPERIMENTAL)
264 #define dns_rrtype_null 10 // [RFC1035] a null RR (EXPERIMENTAL)
265 #define dns_rrtype_wks 11 // [RFC1035] a well known service description
266 #define dns_rrtype_ptr 12 // [RFC1035] a domain name pointer
267 #define dns_rrtype_hinfo 13 // [RFC1035] host information
268 #define dns_rrtype_minfo 14 // [RFC1035] mailbox or mail list information
269 #define dns_rrtype_mx 15 // [RFC1035] mail exchange
270 #define dns_rrtype_txt 16 // [RFC1035] text strings
271 #define dns_rrtype_rp 17 // [RFC1183] for Responsible Person
272 #define dns_rrtype_afsdb 18 // [RFC1183,RFC5864] for AFS Data Base location
273 #define dns_rrtype_x25 19 // [RFC1183] for X.25 PSDN address
274 #define dns_rrtype_isdn 20 // [RFC1183] for ISDN address
275 #define dns_rrtype_rt 21 // [RFC1183] for Route Through
276 #define dns_rrtype_nsap 22 // [RFC1706] for NSAP address, NSAP style A record
277 #define dns_rrtype_nsap_ptr 23 // [RFC1348,RFC1637,RFC1706] for domain name pointer, NSAP style
278 #define dns_rrtype_sig 24 // [RFC4034,RFC3755,RFC2535,RFC2536,RFC2537,RFC2931,RFC3110,RFC3008]
279 #define dns_rrtype_key 25 // [RFC4034,RFC3755,RFC2535,RFC2536,RFC2537,RFC2539,RFC3008,RFC3110]
280 #define dns_rrtype_px 26 // [RFC2163] X.400 mail mapping information
281 #define dns_rrtype_gpos 27 // [RFC1712] Geographical Position
282 #define dns_rrtype_aaaa 28 // [RFC3596] IP6 Address
283 #define dns_rrtype_loc 29 // [RFC1876] Location Information
284 #define dns_rrtype_nxt 30 // [RFC3755] [RFC2535] Next Domain (OBSOLETE)
285 #define dns_rrtype_eid 31 // [http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt] Endpoint Identifier
286 #define dns_rrtype_nimloc 32 // [http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt] Nimrod Locator
287 #define dns_rrtype_srv 33 // [RFC2782] Server Selection
288 #define dns_rrtype_atma 34 // ["ATM Name System, V2.0"] ATM Address
289 #define dns_rrtype_naptr 35 // [RFC2915] [RFC2168] [RFC3403] Naming Authority Pointer
290 #define dns_rrtype_kx 36 // [RFC2230] Key Exchanger
291 #define dns_rrtype_cert 37 // [RFC4398] CERT
292 #define dns_rrtype_a6 38 // [RFC3226] [RFC2874] [RFC6563] A6 (OBSOLETE - use AAAA)
293 #define dns_rrtype_dname 39 // [RFC6672]
294 #define dns_rrtype_sink 40 // [http://tools.ietf.org/html/draft-eastlake-kitchen-sink]
295 #define dns_rrtype_opt 41 // [RFC6891] [RFC3225]
296 #define dns_rrtype_apl 42 // [RFC3123]
297 #define dns_rrtype_ds 43 // [RFC4034] [RFC3658] Delegation Signer
298 #define dns_rrtype_sshfp 44 // [RFC4255] SSH Key Fingerprint
299 #define dns_rrtype_ipseckey 45 // [RFC4025]
300 #define dns_rrtype_rrsig 46 // [RFC4034] [RFC3755]
301 #define dns_rrtype_nsec 47 // [RFC4034] [RFC3755]
302 #define dns_rrtype_dnskey 48 // [RFC4034] [RFC3755]
303 #define dns_rrtype_dhcid 49 // [RFC4701] DHCID
304 #define dns_rrtype_nsec3 50 // [RFC5155] NSEC3
305 #define dns_rrtype_nsec3param 51 // [RFC5155] NSEC3PARAM
306 #define dns_rrtype_tlsa 52 // [RFC6698] TLSA
307 #define dns_rrtype_smimea 53 // [RFC8162] S/MIME cert association
308 #define dns_rrtype_hip 55 // Host Identity Protocol
309 #define dns_rrtype_ninfo 56 // [Jim_Reid] NINFO/ninfo-completed-template
310 #define dns_rrtype_rkey 57 // [Jim_Reid] RKEY/rkey-completed-template
311 #define dns_rrtype_talink 58 // [Wouter_Wijngaards] Trust Anchor LINK
312 #define dns_rrtype_cds 59 // [RFC7344] Child DS
313 #define dns_rrtype_cdnskey 60 // [RFC7344] DNSKEY(s) the Child wants reflected in DS
314 #define dns_rrtype_openpgpkey 61 // [RFC7929] OpenPGP Key
315 #define dns_rrtype_csync 62 // [RFC7477] Child-To-Parent Synchronization
316 #define dns_rrtype_spf 99 // [RFC7208]
317 #define dns_rrtype_uinfo 100 // [IANA-Reserved]
318 #define dns_rrtype_uid 101 // [IANA-Reserved]
319 #define dns_rrtype_gid 102 // [IANA-Reserved]
320 #define dns_rrtype_unspec 103 // [IANA-Reserved]
321 #define dns_rrtype_nid 104 // [RFC6742]
322 #define dns_rrtype_l32 105 // [RFC6742]
323 #define dns_rrtype_l64 106 // [RFC6742]
324 #define dns_rrtype_lp 107 // [RFC6742]
325 #define dns_rrtype_eui48 108 // an EUI-48 address [RFC7043]
326 #define dns_rrtype_eui64 109 // an EUI-64 address [RFC7043]
327 #define dns_rrtype_tkey 249 // Transaction Key [RFC2930]
328 #define dns_rrtype_tsig 250 // Transaction Signature [RFC2845]
329 #define dns_rrtype_ixfr 251 // incremental transfer [RFC1995]
330 #define dns_rrtype_axfr 252 // transfer of an entire zone [RFC1035][RFC5936]
331 #define dns_rrtype_mailb 253 // mailbox-related RRs (MB, MG or MR) [RFC1035]
332 #define dns_rrtype_maila 254 // mail agent RRs (OBSOLETE - see MX) [RFC1035]
333 #define dns_rrtype_any 255 // A request for some or all records the server has available
334 #define dns_rrtype_uri 256 // URI [RFC7553] URI/uri-completed-template
335 #define dns_rrtype_caa 257 // Certification Authority Restriction [RFC6844]
336 #define dns_rrtype_avc 258 // Application Visibility and Control [Wolfgang_Riedel]
337 #define dns_rrtype_doa 259 // Digital Object Architecture [draft-durand-doa-over-dns]
338
339 #define dns_opt_llq 1 // On-hold [http://files.dns-sd.org/draft-sekar-dns-llq.txt]
340 #define dns_opt_update_lease 2 // On-hold [http://files.dns-sd.org/draft-sekar-dns-ul.txt]
341 #define dns_opt_nsid 3 // [RFC5001]
342 #define dns_opt_owner 4 // [draft-cheshire-edns0-owner-option]
343 #define dns_opt_dau 5 // [RFC6975]
344 #define dns_opt_dhu 6 // [RFC6975]
345 #define dns_opt_n3u 7 // [RFC6975]
346 #define dns_opt_client_subnet 8 // [RFC7871]
347 #define dns_opt_expire 9 // [RFC7314]
348 #define dns_opt_cookie 10 // [RFC7873]
349 #define dns_opt_keepalive 11 // [RFC7828]
350 #define dns_opt_padding 12 // [RFC7830]
351 #define dns_opt_chain 13 // [RFC7901]
352 #define dns_opt_key_tag 14 // [RFC8145]
353
354 // towire.c:
355
356 uint16_t srp_random16(void);
357 void dns_name_to_wire(dns_name_pointer_t *NULLABLE r_pointer,
358 dns_towire_state_t *NONNULL txn,
359 const char *NONNULL name);
360 void dns_full_name_to_wire(dns_name_pointer_t *NULLABLE r_pointer,
361 dns_towire_state_t *NONNULL txn,
362 const char *NONNULL name);
363 void dns_pointer_to_wire(dns_name_pointer_t *NULLABLE r_pointer,
364 dns_towire_state_t *NONNULL txn,
365 dns_name_pointer_t *NONNULL pointer);
366 void dns_u8_to_wire(dns_towire_state_t *NONNULL txn,
367 uint8_t val);
368 void dns_u16_to_wire(dns_towire_state_t *NONNULL txn,
369 uint16_t val);
370 void dns_u32_to_wire(dns_towire_state_t *NONNULL txn,
371 uint32_t val);
372 void dns_ttl_to_wire(dns_towire_state_t *NONNULL txn,
373 int32_t val);
374 void dns_rdlength_begin(dns_towire_state_t *NONNULL txn);
375 void dns_rdlength_end(dns_towire_state_t *NONNULL txn);
376 void dns_rdata_a_to_wire(dns_towire_state_t *NONNULL txn,
377 const char *NONNULL ip_address);
378 void dns_rdata_aaaa_to_wire(dns_towire_state_t *NONNULL txn,
379 const char *NONNULL ip_address);
380 uint16_t dns_rdata_key_to_wire(dns_towire_state_t *NONNULL txn,
381 unsigned key_type,
382 unsigned name_type,
383 unsigned signatory,
384 srp_key_t *NONNULL key);
385 void dns_rdata_txt_to_wire(dns_towire_state_t *NONNULL txn,
386 const char *NONNULL txt_record);
387 void dns_rdata_raw_data_to_wire(dns_towire_state_t *NONNULL txn, const void *NONNULL raw_data, size_t length);
388 void dns_edns0_header_to_wire(dns_towire_state_t *NONNULL txn,
389 int mtu,
390 int xrcode,
391 int version,
392 int DO);
393 void dns_edns0_option_begin(dns_towire_state_t *NONNULL txn);
394 void dns_edns0_option_end(dns_towire_state_t *NONNULL txn);
395 void dns_sig0_signature_to_wire(dns_towire_state_t *NONNULL txn,
396 srp_key_t *NONNULL key, uint16_t key_tag,
397 dns_name_pointer_t *NONNULL signer,
398 const char *NONNULL signer_fqdn);
399 int dns_send_to_server(dns_transaction_t *NONNULL txn,
400 const char *NONNULL anycast_address, uint16_t port,
401 dns_response_callback_t NONNULL callback);
402
403 // fromwire.c:
404 dns_label_t *NULLABLE dns_label_parse(const uint8_t *NONNULL buf, unsigned mlen, unsigned *NONNULL offp);
405 bool dns_opt_parse(dns_edns0_t *NONNULL *NULLABLE ret, dns_rr_t *NONNULL rrset);
406 bool dns_name_parse(dns_label_t *NONNULL *NULLABLE ret, const uint8_t *NONNULL buf, unsigned len,
407 unsigned *NONNULL offp, unsigned base);
408 bool dns_u8_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint8_t *NONNULL ret);
409 bool dns_u16_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint16_t *NONNULL ret);
410 bool dns_u32_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint32_t *NONNULL ret);
411 bool dns_rdata_parse_data(dns_rr_t *NONNULL rr, const uint8_t *NONNULL buf, unsigned *NONNULL offp,
412 unsigned target, unsigned rdlen, unsigned rrstart);
413 bool dns_rr_parse(dns_rr_t *NONNULL rrset,
414 const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, bool rrdata_permitted);
415 void dns_name_free(dns_label_t *NONNULL name);
416 void dns_rrdata_free(dns_rr_t *NONNULL rr);
417 void dns_message_free(dns_message_t *NONNULL message);
418 bool dns_rdata_parse_data(dns_rr_t *NONNULL rr, const uint8_t *NONNULL buf, unsigned *NONNULL offp,
419 unsigned target, unsigned rdlen, unsigned rrstart);
420 bool dns_wire_parse(dns_message_t *NONNULL *NULLABLE ret, dns_wire_t *NONNULL message, unsigned len);
421 bool dns_names_equal(dns_label_t *NONNULL name1, dns_label_t *NONNULL name2);
422 const char *NONNULL dns_name_print(dns_name_t *NONNULL name, char *NONNULL buf, int bufmax);
423 bool dns_names_equal_text(dns_label_t *NONNULL name1, const char *NONNULL name2);
424 size_t dns_name_wire_length(dns_label_t *NONNULL name);
425 size_t dns_name_to_wire_canonical(uint8_t *NONNULL buf, size_t max, dns_label_t *NONNULL name);
426 #endif // _DNS_MSG_H
427
428 // Local Variables:
429 // mode: C
430 // tab-width: 4
431 // c-file-style: "bsd"
432 // c-basic-offset: 4
433 // fill-column: 108
434 // indent-tabs-mode: nil
435 // End: