github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

bluemonk / net-dns

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 36
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (3)
  • Downloads (4)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (3)
    • 0.6.x
    • master ✓
    • refactoring
  • Tags (4)
    • v0.6.1
    • v0.6.0
    • v0.5.3
    • v0.5.2
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Net::DNS is a DNS library written in Ruby. — Read more

  cancel

http://net-dns.rubyforge.org

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Merge branch '0.7.0' 
weppos (author)
Sun Feb 07 16:52:29 -0800 2010
commit  306fdb1e7189c776d4dc87be43b01ebd3490e24f
tree    0cbb7e2087a4b26d85b34522f1617b398b7fd447
parent  a837521047a64aa4aa2e74ec5d5ed44cb4a290c7 parent  5b054be7a56f02b79498f6ee86f14b68c443309c
net-dns /
name age
history
message
file .gitignore Tue Jul 28 05:56:53 -0700 2009 Default ignored files and folders. [weppos]
file AUTHORS.rdoc Tue Nov 03 03:51:54 -0800 2009 Fixed my email and normalized changelog. [weppos]
file CHANGELOG.rdoc Sun Feb 07 16:52:29 -0800 2010 Merge branch '0.7.0' [weppos]
file README.rdoc Sun Nov 01 04:55:33 -0800 2009 Updated Readme file. [weppos]
file Rakefile Mon Jan 18 08:27:29 -0800 2010 Fixed invalid development dependencies. [weppos]
file THANKS.rdoc Sun Nov 01 04:56:50 -0800 2009 Normalized extra Rdoc files. [weppos]
file VERSION.yml Mon Jan 18 07:41:15 -0800 2010 Version bump to 0.6.1 [weppos]
directory demo/ Wed Nov 04 12:20:46 -0800 2009 Removed old SVN properties. [weppos]
directory lib/ Sun Feb 07 16:52:29 -0800 2010 Merge branch '0.7.0' [weppos]
file setup.rb Mon May 25 02:24:42 -0700 2009 Initial import on github [bluemonk]
directory test/ Sun Feb 07 16:52:29 -0800 2010 Merge branch '0.7.0' [weppos]
README.rdoc

Net::DNS

Net::DNS is a DNS library written in pure Ruby. It started as a port of Perl Net::DNS module, but it evolved in time into a full Ruby library.

Features

  • Complete OO interface
  • Clean and intuitive API
  • Modular and flexible

Requirements

  • Ruby >= 1.8.6 (not tested with previous versions)

As of release TODO, Net::DNS is compatible with Ruby 1.9.1.

Install

Just use RubyGems:

  $ gem install net-dns

If you want to install from source, you can use Rake:

  $ rake install

Or directly from setup.rb

  $ ruby setup.rb

API Documentation

Visit the page marcoceresa.com/net-dns

Trivial resolver

The simplest way to use the library is to invoke the Resolver() method:

    require 'rubygems'
    require 'net/dns/resolver'
    p Resolver("www.google.com")

The output is compatible with BIND zone files and it’s the same you would get with the dig utility.

    ;; Answer received from localhost:53 (212 bytes)
    ;;
    ;; HEADER SECTION
    ;; id = 64075
    ;; qr = 1       opCode: QUERY   aa = 0  tc = 0  rd = 1
    ;; ra = 1       ad = 0  cd = 0  rcode = NoError
    ;; qdCount = 1  anCount = 3     nsCount = 4     arCount = 4

    ;; QUESTION SECTION (1 record):
    ;; google.com.                  IN      A

    ;; ANSWER SECTION (3 records):
    google.com.             212     IN      A       74.125.45.100
    google.com.             212     IN      A       74.125.67.100
    google.com.             212     IN      A       209.85.171.100

    ;; AUTHORITY SECTION (4 records):
    google.com.             345512  IN      NS      ns1.google.com.
    google.com.             345512  IN      NS      ns4.google.com.
    google.com.             345512  IN      NS      ns2.google.com.
    google.com.             345512  IN      NS      ns3.google.com.

    ;; ADDITIONAL SECTION (4 records):
    ns1.google.com.         170275  IN      A       216.239.32.10
    ns2.google.com.         170275  IN      A       216.239.34.10
    ns3.google.com.         170275  IN      A       216.239.36.10
    ns4.google.com.         170275  IN      A       216.239.38.10

An optional block can be passed yielding the Net::DNS::Packet object

   Resolver("www.google.com") {|packet| puts packet.size + " bytes"}
     #=> 484 bytes

Same for Net::DNS::Resolver.start():

    Net::DNS::Resolver.start("google.com").answer.size
      #=> 5

As optional parameters, TYPE and CLASS can be specified.

   p Net::DNS::Resolver.start("google.com", Net::DNS::MX)

   ;; Answer received from localhost:53 (316 bytes)
   ;;
   ;; HEADER SECTION
   ;; id = 59980
   ;; qr = 1       opCode: QUERY   aa = 0  tc = 0  rd = 1
   ;; ra = 1       ad = 0  cd = 0  rcode = NoError
   ;; qdCount = 1  anCount = 4     nsCount = 4     arCount = 8

   ;; QUESTION SECTION (1 record):
   ;; google.com.                  IN      MX

   ;; ANSWER SECTION (4 records):
   google.com.             10800   IN      MX      10 smtp2.google.com.
   google.com.             10800   IN      MX      10 smtp3.google.com.
   google.com.             10800   IN      MX      10 smtp4.google.com.
   google.com.             10800   IN      MX      10 smtp1.google.com.

Handling the response packet

The method Net::DNS::Resolver.start is a wrapper around Resolver.new. It returns a new Net::DNS::Packet object.

A DNS packet is divided into 5 sections:

  • header section # => a Net::DNS::Header object
  • question section # => a Net::DNS::Question object
  • answer section # => an Array of Net::DNS::RR objects
  • authority section # => an Array of Net::DNS::RR objects
  • additional section # => an Array of Net::DNS::RR objects

You can access each section by calling the attribute with the same name on a Packet object:

    packet = Net::DNS::Resolver.start("google.com")

    header = packet.header
    answer = packet.answer

    puts "The packet is #{packet.data.size} bytes"
    puts "It contains #{header.anCount} answer entries"

    answer.any? {|ans| p ans}

The output is

    The packet is 378 bytes
    It contains 3 answer entries
    google.com.             244     IN      A       74.125.45.100
    google.com.             244     IN      A       74.125.67.100
    google.com.             244     IN      A       209.85.171.100

A better way to handle the answer section is to use the iterators directly on a Packet object:

    packet.each_address do |ip|
      puts "#{ip} is alive" if Ping.pingecho(ip.to_s, 10, 80)
    end

Gives:

    74.125.45.100 is alive
    74.125.67.100 is alive
    209.85.171.100 is alive

Licence

Net::DNS is distributed under the same license Ruby is.

Author

© Marco Ceresa 2006-2009

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server