system-getifaddrs 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,8 @@
1
+ *.sw?
2
+ *~
3
+ .DS_Store
4
+ coverage
5
+ rdoc
6
+ pkg
7
+ tmp
8
+ *.so
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.
@@ -0,0 +1,39 @@
1
+ = 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
+ lo Link encap:Local Loopback
13
+ inet addr:127.0.0.1 Mask:255.0.0.0
14
+ inet6 addr: ::1/128 Scope:Host
15
+ UP LOOPBACK RUNNING MTU:16436 Metric:1
16
+ RX packets:86688 errors:0 dropped:0 overruns:0 frame:0
17
+ TX packets:86688 errors:0 dropped:0 overruns:0 carrier:0
18
+ collisions:0 txqueuelen:0
19
+ RX bytes:10903658 (10.3 MiB) TX bytes:10903658 (10.3 MiB)
20
+
21
+ Consider test.rb below:
22
+
23
+ # test.rb
24
+ require "pp"
25
+ require "system/getifaddrs"
26
+ pp System.get_ifaddrs
27
+
28
+ When test.rb is executed:
29
+
30
+ $ ruby test.rb
31
+
32
+ Should return:
33
+
34
+ {:lo=>{:inet_addr=>"127.0.0.1", :netmask=>"255.0.0.0"}}
35
+
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2010 Bruno Coimbra. See LICENSE for details.
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "system-getifaddrs"
8
+ gem.summary = %Q{This lib is a wrapper for get_ifaddrs C routine}
9
+ gem.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.}
10
+ gem.email = "bbcoimbra@gmail.com"
11
+ gem.homepage = "http://github.com/bbcoimbra/system-getifaddrs"
12
+ gem.authors = ["Bruno Coimbra"]
13
+ gem.add_development_dependency "jeweler"
14
+ gem.add_development_dependency "rspec"
15
+ gem.add_development_dependency "rake-compiler"
16
+ gem.extensions << 'ext/rb_getifaddrs/extconf.rb'
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION')
38
+ version = File.read('VERSION')
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "system-ifaddrs #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
49
+ require 'rake/extensiontask'
50
+ Rake::ExtensionTask.new('rb_getifaddrs')
data/TODO ADDED
File without changes
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -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,96 @@
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
+ 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
+ }
28
+
29
+ int get_if_netmask(struct ifaddrs *ifa, char *netmask){
30
+ if(getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
31
+ netmask, NI_MAXHOST,
32
+ NULL, 0, NI_NUMERICHOST))
33
+ return 0;
34
+ return 1;
35
+ }
36
+
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;
54
+ }
55
+
56
+ VALUE rb_get_ifaddrs(void)
57
+ {
58
+ struct ifaddrs *ifaddr, *ifa;
59
+ VALUE rb_if_hash;
60
+
61
+ if (getifaddrs(&ifaddr) == -1)
62
+ {
63
+ rb_raise(rb_eSystemCallError, "Can't get info about interfaces");
64
+ }
65
+ rb_if_hash = rb_hash_new();
66
+ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
67
+ {
68
+ int family;
69
+
70
+ family = get_if_family(ifa);
71
+ if (family == AF_INET)
72
+ {
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);
85
+ }
86
+ }
87
+ freeifaddrs(ifaddr);
88
+ return rb_if_hash;
89
+ }
90
+
91
+ VALUE mSystem;
92
+ void Init_rb_getifaddrs(){
93
+ mSystem = rb_define_module("System");
94
+ rb_define_module_function(mSystem, "get_ifaddrs", rb_get_ifaddrs, 0);
95
+ }
96
+
File without changes
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rb_getifaddrs')
@@ -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,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{system-getifaddrs}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bruno Coimbra"]
12
+ s.date = %q{2010-09-14}
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.email = %q{bbcoimbra@gmail.com}
15
+ s.extensions = ["ext/rb_getifaddrs/extconf.rb", "ext/rb_getifaddrs/extconf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc",
19
+ "TODO"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "TODO",
28
+ "VERSION",
29
+ "ext/rb_getifaddrs/extconf.rb",
30
+ "ext/rb_getifaddrs/rb_getifaddrs.c",
31
+ "lib/system/.gitignore",
32
+ "lib/system/getifaddrs.rb",
33
+ "spec/spec_helper.rb",
34
+ "spec/system-ifaddrs_spec.rb",
35
+ "system-getifaddrs.gemspec"
36
+ ]
37
+ s.homepage = %q{http://github.com/bbcoimbra/system-getifaddrs}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{This lib is a wrapper for get_ifaddrs C routine}
42
+ s.test_files = [
43
+ "spec/system-ifaddrs_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
53
+ s.add_development_dependency(%q<rspec>, [">= 0"])
54
+ s.add_development_dependency(%q<rake-compiler>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<jeweler>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ s.add_dependency(%q<rake-compiler>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<jeweler>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, [">= 0"])
63
+ s.add_dependency(%q<rake-compiler>, [">= 0"])
64
+ end
65
+ end
66
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: system-getifaddrs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Bruno Coimbra
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-14 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: jeweler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake-compiler
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ 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.
64
+ email: bbcoimbra@gmail.com
65
+ executables: []
66
+
67
+ extensions:
68
+ - ext/rb_getifaddrs/extconf.rb
69
+ - ext/rb_getifaddrs/extconf.rb
70
+ extra_rdoc_files:
71
+ - LICENSE
72
+ - README.rdoc
73
+ - TODO
74
+ files:
75
+ - .document
76
+ - .gitignore
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - TODO
81
+ - VERSION
82
+ - ext/rb_getifaddrs/extconf.rb
83
+ - ext/rb_getifaddrs/rb_getifaddrs.c
84
+ - lib/system/.gitignore
85
+ - lib/system/getifaddrs.rb
86
+ - spec/spec_helper.rb
87
+ - spec/system-ifaddrs_spec.rb
88
+ - system-getifaddrs.gemspec
89
+ has_rdoc: true
90
+ homepage: http://github.com/bbcoimbra/system-getifaddrs
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --charset=UTF-8
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: This lib is a wrapper for get_ifaddrs C routine
123
+ test_files:
124
+ - spec/system-ifaddrs_spec.rb
125
+ - spec/spec_helper.rb