sys-host 0.6.0 → 0.6.1

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.1 - 18-Nov-2008
2
+ * Added the host_id method for Windows, which just returns the MAC address.
3
+ * Fixed an issue in the Host.ip_addr method for MS Windows where multiple ip
4
+ addresses might not be handled properly. Thanks go to Murmansk Manny for the
5
+ spot and patch.
6
+ * Updated the gemspec to include all .c and .h files now so that it should
7
+ successfully build and install on all Unix platforms.
8
+ * Rakefile fix for MS Windows for the install task.
9
+ * The test file is now "test_sys_host.rb".
10
+
1
11
  == 0.6.0 - 8-Jun-2007
2
12
  * Now supports OS X!
3
13
  * Added the Host.host_id class method.
data/MANIFEST CHANGED
@@ -12,4 +12,4 @@
12
12
  * ext/linux/linux.c
13
13
  * ext/sunos/sunos.c
14
14
  * lib/sys/windows.rb
15
- * test/tc_host.rb
15
+ * test/test_sys_host.rb
data/Rakefile CHANGED
@@ -32,12 +32,13 @@ task :example => [:build] do
32
32
  ruby 'host_test.rb'
33
33
  end
34
34
 
35
- if RUBY_PLATFORM.match('mswin')
35
+ if CONFIG['host_os'].match('mswin')
36
36
  desc "Install the sys-host package (non-gem)"
37
37
  task :install do
38
38
  install_dir = File.join(CONFIG['sitelibdir'], 'sys')
39
+ install_file = File.join(install_dir, 'host.rb')
39
40
  Dir.mkdir(install_dir) unless File.exists?(install_dir)
40
- FileUtils.cp('lib/sys/windows.rb', install_dir, :verbose => true)
41
+ FileUtils.cp('lib/sys/windows.rb', install_file, :verbose => true)
41
42
  end
42
43
  else
43
44
  desc "Install the sys-host package (non-gem)"
@@ -58,7 +59,7 @@ end
58
59
  desc "Run the example sys-host program"
59
60
  task :example => [:build] do
60
61
  Dir.mkdir('sys') unless File.exists?('sys')
61
- if RUBY_PLATFORM.match('mswin')
62
+ if CONFIG['host_os'].match('mswin')
62
63
  FileUtils.cp('lib/sys/windows.rb', 'lib/sys/host.rb')
63
64
  ruby '-Ilib examples/host_test.rb'
64
65
  else
@@ -67,13 +68,11 @@ task :example => [:build] do
67
68
  end
68
69
 
69
70
  Rake::TestTask.new do |t|
70
- if RUBY_PLATFORM.match('mswin')
71
+ if CONFIG['host_os'].match('mswin')
71
72
  FileUtils.cp('lib/sys/windows.rb', 'lib/sys/host.rb')
72
- t.libs << 'lib'
73
73
  else
74
74
  task :test => :build
75
75
  t.libs << 'ext'
76
76
  t.libs.delete('lib')
77
77
  end
78
- t.test_files = FileList['test/tc_host.rb']
79
78
  end
data/doc/host.txt CHANGED
@@ -15,7 +15,7 @@
15
15
 
16
16
  == Constants
17
17
  VERSION
18
- Returns the current version number of this package (as a string)
18
+ Returns the current version number of this library (as a string)
19
19
 
20
20
  == Class Methods
21
21
  Host.hostname
@@ -35,7 +35,7 @@ Host.info(host=localhost){ |h| ... }
35
35
  == Exception Classes
36
36
  Host::Error < StandardError
37
37
  Raised in the event of a failure for any of the class methods provided
38
- with this package. Generally speaking, it means there was a failure in
38
+ with this library. Generally speaking, it means there was a failure in
39
39
  the underlying gethostname() or gethostbyname() calls.
40
40
 
41
41
  == Future Plans
@@ -43,9 +43,9 @@ Host::Error < StandardError
43
43
  system on an IPV6 network. Any help on this front appreciated.
44
44
 
45
45
  == Notes
46
- The Windows version of this package uses OLE + WMI. The Host.info method
46
+ The Windows version of this library uses OLE + WMI. The Host.info method
47
47
  returns considerably more information than the *nix versions of this
48
- package.
48
+ library.
49
49
 
50
50
  == Comments and Questions
51
51
  Please direct all comments and/or questions to one of the Forums on the
