system-getifaddrs 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +3 -3
- data/LICENSE +1 -1
- data/README.markdown +7 -2
- data/ext/rb_getifaddrs/rb_getifaddrs.c +34 -51
- data/lib/system/getifaddrs.rb +12 -1
- data/lib/system/getifaddrs/version.rb +1 -1
- data/spec/system-ifaddrs_spec.rb +15 -0
- metadata +5 -5
data/Gemfile.lock
CHANGED
data/LICENSE
CHANGED
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# system-ifaddrs ![https://secure.travis-ci.org/bbcoimbra/system-getifaddrs.png](https://secure.travis-ci.org/bbcoimbra/system-getifaddrs.png)
|
1
|
+
# system-ifaddrs ![https://secure.travis-ci.org/bbcoimbra/system-getifaddrs.png](https://secure.travis-ci.org/bbcoimbra/system-getifaddrs.png) [![Code Climate](https://codeclimate.com/github/bbcoimbra/system-getifaddrs.png)](https://codeclimate.com/github/bbcoimbra/system-getifaddrs)
|
2
2
|
|
3
3
|
This lib is a wrapper for get\_ifaddrs C routine.
|
4
4
|
|
@@ -27,6 +27,7 @@ Consider test.rb below:
|
|
27
27
|
require "pp"
|
28
28
|
require "system/getifaddrs"
|
29
29
|
pp System.get_ifaddrs
|
30
|
+
pp System.get_all_ifaddrs
|
30
31
|
```
|
31
32
|
|
32
33
|
When test.rb is executed:
|
@@ -39,8 +40,12 @@ Should return:
|
|
39
40
|
|
40
41
|
```ruby
|
41
42
|
{:lo=>{:inet_addr=>"127.0.0.1", :netmask=>"255.0.0.0"}}
|
43
|
+
[{:interface => "lo", :inet_addr => #<IPAddr '127.0.0.1'>,
|
44
|
+
:netmask => #<IPAddr '255.0.0.0'>},
|
45
|
+
{:interface => "lo", :inet_addr => #<IPAddr '::1'>,
|
46
|
+
:netmask => #<IPAddr 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'>}]
|
42
47
|
```
|
43
48
|
|
44
49
|
## Copyright
|
45
50
|
|
46
|
-
Copyright (c) 2010 Bruno Coimbra. See LICENSE for details.
|
51
|
+
Copyright (c) 2010-2013 Bruno Coimbra. See LICENSE for details.
|
@@ -14,83 +14,66 @@ int get_if_family(struct ifaddrs *ifa){
|
|
14
14
|
return 0;
|
15
15
|
}
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
}
|
17
|
+
VALUE ipaddr(struct sockaddr *ip) {
|
18
|
+
VALUE bytes;
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
switch(ip->sa_family) {
|
21
|
+
case AF_INET:
|
22
|
+
bytes = rb_str_new(&((struct sockaddr_in *)ip)->sin_addr.s_addr, 4);
|
23
|
+
break;
|
24
|
+
|
25
|
+
case AF_INET6:
|
26
|
+
bytes = rb_str_new(&((struct sockaddr_in6 *)ip)->sin6_addr.s6_addr, 16);
|
27
|
+
break;
|
28
|
+
|
29
|
+
default:
|
30
|
+
rb_raise(rb_eSystemCallError, "Unhandled IP family.");
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
return 0;
|
34
|
-
return 1;
|
32
|
+
}
|
33
|
+
|
34
|
+
VALUE ipaddr = rb_const_get(rb_cObject, rb_intern("IPAddr"));
|
35
|
+
return rb_funcall(ipaddr, rb_intern("new_ntoh"), 1, bytes);
|
35
36
|
}
|
36
37
|
|
37
|
-
VALUE
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
rb_if_data_hash);
|
47
|
-
rb_hash_aset(rb_if_data_hash,
|
48
|
-
rb_str_intern(rb_str_new2(str_inet_name)),
|
49
|
-
rb_str_new2(if_host));
|
50
|
-
rb_hash_aset(rb_if_data_hash,
|
51
|
-
rb_str_intern(rb_str_new2(str_netmask_name)),
|
52
|
-
rb_str_new2(if_netmask));
|
53
|
-
return rb_if_data_hash;
|
38
|
+
VALUE if_hash(struct ifaddrs *ifa)
|
39
|
+
{
|
40
|
+
VALUE hash = rb_hash_new();
|
41
|
+
|
42
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("interface")), rb_str_new2(ifa->ifa_name));
|
43
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("inet_addr")), ipaddr(ifa->ifa_addr));
|
44
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("netmask")), ipaddr(ifa->ifa_netmask));
|
45
|
+
|
46
|
+
return hash;
|
54
47
|
}
|
55
48
|
|
56
|
-
VALUE
|
49
|
+
VALUE rb_get_all_ifaddrs(void)
|
57
50
|
{
|
58
51
|
struct ifaddrs *ifaddr, *ifa;
|
59
|
-
VALUE
|
52
|
+
VALUE rb_if_arr;
|
60
53
|
|
61
54
|
if (getifaddrs(&ifaddr) == -1)
|
62
55
|
{
|
63
56
|
rb_raise(rb_eSystemCallError, "Can't get info about interfaces");
|
64
57
|
}
|
65
|
-
|
58
|
+
rb_if_arr = rb_ary_new();
|
66
59
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
|
67
60
|
{
|
68
61
|
int family;
|
69
62
|
|
70
63
|
family = get_if_family(ifa);
|
71
|
-
if (family == AF_INET)
|
64
|
+
if (family == AF_INET || family == AF_INET6)
|
72
65
|
{
|
73
|
-
|
74
|
-
|
75
|
-
if_name = get_if_name(ifa);
|
76
|
-
if_host = malloc(sizeof(char) * NI_MAXHOST);
|
77
|
-
if (! get_if_host(ifa, if_host))
|
78
|
-
rb_raise(rb_eSystemCallError, "Can't get IP from %s", if_name);
|
79
|
-
|
80
|
-
if_netmask = malloc(sizeof(char) * NI_MAXHOST);
|
81
|
-
if (! get_if_netmask(ifa, if_netmask))
|
82
|
-
rb_raise(rb_eSystemCallError, "Can't get IP from %s", if_name);
|
83
|
-
|
84
|
-
set_if_hash(rb_if_hash, if_name, if_host, if_netmask);
|
66
|
+
rb_ary_push(rb_if_arr, if_hash(ifa));
|
85
67
|
}
|
86
68
|
}
|
69
|
+
|
87
70
|
freeifaddrs(ifaddr);
|
88
|
-
return
|
71
|
+
return rb_if_arr;
|
89
72
|
}
|
90
73
|
|
91
74
|
VALUE mSystem;
|
92
75
|
void Init_rb_getifaddrs(){
|
93
76
|
mSystem = rb_define_module("System");
|
94
|
-
rb_define_module_function(mSystem, "
|
77
|
+
rb_define_module_function(mSystem, "get_all_ifaddrs", rb_get_all_ifaddrs, 0);
|
95
78
|
}
|
96
79
|
|
data/lib/system/getifaddrs.rb
CHANGED
@@ -1,5 +1,16 @@
|
|
1
|
+
require 'ipaddr'
|
1
2
|
begin
|
2
3
|
require File.join(File.dirname(__FILE__), 'rb_getifaddrs')
|
3
4
|
rescue LoadError
|
4
5
|
require File.join(File.dirname(__FILE__), '..', 'rb_getifaddrs')
|
5
|
-
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module System
|
9
|
+
def self.get_ifaddrs
|
10
|
+
get_all_ifaddrs.each_with_object({}) do |data, hash|
|
11
|
+
if data[:inet_addr].ipv4?
|
12
|
+
hash[data[:interface].to_sym] = {inet_addr: data[:inet_addr].to_s, netmask: data[:netmask].to_s}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/system-ifaddrs_spec.rb
CHANGED
@@ -57,4 +57,19 @@ describe System do
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
60
|
+
|
61
|
+
context "get_all_ifaddrs" do
|
62
|
+
before :all do
|
63
|
+
@ifconfig_interfaces = get_interfaces_from_ifconfig
|
64
|
+
@get_ifaddrs_interfaces = System.get_all_ifaddrs
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should return an array' do
|
68
|
+
@get_ifaddrs_interfaces.should be_kind_of(Array)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should have same number of interfaces than system' do
|
72
|
+
@get_ifaddrs_interfaces.size.should have_at_least(@ifconfig_interfaces.keys.size).elements
|
73
|
+
end
|
74
|
+
end
|
60
75
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: system-getifaddrs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -83,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
segments:
|
85
85
|
- 0
|
86
|
-
hash:
|
86
|
+
hash: 322430430249784826
|
87
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
88
|
none: false
|
89
89
|
requirements:
|
@@ -92,10 +92,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
segments:
|
94
94
|
- 0
|
95
|
-
hash:
|
95
|
+
hash: 322430430249784826
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.8.
|
98
|
+
rubygems_version: 1.8.25
|
99
99
|
signing_key:
|
100
100
|
specification_version: 3
|
101
101
|
summary: This lib is a wrapper for get_ifaddrs C routine
|