maca 0.2.0 → 0.4.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.
- checksums.yaml +4 -4
- data/README.md +69 -4
- data/lib/maca/macaddress.rb +67 -4
- data/lib/maca/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85373f80878aa63bce4ea371c64c5f3c41ad318f76b2eb72733d2c72b57794bf
|
4
|
+
data.tar.gz: fc31705f45c8209c33da8d9b8d5effb211c417b2fdf6ccbaf3330fdd5e8d9d90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1bfca6661cba8b9b968f789003249d992db9c178970196611d7a40db12f87c88e8d2eabc9e9cc82882f280ad68e96b03ee8c92713b4ab06b354d620659ffacf
|
7
|
+
data.tar.gz: 54deb43cf21756b8d3e81d85557cfb9b6f95def0074edadcdea4f0f7b0c034648be1068eae84a9c8d18b44c1b008d2374f8f90f38b901eba2524da1ad12f4570
|
data/README.md
CHANGED
@@ -17,17 +17,30 @@ gem install maca
|
|
17
17
|
```
|
18
18
|
|
19
19
|
## Usage
|
20
|
+
### Basic
|
21
|
+
|
22
|
+
Create a new `Macaddress` object from a MAC Address in various formats.
|
20
23
|
|
21
24
|
```ruby
|
22
25
|
require 'maca'
|
23
26
|
|
24
|
-
Macaddress.new("
|
25
|
-
=> #<Macaddress @macaddress="
|
27
|
+
Macaddress.new("01:23:45:67:89:ab")
|
28
|
+
=> #<Macaddress:0x000000012073db08 @macaddress="0123456789ab">
|
26
29
|
|
27
30
|
Macaddress.new("01:23:45:67:89:ab").to_s
|
28
31
|
=> "01:23:45:67:89:AB"
|
29
32
|
|
30
|
-
|
33
|
+
Macaddress.new("01:23:45:67:89:ab").to_i
|
34
|
+
=> 1250999896491
|
35
|
+
```
|
36
|
+
|
37
|
+
### Format String
|
38
|
+
|
39
|
+
**Format Normalization**
|
40
|
+
|
41
|
+
Normalize various MAC Address notations (hyphenated, dot-separated, raw hex, etc.) into the standard colon-separated format.
|
42
|
+
|
43
|
+
```ruby
|
31
44
|
Macaddress.new("00-00-00-00-00-00").to_s
|
32
45
|
=> "00:00:00:00:00:00"
|
33
46
|
|
@@ -39,8 +52,22 @@ Macaddress.new("0000.0000.0000").to_s
|
|
39
52
|
|
40
53
|
Macaddress.new("000000-000000").to_s
|
41
54
|
=> "00:00:00:00:00:00"
|
55
|
+
```
|
56
|
+
|
57
|
+
**Format Conversion**
|
58
|
+
|
59
|
+
Convert the MAC Address to a custom string format using a specified delimiter and step size.
|
42
60
|
|
43
|
-
|
61
|
+
```ruby
|
62
|
+
Macaddress.new("00-00-00-00-00-00").to_fs(delimiter: '.', step: 4)
|
63
|
+
=> "0000.0000.0000"
|
64
|
+
```
|
65
|
+
|
66
|
+
### Comparison
|
67
|
+
|
68
|
+
Compare `Macaddress` objects for equality based on their normalized form.
|
69
|
+
|
70
|
+
```ruby
|
44
71
|
Macaddress.new("00:00:00:00:00:00") == Macaddress.new("00:00:00:00:00:00")
|
45
72
|
=> true
|
46
73
|
|
@@ -48,6 +75,44 @@ Macaddress.new("00:00:00:00:00:00") == Macaddress.new("00:00:00:00:00:01")
|
|
48
75
|
=> false
|
49
76
|
```
|
50
77
|
|
78
|
+
### Address type check
|
79
|
+
|
80
|
+
Determine the type of a MAC address, such as whether it is unicast or multicast, and whether it is locally or universally administered.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
Macaddress.new("00:00:00:00:00:00").unicast?
|
84
|
+
=> true
|
85
|
+
|
86
|
+
Macaddress.new("00:00:00:00:00:00").broadcast?
|
87
|
+
=> false
|
88
|
+
|
89
|
+
Macaddress.new("33:33:00:00:00:01").multicast?
|
90
|
+
=> true
|
91
|
+
|
92
|
+
Macaddress.new("02:00:00:00:00:00").locally_administered?
|
93
|
+
=> true
|
94
|
+
|
95
|
+
Macaddress.new("01:00:00:00:00:00").universally_administered?
|
96
|
+
=> true
|
97
|
+
|
98
|
+
Macaddress.new("02:00:00:00:00:00").random?
|
99
|
+
=> true
|
100
|
+
```
|
101
|
+
|
102
|
+
### OUI
|
103
|
+
|
104
|
+
Extract the Organizationally Unique Identifier (OUI) from a MAC address.
|
105
|
+
|
106
|
+
The `#oui` method returns the first 24 bits (or the first 3 octets) of a MAC Address, which typically identifies the device manufacturer or vendor.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
Macaddress.new("01:23:45:67:89:ab").oui # default format: :base16
|
110
|
+
=> "012345"
|
111
|
+
|
112
|
+
Macaddress.new("01:23:45:67:89:ab").oui(format: :hex)
|
113
|
+
=> "01-23-45"
|
114
|
+
```
|
115
|
+
|
51
116
|
## Development
|
52
117
|
|
53
118
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/maca/macaddress.rb
CHANGED
@@ -2,22 +2,85 @@
|
|
2
2
|
|
3
3
|
module Maca
|
4
4
|
class Macaddress
|
5
|
-
|
5
|
+
DEFAULT_DELIMITER = ':'
|
6
|
+
DEFAULT_STEP = 2
|
7
|
+
DELIMITERS = ':.-'
|
8
|
+
REGEXP_LIST_MACADDRESS = [
|
9
|
+
/^([0-9A-Fa-f]{2}[#{DELIMITERS}]){5}([0-9A-Fa-f]{2})$/, # xx:xx:xx:xx:xx:xx
|
10
|
+
/^([0-9A-Fa-f]{4}[#{DELIMITERS}]){2}([0-9A-Fa-f]{4})$/, # xxxx:xxxx:xxxx
|
11
|
+
/^([0-9A-Fa-f]{6}[#{DELIMITERS}]){1}([0-9A-Fa-f]{6})$/, # xxxxxx:xxxxxx
|
12
|
+
/^([0-9A-Fa-f]{12})$/, # xxxxxxxxxxxx
|
13
|
+
]
|
6
14
|
|
7
15
|
def initialize(addr)
|
8
16
|
if Maca::Macaddress.valid?(addr)
|
9
|
-
@macaddress = addr
|
17
|
+
@macaddress = addr.downcase.delete(DELIMITERS)
|
10
18
|
else
|
11
19
|
raise Maca::InvalidAddressError, "Invalid MAC Address #{addr}"
|
12
20
|
end
|
13
21
|
end
|
14
22
|
|
15
23
|
def self.valid?(addr)
|
16
|
-
|
24
|
+
REGEXP_LIST_MACADDRESS.each do |regexp_macaddress|
|
25
|
+
if regexp_macaddress.match?(addr)
|
26
|
+
return true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
false
|
17
31
|
end
|
18
32
|
|
19
33
|
def to_s
|
20
|
-
|
34
|
+
to_fs
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_fs(delimiter: DEFAULT_DELIMITER, step: DEFAULT_STEP)
|
38
|
+
raise RangeError, "step must be even, and between 2 and 6" unless (2..6).cover?(step) && step.even?
|
39
|
+
|
40
|
+
@macaddress.upcase.scan(/.{1,#{step}}/).join(delimiter)
|
41
|
+
end
|
42
|
+
alias_method :to_formatted_s, :to_fs
|
43
|
+
|
44
|
+
def to_i
|
45
|
+
@macaddress.hex
|
46
|
+
end
|
47
|
+
|
48
|
+
def broadcast?
|
49
|
+
@macaddress == 'ff' * 6
|
50
|
+
end
|
51
|
+
|
52
|
+
def multicast?
|
53
|
+
(@macaddress[0..1].hex & 0x01).positive?
|
54
|
+
end
|
55
|
+
|
56
|
+
def unicast?
|
57
|
+
!multicast?
|
58
|
+
end
|
59
|
+
|
60
|
+
def locally_administered?
|
61
|
+
return false if broadcast?
|
62
|
+
(@macaddress[0..1].hex & 0x02).positive?
|
63
|
+
end
|
64
|
+
|
65
|
+
def universally_administered?
|
66
|
+
!locally_administered?
|
67
|
+
end
|
68
|
+
|
69
|
+
def random?
|
70
|
+
unicast? && locally_administered?
|
71
|
+
end
|
72
|
+
|
73
|
+
def oui(format: :base16)
|
74
|
+
oui = @macaddress[0..5]
|
75
|
+
|
76
|
+
# format: base 16 (xxxxxx)
|
77
|
+
if format.to_s == "base16"
|
78
|
+
return oui.upcase
|
79
|
+
end
|
80
|
+
|
81
|
+
# default format: hex (xx-xx-xx)
|
82
|
+
delimiter = "-"
|
83
|
+
oui.upcase.scan(/.{1,2}/).join(delimiter)
|
21
84
|
end
|
22
85
|
|
23
86
|
def ==(other)
|
data/lib/maca/version.rb
CHANGED