net

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2020 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMessageQueueSize uint = 5120

DefaultMessageQueueSize is the buffer size of each queue mentioned above. (queues are buffered channels)

View Source
const DefaultQueueCount uint = 6

DefaultQueueCount is the default number of messages queue we hold. messages queues are used to serialize message receiving

View Source
const MessageQueueSize = 250

Variables

View Source
var (
	// ErrClosedIncomingChannel is sent when the connection is closed because the underlying formatter incoming channel was closed
	ErrClosedIncomingChannel = errors.New("unexpected closed incoming channel")
	// ErrConnectionClosed is sent when the connection is closed after Close was called
	ErrConnectionClosed = errors.New("connections was intentionally closed")
)
View Source
var ErrAlreadyClosed = errors.New("connection is already closed")
View Source
var ErrIncomingSessionTimeout = errors.New("timeout waiting for handshake message")
View Source
var ErrMsgExceededLimit = errors.New("message size exceeded limit")
View Source
var ErrTriedToSetupExistingConn = errors.New("tried to setup existing connection")
View Source
var IPv4LoopbackAddress = net.IP{127, 0, 0, 1}

Functions

func GetHostName

func GetHostName(address string) string

GetHostName returns the host name part of an ip address.

func GetPort

func GetPort(address string) (string, error)

GetPort returns the port name part of an ip address which includes a port number part.

func GetPublicIPAddress

func GetPublicIPAddress() (string, error)

GetPublicIPAddress returns this host public ip address. Method is implemented using the ipify.org service.

func NodeAddr

func NodeAddr(info *node.NodeInfo) *net.UDPAddr

func Temporary

func Temporary(err error) bool

Temporary checks whether the given error should be considered temporary.

Types

type Connection

type Connection interface {
	fmt.Stringer

	ID() string
	RemotePublicKey() p2pcrypto.PublicKey
	SetRemotePublicKey(key p2pcrypto.PublicKey)

	RemoteAddr() net.Addr

	Session() NetworkSession
	SetSession(session NetworkSession)

	Send(m []byte) error
	Close() error
	Closed() bool
}

Connection is an interface stating the API of all secured connections in the system

type ConnectionMock

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

func NewConnectionMock

func NewConnectionMock(key p2pcrypto.PublicKey) *ConnectionMock

func (*ConnectionMock) Close

func (cm *ConnectionMock) Close() error

func (ConnectionMock) Closed

func (cm ConnectionMock) Closed() bool

func (ConnectionMock) ID

func (cm ConnectionMock) ID() string

func (ConnectionMock) IncomingChannel

func (cm ConnectionMock) IncomingChannel() chan []byte

func (*ConnectionMock) RemoteAddr

func (cm *ConnectionMock) RemoteAddr() net.Addr

func (ConnectionMock) RemotePublicKey

func (cm ConnectionMock) RemotePublicKey() p2pcrypto.PublicKey

func (*ConnectionMock) Send

func (cm *ConnectionMock) Send(m []byte) error

func (ConnectionMock) SendCount

func (cm ConnectionMock) SendCount() int32

func (ConnectionMock) Session

func (cm ConnectionMock) Session() NetworkSession

func (*ConnectionMock) SetRemotePublicKey

func (cm *ConnectionMock) SetRemotePublicKey(key p2pcrypto.PublicKey)

func (*ConnectionMock) SetSendDelay

func (cm *ConnectionMock) SetSendDelay(delayMs int)

func (*ConnectionMock) SetSendResult

func (cm *ConnectionMock) SetSendResult(err error)

func (*ConnectionMock) SetSession

func (cm *ConnectionMock) SetSession(session NetworkSession)

func (ConnectionMock) String

func (cm ConnectionMock) String() string

type ConnectionSource

type ConnectionSource int

ConnectionSource specifies the connection originator - local or remote node.

const (
	Local ConnectionSource = iota
	Remote
)

ConnectionSource values

type ConnectionWithErr

type ConnectionWithErr struct {
	Conn Connection
	Err  error
}

type FormattedConnection

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

FormattedConnection is an io.Writer and an io.Closer A network connection supporting full-duplex messaging

func (*FormattedConnection) Close

func (c *FormattedConnection) Close() error

Close closes the connection (implements io.Closer). It is go safe.

func (*FormattedConnection) Closed

func (c *FormattedConnection) Closed() bool

Closed returns whether the connection is closed

func (*FormattedConnection) ID

func (c *FormattedConnection) ID() string

