ext_ipaddr 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/ext_ipaddr.gemspec +25 -0
- data/lib/ext_ipaddr/version.rb +3 -0
- data/lib/ext_ipaddr.rb +62 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 489e2b7c5c0b4b23ad5897f4804a53bcc7675319
|
4
|
+
data.tar.gz: f0b9de82f716849545655954e905ec6b17806fd8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2cf40c1b9232ab710c6ab3f94a55e8aaf0430e82bb553fa1dd29beed95057e4493bf65b3cfafbade7261124e077e58fb6788015cbeefa3eacfde3851af39f833
|
7
|
+
data.tar.gz: af6fb99eaeb8d33f14c594069c1c1a99bf257be79cdcc6ab4c62bec3ba7ad0dcdfcf7a2b3f63c7f73e029157e60cc19972d8476cff289c3bda314acc0b20e6e9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# ExtIPAddr
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ext_ipaddr`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ext_ipaddr'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ext_ipaddr
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ext_ipaddr.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.libs << "lib"
|
10
|
+
t.test_files = FileList['test/**/test_*.rb']
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => [:spec, :test]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ext_ipaddr"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/ext_ipaddr.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ext_ipaddr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ext_ipaddr"
|
8
|
+
spec.version = ExtIPAddr::VERSION
|
9
|
+
spec.authors = ["Shu Sugimoto"]
|
10
|
+
spec.email = ["shu@su.gimo.to"]
|
11
|
+
|
12
|
+
spec.summary = %q{Monkey patch for built-in IPAddr class: CIDR support}
|
13
|
+
spec.description = %q{Monkey patch for built-in IPAddr class: CIDR support}
|
14
|
+
spec.homepage = "https://github.com/s2ugimot/ext_ipaddr"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "test-unit"
|
25
|
+
end
|
data/lib/ext_ipaddr.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "ext_ipaddr/version"
|
2
|
+
|
3
|
+
require 'ipaddr'
|
4
|
+
|
5
|
+
IPAddr.class_eval do
|
6
|
+
|
7
|
+
alias_method :_initialize, :initialize
|
8
|
+
def initialize(addr='::', family=Socket::AF_UNSPEC)
|
9
|
+
_initialize(addr, family)
|
10
|
+
|
11
|
+
# DIRTY HACK
|
12
|
+
# IPAddr by default cannot handle host address with prefix
|
13
|
+
# e.g. 192.168.0.1/24
|
14
|
+
# when passing above to the original constructor,
|
15
|
+
# IPAddr returns 192.168.0.0/24
|
16
|
+
# but using "|" operator somehow makes it possible, LOL
|
17
|
+
# here we overwrite @addr again with the correct host address
|
18
|
+
if addr.kind_of?(String)
|
19
|
+
address, prefixlen = addr.split('/')
|
20
|
+
if prefixlen
|
21
|
+
@addr |= IPAddr.new(address, family).to_i
|
22
|
+
# this means "192.168.0.0/24" | "192.168.0.1/32"
|
23
|
+
# then we get "192.168.0.1/24"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def mask_address
|
29
|
+
IPAddr.new(@mask_addr, @family)
|
30
|
+
end
|
31
|
+
|
32
|
+
def prefix
|
33
|
+
begin_addr = (@addr & @mask_addr)
|
34
|
+
|
35
|
+
case @family
|
36
|
+
when Socket::AF_INET
|
37
|
+
end_addr = (@addr | (self::class::IN4MASK ^ @mask_addr))
|
38
|
+
return 32 - Math.log(end_addr - begin_addr + 1, 2).to_i
|
39
|
+
when Socket::AF_INET6
|
40
|
+
end_addr = (@addr | (self::class::IN6MASK ^ @mask_addr))
|
41
|
+
return 128 - Math.log(end_addr - begin_addr + 1, 2).to_i
|
42
|
+
else
|
43
|
+
raise AddressFamilyError, "unsupported address family"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_cidr_s
|
48
|
+
return to_s + "/#{prefix}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def ==(other)
|
52
|
+
return false unless other.class == IPAddr
|
53
|
+
self && other && @addr == other.to_i && @mask_addr == other.mask_addr
|
54
|
+
end
|
55
|
+
|
56
|
+
protected
|
57
|
+
|
58
|
+
def mask_addr
|
59
|
+
return @mask_addr
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ext_ipaddr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shu Sugimoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: test-unit
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: 'Monkey patch for built-in IPAddr class: CIDR support'
|
70
|
+
email:
|
71
|
+
- shu@su.gimo.to
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- ext_ipaddr.gemspec
|
85
|
+
- lib/ext_ipaddr.rb
|
86
|
+
- lib/ext_ipaddr/version.rb
|
87
|
+
homepage: https://github.com/s2ugimot/ext_ipaddr
|
88
|
+
licenses: []
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.5.1
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: 'Monkey patch for built-in IPAddr class: CIDR support'
|
110
|
+
test_files: []
|