Home | History | Annotate | Line # | Download | only in dist
      1 /*
      2  * rdata.h -- RDATA conversion functions.
      3  *
      4  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
      5  *
      6  * See LICENSE for the license.
      7  *
      8  */
      9 
     10 #ifndef RDATA_H
     11 #define RDATA_H
     12 
     13 #include "dns.h"
     14 #include "namedb.h"
     15 
     16 /* High bit of the APL length field is the negation bit.  */
     17 #define APL_NEGATION_MASK      0x80U
     18 #define APL_LENGTH_MASK	       (~APL_NEGATION_MASK)
     19 
     20 /* High bit of the AMTRELAY Type byte in rdata[1] is the Discovery Optional
     21  * flag, the D-bit. */
     22 #define AMTRELAY_DISCOVERY_OPTIONAL_MASK 0x80U
     23 #define AMTRELAY_TYPE_MASK		 0x7fU
     24 
     25 extern lookup_table_type dns_certificate_types[];
     26 extern lookup_table_type dns_algorithms[];
     27 
     28 /*
     29  * Function signature for svcparam print. Input offset is at key uint16_t
     30  * in rdata.
     31  * @param output: the string is printed to the buffer.
     32  * @param svcparamkey: the key that is printed.
     33  * @param data: the data for the svcparam, from rdata.
     34  * @param datalen: length of data in bytes.
     35  * @return false on failure.
     36  */
     37 typedef int(*nsd_print_svcparam_rdata_type)(
     38 	struct buffer* output,
     39 	uint16_t svcparamkey,
     40 	const uint8_t* data,
     41 	uint16_t datalen);
     42 
     43 typedef struct nsd_svcparam_descriptor nsd_svcparam_descriptor_type;
     44 
     45 /* Descriptor for svcparam rdata fields. With type, name and print func. */
     46 struct nsd_svcparam_descriptor {
     47 	/* The svc param key */
     48 	uint16_t key;
     49 	/* The name of the key */
     50 	const char *name;
     51 	/* Print function that prints the key, from rdata. */
     52 	nsd_print_svcparam_rdata_type print_rdata;
     53 };
     54 
     55 int print_unknown_rdata_field(buffer_type *output,
     56 	const nsd_type_descriptor_type *descriptor, const rr_type *rr);
     57 int print_unknown_rdata(buffer_type *output,
     58 	const nsd_type_descriptor_type *descriptor, const rr_type *rr);
     59 
     60 /* print rdata to a text string (as for a zone file) returns 0
     61   on a failure (bufpos is reset to original position).
     62   returns 1 on success, bufpos is moved. */
     63 int print_rdata(buffer_type *output, const nsd_type_descriptor_type *descriptor,
     64 	const rr_type *rr);
     65 
     66 /* Read rdata for an unknown RR type. */
     67 int32_t read_generic_rdata(struct domain_table *domains, uint16_t rdlength,
     68 	struct buffer *packet, struct rr **rr);
     69 
     70 /* Write rdata for an unknown RR type. */
     71 void write_generic_rdata(struct query *query, const struct rr *rr);
     72 
     73 /* Print rdata for an unknown RR type. */
     74 int print_generic_rdata(struct buffer *output, const struct rr *rr);
     75 
     76 /* Read rdata for an RR type with one compressed dname. */
     77 int32_t read_compressed_name_rdata(struct domain_table *domains,
     78 	uint16_t rdlength, struct buffer *packet, struct rr **rr);
     79 
     80 /* Write rdata for an RR type with one compressed dname. */
     81 void write_compressed_name_rdata(struct query *query, const struct rr *rr);
     82 
     83 /* Print rdata for an RR type with one compressed or uncompressed dname.
     84  * But not a dname type literal. */
     85 int print_name_rdata(struct buffer *output, const struct rr *rr);
     86 
     87 /* Read rdata for an RR type with one uncompressed dname. */
     88 int32_t read_uncompressed_name_rdata(struct domain_table *domains,
     89 	uint16_t rdlength, struct buffer *packet, struct rr **rr);
     90 
     91 /* Write rdata for an RR type with one uncompressed dname. */
     92 void write_uncompressed_name_rdata(struct query *query, const struct rr *rr);
     93 
     94 /* Read rdata for type A. */
     95 int32_t read_a_rdata(struct domain_table *domains, uint16_t rdlength,
     96 	struct buffer *packet, struct rr **rr);
     97 
     98 /* Print rdata for type A. */
     99 int print_a_rdata(struct buffer *output, const struct rr *rr);
    100 
    101 /* Read rdata for type SOA. */
    102 int32_t read_soa_rdata(struct domain_table *domains, uint16_t rdlength,
    103 	struct buffer *packet, struct rr **rr);
    104 
    105 /* Write rdata for type SOA. */
    106 void write_soa_rdata(struct query *query, const struct rr *rr);
    107 
    108 /* Print rdata for type SOA. */
    109 int print_soa_rdata(struct buffer *output, const struct rr *rr);
    110 
    111 /* Print rdata for type SOA, on two lines, with parentheses. */
    112 int print_soa_rdata_twoline(struct buffer *output, const struct rr *rr);
    113 
    114 /* Read rdata for type WKS. */
    115 int32_t read_wks_rdata(struct domain_table *domains, uint16_t rdlength,
    116 	struct buffer *packet, struct rr **rr);
    117 
    118 /* Print rdata for type WKS. */
    119 int print_wks_rdata(struct buffer *output, const struct rr *rr);
    120 
    121 /* Read rdata for type HINFO. */
    122 int32_t read_hinfo_rdata(struct domain_table *domains, uint16_t rdlength,
    123 	struct buffer *packet, struct rr **rr);
    124 
    125 /* Print rdata for type HINFO. */
    126 int print_hinfo_rdata(struct buffer *output, const struct rr *rr);
    127 
    128 /* Read rdata for type MINFO. */
    129 int32_t read_minfo_rdata(struct domain_table *domains, uint16_t rdlength,
    130 	struct buffer *packet, struct rr **rr);
    131 
    132 /* Write rdata for type MINFO. */
    133 void write_minfo_rdata(struct query *query, const struct rr *rr);
    134 
    135 /* Print rdata for type MINFO. */
    136 int print_minfo_rdata(struct buffer *output, const struct rr *rr);
    137 
    138 /* Read rdata for type MX. */
    139 int32_t read_mx_rdata(struct domain_table *domains, uint16_t rdlength,
    140 	struct buffer *packet, struct rr **rr);
    141 
    142 /* Write rdata for type MX. */
    143 void write_mx_rdata(struct query *query, const struct rr *rr);
    144 
    145 /* Print rdata for type MX. */
    146 int print_mx_rdata(struct buffer *output, const struct rr *rr);
    147 
    148 /* Read rdata for type TXT. */
    149 int32_t read_txt_rdata(struct domain_table *domains, uint16_t rdlength,
    150 	struct buffer *packet, struct rr **rr);
    151 
    152 /* Print rdata for type TXT. */
    153 int print_txt_rdata(struct buffer *output, const struct rr *rr);
    154 
    155 /* Read rdata for type RP. */
    156 int32_t read_rp_rdata(struct domain_table *domains, uint16_t rdlength,
    157 	struct buffer *packet, struct rr **rr);
    158 
    159 /* Write rdata for type RP. */
    160 void write_rp_rdata(struct query *query, const struct rr *rr);
    161 
    162 /* Print rdata for type RP. */
    163 int print_rp_rdata(struct buffer *output, const struct rr *rr);
    164 
    165 /* Read rdata for type AFSDB. */
    166 int32_t read_afsdb_rdata(struct domain_table *domains, uint16_t rdlength,
    167 	struct buffer *packet, struct rr **rr);
    168 
    169 /* Write rdata for type AFSDB. */
    170 void write_afsdb_rdata(struct query *query, const struct rr *rr);
    171 
    172 /* Print rdata for type AFSDB. */
    173 int print_afsdb_rdata(struct buffer *output, const struct rr *rr);
    174 
    175 /* Read rdata for type X25. */
    176 int32_t read_x25_rdata(struct domain_table *domains, uint16_t rdlength,
    177 	struct buffer *packet, struct rr **rr);
    178 
    179 /* Print rdata for type X25. */
    180 int print_x25_rdata(struct buffer *output, const struct rr *rr);
    181 
    182 /* Read rdata for type ISDN. */
    183 int32_t read_isdn_rdata(struct domain_table *domains, uint16_t rdlength,
    184 	struct buffer *packet, struct rr **rr);
    185 
    186 /* Print rdata for type ISDN. */
    187 int print_isdn_rdata(struct buffer *output, const struct rr *rr);
    188 
    189 /* Read rdata for type RT. */
    190 int32_t read_rt_rdata(struct domain_table *domains, uint16_t rdlength,
    191 	struct buffer *packet, struct rr **rr);
    192 
    193 /* Write rdata for type RT. */
    194 void write_rt_rdata(struct query *query, const struct rr *rr);
    195 
    196 /* Print rdata for type NSAP. */
    197 int print_nsap_rdata(struct buffer *output, const struct rr *rr);
    198 
    199 /* Print rdata for type NSAP-PTR. */
    200 int print_nsap_ptr_rdata(struct buffer *output, const struct rr *rr);
    201 
    202 /* Print rdata for type KEY. */
    203 int print_key_rdata(struct buffer *output, const struct rr *rr);
    204 
    205 /* Read rdata for type PX. */
    206 int32_t read_px_rdata(struct domain_table *domains, uint16_t rdlength,
    207 	struct buffer *packet, struct rr **rr);
    208 
    209 /* Write rdata for type PX. */
    210 void write_px_rdata(struct query *query, const struct rr *rr);
    211 
    212 /* Print rdata for type PX. */
    213 int print_px_rdata(struct buffer *output, const struct rr *rr);
    214 
    215 /* Print rdata for type GPOS. */
    216 int print_gpos_rdata(struct buffer *output, const struct rr *rr);
    217 
    218 /* Read rdata for type AAAA. */
    219 int32_t read_aaaa_rdata(struct domain_table *domains, uint16_t rdlength,
    220 	struct buffer *packet, struct rr **rr);
    221 
    222 /* Print rdata for type AAAA. */
    223 int print_aaaa_rdata(struct buffer *output, const struct rr *rr);
    224 
    225 /* Print rdata for type LOC. */
    226 int32_t read_loc_rdata(struct domain_table *domains, uint16_t rdlength,
    227 	struct buffer *packet, struct rr **rr);
    228 
    229 /* Print rdata for type LOC. */
    230 int print_loc_rdata(struct buffer *output, const struct rr *rr);
    231 
    232 /* Read rdata for type NXT. */
    233 int32_t read_nxt_rdata(struct domain_table *domains, uint16_t rdlength,
    234 	struct buffer *packet, struct rr **rr);
    235 
    236 /* Write rdata for type NXT. */
    237 void write_nxt_rdata(struct query *query, const struct rr *rr);
    238 
    239 /* Print rdata for type NXT. */
    240 int print_nxt_rdata(struct buffer *output, const struct rr *rr);
    241 
    242 /* Print rdata for type EID. */
    243 int print_eid_rdata(struct buffer *output, const struct rr *rr);
    244 
    245 /* Print rdata for type NIMLOC. */
    246 int print_nimloc_rdata(struct buffer *output, const struct rr *rr);
    247 
    248 /* Read rdata for type SRV. */
    249 int32_t read_srv_rdata(struct domain_table *domains, uint16_t rdlength,
    250 	struct buffer *packet, struct rr **rr);
    251 
    252 /* Write rdata for type SRV. */
    253 void write_srv_rdata(struct query *query, const struct rr *rr);
    254 
    255 /* Print rdata for type SRV. */
    256 int print_srv_rdata(struct buffer *output, const struct rr *rr);
    257 
    258 /* Print rdata for type ATMA. */
    259 int print_atma_rdata(struct buffer *output, const struct rr *rr);
    260 
    261 /* Read rdata for type NAPTR. */
    262 int32_t read_naptr_rdata(struct domain_table *domains, uint16_t rdlength,
    263 	struct buffer *packet, struct rr **rr);
    264 
    265 /* Write rdata for type NAPTR. */
    266 void write_naptr_rdata(struct query *query, const struct rr *rr);
    267 
    268 /* Print rdata for type NAPTR. */
    269 int print_naptr_rdata(struct buffer *output, const struct rr *rr);
    270 
    271 /* Read rdata for type KX. */
    272 int32_t read_kx_rdata(struct domain_table *domains, uint16_t rdlength,
    273 	struct buffer *packet, struct rr **rr);
    274 
    275 /* Write rdata for type KX. */
    276 void write_kx_rdata(struct query *query, const struct rr *rr);
    277 
    278 /* Read rdata for type CERT. */
    279 int32_t read_cert_rdata(struct domain_table *domains, uint16_t rdlength,
    280 	struct buffer *packet, struct rr **rr);
    281 
    282 /* Print rdata for type CERT. */
    283 int print_cert_rdata(struct buffer *output, const struct rr *rr);
    284 
    285 /* Print rdata for type SINK. */
    286 int print_sink_rdata(struct buffer *output, const struct rr *rr);
    287 
    288 /* Read rdata for type APL. */
    289 int32_t read_apl_rdata(struct domain_table *domains, uint16_t rdlength,
    290 	struct buffer *packet, struct rr **rr);
    291 
    292 /* Print rdata for type APL. */
    293 int print_apl_rdata(struct buffer *output, const struct rr *rr);
    294 
    295 /* Read rdata for type DS. */
    296 int32_t read_ds_rdata(struct domain_table *domains, uint16_t rdlength,
    297 	struct buffer *packet, struct rr **rr);
    298 
    299 /* Print rdata for type DS. */
    300 int print_ds_rdata(struct buffer *output, const struct rr *rr);
    301 
    302 /* Read rdata for type SSHFP. */
    303 int32_t read_sshfp_rdata(struct domain_table *domains, uint16_t rdlength,
    304 	struct buffer *packet, struct rr **rr);
    305 
    306 /* Print rdata for type SSHFP. */
    307 int print_sshfp_rdata(struct buffer *output, const struct rr *rr);
    308 
    309 /* Read rdata for type IPSECKEY. */
    310 int32_t read_ipseckey_rdata(struct domain_table *domains, uint16_t rdlength,
    311 	struct buffer *packet, struct rr **rr);
    312 
    313 /* Print rdata for type IPSECKEY. */
    314 int print_ipseckey_rdata(struct buffer *output, const struct rr *rr);
    315 
    316 /* Determine length of IPSECKEY gateway field. */
    317 int32_t ipseckey_gateway_length(uint16_t rdlength, const uint8_t *rdata,
    318 	uint16_t offset, struct domain** domain);
    319 
    320 /* Read rdata for type RRSIG. */
    321 int32_t read_rrsig_rdata(struct domain_table *domains, uint16_t rdlength,
    322 	struct buffer *packet, struct rr **rr);
    323 
    324 /* Print rdata for type RRSIG. */
    325 int print_rrsig_rdata(struct buffer *output, const struct rr *rr);
    326 
    327 /* Read rdata for type NSEC. */
    328 int32_t read_nsec_rdata(struct domain_table *domains, uint16_t rdlength,
    329 	struct buffer *packet, struct rr **rr);
    330 
    331 /* Print rdata for type NSEC. */
    332 int print_nsec_rdata(struct buffer *output, const struct rr *rr);
    333 
    334 /* Read rdata for type DNSKEY. */
    335 int32_t read_dnskey_rdata(struct domain_table *domains, uint16_t rdlength,
    336 	struct buffer *packet, struct rr **rr);
    337 
    338 /* Print rdata for type DNSKEY. */
    339 int print_dnskey_rdata(struct buffer *output, const struct rr *rr);
    340 
    341 /* Read rdata for type DHCID. */
    342 int32_t read_dhcid_rdata(struct domain_table *domains, uint16_t rdlength,
    343 	struct buffer *packet, struct rr **rr);
    344 
    345 /* Print rdata for type DHCID. */
    346 int print_dhcid_rdata(struct buffer *output, const struct rr *rr);
    347 
    348 /* Read rdata for type NSEC3. */
    349 int32_t read_nsec3_rdata(struct domain_table *domains, uint16_t rdlength,
    350 	struct buffer *packet, struct rr **rr);
    351 
    352 /* Print rdata for type NSEC3. */
    353 int print_nsec3_rdata(struct buffer *output, const struct rr *rr);
    354 
    355 /* Read rdata for type NSEC3PARAM. */
    356 int32_t read_nsec3param_rdata(struct domain_table *domains, uint16_t rdlength,
    357 	struct buffer *packet, struct rr **rr);
    358 
    359 /* Print rdata for type NSEC3PARAM. */
    360 int print_nsec3param_rdata(struct buffer *output, const struct rr *rr);
    361 
    362 /* Read rdata for type TLSA. */
    363 int32_t read_tlsa_rdata(struct domain_table *domains, uint16_t rdlength,
    364 	struct buffer *packet, struct rr **rr);
    365 
    366 /* Print rdata for type TLSA. */
    367 int print_tlsa_rdata(struct buffer *output, const struct rr *rr);
    368 
    369 /* Read rdata for type HIP. */
    370 int32_t read_hip_rdata(struct domain_table *domains, uint16_t rdlength,
    371 	struct buffer *packet, struct rr **rr);
    372 
    373 /* Print rdata for type HIP. */
    374 int print_hip_rdata(struct buffer *output, const struct rr *rr);
    375 
    376 /* Read rdata for type RKEY. */
    377 int32_t read_rkey_rdata(struct domain_table *domains, uint16_t rdlength,
    378 	struct buffer *packet, struct rr **rr);
    379 
    380 /* Print rdata for type RKEY. */
    381 int print_rkey_rdata(struct buffer *output, const struct rr *rr);
    382 
    383 /* Read rdata for type TALINK. */
    384 int32_t read_talink_rdata(struct domain_table *domains, uint16_t rdlength,
    385 	struct buffer *packet, struct rr **rr);
    386 
    387 /* Print rdata for type TALINK. */
    388 int print_talink_rdata(struct buffer *output, const struct rr *rr);
    389 
    390 /* Print rdata for type OPENPGPKEY. */
    391 int print_openpgpkey_rdata(struct buffer *output, const struct rr *rr);
    392 
    393 /* Read rdata for type CSYNC. */
    394 int32_t read_csync_rdata(struct domain_table *domains, uint16_t rdlength,
    395 	struct buffer *packet, struct rr **rr);
    396 
    397 /* Print rdata for type CSYNC. */
    398 int print_csync_rdata(struct buffer *output, const struct rr *rr);
    399 
    400 /* Read rdata for type ZONEMD. */
    401 int32_t read_zonemd_rdata(struct domain_table *domains, uint16_t rdlength,
    402 	struct buffer *packet, struct rr **rr);
    403 
    404 /* Print rdata for type ZONEMD. */
    405 int print_zonemd_rdata(struct buffer *output, const struct rr *rr);
    406 
    407 /* Read rdata for type SVCB. */
    408 int32_t read_svcb_rdata(struct domain_table *domains, uint16_t rdlength,
    409 	struct buffer *packet, struct rr **rr);
    410 
    411 /* Write rdata for type SVCB. */
    412 void write_svcb_rdata(struct query *query, const struct rr *rr);
    413 
    414 /* Print rdata for type SVCB. */
    415 int print_svcb_rdata(struct buffer *output, const struct rr *rr);
    416 
    417 /* Read rdata for type DSYNC. */
    418 int32_t read_dsync_rdata(struct domain_table *domains, uint16_t rdlength,
    419 	struct buffer *packet, struct rr **rr);
    420 
    421 /* Print rdata for type DSYNC. */
    422 int print_dsync_rdata(struct buffer *output, const struct rr *rr);
    423 
    424 /* Read rdata for type NID. */
    425 int32_t read_nid_rdata(struct domain_table *domains, uint16_t rdlength,
    426 	struct buffer *packet, struct rr **rr);
    427 
    428 /* Print rdata for type NID. */
    429 int print_nid_rdata(struct buffer *output, const struct rr *rr);
    430 
    431 /* Read rdata for type L32. */
    432 int32_t read_l32_rdata(struct domain_table *domains, uint16_t rdlength,
    433 	struct buffer *packet, struct rr **rr);
    434 
    435 /* Print rdata for type L32. */
    436 int print_l32_rdata(struct buffer *output, const struct rr *rr);
    437 
    438 /* Read rdata for type L64. */
    439 int32_t read_l64_rdata(struct domain_table *domains, uint16_t rdlength,
    440 	struct buffer *packet, struct rr **rr);
    441 
    442 /* Print rdata for type L64. */
    443 int print_l64_rdata(struct buffer *output, const struct rr *rr);
    444 
    445 /* Read rdata for type LP. */
    446 int32_t read_lp_rdata(struct domain_table *domains, uint16_t rdlength,
    447 	struct buffer *packet, struct rr **rr);
    448 
    449 /* Write rdata for type LP. */
    450 void write_lp_rdata(struct query *query, const struct rr *rr);
    451 
    452 /* Print rdata for type LP. */
    453 int print_lp_rdata(struct buffer *output, const struct rr *rr);
    454 
    455 /* Read rdata for type EUI48. */
    456 int32_t read_eui48_rdata(struct domain_table *domains, uint16_t rdlength,
    457 	struct buffer *packet, struct rr **rr);
    458 
    459 /* Print rdata for type EUI48. */
    460 int print_eui48_rdata(struct buffer *output, const struct rr *rr);
    461 
    462 /* Read rdata for type EUI64. */
    463 int32_t read_eui64_rdata(struct domain_table *domains, uint16_t rdlength,
    464 	struct buffer *packet, struct rr **rr);
    465 
    466 /* Print rdata for type EUI64. */
    467 int print_eui64_rdata(struct buffer *output, const struct rr *rr);
    468 
    469 /* Read rdata for type URI. */
    470 int32_t read_uri_rdata(struct domain_table *domains, uint16_t rdlength,
    471 	struct buffer *packet, struct rr **rr);
    472 
    473 /* Print rdata for type URI. */
    474 int print_uri_rdata(struct buffer *output, const struct rr *rr);
    475 
    476 /* Print rdata for type resinfo. */
    477 int print_resinfo_rdata(struct buffer *output, const struct rr *rr);
    478 
    479 /* Read rdata for type CAA. */
    480 int32_t read_caa_rdata(struct domain_table *domains, uint16_t rdlength,
    481 	struct buffer *packet, struct rr **rr);
    482 
    483 /* Print rdata for type CAA. */
    484 int print_caa_rdata(struct buffer *output, const struct rr *rr);
    485 
    486 /* Print rdata for type DOA. */
    487 int print_doa_rdata(struct buffer *output, const struct rr *rr);
    488 
    489 /* Read rdata for type AMTRELAY. */
    490 int32_t read_amtrelay_rdata(struct domain_table *domains, uint16_t rdlength,
    491 	struct buffer *packet, struct rr **rr);
    492 
    493 /* Print rdata for type AMTRELAY. */
    494 int print_amtrelay_rdata(struct buffer *output, const struct rr *rr);
    495 
    496 /* Determine length of AMTRELAY relay field. */
    497 int32_t amtrelay_relay_length(uint16_t rdlength, const uint8_t *rdata,
    498 	uint16_t offset, struct domain** domain);
    499 
    500 /* Print rdata for type IPN. */
    501 int print_ipn_rdata(struct buffer *output, const struct rr *rr);
    502 
    503 /* Read rdata for type DLV. */
    504 int32_t read_dlv_rdata(struct domain_table *domains, uint16_t rdlength,
    505 	struct buffer *packet, struct rr **rr);
    506 
    507 /* Print rdata for type DLV. */
    508 int print_dlv_rdata(struct buffer *output, const struct rr *rr);
    509 
    510 /*
    511  * Look up the uncompressed wireformat length of the rdata.
    512  * The pointer references in it are taking up the length of their uncompressed
    513  * domain names. The length is without the RR's rdatalength uint16 preceding.
    514  * @param rr: the rr, the RR type and rdata are used.
    515  * @result -1 on failure, otherwise length in bytes.
    516  */
    517 int32_t rr_calculate_uncompressed_rdata_length(const rr_type* rr);
    518 
    519 /*
    520  * Write uncompressed wireformat rdata to buffer. The pointer references
    521  * and domains are uncompressed wireformat domain names. The uint16 rdlength
    522  * is not written before it.
    523  * @param rr: the rr, with RR type and rdata.
    524  * @param buf: destination.
    525  * @param len: length of buffer.
    526  */
    527 void rr_write_uncompressed_rdata(const rr_type* rr, uint8_t* buf, size_t len);
    528 
    529 /*
    530  * Look up the field length. The field length is returned as a length
    531  * in the rdata that is stored. For a reference, the pointer is returned too.
    532  * Before calling it check if the field is_optional, and rdlength is
    533  * reached by offset, then there are no more rdata fields.
    534  * Also if the index has reached the rdata field length count, fields end.
    535  * It checks if the field fits in the rdata buffer, failure if not.
    536  * Then check for domain ptr or not, and handle the field at rr->rdata+offset.
    537  * Continue the loop by incrementing offset with field_len, and index++.
    538  *
    539  * @param descriptor: type descriptor.
    540  * @param index: field index.
    541  * @param rr: the rr with the rdata.
    542  * @param offset: current position in the rdata.
    543  *	It is not updated, because the caller has to do that.
    544  * @param field_len: the field length is returned.
    545  * @param domain: the pointer is returned when the field is a reference.
    546  * @return false on failure, when the rdata stored is badly formatted, like
    547  *	the rdata buffer is too short.
    548  */
    549 int lookup_rdata_field_entry(const nsd_type_descriptor_type* descriptor,
    550 	size_t index, const rr_type* rr, uint16_t offset, uint16_t* field_len,
    551 	struct domain** domain);
    552 
    553 /* Look up the field length. Same as lookup_rdata_field_entry, but the rdata
    554  * is uncompressed wireformat. The length returned skips the field in the
    555  * uncompressed wireformat. */
    556 int lookup_rdata_field_entry_uncompressed_wire(
    557 	const nsd_type_descriptor_type* descriptor, size_t index,
    558 	const uint8_t* rdata, uint16_t rdlength, uint16_t offset,
    559 	uint16_t* field_len, struct domain** domain);
    560 
    561 /*
    562  * Compare rdata for equality. This is easier than the sorted compare,
    563  * it treats field types as a difference too, so a reference instead of
    564  * a wireformat field makes for a different RR.
    565  * The RRs have to be the same type already.
    566  * It iterates over the RR type fields. The RRs and the rdatas are the
    567  * namedb format, that is with references stored as pointers.
    568  * @param rr1: RR to compare rdata 1. The rdata can contain pointers.
    569  * @param rr2: RR to compare rdata 2. The rdata can contain pointers.
    570  * @return true if rdata is equal.
    571  */
    572 int equal_rr_rdata(const nsd_type_descriptor_type *descriptor,
    573 	const struct rr *rr1, const struct rr *rr2);
    574 
    575 /*
    576  * Compare rdata for equality. Same as equal_rr_rdata, but the second
    577  * rdata is passed as uncompressed wireformat, the first has the in-memory
    578  * rdata format.
    579  */
    580 int equal_rr_rdata_uncompressed_wire(const nsd_type_descriptor_type *descriptor,
    581 	const struct rr *rr1, const uint8_t* rr2_rdata, uint16_t rr2_rdlen);
    582 
    583 /*
    584  * Retrieve domain ref at an offset in the rdata.
    585  * @param rr: the RR to retrieve it for.
    586  * @param offset: where in the rdata the reference pointer is.
    587  * @return domain ptr.
    588  */
    589 struct domain* retrieve_rdata_ref_domain_offset(const struct rr* rr,
    590 	uint16_t offset);
    591 
    592 /*
    593  * Retrieve domain ref from rdata. No offset, rdata starts with ref.
    594  * @param rr: the RR to retrieve it for.
    595  * @return domain ptr.
    596  */
    597 struct domain* retrieve_rdata_ref_domain(const struct rr* rr);
    598 
    599 /*
    600  * Accessor function to the domain in the rdata of the type.
    601  * The RR must be of the type. The type must have references.
    602  * @param rr: the rr with rdata
    603  * @return domain pointer.
    604  */
    605 typedef struct domain*(*nsd_rdata_ref_domain_type)(
    606 	const struct rr* rr);
    607 
    608 /* Access the domain reference for type NS */
    609 struct domain* retrieve_ns_ref_domain(const struct rr* rr);
    610 
    611 /* Access the domain reference for type CNAME */
    612 struct domain* retrieve_cname_ref_domain(const struct rr* rr);
    613 
    614 /* Access the domain reference for type DNAME */
    615 struct domain* retrieve_dname_ref_domain(const struct rr* rr);
    616 
    617 /* Access the domain reference for type MB */
    618 struct domain* retrieve_mb_ref_domain(const struct rr* rr);
    619 
    620 /* Access the domain reference for type MX */
    621 struct domain* retrieve_mx_ref_domain(const struct rr* rr);
    622 
    623 /* Access the domain reference for type KX */
    624 struct domain* retrieve_kx_ref_domain(const struct rr* rr);
    625 
    626 /* Access the domain reference for type RT */
    627 struct domain* retrieve_rt_ref_domain(const struct rr* rr);
    628 
    629 /* Access the domain reference for type SRV */
    630 struct domain* retrieve_srv_ref_domain(const struct rr* rr);
    631 
    632 /* Access the domain reference for type PTR */
    633 struct domain* retrieve_ptr_ref_domain(const struct rr* rr);
    634 
    635 /* Access the serial number for type SOA, false if malformed. */
    636 int retrieve_soa_rdata_serial(const struct rr* rr, uint32_t* serial);
    637 
    638 /* Access the minimum ttl for type SOA, false if malformed. */
    639 int retrieve_soa_rdata_minttl(const struct rr* rr, uint32_t* minttl);
    640 
    641 /* Access the dname reference for type CNAME */
    642 struct dname* retrieve_cname_ref_dname(const struct rr* rr);
    643 
    644 /*
    645  * Access the domain name reference, that is stored as COMPRESSED_DNAME,
    646  * or UNCOMPRESSED DNAME, at the start of the rdata, or only part of the rdata.
    647  * Not for literal DNAMEs. This is similar to a pointer reference,
    648  * but it may be stored unaligned.
    649  * @param rr: the resource record.
    650  * @return domain pointer.
    651  */
    652 static inline struct domain* rdata_domain_ref(const struct rr* rr) {
    653 	struct domain* domain;
    654 	assert(rr->rdlength >= (uint16_t)sizeof(void*));
    655 	memcpy(&domain, rr->rdata, sizeof(void*));
    656 	return domain;
    657 }
    658 
    659 /*
    660  * Access the domain name reference, that is stored as COMPRESSED_DNAME,
    661  * or UNCOMPRESSED DNAME, the reference is at an offset in the rdata.
    662  * Not for literal DNAMEs. This is similar to a pointer reference,
    663  * but it may be stored unaligned.
    664  * @param rr: the resource record
    665  * @param offset: where the reference is found in the rdata. Pass like 2,
    666  *	for type MX, or sizeof(void*) to access the second domain pointer
    667  *	of SOA.
    668  * @return domain pointer.
    669  */
    670 static inline struct domain* rdata_domain_ref_offset(const struct rr* rr,
    671 	uint16_t offset) {
    672 	struct domain* domain;
    673 	assert(rr->rdlength >= offset+(uint16_t)sizeof(void*));
    674 	memcpy(&domain, rr->rdata+offset, sizeof(void*));
    675 	return domain;
    676 }
    677 
    678 /* fixup usage lower for domain names in the rdata */
    679 void rr_lower_usage(namedb_type* db, rr_type* rr);
    680 
    681 /* return error string for read_rdata return code that is < 0 */
    682 const char* read_rdata_fail_str(int32_t code);
    683 
    684 #endif /* RDATA_H */
    685