-
Notifications
You must be signed in to change notification settings - Fork 16
/
udp.go
105 lines (83 loc) · 3.21 KB
/
udp.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
package zerocopy
import (
"context"
"sync"
"time"
"github.com/database64128/shadowsocks-go/conn"
)
// UDPClientInfo contains information about a UDP client.
type UDPClientInfo struct {
// Name is the name of the UDP client.
Name string
// PackerHeadroom is the headroom required by the packet packer.
PackerHeadroom Headroom
// MTU is the MTU of the client's designated network path.
MTU int
// ListenConfig is the [conn.ListenConfig] for opening client sockets.
ListenConfig conn.ListenConfig
}
// UDPClientSession contains information about a UDP client session.
type UDPClientSession struct {
// MaxPacketSize is the maximum size of outgoing packets.
MaxPacketSize int
// Packer is the packet packer for the session.
Packer ClientPacker
// Unpacker is the packet unpacker for the session.
Unpacker ClientUnpacker
// Close closes the session.
Close func() error
}
// NoopClose is a no-op close function.
func NoopClose() error {
return nil
}
// UDPClient stores information for creating new client sessions.
type UDPClient interface {
// Info returns information about the client.
Info() UDPClientInfo
// NewSession creates a new client session, and returns the session info or an error.
// The returned [UDPClientInfo] is always valid, even when session creation fails.
NewSession(ctx context.Context) (UDPClientInfo, UDPClientSession, error)
}
// UDPNATServerInfo contains information about a UDP NAT server.
type UDPNATServerInfo struct {
// UnpackerHeadroom is the headroom required by the packet unpacker.
UnpackerHeadroom Headroom
}
// UDPNATServer stores information for creating new server sessions.
type UDPNATServer interface {
// Info returns information about the server.
Info() UDPNATServerInfo
// NewUnpacker creates a new packet unpacker for the session.
//
// The returned unpacker is then used by the caller to unpack the incoming packet.
// Upon successful unpacking, the unpacker's NewPacker method can be called to create
// a corresponding packet packer.
NewUnpacker() (ServerUnpacker, error)
}
// UDPSessionServerInfo contains information about a UDP session server.
type UDPSessionServerInfo struct {
// UnpackerHeadroom is the headroom required by the packet unpacker.
UnpackerHeadroom Headroom
// MinNATTimeout is the server's minimum allowed NAT timeout.
// 0 means no requirement.
MinNATTimeout time.Duration
}
// UDPSessionServer deals with incoming sessions.
type UDPSessionServer interface {
sync.Locker
// Info returns information about the server.
Info() UDPSessionServerInfo
// SessionInfo extracts session ID from a received packet b.
//
// The returned session ID is then used by the caller to look up the session table.
// If no matching entries were found, NewUnpacker should be called to create a new
// packet unpacker for the packet.
SessionInfo(b []byte) (csid uint64, err error)
// NewUnpacker creates a new packet unpacker for the specified client session.
//
// The returned unpacker is then used by the caller to unpack the incoming packet.
// Upon successful unpacking, the unpacker's NewPacker method can be called to create
// a corresponding server session.
NewUnpacker(b []byte, csid uint64) (serverUnpacker ServerUnpacker, username string, err error)
}