cidrmergerb 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61a72484791fa6a70fabda31cfad10454b80aa03
4
- data.tar.gz: 0583b3315b9829f120e562ba10d2d310b4feda92
3
+ metadata.gz: 9d1eaf7bb0f73fddd4c5c1832e75ae5201f801d8
4
+ data.tar.gz: 7eebdcd2ccf434e84cf62ca0e06395abacf0a936
5
5
  SHA512:
6
- metadata.gz: eb8e08e2f82f7b83c080fa7ecb126d66d25cb50112ac5a8979d91524dcc068463b5716b28a2b9ad034d1194614cc487246ff8cccb7b8e1a2bc9d674830d4d41e
7
- data.tar.gz: 2d1180a36337d7de672bbd3178d600d380b57a892c0b7c1173376c40f2d0066d2874f2dffd96013afc9048297997c040f7414795960ecc296b40b8e632be10b6
6
+ metadata.gz: ee282dcf4819ecb41e03962ad7138e463cf9adb4bf0115e88048aec666a84b9b60f5c0deb3e665e9e12874002c7dc57badd72a911e108921c3d2ea0372600d26
7
+ data.tar.gz: 3bed5a3f733d7e020bd64ccb309d1abed9f24ada62a9d40fedce6b332f7af3aadbceb0b37d17c5a922b076134df9ab658eb763828299a7c237643dd39a6bfffe
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ 0.1.3
2
+
3
+ + Add helper methods(.netmask_to_int, .host_size_for_netmask, .netmask_valid?, .ip_to_int, .int_to_ip, .cidr_to_ip_netmask, .cidr_to_range)
4
+ + Add CIDR Random test
5
+
6
+ 0.1.2
7
+
8
+ + Degrade ffi dependency version
9
+
10
+ 0.1.1
11
+
12
+ + bugfix: catch IPAddr::Error
13
+
14
+ 0.1.0
15
+
16
+ + support #optimize
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cidrmergerb (0.1.1)
4
+ cidrmergerb (0.1.3)
5
5
  ffi (~> 1.0)
6
6
 
7
7
  GEM
@@ -9,6 +9,7 @@ GEM
9
9
  specs:
10
10
  ffi (1.9.10)
11
11
  minitest (5.9.0)
12
+ netaddr (1.5.1)
12
13
  rake (10.5.0)
13
14
 
14
15
  PLATFORMS
@@ -18,6 +19,7 @@ DEPENDENCIES
18
19
  bundler (~> 1.11)
19
20
  cidrmergerb!
20
21
  minitest (~> 5.0)
22
+ netaddr
21
23
  rake (~> 10.0)
22
24
 
23
25
  BUNDLED WITH
data/README.md CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ ```
23
24
  Cidrmergerb.optimize(["10.1.1.0/24", "10.1.1.1/32", "192.1.4.5/32", "192.1.4.4/32"]) #=> ['10.1.1.0/24', '192.1.4.4/31']
25
+ ```
24
26
 
25
27
  more example: [cidrmergerb_test.rb](./test/cidrmergerb_test.rb)
data/Rakefile CHANGED
@@ -7,10 +7,15 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
+ Rake::TestTask.new(:bench) do |t|
11
+ t.libs = %w(lib test)
12
+ t.pattern = 'test/**/*_benchmark.rb'
13
+ end
14
+
10
15
  desc "clean shared library"
11
16
  task :clean_library do
12
17
  FileUtils.cd(File.join(File.dirname(__FILE__), 'ext/cidrmergerb'))
13
18
  %x{make clean}
14
19
  end
15
20
 
16
- task :default => [:clean_library, :spec]
21
+ task :default => :test
data/cidrmergerb.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.11"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "netaddr"
26
27
  end
