memnet

package module
v0.0.0-...-823edbe Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2022 License: Apache-2.0 Imports: 12 Imported by: 2

README

Portable in-memory Go "net" package implementations GoDoc

Allows portable and hermetic testing of code which uses the go net package. Useful for all cases where a Unix socket would (or could) be used, but you don't actually need to cross process boundaries. Fully portable and should work even on operating systems without native Unix socket support.

Unbuffered

Simple and light-weight implementation built on net.Pipe. Prefer this implementation if it works for you. Supports Listen, Dial and pairs. Without buffering, there is no difference between stream and message oriented sockets, so there is only one type of unbuffered connection. Unlike net.Pipe, these implement both net.Conn and net.PacketConn.

Buffered

Buffered types emulate Linux's version of Unix sockets and are based on gVisor's Unix socket implementation. These are heavier weight than the unbuffered implementations, but should prevent deadlocks in applications which depend on socket buffering. These should work for all applications which do not depend on IP addresses or ports.

The modifications to gVisor's Unix socket implementation have been intentionally kept minimal, will all the complexity of adapting it to net interfaces in the top-level package.

Need more?

memipnet provides full TCP and UDP loopback emulation.

Documentation

Overview

Package memnet provides portable in-memory userspace "net" package implementations.

Allows portable and hermetic testing and connecting. Useful for all cases where a Unix socket would be used, but you don't actually need to cross process boundaries.

Buffered implementations are based on gVisor's Unix socket implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBufferedPacketConnPair

func NewBufferedPacketConnPair(network string) (*BufferedPacketConn, *BufferedPacketConn, error)

NewBufferedPacketConnPair creates a connected pair (socketpair) of BufferedPacketConns.

Supported networks: "unixgram", "unixpacket".

func NewBufferedPair

func NewBufferedPair(network string) (net.Conn, net.Conn, error)

NewBufferedPair creates a connected pair (socketpair) of buffered in-memory net.Conns.

Supported networks: "unix", "unixgram", "unixpacket".

func NewBufferedStreamConnPair

func NewBufferedStreamConnPair() (*BufferedStreamConn, *BufferedStreamConn)

NewBufferedStreamConnPair creates a connected pair (socketpair) of BufferedStreamConns.

func NewUnbufferedPair

func NewUnbufferedPair() (net.Conn, net.Conn)

NewUnbufferedPair creates a connected pair (socketpair) of unbuffered net.Conns.

The returned values also implement net.PacketConn.

The implementation is inherently message oriented due to the lack of buffering. For each WriteXxx call, there is one opportunity to read the written data. If the buffer provided to ReadXxx is smaller than the buffer provided to the corresponding WriteXxx call, the unreadable data will be silently discarded.

Types

type BufferedListener

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

A BufferedListener is an in-memory Unix socket emulator that implements the net.Listener interface.

func BufferedListen

func BufferedListen(network string, addr *net.UnixAddr) (*BufferedListener, error)

BufferedListen creates a new BufferedListener not in an address namespace.

Supported networks: "unix", "unixpacket".

func (*BufferedListener) Accept

func (l *BufferedListener) Accept() (net.Conn, error)

Accept implements net.Conn.Accept.

func (*BufferedListener) Addr

func (l *BufferedListener) Addr() net.Addr

Addr implements net.Listener.Addr.

func (*BufferedListener) Close

func (l *BufferedListener) Close() error

Close implements net.Listener.Close.

func (*BufferedListener) Dial

func (l *BufferedListener) Dial(laddr *net.UnixAddr) (net.Conn, error)

Dial creates a new net.Conn connected to the listener and optionally bound to laddr.

func (*BufferedListener) DialContext

func (l *BufferedListener) DialContext(ctx context.Context, laddr *net.UnixAddr) (net.Conn, error)

DialContext creates a new net.Conn connected to the listener and optionally bound to laddr with the option of adding cancellation and timeouts.

func (*BufferedListener) Shutdown

func (l *BufferedListener) Shutdown()

Shutdown stops the listener.

type BufferedPacketConn

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

A BufferedPacketConn is an in-memory Unix dgram/seqpacket socket emulator that implements the net.Conn and net.PacketConn interfaces.

Effectively equivalent to the "unixgram" and "unixpacket" networks (SOCK_DGRAM and SOCK_SEQPACKET socket types respectively), message boundaries are not preserved, but bytes are transferred reliably and in the order that they were sent. ReadXxx calls will read the next available bytes and any remaining unread bytes will be available to the next ReadXxx call.

func (*BufferedPacketConn) Close

func (c *BufferedPacketConn) Close() error

Close implements net.PacketConn.Close.

func (*BufferedPacketConn) CloseRead

func (c *BufferedPacketConn) CloseRead() error

CloseRead shuts down the reading side of the connection. Most callers should just use Close.

A Half-Close is performed the same as CloseRead for *net.UnixConn.

func (*BufferedPacketConn) CloseWrite

func (c *BufferedPacketConn) CloseWrite() error

CloseWrite shuts down the writing side of the connection. Most callers should just use Close.

A Half-Close is performed the same as CloseWrite for *net.UnixConn.

func (*BufferedPacketConn) LocalAddr

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

LocalAddr implements net.Conn.LocalAddr.

func (*BufferedPacketConn) Read

