|
| 1 | +# Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz> |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +begin |
| 22 | +require 'win32/resolv' |
| 23 | +rescue LoadError |
| 24 | +# Ignore this - we aren't running on windows. |
| 25 | +end |
| 26 | + |
| 27 | +module RubyDNS |
| 28 | +# This module encapsulates system dependent name lookup functionality. |
| 29 | +module System |
| 30 | +RESOLV_CONF = "/etc/resolv.conf" |
| 31 | +HOSTS = "/etc/hosts" |
| 32 | + |
| 33 | +def self.hosts_path |
| 34 | +if RUBY_PLATFORM =~ /mswin32|mingw|bccwin/ |
| 35 | +Win32::Resolv.get_hosts_path |
| 36 | +else |
| 37 | +HOSTS |
| 38 | +end |
| 39 | +end |
| 40 | + |
| 41 | +# This code is very experimental |
| 42 | +class Hosts |
| 43 | +def initialize |
| 44 | +@addresses = {} |
| 45 | +@names = {} |
| 46 | +end |
| 47 | + |
| 48 | +attr :addresses |
| 49 | +attr :names |
| 50 | + |
| 51 | +# This is used to match names against the list of known hosts: |
| 52 | +def call(name) |
| 53 | +@names.include?(name) |
| 54 | +end |
| 55 | + |
| 56 | +def lookup(name) |
| 57 | +addresses = @names[name] |
| 58 | + |
| 59 | +if addresses |
| 60 | +addresses.last |
| 61 | +else |
| 62 | +nil |
| 63 | +end |
| 64 | +end |
| 65 | + |
| 66 | +alias [] lookup |
| 67 | + |
| 68 | +def add(address, names) |
| 69 | +@addresses[address] ||= [] |
| 70 | +@addresses[address] += names |
| 71 | + |
| 72 | +names.each do |name| |
| 73 | +@names[name] ||= [] |
| 74 | +@names[name] << address |
| 75 | +end |
| 76 | +end |
| 77 | + |
| 78 | +def parse_hosts(io) |
| 79 | +io.each do |line| |
| 80 | +line.sub!(/#.*/, '') |
| 81 | +address, hostname, *aliases = line.split(/\s+/) |
| 82 | + |
| 83 | +add(address, [hostname] + aliases) |
| 84 | +end |
| 85 | +end |
| 86 | + |
| 87 | +def self.local |
| 88 | +hosts = self.new |
| 89 | + |
| 90 | +path = System::hosts_path |
| 91 | + |
| 92 | +if path and File.exist?(path) |
| 93 | +File.open(path) do |file| |
| 94 | +hosts.parse_hosts(file) |
| 95 | +end |
| 96 | +end |
| 97 | + |
| 98 | +return hosts |
| 99 | +end |
| 100 | +end |
| 101 | + |
| 102 | +def self.parse_resolv_configuration(path) |
| 103 | +nameservers = [] |
| 104 | +File.open(path) do |file| |
| 105 | +file.each do |line| |
| 106 | +# Remove any comments: |
| 107 | +line.sub!(/[#;].*/, '') |
| 108 | + |
| 109 | +# Extract resolv.conf command: |
| 110 | +keyword, *args = line.split(/\s+/) |
| 111 | + |
| 112 | +case keyword |
| 113 | +when 'nameserver' |
| 114 | +nameservers += args |
| 115 | +end |
| 116 | +end |
| 117 | +end |
| 118 | + |
| 119 | +return nameservers |
| 120 | +end |
| 121 | + |
| 122 | +def self.standard_connections(nameservers) |
| 123 | +connections = [] |
| 124 | + |
| 125 | +nameservers.each do |host| |
| 126 | +connections << [:udp, host, 53] |
| 127 | +connections << [:tcp, host, 53] |
| 128 | +end |
| 129 | + |
| 130 | +return connections |
| 131 | +end |
| 132 | + |
| 133 | +# Get a list of standard nameserver connections which can be used for querying any standard servers that the system has been configured with. There is no equivalent facility to use the `hosts` file at present. |
| 134 | +def self.nameservers |
| 135 | +nameservers = [] |
| 136 | + |
| 137 | +if File.exist? RESOLV_CONF |
| 138 | +nameservers = parse_resolv_configuration(RESOLV_CONF) |
| 139 | +elsif defined?(Win32::Resolv) and RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/ |
| 140 | +search, nameservers = Win32::Resolv.get_resolv_info |
| 141 | +end |
| 142 | + |
| 143 | +return standard_connections(nameservers) |
| 144 | +end |
| 145 | +end |
| 146 | +end |
0 commit comments