Data Structures | Macros | Functions
msgreply.h File Reference

This file contains a data structure to store a message and its reply. More...

#include "util/storage/lruhash.h"
#include "util/data/packed_rrset.h"

Data Structures

struct  query_info
 Structure to store query information that makes answers to queries different. More...
 
struct  rrset_ref
 Information to reference an rrset. More...
 
struct  reply_info
 Structure to store DNS query and the reply packet. More...
 
struct  msgreply_entry
 Structure to keep hash table entry for message replies. More...
 

Macros

#define PREFETCH_TTL_CALC(ttl)   ((ttl) - (ttl)/10)
 calculate the prefetch TTL as 90% of original. More...
 

Functions

struct reply_infoconstruct_reply_info_base (struct regional *region, uint16_t flags, size_t qd, time_t ttl, time_t prettl, size_t an, size_t ns, size_t ar, size_t total, enum sec_status sec)
 Constructor for replyinfo. More...
 
int query_info_parse (struct query_info *m, struct sldns_buffer *query)
 Parse wire query into a queryinfo structure, return 0 on parse error. More...
 
int reply_info_parse (struct sldns_buffer *pkt, struct alloc_cache *alloc, struct query_info *qinf, struct reply_info **rep, struct regional *region, struct edns_data *edns)
 Parse query reply. More...
 
int parse_create_msg (struct sldns_buffer *pkt, struct msg_parse *msg, struct alloc_cache *alloc, struct query_info *qinf, struct reply_info **rep, struct regional *region)
 Allocate and decompress parsed message and rrsets. More...
 
void reply_info_sortref (struct reply_info *rep)
 Sorts the ref array. More...
 
void reply_info_set_ttls (struct reply_info *rep, time_t timenow)
 Set TTLs inside the replyinfo to absolute values. More...
 
void reply_info_parsedelete (struct reply_info *rep, struct alloc_cache *alloc)
 Delete reply_info and packed_rrsets (while they are not yet added to the hashtables.). More...
 
int query_info_compare (void *m1, void *m2)
 Compare two queryinfo structures, on query and type, class. More...
 
void query_info_clear (struct query_info *m)
 clear out query info structure
 
size_t msgreply_sizefunc (void *k, void *d)
 calculate size of struct query_info + reply_info
 
void query_entry_delete (void *q, void *arg)
 delete msgreply_entry key structure
 
void reply_info_delete (void *d, void *arg)
 delete reply_info data structure
 
hashvalue_t query_info_hash (struct query_info *q, uint16_t flags)
 calculate hash value of query_info, lowercases the qname, uses CD flag for AAAA qtype
 
struct msgreply_entryquery_info_entrysetup (struct query_info *q, struct reply_info *r, hashvalue_t h)
 Setup query info entry. More...
 
struct reply_inforeply_info_copy (struct reply_info *rep, struct alloc_cache *alloc, struct regional *region)
 Copy reply_info and all rrsets in it and allocate. More...
 
int parse_copy_decompress_rrset (struct sldns_buffer *pkt, struct msg_parse *msg, struct rrset_parse *pset, struct regional *region, struct ub_packed_rrset_key *pk)
 Copy a parsed rrset into given key, decompressing and allocating rdata. More...
 
uint8_t * reply_find_final_cname_target (struct query_info *qinfo, struct reply_info *rep)
 Find final cname target in reply, the one matching qinfo. More...
 
int reply_check_cname_chain (struct reply_info *rep)
 Check if cname chain in cached reply is still valid. More...
 
int reply_all_rrsets_secure (struct reply_info *rep)
 Check security status of all RRs in the message. More...
 
struct ub_packed_rrset_keyreply_find_answer_rrset (struct query_info *qinfo, struct reply_info *rep)
 Find answer rrset in reply, the one matching qinfo. More...
 
