unbound: util/data/msgencode.h File Reference

unbound 0.1

Functions
msgencode.h File Reference

This file contains temporary data structures and routines to create compressed DNS messages. More...

## Functions
int | reply_info_answer_encode (struct query_info *qinf, struct reply_info *rep, uint16_t id, uint16_t qflags, struct sldns_buffer *dest, time_t timenow, int cached, struct regional *region, uint16_t udpsize, struct edns_data *edns, int dnssec, int secure)
| Generate answer from reply_info.
int | reply_info_encode (struct query_info *qinfo, struct reply_info *rep, uint16_t id, uint16_t flags, struct sldns_buffer *buffer, time_t timenow, struct regional *region, uint16_t udpsize, int dnssec, int minimise)
| Regenerate the wireformat from the stored msg reply.
void | qinfo_query_encode (struct sldns_buffer *pkt, struct query_info *qinfo)
| Encode query packet.
size_t | calc_edns_field_size (struct edns_data *edns)
| Estimate size of EDNS record in packet.
uint16_t | calc_edns_option_size (struct edns_data *edns, uint16_t code)
| Calculate the size of a specific EDNS option in packet.
uint16_t | calc_ede_option_size (struct edns_data *edns, size_t *txt_size)
| Calculate the size of the EDE option(s) in packet.
void | attach_edns_record (struct sldns_buffer *pkt, struct edns_data *edns)
| Attach EDNS record to buffer.
void | error_encode (struct sldns_buffer *pkt, int r, struct query_info *qinfo, uint16_t qid, uint16_t qflags, struct edns_data *edns)
| Encode an error.
void | extended_error_encode (struct sldns_buffer *pkt, uint16_t rcode, struct query_info *qinfo, uint16_t qid, uint16_t qflags, uint16_t xflags, struct edns_data *edns)
| Encode an extended error.

## Detailed Description

This file contains temporary data structures and routines to create compressed DNS messages.

## Function Documentation

## ◆ reply_info_answer_encode()

int reply_info_answer_encode | ( | struct query_info * | qinf,
|  | struct reply_info * | rep,
|  | uint16_t | id,
|  | uint16_t | qflags,
|  | struct sldns_buffer * | dest,
|  | time_t | timenow,
|  | int | cached,
|  | struct regional * | region,
|  | uint16_t | udpsize,
|  | struct edns_data * | edns,
|  | int | dnssec,
|  | int | secure )

Generate answer from reply_info.

Parameters

  qinf | query information that provides query section in packet.
  rep | reply to fill in.
  id | id word from the query.
  qflags | flags word from the query.
  dest | buffer to put message into; will truncate if it does not fit.
  timenow | time to subtract.
  cached | set true if a cached reply (so no AA bit). set false for the first reply.
  region | where to allocate temp variables (for compression).
  udpsize | size of the answer, 512, from EDNS, or 64k for TCP.
  edns | EDNS data included in the answer, NULL for none. or if edns_present = 0, it is not included.
  dnssec | if 0 DNSSEC records are omitted from the answer.
  secure | if 1, the AD bit is set in the reply.

Returns
  : 0 on error (server failure).

References reply_info::authoritative, BIT_AA, BIT_AD, BIT_CD, BIT_QR, BIT_RD, calc_ede_option_size(), calc_edns_field_size(), edns_opt_list_remove(), edns_data::edns_present, reply_info::flags, FLAGS_GET_RCODE, query_info::local_alias, log_assert, log_err(), MINIMAL_RESPONSES, edns_data::opt_list_inplace_cb_out, edns_data::opt_list_out, reply_info_encode(), sldns_buffer_capacity(), and sldns_buffer_limit().

Referenced by answer_from_cache(), answer_norec_from_cache(), auth_answer_encode(), local_encode(), mesh_do_callback(), mesh_send_reply(), and rpz_local_encode().

## ◆ reply_info_encode()

