sys-host 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES CHANGED
@@ -1,3 +1,13 @@
1
+ == 0.6.0 - 8-Jun-2007
2
+ * Now supports OS X!
3
+ * Added the Host.host_id class method.
4
+ * Bug/warning fix for OS X.
5
+ * HostError is now Host::Error.
6
+ * Added a Rakefile, including tasks for installation and testing.
7
+ * Fixed the gemspec (I hope).
8
+ * Lots of internal reorganization, including the removal of the install.rb
9
+ file. That's handled by the Rakefile.
10
+
1
11
  == 0.5.2 - 27-Jun-2006
2
12
  * Added the Host.info method for Linux.
3
13
 
data/MANIFEST CHANGED
@@ -1,19 +1,15 @@
1
- CHANGES
2
- MANIFEST
3
- INSTALL
4
- extconf.rb
5
- install.rb
6
-
7
- doc/host.txt
8
-
9
- doc/examples/test.rb
10
-
11
- ext/constants.h
12
- ext/freebsd.c
13
- ext/generic.c
14
- ext/linux.c
15
- ext/sunos.c
16
-
17
- lib/windows.rb
18
-
19
- test/tc_host.rb
1
+ * CHANGES
2
+ * MANIFEST
3
+ * README
4
+ * Rakefile
5
+ * sys-host.gemspec
6
+ * doc/host.txt
7
+ * examples/host_test.rb
8
+ * ext/constants.h
9
+ * ext/extconf.rb
10
+ * ext/bsd/bsd.c
11
+ * ext/generic/generic.c
12
+ * ext/linux/linux.c
13
+ * ext/sunos/sunos.c
14
+ * lib/sys/windows.rb
15
+ * test/tc_host.rb
data/README CHANGED
@@ -1,14 +1,26 @@
1
1
  = Prerequisites
2
- Ruby 1.8.0 or later.
3
- Ruby 1.8.2 or later preferred on Windows.
2
+ Ruby 1.8.0 or later.
3
+ Ruby 1.8.2 or later preferred on Windows.
4
+
5
+ = Synopsis
6
+ require 'sys/host'
7
+ include Sys
8
+
9
+ p Host.hostname
10
+ p Host.ip_addr
11
+
12
+ Host.info{ |h|
13
+ p h
14
+ }
4
15
 
5
16
  = Installation
