/
ethernet.go
197 lines (177 loc) · 3.71 KB
/
ethernet.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package pacit
import (
"bytes"
"encoding/binary"
"io"
"net"
)
// see http://en.wikipedia.org/wiki/EtherType
const (
IPv4_MSG = 0x0800
ARP_MSG = 0x0806
LLDP_MSG = 0x88cc
WOL_MSG = 0x0842
RARP_MSG = 0x8035
VLAN_MSG = 0x8100
)
type ReadWriterMeasurer interface {
io.Reader
io.ReaderFrom
Len() uint16
}
type ReadWriteMeasurer interface {
io.ReadWriter
Len() uint16
}
type Ethernet struct {
Delimiter uint8
HWDst net.HardwareAddr
HWSrc net.HardwareAddr
VLANID VLAN
Ethertype uint16
Data ReadWriteMeasurer
}
func NewEthernet() *Ethernet {
eth := new(Ethernet)
eth.HWDst = net.HardwareAddr(make([]byte, 6))
eth.HWSrc = net.HardwareAddr(make([]byte, 6))
eth.VLANID = *NewVLAN()
eth.Ethertype = 0x800
return eth
}
func (e *Ethernet) Len() (n uint16) {
if e.VLANID.VID != 0 {
n += 5
}
n += 12
n += 2
if e.Data != nil {
n += e.Data.Len()
}
return
}
func (e *Ethernet) Read(b []byte) (n int, err error) {
buf := new(bytes.Buffer)
//If you send a packet with the delimiter to the wire
//packets are incorrectly interpreted.
binary.Write(buf, binary.BigEndian, e.HWDst)
binary.Write(buf, binary.BigEndian, e.HWSrc)
if e.VLANID.VID != 0 {
c := []byte{0, 0}
e.VLANID.Read(c)
binary.Write(buf, binary.BigEndian, c)
}
binary.Write(buf, binary.BigEndian, e.Ethertype)
// In case the data type isn't known
if e.Data != nil {
if n, err := buf.ReadFrom(e.Data); n == 0 {
return int(n), err
}
}
n, err = buf.Read(b)
return n, io.EOF
}
type PacitBuffer struct{ *bytes.Buffer }
func NewBuffer(buf []byte) *PacitBuffer {
b := &PacitBuffer{Buffer: bytes.NewBuffer(buf)}
return b
}
func (b *PacitBuffer) Len() uint16 {
return uint16(b.Buffer.Len())
}
func (e *Ethernet) Write(b []byte) (n int, err error) {
// Delimiter comes in from the wire. Not sure why this is the case, ignore
n += 1
e.HWDst = make([]byte, 6)
e.HWDst = b[1:7]
n += 6
e.HWSrc = make([]byte, 6)
e.HWSrc = b[7:13]
n += 6
e.Ethertype = binary.BigEndian.Uint16(b[13:15])
n += 2
// If tagged
if e.Ethertype == VLAN_MSG {
e.VLANID = *new(VLAN)
e.VLANID.Write(b[13:17])
n += 2
e.Ethertype = binary.BigEndian.Uint16(b[13:15])
n += 2
} else {
e.VLANID = *new(VLAN)
e.VLANID.VID = 0
}
switch e.Ethertype {
case IPv4_MSG:
e.Data = new(IPv4)
m, _ := e.Data.Write(b[n:])
n += m
case ARP_MSG:
e.Data = new(ARP)
m, _ := e.Data.Write(b[n:])
n += m
case RARP_MSG:
/*case LLDP_MSG:
e.Data = new(LLDP)
m, _ := e.Data.Write(b[n:])
n += m*/
default:
e.Data = NewBuffer(b[n:])
n += int(e.Data.Len())
//n += len(b[n:])
}
return
}
const (
PCP_MASK = 0xe000
DEI_MASK = 0x1000
VID_MASK = 0x0fff
)
type VLAN struct {
TPID uint16
PCP uint8
DEI uint8
VID uint8
}
func NewVLAN() *VLAN {
v := new(VLAN)
v.TPID = 0x8100
v.VID = 0
return v
}
func (v *VLAN) Read(b []byte) (n int, err error) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, v.TPID)
var tci uint16 = 0
tci = (tci | uint16(v.PCP)<<13) + (tci | uint16(v.DEI)<<12) + (tci | uint16(v.VID))
binary.Write(buf, binary.BigEndian, tci)
n, err = buf.Read(b)
return
}
func (v *VLAN) ReadFrom(r io.Reader) (n int64, err error) {
var tci uint16 = 0
if err = binary.Read(r, binary.BigEndian, &tci); err != nil {
return
}
n += 2
v.PCP = uint8(PCP_MASK & tci >> 13)
v.DEI = uint8(DEI_MASK & tci >> 12)
v.VID = uint8(VID_MASK & tci)
return
}
func (v *VLAN) Write(b []byte) (n int, err error) {
var tci uint16 = 0
buf := bytes.NewBuffer(b)
if err = binary.Read(buf, binary.BigEndian, &v.TPID); err != nil {
return
}
n += 2
if err = binary.Read(buf, binary.BigEndian, &tci); err != nil {
return
}
n += 2
v.PCP = uint8(PCP_MASK & tci >> 13)
v.DEI = uint8(DEI_MASK & tci >> 12)
v.VID = uint8(VID_MASK & tci)
return
}