package model // FamilyRefs are a reference list's part for one address family: names of // sets that have entries of the family (dynamic sets always do), and // literal entries. type FamilyRefs struct { Sets []string Literals []string } // Empty reports whether nothing of the family was referenced. func (f FamilyRefs) Empty() bool { return len(f.Sets) == 0 && len(f.Literals) == 0 } // SplitRefs splits source or destination references by family. Unknown // names are left out (validation reports them). func (st *State) SplitRefs(refs []string) (v4, v6 FamilyRefs) { for _, r := range refs { if a, err := ParseAddress(r); err == nil { if a.IsV6() { v6.Literals = append(v6.Literals, a.String()) } else { v4.Literals = append(v4.Literals, a.String()) } continue } s := st.Set(r) if s == nil { continue } has4, has6 := s.Families() if has4 { v4.Sets = append(v4.Sets, s.Name) } if has6 { v6.Sets = append(v6.Sets, s.Name) } } return v4, v6 } // Families reports which families a set holds: a dynamic set may get // either at run time. func (s *AddressSet) Families() (v4, v6 bool) { if s.Dynamic { return true, true } for _, e := range s.Entries { if a, err := ParseAddress(e); err == nil { if a.IsV6() { v6 = true } else { v4 = true } } } return v4, v6 } // FamilyEntries returns a set's static entries of one family. func (s *AddressSet) FamilyEntries(v6 bool) []string { var out []string for _, e := range s.Entries { if a, err := ParseAddress(e); err == nil && a.IsV6() == v6 { out = append(out, a.String()) } } return out } // ServiceEntries resolves service references (names and entries) to // entries, without duplicates. Unknown names are left out. func (st *State) ServiceEntries(refs []string) []ServiceEntry { var out []ServiceEntry seen := map[string]bool{} add := func(s string) { e, err := ParseServiceEntry(s) if err != nil || seen[e.String()] { return } seen[e.String()] = true out = append(out, e) } for _, r := range refs { if IsServiceEntry(r) { add(r) continue } if svc := st.Service(r); svc != nil { for _, e := range svc.Entries { add(e) } } } return out }