// ReserveIPv6HostID sets the hostId in the AllocMap
344329
func ReserveIPv6HostID(hostID string, IPv6AllocMap *map[string]bool) {
	if hostID == "" {
		return
	}
	if *IPv6AllocMap == nil {
		*IPv6AllocMap = make(map[string]bool)
	}
	(*IPv6AllocMap)[hostID] = true
}
1246186
func (i *IDMappings) ToHost(pair IDPair) (IDPair, error) {
	var err error
	target := i.RootPair()

	if pair.UID != target.UID {
		target.UID, err = toHost(pair.UID, i.uids)
		if err != nil {
			return target, err
		}
	}

	if pair.GID != target.GID {
		target.GID, err = toHost(pair.GID, i.gids)
	}
	return target, err
}
503755
func includesIPv6(ips []net.IP) bool {
	for _, ip := range ips {
		if ip.To4() == nil && ip.To16() != nil {
			return true
		}
	}

	return false
}
676703
func hostIsIP(u *url.URL) bool {
	return net.ParseIP(u.Hostname()) != nil
}
232862
func ipAddrInit() {
	// Sorted for human readability
	ipAddrAttrs = []AttrName{
		"host",
		"address",
		"port",
		"netmask",
		"network",
		"mask_bits",
		"binary",
		"hex",
		"first_usable",
		"last_usable",
		"octets",
	}

	ipAddrAttrMap = map[AttrName]func(ip IPAddr) string{
		"address": func(ip IPAddr) string {
			return ip.NetIP().String()
		},
		"binary": func(ip IPAddr) string {
			return ip.AddressBinString()
		},
		"first_usable": func(ip IPAddr) string {
			return ip.FirstUsable().String()
		},
		"hex": func(ip IPAddr) string {
			return ip.AddressHexString()
		},
		"host": func(ip IPAddr) string {
			return ip.Host().String()
		},
		"last_usable": func(ip IPAddr) string {
			return ip.LastUsable().String()
		},
		"mask_bits": func(ip IPAddr) string {
			return fmt.Sprintf("%d", ip.Maskbits())
		},
		"netmask": func(ip IPAddr) string {
			switch v := ip.(type) {
			case IPv4Addr:
				ipv4Mask := IPv4Addr{
					Address: IPv4Address(v.Mask),
					Mask:    IPv4HostMask,
				}
				return ipv4Mask.String()
			case IPv6Addr:
				ipv6Mask := new(big.Int)
				ipv6Mask.Set(v.Mask)
				ipv6MaskAddr := IPv6Addr{
					Address: IPv6Address(ipv6Mask),
					Mask:    ipv6HostMask,
				}
				return ipv6MaskAddr.String()
			default:
				return fmt.Sprintf("<unsupported type: %T>", ip)
			}
		},
		"network": func(ip IPAddr) string {
			return ip.Network().NetIP().String()
		},
		"octets": func(ip IPAddr) string {
			octets := ip.Octets()
			octetStrs := make([]string, 0, len(octets))
			for _, octet := range octets {
				octetStrs = append(octetStrs, fmt.Sprintf("%d", octet))
			}
			return strings.Join(octetStrs, " ")
		},
		"port": func(ip IPAddr) string {
			return fmt.Sprintf("%d", ip.IPPort())
		},
	}
}
760466
func GetNextIPv6HostID(hostID, subnetAddr string, subnetLen uint, IPv6AllocMap map[string]bool) (string, error) {
	if hostID == "" {
		hostID = "::"
	}
	if subnetLen == 0 {
		return "", core.Errorf("subnet length %d is invalid", subnetLen)
	}

	hostidIP := net.ParseIP(hostID)

	// start with the carryOver 1 to get the next hostID
	var carryOver = 1
	var allocd = true

	for allocd == true {
		// Add 1 to hostID
		for i := len(hostidIP) - 1; i >= 0; i-- {
			var temp int
			temp = int(hostidIP[i]) + carryOver
			if temp > int(0xFF) {
				hostidIP[i] = 0
				carryOver = 1
			} else {
				hostidIP[i] = uint8(temp)
				carryOver = 0
				break
			}
		}
		// Check if this hostID is already allocated
		if _, allocd = IPv6AllocMap[hostidIP.String()]; allocd == true {
			// Already allocated find the next hostID
			carryOver = 1
		} else {
			// allocd == false. check if we reached MaxHosts
			offset := (subnetLen - 1) / 8
			masklen := subnetLen % 8
			mask := ((1 << masklen) - 1) << (8 - masklen)
			if (hostidIP[offset] & byte(mask)) != 0 {
				// if hostID is outside subnet range,
				//	check if we have reached MaxHosts
				maxHosts := math.Pow(2, float64(128-subnetLen)) - 1
				if float64(len(IPv6AllocMap)) < maxHosts {
					hostID = "::"
					hostidIP = net.ParseIP(hostID)
					carryOver = 1
					allocd = true // continue the loop
				} else {
					return "", core.Errorf("Reached MaxHosts (%v). Cannot allocate more hosts", maxHosts)
				}
			}
		}
	}
	return hostidIP.String(), nil
}
642137
func getHostID() (hostid string, err error) {
	var mib []C.int = []C.int{ C.CTL_KERN, C.KERN_HOSTID }
	var value C.int64_t = 0
	var length C.size_t = 8
	var unused unsafe.Pointer

	if rc, errno := C.sysctl(&mib[0], 2, unsafe.Pointer(&value), &length, unused, 0); rc == -1 {
		return "", fmt.Errorf("unable to get hostID: errno=%d", errno)
	}

	if uint64(value) > 0 {
		return fmt.Sprintf("%x", uint64(value)), nil
	}

	hostIP, err := getHostIPv4Addr()
	if err != nil {
		return "", err
	}

	var numericIP uint32
	numericIP |= uint32(hostIP[0])
	numericIP |= uint32(hostIP[1]) << 8
	numericIP |= uint32(hostIP[2]) << 16
	numericIP |= uint32(hostIP[3]) << 24

	hostID := uint32(numericIP<<16 | numericIP>>16)
	return fmt.Sprintf("%x", hostID), nil
}
631390
func ExtractHostID(id string) (string, error) {
	ids := strings.SplitN(id, "-", 2)
	if len(ids) != 2 || ids[0] == "" || ids[1] == "" {
		return "", errors.New("invalid ID")
	}
	return ids[0], nil
}
300247
func initHostPidId() {
	var sum [4]byte
	hostB := sum[:]
	host, err := os.Hostname()
	if err != nil {
		// if we cannot get hostname, just use a random set of bytes
		_, err2 := io.ReadFull(rand.Reader, hostB)
		if err2 != nil {
			panic(fmt.Errorf("cannot get hostname: %v; %v", err, err2))
		}
	} else {
		hw := md5.New()
		hw.Write([]byte(host))
		copy(hostB, hw.Sum(nil))
	}
	pid := os.Getpid()
	hostI := binary.BigEndian.Uint32(hostB)
	uid := uint32(pid) + uint32(hostI)
	binary.BigEndian.PutUint32(hostB, uid)
	b := make([]byte, 4)
	binary.BigEndian.PutUint32(b, uid)
	hostPidId = b[:]
}
172771
func (h *Hostsfile) LookupStaticHostV6(host string) []net.IP {
	return h.lookupStaticHost(h.hmap.byNameV6, host)
}
912761
func GetPublicIPv6() ([]*net.IPAddr, error) {
	addresses, err := net.InterfaceAddrs()
	if err != nil {
		return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
	}

	var addrs []*net.IPAddr
	for _, rawAddr := range addresses {
		var ip net.IP
		switch addr := rawAddr.(type) {
		case *net.IPAddr:
			ip = addr.IP
		case *net.IPNet:
			ip = addr.IP
		default:
			continue
		}
		if ip.To4() != nil {
			continue
		}
		if isPrivate(ip) {
			continue
		}
		addrs = append(addrs, &net.IPAddr{IP: ip})
	}
	return addrs, nil
}
244876
func (d *NvidiaDevice) Reserve(deviceIDs []string) (*device.ContainerReservation, error) {
	if len(deviceIDs) == 0 {
		return &device.ContainerReservation{}, nil
	}
	// Due to the asynchronous nature of NvidiaPlugin, there is a possibility
	// of race condition
	//
	// Timeline:
	// 	1 - fingerprint reports that GPU with id "1" is present
	//  2 - the following events happen at the same time:
	// 		a) server decides to allocate GPU with id "1"
	//      b) fingerprint check reports that GPU with id "1" is no more present
	//
	// The latest and always valid version of fingerprinted ids are stored in
	// d.devices map. To avoid this race condition an error is returned if
	// any of provided deviceIDs is not found in d.devices map
	d.deviceLock.RLock()
	var notExistingIDs []string
	for _, id := range deviceIDs {
		if _, deviceIDExists := d.devices[id]; !deviceIDExists {
			notExistingIDs = append(notExistingIDs, id)
		}
	}
	d.deviceLock.RUnlock()
	if len(notExistingIDs) != 0 {
		return nil, &reservationError{notExistingIDs}
	}

	return &device.ContainerReservation{
		Envs: map[string]string{
			NvidiaVisibleDevices: strings.Join(deviceIDs, ","),
		},
	}, nil
}
534453
func (s *HostService) NewCancelHostMaintenanceParams(id string) *CancelHostMaintenanceParams {
	p := &CancelHostMaintenanceParams{}
	p.p = make(map[string]interface{})
	p.p["id"] = id
	return p
}
1243286
func ParseIPID(ipid string) (hostid string, ipaddress string, err error) {
	parts := strings.SplitN(ipid, "-", 2)
	if len(parts) != 2 {
		// TODO: error
		return "", "", ErrInvalidIPID
	}
	return parts[0], parts[1], nil
}
78933
func (s *AwsSecurityFindingFilters) SetNetworkSourceIpV6(v []*IpFilter) *AwsSecurityFindingFilters {
	s.NetworkSourceIpV6 = v
	return s
}
864336
func (n *switchingNetworking) AllocatePublicIP(instId instance.Id) (*string, error) {
	if err := n.initNetworking(); err != nil {
		return nil, errors.Trace(err)
	}
	return n.networking.AllocatePublicIP(instId)
}
474256
func (na *cnmNetworkAllocator) allocateVIP(vip *api.Endpoint_VirtualIP) error {
	var opts map[string]string
	localNet := na.getNetwork(vip.NetworkID)
	if localNet == nil {
		return errors.New("networkallocator: could not find local network state")
	}

	if localNet.isNodeLocal {
		return nil
	}

	// If this IP is already allocated in memory we don't need to
	// do anything.
	if _, ok := localNet.endpoints[vip.Addr]; ok {
		return nil
	}

	ipam, _, _, err := na.resolveIPAM(localNet.nw)
	if err != nil {
		return errors.Wrap(err, "failed to resolve IPAM while allocating")
	}

	var addr net.IP
	if vip.Addr != "" {
		var err error

		addr, _, err = net.ParseCIDR(vip.Addr)
		if err != nil {
			return err
		}
	}
	if localNet.nw.IPAM != nil && localNet.nw.IPAM.Driver != nil {
		// set ipam allocation method to serial
		opts = setIPAMSerialAlloc(localNet.nw.IPAM.Driver.Options)
	}

	for _, poolID := range localNet.pools {
		ip, _, err := ipam.RequestAddress(poolID, addr, opts)
		if err != nil && err != ipamapi.ErrNoAvailableIPs && err != ipamapi.ErrIPOutOfRange {
			return errors.Wrap(err, "could not allocate VIP from IPAM")
		}

		// If we got an address then we are done.
		if err == nil {
			ipStr := ip.String()
			localNet.endpoints[ipStr] = poolID
			vip.Addr = ipStr
			return nil
		}
	}

	return errors.New("could not find an available IP while allocating VIP")
}