forked from Lukasa/gopcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
111 lines (90 loc) · 2.6 KB
/
link.go
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
package gopcap
import (
"encoding/binary"
"io"
)
// The minimum value of the EtherType field. If the value is less than this, it's a length.
// The above statement isn't entirely true, but it's true enough.
const minEtherType uint16 = 1536
//-------------------------------------------------------------------------------------------
// UnknownLink
//-------------------------------------------------------------------------------------------
// UnknownLink represents the data for a link type that gopcap doesn't understand. It simply
// provides uninterpreted data representing the entire link-layer packet.
type UnknownLink struct {
data InternetLayer
}
func (u *UnknownLink) LinkData() InternetLayer {
return u.data
}
func (u *UnknownLink) ReadFrom(src io.Reader) error {
u.data = new(UnknownINet)
err := u.data.ReadFrom(src)
return err
}
//-------------------------------------------------------------------------------------------
// EthernetFrame
//-------------------------------------------------------------------------------------------
// EthernetFrame represents a single ethernet frame. Valid only when the LinkType is ETHERNET.
type EthernetFrame struct {
MACSource [6]byte
MACDestination [6]byte
VLANTag []byte
Length uint16
EtherType EtherType
data InternetLayer
}
func (e *EthernetFrame) LinkData() InternetLayer {
return e.data
}
// Given a series of bytes, populate the EthernetFrame structure.
func (e *EthernetFrame) ReadFrom(src io.Reader) error {
err := readFields(src, networkByteOrder, []interface{}{
&e.MACDestination,
&e.MACSource,
})
if err != nil {
return err
}
nextValue := uint16(0)
err = binary.Read(src, networkByteOrder, &nextValue)
if err != nil {
return err
}
// Check for a VLAN tag.
if nextValue == 0x8100 {
vlanTag := make([]byte, 4)
vlanTag[0] = 0x81
vlanTag[1] = 0x00
_, err = src.Read(vlanTag[2:])
if err != nil {
return err
}
e.VLANTag = vlanTag
// Re-read the next value
err = binary.Read(src, networkByteOrder, &nextValue)
if err != nil {
return err
}
}
// Read the size or type
if nextValue < minEtherType {
e.Length = nextValue
} else {
e.EtherType = EtherType(nextValue)
}
// Everything else is payload data.
return e.readInternetLayer(src)
}
// buildInternetLayer creates the internet layer sub-data for a link layer datagram.
func (e *EthernetFrame) readInternetLayer(src io.Reader) error {
switch e.EtherType {
case ETHERTYPE_IPV4:
e.data = new(IPv4Packet)
case ETHERTYPE_IPV6:
e.data = new(IPv6Packet)
default:
e.data = new(UnknownINet)
}
return e.data.ReadFrom(src)
}