int reply_info_encode | ( | struct query_info * | qinfo,
|  | struct reply_info * | rep,
|  | uint16_t | id,
|  | uint16_t | flags,
|  | struct sldns_buffer * | buffer,
|  | time_t | timenow,
|  | struct regional * | region,
|  | uint16_t | udpsize,
|  | int | dnssec,
|  | int | minimise )

Regenerate the wireformat from the stored msg reply.

If the buffer is too small then the message is truncated at a whole rrset and the TC bit set, or whole rrsets are left out of the additional and the TC bit is not set.

Parameters

  qinfo | query info to store.
  rep | reply to store.
  id | id value to store, network order.
  flags | flags value to store, host order.
  buffer | buffer to store the packet into.
  timenow | time now, to adjust ttl values.
  region | to store temporary data in.
  udpsize | size of the answer, 512, from EDNS, or 64k for TCP.
  dnssec | if 0 DNSSEC records are omitted from the answer.
  minimise | if true, the answer is a minimal response, with authority and additional removed if possible.

Returns
  : nonzero is success, or 0 on error: malloc failure (no log_err has been done).

References reply_info::an_numrrsets, reply_info::ar_numrrsets, BIT_AA, reply_info::flags, insert_query(), insert_section(), query_info::local_alias, reply_info::ns_numrrsets, reply_info::qdcount, query_info::qtype, RETVAL_OK, RETVAL_TRUNC, local_rrset::rrset, reply_info::rrset_count, RRSET_ROUNDROBIN, reply_info::rrsets, sldns_buffer_begin(), sldns_buffer_clear(), sldns_buffer_flip(), sldns_buffer_limit(), sldns_buffer_remaining(), sldns_buffer_set_limit(), sldns_buffer_write(), sldns_buffer_write_u16(), and sldns_buffer_write_u16_at().

Referenced by log_dns_msg(), perf_encode(), reply_info_answer_encode(), and testpkt().

## ◆ qinfo_query_encode()

void qinfo_query_encode | ( | struct sldns_buffer * | pkt,
|  | struct query_info * | qinfo )

Encode query packet.

Assumes the buffer is large enough.

Parameters

  pkt | where to store the packet.
  qinfo | query info.

References packed_rrset_key::dname, packed_rrset_key::dname_len, query_info::local_alias, log_assert, query_info::qclass, query_info::qname, query_info::qname_len, query_info::qtype, ub_packed_rrset_key::rk, local_rrset::rrset, sldns_buffer_clear(), sldns_buffer_flip(), sldns_buffer_remaining(), sldns_buffer_skip(), sldns_buffer_write(), and sldns_buffer_write_u16().

Referenced by qlist_parse_line(), write_q(), xfr_create_ixfr_packet(), and xfr_create_soa_probe_packet().

## ◆ calc_edns_field_size()

size_t calc_edns_field_size | ( | struct edns_data * | edns | ) |

Estimate size of EDNS record in packet.

EDNS record will be no larger.

Parameters

  edns | edns data or NULL.

Returns
  octets to reserve for EDNS.

References edns_data::edns_present, edns_option::next, edns_option::opt_len, edns_data::opt_list_inplace_cb_out, and edns_data::opt_list_out.

Referenced by chaos_replystr(), extended_error_encode(), reply_info_answer_encode(), testpkt(), and write_q().

## ◆ calc_edns_option_size()

uint16_t calc_edns_option_size | ( | struct edns_data * | edns,
|  | uint16_t | code )

Calculate the size of a specific EDNS option in packet.

Parameters

  edns | edns data or NULL.
  code | the opt code to get the size of.

Returns
  octets the option will take up.

References edns_data::edns_present, edns_option::next, edns_option::opt_code, edns_option::opt_len, edns_data::opt_list_inplace_cb_out, and edns_data::opt_list_out.

## ◆ calc_ede_option_size()

uint16_t calc_ede_option_size | ( | struct edns_data * | edns,
|  | size_t * | txt_size )

Calculate the size of the EDE option(s) in packet.

