-
Notifications
You must be signed in to change notification settings - Fork 10
/
packet.go
113 lines (100 loc) · 2.6 KB
/
packet.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
package mwgp
import (
"encoding/binary"
"fmt"
"golang.zx2c4.com/wireguard/device"
"net"
)
const (
defaultMaxPacketSize = 65536
)
const (
PacketFlagDeobfuscatedAfterReceived = 1 << iota
PacketFlagObfuscateBeforeSend
)
type Packet struct {
Data []byte
Length int
Source *net.UDPAddr
Destination *net.UDPAddr
Flags uint64
}
func (p *Packet) Reset() {
//p.Data = [kMTU]byte{}
p.Length = 0
p.Source = nil
p.Destination = nil
p.Flags = 0
}
func (p *Packet) Slice() []byte {
return p.Data[:p.Length]
}
func (p *Packet) MessageType() int {
if p.Length < 1 {
return -1
}
return int((p.Data)[0])
}
func (p *Packet) ReceiverIndex() (index uint32, err error) {
messageType := p.MessageType()
switch messageType {
case device.MessageInitiationType:
index, err = p.getLEUint32Offset(8)
case device.MessageResponseType:
index, err = p.getLEUint32Offset(8)
case device.MessageCookieReplyType:
index, err = p.getLEUint32Offset(4)
case device.MessageTransportType:
index, err = p.getLEUint32Offset(4)
default:
err = fmt.Errorf("cannot get receiver_index for message type %d", messageType)
}
return
}
func (p *Packet) SetSenderIndex(index uint32) (err error) {
messageType := p.MessageType()
switch messageType {
case device.MessageInitiationType:
err = p.putLEUint32Offset(4, index)
case device.MessageResponseType:
err = p.putLEUint32Offset(4, index)
default:
err = fmt.Errorf("cannot set sender_index for message type %d", messageType)
}
return
}
func (p *Packet) SetReceiverIndex(index uint32) (err error) {
messageType := p.MessageType()
switch messageType {
case device.MessageInitiationType:
err = p.putLEUint32Offset(8, index)
case device.MessageResponseType:
err = p.putLEUint32Offset(8, index)
case device.MessageCookieReplyType:
err = p.putLEUint32Offset(4, index)
case device.MessageTransportType:
err = p.putLEUint32Offset(4, index)
default:
err = fmt.Errorf("cannot set receiver_index for message type %d", messageType)
}
return
}
func (p *Packet) getLEUint32Offset(bytesOffset int) (value uint32, err error) {
if p.Length < bytesOffset+4 {
err = fmt.Errorf("packet is too short to get uint32 at offset %d", bytesOffset)
return
}
value = binary.LittleEndian.Uint32(p.Data[bytesOffset:])
return
}
func (p *Packet) putLEUint32Offset(bytesOffset int, value uint32) (err error) {
if p.Length < bytesOffset+4 {
err = fmt.Errorf("packet is too short to put uint32 at offset %d", bytesOffset)
return
}
binary.LittleEndian.PutUint32(p.Data[bytesOffset:], value)
return
}
func (p *Packet) FixMACs(cg *device.CookieGenerator) {
cg.AddMacs(p.Slice())
}