Skip to content

Commit 30f817a

Browse files
added oui for MAC addresses
1 parent dcddb4f commit 30f817a

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/shadowy-pycoder/mshark
22

3-
go 1.23.0
3+
go 1.24.1
44

55
require (
66
github.com/mdlayher/packet v1.1.2
77
github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5
8+
github.com/shadowy-pycoder/oui v0.5.2
89
github.com/stretchr/testify v1.9.0
910
golang.org/x/net v0.28.0
1011
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5 h1:p4VuaitqUAqSZ
1414
github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5/go.mod h1:zIAoVKeWP0mz4zXY50UYQt6NLg2uwKRswMDcGEqOms4=
1515
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1616
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/shadowy-pycoder/oui v0.5.2 h1:865RE2ohoKxLFtUmQVfb/3LWaWyzREy0ksjTl4LdfsY=
18+
github.com/shadowy-pycoder/oui v0.5.2/go.mod h1:CLMFNpxNZ1ftiy+zCImvM15kVHjW7L1o8ehiTO5Dim8=
1719
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1820
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1921
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=

layers/arp.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"net"
77
"net/netip"
8+
9+
"github.com/shadowy-pycoder/oui"
810
)
911

1012
const headerSizeARP = 28
@@ -45,6 +47,8 @@ type ARPPacket struct {
4547
// In an ARP reply this field is used to indicate the address of the host that originated the ARP request.
4648
TargetMAC net.HardwareAddr
4749
TargetIP netip.Addr // Internetwork address of the intended receiver.
50+
dstVendor string
51+
srcVendor string
4852
}
4953

5054
func NewARPPacket(
@@ -71,6 +75,8 @@ func NewARPPacket(
7175
SenderIP: senderIP,
7276
TargetMAC: targetMAC,
7377
TargetIP: targetIP,
78+
dstVendor: oui.VendorWithMAC(targetMAC),
79+
srcVendor: oui.VendorWithMAC(senderMAC),
7480
}, nil
7581
}
7682

@@ -81,9 +87,9 @@ func (ap *ARPPacket) String() string {
8187
- HLen: %d
8288
- PLen: %d
8389
- Operation: %s
84-
- Sender MAC Address: %s
90+
- Sender MAC Address: %s (%s)
8591
- Sender IP Address: %s
86-
- Target MAC Address: %s
92+
- Target MAC Address: %s (%s)
8793
- Target IP Address: %s
8894
`,
8995
ap.Summary(),
@@ -94,8 +100,10 @@ func (ap *ARPPacket) String() string {
94100
ap.Plen,
95101
ap.Op,
96102
ap.SenderMAC,
103+
ap.srcVendor,
97104
ap.SenderIP,
98105
ap.TargetMAC,
106+
ap.dstVendor,
99107
ap.TargetIP,
100108
)
101109
}
@@ -167,6 +175,8 @@ func (ap *ARPPacket) UnmarshalBinary(data []byte) error {
167175
if !ok {
168176
return fmt.Errorf("failed parsing target IP address")
169177
}
178+
ap.dstVendor = oui.VendorWithMAC(ap.TargetMAC)
179+
ap.srcVendor = oui.VendorWithMAC(ap.SenderMAC)
170180
return nil
171181
}
172182

layers/ethernet.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/hex"
66
"fmt"
77
"net"
8+
9+
"github.com/shadowy-pycoder/oui"
810
)
911

1012
const headerSizeEthernet = 14
@@ -33,6 +35,8 @@ type EthernetFrame struct {
3335
SrcMAC net.HardwareAddr // MAC address of the source device.
3436
EtherType *EthernetType // The protocol of the upper layer.
3537
Payload []byte
38+
dstVendor string
39+
srcVendor string
3640
}
3741

3842
func NewEthernetFrame(dstMAC, srcMAC net.HardwareAddr, et EtherType, payload []byte) (*EthernetFrame, error) {
@@ -44,26 +48,30 @@ func NewEthernetFrame(dstMAC, srcMAC net.HardwareAddr, et EtherType, payload []b
4448
SrcMAC: srcMAC,
4549
EtherType: &EthernetType{Val: et, Desc: ethertypedesc(et)},
4650
Payload: payload,
51+
dstVendor: oui.VendorWithMAC(dstMAC),
52+
srcVendor: oui.VendorWithMAC(srcMAC),
4753
}, nil
4854
}
4955

5056
func (ef *EthernetFrame) String() string {
5157
return fmt.Sprintf(`%s
52-
- DstMAC: %s
53-
- SrcMAC: %s
58+
- DstMAC: %s (%s)
59+
- SrcMAC: %s (%s)
5460
- EtherType: %s
5561
- Payload: %d bytes
5662
%s`,
5763
ef.Summary(),
5864
ef.DstMAC,
65+
ef.dstVendor,
5966
ef.SrcMAC,
67+
ef.srcVendor,
6068
ef.EtherType,
6169
len(ef.Payload),
6270
hex.Dump(ef.ToBytes()))
6371
}
6472

6573
func (ef *EthernetFrame) Summary() string {
66-
return fmt.Sprintf("Ethernet Frame: Src MAC: %s → Dst MAC: %s", ef.SrcMAC, ef.DstMAC)
74+
return fmt.Sprintf("Ethernet Frame: Src MAC: %s → Dst MAC: %s", ef.srcVendor, ef.dstVendor)
6775
}
6876

6977
func (ef *EthernetFrame) MarshalBinary() ([]byte, error) {
@@ -90,6 +98,8 @@ func (ef *EthernetFrame) UnmarshalBinary(data []byte) error {
9098
etdesc := ethertypedesc(et)
9199
ef.EtherType = &EthernetType{Val: et, Desc: etdesc}
92100
ef.Payload = data[headerSizeEthernet:]
101+
ef.dstVendor = oui.VendorWithMAC(ef.DstMAC)
102+
ef.srcVendor = oui.VendorWithMAC(ef.SrcMAC)
93103
return nil
94104
}
95105

0 commit comments

Comments
 (0)