cidr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 610a3eb0b944e589248c9ed9943b7a6844bbc294ab37f8ab37644ea8944359a2
4
+ data.tar.gz: 449db9698b3e4a7723feb499fb7c2ec9459d0408aff0446d9d24b93480775872
5
+ SHA512:
6
+ metadata.gz: a6fb99be6b09b4638f3c7eebdf991e4ddcb575136b90cb5b81c3c568e625f89fcf4b89ad8b47f87ffc7f0b625e8ffa7f0c9e5d4b6fdebb476aa799fb5bc9b8a8
7
+ data.tar.gz: 138e252b5e8666b88e593f06acd2b720cbbbcad6c224e31347e54db1a70332d4a0fedc96ab952266f3d0f71043f8321271fbf6de8c7031832b22eae687285d84
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cidr.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 izawa@izawa.org (Yukimitsu Izawa)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Cidr
2
+
3
+ CIDR aggregator for IPv4 addresses
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cidr', :git => "https://github.com/izawa/cidr.git"
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cidr
18
+
19
+ ## Usage
20
+
21
+ require 'cidr'
22
+
23
+ aggregator = Cidr::Aggregator.new
24
+
25
+ aggregator.add('192.168.0.1')
26
+ aggregator.add('192.168.0.2')
27
+ aggregator.add('192.168.0.3')
28
+ aggregator.add('192.168.0.4')
29
+ aggregator.add('192.168.0.5')
30
+ aggregator.add('192.168.0.6')
31
+ aggregator.add('192.168.0.7')
32
+
33
+ aggregator.list
34
+ #=> [{:addr=>"192.168.0.1", :prefix=>32},
35
+ {:addr=>"192.168.0.2", :prefix=>31},
36
+ {:addr=>"192.168.0.4", :prefix=>30}]
37
+
38
+ aggregator.add will be able to recognize the input string in the following format.
39
+
40
+ * 192.168.0.1 (without prefix)
41
+ * 192.168.0.0/24 (with prefix)
42
+
43
+ 'xxx.xxx.xxx.xxx' and 'xxx.xxx.xxx.xxx/32' are assumed to be identical.
44
+
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/cidr.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/cidr/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["いざわゆきみつ (Yukimitsu Izawa)"]
6
+ gem.email = ["izawa@izawa.org"]
7
+ gem.description = %q{IPv4 CIDR aggregator for IPv4 addresses}
8
+ gem.summary = %q{IPv4 CIDR aggregator for IPv4 addresses}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "cidr"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Cidr::VERSION
17
+ end
data/lib/cidr.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "cidr/version"
2
+ require "cidr/aggregator"
3
+
4
+ module Cidr
5
+
6
+ end
@@ -0,0 +1,107 @@
1
+ module Cidr
2
+
3
+ class Aggregator
4
+ require 'ipaddr'
5
+
6
+ ##
7
+ ## private methods
8
+ ##
9
+
10
+ private
11
+
12
+ def initialize
13
+ @addr_pool = Array.new(32) {[]}
14
+ end
15
+
16
+ def _merge_address (pool, next_pool, hostmask)
17
+ i=0
18
+ while(i <= pool.length-2)
19
+ modified_flag = false
20
+ if (pool[i] >> hostmask) ^ (pool[i+1] >> hostmask) == 1 then
21
+ next_pool.push pool[i]
22
+ pool.delete_at i
23
+ pool.delete_at i
24
+ modified_flag = true
25
+ end
26
+ i+=1
27
+ i-=1 if modified_flag
28
+ end
29
+ end
30
+
31
+ def _store_ipaddr_str (ip)
32
+ begin
33
+ ipa = IPAddr.new(ip,Socket::AF_INET)
34
+ rescue
35
+ return nil
36
+ end
37
+ prefixlen = _netmask2prefixlen(ipa.inspect.match(/^.*\/(.*)\>/)[1])
38
+ @addr_pool[32 - prefixlen].push ipa.to_i
39
+ end
40
+
41
+ def _store_ipaddr_int (ip)
42
+ if 2**32 > ip && ip >= 0 then
43
+ @addr_pool[0].push ip
44
+ else
45
+ return nil
46
+ end
47
+ end
48
+
49
+ def _countbits (bits)
50
+ bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
51
+ bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
52
+ bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
53
+ bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
54
+ return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
55
+ end
56
+
57
+ def _netmask2prefixlen (netmask)
58
+ prefix=0
59
+ netmask.split('.').each{|bits|
60
+ prefix += _countbits (bits.to_i)
61
+ }
62
+ return prefix
63
+ end
64
+
65
+ def _aggregate
66
+ for i in 0..30 do
67
+ if @addr_pool[i].length > 1
68
+ @addr_pool[i].sort!
69
+ @addr_pool[i].uniq!
70
+ _merge_address(@addr_pool[i], @addr_pool[i+1], i)
71
+ end
72
+ end
73
+ end
74
+
75
+
76
+ ##
77
+ ## public methods
78
+ ##
79
+
80
+ public
81
+
82
+ def add (ip)
83
+ case ip.class.to_s
84
+ when 'String'
85
+ return nil unless _store_ipaddr_str (ip)
86
+ when 'Fixnum'
87
+ return nil unless _store_ipaddr_int (ip)
88
+ else
89
+ return nil
90
+ end
91
+ end
92
+
93
+ def list
94
+ _aggregate
95
+ return_addrs = Array.new
96
+
97
+ for i in 0..31 do
98
+ @addr_pool[i].each {|ipaddr|
99
+ return_addrs.push ({:addr => IPAddr.new(ipaddr,Socket::AF_INET).to_s,
100
+ :prefix => 32-i})
101
+ }
102
+ end
103
+ return_addrs
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,3 @@
1
+ module Cidr
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cidr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - いざわゆきみつ (Yukimitsu Izawa)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: IPv4 CIDR aggregator for IPv4 addresses
14
+ email:
15
+ - izawa@izawa.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - cidr.gemspec
26
+ - lib/cidr.rb
27
+ - lib/cidr/aggregator.rb
28
+ - lib/cidr/version.rb
29
+ homepage: ''
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.1.6
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: IPv4 CIDR aggregator for IPv4 addresses
51
+ test_files: []