当前位置: 首页>>代码示例>>Golang>>正文


Golang IPMask.Size方法代码示例

本文整理汇总了Golang中net.IPMask.Size方法的典型用法代码示例。如果您正苦于以下问题:Golang IPMask.Size方法的具体用法?Golang IPMask.Size怎么用?Golang IPMask.Size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.IPMask的用法示例。


在下文中一共展示了IPMask.Size方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ifaceAddIPv4Addr

// ifaceAddIPv4Addr adds the given IPv4 address to the network interface.
func ifaceAddIPv4Addr(iface *net.Interface, ip net.IP, mask net.IPMask) error {
	if ip.To4() == nil {
		return fmt.Errorf("IP %v is not a valid IPv4 address", ip)
	}
	brd := make(net.IP, net.IPv4len)
	copy(brd, ip.To4())
	for i := 0; i < net.IPv4len; i++ {
		brd[i] |= ^mask[i]
	}
	prefixLen, _ := mask.Size()
	return ipRunIface(iface, "addr add %s/%d brd %s dev %s", ip, prefixLen, brd, iface.Name)
}
开发者ID:Cepave,项目名称:lvs-metrics,代码行数:13,代码来源:ip.go

示例2: addNetwork

func (r *MySQL) addNetwork(tx *sql.Tx, addr net.IP, mask net.IPMask) (netID uint64, err error) {
	qry := "INSERT INTO network (address, mask) VALUES (INET_ATON(?), ?)"
	ones, _ := mask.Size()
	result, err := tx.Exec(qry, addr.String(), ones)
	if err != nil {
		return 0, err
	}
	id, err := result.LastInsertId()
	if err != nil {
		return 0, err
	}

	return uint64(id), nil
}
开发者ID:yebinMoon,项目名称:cherry,代码行数:14,代码来源:mysql.go

示例3: addIPAddrs

func (r *MySQL) addIPAddrs(tx *sql.Tx, netID uint64, addr net.IP, mask net.IPMask) error {
	stmt, err := tx.Prepare("INSERT INTO ip (network_id, address) VALUES (?, INET_ATON(?) + ?)")
	if err != nil {
		return err
	}
	defer stmt.Close()

	ones, bits := mask.Size()
	n_addrs := int(math.Pow(2, float64(bits-ones))) - 2 // Minus two due to network and broadcast addresses
	for i := 0; i < n_addrs; i++ {
		if _, err := stmt.Exec(netID, addr.String(), i+1); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:yebinMoon,项目名称:cherry,代码行数:17,代码来源:mysql.go

示例4: NewContext

func NewContext(bridgePool net.IPNet, bridgeMask net.IPMask) (*Context, error) {
	pones, pbits := bridgePool.Mask.Size()
	mones, mbits := bridgeMask.Size()
	if pbits != mbits || mones < pones {
		return nil, fmt.Errorf("bridge mask is not compatiable with bridge pool mask")
	}

	ctx := &Context{
		defaultBridgeMask: bridgeMask,
		defaultBridgePool: NewAddressSpaceFromNetwork(&bridgePool),
		scopes:            make(map[string]*Scope),
		containers:        make(map[uid.UID]*Container),
	}

	s, err := ctx.NewScope(DefaultScopeName, BridgeScopeType, nil, net.IPv4(0, 0, 0, 0), nil, nil)
	if err != nil {
		return nil, err
	}
	s.builtin = true
	s.dns = []net.IP{s.gateway}
	ctx.defaultScope = s

	// add any external networks
	for nn, n := range Config.ContainerNetworks {
		if nn == "bridge" {
			continue
		}

		pools := make([]string, len(n.Pools))
		for i, p := range n.Pools {
			pools[i] = p.String()
		}

		s, err := ctx.NewScope(ExternalScopeType, nn, &net.IPNet{IP: n.Gateway.IP.Mask(n.Gateway.Mask), Mask: n.Gateway.Mask}, n.Gateway.IP, n.Nameservers, pools)
		if err != nil {
			return nil, err
		}

		s.builtin = true
	}

	return ctx, nil
}
开发者ID:kjplatz,项目名称:vic,代码行数:43,代码来源:context.go

示例5: ReserveNextIP4Net

// ReserveNextIP4Net reserves a new sub address space within the given address
// space, given a bitmask specifying the "width" of the requested space.
func (s *AddressSpace) ReserveNextIP4Net(mask net.IPMask) (*AddressSpace, error) {
	ones, _ := mask.Size()
	for i, r := range s.availableRanges {
		network := r.FirstIP.Mask(mask).To16()
		var firstIP net.IP
		// check if the start of the current range
		// is lower than the network boundary
		if compareIP4(network, r.FirstIP) >= 0 {
			// found the start of the range
			firstIP = network
		} else {
			// network address is lower than the first
			// ip in the range; try the next network
			// in the mask
			for i := len(network) - 1; i >= 12; i-- {
				partialByteIndex := ones/8 + 12
				var inc byte
				if i == partialByteIndex {
					// this octet may only be occupied
					// by the mask partially, e.g.
					// for a /25, the last octet has
					// only one bit in the mask
					//
					// in order to get the next network
					// we need to increment starting at
					// the last bit of the mask, e.g. 25
					// in this example, which would be
					// bit 8 in the last octet.
					inc = (byte)(1 << (uint)(8-ones%8))
				} else if i < partialByteIndex {
					// we are past the partial octets,
					// so this is portion where the mask
					// occupies the octets fully, so
					// we can just increment the last bit
					inc = 1
				}

				if inc == 0 {
					continue
				}

				network[i] += inc
				if network[i] > 0 {
					firstIP = network
					break
				}

			}
		}

		if firstIP != nil {
			// we found the first IP for the requested range,
			// now check if the available range can accommodate
			// the highest address given the first IP and the mask
			lastIP := highestIP4(&net.IPNet{IP: firstIP, Mask: mask})
			if compareIP4(lastIP, r.LastIP) <= 0 {
				s.reserveSubRange(firstIP, lastIP, i)
				subSpace := NewAddressSpaceFromRange(firstIP, lastIP)
				subSpace.Network = &net.IPNet{IP: firstIP, Mask: mask}
				subSpace.Parent = s
				return subSpace, nil
			}
		}
	}

	return nil, fmt.Errorf("could not find IP range for mask %s", mask)
}
开发者ID:jak-atx,项目名称:vic,代码行数:69,代码来源:ipam.go

示例6: ifaceDelIPAddr

// ifaceDelIPAddr deletes the given IP address from the network interface.
func ifaceDelIPAddr(iface *net.Interface, ip net.IP, mask net.IPMask) error {
	prefixLen, _ := mask.Size()
	return ipRunIface(iface, "addr del %s/%d dev %s", ip, prefixLen, iface.Name)
}
开发者ID:Cepave,项目名称:lvs-metrics,代码行数:5,代码来源:ip.go


注:本文中的net.IPMask.Size方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。