Documentation ¶
Index ¶
- Constants
- Variables
- func GetHostName(address string) string
- func GetPort(address string) (string, error)
- func GetPublicIPAddress() (string, error)
- func NodeAddr(info *node.NodeInfo) *net.UDPAddr
- func Temporary(err error) bool
- type Connection
- type ConnectionMock
- func (cm *ConnectionMock) Close() error
- func (cm ConnectionMock) Closed() bool
- func (cm ConnectionMock) ID() string
- func (cm ConnectionMock) IncomingChannel() chan []byte
- func (cm *ConnectionMock) RemoteAddr() net.Addr
- func (cm ConnectionMock) RemotePublicKey() p2pcrypto.PublicKey
- func (cm *ConnectionMock) Send(m []byte) error
- func (cm ConnectionMock) SendCount() int32
- func (cm ConnectionMock) Session() NetworkSession
- func (cm *ConnectionMock) SetRemotePublicKey(key p2pcrypto.PublicKey)
- func (cm *ConnectionMock) SetSendDelay(delayMs int)
- func (cm *ConnectionMock) SetSendResult(err error)
- func (cm *ConnectionMock) SetSession(session NetworkSession)
- func (cm ConnectionMock) String() string
- type ConnectionSource
- type ConnectionWithErr
- type FormattedConnection
- func (c *FormattedConnection) Close() error
- func (c *FormattedConnection) Closed() bool
- func (c *FormattedConnection) ID() string
- func (c *FormattedConnection) RemoteAddr() net.Addr
- func (c *FormattedConnection) RemotePublicKey() p2pcrypto.PublicKey
- func (c *FormattedConnection) Send(m []byte) error
- func (c *FormattedConnection) SendSock(m []byte) error
- func (c *FormattedConnection) Session() NetworkSession
- func (c *FormattedConnection) SetRemotePublicKey(key p2pcrypto.PublicKey)
- func (c *FormattedConnection) SetSession(session NetworkSession)
- func (c *FormattedConnection) String() string
- type HandshakeData
- type IncomingMessageEvent
- type ManagedConnection
- type Net
- func (n *Net) Dial(address net.Addr, remotePubkey p2pcrypto.PublicKey) (Connection, error)
- func (n *Net) EnqueueMessage(event IncomingMessageEvent)
- func (n *Net) HandlePreSessionIncomingMessage(c Connection, message []byte) error
- func (n *Net) IncomingMessages() []chan IncomingMessageEvent
- func (n *Net) LocalAddr() net.Addr
- func (n *Net) LocalNode() node.LocalNode
- func (n *Net) Logger() log.Log
- func (n *Net) NetworkID() int8
- func (n *Net) Shutdown()
- func (n *Net) Start() error
- func (n *Net) SubscribeClosingConnections(f func(connection ConnectionWithErr))
- func (n *Net) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))
- type NetworkMock
- func (n *NetworkMock) Dial(address net.Addr, remotePublicKey p2pcrypto.PublicKey) (Connection, error)
- func (n *NetworkMock) DialCount() int32
- func (n *NetworkMock) EnqueueMessage(event IncomingMessageEvent)
- func (n *NetworkMock) HandlePreSessionIncomingMessage(c Connection, msg []byte) error
- func (n *NetworkMock) IncomingMessages() []chan IncomingMessageEvent
- func (n *NetworkMock) Logger() log.Log
- func (n *NetworkMock) NetworkID() int8
- func (n NetworkMock) PreSessionCount() int32
- func (n NetworkMock) PublishClosingConnection(con ConnectionWithErr)
- func (n NetworkMock) PublishNewRemoteConnection(nce NewConnectionEvent)
- func (n *NetworkMock) SetDialDelayMs(delay int8)
- func (n *NetworkMock) SetDialResult(err error)
- func (n *NetworkMock) SetNextDialSessionID(sID []byte)
- func (n *NetworkMock) SetPreSessionResult(err error)
- func (n *NetworkMock) SubscribeClosingConnections(f func(connection ConnectionWithErr))
- func (n *NetworkMock) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))
- type NetworkSession
- type NewConnectionEvent
- type ReadWriteCloseAddresserMock
- func (rwcam *ReadWriteCloseAddresserMock) Close() error
- func (rwcam *ReadWriteCloseAddresserMock) CloseCount() int
- func (rwcam *ReadWriteCloseAddresserMock) Read(p []byte) (n int, err error)
- func (rwcam *ReadWriteCloseAddresserMock) ReadCount() int
- func (rwcam *ReadWriteCloseAddresserMock) RemoteAddr() net.Addr
- func (rwcam *ReadWriteCloseAddresserMock) SetReadDeadline(t time.Time) error
- func (rwcam *ReadWriteCloseAddresserMock) SetReadResult(p []byte, err error)
- func (rwcam *ReadWriteCloseAddresserMock) SetWriteDeadline(t time.Time) error
- func (rwcam *ReadWriteCloseAddresserMock) SetWriteResult(err error)
- func (rwcam *ReadWriteCloseAddresserMock) Write(p []byte) (n int, err error)
- func (rwcam *ReadWriteCloseAddresserMock) WriteCount() int
- func (rwcam *ReadWriteCloseAddresserMock) WriteOut() (p []byte)
- type ReadWriteCloserMock
- type SessionMock
- type UDPMessageEvent
- type UDPNet
Constants ¶
const DefaultMessageQueueSize uint = 5120
DefaultMessageQueueSize is the buffer size of each queue mentioned above. (queues are buffered channels)
const DefaultQueueCount uint = 6
DefaultQueueCount is the default number of messages queue we hold. messages queues are used to serialize message receiving
const MessageQueueSize = 250
Variables ¶
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") )
var ErrAlreadyClosed = errors.New("connection is already closed")
var ErrIncomingSessionTimeout = errors.New("timeout waiting for handshake message")
var ErrMsgExceededLimit = errors.New("message size exceeded limit")
var ErrTriedToSetupExistingConn = errors.New("tried to setup existing connection")
var IPv4LoopbackAddress = net.IP{127, 0, 0, 1}
Functions ¶
func GetHostName ¶
GetHostName returns the host name part of an ip address.
func GetPort ¶
GetPort returns the port name part of an ip address which includes a port number part.
func GetPublicIPAddress ¶
GetPublicIPAddress returns this host public ip address. Method is implemented using the ipify.org service.
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 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 ¶
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 ¶
LocalAddr returns the local listening address. panics before calling Start or if Start errored
func (*Net) Shutdown ¶
func (n *Net) Shutdown()
Shutdown initiate a graceful closing of the TCP listener and all other internal routines
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 (*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) 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) 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
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) 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 ¶
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 ¶
LocalAddr returns the local listening addr, will panic before running Start. or if start errored