Method: IP::Address::Util.short_netmask

Defined in:
lib/ip/util.rb

.short_netmask(ip) ⇒ Object

Returns a short subnet mask - works for all IP::Address objects.

ex: short_netmask(IP::Address::IPv4.new(“255.255.255.255”)) => 32 short_netmask(IP::Address::IPv6.new(“2001:0DB8:0000:CD30:0000:0000:0000:0000”)) => 60



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ip/util.rb', line 82

def short_netmask(ip)
  #
  # This method handles 128-bit integers better for both types of
  # addresses, even though it is a bit slower.
  #
  # TODO: there really is probably a better way to do this.
  #

  s = ip.pack.to_s(2)

  pad = 0

  case ip.class.object_id
  when IP::Address::IPv6.object_id
    pad = 128
  when IP::Address::IPv4.object_id
    pad = 32
  end

  s = ("0" * (pad - s.length)) + s

  return s.rindex("1") + 1
end