6
- == Windows
7
- ruby test\tc_host.rb (optional)
8
- ruby install.rb
9
-
10
- == Unix
11
- ruby extconf.rb
12
- make
13
- ruby test/tc_host.rb (optional)
14
- make install
17
+ == Standard (non-gem) installation
18
+ rake test (optional)
19
+ rake install
20
+
21
+ == Gem Installation
22
+ rake test (optional)
23
+ rake install_gem
24
+
25
+ = Documentation
26
+ See the doc/host.txt file for more information.
data/Rakefile ADDED
@@ -0,0 +1,79 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rbconfig'
5
+ include Config
6
+
7
+ desc "Clean the build files for the sys-host source for UNIX systems"
8
+ task :clean do
9
+ Dir.chdir('ext') do
10
+ unless RUBY_PLATFORM.match('mswin')
11
+ sh 'make distclean' if File.exists?('host.o')
12
+ FileUtils.rm_rf('host.c') if File.exists?('host.c')
13
+ FileUtils.rm_rf('sys') if File.exists?('sys')
14
+ end
15
+ end
16
+ FileUtils.rm_rf('sys') if File.exists?('sys')
17
+ end
18
+
19
+ desc "Build the sys-host package on UNIX systems (but don't install it)"
20
+ task :build => [:clean] do
21
+ Dir.chdir('ext') do
22
+ ruby 'extconf.rb'
23
+ sh 'make'
24
+ build_file = 'host.' + Config::CONFIG['DLEXT']
25
+ Dir.mkdir('sys') unless File.exists?('sys')
26
+ FileUtils.cp(build_file, 'sys')
27
+ end
28
+ end
29
+
30
+ desc "Run the example program"
31
+ task :example => [:build] do
32
+ ruby 'host_test.rb'
33
+ end
34
+
35
+ if RUBY_PLATFORM.match('mswin')
36
+ desc "Install the sys-host package (non-gem)"
37
+ task :install do
38
+ install_dir = File.join(CONFIG['sitelibdir'], 'sys')
39
+ Dir.mkdir(install_dir) unless File.exists?(install_dir)
40
+ FileUtils.cp('lib/sys/windows.rb', install_dir, :verbose => true)
41
+ end
42
+ else
43
+ desc "Install the sys-host package (non-gem)"
44
+ task :install => [:build] do
45
+ Dir.chdir('ext') do
46
+ sh 'make install'
47
+ end
48
+ end
49
+ end
50
+
51
+ desc "Install the sys-host package as a gem"
52
+ task :install_gem do
53
+ ruby 'sys-host.gemspec'
54
+ file = Dir["sys-host*.gem"].last
55
+ sh "gem install #{file}"
56
+ end
57
+
58
+ desc "Run the example sys-host program"
59
+ task :example => [:build] do
60
+ Dir.mkdir('sys') unless File.exists?('sys')
61
+ if RUBY_PLATFORM.match('mswin')
62
+ FileUtils.cp('lib/sys/windows.rb', 'lib/sys/host.rb')
63
+ ruby '-Ilib examples/host_test.rb'
64
+ else
65
+ ruby '-Iext examples/host_test.rb'
66
+ end
67
+ end
68
+
69
+ Rake::TestTask.new do |t|
70
+ if RUBY_PLATFORM.match('mswin')
71
+ FileUtils.cp('lib/sys/windows.rb', 'lib/sys/host.rb')
72
+ t.libs << 'lib'
73
+ else
74
+ task :test => :build
75
+ t.libs << 'ext'
76
+ t.libs.delete('lib')
77
+ end
78
+ t.test_files = FileList['test/tc_host.rb']
79
+ end
data/doc/host.txt CHANGED
@@ -33,7 +33,7 @@ Host.info(host=localhost){ |h| ... }
33
33
  vary depending on your platform.
34
34
 
35
35
  == Exception Classes
36
- HostError < StandardError
36
+ Host::Error < StandardError
37
37
  Raised in the event of a failure for any of the class methods provided
38
38
  with this package. Generally speaking, it means there was a failure in
39
39
  the underlying gethostname() or gethostbyname() calls.
@@ -68,7 +68,7 @@ HostError < StandardError
68
68
  Ruby's
69
69
 
70
70
  == Copyright
71
- Copyright 2002-2006, Daniel J. Berger, djberg96 at gmail dot com
71
+ Copyright 2002-2007, Daniel J. Berger, djberg96 at gmail dot com
72
72
 
73
73
  All Rights Reserved. This module is free software. It may be used,
74
74
  redistributed and/or modified under the same terms as Ruby itself.
