rlpx

package
v0.0.0-...-37ff43f Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package rlpx implements the RLPx transport protocol.

Index

Constants

View Source
const (
	PacketTypeHandshake       PacketType = 21
	PacketTypeApplicationData PacketType = 23
	ReadTimeout                          = time.Second * 10
	WriteTimeout                         = time.Second * 20
)

Variables

This section is empty.

Functions

func CalculateNonce

func CalculateNonce(recordCount uint, input []byte) []byte

func Encrypt

func Encrypt(cipher1 cipher.AEAD, fragment []byte, additionalData []byte, packetType PacketType, handshakeIv []byte, seqNum uint) (encrypted []byte, err error)

func HkdfExpandLabel

func HkdfExpandLabel(secret []byte, label string, hashVal []byte, outputLength int) ([]byte, error)

Types

type AdditionalData

type AdditionalData struct {
	PacketType   uint
	MinorVersion uint
	MajorVersion uint
	DataLength   uint
	Rest         []rlp.RawValue `rlp:"tail"`
}

type Client

type Client struct {
	Nonce uint

	RecordCount uint
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn io.ReadWriter, clientSigningPrivateKey *signaturealgorithm.PrivateKey, serverSigningPublicKey *signaturealgorithm.PublicKey, context string) *Client

func (*Client) Cleanup

func (c *Client) Cleanup()

func (*Client) InitWithSecrets

func (c *Client) InitWithSecrets(secret SessionSecret)

func (*Client) PerformHandshake

func (c *Client) PerformHandshake() error

func (*Client) ReadAndDecrypt

func (c *Client) ReadAndDecrypt(packetType PacketType) (*DataPacket, error)

func (*Client) ReadAndDecryptMessage

func (c *Client) ReadAndDecryptMessage(msg interface{}, packetType PacketType) error

func (*Client) SetClientSigningPrivateKey

func (c *Client) SetClientSigningPrivateKey(clientSigningPrivateKey *signaturealgorithm.PrivateKey)

func (*Client) SetServer

func (c *Client) SetServer(server *Server)

func (*Client) SetServerSigningPublicKey

func (c *Client) SetServerSigningPublicKey(serverSigningPublicKey *signaturealgorithm.PublicKey)

func (*Client) WriteEncrypted

func (c *Client) WriteEncrypted(data []byte, context uint64, packetType PacketType) error

type Conn

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

Conn is an RLPx network connection. It wraps a low-level network connection. The underlying connection should not be used for other activity when it is wrapped by Conn.

Before sending messages, a handshake must be performed by calling the Handshake method. This type is not generally safe for concurrent use, but reading and writing of messages may happen concurrently after the handshake.

func NewConn

func NewConn(conn net.Conn, dialDest *signaturealgorithm.PublicKey, context string) *Conn

NewConn wraps the given network connection. If dialDest is non-nil, the connection behaves as the initiator during the handshake.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the underlying network connection.

func (*Conn) Handshake

Handshake performs the handshake. This must be called before any data is written or read from the connection.

func (*Conn) InitWithSecrets

func (c *Conn) InitWithSecrets(secret SessionSecret)

func (*Conn) Read

func (c *Conn) Read() (code uint64, data []byte, wireSize int, err error)

Read reads a message from the connection. The returned data buffer is valid until the next call to Read.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(deadlineTime time.Time) error

SetDeadline sets the deadline for all future read and write operations.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(deadlineTime time.Time) error

SetReadDeadline sets the deadline for all future read operations.

func (*Conn) SetSnappy

func (c *Conn) SetSnappy(snappy bool)

SetSnappy enables or disables snappy compression of messages. This is usually called after the devp2p Hello message exchange when the negotiated version indicates that compression is available on both ends of the connection.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(deadlineTime time.Time) error

SetWriteDeadline sets the deadline for all future write operations.

func (*Conn) Write

func (c *Conn) Write(code uint64, data []byte) (uint32, error)

Write writes a message to the connection.

Write returns the written size of the message data. This may be less than or equal to len(data) depending on whether snappy compression is enabled.

type DataPacket

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

func Decrypt

func Decrypt(cipher1 cipher.AEAD, encryptedData []byte, additionalData []byte, packetType PacketType, iv []byte, seqNum uint) (*DataPacket, error)
type Header struct {
	PacketType     uint
	MinorVersion   uint
	MajorVersion   uint
	RecordLength   uint
	Context        uint64
	AdditionalData [common.HashLength]byte
	Rest           []rlp.RawValue `rlp:"tail"`
}

type PacketType

