summaryrefslogtreecommitdiff
path: root/database/connections.go
diff options
context:
space:
mode:
Diffstat (limited to 'database/connections.go')
-rw-r--r--database/connections.go16
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) {