// IPv6 checks the validity and properties of the given IPv6 packet. The usage
// is similar to IPv4.
322886
func IPv6(t *testing.T, b []byte, checkers ...NetworkChecker) {
	t.Helper()

	ipv6 := header.IPv6(b)
	if !ipv6.IsValid(len(b)) {
		t.Error("Not a valid IPv6 packet")
	}

	for _, f := range checkers {
		f(t, []header.Network{ipv6})
	}
	if t.Failed() {
		t.FailNow()
	}
}
555388
func IPLess(a, b net.IP) bool {
	b = b.To4()
	for i, ai := range a.To4() {
		if ai != b[i] {
			return ai < b[i]
		}
	}
	return false
}
81850
func IsIPv4(str string) bool {
	ip := net.ParseIP(str)
	return ip != nil && strings.Contains(str, ".")
}
835696
func IsOverlappingSubnetv6(inputIPv6Subnet string, existingIPv6Subnet string) bool {
	inputIPv6StartRange, inputIPv6EndRange := getIPv6Range(inputIPv6Subnet)
	existingIPv6StartRange, existingIPv6EndRange := getIPv6Range(existingIPv6Subnet)

	inputStartRange := ipv6ToBigint(inputIPv6StartRange)
	inputEndRange := ipv6ToBigint(inputIPv6EndRange)

	existingStartRange := ipv6ToBigint(existingIPv6StartRange)
	existingEndRange := ipv6ToBigint(existingIPv6EndRange)

	if (existingStartRange.Cmp(inputStartRange) <= 0 && inputStartRange.Cmp(existingEndRange) <= 0) ||
		(existingStartRange.Cmp(inputEndRange) <= 0 && inputEndRange.Cmp(existingEndRange) <= 0) {
		return true
	}

	if (inputStartRange.Cmp(existingStartRange) <= 0 && existingStartRange.Cmp(inputEndRange) <= 0) ||
		(inputStartRange.Cmp(existingEndRange) <= 0 && existingEndRange.Cmp(inputEndRange) <= 0) {
		return true
	}
	return false
}
1280984
func IPv6Fragment(checkers ...NetworkChecker) NetworkChecker {
	return func(t *testing.T, h []header.Network) {
		t.Helper()

		if p := h[0].TransportProtocol(); p != header.IPv6FragmentHeader {
			t.Errorf("Bad protocol, got %v, want %v", p, header.UDPProtocolNumber)
		}

		ipv6Frag := header.IPv6Fragment(h[0].Payload())
		if !ipv6Frag.IsValid() {
			t.Error("Not a valid IPv6 fragment")
		}

		for _, f := range checkers {
			f(t, []header.Network{h[0], ipv6Frag})
		}
		if t.Failed() {
			t.FailNow()
		}
	}
}
889999
func IPv6Routes(addr *models.NodeAddressing, linkMTU int) ([]route.Route, error) {
	ip := net.ParseIP(addr.IPV6.IP)
	if ip == nil {
		return []route.Route{}, fmt.Errorf("Invalid IP address: %s", addr.IPV6.IP)
	}
	return []route.Route{
		{
			Prefix: net.IPNet{
				IP:   ip,
				Mask: defaults.ContainerIPv6Mask,
			},
		},
		{
			Prefix:  defaults.IPv6DefaultRoute,
			Nexthop: &ip,
			MTU:     linkMTU,
		},
	}, nil
}
758718
func EnableIPv6Forwarding() error {
	ip6ConfPath := "/proc/sys/net/ipv6/conf/"
	device := "all"
	forwarding := "forwarding"
	forwardingOn := "1"
	path := filepath.Join(ip6ConfPath, device, forwarding)
	return ioutil.WriteFile(path, []byte(forwardingOn), 0644)
}
158433
func IPv6() string {
	size := 16
	ip := make([]byte, size)
	for i := 0; i < size; i++ {
		ip[i] = byte(r.Intn(256))
	}
	return net.IP(ip).To16().String()
}
221844
func (b IPv6) IsValid(pktSize int) bool {
	if len(b) < IPv6MinimumSize {
		return false
	}

	dlen := int(b.PayloadLength())
	if dlen > pktSize-IPv6MinimumSize {
		return false
	}

	return true
}
1313203
func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP {
	p := new(net.IP)
	f.IPVarP(p, name, "", value, usage)
	return p
}
1105296
func (b IPv6) TOS() (uint8, uint32) {
	v := binary.BigEndian.Uint32(b[versTCFL:])
	return uint8(v >> 20), v & 0xfffff
}
254161
func IsHostIPv6(ip net.IP) bool {
	return ip.Equal(GetIPv6()) || ip.Equal(GetIPv6Router())
}
1041540
func lastIP(n *net.IPNet) net.IP {
	mask := n.Mask
	ip := n.IP
	lastip := make(net.IP, len(ip))
	// set bits zero in the mask to ones in ip
	for i, m := range mask {
		lastip[i] = (^m) | ip[i]
	}
	return lastip
}
734817
func (s *Server) verifyNICsWithAPI(nics map[string]device) error {
	checked := make([]string, 0, len(nics))
	var ipV6ErrMsg error
	for name, nic := range nics {
		checked = append(checked, name)

		if !isValidNICType(nic) {
			continue
		}

		netName := nic["parent"]
		if netName == "" {
			continue
		}

		net, _, err := s.GetNetwork(netName)
		if err != nil {
			return errors.Annotatef(err, "retrieving network %q", netName)
		}
		if err := verifyNoIPv6(net); err != nil {
			ipV6ErrMsg = err
			continue
		}

		logger.Tracef("found usable network device %q with parent %q", name, netName)
		s.localBridgeName = netName
		return nil
	}

	// A nic with valid type found, but the network configures IPv6.
	if ipV6ErrMsg != nil {
		return ipV6ErrMsg
	}

	// No nics with a nictype of nicTypeBridged, nicTypeMACVLAN was found.
	return errors.Errorf(fmt.Sprintf(
		"no network device found with nictype %q or %q"+
			"\n\tthe following devices were checked: %s"+
			"\nNote: juju does not support IPv6."+
			"\nReconfigure lxd to use a network of type %q or %q, disabling IPv6.",
		nicTypeBridged, nicTypeMACVLAN, strings.Join(checked, ", "), nicTypeBridged, nicTypeMACVLAN))
}
893326
func LooksLikeIPv6(ip string) bool {
	if !strings.Contains(ip, ":") {
		return false
	}
	return ipv6Pattern.MatchString(ip)
}
1348833
func (h *Host) IPv4Net() *net.IPNet {
	return &net.IPNet{IP: h.IPv4Addr, Mask: h.IPv4Mask}
}
1040751
func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
	for _, s := range n.config.Ipv6Subnets {
		_, snet, err := net.ParseCIDR(s.SubnetIP)
		if err != nil {
			return nil
		}
		// first check if the mask lengths are the same
		i, _ := snet.Mask.Size()
		j, _ := ip.Mask.Size()
		if i != j {
			continue
		}
		if snet.Contains(ip.IP) {
			return s
		}
	}

	return nil
}