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 CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- system-getifaddrs (0.1.5)
4
+ system-getifaddrs (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- rake (0.9.2)
10
- rake-compiler (0.7.9)
9
+ rake (10.0.4)
10
+ rake-compiler (0.8.3)
11
11
  rake
12
12
  rspec (1.3.2)
13
13
 
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Bruno Coimbra
1
+ Copyright (c) 2010-2013 Bruno Coimbra
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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
- char * get_if_name(struct ifaddrs *ifa){
18
- return ifa->ifa_name;
19
- }
17
+ VALUE ipaddr(struct sockaddr *ip) {
18
+ VALUE bytes;
20
19
 
21
- int get_if_host(struct ifaddrs *ifa, char *host){
22
- if(getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
23
- host, NI_MAXHOST,
24
- NULL, 0, NI_NUMERICHOST))
25
- return 0;
26
- return 1;
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
- int get_if_netmask(struct ifaddrs *ifa, char *netmask){
30
- if(getnameinfo(ifa->ifa_netmask, sizeof(struct sockaddr_in),
31
- netmask, NI_MAXHOST,
32
- NULL, 0, NI_NUMERICHOST))
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 set_if_hash(VALUE rb_if_hash, char *if_name, char *if_host, char *if_netmask){
38
-
39
- VALUE rb_if_data_hash;
40
- char *str_inet_name = "inet_addr";
41
- char *str_netmask_name = "netmask";
42
-
43
- rb_if_data_hash = rb_hash_new();
44
- rb_hash_aset(rb_if_hash,
45
- rb_str_intern(rb_str_new2(if_name)),
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 rb_get_ifaddrs(void)
49
+ VALUE rb_get_all_ifaddrs(void)
57
50
  {
58
51
  struct ifaddrs *ifaddr, *ifa;
59
- VALUE rb_if_hash;
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
- rb_if_hash = rb_hash_new();
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
- char *if_host, *if_netmask, *if_name;
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 rb_if_hash;
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, "get_ifaddrs", rb_get_ifaddrs, 0);
77
+ rb_define_module_function(mSystem, "get_all_ifaddrs", rb_get_all_ifaddrs, 0);
95
78
  }
96
79
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  module System
2
2
  class Getifaddrs
3
- VERSION = "0.1.5"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -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.1.5
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: 2012-09-12 00:00:00.000000000 Z
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: -3322810239089075693
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: -3322810239089075693
95
+ hash: 322430430249784826
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 1.8.24
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