core

package
v1.16.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LWIP_ERR_OK int = iota
	LWIP_ERR_ABRT
	LWIP_ERR_CONN
	LWIP_ERR_CLSD
)
View Source
const BufSize = 2 * 1024
View Source
const CHECK_TIMEOUTS_INTERVAL = 250 // in millisecond
View Source
const TCP_POLL_INTERVAL = 8 // poll every 4 seconds

Variables

View Source
var ErrClosedPipe = errors.New("io: read/write on closed pipe")

ErrClosedPipe is the error used for read or write operations on a closed pipe.

View Source
var OutputFn func([]byte) (int, error)

Functions

func FreeBytes

func FreeBytes(b []byte)

func NewBytes

func NewBytes(size int) []byte

func NewLWIPError

func NewLWIPError(code int) error

func ParseTCPAddr

func ParseTCPAddr(addr string, port uint16) *net.TCPAddr

func ParseUDPAddr

func ParseUDPAddr(addr string, port uint16) *net.UDPAddr

func Pipe added in v1.6.14

func Pipe() (*PipeReader, *PipeWriter)

Pipe creates a synchronous in-memory pipe. It can be used to connect code expecting an io.Reader with code expecting an io.Writer.

Reads and Writes on the pipe are matched one to one except when multiple Reads are needed to consume a single Write. That is, each Write to the PipeWriter blocks until it has satisfied one or more Reads from the PipeReader that fully consume the written data. The data is copied directly from the Write to the corresponding Read (or Reads); there is no internal buffering.

It is safe to call Read and Write in parallel with each other or with Close. Parallel calls to Read and parallel calls to Write are also safe: the individual calls will be gated sequentially.

func RegisterOutputFn

func RegisterOutputFn(fn func([]byte) (int, error))

func RegisterTCPConnHandler

func RegisterTCPConnHandler(h TCPConnHandler)

func RegisterUDPConnHandler

func RegisterUDPConnHandler(h UDPConnHandler)

func SetBufferPool

func SetBufferPool(p *sync.Pool)

Types

type LWIPStack

type LWIPStack interface {
	Write([]byte) (int, error)
	Close() error
	RestartTimeouts()
}

func NewLWIPStack

func NewLWIPStack() LWIPStack

NewLWIPStack listens for any incoming connections/packets and registers corresponding accept/recv callback functions.

type PipeReader added in v1.6.14

type PipeReader struct {
	// contains filtered or unexported fields
}

A PipeReader is the read half of a pipe.

func (*PipeReader) Close added in v1.6.14

func (r *PipeReader) Close() error

Close closes the reader; subsequent writes to the write half of the pipe will return the error ErrClosedPipe.

func (*PipeReader) CloseWithError added in v1.6.14

func (r *PipeReader) CloseWithError(err error) error

CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeReader) Read added in v1.6.14

func (r *PipeReader) Read(data []byte) (n int, err error)

Read implements the standard Read interface: it reads data from the pipe, blocking until a writer arrives or the write end is closed. If the write end is closed with an error, that error is returned as err; otherwise err is EOF.

type PipeWriter added in v1.6.14

type PipeWriter struct {
	// contains filtered or unexported fields
}

A PipeWriter is the write half of a pipe.

func (*PipeWriter) Close added in v1.6.14

func (w *PipeWriter) Close() error

Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.

func (*PipeWriter) CloseWithError added in v1.6.14

func (w *PipeWriter) CloseWithError(err error) error

CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error err, or EOF if err is nil.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeWriter) Write added in v1.6.14

func (w *PipeWriter) Write(data []byte) (n int, err error)

Write implements the standard Write interface: it writes data to the pipe, blocking until one or more readers have consumed all the data or the read end is closed. If the read end is closed with an error, that err is returned as err; otherwise err is ErrClosedPipe.

type TCPConn

type TCPConn interface {
	// Sent will be called when sent data has been acknowledged by peer.
	Sent(len uint16) error

	// Receive will be called when data arrives from TUN.
	Receive(data []byte) error

	// Err will be called when a fatal error has occurred on the connection.
	// The corresponding pcb is already freed when this callback is called
	Err(err error)

	// LocalClosed will be called when lwIP receives a FIN segment on a
	// connection.
	LocalClosed() error

	// Poll will be periodically called by TCP timers.
	Poll() error

	// RemoteAddr returns the destination network address.
	RemoteAddr() net.Addr

	// LocalAddr returns the local client network address.
	LocalAddr() net.Addr

	// Read reads data comming from TUN, note that it reads from an
	// underlying pipe that the writer writes in the lwip thread,
	// write op blocks until previous written data is consumed, one
	// should read out all data as soon as possible.
	Read(data []byte) (int, error)

	// Write writes data to TUN.
	Write(data []byte) (int, error)

	// Close closes the connection.
	Close() error

	// CloseWrite closes the writing side by sending a FIN
	// segment to local peer. That means we can write no further
	// data to TUN.
	CloseWrite() error

	// CloseRead closes the reading side. That means we can no longer
	// read more from TUN.
	CloseRead() error

	// Abort aborts the connection by sending a RST segment.
	Abort()

	SetDeadline(t time.Time) error
	SetReadDeadline(t time.Time) error
	SetWriteDeadline(t time.Time) error
}

TCPConn abstracts a TCP connection comming from TUN. This connection should be handled by a registered TCP proxy handler. It's important to note that callback members are called from lwIP, they are already in the lwIP thread when they are called, that is, they are holding the lwipMutex.

type TCPConnHandler

type TCPConnHandler interface {
	// Handle handles the conn for target.
	Handle(conn net.Conn, target *net.TCPAddr) error
}

TCPConnHandler handles TCP connections comming from TUN.

type UDPConn

type UDPConn interface {
	// LocalAddr returns the local client network address.
	LocalAddr() *net.UDPAddr

	// ReceiveTo will be called when data arrives from TUN, and the received
	// data should be sent to addr.
	ReceiveTo(data []byte, addr *net.UDPAddr) error

	// WriteFrom writes data to TUN, addr will be set as source address of
	// UDP packets that output to TUN.
	WriteFrom(data []byte, addr *net.UDPAddr) (int, error)

	// Close closes the connection.
	Close() error
}

TCPConn abstracts a UDP connection comming from TUN. This connection should be handled by a registered UDP proxy handler.

type UDPConnHandler

type UDPConnHandler interface {
	// Connect connects the proxy server. Note that target can be nil.
	Connect(conn UDPConn, target *net.UDPAddr) error

	// ReceiveTo will be called when data arrives from TUN.
	ReceiveTo(conn UDPConn, data []byte, addr *net.UDPAddr) error
}

UDPConnHandler handles UDP connections comming from TUN.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL