[ldns-users] ldns perl wrapper

Henri Asseily henri at asseily.com
Fri May 17 07:27:32 UTC 2013

Hello Erik,
this is looking good. Clearly it's a pure low level API (i.e. a "thin" wrapper to the C API), but from there it becomes easy to create a more perl-ish API. For example, the creation of an RR shouldn't be as involved as:

my $rr1 = new Net::LDNS::RR(
    type => LDNS_RR_TYPE_SOA,
    class => LDNS_RR_CLASS_CH,
    ttl => 1234,
    owner => new Net::LDNS::RData(LDNS_RDF_TYPE_DNAME, 'myzone.org'),
    rdata => [
	new Net::LDNS::RData(LDNS_RDF_TYPE_DNAME, 'hostmaster.myzone.org'),
	new Net::LDNS::RData(LDNS_RDF_TYPE_DNAME, 'master.myzone.org'),
	new Net::LDNS::RData(LDNS_RDF_TYPE_INT32, '2012113030'),
	new Net::LDNS::RData(LDNS_RDF_TYPE_PERIOD, '12345'),
	new Net::LDNS::RData(LDNS_RDF_TYPE_PERIOD, '1827'),
	new Net::LDNS::RData(LDNS_RDF_TYPE_PERIOD, '2345678'),
	new Net::LDNS::RData(LDNS_RDF_TYPE_PERIOD, '87654')

For starters, I would create a map for standard RR types that would know the content (and types) of the rdata array, or create new RR type classes. So one would be able to do:

my $rr1 = new Net::LDNS::RR::SOA(
    ttl => 1234,
    owner => 'myzone.org',
    serial => 2012113030,
    refresh => 12345,
    update => 1827,
    expiry => 2345678,
    minimum => 87654,

with of course default values where it makes sense.

Again, this can (and should) all be done in pure perl, now that the low level interface is there.

Regarding the garbage collection, you should indeed be leveraging the Perl GC. There shouldn't be a concept of an explicit owner in Perl if one can avoid it.
You shouldn't have to worry about higher-ups "owning" via the C API lower level objects (i.e. RR owning RDATAs) since the RDATAs will be linked to the RR through the Perl layer anyway (RDATAs being array members of RRs).
All you should worry about is that when destroy'ing the objects that you keep doing what you do:


(but see below)

All the deep_free stuff is useless here, since the Perl GC will call DESTROY on every object that is refcounted to zero anyway, so all RDATAs for example will be DESTROYed in turn when an RR is destroyed, assuming the RDATA object only exists in one RR.

I think you should be able to simply get totally rid of all your GC since XS will add xx_newmortal() in the generated code for your object constructors. That ensures that Perl "owns" the generated object and manages the refcounts.

Now there's another thing you can do as well, which should make your code cleaner: you can get rid of your "sub DESTROY" in each of the Perl packages by putting the DESTROY code in the XS file since it all just calls the C destruction code.

Say you have:


Replace it with:


and then you add:

      ldns_rr *rr

I hope that what I wrote above doesn't confuse more than it helps.

Henri Asseily

"For me, it is far better to grasp the Universe as it really is than to persist in delusion, however satisfying and reassuring." 
     —Carl Sagan

On May 13, 2013, at 2:59 PM, "Erik P. Ostlyngen" <erik.ostlyngen at uninett.no> wrote:

> Hi,
> I have created a perl xs wrapper for the ldns library which might be
> useful for other programmers. My motivation for creating another DNS
> library for perl was that I needed a tool for doing zonefile analysis
> and dnssec with better performance than Net::DNS (which is otherwise
> the preferred library).
> https://github.com/erikoest/Net-LDNS
> Comments are welcome. Please be aware that it is still beta. The
> object freeing and reference counting is a bit involved. I wonder if
> it could have been done in a simpler way, e.g. using the built-in perl gc.
> If you find it useful, you may include it in the ldns source package.
> Regards,
> Erik Østlyngen
> _______________________________________________
> ldns-users mailing list
> ldns-users at open.nlnetlabs.nl
> http://open.nlnetlabs.nl/mailman/listinfo/ldns-users

More information about the ldns-users mailing list