kadv6

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BootstrapReqOp     byte = 0x01
	BootstrapResOp     byte = 0x09
	HelloReqOp         byte = 0x11
	HelloResOp         byte = 0x19
	FindNodeReqOp      byte = 0x21
	FindNodeResOp      byte = 0x29
	SearchKeysReqOp    byte = 0x33
	SearchSrcReqOp     byte = 0x34
	SearchNotesReqOp   byte = 0x35
	SearchResOp        byte = 0x3B
	PublishKeysReqOp   byte = 0x43
	PublishSourceReqOp byte = 0x44
	PublishNotesReqOp  byte = 0x45
	PublishResOp       byte = 0x4B
	PingOp             byte = 0x60
	PongOp             byte = 0x61

	FindValue byte = 0x02
	Store     byte = 0x04
	FindNode  byte = 0x0B
)
View Source
const (
	// ProtocolHeader marks KADV6 UDP packets.
	ProtocolHeader byte = 0xE6
	// KademliaVersion is the first published KADV6 protocol version.
	KademliaVersion byte = 0x01

	// Tag types use a simpler typed tag encoding than legacy Kad.
	TagTypeString byte = 0x02
	TagTypeUint32 byte = 0x03
	TagTypeUint16 byte = 0x08
	TagTypeUint8  byte = 0x09
	TagTypeUint64 byte = 0x0B
	TagTypeBytes  byte = 0x0C

	// Source-related tag identifiers.
	TagName          byte = 0x01
	TagFileSize      byte = 0xD3
	TagAddrFamily    byte = 0xEE
	TagSourceIP6     byte = 0xF0
	TagSourceUDPPort byte = 0xFC
	TagSourcePort    byte = 0xFD
	TagSourceType    byte = 0xFF

	// Supported address family in KADV6.
	AddrFamilyIPv6 byte = 6
)

Variables

This section is empty.

Functions

func DecodePacket

func DecodePacket(packet []byte) (byte, []byte, error)

DecodePacket extracts the opcode and payload from a packet.

func DistanceCompare

func DistanceCompare(a, b, target ID) int

DistanceCompare compares XOR distances to the target.

Types

type BootstrapReq

type BootstrapReq struct{}

BootstrapReq asks a known node for contacts.

func (BootstrapReq) Pack

func (BootstrapReq) Pack() ([]byte, error)

Pack encodes the request.

type BootstrapRes

type BootstrapRes struct {
	ID       ID
	TCPPort  uint16
	Version  byte
	Contacts []EntryV6
}

BootstrapRes returns contacts near the responder.

func (BootstrapRes) Pack

func (b BootstrapRes) Pack() ([]byte, error)

Pack encodes the response payload.

func (*BootstrapRes) Unpack

func (b *BootstrapRes) Unpack(payload []byte) error

Unpack decodes the response payload.

type EndpointV6

type EndpointV6 struct {
	IP      [16]byte
	UDPPort uint16
	TCPPort uint16
}

EndpointV6 stores a native IPv6 endpoint for KADV6 contacts.

func EndpointFromIP

func EndpointFromIP(ip net.IP, udpPort, tcpPort uint16) (EndpointV6, error)

EndpointFromIP builds an endpoint from a native IPv6 address.

func EndpointFromUDPAddr

func EndpointFromUDPAddr(addr *net.UDPAddr, tcpPort uint16) (EndpointV6, error)

EndpointFromUDPAddr builds an endpoint from a UDP address plus advertised TCP port.

func (*EndpointV6) Get

func (e *EndpointV6) Get(src *bytes.Reader) error

Get decodes the endpoint from a reader.

func (EndpointV6) IPNet

func (e EndpointV6) IPNet() net.IP

IPNet returns a copy of the stored IPv6 address as net.IP.

func (EndpointV6) Put

func (e EndpointV6) Put(dst *bytes.Buffer) error

Put encodes the endpoint into a buffer.

func (EndpointV6) String

func (e EndpointV6) String() string

String renders the endpoint in bracketed IPv6 form.

func (EndpointV6) TCPAddr

func (e EndpointV6) TCPAddr() *net.TCPAddr

TCPAddr converts the endpoint to a TCP address.

func (EndpointV6) UDPAddr

func (e EndpointV6) UDPAddr() *net.UDPAddr

UDPAddr converts the endpoint to a UDP address.

func (EndpointV6) Validate

func (e EndpointV6) Validate() error