@@ -0,0 +1,24 @@
1
+ module Cidrmergerb
2
+ module Cidr
3
+
4
+ # Split CIDR to ip & netmask
5
+ #
6
+ # @example
7
+ # cidr_to_ip_netmask('1.2.3.0/24') #=> ['1.2.3.0', 24]
8
+ def cidr_to_ip_netmask(cidr)
9
+ ip, netmask = cidr.split('/')
10
+ return [ip, netmask.to_i]
11
+ end
12
+
13
+ # Convert CIDR to IP Range with int format
14
+ #
15
+ # @example
16
+ # cidr_to_range('1.2.3.0/24') #=> 16909056...16909312
17
+ # cidr_to_range('1.2.3.4/24') #=> 16909056...16909312
18
+ def cidr_to_range(cidr)
19
+ ip, netmask = cidr_to_ip_netmask(cidr)
20
+ first = ip_to_int(ip) & netmask_to_int(netmask)
21
+ return first...(first + host_size_for_netmask(netmask))
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require 'ipaddr'
2
+
3
+ module Cidrmergerb
4
+ module Ip
5
+ # Convert ip from str to int
6
+ def ip_to_int(ip)
7
+ IPAddr.new(ip).to_i
8
+ end
9
+
10
+ # Convert ip from int to str
11
+ #
12
+ # Benchmark: test/ip_benchmark.rb
13
+ # bench_ipaddr_ip_convert 0.000031 0.000021 0.000015 0.000021 0.000016
14
+ # bench_simple_ip_convert 0.000010 0.000009 0.000010 0.000007 0.000006
15
+ #
16
+ # @return [ip]
17
+ def int_to_ip(int)
18
+ ip1 = (int & 0xff000000) >> 24
19
+ ip2 = (int & 0x00ff0000) >> 16
20
+ ip3 = (int & 0x0000ff00) >> 8
21
+ ip4 = (int & 0x000000ff)
22
+ "#{ip1}.#{ip2}.#{ip3}.#{ip4}"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module Cidrmergerb
2
+ module Netmask
3
+ class NetmaskInvalidError < StandardError; end
4
+
5
+ # Convert netmask to integer
6
+ #
7
+ # @example
8
+ # netmask_to_int(24) #=> 4294967040
9
+ def netmask_to_int(netmask)
10
+ validate(netmask)
11
+ ('1' * netmask).to_i(2) << (32 - netmask)
12
+ end
13
+
14
+ # Get most host size base on netmask
15
+ #
16
+ # @example
17
+ # host_size_for_netmask(24) #=> 256
18
+ # host_size_for_netmask(32) #=> 1
19
+ #
20
+ def host_size_for_netmask(netmask)
21
+ validate(netmask)
22
+ 2**(32 - netmask)
23
+ end
24
+
25
+ # Check netmask valid is or not
26
+ #
27
+ # @example
28
+ # netmask_valid?(100) #=> false
29
+ # netmask_valid?(24) #=> true
30
+ def netmask_valid?(netmask)
31
+ netmask.between?(1, 32)
32
+ end
33
+
34
+ private
35
+ def validate(netmask)
36
+ fail NetmaskInvalidError, "netmask must between [1, 32]" unless netmask_valid?(netmask)
37
+ end
38
+
39
+ end
40
+ end
41
+
@@ -1,3 +1,3 @@
1
1
  module Cidrmergerb
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/cidrmergerb.rb CHANGED
@@ -1,11 +1,21 @@
1
1
  require "cidrmergerb/version"
2
2
  require 'cidrmergerb/ext'
3
+ require 'cidrmergerb/ip'
4
+ require 'cidrmergerb/cidr'
5
+ require 'cidrmergerb/netmask'
3
6
  require 'ipaddr'
4
7
  require 'ffi'
5
8
 
6
9
  module Cidrmergerb
10
+ extend Ip
11
+ extend Cidr
12
+ extend Netmask
7
13
 
8
14
  class << self
15
+ # Optimize CIDRs
16
+ #
17
+ # @example
18
+ # optimize(['1.2.4.0/24', '1.2.5.0/24']) #=> ['1.2.4.0/23']
9
19
  def optimize(cidrs)
10
20
  pointer = build_pointer_to_cidrs(cidrs)
11
21
  optimize_length = Cidrmergerb::Ext.optimize(pointer, cidrs.length, 1)
@@ -22,18 +32,18 @@ module Cidrmergerb
22
32
  entries = wrap_with_struct(pointer, Cidrmergerb::Ext::Entry, cidrs.length)
23
33
 
24
34
  cidrs.each_with_index do |cidr, index|
25
- ip, netmask = cidr.split('/')
26
- entries[index][:network] = IPAddr.new(ip).to_i
27
- entries[index][:prefix] = netmask.to_i
35
+ ip, netmask = cidr_to_ip_netmask(cidr)
36
+ entries[index][:network] = ip_to_int(ip)
37
+ entries[index][:prefix] = netmask
28
38
  end
29
39
 
30
40
  pointer
31
41
  end
32
42
 
33
43
  def cidr_valid?(cidr)
34
- ip, netmask = cidr.split('/')
44
+ ip, netmask = cidr_to_ip_netmask(cidr)
35
45
  ipaddr = IPAddr.new(ip)
36
- ipaddr && netmask.to_i.between?(1, 32)
46
+ ipaddr && netmask_valid?(netmask)
37
47
  rescue IPAddr::Error => e
38
48
  false
39
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cidrmergerb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Newell Zhu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-01 00:00:00.000000000 Z
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: netaddr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: CIDR Merge toolkit -- Wrapping for Daniele's CIDR Optimization library.
70
84
  email:
71
85
  - newell.zhu@kingaxis.com
@@ -74,6 +88,7 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".travis.yml"
91
+ - CHANGELOG.md
77
92
  - Gemfile
78
93
  - Gemfile.lock
79
94
  - README.md
@@ -86,7 +101,10 @@ files:
86
101
  - ffi/libcidrmerge.c
87
102
  - ffi/optimized-sort.h
88
103
  - lib/cidrmergerb.rb
104
+ - lib/cidrmergerb/cidr.rb
89
105
  - lib/cidrmergerb/ext.rb
106
+ - lib/cidrmergerb/ip.rb
107
+ - lib/cidrmergerb/netmask.rb
90
108
  - lib/cidrmergerb/version.rb
91
109
  homepage: https://github.com/kingaxis/cidrmergerb
92
110
  licenses: []