sys-host 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. data/CHANGES +95 -0
  2. data/MANIFEST +19 -0
  3. data/README +14 -0
  4. data/doc/host.txt +88 -0
  5. data/ext/constants.h +14 -0
  6. data/ext/sunos.c +236 -0
  7. data/extconf.rb +48 -0
  8. data/test/tc_host.rb +63 -0
  9. metadata +54 -0
data/CHANGES ADDED
@@ -0,0 +1,95 @@
1
+ == 0.5.2 - 27-Jun-2006
2
+ * Added the Host.info method for Linux.
3
+
4
+ == 0.5.1 - 3-Mar-2006
5
+ * The Host.info method is now supported on Solaris.
6
+ * Moved project to RubyForge.
7
+ * Replaced INSTALL with README.
8
+ * Made documentation rdoc friendly.
9
+
10
+ == 0.5.0 - 3-Jul-2004
11
+ * Replaced Windows C extension with pure Ruby version, using WMI + OLE.
12
+ * Added an additional class method called 'info' that returns a HostInfo
13
+ struct. Currently on Windows only.
14
+ * Removed the block form of Host.ip_addr. It now simply returns an Array.
15
+ * Renamed and revamped test suite.
16
+ * Moved test.rb to doc/examples
17
+ * Removed the host.html file.
18
+ * Replaced previous exception classes with a single exception class,
19
+ HostError, and moved it under the Sys module namespace.
20
+
21
+ == 0.4.0 - 15-Jun-2003
22
+ * Added MS Windows support
23
+ * Modified extconf.rb file - generic test script now in its own file
24
+ (test/test.rb) and the dynamic rd2 stuff was removed, in addition to proper
25
+ handling for MS Windows
26
+ * rd2, txt and html docs now included by default
27
+ * Removed VERSION() class method (use the constant instead)
28
+ * Added a universal constants.h file to store VERSION info, etc, that
29
+ is common to all source files.
30
+ * Added LICENSE info
31
+
32
+ == 0.3.4 - 3-Apr-2003
33
+ * Fixed a header include issue for solaris systems that don't
34
+ define inet_ntop().
35
+
36
+ == 0.3.3 * 24-Mar-2003
37
+ * If inet_ntop() is not defined, now falls back to inet_ntoa().
38
+
39
+ == 0.3.2 - 14-Mar-2003
40
+ * Added a VERSION constant
41
+ * Modified extconf.rb script, including a bugfix for solaris
42
+ * Added a test suite (for those with testunit installed)
43
+ * Removed the sys-uname requirement
44
+ * Changelog and Manifest are now CHANGES and MANIFEST, respectively
45
+ * Minor internal layout changes
46
+ * rd2 documentation removed from C source files and put into 'doc'
47
+ directory
48
+
49
+ == 0.3.1 - 6-Jan-2003
50
+ * Fix to make code C89 compliant so that older compilers don't choke.
51
+ * Added a VERSION class method
52
+ * Added a copyright notice
53
+ * Moved README into doc/host.txt
54
+ * Minor doc changes
55
+ * Fixed extconf.rb file to include socket lib for Solaris. This was
56
+ in 0.2.0 and was mistakenly left out of 0.3.0.
57
+
58
+ == 0.3.0 - 28-Oct-2002
59
+ * Reentrant (thread-safe) support added for ip_addr() method (for Linux,
60
+ FreeBSD and Solaris currently).
61
+ * The file layout changed so that each platform has its own source file.
62
+ This was done because different platforms have different implementations
63
+ of gethostbyname_r(), if they have them at all. Some platforms (e.g.
64
+ FreeBSD) use a different function altogether.
65
+ * The file generic.c is used to create the source if the platform is not
66
+ currently supported, or if gethostbyname_r() isn't found.
67
+ * The FreeBSD implementation uses getipnodebyname() instead of
68
+ gethostbyname(), if possible. According to the man page, it's thread
69
+ safe.
70
+ * Modifications to the extconf.rb file based on the above changes.
71
+ * Added a default value of 16 for INET_ADDRSTRLEN.
72
+ * Added a README file.
73
+
74
+ == 0.2.0 - 1-Sep-2002
75
+ * Added HostException and HostAddressException classes. These are raised
76
+ in the event the gethostname or gethostbyname call fails, respectively.
77
+ * Removed some unnecessary (and warning-causing) free calls.
78
+ * Added an INSTALL file (installation instructions removed from core doc).
79
+
80
+ == 0.1.2 - 9-Aug-2002
81
+ * Got the BSD support working properly this time. Thanks to Sean
82
+ Chittenden for providing BSD access.
83
+
84
+ == 0.1.1 - 21-Jun-2002
85
+ * Fixed a bug that would cause the ip_addr to segfault on some
86
+ operating systems. Apparently ,trying to free memory on a
87
+ null pointer is a bad thing. Hey, Solaris didn't complain!
88
+ Thanks go to Han Holl for the spot.
89
+ * On *BSD systems the 'netinet/in.h' file is now included (via
90
+ the makefile). Thanks go to Magnus Stahre for the spot and
91
+ the fix.
92
+ * Changed the version number to be more "ruby compliant"
93
+
94
+ == 0.01 * 11-Jun-2002
95
+ * First release!
data/MANIFEST ADDED
@@ -0,0 +1,19 @@
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
data/README ADDED
@@ -0,0 +1,14 @@
1
+ = Prerequisites
2
+ Ruby 1.8.0 or later.
3
+ Ruby 1.8.2 or later preferred on Windows.
4
+
5
+ = 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
data/doc/host.txt ADDED
@@ -0,0 +1,88 @@
1
+ == Description
2
+ Provides hostname and ip address information for a given host.
3
+
4
+ == Synopsis
5
+ require 'sys/host'
6
+ include Sys
7
+
8
+ p Host.hostname
9
+ p Host.ip_addr
10
+
11
+ # Some platforms
12
+ Host.info{ |h|
13
+ p h
14
+ }
15
+
16
+ == Constants
17
+ VERSION
18
+ Returns the current version number of this package (as a string)
19
+
20
+ == Class Methods
21
+ Host.hostname
22
+ Returns the hostname of the current host. This may or not return
23
+ the FQDN, depending on your system.
24
+
25
+ Host.ip_addr
26
+ Returns a list of IP addresses for the current host (yes, it is
27
+ possible to have more than one).
28
+
29
+ Host.info(host=localhost)
30
+ Host.info(host=localhost){ |h| ... }
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
+ == Exception Classes
36
+ HostError < StandardError
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
39
+ the underlying gethostname() or gethostbyname() calls.
40
+
41
+ == Future Plans
42
+ Add support for IPV6. This will be difficult unless I have access to a
43
+ system on an IPV6 network. Any help on this front appreciated.
44
+
45
+ == Notes
46
+ The Windows version of this package uses OLE + WMI. The Host.info method
47
+ returns considerably more information than the *nix versions of this
48
+ package.
49
+
50
+ == Comments and Questions
51
+ Please direct all comments and/or questions to one of the Forums on the
52
+ project home page at http://www.rubyforge.org/projects/sysutils.
53
+
54
+ == Known Bugs
55
+ None that I'm aware of.
56
+
57
+ Please log any bugs on the project home page at
58
+ http://www.rubyforge.org/projects/sysutils
59
+
60
+ == Acknowledgements
61
+ Thanks go to Mike Hall, Guy Decoux, and Matz for their help with
62
+ C extensions in general.
63
+
64
+ Thanks also go to Richard Stevens for his excellent Unix programming
65
+ books.
66
+
67
+ == License
68
+ Ruby's
69
+
70
+ == Copyright
71
+ Copyright 2002-2006, Daniel J. Berger, djberg96 at gmail dot com
72
+
73
+ All Rights Reserved. This module is free software. It may be used,
74
+ redistributed and/or modified under the same terms as Ruby itself.
75
+
76
+ == Warranty
77
+ This package is provided "as is" and without any express or
78
+ implied warranties, including, without limitation, the implied
79
+ warranties of merchantability and fitness for a particular purpose.
80
+
81
+ == Author
82
+ Daniel J. Berger
83
+ djberg96 at gmail dot com
84
+ imperator on IRC (freenode)
85
+
86
+ == See Also
87
+ gethostbyname, gethostbyname_r, gethostname, hostname (Unix)
88
+ WMI, OLE (Windows)
data/ext/constants.h ADDED
@@ -0,0 +1,14 @@
1
+ /* constants.h - one version to bind them all... */
2
+ #define SYS_HOST_VERSION "0.5.2"
3
+
4
+ #ifndef MAXHOSTNAMELEN
5
+ #define MAXHOSTNAMELEN 256
6
+ #endif
7
+
8
+ #ifndef INET_ADDRSTRLEN
9
+ #define INET_ADDRSTRLEN 16
10
+ #endif
11
+
12
+ #ifndef HOSTENT_BUF
13
+ #define HOSTENT_BUF 8192
14
+ #endif
data/ext/sunos.c ADDED
@@ -0,0 +1,236 @@
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 ADDED
@@ -0,0 +1,48 @@
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")
data/test/tc_host.rb ADDED
@@ -0,0 +1,63 @@
1
+ #########################################
2
+ # tc_host.rb
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"
31
+ include Sys
32
+
33
+ class TC_Host < Test::Unit::TestCase
34
+ def test_version
35
+ assert_equal("0.5.2",Host::VERSION)
36
+ end
37
+
38
+ def test_hostname
39
+ assert_respond_to(Host, :hostname)
40
+ assert_nothing_raised{ Host.hostname }
41
+ assert_kind_of(String, Host.hostname)
42
+ assert_equal(`hostname`.chomp, Host.hostname) # sanity check
43
+ end
44
+
45
+ def test_ip_addr
46
+ assert_respond_to(Host, :ip_addr)
47
+ assert_nothing_raised{ Host.ip_addr }
48
+ assert_kind_of(Array, Host.ip_addr)
49
+ end
50
+
51
+ def test_info
52
+ if RUBY_PLATFORM =~ /mswin|solaris|linux/i
53
+ assert_respond_to(Host, :info)
54
+ assert_nothing_raised{ Host.info }
55
+ assert_kind_of(Array, Host.info)
56
+ assert_kind_of(Struct::HostInfo, Host.info.first)
57
+ assert_nil(Host.info{ })
58
+ assert_nothing_raised{ 1000.times{ Host.info } }
59
+ else
60
+ puts "The 'test_info' test was skipped on this platform"
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: sys-host
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.2
7
+ date: 2006-06-30 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:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - doc/host.txt
33
+ - test/tc_host.rb
34
+ - CHANGES
35
+ - MANIFEST
36
+ - README
37
+ - extconf.rb
38
+ - ext/constants.h
39
+ - ext/sunos.c
40
+ test_files:
41
+ - test/tc_host.rb
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files:
45
+ - CHANGES
46
+ - README
47
+ executables: []
48
+
49
+ extensions:
50
+ - extconf.rb
51
+ requirements: []
52
+
53
+ dependencies: []
54
+