net

package
v0.0.0-...-bc2601f Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2019 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMessageQueueSize uint = 256

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 HandshakeReq = "/handshake/1.0/handshake-req/"

HandshakeReq specifies the handshake protocol request message identifier. pattern is [protocol]version[method-name].

View Source
const HandshakeResp = "/handshake/1.0/handshake-resp/"

HandshakeResp specifies the handshake protocol response message identifier.

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")
)

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 ProcessHandshakeResponse

func ProcessHandshakeResponse(remotePub crypto.PublicKey, s NetworkSession, resp *pb.HandshakeData) error

ProcessHandshakeResponse is called by initiator (node1) to handle response from node2 and to establish the session

Types

type Connection

type Connection interface {
	fmt.Stringer

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

	RemoteAddr() net.Addr

	Session() NetworkSession
	SetSession(session NetworkSession)

	Send(m []byte) error
	Close()
	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 crypto.PublicKey) *ConnectionMock

func (*ConnectionMock) Close

func (cm *ConnectionMock) Close()

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() crypto.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 crypto.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 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()

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

func (*FormattedConnection) Closed

func (c *FormattedConnection) Closed() bool

Closed Reports whether the connection was closed. It is go safe.

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() crypto.PublicKey

RemotePublicKey returns the remote peer's public key

func (*FormattedConnection) Send

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

Send binary data to a connection data is copied over so caller can get rid of the data Concurrency: can be called from any go routine

func (*FormattedConnection) Session

func (c *FormattedConnection) Session() NetworkSession

Session returns the network session

func (*FormattedConnection) SetRemotePublicKey

func (c *FormattedConnection) SetRemotePublicKey(key crypto.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 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) (*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 string, remotePublicKey crypto.PublicKey) (Connection, error)

Dial a remote server with provided time out address:: ip:port 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) LocalNode

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

LocalNode return's the local node descriptor

func (*Net) NetworkID

func (n *Net) NetworkID() uint32

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) SubscribeClosingConnections

func (n *Net) SubscribeClosingConnections() chan Connection

SubscribeClosingConnections registers a channel where closing connections events are reported

func (*Net) SubscribeOnNewRemoteConnections

func (n *Net) SubscribeOnNewRemoteConnections() chan NewConnectionEvent

SubscribeOnNewRemoteConnections returns new channel where events of new remote connections are reported

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 string, remotePublicKey crypto.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() *logging.Logger

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 Connection)

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() chan Connection

SubscribeClosingConnections subscribes on new connections

func (*NetworkMock) SubscribeOnNewRemoteConnections

func (n *NetworkMock) SubscribeOnNewRemoteConnections() chan NewConnectionEvent

SubscribeOnNewRemoteConnections subscribes on new connections

type NetworkSession

type NetworkSession interface {
	ID() []byte     // Unique session id
	KeyM() []byte   // session shared sym key for mac - 32 bytes
	PubKey() []byte // 65 bytes session-only pub key uncompressed

	Decrypt(in []byte) ([]byte, error) // decrypt data using session dec key
	Encrypt(in []byte) ([]byte, error) // encrypt data using session enc key

	EncryptGuard() *sync.Mutex // used for creating a per session transaction of data encryption and data delivery
}

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 GenerateHandshakeRequestData

func GenerateHandshakeRequestData(localPublicKey crypto.PublicKey, localPrivateKey crypto.PrivateKey, remotePublicKey crypto.PublicKey,
	networkID uint32, port uint16) (*pb.HandshakeData, NetworkSession, error)

GenerateHandshakeRequestData and session data between node and remoteNode Returns handshake data to send to removeNode and a network session data object that includes the session enc/dec sym key and iv Node that NetworkSession is not yet authenticated - this happens only when the handshake response is processed and authenticated This is called by node1 (initiator)

func ProcessHandshakeRequest