Also calculate separately the size of the EXTRA-TEXT field(s) in case we can trim them to fit. In this case include any LDNS_EDE_OTHER options in their entirety since they are useless without extra text.

Parameters

  edns | edns data or NULL.
  txt_size | the size of the EXTRA-TEXT field(s); this includes LDNS_EDE_OTHER in their entirety since they are useless without extra text.

Returns
  octets the option will take up.

References edns_data::edns_present, edns_option::next, edns_option::opt_code, edns_option::opt_data, edns_option::opt_len, edns_data::opt_list_inplace_cb_out, and edns_data::opt_list_out.

Referenced by extended_error_encode(), and reply_info_answer_encode().

## ◆ attach_edns_record()

void attach_edns_record | ( | struct sldns_buffer * | pkt,
|  | struct edns_data * | edns )

Attach EDNS record to buffer.

Buffer has complete packet. There must be enough room left for the EDNS record.

Parameters

  pkt | packet added to.
  edns | if NULL or present=0, nothing is added to the packet.

References edns_data::edns_present, and edns_data::udp_size.

Referenced by chaos_replystr(), perf_encode(), qlist_parse_line(), serviced_encode(), testpkt(), and write_q().

## ◆ error_encode()

void error_encode | ( | struct sldns_buffer * | pkt,
|  | int | r,
|  | struct query_info * | qinfo,
|  | uint16_t | qid,
|  | uint16_t | qflags,
|  | struct edns_data * | edns )

Encode an error.

With QR and RA set.

Parameters

  pkt | where to store the packet.
  r | RCODE value to encode (may contain extra flags).
  qinfo | if not NULL, the query is included.
  qid | query ID to set in packet. network order.
  qflags | original query flags (to copy RD and CD bits). host order.
  edns | if not NULL, this is the query edns info, and an edns reply is attached. Only attached if EDNS record fits reply.

References extended_error_encode().

Referenced by answer_from_cache(), answer_norec_from_cache(), answer_notify(), auth_answer_encode(), auth_error_encode(), libworker_bg_done_cb(), local_encode(), local_error_encode(), mesh_new_client(), mesh_send_reply(), rpz_local_encode(), and worker_handle_request().

## ◆ extended_error_encode()

void extended_error_encode | ( | struct sldns_buffer * | pkt,
|  | uint16_t | rcode,
|  | struct query_info * | qinfo,
|  | uint16_t | qid,
|  | uint16_t | qflags,
|  | uint16_t | xflags,
|  | struct edns_data * | edns )

Encode an extended error.

With QR and RA set.

Parameters

  pkt | where to store the packet.
  rcode | Extended RCODE value to encode.
  qinfo | if not NULL, the query is included.
  qid | query ID to set in packet. network order.
  qflags | original query flags (to copy RD and CD bits). host order.
  xflags | extra flags to set (such as for example BIT_AA and/or BIT_TC)
  edns | if not NULL, this is the query edns info, and an edns reply is attached. Only attached if EDNS record fits reply. Without edns extended errors (i.e. > 15) will not be conveyed.

References BIT_CD, BIT_QR, BIT_RA, BIT_RD, edns_data::bits, calc_ede_option_size(), calc_edns_field_size(), packed_rrset_key::dname, packed_rrset_key::dname_len, EDNS_ADVERTISED_SIZE, EDNS_ADVERTISED_VERSION, EDNS_DO, edns_opt_list_remove(), edns_data::edns_present, edns_data::edns_version, edns_data::ext_rcode, query_info::local_alias, edns_data::opt_list_inplace_cb_out, edns_data::opt_list_out, query_info::qclass, query_info::qname, query_info::qname_len, query_info::qtype, ub_packed_rrset_key::rk, local_rrset::rrset, sldns_buffer_clear(), sldns_buffer_current(), sldns_buffer_flip(), sldns_buffer_limit(), sldns_buffer_skip(), sldns_buffer_write(), sldns_buffer_write_u16(), and edns_data::udp_size.

Referenced by error_encode(), and worker_handle_request().