peer

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2017 License: ISC Imports: 16 Imported by: 8

Documentation

Overview

Package peer provides objects for managing a connection to a remote peer that obeys the bitmessage protocol.

Connection abstracts a tcp connection to a remote bitmessage peer so that the rest of the peer does not have to deal with byte arrays. It also prevents timeout by regularly sending pong messages.

Listener listens for incoming tcp connections and creates Connection objects for them when a connection is opened.

Send manages everything that is to be sent to the remote peer eventually. Data requests, inv trickles, and other messages.

Peer manages the peer object overall and routes incoming messages.

Logic is an interface that has functions such as HandleAddrMsg that handles the different kinds of messages in the bitmessage protocol.

Inventory can be used to store the known inventory and the requested inventory for a peer.

Index

Constants

View Source
const (

	// MaxPeerRequests is the maximum number of outstanding object requests a
	// may have at any given time.
	MaxPeerRequests = 120
)

Variables

This section is empty.

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until either UseLogger or SetLogWriter are called.

func SetDialer

func SetDialer(dialer func(string, string) (net.Conn, error))

SetDialer sets the dialer used by peer to connect to peers.

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type Addr

type Addr wire.NetAddress

Addr is an address of a peer. It's distinct from net.TCPAddr so that Tor addresses can be correctly represented as strings using addrmgr.NetAddressKey.

func (*Addr) Network

func (a *Addr) Network() string

Network returns the address's network name.

func (*Addr) String

func (a *Addr) String() string

String returns a string representation of the address.

type Connection

type Connection interface {
	WriteMessage(wire.Message) error
	ReadMessage() (wire.Message, error)
	BytesWritten() uint64
	BytesRead() uint64
	LastWrite() time.Time
	LastRead() time.Time
	RemoteAddr() net.Addr
	Connected() bool
	Connect() error
	Close()
}

Connection is a bitmessage connection that abstracts the underlying tcp connection away. The user of the Connection only uses bitmessage wire.Message objects instead of the underlying byte stream. This is written as an interface so that it can easily be swapped out for a mock object for testing purposes.

func NewConnection

func NewConnection(addr net.Addr, maxDown, maxUp int64) Connection

NewConnection creates a new *connection.

type Inventory

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

Inventory is the part of a peer that manages object hashes and remembers which are known to the peer and which have been requested from it. It is safe for concurrent access.

func NewInventory

func NewInventory() *Inventory

NewInventory returns a new Inventory object.

func (*Inventory) AddKnown

func (I *Inventory) AddKnown(invVect *wire.InvVect)

AddKnown adds the passed inventory to the cache of known inventory for the peer.

func (*Inventory) AddRequest

func (I *Inventory) AddRequest(i int)

AddRequest marks that a certain number of objects have been requested.

func (*Inventory) FilterKnown

func (I *Inventory) FilterKnown(inv []*wire.InvVect) []*wire.InvVect

FilterKnown takes a list of InvVects, adds them to the list of known inventory, and returns those which were not already in the list. It is used to ensure that data is not sent to the peer that it already is known to have.

func (*Inventory) IsKnown

func (I *Inventory) IsKnown(invVect *wire.InvVect) bool

IsKnown returns whether or not the peer is known to have the passed inventory.

func (*Inventory) NumRequests

func (I *Inventory) NumRequests() int

NumRequests is the number of object download requests that have been made.

func (*Inventory) RemoveKnown

func (I *Inventory) RemoveKnown(invVect *wire.InvVect)

RemoveKnown adds the passed inventory to the cache of known inventory for the peer.

type Listener

type Listener interface {
	Accept() (Connection, error)
	Close() error
	Addr() net.Addr
}

Listener represents an open port listening for bitmessage connections. It is given as an interface so that mock peer listeners can easily swapped for the genuine ones.

func Listen

func Listen(service, addr string) (Listener, error)

Listen creates a listener object. The value of listen can be swapped out with a mock connection dialer for testing purposes.

type MruInventoryMap

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

MruInventoryMap provides a map that is limited to a maximum number of items with eviction for the oldest entry when the limit is exceeded.

