p2p

package
v1.2.28 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//==================================================================
	// IMPORTANT: Requests MUST have odd values, responses MUST be even!
	// (If you have multiple responses for a single request, leave out
	// odd numbers from sequence)
	//==================================================================
	ReqPING  = 1 // PING to check if a node is alive
	RespPING = 2 // response to PING
	ReqNODE  = 3 // FIND_NODE returns a list of nodes "near" the requested one
	RespNODE = 4 // response to FIND_NODE
	ReqRELAY = 5 // relay message to another node (response-less)
)

type values for node commands

View Source
const (
	MsgfRelay = 1 // Message was forwarded (sender != originator)
	MsgfDrop  = 2 // Drop message without processing (cover traffic)
)

message flags

View Source
const (
	// Alpha is the concurrency parameter
	Alpha = 3

	// BucketTTLSecs is the lifetime of a drop in a bucket (in seconds)
	BucketTTLSecs = 300

	// PingTimeout is the grace period for "still-alive" checks
	PingTimeout = 10 * time.Second
)
View Source
const (
	SampleCache = 100
	MaxSample   = 5
)

Internal constants

View Source
const HdrSize = 80

HdrSize is the size of the message header in bytes.

View Source
const (
	// MaxMsgSize defines the maximum message size
	MaxMsgSize = 65530
)

Variables

View Source
var (
	ErrHandlerUsed   = errors.New("handler in use")
	ErrHandlerUnused = errors.New("handler not in use")
)

Error codes used

View Source
var (
	ErrNodeTimeout        = errors.New("operation timed out")
	ErrNodeRemote         = errors.New("no remote nodes allowed")
	ErrNodeConnectorSet   = errors.New("connector for node already set")
	ErrNodeSendNoReceiver = errors.New("send has no recipient")
	ErrNodeResolve        = errors.New("can't resolve network address")
	ErrNodeMsgType        = errors.New("invalid message type")
)

Error codes

View Source
var (
	ErrPacketSenderMismatch = errors.New("sender not matching message header")
	ErrPacketIntegrity      = errors.New("packet integrity violated")
	ErrPacketSizeMismatch   = errors.New("packet size mismatch")
)

Error messages

View Source
var (
	// AddrSize is the size of an address in bytes
	AddrSize uint16 = 32

	// ErrAddressInvalid error message for malformed addresses
	ErrAddressInvalid = errors.New("invalid address")
)
View Source
var (
	ErrServiceRequest         = errors.New("request failed or invalid")
	ErrServiceRequestUnknown  = errors.New("unknown request")
	ErrServiceResponseUnknown = errors.New("unknown response")
)

Error messages

View Source
var (
	ErrTransSenderMismatch  = errors.New("sender mismatch")
	ErrTransUnknownSender   = errors.New("unknown sender")
	ErrTransPackaging       = errors.New("failed to create packet")
	ErrTransMarshalling     = errors.New("failed to marshal message")
	ErrTransOpened          = errors.New("transport is opened")
	ErrTransClosed          = errors.New("transport is closed")
	ErrTransWrite           = errors.New("failed write to remote")
	ErrTransWriteShort      = errors.New("short write to remote")
	ErrTransAddressDup      = errors.New("address already registered")
	ErrTransUnknownReceiver = errors.New("unknown receiver")
	ErrTransAddressInvalid  = errors.New("invalid network address")
	ErrTransInvalidConfig   = errors.New("invalid configuration type")
)

Error codes

View Source
var (
	ErrLookupFailed = errors.New("Lookup failed")
)

Error codes

View Source
var (
	// ErrMessageParse if message parsing from binary data failed
	ErrMessageParse = errors.New("failed to parse message")
)
View Source
var (
	// KBuckets is the number of entries in a bucket per address bit.
	KBuckets int = 20
)
View Source
var (
	NodeTick = 1 * time.Minute
)

constants

Functions

This section is empty.

Types

type Address

type Address struct {
	Data []byte `size:"32"` // address data of size "(ADDRESS_BITS+7)/8"
}

