fact

package
v0.14.3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package fact provides the core code for representing facts, and their serialization and deserialization.

Index

Constants

View Source
const MaxPayloadLen = 1024 * 1024

MaxPayloadLen is the largest payload size we will try to decode to avoid excess memory usage.

View Source
const SignedGroupMaxSafeInnerLength = UDPMaxSafePayload - sgvFactOverhead - sgvOverhead

SignedGroupMaxSafeInnerLength is the maximum safe length for `InnerBytes` above which fragmentation or packet drops may happen. This is computed based on the max safe UDP payload for IPv6, minus the fact & crypto overheads.

View Source
const UDPMaxSafePayload = 1212

UDPMaxSafePayload is the maximum payload size of a UDP packet we can safely send. we only need to worry about IPv6 for this

Variables

This section is empty.

Functions

func ScaleExpirationQuantumForTests added in v0.10.0

func ScaleExpirationQuantumForTests(factor uint)

ScaleExpirationQuantumForTests reconfigures how the fact TTL is represented on the wire to permit faster than normal tests

func SliceHas added in v0.9.0

func SliceHas(facts []*Fact, predicate func(*Fact) bool) bool

SliceHas returns true if and only if predicate returns true for a fact in the given slice

func SliceIndexOf added in v0.11.2

func SliceIndexOf(facts []*Fact, predicate func(*Fact) bool) int

SliceIndexOf returns the index of the first element in the slice for which the predicate returns true, or -1 if there is no match or the slice is empty

Types

type Attribute

type Attribute byte

Attribute is a byte identifying what aspect of a Subject a Fact describes

const (
	AttributeUnknown        Attribute = 0
	AttributeAlive          Attribute = '!'
	AttributeEndpointV4     Attribute = 'e'
	AttributeEndpointV6     Attribute = 'E'
	AttributeAllowedCidrV4  Attribute = 'a'
	AttributeAllowedCidrV6  Attribute = 'A'
	AttributeMember         Attribute = 'm'
	AttributeMemberMetadata Attribute = 'M'
	// A signed group is a bit different from other facts
	// in this case, the subject is actually the source,
	// and the value is a signed aggregate of other facts.
	AttributeSignedGroup Attribute = 'S'
)

fact types, denoted as attributes of a subject

type EmptyValue

type EmptyValue struct{}

EmptyValue is currently used as a placeholder in Membership facts

func (EmptyValue) DecodeFrom added in v0.6.0

func (v EmptyValue) DecodeFrom(_ int, _ io.Reader) error

DecodeFrom implements Decodable

func (EmptyValue) MarshalBinary added in v0.6.0

func (v EmptyValue) MarshalBinary() ([]byte, error)

MarshalBinary always returns an empty slice for EmptyValue

func (EmptyValue) String

func (v EmptyValue) String() string

type Fact

type Fact struct {
	encoding.BinaryMarshaler
	util.Decodable

	Attribute Attribute
	Expires   time.Time
	Subject   Subject
	Value     Value
}

Fact represents a single piece of information about a subject, with an associated expiration time

func MergeList

func MergeList(facts []*Fact) []*Fact

MergeList merges duplicate facts in a slice, keeping the latest Expires value

func SortedCopy

func SortedCopy(facts []*Fact) []*Fact

SortedCopy makes a copy of the list and then sorts it "naturally"

func (*Fact) DecodeFrom added in v0.6.0

func (f *Fact) DecodeFrom(_ int, now time.Time, reader util.ByteReader) error

DecodeFrom implements Decodable

func (*Fact) FancyString added in v0.3.0

func (f *Fact) FancyString(
	subjectFormatter func(s Subject) string,
	now time.Time,
) string

FancyString formats the fact as a string using a custom helper to format the subject, most commonly to replace peer keys with names

func (*Fact) MarshalBinary added in v0.6.0

func (f *Fact) MarshalBinary() ([]byte, error)

MarshalBinary serializes a Fact to its on-wire format

func (*Fact) MarshalBinaryNow added in v0.9.0

func (f *Fact) MarshalBinaryNow(now time.Time) ([]byte, error)

