p2p

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package p2p is the public surface of the SDK's local-network layer: the types exchanged with discovery drivers and the injection points platform embedders (gomobile bridges) use to plug native behavior in. Regular embedders never need this package — the SDK wires a default mDNS driver on its own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DriverFactory

func DriverFactory() func() Driver

DriverFactory returns the injected factory, or nil when the SDK should use its built-in driver. SDK-internal.

func InterfaceProvider

func InterfaceProvider() func() ([]net.Interface, error)

InterfaceProvider returns the current interface enumerator. SDK-internal.

func PossibilityProbe

func PossibilityProbe() func(ctx context.Context, port int) Possibility

PossibilityProbe returns the injected probe, or nil when the SDK should use its interface-based default. SDK-internal.

func SetDriverFactory

func SetDriverFactory(f func() Driver)

SetDriverFactory installs a custom discovery-driver factory, called once per SDK instance. Pass nil to restore the built-in mDNS driver.

func SetInterfaceProvider

func SetInterfaceProvider(f func() ([]net.Interface, error))

SetInterfaceProvider overrides how the SDK enumerates network interfaces — Android bridges inject the host's view here, where net.Interfaces is unreliable. Pass nil to restore net.Interfaces.

func SetPossibilityProbe

func SetPossibilityProbe(f func(ctx context.Context, port int) Possibility)

SetPossibilityProbe overrides the discovery-possibility check — iOS bridges inject a self-connection probe here to detect the Local Network permission being denied. Pass nil to restore the default (interface-based) check.

The probe is re-read before every discovery session, not during one, so a change of answer takes effect at the next session start. It is not consulted while local discovery is switched off (SDK.SetLocalDiscoveryEnabled); a host that knows the answer states it through the switch rather than through the probe.

func SetPowerHint

func SetPowerHint(h PowerHint)

SetPowerHint reports the current power state. Process-global like the other injection points; safe to call from any goroutine at any time.

func SubscribePowerHint

func SubscribePowerHint(fn func(PowerHint)) (cancel func())

SubscribePowerHint registers fn for future SetPowerHint calls. The returned cancel is idempotent. SDK-internal.

Types

type AccountStatus

type AccountStatus struct {
	// Enabled — pkarr relays are configured.
	Enabled bool
	// Relays are the configured pkarr relay hosts.
	Relays []string
	// Devices is the number of sibling devices the record names.
	Devices int
	// OwnEntry — the record names this device with its current relay.
	OwnEntry bool
	// LastResolved is when a record was last decoded from a relay.
	LastResolved time.Time
	// LastPublished is when this device last stored the record.
	LastPublished time.Time
	// LastError is the last failed cycle's error; empty after a
	// successful one.
	LastError string
	// ClockAhead is how far the record the relays hold was dated past
	// this device's clock at the last cycle (a sibling's clock runs
	// ahead; this device signs past it); zero when it was not.
	ClockAhead time.Duration
}

AccountStatus is the snapshot of the account-level discovery record: the pkarr record every device of the account registers itself in.

type Announcement

type Announcement struct {
	// PeerId is this device's any-sync peer id — the mDNS instance name.
	PeerId string
	// Port is the QUIC listen port to advertise.
	Port int
	// ServiceType is the DNS-SD service type (e.g. "_any._tcp").
	ServiceType string
}

Announcement is what a Driver publishes on the local network.

type DiscoveredPeer

type DiscoveredPeer struct {
	// PeerId is the any-sync peer id the device announced.
	PeerId string
	// Addrs are "ip:port" endpoints the peer listens on (QUIC).
	Addrs []string
}

DiscoveredPeer is one device found on the local network.

type Driver

type Driver interface {
	// Announce publishes the given announcement until ctx is done.
	Announce(ctx context.Context, a Announcement) error
	// Browse watches for peers of the given service type until ctx is
	// done. found fires when a peer appears or its records change;
	// lost fires when a peer's announcement expires or is withdrawn.
	Browse(ctx context.Context, serviceType string, found func(DiscoveredPeer), lost func(peerId string)) error
}

Driver is the pluggable discovery backend. The SDK ships an mDNS driver; platform bridges may inject a native one (e.g. Android NSD, where in-process multicast is unreliable) via SetDriverFactory.

Both methods block until ctx is done and are called from dedicated goroutines. The SDK may cancel and re-call them on network changes, so implementations must be restartable.

type GlobalStatus