Validate checks whether the endpoint can be routed inside KADV6.

type EntryV6

type EntryV6 struct {
	ID       ID
	Endpoint EndpointV6
	Version  byte
	Verified bool
}

EntryV6 is a KADV6 routing entry.

func (*EntryV6) Get

func (e *EntryV6) Get(src *bytes.Reader) error

Get decodes an entry from a reader.

func (EntryV6) Put

func (e EntryV6) Put(dst *bytes.Buffer) error

Put encodes an entry into a buffer.

type FindNodeReq

type FindNodeReq struct {
	SearchType byte
	Target     ID
	Receiver   ID
}

FindNodeReq asks for closer contacts to a target id.

func (FindNodeReq) Pack

func (r FindNodeReq) Pack() ([]byte, error)

Pack encodes a find-node request.

func (*FindNodeReq) Unpack

func (r *FindNodeReq) Unpack(payload []byte) error

Unpack decodes a find-node request.

type FindNodeRes

type FindNodeRes struct {
	Target  ID
	Results []EntryV6
}

FindNodeRes returns contacts for a target id.

func (FindNodeRes) Pack

func (r FindNodeRes) Pack() ([]byte, error)

Pack encodes a find-node response.

func (*FindNodeRes) Unpack

func (r *FindNodeRes) Unpack(payload []byte) error

Unpack decodes a find-node response.

type Hello

type Hello struct {
	ID      ID
	TCPPort uint16
	Version byte
	Tags    []Tag
}

Hello confirms node identity and service port.

func (Hello) Pack

func (h Hello) Pack(opcode byte) ([]byte, error)

Pack encodes the hello payload with the given opcode.

func (*Hello) Unpack

func (h *Hello) Unpack(payload []byte) error

Unpack decodes a hello payload.

type ID

type ID struct {
	protocol.Hash
}

ID is the KADV6 node identifier.

func NewID

func NewID(hash protocol.Hash) ID

NewID wraps a protocol hash as a KADV6 node id.

func (*ID) Get

func (i *ID) Get(src *bytes.Reader) error

Get decodes the node id from a reader.

func (ID) Put

func (i ID) Put(dst *bytes.Buffer) error

Put encodes the node id into a buffer.

type NodesDat

type NodesDat struct {
	Version          uint32
	BootstrapEdition uint32
	Contacts         []EntryV6
}

NodesDat stores bootstrap contacts for KADV6.

func LoadNodesDat

func LoadNodesDat(path string) (*NodesDat, error)

LoadNodesDat loads and parses a KADV6 nodes file from disk.

func ParseNodesDat

func ParseNodesDat(raw []byte) (*NodesDat, error)

ParseNodesDat decodes a KADV6 nodes file from raw bytes.

func (NodesDat) Pack

func (n NodesDat) Pack() ([]byte, error)

Pack encodes the nodes6.dat payload.

func (*NodesDat) Unpack

func (n *NodesDat) Unpack(payload []byte) error

Unpack decodes the binary nodes6.dat payload.

type PacketCombiner

type PacketCombiner struct{}

PacketCombiner packs and unpacks KADV6 packets by opcode.

func (PacketCombiner) Pack

func (PacketCombiner) Pack(packet any, extra ...byte) ([]byte, error)

Pack encodes a typed message into a full packet.

func (PacketCombiner) Unpack

func (PacketCombiner) Unpack(packet []byte) (byte, any, error)

Unpack decodes a full packet into a typed message.

type Ping

type Ping struct{}

Ping is an empty liveness probe.

func (Ping) Pack

func (Ping) Pack() ([]byte, error)

Pack encodes a ping packet.

type Pong

type Pong struct {
	UDPPort uint16
}

Pong returns the active UDP port.

func (Pong) Pack

func (p Pong) Pack() ([]byte, error)

Pack encodes a pong payload.

func (*Pong) Unpack

func (p *Pong) Unpack(payload []byte) error

Unpack decodes a pong payload.

type PublishKeysReq

type PublishKeysReq struct {
	KeywordID ID
	Sources   []SearchEntry
}

PublishKeysReq publishes keyword records.

func (PublishKeysReq) Pack

func (p PublishKeysReq) Pack() ([]byte, error)

Pack encodes a keyword publish request.

func (*PublishKeysReq) Unpack

func (p *PublishKeysReq) Unpack(payload []byte) error

Unpack decodes a keyword publish request.

type PublishNotesReq