data/ext/bsd/bsd.c ADDED
@@ -0,0 +1,247 @@
1
+ /******************************************************************************
2
+ * bsd.c (host.c) - Ruby extension for the sys-host package for BSD flavors,
3
+ * including OS X.
4
+ *
5
+ * Author: Daniel Berger
6
+ *****************************************************************************/
7
+ #include <ruby.h>
8
+ #include <constants.h>
9
+ #include <unistd.h>
10
+ #include <netdb.h>
11
+ #include <arpa/inet.h>
12
+ #include <sys/socket.h>
13
+ #include <sys/types.h>
14
+ #include <errno.h>
15
+
16
+ #ifdef __cplusplus
17
+ extern "C"
18
+ {
19
+ #endif
20
+
21
+ VALUE cHostError, sHostInfo;
22
+
23
+ /*
24
+ * call-seq:
25
+ * Host.hostname
26
+ *
27
+ * Returns the hostname of the current host. This may or not return the FQDN,
28
+ * depending on your system.
29
+ */
30
+ static VALUE host_hostname(){
31
+ char host_name[MAXHOSTNAMELEN];
32
+ int rv;
33
+ rv = gethostname(host_name, MAXHOSTNAMELEN);
34
+
35
+ /* If you get this exception, you've got big problems */
36
+ if(rv != 0)
37
+ rb_raise(cHostError, "gethostname() call failed: %s", strerror(errno));
38
+
39
+ return rb_str_new2(host_name);
40
+ }
41
+
42
+ /*
43
+ * call-seq:
44
+ * Host.ip_addr
45
+ *
46
+ * Returns a list of IP addresses for the current host (yes, it is possible
47
+ * to have more than one).
48
+ */
49
+ static VALUE host_ip_addr(){
50
+ char host_name[MAXHOSTNAMELEN];
51
+ char str[INET_ADDRSTRLEN];
52
+ char **pptr;
53
+ struct hostent* hp;
54
+ int err;
55
+ VALUE v_results = rb_ary_new();
56
+ #ifndef HAVE_INET_NTOP
57
+ struct in_addr ipa;
58
+ int n;
59
+ #endif
60
+
61
+ if(gethostname(host_name, MAXHOSTNAMELEN) != 0)
62
+ rb_raise(cHostError, "gethostname() call failed");
63
+
64
+ #ifdef HAVE_GETIPNODEBYNAME
65
+ hp = getipnodebyname(host_name, AF_INET, AI_DEFAULT, &err);
66
+ #else
67
+ hp = gethostbyname(host_name);
68
+ #endif
69
+
70
+ if(hp == NULL)
71
+ rb_raise(cHostError, "getipnodebyname() call failed");
72
+
73
+ pptr = hp->h_addr_list;
74
+
75
+ #ifdef HAVE_INET_NTOP
76
+ for( ; *pptr != NULL; pptr++){
77
+ rb_ary_push(v_results,
78
+ rb_str_new2(inet_ntop(hp->h_addrtype, *pptr, str, INET_ADDRSTRLEN)));
79
+ }
80
+ #else
81
+ for(n = 0; hp->h_addr_list[n] != NULL; n++){
82
+ memcpy(&ipa.s_addr, hp->h_addr_list[n], hp->h_length);
83
+ rb_ary_push(v_results, rb_str_new2(inet_ntoa(ipa)));
84
+ }
85
+ #endif
86
+ return v_results;
87
+ }
88
+
89
+ /* :call-seq:
90
+ * Sys::Host.info
91
+ * Sys::Host.info{ |i| ... }
92
+ *
93
+ * Yields a HostInfo struct containing various bits of information about
94
+ * the local machine for each entry in the hosts table. In non-block form,
95
+ * returns an array of HostInfo structs.
96
+ *
97
+ * The Struct::HostInfo struct contains 5 fields:
98
+ *
99
+ * * name (String)
100
+ * * aliases (Array)
101
+ * * addr_type (Integer)
102
+ * * length (Integer)
103
+ * * addr_list (Array)
104
+ */
105
+ static VALUE host_info(VALUE klass){
106
+ VALUE v_hostinfo = Qnil;
107
+ VALUE v_addr = rb_ary_new();
108
+ VALUE v_array = rb_ary_new();
109
+ VALUE v_aliases = rb_ary_new();
110
+
111
+ sethostent(0);
112
+
113
+ #ifdef HAVE_GETHOSTENT_R
114
+ struct hostent host;
115
+ struct hostent* result;
116
+ char buf[HOSTENT_BUF];
117
+ int err;
118
+
119
+ while(!gethostent_r(&host, buf, HOSTENT_BUF, &result, &err)){
120
+ while(*result->h_aliases){
121
+ rb_ary_push(v_aliases, rb_str_new2(*result->h_aliases));
122
+ *result->h_aliases++;
123
+ }
124
+
125
+ #ifdef __MACH__ or #ifdef __APPLE__
126
+ char **addrs = host->h_addr_list;
127
+ while(*addrs){
128
+ inet_ntop(result->h_addrtype, addrs, buf, HOSTENT_BUF);
129
+ rb_ary_push(v_addr, rb_str_new2(buf));
130
+ addrs++;
131
+ }
132
+ #else
133
+ while(*result->h_addr_list){
134
+ inet_ntop(result->h_addrtype, *result->h_addr_list, buf, HOSTENT_BUF);
135
+ rb_ary_push(v_addr, rb_str_new2(buf));
136
+ *result->h_addr_list++;
137
+ }
138
+ #endif
139
+
140
+ /* We need to dup the arrays because Ruby is holding onto the same
141
+ * reference.
142
+ */
143
+ v_hostinfo = rb_struct_new(sHostInfo,
144
+ rb_str_new2(result->h_name),
145
+ rb_ary_dup(v_aliases),
146
+ INT2FIX(result->h_addrtype),
147
+ INT2FIX(result->h_length),
148
+ rb_ary_dup(v_addr)
149
+ );
150
+ #else
151
+ struct hostent* host;
152
+ char buf[INET_ADDRSTRLEN];
153
+
154
+ while((host = gethostent())){
155
+ while(*host->h_aliases){
156
+ rb_ary_push(v_aliases, rb_str_new2(*host->h_aliases));
157
+ *host->h_aliases++;
158
+ }
159
+
160
+ #ifdef __MACH__ or #ifdef __APPLE__
161
+ char **addrs = host->h_addr_list;
162
+ while(*addrs){
163
+ inet_ntop(host->h_addrtype, addrs, buf, HOSTENT_BUF);
164
+ rb_ary_push(v_addr, rb_str_new2(buf));
165
+ addrs++;
166
+ }
167
+ #else
168
+ while(*host->h_addr_list){
169
+ inet_ntop(host->h_addrtype, *host->h_addr_list, buf, HOSTENT_BUF);
170
+ rb_ary_push(v_addr, rb_str_new2(buf));
171
+ *host->h_addr_list++;
172
+ }
173
+ #endif
174
+
175
+ v_hostinfo = rb_struct_new(sHostInfo,
176
+ rb_str_new2(host->h_name),
177
+ rb_ary_dup(v_aliases),
178
+ INT2FIX(host->h_addrtype),
179
+ INT2FIX(host->h_length),
180
+ rb_ary_dup(v_addr)
181
+ );
182
+ #endif
183
+
184
+ if(rb_block_given_p())
185
+ rb_yield(v_hostinfo);
186
+ else
187
+ rb_ary_push(v_array, v_hostinfo);
188
+
189
+ rb_ary_clear(v_aliases);
190
+ rb_ary_clear(v_addr);
191
+ }
192
+
193
+ endhostent();
194
+
195
+ if(rb_block_given_p())
196
+ return Qnil;
197
+
198
+ return v_array;
199
+ }
200
+
201
+ #ifdef HAVE_GETHOSTID
202
+ /*
203
+ * Sys::Host.host_id
204
+ *
205
+ * Returns the host id of the current machine.
206
+ */
207
+ static VALUE host_host_id(){
208
+ return ULL2NUM(gethostid());
209
+ }
210
+ #endif
211
+
212
+ void Init_host()
213
+ {
214
+ VALUE sys_mSys, cHost;
215
+
216
+ /* The Sys module serves as a toplevel namespace, nothing more. */
217
+ sys_mSys = rb_define_module("Sys");
218
+
219
+ /* The Host class encapsulates information about your machine, such as
220
+ * the host name and IP address.
221
+ */
222
+ cHost = rb_define_class_under(sys_mSys, "Host", rb_cObject);
223
+
224
+ /* This error is raised if any of the Host methods fail. */
225
+ cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);
226
+
227
+ /* 0.6.0: The version of this package. This is a string, not a number. */
228
+ rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
229
+
230
+ /* Structs */
231
+ sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
232
+ "length", "addr_list", NULL
233
+ );
234
+
235
+ /* Class Methods */
236
+ rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
237
+ rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
238
+ rb_define_singleton_method(cHost, "info", host_info, 0);
239
+
240
+ #ifdef HAVE_GETHOSTID
241
+ rb_define_singleton_method(cHost, "host_id", host_host_id, 0);
242
+ #endif
243
+ }
244
+
245
+ #ifdef __cplusplus
246
+ }
247
+ #endif
data/ext/constants.h CHANGED
@@ -1,5 +1,5 @@
1
1
  /* constants.h - one version to bind them all... */
