sys-host 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +11 -0
- data/MANIFEST +10 -8
- data/Rakefile +139 -48
- data/doc/host.txt +46 -46
- data/examples/example_sys_host.rb +21 -0
- data/ext/bsd/extconf.rb +11 -0
- data/ext/bsd/sys/host.c +268 -0
- data/sys-host.gemspec +40 -0
- data/test/test_sys_host.rb +112 -78
- metadata +33 -25
- data/ext/bsd/bsd.c +0 -247
- data/ext/constants.h +0 -14
- data/ext/extconf.rb +0 -35
- data/ext/generic/generic.c +0 -189
- data/ext/linux/linux.c +0 -228
- data/ext/sunos/sunos.c +0 -242
data/CHANGES
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
== 0.6.2 - 20-Mar-2010
|
2
|
+
* Changed license to Artistic 2.0.
|
3
|
+
* The Host.host_id method has been modified to return a hex value on MS
|
4
|
+
Windows in order to be consistent with other platforms.
|
5
|
+
* Some internal directory layout changes and corresponding changes to the
|
6
|
+
Rakefile.
|
7
|
+
* Additional Rakefile tasks that remove the need for some code that was
|
8
|
+
previously inlined into the gemspec directly.
|
9
|
+
* Updated the gemspec, including an updated description, the addition of a
|
10
|
+
license, and changing test-unit to a development dependency.
|
11
|
+
|
1
12
|
== 0.6.1 - 18-Nov-2008
|
2
13
|
* Added the host_id method for Windows, which just returns the MAC address.
|
3
14
|
* Fixed an issue in the Host.ip_addr method for MS Windows where multiple ip
|
data/MANIFEST
CHANGED
@@ -4,12 +4,14 @@
|
|
4
4
|
* Rakefile
|
5
5
|
* sys-host.gemspec
|
6
6
|
* doc/host.txt
|
7
|
-
* examples/
|
8
|
-
* ext/
|
9
|
-
* ext/
|
10
|
-
* ext/
|
11
|
-
* ext/generic/
|
12
|
-
* ext/linux/
|
13
|
-
* ext/
|
14
|
-
*
|
7
|
+
* examples/example_sys_host.rb
|
8
|
+
* ext/bsd/extconf.rb
|
9
|
+
* ext/bsd/sys/host.c
|
10
|
+
* ext/generic/extconf.rb
|
11
|
+
* ext/generic/sys/host.c
|
12
|
+
* ext/linux/extconf.rb
|
13
|
+
* ext/linux/sys/host.c
|
14
|
+
* ext/sunos/extconf.rb
|
15
|
+
* ext/sunos/sys/host.c
|
16
|
+
* lib/windows/sys/host.rb
|
15
17
|
* test/test_sys_host.rb
|
data/Rakefile
CHANGED
@@ -1,78 +1,169 @@
|
|
1
1
|
require 'rake'
|
2
|
-
require 'rake/clean'
|
3
2
|
require 'rake/testtask'
|
4
3
|
require 'rbconfig'
|
5
4
|
include Config
|
6
5
|
|
7
6
|
desc "Clean the build files for the sys-host source for UNIX systems"
|
8
7
|
task :clean do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
rm_rf('.test-result') if File.exists?('.test-result')
|
9
|
+
rm_rf 'conftest.dSYM' if File.exists?('conftest.dSYM') # OS X
|
10
|
+
|
11
|
+
Dir['*.gem'].each{ |f| File.delete(f) }
|
12
|
+
|
13
|
+
case Config::CONFIG['host_os']
|
14
|
+
when /bsd|darwin/i
|
15
|
+
dir = 'ext/bsd'
|
16
|
+
when /sunos|solaris/i
|
17
|
+
dir = 'ext/sunos'
|
18
|
+
when /linux/i
|
19
|
+
dir = 'ext/linux'
|
20
|
+
else
|
21
|
+
dir = 'ext/generic'
|
22
|
+
end
|
23
|
+
|
24
|
+
unless Config::CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw/i
|
25
|
+
Dir.chdir(dir) do
|
26
|
+
if Dir['*.o'].length > 0
|
27
|
+
sh 'make distclean'
|
28
|
+
Dir['sys/host.*'].each{ |f| rm(f) if File.extname(f) != '.c' }
|
14
29
|
end
|
15
|
-
|
16
|
-
|
30
|
+
end
|
31
|
+
end
|
17
32
|
end
|
18
33
|
|
19
|
-
desc "Build the sys-host
|
34
|
+
desc "Build the sys-host library on UNIX systems (but don't install it)"
|
20
35
|
task :build => [:clean] do
|
21
|
-
|
36
|
+
case Config::CONFIG['host_os']
|
37
|
+
when /bsd|darwin/i
|
38
|
+
dir = 'ext/bsd'
|
39
|
+
when /sunos|solaris/i
|
40
|
+
dir = 'ext/sunos'
|
41
|
+
when /linux/i
|
42
|
+
dir = 'ext/linux'
|
43
|
+
else
|
44
|
+
dir = 'ext/generic'
|
45
|
+
end
|
46
|
+
|
47
|
+
unless Config::CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw/i
|
48
|
+
Dir.chdir(dir) do
|
22
49
|
ruby 'extconf.rb'
|
23
50
|
sh 'make'
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
51
|
+
cp 'host.' + Config::CONFIG['DLEXT'], 'sys'
|
52
|
+
end
|
53
|
+
end
|
28
54
|
end
|
29
55
|
|
30
|
-
desc
|
31
|
-
task :
|
32
|
-
|
33
|
-
|
56
|
+
desc 'Install the sys-host library'
|
57
|
+
task :install => [:build] do
|
58
|
+
file = nil
|
59
|
+
dir = File.join(Config::CONFIG['sitelibdir'], 'sys')
|
34
60
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
61
|
+
Dir.mkdir(dir) unless File.exists?(dir)
|
62
|
+
|
63
|
+
case Config::CONFIG['host_os']
|
64
|
+
when /mswin|win32|msdos|cygwin|mingw/i
|
65
|
+
file = 'lib/windows/sys/host.rb'
|
66
|
+
when /bsd|darwin/i
|
67
|
+
Dir.chdir('ext/bsd'){ sh 'make install' }
|
68
|
+
when /sunos|solaris/i
|
69
|
+
Dir.chdir('ext/sunos'){ sh 'make install' }
|
70
|
+
when /linux/i
|
71
|
+
Dir.chdir('ext/linux'){ sh 'make install' }
|
72
|
+
else
|
73
|
+
Dir.chdir('ext/generic'){ sh 'make install' }
|
74
|
+
end
|
75
|
+
|
76
|
+
cp(file, dir, :verbose => true) if file
|
50
77
|
end
|
51
78
|
|
52
|
-
desc
|
53
|
-
task :
|
54
|
-
|
55
|
-
|
56
|
-
|
79
|
+
desc 'Uninstall the sys-host library'
|
80
|
+
task :uninstall do
|
81
|
+
case Config::CONFIG['host_os']
|
82
|
+
when /win32|mswin|dos|cygwin|mingw/i
|
83
|
+
dir = File.join(Config::CONFIG['sitelibdir'], 'sys')
|
84
|
+
file = File.join(dir, 'host.rb')
|
85
|
+
else
|
86
|
+
dir = File.join(Config::CONFIG['sitearchdir'], 'sys')
|
87
|
+
file = File.join(dir, 'host.' + Config::CONFIG['DLEXT'])
|
88
|
+
end
|
89
|
+
|
90
|
+
rm(file)
|
57
91
|
end
|
58
92
|
|
59
93
|
desc "Run the example sys-host program"
|
60
94
|
task :example => [:build] do
|
61
95
|
Dir.mkdir('sys') unless File.exists?('sys')
|
62
96
|
if CONFIG['host_os'].match('mswin')
|
63
|
-
|
64
|
-
ruby '-Ilib examples/host_test.rb'
|
97
|
+
ruby '-Ilib/windows examples/example_sys_host.rb'
|
65
98
|
else
|
66
|
-
|
99
|
+
case Config::CONFIG['host_os']
|
100
|
+
when /bsd|darwin/i
|
101
|
+
dir = 'ext/bsd'
|
102
|
+
when /sunos|solaris/i
|
103
|
+
dir = 'ext/sunos'
|
104
|
+
when /linux/i
|
105
|
+
dir = 'ext/linux'
|
106
|
+
else
|
107
|
+
dir = 'ext/generic'
|
108
|
+
end
|
109
|
+
ruby "-I#{dir} examples/example_sys_host.rb"
|
67
110
|
end
|
68
111
|
end
|
69
112
|
|
113
|
+
desc 'Run the test suite'
|
70
114
|
Rake::TestTask.new do |t|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
115
|
+
task :test => :build
|
116
|
+
t.libs << 'test'
|
117
|
+
t.test_files = FileList['test/test_sys_host.rb']
|
118
|
+
|
119
|
+
case Config::CONFIG['host_os']
|
120
|
+
when /mswin|msdos|cygwin|mingw/i
|
121
|
+
t.libs << 'lib/windows'
|
122
|
+
when /linux/i
|
123
|
+
t.libs << 'ext/linux'
|
124
|
+
when /sunos|solaris/i
|
125
|
+
t.libs << 'ext/sunos'
|
126
|
+
when /bsd|darwin/i
|
127
|
+
t.libs << 'ext/bsd'
|
128
|
+
else
|
129
|
+
t.libs << 'ext/generic'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
namespace 'gem' do
|
134
|
+
desc 'Create the sys-host gem file'
|
135
|
+
task :create => [:clean] do
|
136
|
+
spec = eval(IO.read('sys-host.gemspec'))
|
137
|
+
|
138
|
+
case Config::CONFIG['host_os']
|
139
|
+
when /bsd|darwin/i
|
140
|
+
spec.files << 'ext/bsd/sys/host.c'
|
141
|
+
spec.extra_rdoc_files << 'ext/bsd/sys/host.c'
|
142
|
+
spec.extensions = ['ext/bsd/extconf.rb']
|
143
|
+
when /linux/i
|
144
|
+
spec.files << 'ext/linux/sys/host.c'
|
145
|
+
spec.extra_rdoc_files << 'ext/linux/sys/host.c'
|
146
|
+
spec.extensions = ['ext/linux/extconf.rb']
|
147
|
+
when /sunos|solaris/i
|
148
|
+
spec.files << 'ext/sunos/sys/host.c'
|
149
|
+
spec.extra_rdoc_files << 'ext/sunos/sys/host.c'
|
150
|
+
spec.extensions = ['ext/sunos/extconf.rb']
|
151
|
+
when /mswin|win32|dos|cygwin|mingw/i
|
152
|
+
spec.platform = Gem::Platform::CURRENT
|
153
|
+
spec.require_paths = ['lib', 'lib/windows']
|
154
|
+
spec.files += ['lib/windows/sys/host.rb']
|
155
|
+
else
|
156
|
+
spec.files << 'ext/generic/sys/host.c'
|
157
|
+
spec.extra_rdoc_files << 'ext/generic/sys/host.c'
|
158
|
+
spec.extensions = ['ext/generic/extconf.rb']
|
159
|
+
end
|
160
|
+
|
161
|
+
Gem::Builder.new(spec).build
|
162
|
+
end
|
163
|
+
|
164
|
+
desc 'Install the sys-host gem'
|
165
|
+
task :install => [:create] do
|
166
|
+
file = Dir["*.gem"].first
|
167
|
+
sh "gem install #{file}"
|
168
|
+
end
|
78
169
|
end
|
data/doc/host.txt
CHANGED
@@ -1,88 +1,88 @@
|
|
1
1
|
== Description
|
2
|
-
|
2
|
+
Provides hostname and ip address information for a given host.
|
3
3
|
|
4
4
|
== Synopsis
|
5
|
-
|
6
|
-
|
5
|
+
require 'sys/host'
|
6
|
+
include Sys
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
p Host.hostname
|
9
|
+
p Host.ip_addr
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
# Some platforms
|
12
|
+
Host.info{ |h|
|
13
|
+
p h
|
14
|
+
}
|
15
15
|
|
16
16
|
== Constants
|
17
17
|
VERSION
|
18
|
-
|
18
|
+
Returns the current version number of this library (as a string)
|
19
19
|
|
20
20
|
== Class Methods
|
21
21
|
Host.hostname
|
22
|
-
|
23
|
-
|
22
|
+
Returns the hostname of the current host. This may or not return
|
23
|
+
the FQDN, depending on your system.
|
24
24
|
|
25
25
|
Host.ip_addr
|
26
|
-
|
27
|
-
|
26
|
+
Returns a list of IP addresses for the current host (yes, it is
|
27
|
+
possible to have more than one).
|
28
28
|
|
29
29
|
Host.info(host=localhost)
|
30
30
|
Host.info(host=localhost){ |h| ... }
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
Yields a HostInfo struct for each network adapter on 'host', or an array
|
32
|
+
of HostInfo struct's in non-block form. The exact members of this struct
|
33
|
+
vary depending on your platform.
|
34
|
+
|
35
|
+
Host.host_id
|
36
|
+
Returns the unique hexadecimal identifier for the current host.
|
34
37
|
|
35
38
|
== Exception Classes
|
36
39
|
Host::Error < StandardError
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
Raised in the event of a failure for any of the class methods provided
|
41
|
+
with this library. Generally speaking, it means there was a failure in
|
42
|
+
the underlying gethostname() or gethostbyname() calls.
|
40
43
|
|
41
44
|
== Future Plans
|
42
|
-
|
43
|
-
|
45
|
+
Add support for IPV6. This will be difficult unless I have access to a
|
46
|
+
system on an IPV6 network. Any help on this front appreciated.
|
44
47
|
|
45
48
|
== Notes
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
The Windows version of this library uses OLE + WMI. The Host.info method
|
50
|
+
returns considerably more information than the *nix versions of this
|
51
|
+
library.
|
49
52
|
|
50
53
|
== Comments and Questions
|
51
|
-
|
52
|
-
|
54
|
+
Please direct all comments and/or questions to one of the Forums on the
|
55
|
+
project home page at http://www.rubyforge.org/projects/sysutils.
|
53
56
|
|
54
57
|
== Known Bugs
|
55
|
-
|
58
|
+
None that I'm aware of.
|
56
59
|
|
57
|
-
|
58
|
-
|
60
|
+
Please log any bugs on the project home page at
|
61
|
+
http://www.rubyforge.org/projects/sysutils
|
59
62
|
|
60
63
|
== Acknowledgements
|
61
|
-
|
62
|
-
|
64
|
+
Thanks go to Mike Hall, Guy Decoux, and Matz for their help with
|
65
|
+
C extensions in general.
|
63
66
|
|
64
|
-
|
65
|
-
books.
|
67
|
+
Thanks also go to Richard Stevens for his excellent Unix programming books.
|
66
68
|
|
67
69
|
== License
|
68
|
-
|
70
|
+
Artistic 2.0
|
69
71
|
|
70
72
|
== Copyright
|
71
|
-
|
73
|
+
Copyright 2002-2010, Daniel J. Berger, djberg96 at gmail dot com
|
72
74
|
|
73
|
-
|
74
|
-
|
75
|
+
All Rights Reserved. This module is free software. It may be used,
|
76
|
+
redistributed and/or modified under the same terms as Ruby itself.
|
75
77
|
|
76
78
|
== Warranty
|
77
|
-
|
78
|
-
|
79
|
-
|
79
|
+
This library is provided "as is" and without any express or
|
80
|
+
implied warranties, including, without limitation, the implied
|
81
|
+
warranties of merchantability and fitness for a particular purpose.
|
80
82
|
|
81
83
|
== Author
|
82
|
-
|
83
|
-
djberg96 at gmail dot com
|
84
|
-
imperator on IRC (freenode)
|
84
|
+
Daniel J. Berger
|
85
85
|
|
86
86
|
== See Also
|
87
|
-
|
88
|
-
|
87
|
+
gethostbyname, gethostbyname_r, gethostname, hostname (Unix)
|
88
|
+
WMI, OLE (Windows)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
####################################################################
|
2
|
+
# example_sys_host.rb
|
3
|
+
#
|
4
|
+
# Generic test script for general futzing. Use the 'rake example'
|
5
|
+
# task to run this.
|
6
|
+
#
|
7
|
+
# Modify as you see fit.
|
8
|
+
####################################################################
|
9
|
+
require 'sys/host'
|
10
|
+
include Sys
|
11
|
+
|
12
|
+
puts "VERSION: " + Host::VERSION
|
13
|
+
puts "Hostname: " + Host.hostname
|
14
|
+
puts "IP Addresses : " + Host.ip_addr.join(',')
|
15
|
+
puts "Host ID: " + Host.host_id.to_s
|
16
|
+
|
17
|
+
puts "Info: "
|
18
|
+
|
19
|
+
Host.info{ |i|
|
20
|
+
p i
|
21
|
+
}
|
data/ext/bsd/extconf.rb
ADDED
data/ext/bsd/sys/host.c
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
/******************************************************************************
|
2
|
+
* host.c - Source code for BSD and OS X for the sys-host library.
|
3
|
+
*
|
4
|
+
* Author: Daniel Berger
|
5
|
+
*****************************************************************************/
|
6
|
+
#include <ruby.h>
|
7
|
+
#include <unistd.h>
|
8
|
+
#include <netdb.h>
|
9
|
+
#include <arpa/inet.h>
|
10
|
+
#include <sys/socket.h>
|
11
|
+
#include <sys/types.h>
|
12
|
+
#include <errno.h>
|
13
|
+
|
14
|
+
#define SYS_HOST_VERSION "0.6.2"
|
15
|
+
|
16
|
+
#ifndef MAXHOSTNAMELEN
|
17
|
+
#define MAXHOSTNAMELEN 256
|
18
|
+
#endif
|
19
|
+
|
20
|
+
#ifndef INET_ADDRSTRLEN
|
21
|
+
#define INET_ADDRSTRLEN 16
|
22
|
+
#endif
|
23
|
+
|
24
|
+
#ifndef HOSTENT_BUF
|
25
|
+
#define HOSTENT_BUF 8192
|
26
|
+
#endif
|
27
|
+
|
28
|
+
VALUE cHostError, sHostInfo;
|
29
|
+
|
30
|
+
/*
|
31
|
+
* call-seq:
|
32
|
+
* Host.hostname
|
33
|
+
*
|
34
|
+
* Returns the hostname of the current host. This may or not return the FQDN,
|
35
|
+
* depending on your system.
|
36
|
+
*/
|
37
|
+
static VALUE host_hostname(){
|
38
|
+
char host_name[MAXHOSTNAMELEN];
|
39
|
+
int rv;
|
40
|
+
|
41
|
+
rv = gethostname(host_name, MAXHOSTNAMELEN);
|
42
|
+
|
43
|
+
// If you get this exception, you've got big problems
|
44
|
+
if(rv != 0)
|
45
|
+
rb_raise(cHostError, "gethostname() call failed: %s", strerror(errno));
|
46
|
+
|
47
|
+
return rb_str_new2(host_name);
|
48
|
+
}
|
49
|
+
|
50
|
+
/*
|
51
|
+
* call-seq:
|
52
|
+
* Host.ip_addr
|
53
|
+
*
|
54
|
+
* Returns a list of IP addresses for the current host (yes, it is possible
|
55
|
+
* to have more than one).
|
56
|
+
*/
|
57
|
+
static VALUE host_ip_addr(){
|
58
|
+
char host_name[MAXHOSTNAMELEN];
|
59
|
+
char str[INET_ADDRSTRLEN];
|
60
|
+
char **pptr;
|
61
|
+
struct hostent* hp;
|
62
|
+
int err;
|
63
|
+
VALUE v_results = rb_ary_new();
|
64
|
+
#ifndef HAVE_INET_NTOP
|
65
|
+
struct in_addr ipa;
|
66
|
+
int n;
|
67
|
+
#endif
|
68
|
+
|
69
|
+
if(gethostname(host_name, MAXHOSTNAMELEN) != 0)
|
70
|
+
rb_raise(cHostError, "gethostname() call failed");
|
71
|
+
|
72
|
+
#ifdef HAVE_GETIPNODEBYNAME
|
73
|
+
hp = getipnodebyname(host_name, AF_INET, AI_DEFAULT, &err);
|
74
|
+
#else
|
75
|
+
hp = gethostbyname(host_name);
|
76
|
+
#endif
|
77
|
+
|
78
|
+
if(hp == NULL)
|
79
|
+
rb_raise(cHostError, "getipnodebyname() call failed");
|
80
|
+
|
81
|
+
pptr = hp->h_addr_list;
|
82
|
+
|
83
|
+
#ifdef HAVE_INET_NTOP
|
84
|
+
for( ; *pptr != NULL; pptr++){
|
85
|
+
rb_ary_push(v_results,
|
86
|
+
rb_str_new2(inet_ntop(hp->h_addrtype, *pptr, str, INET_ADDRSTRLEN)));
|
87
|
+
}
|
88
|
+
#else
|
89
|
+
for(n = 0; hp->h_addr_list[n] != NULL; n++){
|
90
|
+
memcpy(&ipa.s_addr, hp->h_addr_list[n], hp->h_length);
|
91
|
+
rb_ary_push(v_results, rb_str_new2(inet_ntoa(ipa)));
|
92
|
+
}
|
93
|
+
#endif
|
94
|
+
|
95
|
+
return v_results;
|
96
|
+
}
|
97
|
+
|
98
|
+
/* :call-seq:
|
99
|
+
* Sys::Host.info
|
100
|
+
* Sys::Host.info{ |i| ... }
|
101
|
+
*
|
102
|
+
* Yields a HostInfo struct containing various bits of information about
|
103
|
+
* the local machine for each entry in the hosts table. In non-block form,
|
104
|
+
* returns an array of HostInfo structs.
|
105
|
+
*
|
106
|
+
* The Struct::HostInfo struct contains 5 fields:
|
107
|
+
*
|
108
|
+
* * name (String)
|
109
|
+
* * aliases (Array)
|
110
|
+
* * addr_type (Integer) => Typically 2 (AF_INET) or 28 (AF_INET6)
|
111
|
+
* * length (Integer) => Typically 4 (IPv4) or 16 (IPv6)
|
112
|
+
* * addr_list (Array)
|
113
|
+
*/
|
114
|
+
static VALUE host_info(VALUE klass){
|
115
|
+
VALUE v_hostinfo = Qnil;
|
116
|
+
VALUE v_addr = rb_ary_new();
|
117
|
+
VALUE v_array = rb_ary_new();
|
118
|
+
VALUE v_aliases = rb_ary_new();
|
119
|
+
|
120
|
+
sethostent(0);
|
121
|
+
|
122
|
+
#ifdef HAVE_GETHOSTENT_R
|
123
|
+
struct hostent host;
|
124
|
+
struct hostent* result;
|
125
|
+
char sbuf[HOSTENT_BUF];
|
126
|
+
char ibuf[INET_ADDRSTRLEN];
|
127
|
+
int err;
|
128
|
+
|
129
|
+
while(!gethostent_r(&host, sbuf, HOSTENT_BUF, &result, &err)){
|
130
|
+
#ifdef __MACH__ or #ifdef __APPLE__
|
131
|
+
char **aliases = result->h_aliases;
|
132
|
+
char **addrs = result->h_addr_list;
|
133
|
+
|
134
|
+
while(*aliases){
|
135
|
+
rb_ary_push(v_aliases, rb_str_new2(*aliases));
|
136
|
+
*aliases++;
|
137
|
+
}
|
138
|
+
|
139
|
+
while(*addrs){
|
140
|
+
inet_ntop(result->h_addrtype, addrs, addr, INET_ADDRSTRLEN);
|
141
|
+
rb_ary_push(v_addr, rb_str_new2(ibuf));
|
142
|
+
memset(ibuf, 0, sizeof(ibuf));
|
143
|
+
addrs++;
|
144
|
+
}
|
145
|
+
#else
|
146
|
+
while(*result->h_aliases){
|
147
|
+
rb_ary_push(v_aliases, rb_str_new2(*result->h_aliases));
|
148
|
+
*result->h_aliases++;
|
149
|
+
}
|
150
|
+
|
151
|
+
while(*result->h_addr_list){
|
152
|
+
inet_ntop(result->h_addrtype, *result->h_addr_list, ibuf, INET_ADDRSTRLEN);
|
153
|
+
rb_ary_push(v_addr, rb_str_new2(ibuf));
|
154
|
+
*result->h_addr_list++;
|
155
|
+
memset(ibuf, 0, sizeof(ibuf));
|
156
|
+
}
|
157
|
+
#endif
|
158
|
+
|
159
|
+
// Dup the arrays because Ruby is holding onto the same reference.
|
160
|
+
v_hostinfo = rb_struct_new(sHostInfo,
|
161
|
+
rb_str_new2(result->h_name),
|
162
|
+
rb_ary_dup(v_aliases),
|
163
|
+
INT2FIX(result->h_addrtype),
|
164
|
+
INT2FIX(result->h_length),
|
165
|
+
rb_ary_dup(v_addr)
|
166
|
+
);
|
167
|
+
#else
|
168
|
+
struct hostent* host;
|
169
|
+
char ibuf[INET_ADDRSTRLEN];
|
170
|
+
|
171
|
+
while((host = gethostent())){
|
172
|
+
#ifdef __MACH__ or #ifdef __APPLE__
|
173
|
+
char **aliases = host->h_aliases;
|
174
|
+
char **addrs = host->h_addr_list;
|
175
|
+
|
176
|
+
while(*aliases){
|
177
|
+
rb_ary_push(v_aliases, rb_str_new2(*aliases));
|
178
|
+
*aliases++;
|
179
|
+
}
|
180
|
+
|
181
|
+
while(*addrs){
|
182
|
+
inet_ntop(host->h_addrtype, addrs, ibuf, INET_ADDRSTRLEN);
|
183
|
+
rb_ary_push(v_addr, rb_str_new2(ibuf));
|
184
|
+
memset(ibuf, 0, sizeof(ibuf));
|
185
|
+
addrs++;
|
186
|
+
}
|
187
|
+
#else
|
188
|
+
while(*host->h_aliases){
|
189
|
+
rb_ary_push(v_aliases, rb_str_new2(*host->h_aliases));
|
190
|
+
*host->h_aliases++;
|
191
|
+
}
|
192
|
+
|
193
|
+
while(*host->h_addr_list){
|
194
|
+
inet_ntop(host->h_addrtype, *host->h_addr_list, ibuf, INET_ADDRSTRLEN);
|
195
|
+
rb_ary_push(v_addr, rb_str_new2(ibuf));
|
196
|
+
memset(ibuf, 0, sizeof(ibuf));
|
197
|
+
*host->h_addr_list++;
|
198
|
+
}
|
199
|
+
#endif
|
200
|
+
|
201
|
+
v_hostinfo = rb_struct_new(sHostInfo,
|
202
|
+
rb_str_new2(host->h_name),
|
203
|
+
rb_ary_dup(v_aliases),
|
204
|
+
INT2FIX(host->h_addrtype),
|
205
|
+
INT2FIX(host->h_length),
|
206
|
+
rb_ary_dup(v_addr)
|
207
|
+
);
|
208
|
+
#endif
|
209
|
+
if(rb_block_given_p())
|
210
|
+
rb_yield(v_hostinfo);
|
211
|
+
else
|
212
|
+
rb_ary_push(v_array, v_hostinfo);
|
213
|
+
|
214
|
+
rb_ary_clear(v_aliases);
|
215
|
+
rb_ary_clear(v_addr);
|
216
|
+
}
|
217
|
+
|
218
|
+
endhostent();
|
219
|
+
|
220
|
+
if(rb_block_given_p())
|
221
|
+
return Qnil;
|
222
|
+
|
223
|
+
return v_array;
|
224
|
+
}
|
225
|
+
|
226
|
+
#ifdef HAVE_GETHOSTID
|
227
|
+
/*
|
228
|
+
* Sys::Host.host_id
|
229
|
+
*
|
230
|
+
* Returns the host id of the current machine.
|
231
|
+
*/
|
232
|
+
static VALUE host_host_id(){
|
233
|
+
return ULL2NUM(gethostid());
|
234
|
+
}
|
235
|
+
#endif
|
236
|
+
|
237
|
+
void Init_host()
|
238
|
+
{
|
239
|
+
VALUE sys_mSys, cHost;
|
240
|
+
|
241
|
+
/* The Sys module serves as a toplevel namespace, nothing more. */
|
242
|
+
sys_mSys = rb_define_module("Sys");
|
243
|
+
|
244
|
+
/* The Host class encapsulates information about your machine, such as
|
245
|
+
* the host name and IP address.
|
246
|
+
*/
|
247
|
+
cHost = rb_define_class_under(sys_mSys, "Host", rb_cObject);
|
248
|
+
|
249
|
+
/* This error is raised if any of the Host methods fail. */
|
250
|
+
cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);
|
251
|
+
|
252
|
+
/* 0.6.2: The version of this library. This is a string, not a number. */
|
253
|
+
rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
|
254
|
+
|
255
|
+
/* Structs */
|
256
|
+
sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
|
257
|
+
"length", "addr_list", NULL
|
258
|
+
);
|
259
|
+
|
260
|
+
/* Class Methods */
|
261
|
+
rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
|
262
|
+
rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
|
263
|
+
rb_define_singleton_method(cHost, "info", host_info, 0);
|
264
|
+
|
265
|
+
#ifdef HAVE_GETHOSTID
|
266
|
+
rb_define_singleton_method(cHost, "host_id", host_host_id, 0);
|
267
|
+
#endif
|
268
|
+
}
|