network_interface 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,185 @@
1
+ #ifndef WIN32
2
+
3
+ # include <sys/types.h>
4
+ # include <sys/socket.h>
5
+ # include <net/if.h>
6
+ # include <netdb.h>
7
+
8
+ # if HAVE_SOCKET_IOCTLS
9
+ # include <sys/ioctl.h>
10
+ # include <netinet/in.h>
11
+ # include <arpa/inet.h>
12
+ #if defined(__sun)
13
+ #include <unistd.h>
14
+ #include <stropts.h>
15
+ #include <sys/sockio.h>
16
+ #endif
17
+ # endif /* HAVE_SOCKET_IOCTLS */
18
+
19
+ /* For logical interfaces support we convert all names to same name prefixed with l */
20
+ #if HAVE_SIOCGLIFNUM
21
+ #define CNAME(x) l##x
22
+ #else
23
+ #define CNAME(x) x
24
+ #endif
25
+
26
+ #if HAVE_NET_IF_DL_H
27
+ # include <net/if_dl.h>
28
+ #endif
29
+
30
+ /* For Linux, include all the sockaddr
31
+ definitions we can lay our hands on. */
32
+ #if !HAVE_SOCKADDR_SA_LEN
33
+ # if HAVE_NETASH_ASH_H
34
+ # include <netash/ash.h>
35
+ # endif
36
+ # if HAVE_NETATALK_AT_H
37
+ # include <netatalk/at.h>
38
+ # endif
39
+ # if HAVE_NETAX25_AX25_H
40
+ # include <netax25/ax25.h>
41
+ # endif
42
+ # if HAVE_NETECONET_EC_H
43
+ # include <neteconet/ec.h>
44
+ # endif
45
+ # if HAVE_NETIPX_IPX_H
46
+ # include <netipx/ipx.h>
47
+ # endif
48
+ # if HAVE_NETPACKET_PACKET_H
49
+ # include <netpacket/packet.h>
50
+ # endif
51
+ # if HAVE_NETROSE_ROSE_H
52
+ # include <netrose/rose.h>
53
+ # endif
54
+ # if HAVE_LINUX_IRDA_H
55
+ # include <linux/irda.h>
56
+ # endif
57
+ # if HAVE_LINUX_ATM_H
58
+ # include <linux/atm.h>
59
+ # endif
60
+ # if HAVE_LINUX_LLC_H
61
+ # include <linux/llc.h>
62
+ # endif
63
+ # if HAVE_LINUX_TIPC_H
64
+ # include <linux/tipc.h>
65
+ # endif
66
+ # if HAVE_LINUX_DN_H
67
+ # include <linux/dn.h>
68
+ # endif
69
+
70
+ /* Map address families to sizes of sockaddr structs */
71
+ static int af_to_len(int af)
72
+ {
73
+ switch (af)
74
+ {
75
+ case AF_INET: return sizeof (struct sockaddr_in);
76
+ #if defined(AF_INET6) && HAVE_SOCKADDR_IN6
77
+ case AF_INET6: return sizeof (struct sockaddr_in6);
78
+ #endif
79
+ #if defined(AF_AX25) && HAVE_SOCKADDR_AX25
80
+ # if defined(AF_NETROM)
81
+ case AF_NETROM: /* I'm assuming this is carried over x25 */
82
+ # endif
83
+ case AF_AX25: return sizeof (struct sockaddr_ax25);
84
+ #endif
85
+ #if defined(AF_IPX) && HAVE_SOCKADDR_IPX
86
+ case AF_IPX: return sizeof (struct sockaddr_ipx);
87
+ #endif
88
+ #if defined(AF_APPLETALK) && HAVE_SOCKADDR_AT
89
+ case AF_APPLETALK: return sizeof (struct sockaddr_at);
90
+ #endif
91
+ #if defined(AF_ATMPVC) && HAVE_SOCKADDR_ATMPVC
92
+ case AF_ATMPVC: return sizeof (struct sockaddr_atmpvc);
93
+ #endif
94
+ #if defined(AF_ATMSVC) && HAVE_SOCKADDR_ATMSVC
95
+ case AF_ATMSVC: return sizeof (struct sockaddr_atmsvc);
96
+ #endif
97
+ #if defined(AF_X25) && HAVE_SOCKADDR_X25
98
+ case AF_X25: return sizeof (struct sockaddr_x25);
99
+ #endif
100
+ #if defined(AF_ROSE) && HAVE_SOCKADDR_ROSE
101
+ case AF_ROSE: return sizeof (struct sockaddr_rose);
102
+ #endif
103
+ #if defined(AF_DECnet) && HAVE_SOCKADDR_DN
104
+ case AF_DECnet: return sizeof (struct sockaddr_dn);
105
+ #endif
106
+ #if defined(AF_PACKET) && HAVE_SOCKADDR_LL
107
+ case AF_PACKET: return sizeof (struct sockaddr_ll);
108
+ #endif
109
+ #if defined(AF_ASH) && HAVE_SOCKADDR_ASH
110
+ case AF_ASH: return sizeof (struct sockaddr_ash);
111
+ #endif
112
+ #if defined(AF_ECONET) && HAVE_SOCKADDR_EC
113
+ case AF_ECONET: return sizeof (struct sockaddr_ec);
114
+ #endif
115
+ #if defined(AF_IRDA) && HAVE_SOCKADDR_IRDA
116
+ case AF_IRDA: return sizeof (struct sockaddr_irda);
117
+ #endif
118
+ }
119
+ return sizeof (struct sockaddr);
120
+ }
121
+
122
+ #define SA_LEN(sa) af_to_len(sa->sa_family)
123
+ #if HAVE_SIOCGLIFNUM
124
+ #define SS_LEN(sa) af_to_len(sa->ss_family)
125
+ #else
126
+ #define SS_LEN(sa) SA_LEN(sa)
127
+ #endif
128
+ #else
129
+ //remove a warning on openbsd
130
+ #ifndef SA_LEN
131
+ #define SA_LEN(sa) sa->sa_len
132
+ #endif
133
+ #endif /* !HAVE_SOCKADDR_SA_LEN */
134
+
135
+ # if HAVE_GETIFADDRS
136
+ # include <ifaddrs.h>
137
+ # endif /* HAVE_GETIFADDRS */
138
+
139
+ # if !HAVE_GETIFADDRS && (!HAVE_SOCKET_IOCTLS || !HAVE_SIOCGIFCONF)
140
+ /* If the platform doesn't define, what we need, barf. If you're seeing this,
141
+ it means you need to write suitable code to retrieve interface information
142
+ on your system. */
143
+ # error You need to add code for your platform.
144
+ # endif
145
+
146
+ #else /* defined(WIN32) */
147
+
148
+ #include <windows.h>
149
+ #include <winsock2.h>
150
+ #include <iphlpapi.h>
151
+
152
+ #endif /* defined(WIN32) */
153
+
154
+ #ifndef TRUE
155
+ #define TRUE 1
156
+ #endif
157
+
158
+ #ifndef FALSE
159
+ #define FALSE 0
160
+ #endif
161
+
162
+ /* On systems without AF_LINK (Windows, for instance), define it anyway, but
163
+ give it a crazy value. On Linux, which has AF_PACKET but not AF_LINK,
164
+ define AF_LINK as the latter instead. */
165
+ #ifndef AF_LINK
166
+ # ifdef AF_PACKET
167
+ # define AF_LINK AF_PACKET
168
+ # else
169
+ # define AF_LINK -1000
170
+ # endif
171
+ # define HAVE_AF_LINK 0
172
+ #else
173
+ # define HAVE_AF_LINK 1
174
+ #endif
175
+
176
+
177
+ //Prototypes
178
+ //Get a list of the adresses for a network interface
179
+ VALUE rbnetifaces_s_addresses (VALUE class, VALUE dev);
180
+ //Get a list of the network interfaces
181
+ VALUE rbnetifaces_s_interfaces (VALUE self);
182
+ //This function is usefull only under windows to retrieve some additionnal interfaces informations
183
+ VALUE rbnetifaces_s_interface_info (VALUE self, VALUE dev);
184
+
185
+ void Init_network_interface_ext();
@@ -0,0 +1,6 @@
1
+ require 'network_interface/version'
2
+
3
+ module NetworkInterface
4
+ end
5
+
6
+ require 'network_interface_ext'
@@ -0,0 +1,3 @@
1
+ module NetworkInterface
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'network_interface/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "network_interface"
8
+ spec.version = NetworkInterface::VERSION
9
+ spec.authors = ["Brandon Turner", "Lance Sanchez"]
10
+ spec.email = ["lance.sanchez@rapid7.com", "brandon_turner@rapid7.com"]
11
+ spec.summary = "A cross platform gem to help get network interface information"
12
+ spec.description = %q{
13
+ This gem was originally added to the Metasploit Pcaprub gem. It's been spun
14
+ out into its own gem for anyone who might want to programmatically get
15
+ information on their network interfaces. }
16
+ spec.homepage = "https://github.com/rapid7/network_interface"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.extensions = ['ext/network_interface_ext/extconf.rb']
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rake-compiler", ">= 0"
28
+ spec.add_development_dependency "rspec"
29
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe NetworkInterface do
4
+
5
+ describe "#interfaces" do
6
+ it "should have the same interfaces as the system_interfaces" do
7
+ NetworkInterface.interfaces.should include(*system_interfaces_with_addresses.keys)
8
+ end
9
+ end
10
+
11
+ describe "#addresses" do
12
+ system_interfaces_with_addresses.each do |interface, hash|
13
+ describe "#{friendly_interface_names.key(interface)}" do
14
+ if hash.has_key?(:ipv4)
15
+ describe "ipv4" do
16
+ it "should have an ipv4 address" do
17
+ NetworkInterface.addresses(interface).should have_key NetworkInterface::AF_INET
18
+ end
19
+ it "should match the system interface of #{hash[:ipv4]}" do
20
+ NetworkInterface.addresses(interface)[NetworkInterface::AF_INET][0]["addr"].should == hash[:ipv4]
21
+ end
22
+ end
23
+ end
24
+ if hash.has_key?(:ipv6)
25
+ describe "ipv6" do
26
+ it "should have an ipv6 address" do
27
+ NetworkInterface.addresses(interface).should have_key NetworkInterface::AF_INET6
28
+ end
29
+ it "should match the system interface of #{hash[:ipv6]}" do
30
+ NetworkInterface.addresses(interface)[NetworkInterface::AF_INET6][0]["addr"].should == hash[:ipv6]
31
+ end
32
+ end
33
+ end
34
+ if hash.has_key?(:mac)
35
+ describe "MAC address" do
36
+ it "should have a MAC address" do
37
+ NetworkInterface.addresses(interface).should have_key NetworkInterface::AF_LINK
38
+ end
39
+ it "should match the system interface of #{hash[:mac]}" do
40
+ NetworkInterface.addresses(interface)[NetworkInterface::AF_LINK][0]["addr"].should == hash[:mac]
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,107 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+ require 'network_interface'
4
+ require 'rspec'
5
+ require 'rspec/autorun'
6
+
7
+ RSpec.configure do |config|
8
+ end
9
+
10
+ def friendly_interface_names
11
+ interfaces = NetworkInterface.interfaces
12
+ interface_names ||= begin
13
+ h = {}
14
+ interfaces.each do |interface|
15
+ info = NetworkInterface.interface_info(interface)
16
+ name = if info && info.has_key?('name')
17
+ info['name']
18
+ else
19
+ interface
20
+ end
21
+ h[name] = interface
22
+ end
23
+ h
24
+ end
25
+ interface_names
26
+ end
27
+
28
+ if RUBY_PLATFORM =~ /i386-mingw32/
29
+ def system_interfaces
30
+ ipconfig = `ipconfig`
31
+ ipconfig_array = ipconfig.split("\n").reject {|s| s.empty?}
32
+
33
+ getmac = `getmac -nh`
34
+ getmac_array = getmac.split("\n").reject {|s| s.empty?}
35
+ getmac_array.map!{|element| element.split(" ")}
36
+ getmac_hash = getmac_array.inject({}) do |hash, array|
37
+ hash.merge!({array[1][/\{(.*)\}/,1] => array[0].gsub("-",":").downcase})
38
+ end
39
+
40
+ interfaces = {}
41
+ @key = nil
42
+ ipconfig_array.each do |element|
43
+ if element.start_with? " "
44
+ case element
45
+ when /IPv6 Address.*: (.*)/
46
+ # interfaces[@key][:ipv6] = $1
47
+ when /IPv4 Address.*: (.*)/
48
+ interfaces[@key][:ipv4] = $1
49
+ interfaces[@key][:mac] = getmac_hash[@key[/\{(.*)\}/,1]]
50
+ end
51
+ elsif element[/Windows IP Configuration/]
52
+ elsif element[/Ethernet adapter (.*):/]
53
+ @key = friendly_interface_names[$1]
54
+ interfaces[@key] = {}
55
+ else
56
+ @key = element[/(.*):/,1]
57
+ interfaces[@key] = {}
58
+ end
59
+ end
60
+
61
+ interfaces
62
+ end
63
+
64
+ else
65
+ def system_interfaces
66
+ ifconfig = `/sbin/ifconfig`
67
+ ifconfig_array = ifconfig.split("\n")
68
+ ifconfig_array.map!{|element| element.split("\n")}
69
+ ifconfig_array.flatten!
70
+ interfaces = {}
71
+ @key = nil
72
+ ifconfig_array.each do |element|
73
+ if element.start_with?("\t") || element.start_with?(" ")
74
+ case element
75
+ when /ether ((\w{2}\:){5}(\w{2}))/
76
+ interfaces[@key][:mac] = $1
77
+ when /inet6 (.*) prefixlen/
78
+ interfaces[@key][:ipv6] = $1
79
+ when /inet ((\d{1,3}\.){3}\d{1,3}).*broadcast ((\d{1,3}\.){3}\d{1,3})/
80
+ interfaces[@key][:ipv4] = $1
81
+ interfaces[@key][:broadcast] = $3
82
+ when /addr:((\d{1,3}\.){3}\d{1,3})\s+Bcast:((\d{1,3}\.){3}\d{1,3})/i
83
+ interfaces[@key][:ipv4] = $1
84
+ interfaces[@key][:broadcast] = $3
85
+ end
86
+ else
87
+ @key = element.split(' ').first[/(\w*)/,1]
88
+ interfaces[@key] = {}
89
+ if element[/HWaddr ((\w{2}\:){5}(\w{2}))/]
90
+ interfaces[@key][:mac] = $1
91
+ end
92
+ end
93
+ end
94
+ interfaces
95
+ end
96
+
97
+ end
98
+
99
+ def system_interfaces_with_addresses
100
+ interfaces = {}
101
+ system_interfaces.each do |key, value|
102
+ if value.has_key? :ipv4
103
+ interfaces[key] = value
104
+ end
105
+ end
106
+ interfaces
107
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: network_interface
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brandon Turner
9
+ - Lance Sanchez
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-07-08 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake-compiler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: ! "\n This gem was originally added to the Metasploit Pcaprub gem.
80
+ It's been spun\n out into its own gem for anyone who might want to programmatically
81
+ get\n information on their network interfaces. "
82
+ email:
83
+ - lance.sanchez@rapid7.com
84
+ - brandon_turner@rapid7.com
85
+ executables: []
86
+ extensions:
87
+ - ext/network_interface_ext/extconf.rb
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - ext/network_interface_ext/extconf.rb
98
+ - ext/network_interface_ext/netifaces.c
99
+ - ext/network_interface_ext/netifaces.h
100
+ - lib/network_interface.rb
101
+ - lib/network_interface/version.rb
102
+ - network_interface.gemspec
103
+ - spec/netiface_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/rapid7/network_interface
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: -3620416272362916495
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: -3620416272362916495
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.25
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: A cross platform gem to help get network interface information
136
+ test_files:
137
+ - spec/netiface_spec.rb
138
+ - spec/spec_helper.rb