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
- func DisableLog()
- func SetDialer(dialer func(string, string) (net.Conn, error))
- func UseLogger(logger btclog.Logger)
- type Addr
- type Connection
- type Inventory
- func (I *Inventory) AddKnown(invVect *wire.InvVect)
- func (I *Inventory) AddRequest(i int)
- func (I *Inventory) FilterKnown(inv []*wire.InvVect) []*wire.InvVect
- func (I *Inventory) IsKnown(invVect *wire.InvVect) bool
- func (I *Inventory) NumRequests() int
- func (I *Inventory) RemoveKnown(invVect *wire.InvVect)
- type Listener
- type MruInventoryMap
- type ObjectManager
- type Peer
- func (p *Peer) Addr() net.Addr
- func (p *Peer) Connected() bool
- func (p *Peer) Disconnect()
- func (p *Peer) HandleAddrMsg(msg *wire.MsgAddr) error
- func (p *Peer) HandleGetDataMsg(msg *wire.MsgGetData) error
- func (p *Peer) HandleInvMsg(msg *wire.MsgInv) error
- func (p *Peer) HandleObjectMsg(msg *wire.MsgObject) error
- func (p *Peer) HandleRelayInvMsg(inv []*wire.InvVect) error
- func (p *Peer) HandleVerAckMsg() error
- func (p *Peer) HandleVersionMsg(msg *wire.MsgVersion) error
- func (p *Peer) HandshakeComplete() bool
- func (p *Peer) NetAddress() *wire.NetAddress
- func (p *Peer) PrependAddr(str string) string
- func (p *Peer) ProtocolVersion() uint32
- func (p *Peer) PushAddrMsg(addresses []*wire.NetAddress) error
- func (p *Peer) PushGetDataMsg(ivl []*wire.InvVect)
- func (p *Peer) PushInvMsg(invVect []*wire.InvVect)
- func (p *Peer) PushObjectMsg(sha *hash.Sha)
- func (p *Peer) PushVerAckMsg()
- func (p *Peer) PushVersionMsg()
- func (p *Peer) QueueMessage(msg wire.Message)
- func (p *Peer) Start() error
- func (p *Peer) VersionKnown() bool
- type Send
Constants ¶
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.
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.
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 (*Inventory) AddKnown ¶
AddKnown adds the passed inventory to the cache of known inventory for the peer.
func (*Inventory) AddRequest ¶
AddRequest marks that a certain number of objects have been requested.
func (*Inventory) FilterKnown ¶
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 ¶
IsKnown returns whether or not the peer is known to have the passed inventory.
func (*Inventory) NumRequests ¶
NumRequests is the number of object download requests that have been made.
func (*Inventory) RemoveKnown ¶
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.
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) 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 ¶
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 ¶
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 ¶
HandleObjectMsg updates the peer's request list and sends the object to the object manager.
func (*Peer) HandleRelayInvMsg ¶
HandleRelayInvMsg takes an inv list and queues it to be sent to the remote peer eventually.
func (*Peer) HandleVerAckMsg ¶
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 ¶
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 ¶
PrependAddr is a helper function for logging that adds the ip address to the start of the string to be logged.
func (*Peer) ProtocolVersion ¶
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 ¶
PushGetDataMsg creates a GetData message and sends it to the remote peer.
func (*Peer) PushInvMsg ¶
PushInvMsg creates and sends an Inv message and sends it to the remote peer.
func (*Peer) PushObjectMsg ¶
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 ¶
QueueMessage takes a message and sends it to the remote peer.
func (*Peer) VersionKnown ¶
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.