netstack

package
v0.0.0-...-90ffb0e Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EADDRNOTAVAIL is the address not available error.
	EADDRNOTAVAIL = unix.EADDRNOTAVAIL

	// EADDRINUSE is the address in use error.
	EADDRINUSE = unix.EADDRINUSE

	// ECONNABORTED is the connection aborted error.
	ECONNABORTED = unix.ECONNABORTED

	// ECONNREFUSED is the connection refused error.
	ECONNREFUSED = unix.ECONNREFUSED

	// ECONNRESET is the connection reset by peer error.
	ECONNRESET = unix.ECONNRESET

	// EHOSTUNREACH is the host unreachable error.
	EHOSTUNREACH = unix.EHOSTUNREACH

	// EINVAL is the invalid argument error.
	EINVAL = unix.EINVAL

	// ENETDOWN is the network is down error.
	ENETDOWN = unix.ENETDOWN

	// ENOBUFS is the no buffer space available error.
	ENOBUFS = unix.ENOBUFS

	// ENOTCONN is the not connected error.
	ENOTCONN = unix.ENOTCONN

	// EPROTONOSUPPORT is the protocol not supported error.
	EPROTONOSUPPORT = unix.EPROTONOSUPPORT
)
View Source
const (
	IPProtocolTCP = packet.IPProtocolTCP
	IPProtocolUDP = packet.IPProtocolUDP

	TCPFlagFIN = packet.TCPFlagFIN
	TCPFlagSYN = packet.TCPFlagSYN
	TCPFlagRST = packet.TCPFlagRST
	TCPFlagPSH = packet.TCPFlagPSH
	TCPFlagACK = packet.TCPFlagACK
)

Constant aliases

Variables

View Source
var ErrNoConfiguredResolvers = errors.New("no configured resolvers")

ErrNoConfiguredResolvers is returned when there are no configured resolvers.

Functions

This section is empty.

Types

type Addr

type Addr struct {
	// AddrPort is the endpoint address and port.
	AddrPort netip.AddrPort

	// Protocol is the endpoint protocol.
	Protocol IPProtocol
}

Addr represents a TCP/UDP address.

func (*Addr) Network

func (sa *Addr) Network() string

Network implements net.Addr.

func (*Addr) String

func (sa *Addr) String() string

String implements net.Addr.

type IPProtocol

type IPProtocol = packet.IPProtocol

Type aliases

type Packet

type Packet = packet.Packet

Type aliases

type Port

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

Port models an open TCP/UDP port.

func NewPort

func NewPort(stack PortStack, addr *PortAddr) *Port

NewPort creates a *Port instance with the given *PortAddr.

Leave the *PortAddr `RemoteAddr` field zero when you want to create a port that is not connected to a peer (i.e., a TCP/UDP listener).

func (*Port) Close

func (up *Port) Close() error

Close closes the *Port terminating any pending I/O.

func (*Port) Input

func (gp *Port) Input() chan<- *Packet

Input returns the channel to write to send a *Packet to the *Port.

func (*Port) LocalAddr

func (gp *Port) LocalAddr() net.Addr

LocalAddr returns the local address of this *Port.

func (*Port) Output

func (gp *Port) Output() <-chan *Packet

Output returns the channel to read to recv a *Packet from the *Port.

func (*Port) ReadFrom

func (gp *Port) ReadFrom(buf []byte) (int, net.Addr, error)

ReadFrom implements net.PacketConn.

func (*Port) ReadPacket

func (gp *Port) ReadPacket() (*Packet, error)

ReadPacket receives a packet from a remote endpoint.

We discard packets that do not match the remote address unless the remote address is not set, in which case we accept all packets.

The following errors are possible:

1. nil if we receive a packet from the `Input` channel.

2. net.ErrClosed if the port is closed before we receive a packet;

3. os.ErrDeadlineExceeded if the read deadline is exceeded.

func (*Port) RemoteAddr

func (gp *Port) RemoteAddr() net.Addr

RemoteAddr returns the remote address of this *Port.

