Class: IPHandling
- Inherits:
-
Object
- Object
- IPHandling
- Defined in:
- lib/ip2cidr/iphandling.rb
Instance Method Summary collapse
-
#iprange_to_cidr(startip, endip) ⇒ Object
iprange_to_cidr Return a list of cidr addresses given a range of ip addresses.
-
#ipstring_to_long(ipstring) ⇒ Object
ipstring_to_long Convert an ip address in string format to integer.
-
#long_to_ipstring(longip) ⇒ Object
long_to_ipstring Convert an ip address in integer format to string.
Instance Method Details
#iprange_to_cidr(startip, endip) ⇒ Object
iprange_to_cidr Return a list of cidr addresses given a range of ip addresses.
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 62 63 64 65 66 67 68 69 70 |
# File 'lib/ip2cidr/iphandling.rb', line 30 def iprange_to_cidr(startip, endip) cidr2mask = [0x00000000, 0x80000000, 0xC0000000, 0xE0000000, 0xF0000000, 0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000, 0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000, 0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000, 0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000, 0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00, 0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0, 0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF] startaddr = ipstring_to_long(startip) endaddr = ipstring_to_long (endip) # Check if Ending IP Address is greater than Starting IP Address raise ArgumentError, 'Starting IP must be less than the end IP' unless startaddr < endaddr cidrlist = Array.new while endaddr >= startaddr maxsize = 32 while maxsize > 0 mask = cidr2mask[maxsize - 1] maskedbase = startaddr & mask break if maskedbase != startaddr maxsize -= 1 end x = Math.log(endaddr - startaddr + 1) / Math.log(2) maxdiff = 32 - x.floor maxsize = maxdiff if maxsize < maxdiff cidrlist.push(long_to_ipstring(startaddr) + "/" + maxsize.to_s) startaddr += 2**(32 - maxsize) end return cidrlist end |
#ipstring_to_long(ipstring) ⇒ Object
ipstring_to_long Convert an ip address in string format to integer.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/ip2cidr/iphandling.rb', line 4 def ipstring_to_long(ipstring) ipstring = ipstring.split(".") longip = Array.new(4) for i in 0..3 longip[i] = ipstring[i].to_i end #return longip return (longip[0] << 24) + (longip[1] << 16) + (longip[2] << 8) + longip[3] end |
#long_to_ipstring(longip) ⇒ Object
long_to_ipstring Convert an ip address in integer format to string.
18 19 20 21 22 23 24 25 |
# File 'lib/ip2cidr/iphandling.rb', line 18 def long_to_ipstring(longip) ipstring = "" ipstring = (longip >> 24).to_s + "." + ((longip & 0x00FFFFFF)>> 16).to_s + "." + ((longip & 0x0000FFFF) >> 8).to_s + "." + (longip & 0x000000FF).to_s return ipstring end |