Porting Net_Dig to Net_DNS

24 March 2004 • code | PHP • PermaLink

As of January 15, 2004, I’m no longer going to maintain the PEAR Net_Dig class. The original code was a bit of a hack anyway, simply running the system’s dig command, and parsing the results with regular expressions to come up with a useable PHP array.

The Net_DNS class is a much better alternative. It provides many more query options and, best of all, it’s being maintained. I encourage you all to switch.

If you need to port your applications from Net_Dig to Net_DNS, here is a quick primer on how it’s done. Let’s say your existing code looks like this:

<  php
require 'Net/Dig.php';

$D = new Net_Dig();
$result = $D->dig('viebrock.ca');

print_r($result);
  >

The way to do this using Net_DNS is very similar. To get the same functionality, your code should now look like this:

<  php
require 'Net/DNS.php';

$D = new Net_DNS();
$result = $D->resolver->query('viebrock.ca');

print_r($result);
  >

The output from the Net_Dig and Net_DNS scripts are listed below. As you can see, the output from Net_DNS is slightly different, but all the same information is there.

To check if the query was successful and, if so, to output the IP address, here is the Net_Dig way:

<  php
if ( $result->status != 'NOERROR' && $result->answer_count > 0 ) {
  echo
$result->answer[0]['data'];
}
  >

And this is the Net_DNS way:

<  php
if ( $result->header->rcode != 'NOERROR' && $result->header->ancount > 0 ) {
  echo
$result->answer[0]->address;
}
  >

For more information on using Net_DNS, you can check out the homepage of the package maintainer at http://www.ypass.net/software/php/Net_DNS/.

Output from Net_Dig method

net_dig_result Object ( [status] => NOERROR [id] => 54398 [flags] => qr rd ra [query_count] => 1 [answer_count] => 1 [authority_count] => 4 [additional_count] => 4 [dig_version] => 9.2.1 [dig_server] => 66.207.199.35 [dig_port] => 53 [query] => Array ( [0] => net_dig_resource Object ( [host] => viebrock.ca. [ttl] => [class] => IN [type] => A [data] => ) ) [answer] => Array ( [0] => net_dig_resource Object ( [host] => viebrock.ca. [ttl] => 120 [class] => IN [type] => A [data] => 209.61.155.107 ) ) [authority] => Array ( [0] => net_dig_resource Object ( [host] => viebrock.ca. [ttl] => 120 [class] => IN [type] => NS [data] => ns2.easydns.com. ) [1] => net_dig_resource Object ( [host] => viebrock.ca. [ttl] => 120 [class] => IN [type] => NS [data] => remote1.easydns.com. ) [2] => net_dig_resource Object ( [host] => viebrock.ca. [ttl] => 120 [class] => IN [type] => NS [data] => remote2.easydns.com. ) [3] => net_dig_resource Object ( [host] => viebrock.ca. [ttl] => 120 [class] => IN [type] => NS [data] => ns1.easydns.com. ) ) [additional] => Array ( [0] => net_dig_resource Object ( [host] => ns1.easydns.com. [ttl] => 3604 [class] => IN [type] => A [data] => 216.220.40.243 ) [1] => net_dig_resource Object ( [host] => ns2.easydns.com. [ttl] => 166281 [class] => IN [type] => A [data] => 205.210.42.20 ) [2] => net_dig_resource Object ( [host] => remote1.easydns.com. [ttl] => 82955 [class] => IN [type] => A [data] => 64.39.29.212 ) [3] => net_dig_resource Object ( [host] => remote2.easydns.com. [ttl] => 95573 [class] => IN [type] => A [data] => 212.100.224.80 ) ) [consistency_check] => 1 [query_time] => 117 msec )

Output from Net_DNS method

