Class: IPAddr

Inherits:
Object show all
Defined in:
lib/infra/ospf_common.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(arg) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/infra/ospf_common.rb', line 135

def self.create(arg)
  if arg.is_a?(String) and arg.is_packed?
    IPAddr.new_ntoh(arg)
  elsif arg.is_a?(Integer)
    IPAddr.new_ntoh([arg].pack('N'))
  elsif arg.is_a?(Array) and arg[0].is_a?(Fixnum)
    IPAddr.new_ntoh([arg].pack('C*'))
  elsif arg.is_a?(self)
    IPAddr.new
  else
    IPAddr.new(arg)
  end
end

.to_ary(prefix) ⇒ Object



178
179
180
181
182
183
# File 'lib/infra/ospf_common.rb', line 178

def IPAddr.to_ary(prefix)
  source_address,mlen = prefix.split('/')
  ip = IPAddr.new(prefix)
  network = ip.to_s
  [ip, source_address, mlen.to_i, network, ip.netmask]
end

Instance Method Details

#+(i) ⇒ Object



153
154
155
# File 'lib/infra/ospf_common.rb', line 153

def +(i)
  [IPAddr.create(to_i + i).to_s, mlen].join("/")
end

#^(i) ⇒ Object



156
157
158
159
# File 'lib/infra/ospf_common.rb', line 156

def ^(i)
  @increment ||= _generate_network_inc_
  [IPAddr.create(to_i + @increment.call(i)).to_s, mlen].join("/")
end

#mlenObject



149
150
151
# File 'lib/infra/ospf_common.rb', line 149

def mlen
  @_jme_mlen_ ||= _mlen_
end

#netmaskObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/infra/ospf_common.rb', line 161

def netmask
  if ipv4?
    [@mask_addr].pack('N').unpack('C4').collect { |x| x.to_s}.join('.')
  else
    i = @mask_addr
    arr=[]
    while i>0 
      arr <<  (i & 0xffff) and i >>= 16
    end
    arr.reverse.collect { |x| x.to_s(16) }.join(':')
  end
end

#to_s_netObject



174
175
176
# File 'lib/infra/ospf_common.rb', line 174

def to_s_net
  [to_s,mlen].join('/')
end