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.
@@ -1,14 +0,0 @@
1
- /* constants.h - one version to bind them all... */
2
- #define SYS_HOST_VERSION "0.6.1"
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
@@ -1,35 +0,0 @@
1
- require 'mkmf'
2
- require 'fileutils'
3
-
4
- dir_config('host')
5
-
6
- have_func('gethostid')
7
- have_func('inet_ntop')
8
- Dir.mkdir('sys') unless File.exists?('sys')
9
-
10
- case RUBY_PLATFORM
11
- when /bsd|darwin|powerpc|mach/i
12
- have_func('getipnodebyname')
13
- FileUtils.cp('bsd/bsd.c', 'sys/host.c')
14
- when /sunos|solaris/i
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')
20
- when /linux/i
21
- have_func('gethostbyname_r', 'netdb.h')
22
- have_func('gethostent_r', 'netdb.h')
23
- FileUtils.cp('linux/linux.c', 'sys/host.c')
24
- when /win32|windows|dos|mingw|cygwin/i
25
- STDERR.puts 'Run the "rake install" task to install on MS Windows'
26
- else
27
- FileUtils.cp('generic/generic.c', 'sys/host.c')
28
- end
29
-
30
- # Already grabbed this from netdb.h on Solaris
31
- unless RUBY_PLATFORM =~ /sunos|solaris/i
32
- have_func('gethostent_r')
33
- end
34
-
35
- create_makefile('sys/host', 'sys')
@@ -1,189 +0,0 @@
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
@@ -1,228 +0,0 @@
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