net

package
v3.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 16 Imported by: 22 modules, 23 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrListenerIsClosed   = io.EOF
	ErrConnectionIsClosed = io.EOF
	ErrWriteInterrupted   = errors.New("only part data was written to socket")
	// ErrWriteTimeout identifies expiration of the configured server write timeout.
	ErrWriteTimeout = writecontext.ErrWriteTimeout
)
View Source
var DefaultUDPConnConfig = UDPConnConfig{
	Errors: func(error) {

	},
}

Functions

func IsCancelOrCloseError

func IsCancelOrCloseError(err error) bool

func IsConnectionBrokenError

func IsConnectionBrokenError(err error) bool

Check if error returned by operation on a socket failed because the other side has closed the connection.

func IsIPv6

func IsIPv6(addr net.IP) bool

IsIPv6 return's true if addr is IPV6.

func IsMessageSizeError added in v3.5.7

func IsMessageSizeError(err error) bool

IsMessageSizeError reports whether a socket operation failed because a datagram exceeded the buffer or network limit.

Types

type Conn

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

Conn is a network connection that provides Read/Write with context.

Multiple goroutines may invoke methods on a Conn simultaneously.

func NewConn

func NewConn(c net.Conn) *Conn

NewConn creates a connection over net.Conn. Pion DTLS connections retain datagram write handling. Asynchronous close errors are not reported; use NewConnWithErrors to receive them.

func NewConnWithErrors added in v3.9.2

func NewConnWithErrors(c net.Conn, onError func(error)) *Conn

NewConnWithErrors creates a connection and reports asynchronous close errors through onError. A nil callback disables error reporting.

func NewDTLSClientConn added in v3.9.2

func NewDTLSClientConn(c net.Conn, onError func(error)) *Conn

NewDTLSClientConn creates a connection that keeps the DTLS client open when a write is canceled or times out. Later writes wait for the interrupted write.

func NewDTLSServerConn added in v3.9.2

func NewDTLSServerConn(c net.Conn, onError func(error)) *Conn

NewDTLSServerConn creates a connection that closes after a DTLS write is interrupted by cancellation or timeout.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) LocalAddr

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

LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.

func (*Conn) NetConn

func (c *Conn) NetConn() net.Conn

NetConn returns the underlying connection that is wrapped by c. The Conn returned is shared by all invocations of Connection, so do not modify it.

func (*Conn) ReadFullWithContext

func (c *Conn) ReadFullWithContext(ctx context.Context, buffer []byte) error

ReadFullWithContext reads stream with context until whole buffer is satisfied.

func (*Conn) ReadWithContext

func (c *Conn) ReadWithContext(ctx context.Context, buffer []byte) (int, error)

ReadWithContext reads stream with context.

func (*Conn) RemoteAddr

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

RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.

func (*Conn) WriteWithContext

func (c *Conn) WriteWithContext(ctx context.Context, data []byte) error

WriteWithContext writes data with context. A failure after any bytes have been written closes the connection to prevent reuse of an incomplete frame. Writes are synchronous: cancellation interrupts them through the connection write deadline where the transport honors one, and the call waits for the underlying write to finish. A started DTLS server write that fails with a timeout or cancellation also closes the connection; DTLS client connections created with NewDTLSClientConn remain open on cancellation or timeout.

type ContextOption added in v3.2.0

type ContextOption struct {
	Ctx context.Context
}

func WithContext added in v3.2.0

func WithContext(ctx context.Context) ContextOption

WithContext sets the context of operation.

func (ContextOption) ApplyRead added in v3.2.0

func (o ContextOption) ApplyRead(cfg *UDPReadCfg)

func (ContextOption) ApplyWrite added in v3.2.0

func (o ContextOption) ApplyWrite(cfg *UDPWriteCfg)

type ControlMessage

type ControlMessage struct {
	Dst     net.IP // destination address of the packet
	Src     net.IP // source address of the packet
	IfIndex int    // interface index, 0 means any interface
}

func (*ControlMessage) GetIfIndex added in v3.2.0

func (c *ControlMessage) GetIfIndex() int

GetIfIndex returns the interface index of the network interface. 0 means no interface index specified.

func (*ControlMessage) String added in v3.2.0

func (c *ControlMessage) String() string

type DTLSAcceptError added in v3.9.2

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