Address encapsulates data representing the object identifier.

func NewAddress

func NewAddress(b []byte) *Address

NewAddress creates a new address from a binary object

func NewAddressFromKey

func NewAddressFromKey(pub *ed25519.PublicKey) *Address

NewAddressFromKey generate an address from public key

func NewAddressFromString

func NewAddressFromString(addr string) (*Address, error)

NewAddressFromString recreates an address from string label

func (*Address) Distance

func (a *Address) Distance(b *Address) *math.Int

Distance returns the distance between two addresses. The distance metric is based on XOR'ing the address values.

func (*Address) Equals

func (a *Address) Equals(o *Address) bool

Equals checks if two addresses are the same

func (*Address) PublicKey

func (a *Address) PublicKey() *ed25519.PublicKey

PublicKey returns the public node key (from its address)

func (*Address) String

func (a *Address) String() string

String returns a human-readable address

type Bucket

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

Bucket is used to store nodes depending on their distance to a reference node (the local node usually). All addresses in one bucket have the same distance value. A bucket is ordered: LRU addresses are at the beginning of the list, the MRU addresses are at the end (Kademlia scheme)

func NewBucket

func NewBucket(n int) *Bucket

NewBucket returns a new bucket of given length.

func (*Bucket) Add

func (b *Bucket) Add(addr *Address) bool

Add address to bucket. Returns false if the bucket is full.

func (*Bucket) Contains

func (b *Bucket) Contains(addr *Address) int

Contains checks if address is already in the bucket and returns its index in the list (or -1 if not found)

func (*Bucket) Count

func (b *Bucket) Count() int

Count returns the number of addresses in bucket

func (*Bucket) Expired

func (b *Bucket) Expired(pos int) bool

Expired returns true if the indexed drop has expired.

func (*Bucket) MRU

func (b *Bucket) MRU(offs int) *Address

MRU returns the most recently used entries in bucket, offs=0 refers to latest entry, offs=1 to second-latest and so on. offset must be smaller then the number of addresses in the bucket.

func (*Bucket) Update

func (b *Bucket) Update(pos int, drop *drop)

Update changes and moves the address from position to end of list (tail) If addr is nil, the currently stored address is moved.

type BucketList

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

BucketList is a list of buckets (one for each address bit)

func NewBucketList

func NewBucketList(addr *Address, ping *PingService) *BucketList

NewBucketList returns a new BucketList with given address as reference point for distances.

func (*BucketList) Add

func (bl *BucketList) Add(addr *Address)

Add a new peer to the routing table (possibly)

func (*BucketList) Closest

func (bl *BucketList) Closest(n int) (res []*Address)

Closest returns the n closest nodes we know of The number of returned nodes can be smaller if the node does not know about that many more nodes. Addresses are ordered by distance and MRU.

func (*BucketList) Run

func (bl *BucketList) Run(ctx context.Context)

Run the processing loop for the bucket list.

type BucketListTask

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

BucketListTask describes a maintenance job on the bucket list

type Connector

type Connector interface {
	// Send packet to endpoint (low-level transport)
	Send(context.Context, net.Addr, *Packet) error

	// Listen to messages from transport
	Listen(context.Context, chan Message)

	// Learn network address of node address
	Learn(*Address, net.Addr) error

	// Resolve the network address of a node address
	Resolve(*Address) net.Addr

	// NewAddress to instaniate a new endpoint address
	NewAddress(string) (net.Addr, error)

	// Sample given number of nodes with network addresses
	// stored in cache.
	Sample(int, *Address) []*Address

	// Epoch step: perform periodic tasks
	Epoch(int)
}

Connector can send and receive message over a transport layer.

type Endpoint

type Endpoint struct {
	Addr *Address // address of node
	Endp *String  // listen address
}

Endpoint specifies a node on the network

func NewEndpoint

func NewEndpoint(addr *Address, endp string) *Endpoint

NewEndpoint creates a new instance for given address,endp pair

func (*Endpoint) Size

