tsnet

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package tsnet is the network library support for tsync (discovery/registration and communication).

Index

Constants

View Source
const (
	// BufSize is the max size of messages (safe size).
	// 576 byte IP packet - 60 byte IP header - 8 byte UDP header = 508 bytes.
	BufSize = 576 - 60 - 8
	// DefaultTarget: which udp address we try by default to find our interface and ip.
	DefaultTarget            = "8.8.8.8:53"
	DefaultBroadcastInterval = 1500 * time.Millisecond
	TimeFormat               = "15:04:05.000" // time only + millis.
	DefaultPeerTimeout       = 10 * time.Second

	DefaultDiscoveryPort = 29556 // "ts" in ascii.
)
View Source
const (
	DiscoveryMessageFormat = "tsync1 %q %s e %d" // name, public key, epoch
	ConnectMessageFormat   = "connect1 %q %q"    // requester_name, target_name
	AcceptMessageFormat    = "accept1 %q"        // target_name
	RejectMessageFormat    = "reject1 %q %q"     // target_name, reason
	DataMessageFormat      = "data1 %q %s"       // target_name, signed_data
)

Variables

This section is empty.

Functions

func GetInternetInterface

func GetInternetInterface(ctx context.Context, target string) (*net.Interface, *net.UDPAddr, error)

GetInternetInterface returns the interface used to reach a public IP (default route). Windows tend to pick somehow the wrong interface instead of listening to all/correct default one so we try to guess the right one by connecting to an external address.

func PeerKVSort added in v0.8.0

func PeerKVSort(a, b smap.KV[Peer, PeerData]) int

PeerKVSort sort function for slices.SortFunc of smap.KV[Peer, PeerData]. Sorts by IP, then name, then public key.

func PeerLess added in v0.8.0

func PeerLess(a, b Peer) bool

PeerLess sort function for smap.AllSorted. Sorts by IP, then name, then public key.

Types

type Config

type Config struct {
	// Name to use, if empty hostname will be used.
	Name  string
	Port  int
	Mcast string
	// Which ip:port we try to resolve to find our address and interface.
	Target string
	// Callback called when a the Server Peers map has changed, a new peer is detected
	// or old one removed or updated. Must not block for long or
	// it will delay processing of incoming messages.
	OnChange              func(version uint64)
	Identity              *tcrypto.Identity // long term identity for this server
	BaseBroadcastInterval time.Duration     // default to 1.5s if 0
	PeerTimeout           time.Duration     // default to 10s if 0
}

func (*Config) NewServer

func (c *Config) NewServer() *Server

type ConnectionStatus added in v0.9.0

type ConnectionStatus int
const (
	// NotLinked is the initial state once discovered.
	NotLinked ConnectionStatus = iota
	// SentConn is the state when a connection is being established.
	SentConn
	// ReceivedConn is the state when a connection request has been received.
	ReceivedConn
	// Connected is the state when a connection has been established.
	Connected
	// Failed is the state when a connection has failed.
	Failed
)

type Peer

type Peer struct {
	IP        string
	Name      string
	PublicKey string
}

type PeerData added in v0.4.0

type PeerData struct {
	HumanHash string
	Port      int
	Epoch     int32
	LastSeen  time.Time
	Status    ConnectionStatus
}

type Server

type Server struct {
	// Our copy of the input config.
	Config

	Peers   *smap.Map[Peer, PeerData]
	Sources *smap.Map[Source, Peer] // maps ip,port to peer
	// contains filtered or unexported fields
}

func (*Server) ConnectToPeer added in v0.9.0

func (s *Server) ConnectToPeer(peer Peer) error

ConnectToPeer initiates a connection to the specified peer.

func (*Server) MCastMessageDecode added in v0.9.0

func (s *Server) MCastMessageDecode(buf []byte) (string, string, int32, error)

func (*Server) MCastMessageSend added in v0.9.1

func (s *Server) MCastMessageSend(epoch int32) error

func (*Server) OurAddress

func (s *Server) OurAddress() *net.UDPAddr

func (*Server) PeersCleanup added in v0.5.0

func (s *Server) PeersCleanup()

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

func (*Server) Stop

func (s *Server) Stop()

func (*Server) Stopped added in v0.5.0

func (s *Server) Stopped() bool

type Source added in v0.11.0

type Source struct {
	IP   string
	Port int
}

Jump to

Keyboard shortcuts

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