ip_methods 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.
@@ -0,0 +1,3 @@
1
+ == 2010-09-24 version 0.1.0
2
+
3
+ * initial version
@@ -0,0 +1,11 @@
1
+ = Introduction
2
+
3
+ ip_methods is a gem for helping dealing with IP operations
4
+
5
+ = Basics
6
+
7
+ Here's what you need to know before starting.
8
+
9
+ == Example Usage
10
+
11
+
@@ -0,0 +1,52 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/lib")
2
+
3
+ require "rake"
4
+ require "spec/rake/spectask"
5
+ require "ip_methods/version"
6
+
7
+ begin
8
+ require "hanna/rdoctask"
9
+ rescue LoadError => e
10
+ require "rake/rdoctask"
11
+ end
12
+
13
+ begin
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "ip_methods"
17
+ gem.version = IpMethods::Version::STRING
18
+ gem.summary = %Q{IpMethods gem for helping with IP operations}
19
+ gem.description = %Q{Provides an easy way for dealing with IP operations!}
20
+ gem.email = "morellon@gmail.com"
21
+ gem.homepage = "http://github.com/morellon/ip_methods"
22
+ gem.authors = ["morellon"]
23
+ gem.add_development_dependency "rspec"
24
+ end
25
+ Jeweler::GemcutterTasks.new
26
+ rescue LoadError
27
+ puts "Jeweler not available. Install it with: gem install jeweler"
28
+ end
29
+
30
+ desc 'Run the specs'
31
+ Spec::Rake::SpecTask.new(:spec) do |t|
32
+ t.spec_opts = ['--colour --format specdoc --loadby mtime --reverse']
33
+ t.spec_files = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ desc "Rspec : run all with RCov"
37
+ Spec::Rake::SpecTask.new('spec:rcov') do |t|
38
+ t.spec_files = FileList['spec/**/*.rb']
39
+ t.rcov = true
40
+ t.rcov_opts = ['--exclude', 'gems', '--exclude', 'spec']
41
+ end
42
+
43
+ Rake::RDocTask.new do |rdoc|
44
+ rdoc.main = "README.rdoc"
45
+ rdoc.rdoc_dir = "doc"
46
+ rdoc.title = "IP Methods"
47
+ rdoc.options += %w[ --line-numbers --inline-source --charset utf-8 ]
48
+ rdoc.rdoc_files.include("README.rdoc", "CHANGELOG.rdoc")
49
+ rdoc.rdoc_files.include("lib/**/*.rb")
50
+ end
51
+
52
+ task :default => :spec
@@ -0,0 +1,99 @@
1
+ require "ip_methods/version"
2
+
3
+ # -*- coding: UTF-8 -*-
4
+ module IpMethods
5
+ IPV4_PATTERN = /^(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}$/
6
+ IPV4_BIT_WIDTH = 32
7
+ IPV4_BIT_OCTET = 8
8
+
9
+ def self.from_bin(ip_bin)
10
+ "#{ip_bin >> 24}.#{(ip_bin >> 16) & 255}.#{(ip_bin >> 8) & 255}.#{ip_bin & 255}"
11
+ end
12
+
13
+ def self.ip?(ip)
14
+ ip =~ IPV4_PATTERN
15
+ end
16
+
17
+ def self.mask?(mask)
18
+ mask.to_s =~ /^\d+$/ && (0..32).include?(mask.to_i)
19
+ end
20
+
21
+ def self.ip_and_mask?(address)
22
+ parts = address.to_s.split('/')
23
+ parts.size == 2 && ip?(parts[0]) && mask?(parts[1])
24
+ end
25
+
26
+ def self.to_bin(ip)
27
+ ip_bin = 0
28
+ shift = IPV4_BIT_WIDTH - IPV4_BIT_OCTET
29
+ ip.split('.').each do |part|
30
+ ip_bin += (part.to_i << shift)
31
+ shift -= IPV4_BIT_OCTET
32
+ end
33
+ ip_bin
34
+ end
35
+
36
+ def network
37
+ IpMethods.from_bin(network_bin)
38
+ end
39
+
40
+ def net_mask
41
+ IpMethods.from_bin(mask_bin)
42
+ end
43
+
44
+ def default_gateway
45
+ IpMethods.from_bin(network_bin+1)
46
+ end
47
+
48
+ def broadcast
49
+ IpMethods.from_bin(broadcast_bin)
50
+ end
51
+
52
+ def generate_ips
53
+ ips = []
54
+ (network_bin+1..broadcast_bin-1).each {|ip_bin| ips << IpMethods.from_bin(ip_bin)}
55
+ ips
56
+ end
57
+
58
+ def to_bin
59
+ IpMethods.to_bin(address)
60
+ end
61
+
62
+ def mask_bin
63
+ umask = 0
64
+ range = 32 - mask
65
+ (0..mask-1).each {|i| umask += 1 << i}
66
+ umask << range
67
+ end
68
+
69
+ def umask_bin
70
+ umask = 0
71
+ range = 32 - mask
72
+ (0..range-1).each {|i| umask += 1 << i}
73
+ umask
74
+ end
75
+
76
+ def network_bin
77
+ self.to_bin & mask_bin
78
+ end
79
+
80
+ def broadcast_bin
81
+ mask_bin & network_bin | umask_bin;
82
+ end
83
+
84
+ def belongs_to?(range)
85
+ range = SimpleIP.new(range) unless range.is_a? IpMethods
86
+ range.mask <= self.mask && SimpleIP.new(self.address, range.mask).network == range.network
87
+ end
88
+
89
+ class SimpleIP
90
+ include IpMethods
91
+ attr_accessor :address, :mask
92
+
93
+ def initialize(address, mask = 32)
94
+ (@address, @mask) = address.split('/')
95
+ @mask ||= mask
96
+ @mask = @mask.to_i
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,8 @@
1
+ module IpMethods
2
+ module Version #:nodoc: all
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,56 @@
1
+ # -*- coding: UTF-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe IpMethods do
5
+
6
+ subject { IpMethods::SimpleIP.new "10.1.2.34", 26 }
7
+
8
+ it "should give the binary ip representation" do
9
+ subject.to_bin.should == 0b00001010_00000001_00000010_00100010
10
+ end
11
+
12
+ it "should convert from binary ip representation" do
13
+ IpMethods.from_bin(subject.to_bin).should == subject.address
14
+ end
15
+
16
+ it "should give the ip's network address" do
17
+ subject.network.should == "10.1.2.0"
18
+ end
19
+
20
+ it "should give the ip's broadcast address" do
21
+ subject.broadcast.should == "10.1.2.63"
22
+ end
23
+
24
+ it "should give the ip's default gateway address" do
25
+ subject.default_gateway.should == "10.1.2.1"
26
+ end
27
+
28
+ it "should give the ip's net mask address" do
29
+ subject.net_mask.should == "255.255.255.192"
30
+ end
31
+
32
+ it "should generate all host ips on this ip range" do
33
+ ips = []
34
+ ip = subject.network
35
+ 62.times {|i| ips << "10.1.2.#{i+1}"}
36
+ subject.generate_ips.should == ips
37
+ end
38
+
39
+ it "should belong to a wider range" do
40
+ subject.belongs_to?(IpMethods::SimpleIP.new "10.1.2.34", subject.mask - 1).should be_true
41
+ subject.belongs_to?(IpMethods::SimpleIP.new "0.0.0.0/0").should be_true
42
+ end
43
+
44
+ it "should belong to itself" do
45
+ subject.belongs_to?(subject).should be_true
46
+ subject.belongs_to?(IpMethods::SimpleIP.new "10.1.2.32/26").should be_true
47
+ end
48
+
49
+ it "should not belong to a narrower range" do
50
+ subject.belongs_to?(IpMethods::SimpleIP.new "10.1.2.34", subject.mask + 1).should be_false
51
+ end
52
+
53
+ it "should not belong to a different range" do
54
+ subject.belongs_to?(IpMethods::SimpleIP.new "10.2.2.0/26").should be_false
55
+ end
56
+ end
@@ -0,0 +1,4 @@
1
+ require "rubygems"
2
+ require "spec"
3
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
4
+ require "ip_methods"
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip_methods
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - morellon
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-24 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Provides an easy way for dealing with IP operations!
36
+ email: morellon@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - CHANGELOG.rdoc
45
+ - README.rdoc
46
+ - Rakefile
47
+ - lib/ip_methods.rb
48
+ - lib/ip_methods/version.rb
49
+ - spec/ip_methods_spec.rb
50
+ - spec/spec_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/morellon/ip_methods
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: IpMethods gem for helping with IP operations
85
+ test_files:
86
+ - spec/ip_methods_spec.rb
87
+ - spec/spec_helper.rb