-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheth_packet.go
More file actions
60 lines (51 loc) · 1.27 KB
/
eth_packet.go
File metadata and controls
60 lines (51 loc) · 1.27 KB
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
package l2
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"log"
"strings"
)
// Basic utility function to take a string and turn it into a mac address.
func MacToBytes(m string) ([]byte, error) {
b, err := hex.DecodeString(strings.Replace(m, ":", "", -1))
if err != nil {
return nil, err
}
if len(b) != 6 {
return nil, errors.New(fmt.Sprint("Expected mac of length 6 bytes got %d", len(b)))
}
return b, nil
}
func macToBytesOrDie(m string) []byte {
b, err := MacToBytes(m)
if err != nil {
log.Fatal(err)
}
return b
}
// A utility type to make introspecting ethernet frames easier.
type EthFrame []byte
func NewEthFrame(destination, source []byte, protocol uint16, data []byte) EthFrame {
p := make([]byte, 12+2+len(data))
copy(p[0:6], destination)
copy(p[6:12], source)
binary.BigEndian.PutUint16(p[12:14], protocol)
copy(p[14:], data)
return EthFrame(p)
}
func (p EthFrame) Destination() []byte {
return p[0:6]
}
func (p EthFrame) Source() []byte {
return p[6:12]
}
// The ethernet type. Note that if this is <1504 it is likely a length instead
// and you are communicating with an extremely non standard ethernet device.
func (p EthFrame) Type() uint16 {
return binary.BigEndian.Uint16(p[12:14])
}
func (p EthFrame) Data() []byte {
return p[14:]
}