netaddr

package
v0.0.0-...-1d7e402 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package netaddr provides endpoint and transport addresses for go-iroh.

An EndpointAddr combines a key.EndpointID with the network-level TransportAddr values at which the endpoint may be reached. A transport address is a RelayAddr, IPAddr, or CustomAddr. A RelayURL identifies an iroh relay server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCustomAddrMissingSeparator = errors.New("missing '_' separator")
	ErrCustomAddrInvalidID        = errors.New("invalid ID")
	ErrCustomAddrInvalidData      = errors.New("invalid data")
	ErrCustomAddrTooShort         = errors.New("data too short")
)

CustomAddr parse/encode errors.

View Source
var ErrParseRelayURL = errors.New("failed to parse relay URL")

ErrParseRelayURL is returned (wrapped) when a relay URL cannot be parsed.

Functions

This section is empty.

Types

type CustomAddr

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

CustomAddr is a custom transport address: a freely-chosen u64 transport id plus opaque, unvalidated address data.

A registry of well-known transport ids is at https://github.com/n0-computer/iroh/blob/main/TRANSPORTS.md.

String encoding (CustomAddr.String, ParseCustomAddr): "<id>_<data>" where <id> is the transport ID as lowercase hex (no "0x", no leading zeros) and <data> is the address bytes as lowercase hex.

Binary encoding (CustomAddr.MarshalBinary, CustomAddr.UnmarshalBinary): 8-byte little-endian u64 ID followed by the raw data bytes (minimum 8 bytes).

func NewCustomAddr

func NewCustomAddr(id uint64, data []byte) CustomAddr

NewCustomAddr creates a CustomAddr from a transport ID and raw address data. The data is copied.

func ParseCustomAddr

func ParseCustomAddr(s string) (CustomAddr, error)

ParseCustomAddr parses a CustomAddr from its "<id>_<data>" string form. It also accepts the "custom:" prefix used by ParseTransportAddr.

func (CustomAddr) Compare

func (a CustomAddr) Compare(other TransportAddr) int

Compare orders custom addresses by numeric transport id, then by data bytes.

func (CustomAddr) Data

func (a CustomAddr) Data() []byte

Data returns the opaque address data.

func (CustomAddr) ID

func (a CustomAddr) ID() uint64

ID returns the transport ID.

func (CustomAddr) MarshalBinary

func (a CustomAddr) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler using the binary encoding described on CustomAddr.

func (CustomAddr) MarshalText

func (a CustomAddr) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler using the string encoding described on CustomAddr.

func (CustomAddr) Network

func (CustomAddr) Network() string

func (CustomAddr) String

func (a CustomAddr) String() string

func (*CustomAddr) UnmarshalBinary

func (a *CustomAddr) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler using the binary encoding described on CustomAddr.

func (*CustomAddr) UnmarshalText

func (a *CustomAddr) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler using the string encoding described on CustomAddr.

type EndpointAddr

type EndpointAddr struct {
	// ID is the endpoint's identifier.
	ID key.EndpointID
	// contains filtered or unexported fields
}

EndpointAddr combines an endpoint's key.EndpointID with the network-level addresses at which it may be reached.

To establish a connection both the key.EndpointID and at least one path (a relay URL or a direct IP address) are needed; an EndpointAddr with no addresses is still usable together with an address-lookup service.

func NewEndpointAddr

func NewEndpointAddr(id key.EndpointID, addrs ...TransportAddr) EndpointAddr

NewEndpointAddr creates an EndpointAddr with the given id and transport addresses. Addresses are deduplicated and sorted.

func (EndpointAddr) Addrs

func (a EndpointAddr) Addrs() []TransportAddr

Addrs returns the sorted, deduplicated transport addresses.

func (EndpointAddr) IPAddrs

func (a EndpointAddr) IPAddrs() []netip.AddrPort

IPAddrs returns the IP socket addresses of this endpoint.

func (EndpointAddr) IsEmpty

func (a EndpointAddr) IsEmpty() bool

IsEmpty reports whether only the key.EndpointID is present.

func (EndpointAddr) RelayURLs

func (a EndpointAddr) RelayURLs() []RelayURL

RelayURLs returns the relay URLs of this endpoint. In practice this is expected to be zero or one home relay.

func (EndpointAddr) String

func (a EndpointAddr) String() string

String returns a diagnostic string for a.

func (EndpointAddr) WithAddrs

func (a EndpointAddr) WithAddrs(addrs ...TransportAddr) EndpointAddr

