github.com/database64128/shadowsocks-go@v1.7.0/zerocopy/udp.go (about)

     1  package zerocopy
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/database64128/tfo-go/v2"
     7  )
     8  
     9  // UDPClientInfo contains information about a UDP client.
    10  type UDPClientInfo struct {
    11  	// Name is the name of the UDP client.
    12  	Name string
    13  
    14  	// PackerHeadroom is the headroom required by the packet packer.
    15  	PackerHeadroom Headroom
    16  
    17  	// MaxPacketSize is the maximum size of outgoing packets.
    18  	MaxPacketSize int
    19  
    20  	// ListenConfig is the [tfo.ListenConfig] for opening client sockets.
    21  	ListenConfig tfo.ListenConfig
    22  }
    23  
    24  // UDPClient stores information for creating new client sessions.
    25  type UDPClient interface {
    26  	// Info returns information about the client.
    27  	Info() UDPClientInfo
    28  
    29  	// NewSession creates a new session and returns the client info,
    30  	// the packet packer and unpacker for the session, or an error.
    31  	NewSession() (UDPClientInfo, ClientPacker, ClientUnpacker, error)
    32  }
    33  
    34  // UDPNATServerInfo contains information about a UDP NAT server.
    35  type UDPNATServerInfo struct {
    36  	// UnpackerHeadroom is the headroom required by the packet unpacker.
    37  	UnpackerHeadroom Headroom
    38  }
    39  
    40  // UDPNATServer stores information for creating new server sessions.
    41  type UDPNATServer interface {
    42  	// Info returns information about the server.
    43  	Info() UDPNATServerInfo
    44  
    45  	// NewSession creates a new session and returns the packet packer
    46  	// and unpacker for the session, or an error.
    47  	NewSession() (ServerPacker, ServerUnpacker, error)
    48  }
    49  
    50  // UDPSessionServerInfo contains information about a UDP session server.
    51  type UDPSessionServerInfo struct {
    52  	// UnpackerHeadroom is the headroom required by the packet unpacker.
    53  	UnpackerHeadroom Headroom
    54  }
    55  
    56  // UDPSessionServer deals with incoming sessions.
    57  type UDPSessionServer interface {
    58  	sync.Locker
    59  
    60  	// Info returns information about the server.
    61  	Info() UDPSessionServerInfo
    62  
    63  	// SessionInfo extracts session ID from a received packet b.
    64  	//
    65  	// The returned session ID is then used by the caller to look up the session table.
    66  	// If no matching entries were found, NewUnpacker should be called to create a new
    67  	// packet unpacker for the packet.
    68  	SessionInfo(b []byte) (csid uint64, err error)
    69  
    70  	// NewUnpacker creates a new packet unpacker for the specified client session.
    71  	//
    72  	// The returned unpacker is then used by the caller to unpack the incoming packet.
    73  	// Upon successful unpacking, the unpacker's NewPacker method can be called to create
    74  	// a corresponding server session.
    75  	NewUnpacker(b []byte, csid uint64) (sessionServerUnpacker SessionServerUnpacker, username string, err error)
    76  }
    77  
    78  // SimpleUDPClient wraps a PackUnpacker and uses it for all sessions.
    79  //
    80  // SimpleUDPClient implements the UDPClient interface.
    81  type SimpleUDPClient struct {
    82  	info     UDPClientInfo
    83  	packer   ClientPacker
    84  	unpacker ClientUnpacker
    85  }
    86  
    87  // NewSimpleUDPClient wraps a PackUnpacker into a UDPClient and uses it for all sessions.
    88  func NewSimpleUDPClient(name string, maxPacketSize int, listenConfig tfo.ListenConfig, packer ClientPacker, unpacker ClientUnpacker) *SimpleUDPClient {
    89  	return &SimpleUDPClient{
    90  		info: UDPClientInfo{
    91  			Name:           name,
    92  			PackerHeadroom: packer.ClientPackerInfo().Headroom,
    93  			MaxPacketSize:  maxPacketSize,
    94  			ListenConfig:   listenConfig,
    95  		},
    96  		packer:   packer,
    97  		unpacker: unpacker,
    98  	}
    99  }
   100  
   101  // Info implements the UDPClient Info method.
   102  func (c *SimpleUDPClient) Info() UDPClientInfo {
   103  	return c.info
   104  }
   105  
   106  // NewSession implements the UDPClient NewSession method.
   107  func (c *SimpleUDPClient) NewSession() (UDPClientInfo, ClientPacker, ClientUnpacker, error) {
   108  	return c.info, c.packer, c.unpacker, nil
   109  }