DNS-Unbound

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


    $dns->set_option( verbosity => 1 + $verbosity );

Synchronous queries:

    my $res_hr = $dns->resolve( 'cpan.org', 'NS' );

    # See below about encodings in “data”.
    my @ns = map { $dns->decode_name($_) } @{ $res_hr->data() };

Asynchronous queries use [the “Promise” pattern](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). Assuming you’re using
an off-the-shelf event loop, you can do something like:

    my $dns = DNS::Unbound::AnyEvent->new();

    my $query1 = $dns->resolve_async( 'usa.gov', 'A' )->then(
        sub { my $data = shift()->data(); ... },  # success handler
        sub { ... },                              # failure handler
    );

    my $query2 = $dns->resolve_async( 'in-addr.arpa', 'NS' )->then(
        sub { ... },
        sub { ... },
    );

You can also integrate with a custom event loop; see ["EVENT LOOPS"](#event-loops) below.

# DESCRIPTION

Typical DNS lookups involve a request to a local server that caches
information from DNS. The caching makes it fast, but it also means
updates to DNS aren’t always available via that local server right away.
Most applications don’t need to care and so can enjoy the speed of
cached results.

Applications that need up-to-date DNS query results, though, need
_fully-recursive_ DNS queries. NLnet Labs’s
[libunbound](https://www.nlnetlabs.nl/documentation/unbound/libunbound/)
is a popular solution for such queries; the present Perl module is an
interface to that library.

# CHARACTER ENCODING

DNS doesn’t know about character encodings, so neither does Unbound.
Thus, all strings given to this module must be **byte** **strings**.
All returned strings will be byte strings as well.

# EVENT LOOPS

This distribution includes the classes [DNS::Unbound::AnyEvent](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AAnyEvent),
[DNS::Unbound::IOAsync](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AIOAsync), and [DNS::Unbound::Mojo](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AMojo), which provide
out-of-the-box compatibility with those popular event loop interfaces.
You should probably use one of these.

You can also integrate with a custom event loop via the `fd()` method
of this class: wait for that file descriptor to be readable, then
call this class’s `perform()` method.

# MEMORY LEAK DETECTION

Objects in this namespace will, if left alive at global destruction,
throw a warning about memory leaks. To silence these warnings, either
allow all queries to complete, or cancel queries you no longer care about.

# ERRORS

This library throws 3 kinds of errors:

- Plain strings. Generally thrown in “simple” failure cases,
e.g., invalid inputs.
- [DNS::Unbound::X::Unbound](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AX%3A%3AUnbound) instances. Thrown whenever
Unbound gives an error.
- [DNS::Unbound::X::ResolveError](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AX%3A%3AResolveError) instances. A subclass
of the last kind, for (Unbound-reported) resolution failures.
(This is **NOT** for DNS-reported failures.)

# CONSTANTS

The following from `libunbound/context.h` are defined here:
`UB_NOERROR`, `UB_SOCKET`, `UB_NOMEM`, `UB_SYNTAX`, `UB_SERVFAIL`,
`UB_FORKFAIL`, `UB_AFTERFINAL`, `UB_INITFAIL`, `UB_PIPE`,
`UB_READFILE`, `UB_NOID`

# METHODS

## _CLASS_->new()

Instantiates this class.

## $result\_obj = _OBJ_->resolve( $NAME, $TYPE \[, $CLASS \] )

Runs a synchronous query for a given $NAME and $TYPE. $TYPE may be
expressed numerically or, for convenience, as a string. $CLASS is
optional and defaults to 1 (`IN`), which is probably what you want.

Returns a [DNS::Unbound::Result](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AResult) instance.

**NOTE:** libunbound doesn’t seem to offer effective controls for
timing out a synchronous query.
If timeouts are relevant for you, you probably need
to use `resolve_async()` instead.

## $query\_obj = _OBJ_->resolve\_async( $NAME, $TYPE \[, $CLASS \] );

Like `resolve()` but starts an asynchronous query rather than a
synchronous one.

This returns an instance of [DNS::Unbound::AsyncQuery](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AAsyncQuery) (a subclass
thereof, to be precise).

If you’re using one of the special event interface subclasses
(e.g., [DNS::Unbound::IOAsync](https://metacpan.org/pod/DNS%3A%3AUnbound%3A%3AIOAsync)) then the returned promise will resolve
as part of the event loop’s normal operation. Otherwise,
[see below](#custom-event-loop-integration) for the methods you’ll need
to use in tandem with this one to get your query result.

## _OBJ_->enable\_threads()

Sets _OBJ_’s asynchronous queries to use threads rather than forking.
Off by default. Throws an exception if called after an asynchronous query has
already been sent.



( run in 1.749 second using v1.01-cache-2.11-cpan-98e64b0badf )