WithAddrs returns a copy of a with the given addresses added. The result's address set is sorted and deduplicated.

func (EndpointAddr) WithIP

func (a EndpointAddr) WithIP(ap netip.AddrPort) EndpointAddr

WithIP returns a copy of a with the given IP address added.

func (EndpointAddr) WithRelayURL

func (a EndpointAddr) WithRelayURL(u RelayURL) EndpointAddr

WithRelayURL returns a copy of a with the given relay URL added.

type IPAddr

type IPAddr struct{ Addr netip.AddrPort }

IPAddr is a TransportAddr reachable at an IP socket address.

func (IPAddr) Compare

func (a IPAddr) Compare(other TransportAddr) int

Compare orders IP addresses numerically (by netip.AddrPort.Compare).

func (IPAddr) MarshalText

func (a IPAddr) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler using the string encoding described on TransportAddr.

func (IPAddr) Network

func (IPAddr) Network() string

func (IPAddr) String

func (a IPAddr) String() string

func (*IPAddr) UnmarshalText

func (a *IPAddr) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler using the string encoding described on TransportAddr.

type RelayAddr

type RelayAddr struct{ URL RelayURL }

RelayAddr is a TransportAddr reachable via a relay server.

func (RelayAddr) Compare

func (a RelayAddr) Compare(other TransportAddr) int

Compare orders relay addresses by their normalized URL string.

func (RelayAddr) MarshalText

func (a RelayAddr) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler using the string encoding described on TransportAddr.

func (RelayAddr) Network

func (RelayAddr) Network() string

func (RelayAddr) String

func (a RelayAddr) String() string

func (*RelayAddr) UnmarshalText

func (a *RelayAddr) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler using the string encoding described on TransportAddr.

type RelayURL

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

RelayURL is a URL identifying a relay server.

It wraps a parsed URL and is cheap to copy. It is encouraged to use a fully-qualified DNS domain name (one ending in a ".", e.g. "relay.example.com.") so that local DNS search domains do not interfere with resolution.

The zero value is not usable; construct a RelayURL with ParseRelayURL or RelayURLFromURL.

func ParseRelayURL

func ParseRelayURL(s string) (RelayURL, error)

ParseRelayURL parses s into a RelayURL. It returns an error wrapping ErrParseRelayURL if s is not a valid URL.

func RelayURLFromURL

func RelayURLFromURL(u *url.URL) RelayURL

RelayURLFromURL wraps an already-parsed URL as a RelayURL, normalizing it so that equivalent URLs compare equal (see RelayURL.String).

func (RelayURL) Compare

func (r RelayURL) Compare(other RelayURL) int

Compare returns -1, 0, or +1 comparing r and other by their normalized string form, giving RelayURL a total order.

func (RelayURL) Equal

func (r RelayURL) Equal(other RelayURL) bool

Equal reports whether r and other are the same relay URL.

func (RelayURL) Host

func (r RelayURL) Host() string

Host returns the host (without port) of the relay URL.

func (RelayURL) IsZero

func (r RelayURL) IsZero() bool

IsZero reports whether r is the unusable zero value.

func (RelayURL) MarshalText

func (r RelayURL) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (RelayURL) String

func (r RelayURL) String() string

String returns the normalized string form of the URL. An empty path is rendered as "/", matching the WHATWG URL serialization used by the Rust reference implementation (e.g. "https://example.com" -> "https://example.com/").

func (RelayURL) URL

func (r RelayURL) URL() *url.URL

URL returns a copy of the underlying parsed URL.

func (*RelayURL) UnmarshalText

func (r *RelayURL) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type TransportAddr

type TransportAddr interface {
	// Network returns the transport kind: "relay", "ip", or "custom".
	Network() string
	// String renders the address in its "kind:value" form, e.g. "ip:127.0.0.1:9".
	String() string
	// Compare returns -1, 0, or +1 ordering this address against other. The
	// order matches the Rust reference's derived Ord on the TransportAddr enum:
	// by kind first (relay < ip < custom), then by value (relay URLs by their
	// normalized string, IP addresses numerically, custom by id then data).
	Compare(other TransportAddr) int
	// contains filtered or unexported methods
}

TransportAddr is a network-level address at which an endpoint may be reached. It is one of RelayAddr, IPAddr, or CustomAddr.

The interface is closed: only the implementations in this package satisfy it.

func ParseTransportAddr

func ParseTransportAddr(s string) (TransportAddr, error)

ParseTransportAddr parses a TransportAddr from its "kind:value" string form.

Jump to

Keyboard shortcuts

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