Class: IPAddress::Prefix

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ipaddress/prefix.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Prefix

Returns a new instance of Prefix.



13
14
15
16
17
18
# File 'lib/ipaddress/prefix.rb', line 13

def initialize(obj)
  @num = obj[:num]
  @ip_bits = obj[:ip_bits]
  @net_mask = obj[:net_mask]
  @vt_from = obj[:vt_from]
end

Instance Attribute Details

#ip_bitsObject (readonly)

Returns the value of attribute ip_bits.



11
12
13
# File 'lib/ipaddress/prefix.rb', line 11

def ip_bits
  @ip_bits
end

#net_maskObject (readonly)

Returns the value of attribute net_mask.



11
12
13
# File 'lib/ipaddress/prefix.rb', line 11

def net_mask
  @net_mask
end

#numObject (readonly)

Returns the value of attribute num.



11
12
13
# File 'lib/ipaddress/prefix.rb', line 11

def num
  @num
end

#vt_fromObject (readonly)

Returns the value of attribute vt_from.



11
12
13
# File 'lib/ipaddress/prefix.rb', line 11

def vt_from
  @vt_from
end

Class Method Details

.new_netmask(prefix, bits) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/ipaddress/prefix.rb', line 71

def self.new_netmask(prefix, bits)
  mask = Crunchy.zero()
  host_prefix = bits - prefix
  prefix.times do  |i|
    # console.log(">>>", i, host_prefix, mask)
    mask = mask.add(Crunchy.one().shl(host_prefix + i))
  end

  return mask
end

Instance Method Details

#<=>(oth) ⇒ Object



38
39
40
# File 'lib/ipaddress/prefix.rb', line 38

def <=>(oth)
  cmp(oth)
end

#add(other) ⇒ Object



156
157
158
# File 'lib/ipaddress/prefix.rb', line 156

def add(other)
  return from(get_prefix() + other)
end

#add_prefix(other) ⇒ Object



152
153
154
# File 'lib/ipaddress/prefix.rb', line 152

def add_prefix(other)
  return from(get_prefix() + other.get_prefix())
end

#bitsObject

Transforms the prefix into a string of bits

representing the netmask

  prefix = IPAddress::Prefix128.new 64

  prefix.bits
    # => "1111111111111111111111111111111111111111111111111111111111111111"
        "0000000000000000000000000000000000000000000000000000000000000000"


131
132
133
# File 'lib/ipaddress/prefix.rb', line 131

def bits()
  return netmask().toString(2)
end

#cloneObject



20
21
22
23
24
25
26
27
# File 'lib/ipaddress/prefix.rb', line 20

def clone()
  return Prefix.new({
    num: @num,
    ip_bits: @ip_bits,
    net_mask: @net_mask,
    vt_from: @vt_from
  })
end

#cmp(oth) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ipaddress/prefix.rb', line 42

def cmp(oth)
  if (@ip_bits.version < oth.ip_bits.version)
    return -1
  elsif (@ip_bits.version > oth.ip_bits.version)
    return 1
  else
    if (@num < oth.num)
      return -1
    elsif (@num > oth.num)
      return 1
    else
      return 0
    end
  end
end

#eq(other) ⇒ Object



29
30
31
32
# File 'lib/ipaddress/prefix.rb', line 29

def eq(other)
  return @ip_bits.version == other.ip_bits.version &&
    @num == other.num
end

#from(num) ⇒ Object

allow(dead_code)


59
60
61
# File 'lib/ipaddress/prefix.rb', line 59

def from(num)
  return (@vt_from).call(self, num)
end

#get_prefixObject



86
87
88
# File 'lib/ipaddress/prefix.rb', line 86

def get_prefix()
  return @num
end

#host_maskObject

The hostmask is the contrary of the subnet mask,

as it shows the bits that can change within the
hosts

  prefix = IPAddress::Prefix32.new 24

  prefix.hostmask
    # => "0.0.0.255"


99
100
101
102
103
104
105
106
# File 'lib/ipaddress/prefix.rb', line 99

def host_mask()
  ret = Crunchy.zero()
  (@ip_bits.bits - @num).times do
    ret = ret.shl(1).add(Crunchy.one())
  end

  return ret
end

#host_prefixObject

Returns the length of the host portion

of a netmask.

  prefix = Prefix128.new 96

  prefix.host_prefix
    # => 128


117
118
119
# File 'lib/ipaddress/prefix.rb', line 117

def host_prefix()
  return @ip_bits.bits - @num
end

#ne(other) ⇒ Object



34
35
36
# File 'lib/ipaddress/prefix.rb', line 34

def ne(other)
  return !eq(other)
end

#netmaskObject



82
83
84
# File 'lib/ipaddress/prefix.rb', line 82

def netmask()
  return @net_mask
end

#sizeObject



67
68
69
# File 'lib/ipaddress/prefix.rb', line 67

def size()
  return Crunchy.one().shl(@ip_bits.bits - @num)
end

#sub(other) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/ipaddress/prefix.rb', line 164

def sub(other)
  if (other > get_prefix())
    return from(other - get_prefix())
  end

  return from(get_prefix() - other)
end

#sub_prefix(other) ⇒ Object



160
161
162
# File 'lib/ipaddress/prefix.rb', line 160

def sub_prefix(other)
  return sub(other.get_prefix())
end

#to_iObject

allow(dead_code)

def inspect(&self) -> String

return self.to_s()



148
149
150
# File 'lib/ipaddress/prefix.rb', line 148

def to_i()
  return get_prefix()
end

#to_ip_strObject



63
64
65
# File 'lib/ipaddress/prefix.rb', line 63

def to_ip_str()
  return @ip_bits.vt_as_compressed_string.call(@ip_bits, @net_mask)
end

#to_sObject

#[allow(dead_code)] def net_mask(&self) -> BigUint

return (self.in_mask.clone() >> (self.host_prefix() as usize)) << (self.host_prefix() as usize)



140
141
142
# File 'lib/ipaddress/prefix.rb', line 140

def to_s()
  return get_prefix().to_s
end