rumblinthebronx-system-getifaddrs 0.2.1-universal-java

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.sw?
2
+ *~
3
+ .DS_Store
4
+ coverage
5
+ rdoc
6
+ pkg
7
+ tmp
8
+ *.so
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+ gemspec
3
+ gem 'rake'
4
+
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rumblinthebronx-system-getifaddrs (0.2.1-java)
5
+ netaddr (~> 1.5.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ netaddr (1.5.0)
11
+ rake (10.0.4)
12
+ rake-compiler (0.8.3)
13
+ rake
14
+ rspec (1.3.2)
15
+
16
+ PLATFORMS
17
+ java
18
+
19
+ DEPENDENCIES
20
+ rake
21
+ rake-compiler
22
+ rspec (~> 1.3)
23
+ rumblinthebronx-system-getifaddrs!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bruno Coimbra
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,51 @@
1
+ # rumblinthebronx-system-ifaddrs
2
+
3
+ This lib is a wrapper for get\_ifaddrs C routine.
4
+
5
+ The original routine returns a linked list that contains avaliable inet interfaces.
6
+ This lib walks on list and return an hash that contains the interface names and sub-hashes with respectives ip addresses and netmasks.
7
+
8
+ ## Example
9
+
10
+ Supose that /sbin/ifconfig returns:
11
+
12
+ ```bash
13
+ lo Link encap:Local Loopback
14
+ inet addr:127.0.0.1 Mask:255.0.0.0
15
+ inet6 addr: ::1/128 Scope:Host
16
+ UP LOOPBACK RUNNING MTU:16436 Metric:1
17
+ RX packets:86688 errors:0 dropped:0 overruns:0 frame:0
18
+ TX packets:86688 errors:0 dropped:0 overruns:0 carrier:0
19
+ collisions:0 txqueuelen:0
20
+ RX bytes:10903658 (10.3 MiB) TX bytes:10903658 (10.3 MiB)
21
+ ```
22
+
23
+ Consider test.rb below:
24
+
25
+ ```ruby
26
+ # test.rb
27
+ require "pp"
28
+ require "system/getifaddrs"
29
+ pp System.get_ifaddrs
30
+ ```
31
+
32
+ When test.rb is executed:
33
+
34
+ ```bash
35
+ $ ruby test.rb
36
+ ```
37
+
38
+ Should return:
39
+
40
+ ```ruby
41
+ {:lo=>
42
+ {:inet_addr_v4=>"127.0.0.1",
43
+ :netmask_v4=>"255.0.0.0",
44
+ :inet_addr_v6=>"::1",
45
+ :netmask_v6=>"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"}}
46
+
47
+ ```
48
+
49
+ ## Copyright
50
+
51
+ Copyright (c) 2011 rumblinthebronx. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'spec/rake/spectask'
5
+ Spec::Rake::SpecTask.new
6
+
7
+ if RUBY_PLATFORM == "java"
8
+ task :default => [:spec]
9
+ else
10
+ require 'rake/extensiontask'
11
+ Rake::ExtensionTask.new('rb_getifaddrs')
12
+ task :default => [:compile, :spec]
13
+ end
@@ -0,0 +1,16 @@
1
+ require 'mkmf'
2
+
3
+ dir_config "rb_getifaddrs"
4
+
5
+ $CFLAGS = "-D_GNU_SOURCE -Wall"
6
+
7
+ have_header('arpa/inet.h')
8
+ have_header('sys/socket.h')
9
+ have_header('sys/types.h')
10
+ have_header('netdb.h')
11
+ have_header('ifaddrs.h')
12
+ have_header('stdio.h')
13
+ have_header('stdlib.h')
14
+ have_header('unistd.h')
15
+
16
+ create_makefile("system/rb_getifaddrs")
@@ -0,0 +1,105 @@
1
+ #include "ruby.h"
2
+ #include <arpa/inet.h>
3
+ #include <sys/socket.h>
4
+ #include <sys/types.h>
5
+ #include <netdb.h>
6
+ #include <ifaddrs.h>
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <unistd.h>
10
+
11
+ int get_if_family(struct ifaddrs *ifa){
12
+ if(ifa && ifa->ifa_addr)
13
+ return ifa->ifa_addr->sa_family;
14
+ return 0;
15
+ }
16
+
17
+ char * get_if_name(struct ifaddrs *ifa){
18
+ return ifa->ifa_name;
19
+ }
20
+
21
+ int get_if_host(struct ifaddrs *ifa, char *host){
22
+ int family = get_if_family(ifa);
23
+ if(getnameinfo(ifa->ifa_addr,
24
+ (family == AF_INET) ? sizeof(struct sockaddr_in) :
25
+ sizeof(struct sockaddr_in6),
26
+ host, NI_MAXHOST,
27
+ NULL, 0, NI_NUMERICHOST))
28
+ return 0;
29
+ return 1;
30
+ }
31
+
32
+ int get_if_netmask(struct ifaddrs *ifa, char *netmask){
33
+ int family = get_if_family(ifa);
34
+ if(getnameinfo(ifa->ifa_netmask,
35
+ (family == AF_INET) ? sizeof(struct sockaddr_in) :
36
+ sizeof(struct sockaddr_in6),
37
+ netmask, NI_MAXHOST,
38
+ NULL, 0, NI_NUMERICHOST))
39
+ return 0;
40
+ return 1;
41
+ }
42
+
43
+ VALUE set_if_hash(VALUE rb_if_hash, struct ifaddrs *ifa, int family){
44
+ char *if_host, *if_netmask, *if_name;
45
+ if_name = get_if_name(ifa);
46
+ if_host = malloc(sizeof(char) * NI_MAXHOST);
47
+ if (! get_if_host(ifa, if_host))
48
+ rb_raise(rb_eSystemCallError, "Can't get IP from %s", if_name);
49
+
50
+ if_netmask = malloc(sizeof(char) * NI_MAXHOST);
51
+ if (! get_if_netmask(ifa, if_netmask))
52
+ rb_raise(rb_eSystemCallError, "Can't get IP from %s", if_name);
53
+
54
+ char *str_inet_name = "inet_addr_v4";
55
+ char *str_inet_name6 = "inet_addr_v6";
56
+ char *str_netmask_name = "netmask_v4";
57
+ char *str_netmask_name6 = "netmask_v6";
58
+ VALUE rb_if_data_hash = rb_hash_aref(rb_if_hash, rb_str_intern(rb_str_new2(if_name)));
59
+
60
+ if (rb_if_data_hash == Qnil) {
61
+ rb_if_data_hash = rb_hash_new();
62
+ rb_hash_aset(rb_if_hash,
63
+ rb_str_intern(rb_str_new2(if_name)),
64
+ rb_if_data_hash);
65
+ }
66
+
67
+ rb_hash_aset(rb_if_data_hash,
68
+ rb_str_intern(rb_str_new2((family == AF_INET) ? str_inet_name : str_inet_name6)),
69
+ rb_str_new2(if_host));
70
+ rb_hash_aset(rb_if_data_hash,
71
+ rb_str_intern(rb_str_new2((family == AF_INET) ? str_netmask_name : str_netmask_name6)),
72
+ rb_str_new2(if_netmask));
73
+ return rb_if_data_hash;
74
+ }
75
+
76
+ VALUE rb_get_ifaddrs(void)
77
+ {
78
+ struct ifaddrs *ifaddr, *ifa;
79
+ VALUE rb_if_hash;
80
+
81
+ if (getifaddrs(&ifaddr) == -1)
82
+ {
83
+ rb_raise(rb_eSystemCallError, "Can't get info about interfaces");
84
+ }
85
+ rb_if_hash = rb_hash_new();
86
+ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
87
+ {
88
+ int family;
89
+
90
+ family = get_if_family(ifa);
91
+ if (family == AF_INET || family == AF_INET6)
92
+ {
93
+ set_if_hash(rb_if_hash, ifa, family);
94
+ }
95
+ }
96
+ freeifaddrs(ifaddr);
97
+ return rb_if_hash;
98
+ }
99
+
100
+ VALUE mSystem;
101
+ void Init_rb_getifaddrs(){
102
+ mSystem = rb_define_module("System");
103
+ rb_define_module_function(mSystem, "get_ifaddrs", rb_get_ifaddrs, 0);
104
+ }
105
+
File without changes
@@ -0,0 +1,42 @@
1
+ if RUBY_ENGINE == "jruby"
2
+ module System
3
+ require 'netaddr'
4
+
5
+ # Jruby version of the system-getifaddrs gem. Gives info on all network
6
+ # interfaces.
7
+ #
8
+ # @return [Hash] Network interfaces and their addresses/netmasks.
9
+ def self.get_ifaddrs
10
+ ifaddrs = {}
11
+ interfaces = java.net.NetworkInterface.network_interfaces
12
+
13
+ interfaces.each do |interface|
14
+ name = interface.display_name.to_sym
15
+ ifaddrs[name] = {}
16
+
17
+ interface.interface_addresses.each do |interface_address|
18
+ ip_address, index = interface_address.address.host_address.split('%')
19
+ prefix = interface_address.network_prefix_length
20
+ netmask = NetAddr::CIDR.create("#{ip_address}/#{prefix}").wildcard_mask
21
+
22
+ if interface_address.address.is_a? Java::JavaNet::Inet4Address
23
+ ifaddrs[name][:inet_addr_v4] = ip_address
24
+ netmask = '255.0.0.0' if interface_address.address.is_loopback_address?
25
+ ifaddrs[name][:netmask_v4] = netmask
26
+ else
27
+ ifaddrs[name][:inet_addr_v6] = "#{ip_address}%#{index}"
28
+ ifaddrs[name][:netmask_v6] = NetAddr.shorten(netmask)
29
+ end
30
+ end
31
+ end
32
+
33
+ ifaddrs
34
+ end
35
+ end
36
+ else
37
+ begin
38
+ require File.join(File.dirname(__FILE__), 'rb_getifaddrs')
39
+ rescue LoadError
40
+ require File.join(File.dirname(__FILE__), '..', 'rb_getifaddrs')
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module System
2
+ class Getifaddrs
3
+ VERSION = "0.2.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'system/getifaddrs'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ def get_interfaces_from_ifconfig
4
+ str = `/sbin/ifconfig`
5
+ ary = str.split("\n\n")
6
+ ary.map!{|e| e.split("\n")}
7
+ ifs = Hash.new{|v| v = Array.new}
8
+ ary.each do |elem|
9
+ elem.map!{|e| e.squeeze(' ').strip}
10
+ end
11
+ ary.each do |elem|
12
+ key = elem.first.split(' ').first
13
+ ifs[key.to_sym] = elem.select{|e| e =~ /inet addr:/}
14
+ end
15
+ interfaces = {}
16
+ ifs.each_pair do |key,value|
17
+ unless value.empty?
18
+ value.first.gsub!('inet addr:','inet_addr:')
19
+ ary = value.first.split(' ')
20
+ temp_hash = {}
21
+ ary.each do |elem|
22
+ k, v = elem.split(':')
23
+ case k
24
+ when 'Mask'
25
+ k = 'netmask'
26
+ when 'Bcast'
27
+ k = 'broadcast'
28
+ end
29
+ temp_hash[k.to_sym] = v
30
+ end
31
+ interfaces[key] = temp_hash
32
+ end
33
+ end
34
+ interfaces
35
+ end
36
+
37
+
38
+ describe System do
39
+
40
+ context "getifaddrs" do
41
+ before :all do
42
+ @ifconfig_interfaces = get_interfaces_from_ifconfig
43
+ @get_ifaddrs_interfaces = System.get_ifaddrs
44
+ end
45
+
46
+ it 'should return a hash' do
47
+ @get_ifaddrs_interfaces.should be_kind_of(Hash)
48
+ end
49
+
50
+ it 'should have same number of interfaces than system' do
51
+ @get_ifaddrs_interfaces.keys.size.should have_at_least(@ifconfig_interfaces.keys.size).elements
52
+ end
53
+
54
+ it 'should have same interfaces than system' do
55
+ @ifconfig_interfaces.keys.each do |k|
56
+ @get_ifaddrs_interfaces.should include(k)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encodign: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "system/getifaddrs/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rumblinthebronx-system-getifaddrs"
7
+ s.version = System::Getifaddrs::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.platform = Gem::Platform.new(['universal', 'java']) if RUBY_PLATFORM == "java"
10
+ s.authors = ["Sujin Philip", "Bruno Coimbra"]
11
+ s.email = %q{sujin.phil@gmail.com}
12
+ s.homepage = %q{http://github.com/rumblinthebronx/system-getifaddrs}
13
+ s.summary = %q{This library provides the information about the inet interfaces. }
14
+ s.description = %q{This lib is a wrapper for get_ifaddrs C routine. The original routine returns a linked list that contains avaliable inet interfaces. This lib walks on list and return an hash that contains the interface names and sub-hashes with respectives ip addresses and netmasks.}
15
+ s.add_development_dependency(%q<rspec>, "~> 1.3")
16
+ s.add_development_dependency(%q<rake-compiler>, [">= 0"])
17
+ s.add_runtime_dependency(%q<netaddr>, "~> 1.5.0") if RUBY_PLATFORM == "java"
18
+
19
+ unless RUBY_PLATFORM.include?("mingw") || RUBY_PLATFORM == "java"
20
+ s.extensions = `git ls-files -- ext/*`.split("\n").select{|f| f =~ /extconf/}
21
+ end
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rumblinthebronx-system-getifaddrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: universal-java
7
+ authors:
8
+ - Sujin Philip
9
+ - Bruno Coimbra
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-04-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ version_requirements: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ none: false
29
+ prerelease: false
30
+ type: :development
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake-compiler
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: !binary |-
38
+ MA==
39
+ none: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: !binary |-
45
+ MA==
46
+ none: false
47
+ prerelease: false
48
+ type: :development
49
+ - !ruby/object:Gem::Dependency
50
+ name: netaddr
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 1.5.0
56
+ none: false
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.0
62
+ none: false
63
+ prerelease: false
64
+ type: :runtime
65
+ description: This lib is a wrapper for get_ifaddrs C routine. The original routine returns a linked list that contains avaliable inet interfaces. This lib walks on list and return an hash that contains the interface names and sub-hashes with respectives ip addresses and netmasks.
66
+ email: sujin.phil@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - ".document"
72
+ - ".gitignore"
73
+ - ".travis.yml"
74
+ - Gemfile
75
+ - Gemfile.lock
76
+ - LICENSE
77
+ - README.markdown
78
+ - Rakefile
79
+ - ext/rb_getifaddrs/extconf.rb
80
+ - ext/rb_getifaddrs/rb_getifaddrs.c
81
+ - lib/system/.gitignore
82
+ - lib/system/getifaddrs.rb
83
+ - lib/system/getifaddrs/version.rb
84
+ - spec/spec_helper.rb
85
+ - spec/system-ifaddrs_spec.rb
86
+ - system-getifaddrs.gemspec
87
+ homepage: http://github.com/rumblinthebronx/system-getifaddrs
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: !binary |-
100
+ MA==
101
+ hash: 2
102
+ none: false
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: !binary |-
110
+ MA==
111
+ hash: 2
112
+ none: false
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.24
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: This library provides the information about the inet interfaces.
119
+ test_files:
120
+ - spec/spec_helper.rb
121
+ - spec/system-ifaddrs_spec.rb