unixinfo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2011-03-31
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ ext/unixinfo/extconf.rb
6
+ ext/unixinfo/unixinfo_native.c
7
+ ext/unixinfo/unixinfo_native.h
8
+ lib/unixinfo.rb
data/README.txt ADDED
@@ -0,0 +1,63 @@
1
+ = unixinfo
2
+
3
+ * http://github.com/erikh/unixinfo
4
+
5
+ == DESCRIPTION:
6
+
7
+ A small hodgepodge of neat little UNIX API things I like to have access to without shelling out.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * getloadavg returns a 3-element tuple with your 1, 5, and 15 minute load averages.
12
+ * gethostname returns your hostname.
13
+ * getifaddrs returns a data structure with the ipv4 and ipv6 addresses bound to each of your interfaces, including netmask and broadcast information.
14
+
15
+ == SYNOPSIS:
16
+
17
+ require 'unixinfo'
18
+
19
+ UnixInfo.getloadavg
20
+ UnixInfo.gethostname
21
+ UnixInfo.getifaddrs
22
+
23
+ == REQUIREMENTS:
24
+
25
+ Something recognizably unix-compatible, a compiler and macro preprocessor, ruby.
26
+
27
+ == INSTALL:
28
+
29
+ * gem install unixinfo
30
+
31
+ == DEVELOPERS:
32
+
33
+ After checking out the source, run:
34
+
35
+ $ rake newb
36
+
37
+ This task will install any missing dependencies, run the tests/specs,
38
+ and generate the RDoc.
39
+
40
+ == LICENSE:
41
+
42
+ (The MIT License)
43
+
44
+ Copyright (c) 2011 Erik Hollensbe
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining
47
+ a copy of this software and associated documentation files (the
48
+ 'Software'), to deal in the Software without restriction, including
49
+ without limitation the rights to use, copy, modify, merge, publish,
50
+ distribute, sublicense, and/or sell copies of the Software, and to
51
+ permit persons to whom the Software is furnished to do so, subject to
52
+ the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be
55
+ included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
58
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
60
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
61
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
62
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
63
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugins.delete :rubyforge
7
+ Hoe.plugin :git
8
+ Hoe.plugin :rcov
9
+
10
+ spec = Hoe.spec 'unixinfo' do
11
+ developer('Erik Hollensbe', 'erik@hollensbe.org')
12
+
13
+ self.rubyforge_name = nil
14
+
15
+ spec_extras[:extensions] = [ "ext/unixinfo/extconf.rb" ]
16
+ end
17
+
18
+ task :install => [:gem] do
19
+ sh "gem install pkg/#{spec.name}-#{spec.version}.gem"
20
+ end
21
+
22
+ # vim: syntax=ruby
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ require 'mkmf'
3
+
4
+ HEADERS = %w[
5
+ stdlib.h
6
+ unistd.h
7
+ sys/types.h
8
+ sys/socket.h
9
+ ifaddrs.h
10
+ netdb.h
11
+ ]
12
+
13
+ CALLS = %w[
14
+ getloadavg
15
+ gethostname
16
+ getifaddrs
17
+ getnameinfo
18
+ ]
19
+
20
+ all_calls = CALLS.all? { |x| have_library("c", x) }
21
+ all_headers = HEADERS.all? { |x| have_header(x) }
22
+
23
+ if all_calls and all_headers
24
+ #$CFLAGS << " -g "
25
+ if have_struct_member("struct sockaddr", "sa_len", "sys/socket.h")
26
+ $CFLAGS << " -DHAVE_SA_LEN "
27
+ end
28
+ dir_config("unixinfo_native")
29
+ create_makefile("unixinfo_native")
30
+ else
31
+ rm_f "Makefile"
32
+ end
33
+
@@ -0,0 +1,172 @@
1
+ #include "unixinfo_native.h"
2
+
3
+ void Init_unixinfo_native(void) {
4
+ VALUE si;
5
+ si = rb_define_module("UnixInfo");
6
+ rb_define_singleton_method(si, "getloadavg", si_getloadavg, 0);
7
+ rb_define_singleton_method(si, "gethostname", si_gethostname, 0);
8
+ rb_define_singleton_method(si, "getifaddrs", si_getifaddrs, 0);
9
+ }
10
+
11
+ /*
12
+ * SysInfo.getloadavg()
13
+ *
14
+ * Returns 3 float values which correspond to the 1 minute, 5 minute, and 15
15
+ * minute load averages respectively.
16
+ *
17
+ * If for some reason the load average cannot be retreived, nil is returned.
18
+ *
19
+ */
20
+ VALUE si_getloadavg(VALUE self) {
21
+ VALUE ary;
22
+ double averages[3];
23
+ int i = 0;
24
+
25
+ ary = rb_ary_new();
26
+
27
+ if(getloadavg(averages, 3) == 3) {
28
+ for(; i < 3; i++) {
29
+ rb_ary_push(ary, rb_float_new(averages[i]));
30
+ }
31
+ } else {
32
+ return Qnil;
33
+ }
34
+
35
+ return ary;
36
+ }
37
+
38
+ #define HOSTNAME_MAX_LEN 1000
39
+
40
+ VALUE si_gethostname(VALUE self) {
41
+ char str[HOSTNAME_MAX_LEN];
42
+
43
+ if (!gethostname(str, HOSTNAME_MAX_LEN)){
44
+ return rb_str_new2(str);
45
+ } else {
46
+ return Qnil;
47
+ }
48
+ }
49
+
50
+ char * si_my_getnameinfo(struct sockaddr *addr) {
51
+ char *address;
52
+
53
+ address = malloc(NI_MAXHOST * sizeof(char));
54
+
55
+ // BSD uses sa_len, linux switches on the family type.
56
+ #ifdef HAVE_SA_LEN
57
+ getnameinfo(
58
+ addr,
59
+ addr->sa_len,
60
+ address,
61
+ NI_MAXHOST,
62
+ NULL, 0,
63
+ NI_NUMERICHOST
64
+ );
65
+ #else
66
+ getnameinfo(
67
+ addr,
68
+ (addr->sa_family == AF_INET) ?
69
+ sizeof(struct sockaddr_in) :
70
+ sizeof(struct sockaddr_in6),
71
+ address, NI_MAXHOST, NULL, 0, NI_NUMERICHOST
72
+ );
73
+ #endif
74
+
75
+ return address;
76
+ }
77
+
78
+ VALUE si_my_get_new_af_hash(struct sockaddr *addr, VALUE hash) {
79
+ VALUE key;
80
+ VALUE inner_hash;
81
+ VALUE array;
82
+
83
+ if (addr->sa_family == AF_INET) {
84
+ key = rb_str_new2("ipv4");
85
+ } else if (addr->sa_family == AF_INET6) {
86
+ key = rb_str_new2("ipv6");
87
+ }
88
+
89
+ array = rb_hash_aref(hash, key);
90
+
91
+ if (array == Qnil) {
92
+ array = rb_ary_new();
93
+ rb_hash_aset(hash, key, array);
94
+ }
95
+
96
+ inner_hash = rb_hash_new();
97
+
98
+ rb_ary_push(array, inner_hash);
99
+
100
+ return inner_hash;
101
+ }
102
+
103
+ void si_my_fill_address_hash(VALUE hash, struct ifaddrs *addr) {
104
+ char *address;
105
+
106
+ address = si_my_getnameinfo(addr->ifa_addr);
107
+
108
+ if (address) {
109
+ rb_hash_aset(hash, rb_str_new2("address"), rb_str_new2(address));
110
+ free(address);
111
+ }
112
+
113
+ if (addr->ifa_netmask) {
114
+ address = si_my_getnameinfo(addr->ifa_netmask);
115
+ if (address) {
116
+ rb_hash_aset(hash, rb_str_new2("netmask"), rb_str_new2(address));
117
+ free(address);
118
+ }
119
+ }
120
+
121
+ if (addr->ifa_dstaddr) {
122
+ address = si_my_getnameinfo(addr->ifa_dstaddr);
123
+ if (address) {
124
+ rb_hash_aset(hash, rb_str_new2("dst_address"), rb_str_new2(address));
125
+ rb_hash_aset(hash, rb_str_new2("broadcast"), rb_str_new2(address));
126
+ free(address);
127
+ }
128
+ }
129
+ }
130
+
131
+ VALUE si_getifaddrs(VALUE self) {
132
+ VALUE hash;
133
+ VALUE key;
134
+ VALUE inner_hash;
135
+ VALUE inner_inner_hash;
136
+
137
+ struct ifaddrs *ifap;
138
+ struct ifaddrs *cur;
139
+
140
+ struct sockaddr *tmp;
141
+
142
+ if (getifaddrs(&ifap)) {
143
+ return Qnil;
144
+ }
145
+
146
+ cur = ifap;
147
+ hash = rb_hash_new();
148
+
149
+ if (ifap == NULL)
150
+ return Qnil;
151
+
152
+ do {
153
+ key = rb_str_new2(cur->ifa_name);
154
+ inner_hash = rb_hash_aref(hash, key);
155
+
156
+ if (Qnil == inner_hash)
157
+ inner_hash = rb_hash_new();
158
+
159
+ tmp = cur->ifa_addr;
160
+
161
+ if (tmp && (tmp->sa_family == AF_INET || tmp->sa_family == AF_INET6)) {
162
+ inner_inner_hash = si_my_get_new_af_hash(tmp, inner_hash);
163
+ si_my_fill_address_hash(inner_inner_hash, cur);
164
+ }
165
+
166
+ rb_hash_aset(hash, key, inner_hash);
167
+ } while (cur = cur->ifa_next);
168
+
169
+ freeifaddrs(ifap);
170
+
171
+ return hash;
172
+ }
@@ -0,0 +1,16 @@
1
+ #include <stdlib.h>
2
+ #include <unistd.h>
3
+ #include <sys/types.h>
4
+ #include <sys/socket.h>
5
+ #include <ifaddrs.h>
6
+ #include <netdb.h>
7
+ #include <ruby.h>
8
+
9
+ void si_my_fill_address_hash(VALUE hash, struct ifaddrs *addr);
10
+ VALUE si_my_get_new_af_array(struct sockaddr *addr, VALUE hash);
11
+ char * si_my_getnameinfo(struct sockaddr *addr);
12
+ VALUE si_getloadavg(VALUE self);
13
+ VALUE si_gethostname(VALUE self);
14
+ VALUE si_getifaddrs(VALUE self);
15
+ void Init_unixinfo_native(void);
16
+
data/lib/unixinfo.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'unixinfo_native'
2
+ module UnixInfo
3
+ VERSION = "1.0.0"
4
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unixinfo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Erik Hollensbe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-31 00:00:00 Z
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hoe
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.9.1
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: A small hodgepodge of neat little UNIX API things I like to have access to without shelling out.
28
+ email:
29
+ - erik@hollensbe.org
30
+ executables: []
31
+
32
+ extensions:
33
+ - ext/unixinfo/extconf.rb
34
+ extra_rdoc_files:
35
+ - History.txt
36
+ - Manifest.txt
37
+ - README.txt
38
+ files:
39
+ - History.txt
40
+ - Manifest.txt
41
+ - README.txt
42
+ - Rakefile
43
+ - ext/unixinfo/extconf.rb
44
+ - ext/unixinfo/unixinfo_native.c
45
+ - ext/unixinfo/unixinfo_native.h
46
+ - lib/unixinfo.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/erikh/unixinfo
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.txt
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.6.2
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A small hodgepodge of neat little UNIX API things I like to have access to without shelling out.
76
+ test_files: []
77
+