ID returns the channel's ID

func (*FormattedConnection) RemoteAddr

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

RemoteAddr returns the channel's remote peer address

func (*FormattedConnection) RemotePublicKey

func (c *FormattedConnection) RemotePublicKey() p2pcrypto.PublicKey

RemotePublicKey returns the remote peer's public key

func (*FormattedConnection) Send

func (c *FormattedConnection) Send(m []byte) error

func (*FormattedConnection) SendSock

func (c *FormattedConnection) SendSock(m []byte) error

func (*FormattedConnection) Session

func (c *FormattedConnection) Session() NetworkSession

Session returns the network session

func (*FormattedConnection) SetRemotePublicKey

func (c *FormattedConnection) SetRemotePublicKey(key p2pcrypto.PublicKey)

SetRemotePublicKey sets the remote peer's public key

func (*FormattedConnection) SetSession

func (c *FormattedConnection) SetSession(session NetworkSession)

SetSession sets the network session

func (*FormattedConnection) String

func (c *FormattedConnection) String() string

String returns a string describing the connection

type HandshakeData

type HandshakeData struct {
	ClientVersion string
	NetworkID     int32
	Port          uint16
}

type IncomingMessageEvent

type IncomingMessageEvent struct {
	Conn    Connection
	Message []byte
}

IncomingMessageEvent is the event reported on new incoming message, it contains the message and the Connection carrying the message

type ManagedConnection

type ManagedConnection interface {
	Connection
	// contains filtered or unexported methods
}

ManagedConnection in an interface extending Connection with some internal methods that are required for Net to manage Connections

type Net

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

Net is a connection factory able to dial remote endpoints Net clients should register all callbacks Connections may be initiated by Dial() or by remote clients connecting to the listen address ConnManager includes a TCP server, and a TCP client It provides full duplex messaging functionality over the same tcp/ip connection Network should not know about higher-level networking types such as remoteNode, swarm and networkSession Network main client is the swarm Net has no channel events processing loops - clients are responsible for polling these channels and popping events from them

func NewNet

func NewNet(conf config.Config, localEntity node.LocalNode, addr *net.TCPAddr, logger log.Log) (*Net, error)

NewNet creates a new network. It attempts to tcp listen on address. e.g. localhost:1234 .

func (*Net) Dial

func (n *Net) Dial(address net.Addr, remotePubkey p2pcrypto.PublicKey) (Connection, error)

Dial a remote server with provided time out address:: net.Addr Returns established connection that local clients can send messages to or error if failed to establish a connection, currently only secured connections are supported

func (*Net) EnqueueMessage

func (n *Net) EnqueueMessage(event IncomingMessageEvent)

EnqueueMessage inserts a message into a queue, to decide on which queue to send the message to it sum the remote public key bytes as integer to segment to queueCount queues.

func (*Net) HandlePreSessionIncomingMessage

func (n *Net) HandlePreSessionIncomingMessage(c Connection, message []byte) error

HandlePreSessionIncomingMessage establishes session with the remote peer and update the Connection with the new session

func (*Net) IncomingMessages

func (n *Net) IncomingMessages() []chan IncomingMessageEvent

IncomingMessages returns a slice of channels which incoming messages are delivered on the receiver should iterate on all the channels and read all messages. to sync messages order but enable parallel messages handling.

func (*Net) LocalAddr

func (n *Net) LocalAddr() net.Addr

LocalAddr returns the local listening address. panics before calling Start or if Start errored

func (*Net) LocalNode

func (n *Net) LocalNode() node.LocalNode

LocalNode return's the local node descriptor

func (*Net) Logger

func (n *Net) Logger() log.Log

Logger returns a reference to logger

func (*Net) NetworkID

func (n *Net) NetworkID() int8

NetworkID retuers Net's network ID

func (*Net) Shutdown

func (n *Net) Shutdown()

Shutdown initiate a graceful closing of the TCP listener and all other internal routines

func (*Net) Start

func (n *Net) Start() error

func (*Net) SubscribeClosingConnections

func (n *Net) SubscribeClosingConnections(f func(connection ConnectionWithErr))

SubscribeClosingConnections registers a callback for a new connection event. all registered callbacks are called before moving.

func (*Net) SubscribeOnNewRemoteConnections

func (n *Net) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))

SubscribeOnNewRemoteConnections registers a callback for a new connection event. all registered callbacks are called before moving.

type NetworkMock

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

NetworkMock is a mock struct

func NewNetworkMock

