Skip to content

ipaddr

Package ipaddr provides typed IP / CIDR helper functions for SQL queries. Pure Go via net/netip; no external dependencies.

  • ipcontains(prefix, ip) → BOOL: true if prefix contains ip
  • ipoverlaps(prefix1, prefix2) → BOOL: true if the two prefixes overlap
  • ipfamily(ip-or-prefix) → INTEGER (4 or 6)
  • iphost(ip-or-prefix) → TEXT: canonical address form
  • ipmasklen(prefix) → INTEGER: prefix bit count
  • ipnetwork(prefix) → TEXT: the prefix’s masked network address

Ported from ncruces/ext/ipaddr with one upstream bug fix: ipoverlaps in ncruces parses arg[0] twice (lines 50-51 of upstream ipaddr.go), effectively comparing prefix1 to itself. Our version reads each prefix from its own argument, matching the function’s documented semantics.

import (
sqlite "gosqlite.org"
"gosqlite.org/ext/ipaddr"
)
if err := ipaddr.Register(conn); err != nil { ... }
rows, _ := db.QueryContext(ctx,
`SELECT ip FROM events WHERE ipcontains('10.0.0.0/8', ip)`)

For pool-wide install via gosqlite.org.Driver.ConnectHook, blank-import the auto sub-package:

import _ "gosqlite.org/ext/ipaddr/auto"

Full API: pkg.go.dev/gosqlite.org/ext/ipaddr