Files
housecarl/internal/model/addr.go
T
abrown38andClaude Opus 5.5 34292eeebb Build Housecarl 0.1.0: the nftables firewall manager
Zones, address sets (IPv4 and IPv6 mixed, dynamic sets with timeouts) and
services, ordered rules in the early, input, forward and output chains,
port forwards with hairpin NAT and IPv6 pinholes, source NAT, redirects,
and switchable built-ins (loopback, conntrack, ICMPv6, antispoofing, MSS
clamping, DHCPv6 client, flowtable). Every change is validated, checked
with nft -c and applied in one transaction to Housecarl's own table,
inet housecarl; changes made over the network roll back unless
confirmed, and the confirmed ruleset is loaded at boot by
housecarl-ruleset.service. An empty configuration applies nothing.

Also: hit counters and the drop log (nflog), 'hc import' of an existing
nftables ruleset and 'hc takeover', the hc CLI, the REST API and the web
UI, users, scoped tokens (sets:write:NAME), the audit log, the web UI's
certificate through Jarl, Debian packages, docs, unit tests with real
nft 1.1.3, and an e2e test with real traffic (71 checks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 07:57:14 +01:00

88 lines
2.5 KiB
Go

package model
import (
"fmt"
"net/netip"
"strings"
)
// Address is a parsed address entry: one address, a prefix or a range.
type Address struct {
Prefix netip.Prefix // a single address is a /32 or /128
First, End netip.Addr // a range (First valid only for ranges)
}
// IsV6 reports whether the entry is IPv6.
func (a Address) IsV6() bool {
if a.First.IsValid() {
return a.First.Is6()
}
return a.Prefix.Addr().Is6()
}
// String writes the entry as nftables and Housecarl write it:
// "10.0.0.5", "10.0.0.0/24", "10.0.0.1-10.0.0.9".
func (a Address) String() string {
if a.First.IsValid() {
return a.First.String() + "-" + a.End.String()
}
if a.Prefix.IsSingleIP() {
return a.Prefix.Addr().String()
}
return a.Prefix.String()
}
// ParseAddress reads an address, a prefix (host bits must be zero) or a
// range, IPv4 or IPv6.
func ParseAddress(s string) (Address, error) {
s = strings.TrimSpace(s)
if a, b, ok := strings.Cut(s, "-"); ok {
first, err1 := netip.ParseAddr(strings.TrimSpace(a))
end, err2 := netip.ParseAddr(strings.TrimSpace(b))
if err1 != nil || err2 != nil || first.Is4() != end.Is4() {
return Address{}, fmt.Errorf("%q is not an address range (write it as 10.0.0.1-10.0.0.9)", s)
}
first, end = first.Unmap(), end.Unmap()
if end.Less(first) {
return Address{}, fmt.Errorf("the range %q ends before it starts", s)
}
if first == end {
return Address{Prefix: netip.PrefixFrom(first, first.BitLen())}, nil
}
return Address{First: first, End: end}, nil
}
if strings.Contains(s, "/") {
p, err := netip.ParsePrefix(s)
if err != nil {
return Address{}, fmt.Errorf("%q is not an address or prefix", s)
}
p = netip.PrefixFrom(p.Addr().Unmap(), p.Bits())
if p.Masked() != p {
return Address{}, fmt.Errorf("%q has host bits set; the prefix is %s", s, p.Masked())
}
return Address{Prefix: p}, nil
}
a, err := netip.ParseAddr(s)
if err != nil || a.Zone() != "" {
return Address{}, fmt.Errorf("%q is not an address, prefix, range or set name", s)
}
a = a.Unmap()
return Address{Prefix: netip.PrefixFrom(a, a.BitLen())}, nil
}
// IsLiteral reports whether a source/destination reference is an
// address, prefix or range rather than a set name.
func IsLiteral(ref string) bool {
_, err := ParseAddress(ref)
return err == nil
}
// NormalizeAddress rewrites an address entry in its canonical form.
func NormalizeAddress(s string) (string, error) {
a, err := ParseAddress(s)
if err != nil {
return "", err
}
return a.String(), nil
}