Class: Scriptroute::IPaddress

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptroute/packets.rb

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ IPaddress

Returns a new instance of IPaddress.

Parameters:

  • n (String, IPaddress, Integer)

    a hostname, an IP address in string form, another IPaddress or an IP address in numeric form



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/scriptroute/packets.rb', line 91

def initialize(n)
  raise "Seems bad to initialize an IPaddress with nil." unless n
  if(n.is_a?(String)) then
    shf = 32
    if n =~ /^[\d\.]+$/ then
      @addr = n.split('.').map { |i| shf -= 8; i.to_i << shf }.sum
    else
      addr = Scriptroute::dns_lookup(n)
      if addr then
        @addr = addr.split('.').map { |i| shf -= 8; i.to_i << shf }.sum
      else
        $stderr.puts "unable to lookup #{n}"
        @addr = 0
      end
    end
  elsif(n.is_a?(IPaddress)) then
    @addr = n.to_i
  else
    @addr = n
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compares numerically, for sorting.



134
135
136
# File 'lib/scriptroute/packets.rb', line 134

def <=>(other)
  @addr <=> other.to_i
end

#==(other) ⇒ Object

Compares numerically, for equality test.



138
139
140
# File 'lib/scriptroute/packets.rb', line 138

def ==(other)
  @addr == other.to_i
end

#eql?(other) ⇒ Boolean

in case you would like to store a Hash with IPaddresses as keys

Returns:

  • (Boolean)


130
131
132
# File 'lib/scriptroute/packets.rb', line 130

def eql?(other)
  @addr == other.to_i
end

#hashObject

in case you would like to store a Hash with IPaddresses as keys



126
127
128
# File 'lib/scriptroute/packets.rb', line 126

def hash
  @addr
end

#inspectString

just invokes to_s for now.

Returns:

  • (String)


118
119
120
# File 'lib/scriptroute/packets.rb', line 118

def inspect
  to_s
end

#nameifyObject



142
143
144
# File 'lib/scriptroute/packets.rb', line 142

def nameify
  Scriptroute.nameify(self.to_s)
end

#to_iInteger

Returns:

  • (Integer)


122
123
124
# File 'lib/scriptroute/packets.rb', line 122

def to_i
  @addr
end

#to_sString

Returns dotted quad notation of the IP address.

Returns:

  • (String)

    dotted quad notation of the IP address.



113
114
115
# File 'lib/scriptroute/packets.rb', line 113

def to_s
  [ @addr >> 24, @addr >> 16, @addr >> 8, @addr ].map { |i| i & 0xff }.join(".")
end