func NewMruInventoryMap

func NewMruInventoryMap(limit uint) *MruInventoryMap

NewMruInventoryMap returns a new inventory map that is limited to the number of entries specified by limit. When the number of entries exceeds the limit, the oldest (least recently used) entry will be removed to make room for the new entry.

func (*MruInventoryMap) Add

func (m *MruInventoryMap) Add(iv *wire.InvVect)

Add adds the passed inventory to the map and handles eviction of the oldest item if adding the new item would exceed the max limit.

func (*MruInventoryMap) Delete

func (m *MruInventoryMap) Delete(iv *wire.InvVect)

Delete deletes the passed inventory item from the map (if it exists).

func (*MruInventoryMap) Exists

func (m *MruInventoryMap) Exists(iv *wire.InvVect) bool

Exists returns whether or not the passed inventory item is in the map.

func (*MruInventoryMap) Filter

func (m *MruInventoryMap) Filter(invs []*wire.InvVect) []*wire.InvVect

Filter takes a slice of values and adds those to the map that don't already exist. It returns a slice containing only the values that were added.

func (MruInventoryMap) String

func (m MruInventoryMap) String() string

String returns the map as a human-readable string.

type ObjectManager

type ObjectManager interface {
	NewPeer(*Peer)
	DonePeer(*Peer)
	ReadyPeer(*Peer)
	QueueInv(inv *wire.MsgInv, p *Peer)
	QueueObject(inv *wire.MsgObject, p *Peer)
}

ObjectManager represents the object manager. It is returned by the server when the ObjectManager function is called.

type Peer

type Peer struct {
	Persistent bool
	Inbound    bool

	Inventory *Inventory

	StatsMtx sync.RWMutex // protects all statistics below here.
	// contains filtered or unexported fields
}

Peer provides for the handling of messages from a bitmessage peer. For inbound data-related messages such as objects and inventory, the data is passed on to the object manager to handle it. Outbound messages are queued via a Send object. In addition, the peer contains several functions which are of the form pushX, that are used to push messages to the peer. Internally they use QueueMessage.

func NewPeer

func NewPeer(s server, conn Connection, inventory *Inventory, send Send,
	na *wire.NetAddress, inbound, persistent bool) *Peer

NewPeer returns a new base bitmessage peer for the provided server and inbound flag. This is used by the newInboundPeer and newOutboundPeer functions to perform base setup needed by both types of peers.

func NewPeerHandshakeComplete

func NewPeerHandshakeComplete(s server, conn Connection, inventory *Inventory, send Send,
	na *wire.NetAddress) *Peer

NewPeerHandshakeComplete creates a new peer object that has already nearly completed its initial handshake. It just needs to be sent a ver ack. This function exists mainly for testing purposes.

func (*Peer) Addr

func (p *Peer) Addr() net.Addr

Addr returns the address of the remote peer in the form of a net.Addr

func (*Peer) Connected

func (p *Peer) Connected() bool

Connected returns whether or not the peer is currently connected.

func (*Peer) Disconnect

func (p *Peer) Disconnect()

Disconnect disconnects the peer by closing the connection and signals to the server and object manager that the peer is done. It also sets a flag so the impending shutdown can be detected.

func (*Peer) HandleAddrMsg

func (p *Peer) HandleAddrMsg(msg *wire.MsgAddr) error

HandleAddrMsg is invoked when a peer receives an addr bitmessage message and is used to notify the server about advertised addresses.

func (*Peer) HandleGetDataMsg

func (p *Peer) HandleGetDataMsg(msg *wire.MsgGetData) error

HandleGetDataMsg is invoked when a peer receives a getdata message and is used to deliver object information.

func (*Peer) HandleInvMsg

func (p *Peer) HandleInvMsg(msg *wire.MsgInv) error

HandleInvMsg is invoked when a peer receives an inv bitmessage message and is used to examine the inventory being advertised by the remote peer and react accordingly. We pass the message down to objectmanager which will call QueueMessage with any appropriate responses.

