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



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/infra/ospf_common.rb', line 126

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_arr(prefix) ⇒ Object



169
170
171
172
173
174
# File 'lib/infra/ospf_common.rb', line 169

def IPAddr.to_arr(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



144
145
146
# File 'lib/infra/ospf_common.rb', line 144

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

#^(i) ⇒ Object



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

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

#mlenObject



140
141
142
# File 'lib/infra/ospf_common.rb', line 140

def mlen
  @_jme_mlen_ ||= _mlen_
end

#netmaskObject



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/infra/ospf_common.rb', line 152

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



165
166
167
# File 'lib/infra/ospf_common.rb', line 165

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