func (*Port) SetDeadline

func (gp *Port) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines.

func (*Port) SetReadDeadline

func (gp *Port) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline.

func (*Port) SetWriteDeadline

func (gp *Port) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline.

func (*Port) Write

func (gp *Port) Write(data []byte) (int, error)

Write implements net.Conn.

func (*Port) WritePacket

func (gp *Port) WritePacket(payload []byte, flags TCPFlags, raddr netip.AddrPort) error

WritePacket creates and writes a packet to a remote endpoint using the given payload, TCP flags, and remote address.

If the `raddr` field is a zero value, we use the `RemoteAddr` field of the *PortAddr. If also such a field is a zero value, we return ENOTCONN to indicate we don't know the peer addr.

Also, we copy the payload to avoid issues with buffer pools, which occur, for example, when using the crypto/tls package.

The following errors are possible:

1. ENOTCONN if the port is not connected to a peer and the raddr is zero;

2. nil if the packet is sent (i.e., delivered to the `Output` channel);

3. net.ErrClosed if the port is closed before we send the packet;

4. os.ErrDeadlineExceeded if the write deadline is exceeded.

func (*Port) WriteTo

func (gp *Port) WriteTo(pkt []byte, addr net.Addr) (int, error)

WriteTo implements net.PacketConn.

type PortAddr

type PortAddr struct {
	// LocalAddr is the local address. This field must
	// always have valid address and port.
	LocalAddr netip.AddrPort

	// Protocol is the port protocol.
	Protocol IPProtocol

	// RemoteAddr is the remote address. This field
	// may be zero for non-connected ports.
	RemoteAddr netip.AddrPort
}

PortAddr is the *Port address.

func (*PortAddr) String

func (pa *PortAddr) String() string

String returns the string representation of the *PortAddr.

type PortStack

type PortStack interface {
	// ClosePort closes the given port.
	ClosePort(addr *PortAddr)

	// FindLocalAddrFor finds the first local address with the
	// same family of the given remote address.
	FindLocalAddrFor(raddr netip.Addr) (netip.Addr, error)
}

PortStack is the stack to which a *Port is attached.

type Stack

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

Stack models a network stack.

func New

func New(addrs ...netip.Addr) *Stack

New creates a new *Stack instance and starts a goroutine demuxing incoming traffic. Remember to invoke Close to stop any muxing/demuxing goroutine.

func (*Stack) Addresses

func (ns *Stack) Addresses() []netip.Addr

Addresses returns the network stack addresses.

func (*Stack) Close

func (ns *Stack) Close() error

Close closes the network stack and stops all traffic muxing/demuxing.

func (*Stack) ClosePort

func (ns *Stack) ClosePort(addr *PortAddr)

ClosePort implements PortStack.

func (*Stack) DialContext

func (ns *Stack) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext dials a network address.

func (*Stack) EOF

func (ns *Stack) EOF() <-chan struct{}

EOF returns the channel to wait for the stack to close.

func (*Stack) FindLocalAddrFor

func (ns *Stack) FindLocalAddrFor(raddr netip.Addr) (netip.Addr, error)

FindLocalAddrFor returns the first local address that has the same IP version as the remote address.

func (*Stack) Input

func (ns *Stack) Input() chan<- *Packet

Input returns the channel where to write incoming packets.

func (*Stack) Listen

func (ns *Stack) Listen(ctx context.Context, network, address string) (net.Listener, error)

Listen creates a new listening net.Listener.

func (*Stack) ListenPacket

func (ns *Stack) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error)

ListenPacket creates a new listening net.PacketConn.

func (*Stack) NewTCPConn

func (ns *Stack) NewTCPConn(laddr, raddr netip.AddrPort) (*TCPConn, error)

NewTCPConn implements TCPListenerStack.

func (*Stack) Output

func (ns *Stack) Output() <-chan *Packet

Output returns the channel from which to read outgoing packets.

func (*Stack) SetResolvers