2
- #define SYS_HOST_VERSION "0.5.2"
2
+ #define SYS_HOST_VERSION "0.6.0"
3
3
 
4
4
  #ifndef MAXHOSTNAMELEN
5
5
  #define MAXHOSTNAMELEN 256
data/ext/extconf.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "mkmf"
2
+ require "fileutils"
3
+
4
+ have_func("gethostid")
5
+ have_func("inet_ntop")
6
+
7
+ case RUBY_PLATFORM
8
+ when /bsd|darwin|powerpc|mach/i
9
+ have_func("getipnodebyname")
10
+ FileUtils.cp("bsd/bsd.c", "host.c")
11
+ when /sunos|solaris/i
12
+ have_library("nsl")
13
+ have_library("socket")
14
+ have_func("gethostbyname_r", "netdb.h")
15
+ have_func("gethostent_r", "netdb.h")
16
+ FileUtils.cp("sunos/sunos.c", "host.c")
17
+ when /linux/i
18
+ have_func("gethostbyname_r", "netdb.h")
19
+ have_func("gethostent_r", "netdb.h")
20
+ FileUtils.cp("linux/linux.c", "host.c")
21
+ when /win32|windows|dos|mingw|cygwin/i
22
+ STDERR.puts "Run the 'rake install' task to install on Windows systems"
23
+ else
24
+ FileUtils.cp("generic/generic.c", "host.c")
25
+ end
26
+
27
+ # Already grabbed this from netdb.h on Solaris
28
+ unless RUBY_PLATFORM =~ /sunos|solaris/i
29
+ have_func("gethostent_r")
30
+ end
31
+
32
+ create_makefile("sys/host")
data/test/tc_host.rb CHANGED
@@ -1,38 +1,15 @@
1
- #########################################
1
+ #################################################
2
2
  # tc_host.rb