func (e *Endpoint) Size() uint16

Size of binary representation

func (*Endpoint) String

func (e *Endpoint) String() string

String returns human-readable endpoint description

type FindNodeMsg

type FindNodeMsg struct {
	MsgHeader

	Addr *Address // address of node we are looking for
}

FindNodeMsg for FIND_NODE requests

func (*FindNodeMsg) Set

func (m *FindNodeMsg) Set(addr *Address) *FindNodeMsg

Set the additional address field

func (*FindNodeMsg) String

func (m *FindNodeMsg) String() string

String returns human-readable message

type FindNodeRespMsg

type FindNodeRespMsg struct {
	MsgHeader

	// List of response entries
	List []*Endpoint `size:"*"`
}

FindNodeRespMsg for FIND_NODE responses

func (*FindNodeRespMsg) Add

func (m *FindNodeRespMsg) Add(e *Endpoint)

Add an lookup entry to the list

func (*FindNodeRespMsg) String

func (m *FindNodeRespMsg) String() string

String returns human-readable message

type HandlerList

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

HandlerList maps an integer to a message handler instance.

func NewHandlerList

func NewHandlerList() *HandlerList

NewHandlerList instantiates a new list of handler mappings.

func (*HandlerList) Add

func (r *HandlerList) Add(id int, f MessageHandler) error

Add a message handler for given integer to list.

func (*HandlerList) Handle

func (r *HandlerList) Handle(ctx context.Context, id int, m Message) (bool, error)

Handle message

func (*HandlerList) Remove

func (r *HandlerList) Remove(id int) error

Remove handler for given integer from list.

type LocalAddress

type LocalAddress struct {
	Name string
}

LocalAddress is the network address (net.Addr) of a local node.

func NewLocalAddress

func NewLocalAddress(name string) *LocalAddress

NewLocalAddress creates a new address with give name

func (*LocalAddress) Network

func (a *LocalAddress) Network() string

Network returns the network label of the address

func (*LocalAddress) String

func (a *LocalAddress) String() string

String returns the human-readable network address

type LocalConnector

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

LocalConnector is used in LocalTransport

func (*LocalConnector) Epoch added in v1.2.4

func (c *LocalConnector) Epoch(epoch int)

Epoch step: perform periodic tasks

func (*LocalConnector) Learn

func (c *LocalConnector) Learn(addr *Address, endp net.Addr) error

Learn network address of node address

func (*LocalConnector) Listen

func (c *LocalConnector) Listen(ctx context.Context, ch chan Message)

Listen to messages from "outside" not necessary in local transport

func (*LocalConnector) NewAddress

func (c *LocalConnector) NewAddress(endp string) (net.Addr, error)

NewAddress returns a new network address for the transport based on an endpoint specification.

func (*LocalConnector) Resolve

func (c *LocalConnector) Resolve(addr *Address) net.Addr

Resolve node address into a network address

func (*LocalConnector) Sample

func (c *LocalConnector) Sample(num int, skip *Address) []*Address

Sample returns a random collection of node/network address pairs this node has learned during up-time.

func (*LocalConnector) Send

func (c *LocalConnector) Send(ctx context.Context, dst net.Addr, pkt *Packet) error

Send a message locally.

type LocalTransport

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

LocalTransport handles all common transport functionality and is able to route messages to local nodes

func NewLocalTransport

func NewLocalTransport() *LocalTransport

NewLocalTransport instantiates a local transport implementation

func (*LocalTransport) Close added in v1.2.2

func (t *LocalTransport) Close() error

Close transport

func (*LocalTransport) Open added in v1.2.2

func (t *LocalTransport) Open(cfg TransportConfig) error

Open transport based on configuration

func (*LocalTransport) Register

func (t *LocalTransport) Register(ctx context.Context, n *Node, endp string) error

Register a node for participation in the transport layer.

type LookupService

type LookupService struct {
	ServiceImpl
}

LookupService to resolve node addresses (routing)

func NewLookupService

func NewLookupService() *LookupService

