This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
outside.go
73 lines (65 loc) · 1.6 KB
/
outside.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
package wireguard
import (
"fmt"
"net"
)
type UDPConn interface {
ReadFromUDP(b []byte) (int, *net.UDPAddr, error)
WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
Close() error
}
type packet struct {
addr *net.UDPAddr
data []byte
}
func (f *Interface) acceptOutsidePackets() {
for {
buf := make([]byte, mtu)
n, addr, err := f.outside.ReadFromUDP(buf)
if err != nil {
// TODO: figure out what kind of errors can be returned
}
buf = buf[:n]
f.receiveOutsidePacket(packet{addr, buf})
}
}
func (f *Interface) receiveOutsidePacket(p packet) {
switch checkMessageType(p.data) {
case messageHandshakeInitiation, messageHandshakeResponse, messageHandshakeCookie:
// queue handshake
case messageData:
// queue for data processing
default:
// log invalid packet
}
}
func (f *Interface) receiveHandshakePacket(typ messageType, p packet) {
// TODO: cookie check
var peer *peer
var err error
switch typ {
case messageHandshakeInitiation:
peer, err = f.handshakeConsumeInitiation(p.data)
if err != nil {
// TODO: log error
return
}
peer.updateLatestAddr(p.addr)
res := f.handshakeCreateResponse(&peer.handshake)
_ = res
case messageHandshakeResponse:
peer, err = f.handshakeConsumeResponse(p.data)
if err != nil {
// TODO: log error
return
}
peer.timerEphemeralKeyCreated()
peer.timerHandshakeComplete()
default:
panic(fmt.Sprintf("invalid packet type %d from %s in receiveHandshakePacket", typ, p.addr))
}
peer.rxStats(len(p.data))
peer.timerAnyAuthenticatedPacketReceived()
peer.timerAnyAuthenticatedPacketTraversal()
peer.updateLatestAddr(p.addr)
}