struct ub_packed_rrset_keyreply_find_rrset_section_an (struct reply_info *rep, uint8_t *name, size_t namelen, uint16_t type, uint16_t dclass)
 Find rrset in reply, inside the answer section. More...
 
struct ub_packed_rrset_keyreply_find_rrset_section_ns (struct reply_info *rep, uint8_t *name, size_t namelen, uint16_t type, uint16_t dclass)
 Find rrset in reply, inside the authority section. More...
 
struct ub_packed_rrset_keyreply_find_rrset (struct reply_info *rep, uint8_t *name, size_t namelen, uint16_t type, uint16_t dclass)
 Find rrset in reply, inside any section. More...
 
void log_dns_msg (const char *str, struct query_info *qinfo, struct reply_info *rep)
 Debug send the query info and reply info to the log in readable form. More...
 
void log_query_info (enum verbosity_value v, const char *str, struct query_info *qinf)
 Print string with neat domain name, type, class from query info. More...
 

Detailed Description

This file contains a data structure to store a message and its reply.

Macro Definition Documentation

#define PREFETCH_TTL_CALC (   ttl)    ((ttl) - (ttl)/10)

calculate the prefetch TTL as 90% of original.

Calculation without numerical overflow (uin32_t)

Referenced by error_response_cache(), load_msg(), parse_copy_decompress(), processFinished(), rrset_msg(), synth_dname_msg(), and tomsg().

Function Documentation

struct reply_info* construct_reply_info_base ( struct regional region,
uint16_t  flags,
size_t  qd,
time_t  ttl,
time_t  prettl,
size_t  an,
size_t  ns,
size_t  ar,
size_t  total,
enum sec_status  sec 
)

Constructor for replyinfo.

Parameters
regionwhere to allocate the results, pass NULL to use malloc.
flagsflags for the replyinfo.
qdqd count
ttlTTL of replyinfo
prettlprefetch ttl
anan count
nsns count
arar count
totaltotal rrset count (presumably an+ns+ar).
secsecurity status of the reply info.
Returns
the reply_info base struct with the array for putting the rrsets in. The array has been zeroed. Returns NULL on malloc failure.

Constructor for replyinfo.

References reply_info::an_numrrsets, reply_info::ar_numrrsets, reply_info::authoritative, reply_info::flags, reply_info::ns_numrrsets, reply_info::prefetch_ttl, reply_info::qdcount, reply_info::ref, regional_alloc(), reply_info::rrset_count, reply_info::rrsets, reply_info::security, and reply_info::ttl.

Referenced by dns64_adjust_a(), parse_create_repinfo(), and reply_info_copy().

int query_info_parse ( struct query_info m,
struct sldns_buffer query 
)

Parse wire query into a queryinfo structure, return 0 on parse error.

initialises the (prealloced) queryinfo structure as well. This query structure contains a pointer back info the buffer! This pointer avoids memory allocation. allocqname does memory allocation.

Parameters
mthe prealloced queryinfo structure to put query into. must be unused, or _clear()ed.
querythe wireformat packet query. starts with ID.
Returns
: 0 on format error.

References query_info::qclass, query_info::qname, query_info::qname_len, query_info::qtype, query_dname_len(), sldns_buffer_begin(), sldns_buffer_current(), sldns_buffer_limit(), sldns_buffer_position(), sldns_buffer_read_u16(), sldns_buffer_remaining(), and sldns_buffer_skip().

Referenced by worker_handle_request().

int reply_info_parse ( struct sldns_buffer pkt,
struct alloc_cache alloc,
struct query_info qinf,
struct reply_info **  rep,
struct regional region,
struct edns_data edns 
)

Parse query reply.

Fills in preallocated query_info structure (with ptr into buffer). Allocates reply_info and packed_rrsets. These are not yet added to any caches or anything, this is only parsing. Returns formerror on qdcount > 1.

