mac_addresses 0.0.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ece96d157476727f2e33bae51953a10c7ea54341fe22d725ff74a768bcb1f75
4
- data.tar.gz: faee1e34e0ddd21050254c5b14247e56c36a50a94f6594e39f42c181a12e0d8b
3
+ metadata.gz: 176bda0f3853e9afa4bb6ee460b2022ad4a0802ca8b9e98b9b373fe4e3ec63af
4
+ data.tar.gz: 432de641cc9582674578844391c15174e6ed7ef7be46e1494ab9c240f797ca31
5
5
  SHA512:
6
- metadata.gz: c2070d6473e8936b59e0599b65b21f04ea45625284f23cd9fdc16e426ee22c5106443ba7e48356e40317b54756ed9feba9e959b153f165fb947fd59f3988a2e5
7
- data.tar.gz: e58ee4bf62eddecec81c5a9cb2cf9cd4f9698a91e45a00eb024664e8cce707b0dff9e245e0792d909ca667a1167e1289d659763030f64c05b24b5bb5b103e1fa
6
+ metadata.gz: 1def1d0cebcd8e2fc833975b6fc06568f48fc653f0cc3247ab3ff29e62d199a54128bb575f4b3d3c6235ef1abc266dda47ed02a841dc32fcf4c3b32511fb8090
7
+ data.tar.gz: 78cf3ce777df188d59994e581bf937db87bccb13215764f2dc4b258659688e85147a681296c4135120a5adcce8067501611f8dd0880394a5c077edf88aaaadde
@@ -14,14 +14,20 @@
14
14
  # MacAddresses.list
15
15
 
16
16
  require 'socket'
17
+ require_relative 'mac_addresses/exceptions'
17
18
 
18
19
  module MacAddresses
19
20
 
20
21
  COMMANDS = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address'
21
- ADDRESS_REGEXP = %r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io
22
- LINK = Socket::PF_LINK if Socket.const_defined? :PF_LINK
23
- PACKET = Socket::PF_PACKET if Socket.const_defined? :PF_PACKET
24
- INTERFACE_PACKET_FAMILY = LINK || PACKET
22
+ ADDRESS_REGEXP = /([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/
23
+ NOT_VALID_MACS = ['00:00:00:00:00:00']
24
+ PROTOCOL_FAMILIES = 'PF_LINK', 'PF_PACKET'
25
+ fam = PROTOCOL_FAMILIES.find { |fam| Socket.const_defined?(fam) }
26
+ if fam
27
+ PROTOCOL_FAMILY = Socket.const_get fam
28
+ else
29
+ raise Exceptions::ProtocolFamilyNotFound, PROTOCOL_FAMILIES
30
+ end
25
31
 
26
32
  class << self
27
33
 
@@ -32,7 +38,6 @@ module MacAddresses
32
38
  ##
33
39
  # Discovers and returns the system's MAC addresses.
34
40
  # MacAddresses.fetch
35
-
36
41
  def fetch
37
42
  @addresses = from_getifaddrs
38
43
  return @addresses if @addresses
@@ -46,7 +51,9 @@ module MacAddresses
46
51
  break # break as soon as we successfully parse a command output
47
52
  end
48
53
 
49
- raise "None of #{ COMMANDS.join ', ' } succeeded returning MAC addresses info" unless success
54
+ unless success
55
+ raise Exceptions::NoneOfCommandsSuccessful, COMMANDS
56
+ end
50
57
 
51
58
  @addresses
52
59
  end
@@ -56,29 +63,18 @@ module MacAddresses
56
63
 
57
64
  interfaces = Socket.getifaddrs.select do |addr|
58
65
  addr.addr && # Some VPN ifcs don't have an addr - ignore them
59
- addr.addr.pfamily == INTERFACE_PACKET_FAMILY
66
+ addr.addr.pfamily == MacAddresses::PROTOCOL_FAMILY
60
67
  end
61
68
 
62
- macs = if Socket.const_defined? :PF_LINK
63
- interfaces.map do |addr|
64
- addr.addr.getnameinfo
65
- end.map do |m,|
66
- m if (m && !m.empty?)
67
- end.compact
68
- elsif Socket.const_defined? :PF_PACKET
69
- interfaces.map do |addr|
70
- addr.addr.inspect_sockaddr[/hwaddr=([\h:]+)/, 1]
71
- end.map do |mac_addr|
72
- mac_addr != '00:00:00:00:00:00'
73
- end
74
- end
75
- macs
69
+ interfaces.inject([]) do |found, iface|
70
+ macs = parse iface.addr.inspect_sockaddr
71
+ found << macs.select { |mac| mac != '00:00:00:00:00:00' }
72
+ end.flatten
76
73
  end
77
74
 
78
- def parse(output)
79
- candidates = output.scan(ADDRESS_REGEXP).map &:strip
80
- raise 'No mac address candidates' unless candidates.any?
81
- candidates
75
+ # Scans a string and returns an Array of found MACs
76
+ def parse(string)
77
+ string.scan(ADDRESS_REGEXP).flatten
82
78
  end
83
79
  end
84
80
  end
@@ -0,0 +1,18 @@
1
+ module MacAddresses
2
+ module Exceptions
3
+
4
+ class NoneOfCommandsSuccessful < StandardError
5
+ def initialize(commands, message = nil)
6
+ message = message || "None of #{ commands.join(', ') } succeeded returning MAC addresses info"
7
+ super(message)
8
+ end
9
+ end
10
+
11
+ class ProtocolFamilyNotFound < StandardError
12
+ def initialize(families, msg = nil)
13
+ message = message || "Socket protocol family not found: tried with #{families.join ', '}"
14
+ super(message)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module MacAddresses
2
- VERSION = '0.0.1'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mac_addresses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Verlato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
11
+ date: 2020-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -33,6 +33,7 @@ files:
33
33
  - LICENSE
34
34
  - README.md
35
35
  - lib/mac_addresses.rb
36
+ - lib/mac_addresses/exceptions.rb
36
37
  - lib/mac_addresses/version.rb
37
38
  homepage: https://github.com/FancyPixel/mac_addresses
38
39
  licenses: