package store import ( "net/netip" "strings" ) // EffectivePeerEnabledOnCreate matches API/JSON decoding: omitted "enabled" unmarshals as false in Go, // but new peers should be enabled by default. If session_state is non-empty, false is preserved // (agent-managed rows may be intentionally disabled). func EffectivePeerEnabledOnCreate(inEnabled bool, sessionState string) bool { if inEnabled { return true } if strings.TrimSpace(sessionState) != "" { return false } return true } // ParsePeerNeighbor parses a BGP neighbor value for BIRD output: a plain IPv4/IPv6 // address, or a host prefix (/32 or /128) which is a common input mistake. func ParsePeerNeighbor(s string) (netip.Addr, bool) { s = strings.TrimSpace(s) if s == "" { return netip.Addr{}, false } if addr, err := netip.ParseAddr(s); err == nil { return addr, true } pfx, err := netip.ParsePrefix(s) if err != nil { return netip.Addr{}, false } addr := pfx.Addr() if addr.Is4() && pfx.Bits() == 32 { return addr, true } if addr.Is6() && pfx.Bits() == 128 { return addr, true } return netip.Addr{}, false } // NormalizePeerNeighborString returns the canonical host address string for storage // and BIRD, or false if s is not a usable neighbor. func NormalizePeerNeighborString(s string) (string, bool) { addr, ok := ParsePeerNeighbor(s) if !ok { return "", false } return addr.String(), true }