Parameters
pktthe packet buffer. Must be positioned after the query section.
alloccreates packed rrset key structures.
repallocated reply_info is returned (only on no error).
qinfquery_info is returned (only on no error).
regionwhere to store temporary data (for parsing).
ednswhere to store edns information, does not need to be inited.
Returns
: zero is OK, or DNS error code in case of error o FORMERR for parse errors. o SERVFAIL for memory allocation errors.

References parse_create_msg(), parse_extract_edns(), parse_packet(), query_info::qname, query_info_clear(), regional_alloc(), reply_info_parsedelete(), and sldns_buffer_set_position().

Referenced by entry_to_repinfo(), perftestpkt(), and testpkt().

int parse_create_msg ( struct sldns_buffer pkt,
struct msg_parse msg,
struct alloc_cache alloc,
struct query_info qinf,
struct reply_info **  rep,
struct regional region 
)

Allocate and decompress parsed message and rrsets.

Parameters
pktfor name decompression.
msgparsed message in scratch region.
allocalloc cache for special rrset key structures. Not used if region!=NULL, it can be NULL in that case.
qinfwhere to store query info. qinf itself is allocated by the caller.
repreply info is allocated and returned.
regionif this parameter is NULL then malloc and the alloc is used. otherwise, everything is allocated in this region. In a region, no special rrset key structures are needed (not shared), and no rrset_ref array in the reply is built up.
Returns
0 if allocation failed.

References log_assert, parse_copy_decompress(), parse_create_qinfo(), parse_create_repinfo(), and repinfo_alloc_rrset_keys().

Referenced by createResponse(), dns_alloc_msg(), parse_reply(), and reply_info_parse().

void reply_info_sortref ( struct reply_info rep)

Sorts the ref array.

Parameters
repreply info. rrsets must be filled in.

References reply_info::ref, reply_info_sortref_cmp(), and reply_info::rrset_count.

Referenced by dns_cache_store_msg().

void reply_info_set_ttls ( struct reply_info rep,
time_t  timenow 
)

Set TTLs inside the replyinfo to absolute values.

Parameters
repreply info. rrsets must be filled in. Also refs must be filled in.
timenowthe current time.

References packed_rrset_data::count, lruhash_entry::data, ub_packed_rrset_key::entry, rrset_ref::key, reply_info::prefetch_ttl, reply_info::ref, packed_rrset_data::rr_ttl, reply_info::rrset_count, packed_rrset_data::rrsig_count, reply_info::ttl, and packed_rrset_data::ttl.

Referenced by dns_cache_store_msg().

void reply_info_parsedelete ( struct reply_info rep,
struct alloc_cache alloc 
)

Delete reply_info and packed_rrsets (while they are not yet added to the hashtables.).

Returns rrsets to the alloc cache.

Parameters
repreply_info to delete.
allocwhere to return rrset structures to.

References reply_info::rrset_count, reply_info::rrsets, and ub_packed_rrset_parsedelete().

Referenced by dns_cache_store(), dstest_entry(), extract_keys(), nsec3_hash_test_entry(), perftestpkt(), reply_info_copy(), reply_info_parse(), testpkt(), and verifytest_entry().

int query_info_compare ( void *  m1,
void *  m2 
)

Compare two queryinfo structures, on query and type, class.

It is not sorted in canonical ordering.

Parameters
m1struct query_info* , void* here to ease use as function pointer.
m2struct query_info* , void* here to ease use as function pointer.
Returns
: 0 = same, -1 m1 is smaller, +1 m1 is larger.

References COMPARE_IT, log_assert, query_info::qclass, query_info::qname, query_info::qname_len, query_info::qtype, and query_dname_compare().

Referenced by context_finalize(), daemon_apply_cfg(), fptr_whitelist_hash_compfunc(), and mesh_state_compare().

struct msgreply_entry* query_info_entrysetup ( struct query_info q,
struct reply_info r,
hashvalue_t  h 
)

Setup query info entry.

