21 #include <openssl/rand.h>
28 #define LDNS_EDNS_MASK_DO_BIT 0x8000
29 #define LDNS_EDNS_MASK_UNASSIGNED (0xFFFF & ~LDNS_EDNS_MASK_DO_BIT)
185 return packet->
_size;
203 return packet->timestamp;
601 packet->
timestamp.tv_sec = timeval.tv_sec;
602 packet->
timestamp.tv_usec = timeval.tv_usec;
789 size_t size = ldns_read_uint16(&wire[pos+2]);
792 if (pos + size > max) {
802 memcpy(data, &wire[pos], size);
1021 ldns_pkt_query_new_frm_str_internal(
ldns_pkt **p,
const char *name,
1048 if (rr_class == 0) {
1084 return ldns_pkt_query_new_frm_str_internal(p, name, rr_type,
1085 rr_class, flags, NULL);
1096 authsoa_rr = ldns_pkt_authsoa(name_rdf, rr_class);
1101 rr_class, flags, authsoa_rr);
1129 if (rr_class == 0) {
1151 return ldns_pkt_query_new_internal(rr_name, rr_type,
1152 rr_class, flags, NULL);
1161 authsoa_rr = ldns_pkt_authsoa(rr_name, rr_class);
1164 rr_class, flags, authsoa_rr);
int ldns_dname_compare(const ldns_rdf *dname1, const ldns_rdf *dname2)
Compares the two dname rdf's according to the algorithm for ordering in RFC4034 Section 6.
bool ldns_edns_option_list_push(ldns_edns_option_list *option_list, ldns_edns_option *option)
adds an EDNS option at the end of the list of options.
ldns_edns_option_list * ldns_edns_option_list_clone(ldns_edns_option_list *old_list)
clone the EDNS options list and it's contents
void ldns_edns_option_list_deep_free(ldns_edns_option_list *option_list)
ldns_edns_option_list * ldns_edns_option_list_new()
allocates space for a new list of EDNS options
ldns_edns_option * ldns_edns_new(ldns_edns_option_code code, size_t size, void *data)
allocates a new EDNS structure and fills it.
enum ldns_enum_edns_option ldns_edns_option_code
enum ldns_enum_status ldns_status
Including this file will include all ldns files, and define some lookup tables.
ldns_pkt * ldns_pkt_clone(const ldns_pkt *pkt)
clones the given packet, creating a fully allocated copy
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
uint8_t ldns_pkt_edns_version(const ldns_pkt *packet)
return the packet's edns version
void ldns_pkt_set_tc(ldns_pkt *packet, bool tc)
Set the packet's tc bit.
void ldns_pkt_set_ancount(ldns_pkt *packet, uint16_t ancount)
Set the packet's an count.
void ldns_pkt_set_rd(ldns_pkt *packet, bool rd)
Set the packet's rd bit.
ldns_rdf * ldns_pkt_answerfrom(const ldns_pkt *packet)
Return the packet's answerfrom.
void ldns_pkt_set_querytime(ldns_pkt *packet, uint32_t time)
Set the packet's query time.
void ldns_pkt_set_tsig(ldns_pkt *pkt, ldns_rr *rr)
Set the packet's tsig rr.
ldns_pkt_opcode ldns_pkt_get_opcode(const ldns_pkt *packet)
Read the packet's code.
ldns_pkt * ldns_pkt_ixfr_request_new(ldns_rdf *rr_name, ldns_rr_class rr_class, uint16_t flags, ldns_rr *soa)
creates an IXFR request packet for the given name, type and class.
void ldns_pkt_set_edns_udp_size(ldns_pkt *packet, uint16_t s)
Set the packet's edns udp size.
void ldns_pkt_set_opcode(ldns_pkt *packet, ldns_pkt_opcode opcode)
Set the packet's opcode.
void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr)
directly set the additional section
void ldns_pkt_set_random_id(ldns_pkt *packet)
Set the packet's id to a random value.
void ldns_pkt_set_edns_option_list(ldns_pkt *packet, ldns_edns_option_list *list)
Set the packet's structured EDNS data.
ldns_rr_list * ldns_pkt_question(const ldns_pkt *packet)
Return the packet's question section.
void ldns_pkt_set_aa(ldns_pkt *packet, bool aa)
Set the packet's aa bit.
ldns_edns_option_list * pkt_edns_data2edns_option_list(const ldns_rdf *edns_data)
uint16_t ldns_pkt_id(const ldns_pkt *packet)
Read the packet id.
uint16_t ldns_pkt_arcount(const ldns_pkt *packet)
Return the packet's ar count.
#define LDNS_EDNS_MASK_UNASSIGNED
struct timeval ldns_pkt_timestamp(const ldns_pkt *packet)
Return the packet's timestamp.
bool ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr)
push an rr on a packet
void ldns_pkt_set_nscount(ldns_pkt *packet, uint16_t nscount)
Set the packet's ns count.
void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr)
directly set the authority section
bool ldns_pkt_safe_push_rr_list(ldns_pkt *p, ldns_pkt_section s, ldns_rr_list *list)
push an rr_list to a packet, provided the RRs are not already there.
void ldns_pkt_set_timestamp(ldns_pkt *packet, struct timeval timeval)
Set the packet's timestamp.
bool ldns_pkt_qr(const ldns_pkt *packet)
Read the packet's qr bit.
ldns_rr_list * ldns_pkt_rr_list_by_name(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_pkt_section sec)
return all the rr with a specific name from a packet.
void ldns_pkt_set_size(ldns_pkt *packet, size_t s)
Set the packet's size.
bool ldns_pkt_push_rr_list(ldns_pkt *p, ldns_pkt_section s, ldns_rr_list *list)
push a rr_list on a packet
ldns_pkt * ldns_pkt_new(void)
allocates and initializes a ldns_pkt structure.
void ldns_pkt_set_ad(ldns_pkt *packet, bool ad)
Set the packet's ad bit.
bool ldns_pkt_empty(ldns_pkt *p)
check if a packet is empty
#define LDNS_EDNS_MASK_DO_BIT
bool ldns_pkt_edns(const ldns_pkt *pkt)
returns true if this packet needs and EDNS rr to be sent.
ldns_rr_list * ldns_pkt_authority(const ldns_pkt *packet)
Return the packet's authority section.
uint16_t ldns_pkt_edns_unassigned(const ldns_pkt *packet)
return the packet's EDNS header bits that are unassigned.
void ldns_pkt_set_cd(ldns_pkt *packet, bool cd)
Set the packet's cd bit.
uint16_t ldns_pkt_ancount(const ldns_pkt *packet)
Return the packet's an count.
ldns_rr_list * ldns_pkt_rr_list_by_name_and_type(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_rr_type type, ldns_pkt_section sec)
return all the rr with a specific type and type from a packet.
void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr)
directly set the answer section
void ldns_pkt_set_question(ldns_pkt *p, ldns_rr_list *rr)
directly set the question section
bool ldns_pkt_rr(const ldns_pkt *pkt, ldns_pkt_section sec, const ldns_rr *rr)
check to see if an rr exist in the packet
void ldns_pkt_set_edns_do(ldns_pkt *packet, bool value)
Set the packet's edns do bit.
bool ldns_pkt_cd(const ldns_pkt *packet)
Read the packet's cd bit.
void ldns_pkt_set_qdcount(ldns_pkt *packet, uint16_t qdcount)
Set the packet's qd count.
ldns_rr_list * ldns_pkt_all_noquestion(const ldns_pkt *packet)
Return the packet's answer, authority and additional sections concatenated, in a new rr_list clone.
ldns_rr_list * ldns_pkt_additional(const ldns_pkt *packet)
Return the packet's additional section.
bool ldns_pkt_safe_push_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr)
push an rr on a packet, provided the RR is not there.
size_t ldns_pkt_size(const ldns_pkt *packet)
Return the packet's size in bytes.
void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z)
Set the packet's edns z value.
bool ldns_pkt_ad(const ldns_pkt *packet)
Read the packet's ad bit.
uint32_t ldns_pkt_querytime(const ldns_pkt *packet)
Return the packet's querytime.
bool ldns_pkt_rd(const ldns_pkt *packet)
Read the packet's rd bit.
uint8_t ldns_pkt_edns_extended_rcode(const ldns_pkt *packet)
return the packet's edns extended rcode
uint16_t ldns_pkt_section_count(const ldns_pkt *packet, ldns_pkt_section s)
Return the number of RRs in the given section.
void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data)
Set the packet's EDNS data.
ldns_pkt_type ldns_pkt_reply_type(const ldns_pkt *p)
looks inside the packet to determine what kind of packet it is, AUTH, NXDOMAIN, REFERRAL,...
bool ldns_pkt_ra(const ldns_pkt *packet)
Read the packet's ra bit.
ldns_rr_list * ldns_pkt_answer(const ldns_pkt *packet)
Return the packet's answer section.
bool ldns_pkt_set_flags(ldns_pkt *packet, uint16_t flags)
sets the flags in a packet.
void ldns_pkt_set_ra(ldns_pkt *packet, bool ra)
Set the packet's ra bit.
uint16_t ldns_pkt_nscount(const ldns_pkt *packet)
Return the packet's ns count.
ldns_rr_list * ldns_pkt_all(const ldns_pkt *packet)
Return the packet's question, answer, authority and additional sections concatenated,...
ldns_rr_list * ldns_pkt_get_section_clone(const ldns_pkt *packet, ldns_pkt_section s)
return all the rr_list's in the packet.
void ldns_pkt_set_id(ldns_pkt *packet, uint16_t id)
Set the packet's id.
uint16_t ldns_pkt_qdcount(const ldns_pkt *packet)
Return the packet's qd count.
ldns_lookup_table ldns_edns_flags[]
EDNS flags.
void ldns_pkt_set_edns_extended_rcode(ldns_pkt *packet, uint8_t c)
Set the packet's edns extended rcode.
ldns_rdf * ldns_pkt_edns_data(const ldns_pkt *packet)
return the packet's EDNS data
ldns_status ldns_pkt_ixfr_request_new_frm_str(ldns_pkt **p, const char *name, ldns_rr_class rr_class, uint16_t flags, ldns_rr *soa)
creates an IXFR request packet for the given name, class.
ldns_rr * ldns_pkt_tsig(const ldns_pkt *pkt)
Return the packet's tsig pseudo rr's.
bool ldns_pkt_edns_do(const ldns_pkt *packet)
return the packet's edns do bit
void ldns_pkt_set_answerfrom(ldns_pkt *packet, ldns_rdf *answerfrom)
Set the packet's answering server.
void ldns_pkt_set_section_count(ldns_pkt *packet, ldns_pkt_section s, uint16_t count)
Set a packet's section count to x.
void ldns_pkt_set_qr(ldns_pkt *packet, bool qr)
Set the packet's qr bit.
ldns_status ldns_pkt_query_new_frm_str(ldns_pkt **p, const char *name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
creates a query packet for the given name, type, class.
uint16_t ldns_pkt_edns_z(const ldns_pkt *packet)
return the packet's edns z value
bool ldns_pkt_aa(const ldns_pkt *packet)
Read the packet's aa bit.
ldns_edns_option_list * ldns_pkt_edns_get_option_list(ldns_pkt *packet)
Returns a list of structured EDNS options.
void ldns_pkt_set_arcount(ldns_pkt *packet, uint16_t arcount)
Set the packet's arcount.
ldns_pkt_rcode ldns_pkt_get_rcode(const ldns_pkt *packet)
Return the packet's response code.
void ldns_pkt_set_rcode(ldns_pkt *packet, uint8_t rcode)
Set the packet's response code.
bool ldns_pkt_tc(const ldns_pkt *packet)
Read the packet's tc bit.
ldns_pkt * ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
creates a packet with a query in it for the given name, type and class.
uint16_t ldns_pkt_edns_udp_size(const ldns_pkt *packet)
return the packet's edns udp size
void ldns_pkt_set_edns_version(ldns_pkt *packet, uint8_t v)
Set the packet's edns version.
void ldns_pkt_set_edns_unassigned(ldns_pkt *packet, uint16_t value)
Set the packet's EDNS header bits that are unassigned.
ldns_rr_list * ldns_pkt_rr_list_by_type(const ldns_pkt *packet, ldns_rr_type type, ldns_pkt_section sec)
return all the rr with a specific type from a packet.
enum ldns_enum_pkt_type ldns_pkt_type
enum ldns_enum_pkt_rcode ldns_pkt_rcode
enum ldns_enum_pkt_opcode ldns_pkt_opcode
enum ldns_enum_pkt_section ldns_pkt_section
@ LDNS_SECTION_ANY
bogus section, if not interested
@ LDNS_SECTION_ADDITIONAL
@ LDNS_SECTION_ANY_NOQUESTION
used to get all non-question rrs from a packet
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
uint8_t * ldns_rdf_data(const ldns_rdf *rd)
returns the data of the rdf.
ldns_rdf * ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value)
returns an rdf that contains the given int32 value.
void ldns_rdf_free(ldns_rdf *rd)
frees a rdf structure, leaving the data pointer intact.
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
@ LDNS_RDF_TYPE_INT32
32 bits
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
ldns_rr * ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
returns a specific rr of an rrlist.
ldns_rdf * ldns_rr_owner(const ldns_rr *rr)
returns the owner name of an rr structure.
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
void ldns_rr_set_owner(ldns_rr *rr, ldns_rdf *owner)
sets the owner in the rr structure.
void ldns_rr_set_type(ldns_rr *rr, ldns_rr_type rr_type)
sets the type in the rr.
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr's in an rr_list.
void ldns_rr_set_question(ldns_rr *rr, bool question)
sets the question flag in the rr structure.
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
bool ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
ldns_rr_list * ldns_rr_list_cat_clone(const ldns_rr_list *left, const ldns_rr_list *right)
concatenates two ldns_rr_lists together, but makes clones of the rr's (instead of pointer copying).
bool ldns_rr_list_contains_rr(const ldns_rr_list *rr_list, const ldns_rr *rr)
returns true if the given rr is one of the rrs in the list, or if it is equal to one
void ldns_rr_set_class(ldns_rr *rr, ldns_rr_class rr_class)
sets the class in the rr.
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
bool ldns_rr_push_rdf(ldns_rr *rr, const ldns_rdf *f)
sets rd_field member, it will be placed in the next available spot.
ldns_rr * ldns_rr_clone(const ldns_rr *rr)
clones a rr and all its data
ldns_rr_list * ldns_rr_list_clone(const ldns_rr_list *rrlist)
clones an rrlist.
ldns_rr * ldns_rr_new(void)
creates a new rr structure.
enum ldns_enum_rr_type ldns_rr_type
@ LDNS_RR_TYPE_A
a host address
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
@ LDNS_RR_TYPE_NS
an authoritative name server
enum ldns_enum_rr_class ldns_rr_class
@ LDNS_RR_CLASS_IN
the Internet
ldns_status ldns_str2rdf_dname(ldns_rdf **d, const char *str)
convert a dname string into wireformat
The struct that stores an ordered EDNS option.
uint16_t _id
Id of a packet.
ldns_pkt_opcode _opcode
Query type.
uint16_t _ancount
answer sec
bool _rd
Recursion desired.
uint16_t _qdcount
question sec
bool _cd
Checking disabled.
bool _aa
Authoritative answer.
uint8_t _rcode
Response code.
bool _qr
Query bit (0=query, 1=answer)
uint16_t _nscount
auth sec
bool _tc
Packet truncated.
bool _ra
Recursion available.
A general purpose lookup table.
struct timeval timestamp
Timestamp of the time the packet was sent or created.
uint8_t _edns_extended_rcode
EDNS0 Extended rcode.
ldns_rr_list * _authority
Authority section.
ldns_hdr * _header
Header section.
uint32_t _querytime
The duration of the query this packet is an answer to.
uint16_t _edns_udp_size
EDNS0 available buffer size, see RFC2671.
ldns_rdf * _answerfrom
an rdf (A or AAAA) with the IP address of the server it is from
uint8_t _edns_version
EDNS Version.
size_t _size
The size of the wire format of the packet in octets.
ldns_rr_list * _answer
Answer section.
ldns_rdf * _edns_data
Arbitrary EDNS rdata.
ldns_edns_option_list * _edns_list
Structed EDNS data.
uint16_t _edns_z
Reserved EDNS data bits.
ldns_rr_list * _additional
Additional section.
ldns_rr * _tsig_rr
Optional tsig rr.
ldns_rr_list * _question
Question section.
Resource record data field.
List or Set of Resource Records.
uint16_t ldns_get_random(void)
Get random number.
#define LDNS_MALLOC(type)
Memory management macros.
#define LDNS_XMALLOC(type, count)