Documentation
¶
Overview ¶
Package devicestore is the in-memory map of devices observed on the LAN. MAC addresses are the canonical identifier; IPs may change over time.
Index ¶
- type Device
- type Event
- type EventKind
- type Observation
- type Store
- func (s *Store) Evict(now time.Time, ttl time.Duration, hasPolicy func(mac string) bool) int
- func (s *Store) Get(m net.HardwareAddr) (Device, bool)
- func (s *Store) List() []Device
- func (s *Store) ResolveIP(ip net.IP) (net.HardwareAddr, bool)
- func (s *Store) Subscribe() chan Event
- func (s *Store) Unsubscribe(ch chan Event)
- func (s *Store) Upsert(o Observation)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
MAC net.HardwareAddr
IP net.IP
Hostname string
Vendor string
Model string
LastSeen time.Time
}
Device is the public record for a host we have observed.
Vendor and Model are deliberately separate. Vendor is the IEEE OUI vendor of the MAC's manufacturer (always the silicon maker — Apple, Samsung, Intel). Model is the SSDP/UPnP server string ("Linux/3.10 UPnP/1.0 SSDP/1.6 PhilipsHue/1.0") which describes the firmware / device kind. Conflating them — as the v1 SSDP scanner did — would overwrite the OUI lookup every time a device announced itself, hiding the manufacturer from the operator.
type Observation ¶
type Observation struct {
MAC net.HardwareAddr
IP net.IP
Hostname string
Vendor string
Model string
Seen time.Time
}
Observation is one fact about a device, fed in by a scanner. Empty fields mean "no new information"; the store will preserve prior values.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the device map. Safe for concurrent use.
Indexes:
- byMAC: canonical map (MAC string → *Device)
- byIP: reverse index (IP string → MAC string), kept in sync with byMAC[mac].IP. ResolveIP used to be O(N) over byMAC; on a busy LAN with hundreds of devices, called from every Policy.Set/Clear handler, that scan was a real cost. byIP makes it O(1).
func (*Store) Evict ¶
Evict removes every device whose LastSeen is older than now-ttl, EXCEPT devices for which hasPolicy(mac.String()) returns true. A device with an active policy is preserved regardless of ttl: the poison/throttle/pcap path holds the canonical reference and we don't want to silently drop a target the operator is actively manipulating. Returns the number of devices evicted.
hasPolicy may be nil — equivalent to "no device has a policy", useful for tests and for the rare daemon that runs without a policycompiler. Production daemons pass a closure over compiler.Snapshot() so the eviction guard reflects authoritative policy state. (Earlier versions stored policy state on the Device itself via SetPolicy, but nothing in production wired SetPolicy up, so the guard was effectively dead and policied-but-idle devices could be silently evicted while their poison kept running.)
Intended to be called periodically (e.g. once a minute) by the daemon so the device map doesn't grow unbounded on long-running sessions — roaming guests with privacy-randomised MACs, IoT devices that come and go, etc.
Processes the sweep in batches of evictBatchSize to bound the worst-case lock-hold time: between batches the write lock is released so Upserts from the passive sniffer can interleave. Subscribers receive evicted events as each batch completes — not all-at-once at the end — so a slow consumer sees the eviction stream promptly even when N is large.
hasPolicy is called while s.mu is held — it must not call back into the store (re-entrant lock would deadlock). The compiler's Snapshot is independently locked and safe to call from here.
func (*Store) Get ¶
func (s *Store) Get(m net.HardwareAddr) (Device, bool)
Get returns a device by MAC, or (zero, false) if unknown.
func (*Store) ResolveIP ¶
ResolveIP looks up the MAC currently associated with the given IP. O(1) via the byIP reverse index. Returns (mac, true) on hit, (nil, false) on miss.
func (*Store) Subscribe ¶
Subscribe returns a channel that receives every event. Buffer size 64. When a slow consumer's buffer fills, the newest incoming event is dropped. Caller MUST call Unsubscribe with the returned channel when done.
func (*Store) Unsubscribe ¶
Unsubscribe removes ch from the subscriber set and closes it. Safe to call multiple times; subsequent calls are no-ops.
func (*Store) Upsert ¶
func (s *Store) Upsert(o Observation)
Upsert merges an observation into the store. New MAC = discovery; existing MAC = update (only non-empty fields overwrite).