Parameters
qquery info to copy. Emptied as if clear is called.
rreply to init data.
hhash value.
Returns
: newly allocated message reply cache item.

References lruhash_entry::data, msgreply_entry::entry, lruhash_entry::hash, msgreply_entry::key, lruhash_entry::key, lruhash_entry::lock, query_info::qname, and query_info::qname_len.

Referenced by dns_cache_store_msg().

struct reply_info* reply_info_copy ( struct reply_info rep,
struct alloc_cache alloc,
struct regional region 
)

Copy reply_info and all rrsets in it and allocate.

Parameters
repwhat to copy, probably inside region, no ref[] array in it.
allochow to allocate rrset keys. Not used if region!=NULL, it can be NULL in that case.
regionif this parameter is NULL then malloc and the alloc is used. otherwise, everything is allocated in this region. In a region, no special rrset key structures are needed (not shared), and no rrset_ref array in the reply is built up.
Returns
new reply info or NULL on memory error.

References reply_info::an_numrrsets, reply_info::ar_numrrsets, construct_reply_info_base(), reply_info::flags, reply_info::ns_numrrsets, reply_info::prefetch_ttl, reply_info::qdcount, repinfo_alloc_rrset_keys(), repinfo_copy_rrsets(), reply_info_parsedelete(), reply_info::rrset_count, reply_info::security, and reply_info::ttl.

Referenced by dns64_adjust_ptr(), dns_cache_store(), and dns_copy_msg().

int parse_copy_decompress_rrset ( struct sldns_buffer pkt,
struct msg_parse msg,
struct rrset_parse pset,
struct regional region,
struct ub_packed_rrset_key pk 
)

Copy a parsed rrset into given key, decompressing and allocating rdata.

Parameters
pktpacket for decompression
msgthe parser message (for flags for trust).
psetthe parsed rrset to copy.
regionif NULL - malloc, else data is allocated in this region.
pka freshly obtained rrsetkey structure. No dname is set yet, will be set on return. Note that TTL will still be relative on return.
Returns
false on alloc failure.

copy & decompress dname

copy over type and class

read data part.

References lruhash_entry::data, packed_rrset_key::dname, rrset_parse::dname, packed_rrset_key::dname_len, rrset_parse::dname_len, dname_pkt_copy(), ub_packed_rrset_key::entry, packed_rrset_key::flags, rrset_parse::flags, get_rrset_trust(), rrset_parse::hash, lruhash_entry::hash, lruhash_entry::key, parse_create_rrset(), regional_alloc(), ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, rrset_parse::rrset_class, packed_rrset_data::trust, packed_rrset_key::type, and rrset_parse::type.

Referenced by parse_copy_decompress(), and store_rrset().

uint8_t* reply_find_final_cname_target ( struct query_info qinfo,
struct reply_info rep 
)

Find final cname target in reply, the one matching qinfo.

Follows CNAMEs.

Parameters
qinfowhat to start with.
replooks in answer section of this message.
Returns
: pointer dname, or NULL if not found.

References reply_info::an_numrrsets, packed_rrset_key::dname, packed_rrset_key::dname_len, get_cname_target(), LDNS_RR_TYPE_CNAME, query_info::qclass, query_info::qname, query_info::qname_len, query_dname_compare(), ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, reply_info::rrsets, and packed_rrset_key::type.

Referenced by libworker_enter_result().

int reply_check_cname_chain ( struct reply_info rep)

Check if cname chain in cached reply is still valid.

Parameters
repreply to check.
Returns
: true if valid, false if invalid.

References reply_info::an_numrrsets, packed_rrset_key::dname, packed_rrset_key::dname_len, get_cname_target(), LDNS_RR_TYPE_CNAME, LDNS_RR_TYPE_DNAME, query_dname_compare(), ub_packed_rrset_key::rk, reply_info::rrsets, and packed_rrset_key::type.