func (ns *Stack) SetResolvers(addrs ...netip.AddrPort)

SetResolvers sets the resolvers endpoints to use.

Note that this method IS NOT goroutine safe.

type TCPConn

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

TCPConn is a TCP connection.

The zero value is invalid; construct using NewTCPConn.

func NewTCPConn

func NewTCPConn(p *Port) *TCPConn

NewTCPConn creates a new TCP connection.

func (*TCPConn) Accept

func (c *TCPConn) Accept() (err error)

Accept responds to the incoming SYN with SYN|ACK.

func (*TCPConn) Close

func (c *TCPConn) Close() error

Close implements net.Conn.

func (*TCPConn) Connect

func (c *TCPConn) Connect(ctx context.Context) (err error)

Connect perform the three-way handshake.

func (*TCPConn) LocalAddr

func (c *TCPConn) LocalAddr() net.Addr

LocalAddr implements net.Conn.

func (*TCPConn) Read

func (c *TCPConn) Read(buf []byte) (int, error)

Read implements net.Conn.

func (*TCPConn) RemoteAddr

func (c *TCPConn) RemoteAddr() net.Addr

RemoteAddr implements net.Conn.

func (*TCPConn) SetDeadline

func (c *TCPConn) SetDeadline(t time.Time) error

SetDeadline implements net.Conn.

func (*TCPConn) SetReadDeadline

func (c *TCPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn.

func (*TCPConn) SetWriteDeadline

func (c *TCPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn.

func (*TCPConn) Write

func (c *TCPConn) Write(data []byte) (int, error)

Write implements net.Conn.

type TCPFlags

type TCPFlags = packet.TCPFlags

Type aliases

type TCPListener

type TCPListener struct {
	*Port
	// contains filtered or unexported fields
}

TCPListener is a TCP listener.

The zero value is invalid; construct using NewTCPListener.

func NewTCPListener

func NewTCPListener(stack TCPListenerStack, p *Port) *TCPListener

NewTCPListener creates a new TCP connection.

func (*TCPListener) Accept

func (tl *TCPListener) Accept() (net.Conn, error)

Accept implements net.Listener.

func (*TCPListener) Addr

func (tl *TCPListener) Addr() net.Addr

Addr implements net.Listener.

type TCPListenerStack

type TCPListenerStack interface {
	NewTCPConn(laddr, raddr netip.AddrPort) (*TCPConn, error)
}

TCPListenerStack is the stack to which a *TCPListener is attached.

type UDPConn

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

UDPConn is a UDP connection.

The zero value is invalid; construct using NewUDPConn.

func NewUDPConn

func NewUDPConn(p *Port) *UDPConn

NewUDPConn creates a new UDP connection.

func (*UDPConn) Close

func (c *UDPConn) Close() error

Close implements net.PacketConn.

func (*UDPConn) LocalAddr

func (c *UDPConn) LocalAddr() net.Addr

LocalAddr implements net.PacketConn.

func (*UDPConn) Read

func (c *UDPConn) Read(buf []byte) (int, error)

Read implements net.Conn.

func (*UDPConn) ReadFrom

func (c *UDPConn) ReadFrom(buf []byte) (int, net.Addr, error)

ReadFrom implements net.PacketConn.

func (*UDPConn) RemoteAddr

func (c *UDPConn) RemoteAddr() net.Addr

RemoteAddr implements net.Conn.

func (*UDPConn) SetDeadline

func (c *UDPConn) SetDeadline(t time.Time) error

SetDeadline implements net.PacketConn.

func (*UDPConn) SetReadDeadline

func (c *UDPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.PacketConn.

func (*UDPConn) SetWriteDeadline

func (c *UDPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.PacketConn.

func (*UDPConn) Write

func (c *UDPConn) Write(data []byte) (int, error)

Write implements net.Conn.

func (*UDPConn) WriteTo

func (c *UDPConn) WriteTo(pkt []byte, addr net.Addr) (int, error)

WriteTo implements net.PacketConn.

Jump to

Keyboard shortcuts

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