p2p

package
v0.7.10 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2014 License: LGPL-2.1-or-later Imports: 24 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DiscRequested           DiscReason = 0x00
	DiscNetworkError                   = 0x01
	DiscProtocolError                  = 0x02
	DiscUselessPeer                    = 0x03
	DiscTooManyPeers                   = 0x04
	DiscAlreadyConnected               = 0x05
	DiscIncompatibleVersion            = 0x06
	DiscInvalidIdentity                = 0x07
	DiscQuitting                       = 0x08
	DiscUnexpectedIdentity             = 0x09
	DiscSelf                           = 0x0a
	DiscReadTimeout                    = 0x0b
	DiscSubprotocolError               = 0x10
)

Variables

View Source
var ErrPipeClosed = errors.New("p2p: read or write on closed message pipe")

ErrPipeClosed is returned from pipe operations after the pipe has been closed.

Functions

func MsgPipe

func MsgPipe() (*MsgPipeRW, *MsgPipeRW)

MsgPipe creates a message pipe. Reads on one end are matched with writes on the other. The pipe is full-duplex, both ends implement MsgReadWriter.

Example
rw1, rw2 := MsgPipe()
go func() {
	rw1.EncodeMsg(8, []byte{0, 0})
	rw1.EncodeMsg(5, []byte{1, 1})
	rw1.Close()
}()

for {
	msg, err := rw2.ReadMsg()
	if err != nil {
		break
	}
	var data [1][]byte
	msg.Decode(&data)
	fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
}
Output:

msg: 8, 0000
msg: 5, 0101

Types

type Blacklist

type Blacklist interface {
	Get([]byte) (bool, error)
	Put([]byte) error
	Delete([]byte) error
	Exists(pubkey []byte) (ok bool)
}

type BlacklistMap

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

func NewBlacklist

func NewBlacklist() *BlacklistMap

func (*BlacklistMap) Delete

func (self *BlacklistMap) Delete(pubkey []byte) error

func (*BlacklistMap) Exists

func (self *BlacklistMap) Exists(pubkey []byte) (ok bool)

func (*BlacklistMap) Get

func (self *BlacklistMap) Get(pubkey []byte) (bool, error)

func (*BlacklistMap) Put

func (self *BlacklistMap) Put(pubkey []byte) error

type Cap

type Cap struct {
	Name    string
	Version uint
}

Cap is the structure of a peer capability.

func (Cap) RlpData

func (cap Cap) RlpData() interface{}

type ClientIdentity

type ClientIdentity interface {
	String() string // human readable identity
	Pubkey() []byte // 512-bit public key
}

ClientIdentity represents the identity of a peer.

type DiscReason

type DiscReason byte

func (DiscReason) String

func (d DiscReason) String() string

type Msg

type Msg struct {
	Code    uint64
	Size    uint32 // size of the paylod
	Payload io.Reader
}

Msg defines the structure of a p2p message.

Note that a Msg can only be sent once since the Payload reader is consumed during sending. It is not possible to create a Msg and send it any number of times. If you want to reuse an encoded structure, encode the payload into a byte array and create a separate Msg with a bytes.Reader as Payload for each send.

func NewMsg

func NewMsg(code uint64, params ...interface{}) Msg

NewMsg creates an RLP-encoded message with the given code.

func (Msg) Decode

func (msg Msg) Decode(val interface{}) error

Decode parse the RLP content of a message into the given value, which must be a pointer.

For the decoding rules, please see package rlp.

func (Msg) Discard

func (msg Msg) Discard() error

Discard reads any remaining payload data into a black hole.

type MsgPipeRW

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

MsgPipeRW is an endpoint of a MsgReadWriter pipe.

func (*MsgPipeRW) Close

func (p *MsgPipeRW) Close() error

Close unblocks any pending ReadMsg and WriteMsg calls on both ends of the pipe. They will return ErrPipeClosed. Note that Close does not interrupt any reads from a message payload.

func (*MsgPipeRW) EncodeMsg

func (p *MsgPipeRW) EncodeMsg(code uint64, data ...interface{}) error

EncodeMsg is a convenient shorthand for sending an RLP-encoded message.

func (*MsgPipeRW) ReadMsg

func (p *MsgPipeRW) ReadMsg() (Msg, error)

ReadMsg returns a message sent on the other end of the pipe.

func (*MsgPipeRW) WriteMsg

func (p *MsgPipeRW) WriteMsg(msg Msg) error

WriteMsg sends a messsage on the pipe. It blocks until the receiver has consumed the message payload.

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

MsgReadWriter provides reading and writing of encoded messages.

type MsgReader

type MsgReader interface {
	ReadMsg() (Msg, error)
}

type MsgWriter

type MsgWriter interface {
	// WriteMsg sends an existing message.
	// The Payload reader of the message is consumed.
	// Note that messages can be sent only once.
	WriteMsg(Msg) error

	// EncodeMsg writes an RLP-encoded message with the given
	// code and data elements.
	EncodeMsg(code uint64, data ...interface{}) error
}

