9 require File.dirname(__FILE__) + '/server/server'
11 # Module for manage all Whois Class
14 # Base exception of Whois
15 class WhoisException < Exception
18 # Exception of Whois who made report a bug
19 class WhoisExceptionError < WhoisException
21 WhoisException.initialize('Report a bug with error #{i} to http://rubyforge.org/projects/whois/')
25 # Class to get all information about Host or IP with the Whois request
34 attr_accessor :host_search
36 # Initialize with a request. The request must be an IPv4, or a host string
38 # The first params now is :
39 # * a string which a Ipv4, Ipv6 or a host.
42 # A second param, host_search is optionnal. By default he is false.
43 # If this value is true, a resolv host is made for know the host to this IPv4
44 def initialize(request, host_search=false)
45 @host_search = host_search
47 if request.instance_of? IPAddr
55 raise WhoisExceptionError.new(1)
57 elsif Resolv::IPv4::Regex =~ request
60 raise WhoisException.new("no server found for this IPv4 : #{request}")
62 elsif Resolv::IPv6::Regex =~ request
65 raise WhoisException.new("no server found for this Ipv6 : #{request}")
68 # Test if the request is an host or not
70 ip = Resolv.getaddress request
74 rescue Resolv::ResolvError
75 raise WhoisException.new('host #{request} has no DNS result')
79 search_host unless @host
84 s = TCPsocket.open(self.server.server, 43)
85 s.write("#{self.ip.to_s}\n")
87 while s.gets do ret += $_ end
93 # Search the host for this IPv4, if the value host_search is true, else host = nil
98 timeout(5) {@host = Resolv.getname self.ip.to_s}
105 rescue Resolv::ResolvError
112 # Init value for a ipv4 request
115 @server = server_ipv4
118 # Init value for a ipv6 request
121 @server = server_ipv6
124 # Return the Server with the hash of mask
125 def server_with_hash(ip_hash)
126 # Sort by mask of Ip Range
127 arr_tmp = ip_hash.sort{|b,c| c[0][/\/(.+)/, 1].to_i <=> b[0][/\/(.+)/, 1].to_i}
129 ip_range = IPAddr.new l[0]
130 if ip_range.include? self.ip and l[1].length > 0
131 return Object.instance_eval("Server::#{l[1]}.new")
133 return Server::Arin.new
137 # Define the server of Whois in IPC6 list of YAML
139 ipv6_list = YAML::load_file(File.dirname(__FILE__) + '/data/ipv6.yaml')
140 server = server_with_hash(ipv6_list)
141 unless server.kind_of? Server::Server
142 return Server::Arin.new
148 # Define the server of Whois in IPV4 list of YAML
150 ipv4_list = YAML::load_file(File.dirname(__FILE__) + '/data/ipv4.yaml')
151 server = server_with_hash(ipv4_list)
152 unless server.kind_of? Server::Server
153 return Server::Ripe.new
163 w = Whois::Whois.new '218.14.221.147'