DTLSAcceptError identifies an accept error returned while the DTLS listener was open. Its cause is available through Unwrap.

func (*DTLSAcceptError) Error added in v3.9.2

func (e *DTLSAcceptError) Error() string

func (*DTLSAcceptError) Unwrap added in v3.9.2

func (e *DTLSAcceptError) Unwrap() error

type DTLSListener

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

DTLSListener is a DTLS listener that provides accept with context.

func NewDTLSListener

func NewDTLSListener[T DTLSServerConfig](network, addr string, cfg T) (*DTLSListener, error)

NewDTLSListener creates a DTLS listener. Known networks are "udp", "udp4" (IPv4-only), "udp6" (IPv6-only). cfg accepts either a *dtls.Config (backward-compatible legacy path) or a DTLSServerOptions value built with NewDTLSServerOptions (recommended).

func (*DTLSListener) Accept

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

Accept waits for a generic Conn.

func (*DTLSListener) AcceptWithContext

func (l *DTLSListener) AcceptWithContext(ctx context.Context) (net.Conn, error)

AcceptWithContext waits with context for a generic Conn. An error that matches ErrListenerIsClosed while the listener is open is returned as a DTLSAcceptError, preserving its original error chain.

func (*DTLSListener) Addr

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

Addr represents a network end point address.

func (*DTLSListener) Close

func (l *DTLSListener) Close() error

Close closes the connection.

type DTLSServerConfig added in v3.5.0

type DTLSServerConfig interface {
	*piondtls.Config | DTLSServerOptions
}

DTLSServerConfig is a type constraint accepted by NewDTLSListener, ListenAndServeDTLS and ListenAndServeDTLSWithOptions. It allows callers to pass either the legacy *piondtls.Config (backward-compatible) or the recommended DTLSServerOptions wrapper (built via NewDTLSServerOptions).

type DTLSServerOptions added in v3.5.0

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

DTLSServerOptions holds DTLS server-side configuration options for use with NewDTLSListener. It wraps the options-based API of pion/dtls, keeping pion/dtls option types out of go-coap function signatures.

func NewDTLSServerOptions added in v3.5.0

func NewDTLSServerOptions(opts ...piondtls.ServerOption) DTLSServerOptions

NewDTLSServerOptions creates a DTLSServerOptions from the provided pion/dtls ServerOption values (e.g. piondtls.WithPSK, piondtls.WithCertificates, …).

Most pion/dtls options implement the shared piondtls.Option interface, which satisfies both ServerOption and ClientOption, so they can be passed here directly. Server-only options such as piondtls.WithClientAuth are also accepted.

type ErrorsOpt

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

func WithErrors

func WithErrors(v func(err error)) ErrorsOpt

func (ErrorsOpt) ApplyUDP

func (h ErrorsOpt) ApplyUDP(o *UDPConnConfig)

type InterfaceError

type InterfaceError = func(iface *net.Interface, err error)

type MulticastHoplimitOpt

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

type MulticastInterfaceErrorOpt

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

type MulticastInterfaceMode

type MulticastInterfaceMode int
const (
	MulticastAllInterface      MulticastInterfaceMode = 0
	MulticastAnyInterface      MulticastInterfaceMode = 1
	MulticastSpecificInterface MulticastInterfaceMode = 2
)

type MulticastInterfaceModeOpt

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

type MulticastInterfaceOpt

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

type MulticastOption

type MulticastOption interface {
	// contains filtered or unexported methods
}

A MulticastOption sets options such as hop limit, etc.

func WithAllMulticastInterface

func WithAllMulticastInterface() MulticastOption

func WithAnyMulticastInterface

func WithAnyMulticastInterface() MulticastOption

func WithMulticastHoplimit

func WithMulticastHoplimit(hoplimit int) MulticastOption

func WithMulticastInterface

func WithMulticastInterface(iface net.Interface) MulticastOption

func WithMulticastInterfaceError

func WithMulticastInterfaceError(interfaceError InterfaceError) MulticastOption

WithMulticastInterfaceError sets the callback for interface errors. Callbacks run after socket writes finish. Interface errors are not returned by WriteMulticast when a callback is set, but context cancellation and deadline errors are returned.

func WithMulticastSource

func WithMulticastSource(source net.IP) MulticastOption