@@ -68,13 +68,13 @@ Host::Error < StandardError
68
68
  Ruby's
69
69
 
70
70
  == Copyright
71
- Copyright 2002-2007, Daniel J. Berger, djberg96 at gmail dot com
71
+ Copyright 2002-2008, 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.
75
75
 
76
76
  == Warranty
77
- This package is provided "as is" and without any express or
77
+ This library is provided "as is" and without any express or
78
78
  implied warranties, including, without limitation, the implied
79
79
  warranties of merchantability and fitness for a particular purpose.
80
80
 
data/ext/bsd/bsd.c CHANGED
@@ -224,7 +224,7 @@ void Init_host()
224
224
  /* This error is raised if any of the Host methods fail. */
225
225
  cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);
226
226
 
227
- /* 0.6.0: The version of this package. This is a string, not a number. */
227
+ /* 0.6.1: The version of this library. This is a string, not a number. */
228
228
  rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
229
229
 
230
230
  /* Structs */
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.6.0"
2
+ #define SYS_HOST_VERSION "0.6.1"
3
3
 
4
4
  #ifndef MAXHOSTNAMELEN
5
5
  #define MAXHOSTNAMELEN 256
data/ext/extconf.rb CHANGED
@@ -1,32 +1,35 @@
1
- require "mkmf"
2
- require "fileutils"
1
+ require 'mkmf'
2
+ require 'fileutils'
3
3
 
4
- have_func("gethostid")
5
- have_func("inet_ntop")
4
+ dir_config('host')
5
+
6
+ have_func('gethostid')
7
+ have_func('inet_ntop')
8
+ Dir.mkdir('sys') unless File.exists?('sys')
6
9
 
7
10
  case RUBY_PLATFORM
8
11
  when /bsd|darwin|powerpc|mach/i
9
- have_func("getipnodebyname")
10
- FileUtils.cp("bsd/bsd.c", "host.c")
12
+ have_func('getipnodebyname')
13
+ FileUtils.cp('bsd/bsd.c', 'sys/host.c')
11
14
  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")
15
+ have_library('nsl')
16
+ have_library('socket')
17
+ have_func('gethostbyname_r', 'netdb.h')
18
+ have_func('gethostent_r', 'netdb.h')
19
+ FileUtils.cp('sunos/sunos.c', 'sys/host.c')
17
20
  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
+ have_func('gethostbyname_r', 'netdb.h')
22
+ have_func('gethostent_r', 'netdb.h')
23
+ FileUtils.cp('linux/linux.c', 'sys/host.c')
21
24
  when /win32|windows|dos|mingw|cygwin/i
22
- STDERR.puts "Run the 'rake install' task to install on Windows systems"
25
+ STDERR.puts 'Run the "rake install" task to install on MS Windows'
23
26
  else
24
- FileUtils.cp("generic/generic.c", "host.c")
27
+ FileUtils.cp('generic/generic.c', 'sys/host.c')
25
28
  end
26
29
 
27
30
  # Already grabbed this from netdb.h on Solaris
28
31
  unless RUBY_PLATFORM =~ /sunos|solaris/i
29
- have_func("gethostent_r")
32
+ have_func('gethostent_r')
30
33
  end
31
34
 
