Dns Chilkat2 Perl Reference Documentation
Chilkat2::Dns
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Dns
Query common DNS record types and receive structured JSON results that
are easy to inspect or pass to other application logic.
Configure the nameservers Chilkat uses instead of relying only on the
operating system's default DNS configuration.
Prefer DNS-over-TLS when appropriate for applications that need encrypted
DNS lookups.
Control how long DNS queries may wait before failing, which is useful for
responsive applications and troubleshooting.
Nameserver settings affect DNS resolution across Chilkat classes, not
only the current
Use
For an extended overview, see
Dns Class Overview.
Configure DNS resolution and query DNS records directly from Chilkat.
Chilkat.Dns is a focused class for DNS configuration and DNS
record querying. It allows applications to control the nameservers used by
Chilkat, query common DNS record types into JSON, configure DNS-over-TLS
preference, set DNS query timeouts, and obtain useful diagnostics through
LastErrorText.
DNS record queries
Nameserver control
DNS-over-TLS preference
Timeout settings
Application-wide behavior
Dns object.
Diagnostics
LastErrorText to troubleshoot DNS configuration,
lookup failures, timeout problems, or nameserver connectivity issues.
Dns either to perform direct DNS lookups, or to configure
the DNS behavior used by Chilkat as a whole. Because nameserver changes can
affect DNS resolution across Chilkat classes, treat them as application-wide
configuration rather than object-local state.
Object Creation
use Chilkat2;
my $dns = Chilkat2::Dns->new;
# or set properties (and RaiseError) at creation:
# my $dns = Chilkat2::Dns->new(VerboseLogging => 1, RaiseError => 1);Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/Dns.pm file holds only this class's documentation (perldoc Chilkat2::Dns).
Perl frees the object when its last reference goes (undef $dns, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::Dns';): an object is a blessed hash reference whose hash is free for the subclass's own fields.
When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.
Errors
A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::Dns'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.
Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.
Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.
Properties
DebugLogFilePath
my $strVal = $dns->DebugLogFilePath;
$dns->DebugLogFilePath($strVal);
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
HeartbeatMs
my $intVal = $dns->HeartbeatMs;
$dns->HeartbeatMs($intVal);
The interval in milliseconds between each AbortCheck event callback, which enables an application to abort certain method calls before they complete. By default, HeartbeatMs is set to 0, meaning no AbortCheck event callbacks will trigger.
LastErrorHtml
my $strVal = $dns->LastErrorHtml;
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
my $strVal = $dns->LastErrorText;
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
my $strVal = $dns->LastErrorXml;
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
my $boolVal = $dns->LastMethodSuccess;
$dns->LastMethodSuccess($boolVal);
Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
MaxWaitMs
my $intVal = $dns->MaxWaitMs;
$dns->MaxWaitMs($intVal);
The maxiumum number of milliseconds to wait for the answer to a DNS query. A value of 0 is to wait indefinitely (no timeout). It's not a good idea to specify 0 (infinite timeout) or a value less than 1000 (1 second). The default value is 10000 milliseconds.
topNumNameservers
my $intVal = $dns->NumNameservers;
The number of nameservers currently being used by Chilkat for DNS queries in all objects/classes, such as for HTTP, Email, FTP, SSH, etc.
topTlsPref
my $intVal = $dns->TlsPref;
$dns->TlsPref($intVal);
Specifies the TLS preference. Possible values are:
- 0 = No TLS
- 1 = Prefer TLS if possible.
- 2 = Require TLS
The default value is 0. If TLS is preferred then DNS nameservers with TLS capability will be tried first. If none succeed, then UDP/TCP will be tried. If TLS is required, then only TLS capable nameservers will be tried and all DNS queries will use TLS.
Note: To use TLS, your application needs to first specify nameservers by calling AddNameserver once per nameserver with the supportsTls argument set to 1.
UncommonOptions
my $strVal = $dns->UncommonOptions;
$dns->UncommonOptions($strVal);
This is a catch-all property to be used for uncommon needs. This property defaults to the empty string and should typically remain empty.
topVerboseLogging
my $boolVal = $dns->VerboseLogging;
$dns->VerboseLogging($boolVal);
If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
my $strVal = $dns->Version;
Methods
AddDefaultNameservers
Adds the default (auto-assigned) nameservers to the collection of nameservers used by Chilkat for DNS queries and domain to IP address lookups.
Returns the object, so calls can be chained.
AddNameserver
# $supportsTls is a boolean
$dns->AddNameserver($ipAddr, $supportsTls);
Adds a new nameserver to the collection of nameservers used by Chilkat for DNS queries and domain to IP address lookups. The $ipAddr specifies the IPv4 address of the nameserver. The $supportsTls indicates if the namserver supports DoT (DNS over TLS. DoT uses port 853 for encrypted DNS communication.) If the TlsPreference property is set to 1 or 2, then Chilkat will use TLS nameservers.
Returns the object, so calls can be chained.
GetNameserver
# $str is a string or undef
my $str = $dns->GetNameserver($index);
Returns the IP address of the Nth nameserver currently used by Chilkat for DNS queries and domain to IP address lookups. The $index specifies the index of the nameserver. The 1st nameserver is at $index 0.
Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
Query
# $domain is a string
# $answer is a Chilkat2::JsonObject
# $flag is true or false
my $flag = $dns->Query($recordType, $domain, $answer);
Performs an DNS query for $domain and returns the answer in $answer. The type of DNS query is specified in $recordType and can be A, AAAA, ALIAS, CAA, MX, NS, SOA, TXT. Additional record types can be added as needed.
Never dies: false is an answer, not a failure.
RemoveAllNameservers
Removes all nameservers from the collection of nameservers used by Chilkat. An application can call this method to reset to 0 nameservers before adding new nameservers. If a DNS domain to IP address resolution is needed by Chilkat, then the auto-assigned nameserver IP addresses will be automaticaly restored.
Returns the object, so calls can be chained.
RemoveNameserver
$dns->RemoveNameserver($ipAddr);
Removes a nameserver from the collection of nameservers used by Chilkat. The $ipAddr is the IPv4 address of the nameserver to be removed.
Returns the object, so calls can be chained.
topEvents
All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, Chilkat2::Dns reports progress to handlers installed with on, which takes Event => CODE pairs (closures, so a handler can use the variables around it):
$dns->on(
PercentDone => sub { my ($pctDone) = @_; print "$pctDone%\n"; return 0 }, # a true return aborts
ProgressInfo => sub { my ($name, $value) = @_; print "$name: $value\n" },
);
$dns->HeartbeatMs(250); # the AbortCheck handler is called 4 times per secondInstalls the handlers given, replacing those installed before for the same events; undef instead of a CODE reference removes one, and $dns->on() with no arguments returns the installed handlers as a list of pairs. The event names are checked: a misspelled one croaks, and nothing is installed.
The AbortCheck handler is called at the interval set by the HeartbeatMs property (0, the default, disables it); PercentDone is called when an operation's completion percentage is known; ProgressInfo delivers named progress values. A true return from the AbortCheck or PercentDone handler aborts the running method, which then fails in the usual way (false or undef, or a Chilkat2::Error under RaiseError).
Handlers run during the method call, on the thread that called it. Inside a handler, other objects may be used, but calling a method of the same object (or its on) croaks. A die in a handler aborts the running method and is re-thrown from the method call; the object remains usable. A handler that uses $dns itself makes a reference cycle: weaken the captured copy (Scalar::Util) or remove the handler when done.
AbortCheck
$dns->on(AbortCheck => sub { ... });
Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.
Example
my $deadline = time() + 30; # give up after 30 seconds
$dns->HeartbeatMs(250); # call the handler 4 times per second
$dns->on(AbortCheck => sub { return time() > $deadline });PercentDone
$dns->on(PercentDone => sub { ... });
This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).
The PercentDone callback also acts as an AbortCheck event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.
To abort the operation, set the abort output argument to 1. This will cause the method to terminate and return a failure status or corresponding failure value.
Example
$dns->on(PercentDone => sub {
my ($pctDone) = @_; # 0 to 100
print "Percent done: $pctDone\n";
return 0; # return a true value to abort the method in progress
});ProgressInfo
$dns->on(ProgressInfo => sub { ... });
This event callback provides tag name/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.
Note: Some Chilkat methods don't fire any ProgressInfo events.
Example
$dns->on(ProgressInfo => sub {
my ($name, $value) = @_;
print "$name: $value\n";
});