NewLookupService creates a new service instance

func (*LookupService) Lookup

func (s *LookupService) Lookup(ctx context.Context, addr *Address, resolver Query, timeout time.Duration) (res interface{}, err error)

Lookup with specific resolver logic to handle mutlitple lookup scenarios.

func (*LookupService) LookupNode

func (s *LookupService) LookupNode(ctx context.Context, addr *Address, timeout time.Duration) (entry *Endpoint, err error)

LookupNode a node endpoint address.

func (*LookupService) Name

func (s *LookupService) Name() string

Name is a human-readble and short service description like "PING"

func (*LookupService) NewMessage

func (s *LookupService) NewMessage(mt int) Message

NewMessage creates an empty service message of given type

func (*LookupService) Request

func (s *LookupService) Request(ctx context.Context, rcv, addr *Address, timeout time.Duration) (res []*Endpoint, err error)

Request resolves an address 'addr' on peer 'rcv' synchronously

func (*LookupService) Respond

func (s *LookupService) Respond(ctx context.Context, m Message) (bool, error)

Respond to a service request from peer.

type Message

type Message interface {
	// Header of message (common data)
	Header() *MsgHeader

	// Data returns the binary representation of the message
	Data() []byte

	// String returns a human-readable message
	String() string
}

Message interface

func NewFindNodeMsg

func NewFindNodeMsg() Message

NewFindNodeMsg creates an empty FIND_NODE message

func NewFindNodeRespMsg

func NewFindNodeRespMsg() Message

NewFindNodeRespMsg creates an empty FIND_NODE response

func NewPingMsg

func NewPingMsg() Message

NewPingMsg creates an empty PING request

func NewPongMsg

func NewPongMsg() Message

NewPongMsg creates an empty PONG response

func NewRelayMsg

func NewRelayMsg() Message

NewRelayMsg creates an empty forward message

type MessageFactory

type MessageFactory func([]byte) (Message, error)

MessageFactory reassembles messages from binary data

type MessageHandler

type MessageHandler func(context.Context, Message) (bool, error)

MessageHandler is a (async) function that handles a message

type MsgHeader

type MsgHeader struct {
	Size     uint16 `order:"big"` // Size of message (including size)
	Type     uint16 `order:"big"` // Message type (see constants)
	Flags    uint32 `order:"big"` // Message flags (see constants)
	TxID     uint64 `order:"big"` // transaction identifier
	Sender   *Address
	Receiver *Address
}

MsgHeader (common header for requests and responses, 80 bytes)

func (*MsgHeader) Data

func (m *MsgHeader) Data() []byte

Data returns the binary representation the message

func (*MsgHeader) Header

func (m *MsgHeader) Header() *MsgHeader

Header returns the common message header data structure

type NewMessage

type NewMessage func() Message

NewMessage returns a message (of specific type)

type Node

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

Node represents a local network peer

func NewNode

func NewNode(prv *ed25519.PrivateKey) (n *Node, err error)

NewNode instantiates a new local node with given private key.

func (*Node) AddService

func (n *Node) AddService(s Service) bool

AddService to add a new service running on the node.

func (*Node) Address

func (n *Node) Address() *Address

Address returns the P2P address of a node

func (*Node) Closest

func (n *Node) Closest(num int) []*Address

Closest returns the 'num' closest nodes we know of

func (*Node) Connect

func (n *Node) Connect(c Connector) error

Connect to the transport network with given connector.

func (*Node) Handle

func (n *Node) Handle() chan Message

Handle messages from channel

func (*Node) Learn

func (n *Node) Learn(addr *Address, endp string) error

Learn about a new peer in the network

func (*Node) LookupService added in v1.2.2

func (n *Node) LookupService() *LookupService

LookupService returns the PING service instance

func (*Node) NewNetworkAddr

func (n *Node) NewNetworkAddr(endp string) (net.Addr, error)

NewNetworkAddr returns the network address for endpoint (transport-specific)

func (*Node) NextID added in v1.2.2

