diff options
| author | Alexander Hunt <me@liz.coffee> | 2026-06-21 12:43:31 -0700 |
|---|---|---|
| committer | Alexander Hunt <me@liz.coffee> | 2026-06-21 12:43:31 -0700 |
| commit | cfc0d34a584254a9ed76620951c810771aff6f3b (patch) | |
| tree | 7fba034f7347ce0003e7a3a56adcaf862c834907 /database/connections.go | |
| parent | fb653292612eddca5b088ed88466c546d1597107 (diff) | |
| download | penguins.lan-cfc0d34a584254a9ed76620951c810771aff6f3b.tar.gz penguins.lan-cfc0d34a584254a9ed76620951c810771aff6f3b.zip | |
STUFF
Diffstat (limited to 'database/connections.go')
| -rw-r--r-- | database/connections.go | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/database/connections.go b/database/connections.go index e16a17e..1af9de7 100644 --- a/database/connections.go +++ b/database/connections.go @@ -60,6 +60,22 @@ func MacForIP(db *sql.DB, ip string) (string, error) { return mac, err } +// IPForMac returns the freshest live IP for a MAC, or "" if the device hasn't +// been seen within the freshness window. This is what the DNS server answers a +// device's A query with. A MAC can briefly hold more than one IP (a lease +// change mid-window), so the most recently updated entry wins. +func IPForMac(db *sql.DB, mac string) (string, error) { + var ip string + err := db.QueryRow( + "SELECT ip FROM connections WHERE mac = ? AND updated_at > ? ORDER BY updated_at DESC LIMIT 1", + mac, time.Now().Add(-connectionFreshness), + ).Scan(&ip) + if errors.Is(err, sql.ErrNoRows) { + return "", nil + } + return ip, err +} + // NetworkUserIDs returns the set of users whose MAC was seen on the LAN within // the freshness window, whether or not they're on the chat. func NetworkUserIDs(db *sql.DB) (map[string]bool, error) { |
