Class: IPAddr
Overview
A few augmentations to IPAddr handling.
Class Method Summary collapse
-
.jss_cidr_from_ends(starting, ending) ⇒ FixNum
Given starting and ending IPv4 IP addresses (either Strings or IPAddrs) return the CIDR notation routing prefix mask.
-
.jss_ending_address(starting, cidr) ⇒ IPAddr
Convert a starting address (either String or IPAddr) and a CIDR notation routing prefix mask into the IPv4 address of at the end of the range of addresses.
-
.jss_masked_v4addr(starting, ending) ⇒ IPAddr
Convert starting and ending IPv4 IP addresses (either Strings or IPAddrs) into a single masked IPv4 IPAddr.
Class Method Details
.jss_cidr_from_ends(starting, ending) ⇒ FixNum
Given starting and ending IPv4 IP addresses (either Strings or IPAddrs) return the CIDR notation routing prefix mask
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jss/ruby_extensions/ipaddr.rb', line 59 def self.jss_cidr_from_ends(starting,ending) starting = IPAddr.new(starting) unless starting.kind_of? IPAddr ending = IPAddr.new(ending) unless ending.kind_of? IPAddr ### how many possible addresses in the range? num_addrs = ending.to_i - starting.to_i + 1 ### convert the number of possible addresses to ### binary then subtract the number of bits from ### the full length of an IPv4 addr ### (32 bits) and that gives the CIDR prefix return 32 - num_addrs.to_s(2).length + 1 end |
.jss_ending_address(starting, cidr) ⇒ IPAddr
Convert a starting address (either String or IPAddr) and a CIDR notation routing prefix mask into the IPv4 address of at the end of the range of addresses.
88 89 90 |
# File 'lib/jss/ruby_extensions/ipaddr.rb', line 88 def self.jss_ending_address(starting, cidr) IPAddr.new( "#{starting}/#{cidr}").to_range.max end |
.jss_masked_v4addr(starting, ending) ⇒ IPAddr
Convert starting and ending IPv4 IP addresses (either Strings or IPAddrs) into a single masked IPv4 IPAddr
43 44 45 |
# File 'lib/jss/ruby_extensions/ipaddr.rb', line 43 def self.jss_masked_v4addr(starting,ending) IPAddr.new "#{starting}/#{self.jss_cidr_from_ends(starting,ending)}" end |