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,242 +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, 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