ip2cidr 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ip2cidr.rb +70 -0
  3. data/test/test_ip2cidr.rb +46 -0
  4. metadata +51 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76f038939cf86804bbc76acdcb6849b7ab8274e7
4
+ data.tar.gz: c9602a93228f5c2a452c035087358773a3bee284
5
+ SHA512:
6
+ metadata.gz: 90837c65e8c4a1bfed92bd1bac323eee9cf7ea7429c2f3c3b5c965eca1f1d5d9b18756a23182e9ede84393504fe65bb2639473437bbb8bb6598bb92f53c7d410
7
+ data.tar.gz: c6426ea461a77d542128c2759f57f87027515c3a6abcf0c68ce9ff82c9472eaa7ca5ba8d0a8f94fd72e3b8ca222525c231545c25dbbbf8876c007746abd59262
@@ -0,0 +1,70 @@
1
+ class IPtoCIDR
2
+ # IPString2Long
3
+ # Convert an ip address in string format to integer.
4
+ def self.IPString2Long(ipstring)
5
+ ipstring = ipstring.split(".")
6
+ longip = Array.new(4)
7
+ for i in 0..3
8
+ longip[i] = ipstring[i].to_i
9
+ end
10
+ #return longip
11
+ return (longip[0] << 24) + (longip[1] << 16) +
12
+ (longip[2] << 8) + longip[3]
13
+ end
14
+
15
+ # Long2IPString
16
+ # Convert an ip address in integer format to string.
17
+ def self.Long2IPString(longip)
18
+ ipstring = ""
19
+ ipstring = (longip >> 24).to_s + "." +
20
+ ((longip & 0x00FFFFFF)>> 16).to_s + "." +
21
+ ((longip & 0x0000FFFF) >> 8).to_s + "." +
22
+ (longip & 0x000000FF).to_s
23
+ return ipstring
24
+ end
25
+
26
+ # IPRange2CIDR
27
+ # Return a list of cidr addresses given a range of ip
28
+ # addresses.
29
+ def self.IPRange2CIDR(startip, endip)
30
+ cidr2mask = [0x00000000, 0x80000000, 0xC0000000,
31
+ 0xE0000000, 0xF0000000, 0xF8000000,
32
+ 0xFC000000, 0xFE000000, 0xFF000000,
33
+ 0xFF800000, 0xFFC00000, 0xFFE00000,
34
+ 0xFFF00000, 0xFFF80000, 0xFFFC0000,
35
+ 0xFFFE0000, 0xFFFF0000, 0xFFFF8000,
36
+ 0xFFFFC000, 0xFFFFE000, 0xFFFFF000,
37
+ 0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00,
38
+ 0xFFFFFF00, 0xFFFFFF80, 0xFFFFFFC0,
39
+ 0xFFFFFFE0, 0xFFFFFFF0, 0xFFFFFFF8,
40
+ 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF]
41
+ startaddr = IPString2Long(startip)
42
+ endaddr = IPString2Long (endip)
43
+ cidrlist = Array.new
44
+
45
+ while endaddr >= startaddr
46
+ maxsize = 32
47
+ while maxsize > 0
48
+ mask = cidr2mask[maxsize - 1]
49
+ maskedbase = startaddr & mask
50
+
51
+ if maskedbase != startaddr
52
+ break
53
+ end
54
+
55
+ maxsize -= 1
56
+ end
57
+
58
+ x = Math.log(endaddr - startaddr + 1) / Math.log(2)
59
+ maxdiff = 32 - x.floor
60
+ if maxsize < maxdiff
61
+ maxsize = maxdiff
62
+ end
63
+
64
+ cidrlist.push(Long2IPString(startaddr) + "/" + maxsize.to_s)
65
+ startaddr += 2**(32 - maxsize)
66
+ end
67
+
68
+ return cidrlist
69
+ end
70
+ end
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'ip2cidr'
3
+
4
+ class CIDRCalculator::IPtoCIDRTest < Test::Unit::TestCase
5
+ def test_IPString2Long
6
+ assert_equal 0xc0a80001, CIDRCalculator::IPtoCIDR.IPString2Long("192.168.0.1").to_s(16)
7
+ assert_equal 0xffffffff, CIDRCalculator::IPtoCIDR.IPString2Long("255.255.255.255").to_s(16)
8
+ assert_equal 0x00000000, CIDRCalculator::IPtoCIDR.IPString2Long("0.0.0.0").to_s(16)
9
+ assert_equal 0xfefefefe, CIDRCalculator::IPtoCIDR.IPString2Long("254.254.254.254").to_s(16)
10
+ assert_equal 0x18ff0701, CIDRCalculator::IPtoCIDR.IPString2Long("24.255.7.1").to_s(16)
11
+ assert_equal 0xc23f0f60, CIDRCalculator::IPtoCIDR.IPString2Long("194.63.15.96").to_s(16)
12
+ assert_equal 0x2ef653ca, CIDRCalculator::IPtoCIDR.IPString2Long("46.246.83.202").to_s(16)
13
+ end
14
+
15
+ def test_Long2IPString
16
+ assert_equal "192.168.0.1", CIDRCalculator::IPtoCIDR.Long2IPString(0xc0a80001)
17
+ assert_equal "255.255.255.255", CIDRCalculator::IPtoCIDR.Long2IPString(0xffffffff)
18
+ assert_equal "0.0.0.0", CIDRCalculator::IPtoCIDR.Long2IPString(0x00000000)
19
+ assert_equal "254.254.254.254", CIDRCalculator::IPtoCIDR.Long2IPString(0xfefefefe)
20
+ assert_equal "24.255.7.1", CIDRCalculator::IPtoCIDR.Long2IPString(0x18ff0701)
21
+ assert_equal "194.63.15.96", CIDRCalculator::IPtoCIDR.Long2IPString(0xc23f0f60)
22
+ assert_equal "46.246.83.202", CIDRCalculator::IPtoCIDR.Long2IPString(0x2ef653ca)
23
+ end
24
+
25
+ def test_IPRange2CIDR
26
+ test2 = ["192.168.0.0/13", "192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8", "194.0.0.0/7",
27
+ "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4", "224.0.0.0/4", "240.0.0.0/5", "248.0.0.0/6",
28
+ "252.0.0.0/7", "254.0.0.0/8", "255.0.0.0/9", "255.128.0.0/11", "255.160.0.0/13", "255.168.0.0/29"]
29
+
30
+ test4 = ["0.168.0.1/32", "0.168.0.2/31", "0.168.0.4/30", "0.168.0.8/29", "0.168.0.16/28",
31
+ "0.168.0.32/27", "0.168.0.64/26", "0.168.0.128/25", "0.168.1.0/24",
32
+ "0.168.2.0/23", "0.168.4.0/22", "0.168.8.0/21", "0.168.16.0/20",
33
+ "0.168.32.0/19", "0.168.64.0/18", "0.168.128.0/17", "0.169.0.0/16",
34
+ "0.170.0.0/15", "0.172.0.0/14", "0.176.0.0/12", "0.192.0.0/10",
35
+ "1.0.0.0/8", "2.0.0.0/7", "4.0.0.0/6", "8.0.0.0/5", "16.0.0.0/4",
36
+ "32.0.0.0/3", "64.0.0.0/2", "128.0.0.0/2", "192.0.0.0/3", "224.0.0.0/4",
37
+ "240.0.0.0/5", "248.0.0.0/6", "252.0.0.0/7", "254.0.0.0/24"]
38
+
39
+ assert_equal "192.168.0.0/29", CIDRCalculator::IPtoCIDR.IPRange2CIDR("192.168.0.0", "192.168.0.7")
40
+ assert_equal test2, CIDRCalculator::IPtoCIDR.IPRange2CIDR("192.168.0.0", "255.168.0.7")
41
+ assert_equal "192.168.0.1/32", CIDRCalculator::IPtoCIDR.IPRange2CIDR("192.168.0.1", "192.168.0.1")
42
+ assert_equal test4, CIDRCalculator::IPtoCIDR.IPRange2CIDR("0.168.0.1", "254.0.0.255")
43
+ end
44
+ end
45
+
46
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip2cidr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlo Niño A. Bitoon
8
+ - Kirk Gandril B. Madraga
9
+ - John Patrick Suelto
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-04-28 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: Converts a range of IP Adrresses to a list of CIDR Adresses
16
+ email:
17
+ - carloninobitoon@yahoo.com
18
+ - kirkmadraga@outlook.com
19
+ - jp_suelto@yahoo.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/ip2cidr.rb
25
+ - test/test_ip2cidr.rb
26
+ homepage: http://rubygems.org/gems/iptocidr
27
+ licenses:
28
+ - ''
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.5
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Converts IP Range to CIDR
50
+ test_files:
51
+ - test/test_ip2cidr.rb