MarshalBinaryNow is like MarshalBinary, except it uses a provided value of `now` so that the output is deterministic

func (*Fact) String

func (f *Fact) String() string

type GroupAccumulator added in v0.2.0

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

GroupAccumulator is a helper to aggregate individual facts into (signed) groups of a max size

func NewAccumulator added in v0.2.0

func NewAccumulator(maxGroupLen int, now time.Time) *GroupAccumulator

NewAccumulator initializes a new GroupAccumulator with a given max inner size per group.

func (*GroupAccumulator) AddFact added in v0.2.0

func (ga *GroupAccumulator) AddFact(f *Fact) error

AddFact appends the given fact into the accumulator

func (*GroupAccumulator) AddFactIfRoom added in v0.2.1

func (ga *GroupAccumulator) AddFactIfRoom(f *Fact) (added bool, err error)

AddFactIfRoom conditionally adds the fact if and only if it won't result in creating a new group

func (*GroupAccumulator) MakeSignedGroups added in v0.2.0

func (ga *GroupAccumulator) MakeSignedGroups(
	s *signing.Signer,
	recipient *wgtypes.Key,
) ([]*Fact, error)

MakeSignedGroups converts all the accumulated facts into SignedGroups of no more than the specified max inner size.

type IPNetValue

type IPNetValue struct {
	net.IPNet
}

IPNetValue represents some IP+Mask as an Attribute of a Subject

func (*IPNetValue) DecodeFrom added in v0.6.0

func (ipn *IPNetValue) DecodeFrom(lengthHint int, reader io.Reader) error

DecodeFrom implements Decodable

func (IPNetValue) MarshalBinary added in v0.6.0

func (ipn IPNetValue) MarshalBinary() ([]byte, error)

MarshalBinary gives the binary representation of the ip and cidr prefix

func (*IPNetValue) UnmarshalBinary added in v0.6.0

func (ipn *IPNetValue) UnmarshalBinary(data []byte) error

UnmarshalBinary implements BinaryUnmarshaler

type IPPortValue

type IPPortValue struct {
	IP   net.IP
	Port int
}

IPPortValue represents an IP:port pair as an Attribute of a Subject

func (*IPPortValue) DecodeFrom added in v0.6.0

func (ipp *IPPortValue) DecodeFrom(lengthHint int, reader io.Reader) error

DecodeFrom implements Decodable

func (*IPPortValue) MarshalBinary added in v0.6.0

func (ipp *IPPortValue) MarshalBinary() ([]byte, error)

MarshalBinary returns the normalized binary representation

func (*IPPortValue) String

func (ipp *IPPortValue) String() string

func (*IPPortValue) UnmarshalBinary added in v0.6.0

func (ipp *IPPortValue) UnmarshalBinary(data []byte) error

UnmarshalBinary implements BinaryUnmarshaler

type Key

type Key struct {
	// Attribute is a byte, nothing to worry about in comparisons
	Attribute Attribute
	// contains filtered or unexported fields
}

Key is a comparable version of the subject, attribute, and value of a Fact

func KeyOf

func KeyOf(fact *Fact) Key

KeyOf returns the FactKey for a Fact

func KeysDifference added in v0.12.2

func KeysDifference(old, new []*Fact) (onlyOld, onlyNew []Key)

KeysDifference computes the fact keys that are different between two slices

func (*Key) FancyString added in v0.13.0

func (k *Key) FancyString(
	subjectFormatter func(s Subject) string,
) string

FancyString formats the fact as a string using a custom helper to format the subject, most commonly to replace peer keys with names

func (Key) String added in v0.12.2

func (k Key) String() string

func (*Key) ToFact added in v0.13.0

func (k *Key) ToFact() (*Fact, error)

ToFact turns a key back into a corresponding fact, with a zero TTL

type MemberAttribute added in v0.11.0

type MemberAttribute byte

MemberAttribute is a single byte identifying some attribute of a member.

const (
	// MemberName is the friendly / display name to use for a peer
	MemberName MemberAttribute = 'n'
	// MemberIsBasic flags if the member is a "basic" member which only runs
	// wireguard and not wirelink
	MemberIsBasic MemberAttribute = 'b'
)