type MulticastOptions

type MulticastOptions struct {
	IFaceMode      MulticastInterfaceMode
	Iface          *net.Interface
	Source         *net.IP
	HopLimit       int
	InterfaceError InterfaceError
}

func DefaultMulticastOptions

func DefaultMulticastOptions() MulticastOptions

func (*MulticastOptions) Apply

func (m *MulticastOptions) Apply(o MulticastOption)

type MulticastSourceOpt

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

type ReadWriteOptionHandler added in v3.2.0

type ReadWriteOptionHandler[F UDPWriteApplyFunc | UDPReadApplyFunc] struct {
	Func F
}

func (ReadWriteOptionHandler[F]) ApplyRead added in v3.2.0

func (o ReadWriteOptionHandler[F]) ApplyRead(cfg *UDPReadCfg)

func (ReadWriteOptionHandler[F]) ApplyWrite added in v3.2.0

func (o ReadWriteOptionHandler[F]) ApplyWrite(cfg *UDPWriteCfg)

type TCPListener

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

TCPListener is a TCP network listener that provides accept with context.

func NewTCPListener

func NewTCPListener(network string, addr string) (*TCPListener, error)

NewTCPListener creates tcp listener. Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only).

func (*TCPListener) Accept

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

Accept waits for a generic Conn.

func (*TCPListener) AcceptWithContext

func (l *TCPListener) AcceptWithContext(ctx context.Context) (net.Conn, error)

AcceptWithContext waits with context for a generic Conn.

func (*TCPListener) Addr

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

Addr represents a network end point address.

func (*TCPListener) Close

func (l *TCPListener) Close() error

Close closes the connection.

type TLSListener

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

TLSListener is a TLS listener that provides accept with context.

func NewTLSListener

func NewTLSListener(network string, addr string, tlsCfg *tls.Config) (*TLSListener, error)

NewTLSListener creates tcp listener. Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only).

func (*TLSListener) Accept

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

Accept waits for a generic Conn.

func (*TLSListener) AcceptWithContext

func (l *TLSListener) AcceptWithContext(ctx context.Context) (net.Conn, error)

AcceptWithContext waits with context for a generic Conn.

func (*TLSListener) Addr

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

Addr represents a network end point address.

func (*TLSListener) Close

func (l *TLSListener) Close() error

Close closes the connection.

type UDPConn

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

UDPConn is a udp connection provides Read/Write with context.

Multiple goroutines may invoke methods on a UDPConn simultaneously. Writes are serialized to coordinate context deadlines. The underlying socket must not have its write deadline changed or be wrapped by another UDPConn.

func NewListenUDP

func NewListenUDP(network, addr string, opts ...UDPOption) (*UDPConn, error)

func NewUDPConn

func NewUDPConn(network string, c *net.UDPConn, opts ...UDPOption) *UDPConn

NewUDPConn creates connection over net.UDPConn.

func (*UDPConn) Close

func (c *UDPConn) Close() error

Close closes the connection.

func (*UDPConn) JoinGroup

func (c *UDPConn) JoinGroup(ifi *net.Interface, group net.Addr) error

JoinGroup joins the group address group on the interface ifi. By default all sources that can cast data to group are accepted. It's possible to mute and unmute data transmission from a specific source by using ExcludeSourceSpecificGroup and IncludeSourceSpecificGroup. JoinGroup uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.

func (*UDPConn) LeaveGroup

func (c *UDPConn) LeaveGroup(ifi *net.Interface, group net.Addr) error

LeaveGroup leaves the group address group on the interface ifi regardless of whether the group is any-source group or source-specific group.

func (*UDPConn) LocalAddr

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

LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.

func (*UDPConn) NetConn

func (c *UDPConn) NetConn() net.Conn

NetConn returns the underlying connection that is wrapped by c. The Conn returned is shared by all invocations of NetConn, so do not modify it.

func (*UDPConn) Network

func (c *UDPConn) Network() string

Network name of the network (for example, udp4, udp6, udp)

func (*UDPConn) ReadWithContext

func (c *UDPConn) ReadWithContext(ctx context.Context, buffer []byte) (int, *net.UDPAddr, error)

ReadWithContext reads packet with context.

func (*UDPConn) ReadWithOptions added in v3.2.0

