rumblinthebronx-system-getifaddrs 0.2.0-mingw32

Sign up to get free protection for your applications and to get access to all the features.
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,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ system-getifaddrs (0.1.4)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ rake (0.9.2)
10
+ rake-compiler (0.7.9)
11
+ rake
12
+ rspec (1.3.2)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ rake
19
+ rake-compiler
20
+ rspec (~> 1.3)
21
+ 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,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'spec/rake/spectask'
5
+ Spec::Rake::SpecTask.new
6
+
7
+ require 'rake/extensiontask'
8
+ Rake::ExtensionTask.new('rb_getifaddrs')
9
+
10
+ task :default => [:compile, :spec]
@@ -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,7 @@
1
+ raise "This platform is not yet supported" if OS.windows?
2
+
3
+ begin
4
+ require File.join(File.dirname(__FILE__), 'rb_getifaddrs')
5
+ rescue LoadError
6
+ require File.join(File.dirname(__FILE__), '..', 'rb_getifaddrs')
7
+ end
@@ -0,0 +1,5 @@
1
+ module System
2
+ class Getifaddrs
3
+ VERSION = "0.2.0"
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,23 @@
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 = "mingw32"
9
+ s.authors = ["Sujin Philip", "Bruno Coimbra"]
10
+ s.email = %q{sujin.phil@gmail.com}
11
+ s.homepage = %q{http://github.com/rumblinthebronx/system-getifaddrs}
12
+ s.summary = %q{This lib is a wrapper for get_ifaddrs C routine}
13
+ 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.}
14
+ s.add_development_dependency(%q<rspec>, "~> 1.3")
15
+ s.add_development_dependency(%q<rake-compiler>, [">= 0"])
16
+ s.add_runtime_dependency(%q<os>, [">= 0"])
17
+ s.extensions = `git ls-files -- ext/*`.split("\n").select{|f| f =~ /extconf/} unless RUBY_PLATFORM.include?("mingw")
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rumblinthebronx-system-getifaddrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: mingw32
7
+ authors:
8
+ - Sujin Philip
9
+ - Bruno Coimbra
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-12-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake-compiler
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: os
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ description: This lib is a wrapper for get_ifaddrs C routine. The original routine
64
+ returns a linked list that contains avaliable inet interfaces. This lib walks on
65
+ list and return an hash that contains the interface names and sub-hashes with respectives
66
+ ip addresses and netmasks.
67
+ email: sujin.phil@gmail.com
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - .document
73
+ - .gitignore
74
+ - .travis.yml
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - LICENSE
78
+ - README.markdown
79
+ - Rakefile
80
+ - ext/rb_getifaddrs/extconf.rb
81
+ - ext/rb_getifaddrs/rb_getifaddrs.c
82
+ - lib/system/.gitignore
83
+ - lib/system/getifaddrs.rb
84
+ - lib/system/getifaddrs/version.rb
85
+ - spec/spec_helper.rb
86
+ - spec/system-ifaddrs_spec.rb
87
+ - system-getifaddrs.gemspec
88
+ homepage: http://github.com/rumblinthebronx/system-getifaddrs
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.24
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: This lib is a wrapper for get_ifaddrs C routine
112
+ test_files: []