type MemberMetadata added in v0.11.0

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

MemberMetadata represents a set of attributes and their values for a single peer.

func (*MemberMetadata) DecodeFrom added in v0.11.0

func (mm *MemberMetadata) DecodeFrom(_ int, reader io.Reader) error

DecodeFrom implements Decodable

func (*MemberMetadata) ForEach added in v0.11.0

func (mm *MemberMetadata) ForEach(visitor func(MemberAttribute, string))

ForEach calls visitor for each attribute in the metadata.

func (*MemberMetadata) MarshalBinary added in v0.11.0

func (mm *MemberMetadata) MarshalBinary() ([]byte, error)

MarshalBinary implements BinaryEncoder

func (*MemberMetadata) String added in v0.11.0

func (mm *MemberMetadata) String() string

func (*MemberMetadata) With added in v0.13.0

func (mm *MemberMetadata) With(name string, basic bool) *MemberMetadata

With returns a copy of the member metadata with the given info updated: name will be assigned if non-empty, basic will be assigned if true, or if not present in the initial value.

type PeerSubject

type PeerSubject struct {
	wgtypes.Key
}

PeerSubject is a subject that is a peer identified via its public key

func (*PeerSubject) DecodeFrom added in v0.6.0

func (s *PeerSubject) DecodeFrom(_ int, reader io.Reader) error

DecodeFrom implements Decodable

func (*PeerSubject) IsSubject added in v0.11.3

func (s *PeerSubject) IsSubject()

IsSubject implements Subject

func (*PeerSubject) MarshalBinary added in v0.6.0

func (s *PeerSubject) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler

func (*PeerSubject) UnmarshalBinary added in v0.6.0

func (s *PeerSubject) UnmarshalBinary(data []byte) error

UnmarshalBinary implements BinaryUnmarshaler

type Set added in v0.13.0

type Set map[Key]*Fact

Set is used to map fact keys to the "best" fact for that key

func SetOf added in v0.13.0

func SetOf(facts []*Fact) Set

SetOf makes a new FactSet out of a slice of Facts

type SignedGroupValue added in v0.2.0

type SignedGroupValue struct {
	Nonce      [chacha20poly1305.NonceSizeX]byte
	Tag        [chacha20poly1305.Overhead]byte
	InnerBytes []byte
}

SignedGroupValue represents a signed chunk of other fact data. Note that this structure does _not_ include parsing those inner bytes!

func (*SignedGroupValue) DecodeFrom added in v0.6.0

func (sgv *SignedGroupValue) DecodeFrom(_ int, reader io.Reader) error

DecodeFrom implements Decodable

func (*SignedGroupValue) MarshalBinary added in v0.6.0

func (sgv *SignedGroupValue) MarshalBinary() ([]byte, error)

MarshalBinary gives the on-wire form of the value

func (*SignedGroupValue) ParseInner added in v0.2.0

func (sgv *SignedGroupValue) ParseInner(now time.Time) (ret []*Fact, err error)

ParseInner parses the inner bytes of a SignedGroupValue into facts. Validating the signature must be done separately, and should be done before calling this method.

func (*SignedGroupValue) String added in v0.2.0

func (sgv *SignedGroupValue) String() string

type Subject

type Subject interface {
	fmt.Stringer
	encoding.BinaryMarshaler
	util.Decodable
	// IsSubject tags Subjects as semantically different from Values
	IsSubject()
}

Subject is the subject of a Fact

type UUIDValue added in v0.4.0

type UUIDValue struct {
	uuid.UUID
}

UUIDValue represents a UUID, often used as a random marker or tag

func (*UUIDValue) DecodeFrom added in v0.6.0

func (u *UUIDValue) DecodeFrom(_ int, reader io.Reader) error

DecodeFrom implements Decodable

type Value

type Value interface {
	fmt.Stringer
	encoding.BinaryMarshaler
	util.Decodable
}

Value represents the value of a Fact

Jump to

Keyboard shortcuts

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