mac_address_eui48 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+
2
+ # Copyright (C) 2015 Mathieu Cunche <mathieu.cunche@innsa-lyon.fr>
3
+
4
+ require "mac_address_eui48/version"
5
+ require "mac_address_eui48/mac_address"
6
+ require "mac_address_eui48/oui_resolver"
7
+
8
+
9
+ module MacAddressEui48
10
+ OUI_FILE="data/oui.txt"
11
+
12
+ # MAC address format is hexadecimal characters separated by ':'
13
+ # ex: "AA:BB:CC:DD:EE:FF" or "11:22:33:44:55:66
14
+
15
+ def MacAddressEui48::is_valid_mac(mac_addr)
16
+ return (mac_addr =~ /^(\h\h:){5}\h\h$/)
17
+ end
18
+
19
+ def MacAddressEui48::macaddr_to_int(a)
20
+ return a = a.delete(":").to_i(16)
21
+ end
22
+
23
+ def MacAddressEui48::int_to_macaddr(i)
24
+ a = i.to_s(16)
25
+ a = a.rjust(12,'0')
26
+ a = a.insert(10,':').insert(8,':').insert(6,':').insert(4,':').insert(2,':')
27
+ return a.upcase
28
+ end
29
+
30
+ def MacAddressEui48::oui_lookup(mac_addr)
31
+ oui = ""
32
+ mac_array=mac_addr.upcase.split(":")
33
+ prefix = mac_array[0] + "-" + mac_array[1] + "-" + mac_array[2]
34
+ oui_file = File.open(OUI_FILE)
35
+ oui_file.each_line do |line|
36
+ if (line =~ /.*#{prefix}.*(hex)/) then
37
+
38
+ oui = line.split("\t")[2].chomp
39
+ end
40
+ end
41
+ return oui
42
+ end
43
+
44
+
45
+ end
@@ -0,0 +1,64 @@
1
+ # Copyright (C) 2015 Mathieu Cunche <mathieu.cunche@innsa-lyon.fr>
2
+
3
+ module MacAddressEui48
4
+
5
+
6
+ class MacAddress
7
+ include Comparable
8
+ attr_reader :mac_str
9
+
10
+ def initialize(arg)
11
+ case arg
12
+ when MacAddress
13
+ @mac_str = arg.to_s
14
+ when Integer
15
+ @mac_str = MacAddressEui48::int_to_macaddr(arg)
16
+ when String
17
+ if (MacAddressEui48::is_valid_mac(arg)) then
18
+ @mac_str=arg
19
+ else
20
+ raise "Wrong format for string mac address #{arg}"
21
+ end
22
+ else
23
+ raise "Incompatible type for MacAddress Initialization: #{arg.class}"
24
+ end
25
+ end
26
+
27
+
28
+ def to_i
29
+ return MacAddressEui48::macaddr_to_int(@mac_str)
30
+ end
31
+
32
+ def <=>(other)
33
+ MacAddressEui48::macaddr_to_int(self.mac_str) <=> MacAddressEui48::macaddr_to_int(other.mac_str)
34
+ end
35
+
36
+ def to_s
37
+ @mac_str
38
+ end
39
+
40
+ def is_broadcast
41
+ self.mac_str == "FF:FF:FF:FF:FF:FF"
42
+ end
43
+
44
+ def is_unicast
45
+ return !is_multicast
46
+ end
47
+ def is_multicast
48
+
49
+ end
50
+ def is_glob_uniq
51
+ return !is_loc_admin
52
+ end
53
+ def is_loc_admin
54
+
55
+ end
56
+
57
+
58
+ def next
59
+ return MacAddress.new(MacAddressEui48::int_to_macaddr(((self.to_i) +1)%2**48))
60
+ end
61
+ alias_method :succ, :next
62
+
63
+ end
64
+ end
@@ -0,0 +1,52 @@
1
+ # Copyright (C) 2015 Mathieu Cunche <mathieu.cunche@innsa-lyon.fr>
2
+
3
+ module MacAddressEui48
4
+ class OuiResolver
5
+ def initialize()
6
+ @hash=Hash.new(nil)
7
+
8
+ oui_file = File.open(OUI_FILE)
9
+ oui_file.each_line do |line|
10
+ if (line =~ /\(hex\)/) then
11
+ prefix = line.split("\t")[0].split(" ")[0].gsub("-",":").chomp
12
+ oui = line.split("\t")[2].chomp
13
+ @hash[prefix]=oui
14
+ end
15
+
16
+ end
17
+ end
18
+ def oui_lookup(mac_addr)
19
+ return @hash[mac_addr[0..7].upcase]
20
+ end
21
+ def reverse_lookup(vendor)
22
+ return @hash.select { |key, val| val == vendor }.keys
23
+ end
24
+ def has_oui(mac)
25
+ return (oui_lookup(mac) != nil)
26
+ end
27
+
28
+ def random_mac
29
+ i = rand(2**48)
30
+ return MacAddressEui48::int_to_macaddr(i).upcase
31
+ end
32
+ def random_oui_mac
33
+ vendor = @hash.values.uniq[rand(@hash.values.uniq.size)]
34
+ return random_mac_vendor(vendor)
35
+ end
36
+
37
+ def random_mac_vendor(vendor)
38
+ ouis = reverse_lookup(vendor)
39
+ if (ouis.size <=0) then
40
+ raise "OUI not found #{vendor}"
41
+ end
42
+ oui = ouis[rand(ouis.size)]
43
+ i1 = MacAddressEui48::macaddr_to_int( oui + ":00:00:00")
44
+ i2 = rand(2**24)
45
+ i = i1 + i2
46
+ return MacAddressEui48::int_to_macaddr(i)
47
+ end
48
+
49
+ end
50
+
51
+
52
+ end
@@ -0,0 +1,5 @@
1
+ # Copyright (C) 2015 Mathieu Cunche <mathieu.cunche@innsa-lyon.fr>
2
+
3
+ module MacAddressEui48
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mac_address_eui48/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mac_address_eui48"
8
+ spec.version = MacAddressEui48::VERSION
9
+ spec.authors = ["Mathieu Cunche"]
10
+ spec.email = ["mathieu.cunche@insa-lyon.fr"]
11
+
12
+ spec.summary = %q{An implementation of class for MAC address (i.e. IEEE EUI48 identifiers).}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/cunchem/mac_address_eui48"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ #if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ #else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ #end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(tests|spec|features)/}) }
25
+ spec.bindir = "bin"
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mac_address_eui48
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mathieu Cunche
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-09-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.10'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '10.0'
46
+ description:
47
+ email:
48
+ - mathieu.cunche@insa-lyon.fr
49
+ executables:
50
+ - console
51
+ - setup
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - bin/console
60
+ - bin/setup
61
+ - data/oui.txt
62
+ - lib/mac_address_eui48.rb
63
+ - lib/mac_address_eui48/mac_address.rb
64
+ - lib/mac_address_eui48/oui_resolver.rb
65
+ - lib/mac_address_eui48/version.rb
66
+ - mac_address_eui48.gemspec
67
+ homepage: https://github.com/cunchem/mac_address_eui48
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.23
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: An implementation of class for MAC address (i.e. IEEE EUI48 identifiers).
91
+ test_files: []