3
3
  #
4
- # Test suite for sys-host, all platforms
5
- #########################################
6
- base = File.basename(Dir.pwd)
7
- if base == "test" || base =~ /host.*/
8
- require "ftools"
9
- Dir.chdir("..") if base == "test"
10
- Dir.mkdir("sys") unless File.exists?("sys")
11
-
12
- if RUBY_PLATFORM.match("mswin")
13
- File.copy("lib/windows.rb", "sys/host.rb")
14
- else
15
- case RUBY_PLATFORM
16
- when /hpux/i
17
- File.copy("host.sl","sys")
18
- when /osx|darwin|mac/i
19
- File.copy("host.bundle","sys")
20
- else
21
- File.copy("host.so","sys")
22
- end
23
- end
24
-
25
- $LOAD_PATH.unshift Dir.pwd
26
- Dir.chdir("test") rescue nil
27
- end
28
-
29
- require "sys/host"
30
- require "test/unit"
4
+ # Test suite for sys-host, all platforms.
5
+ #################################################
6
+ require 'sys/host'
7
+ require 'test/unit'
31
8
  include Sys
32
9
 
33
10
  class TC_Host < Test::Unit::TestCase
34
11
  def test_version
35
- assert_equal("0.5.2",Host::VERSION)
12
+ assert_equal('0.6.0', Host::VERSION)
36
13
  end
37
14
 
38
15
  def test_hostname
@@ -48,14 +25,20 @@ class TC_Host < Test::Unit::TestCase
48
25
  assert_kind_of(Array, Host.ip_addr)
49
26
  end
50
27
 
