class Whatmask
Copyright © 2015 Mike Gee (geezyx at gmail.com)
whatmask is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
whatmask is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with whatmask. If not, see <www.gnu.org/licenses/>.
Constants
- VERSION
whatmask version
Public Class Methods
new(mask, address=nil)
click to toggle source
# File lib/whatmask.rb, line 5 def initialize(mask, address=nil) @mask = get_mask(mask) if /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(address) @address = octets_to_bin(address) end @allones = "11111111111111111111111111111111".to_i(2) validate(@mask) end
Public Instance Methods
address()
click to toggle source
# File lib/whatmask.rb, line 14 def address return bin_to_octets(@address) if @address end
avail()
click to toggle source
# File lib/whatmask.rb, line 56 def avail return ((2**(32-cidr))-2) if cidr <= 30 return 1 end
broadcast()
click to toggle source
# File lib/whatmask.rb, line 22 def broadcast return bin_to_octets(get_broadcast) if @address end
cidr()
click to toggle source
# File lib/whatmask.rb, line 52 def cidr return @mask.to_s(2).count('1') end
first()
click to toggle source
# File lib/whatmask.rb, line 26 def first if @mask != @allones return bin_to_octets(get_network+1) if @address end return address if @address end
hex()
click to toggle source
# File lib/whatmask.rb, line 48 def hex return bin_to_hex(@mask) end
inverse()
click to toggle source
# File lib/whatmask.rb, line 40 def inverse return bin_to_octets(get_inverse) end
last()
click to toggle source
# File lib/whatmask.rb, line 33 def last if @mask != @allones return bin_to_octets(get_broadcast-1) if @address end return address if @address end
mask()
click to toggle source
# File lib/whatmask.rb, line 44 def mask return bin_to_octets(@mask) end
network()
click to toggle source
# File lib/whatmask.rb, line 18 def network return bin_to_octets(get_network) if @address end
Private Instance Methods
bin_to_32bit(bin)
click to toggle source
# File lib/whatmask.rb, line 109 def bin_to_32bit(bin) bits = bin.to_s(2) while bits.length < 32 bits = '0'+bits end return bits if (bits.length <= 32) abort 'whatmask: "'+bin+'" is greater than 32 bits!' end
bin_to_hex(bin)
click to toggle source
# File lib/whatmask.rb, line 126 def bin_to_hex(bin) bin = bin.to_s(2) while bin.length < 32 bin = '0'+bin end return '0x'+bin.scan(/.{8}/).map { |b| ('0'+b.to_i(2).to_s(16))[-2,2] }.join end
bin_to_octets(bin)
click to toggle source
# File lib/whatmask.rb, line 118 def bin_to_octets(bin) bin = bin.to_s(2) while bin.length < 32 bin = '0'+bin end return bin.scan(/.{8}/).map { |b| b.to_i(2) }.join('.') end
cidr_to_bin(cidr)
click to toggle source
# File lib/whatmask.rb, line 147 def cidr_to_bin(cidr) bin = '' if (0..32) === cidr.to_i for i in 0..(cidr.to_i-1) bin = '1'+bin end while bin.length < 32 bin = bin+'0' end return bin.to_i(2) else abort 'whatmask: '+cidr+' is an invalid CIDR mask!' end end
get_broadcast()
click to toggle source
# File lib/whatmask.rb, line 71 def get_broadcast return (get_network | get_inverse) end
get_inverse()
click to toggle source
# File lib/whatmask.rb, line 67 def get_inverse return (@mask ^ @allones) end
get_mask(mask)
click to toggle source
# File lib/whatmask.rb, line 97 def get_mask(mask) if (/\h\h\h\h\h\h\h\h/ =~ mask) == 2 return mask.hex elsif mask.length <= 2 return cidr_to_bin(mask) elsif /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(mask) return octets_to_bin(mask) else abort 'whatmask: "'+mask+'" is not a valid subnet mask or wildcard bit mask!' end end
get_network()
click to toggle source
# File lib/whatmask.rb, line 63 def get_network return (@address & @mask) end
octets_to_bin(octets)
click to toggle source
# File lib/whatmask.rb, line 134 def octets_to_bin(octets) bin = octets.split('.').map { |octet| octet = octet.to_i.to_s(2) if octet.length > 8 abort 'whatmask: "'+octets+'" is not a valid subnet mask or wildcard bit mask, or ip address!' end while octet.length < 8 octet = '0'+octet end octet }.join.to_i(2) return bin end
validate(mask)
click to toggle source
# File lib/whatmask.rb, line 75 def validate(mask) mask = bin_to_32bit(mask) if mask[0] == '1' if /1/.match(mask.sub(/1+/, '0')) == nil return 1 else abort 'whatmask: "'+mask+'" is not a valid subnet mask or wildcard bit mask!' end elsif mask == '0'*32 return 1 elsif mask[0] == '0' if /0/.match(mask.sub(/0+/, '1')) == nil @mask = get_inverse return 1 else abort 'whatmask: "'+mask+'" is not a valid subnet mask or wildcard bit mask!' end else abort 'whatmask: error encountered in validation' end end