func (n *Node) NextID() uint64

NextID returns the next unique identifier for this node context

func (*Node) Pack

func (n *Node) Pack(msg Message) ([]byte, error)

Pack message into a byte array

func (*Node) PingService added in v1.2.2

func (n *Node) PingService() *PingService

PingService returns the PING service instance

func (*Node) RelayService added in v1.2.2

func (n *Node) RelayService() *RelayService

RelayService returns the RELAY service instance

func (*Node) RelayedMessage

func (n *Node) RelayedMessage(msg Message, peers []*Address) (Message, error)

RelayedMessage creates a nested relay message

func (*Node) Resolve

func (n *Node) Resolve(addr *Address) net.Addr

Resolve peer address to network address This will only deliver a result if the address has been learned before by the transport connector. Unknown endpoints must be resolved with the LookupService.

func (*Node) Run

func (n *Node) Run(ctx context.Context)

Run the local node

func (*Node) Sample

func (n *Node) Sample(num int, skip *Address) []*Address

Sample returns a random collection of node/network address pairs this node has learned during up-time.

func (*Node) Send

func (n *Node) Send(ctx context.Context, msg Message) (err error)

Send a message from this node to a peer on the network

func (*Node) SendRaw

func (n *Node) SendRaw(ctx context.Context, dst net.Addr, pkt *Packet) error

SendRaw message from this node to a peer on the network

func (*Node) Service

func (n *Node) Service(name string) Service

Service returns the named service instance for node. Useful for external services that are unknown within the framework.

func (*Node) Unpack

func (n *Node) Unpack(buf []byte, size int) (msg Message, err error)

Unpack byte array into a message

func (*Node) Unwrap

func (n *Node) Unwrap(pkt *Packet) (msg Message, err error)

Unwrap packet into a message

func (*Node) Wrap

func (n *Node) Wrap(msg Message) (pkt *Packet, err error)

Wrap message into a packet

type Packet

type Packet struct {
	Size uint16 `order:"big"` // size of packet (including this field)
	KXT  []byte `size:"32"`   // Key Exchange Token
	Body []byte `size:"*"`    // encrypted body
}

Packet data structure

func NewPacket

func NewPacket(msg Message, skey *ed25519.PrivateKey) (*Packet, error)

NewPacket creates a new packet from a message.

func NewPacketFromData

func NewPacketFromData(buf []byte, sender *ed25519.PrivateKey, receiver *ed25519.PublicKey) (*Packet, error)

NewPacketFromData creates a new packet from a binary object.

func (*Packet) Unpack

func (p *Packet) Unpack(receiver *ed25519.PrivateKey) ([]byte, error)

Unpack a packet

func (*Packet) Unwrap

func (p *Packet) Unwrap(receiver *ed25519.PrivateKey, mf MessageFactory) (Message, error)

Unwrap a packet

type PingMsg

type PingMsg struct {
	MsgHeader
}

PingMsg for PING requests

func (*PingMsg) String

func (m *PingMsg) String() string

String returns human-readable message

type PingService

type PingService struct {
	ServiceImpl
}

PingService responds to PING requests from remote peer

func NewPingService

func NewPingService() *PingService

NewPingService creates a new service instance

func (*PingService) Name

func (s *PingService) Name() string

Name is a human-readble and short service description like "PING"

func (*PingService) NewMessage

func (s *PingService) NewMessage(mt int) Message

NewMessage creates an empty service message of given type

func (*PingService) Ping

func (s *PingService) Ping(ctx context.Context, rcv *Address, timeout time.Duration, relays int) error

Ping sends a ping to another node and waits for a response (with timeout)

func (*PingService) Respond

func (s *PingService) Respond(ctx context.Context, m Message) (bool, error)

Respond to a service request from peer.

type PongMsg

type PongMsg struct {
	MsgHeader
}

PongMsg for PONG responses

func (*PongMsg) String

func (m *PongMsg) String() string

String returns human-readable message

type Query

type Query func(ctx context.Context, peer, addr *Address) interface{}

