Maintained by: NLnet Labs

Trying to fetch SRV data with libunbound / libldns

Willem Toorop
Thu May 12 16:15:29 CEST 2016


Rick,

You can parse the packet returned by ub_result.answer_packet with
ldns_wire2pkt().  For example when res is of type ub_result *, then

if ((s = ldns_wire2pkt(&pkt, res->answer_packet, res->answer_len)))
	/* handle error */

else if (!(rrs = ldns_pkt_answer(pkt)))
	/* handle error */

else for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
	ldns_rr *rr = ldns_rr_list_rr(rrs, i);

	if (!rr || ldns_rr_get_type(rr) != LDNS_RR_TYPE_SRV)
		continue;

	uint16_t priority = ldns_rdf2native_int16(ldns_rr_rdf(rr, 0));
	uint16_t weight = ldns_rdf2native_int16(ldns_rr_rdf(rr, 1));
	uint16_t port = ldns_rdf2native_int16(ldns_rr_rdf(rr, 2));
	ldns_rdf *target = ldns_rr_rdf(rr, 3);

	/* Do stuff */
}

Not that it is not safe to use ldns_rdf2native_int16 directly on what
ldns_rr_rdf returns.  You should test for NULL first.  I just wrote it
so for clarity.

Cheers,

-- Willem

Op 12-05-16 om 15:51 schreef Rick van Rein via Unbound-users:
> Hello,
> 
> I'm trying to use libunbound (and possibly libldns) to pull out SRV
> records.  I am not certain how to proceed, based on the documentation
> that I could found in the form of man pages, tutorials and doxygen.
> 
> Am I correct that libunbound returns RDATA unmodified, and is basically
> concerned with the security chain but not the RDATA contents?  And so,
> that I have to turn to another tool?
> 
> I therefore looked at LDNS, but that does not seem to help either.  I
> don't mind following the RFC (though in the case of SRV it is not
> explicit on the wire format) to pull out data, but I would be surprised
> if I was to make assumptions about the structure of hosts, especially
> because it may point back into a DNS packet.  Is this really how it is
> done though?  Then, should I assume the data[i]/len[i] to be pointers
> into the frame, and compute offsets manually?
> 
> I can hardly believe that this hasn't been tried before :) but as I
> said, I can't find the documentation that answers this with clarity.  So
> any help is welcome, including pointers to documentation that I've
> overlooked!
> 
> Thanks,
>  -Rick
>