Class: Resolv::Hosts

Class | Resolv::Hosts
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 288
        def initialize(filename = DefaultFileName)
          @filename = filename
          @mutex = Mutex.new
          @initialized = nil
        end

### Public Instance methods

each_address(name, &proc)

[Source]

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

each_name(address, &proc)

[Source]

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

getaddress(name)

[Source]

    # File resolv.rb, line 325
        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 330
        def getaddresses(name)
          ret = []
          each_address(name) {|address| ret << address}
          return ret
        end

getname(address)

[Source]

    # File resolv.rb, line 343
        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 348
        def getnames(address)
          ret = []
          each_name(address) {|name| ret << name}
          return ret
        end

lazy_initialize()

[Source]

    # File resolv.rb, line 294
        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
          }
          self
        end

[Validate]