Query remote peer for given address; result depends on query implementation. It is either an error, a boolean "done" signal (no result) or a result instance. In this service the "final" result is of type Endoint; other services (like DHT) can use their own results (Value). If the result is an address list, the referenced nodes are queried for a result.

type RelayMsg

type RelayMsg struct {
	MsgHeader

	NextHop *Endpoint // next hop address
	Pkt     *Packet   // packet to deliver to next hop
}

RelayMsg is used to forward packets to nodes. The forwarded packet can itself be a RelayMsg, thus allowing a nested relay path (onion-like)

func (*RelayMsg) Set

func (m *RelayMsg) Set(e *Endpoint, pkt *Packet)

Set the forward parameters

func (*RelayMsg) String

func (m *RelayMsg) String() string

String returns human-readable message

type RelayService

type RelayService struct {
	ServiceImpl
}

RelayService to forward messages to other nodes.

func NewRelayService

func NewRelayService() *RelayService

NewRelayService creates a new service instance

func (*RelayService) Name

func (s *RelayService) Name() string

Name is a human-readble and short service description like "PING"

func (*RelayService) NewMessage

func (s *RelayService) NewMessage(mt int) Message

NewMessage creates an empty service message of given type

func (*RelayService) Relay

func (s *RelayService) Relay(ctx context.Context, msg *RelayMsg) error

Relay message to next hop

func (*RelayService) Respond

func (s *RelayService) Respond(ctx context.Context, m Message) (ok bool, err error)

Respond to a service request from peer.

type Service

type Service interface {
	// Name is a human-readble and short service description like "PING"
	Name() string

	// Node the service is running on
	Node() *Node

	// Respond to a service request from peer.
	// The bool return value indicates whether the message has been processed
	// or not and if there was an error message with it.
	Respond(context.Context, Message) (bool, error)

	// Listen to service responses from peer.
	// The bool return value indicates whether the message has been processed
	// or not and if there was an error message with it.
	Listen(context.Context, Message) (bool, error)

	// Send a message to network
	Send(context.Context, Message) error

	// NewMessage creates an empty service message of given type.
	// The type must be known to the service.
	NewMessage(mt int) Message
	// contains filtered or unexported methods
}

Service is running on a node and provides: - a responder for requests with matching message type - a listener for service messages with matching TxId Service is the building block for upper levels of the protocol stack.

type ServiceImpl

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

ServiceImpl is a basic implementation of a Service instance and the building block for custom services implementing new functionality.

func NewServiceImpl

func NewServiceImpl() *ServiceImpl

NewServiceImpl returns a new basic service instance (NOP)

func (*ServiceImpl) Listen

func (s *ServiceImpl) Listen(ctx context.Context, msg Message) (bool, error)

Listen to response message

func (*ServiceImpl) MessageFactory

func (s *ServiceImpl) MessageFactory(mt int) Message

MessageFactory returns an empty message for a given type

func (*ServiceImpl) Node

func (s *ServiceImpl) Node() *Node

Node returns the node the service is running on

func (*ServiceImpl) Respond

func (s *ServiceImpl) Respond(ctx context.Context, msg Message) (bool, error)

Respond to a service request

func (*ServiceImpl) Send

func (s *ServiceImpl) Send(ctx context.Context, msg Message) error

Send a message from this node to the network

func (*ServiceImpl) Task

func (s *ServiceImpl) Task(ctx context.Context, m Message, f *TaskHandler) (err error)

Task is a generic wrapper for tasks running on a local node. It sends an initial message 'm'; responses are handled by 'f'. If 'f' returns true, no further responses from the receiver are expected.

type ServiceList

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

ServiceList for all services registered on a node

func NewServiceList

func NewServiceList() *ServiceList

NewServiceList returns a new list instance

func (*ServiceList) Add

func (sl *ServiceList) Add(s Service) bool

Add service to list

func (*ServiceList) Get

func (sl *ServiceList) Get(name string) Service

Get service from list

func (*ServiceList) Listen