32
- create_makefile("sys/host")
35
+ create_makefile('sys/host', 'sys')
@@ -0,0 +1,189 @@
1
+ /******************************************************************************
2
+ * generic.c (host.c) - Ruby Extension
3
+ *
4
+ * Author: Daniel Berger
5
+ *
6
+ * This source is mostly borrowed from the linux source for sys-host, except
7
+ * that it does not support the gethostbyname_r() method.
8
+ *****************************************************************************/
9
+ #include <ruby.h>
10
+ #include <constants.h>
11
+ #include <unistd.h>
12
+ #include <netdb.h>
13
+ #include <arpa/inet.h>
14
+
15
+ #ifdef __cplusplus
16
+ extern "C"
17
+ {
18
+ #endif
19
+
20
+ VALUE cHostError, sHostInfo;
21
+
22
+ /*
23
+ * Sys::Host.hostname
24
+ *
25
+ * Returns the hostname of the current machine.
26
+ */
27
+ static VALUE host_hostname()
28
+ {
29
+ char host_name[MAXHOSTNAMELEN];
30
+
31
+ /* If you get this exception, you've got big problems */
32
+ if(gethostname(host_name, MAXHOSTNAMELEN) != 0)
33
+ rb_raise(cHostError,"gethostname() call failed");
34
+
35
+ return rb_str_new2(host_name);
36
+ }
37
+
38
+ /*
39
+ * Sys::Host.ip_addr
40
+ *
41
+ * Returns an array of IP addresses associated with the current hostname.
42
+ */
43
+ static VALUE host_ip_addr()
44
+ {
45
+ char host_name[MAXHOSTNAMELEN];
46
+ char str[INET_ADDRSTRLEN];
47
+ char **pptr;
48
+ struct hostent* hp;
49
+ VALUE v_results = rb_ary_new();
50
+ #ifndef HAVE_INET_NTOP
51
+ struct in_addr ipa;
52
+ int n;
53
+ #endif
54
+
55
+ if(gethostname(host_name, MAXHOSTNAMELEN) != 0)
56
+ rb_raise(cHostError, "gethostname() call failed");
57
+
58
+ hp = gethostbyname(host_name);
59
+
60
+ if(!hp)
61
+ rb_raise(cHostError, "gethostbyname() call failed");
62
+
63
+ pptr = hp->h_addr_list;
64
+
65
+ #ifdef HAVE_INET_NTOP
66
+ for( ; *pptr != NULL; pptr++) {
67
+ rb_ary_push(v_results,
68
+ rb_str_new2(inet_ntop(hp->h_addrtype, *pptr, str, sizeof(str))));
69
+ }
70
+ #else
71
+ for(n = 0; hp->h_addr_list[n] != NULL; n++) {
72
+ memcpy(&ipa.s_addr, hp->h_addr_list[n], hp->h_length);
73
+ rb_ary_push(v_results, rb_str_new2(inet_ntoa(ipa)));
74
+ }
75
+ #endif
76
+ return v_results;
77
+
78
+ }
79
+
80
+ #ifdef HAVE_GETHOSTID
81
+ /*
82
+ * Sys::Host.host_id
83
+ *
84
+ * Returns the host id of the current machine.
85
+ */
86
+ static VALUE host_host_id(){
87
+ return ULL2NUM(gethostid());
88
+ }
89
+ #endif
90
+
91
+ /*
92
+ * Sys::Host.info
93
+ * Sys::Host.info{ |i| ... }
94
+ *
95
+ * Yields a HostInfo struct containing various bits of information about
96
+ * the local machine for each entry in the hosts table. In non-block form,
97
+ * returns an array of HostInfo structs.
98
+ *
99
+ * The Struct::HostInfo struct contains 5 fields:
100
+ *
101
+ * * name (String)
102
+ * * aliases (Array)
103
+ * * addr_type (Integer)
104
+ * * length (Integer)
105
+ * * addr_list (Array)
106
+ */
107
+ static VALUE host_info(VALUE klass){
108
+ struct hostent* host;
109
+ char buf[INET_ADDRSTRLEN];
110
+ VALUE v_hostinfo;
111
+ VALUE v_addr = rb_ary_new();
112
+ VALUE v_array = rb_ary_new();
113
+ VALUE v_aliases = rb_ary_new();
114
+
115
+ sethostent(0);
116
+
117
+ while((host = gethostent())){
118
+ while(*host->h_aliases){
119
+ rb_ary_push(v_aliases, rb_str_new2(*host->h_aliases));
120
+ *host->h_aliases++;
121
+ }
122
+
123
+ while(*host->h_addr_list){
124
+ inet_ntop(host->h_addrtype, *host->h_addr_list, buf, sizeof(buf));
125
+ rb_ary_push(v_addr, rb_str_new2(buf));
126
+ *host->h_addr_list++;
127
+ }
128
+
129
+ v_hostinfo = rb_struct_new(sHostInfo,
130
+ rb_str_new2(host->h_name),
131
+ v_aliases,
132
+ INT2FIX(host->h_addrtype),
133
+ INT2FIX(host->h_length),
134
+ v_addr
135
+ );
136
+
137
+ if(rb_block_given_p())
138
+ rb_yield(v_hostinfo);
139
+ else
140
+ rb_ary_push(v_array, v_hostinfo);
141
+
142
+ rb_ary_clear(v_aliases);
143
+ rb_ary_clear(v_addr);
144
+ }
145
+
146
+ endhostent();
147
+
148
+ if(rb_block_given_p())
149
+ return Qnil;
150
+
151
+ return v_array;
152
+ }
153
+
154
+ void Init_host()
155
+ {
156
+ VALUE mSys, cHost;
157
+
158
+ /* The Sys module serves as a toplevel namespace, nothing more. */
159
+ mSys = rb_define_module("Sys");
160
+
161
+ /* The Host class encapsulates information about your machine, such as
162
+ * the host name and IP address.
163
+ */
164
+ cHost = rb_define_class_under(mSys, "Host", rb_cObject);
165
+
166
+ /* This error is raised if any of the Host methods fail. */
167
+ cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);
168
+
169
+ /* 0.6.1: The version of this library. This is a string, not a number. */
170
+ rb_define_const(cHost,"VERSION",rb_str_new2(SYS_HOST_VERSION));
171
+
172
+ /* Class Methods */
173
+ rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
174
+ rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
175
+ rb_define_singleton_method(cHost, "info", host_info, 0);
176
+
177
+ #ifdef HAVE_GETHOSTID
178
+ rb_define_singleton_method(cHost, "host_id", host_host_id, 0);
179
+ #endif
180
+
181
+ /* Structs */
182
+ sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
183
+ "length", "addr_list", 0
184
+ );
185
+ }
186
+
187
+ #ifdef __cplusplus
188
+ }
189
+ #endif
data/ext/linux/linux.c ADDED
@@ -0,0 +1,228 @@
1
+ /******************************************************************************
2
+ * linux.c (host.c) - Linux specific version for sys-host
3
+ *
4
+ * Author: Daniel Berger
5
+ *****************************************************************************/
6
+ #include <ruby.h>
7
+ #include <constants.h>
8
+ #include <unistd.h>
9
+ #include <netdb.h>
10
+ #include <arpa/inet.h>
11
+
12
+ #ifdef __cplusplus
13
+ extern "C"
14
+ {
15
+ #endif
16
+
17
+ VALUE cHostError, sHostInfo;
18
+
19
+ /* Returns the hostname of the current host. This may or not return the
20
+ * FQDN, depending on your system.
21
+ */
22
+ static VALUE host_hostname()
23
+ {
24
+ char host_name[MAXHOSTNAMELEN];
25
+ int rv;
26
+ rv = gethostname(host_name, sizeof(host_name));
27
+
28
+ /* If you get this exception, you've got big problems */
29
+ if(rv != 0)
30
+ rb_raise(cHostError, "gethostname() call failed");
31
+
32
+ return rb_str_new2(host_name);
33
+ }
34
+
35
+ /* Returns a list of IP addresses for the current host (yes, it is
36
+ * possible to have more than one).
37
+ */
38
+ static VALUE host_ip_addr()
39
+ {
40
+ char host_name[MAXHOSTNAMELEN];
41
+ char str[INET_ADDRSTRLEN];
42
+ char **pptr;
43
+ struct hostent* hp;
44
+ int rv;
45
+ VALUE rbResults = rb_ary_new();
46
+ #ifndef HAVE_INET_NTOP
47
+ struct in_addr ipa;
48
+ int n;
49
+ #endif
50
+
51
+ rv = gethostname(host_name, sizeof(host_name));
52
+
53
+ if(rv != 0)
54
+ rb_raise(cHostError, "gethostname() call failed");
55
+
56
+ #ifdef HAVE_GETHOSTBYNAME_R
57
+ {
58
+ struct hostent hpbuf;
59
+ char strbuf[HOSTENT_BUF];
60
+ int err;
61
+ size_t no_op = HOSTENT_BUF;
62
+ gethostbyname_r(host_name, &hpbuf, strbuf, no_op, &hp, &err);
63
+ }
64
+ #else
65
+ hp = gethostbyname(host_name);
66
+ #endif
67
+
68
+ if(!hp)
69
+ rb_raise(cHostError,"gethostbyname() call failed");
70
+
71
+ pptr = hp->h_addr_list;
72
+
73
+ #ifdef HAVE_INET_NTOP
74
+ for( ; *pptr != NULL; pptr++){
75
+ rb_ary_push(rbResults,
76
+ rb_str_new2(inet_ntop(hp->h_addrtype, *pptr, str, sizeof(str))));
77
+ }
78
+ #else
79
+ for(n = 0; hp->h_addr_list[n] != NULL; n++){
80
+ memcpy(&ipa.s_addr, hp->h_addr_list[n], hp->h_length);
81
+ rb_ary_push(rbResults, rb_str_new2(inet_ntoa(ipa)));
82
+ }
83
+ #endif
84
+
85
+ return rbResults;
86
+ }
87
+
88
+ /* :call-seq:
89
+ * Sys::Host.info
90
+ * Sys::Host.info{ |i| ... }
91
+ *
92
+ * Yields a HostInfo struct containing various bits of information about
93
+ * the local machine for each entry in the hosts table. In non-block form,
94
+ * returns an array of HostInfo structs.
95
+ *
96
+ * The Struct::HostInfo struct contains 5 fields:
97
+ *
98
+ * * name (String)
99
+ * * aliases (Array)
100
+ * * addr_type (Integer)
101
+ * * length (Integer)
102
+ * * addr_list (Array)
103
+ */
104
+ static VALUE host_info(VALUE klass){
105
+ VALUE v_hostinfo = Qnil;
106
+ VALUE v_addr = rb_ary_new();
107
+ VALUE v_array = rb_ary_new();
108
+ VALUE v_aliases = rb_ary_new();
109
+
110
+ sethostent(0);
111
+
112
+ #ifdef HAVE_GETHOSTENT_R
113
+ struct hostent host;
114
+ struct hostent* result;
115
+ char buf[HOSTENT_BUF];
116
+ int err;
117
+
118
+ while(!gethostent_r(&host, buf, sizeof(buf), &result, &err)){
119
+ while(*result->h_aliases){
120
+ rb_ary_push(v_aliases, rb_str_new2(*result->h_aliases));
121
+ *result->h_aliases++;
122
+ }
123
+
124
+ while(*result->h_addr_list){
125
+ inet_ntop(result->h_addrtype, *result->h_addr_list, buf, sizeof(buf));
126
+ rb_ary_push(v_addr, rb_str_new2(buf));
127
+ *result->h_addr_list++;
128
+ }
129
+
130
+ /* We need to dup the arrays because Ruby is holding onto the same
131
+ * reference.
132
+ */
133
+ v_hostinfo = rb_struct_new(sHostInfo,
134
+ rb_str_new2(result->h_name),
135
+ rb_ary_dup(v_aliases),
136
+ INT2FIX(result->h_addrtype),
137
+ INT2FIX(result->h_length),
138
+ rb_ary_dup(v_addr)
139
+ );
140
+ #else
141
+ struct hostent* host;
142
+ char buf[INET_ADDRSTRLEN];
143
+
144
+ while((host = gethostent())){
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
+ rb_ary_dup(v_aliases),
159
+ INT2FIX(host->h_addrtype),
160
+ INT2FIX(host->h_length),
161
+ rb_ary_dup(v_addr)
162
+ );
163
+ #endif
164
+
165
+ if(rb_block_given_p())
166
+ rb_yield(v_hostinfo);
167
+ else
168
+ rb_ary_push(v_array, v_hostinfo);
169
+
170
+ rb_ary_clear(v_aliases);
171
+ rb_ary_clear(v_addr);
172
+ }
173
+
174
+ endhostent();
175
+
176
+ if(rb_block_given_p())
177
+ return Qnil;
178
+
179
+ return v_array;
180
+ }
181
+
182
+ #ifdef HAVE_GETHOSTID
183
+ /*
184
+ * Sys::Host.host_id
185
+ *
186
+ * Returns the host id of the current machine.
187
+ */
188
+ static VALUE host_host_id(){
189
+ return ULL2NUM(gethostid());
190
+ }
191
+ #endif
192
+
193
+ void Init_host()
194
+ {
195
+ VALUE mSys, cHost;
196
+
197
+ /* The Sys module serves as a toplevel namespace, nothing more. */
198
+ mSys = rb_define_module("Sys");
199
+
200
+ /* The Host class encapsulates information about your machine, such as
201
+ * the host name and IP address.
202
+ */
203
+ cHost = rb_define_class_under(mSys, "Host", rb_cObject);
204
+
205
+ /* This error is raised if any of the Host methods fail. */
206
+ cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);
207
+
208
+ /* 0.6.1: The version of this library. This is a string, not a number. */
209
+ rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
210
+
211
+ /* Structs */
212
+ sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
213
+ "length", "addr_list", NULL
214
+ );
215
+
216
+ /* Class Methods */
217
+ rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
218
+ rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
219
+ rb_define_singleton_method(cHost, "info", host_info, 0);
220
+
221
+ #ifdef HAVE_GETHOSTID
222
+ rb_define_singleton_method(cHost, "host_id", host_host_id, 0);
223
+ #endif
224
+ }
225
+
226
+ #ifdef __cplusplus
227
+ }
228
+ #endif
data/ext/sunos/sunos.c ADDED
@@ -0,0 +1,242 @@
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, MAXHOSTNAMELEN) != 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.host_id
56
+ *
57
+ * Returns the host id of the current machine.
58
+ */
59
+ static VALUE host_host_id(){
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, MAXHOSTNAMELEN) != 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, HOSTENT_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(
101
+ inet_ntop(hp->h_addrtype, *hp->h_addr_list, str, INET_ADDRSTRLEN)
102
+ )
103
+ );
104
+ *hp->h_addr_list++;
105
+ }
106
+ #else
107
+ int n;
108
+ for(n = 0; hp->h_addr_list[n] != NULL; n++){
109
+ memcpy(&ipa.s_addr, hp->h_addr_list[n], hp->h_length);
110
+ rb_ary_push(v_addr, rb_str_new2(inet_ntoa(ipa)));
111
+ }
112
+ #endif
113
+
114
+ return v_addr;
115
+ }
116
+
117
+ /*
118
+ * Sys::Host.info
119
+ * Sys::Host.info{ |i| ... }
120
+ *
121
+ * Yields a HostInfo struct containing various bits of information about
122
+ * the local machine for each entry in the hosts table. In non-block form,
123
+ * returns an array of HostInfo structs.
124
+ *
125
+ * The Struct::HostInfo struct contains 5 fields:
126
+ *
127
+ * * name (String)
128
+ * * aliases (Array)
129
+ * * addr_type (Integer)
130
+ * * length (Integer)
131
+ * * addr_list (Array)
132
+ */
133
+ static VALUE host_info(VALUE klass){
134
+ VALUE v_hostinfo;
135
+ VALUE v_addr = rb_ary_new();
136
+ VALUE v_array = rb_ary_new();
137
+ VALUE v_aliases = rb_ary_new();
138
+
139
+ sethostent(0);
140
+
141
+ #ifdef HAVE_GETHOSTENT_R
142
+ struct hostent host;
143
+ char buf[MAXBUF];
144
+ int err;
145
+
146
+ while(gethostent_r(&host, buf, MAXBUF, &err)){
147
+ while(*host.h_aliases){
148
+ rb_ary_push(v_aliases, rb_str_new2(*host.h_aliases));
149
+ *host.h_aliases++;
150
+ }
151
+
152
+ while(*host.h_addr_list){
153
+ inet_ntop(host.h_addrtype, *host.h_addr_list, buf, MAXBUF);
154
+ rb_ary_push(v_addr, rb_str_new2(buf));
155
+ *host.h_addr_list++;
156
+ }
157
+
158
+ v_hostinfo = rb_struct_new(sHostInfo,
159
+ rb_str_new2(host.h_name),
160
+ v_aliases,
161
+ INT2FIX(host.h_addrtype),
162
+ INT2FIX(host.h_length),
163
+ v_addr
164
+ );
165
+ #else
166
+ struct hostent* host;
167
+ char buf[INET_ADDRSTRLEN];
168
+
169
+ while((host = gethostent())){
170
+ while(*host->h_aliases){
171
+ rb_ary_push(v_aliases, rb_str_new2(*host->h_aliases));
172
+ *host->h_aliases++;
173
+ }
174
+
175
+ while(*host->h_addr_list){
176
+ inet_ntop(host->h_addrtype, *host->h_addr_list, buf, INET_ADDRSTRLEN);
177
+ rb_ary_push(v_addr, rb_str_new2(buf));
178
+ *host->h_addr_list++;
179
+ }
180
+
181
+ v_hostinfo = rb_struct_new(sHostInfo,
182
+ rb_str_new2(host->h_name),
183
+ v_aliases,
184
+ INT2FIX(host->h_addrtype),
185
+ INT2FIX(host->h_length),
186
+ v_addr
187
+ );
188
+ #endif
189
+
190
+ if(rb_block_given_p())
191
+ rb_yield(v_hostinfo);
192
+ else
193
+ rb_ary_push(v_array, v_hostinfo);
194
+
195
+ rb_ary_clear(v_aliases);
196
+ rb_ary_clear(v_addr);
197
+ }
198
+
199
+ endhostent();
200
+
201
+ if(rb_block_given_p())
202
+ return Qnil;
203
+
204
+ return v_array;
205
+ }
206
+
207
+ void Init_host()
208
+ {
209
+ VALUE mSys, cHost;
210
+
211
+ /* The Sys module serves as a toplevel namespace, nothing more. */
212
+ mSys = rb_define_module("Sys");
213
+
214
+ /* The Host class encapsulates information about your machine, such as
215
+ * the host name and IP address.
216
+ */
217
+ cHost = rb_define_class_under(mSys, "Host", rb_cObject);
218
+
219
+ /* This error is raised if any of the Host methods fail. */
220
+ cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);
221
+
222
+ /* 0.6.1: The version of this library. This is a string, not a number. */
223
+ rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
224
+
225
+ /* Structs */
226
+ sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
227
+ "length", "addr_list", 0
228
+ );
229
+
230
+ /* Class Methods */
231
+ rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
232
+ rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
233
+ rb_define_singleton_method(cHost, "info", host_info, 0);
234
+
235
+ #ifdef HAVE_GETHOSTID
236
+ rb_define_singleton_method(cHost, "host_id", host_host_id, 0);
237
+ #endif
238
+ }
239
+
240
+ #ifdef __cplusplus
241
+ }
242
+ #endif
@@ -0,0 +1,95 @@
1
+ ###########################################################################
2
+ # test_sys_host.rb
3
+ #
4
+ # Test suite for sys-host, all platforms. You should run these tests via
5
+ # the 'rake test' task.
6
+ ###########################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'sys/host'
11
+ require 'test/unit'
12
+ require 'rbconfig'
13
+ include Config
14
+ include Sys
15
+
16
+ class TC_Sys_Host < Test::Unit::TestCase
17
+ def self.startup
18
+ if CONFIG['host_os'] =~ /mswin|solaris|linux|bsd|mach|darwin|osx/i
19
+ @@info_supported = true
20
+ else
21
+ @@info_supported = false
22
+ end
23
+
24
+ @@windows = CONFIG['host_os'].match('mswin')
25
+ end
26
+
27
+ def test_version
28
+ assert_equal('0.6.1', Host::VERSION)
29
+ end
30
+
31
+ def test_hostname_basic
32
+ assert_respond_to(Host, :hostname)
33
+ assert_nothing_raised{ Host.hostname }
34
+ end
35
+
36
+ def test_hostname
37
+ assert_kind_of(String, Host.hostname)
38
+ assert_equal(`hostname`.chomp, Host.hostname) # sanity check
39
+ end
40
+
41
+ def test_hostname_expected_errors
42
+ assert_raise(ArgumentError){ Host.hostname(true) }
43
+ end
44
+
45
+ def test_ip_addr_basic
46
+ assert_respond_to(Host, :ip_addr)
47
+ assert_nothing_raised{ Host.ip_addr }
48
+ end
49
+
50
+ def test_ip_addr
51
+ assert_kind_of(Array, Host.ip_addr)
52
+ assert_kind_of(String, Host.ip_addr.first)
53
+ end
54
+
55
+ def test_ip_addr_expected_errors
56
+ assert_raise(ArgumentError){ Host.ip_addr(true) }
57
+ end
58
+
59
+ def test_host_id_basic
60
+ assert_respond_to(Host, :host_id)
61
+ assert_nothing_raised{ Host.host_id }
62
+ end
63
+
64
+ def test_host_id
65
+ type = @@windows ? String : Integer
66
+ assert_kind_of(type, Host.host_id)
67
+ end
68
+
69
+ def test_host_id_expected_errors
70
+ assert_raise(ArgumentError){ Host.host_id(true) }
71
+ end
72
+
73
+ def test_info_basic
74
+ omit_unless(@@info_supported, 'info test skipped on this platform')
75
+ assert_respond_to(Host, :info)
76
+ assert_nothing_raised{ Host.info }
77
+ end
78
+
79
+ def test_info
80
+ omit_unless(@@info_supported, 'info test skipped on this platform')
81
+ assert_kind_of(Array, Host.info)
82
+ assert_kind_of(Struct::HostInfo, Host.info.first)
83
+ assert_nil(Host.info{ })
84
+ end
85
+
86
+ def test_info_high_iteration
87
+ omit_unless(@@info_supported, 'info test skipped on this platform')
88
+ assert_nothing_raised{ 100.times{ Host.info } }
89
+ end
90
+
91
+ def self.shutdown
92
+ @@info_supported = nil
93
+ @@windows = nil
94
+ end
95
+ end
metadata CHANGED
@@ -1,56 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: sys-host
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.6.0
7
- date: 2007-06-08 00:00:00 -06:00
8
- summary: Provides hostname and ip address info for a given host
9
- require_paths:
10
- - lib
11
- email: djberg96@gmail.com
12
- homepage: http://www.rubyforge.org/projects/sysutils
13
- rubyforge_project: sysutils
14
- description: Provides hostname and ip address info for a given host
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.8.0
24
- version:
4
+ version: 0.6.1
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Daniel J. Berger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: test-unit
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.0.0
24
+ version:
25
+ description: Provides hostname and ip address info for a given host
26
+ email: djberg96@gmail.com
27
+ executables: []
28
+
29
+ extensions:
30
+ - ext/extconf.rb
31
+ extra_rdoc_files:
32
+ - CHANGES
33
+ - README
34
+ - MANIFEST
35
+ - ext/bsd/bsd.c
36
+ - ext/generic/generic.c
37
+ - ext/linux/linux.c
38
+ - ext/sunos/sunos.c
31
39
  files:
