Class: NetAddr::Mask32

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

Overview

Mask32 represents a 32-bit netmask.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix_len) ⇒ Mask32

Create a Mask32 from an Integer prefix length. Valid values are 0-32. Throws ValidationError on error.



13
14
15
16
17
18
19
20
21
# File 'lib/mask32.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 > 32) )
    raise ValidationError, "#{prefix_len} must be in the range of 0-32."
  end
  @prefix_len = prefix_len
  @mask = NetAddr::F32 ^ (NetAddr::F32 >> @prefix_len)
end

Instance Attribute Details

#maskObject (readonly)

mask is the Integer representation of this netmask



6
7
8
# File 'lib/mask32.rb', line 6

def mask
  @mask
end

#prefix_lenObject (readonly)

prefix_len is the Integer prefix length of this netmask



9
10
11
# File 'lib/mask32.rb', line 9

def prefix_len
  @prefix_len
end

Class Method Details

.parse(mask) ⇒ Object

parse will create an Mask32 from its string representation. arguments:

  • mask - String representing a netmask (ie. “/24” or “255.255.255.0”).

Throws ValidationError on error.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mask32.rb', line 28

def Mask32.parse(mask)
  mask.strip!
  if (mask.start_with?("/")) # cidr format
    return Mask32.new(mask[1..-1].to_i) # remove "/"
  elsif (!mask.include?("."))
    return Mask32.new(mask.to_i)
  end
  
  # for extended netmask
  # determine length of netmask by cycling through bit by bit and looking
  # for the first '1' bit, tracking the length as we go. we also want to verify
  # that the mask is valid (ie. not something like 255.254.255.0). we do this
  # by creating a hostmask which covers the '0' bits of the mask. once we have
  # separated the net vs host mask we xor them together. the result should be that
  # all bits are now '1'. if not then we know we have an invalid netmask.
  maskI = Util.parse_IPv4(mask)
  prefix = 32
  hostmask = 1
  i = maskI 
  32.downto(1) do
    if (i&1 == 1)
      hostmask = hostmask >> 1
      if (maskI ^hostmask != NetAddr::F32)
        raise ValidationError, "#{mask} is invalid. It contains '1' bits in its host portion."
      end
      break
    end
    hostmask = (hostmask << 1) | 1
    i = i>>1
    prefix -= 1
  end
  return Mask32.new(prefix)
  
end

Instance Method Details

#cmp(other) ⇒ Object

cmp compares equality with another Mask32. Return:

  • 1 if this Mask128 is larger in capacity

  • 0 if the two are equal

  • -1 if this Mask128 is smaller in capacity



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mask32.rb', line 72

def cmp(other)
  if (!other.kind_of?(Mask32))
    raise ArgumentError, "Expected an Mask32 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

#extendedObject

extended returns the Mask32 in extended format (eg. x.x.x.x)



64
65
66
# File 'lib/mask32.rb', line 64

def extended()
  Util.intToMask32(@mask)
end

#lenObject

len returns the number of IP addresses in this network. It will always return 0 for /0 networks.



85
86
87
88
89
90
# File 'lib/mask32.rb', line 85

def len()
  if (self.prefix_len == 0)
    return 0
  end
  return (self.mask ^ NetAddr::F32) + 1 # bit flip the netmask and add 1
end

#to_sObject

to_s returns the Mask32 as a String



93
94
95
# File 'lib/mask32.rb', line 93

def to_s()
  return "/#{@prefix_len}"
end