func NewNetworkMock() *NetworkMock

NewNetworkMock is a mock

func (*NetworkMock) Dial

func (n *NetworkMock) Dial(address net.Addr, remotePublicKey p2pcrypto.PublicKey) (Connection, error)

Dial dials

func (*NetworkMock) DialCount

func (n *NetworkMock) DialCount() int32

DialCount gets the dial count

func (*NetworkMock) EnqueueMessage

func (n *NetworkMock) EnqueueMessage(event IncomingMessageEvent)

EnqueueMessage return channel of IncomingMessages

func (*NetworkMock) HandlePreSessionIncomingMessage

func (n *NetworkMock) HandlePreSessionIncomingMessage(c Connection, msg []byte) error

HandlePreSessionIncomingMessage and stuff

func (*NetworkMock) IncomingMessages

func (n *NetworkMock) IncomingMessages() []chan IncomingMessageEvent

IncomingMessages return channel of IncomingMessages

func (*NetworkMock) Logger

func (n *NetworkMock) Logger() log.Log

Logger return the logger

func (*NetworkMock) NetworkID

func (n *NetworkMock) NetworkID() int8

NetworkID is netid

func (NetworkMock) PreSessionCount

func (n NetworkMock) PreSessionCount() int32

PreSessionCount counts

func (NetworkMock) PublishClosingConnection

func (n NetworkMock) PublishClosingConnection(con ConnectionWithErr)

PublishClosingConnection is a hack to expose the above method in the mock but still impl the same interface

func (NetworkMock) PublishNewRemoteConnection

func (n NetworkMock) PublishNewRemoteConnection(nce NewConnectionEvent)

PublishNewRemoteConnection and stuff

func (*NetworkMock) SetDialDelayMs

func (n *NetworkMock) SetDialDelayMs(delay int8)

SetDialDelayMs sets delay

func (*NetworkMock) SetDialResult

func (n *NetworkMock) SetDialResult(err error)

SetDialResult is a mock

func (*NetworkMock) SetNextDialSessionID

func (n *NetworkMock) SetNextDialSessionID(sID []byte)

func (*NetworkMock) SetPreSessionResult

func (n *NetworkMock) SetPreSessionResult(err error)

SetPreSessionResult does this

func (*NetworkMock) SubscribeClosingConnections

func (n *NetworkMock) SubscribeClosingConnections(f func(connection ConnectionWithErr))

SubscribeClosingConnections subscribes on new connections

func (*NetworkMock) SubscribeOnNewRemoteConnections

func (n *NetworkMock) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))

SubscribeOnNewRemoteConnections subscribes on new connections

type NetworkSession

type NetworkSession interface {
	ID() p2pcrypto.PublicKey // Unique session id, currently the peer pubkey TODO: @noam use pubkey from conn and remove this

	OpenMessage(boxedMessage []byte) ([]byte, error) // decrypt data using session dec key
	SealMessage(message []byte) []byte               // encrypt data using session enc key
}

NetworkSession is an authenticated network session between 2 peers. Sessions may be used between 'connections' until they expire. Session provides the encryptor/decryptor for all messages exchanged between 2 peers. enc/dec is using an ephemeral sym key exchanged securely between the peers via the handshake protocol The handshake protocol goal is to create an authenticated network session.

func NewNetworkSession

func NewNetworkSession(sharedSecret p2pcrypto.SharedSecret, peerPubkey p2pcrypto.PublicKey) NetworkSession

NewNetworkSession creates a new network session based on provided data

type NewConnectionEvent

type NewConnectionEvent struct {
	Conn Connection
	Node *node.NodeInfo
}

NewConnectionEvent is a struct holding a new created connection and a node info.

type ReadWriteCloseAddresserMock

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

ReadWriteCloseAddresserMock is a ninja robot

func NewReadWriteCloseAddresserMock

func NewReadWriteCloseAddresserMock() *ReadWriteCloseAddresserMock

NewReadWriteCloseAddresserMock is this

func (*ReadWriteCloseAddresserMock) Close

func (rwcam *ReadWriteCloseAddresserMock) Close() error

Close is mock close

func (*ReadWriteCloseAddresserMock) CloseCount

func (rwcam *ReadWriteCloseAddresserMock) CloseCount() int

CloseCount oh yeah

func (*ReadWriteCloseAddresserMock) Read

func (rwcam *ReadWriteCloseAddresserMock) Read(p []byte) (n int, err error)

Read is this

