Class: Dnsruby::Hosts

### Home Classes Methods

### In Files

- lib/Dnsruby/Hosts.rb

### Parent

Object

### Methods

- ::new
- #each_address
- #each_name
- #getaddress
- #getaddresses
- #getname
- #getnames

### Files

- DNSSEC
- EVENTMACHINE
- EXAMPLES
- README

### Class/Module Index

Quicksearch

- Dnsruby
- Dnsruby::Algorithms
- Dnsruby::Classes
- Dnsruby::CodeMapper
- Dnsruby::Config
- Dnsruby::DNS
- Dnsruby::DecodeError
- Dnsruby::Dnssec
- Dnsruby::Dnssec::ValidationPolicy
- Dnsruby::EncodeError
- Dnsruby::ExtendedRCode
- Dnsruby::FormErr
- Dnsruby::Header
- Dnsruby::Hosts
- Dnsruby::IPv4
- Dnsruby::IPv6
- Dnsruby::Message
- Dnsruby::Message::Section
- Dnsruby::Message::SecurityLevel
- Dnsruby::MetaTypes
- Dnsruby::Modes
- Dnsruby::NXDomain
- Dnsruby::NXRRSet
- Dnsruby::Name
- Dnsruby::Name::Label
- Dnsruby::NotAuth
- Dnsruby::NotImp
- Dnsruby::NotZone
- Dnsruby::Nsec3HashAlgorithms
- Dnsruby::OpCode
- Dnsruby::OtherResolvError
- Dnsruby::QTypes
- Dnsruby::Question
- Dnsruby::RCode
- Dnsruby::RR
- Dnsruby::RR::ANY
- Dnsruby::RR::CERT
- Dnsruby::RR::CERT::CertificateTypes
- Dnsruby::RR::CNAME
- Dnsruby::RR::DHCID
- Dnsruby::RR::DLV
- Dnsruby::RR::DNAME
- Dnsruby::RR::DNSKEY
- Dnsruby::RR::DS
- Dnsruby::RR::DS::DigestTypes
- Dnsruby::RR::DomainName
- Dnsruby::RR::Generic
- Dnsruby::RR::HINFO
- Dnsruby::RR::HIP
- Dnsruby::RR::IN
- Dnsruby::RR::IN::A
- Dnsruby::RR::IN::AAAA
- Dnsruby::RR::IN::AFSDB
- Dnsruby::RR::IN::PX
- Dnsruby::RR::IN::SRV
- Dnsruby::RR::IN::WKS
- Dnsruby::RR::IPSECKEY
- Dnsruby::RR::ISDN
- Dnsruby::RR::KX
- Dnsruby::RR::LOC
- Dnsruby::RR::MB
- Dnsruby::RR::MG
- Dnsruby::RR::MINFO
- Dnsruby::RR::MR
- Dnsruby::RR::MX
- Dnsruby::RR::NAPTR
- Dnsruby::RR::NS
- Dnsruby::RR::NSAP
- Dnsruby::RR::NSEC
- Dnsruby::RR::NSEC3
- Dnsruby::RR::NSEC3PARAM
- Dnsruby::RR::OPT
- Dnsruby::RR::PTR
- Dnsruby::RR::RP
- Dnsruby::RR::RRSIG
- Dnsruby::RR::RT
- Dnsruby::RR::SOA
- Dnsruby::RR::SPF
- Dnsruby::RR::SSHFP
- Dnsruby::RR::SSHFP::Algorithms
- Dnsruby::RR::SSHFP::FpTypes
- Dnsruby::RR::TKEY
- Dnsruby::RR::TSIG
- Dnsruby::RR::TXT
- Dnsruby::RR::X25
- Dnsruby::RRSet
- Dnsruby::Recursor
- Dnsruby::Refused
- Dnsruby::Resolv
- Dnsruby::ResolvError
- Dnsruby::ResolvTimeout
- Dnsruby::Resolver
- Dnsruby::Resolver::EventType
- Dnsruby::SelectThread
- Dnsruby::ServFail
- Dnsruby::SingleResolver
- Dnsruby::TheLog
- Dnsruby::TsigError
- Dnsruby::TsigNotSignedResponseError
- Dnsruby::Types
- Dnsruby::Update
- Dnsruby::VerifyError
- Dnsruby::YXDomain
- Dnsruby::YXRRSet
- Dnsruby::ZoneReader
- Dnsruby::ZoneReader::ParseException
- Dnsruby::ZoneSerialError
- Dnsruby::ZoneTransfer
- Dnsruby::ZoneTransfer::Delta
- Base32

No matching classes.

# Dnsruby::Hosts

## Dnsruby::Hosts class

Dnsruby::Hosts is a hostname resolver that uses the system hosts file

### class methods

- Dnsruby::Hosts.new(hosts='/etc/hosts')

### methods

- Dnsruby::Hosts#getaddress(name)

- Dnsruby::Hosts#getaddresses(name)

- Dnsruby::Hosts#each_address(name) {|address| ...}

      address lookup methods.

- Dnsruby::Hosts#getname(address)

- Dnsruby::Hosts#getnames(address)

- Dnsruby::Hosts#each_name(address) {|name| ...}

      hostnames lookup methods.

### Constants

DefaultFileName

### Public Class Methods

new(filename = DefaultFileName) click to toggle source

Creates a new Dnsruby::Hosts using `filename` for its data source

    # File lib/Dnsruby/Hosts.rb, line 43
    def initialize(filename = DefaultFileName)
      @filename = filename
      @mutex = Mutex.new
      @initialized = nil
    end

### Public Instance Methods

each_address(name, &proc) click to toggle source

Iterates over all IP addresses for `name` retrieved from the hosts file

    # File lib/Dnsruby/Hosts.rb, line 98
    def each_address(name, &proc)
      lazy_initialize
      if @name2addr.include?(name)
        @name2addr[name].each(&proc)
      end
    end

each_name(address, &proc) click to toggle source

Iterates over all hostnames for `address` retrieved from the hosts file

    # File lib/Dnsruby/Hosts.rb, line 119
    def each_name(address, &proc)
      lazy_initialize
      if @addr2name.include?(address)
        @addr2name[address].each(&proc)
      end
    end

getaddress(name) click to toggle source

Gets the first IP address for `name` from the hosts file

    # File lib/Dnsruby/Hosts.rb, line 85
    def getaddress(name)
      each_address(name) {|address| return address}
      raise ResolvError.new("#{@filename} has no name: #{name}")
    end

getaddresses(name) click to toggle source

Gets all IP addresses for `name` from the hosts file

    # File lib/Dnsruby/Hosts.rb, line 91
    def getaddresses(name)
      ret = []
      each_address(name) {|address| ret << address}
      return ret
    end

getname(address) click to toggle source

Gets the first hostname of `address` from the hosts file

    # File lib/Dnsruby/Hosts.rb, line 106
    def getname(address)
      each_name(address) {|name| return name}
      raise ResolvError.new("#{@filename} has no address: #{address}")
    end

getnames(address) click to toggle source

Gets all hostnames for `address` from the hosts file

    # File lib/Dnsruby/Hosts.rb, line 112
    def getnames(address)
      ret = []
      each_name(address) {|name| ret << name}
      return ret
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.