28
+ def test_host_id
29
+ assert_respond_to(Host, :host_id)
30
+ assert_nothing_raised{ Host.host_id }
31
+ assert_kind_of(Integer, Host.host_id)
32
+ end
33
+
51
34
  def test_info
52
- if RUBY_PLATFORM =~ /mswin|solaris|linux/i
35
+ if RUBY_PLATFORM =~ /mswin|solaris|linux|bsd|mach|darwin|osx/i
53
36
  assert_respond_to(Host, :info)
54
37
  assert_nothing_raised{ Host.info }
55
38
  assert_kind_of(Array, Host.info)
56
39
  assert_kind_of(Struct::HostInfo, Host.info.first)
57
40
  assert_nil(Host.info{ })
58
- assert_nothing_raised{ 1000.times{ Host.info } }
41
+ assert_nothing_raised{ 100.times{ Host.info } }
59
42
  else
60
43
  puts "The 'test_info' test was skipped on this platform"
61
44
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: sys-host
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.2
7
- date: 2006-06-30 00:00:00 -06:00
6
+ version: 0.6.0
7
+ date: 2007-06-08 00:00:00 -06:00
8
8
  summary: Provides hostname and ip address info for a given host
9
9
  require_paths:
10
10
  - lib
@@ -33,10 +33,11 @@ files:
33
33
  - test/tc_host.rb
34
34
  - CHANGES
35
35
  - MANIFEST
36
+ - Rakefile
36
37
  - README
37
- - extconf.rb
38
+ - ext/extconf.rb
38
39
  - ext/constants.h
39
- - ext/sunos.c
40
+ - ext/bsd/bsd.c
40
41
  test_files:
41
42
  - test/tc_host.rb
42
43
  rdoc_options: []
@@ -44,10 +45,11 @@ rdoc_options: []
44
45
  extra_rdoc_files:
45
46
  - CHANGES
46
47
  - README
48
+ - MANIFEST
47
49
  executables: []
48
50
 
49
51
  extensions:
50
- - extconf.rb
52
+ - ext/extconf.rb
51
53
  requirements: []
52
54
 
53
55
  dependencies: []
