Class: NetAddr::Mask128
- Inherits:
-
Object
- Object
- NetAddr::Mask128
- Defined in:
- lib/mask128.rb
Overview
Mask128 represents a 128-bit netmask.
Instance Attribute Summary collapse
-
#mask ⇒ Object
readonly
mask is the Integer representation of this netmask.
-
#prefix_len ⇒ Object
readonly
prefix_len is the Integer prefix length of this netmask.
Class Method Summary collapse
-
.parse(mask) ⇒ Object
parse will create an Mask128 from its string representation.
Instance Method Summary collapse
-
#cmp(other) ⇒ Object
cmp compares equality with another Mask128.
-
#initialize(prefix_len) ⇒ Mask128
constructor
Create a Mask128 from an Integer prefix length.
-
#len ⇒ Object
len returns the number of IP addresses in this network.
-
#to_s ⇒ Object
to_s returns the Mask128 as a String.
Constructor Details
#initialize(prefix_len) ⇒ Mask128
Create a Mask128 from an Integer prefix length. Valid values are 0-128. Throws ValidationError on error.
13 14 15 16 17 18 19 20 21 |
# File 'lib/mask128.rb', line 13 def initialize(prefix_len) if (!prefix_len.kind_of?(Integer)) raise ValidationError, "Expected an Integer for 'prefix_len' but got a #{prefix_len.class}." elsif ( (prefix_len < 0) || (prefix_len > 128) ) raise ValidationError, "#{prefix_len} must be in the range of 0-128." end @prefix_len = prefix_len @mask = NetAddr::F128 ^ (NetAddr::F128 >> @prefix_len) end |
Instance Attribute Details
#mask ⇒ Object (readonly)
mask is the Integer representation of this netmask
6 7 8 |
# File 'lib/mask128.rb', line 6 def mask @mask end |
#prefix_len ⇒ Object (readonly)
prefix_len is the Integer prefix length of this netmask
9 10 11 |
# File 'lib/mask128.rb', line 9 def prefix_len @prefix_len end |
Class Method Details
.parse(mask) ⇒ Object
parse will create an Mask128 from its string representation. arguments:
-
mask - String representing an netmask (ie. “/64”).
Throws ValidationError on error.
28 29 30 31 32 33 34 35 36 |
# File 'lib/mask128.rb', line 28 def Mask128.parse(mask) mask = mask.strip if (mask.start_with?("/")) # cidr format mask = mask[1..-1] # remove "/" end return Mask128.new(Integer(mask)) rescue ArgumentError raise ValidationError, "#{mask} is not valid integer." end |
Instance Method Details
#cmp(other) ⇒ Object
cmp compares equality with another Mask128. Return:
-
1 if this Mask128 is larger in capacity
-
0 if the two are equal
-
-1 if this Mask128 is smaller in capacity
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mask128.rb', line 42 def cmp(other) if (!other.kind_of?(Mask128)) raise ArgumentError, "Expected an Mask128 object for 'other' but got a #{other.class}." end if (self.prefix_len < other.prefix_len) return 1 elsif (self.prefix_len > other.prefix_len) return -1 end return 0 end |
#len ⇒ Object
len returns the number of IP addresses in this network. This is only useful if you have a subnet smaller than a /64 as it will always return 0 for prefixes <= 64.
56 57 58 59 60 61 |
# File 'lib/mask128.rb', line 56 def len() if (self.prefix_len <= 64) return 0 end return (self.mask ^ NetAddr::F128) + 1 # bit flip the netmask and add 1 end |
#to_s ⇒ Object
to_s returns the Mask128 as a String
64 65 66 |
# File 'lib/mask128.rb', line 64 def to_s() return "/#{@prefix_len}" end |