From cfc0d34a584254a9ed76620951c810771aff6f3b Mon Sep 17 00:00:00 2001 From: Alexander Hunt Date: Sun, 21 Jun 2026 12:43:31 -0700 Subject: STUFF --- database/connections.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'database/connections.go') 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) { -- cgit v1.3