Home | History | Annotate | Line # | Download | only in ServiceRegistration
      1 /* dns-msg.h
      2  *
      3  * Copyright (c) 2018-2023 Apple 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  *     https://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 #include "srp.h"
     25 
     26 #ifndef DNS_MAX_UDP_PAYLOAD
     27 #define DNS_MAX_UDP_PAYLOAD 1410
     28 #endif
     29 
     30 #define DNS_HEADER_SIZE            12
     31 #define DNS_DATA_SIZE              (DNS_MAX_UDP_PAYLOAD - DNS_HEADER_SIZE)
     32 #define DNS_MAX_POINTER            ((2 << 14) - 1)
     33 #define DNS_MAX_LABEL_SIZE         63
     34 #define DNS_MAX_LABEL_SIZE_ESCAPED 252
     35 #define DNS_MAX_NAME_SIZE          255
     36 #define DNS_MAX_NAME_SIZE_ESCAPED  1009
     37 #define DNS_MAX_LABELS             128
     38 
     39 typedef struct message message_t;
     40 
     41 typedef struct dns_wire dns_wire_t;
     42 struct dns_wire {
     43     uint16_t id;
     44     uint16_t bitfield;
     45     uint16_t qdcount;
     46     uint16_t ancount;
     47     uint16_t nscount;
     48     uint16_t arcount;
     49     uint8_t data[DNS_DATA_SIZE];
     50 };
     51 
     52 typedef struct dns_name_pointer dns_name_pointer_t;
     53 struct dns_name_pointer {
     54     dns_name_pointer_t *NULLABLE next;
     55     uint8_t *NONNULL message_start;
     56     uint8_t *NONNULL name_start;
     57     int num_labels;
     58     int length;
     59 };
     60 
     61 typedef struct dns_towire_state dns_towire_state_t;
     62 struct dns_towire_state {
     63     dns_wire_t *NULLABLE message;
     64     uint8_t *NONNULL p;
     65     uint8_t *NONNULL lim;
     66     uint8_t *NULLABLE p_rdlength;
     67     uint8_t *NULLABLE p_opt;
     68     int line, outer_line;
     69     bool truncated : 1;
     70     unsigned int error : 31;
     71 };
     72 
     73 typedef struct dns_transaction dns_transaction_t;
     74 struct dns_transaction {
     75     dns_transaction_t *NULLABLE next;
     76     dns_towire_state_t towire;
     77     dns_wire_t *NULLABLE response;
     78     int response_length;
     79     int sock;
     80 };
     81 
     82 typedef void (*dns_response_callback_t)(dns_transaction_t *NONNULL txn);
     83 
     84 typedef struct dns_label dns_label_t;
     85 typedef dns_label_t dns_name_t;
     86 struct dns_label {
     87     dns_label_t *NULLABLE next;
     88     uint8_t len;
     89     char data[DNS_MAX_LABEL_SIZE];
     90 };
     91 
     92 typedef struct dns_rdata_txt dns_rdata_txt_t;
     93 struct dns_rdata_txt {
     94     uint8_t len;
     95     uint8_t *NONNULL data;
     96 };
     97 
     98 typedef struct dns_rdata_unparsed dns_rdata_unparsed_t;
     99 struct dns_rdata_unparsed {
    100     uint8_t *NULLABLE data;
    101     uint16_t len;
    102 };
    103 
    104 typedef struct dns_rdata_single_name dns_rdata_ptr_t;
    105 typedef struct dns_rdata_single_name dns_rdata_ns_t;
    106 typedef struct dns_rdata_single_name dns_rdata_cname_t;
    107 struct dns_rdata_single_name {
    108     dns_label_t *NONNULL name;
    109 };
    110 
    111 typedef struct dns_rdata_srv dns_rdata_srv_t;
    112 struct dns_rdata_srv {
    113     dns_label_t *NONNULL name;
    114     uint16_t priority;
    115     uint16_t weight;
    116     uint16_t port;
    117 };
    118 
    119 typedef struct dns_rdata_sig dns_rdata_sig_t;
    120 struct dns_rdata_sig {
    121     uint16_t type;
    122     uint8_t algorithm;
    123     uint8_t label;
    124     uint32_t rrttl;
    125     uint32_t expiry;
    126     uint32_t inception;
    127     uint16_t key_tag;
    128     dns_label_t *NONNULL signer;
    129     unsigned start;
    130     unsigned len;
    131     uint8_t *NONNULL signature;
    132 };
    133 
    134 typedef struct dns_rdata_key dns_rdata_key_t;
    135 struct dns_rdata_key {
    136     uint16_t flags;
    137     uint8_t protocol;
    138     uint8_t algorithm;
    139     unsigned len;
    140     uint8_t *NONNULL key;
    141 };
    142 
    143 typedef struct dns_rdata_soa dns_rdata_soa_t;
    144 struct dns_rdata_soa {
    145     dns_label_t *NONNULL mname;
    146     dns_label_t *NONNULL rname;
    147     uint32_t serial;
    148     uint32_t refresh;
    149     uint32_t retry;
    150     uint32_t expire;
    151     uint32_t minimum;
    152 };
    153 
    154 typedef struct dns_rr dns_rr_t;
    155 struct dns_rr {
    156     dns_label_t *NONNULL name;
    157     uint16_t type;
    158     uint16_t qclass;
    159     uint32_t ttl;
    160     union {
    161         dns_rdata_unparsed_t unparsed;
    162         dns_rdata_ptr_t ptr;
    163         dns_rdata_cname_t cname;
    164         dns_rdata_ns_t ns;
    165         struct in_addr a;
    166         struct in6_addr aaaa;
    167         dns_rdata_srv_t srv;
    168         dns_rdata_txt_t txt;
    169         dns_rdata_sig_t sig;
    170         dns_rdata_key_t key;
    171         dns_rdata_soa_t soa;
    172     } data;
    173 };
    174 
    175 typedef struct dns_edns0 dns_edns0_t;
    176 struct dns_edns0 {
    177     dns_edns0_t *NULLABLE next;
    178     uint16_t length;
    179     uint16_t type;
    180     uint8_t data[0];
    181 };
    182 
    183 typedef struct dns_message dns_message_t;
    184 struct dns_message {
    185     int ref_count;
    186     unsigned qdcount, ancount, nscount, arcount;
    187     dns_rr_t *NULLABLE questions;
    188     dns_rr_t *NULLABLE answers;
    189     dns_rr_t *NULLABLE authority;
    190     dns_rr_t *NULLABLE additional;
    191     dns_edns0_t *NULLABLE edns0;
    192 };
    193 
    194 // Masks for bitfield data
    195 #define dns_qr_mask     0x8000
    196 #define dns_opcode_mask 0x7800
    197 #define dns_flags_mask  0x07f0
    198 #define dns_rcode_mask  0x000f
    199 
    200 // Shifts for bitfield data
    201 #define dns_qr_shift     15
    202 #define dns_opcode_shift 11
    203 #define dns_rcode_shift  0
    204 
    205 // Booleans
    206 #define dns_flags_aa 0x0400
    207 #define dns_flags_tc 0x0200
    208 #define dns_flags_rd 0x0100
    209 #define dns_flags_ra 0x0080
    210 #define dns_flags_ad 0x0020
    211 #define dns_flags_cd 0x0010
    212 
    213 // Getters
    214 #define dns_qr_get(w)     ((ntohs((w)->bitfield) & dns_qr_mask) >> dns_qr_shift)
    215 #define dns_opcode_get(w) ((ntohs((w)->bitfield) & dns_opcode_mask) >> dns_opcode_shift)
    216 #define dns_rcode_get(w)  ((ntohs((w)->bitfield) & dns_rcode_mask) >> dns_rcode_shift)
    217 
    218 // Setters
    219 #define dns_qr_set(w, value) \
    220     ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_qr_mask) | ((value) << dns_qr_shift))))
    221 #define dns_opcode_set(w, value) \
    222     ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_opcode_mask) | ((value) << dns_opcode_shift))))
    223 #define dns_rcode_set(w, value) \
    224     ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_rcode_mask) | ((value) << dns_rcode_shift))))
    225 
    226 // Query/Response
    227 #define dns_qr_query           0
    228 #define dns_qr_response        1
    229 
    230 // Opcodes
    231 #define dns_opcode_query       0
    232 #define dns_opcode_iquery      1
    233 #define dns_opcode_status      2
    234 #define dns_opcode_notify      4
    235 #define dns_opcode_update      5
    236 #define dns_opcode_dso         6
    237 
    238 // Response Codes
    239 #define dns_rcode_noerror      0 // [RFC1035] No Error
    240 #define dns_rcode_formerr      1 // [RFC1035] Format Error
    241 #define dns_rcode_servfail     2 // [RFC1035] Server Failure
    242 #define dns_rcode_nxdomain     3 // [RFC1035] Non-Existent Domain
    243 #define dns_rcode_notimp       4 // [RFC1035] Not Implemented
    244 #define dns_rcode_refused      5 // [RFC1035] Query Refused
    245 #define dns_rcode_yxdomain     6 // [RFC2136][RFC6672] Name Exists when it should not
    246 #define dns_rcode_yxrrset      7 // [RFC2136] RR Set Exists when it should not
    247 #define dns_rcode_nxrrset      8 // [RFC2136] RR Set that should exist does not
    248 #define dns_rcode_notauth      9 // [RFC2136] Server Not Authoritative for zone, or [RFC2845] Not Authorized
    249 #define dns_rcode_notzone     10 // [RFC2136] Name not contained in zone
    250 #define dns_rcode_dsotypeni   11 // [RFC8490] DSO-Type Not Implemented
    251 #define dns_rcode_badvers     16 // [RFC6891] Bad OPT Version, or [RFC2845] TSIG Signature Failure
    252 #define dns_rcode_badkey      17 // [RFC2845] Key not recognized
    253 #define dns_rcode_badtime     18 // [RFC2845] Signature out of time window
    254 #define dns_rcode_badmode     19 // [RFC2930] Bad TKEY Mode
    255 #define dns_rcode_badname     20 // [RFC2930] Duplicate key name
    256 #define dns_rcode_badalg      21 // [RFC2930] Algorithm not supported
    257 #define dns_rcode_badtrunc    22 // [RFC4635] Bad Truncation
    258 #define dns_rcode_badcookie   23 // [RFC7873] Bad/missing Server Cookie
    259 
    260 #define dns_qclass_in          1 // [RFC1035] Internet (IN)
    261 #define dns_qclass_chaos       3 // [D. Moon, "Chaosnet"] Chaosnet (MIT)
    262 #define dns_qclass_hesiod      4 // [MIT Project Athena Technical Plan] Hesiod service
    263 #define dns_qclass_none      254 // [RFC2136] NONE (delete, or not in use)
    264 #define dns_qclass_any       255 // [RFC1035] ANY (wildcard)
    265 
    266 #define dns_invalid_rr         0 // If it's zero, rr is invalid.
    267 
    268 #define dns_rrtype_a           1 // [RFC1035] a host address
    269 #define dns_rrtype_ns          2 // [RFC1035] an authoritative name server
    270 #define dns_rrtype_md          3 // [RFC1035] a mail destination (OBSOLETE - use MX)
    271 #define dns_rrtype_mf          4 // [RFC1035] a mail forwarder (OBSOLETE - use MX)
    272 #define dns_rrtype_cname       5 // [RFC1035] the canonical name for an alias
    273 #define dns_rrtype_soa         6 // [RFC1035] marks the start of a zone of authority
    274 #define dns_rrtype_mb          7 // [RFC1035] a mailbox domain name (EXPERIMENTAL)
    275 #define dns_rrtype_mg          8 // [RFC1035] a mail group member (EXPERIMENTAL)
    276 #define dns_rrtype_mr          9 // [RFC1035] a mail rename domain name (EXPERIMENTAL)
    277 #define dns_rrtype_null       10 // [RFC1035]    a null RR (EXPERIMENTAL)
    278 #define dns_rrtype_wks        11 // [RFC1035]    a well known service description
    279 #define dns_rrtype_ptr        12 // [RFC1035]    a domain name pointer
    280 #define dns_rrtype_hinfo      13 // [RFC1035]    host information
    281 #define dns_rrtype_minfo      14 // [RFC1035]    mailbox or mail list information
    282 #define dns_rrtype_mx         15 // [RFC1035]    mail exchange
    283 #define dns_rrtype_txt        16 // [RFC1035] text strings
    284 #define dns_rrtype_rp         17 // [RFC1183] for Responsible Person
    285 #define dns_rrtype_afsdb      18 // [RFC1183,RFC5864] for AFS Data Base location
    286 #define dns_rrtype_x25        19 // [RFC1183] for X.25 PSDN address
    287 #define dns_rrtype_isdn       20 // [RFC1183] for ISDN address
    288 #define dns_rrtype_rt         21 // [RFC1183] for Route Through
    289 #define dns_rrtype_nsap       22 // [RFC1706] for NSAP address, NSAP style A record
    290 #define dns_rrtype_nsap_ptr   23 // [RFC1348,RFC1637,RFC1706] for domain name pointer, NSAP style
    291 #define dns_rrtype_sig        24 // [RFC4034,RFC3755,RFC2535,RFC2536,RFC2537,RFC2931,RFC3110,RFC3008]
    292 #define dns_rrtype_key        25 // [RFC4034,RFC3755,RFC2535,RFC2536,RFC2537,RFC2539,RFC3008,RFC3110]
    293 #define dns_rrtype_px         26 // [RFC2163] X.400 mail mapping information
    294 #define dns_rrtype_gpos       27 // [RFC1712] Geographical Position
    295 #define dns_rrtype_aaaa       28 // [RFC3596] IP6 Address
    296 #define dns_rrtype_loc        29 // [RFC1876] Location Information
    297 #define dns_rrtype_nxt        30 // [RFC3755] [RFC2535] Next Domain (OBSOLETE)
    298 #define dns_rrtype_eid        31 // [http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt] Endpoint Identifier
    299 #define dns_rrtype_nimloc     32 // [http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt] Nimrod Locator
    300 #define dns_rrtype_srv        33 // [RFC2782] Server Selection
    301 #define dns_rrtype_atma       34 // ["ATM Name System, V2.0"] ATM Address
    302 #define dns_rrtype_naptr      35 // [RFC2915] [RFC2168] [RFC3403] Naming Authority Pointer
    303 #define dns_rrtype_kx         36 // [RFC2230] Key Exchanger
    304 #define dns_rrtype_cert       37 // [RFC4398] CERT
    305 #define dns_rrtype_a6         38 // [RFC3226] [RFC2874] [RFC6563]    A6 (OBSOLETE - use AAAA)
    306 #define dns_rrtype_dname      39 // [RFC6672]
    307 #define dns_rrtype_sink       40 // [http://tools.ietf.org/html/draft-eastlake-kitchen-sink]
    308 #define dns_rrtype_opt        41 // [RFC6891] [RFC3225]
    309 #define dns_rrtype_apl        42 // [RFC3123]
    310 #define dns_rrtype_ds         43 // [RFC4034] [RFC3658] Delegation Signer
    311 #define dns_rrtype_sshfp      44 // [RFC4255] SSH Key Fingerprint
    312 #define dns_rrtype_ipseckey   45 // [RFC4025]
    313 #define dns_rrtype_rrsig      46 // [RFC4034] [RFC3755]
    314 #define dns_rrtype_nsec       47 // [RFC4034] [RFC3755]
    315 #define dns_rrtype_dnskey     48 // [RFC4034] [RFC3755]
    316 #define dns_rrtype_dhcid      49 // [RFC4701] DHCID
    317 #define dns_rrtype_nsec3      50 // [RFC5155] NSEC3
    318 #define dns_rrtype_nsec3param 51 // [RFC5155] NSEC3PARAM
    319 #define dns_rrtype_tlsa       52 // [RFC6698] TLSA
    320 #define dns_rrtype_smimea     53 // [RFC8162] S/MIME cert association
    321 #define dns_rrtype_hip        55 // Host Identity Protocol
    322 #define dns_rrtype_ninfo      56 // [Jim_Reid] NINFO/ninfo-completed-template
    323 #define dns_rrtype_rkey       57 // [Jim_Reid] RKEY/rkey-completed-template
    324 #define dns_rrtype_talink     58 // [Wouter_Wijngaards] Trust Anchor LINK
    325 #define dns_rrtype_cds        59 // [RFC7344] Child DS
    326 #define dns_rrtype_cdnskey    60 // [RFC7344]   DNSKEY(s) the Child wants reflected in DS
    327 #define dns_rrtype_openpgpkey 61 // [RFC7929]   OpenPGP Key
    328 #define dns_rrtype_csync      62 // [RFC7477] Child-To-Parent Synchronization
    329 #define dns_rrtype_zonemd     63 // [RFC8976]
    330 #define dns_rrtype_svcb       64 // [RFC9460]
    331 #define dns_rrtype_https      65 // [RFC9460]
    332 #define dns_rrtype_spf        99 // [RFC7208]
    333 #define dns_rrtype_uinfo     100 // [IANA-Reserved]
    334 #define dns_rrtype_uid       101 // [IANA-Reserved]
    335 #define dns_rrtype_gid       102 // [IANA-Reserved]
    336 #define dns_rrtype_unspec    103 // [IANA-Reserved]
    337 #define dns_rrtype_nid       104 // [RFC6742]
    338 #define dns_rrtype_l32       105 // [RFC6742]
    339 #define dns_rrtype_l64       106 // [RFC6742]
    340 #define dns_rrtype_lp        107 // [RFC6742]
    341 #define dns_rrtype_eui48     108 // an EUI-48 address [RFC7043]
    342 #define dns_rrtype_eui64     109 // an EUI-64 address [RFC7043]
    343 #define dns_rrtype_tkey      249 // Transaction Key [RFC2930]
    344 #define dns_rrtype_tsig      250 // Transaction Signature [RFC2845]
    345 #define dns_rrtype_ixfr      251 // incremental transfer    [RFC1995]
    346 #define dns_rrtype_axfr      252 // transfer of an entire zone [RFC1035][RFC5936]
    347 #define dns_rrtype_mailb     253 // mailbox-related RRs (MB, MG or MR) [RFC1035]
    348 #define dns_rrtype_maila     254 // mail agent RRs (OBSOLETE - see MX) [RFC1035]
    349 #define dns_rrtype_any       255 // A request for some or all records the server has available
    350 #define dns_rrtype_uri       256 // URI [RFC7553]   URI/uri-completed-template
    351 #define dns_rrtype_caa       257 // Certification Authority Restriction [RFC6844]
    352 #define dns_rrtype_avc       258 // Application Visibility and Control [Wolfgang_Riedel]
    353 #define dns_rrtype_doa       259 // Digital Object Architecture [draft-durand-doa-over-dns]
    354 #define dns_rrtype_amtrelay  260 // [RFC8777]
    355 #define dns_rrtype_ta      32768 // Trust authorities [Sam Weiler]
    356 #define dns_rrtype_dlv     32769 // [RFC8749]
    357 
    358 #define dns_opt_llq            1 // On-hold [http://files.dns-sd.org/draft-sekar-dns-llq.txt]
    359 #define dns_opt_update_lease   2 // On-hold [http://files.dns-sd.org/draft-sekar-dns-ul.txt]
    360 #define dns_opt_nsid           3 // [RFC5001]
    361 #define dns_opt_owner          4 // [draft-cheshire-edns0-owner-option]
    362 #define dns_opt_dau            5 // [RFC6975]
    363 #define dns_opt_dhu            6 // [RFC6975]
    364 #define dns_opt_n3u            7 // [RFC6975]
    365 #define dns_opt_client_subnet  8 // [RFC7871]
    366 #define dns_opt_expire         9 // [RFC7314]
    367 #define dns_opt_cookie        10 // [RFC7873]
    368 #define dns_opt_keepalive     11 // [RFC7828]
    369 #define dns_opt_padding       12 // [RFC7830]
    370 #define dns_opt_chain         13 // [RFC7901]
    371 #define dns_opt_key_tag       14 // [RFC8145]
    372 #define dns_opt_srp_serial 65186 // ???
    373 
    374 // towire.c:
    375 
    376 uint16_t srp_random16(void);
    377 void dns_name_to_wire_(dns_name_pointer_t *NULLABLE r_pointer,
    378                        dns_towire_state_t *NONNULL txn,
    379                        const char *NONNULL name, int line);
    380 #define dns_name_to_wire(r_pointer, txn, name) dns_name_to_wire_(r_pointer, txn, name, __LINE__)
    381 
    382 void dns_full_name_to_wire_(dns_name_pointer_t *NULLABLE r_pointer,
    383                             dns_towire_state_t *NONNULL txn,
    384                             const char *NONNULL name, int line);
    385 #define dns_full_name_to_wire(r_pointer, txn, name) dns_full_name_to_wire_(r_pointer, txn, name, __LINE__)
    386 
    387 void dns_pointer_to_wire_(dns_name_pointer_t *NULLABLE r_pointer,
    388                           dns_towire_state_t *NONNULL txn,
    389                           dns_name_pointer_t *NONNULL pointer, int line);
    390 #define dns_pointer_to_wire(r_pointer, txn, pointer) dns_pointer_to_wire_(r_pointer, txn, pointer, __LINE__)
    391 
    392 void dns_u8_to_wire_(dns_towire_state_t *NONNULL txn, uint8_t val, int line);
    393 #define dns_u8_to_wire(txn, val) dns_u8_to_wire_(txn, val, __LINE__)
    394 
    395 void dns_u16_to_wire_(dns_towire_state_t *NONNULL txn, uint16_t val, int line);
    396 #define dns_u16_to_wire(txn, val) dns_u16_to_wire_(txn, val, __LINE__)
    397 
    398 void dns_u32_to_wire_(dns_towire_state_t *NONNULL txn, uint32_t val, int line);
    399 #define dns_u32_to_wire(txn, val) dns_u32_to_wire_(txn, val, __LINE__)
    400 
    401 void dns_u64_to_wire_(dns_towire_state_t *NONNULL txn, uint64_t val, int line);
    402 #define dns_u64_to_wire(txn, val) dns_u64_to_wire_(txn, val, __LINE__)
    403 
    404 void dns_ttl_to_wire_(dns_towire_state_t *NONNULL txn, int32_t val, int line);
    405 #define dns_ttl_to_wire(txn, val) dns_ttl_to_wire_(txn, val, __LINE__)
    406 
    407 void dns_rdlength_begin_(dns_towire_state_t *NONNULL txn, int line);
    408 #define dns_rdlength_begin(txn) dns_rdlength_begin_(txn, __LINE__)
    409 
    410 void dns_rdlength_end_(dns_towire_state_t *NONNULL txn, int line);
    411 #define dns_rdlength_end(txn) dns_rdlength_end_(txn, __LINE__)
    412 
    413 void dns_rdata_a_to_wire_(dns_towire_state_t *NONNULL txn, const char *NONNULL ip_address, int line);
    414 #define dns_rdata_a_to_wire(txn, ip_address) dns_rdata_a_to_wire_(txn, ip_address, __LINE__)
    415 
    416 void dns_rdata_aaaa_to_wire_(dns_towire_state_t *NONNULL txn, const char *NONNULL ip_address, int line);
    417 #define dns_rdata_aaaa_to_wire(txn, ip_address) dns_rdata_aaaa_to_wire_(txn, ip_address, __LINE__)
    418 
    419 uint16_t dns_rdata_key_to_wire_(dns_towire_state_t *NONNULL txn,
    420                                 unsigned key_type,
    421                                 unsigned name_type,
    422                                 uint8_t signatory,
    423                                 srp_key_t *NONNULL key, int line);
    424 #define dns_rdata_key_to_wire(txn, key_type, name_type, signatory, key) \
    425     dns_rdata_key_to_wire_(txn, key_type, name_type, signatory, key, __LINE__)
    426 
    427 void dns_rdata_txt_to_wire_(dns_towire_state_t *NONNULL txn, const char *NONNULL txt_record, int line);
    428 #define dns_rdata_txt_to_wire(txn, txt_record) dns_rdata_txt_to_wire_(txn, txt_record, __LINE__)
    429 
    430 void dns_rdata_raw_data_to_wire_(dns_towire_state_t *NONNULL txn,
    431                                  const void *NONNULL raw_data, size_t length, int line);
    432 #define dns_rdata_raw_data_to_wire(txn, raw_data, length) dns_rdata_raw_data_to_wire_(txn, raw_data, length, __LINE__)
    433 
    434 void dns_edns0_header_to_wire_(dns_towire_state_t *NONNULL txn,
    435                                uint16_t mtu, uint8_t xrcode, uint8_t version, bool DO, int line);
    436 #define dns_edns0_header_to_wire(txn, mtu, xrcode, version, DO) \
    437     dns_edns0_header_to_wire_(txn, mtu, xrcode, version, DO, __LINE__)
    438 
    439 void dns_edns0_option_begin_(dns_towire_state_t *NONNULL txn, int line);
    440 #define dns_edns0_option_begin(txn) dns_edns0_option_begin_(txn, __LINE__)
    441 
    442 void dns_edns0_option_end_(dns_towire_state_t *NONNULL txn, int line);
    443 #define dns_edns0_option_end(txn) dns_edns0_option_end_(txn, __LINE__)
    444 
    445 void dns_sig0_signature_to_wire_(dns_towire_state_t *NONNULL txn,
    446                                  srp_key_t *NONNULL key, uint16_t key_tag,
    447                                  dns_name_pointer_t *NONNULL signer, const char *NONNULL signer_hostname,
    448                                  const char *NONNULL signer_domain, uint32_t timenow, int line);
    449 #define dns_sig0_signature_to_wire(txn, key, key_tag, signer, signer_hostname, signer_domain, timenow) \
    450     dns_sig0_signature_to_wire_(txn, key, key_tag, signer, signer_hostname, signer_domain, timenow, __LINE__)
    451 
    452 int dns_send_to_server(dns_transaction_t *NONNULL txn,
    453                        const char *NONNULL anycast_address, uint16_t port,
    454                        dns_response_callback_t NONNULL callback);
    455 
    456 // fromwire.c:
    457 #define dns_label_parse(buf, mlen, offp) dns_label_parse_(buf, mlen, offp, __FILE__, __LINE__)
    458 dns_label_t *NULLABLE dns_label_parse_(const uint8_t *NONNULL buf, unsigned mlen, unsigned *NONNULL offp,
    459                                        const char *NONNULL file, int line);
    460 bool dns_opt_parse(dns_edns0_t *NONNULL *NULLABLE ret, dns_rr_t *NONNULL rrset);
    461 #define dns_name_parse(ret, buf, len, offp, base) dns_name_parse_(ret, buf, len, offp, base, __FILE__, __LINE__)
    462 bool dns_name_parse_(dns_label_t *NONNULL *NULLABLE ret, const uint8_t *NONNULL buf, unsigned len,
    463                      unsigned *NONNULL offp, unsigned base, const char *NONNULL file, int line);
    464 bool dns_u8_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint8_t *NONNULL ret);
    465 bool dns_u16_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint16_t *NONNULL ret);
    466 bool dns_u32_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint32_t *NONNULL ret);
    467 bool dns_u64_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint64_t *NONNULL ret);
    468 size_t dns_rdata_dump_to_buf(dns_rr_t *NONNULL rr, char *NONNULL buf, size_t bufsize);
    469 #define dns_rdata_parse_data(rr, buf, offp, target, rdlen, rrstart) \
    470     dns_rdata_parse_data_(rr, buf, offp, target, rdlen, rrstart, __FILE__, __LINE__)
    471 bool dns_rdata_parse_data_(dns_rr_t *NONNULL rr, const uint8_t *NONNULL buf, unsigned *NONNULL offp,
    472                            unsigned target, uint16_t rdlen, unsigned rrstart, const char *NONNULL file, int line);
    473 #define dns_rr_parse(rrset, buf, len, offp, rrdata_permitted, dump_to_stderr) \
    474     dns_rr_parse_(rrset, buf, len, offp, rrdata_permitted, dump_to_stderr, __FILE__, __LINE__)
    475 bool dns_rr_parse_(dns_rr_t *NONNULL rrset, const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp,
    476                    bool rrdata_permitted, bool dump_to_stderr, const char *NONNULL file, int line);
    477 void dns_name_free(dns_label_t *NONNULL name);
    478 void dns_rrdata_free(dns_rr_t *NONNULL rr);
    479 void dns_message_free(dns_message_t *NONNULL message);
    480 #define dns_wire_parse(ret, message, len, dump_to_stderr) \
    481     dns_wire_parse_(ret, message, len, dump_to_stderr, __FILE__, __LINE__)
    482 bool dns_wire_parse_(dns_message_t *NONNULL *NULLABLE ret, dns_wire_t *NONNULL message, unsigned len,
    483                      bool dump_to_stderr, const char *NONNULL FILE, int line);
    484 bool dns_names_equal(dns_label_t *NONNULL name1, dns_label_t *NONNULL name2);
    485 
    486 // wireutils.c
    487 dns_name_t *NULLABLE dns_name_copy(dns_name_t *NONNULL original);
    488 void dns_u48_to_wire_(dns_towire_state_t *NONNULL txn, uint64_t val, int line);
    489 #define dns_u48_to_wire(txn, val) dns_u48_to_wire_(txn, val, __LINE__)
    490 
    491 void dns_concatenate_name_to_wire_(dns_towire_state_t *NONNULL towire,
    492                                    dns_name_t *NULLABLE labels_prefix,
    493                                    const char *NULLABLE prefix, const char *NULLABLE suffix, int line);
    494 #define dns_concatenate_name_to_wire(txn, labels_prefix, prefix, suffix) \
    495     dns_concatenate_name_to_wire_(txn, labels_prefix, prefix, suffix, __LINE__)
    496 
    497 const char *NONNULL dns_name_print_to_limit(dns_name_t *NONNULL name, dns_name_t *NULLABLE limit, char *NULLABLE buf,
    498                                             size_t bufmax);
    499 const char *NONNULL dns_name_print(dns_name_t *NONNULL name, char *NONNULL buf, size_t bufmax);
    500 bool dns_labels_equal(const char *NONNULL label1, const char *NONNULL label2, size_t len);
    501 bool dns_names_equal_text(dns_label_t *NONNULL name1, const char *NONNULL name2);
    502 size_t dns_name_wire_length(dns_label_t *NONNULL name);
    503 size_t dns_name_to_wire_canonical(uint8_t *NONNULL buf, size_t max, dns_label_t *NONNULL name);
    504 dns_name_t *NULLABLE dns_pres_name_parse(const char *NONNULL pname);
    505 dns_name_t *NULLABLE dns_name_subdomain_of(dns_name_t *NONNULL name, dns_name_t *NONNULL domain);
    506 const char *NONNULL dns_rcode_name(int rcode);
    507 bool dns_keys_rdata_equal(dns_rr_t *NONNULL key1, dns_rr_t *NONNULL key2);
    508 void dns_txt_data_print(char *NONNULL txt_buf, size_t buf_size, uint16_t txt_length, uint8_t *NONNULL txt_data);
    509 bool dns_rrs_equal(dns_rr_t *NONNULL a, dns_rr_t *NONNULL b, bool rdata_present);
    510 bool dns_rr_to_wire(dns_towire_state_t *NONNULL towire, dns_rr_t *NONNULL rr, bool question);
    511 void dns_message_rrs_to_wire(dns_towire_state_t *NONNULL towire, dns_message_t *NONNULL message);
    512 
    513 /*!
    514  *  @brief
    515  *      Check if the IPv4 address represented by a 4-byte array is a link-local address.
    516  *
    517  *  @param bytes
    518  *      A bytes array whose length is 4, which represents an IPv4 address in the network byte order.
    519  *
    520  *  @result
    521  *      True if the IPv4 address is a link-local address, otherwise, false.
    522  */
    523 static inline bool is_ipv4_bytes_link_local(const uint8_t bytes[static const 4])
    524 {
    525     return bytes[0] == 169 && bytes[1] == 254;
    526 }
    527 
    528 /*!
    529  *  @brief
    530  *      Check if the IPv4 address represented by a 4-byte array is a loopback address.
    531  *
    532  *  @param bytes
    533  *      A bytes array whose length is 4, which represents an IPv4 address in the network byte order.
    534  *
    535  *  @result
    536  *      True if the IPv4 address is a loopback address, otherwise, false.
    537  */
    538 static inline bool is_ipv4_bytes_loopback(const uint8_t bytes[static const 4])
    539 {
    540     return bytes[0] == 127;
    541 }
    542 
    543 /*!
    544  *  @brief
    545  *      Check if the IPv4 address represented by a pointer to struct in_addr is a link-local address.
    546  *
    547  *  @param addr
    548  *      A pointer to struct in_addr structure.
    549  *
    550  *  @result
    551  *      True if the IPv4 address is a link-local address, otherwise, false.
    552  */
    553 static inline bool is_in_addr_link_local(const struct in_addr *const NONNULL addr)
    554 {
    555     return is_ipv4_bytes_link_local((const uint8_t *)&addr->s_addr);
    556 }
    557 
    558 /*!
    559  *  @brief
    560  *      Check if the IPv4 address represented by a pointer to struct in_addr is a loopback address.
    561  *
    562  *  @param addr
    563  *      A pointer to struct in_addr structure.
    564  *
    565  *  @result
    566  *      True if the IPv4 address is a loopback address, otherwise, false.
    567  */
    568 static inline bool is_in_addr_loopback(const struct in_addr *const NONNULL addr)
    569 {
    570     return is_ipv4_bytes_loopback((const uint8_t *)&addr->s_addr);
    571 }
    572 
    573 #endif // _DNS_MSG_H
    574 
    575 // Local Variables:
    576 // mode: C
    577 // tab-width: 4
    578 // c-file-style: "bsd"
    579 // c-basic-offset: 4
    580 // fill-column: 108
    581 // indent-tabs-mode: nil
    582 // End:
    583