func (c *BufferedPacketConn) Read(b []byte) (int, error)

Read implements net.Conn.Read.

func (*BufferedPacketConn) ReadFrom

func (c *BufferedPacketConn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom implements net.PacketConn.ReadFrom.

func (*BufferedPacketConn) RemoteAddr

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

RemoteAddr implements net.Conn.RemoteAddr.

func (*BufferedPacketConn) SetDeadline

func (d *BufferedPacketConn) SetDeadline(t time.Time) error

SetDeadline implements net.Conn.SetDeadline and net.PacketConn.SetDeadline.

func (*BufferedPacketConn) SetReadDeadline

func (d *BufferedPacketConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn.SetReadDeadline and net.PacketConn.SetReadDeadline.

func (*BufferedPacketConn) SetWriteDeadline

func (d *BufferedPacketConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn.SetWriteDeadline and net.PacketConn.SetWriteDeadline.

func (*BufferedPacketConn) Write

func (c *BufferedPacketConn) Write(b []byte) (int, error)

Write implements net.Conn.Write.

func (*BufferedPacketConn) WriteTo

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

WriteTo implements net.PacketConn.WriteTo.

type BufferedStreamConn

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

A BufferedStreamConn is an in-memory Unix stream socket emulator that implements the net.Conn interface.

Emulates the "unix" network in the net package (SOCK_STREAM), message boundaries are not preserved, but bytes are transferred reliably and in the order that they were sent. ReadXxx calls will read the next available bytes and any remaining unread bytes will be available to the next ReadXxx call.

func (*BufferedStreamConn) Close

func (c *BufferedStreamConn) Close() error

Close implements net.Conn.Close.

func (*BufferedStreamConn) CloseRead

func (c *BufferedStreamConn) CloseRead() error

CloseRead shuts down the reading side of the connection. Most callers should just use Close.

A Half-Close is performed the same as CloseRead for *net.UnixConn.

func (*BufferedStreamConn) CloseWrite

func (c *BufferedStreamConn) CloseWrite() error

CloseWrite shuts down the writing side of the connection. Most callers should just use Close.

A Half-Close is performed the same as CloseWrite for *net.UnixConn.

func (*BufferedStreamConn) LocalAddr

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

LocalAddr implements net.Conn.LocalAddr.

func (*BufferedStreamConn) Read

func (c *BufferedStreamConn) Read(b []byte) (int, error)

Read implements net.Conn.Read.

func (*BufferedStreamConn) RemoteAddr

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

RemoteAddr implements net.Conn.RemoteAddr.

func (*BufferedStreamConn) SetDeadline

func (d *BufferedStreamConn) SetDeadline(t time.Time) error

SetDeadline implements net.Conn.SetDeadline and net.PacketConn.SetDeadline.

func (*BufferedStreamConn) SetReadDeadline

func (d *BufferedStreamConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn.SetReadDeadline and net.PacketConn.SetReadDeadline.

func (*BufferedStreamConn) SetWriteDeadline

func (d *BufferedStreamConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn.SetWriteDeadline and net.PacketConn.SetWriteDeadline.

func (*BufferedStreamConn) Write

func (c *BufferedStreamConn) Write(b []byte) (int, error)

Write implements net.Conn.Write.

type Unbuffered

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

Unbuffered is an unbuffered net.Listener implemented in memory in userspace.

Lighter weight, but not compatible with all applications. Creates unbuffered net.Conns.

func NewUnbuffered

func NewUnbuffered() *Unbuffered

NewUnbuffered creates a new unbuffered listener.

func (*Unbuffered) Accept

func (u *Unbuffered) Accept() (net.Conn, error)

Accept implements net.Listener.Accept.

func (*Unbuffered) Addr

func (u *Unbuffered) Addr() net.Addr

Addr implements net.Listener.Addr.

func (*Unbuffered) Close

func (u *Unbuffered) Close() error

Close implements net.Listener.Close.

func (*Unbuffered) Dial

func (u *Unbuffered) Dial() (net.Conn, error)

Dial creates a new unbuffered connection with no timeout.

func (*Unbuffered) DialContext

func (u *Unbuffered) DialContext(ctx context.Context) (net.Conn, error)

DialContext creates a new unbuffered connection with a context.

Directories

Path Synopsis
Package unix contains the implementation of Unix endpoints.
Package unix contains the implementation of Unix endpoints.
linux
Package linux provides Linux constants.
Package linux provides Linux constants.
refs
Package refs defines an interface for reference counted objects.
Package refs defines an interface for reference counted objects.
syserr
Package syserr contains sandbox-internal errors.
Package syserr contains sandbox-internal errors.
tcpip
Package tcpip provides the interfaces and related types that users of the tcpip stack will use in order to create endpoints used to send and receive data over the network stack.
Package tcpip provides the interfaces and related types that users of the tcpip stack will use in order to create endpoints used to send and receive data over the network stack.
tcpip/buffer
Package buffer provides the implementation of a buffer view.
Package buffer provides the implementation of a buffer view.
waiter
Package waiter provides the implementation of a wait queue, where waiters can be enqueued to be notified when an event of interest happens.
Package waiter provides the implementation of a wait queue, where waiters can be enqueued to be notified when an event of interest happens.

Jump to

Keyboard shortcuts

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