type GlobalStatus struct {
	// Enabled is the config opt-in state.
	Enabled bool
	// EndpointId is this device's iroh endpoint id (its peer key).
	EndpointId string
	// Ticket is the endpoint ticket this device publishes; empty until
	// the relay session is up.
	Ticket string
	// HomeRelay is the relay URL carried by Ticket, if any.
	HomeRelay string
	// RelayConnected — the session to the home relay is up.
	RelayConnected bool
	// Peers lists every peer known through records: space rows or the
	// account record.
	Peers []PeerStatus
	// Account is the account-level discovery record.
	Account AccountStatus
}

GlobalStatus is the snapshot of the internet-wide p2p layer.

type OwnAddresses

type OwnAddresses struct {
	// Addrs are this device's LAN IPs (no port).
	Addrs []string
	// Port is the local QUIC listen port.
	Port int
}

OwnAddresses describes this device's own local-network listener, sent to discovered peers so they can dial back.

type PeerStatus

type PeerStatus struct {
	// PeerId of the peer.
	PeerId string
	// SpaceIds it is known to share with this device, over any source.
	SpaceIds []string
	// Connected — a live connection exists right now.
	Connected bool
	// Sources that know the peer: "lan" (space exchange), "global"
	// (space key-value records), "account" (the account's device
	// record), in any combination.
	Sources []string
	// LastSeen is the newest liveness evidence: a key-value heartbeat
	// or a local connection. Zero for LAN-only peers.
	LastSeen time.Time
	// Tier is the liveness tier derived from LastSeen ("active",
	// "stale", "dormant", "disabled"); empty for LAN-only peers.
	Tier string
	// Failures counts consecutive failed global dials.
	Failures int
}

PeerStatus describes one known peer (LAN or global) for the debug surface.

type Possibility

type Possibility uint8

Possibility is whether local discovery can work on this device right now. Mirrors the states surfaced in sync status.

const (
	PossibilityUnknown Possibility = iota
	// PossibilityPossible — discovery can run.
	PossibilityPossible
	// PossibilityNoInterfaces — no multicast-capable network interface.
	PossibilityNoInterfaces
	// PossibilityRestricted — the OS denies local-network access
	// (e.g. iOS Local Network permission).
	PossibilityRestricted
	// PossibilityDisabled — the host switched local discovery off
	// (config p2p.localDiscovery, SDK.SetLocalDiscoveryEnabled).
	PossibilityDisabled
)

func (Possibility) String

func (p Possibility) String() string

String returns a stable lowercase token for logging / wire mapping.

type PowerHint

type PowerHint uint8

PowerHint tells the global p2p connector how eager to be. Platform bridges report the app moving to the background or the OS entering a low-power mode; the connector then stops probing and lets idle connections lapse until PowerNormal is reported again.

const (
	// PowerNormal — foreground, no power constraint.
	PowerNormal PowerHint = iota
	// PowerLow — background or low-power mode: no global dials.
	PowerLow
)

func CurrentPowerHint

func CurrentPowerHint() PowerHint

CurrentPowerHint returns the last reported power state.

func (PowerHint) String

func (p PowerHint) String() string

String returns a stable lowercase token for logging.

type Status

type Status struct {
	// PeerId is THIS device's peer id — what it announces on the LAN.
	// Every device of an account must have a distinct one (the device
	// key is per-device); two devices sharing a peerId cannot pair.
	PeerId string
	// Enabled is the LAN config opt-out state.
	Enabled bool
	// LocalDiscovery is the mDNS switch state (config p2p.localDiscovery,
	// SDK.SetLocalDiscoveryEnabled), false as well while Enabled is.
	// Possibility follows it: off reads as PossibilityDisabled as soon as
	// the running session has ended.
	LocalDiscovery bool
	// ListenerStarted — the inbound QUIC listener is up.
	ListenerStarted bool
	// Port is the QUIC listen port (zero when not started).
	Port int
	// Possibility is the discovery-possibility state.
	Possibility Possibility
	// State is the account-wide p2p state (Connected when any direct
	// peer, LAN or global, has a live connection).
	State space.P2PState
	// Peers lists every peer currently in the LAN peer store.
	Peers []PeerStatus
	// Global is the internet-wide layer.
	Global GlobalStatus
}

Status is the account-wide snapshot of the p2p layers, returned by SDK.P2PStatus. A debug surface: cheap to read, not a stable wire contract.

Jump to

Keyboard shortcuts

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