github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/zerocopy/udp.go (about) 1 package zerocopy 2 3 import ( 4 "context" 5 "sync" 6 "time" 7 8 "github.com/database64128/shadowsocks-go/conn" 9 ) 10 11 // UDPClientInfo contains information about a UDP client. 12 type UDPClientInfo struct { 13 // Name is the name of the UDP client. 14 Name string 15 16 // PackerHeadroom is the headroom required by the packet packer. 17 PackerHeadroom Headroom 18 19 // MTU is the MTU of the client's designated network path. 20 MTU int 21 22 // ListenConfig is the [conn.ListenConfig] for opening client sockets. 23 ListenConfig conn.ListenConfig 24 } 25 26 // UDPClientSession contains information about a UDP client session. 27 type UDPClientSession struct { 28 // MaxPacketSize is the maximum size of outgoing packets. 29 MaxPacketSize int 30 31 // Packer is the packet packer for the session. 32 Packer ClientPacker 33 34 // Unpacker is the packet unpacker for the session. 35 Unpacker ClientUnpacker 36 37 // Close closes the session. 38 Close func() error 39 } 40 41 // NoopClose is a no-op close function. 42 func NoopClose() error { 43 return nil 44 } 45 46 // UDPClient stores information for creating new client sessions. 47 type UDPClient interface { 48 // Info returns information about the client. 49 Info() UDPClientInfo 50 51 // NewSession creates a new client session, and returns the session info or an error. 52 // The returned [UDPClientInfo] is always valid, even when session creation fails. 53 NewSession(ctx context.Context) (UDPClientInfo, UDPClientSession, error) 54 } 55 56 // UDPNATServerInfo contains information about a UDP NAT server. 57 type UDPNATServerInfo struct { 58 // UnpackerHeadroom is the headroom required by the packet unpacker. 59 UnpackerHeadroom Headroom 60 } 61 62 // UDPNATServer stores information for creating new server sessions. 63 type UDPNATServer interface { 64 // Info returns information about the server. 65 Info() UDPNATServerInfo 66 67 // NewUnpacker creates a new packet unpacker for the session. 68 // 69 // The returned unpacker is then used by the caller to unpack the incoming packet. 70 // Upon successful unpacking, the unpacker's NewPacker method can be called to create 71 // a corresponding packet packer. 72 NewUnpacker() (ServerUnpacker, error) 73 } 74 75 // UDPSessionServerInfo contains information about a UDP session server. 76 type UDPSessionServerInfo struct { 77 // UnpackerHeadroom is the headroom required by the packet unpacker. 78 UnpackerHeadroom Headroom 79 80 // MinNATTimeout is the server's minimum allowed NAT timeout. 81 // 0 means no requirement. 82 MinNATTimeout time.Duration 83 } 84 85 // UDPSessionServer deals with incoming sessions. 86 type UDPSessionServer interface { 87 sync.Locker 88 89 // Info returns information about the server. 90 Info() UDPSessionServerInfo 91 92 // SessionInfo extracts session ID from a received packet b. 93 // 94 // The returned session ID is then used by the caller to look up the session table. 95 // If no matching entries were found, NewUnpacker should be called to create a new 96 // packet unpacker for the packet. 97 SessionInfo(b []byte) (csid uint64, err error) 98 99 // NewUnpacker creates a new packet unpacker for the specified client session. 100 // 101 // The returned unpacker is then used by the caller to unpack the incoming packet. 102 // Upon successful unpacking, the unpacker's NewPacker method can be called to create 103 // a corresponding server session. 104 NewUnpacker(b []byte, csid uint64) (serverUnpacker ServerUnpacker, username string, err error) 105 }