# Resolv::Hosts (Class)

In: | resolv.rb
Parent: | Object

## Methods

each_address each_name getaddress getaddresses getname getnames lazy_initialize new

## Constants

DefaultFileName | = | Win32::Resolv.get_hosts_path
DefaultFileName | = | '/etc/hosts'

## Public Class methods

new(filename = DefaultFileName)

[Source]

    # File resolv.rb, line 280
        def initialize(filename = DefaultFileName)
          @filename = filename
          @mutex = Mutex.new
          @initialized = nil
        end

## Public Instance methods

lazy_initialize()

[Source]

    # File resolv.rb, line 286
        def lazy_initialize
          @mutex.synchronize {
            unless @initialized
              @name2addr = {}
              @addr2name = {}
              open(@filename) {|f|
                f.each {|line|
                  line.sub!(/#.*/, '')
                  addr, hostname, *aliases = line.split(/\s+/)
                  next unless addr
                  addr.untaint
                  hostname.untaint
                  @addr2name[addr] = [] unless @addr2name.include? addr
                  @addr2name[addr] << hostname
                  @addr2name[addr] += aliases
                  @name2addr[hostname] = [] unless @name2addr.include? hostname
                  @name2addr[hostname] << addr
                  aliases.each {|n|
                    n.untaint
                    @name2addr[n] = [] unless @name2addr.include? n
                    @name2addr[n] << addr
                  }
                }
              }
              @name2addr.each {|name, arr| arr.reverse!}
              @initialized = true
            end
          }
        end

getaddress(name)

[Source]

    # File resolv.rb, line 316
        def getaddress(name)
          each_address(name) {|address| return address}
          raise ResolvError.new("#{@filename} has no name: #{name}")
        end

getaddresses(name)

[Source]

    # File resolv.rb, line 321
        def getaddresses(name)
          ret = []
          each_address(name) {|address| ret << address}
          return ret
        end

each_address(name, &proc)

[Source]

    # File resolv.rb, line 327
        def each_address(name, &proc)
          lazy_initialize
          if @name2addr.include?(name)
            @name2addr[name].each(&proc)
          end
        end

getname(address)

[Source]

    # File resolv.rb, line 334
        def getname(address)
          each_name(address) {|name| return name}
          raise ResolvError.new("#{@filename} has no address: #{address}")
        end

getnames(address)

[Source]

    # File resolv.rb, line 339
        def getnames(address)
          ret = []
          each_name(address) {|name| ret << name}
          return ret
        end

each_name(address, &proc)

[Source]

    # File resolv.rb, line 345
        def each_name(address, &proc)
          lazy_initialize
          if @addr2name.include?(address)
            @addr2name[address].each(&proc)
          end
        end

[Validate]