func (c *UDPConn) ReadWithOptions(buffer []byte, opts ...UDPReadOption) (int, error)

ReadWithOptions reads packet with options. Via opts you can get also the remote address and control message.

func (*UDPConn) RemoteAddr

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

RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.

func (*UDPConn) SetMulticastLoopback

func (c *UDPConn) SetMulticastLoopback(on bool) error

SetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.

func (*UDPConn) WriteMulticast

func (c *UDPConn) WriteMulticast(ctx context.Context, raddr *net.UDPAddr, buffer []byte, opts ...MulticastOption) error

WriteMulticast sends multicast to the remote multicast address. By default it is sent over all network interfaces and all compatible source IP addresses with hop limit 1. Via opts you can specify the network interface, source IP address, and hop limit.

func (*UDPConn) WriteWithContext

func (c *UDPConn) WriteWithContext(ctx context.Context, raddr *net.UDPAddr, buffer []byte) error

WriteWithContext writes data with context.

func (*UDPConn) WriteWithOptions added in v3.2.0

func (c *UDPConn) WriteWithOptions(buffer []byte, opts ...UDPWriteOption) error

WriteWithOptions writes data with options. Via opts you can specify the remote address and control message.

type UDPConnConfig

type UDPConnConfig struct {
	Errors func(err error)
}

type UDPOption

type UDPOption interface {
	ApplyUDP(*UDPConnConfig)
}

A UDPOption sets options such as errors parameters, etc.

type UDPReadApplyFunc added in v3.2.0

type UDPReadApplyFunc func(cfg *UDPReadCfg)

type UDPReadCfg added in v3.2.0

type UDPReadCfg struct {
	Ctx            context.Context
	RemoteAddr     **net.UDPAddr
	ControlMessage **ControlMessage
}

func (*UDPReadCfg) ApplyRead added in v3.2.0

func (c *UDPReadCfg) ApplyRead(cfg *UDPReadCfg)

type UDPReadOption added in v3.2.0

type UDPReadOption interface {
	ApplyRead(cfg *UDPReadCfg)
}

func WithGetControlMessage added in v3.2.0

func WithGetControlMessage(cm **ControlMessage) UDPReadOption

WithGetControlMessage fills the control message when reading succeeds.

func WithGetRemoteAddr added in v3.2.0

func WithGetRemoteAddr(raddr **net.UDPAddr) UDPReadOption

WithGetRemoteAddr fills the remote address when reading succeeds.

type UDPWriteApplyFunc added in v3.2.0

type UDPWriteApplyFunc func(cfg *UDPWriteCfg)

type UDPWriteCfg added in v3.2.0

type UDPWriteCfg struct {
	Ctx            context.Context
	RemoteAddr     *net.UDPAddr
	ControlMessage *ControlMessage
}

func (*UDPWriteCfg) ApplyWrite added in v3.2.0

func (c *UDPWriteCfg) ApplyWrite(cfg *UDPWriteCfg)

type UDPWriteOption added in v3.2.0

type UDPWriteOption interface {
	ApplyWrite(cfg *UDPWriteCfg)
}

func WithControlMessage added in v3.2.0

func WithControlMessage(cm *ControlMessage) UDPWriteOption

WithControlMessage sets the control message to packet.

func WithRemoteAddr added in v3.2.0

func WithRemoteAddr(raddr *net.UDPAddr) UDPWriteOption

WithRemoteAddr sets the remote address to packet.

Directories

Path Synopsis
internal/blocks
Package blocks contains wire types shared by the legacy and Q-Block engines.
Package blocks contains wire types shared by the legacy and Q-Block engines.
qblock
Package qblock implements RFC 9177 datagram transfers independently of the legacy RFC 7959 blockwise engine.
Package qblock implements RFC 9177 datagram transfers independently of the legacy RFC 7959 blockwise engine.
monitor
Package oscore contains transport-independent RFC 8613 security-context and message-protection support.
Package oscore contains transport-independent RFC 8613 security-context and message-protection support.
proxy
Package proxy provides a minimal opaque forwarder for OSCORE-protected CoAP requests.
Package proxy provides a minimal opaque forwarder for OSCORE-protected CoAP requests.
store
Package store provides file-backed OSCORE state stores.
Package store provides file-backed OSCORE state stores.

Jump to

Keyboard shortcuts

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