Module: IPArith
- Defined in:
- lib/iparith.rb,
lib/iparith/check.rb,
lib/iparith/parse.rb
Class Method Summary collapse
- .addr_to_int(str) ⇒ Object
- .ipstr_valid?(ipstr) ⇒ Boolean
- .macstr_valid?(macstr) ⇒ Boolean
- .parse_macstr(mac_str) ⇒ Object
- .prefix_valid?(ipstr) ⇒ Boolean
Class Method Details
.addr_to_int(str) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/iparith.rb', line 7 def self.addr_to_int(str) addr = ::IPAddr.new str addr.to_i rescue ::IPAddr::InvalidAddressError, ::IPAddr::AddressFamilyError nil end |
.ipstr_valid?(ipstr) ⇒ Boolean
26 27 28 29 30 31 |
# File 'lib/iparith/parse.rb', line 26 def self.ipstr_valid?(ipstr) ::IPAddr.new ipstr true rescue ::IPAddr::InvalidAddressError, ::IPAddr::AddressFamilyError nil end |
.macstr_valid?(macstr) ⇒ Boolean
3 4 5 6 7 8 9 10 |
# File 'lib/iparith/parse.rb', line 3 def self.macstr_valid?(macstr) return nil unless macstr.is_a? String rg_raw = /\A([a-f0-9]{12})\z/ mdata = rg_raw.match(macstr) mdata ? true : nil end |
.parse_macstr(mac_str) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/iparith/parse.rb', line 12 def self.parse_macstr(mac_str) return nil unless mac_str.is_a? String cleaned_str = mac_str.downcase cleaned_str.gsub!(/[-:.]/, '') cleaned_str.gsub!('с', 'c') # replace russian c rg_raw = /\A([a-f0-9]{12})\z/ mdata = rg_raw.match(cleaned_str) return mdata[1] if mdata nil end |
.prefix_valid?(ipstr) ⇒ Boolean
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/iparith/check.rb', line 3 def self.prefix_valid?(ipstr) netstr, mask = ipstr.split('/') pref = ::IPAddr.new ipstr # mask must be assumed /32 if not defined # /32 prefixes always valid in this check # so, we just return true after successfuly parse ipstr return true unless mask pref.to_s == netstr rescue ::IPAddr::InvalidAddressError, ::IPAddr::AddressFamilyError nil end |