ipconverter 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b01a9e80032c6c523fe70643f565243dfa4e29f
4
+ data.tar.gz: 25c4af9335552cd0a58dc8bdbc1c8b50251057de
5
+ SHA512:
6
+ metadata.gz: 1360cf3d1f26031a4148b60ced541a31dbbb2d9d4714bde838025a8ce55963aae5cbdb4c24f5d538b2f568b08b3c2a50641e646f1ee1381284bf74581167811a
7
+ data.tar.gz: d6b6fe8f449d19aa9538b173b01d46d4fd30957b28418d608db74152a8300a748d0565514e35538073e9ebb3502f8fd67537f987c2cf7ad8773dbddd301b6587
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.swp
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ipconverter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Joshua Scott
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,40 @@
1
+ # IpConverter
2
+
3
+ Library to deal with IP Address conversions/manipulation, such as converting
4
+ a string representation like "192.168.2.1" to its integer representation
5
+ (3232236033)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ipconverter'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ipconverter
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ require 'ipconverter'
25
+ IpConverter.str_to_int "192.168.2.1"
26
+ => 3232236033
27
+ ```
28
+ str_to_int raises an error if the address is not valid:
29
+ ```
30
+ IpConverter.str_to_int "192.168.2"
31
+ => raises ArgumentError
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/joshuawscott/ipconverter/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ require "rake/extensiontask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/test_*.rb'
8
+ end
9
+
10
+ Rake::ExtensionTask.new "ipconverter" do |ext|
11
+ ext.lib_dir = "lib/ipconverter"
12
+ end
13
+
data/bench.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'benchmark'
2
+ require 'ipaddr'
3
+ require 'ipconverter'
4
+
5
+ def ip_to_i(ip)
6
+ ip.split('.').collect(&:to_i).pack('C*').unpack('N').first
7
+ end
8
+
9
+ def ip_to_i2(ip)
10
+ octets = ip.split('.').map(&:to_i)
11
+ octets[3] +
12
+ octets[2] * 256 +
13
+ octets[1] * 256*256 +
14
+ octets[0] * 256*256*256
15
+ end
16
+
17
+ def ipaddr_to_i(ip)
18
+ IPAddr.new(ip).to_i
19
+ end
20
+
21
+ ips = []
22
+ (50..59).each do |o1|
23
+ (155..254).each do |o2|
24
+ (100..199).each do |o3|
25
+ (1..10).each do |o4|
26
+ ips << "#{o1}.#{o2}.#{o3}.#{o4}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ puts "iterations: " + ips.length
33
+
34
+ Benchmark.bmbm do |x|
35
+ x.report('IPAddr#to_i') { ips.each { |ip| IPAddr.new(ip).to_i } }
36
+ x.report('pack/unpack') { ips.each { |ip| ip_to_i(ip) } }
37
+ x.report('split/multiply') { ips.each { |ip| ip_to_i2(ip) } }
38
+ x.report('C split/multiply') { ips.each {|ip| IpConverter.str_to_int(ip) } }
39
+ x.report('noop') { ips.each {|ip| ip } }
40
+ end
41
+
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+ extension_name = 'ipconverter'
3
+ $CFLAGS << ' -I. '
4
+ dir_config(extension_name)
5
+ create_makefile(extension_name)
@@ -0,0 +1,58 @@
1
+ #include <ruby.h>
2
+ #include "ipconverter.h"
3
+
4
+ // Module name
5
+ VALUE IpConverter = Qnil;
6
+
7
+ void Init_ipconverter() {
8
+ IpConverter = rb_define_module("IpConverter");
9
+ rb_define_method(IpConverter, "str_to_int", method_str_to_int, 1);
10
+ }
11
+
12
+ // Converts a passed ruby string into an integer
13
+ // returns integer or nil (if ip is invalid)
14
+ VALUE method_str_to_int(VALUE _module_, VALUE ip_string) {
15
+
16
+ // C string version of the ip_string
17
+ char* c_string = RSTRING_PTR(StringValue(ip_string));
18
+
19
+ uint32_t result;
20
+
21
+ int success = ip_string_to_long(c_string, &result);
22
+
23
+ if (success) {
24
+ // Convert the uint32_t back to a ruby Fixnum.
25
+ return UINT2NUM(result);
26
+ } else {
27
+ rb_raise(rb_eArgError, "Invalid IP Address String");
28
+ }
29
+ }
30
+
31
+ // Returns 0 if ip is invalid; 1 otherwise.
32
+ // takes an ip address string, like "192.168.2.254", finds the 32-bit integer
33
+ // value and stores that in result.
34
+ static int
35
+ ip_string_to_long(char c_string[], uint32_t *result) {
36
+ int32_t i, found_octets;
37
+ char junk;
38
+ int32_t octets[4];
39
+
40
+ found_octets = sscanf((char*)c_string, "%d.%d.%d.%d%c", (int*)&octets[3], (int*)&octets[2], (int*)&octets[1], (int*)&octets[0], &junk);
41
+
42
+ // If we didn't find exactly 4 octets, bail out, unless the extra is whitespace
43
+ if (found_octets != 4
44
+ && !(found_octets == 5 && junk == ' ')) {
45
+ return 0;
46
+ }
47
+
48
+ // If any of the octets are not in-range, bail out
49
+ for (i = 0; i < 4; i++) {
50
+ if (octets[i] > 255 || octets[i] < 0) {
51
+ return 0;
52
+ }
53
+ }
54
+
55
+ *result = (octets[0] + octets[1] * 256 + octets[2] * 256 * 256 + octets[3] * 256 * 256 * 256);
56
+ return 1;
57
+ }
58
+
@@ -0,0 +1,3 @@
1
+ VALUE method_str_to_int(VALUE, VALUE);
2
+ static int ip_string_to_long(char[], uint32_t*);
3
+ void Init_ipconverter();
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ ext = File.expand_path('../ext', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ $LOAD_PATH.unshift(ext) unless $LOAD_PATH.include?(ext)
6
+ require 'ipconverter/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "ipconverter"
10
+ spec.version = IpConverter::VERSION
11
+ spec.authors = ["Joshua Scott"]
12
+ spec.email = ["joshua.scott@gmail.com"]
13
+ spec.summary = %q{Utilities for working with IP addresses}
14
+ spec.description = %q{Fast C extension for converting IP addresses from string to integer}
15
+ spec.homepage = "http://github.com/joshuawscott/ipconverter"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.extensions = ['ext/ipconverter/extconf.rb']
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib", "ext"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake", "~> 10"
26
+ spec.add_development_dependency "minitest", "~> 5"
27
+ spec.add_development_dependency 'rake-compiler', "~> 0.9"
28
+ end
@@ -0,0 +1,6 @@
1
+ require "ipconverter/version"
2
+ require "ipconverter/ipconverter"
3
+
4
+ module IpConverter
5
+ module_function :str_to_int
6
+ end
@@ -0,0 +1,3 @@
1
+ module IpConverter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'ipconverter'
2
+ require 'minitest/autorun'
3
+
4
+ class TestIpConverter < MiniTest::Test
5
+ def setup
6
+ end
7
+
8
+ def test_smallest_ip
9
+ int = IpConverter.str_to_int "0.0.0.0"
10
+ assert_equal int, 0
11
+ end
12
+
13
+ def test_largest_ip
14
+ int = IpConverter.str_to_int "255.255.255.255"
15
+ # maximum 32-bit integer
16
+ assert_equal int, 256 * 256 * 256 * 256 - 1
17
+ end
18
+
19
+ def test_handles_spaces
20
+ int = IpConverter.str_to_int " 35.49.102.66 "
21
+ assert_equal int, IpConverter.str_to_int("35.49.102.66")
22
+ end
23
+ def test_no_overflow
24
+ assert_raises(ArgumentError) { IpConverter.str_to_int "12.12.0.256" }
25
+ end
26
+
27
+ def test_short_ip
28
+ assert_raises(ArgumentError) { IpConverter.str_to_int "12.34.56" }
29
+ end
30
+
31
+ def test_long_ip
32
+ assert_raises(ArgumentError) { IpConverter.str_to_int "12.34.56.78.90" }
33
+ end
34
+
35
+ def test_junk
36
+ assert_raises(ArgumentError) { IpConverter.str_to_int "junk" }
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ipconverter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-23 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description: Fast C extension for converting IP addresses from string to integer
70
+ email:
71
+ - joshua.scott@gmail.com
72
+ executables: []
73
+ extensions:
74
+ - ext/ipconverter/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bench.rb
83
+ - ext/ipconverter/extconf.rb
84
+ - ext/ipconverter/ipconverter.c
85
+ - ext/ipconverter/ipconverter.h
86
+ - ipconverter.gemspec
87
+ - lib/ipconverter.rb
88
+ - lib/ipconverter/version.rb
89
+ - test/test_ipconverter.rb
90
+ homepage: http://github.com/joshuawscott/ipconverter
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ - ext
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Utilities for working with IP addresses
115
+ test_files:
116
+ - test/test_ipconverter.rb