type PublishNotesReq struct {
	FileID ID
	Notes  []SearchEntry
}

PublishNotesReq publishes note records.

func (PublishNotesReq) Pack

func (p PublishNotesReq) Pack() ([]byte, error)

Pack encodes a notes publish request.

func (*PublishNotesReq) Unpack

func (p *PublishNotesReq) Unpack(payload []byte) error

Unpack decodes a notes publish request.

type PublishRes

type PublishRes struct {
	FileID ID
	Count  byte
}

PublishRes acknowledges one or more published records.

func (PublishRes) Pack

func (p PublishRes) Pack() ([]byte, error)

Pack encodes a publish response.

func (*PublishRes) Unpack

func (p *PublishRes) Unpack(payload []byte) error

Unpack decodes a publish response.

type PublishSourcesReq

type PublishSourcesReq struct {
	FileID ID
	Source SearchEntry
}

PublishSourcesReq publishes one source record.

func (PublishSourcesReq) Pack

func (p PublishSourcesReq) Pack() ([]byte, error)

Pack encodes a source publish request.

func (*PublishSourcesReq) Unpack

func (p *PublishSourcesReq) Unpack(payload []byte) error

Unpack decodes a source publish request.

type SearchEntry

type SearchEntry struct {
	ID   ID
	Tags []Tag
}

SearchEntry stores source, keyword, or note metadata.

func (SearchEntry) BytesTag

func (s SearchEntry) BytesTag(id byte) ([]byte, bool)

BytesTag returns the first matching raw-bytes tag.

func (*SearchEntry) Get

func (s *SearchEntry) Get(src *bytes.Reader) error

Get decodes a search entry from a reader.

func (SearchEntry) Put

func (s SearchEntry) Put(dst *bytes.Buffer) error

Put encodes a search entry into a buffer.

func (SearchEntry) SourceAddr

func (s SearchEntry) SourceAddr() (*net.TCPAddr, bool)

SourceAddr extracts a direct IPv6 TCP source from the search entry.

func (SearchEntry) StringTag

func (s SearchEntry) StringTag(id byte) (string, bool)

StringTag returns the first matching string tag.

func (SearchEntry) UIntTag

func (s SearchEntry) UIntTag(id byte) (uint64, bool)

UIntTag returns the first matching unsigned integer tag.

type SearchKeysReq

type SearchKeysReq struct {
	Target   ID
	StartPos uint16
}

SearchKeysReq requests keyword records.

func (SearchKeysReq) Pack

func (s SearchKeysReq) Pack() ([]byte, error)

Pack encodes a keyword search request.

func (*SearchKeysReq) Unpack

func (s *SearchKeysReq) Unpack(payload []byte) error

Unpack decodes a keyword search request.

type SearchNotesReq

type SearchNotesReq struct {
	Target   ID
	StartPos uint16
}

SearchNotesReq requests note records.

func (SearchNotesReq) Pack

func (s SearchNotesReq) Pack() ([]byte, error)

Pack encodes a notes search request.

func (*SearchNotesReq) Unpack

func (s *SearchNotesReq) Unpack(payload []byte) error

Unpack decodes a notes search request.

type SearchRes

type SearchRes struct {
	Source  ID
	Target  ID
	Results []SearchEntry
}

SearchRes returns search entries for a target.

func (SearchRes) Pack

func (s SearchRes) Pack() ([]byte, error)

Pack encodes a search response.

func (*SearchRes) Unpack

func (s *SearchRes) Unpack(payload []byte) error

Unpack decodes a search response.

type SearchSourcesReq

type SearchSourcesReq struct {
	Target   ID
	StartPos uint16
	Size     uint64
}

SearchSourcesReq requests source records for a file.

func (SearchSourcesReq) Pack

func (s SearchSourcesReq) Pack() ([]byte, error)

Pack encodes a source search request.

func (*SearchSourcesReq) Unpack

func (s *SearchSourcesReq) Unpack(payload []byte) error

Unpack decodes a source search request.

type Tag

type Tag struct {
	Type   byte
	ID     byte
	UInt64 uint64
	String string
	Bytes  []byte
}

Tag stores one typed metadata value.

func (*Tag) Get

func (t *Tag) Get(src *bytes.Reader) error

Get decodes a tag from a reader.

func (Tag) Put

func (t Tag) Put(dst *bytes.Buffer) error

Put encodes a tag into a buffer.

Jump to

Keyboard shortcuts

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