func ValidateUDPCIDRs(ss []string) error {

	cidrmap := make(map[string]*cidr)
	// instantiate NewPCTrieRanger
	ranger := cidranger.NewPCTrieRanger()
	multicastv4SubnetPresent := false
	multicastv6SubnetPresent := false

	for _, s := range ss {
		cidr, err := parseCIDR(s)
		if err != nil {
			return err
		}
		if _, ok := cidrmap[cidr.ipNet.String()]; !ok {
			cidrmap[cidr.ipNet.String()] = cidr
		} else {
			return fmt.Errorf("CIDR subnet parsed from %s is duplicated", cidr.str)
		}
		if cidr.ipNet.String() == multicastv4Subnet {
			multicastv4SubnetPresent = true
		}
		if cidr.ipNet.String() == multicastv6Subnet {
			multicastv6SubnetPresent = true
		}
		if err := ranger.Insert(newCustomRangerEntry(cidr)); err != nil {
			return fmt.Errorf("Error adding CIDR %s", cidr.str)
		}
	}
	// Parse and validate all not CIDRs are included in regular CIDRs
	for _, c := range cidrmap {

		if c.op == opExclude {
			// get all the networks which are containing the network,
			// basically getting all the parent prefixes.
			entries, err := ranger.ContainingNetworks(c.ipNet.IP)
			if err != nil {
				return fmt.Errorf("Cannot find the CIDR: %s", err)
			}

			mask := c.ipNet.Mask
			// make sure the NOT(!) CIDR is part/contained of/by a included CIDR
			present := checkExcPfxContainedInc(entries, mask, c.ipNet)

			// if the excluded CIDR is not contained in the included CIDR then return error
			if !present {
				return fmt.Errorf("%s is not contained in any CIDR", c.str)
			}
		}
	}

	// now if all the CIDR make sense, check for multicast subnet,
	//  check if we have a 224/4 subnet in the pfx tree and if its not excluded,
	// if yes then return error

	_, v4network, err := net.ParseCIDR(multicastv4Subnet)
	if err != nil {
		return fmt.Errorf("%s is not a valid CIDR", v4network)
	}
	_, v6network, err := net.ParseCIDR(multicastv6Subnet)
	if err != nil {
		return fmt.Errorf("%s is not a valid CIDR", v6network)
	}

	// get all the networks that have 224/4 and if anyone is included in the 224/4 subnet
	// then we report error.

	// ipv4 checks
	multicastv4Entries, _ := ranger.CoveredNetworks(*v4network)

	expectedmulticastv4AddrRange := addressv4RangeCount(*v4network)
	actualmulticastv4AddrRange := uint64(0)

	multicastv4SubnetInc := false
	wrongEntryv4CIDR := make([]string, 0, len(multicastv4Entries))

	for _, entry := range multicastv4Entries {
		cidr := entry.(*cidr)
		if cidr.op == opInclude {
			multicastv4SubnetInc = true
			wrongEntryv4CIDR = append(wrongEntryv4CIDR, cidr.str)
		}
		// we count the actual host addresses when the pfx tree has 224/4 and all of them are excluded.
		actualmulticastv4AddrRange += addressv4RangeCount(entry.Network())
		ranger.Remove(cidr.ipNet) //nolint
	}
	if len(multicastv4Entries) > 0 && multicastv4SubnetInc {
		return fmt.Errorf("The CIDR %s are multicast subnets, should not be included in UDP target networks", wrongEntryv4CIDR)
	}
	var lastMultiContainedv4CIDR, lastMultiContainedv6CIDR *cidr
	// now check here if multicast subnet is contained in the pfx-tree

	MulticastContainedv4Subnets, _ := ranger.ContainingNetworks(v4network.IP)
	if len(MulticastContainedv4Subnets) > 0 {
		lastMultiContainedv4CIDR = MulticastContainedv4Subnets[len(MulticastContainedv4Subnets)-1].(*cidr)
	}
	// now we come only in 3 cases:
	// 1. When the pfx tree has 224/4, and itself and all the network under them are excluded,
	// 		so, we cal the actual multicast addresses count to be sure that really everything is excluded.
	// 2. When there is no 224/4 network in tree, ex: 0/0, 128/0 so get the last contained subnet and if
	// 		the last subnet is excluded then we are good else return error.
	// 3. When the pfx tree doesn't have the exact 224/4 but has 224/5, 232/5, we need to check for the actual multicastrange
	if lastMultiContainedv4CIDR != nil && !multicastv4SubnetPresent && actualmulticastv4AddrRange != expectedmulticastv4AddrRange {
		if lastMultiContainedv4CIDR.op == opInclude {
			return fmt.Errorf("The CIDR %s includes multicast subnets, should not be included in UDP target networks", lastMultiContainedv4CIDR.str)
		}
	}
	multicastv6Entries, _ := ranger.CoveredNetworks(*v6network)

	expectedmulticastv6AddrRange := addressv6RangeCount(*v6network)
	actualmulticastv6AddrRange := big.NewInt(0)

	multicastv6SubnetInc := false
	wrongEntryv6CIDR := make([]string, 0, len(multicastv6Entries))
	for _, entry := range multicastv6Entries {
		cidr := entry.(*cidr)
		if cidr.op == opInclude {
			multicastv6SubnetInc = true
			wrongEntryv6CIDR = append(wrongEntryv6CIDR, cidr.str)
		}

		actualmulticastv6AddrRange.Add(addressv6RangeCount(entry.Network()), actualmulticastv6AddrRange)
		ranger.Remove(cidr.ipNet) //nolint
	}
	if len(multicastv6Entries) > 0 && multicastv6SubnetInc {
		return fmt.Errorf("The CIDR %s are multicast subnets, should not be included in UDP target networks", wrongEntryv6CIDR)
	}

	MulticastContainedv6Subnets, _ := ranger.ContainingNetworks(v6network.IP)
	if len(MulticastContainedv6Subnets) > 0 {
		lastMultiContainedv6CIDR = MulticastContainedv6Subnets[len(MulticastContainedv6Subnets)-1].(*cidr)
	}
	if lastMultiContainedv6CIDR != nil && !multicastv6SubnetPresent && actualmulticastv6AddrRange != expectedmulticastv6AddrRange {
		if lastMultiContainedv6CIDR.op == opInclude {
			return fmt.Errorf("The CIDR %s includes multicast subnets, should not be included in UDP target networks", lastMultiContainedv6CIDR.str)
		}
	}

	return nil
}
// ValidateUDPCIDRs validates that the list of string provided as a set is a valid CIDR set
ValidateUDPCIDRs validates that the list of string provided as a set is a valid CIDR set
// ValidateUDPCIDRs validates that the list of string provided as a set is a valid CIDR set
func ValidateUDPCIDRs(ss []string) error {
	cidrmap := make(map[string]*cidr)
	ranger := cidranger.NewPCTrieRanger()
	multicastv4SubnetPresent := false
	multicastv6SubnetPresent := false
	for _, s := range ss {
		cidr, err := parseCIDR(s)
		if err != nil {
			return err
		}
		if _, ok := cidrmap[cidr.ipNet.String()]; !ok {
			cidrmap[cidr.ipNet.String()] = cidr
		} else {
			return fmt.Errorf("CIDR subnet parsed from %s is duplicated", cidr.str)
		}
		if cidr.ipNet.String() == multicastv4Subnet {
			multicastv4SubnetPresent = true
		}
		if cidr.ipNet.String() == multicastv6Subnet {
			multicastv6SubnetPresent = true
		}
		if err := ranger.Insert(newCustomRangerEntry(cidr)); err != nil {
			return fmt.Errorf("Error adding CIDR %s", cidr.str)
		}
	}
	for _, c := range cidrmap {
		if c.op == opExclude {
			entries, err := ranger.ContainingNetworks(c.ipNet.IP)
			if err != nil {
				return fmt.Errorf("Cannot find the CIDR: %s", err)
			}
			mask := c.ipNet.Mask
			present := checkExcPfxContainedInc(entries, mask, c.ipNet)
			if !present {
				return fmt.Errorf("%s is not contained in any CIDR", c.str)
			}
		}
	}
	_, v4network, err := net.ParseCIDR(multicastv4Subnet)
	if err != nil {
		return fmt.Errorf("%s is not a valid CIDR", v4network)
	}
	_, v6network, err := net.ParseCIDR(multicastv6Subnet)
	if err != nil {
		return fmt.Errorf("%s is not a valid CIDR", v6network)
	}
	multicastv4Entries, _ := ranger.CoveredNetworks(*v4network)
	expectedmulticastv4AddrRange := addressv4RangeCount(*v4network)
	actualmulticastv4AddrRange := uint64(0)
	multicastv4SubnetInc := false
	wrongEntryv4CIDR := make([]string, 0, len(multicastv4Entries))
	for _, entry := range multicastv4Entries {
		cidr := entry.(*cidr)
		if cidr.op == opInclude {
			multicastv4SubnetInc = true
			wrongEntryv4CIDR = append(wrongEntryv4CIDR, cidr.str)
		}
		actualmulticastv4AddrRange += addressv4RangeCount(entry.Network())
		ranger.Remove(cidr.ipNet) 
	}
	if len(multicastv4Entries) > 0 && multicastv4SubnetInc {
		return fmt.Errorf("The CIDR %s are multicast subnets, should not be included in UDP target networks", wrongEntryv4CIDR)
	}
	var lastMultiContainedv4CIDR, lastMultiContainedv6CIDR *cidr
	MulticastContainedv4Subnets, _ := ranger.ContainingNetworks(v4network.IP)
	if len(MulticastContainedv4Subnets) > 0 {
		lastMultiContainedv4CIDR = MulticastContainedv4Subnets[len(MulticastContainedv4Subnets)-1].(*cidr)
	}
	if lastMultiContainedv4CIDR != nil && !multicastv4SubnetPresent && actualmulticastv4AddrRange != expectedmulticastv4AddrRange {
		if lastMultiContainedv4CIDR.op == opInclude {
			return fmt.Errorf("The CIDR %s includes multicast subnets, should not be included in UDP target networks", lastMultiContainedv4CIDR.str)
		}
	}
	multicastv6Entries, _ := ranger.CoveredNetworks(*v6network)
	expectedmulticastv6AddrRange := addressv6RangeCount(*v6network)
	actualmulticastv6AddrRange := big.NewInt(0)
	multicastv6SubnetInc := false
	wrongEntryv6CIDR := make([]string, 0, len(multicastv6Entries))
	for _, entry := range multicastv6Entries {
		cidr := entry.(*cidr)
		if cidr.op == opInclude {
			multicastv6SubnetInc = true
			wrongEntryv6CIDR = append(wrongEntryv6CIDR, cidr.str)
		}
		actualmulticastv6AddrRange.Add(addressv6RangeCount(entry.Network()), actualmulticastv6AddrRange)
		ranger.Remove(cidr.ipNet) 
	}
	if len(multicastv6Entries) > 0 && multicastv6SubnetInc {
		return fmt.Errorf("The CIDR %s are multicast subnets, should not be included in UDP target networks", wrongEntryv6CIDR)
	}
	MulticastContainedv6Subnets, _ := ranger.ContainingNetworks(v6network.IP)
	if len(MulticastContainedv6Subnets) > 0 {
		lastMultiContainedv6CIDR = MulticastContainedv6Subnets[len(MulticastContainedv6Subnets)-1].(*cidr)
	}
	if lastMultiContainedv6CIDR != nil && !multicastv6SubnetPresent && actualmulticastv6AddrRange != expectedmulticastv6AddrRange {
		if lastMultiContainedv6CIDR.op == opInclude {
			return fmt.Errorf("The CIDR %s includes multicast subnets, should not be included in UDP target networks", lastMultiContainedv6CIDR.str)
		}
	}
	return nil
}
func ValidateUDPCIDRs ( ss [ ] string ) error { cidrmap := make ( map [ string ] * cidr ) ranger := cidranger . NewPCTrieRanger ( ) multicastv4SubnetPresent := false multicastv6SubnetPresent := false for _ , s := range ss { cidr , err := parseCIDR ( s ) if err != nil { return err } if _ , ok := cidrmap [ cidr . ipNet . String ( ) ] ; ! ok { cidrmap [ cidr . ipNet . String ( ) ] = cidr } else { return fmt . Errorf ( " " , cidr . str ) } if cidr . ipNet . String ( ) == multicastv4Subnet { multicastv4SubnetPresent = true } if cidr . ipNet . String ( ) == multicastv6Subnet { multicastv6SubnetPresent = true } if err := ranger . Insert ( newCustomRangerEntry ( cidr ) ) ; err != nil { return fmt . Errorf ( " " , cidr . str ) } } for _ , c := range cidrmap { if c . op == opExclude { entries , err := ranger . ContainingNetworks ( c . ipNet . IP ) if err != nil { return fmt . Errorf ( " " , err ) } mask := c . ipNet . Mask present := checkExcPfxContainedInc ( entries , mask , c . ipNet ) if ! present { return fmt . Errorf ( " " , c . str ) } } } _ , v4network , err := net . ParseCIDR ( multicastv4Subnet ) if err != nil { return fmt . Errorf ( " " , v4network ) } _ , v6network , err := net . ParseCIDR ( multicastv6Subnet ) if err != nil { return fmt . Errorf ( " " , v6network ) } multicastv4Entries , _ := ranger . CoveredNetworks ( * v4network ) expectedmulticastv4AddrRange := addressv4RangeCount ( * v4network ) actualmulticastv4AddrRange := uint64 ( 0 ) multicastv4SubnetInc := false wrongEntryv4CIDR := make ( [ ] string , 0 , len ( multicastv4Entries ) ) for _ , entry := range multicastv4Entries { cidr := entry . ( * cidr ) if cidr . op == opInclude { multicastv4SubnetInc = true wrongEntryv4CIDR = append ( wrongEntryv4CIDR , cidr . str ) } actualmulticastv4AddrRange += addressv4RangeCount ( entry . Network ( ) ) ranger . Remove ( cidr . ipNet ) } if len ( multicastv4Entries ) > 0 && multicastv4SubnetInc { return fmt . Errorf ( " " , wrongEntryv4CIDR ) } var lastMultiContainedv4CIDR , lastMultiContainedv6CIDR * cidr MulticastContainedv4Subnets , _ := ranger . ContainingNetworks ( v4network . IP ) if len ( MulticastContainedv4Subnets ) > 0 { lastMultiContainedv4CIDR = MulticastContainedv4Subnets [ len ( MulticastContainedv4Subnets ) - 1 ] . ( * cidr ) } if lastMultiContainedv4CIDR != nil && ! multicastv4SubnetPresent && actualmulticastv4AddrRange != expectedmulticastv4AddrRange { if lastMultiContainedv4CIDR . op == opInclude { return fmt . Errorf ( " " , lastMultiContainedv4CIDR . str ) } } multicastv6Entries , _ := ranger . CoveredNetworks ( * v6network ) expectedmulticastv6AddrRange := addressv6RangeCount ( * v6network ) actualmulticastv6AddrRange := big . NewInt ( 0 ) multicastv6SubnetInc := false wrongEntryv6CIDR := make ( [ ] string , 0 , len ( multicastv6Entries ) ) for _ , entry := range multicastv6Entries { cidr := entry . ( * cidr ) if cidr . op == opInclude { multicastv6SubnetInc = true wrongEntryv6CIDR = append ( wrongEntryv6CIDR , cidr . str ) } actualmulticastv6AddrRange . Add ( addressv6RangeCount ( entry . Network ( ) ) , actualmulticastv6AddrRange ) ranger . Remove ( cidr . ipNet ) } if len ( multicastv6Entries ) > 0 && multicastv6SubnetInc { return fmt . Errorf ( " " , wrongEntryv6CIDR ) } MulticastContainedv6Subnets , _ := ranger . ContainingNetworks ( v6network . IP ) if len ( MulticastContainedv6Subnets ) > 0 { lastMultiContainedv6CIDR = MulticastContainedv6Subnets [ len ( MulticastContainedv6Subnets ) - 1 ] . ( * cidr ) } if lastMultiContainedv6CIDR != nil && ! multicastv6SubnetPresent && actualmulticastv6AddrRange != expectedmulticastv6AddrRange { if lastMultiContainedv6CIDR . op == opInclude { return fmt . Errorf ( " " , lastMultiContainedv6CIDR . str ) } } return nil }
ValidateUDPCIDRs validates that the list of string provided as a set is a valid CIDR set
ValidateUDPCIDRs validates that the list of string provided as a set is a valid CIDR set
// instantiate NewPCTrieRanger // Parse and validate all not CIDRs are included in regular CIDRs // get all the networks which are containing the network, // basically getting all the parent prefixes. // make sure the NOT(!) CIDR is part/contained of/by a included CIDR // if the excluded CIDR is not contained in the included CIDR then return error // now if all the CIDR make sense, check for multicast subnet, //  check if we have a 224/4 subnet in the pfx tree and if its not excluded, // if yes then return error // get all the networks that have 224/4 and if anyone is included in the 224/4 subnet // then we report error. // ipv4 checks // we count the actual host addresses when the pfx tree has 224/4 and all of them are excluded. //nolint // now check here if multicast subnet is contained in the pfx-tree // now we come only in 3 cases: // 1. When the pfx tree has 224/4, and itself and all the network under them are excluded, // 		so, we cal the actual multicast addresses count to be sure that really everything is excluded. // 2. When there is no 224/4 network in tree, ex: 0/0, 128/0 so get the last contained subnet and if // 		the last subnet is excluded then we are good else return error. // 3. When the pfx tree doesn't have the exact 224/4 but has 224/5, 232/5, we need to check for the actual multicastrange //nolint