32
40
  - doc/host.txt
33
- - test/tc_host.rb
41
+ - test/test_sys_host.rb
34
42
  - CHANGES
35
43
  - MANIFEST
36
- - Rakefile
37
44
  - README
38
- - ext/extconf.rb
39
- - ext/constants.h
45
+ - Rakefile
40
46
  - ext/bsd/bsd.c
41
- test_files:
42
- - test/tc_host.rb
47
+ - ext/generic/generic.c
48
+ - ext/linux/linux.c
49
+ - ext/sunos/sunos.c
50
+ - ext/constants.h
51
+ has_rdoc: true
52
+ homepage: http://www.rubyforge.org/projects/sysutils
53
+ post_install_message:
43
54
  rdoc_options: []
44
55
 
45
- extra_rdoc_files:
46
- - CHANGES
47
- - README
48
- - MANIFEST
49
- executables: []
50
-
51
- extensions:
52
- - ext/extconf.rb
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.8.0
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
53
70
  requirements: []
54
71
 
55
- dependencies: []
56
-
72
+ rubyforge_project: sysutils
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: Provides hostname and ip address info for a given host
77
+ test_files:
78
+ - test/test_sys_host.rb
data/test/tc_host.rb DELETED
@@ -1,46 +0,0 @@
1
- #################################################
2
- # tc_host.rb
3
- #
4
- # Test suite for sys-host, all platforms.
5
- #################################################
6
- require 'sys/host'
7
- require 'test/unit'
8
- include Sys
9
-
10
- class TC_Host < Test::Unit::TestCase
11
- def test_version
12
- assert_equal('0.6.0', Host::VERSION)
13
- end
14
-
15
- def test_hostname
16
- assert_respond_to(Host, :hostname)
17
- assert_nothing_raised{ Host.hostname }
18
- assert_kind_of(String, Host.hostname)
19
- assert_equal(`hostname`.chomp, Host.hostname) # sanity check
20
- end
21
-
22
- def test_ip_addr
23
- assert_respond_to(Host, :ip_addr)
24
- assert_nothing_raised{ Host.ip_addr }
25
- assert_kind_of(Array, Host.ip_addr)
26
- end
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
-
34
- def test_info
35
- if RUBY_PLATFORM =~ /mswin|solaris|linux|bsd|mach|darwin|osx/i
36
- assert_respond_to(Host, :info)
37
- assert_nothing_raised{ Host.info }
38
- assert_kind_of(Array, Host.info)
39
- assert_kind_of(Struct::HostInfo, Host.info.first)
40
- assert_nil(Host.info{ })
41
- assert_nothing_raised{ 100.times{ Host.info } }
42
- else
43
- puts "The 'test_info' test was skipped on this platform"
44
- end
45
- end
46
- end