Referenced by answer_from_cache(), and tomsg().

int reply_all_rrsets_secure ( struct reply_info rep)

Check security status of all RRs in the message.

Parameters
repreply to check
Returns
: true if all RRs are secure. False if not. True if there are zero RRs.

References lruhash_entry::data, ub_packed_rrset_key::entry, reply_info::rrset_count, reply_info::rrsets, and sec_status_secure.

Referenced by answer_from_cache(), and tomsg().

struct ub_packed_rrset_key* reply_find_answer_rrset ( struct query_info qinfo,
struct reply_info rep 
)

Find answer rrset in reply, the one matching qinfo.

Follows CNAMEs, so the result may have a different owner name.

Parameters
qinfowhat to look for.
replooks in answer section of this message.
Returns
: pointer to rrset, or NULL if not found.

References reply_info::an_numrrsets, packed_rrset_key::dname, packed_rrset_key::dname_len, get_cname_target(), LDNS_RR_TYPE_CNAME, query_info::qclass, query_info::qname, query_info::qname_len, query_info::qtype, query_dname_compare(), ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, reply_info::rrsets, and packed_rrset_key::type.

Referenced by dns64_adjust_ptr(), dns64_inform_super(), ds_response_to_ke(), handle_event_moddone(), libworker_enter_result(), process_dnskey_response(), and processTargetResponse().

struct ub_packed_rrset_key* reply_find_rrset_section_an ( struct reply_info rep,
uint8_t *  name,
size_t  namelen,
uint16_t  type,
uint16_t  dclass 
)

Find rrset in reply, inside the answer section.

Does not follow CNAMEs.

Parameters
replooks in answer section of this message.
namewhat to look for.
namelenlength of name.
typelooks for (host order).
dclasslooks for (host order).
Returns
: pointer to rrset, or NULL if not found.

References reply_info::an_numrrsets, packed_rrset_key::dname, packed_rrset_key::dname_len, query_dname_compare(), ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, reply_info::rrsets, and packed_rrset_key::type.

Referenced by ds_response_to_ke(), iter_msg_from_zone(), process_prime_response(), and processQueryResponse().

struct ub_packed_rrset_key* reply_find_rrset_section_ns ( struct reply_info rep,
uint8_t *  name,
size_t  namelen,
uint16_t  type,
uint16_t  dclass 
)

Find rrset in reply, inside the authority section.

Does not follow CNAMEs.

Parameters
replooks in authority section of this message.
namewhat to look for.
namelenlength of name.
typelooks for (host order).
dclasslooks for (host order).
Returns
: pointer to rrset, or NULL if not found.

References reply_info::an_numrrsets, packed_rrset_key::dname, packed_rrset_key::dname_len, reply_info::ns_numrrsets, query_dname_compare(), ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, reply_info::rrsets, and packed_rrset_key::type.

Referenced by iter_indicates_dnssec(), iter_msg_from_zone(), processQueryResponse(), and val_nsec_prove_nodata_dsreply().

struct ub_packed_rrset_key* reply_find_rrset ( struct reply_info rep,
uint8_t *  name,
size_t  namelen,
uint16_t  type,
uint16_t  dclass 
)

Find rrset in reply, inside any section.

Does not follow CNAMEs.

Parameters
replooks in answer,authority and additional section of this message.
namewhat to look for.
namelenlength of name.
typelooks for (host order).
dclasslooks for (host order).
Returns
: pointer to rrset, or NULL if not found.

References packed_rrset_key::dname, packed_rrset_key::dname_len, query_dname_compare(), ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, reply_info::rrset_count, reply_info::rrsets, and packed_rrset_key::type.

Referenced by processDSNSResponse(), and processQueryResponse().

void log_dns_msg ( const char *  str,
struct query_info qinfo,
struct reply_info rep 
)
void log_query_info ( enum verbosity_value  v,
const char *  str,
struct query_info qinf 
)