func (sl *ServiceList) Listen(ctx context.Context, msg Message) (bool, error)

Listen to service responses

func (*ServiceList) MessageFactory

func (sl *ServiceList) MessageFactory(buf []byte) (msg Message, err error)

MessageFactory re-creates a message from binary data.

func (*ServiceList) Respond

func (sl *ServiceList) Respond(ctx context.Context, msg Message) (bool, error)

Respond to service requests

type String

type String struct {
	Len  uint16 `order:"big"` // length of string
	Data []byte `size:"Len"`  // string data
}

String is a sequence of unicode runes in binary format

func NewString

func NewString(s string) *String

NewString encapsulates a standard string

func (*String) Size

func (s *String) Size() uint16

Size returns the total size of String in bytes

func (*String) String

func (s *String) String() string

String returns the standard string object

type TaskHandler

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

TaskHandler is used to notify listener of messages during task processing

type TorAddress added in v1.2.2

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

TorAddress is an onion address as each node is listing for incoming packets as a "hidden service".

func NewTorAddress added in v1.2.2

func NewTorAddress(addr *Address) (*TorAddress, error)

NewTorAddress creates a new onion address from the P2P address of a node.

func (*TorAddress) Network added in v1.2.2

func (a *TorAddress) Network() string

Network returns the network label of the address

func (*TorAddress) String added in v1.2.2

func (a *TorAddress) String() string

String returns the human-readable network address

type TorConnection added in v1.2.4

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

TorConnection is an open TCP connection to a hidden servoice of a peer

func (*TorConnection) Expired added in v1.2.4

func (c *TorConnection) Expired() bool

Expired connection?

type TorConnector added in v1.2.2

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

TorConnector is a stub between a node and the Tor-based transport implementation.

func NewTorConnector added in v1.2.2

func NewTorConnector(trans *TorTransport, node *Node, port int) (*TorConnector, error)

NewTorConnector creates a connector on transport for a given node

func (*TorConnector) Epoch added in v1.2.4

func (c *TorConnector) Epoch(epoch int)

Epoch step: perform periodic tasks

func (*TorConnector) Learn added in v1.2.2

func (c *TorConnector) Learn(addr *Address, endp net.Addr) error

Learn network address of node address is obsolete if Tor transport is used; the network address can be computed from the P2P address.

func (*TorConnector) Listen added in v1.2.2

func (c *TorConnector) Listen(ctx context.Context, ch chan Message)

Listen on an UDP address/port for incoming packets

func (*TorConnector) NewAddress added in v1.2.2

func (c *TorConnector) NewAddress(endp string) (net.Addr, error)

NewAddress returns a new onion address for an endpoint

func (*TorConnector) Resolve added in v1.2.2

func (c *TorConnector) Resolve(addr *Address) net.Addr

Resolve node address into a network address is a deterministic function if Tor transport is used.

func (*TorConnector) Sample added in v1.2.2

func (c *TorConnector) Sample(num int, skip *Address) []*Address

Sample returns a random collection of node/network address pairs this node has learned during up-time.

func (*TorConnector) Send added in v1.2.2

func (c *TorConnector) Send(ctx context.Context, dst net.Addr, pkt *Packet) (err error)

Send message from node to the UDP network.

type TorTransport added in v1.2.2

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

TorTransport handles the transport of packets between nodes over Tor curcuits / hidden services.

func NewTorTransport added in v1.2.2

func NewTorTransport() *TorTransport

NewTorTransport instantiates a new Tor transport layer where the listening socket is bound to the specified address (host:port).

func (*TorTransport) Close added in v1.2.2

func (t *TorTransport) Close() error

Close transport

func (*TorTransport) Open added in v1.2.2

func (t *TorTransport) Open(cfg TransportConfig) (err error)

Open transport based on configuration

func (*TorTransport) Register added in v1.2.2

func (t *TorTransport) Register(ctx context.Context, n *Node, endp string) (err error)

Register a node for participation in the transport layer. The 'endp' argument is ignored as the network address of the node is computed from the P2P address of the node.

