/
MAC_addr.pm6
132 lines (89 loc) · 2.52 KB
/
MAC_addr.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use Net::Packet :util;
=NAME
Net::Packet::MAC_addr - Decode and format MAC addresses.
=begin SYNOPSIS
use Net::Packet::MAC_addr :short;
my $buf = Buf.new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]);
my $mac = MAC_addr.unpack($buf, 1); # Start unpacking at $buf[1].
say $mac.Str; # '11:22:33:44:55:66'
my $mac = MAC_addr.new(0x112233AABBCC);
say $mac.Str; # '11:22:33:AA:BB:CC'
=end SYNOPSIS
=begin EXPORTS
Net::Packet::MAC_addr
:short trait adds export:
constant MAC_addr ::= Net::Packet::MAC_addr
=end EXPORTS
=DESCRIPTION
=head2 class Net::Packet::MAC_addr
unit class Net::Packet::MAC_addr;
my constant MAC_addr is export(:short) ::= Net::Packet::MAC_addr;
=head3 Attributes
=begin code
$.addr is rw is Int
Address
=end code
has Int $.addr is rw = 0;
=head3 Methods
=begin code
.new(Int $addr) returns MAC_addr
.new(Str $addr) returns MAC_addr
Constructor, takes $addr:
- Int;
- Str in the form of '00:11:22:AA:BB:CC'.
=end code
multi method new(Int $addr) returns MAC_addr {
self.bless(:$addr);
}
multi method new(Str $addr) returns MAC_addr {
die('MAC_addr.new: Invalid string format')
unless $addr ~~ /^(<[0..9 A..F a..f]>**2\:)**5 <[0..9 A..F a..f]>**2$/;
self.bless(:addr(('0x'~$addr.subst(':', '', :g)).Int));
}
=begin code
.unpack($buf, Int $i) returns MAC_addr
Constructor, unpacks address from buffer $buf starting at position $i.
$buf can be of builtin type Buf or C_Buf from Net::Pcap.
=end code
method unpack($buf, Int $i) returns MAC_addr {
my Int $hi = unpack_N($buf, $i);
my Int $low = unpack_n($buf, $i+4);
MAC_addr.new(($hi +< 16) + $low);
}
=begin code
.octets() returns Array
Returns the address represented by an Array of 6 Ints (one for each byte of
the address).
=end code
method octets() returns Array {
my @octs;
@octs.push: $.addr +> 40;
@octs.push: ($.addr +> 32) +& 0xFF;
@octs.push: ($.addr +> 24) +& 0xFF;
@octs.push: ($.addr +> 16) +& 0xFF;
@octs.push: ($.addr +> 8) +& 0xFF;
@octs.push: $.addr +& 0xFF;
@octs;
}
=begin code
.Int() returns Int
Returns the address as Int.
=end code
method Int() returns Int {
$.addr;
}
=begin code
.Str() returns Str
Returns the address as string in the form of '00:11:22:AA:BB:CC'.
=end code
method Str() returns Str {
my @octs = self.octets();
sprintf('%02X:%02X:%02X:%02X:%02X:%02X', |@octs);
}
=begin code
.Buf() returns Buf
Returns the address as a byte string.
=end code
method Buf() returns Buf {
Buf.new(|self.octets);
}