7 require File.dirname(__FILE__) + '/server/server'
9 # Module for manage all Whois Class
12 # Base exception of Whois
13 class WhoisException < Exception
16 # Exception of Whois who made report a bug
17 class WhoisExceptionError < WhoisException
19 WhoisException.initialize('Report a bug with error #{i} to http://rubyforge.org/projects/whois/')
23 # Class to get all information about Host or IP with the Whois request
32 attr_accessor :host_search
34 # Initialize with a request. The request must be an IPv4, or a host string
36 # The first params now is :
37 # * a string which a Ipv4, Ipv6 or a host.
40 # A second param, host_search is optionnal. By default he is false.
41 # If this value is true, a resolv host is made for know the host to this IPv4
42 def initialize(request, host_search=false)
43 @host_search = host_search
45 if request.instance_of? IPAddr
53 raise WhoisExceptionError.new(1)
55 elsif Resolv::IPv4::Regex =~ request
58 raise WhoisException.new("no server found for this IPv4 : #{request}")
60 elsif Resolv::IPv6::Regex =~ request
63 raise WhoisException.new("no server found for this Ipv6 : #{request}")
66 # Test if the request is an host or not
68 ip = Resolv.getaddress request
72 rescue Resolv::ResolvError
73 raise WhoisException.new('host #{request} has no DNS result')
77 search_host unless @host
82 s = TCPsocket.open(self.server.server, 43)
83 s.write("#{self.ip.to_s}\n")
85 while s.gets do ret += $_ end
91 # Search the host for this IPv4, if the value host_search is true, else host = nil
95 @host = Resolv.getname self.ip.to_s
99 rescue Resolv::ResolvError
106 # Init value for a ipv4 request
109 @server = server_ipv4
112 # Init value for a ipv6 request
115 @server = server_ipv6
118 # Return the Server with the hash of mask
119 def server_with_hash(ip_hash)
120 # Sort by mask of Ip Range
121 arr_tmp = ip_hash.sort{|b,c| c[0][/\/(.+)/, 1].to_i <=> b[0][/\/(.+)/, 1].to_i}
123 ip_range = IPAddr.new l[0]
124 if ip_range.include? self.ip
125 return Object.instance_eval("Server::#{l[1]}.new")
130 # Define the server of Whois in IPC6 list of YAML
132 ipv6_list = YAML::load_file(File.dirname(__FILE__) + '/data/ipv6.yaml')
133 server = server_with_hash(ipv6_list)
134 unless server.kind_of? Server::Server
135 raise WhoisException.new("no server found for this IPv6 : #{self.ip}")
141 # Define the server of Whois in IPV4 list of YAML
143 ipv4_list = YAML::load_file(File.dirname(__FILE__) + '/data/ipv4.yaml')
144 server = server_with_hash(ipv4_list)
145 unless server.kind_of? Server::Server
146 raise WhoisException.new("no server found for this IPv4 : #{self.ip}")
156 w = Whois::Whois.new '218.14.221.147'