Sha256: da2984b8d1c5c5730c29ccba0c4b8c0e0ba6c3e1c680348610fc4d2a05a41630

Contents?: true

Size: 935 Bytes

Versions: 11

Compression:

Stored size: 935 Bytes

Contents

class Numeric
   def ord; self; end unless RUBY_VERSION[0..2] == '1.9' # helper for 1.8/1.9
end

require 'socket' # sane already loads it for BasicSocket.reverse_lookup

class Socket
 class << self
   def get_local_ips
     get_ips(Socket.gethostname)
   end

   def get_ips hostname
     begin

       socket_info = Socket.getaddrinfo(hostname, nil,
         Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil,
         Socket::AI_CANONNAME).select{|type| type[0] == 'AF_INET'}

       raise if socket_info.length == 0
       ips = socket_info.map{|info| info[3]}
       return ips
     rescue => e # getaddrinfo failed...
       begin
          ipInt = gethostbyname(hostname)[3] # do a DNS lookup
          return ["%d.%d.%d.%d" % [ipInt[0].ord, ipInt[1].ord, ipInt[2].ord, ipInt[3].ord]]
       rescue SocketError # happens in certain instances...
          return nil
       end
     end
   end

 end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sane-0.25.8 lib/sane/socket_ips.rb
sane-0.25.7 lib/sane/socket_ips.rb
sane-0.25.6 lib/sane/socket_ips.rb
sane-0.25.5 lib/sane/socket_ips.rb
sane-0.25.4 lib/sane/socket_ips.rb
sane-0.25.3 lib/sane/socket_ips.rb
sane-0.25.2 lib/sane/socket_ips.rb
sane-0.25.1 lib/sane/socket_ips.rb
sane-0.25.0 lib/sane/socket_ips.rb
sane-0.24.6 lib/sane/socket_ips.rb
sane-0.24.5 lib/sane/socket_ips.rb