type NAT

type NAT interface {
	GetExternalAddress() (net.IP, error)
	AddPortMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error
	DeletePortMapping(protocol string, extport, intport int) error

	// Should return name of the method.
	String() string
}

NAT is implemented by NAT traversal methods.

func PMP

func PMP(gateway net.IP) (nat NAT)

PMP returns a NAT traverser that uses NAT-PMP. The provided gateway address should be the IP of your router.

func UPNP

func UPNP() NAT

UPNP returns a NAT port mapper that uses UPnP. It will attempt to discover the address of your router using UDP broadcasts.

type Peer

type Peer struct {
	// Peers have all the log methods.
	// Use them to display messages related to the peer.
	*logger.Logger
	// contains filtered or unexported fields
}

Peer represents a remote peer.

func NewPeer

func NewPeer(id ClientIdentity, caps []Cap) *Peer

NewPeer returns a peer for testing purposes.

func (*Peer) Caps

func (p *Peer) Caps() []Cap

Caps returns the capabilities (supported subprotocols) of the remote peer.

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason DiscReason)

Disconnect terminates the peer connection with the given reason. It returns immediately and does not wait until the connection is closed.

func (*Peer) Identity

func (p *Peer) Identity() ClientIdentity

Identity returns the client identity of the remote peer. The identity can be nil if the peer has not yet completed the handshake.

func (*Peer) LocalAddr

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

LocalAddr returns the local address of the network connection.

func (*Peer) RemoteAddr

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

RemoteAddr returns the remote address of the network connection.

func (*Peer) String

func (p *Peer) String() string

String implements fmt.Stringer.

type Protocol

type Protocol struct {
	// Name should contain the official protocol name,
	// often a three-letter word.
	Name string

	// Version should contain the version number of the protocol.
	Version uint

	// Length should contain the number of message codes used
	// by the protocol.
	Length uint64

	// Run is called in a new groutine when the protocol has been
	// negotiated with a peer. It should read and write messages from
	// rw. The Payload for each message must be fully consumed.
	//
	// The peer connection is closed when Start returns. It should return
	// any protocol-level error (such as an I/O error) that is
	// encountered.
	Run func(peer *Peer, rw MsgReadWriter) error
}

Protocol represents a P2P subprotocol implementation.

type Server

type Server struct {
	// This field must be set to a valid client identity.
	Identity ClientIdentity

	// MaxPeers is the maximum number of peers that can be
	// connected. It must be greater than zero.
	MaxPeers int

	// Protocols should contain the protocols supported
	// by the server. Matching protocols are launched for
	// each peer.
	Protocols []Protocol

	// If Blacklist is set to a non-nil value, the given Blacklist
	// is used to verify peer connections.
	Blacklist Blacklist

	// If ListenAddr is set to a non-nil address, the server
	// will listen for incoming connections.
	//
	// If the port is zero, the operating system will pick a port. The
	// ListenAddr field will be updated with the actual address when
	// the server is started.
	ListenAddr string

	// If set to a non-nil value, the given NAT port mapper
	// is used to make the listening port available to the
	// Internet.
	NAT NAT

	// If Dialer is set to a non-nil value, the given Dialer
	// is used to dial outbound peer connections.
	Dialer *net.Dialer

	// If NoDial is true, the server will not dial any peers.
	NoDial bool
	// contains filtered or unexported fields
}

Server manages all peer connections.

The fields of Server are used as configuration parameters. You should set them before starting the Server. Fields may not be modified while the server is running.

func (*Server) Broadcast

func (srv *Server) Broadcast(protocol string, code uint64, data ...interface{})

Broadcast sends an RLP-encoded message to all connected peers. This method is deprecated and will be removed later.

func (*Server) PeerCount

func (srv *Server) PeerCount() int

PeerCount returns the number of connected peers.

func (*Server) Peers

func (srv *Server) Peers() (peers []*Peer)

Peers returns all connected peers.

func (*Server) Start

func (srv *Server) Start() (err error)

Start starts running the server. Servers can be re-used and started again after stopping.

func (*Server) Stop

func (srv *Server) Stop()

Stop terminates the server and all active peer connections. It blocks until all active connections have been closed.

func (*Server) SuggestPeer

func (srv *Server) SuggestPeer(ip net.IP, port int, nodeID []byte)

SuggestPeer injects an address into the outbound address pool.

type SimpleClientIdentity

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

func NewSimpleClientIdentity

func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string, pubkey string) *SimpleClientIdentity

func (*SimpleClientIdentity) GetCustomIdentifier

func (c *SimpleClientIdentity) GetCustomIdentifier() string

func (*SimpleClientIdentity) Pubkey

func (c *SimpleClientIdentity) Pubkey() []byte

func (*SimpleClientIdentity) SetCustomIdentifier

func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string)

func (*SimpleClientIdentity) String

func (c *SimpleClientIdentity) String() string

Jump to

Keyboard shortcuts

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