net_dns_packet Object ( [debug] => 0 [header] => net_dns_header Object ( [id] => 22447 [qr] => 1 [opcode] => QUERY [aa] => 0 [tc] => 0 [rd] => 1 [ra] => 1 [rcode] => NOERROR [qdcount] => 1 [ancount] => 1 [nscount] => 4 [arcount] => 4 ) [compnames] => Array ( ) [answerfrom] => 66.207.199.35 [answersize] => 200 [question] => Array ( [0] => net_dns_question Object ( [qname] => viebrock.ca [qtype] => A [qclass] => IN ) ) [answer] => Array ( [0] => net_dns_rr_a Object ( [name] => viebrock.ca [type] => A [class] => IN [ttl] => 120 [rdlength] => 4 [rdata] => <i>binary data</i> [address] => 209.61.155.107 ) ) [authority] => Array ( [0] => net_dns_rr_ns Object ( [name] => viebrock.ca [type] => NS [class] => IN [ttl] => 120 [rdlength] => 17 [rdata] => <i>binary data</i> [nsdname] => ns1.easydns.com ) [1] => net_dns_rr_ns Object ( [name] => viebrock.ca [type] => NS [class] => IN [ttl] => 120 [rdlength] => 6 [rdata] => <i>binary data</i> [nsdname] => ns2.easydns.com ) [2] => net_dns_rr_ns Object ( [name] => viebrock.ca [type] => NS [class] => IN [ttl] => 120 [rdlength] => 10 [rdata] => <i>binary data</i> [nsdname] => remote1.easydns.com ) [3] => net_dns_rr_ns Object ( [name] => viebrock.ca [type] => NS [class] => IN [ttl] => 120 [rdlength] => 10 [rdata] => <i>binary data</i> [nsdname] => remote2.easydns.com ) ) [additional] => Array ( [0] => net_dns_rr_a Object ( [name] => ns1.easydns.com [type] => A [class] => IN [ttl] => 3604 [rdlength] => 4 [rdata] => <i>binary data</i> [address] => 216.220.40.243 ) [1] => net_dns_rr_a Object ( [name] => ns2.easydns.com [type] => A [class] => IN [ttl] => 166281 [rdlength] => 4 [rdata] => <i>binary data</i> [address] => 205.210.42.20 ) [2] => net_dns_rr_a Object ( [name] => remote1.easydns.com [type] => A [class] => IN [ttl] => 82955 [rdlength] => 4 [rdata] => <i>binary data</i> [address] => 64.39.29.212 ) [3] => net_dns_rr_a Object ( [name] => remote2.easydns.com [type] => A [class] => IN [ttl] => 95573 [rdlength] => 4 [rdata] => <i>binary data</i> [address] => 212.100.224.80 ) ) )

Comments

  1. Hi,

    Am using Net_DNS to update a zone file with TSIG ;
    $tsig = new Net_DNS_RR(”$key_name TSIG $key”); which is suggested on http://www.ypass.net/software/php/Net_DNS/examples/update.html
    but I get this error: atal error: Call to undefined function: mhash() in /usr/share/pear/Net/DNS/RR/TSIG.php on line 187
    Thulani
    15 September 2004, 14:30 • PermaLink
  2. The PHP manual has info on the mhash library. I guess Net_DNS has it as a requirement now.
    Colin
    15 September 2004, 22:18 • PermaLink
  3. Hi,

    Could you anounnce this on PEAR-dev and PEAR-QA ?

    Or should I do that for you, please email me the answer (or does the comment system notify me of an answer :P not sure)

    Maybe someone is willing to take this package up, but at least it’s good to tell people on those mailing lists, that it’s not longer maintain.
    – Helgi
    Helgi Þormar
    22 November 2004, 18:44 • PermaLink
  4. Hi Helgi,

    I did post an announcement to the PEAR lists back when I decided to stop maintaining it: http://news.php.net/php.pear.dev/25097

    No one seemed to want to take it up at the time, so I’m considering it abandoned. If someone does want to pick up where I left off, they can. But, honestly, switching to Net_DNS is a much better option.
    Colin
    22 November 2004, 20:48 • PermaLink
  5. I just wish Net_DNS let you specify a server to query instead of using the one from resolv.conf
    Anthony
    8 March 2005, 01:22 • PermaLink
  6. Anthony,

    You can specify the server(s) to query, although it’s a bit kludgy:

    $DNS = new Net_DNS;
    $ns = array( 'ns1.example.com', 'ns2.example.com' );
    $DNS->resolver->nameservers = $ns;

    There is a private method to do this as well, but I’m guessing that if it’s private, PHP5 won’t let you call it from your script:

    $DNS->resolver->nameservers($ns);
    Colin Viebrock
    8 March 2005, 11:42 • PermaLink
  7. Hi,

    i released a new version of Net_DNS right now.

    require_once ‘Net/DNS.php’;

    $dns = new Net_DNS(array(‘nameservers’ => array(‘192.168.0.1’, ‘192.168.0.2’)));

    print_r($dns->resolver->nameservers);

    you can now pass parameters to the Net_DNS_Resolver class. (example for nameservers)

    Marco Kaiser
    1 December 2005, 03:44 • PermaLink
  8. Anyone know why my expected $actual != $len received??

    (happens with an axfr – still using 0.03)

    Could this imply a strange zone file?

    len : 16379

    actual :5838

    Any help appreciated.

    Brian
    15 December 2005, 00:54 • PermaLink
Name:
Email:
Website:
Comment:
What is 32 - 17
Textile Help