data/ext/sunos.c DELETED
@@ -1,236 +0,0 @@
1
- /*****************************************************************
2
- * sunos.c (host.c) - Solaris specific version
3
- *
4
- * Author: Daniel Berger
5
- *****************************************************************/
6
- #include "ruby.h"
7
- #include "constants.h"
8
- #include <unistd.h>
9
- #include <netdb.h>
10
-
11
- #ifndef HAVE_INET_NTOP
12
-
13
- #ifndef _SYS_TYPES_H
14
- #include <sys/types.h>
15
- #endif
16
-
17
- #ifndef _SYS_SOCKET_H
18
- #include <sys/socket.h>
19
- #endif
20
-
21
- #ifndef _NETINET_IN_H
22
- #include <netinet/in.h>
23
- #endif
24
-
25
- #endif
26
-
27
- #include <arpa/inet.h>
28
-
29
- #define MAXBUF 2048
30
-
31
- #ifdef __cplusplus
32
- extern "C"
33
- {
34
- #endif
35
-
36
- VALUE cHostError, sHostInfo;
37
-
38
- /*
39
- * Sys::Host.hostname
40
- *
41
- * Returns the hostname of the current machine.
42
- */
43
- static VALUE host_hostname()
44
- {
45
- char host_name[MAXHOSTNAMELEN];
46
-
47
- if(gethostname(host_name, sizeof(host_name)) != 0)
48
- rb_raise(cHostError, "gethostname() call failed");
49
-
50
- return rb_str_new2(host_name);
51
- }
52
-
53
- #ifdef HAVE_GETHOSTID
54
- /*
55
- * Sys::Host.hostid
56
- *
57
- * Returns the host id of the current machine.
58
- */
59
- static VALUE host_hostid(){
60
- return ULL2NUM(gethostid());
61
- }
62
- #endif
63
-
64
- /*
65
- * Sys::Host.ip_addr
66
- *
67
- * Returns an array of IP addresses associated with the current hostname.
68
- */
69
- static VALUE host_ip_addr()
70
- {
71
- char host_name[MAXHOSTNAMELEN];
72
- struct hostent* hp;
73
- VALUE v_addr = rb_ary_new();
74
- #ifndef HAVE_INET_NTOP
75
- struct in_addr ipa;
76
- #endif
77
-
78
- if(gethostname(host_name, sizeof(host_name)) != 0)
79
- rb_raise(cHostError, "gethostname() call failed");
80
-
81
- #ifdef HAVE_GETHOSTBYNAME_R
82
- {
83
- char buf[HOSTENT_BUF];
84
- struct hostent hp_buf;
85
- int err;
86
- hp = gethostbyname_r(host_name, &hp_buf, buf, sizeof(buf), &err);
87
- }
88
- #else
89
- hp = gethostbyname(host_name);
90
- #endif
91
-
92
- if(!hp)
93
- rb_raise(cHostError, "gethostbyname() call failed");
94
-
95
- #ifdef HAVE_INET_NTOP
96
- char str[INET_ADDRSTRLEN];
97
- while(*hp->h_addr_list){
98
- rb_ary_push(
99
- v_addr,
100
- rb_str_new2(inet_ntop(hp->h_addrtype, *hp->h_addr_list, str, sizeof(str)))
101
- );
102
- *hp->h_addr_list++;
103
- }
104
- #else
105
- int n;
106
- for(n = 0; hp->h_addr_list[n] != NULL; n++){
107
- memcpy(&ipa.s_addr, hp->h_addr_list[n], hp->h_length);
108
- rb_ary_push(rbResults, rb_str_new2(inet_ntoa(ipa)));
109
- }
110
- #endif
111
-
112
- return v_addr;
113
- }
114
-
115
- /*
116
- * Sys::Host.info
117
- * Sys::Host.info{ |i| ... }
118
- *
119
- * Yields a HostInfo struct containing various bits of information about
120
- * the local machine for each entry in the hosts table. In non-block form,
121
- * returns an array of HostInfo structs.
122
- *
123
- * The Struct::HostInfo struct contains 5 fields:
124
- *
125
- * * name (String)
126
- * * aliases (Array)
127
- * * addr_type (Integer)
128
- * * length (Integer)
129
- * * addr_list (Array)
130
- */
131
- static VALUE host_info(VALUE klass){
132
- VALUE v_hostinfo;
133
- VALUE v_addr = rb_ary_new();
134
- VALUE v_array = rb_ary_new();
135
- VALUE v_aliases = rb_ary_new();
136
-
137
- sethostent(0);
138
-
139
- #ifdef HAVE_GETHOSTENT_R
140
- struct hostent host;
141
- char buf[MAXBUF];
142
- int err;
143
-
144
- while(gethostent_r(&host, buf, sizeof(buf), &err)){
145
- while(*host.h_aliases){
146
- rb_ary_push(v_aliases, rb_str_new2(*host.h_aliases));
147
- *host.h_aliases++;
148
- }
149
-
150
- while(*host.h_addr_list){
151
- inet_ntop(host.h_addrtype, *host.h_addr_list, buf, sizeof(buf));
152
- rb_ary_push(v_addr, rb_str_new2(buf));
153
- *host.h_addr_list++;
154
- }
155
-
156
- v_hostinfo = rb_struct_new(sHostInfo,
157
- rb_str_new2(host.h_name),
158
- v_aliases,
159
- INT2FIX(host.h_addrtype),
160
- INT2FIX(host.h_length),
161
- v_addr
162
- );
163
- #else
164
- struct hostent* host;
165
- char buf[INET_ADDRSTRLEN];
166
-
167
- while((host = gethostent())){
168
- while(*host->h_aliases){
169
- rb_ary_push(v_aliases, rb_str_new2(*host->h_aliases));
170
- *host->h_aliases++;
171
- }
172
-
173
- while(*host->h_addr_list){
174
- inet_ntop(host->h_addrtype, *host->h_addr_list, buf, sizeof(buf));
175
- rb_ary_push(v_addr, rb_str_new2(buf));
176
- *host->h_addr_list++;
177
- }
178
-
179
- v_hostinfo = rb_struct_new(sHostInfo,
180
- rb_str_new2(host->h_name),
181
- v_aliases,
182
- INT2FIX(host->h_addrtype),
183
- INT2FIX(host->h_length),
184
- v_addr
185
- );
186
- #endif
187
-
188
- if(rb_block_given_p())
189
- rb_yield(v_hostinfo);
190
- else
191
- rb_ary_push(v_array, v_hostinfo);
192
-
193
- rb_ary_clear(v_aliases);
194
- rb_ary_clear(v_addr);
195
- }
196
-
197
- endhostent();
198
-
199
- if(rb_block_given_p())
200
- return Qnil;
201
-
202
- return v_array;
203
- }
204
-
205
- void Init_host()
206
- {
207
- VALUE mSys, cHost;
208
-
209
- /* Modules and Classes */
210
- mSys = rb_define_module("Sys");
211
- cHost = rb_define_class_under(mSys, "Host", rb_cObject);
212
-
213
- /* Constants */
214
- rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
215
-
216
- /* Structs */
217
- sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
218
- "length", "addr_list", 0
219
- );
220
-
221
- /* Class Methods */
222
- rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
223
- rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
224
- rb_define_singleton_method(cHost, "info", host_info, 0);
225
-
226
- #ifdef HAVE_GETHOSTID
227
- rb_define_singleton_method(cHost, "hostid", host_hostid, 0);
228
- #endif
229
-
230
- /* Exceptions */
231
- cHostError = rb_define_class_under(mSys,"HostError",rb_eStandardError);
232
- }
233
-
234
- #ifdef __cplusplus
235
- }
236
- #endif
data/extconf.rb DELETED
@@ -1,48 +0,0 @@
1
- require "mkmf"
2
- require "ftools"
3
-
4
- # Remove any symlinks that may already exist
5
- File.delete('host.c') if File.exists?('host.c')
6
- File.delete('constants.h') if File.exists?('constants.h')
7
-
8
- have_func("gethostid")
9
-
10
- case RUBY_PLATFORM
11
- when /freebsd/i
12
- have_func("getipnodebyname")
13
- File.symlink("ext/freebsd.c", "host.c")
14
- File.symlink("ext/constants.h", "constants.h")
15
- when /sunos|solaris/i
16
- have_library("nsl")
17
- have_library("socket")
18
- have_func("gethostbyname_r", "netdb.h")
19
- have_func("gethostent_r", "netdb.h")
20
- File.symlink("ext/sunos.c", "host.c")
21
- File.symlink("ext/constants.h", "constants.h")
22
- when /linux/i
23
- have_func("gethostbyname_r", "netdb.h")
24
- have_func("gethostent_r", "netdb.h")
25
- File.symlink("ext/linux.c", "host.c")
26
- File.symlink("ext/constants.h", "constants.h")
27
- when /win32|windows|dos|mingw|cygwin/i
28
- STDERR.puts "Run the 'install.rb' script to install on Windows systems"
29
- else
30
- File.symlink("ext/generic.c", "host.c")
31
- File.symlink("ext/constants.h", "constants.h")
32
- end
33
-
34
- # Rename the windows.rb file to prevent 'make install' from installing it
35
- unless RUBY_PLATFORM.match("mswin")
36
- if File.exists?('lib/windows.rb')
37
- File.rename('lib/windows.rb', 'lib/windows.orig')
38
- end
39
- end
40
-
41
- have_func("inet_ntop")
42
-
43
- # Already grabbed this from netdb.h on Solaris
44
- unless RUBY_PLATFORM =~ /sunos|solaris/i
45
- have_func("gethostent_r")
46
- end
47
-
48
- create_makefile("sys/host")