func (*Peer) HandleObjectMsg

func (p *Peer) HandleObjectMsg(msg *wire.MsgObject) error

HandleObjectMsg updates the peer's request list and sends the object to the object manager.

func (*Peer) HandleRelayInvMsg

func (p *Peer) HandleRelayInvMsg(inv []*wire.InvVect) error

HandleRelayInvMsg takes an inv list and queues it to be sent to the remote peer eventually.

func (*Peer) HandleVerAckMsg

func (p *Peer) HandleVerAckMsg() error

HandleVerAckMsg disconnects if the VerAck was received at the wrong time and otherwise updates the peer's state.

func (*Peer) HandleVersionMsg

func (p *Peer) HandleVersionMsg(msg *wire.MsgVersion) error

HandleVersionMsg is invoked when a peer receives a version bitmessage message and is used to negotiate the protocol version details as well as kick start the communications.

func (*Peer) HandshakeComplete

func (p *Peer) HandshakeComplete() bool

HandshakeComplete returns the whether or the initial handshake has been successfully completed. It is safe for concurrent access.

func (*Peer) NetAddress

func (p *Peer) NetAddress() *wire.NetAddress

NetAddress returns the net address of the remote peer. It may be nil.

func (*Peer) PrependAddr

func (p *Peer) PrependAddr(str string) string

PrependAddr is a helper function for logging that adds the ip address to the start of the string to be logged.

func (*Peer) ProtocolVersion

func (p *Peer) ProtocolVersion() uint32

ProtocolVersion returns the peer protocol version in a manner that is safe for concurrent access.

func (*Peer) PushAddrMsg

func (p *Peer) PushAddrMsg(addresses []*wire.NetAddress) error

PushAddrMsg sends one, or more, addr message(s) to the connected peer using the provided addresses.

func (*Peer) PushGetDataMsg

func (p *Peer) PushGetDataMsg(ivl []*wire.InvVect)

PushGetDataMsg creates a GetData message and sends it to the remote peer.

func (*Peer) PushInvMsg

func (p *Peer) PushInvMsg(invVect []*wire.InvVect)

PushInvMsg creates and sends an Inv message and sends it to the remote peer.

func (*Peer) PushObjectMsg

func (p *Peer) PushObjectMsg(sha *hash.Sha)

PushObjectMsg sends an object message for the provided object hash to the connected peer. An error is returned if the object hash is not known.

func (*Peer) PushVerAckMsg

func (p *Peer) PushVerAckMsg()

PushVerAckMsg sends a ver ack to the remote peer.

func (*Peer) PushVersionMsg

func (p *Peer) PushVersionMsg()

PushVersionMsg sends a version message to the connected peer using the current state.

func (*Peer) QueueMessage

func (p *Peer) QueueMessage(msg wire.Message)

QueueMessage takes a message and sends it to the remote peer.

func (*Peer) Start

func (p *Peer) Start() error

Start starts running the peer.

func (*Peer) VersionKnown

func (p *Peer) VersionKnown() bool

VersionKnown returns the whether or not the version of a peer is known locally. It is safe for concurrent access.

type Send

type Send interface {
	// QueueMessage queues a message to be sent to the peer.
	QueueMessage(wire.Message) error

	// QueueDataRequest
	QueueDataRequest([]*wire.InvVect) error

	// QueueInventory adds the passed inventory to the inventory send queue which
	// might not be sent right away, rather it is trickled to the peer in batches.
	// Inventory that the peer is already known to have is ignored. It is safe for
	// concurrent access.
	QueueInventory([]*wire.InvVect) error

	Start(conn Connection)
	Running() bool
	Stop()
}

Send handles everything that is to be sent to the remote peer eventually. It takes messages and sends them over the outgoing connection, inventory vectors corresponding to objects that the peer has requested, and inventory vectors representing objects that we have, which will be periodically sent to the peer in a series of inv messages.

func NewSend

func NewSend(inventory *Inventory, db database.Db) Send

NewSend returns a new sendQueue object.

Jump to

Keyboard shortcuts

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