type TorTransportConfig added in v1.2.2

type TorTransportConfig struct {
	// Ctrl specifies the Tor control interface. It is formatted like
	// "network:endp", where 'network' is either "tcp" (for TCP/IP) or
	// "unix" (for a local Unix socket). 'endp' specifies the endpoint
	// depending on the network; it is "host:port" for "tcp" and a file
	// path in case of a local Unix socket.
	// The same host (either "host" as deined or localhos) must have the
	// defined SOCKS ports open for access to Tor cuircuits.
	Ctrl string `json:"ctrl"`
	// Auth is the authentication password/cookie. If a cookie is used
	// (only applicable for local Tor service instances), the value is
	// set dynamically (and not in a persistent configuration file).
	Auth string `json:"auth"`
	// HSHost refers to the host running hidden service endpoints
	HSHost string `json:"hshost"`
	// PeerTTL defines (in seconds) how long connections are kept-alive
	// after a message has been send.
	PeerTTL int `json:"peerTTL"`
}

TorTransportConfig specifies the configuration parameters required to instantiate a Tor-based transport for the P2P network.

func (*TorTransportConfig) TransportType added in v1.2.2

func (c *TorTransportConfig) TransportType() string

TransportType returns the kind of transport implementation targeted by the configuration information.

type Transport

type Transport interface {
	// Open transport based on configuration
	Open(TransportConfig) error
	// Register a node for participation in this transport
	Register(context.Context, *Node, string) error
	// Close transport
	Close() error
}

Transport abstraction: Every endpoint (on a local machine) registers with its address and receives channels for communication (incoming and outgoing messages). The transfer process needs to be started with the 'Run()' method for its message pump to work.

type TransportConfig added in v1.2.2

type TransportConfig interface {
	TransportType() string // return type of associated transport
}

TransportConfig is used for transport-specific configurations

type UDPConnector

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

UDPConnector is a stub between a node and the UDP transport implementation.

func NewUDPConnector

func NewUDPConnector(trans *UDPTransport, node *Node, addr *net.UDPAddr) *UDPConnector

NewUDPConnector creates an empty instance for node at given network address

func (*UDPConnector) Epoch added in v1.2.4

func (c *UDPConnector) Epoch(epoch int)

Epoch step: perform periodic tasks

func (*UDPConnector) Learn

func (c *UDPConnector) Learn(addr *Address, endp net.Addr) error

Learn network address of node address

func (*UDPConnector) Listen

func (c *UDPConnector) Listen(ctx context.Context, ch chan Message)

Listen on an UDP address/port for incoming packets

func (*UDPConnector) NewAddress

func (c *UDPConnector) NewAddress(endp string) (net.Addr, error)

NewAddress returns a new UDP address for an endpoint

func (*UDPConnector) Resolve

func (c *UDPConnector) Resolve(addr *Address) net.Addr

Resolve node address into a network address

func (*UDPConnector) Sample

func (c *UDPConnector) Sample(num int, skip *Address) []*Address

Sample returns a random collection of node/network address pairs this node has learned during up-time.

func (*UDPConnector) Send

func (c *UDPConnector) Send(ctx context.Context, dst net.Addr, pkt *Packet) error

Send message from node to the UDP network.

type UDPTransport

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

UDPTransport handles the transport of packets between nodes over the internet using the UDP protocol.

func NewUDPTransport

func NewUDPTransport() *UDPTransport

NewUDPTransport instantiates a new UDP transport layer where the listening socket is bound to the specified address (host:port).

func (*UDPTransport) Close added in v1.2.2

func (t *UDPTransport) Close() error

Close transport

func (*UDPTransport) Open added in v1.2.2

func (t *UDPTransport) Open(cfg TransportConfig) error

Open transport based on configuration

func (*UDPTransport) Register

func (t *UDPTransport) Register(ctx context.Context, n *Node, endp string) error

Register a node for participation in the transport layer.

Jump to

Keyboard shortcuts

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