Net::Bind::Resolv - a class to munge /etc/resolv.conf data. |
new([$filename])
read_from_string($string)
read_from_file($filename)
domain([$domain])
nameservers([@values])
searchlist([@values])
sortlist([@values])
options([@values])
comments([@strings])
print($fh)
check([$policy])
Net::Bind::Resolv - a class to munge /etc/resolv.conf data.
use Net::Bind::Resolv;
This class provides an object oriented perl interface to
/etc/resolv.conf
data.
Here is an example snippet of code:
use Net::Bind::Resolv; my $res = new Net::Bind::Resolv; print $res->domain, "\n";
Or how about:
use Net::Bind::Resolv; use IO::File; my $res = new Net::Bind::Resolv ''; $res->comment("Programmatically generated\nDo not edit by hand"); $res->domain('arf.fz'); $res->nameservers('0.0.0.0'); $res->options('debug'); print $res->as_string;
new([$filename])
Returns a reference to a new Net::Bind::Resolv
object. If
no $filename
is supplied, /etc/resolv.conf is used as a default.
A non-empty $filename
will be passed to read_from_file
.
read_from_string($string)
Populates the object with the parsed contents of $string
. Returns
1
if no errors were encounters, otherwise it returns 0
.
The following directives are understood.
search
directive and domain directive are found in the same
file, the last one encountered will be recorded and all previous ones
will be ignored.
nameserver
directive will cause the given
IP_ADDR
to be remembered.
There are very few requirements placed on the data in $string
.
Multiple entries of certain directives, while technically incorrect,
will cause the last occurrence of the given directive to be the one
remembered. If there is sufficient precedence for this to be
otherwise, let me know.
There is no requirement for the arguments to the directives to be valid pieces of data. That job is delagated to local policy methods to be applied against the object.
read_from_file($filename)
Populates the object with the parsed contents of $filename
. This
really just a wrapper around read_from_string
. Returns 0
if
errors were encountered, otherwise it returns 1
.
Zeros out the internal data in the object. This needs to be done if
multiple read_from_string
methods are called on a given
Net::Bind::Resolv
object and you do not want to retain the previous
values in the object.
domain([$domain])
Returns the value of the domain
directive. If $domain
is
specified, then set the domain to the given value and the
searchlist
, if defined in the object, is undefined.
nameservers([@values])
Returns (in order) the list of nameserver
entries. If called in an
array context it returns an array, otherwise it returns an array
reference.
If @values
is specified, then set the nameserver list to the given
values. Any items in @values
that are list references are
dereferences as they are added.
searchlist([@values])
Returns an array reference containing the items for the search
directive. If called in an array context it returns an array,
otherwise it returns an array reference.
If a list of values is specified, then set the searchlist to those
values and the domain
, if defined in the object, is undefined. Any
items in @values
that are list references are dereferenced as they
are added.
sortlist([@values])
Returns an array reference containing the items for the sortlist
directive. If called in an array context it returns an array,
otherwise it returns an array reference.
If a list of values is specified, then set the sortlist to those
values. Any items in @values
that are list references are
dereferenced as they are added.
options([@values])
Returns the items for the options
directive. If called in an array
context it returns an array, otherwise it returns an array reference.
If a list of values is specified, then set the options to those
values. Any items in @values
that are list references are
dereferenced as they are added.
comments([@strings])
Returns the comments for the object. If called in an array context it returns an array, otherwise it returns an array reference.
If a list of strings is specified, then set the comments to those
values after splitting the items on a NEWLINE
boundary. This
allows several combinations of arrays, array refs, or strings with
embedded newlines to be specified. There is no need to prefix any of
the comment lines with a comment character ([;\#]
); the
as_string
automagically commentifies (:-) the comment strings.
Any items in @strings
that are list references are dereferenced as
they are added.
Returns a string representing the contents of the object.
Technically, this string could be used to populate a resolv.conf
file, but use print
for that. The <print> method is a wrapper
around this method. The data is generated in the following order:
comments domain (mutually exclusive with search) search (mutually exclusive with domain) nameservers (one line for each nameserver entry) sortlist options
print($fh)
A wrapper around as_string
that prints a valid resolver(5)
representation of the data in the object to the given filehandle.
check([$policy])
Performs a policy/validity check of the data contained in the object
using the given subroutine &policy
. The given $policy
routine
is called as &$policy($self)
. If $policy
is not given it
defaults to using default_policy_check
. It returns the return
status of the policy check routine.
A simple wrapper around various check_*
methods.
Returns 1
if the domain member of the object is defined and is a
valid rfc1035 domain name, otherwise returns 0
.
Returns 1
if the searchlist member of the object is defined and
contains only valid rfc1035 domain names, otherwise returns 0
.
Returns 1
if the nameservers member of the object is defined and
contains only ip-addresses, otherwise returns 0
.
Uses valid_ip
to do the real work.
Returns 1
if the sortlist member of the object is defined and
contains only ip-address/netmasks, otherwise returns 0
.
Uses valid_netmask
to do the real work.
Returns 1
if the options member of the object is empty or contains
only valid options, otherwise returns 0
.
Currently recognized options are:
Returns the quantity of nameserver entries present.
The read_from_{file|string}
methods and the print
method are not
isomorphic. Given an arbitrary file or string which is read in, the
output of print
is not guaranteed to be an exact duplicate of the
original file. In the special case of files that are generated with
this module, the results will be isomorphic, assuming no modifications
were made to the data between when it was read in and subsequently
written back out.
Since Net::Bind::Resolv does not impose many requirements on the values
of the various directives present in a /etc/resolv.conf
file, it is
important to apply the appropriate policy methods against the object
before writing it to a file that will be used by the resolver.
Consider yourself warned!
Kevin Johnson <kjj@pobox.com> Rob Brown <rob@roobik.com>
Copyright (c) 1997 Kevin Johnson <kjj@pobox.com>. Copyright (c) 2001 Rob Brown <rob@roobik.com>.
All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Net::Bind::Resolv - a class to munge /etc/resolv.conf data. |