ipaddr-tree 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +17 -0
- data/Rakefile +23 -0
- data/ipaddr-tree.gemspec +20 -0
- data/lib/ipaddr/tree.rb +46 -0
- data/lib/ipaddr/tree/range.rb +26 -0
- data/lib/ipaddr/tree/version.rb +4 -0
- data/spec/ipaddr/tree/range_spec.rb +58 -0
- data/spec/ipaddr/tree_spec.rb +41 -0
- data/spec/spec_helper.rb +7 -0
- metadata +83 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Charles Lowell
|
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,17 @@
|
|
1
|
+
# Ipaddr::Tree
|
2
|
+
|
3
|
+
Map IP address space onto a binary tree
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ipaddr-tree'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ipaddr-tree
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
task :console do
|
5
|
+
def iprange(start, finish)
|
6
|
+
IPAddr.new(start).blocks_until(finish).map(&:cidr_notation)
|
7
|
+
end
|
8
|
+
|
9
|
+
$:.unshift File.expand_path '../lib', __FILE__
|
10
|
+
require 'pry'
|
11
|
+
require 'ipaddr/tree'
|
12
|
+
require 'ipaddr/tree/range'
|
13
|
+
Pry.cli = true
|
14
|
+
Pry.start binding, {
|
15
|
+
:prompt => [
|
16
|
+
proc {|obj, *| "ip::tree> "},
|
17
|
+
proc {|obj, *| "ip::tree* "}
|
18
|
+
],
|
19
|
+
:hooks => Pry::Hooks.from_hash({
|
20
|
+
:before_session => proc {|output, *| output.puts "IPAddr::Tree kicks the ass!"}
|
21
|
+
})
|
22
|
+
}
|
23
|
+
end
|
data/ipaddr-tree.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ipaddr/tree/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Charles Lowell"]
|
6
|
+
gem.email = ["cowboyd@thefrontside.net"]
|
7
|
+
gem.description = %q{Map IP Space onto a binary tree}
|
8
|
+
gem.summary = %q{Sometimes it's helpful to be able to navigate around CIDR space like you would a tree}
|
9
|
+
gem.homepage = "http://github.com/summon/ipaddr-tree.rb"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "ipaddr-tree"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = IPAddr::Tree::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
gem.add_development_dependency "pry"
|
20
|
+
end
|
data/lib/ipaddr/tree.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'ipaddr/tree/version'
|
2
|
+
|
3
|
+
class IPAddr
|
4
|
+
module Tree
|
5
|
+
def parent
|
6
|
+
mask prefix_length - 1 if prefix_length > 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def next_sibling
|
10
|
+
dup.set(@addr + 2 ** block_length) if @addr < (2 ** family_length) - 1
|
11
|
+
end
|
12
|
+
|
13
|
+
def previous_sibling
|
14
|
+
dup.set(@addr - 2 ** block_length) if @addr > 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def left_child
|
18
|
+
mask prefix_length + 1 if block_length > 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def right_child
|
22
|
+
mask(prefix_length + 1).set(@addr + 2 ** (block_length - 1)) if block_length > 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def leaf?
|
26
|
+
prefix_length == family_length
|
27
|
+
end
|
28
|
+
|
29
|
+
def cidr_notation
|
30
|
+
"#{to_s}/#{prefix_length}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def prefix_length
|
34
|
+
family_length - block_length
|
35
|
+
end
|
36
|
+
|
37
|
+
def block_length
|
38
|
+
Math.log2(addr_mask(~@mask_addr) + 1).to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
def family_length
|
42
|
+
ipv4? ? 32 : 128
|
43
|
+
end
|
44
|
+
end
|
45
|
+
include Tree
|
46
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'ipaddr/tree'
|
2
|
+
|
3
|
+
class IPAddr
|
4
|
+
module Tree::Range
|
5
|
+
def blocks_until(other)
|
6
|
+
other = IPAddr.new(other.to_s) unless other.is_a?(IPAddr)
|
7
|
+
return other.blocks_until(self) if other < self
|
8
|
+
if include? other
|
9
|
+
if next_sibling.include? other.next_sibling
|
10
|
+
[self]
|
11
|
+
elsif left_child && left_child.include?(other)
|
12
|
+
left_child.blocks_until(other)
|
13
|
+
elsif leaf?
|
14
|
+
[self]
|
15
|
+
else
|
16
|
+
[left_child] + right_child.blocks_until(other)
|
17
|
+
end
|
18
|
+
elsif parent.include? previous_sibling
|
19
|
+
[self] + next_sibling.blocks_until(other)
|
20
|
+
else
|
21
|
+
parent.blocks_until(other)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
include Tree::Range
|
26
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ipaddr/tree/range'
|
3
|
+
|
4
|
+
describe IPAddr::Tree::Range do
|
5
|
+
|
6
|
+
it "calculates a full tree" do
|
7
|
+
iprange('0.0.0.4', '0.0.0.7').should eql ['0.0.0.4/30']
|
8
|
+
end
|
9
|
+
|
10
|
+
it "calculates that is full on the left" do
|
11
|
+
iprange('0.0.0.3', '0.0.0.7').should eql ['0.0.0.3/32', '0.0.0.4/30']
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'generates a messed up little range' do
|
15
|
+
iprange('0.0.0.3', '0.0.0.9').should eql ['0.0.0.3/32', '0.0.0.4/30', '0.0.0.8/31']
|
16
|
+
end
|
17
|
+
|
18
|
+
it "generates a range that's clipped down to a single node" do
|
19
|
+
iprange('0.0.0.2', '0.0.0.8').should eql ['0.0.0.2/31', '0.0.0.4/30', '0.0.0.8/32']
|
20
|
+
end
|
21
|
+
|
22
|
+
it "generates a range that's clipped down to a single node" do
|
23
|
+
iprange('0.0.0.2', '0.0.0.9').should eql ['0.0.0.2/31', '0.0.0.4/30', '0.0.0.8/31']
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'ahem' do
|
27
|
+
iprange('0.0.0.0/29', '0.0.0.6/32').should eql ["0.0.0.0/30", "0.0.0.4/31", "0.0.0.6/32"]
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'handles can handle weirdness' do
|
31
|
+
iprange('0.0.0.4', '0.0.0.6').should eql ['0.0.0.4/31', '0.0.0.6/32']
|
32
|
+
iprange('155.48.168.0','155.48.171.0').should eql [
|
33
|
+
'155.48.168.0/23',
|
34
|
+
'155.48.170.0/24',
|
35
|
+
'155.48.171.0/32'
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "handles some real ranges from the wild" do
|
40
|
+
iprange('64.70.116.50', '64.70.116.54').should eql ['64.70.116.50/31', '64.70.116.52/31', '64.70.116.54/32']
|
41
|
+
iprange('212.138.110.1','212.138.110.128').should eql [
|
42
|
+
'212.138.110.1/32',
|
43
|
+
'212.138.110.2/31',
|
44
|
+
'212.138.110.4/30',
|
45
|
+
'212.138.110.8/29',
|
46
|
+
'212.138.110.16/28',
|
47
|
+
'212.138.110.32/27',
|
48
|
+
'212.138.110.64/26',
|
49
|
+
'212.138.110.128/32'
|
50
|
+
]
|
51
|
+
iprange('155.48.0.0', '155.48.255.255')
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def iprange(start, finish)
|
56
|
+
IPAddr.new(start).blocks_until(IPAddr.new(finish)).map(&:cidr_notation)
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe IPAddr::Tree do
|
4
|
+
|
5
|
+
describe "a middling node like 0.0.0.4/30" do
|
6
|
+
subject {IPAddr.new "0.0.0.4/30"}
|
7
|
+
its(:parent) {should match_cidr "0.0.0.0/29"}
|
8
|
+
its(:left_child) {should match_cidr "0.0.0.4/31"}
|
9
|
+
its(:right_child) {should match_cidr "0.0.0.6/31"}
|
10
|
+
its(:next_sibling) {should match_cidr "0.0.0.8/30"}
|
11
|
+
its(:previous_sibling) {should match_cidr "0.0.0.0/30"}
|
12
|
+
it {should_not be_leaf}
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "a leaf node like 0.0.0.7/32" do
|
16
|
+
subject {IPAddr.new '0.0.0.7/32'}
|
17
|
+
its(:left_child) {should be_nil}
|
18
|
+
its(:right_child) {should be_nil}
|
19
|
+
it {should be_leaf}
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "the far left corner" do
|
23
|
+
subject {IPAddr.new "0.0.0.0/32"}
|
24
|
+
its(:previous_sibling) {should be_nil}
|
25
|
+
its(:next_sibling) {should match_cidr '0.0.0.1/32'}
|
26
|
+
it {should be_leaf}
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "the far right corner" do
|
30
|
+
subject {IPAddr.new '255.255.255.255'}
|
31
|
+
its(:next_sibling) {should be_nil}
|
32
|
+
its(:previous_sibling) {should match_cidr '255.255.255.254/32'}
|
33
|
+
it {should be_leaf}
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "the root of the tree" do
|
37
|
+
subject {IPAddr.new '0.0.0.0/0'}
|
38
|
+
its(:parent) {should be_nil}
|
39
|
+
it {should_not be_leaf}
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ipaddr-tree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Charles Lowell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2156680780 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156680780
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: pry
|
27
|
+
requirement: &2156680120 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156680120
|
36
|
+
description: Map IP Space onto a binary tree
|
37
|
+
email:
|
38
|
+
- cowboyd@thefrontside.net
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- ipaddr-tree.gemspec
|
49
|
+
- lib/ipaddr/tree.rb
|
50
|
+
- lib/ipaddr/tree/range.rb
|
51
|
+
- lib/ipaddr/tree/version.rb
|
52
|
+
- spec/ipaddr/tree/range_spec.rb
|
53
|
+
- spec/ipaddr/tree_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: http://github.com/summon/ipaddr-tree.rb
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.10
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Sometimes it's helpful to be able to navigate around CIDR space like you
|
79
|
+
would a tree
|
80
|
+
test_files:
|
81
|
+
- spec/ipaddr/tree/range_spec.rb
|
82
|
+
- spec/ipaddr/tree_spec.rb
|
83
|
+
- spec/spec_helper.rb
|