type PacketType byte

type ReadBuffer

type ReadBuffer struct {
	Data []byte
	// contains filtered or unexported fields
}

ReadBuffer implements buffering for network reads. This type is similar to bufio.Reader, with two crucial differences: the buffer slice is exposed, and the buffer keeps all read data available until reset.

How to use this type:

Keep a ReadBuffer b alongside the underlying network connection. When reading a packet from the connection, first call b.reset(). This empties b.data. Now perform reads through b.read() until the end of the packet is reached. The complete packet data is now available in b.data.

func (*ReadBuffer) Grow

func (b *ReadBuffer) Grow(n int)

Grow ensures the buffer has at least n bytes of unused space.

func (*ReadBuffer) Read

func (b *ReadBuffer) Read(r io.Reader, n int) ([]byte, error)

Read reads at least n bytes from r, returning the bytes. The returned slice is valid until the next call to Reset.

func (*ReadBuffer) Reset

func (b *ReadBuffer) Reset()

Reset removes all processed data which was read since the last call to Reset. After Reset, len(b.data) is zero.

type RlpxSerializer

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

func (*RlpxSerializer) Deserialize

func (rs *RlpxSerializer) Deserialize(msg interface{}, reader io.Reader) ([]byte, error)

func (*RlpxSerializer) Serialize

func (rs *RlpxSerializer) Serialize(msg interface{}) ([]byte, error)

func (*RlpxSerializer) SerializeDeterministic

func (rs *RlpxSerializer) SerializeDeterministic(msg interface{}, padLen int) ([]byte, error)

func (*RlpxSerializer) SetContext

func (rs *RlpxSerializer) SetContext(context string)

type Serializer

type Serializer interface {
	Serialize(msg interface{}) ([]byte, error)
	SerializeDeterministic(msg interface{}, padLen int) ([]byte, error)
	Deserialize(msg interface{}, reader io.Reader) ([]byte, error)
	SetContext(context string)
}

func NewRlpxSerializer

func NewRlpxSerializer() Serializer

type Server

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

func NewServer

func NewServer(conn io.ReadWriter, serverSigningPrivateKey *signaturealgorithm.PrivateKey, context string) *Server

func (*Server) Cleanup

func (s *Server) Cleanup()

func (*Server) InitWithSecrets

func (s *Server) InitWithSecrets(secret SessionSecret)

func (*Server) PerformHandshake

func (s *Server) PerformHandshake() error

func (*Server) Read

func (s *Server) Read() error

func (*Server) ReadAndDecrypt

func (s *Server) ReadAndDecrypt(packetType PacketType) (*DataPacket, error)

func (*Server) ReadAndDecryptMessage

func (s *Server) ReadAndDecryptMessage(msg interface{}, packetType PacketType) error

func (*Server) SetClient

func (s *Server) SetClient(client *Client)

func (*Server) SetServerSigningPrivateKey

func (s *Server) SetServerSigningPrivateKey(serverSigningPrivateKey *signaturealgorithm.PrivateKey)

func (*Server) WriteEncrypted

func (s *Server) WriteEncrypted(data []byte, context uint64, packetType PacketType) error

type SessionSecret

type SessionSecret struct {
	ClientHandshakeKey []byte
	ServerHandshakeKey []byte
	ClientHandshakeIv  []byte
	ServerHandshakeIv  []byte

	ClientApplicationKey []byte
	ServerApplicationKey []byte
	ClientApplicationIv  []byte
	ServerApplicationIv  []byte

	ClientHandshakeCipher cipher.AEAD
	ServerHandshakeCipher cipher.AEAD

	ClientApplicationCipher cipher.AEAD
	ServerApplicationCipher cipher.AEAD

	TranscriptHash []byte
	// contains filtered or unexported fields
}

func NewSessionSecret

func NewSessionSecret(transcriptHash []byte, sharedSecret []byte) (*SessionSecret, error)

func (*SessionSecret) CreateApplicationSecrets

func (ss *SessionSecret) CreateApplicationSecrets(transcriptHash []byte) error

type WriteBuffer

type WriteBuffer struct {
	Data []byte
}

WriteBuffer implements buffering for network writes. This is essentially a convenience wrapper around a byte slice.

func (*WriteBuffer) AppendZero

func (b *WriteBuffer) AppendZero(n int) []byte

func (*WriteBuffer) Reset

func (b *WriteBuffer) Reset()

func (*WriteBuffer) Write

func (b *WriteBuffer) Write(data []byte) (int, error)

Jump to

Keyboard shortcuts

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