func ProcessHandshakeRequest(networkID uint32, lPub crypto.PublicKey, lPri crypto.PrivateKey, rPub crypto.PublicKey, req *pb.HandshakeData) (*pb.HandshakeData, NetworkSession, error)

ProcessHandshakeRequest Process a session handshake request data from remoteNode r Returns Handshake data to send to r and a network session data object that includes the session sym enc/dec key This is called by responder in the handshake protocol (node2)

type NetworkSessionImpl

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

NetworkSessionImpl implements NetworkSession.

func NewNetworkSession

func NewNetworkSession(id, keyE, keyM, pubKey []byte, localNodeID, remoteNodeID string) (*NetworkSessionImpl, error)

NewNetworkSession creates a new network session based on provided data

func (*NetworkSessionImpl) Created

func (n *NetworkSessionImpl) Created() time.Time

Created returns the session creation time.

func (*NetworkSessionImpl) Decrypt

func (n *NetworkSessionImpl) Decrypt(in []byte) ([]byte, error)

Decrypt decrypts in binary data that was encrypted with the session's sym enc key.

func (*NetworkSessionImpl) Encrypt

func (n *NetworkSessionImpl) Encrypt(in []byte) ([]byte, error)

Encrypt encrypts in binary data with the session's sym enc key.

func (*NetworkSessionImpl) EncryptGuard

func (n *NetworkSessionImpl) EncryptGuard() *sync.Mutex

EncryptGuard returns a mutex that is used by clients of session to tie encryption and sending together.

func (*NetworkSessionImpl) ID

func (n *NetworkSessionImpl) ID() []byte

ID returns the session's unique id

func (*NetworkSessionImpl) KeyE

func (n *NetworkSessionImpl) KeyE() []byte

KeyE returns the sessions sym encryption key.

func (*NetworkSessionImpl) KeyM

func (n *NetworkSessionImpl) KeyM() []byte

KeyM returns the session's MAC encryption key.

func (*NetworkSessionImpl) LocalNodeID

func (n *NetworkSessionImpl) LocalNodeID() string

LocalNodeID returns the session's local node id.

func (*NetworkSessionImpl) PubKey

func (n *NetworkSessionImpl) PubKey() []byte

PubKey returns the session's public key.

func (*NetworkSessionImpl) RemoteNodeID

func (n *NetworkSessionImpl) RemoteNodeID() string

RemoteNodeID returns the session's remote node id.

func (*NetworkSessionImpl) String

func (n *NetworkSessionImpl) String() string

String returns the session's identifier string.

type NewConnectionEvent

type NewConnectionEvent struct {
	Conn Connection
	Node node.Node
}

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) SetReadResult

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

SetReadResult is this

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 {
	// contains filtered or unexported fields
}

SessionMock is a wonderful fluffy teddybear

func NewSessionMock

func NewSessionMock(ID []byte) *SessionMock

func (SessionMock) Decrypt

func (sm SessionMock) Decrypt(in []byte) ([]byte, error)

Decrypt is this

func (SessionMock) Encrypt

func (sm SessionMock) Encrypt(in []byte) ([]byte, error)

Encrypt is this

func (SessionMock) EncryptGuard

func (n SessionMock) EncryptGuard() *sync.Mutex

func (SessionMock) ID

func (sm SessionMock) ID() []byte

ID is this

func (SessionMock) KeyM

func (sm SessionMock) KeyM() []byte

KeyM is this

func (SessionMock) PubKey

func (sm SessionMock) PubKey() []byte

PubKey is this

func (*SessionMock) SetDecrypt

func (sm *SessionMock) SetDecrypt(res []byte, err error)

SetDecrypt is this

func (*SessionMock) SetEncrypt

func (sm *SessionMock) SetEncrypt(res []byte, err error)

SetEncrypt is this

func (*SessionMock) SetKeyM

func (sm *SessionMock) SetKeyM(x []byte)

SetKeyM is this

func (*SessionMock) SetPubKey

func (sm *SessionMock) SetPubKey(x []byte)

SetPubKey is this

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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