mux

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 8 Imported by: 5

README

Sia Core

GoDoc

mux

SiaMux is a high-performance stream multiplexer. It allows you to operate many distinct bidirectional streams on top of a single underlying connection. We built it for Sia because we weren't satisfied with other multiplexers available at the time.

As a privacy-focused multiplexer, SiaMux behaves differently from other muxes. It transparently encrypts the connection, and supports authentication via Ed25519 public keys. To hinder metadata analysis, it always writes data in fixed-size "packets," inserting padding as necessary. Lastly, SiaMux implements a unique feature known as covert streams. Covert streams hide their data within the padding of other streams, making them completely undetectable to network analysis (at the cost of greatly reduced throughput). SiaMux can be used for any application in need of a multiplexer, but its privacy features make it a particularly good choice for p2p networks and other distributed systems.

Usage

Dialer:

conn, _ := net.Dial("tcp", addr)
defer conn.Close()
m, _ := mux.DialAnonymous(conn)
defer m.Close()
s := m.DialStream()
defer s.Close()
io.WriteString(s, "hello, world")

Listener:

l, _ := net.Listen("tcp", addr)
conn, _ := l.Accept()
m, _ := mux.AcceptAnonymous(conn)
s, _ := m.AcceptStream()
defer s.Close()
io.Copy(os.Stdout, s)

For authenticated communication, use mux.Dial/mux.Accept with a crypto/ed25519 keypair.

To create a covert stream, use m.DialCovertStream. The accepting peer calls m.AcceptStream as usual.

Benchmarks

SiaMux allocates very little memory (some buffers at startup, plus the Stream objects), does not use channels, and spawns just two goroutines per multiplexer. Despite encrypting the connection, in benchmarks SiaMux is competitive with alternatives such as yamux, muxado, and smux.

BenchmarkMux/1            5156 ns/op     830.90 MB/s      193977 frames/sec        0 allocs/op
BenchmarkMux/2            9550 ns/op     897.21 MB/s      210562 frames/sec        0 allocs/op
BenchmarkMux/10          50567 ns/op     847.19 MB/s      198281 frames/sec        0 allocs/op
BenchmarkMux/100        449494 ns/op     953.07 MB/s      223966 frames/sec        0 allocs/op
BenchmarkMux/500       2184647 ns/op     980.48 MB/s      229271 frames/sec        7 allocs/op
BenchmarkMux/1000      4548476 ns/op     941.85 MB/s      221448 frames/sec       28 allocs/op
BenchmarkCovertStream    45805 ns/op      93.53 MB/s       21833 frames/sec        2 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mux

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

A Mux multiplexes multiple duplex Streams onto a single net.Conn.

func Accept

func Accept(conn net.Conn, ourKey ed25519.PrivateKey) (*Mux, error)

Accept reciprocates a mux protocol handshake on the provided conn.

func AcceptAnonymous

func AcceptAnonymous(conn net.Conn) (*Mux, error)

AcceptAnonymous reciprocates a mux protocol handshake without a pre-established identity. The counterparty must initiate the handshake with DialAnonymous.

func Dial

func Dial(conn net.Conn, theirKey ed25519.PublicKey) (*Mux, error)

Dial initiates a mux protocol handshake on the provided conn.

func DialAnonymous

func DialAnonymous(conn net.Conn) (*Mux, error)

DialAnonymous initiates a mux protocol handshake to a party without a pre-established identity. The counterparty must reciprocate the handshake with AcceptAnonymous.

func (*Mux) AcceptStream

func (m *Mux) AcceptStream() (*Stream, error)

AcceptStream waits for and returns the next peer-initiated Stream.

func (*Mux) Close

func (m *Mux) Close() error

Close closes the underlying net.Conn.

func (*Mux) DialStream

func (m *Mux) DialStream() *Stream

DialStream creates a new Stream.

Unlike e.g. net.Dial, this does not perform any I/O; the peer will not be aware of the new Stream until Write is called.

type Stream

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

A Stream is a duplex connection multiplexed over a net.Conn. It implements the net.Conn interface.

func (*Stream) Close

func (s *Stream) Close() error

Close closes the Stream. The underlying connection is not closed.

func (*Stream) LocalAddr

func (s *Stream) LocalAddr() net.Addr

LocalAddr returns the underlying connection's LocalAddr.

func (*Stream) Read

func (s *Stream) Read(p []byte) (int, error)

Read reads data from the Stream.

func (*Stream) RemoteAddr

func (s *Stream) RemoteAddr() net.Addr

RemoteAddr returns the underlying connection's RemoteAddr.

func (*Stream) SetDeadline

func (s *Stream) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the Stream. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

This implementation does not entirely conform to the net.Conn interface: setting a new deadline does not affect pending Read or Write calls, only future calls.

func (*Stream) SetReadDeadline

func (s *Stream) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline associated with the Stream.

This implementation does not entirely conform to the net.Conn interface: setting a new deadline does not affect pending Read calls, only future calls.

func (*Stream) SetWriteDeadline

func (s *Stream) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline associated with the Stream.

This implementation does not entirely conform to the net.Conn interface: setting a new deadline does not affect pending Write calls, only future calls.

func (*Stream) Write

func (s *Stream) Write(p []byte) (int, error)

Write writes data to the Stream.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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