Documentation ¶
Overview ¶
Package net manages the accepting network connections/messages and routing the data upward for the protocols to consume.
Index ¶
- Constants
- Variables
- func NodeAddr(info *node.Info) *net.UDPAddr
- func Temporary(err error) bool
- type Connection
- type ConnectionMock
- func (cm *ConnectionMock) Close() error
- func (cm ConnectionMock) Closed() bool
- func (cm ConnectionMock) Created() time.Time
- 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) SetCreated(time2 time.Time)
- 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) Created() time.Time
- 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 MsgConnection
- func (c *MsgConnection) Close() error
- func (c *MsgConnection) Closed() bool
- func (c *MsgConnection) Created() time.Time
- func (c *MsgConnection) ID() string
- func (c *MsgConnection) RemoteAddr() net.Addr
- func (c *MsgConnection) RemotePublicKey() p2pcrypto.PublicKey
- func (c *MsgConnection) Send(m []byte) error
- func (c *MsgConnection) SendSock(m []byte) error
- func (c *MsgConnection) Session() NetworkSession
- func (c *MsgConnection) SetRemotePublicKey(key p2pcrypto.PublicKey)
- func (c *MsgConnection) SetSession(session NetworkSession)
- func (c *MsgConnection) String() string
- type Net
- func (n *Net) Dial(ctx context.Context, 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(listener net.Listener)
- func (n *Net) SubscribeClosingConnections(f func(connection ConnectionWithErr))
- func (n *Net) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))
- type NetworkMock
- func (n *NetworkMock) Dial(ctx context.Context, 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 ReadWriteCloserMock
- type SessionMock
- type UDPListener
- type UDPMessageEvent
- type UDPNet
- func (n *UDPNet) Dial(ctx context.Context, address net.Addr, remotePublicKey p2pcrypto.PublicKey) (Connection, error)
- func (n *UDPNet) EnqueueMessage(ime IncomingMessageEvent)
- func (n *UDPNet) HandlePreSessionIncomingMessage(c Connection, msg []byte) error
- func (n *UDPNet) IncomingMessages() chan IncomingMessageEvent
- func (n *UDPNet) LocalAddr() net.Addr
- func (n *UDPNet) NetworkID() int8
- func (n *UDPNet) Send(to *node.Info, data []byte) error
- func (n *UDPNet) Shutdown()
- func (n *UDPNet) Start(listener UDPListener)
- func (n *UDPNet) SubscribeClosingConnections(f func(connection ConnectionWithErr))
- func (n *UDPNet) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))
Constants ¶
const ( // ReadBufferSize const is the default value used to set the socket option SO_RCVBUF ReadBufferSize = 5 * 1024 * 1024 // WriteBufferSize const is the default value used to set the socket option SO_SNDBUF WriteBufferSize = 5 * 1024 * 1024 // TCPKeepAlive sets whether KeepAlive is active or not on the socket TCPKeepAlive = true // TCPKeepAlivePeriod sets the interval of KeepAlive TCPKeepAlivePeriod = 10 * time.Second )
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
MessageQueueSize is the size for queue of messages before pushing them on the socket
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 ( // ErrTriedToSetupExistingConn occurs when handshake packet is sent twice on a connection ErrTriedToSetupExistingConn = errors.New("tried to setup existing connection") // ErrMsgExceededLimit occurs when a received message size exceeds the defined message size ErrMsgExceededLimit = errors.New("message size exceeded limit") )
var ErrAlreadyClosed = errors.New("connection is already closed")
ErrAlreadyClosed is an error for when `Close` is called on a closed connection.
var IPv4LoopbackAddress = net.IP{127, 0, 0, 1}
IPv4LoopbackAddress is a local IPv4 loopback
Functions ¶
Types ¶
type Connection ¶
type Connection interface { fmt.Stringer ID() string RemotePublicKey() p2pcrypto.PublicKey SetRemotePublicKey(key p2pcrypto.PublicKey) Created() time.Time 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 ¶
ConnectionMock mocks connections.
func NewConnectionMock ¶
func NewConnectionMock(key p2pcrypto.PublicKey) *ConnectionMock
NewConnectionMock creates a ConnectionMock.
func (ConnectionMock) Created ¶ added in v0.1.2
func (cm ConnectionMock) Created() time.Time
Created mocks the connection interface.
func (ConnectionMock) IncomingChannel ¶
func (cm ConnectionMock) IncomingChannel() chan []byte
IncomingChannel mocks the interface.
func (*ConnectionMock) RemoteAddr ¶
func (cm *ConnectionMock) RemoteAddr() net.Addr
RemoteAddr mocks the interface.
func (ConnectionMock) RemotePublicKey ¶
func (cm ConnectionMock) RemotePublicKey() p2pcrypto.PublicKey
RemotePublicKey mocks the interface.
func (*ConnectionMock) Send ¶
func (cm *ConnectionMock) Send(m []byte) error
Send mocks the interface.
func (ConnectionMock) SendCount ¶
func (cm ConnectionMock) SendCount() int32
SendCount mutates the mock.
func (ConnectionMock) Session ¶
func (cm ConnectionMock) Session() NetworkSession
Session mocks the interface.
func (ConnectionMock) SetCreated ¶ added in v0.1.2
func (cm ConnectionMock) SetCreated(time2 time.Time)
SetCreated mutate the mock.
func (*ConnectionMock) SetRemotePublicKey ¶
func (cm *ConnectionMock) SetRemotePublicKey(key p2pcrypto.PublicKey)
SetRemotePublicKey mutates the mock.
func (*ConnectionMock) SetSendDelay ¶
func (cm *ConnectionMock) SetSendDelay(delayMs int)
SetSendDelay mutates the mock.
func (*ConnectionMock) SetSendResult ¶
func (cm *ConnectionMock) SetSendResult(err error)
SetSendResult mutates the mock.
func (*ConnectionMock) SetSession ¶
func (cm *ConnectionMock) SetSession(session NetworkSession)
SetSession mutates the mock.
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 }
ConnectionWithErr is a pair of Connection and an error occurred within the connection
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) Created ¶ added in v0.1.2
func (c *FormattedConnection) Created() time.Time
Created is the time the connection was created
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
Send pushes a message into the queue if the connection is not closed.
func (*FormattedConnection) SendSock ¶
func (c *FormattedConnection) SendSock(m []byte) error
SendSock sends a message directly on the socket without waiting for the queue.
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 ¶
HandshakeData is the handshake message struct
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 MsgConnection ¶ added in v0.1.2
type MsgConnection struct {
// contains filtered or unexported fields
}
MsgConnection is an io.Writer and an io.Closer A network connection supporting full-duplex messaging It resembles the Connection interface but suits a packet oriented socket.
func (*MsgConnection) Close ¶ added in v0.1.2
func (c *MsgConnection) Close() error
Close closes the connection (implements io.Closer). It is go safe.
func (*MsgConnection) Closed ¶ added in v0.1.2
func (c *MsgConnection) Closed() bool
Closed returns whether the connection is closed
func (*MsgConnection) Created ¶ added in v0.1.2
func (c *MsgConnection) Created() time.Time
Created saves the time when the connection was created
func (*MsgConnection) ID ¶ added in v0.1.2
func (c *MsgConnection) ID() string
ID returns the channel's ID
func (*MsgConnection) RemoteAddr ¶ added in v0.1.2
func (c *MsgConnection) RemoteAddr() net.Addr
RemoteAddr returns the channel's remote peer address
func (*MsgConnection) RemotePublicKey ¶ added in v0.1.2
func (c *MsgConnection) RemotePublicKey() p2pcrypto.PublicKey
RemotePublicKey returns the remote peer's public key
func (*MsgConnection) Send ¶ added in v0.1.2
func (c *MsgConnection) Send(m []byte) error
Send pushes a message to the messages queue
func (*MsgConnection) SendSock ¶ added in v0.1.2
func (c *MsgConnection) SendSock(m []byte) error
SendSock sends a message directly on the socket
func (*MsgConnection) Session ¶ added in v0.1.2
func (c *MsgConnection) Session() NetworkSession
Session returns the network session
func (*MsgConnection) SetRemotePublicKey ¶ added in v0.1.2
func (c *MsgConnection) SetRemotePublicKey(key p2pcrypto.PublicKey)
SetRemotePublicKey sets the remote peer's public key
func (*MsgConnection) SetSession ¶ added in v0.1.2
func (c *MsgConnection) SetSession(session NetworkSession)
SetSession sets the network session
func (*MsgConnection) String ¶ added in v0.1.2
func (c *MsgConnection) String() string
String returns a string describing the connection
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 It provides full duplex messaging functionality over the same tcp/ip connection Net has no channel events processing loops - clients are responsible for polling these channels and popping events from them
func NewNet ¶
NewNet creates a new network. It attempts to tcp listen on address. e.g. localhost:1234 .
func (*Net) Dial ¶
func (n *Net) Dial(ctx context.Context, 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 ¶
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(ctx context.Context, 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)
SetNextDialSessionID mutates the mock to change the next returned session id
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.Info }
NewConnectionEvent is a struct holding a new created connection and a node info.
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 mocks NetworkSession.
func NewSessionMock ¶
func NewSessionMock(pubkey p2pcrypto.PublicKey) *SessionMock
NewSessionMock creates a new mock for given public key.
func (SessionMock) OpenMessage ¶
func (sm SessionMock) OpenMessage(boxedMessage []byte) ([]byte, error)
OpenMessage is a mock.
func (SessionMock) SealMessage ¶
func (sm SessionMock) SealMessage(message []byte) []byte
SealMessage is a mock.
type UDPListener ¶ added in v0.1.6
type UDPListener interface { LocalAddr() net.Addr Close() error WriteToUDP(final []byte, addr *net.UDPAddr) (int, error) ReadFrom(p []byte) (n int, addr net.Addr, err error) WriteTo(p []byte, addr net.Addr) (n int, err error) SetDeadline(t time.Time) error SetReadDeadline(t time.Time) error SetWriteDeadline(t time.Time) error }
UDPListener is the api required for listening on udp messages.
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 (*UDPNet) Dial ¶ added in v0.1.2
func (n *UDPNet) Dial(ctx context.Context, address net.Addr, remotePublicKey p2pcrypto.PublicKey) (Connection, error)
Dial creates a Connection interface which is wrapped around a udp socket with a session and start listening on messages form it. it uses the `connect` syscall
func (*UDPNet) EnqueueMessage ¶ added in v0.1.2
func (n *UDPNet) EnqueueMessage(ime IncomingMessageEvent)
EnqueueMessage pushes an incoming message event into the queue
func (*UDPNet) HandlePreSessionIncomingMessage ¶ added in v0.1.2
func (n *UDPNet) HandlePreSessionIncomingMessage(c Connection, msg []byte) error
HandlePreSessionIncomingMessage is used to satisfy an api similar to the tcpnet. not used here.
func (*UDPNet) IncomingMessages ¶
func (n *UDPNet) IncomingMessages() chan IncomingMessageEvent
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
func (*UDPNet) NetworkID ¶ added in v0.1.2
NetworkID returns the network id given and used for creating a session.
func (*UDPNet) Shutdown ¶
func (n *UDPNet) Shutdown()
Shutdown stops listening and closes the connection.
func (*UDPNet) Start ¶
func (n *UDPNet) Start(listener UDPListener)
Start will start reading messages from the udp socket and pass them up the channels
func (*UDPNet) SubscribeClosingConnections ¶ added in v0.1.2
func (n *UDPNet) SubscribeClosingConnections(f func(connection ConnectionWithErr))
SubscribeClosingConnections registers a callback for a new connection event. all registered callbacks are called before moving.
func (*UDPNet) SubscribeOnNewRemoteConnections ¶ added in v0.1.2
func (n *UDPNet) SubscribeOnNewRemoteConnections(f func(event NewConnectionEvent))
SubscribeOnNewRemoteConnections registers a callback for a new connection event. all registered callbacks are called before moving.