func (*ReadWriteCloseAddresserMock) ReadCount

func (rwcam *ReadWriteCloseAddresserMock) ReadCount() int

ReadCount is this

func (*ReadWriteCloseAddresserMock) RemoteAddr

func (rwcam *ReadWriteCloseAddresserMock) RemoteAddr() net.Addr

RemoteAddr is a RemoteAddr mock

func (*ReadWriteCloseAddresserMock) SetReadDeadline

func (rwcam *ReadWriteCloseAddresserMock) SetReadDeadline(t time.Time) error

func (*ReadWriteCloseAddresserMock) SetReadResult

func (rwcam *ReadWriteCloseAddresserMock) SetReadResult(p []byte, err error)

SetReadResult is this

func (*ReadWriteCloseAddresserMock) SetWriteDeadline

func (rwcam *ReadWriteCloseAddresserMock) SetWriteDeadline(t time.Time) error

func (*ReadWriteCloseAddresserMock) SetWriteResult

func (rwcam *ReadWriteCloseAddresserMock) SetWriteResult(err error)

SetWriteResult is a mock

func (*ReadWriteCloseAddresserMock) Write

func (rwcam *ReadWriteCloseAddresserMock) Write(p []byte) (n int, err error)

Write is a mock

func (*ReadWriteCloseAddresserMock) WriteCount

func (rwcam *ReadWriteCloseAddresserMock) WriteCount() int

WriteCount is a mock

func (*ReadWriteCloseAddresserMock) WriteOut

func (rwcam *ReadWriteCloseAddresserMock) WriteOut() (p []byte)

WriteOut is a mock

type ReadWriteCloserMock

type ReadWriteCloserMock struct {
}

ReadWriteCloserMock is a mock of ReadWriteCloserMock

func (ReadWriteCloserMock) Close

func (m ReadWriteCloserMock) Close() error

Close mocks close

func (ReadWriteCloserMock) Read

func (m ReadWriteCloserMock) Read(p []byte) (n int, err error)

Read reads something

func (ReadWriteCloserMock) RemoteAddr

func (m ReadWriteCloserMock) RemoteAddr() net.Addr

RemoteAddr mocks remote addr return

func (ReadWriteCloserMock) Write

func (m ReadWriteCloserMock) Write(p []byte) (n int, err error)

Write mocks write

type SessionMock

type SessionMock struct {
	SealMessageFunc func(message []byte) []byte
	OpenMessageFunc func(boxedMessage []byte) ([]byte, error)
	// contains filtered or unexported fields
}

SessionMock is a wonderful fluffy teddybear

func NewSessionMock

func NewSessionMock(pubkey p2pcrypto.PublicKey) *SessionMock

func (SessionMock) ID

ID is this

func (SessionMock) OpenMessage

func (sm SessionMock) OpenMessage(boxedMessage []byte) ([]byte, error)

Decrypt is this

func (SessionMock) SealMessage

func (sm SessionMock) SealMessage(message []byte) []byte

Encrypt is this

type UDPMessageEvent

type UDPMessageEvent struct {
	From     p2pcrypto.PublicKey
	FromAddr net.Addr
	Message  []byte
}

UDPMessageEvent is an event about a udp message. passed through a channel

type UDPNet

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

UDPNet is used to listen on or send udp messages

func NewUDPNet

func NewUDPNet(config config.Config, localEntity node.LocalNode, addr *net.UDPAddr, log log.Log) (*UDPNet, error)

NewUDPNet creates a UDPNet. returns error if the listening can't be resolved

func (*UDPNet) IncomingMessages

func (n *UDPNet) IncomingMessages() chan UDPMessageEvent

IncomingMessages is a channel where incoming UDPMessagesEvents will stream

func (*UDPNet) LocalAddr

func (n *UDPNet) LocalAddr() net.Addr

LocalAddr returns the local listening addr, will panic before running Start. or if start errored

func (*UDPNet) Send

func (n *UDPNet) Send(to *node.NodeInfo, data []byte) error

Send writes a udp packet to the target with the given data

func (*UDPNet) Shutdown

func (n *UDPNet) Shutdown()

Shutdown stops listening and closes the connection

func (*UDPNet) Start

func (n *UDPNet) Start() error

Start will trigger listening on the configured port

Directories

Path Synopsis
delimited
Package delimited implements a reader and writer for simple streams of length-delimited byte records.
Package delimited implements a reader and writer for simple streams of length-delimited byte records.

Jump to

Keyboard shortcuts

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